fix(cli): prevent TypeError crash in ag read on non-text content (#4684) - #4686
Merged
Conversation
- Add null-safety check before content.split() in handleRead - Handle messages with undefined text/content gracefully - Print [non-text content] placeholder instead of crashing - Add unit tests for resolveSessionId and content handling Fixes #4684
Contributor
There was a problem hiding this comment.
✅ Approved.
Fix is correct — the null-safety guard on msg.text ?? msg.content and the string normalization before .split() will prevent the TypeError crash.
Two minor notes (non-blocking):
- The
typeof content !== 'string'check after the ternary is dead code —contentis guaranteed to be a string at that point. It doesn't affect correctness, but could be removed in a follow-up. - The 3 new tests cover
resolveSessionIdbut not the actual crash scenario (handleReadwith non-text/undefined content). Consider adding a test for the non-stringrawbranch inhandleReadto lock in the fix.
CI all green. Targeting develop. Good to merge.
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 join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fixes #4684 —
ag readcrashes with TypeError when session messages contain non-text content.Problem
ag read \u003csession-id\u003ecrashed with:This happened when the /read API returned messages with
undefinedtextandcontentfields, causingcontent.split('\n')to fail.Fix
content.split()inhandleRead[non-text content]placeholder and continueprefixis defined before the checkTests
src/__tests__/commands/read.test.tswith 3 tests:Verification
npx tsc --noEmit✅npm run build✅npx vitest run src/__tests__/commands/read.test.ts✅ (3/3 pass)Residual Risk