Repository navigation
Conversation
Claude Code 2.1.x attaches a task completion notification to the running turn as a queued_command attachment instead of writing a user row when a background Bash or MCP task finishes mid-turn. The hook only recognized user and queue-operation rows, so the notification never reached turn assembly: the tool observation kept the "moved to the background" output and the result body never appeared in the trace. Read the notification body from the attachment, map its <task-id> to the launching tool_use by finding the same id in the tool result, and let the existing notification path fill the tool observation's final content. The attachment also counts as delivery for the async gate, so an absorbed notification no longer keeps the turn held until the next prompt. Fixes langfuse#109.
…moved A content-bearing queue-operation remove row proves the notification left the queue; it can no longer continue the turn. Treat it as delivered so the turn ships at its own Stop, matching the attachment path. Remove rows without content stay conservative because they cannot be matched. Refs langfuse#96.
|
We hit this in production and can confirm #113 fixes a case not described in the PR: an agent notification that is persisted before its launch row. What happened (Claude Code 2.1.220, real session):
Replaying the real transcript offline (fake Langfuse client,
Replaying the same session incrementally, with the slice sizes our Stop hook actually sent (124 firings), also leaves nothing held with #113. Four other sessions with background and nested agents emit the same counts on The attachment sits after the launch row, so recognizing it restores the completion that the launch row overwrote. We are running #113 in our deployment until it lands. |
Summary
Background Bash and MCP tasks notify with a
<task-notification>row. When the notification arrives while the turn is still running, Claude Code 2.1.x does not write a user row: it attaches the notification to the running turn as aqueued_commandattachment, then removes it from the queue withreason: absorbed_mid_turn.The hook only recognized user and
queue-operationrows, so the attachment was ignored. The launching Tool observation kept the "moved to the background" output, and the<result>body never appeared anywhere in the trace (#109). The same absorbed notification also kept the async gate closed, so the turn only shipped at the next prompt (#96).Changes
attachment.promptwhen the attachment is aqueued_commandwithcommandMode: task-notification, and recognize it as a task notification.<task-id>, unlike agent notifications. Build atask_id -> tool_use_idmap by finding the same task id in the launching tool result (including structuredtoolUseResult.backgroundTaskId), and merge it into the map used by turn assembly.final_contentand timestamp.queue-operationremoverow now also counts as delivery. An absorbed notification no longer holds the turn open until the next prompt. Aremoverow without content still keeps the gate closed, preserving the conservative behavior for older transcript shapes.emit_ready_observations_of_open_turnsees the same attribution.Tests
Added in
tests/unit/test_task_notifications.pyandtests/unit/test_emission_gate.py:queued_commandattachment is detected as a task notification; other attachment types are nottool_use, andbuild_turnssetsfinal_content/final_timestampabsorbed_mid_turnremove, and a content-bearing remove alone all clear the async gateLocal verification:
uv run --group dev pytest -q: 281 passed, 7 failed, 1 skipped. The 7 failures are pre-existing Windows-only tests (POSIX lock/permission,HOME/~expansion, log encoding) and fail identically on unmodifiedmain.uvx ruff check --select F,E9 .: all checks passed.echo '{}' | uv run --script hooks/langfuse_hook.py: starts cleanly.Fixes #109. Fixes #96.