This repository was archived by the owner on May 15, 2026. It is now read-only.
fix: validate deserialized API messages at boundary to prevent crashes - #10956
Draft
ghost wants to merge 1 commit into
Draft
fix: validate deserialized API messages at boundary to prevent crashes#10956ghost wants to merge 1 commit into
ghost wants to merge 1 commit into
Conversation
…errors Root cause: JSON.parse() in readApiMessages() deserializes data with zero validation, allowing corrupted or legacy data to violate type contracts (e.g., undefined content). This causes downstream crashes in functions like filterNonAnthropicBlocks() that assume message.content is always string | ContentBlock[] per the type system, but receive undefined/null values from corrupted historical data. Solution: - Add validateApiMessage() to sanitize deserialized messages at the boundary - Fix undefined/null/non-array content by converting to empty array - Filter out malformed messages with invalid roles or non-object structure - Filter out invalid content blocks missing the required type field - Add comprehensive test coverage (12 tests covering all edge cases) Fixes: https://www.reddit.com/r/RooCode/s/yUXVFMYu3P Related to closed PR #10954 (which treated symptom, not cause)
Author
Review completed. No issues found. This PR correctly fixes the root cause of the "Cannot read properties of undefined (reading 'filter')" crash by adding validation at the deserialization boundary in
Mention @roomote in a comment to request specific changes to this pull request or fix all unresolved issues. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes the "Cannot read properties of undefined (reading 'filter')" error reported in Reddit.
Root Cause Analysis
The error occurred in
filterNonAnthropicBlocks()when it tried to call.filter()onmessage.contentthat wasundefined,null, or not an array.However, the type system guarantees
contentisstring | ContentBlock[], so receivingundefinedmeans we have a type safety violation at the deserialization boundary.The Real Problem
In
readApiMessages(), we deserialize JSON with zero validation:This allows corrupted or legacy data from disk to bypass TypeScript's type safety, creating messages with
undefinedcontent that violate theAnthropic.MessageParamcontract.Evidence this is a known issue:
ClineProvider.ts:3315-3317already has defensive checks for non-array deserialized dataopenai-format.ts:194usesas anycasts that could create malformed messagesSolution
Add validation at the deserialization boundary in
readApiMessages()to sanitize data before it enters the type system:Changes Made
Added
validateApiMessage()function that:undefined/null/non-array content by converting to empty arraytypefield)Updated
readApiMessages()to validate each deserialized messageAdded 12 comprehensive tests covering:
Testing
All 12 new tests pass, ensuring the validation properly handles:
Impact
This fix prevents crashes when loading historical tasks with corrupted data, while maintaining backward compatibility with valid historical data. Users will no longer encounter the "Cannot read properties of undefined" error when using Anthropic models.
Related
filterNonAnthropicBlocks) rather than the root causeView task on Roo Code Cloud
Important
Adds validation to deserialized API messages in
readApiMessages()to prevent crashes from malformed data.validateApiMessage()inapiMessages.tsto validate deserialized messages, fixing undefined/null/non-array content and filtering invalid messages.readApiMessages()to usevalidateApiMessage()for each message, ensuring only valid messages are processed.apiMessages.spec.tsto cover various cases including undefined/null content, invalid roles, malformed messages, and metadata preservation.apiMessages.ts.This description was created by
for bbdcab3. You can customize this summary. It will automatically update as commits are pushed.