Skip to content

fix(cli): prevent TypeError crash in ag read on non-text content (#4684) - #4686

Merged
aegis-gh-agent[bot] merged 1 commit into
developfrom
fix/4684-read-crash
Jun 13, 2026
Merged

fix(cli): prevent TypeError crash in ag read on non-text content (#4684)#4686
aegis-gh-agent[bot] merged 1 commit into
developfrom
fix/4684-read-crash

Conversation

@OneStepAt4time

Copy link
Copy Markdown
Owner

Summary

Fixes #4684ag read crashes with TypeError when session messages contain non-text content.

Problem

ag read \u003csession-id\u003e crashed with:

TypeError: Cannot read properties of undefined (reading 'split')
    at handleRead (dist/commands/read.js:48:31)

This happened when the /read API returned messages with undefined text and content fields, causing content.split('\n') to fail.

Fix

  • Added null-safety check before content.split() in handleRead
  • If content is not a string, print [non-text content] placeholder and continue
  • Reordered variable declarations to ensure prefix is defined before the check

Tests

  • Added src/__tests__/commands/read.test.ts with 3 tests:
    • resolveSessionId returns full UUID as-is
    • resolveSessionId resolves prefix to full UUID
    • resolveSessionId returns null for ambiguous prefix

Verification

  • npx tsc --noEmit
  • npm run build
  • npx vitest run src/__tests__/commands/read.test.ts ✅ (3/3 pass)
  • File size check: read.ts unchanged in size ✅

Residual Risk

  • The fix handles undefined content gracefully but does not address why the API returns undefined fields. If the API contract changes, this may need revisiting.
  • No integration test against live API — unit tests only.

- 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

@aegis-gh-agent aegis-gh-agent Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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):

  1. The typeof content !== 'string' check after the ternary is dead code — content is guaranteed to be a string at that point. It doesn't affect correctness, but could be removed in a follow-up.
  2. The 3 new tests cover resolveSessionId but not the actual crash scenario (handleRead with non-text/undefined content). Consider adding a test for the non-string raw branch in handleRead to lock in the fix.

CI all green. Targeting develop. Good to merge.

@aegis-gh-agent
aegis-gh-agent Bot merged commit 35a90a5 into develop Jun 13, 2026
18 checks passed
@aegis-gh-agent
aegis-gh-agent Bot deleted the fix/4684-read-crash branch June 13, 2026 10:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant