fix: capture every turn's gen_ai.output.messages (0.2.11)#23
Merged
Conversation
Two bugs combined to drop most assistant outputs from Sentry AI Conversations: 1. closeTurnSpan only ran on the next UserPromptSubmit / SessionEnd, racing the transcript flush. If assistant text wasn't on disk at close time, tokens.response was null and gen_ai.output.messages was silently skipped — and the 200 ms retry only triggered on zero usage, not on missing text. Wire the Stop hook (was a no-op) to closeCurrentTurn; closeCurrentTurn is idempotent so the follow-up close is harmless. Extend the late-flush retry to also trigger when recordOutputs is on and response is null. 2. selectTurn's ordinal fallback misattributed an earlier transcript turn's response to the current turn when the collector spawned mid-session (synthesized=true, local turnIndex out of sync with transcript real-turn index). Refuse ordinal matches under synthesis and record usage_extraction.status = no_matching_turn_synthesized_ordinal so the gap is observable. Adds two regression tests covering Stop-based close + the synthesized-ordinal refusal. All 426 tests pass. Co-Authored-By: Claude Sonnet 4.6 <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
Sentry AI Conversations was showing every user input but only the first assistant output. Two compounding bugs:
closeTurnSpanonly ran on the nextUserPromptSubmit(orSessionEnd). When the assistant text for turn N wasn't yet on disk at close time,tokens.responsewasnullandgen_ai.output.messageswas silently skipped — the existing 200 ms retry only triggered on zero token usage, not on missing text. Fix: wire theStophook (previously a no-op) tocloseCurrentTurn— Stop fires right after assistant text lands.closeCurrentTurnis idempotent, so a later UserPromptSubmit / SessionEnd close is a harmless no-op. The retry also now triggers whenrecordOutputsis on andresponseis null.record.turnIndexstarted at -1 while the transcript already held prior real turns.selectTurn's ordinal fallback would then attach an earlier turn's response to the current turn — visible today as a "how are you" turn surfacing turn 0's "Hi! How can I help today?" output. Fix: refuse ordinal matches under synthesis; recordclaude_code.usage_extraction.status = no_matching_turn_synthesized_ordinalso the gap is observable.Two regression tests added. 426/426 green.
Test plan
pnpm test— 426/426 passingpnpm build— clean🤖 Generated with Claude Code