Skip to content

Read Claude Code's turn and receipt instead of guessing them - #3917

Merged
max-sixty merged 18 commits into
mainfrom
cc-turns-from-claude-code
Oct 25, 2026
Merged

max-sixty merged 18 commits into
mainfrom
cc-turns-from-claude-code

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

Leaf's Claude Code integration guessed two things it can now read. It minted its own turn ids, and it counted an update as received when a later hook in the same turn ran. When Escape interrupted a turn at a tool boundary, that guess was wrong: Claude Code kept the hook's context in the conversation, but no later hook ran, so Leaf offered the comment again and the woken turn received it twice (TODO, "Stop delivering a comment twice after an interrupted tool boundary").

Claude Code 2.1.196 and later send prompt_id on every hook but SessionStart, and a prompt resubmitted after Escape gets a new one. The transcript writes a hook_additional_context entry for each hook output that reached the model, and none for a prompt Escape dropped. This change uses both:

  • Turns are named by prompt_id (harness.py, hooks.py). The renewal window that re-handed a dropped prompt's delivery, and its special case in record_pickup, go. A turn Claude Code submits from its queue after Escape, which no prompt hook announces, is opened by its first step (ClaudeCodeHarness.reconcile_turn).
  • Receipt is read from the transcript (confirm_entered in delivery.py, replacing confirm_turn). The cursor stops at the first pending update the transcript doesn't show, so on a Claude Code without prompt_id a dropped comment is offered again rather than marked received. The transcript is memory-mapped; finding the ids takes 27–37 ms on 150–200 MB transcripts.
  • Each prompt starts the watch (hooks/hooks.json), not only Stop, so an interrupted turn is followed by a watch that wakes the session. The server's interrupt nudge (presence.nudge_interrupted) stays only for the case no watch covers: the turn that first claims a page, whose prompt ran before the session held one.
  • README.md states the 2.1.196 minimum, found by bisecting the npm releases; session-lifetime.md, docs/how-it-works.html and the module headers describe the new mechanisms.

Evidence:

  • leaf-dev journey claude-code gained a check that the boundary comment is handed over once. On main it fails ("handed over 2 times"); on this branch all 11 steps pass, also with --sandbox. journey codex-queue passes its 8 steps.
  • Focused model and process tests pass (323 passed, 1 skipped), with new tests for the queued turn after Escape, the receipt stopping at a handover the transcript lacks, and the server nudging an unwatched first claiming turn. Each fails without its fix.
  • A real-session repro of an interrupted first claiming turn gets its comment answered in 38 s (main: 35 s).

Not checked: the prompt's watch surviving Escape and Claude Code resubmitting a dropped prompt were observed on 2.1.310 only; releases between 2.1.196 and 2.1.310 are unchecked for both.

This was written by Claude Code on behalf of max-sixty

max-sixty and others added 17 commits October 24, 2026 20:07
Survey prototype. Claude Code 2.1.310 names each turn in every hook but
SessionStart (prompt_id) and submits an Escape-dropped prompt again under a
new one; its transcript records each hook context that joined the
conversation (hook_additional_context) and none for a dropped prompt.
Measured in a tmux-driven session; scripts in
~/.tmp/leaf.agent-acdea4495d9359591/ccprobe/.

So ClaudeCodeHarness takes prompt_id as the provider turn
(hook_payload), and every Claude Code hook first records the receipt of
what the transcript shows reached the model (delivery.confirm_entered,
ClaudeCodeHarness.entered_context), replacing the proxy that a later
hook's running means the model read the context (confirm_turn) and the
turn_opened window that let a resubmitted prompt hand a dropped delivery
over again (handed_into_turn, record_pickup's exemption).

Fixes TODO 'Stop delivering a comment twice after an interrupted tool
boundary', both cases: a boundary context Escape kept, and a prompt
delivery read before Escape, are in the transcript, so the woken prompt
names them as owed instead of handing them over again. New tests:
test_a_boundary_handover_escape_kept_is_never_handed_over_twice and
test_claude_codes_transcript_says_which_handovers_reached_the_model.

Not run: leaf-dev journey claude-code. Assumes a Claude Code new enough to
send prompt_id. A full scan of a 150 MB transcript takes 172 ms per hook.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…ary comment

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…ed turn

A claim's new lifetime mints a turn before a hook names one; with only
PostToolUse allowed to adopt, a Claude Code Stop naming its prompt_id would
pass over that turn and never close it.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Survey prototype, stacked on proto/cc-turns-and-receipt-from-claude-code.
Claude Code 2.1.310 keeps an asyncRewake UserPromptSubmit hook running
through an Escape and wakes the idle session on its exit 2 (probed in
tmux; ~/.tmp/leaf.agent-acdea4495d9359591/ccprobe2/). So hooks.json adds
the background watch to the prompt registration, and a prompt's watch takes
its own turn as a later one: it waits while the turn runs, gives way to the
watch its Stop starts, and wakes only for what an interrupt leaves. The
page's server no longer reads interrupts to nudge
(presence.nudge_interrupted and its server_rows trigger go).

What changes for a user: after Escape in a turn a wake opened, the session
is woken by its own hook instead of a cross-session message, which a session
that bypasses permissions holds behind a deliver-or-deny dialog.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…e still named the old mechanisms

The site's how-it-works page and a test docstring still said the turn's next
hook confirms a Claude Code handover, the journey's header still said the
page's server nudges the session after the boundary step's Escape, and
session-lifetime.md claimed every interrupted turn has a watch, which the
turn that first claims a page does not: its prompt's watch ended before the
session held a page.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
A prompt the user types while a turn runs gets its prompt hook at once, under
the running turn's prompt_id. When Escape then ends that turn, Claude Code
submits the typed prompt as a turn of its own under a new prompt_id that no
prompt hook named (measured at 2.1.310 with a hook that logs payloads). The
prototype's turn guard returned from every such step, since the record's turn
was provider-named: the turn's tool boundaries handed over nothing and its
Stop hook neither closed the turn nor ran. In a real session a comment sent
during that turn's shell command waited until after the turn, through a 20 s
settle wait in the Stop watch, where main hands it over at the boundary.

ClaudeCodeHarness.reconcile_turn now opens the turn a step names, since Claude
Code runs a session's hooks in order and sends no late callbacks. That covers
the Leaf-minted turn the prototype widened the shared guard for, so the guard
is back to main's form and Codex's and Pi's turn handling is unchanged.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…ing it

Every hook of a session holding a page reads the transcript for receipt, and
long sessions' transcripts run to hundreds of MB. Reading and splitting a
200 MB one took 105 ms (warm) and about 500 MB of memory in each hook process;
mapping it and parsing only the lines holding Leaf's opening takes 34 ms and
allocates only those lines, with the same ids found on two real transcripts.
The transcript test now also covers a record still being written and an empty
transcript, which mmap refuses.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
… gap in the harness docstring

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
The turn that first claims a page starts its prompt's watch before the
session holds one, so that watch exits at once (`--if-claimed`), and
Escape then ends the turn with no Stop hook to start another. With the
server's interrupt nudge removed, a comment sent during that turn's
long command stayed unanswered: the reviewer's repro
(first_turn_escape.py) saw no answer after 114 s on the prototype,
and main's nudge answered it in 35 s.

The page's server again reads the interrupt from Claude Code's session
record and messages the session for the input the turn left
(`presence.nudge_interrupted`). It sends only while no watch holds the
lease, and a watch that wakes the session marks the ending it woke it
for, so beside the prompt's watch it covers just the turns no watch
follows. New test:
test_the_server_nudges_a_first_claiming_turn_escape_left_unwatched.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
A Claude Code hook's receipt moved the page's cursor to the newest
input the transcript showed, and the cursor passes everything before
it. On Claude Code before 2.1.196, whose hooks send no `prompt_id`, the
prompt resubmitted after Escape dropped one renews Leaf's turn, so the
comment handed into the dropped prompt still read as awaiting receipt
and was not handed over again. A comment the next tool boundary handed
over then reached the transcript alone, and its receipt marked the
dropped comment received though the model never saw it.

Receipt now passes the pending input only as far as the transcript
shows it without a gap, so the dropped comment is input again once the
turn ends. The README states the 2.1.196 requirement and what an
earlier release does instead. 2.1.196 is the first npm release whose
hook base input sets `prompt_id` (bisected over the published binaries;
2.1.195's input has none). New test:
test_receipt_never_passes_a_handover_the_transcript_does_not_show,
which fails on the previous receipt.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…eipt

The pointer paragraph and `render`'s docstring, merged from main's
#3907, still said the turn's next hook would confirm a recorded batch.
On this branch Claude Code's receipt follows the transcript, so the
reason a pointer never follows a recorded batch is that its opened
pickup reads as handed into the turn, and under Pi as received. No
behavior changes.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
No behavior changes.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

@leaf-agent leaf-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This overlaps #3857, which fixes the same duplicate delivery after an interrupted tool boundary in delivery.py, harness.py, hooks.py and hook_transport.py. This transcript-based approach can supersede that PR.

@max-sixty
max-sixty merged commit e78e0b5 into main Oct 25, 2026
3 checks passed
@max-sixty
max-sixty deleted the cc-turns-from-claude-code branch October 25, 2026 15:14
max-sixty added a commit that referenced this pull request Oct 25, 2026
A prompt the user types while a turn runs gets its prompt hook at once, under
the running turn's `prompt_id`, and Leaf hands that turn the pending comment
inline. Claude Code writes that hook's context to the transcript only when the
turn takes the prompt in, after the turn's next tool boundary has run. So that
boundary cannot confirm the receipt. It hands nothing over and kept its
reading, and the launcher then answered every later boundary without starting
Leaf, because the transcript is not part of the reading. The receipt waited for
the next `leaf` command, Stop or prompt. Until then the page counted the
comment as pending, and an Escape in that window would end the turn with the
comment unreceived, which the prompt's watch reads as input to wake the session
for, though the model had read it.

A boundary now keeps its reading only when no page holds a handover awaiting
its receipt (`delivery.awaiting_receipt`, carried in `PagePlan.awaiting`). The
boundary after the transcript records the handover runs whole and confirms it.
`session-lifetime.md` ("The hook") and the `hook_transport` header now name the
transcript among the facts a boundary reads, and say why it holds back the
reading.

Evidence, on Claude Code 2.1.310:

- An interactive session in tmux with a probe hook that logs which earlier
  hook contexts the transcript holds when each hook runs: the second prompt's
  hook ran at 934.2 s, the PostToolBatch at 939.3 s did not yet see its
  context, and the PreToolUse at 940.7 s did. Headless `-p`, and
  `--input-format stream-json`, which runs a queued prompt's hook at the
  boundary instead, showed no such gap.
- Three of 758 hook context records in recent transcripts were written after
  the next model response, each one for a queued command.
- Every one of 47 Leaf inline handovers in those transcripts since #3917 was
  received at the first Leaf hook after it, so outside a queued prompt the
  transcript holds a handover by the next boundary.

`test_a_handover_the_transcript_records_late_is_received_at_the_next_boundary`
replays that order through the launcher's gate. It fails without the change, at
the receipt after the transcript records the handover.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
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.

2 participants