fix: validate deserialized API messages at boundary to prevent crashes #10956
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.