Skip to content

Confirm a Claude Code hook delivery at the turn's next hook, and deliver it again if the turn ends first - #3591

Merged
max-sixty merged 2 commits into
mainfrom
d02-confirm-hook-delivery
Oct 22, 2026
Merged

max-sixty merged 2 commits into
mainfrom
d02-confirm-hook-delivery

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

Max's decision #2 on the overnight report: confirm a Claude Code hook delivery at the turn's next hook (option B), so an update the turn never read is delivered again.

The defect

Claude Code reads a hook's output only once every hook of that event has exited. Escape before then drops it. Leaf's hook recorded the receipt as it returned, so the update read Picked up while no model had seen it, and nothing offered it again. Reproduced on Claude Code 2.1.308 with the new journey steps below, against the base:

  • A resolve whose wake Escape interrupted never reached the agent (no hook context carried it within 120 s).
  • A comment handed over at a tool boundary (PostToolBatch) in a turn a wake opened was not answered within 150 s. Escape cancelled the boundary's hooks and ended the turn ("turn ended in error"), and no watch or nudge followed. This also settles the TODO's untested guess that PostToolBatch shares the window.
  • On 2.1.308, Escape during a wake's prompt hooks makes Claude Code submit the queued wake again at once. On the base the retry carried only a reminder, so an owed comment still got answered, but an update that owes nothing was lost.

What changes

  • Handover and receipt are separate again. A Claude Code hook records only the opened pickup, so the page shows the update as delivered at once, as before. The turn's next hook records the receipt (delivery.confirm_turn). That hook is PreToolUse before a leaf command, PostToolBatch, or Stop, and each runs only after the model has read the context. Pi's extension commits a hook's context itself, so its hooks still record the receipt with the pickup (Harness.commits_hook_context).
  • One rule decides re-offering. delivery.deliverable leaves out what a hook handed into the owner's still-open turn (awaiting_receipt). Once that turn reads ended, the update is deliverable again, by the same path Deliver a page's unanswered updates to its new owner #3272's re-offer to a new owner uses. A turn ends when Stop closes it without confirming, or when Claude Code's record reads idle after an interrupt (activity.claimant_turn). It is also deliverable again when a prompt opens the turn again. Claude Code reuses Leaf's turn id after an Escape, so a handover counts only from the turn's latest opening (handed_into_turn). That is how a resubmitted wake's prompt hook hands it over anew. No new event kind or record is added: the opened pickup is the handed state, and the existing receipt is the confirmation.
  • The re-offer reaches a session with no watch. An interrupted turn runs no hook. The page's server already reads Claude Code's record every presence interval, so when it reads an interrupted turn holding such input and no watch, it nudges the session (presence.nudge_interrupted, called from server_rows). A watch from before the turn wakes for the input itself. As it wakes, the watch marks the ending as messaged (session.woke_after), so the server doesn't also nudge in the moment before the woken turn starts. An earlier journey run of this branch caught that double wake.
  • "Picked up" becomes "Delivered" in every visible string, and "Waiting for pickup" becomes "Waiting for delivery". Under the new contract, Leaf knows it delivered the update into a turn, and it delivers the update again if that turn ends unread. The protocol ids (pickup events, stage picked_up) are unchanged, so this needs no protocol bump. The glossary records the stage id, and the docs, references, examples, corpus and kernel follow.
  • The TODO entry for this decision is removed.

Tradeoff

A turn interrupted after the model read a handover but before any confirming hook ran gets that update again in its next turn. For example, the user presses Escape during the agent's first non-leaf tool call. The agent may then see the update twice. Leaf doesn't run a hook before every tool, because each one costs a process start.

Checks

  • leaf-dev journey claude-code passed every step on this branch, including three new ones. dropped: Escape during a wake's prompt hooks, with a second prompt hook sleeping 6 s, and the comment is delivered again and answered. withdrawn: the same for a resolve, which reaches a hook's context. boundary: Escape during a PostToolBatch in a woken turn, and the server's nudge brings the comment back, which is answered. On the base, withdrawn and boundary fail as described above. The --hooks-module arm named in the decision no longer exists, since Quiet Claude Code delivery: one background watch, no mid-turn wakes, hidden wake rows #3540 removed the module's watch, so the plugin's module loaded in this one run.
  • Hook cost from the journeys' debug logs: PreToolUse before a leaf command went from a median of 145 ms (base, n=15) to 169 ms (n=18) under a loaded host. PostToolBatch had a median of 253 ms on the base and 222 ms here.
  • New test_a_handover_its_turn_never_confirmed_is_offered_again covers an owed comment and a resolve. They are handed over, the turn is read as interrupted, both are deliverable again, the row publisher nudges, a renewed prompt hands them over again, and PreToolUse confirms. Tests that read the cursor right after a handover now read the pickup and confirm with a later hook. Model, process, runtime and snapshot tests covering hooks, waits, receipts, activity and the CLI pass.

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

max-sixty and others added 2 commits October 22, 2026 10:27
…ver it again if the turn ends first

Claude Code reads a hook's output only once every hook of the event has exited,
and Escape before then drops it. Leaf's hook recorded the receipt as it returned,
so an update read Picked up while no model had seen it, and nothing offered it
again: an update that owes no answer, such as a resolve, was lost.

A Claude Code hook now records only the opened pickup (shown at once as
Delivered); the turn's next PreToolUse, PostToolBatch or Stop hook records the
receipt. A handover becomes deliverable again once its turn reads ended, or a
prompt opens the turn again. The page's server nudges a session whose
interrupted turn left such input and has no watch.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@max-sixty
max-sixty merged commit 311c82d into main Oct 22, 2026
3 checks passed
@max-sixty
max-sixty deleted the d02-confirm-hook-delivery branch October 22, 2026 17:45
max-sixty added a commit that referenced this pull request Oct 22, 2026
…in a turn of its own (#3596)

Max's decision #3a on the overnight report: when a Claude Code turn ends
still owing a page answer, end it on its answer and remind the agent in
a turn of its own (option A).

### The defect

Leaf's Stop hook continued a turn that still owed an answer to a
delivered update, telling the agent which update it owed. The agent then
wrote one more message, usually a short note about the comment. Claude
Code's focus view shows only each turn's last message, so that note
stood in place of the answer the turn had written. In Max's sessions of
10–19 October this was 9 of the 65 Stop continuations; #3256 had already
moved the late-input cases to a turn of their own.

### What changes

- **Stop ends the turn on its answer** where a watch follows the turn
(`Harness.watches_between_turns`: Claude Code at its terminal, Pi with a
UI). `hook_transport.stop_continues` no longer counts response debt
there. Under plain `--print` and on Codex, where no watch wakes a later
turn, Stop still continues the turn as before. A page Leaf could not
re-vendor still continues the turn.
- **The watch wakes a turn of its own for the reminder**
(`session.left_owed`, `activity.left_owed`). After a Stop closes the
turn, the watch wakes for each owed answer that turn didn't start and
that no prompt has named since the update's delivery. It wakes the same
way #3256 wakes for late input. That turn's prompt hook names the owed
answer with its exact ref, as every prompt already does. The wake line
says the page has an update still owed an answer, rather than new input.
- **The record that Leaf reminded is the prompt's existing pickup, with
no new record.** When a prompt re-presents an update already received
and still owed, the prompt hook records an opened pickup without the
update's `answer.ref` (`pick_up_acknowledged`). `delivery.reminded`
reads that pickup: a ref-less pickup after the newest delivering one
means a prompt has named the update since its delivery. This is the same
pickup record #3591 (decision #2) reads as handed but unconfirmed. So a
turn that ends still owing the same answer wakes nothing more, whether
the reminder came from the watch's turn or from a prompt the user typed.
A new delivery of the update, as when it changes hands, starts over.
- `leaf wait` and the Codex adapter keep their watch passes as they
were: `session.read_watch_pass`'s default still wakes only for a batch.
- The TODO entry for this decision is removed.

### Checks

- New
`test_a_turn_ends_on_its_answer_and_a_turn_of_its_own_names_what_it_owes`:
a prompt hands a comment over, and Stop ends the turn with no output.
The watch wakes with the owed-answer line, the next prompt names the
comment, and when that turn ends still owing it, nothing more is owed a
wake. On main, the first Stop continues the turn instead.
- `test_agent_sees_the_complete_interaction_recovery`'s snapshot now
shows a Stop with no output, the watch's reminder wake, and the reminder
in the next prompt, where it used to show the reminder in the Stop
hook's output.
- Tests whose subject is what Stop says when it holds a turn now run
under plain `--print` (`printing`), where it still does. Two
monkeypatched policies take the new keyword.
- `leaf-dev journey claude-code` gains a `deferred` step. The user's own
turn, told to leave Leaf's comments for later, is handed a comment at a
tool boundary and ends on its answer, with no Stop hook continuing it.
The step checks that the user's turn closed with the comment unanswered,
that no Stop hook output named it, and that a later prompt named it
before it was answered. The full journey passed every step on this
branch. In `deferred`, the user's turn ended on "done", and the woken
turn answered the comment 35 s after it was sent, with one wake row and
no empty one. The journey's row count now counts a wake as filled when
its prompt carries a reminder (`ATTENTION_OPENING`) as well as a
delivery. The `--hooks-module` arm named in the decision no longer
exists, since #3540 removed the module's watch, so the plugin's module
loaded in this one run.
- Model and process tests covering hooks, waits, receipts, activity,
workflows, Codex and the hooks module pass.

> _This was written by Claude Code on behalf of max-sixty_

---------

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
leaf-agent pushed a commit that referenced this pull request Oct 22, 2026
)

This adds a TODO entry for a follow-up to Max's decision #2 on the
overnight report (#3591). After an interrupted tool boundary, #3591's
re-offer delivers a comment twice.

#3591 offers a Claude Code handover again once its turn ends with no
later hook. That is right for the prompt hook, where Escape drops the
prompt and Leaf's context with it. On Claude Code 2.1.308, though,
Escape during PostToolBatch's hooks keeps Leaf's context in the
conversation. In one `leaf-dev journey claude-code` run (its `boundary`
step), the transcript held the comment in both the PostToolBatch context
and the nudged turn's prompt context, and the agent wrote that it "came
in twice". A turn interrupted after the model read a prompt's delivery,
but before any confirming hook ran, gets that delivery twice as well.

The entry records the finer fix: offer an update again only when a
prompt hook handed it over, and after an interrupted tool boundary
remind the agent of what it owes instead. No code changes.

> _This was written by Claude Code on behalf of max-sixty_

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
max-sixty added a commit that referenced this pull request Oct 22, 2026
A paired replay of #3591 (8 pairs, plus a 4-pair guardrail) found that the
widened "Land a change" check was in context in every rules-arm run and
changed nothing those runs wrote: it loads at session start, 70 to 100
turns before the reference edit. The protocol-reference rule in
src/AGENTS.md never reached the agent either, since agents read code
through git diff, sed and rg, and a nested AGENTS.md loads only when the
Read tool opens a file under it. The guardrail held in both arms.

That rule stays as the standard the tend reviewer enforces: the review
gate is the one place that sees every reference diff. The skill's
paragraph returns to main's text, saving its cost in every session.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
max-sixty added a commit that referenced this pull request Oct 23, 2026
A diagnosis of 28 commits that grew Leaf's maintainer guidance found
that about 40% of the added words change nothing for a reader. Most came
from feature and fix PRs copying their mechanism into the protocol
references, one clause per race closed. This PR states where that text
belongs and adds the check at review, which is where the replay showed a
rule can reach the edit.

- **Root `AGENTS.md`**, in the sentences the maintainer chose: "Stage"
says a change that keeps every guarantee changes no contract prose, and
"Where instructions live" says a shared contract goes in its boundary's
protocol reference, which says what each side relies on rather than how
one side provides it.
- **`src/AGENTS.md`, "Protocol references"**: what a reference holds
(shared records and events, who writes them, when a reader stops
believing them, the orderings a change must keep) and where a module's
mechanism goes instead (its header).
- **`running-tend`, review threshold**: a maintainer-guidance sentence
that narrates how the diff's code works, or which failure it fixed,
joins the prose defects a review flags. Moving that text to the module
header or the commit message is the fix.

An A/B replay of #3591 (8 pairs) tested rules placed at session start.
The `src/AGENTS.md` rule never entered any run's context, and a
`/developing-leaf` landing rule loaded 70–100 turns before the edit and
changed nothing, so that rule was dropped. The review gate is untested.

The same pass fixed four eval cases that were stale against current
main, plus a harness crash:

- `widget-list-keeps-focus` now accepts `performUpdate()` on a
`LeafElement`, which `module-authoring.md` names for drawing at once.
- `pin-takes-no-page-room` requires a route a phone user has: the menu,
not the `o` key.
- `browser-geometry/code-copy-reserves-room` matches a verdict that has
leading markup.
- `leaf-dev eval` gave each provider label a nested work directory, so
`claude-code/base/instructions:…` landed inside `claude-code/base` and
raised `FileExistsError`. Each label now gets one flat directory, and
the smoke test covers a case with instructions next to one without.

> _This was written by Claude Code on behalf of max-sixty_

---------

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
max-sixty added a commit that referenced this pull request Oct 25, 2026
Since #3591, Claude Code's prompt and tool-boundary hooks both left their handover unconfirmed until the turn's next hook. If the turn ended first, Leaf offered the update again. That is right for the prompt hook: Escape while Claude Code still runs prompt hooks drops the prompt, and Leaf's context with it. It is wrong at a tool boundary. Escape while another PostToolBatch hook runs keeps Leaf's context in the conversation, so the server's nudge delivered the comment a second time.

### What changes

- **A Claude Code tool boundary records its receipt as it hands the delivery over**, as every Pi hook already does. `Harness.commits_hook_context` (a bool per harness) becomes `Harness.drops_hook_context`, the set of hook events whose context the harness may still drop after Leaf's hook returns. For Claude Code that set is `{"UserPromptSubmit", "Stop"}`. Stop stays in it because whether Escape drops a Stop hook's output is unmeasured, and Stop hands input over only under plain `--print`. Every other harness's set is empty, which matches its earlier `True`.
- So only a prompt hook's handover can be offered again. Its re-offer path is unchanged (`deliverable`, `awaiting_receipt`, `handed_into_turn`, `confirm_turn`). The prompt-hook case stays covered by `test_a_handover_its_turn_never_confirmed_is_offered_again` and the journey's `dropped` and `withdrawn` steps.
- **After Escape, a comment the tool boundary handed over is treated like any other received input.** It waits for the user's next prompt, and that prompt's hook names what is still owed. The server no longer nudges the session for it. This is how Pi already behaves after an interrupt, and how Claude Code already behaved for input a later hook had confirmed.
- The docstrings, the "Delivered" row in `docs/how-it-works.html`, and the TODO entry now describe this. The TODO entry keeps only the open decision.

### Why there is no reminder nudge, and no protocol bump

The brief asked for a nudge after an interrupted tool boundary that reminds the agent of what it owes. Without a protocol bump, the log can't target that nudge. Once a tool boundary writes its receipt, its handover looks the same in the log as a prompt handover that a later hook confirmed: an opened pickup followed by a receipt. A nudge that reminds the agent about every received, unanswered update in the interrupted turn would also fire in the commonest Escape case: a wake turn whose prompt delivered a comment, and the user pressed Escape while the agent was answering it. Leaf would start the agent again right after the user stopped it. To limit the reminder to tool-boundary handovers, the pickup has to record which hook handed it over. `pickup` is `additionalProperties: false`, so that needs a protocol bump. That choice is left to Max as the narrowed TODO entry.

**A call Max may reverse.** Before this change, a comment handed over at a tool boundary just before Escape was answered without the user doing anything, and the agent received it twice. Now it is delivered once, and it waits for the user's next prompt.

### Evidence

`uv run leaf-dev journey claude-code`, on Claude Code 2.1.310:

- **Before, on main (`f00911ed1`), all steps passed.** In the `boundary` step, the comment "Is the timezone bug a regression?" got two inline handovers. The log shows an opened pickup with refs from PostToolBatch at 03:28:13.597Z, Escape (`onCancel`) at 14.018Z while the other PostToolBatch hook slept, and a second opened pickup with refs from the nudged turn's prompt hook at 16.054Z. The debug log shows that Leaf's PostToolBatch hook `provided additionalContext` before the cancel.
- **After, all steps passed, with the `boundary` step now checking the new behavior.** Ten seconds after Escape, no nudge row had appeared and the comment was still unanswered. The user's next prompt ("Carry on.") named the owed comment, and that turn answered both comments. The transcript holds exactly one inline delivery of the comment, the PostToolBatch one. This also confirms on 2.1.310 that Escape keeps a tool boundary's context in the conversation. The log shows one pickup with refs, then a receipt 5 ms later in the same transaction.

The new `test_a_tool_boundarys_handover_survives_escape_and_is_not_delivered_again` is the reproducer. On main it fails on the receipt. With that assertion removed, it fails because `deliverable()` returns the comment again after the interrupt. `test_claude_codes_turn_confirms_what_its_hooks_hand_over` now expects the boundary to confirm its own handover.

### Checks

- `uv run pytest tests/model/test_hooks.py tests/model/test_delivery.py tests/model/test_hook_registration.py tests/model/test_wait.py tests/process/test_hook_gate.py -n 4`: 131 passed.
- `uv run pytest tests/model/test_activity.py tests/model/test_claims.py tests/process/test_interact_claude_code_module.py tests/process/test_read_state.py -n 4`: 66 passed.
- `uv run pytest dev/tests/model/test_journey.py`: 19 passed.
- The pre-commit hooks pass on every changed file.

### Not verified

- What happens if Escape lands inside Leaf's own PostToolBatch hook, after its receipt but before it prints, is unmeasured. That window is a few milliseconds. An owed comment would still be named by the next prompt through its delivery pointer, but an update that owes nothing would be lost there. Pi accepts the same window.
- Whether Escape drops a Stop hook's context is unmeasured, so Stop keeps the deferred receipt.

### Merging with `pointer-handover`

The local branch `pointer-handover` ("Bound the pointer note's session lock by the hook's deadline") changes `receive_held` to return the batches it recorded and rewrites `carry_turn`'s `confirm=` lambda. This PR changes one argument inside that lambda, from `confirm=harness.commits_hook_context` to `confirm=event not in harness.drops_hook_context`. A merge keeps that branch's lambda and `receive_held` signature, with this argument substituted. Its docstring on `receive_held` should name `Harness.drops_hook_context` in place of `commits_hook_context`.

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