Skip to content

fix(interactive): treat missing thinking/text payloads as empty instead of crashing - #734

Open
groovyplanet wants to merge 1 commit into
code-yeongyu:mainfrom
groovyplanet:fix/malformed-thinking-block-crash
Open

fix(interactive): treat missing thinking/text payloads as empty instead of crashing #734
groovyplanet wants to merge 1 commit into
code-yeongyu:mainfrom
groovyplanet:fix/malformed-thinking-block-crash

Conversation

@groovyplanet

@groovyplanet groovyplanet commented Aug 5, 2026

Copy link
Copy Markdown

Summary

Provider extensions can emit assistant content blocks in the wrong shape
(e.g. { type: "thinking", text: ... } instead of { type: "thinking", thinking: ... },
or a bare block with no payload). The interactive TUI crashed with an uncaught

                                                                                                                                                                                                                                                                                                                                                                       
TypeError: Cannot read properties of undefined (reading 'length')                                                                                                                                                                                                                                                                                                      
                                                                                                                                                                                                                                                                                                                                                                       

during smooth streaming reveal (block.thinking.length), and transcript
rendering could also crash on already-stored messages (content.thinking.trim()).

This hardens the core renderer so malformed blocks degrade to empty content
instead of terminating the whole TUI process.

Changes

  • streaming-reveal-content.ts: countVisibleUnits / buildDisplayMessage
    treat a missing text/thinking payload as "" before counting, slicing,
    and .length checks.
  • assistant-render-descriptors.ts: isVisibleContent and descriptor
    extraction use (content.text ?? "").trim() / (content.thinking ?? "").trim().
  • test/streaming-reveal-content.test.ts: regression test for malformed
    thinking/text blocks; fails with the exact production TypeError when the
    fix is reverted.
  • changes.md: records the fork-visible behavior change.

Test plan

  • RED: regression test fails with the exact production TypeError when the
    fix is reverted.
  • GREEN: 19 reveal tests + 28 render-path tests pass.
  • Root tsc --noEmit, biome check --error-on-warnings, and the repo's
    pre-commit gate (biome + pinned-deps + ts-imports + shrinkwrap + browser
    smoke) all pass.

Summary by cubic

Fixes a crash in the interactive TUI when provider extensions emit malformed assistant blocks. Missing text/thinking payloads are treated as empty strings so streaming reveal and transcript rendering don’t throw.

  • Bug Fixes
    • Normalize missing text/thinking to "" in streaming reveal and render descriptor paths to avoid .length/.trim() on undefined.
    • Add a regression test for malformed blocks that previously caused “Cannot read properties of undefined (reading 'length')”.

Written for commit 2432d02. Summary will update on new commits.

Review in cubic

…ad of crashing

Provider extensions can emit assistant content blocks in the wrong shape
(e.g. { type: "thinking", text: ... } instead of { type: "thinking",
thinking: ... }, or a bare block with no payload). The smooth streaming
reveal read block.thinking.length / block.text.length off undefined and
the transcript renderer called .trim() on the missing payload, crashing
the whole TUI process with an uncaught
TypeError: Cannot read properties of undefined (reading 'length').

Normalize missing payloads to empty strings in countVisibleUnits,
buildDisplayMessage, and createAssistantRenderDescriptors, and add a
regression test that fails with the exact production TypeError when the
fix is reverted.
@groovyplanet

Copy link
Copy Markdown
Author

Remaining unguarded read in the same crash path — follow-up suggestion

This PR hardens the two interactive render paths (streaming-reveal-content.ts, assistant-render-descriptors.ts), but the same crash signature still exists for persisted malformed blocks:

  • packages/coding-agent/src/core/compaction/compaction.ts:339-341estimateTokens() reads block.text.length / block.thinking.length unguarded. A malformed block that survives into a session (e.g. { type: "thinking", text: ... } with no thinking key, or a bare { type: "text" }) throws the identical TypeError: Cannot read properties of undefined (reading 'length') when compaction estimates tokens over that session.

Two options for a follow-up:

  1. Apply the same ?? "" guard in estimateTokens (minimal, same shape as this PR), or
  2. Normalize/validate assistant content blocks at the ingestion boundary so every consumer (streaming, render, compaction, export) is safe at once.

Note: core/extensions/builtin/claude-sdk-oauth/prompt-bridge.ts:35 also calls block.text.trim() unguarded — worth auditing in the same pass.

Happy to open a separate PR for either approach.

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