Skip to content

Fork the conversation at each turn's end to put the agent's missed asks On me (experimental) - #4801

Merged
max-sixty merged 14 commits into
mainfrom
agent-a2fd0f0551f1265b5
Oct 31, 2026
Merged

max-sixty merged 14 commits into
mainfrom
agent-a2fd0f0551f1265b5

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

Late in long sessions, agents leave decisions waiting on the user only in their chat replies, so On me reads empty: on 2026-10-25 five sessions holding live pages did this, and rewording the skill changed 0 of 6 replays of such a session. This adds an experimental check, in Claude Code only: as each main-loop turn ends while the session holds a live page, Leaf forks the conversation and asks the fork what the agent left to the user that On me doesn't hold. Leaf adds each ask to On me as the agent's own to-do, and tells the agent at its next step.

Principles

src/leaf/clerk.py's header states the three that bound it:

  • It adds attention, not an actor. The fork is the agent looking again at its own bookkeeping; it does none of the user's work.
  • The user never meets it. What it writes is the agent's own to-do, authored by the agent's session, with no mark, message, status or control of the fork's on the page. Its failures go to Claude Code's debug log, not the conversation.
  • The agent stays sovereign. The agent hears each to-do at its next step, with its id, and keeps or ends it like one it wrote. Leaf adds no ask whose title a to-do on the user already has, open or ended, and shows the fork the ended ones. It writes nothing once another turn has opened since the fork read the conversation, checked under the session's lock as it writes each page.

How it runs

  • hooks/claude-code.ts, on turn.complete for a turn the model answered, runs leaf hook --if-claimed --fork for the question, forks with $.model.fork (which resends the main thread's own last request, so the cache serves the conversation), and hands the reply back through leaf hook --fork. A turn that ends while a fork is out starts one more after it, whether or not the first failed.
  • src/leaf/clerk.py is the fork's one home, shared by every harness: the prompt with each page's On me, the answer schema ({"asks": [{page, title, quote}]}, checked by Leaf since $.model.fork takes no schema), and applying it. Codex and Pi would add only the fork call.
  • src/leaf/hook_transport.py carries the queued lines at the next prompt or PostToolBatch hook and clears them. leaf hook gains --fork; agent_state._page_state becomes public page_state.
  • How it works lists the check among the agent's sources, and conversation-loop.md ("The On lists") tells agents the to-dos it adds are theirs to keep or end.

Measured

Replaying 21 recorded turn ends three times each on Opus 5.5 (transcript copies cut at the turn end, all tools denied), the shipped question found 59 of 63 labelled asks; 62 of its 64 findings were real and 2 borderline, none false, and 2 of 39 runs at clean turn ends named anything. Labels were written before the runs and are one agent's judgment. A warm fork cost $0.08 (median); in the claude-code journey, twelve forks cost $0.015–0.026 each and took 1–6 s.

Checks

tests/model/test_clerk.py and the Claude Code module tests (with a fake $.model.fork in the driver, answering or refusing) pass, as do the hooks, todos and obligations tests, pnpm run check and pnpm run test:runtime. The claude-code journey passed all 12 steps on an earlier revision of this branch, before the fork's to-dos stopped carrying a mark of their own. An independent review found seven defects, and this revision fixes six of them.

Left out

  • A prompt that Escape drops loses the lines its hook carried: the to-dos stay open and leaf page state lists them, but the agent isn't told. Confirming the lines from the transcript, as delivery.confirm_entered does for input, would close that gap.
  • The conversation-loop.md sentence has no eval score.
  • Forks run at turn ends only, not after runs of tool calls. In the journey, 10 of 12 answers were dropped because the next turn opened within a second; in use, an answer is lost only when a turn opens within the fork's few seconds.

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

The Workflow tab described Leaf's skill, package instructions, delivery
handling and Stop hook notices in four separate places, and left out the
compaction reload, x-instructions, data-contract instructions, package
audiences beyond the author, and command refusals. A new section, Agent
instructions and notices, lists each source with what it tells the agent
and when the agent reads it; the package-instructions details block moves
into it.
The section claimed every instruction sits where it applies, though the
bookkeeping rules live only in the skill; said page instructions joins
x-instructions without --use; and left out PostToolBatch, unpicked input
and how a delivery reaches the agent.
… states

Late in long sessions agents leave decisions waiting on the user only in their
chat replies, so On me reads empty, and rewording the skill changed 0 of 6
replays. Leaf's clerk now forks the conversation as each main-loop turn ends in a
Claude Code session holding a live page, asks it what the page's bookkeeping
misses, and writes the answer itself:

- each ask (🟡) or reversible call (🔵) left to the user that On me lacks becomes
  a to-do on the user, marked `by: "leaf"`, which the On me card shows as added
  by Leaf from the agent's reply;
- the status line becomes the `text` of a `waiting` declaration, shown while no
  start holds an item, so a page waiting on the user says for what;
- each record written, and each missed step the fork names, reaches the agent as
  one line at its next prompt or tool boundary (`clerk.queued`).

`src/leaf/clerk.py` is the one home every harness shares: the prompt with each
page's state (On me, On you, undelivered input, asks waiting on nobody, Leaf's
items the user has settled), the answer schema, and applying the answer.
`hooks/claude-code.ts` forks with `$.model.fork` off the turn's path and hands
the answer back through `leaf hook --fork`. An answer that arrives after another
turn opened is dropped.

The `todo` record gains optional `by` and `status` gains optional `text`; the
build's protocol comparison accepts both at protocol 36, so no bump.

Replayed on the 21 recorded turn ends, three runs each on Opus 5.5 with the
shipped prompt: 59/63 🟡 asks found, 33/36 🔵 calls found, 63/63 answers valid,
$0.11 per warm fork (median).
The conversation loop's On lists now say that in Claude Code Leaf checks each
reply as the turn ends and adds decisions left only in chat to On me, that this
doesn't register questions for the agent, and how to drop one that isn't the
user's to decide. How it works lists the fork among the sources of agent notices.
The clerk takes the page lock a command's write takes, and its notice names
`leaf task end`'s outcome as the positional argument it is.
Claude Code's debug log redacts every `*_tokens` field, so the clerk's usage
line read [REDACTED]; Claude Code already logs each fork's tokens as
`Forked agent [plugin_model_fork] finished`. The header records what the
claude-code journey measured: twelve warm forks at $0.02-0.04 and 3-11 s.
The fork now asks one thing, in Leaf's own words: each decision, answer,
permission or step the agent left to the user, in its replies or anywhere in the
conversation, that no On me item holds and the user hasn't answered since. Its
answer is `{"asks": [{page, title, quote}]}`. The 🟡/🔵 markers come from the
user's personal skills, and Leaf shouldn't depend on them; the user chose to start
the fork from this one class of finding.

Removed with the status and missed parts: the fork-written status line (the
optional `text` on `status`, its served-state field, the listening line in
activity-words.js, `leafRow` and the Claude Code band) and the missed-step lines.
What those steps found that the page state alone settles is code's to report, not
a model's. Kept: the On me to-dos marked `by: "leaf"`, a context line for each at
the agent's next step, and dropping an answer once another turn has opened.
Replayed on the same 21 turn ends, three runs each on Opus 5.5: 59 of 63
labelled asks found, 62 of 64 findings labelled asks and 2 borderline, none
false, $0.08 per warm fork. In the claude-code journey twelve forks cost
$0.015-0.026 each and took 1-6 s.
# Conflicts:
#	browser/runtime/floating-panel.js
#	dist/kernel/kernel.json
#	dist/kernel/leaf.js
…fork

The fork is a second look at the agent's own bookkeeping, not a second actor,
so the user never meets it and the agent keeps the say over every record. A
to-do it adds is now the agent's, authored by the agent's session with no
`by: "leaf"` field, and On me no longer shows "added by Leaf from Claude's
reply". The agent hears each one at its next step as its own and keeps or ends
it; the fork is shown every ended to-do on the user, whoever ended it, so it
never adds one back. clerk.py's header states the three principles.

Behavior change: the todo event and the served obligation lose the optional
`by` field, and the On me card loses its "added by Leaf" line.
# Conflicts:
#	docs/how-it-works.html
How it works, the conversation-loop reference and clerk.py's header now say the fork is an experiment that runs only in Claude Code.
An independent review found that the fork could undo or slip past the agent's
own say over its records. Each change below makes Leaf, not the fork's wording,
hold the guarantee the clerk header states:

- An ask whose title matches a to-do on the user that the agent or the user
  ended is skipped, as one matching an open to-do already was; before, a
  dropped ask came back under a new id when the fork named it again.
- The check that no turn has opened since the fork read the conversation runs
  under each page's locks and then the session's, the lifecycle's lock order,
  so a prompt can no longer open a turn between the check and the writes.
- Each page's lines for the agent are queued as that page is written, so a
  later page's failure no longer leaves earlier to-dos unreported.
- The hooks module logs every failure of the fork to the debug log, never the
  conversation, and a turn that ended while a failed fork was out still forks.
- The module forks after every answered turn and lets Leaf say whether the
  session holds a page, since the band can lag a claim the turn's last command
  made.

A prompt that Escape drops still loses the lines its hook carried; the header
says so, and what would close it.

The How it works row is renamed "Turn-end check (experimental)" and, with its
intro sentence, rewritten to say the to-dos are ordinary ones the agent is told
of, after a fresh read found the old wording hard to follow.
@max-sixty
max-sixty merged commit 9d88f0a into main Oct 31, 2026
4 checks passed
@max-sixty
max-sixty deleted the agent-a2fd0f0551f1265b5 branch October 31, 2026 02:37
max-sixty added a commit that referenced this pull request Oct 31, 2026
…ecks for (#4850)

How it works described the experimental turn-end fork (#4801) only in
two sentences appended to the paragraph on where agent guidance lives,
and didn't say what the fork looks for. It now has its own block under
"Harness connections and receipt", beside "Claude Code integration". The
block covers:

- what the fork is asked to name: a decision, an answer, a permission,
or a step the agent left to the user that On me doesn't hold;
- what it leaves out, and that Leaf skips an ask with the same title as
a to-do on the user;
- what Leaf writes (the agent's own to-do, with nothing marking the
fork) and when the agent hears it;
- why it costs the turn nothing: it starts after the turn ends, reads
the prompt cache, and writes nothing more once another turn opens.

The guidance table's row now points at the block instead of restating
it.

`leaf page check` passes on the docs page, as does
`tests/model/test_product_page.py`. A fresh reviewer read the rendered
block and checked each sentence against `src/leaf/clerk.py` and
`hooks/claude-code.ts`, and found no contradiction.

> _This was written by Claude Code on behalf of max-sixty_
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