Skip to content

Conversation

@roomote
Copy link
Contributor

@roomote roomote bot commented Aug 11, 2025

Summary

This PR fixes a UI crash that occurs when the ContextCondenseRow component receives null or undefined values for token counts. The error "Cannot read properties of null (reading 'toLocaleString')" was causing the entire UI to crash, as reported in issue #6914.

Problem

The ContextCondenseRow component was attempting to call .toLocaleString() on prevContextTokens and newContextTokens without checking if these values were null or undefined first. This caused a runtime error that crashed the UI.

Solution

  • Added null/undefined checks for prevContextTokens, newContextTokens, and cost values
  • Default to 0 when any of these values are null or undefined
  • The component now handles edge cases gracefully without crashing

Testing

  • Added comprehensive test suite for ContextCondenseRow component
  • Tests cover:
    • Normal operation with valid data
    • Handling of null values for all numeric props
    • Handling of undefined values for all numeric props
    • Edge cases like negative numbers and very large numbers
  • All tests pass successfully

Related Issues

Fixes #6914

Checklist

  • Code changes are tested
  • Tests are added for the fix
  • All tests pass
  • Linting passes
  • Type checking passes

Important

Fixes UI crash in ContextCondenseRow by handling null/undefined token values and adds comprehensive tests for edge cases.

  • Behavior:
    • Fixes UI crash in ContextCondenseRow by handling null/undefined prevContextTokens, newContextTokens, and cost.
    • Defaults to 0 for null/undefined values to prevent .toLocaleString() errors.
  • Testing:
    • Adds tests in ContextCondenseRow.spec.tsx for null/undefined handling, valid data, and edge cases like negative and large numbers.
    • Tests ensure component renders correctly and handles all edge cases without crashing.

This description was created by Ellipsis for 22a873d. You can customize this summary. It will automatically update as commits are pushed.

…ent UI crash

- Added null/undefined checks for prevContextTokens, newContextTokens, and cost
- Default to 0 when values are null or undefined
- Added comprehensive test coverage for edge cases
- Fixes #6914
@roomote roomote bot requested review from cte, jr and mrubens as code owners August 11, 2025 02:35
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. bug Something isn't working labels Aug 11, 2025
Copy link
Contributor Author

@roomote roomote bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewing my own code is like debugging in a mirror - everything looks backwards but the bugs are still mine.

const [isExpanded, setIsExpanded] = useState(false)

// Handle null/undefined token values to prevent crashes
const prevTokens = prevContextTokens ?? 0
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I notice we're treating these values as optional here, but the Zod schema in packages/types/src/message.ts defines them as required fields. This creates a type safety issue.

Is this intentional? The schema has:

export const contextCondenseSchema = z.object({
  cost: z.number(),
  prevContextTokens: z.number(),
  newContextTokens: z.number(),
  summary: z.string(),
})

If these values can indeed be null/undefined in practice, we should update the schema to use .number().optional() to maintain type consistency across the codebase.

// Handle null/undefined token values to prevent crashes
const prevTokens = prevContextTokens ?? 0
const newTokens = newContextTokens ?? 0
const displayCost = cost ?? 0
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While defaulting to 0 prevents the crash, could this be misleading to users? When data is unavailable, showing "0 tokens" might imply no tokens were used, when actually the data is missing.

Would it be clearer to display "--" or "N/A" when values are null/undefined?

Suggested change
const displayCost = cost ?? 0
const prevTokens = prevContextTokens ?? 0
const newTokens = newContextTokens ?? 0
const displayCost = cost ?? 0
const hasValidData = prevContextTokens != null && newContextTokens != null

// Should display $0.00 for undefined cost
expect(screen.getByText("$0.00")).toBeInTheDocument()
})
})
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great test coverage for null/undefined cases! One edge case to consider: what happens with NaN values? They would also fail with .toLocaleString(). Might be worth adding a test case:

Suggested change
})
it("should handle NaN values without crashing", () => {
const props = {
cost: NaN,
prevContextTokens: NaN,
newContextTokens: NaN,
summary: "NaN test",
}
const { container } = render(<ContextCondenseRow {...props} />)
expect(container).toBeInTheDocument()
// Should handle NaN gracefully
})

@roomote roomote bot mentioned this pull request Aug 11, 2025
@hannesrudolph hannesrudolph added the Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. label Aug 11, 2025
@daniel-lxs daniel-lxs moved this from Triage to PR [Needs Prelim Review] in Roo Code Roadmap Aug 13, 2025
@hannesrudolph hannesrudolph added PR - Needs Preliminary Review and removed Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. labels Aug 13, 2025
@dosubot dosubot bot added size:S This PR changes 10-29 lines, ignoring generated files. and removed size:L This PR changes 100-499 lines, ignoring generated files. labels Aug 14, 2025
Copy link
Member

@daniel-lxs daniel-lxs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Aug 14, 2025
@daniel-lxs daniel-lxs moved this from PR [Needs Prelim Review] to PR [Needs Review] in Roo Code Roadmap Aug 14, 2025
@mrubens mrubens merged commit b2fdb9a into main Aug 21, 2025
19 checks passed
@mrubens mrubens deleted the fix/issue-6914-context-condense-null-crash branch August 21, 2025 09:21
@github-project-automation github-project-automation bot moved this from New to Done in Roo Code Roadmap Aug 21, 2025
@github-project-automation github-project-automation bot moved this from PR [Needs Review] to Done in Roo Code Roadmap Aug 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working lgtm This PR has been approved by a maintainer PR - Needs Review size:S This PR changes 10-29 lines, ignoring generated files.

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

UI crashed (v3.25.10)

5 participants