Repository navigation
Quiet Claude Code delivery: one background watch, no mid-turn wakes, hidden wake rows - #3540
Merged
Merged
Conversation
…d count what Leaf shows The background Stop hook's watch from one turn's end keeps running into the user's next turn. It woke for every comment that turn's tool boundaries were about to carry, so each steered comment showed a "Stop hook feedback" row and gave the model a reminder carrying no input; one in the baseline journey woke after the turn had already read its input. `watch_between_turns` now leaves input that arrives in a later turn to that turn: it wakes only once Claude Code reads idle with the turn still open, as Escape leaves it, or, when the turn's Stop hook closed it, only if the watch that hook starts has not taken over within the Stop hooks' time. So the watch from before a user's turn outlives an Escape and wakes for what the turn left, where it used to have woken already and left the next comment to a cross-session nudge. The hooks module logs a child that a signal from outside ended (exit above 128, as SIGTERM ends both children while a session sits idle) to the debug log rather than the conversation, and stamps a turn's ending before awaiting anything. `leaf-dev journey claude-code` counts the rows Leaf puts in the conversation the user reads (`leaf_rows`): wakes of either watcher, nudges, notices and hook errors, and wakes no delivery followed. Each step prints them, the sample keeps them, and an empty wake or a notice fails the step. One run each on Claude Code 2.1.307: - background watch before this change: 5 wakes (1 empty) and 1 nudge - background watch after: 4 wakes, no nudge - hooks module: 1 prompt wake and 3 nudges, since Claude Code drops a plugin's prompt after Escape Behavior changes: mid-turn comments no longer wake a running turn; input left by an interrupted turn wakes the session within the watch's 5 s revival check of Claude Code reading idle. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
The background asyncRewake Stop hook is now Claude Code's only watcher between turns, with no option. The module woke the session with `$.prompt.submit`, which Claude Code draws as the user's own message and drops from an Escape until the user next types, so each of its wakes after an Escape became a cross-session nudge: on Claude Code 2.1.307's claude-code journey the module put 1 prompt and 3 nudges on screen where the background hook put 4 one-line wakes. Since the background watch now waits through a running turn, it also keeps watching after an Escape, the one case the module's watch was for. Removed: the plugin's `hooks_module` option, the module's watch and wake (`WatchOwner` use, `$.prompt.submit`, the WakeRefused hook event), the background registration's stand-down, `leaf-dev journey --hooks-module`, and the driver and tests that covered them. The module keeps the band above the prompt and its follower; `hooks/watch.ts` stays as Pi's extension's watch, and its test now runs from the Pi tests. Behavior change: a session with the option on no longer keeps its watch in the module; the background Stop hook watches it like every other session. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Starting an idle Claude Code turn always draws a row: an asyncRewake hook's exit 2 is the only way a hook wakes the session, and it queues a task notification whose summary is the row (read in 2.1.307's source). The background watch's registration now names that summary (`rewakeSummary`, an internal field of Claude Code's hook schema), and the hooks module's `UserMessage` render hook draws that row as nothing, leaving the band to show the agent at work. Leaf's cross-session nudge, otherwise a 5-line message with Claude Code's paragraph on peer permissions, draws as one dim "Leaf: new input". Ctrl+O shows both as Claude Code draws them, and the model reads both whole. Checked in Claude Code 2.1.307: the claude-code journey's screens show no row for any of its 4 wakes, and a nudge sent to a `leaf-dev claude-code-session` drew one line. The driver now files render hooks by component, so a test holds the module to the summary `hooks.json` names. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
leaf-agent
approved these changes
Oct 22, 2026
max-sixty
added a commit
that referenced
this pull request
Oct 22, 2026
…ver it again if the turn ends first (#3591) 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 #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 #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_ Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
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]>
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.
Leaf's delivery into Claude Code now shows the user as little as possible: the background Stop hook is the only watch, it no longer wakes a session mid-turn, and the rows it does draw are hidden or reduced by the hooks module.
Measured with
leaf-dev journey claude-codeon Claude Code 2.1.307 (one run per arm, 7 comments), counting what the conversation shows:Steering pickup is about 25 s on every arm, bounded by the journey's own shell command.
Changes
session.pywatch_between_turns): a watch records the turn it followed. Once a later turn claims the session, it wakes only if that turn has closed and no successor watch took over withinSTOP_HOOK_S, or if Claude Code reports the session idle. This removes the empty and mid-turn wakes.test_wait.pycovers Stop and Escape.hooks/claude-code.ts,hooks.py,plugin.json,background-watch.py): thehooks_moduleoption and the module's own watch go; the module keeps the band above the prompt. Docs (harness-claude-code.md,session-lifetime.md, README, AGENTS.md) follow.hooks.json,claude-code.ts): the async Stop hook names its wakerewakeSummary: "Leaf: new input", and the module'sUserMessagerender hook draws that row as nothing and Leaf's cross-session nudge as one dimLeaf: new inputline. Ctrl+O shows both in full; the model reads both whole.rewakeSummaryis internal in Claude Code's hook schema: if it goes away, the wake shows "Stop hook feedback" again and nothing else changes.claude-code.ts): exit codes above 128 go to Claude Code's debug log instead of the conversation.journey_claude_code.py): each step prints and records what Leaf drew (shown), and fails on an empty wake or a hook-error notice.Known gaps
claude --bgjob whose background work keeps Claude Code's status busy, input after an Escape waits until that work ends.