fix(parser): split turns only on typed user prompts#56
Draft
rgao-coreweave wants to merge 1 commit into
Draft
Conversation
…jections
`buildSession` used `hasText = typeof rawContent === 'string' ||
content.some(b => b.type === 'text')` to identify turn boundaries.
That second disjunct misclassifies *user-text array injections* as
turn boundaries:
- skill content (`/<skill>` payloads land as a `role:'user'` line
with `content: [{type:'text', text:'<skill body>'}]`)
- `<command-message>...</command-message>` envelopes
- system reminders
- `"[Request interrupted by user for tool use]"` interrupt markers
None of these are typed by the user; they're written by the harness
mid-turn. Treating them as boundaries splits one Claude Code turn into
multiple parser turns. When the split lands between an `Agent` tool
dispatch and the parent's synthesis, `handleStop`'s
`parsed.turns[turns.length - 1]` returns the post-split fragment and
the synthesis chat span never gets emitted.
Narrow the boundary predicate to `typeof rawContent === 'string'` —
the bare-string form Claude Code uses for actual user prompts. Array
content (tool_result OR text-injection) no longer triggers a split.
Empirical impact: full corpus scan (161 transcripts, 170 Agent
dispatches) goes from 1/170 split → 0/170 split. The previously-known
0.6% blast radius (user-interrupt mid-Agent) is now covered. The lone
remaining "split" my detector flags is a legitimate case — the user
typed a new prompt while a subagent was running, so there was never a
synthesis to lose.
Subagent transcript shape (pre-context line 0 + user prompt line 1)
still produces 2 turns — verified by the smoke test below.
scripts/smoke/verify-parser-turn-boundary.mjs covers seven cases:
baseline, two-prompts, skill content, user-interrupt, command-message
envelope, multiple injections, and subagent pre-context. All pass.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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
buildSession's oldhasTextpredicate misclassified user-text array injections (skill content,<command-message>, system reminders,[Request interrupted by user for tool use]) as turn boundaries. When the resulting split landed between anAgentdispatch and the parent's synthesis,handleStop'sturns[-1]returned the post-split fragment and the synthesis chat span never got emitted.typeof rawContent === 'string'— the bare-string form Claude Code uses for actual typed user prompts.Why
Full-corpus scan across all on-disk transcripts (161 files, 170
Agentdispatches): 1/170 dispatches had synthesis split into the next parser turn under the old predicate, all triggered by user-interrupt injections. After this change: 0/170 (the one remaining flag is a legit case — user typed a new prompt while a subagent was running, so there was never a synthesis to lose).Subagent transcript shape (pre-context line 0 + user prompt line 1) still produces 2 turns — verified by the smoke test.
Test plan
node scripts/smoke/verify-parser-turn-boundary.mjs— 7 fixtures: baseline, two-prompts, skill content, user-interrupt, command-message envelope, multiple injections, subagent pre-context. All pass.npm run buildclean.🤖 Generated with Claude Code