Skip to content

Let a fork of the conversation do a thread's work in its own turn, behind LEAF_FORK_WORK - #5961

Merged
max-sixty merged 116 commits into
mainfrom
send-pickup-latency
Nov 14, 2026
Merged

max-sixty merged 116 commits into
mainfrom
send-pickup-latency

Conversation

@max-sixty

@max-sixty max-sixty commented Nov 14, 2026 •

Copy link
Copy Markdown
Owner

When the user writes in a thread while the agent is busy, Leaf already forks the conversation to post the agent's working line at once (#5552). This lets that fork go on to do the thread's work itself, in a turn of its own, while the main thread stays in charge: the main thread's pickup or answer takes any thread back, and a thread never has two forks. Forks that do the work run only with LEAF_FORK_WORK=on. The line fork stays on by default, and now also judges whether a fork could take the message.

The design's home is the new Forks guide, docs/forks.html, linked from How it works. notes/fork-replies.md keeps the research and the steps still to come.

How it works

  • One turn per agent. src/leaf/holding.py is the one rule for which turn of a session holds each thread message. Pickups and replies name their turn; a fork's take is recorded as its turn's pickup, so a message goes from kept back to held with no gap. A session's agent turns live in a new state-home record, agent-turns (state.py).
  • Marking a fork's commands. Claude Code runs no plugin module hooks for agents a plugin spawned, so Leaf's hooks.json PreToolUse hook marks a known fork's leaf commands with leaf --agent-id <id> (hooks.agent_command). Its call may contain leaf only as its first word; anything else is denied with how to rerun it. Every command of a fork whose turn has ended is refused.
  • Working forks (forks.py, hooks/claude-code.ts). On take, the module spawns a fork of the conversation ($.agent.spawn, subagentType fork). It answers in the thread, answers and opens a to-do the main thread owes, or hands the thread back with HAND BACK:. A later message in the thread resumes the same fork ($.session.send), which is told to read leaf page state. A resume that fails, a fork that hands back, or one that ends unanswered gives the thread to the main thread, whose next tool boundary or prompt receives the user's message. Follow-ups reach a running fork at its tool boundaries, by pointer when too large for the hook.
  • Who has what. leaf page state names a holder on each obligation record the agent owes, who owes it now: the fork that took the thread while its turn runs, by the name the main thread reaches it by, and otherwise the main thread. Each thread names the turn that last took it, with whether a fork's turn is running, closed or lapsed (holding.holder). The Stop hook and the watch read the same field to leave a running fork's work alone. The new recovery-leaves-a-forks-answer eval case gives a compacted main thread the reference's own page state selection: with holder it left the fork's answer 3/3; without, 0/3.
  • Telling the main thread. Leaf's clerk queues a line as a fork takes a thread and as its run ends, and wakes an idle session for a thread handed back or a to-do a fork opened.
  • Module identity. A session keeps the hooks module it loaded until /reload-plugins. The module names its source's digest to the pages follower, which keeps nothing back for a module older than its Leaf.

Protocol 44

A Leaf at 43 doesn't know that a fork's pickup holds a message, so it would pick the message up again for the main thread, and it reads a reply's turn from start.turn, which moves to the reply itself. Pages go to 44 as each session's Leaf takes them; clerk, pointed and the new agent-turns records start at 44.

Seeing what happened

  • leaf-dev timeline PAGE --message ID lays a page's log and diagnostics beside the session's transcript and debug log: whether a message was kept back, what the line fork judged, the fork's turn, its commands (refused or denied ones included), how its run ended, why no fork lane was up, and each agent turn running, closed or lapsed now.
  • leaf-dev response-times counts line-fork decisions and working-fork endings across pages.
  • The claude-code journey gains --forks and --fork-work steps: idle session, follow-up, resume, to-do and the work itself. The line-fork eval case replaces fork-reply.

Review

An independent reviewer's first pass found 14 issues, and its passes over the fixes found 5, then 3, then none; its review of the holder change found no defect in what the hooks decide and two gaps in what page state showed, both fixed. Each behavior fix has a test that fails without it. Two stay as they are: a fork inside one long tool call loses its thread, as the design has it (listed below); and a simplification that would have Leaf's hooks read Claude Code's internal files about each subagent is left out, to keep Leaf off them. The live claude-code journey with --fork-work passes every step: setup, release, idle session, follow-up, resume, to-do, the work itself and quit.

Left open

  • A leaf command no agent id marks acts as the main thread, whoever ran it: a script a fork runs, a worker subagent, or the user's terminal. The hook reads the call's words, so a fork's script gets past it. The TODO in state.acting_turn has the next step: mark every caller the hook sees.
  • What Claude Code does with a second spawn under the same fork name is untested.
  • holding.rounds settles a round at any agent message, so the main thread answering an older choice refuses a fork's newer answer.
  • A fork that spends more than 15 minutes inside one tool call lapses, and the main thread's next hook takes its thread; the fork's later answer is refused.
  • CI catches a module Claude Code would refuse (claude plugin validate, from main), but doesn't type-check hooks/claude-code.ts, whose types Claude Code's npm package lacks.

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

…imes

The benchmark now carries the fork that decided each input (`fork`: its
outcome and seconds) and adds three summary rows: `delivered`, from the
opened pickup to the agent's first words over inputs no fork answered,
and `fork_reply` / `fork_hand_over`, the first reply's time over inputs a
fork answered or handed to the session.

On 58 thread messages across 26 pages since 1 November: delivered p50
14.1 s, p90 40.5 s; a fork answered 6 (first words p50 10.6 s) and
handed over 33, failures included (first words p50 34.9 s, p90 117 s).
From 1 to 5 November forks answered 6 of 40 thread messages, handed 27 to
the session in a median 3.2 s, and failed on 7 with nothing-to-fork after
a compaction, an away summary or a restart. Handed-over messages showed
nothing from the agent for a median 35 s, the session's first model call
thinking before its one-line reply, though each fork's hand-over reason
already named the work the session's line later named.
A fork that hands a thread message to the session now answers with the
line the agent would say before starting, and Leaf posts it as the
agent's progress at the message's response address before the hand-over.
The thread reads Working with that line about 3 s after sending, instead
of showing nothing from the agent until the session's first model call
ends (a median 35 s on real pages since 1 November). The session's own
next words replace it, and it still owes the answer: the line's start
comes before the pickup, so it lets no turn end over the message.

This reverses the user's 2026-10-31 choice against a fork that says the
work will follow; `forks.py`'s header records both. A hand-over's
`hand_over` is now that line rather than a reason for Leaf, and its
diagnostics record the line (`text`, `line`) in place of `why`.

Evidence: the fork-reply eval on head passed 12 of 12 runs, every
hand-over case with a line its judge accepted, against 0 of 10 lines on
the base; the reply case passed 2 of 2 on both arms. The claude-code
journey with --forks passed, with the hand-over's line as the comment's
first reply 3.0 s after sending, against 25 s to pickup.
…turn

notes/fork-replies.md: why `$.model.fork` had nothing to fork, and a
design not yet built for a fork that does a self-contained request
itself: which recorded hand-overs it could take, what keeps it coherent
with the session, and how evals could score it.

notes/user-feedback-responsiveness.md: a probe of the module's turn.step
stream and $.http.fetch, for streaming the model's own progress into an
update's working line.
A fork's line was progress at the message's response address, so it took
the message in hand as a start naming the session's turn. Every reader of
starts then took it for the session's own: the line renewed an
interrupted turn, so a session with no watch was never nudged; it held the
message at Working, so a stalled one never counted as overdue; and the
Stop hook offered the session a start it never made. A review confirmed
each against a failed fork, which posts no line.

A receipt's progress (`thread.Receipt`, the writer a fork posts through)
now takes nothing in hand. The thread shows the agent's line as its
progress message at once, and the message reads Queued for Claude or
Delivered, as its delivery stands, until the session starts it.

leaf-dev response-times credits a fork's line as the first reply of its
thread's newest waiting message, and `delivered` now runs from the pickup
to the session's start or answer, the time the label reads Delivered. The
claude-code journey with --forks passed: the line was the comment's first
reply 2.4 s after sending, against 24.9 s to pickup.
…he wait

The fork's line no longer opens with the session's running command ("once this
command finishes, …"); the prompt asks what the agent will do about the message
and leaves the order of the session's steps to the fork. The line rubric drops its
demand for that wait and fails a line that speaks of a fork, a main session or a
hand-over, which the user never sees: a prompt asking only "what will happen with
the message" drew "Passing this to the main session…" in 7 of 10 lines.
fork-reply/* passes 12/12 on this commit.

A TODO in forks.take marks where a fork could do self-contained work itself.
…he fork's start

A Claude Code fork made with `$.model.fork` can't call tools, so it no longer
answers held thread messages: it writes the agent's one-line working message,
which Leaf posts as before, and hands every message to the session. A failed
or blank fork hands over with no line. The user chose this on 2026-11-05,
reversing the 2026-10-31 fork that answered from the conversation; answering
moves to a fork that can act, marked by a TODO in `forks.take`.

The user's message now reads On it from the moment Leaf holds its thread for a
fork, and while the fork's line stands, until the session answers.
`forks.forked` lists those messages in the presence reading, and
`served_state.work` serves them as at work: a workflow's `at_work` is now its
session's working or replying stage or a fork's taking its input on. Every
surface that shows whether the agent is at work reads that one stamp, so the
message's word and face, the thread card's live working line, the On panel's
row and the margin agree; the card and the face read it in place of
restating the stage set. Every reader that ages, nudges or holds a turn keeps
reading the delivery stage, so a fork's line still renews no turn, hides no
overdue message and gives the Stop hook no start to offer, the three faults a
start from the line caused.

The lane going down writes nothing to the page, so the presence reading now
names it (`fork_lane`, a live fact): a tab showing On it for a held message
re-reads within the presence cache interval and shows Queued for Claude.

The fork-reply eval now scores the line on every case, including two the fork
used to answer (12/12); the claude-code journey's fork steps check that the
line is each comment's first reply and that the session then answers it.
…hind long commands

notes/user-feedback-responsiveness.md gains two proposals.

Streaming the reply on Claude Code: the reply is a `leaf thread reply` Bash call,
whose arguments the hooks module's `turn.step` hook sees as they are written (a
probe: 749 characters in 35 pieces, one every 50 ms). The source is that hook,
or more sturdily a reply tool of Leaf's own with JSON arguments; the sink a
page-server route that sets the provisional reply as Codex's adapter does. A
`$.model.fork` fork streams nothing a hook sees, since its text comes back whole;
a fork made with `$.agent.spawn` would.

Messages that waited behind a long command: 6 of 58 thread messages from 1 to 5
November waited over a minute for a busy session, 4 of them behind one session's
foreground `sleep 280` loops and a benchmark, run on a standing memory instruction
to wait inside the turn (now steps of about a minute). Fresh sessions already
background long commands, so the note proposes telling the agent at the next
delivery how long its input waited, once measured.
The first step toward a fork that does a thread message's work itself: the fork
that writes the agent's working line now answers `{"line", "work"}`, where `work`
is `take` when a fork of the session with its tools could do the work beside the
session (it needs nothing the session is changing, settles none of its open
questions, and waits on nothing it is running) and `hand_over` otherwise. Leaf
records the judgment with the outcome in the page's diagnostics (`fork_reply`) and
still hands every thread to the session, so real pages show how often a fork would
take a message before anything acts on it. An answer that isn't that JSON hands
the thread over bare, as a failed fork does.

`fork-reply*` labels each case with who should do its work and scores the
judgment: 14/14 over the seven cases twice, three `take` and four `hand_over`. The
suite's base case, `why-ci`, runs only under `fork-reply*`, not `fork-reply/*`.

notes/fork-replies.md, "A fork that takes the work", now holds the design and what
probes of Claude Code 2.1.318 showed a module-spawned fork can do: it runs in the
session's environment, the module can deny or steer its calls, and the main thread
learns of it only from Leaf. A fork's commands would name the main thread's turn,
so Leaf needs a turn per agent before a fork acts on a page itself.
The second step toward a fork that takes the work (notes/fork-replies.md, "A fork
that takes the work"), off unless Claude Code's environment sets
LEAF_FORK_WORK=on. A thread the working-line fork judges `take` stays held
(`forks.TAKEN`, bounded by WORK_BOUND_S) while the hooks module spawns a fork of
the conversation (`$.agent.spawn`) with Leaf's task (`work_prompt`). The module
denies that fork's `leaf` commands, passes it the user's further messages in the
thread (`forks.extend`, `$.session.send`), and at its turn's end hands its answer
to Leaf (`forks.join`), which posts it as the agent's, or gives the thread to the
session on "HAND BACK:" or a failure. The clerk tells the main session as a fork
takes a thread and as it answers or gives it back, since Claude Code tells it
nothing of a fork a module made. A taken thread stays held when the cursor passes
it, and `delivery.deliverable` keeps held input out of the owed messages it
re-offers; with the flag off, neither changes what any reader sees.

`leaf-dev journey claude-code --fork-work` runs it live. On 2026-11-05 a question
the conversation answered had the fork's line 2.1 s after sending and the working
fork's answer at 6.9 s, while the session stayed idle; the session's next turn took
the message in, settled, and didn't answer it again.

A review found TAKEN the wrong primitive: it records who holds a message outside
the log, which only `_held` and `deliverable` read. Once a receipt for later input
moves the cursor past a taken message, the main session's next prompt picks it up
as an acknowledged update it owes, ending the hold and fencing out the fork's
answer; the follower's cache also misses TAKEN changing, and an entry outlives a
fork a reload or exit lost. The note's next step puts the fork's ownership in the
log, as a pickup in a turn of the fork's own, which removes TAKEN, the
posted-for-it answer and the deny.
Main's #5599 removed `clerk.queue`, which had no caller there, but this branch's
prototype of a fork that takes a thread's work (`forks._give`, `forks.join`, behind
LEAF_FORK_WORK=on) queues the main session's lines through it. The merge took the
removal cleanly and left those calls importing a name that no longer exists, so
this restores `queue` and says again, in the `hook_transport` header and in
`clerk.queued` and `delivery.deliverable`, that a fork that took the work queues
lines and holds its thread.
…asn't forked

`leaf-dev timeline PAGE` puts a page's log and diagnostics beside the transcript,
subagents and debug log of each Claude Code session that held it, in one
time-ordered list, with a line per message saying when its fork ran, when the agent's
first words, the session's pickup, start and answer came, and what the pickup waited
on (`response_times.page_timings`). `--message ID` keeps one message's story.

Asking it why two messages on a real page (leaf-issue-triage, 8 Nov) had no working
line found the record too thin to say: the lane's hold ran out at 30 s ("no answer
within 30 s") while the session sat in a 70 s foreground poll, but nothing said
whether a fork was ever asked, and the module's failures go only to a debug log real
sessions don't write. So:

- admission notes whether it held each thread message for a fork and, where it
  didn't, whether the session's lane was up (`fork_hold`, `forks.held_at_admission`);
- `forks.ask` notes each fork Leaf asks for (`fork_asked`), so one that never
  answers shows;
- the module tells Leaf when its own step fails, which hands the thread over at once
  and notes why, rather than leaving the hold to run out unexplained;
- the claude-code journey keeps the session's transcript and its subagents' in the
  run's evidence (`claude/`), where the timeline reads them.
… protocol

A pages follower runs as long as its Claude Code session, and holds the session's
forks lane with a lease naming the page protocol it speaks. A worktree's checkout can
move past a protocol bump meanwhile, and the session's hooks then run the newer Leaf,
which reads that lease as no one's (`leases.lease_speaks_here`). On the
leaf-issue-triage page a follower started on 4 November at protocol 41 was still
running on 9 November, with the checkout at 43: twice on 8 November it held a message
and listed it for a fork, and the fork's step, under the newer Leaf, found it not held
and asked no fork, so the message waited out its 30 s hold with no working line, once
while the session sat in a 70 s foreground poll. `leaf-dev timeline` showed the hold
running out with no fork asked, and the lane file read 41.

The follower now ends, without a last list, once the kernel registry on disk names
another protocol than the one it loaded (`registry.kernel.installed_protocol`, read
each presence interval at about 0.3 ms), and the module starts it again when one ends
cleanly while it listed pages. In a live Claude Code session, bumping the registry's
protocol on disk replaced the follower within 2 s, and a comment sent after it got the
fork's line at 2.4 s.
…cord what the live runs found

The timeline now lists each line Leaf's clerk queued after a delivery, such as a fork
taking or answering a thread, and counts a subagent's tool calls without the call
that spawned it. notes/fork-replies.md records the real pages of 6-9 November and the
follower that outlived two protocol bumps; /developing-leaf points at the timeline for
finding out what happened to one message.
A follower checks the kernel registry on disk each presence interval to see whether
its Leaf moved to another protocol. Where Claude Code removed the plugin version it
was loaded from, or a checkout is mid-write, that read fails: before, the follower
would exit with an error the conversation shows, though a follower started again
from that install couldn't start. It now reads that as no move and keeps running.
…eaf, by an explicit exit

A review found two gaps in starting the follower again across a protocol bump.
The module inferred a renewal from a clean exit while it last listed pages, so a
follower renewed after the session's last page went could start again and again
(the launcher's no-claim gate exits 0 with no list), and one holding only pages
whose servers were down was never renewed. And the follower compared only its own
install's protocol, while Claude Code installs an update into a new directory and
the session can select another checkout: the session's hooks then hand themselves
to that Leaf (`session_runtime.delivering_launcher`), which reads the old
follower's lane as no one's, though the follower's own install never changed.

The follower now ends with EX_TEMPFAIL once the session's delivering launcher is
not its own or its install moved to another protocol, the same test `hooks.main`
makes before it hands a hook over, and the module starts it again on that exit
code alone.
…-dev timeline

The timeline found transcripts and decided each message's fork with copies of what
reply_phases and response_times already do. reply_phases now names the transcript's
path (claude_code_transcript_path), and response_times' fork reading carries Leaf's
reason where it handed a thread over without a line, which the timeline prints. A
journey's debug log goes only to the session the journey ran, and a subagent whose
metadata doesn't say it finished reads as last writing, not ended.
A second review found one more way a follower outlives the Leaf its session runs:
a checkout the session selected is removed while the session still holds a page.
The session's hooks then fall back to the installed copy, while the follower,
running the removed checkout, finds no other selection from where it stands, so it
kept its lane under the old runtime. It now ends for renewal once its own launcher is
gone too; the module starts it again from its own launcher, which hands it to the
Leaf delivering now. This reverses the guard that kept such a follower running: it
assumed a renewal would start from the removed install, but it starts from the
module's. A registry read mid-write still reads as no change.
…kup in the log

The prototype behind LEAF_FORK_WORK=on recorded "a fork has this thread" in a side
record, `forks.TAKEN`, that only `forks._held` and `delivery.deliverable` read. Once a
receipt for later input moved the cursor past a taken message, the main session's next
prompt picked it up as debt it owed, which ended the hold and fenced out the fork's
answer; the follower's cache missed TAKEN changing, and an entry outlived a fork a
reload or a failed join lost (notes/fork-replies.md, step 3).

Leaf now keeps a turn per agent the session runs beside its main thread
(`state.AGENT_TURNS`, a new record kind at protocol 43). The take opens one for the
fork and records its pickup of the thread's waiting messages, with their receipt and
the answer reference a delivery frozen for it gives them, under the hold's fence, so
the page log says who has the message:

- The activity fold reads a pickup or start in a running agent turn as work in hand,
  whatever the main thread's turn does, and lapses it when that turn closes or goes
  unrenewed for the working grace, as a main turn's stamp is believed
  (`activity.running_agent_turns`). The served reading carries the turns
  (`agent_turns`) and stamps their file, so every reader moves when one opens or closes.
- The main thread's Stop hook, its watch's reminders and its prompt's pickup of
  acknowledged debt leave an update a running agent turn holds alone
  (`activity.agent_holds`); its deliveries leave out what an agent took or holds, and a
  receipt passes those inputs (`forks.agent_inputs`, `delivery.receive_passed`).
- The module marks a working fork's `leaf` commands with its agent id
  (`export LEAF_AGENT_ID=…`), so its `leaf thread reply` from that delivery posts as the
  agent's and its starts name its turn (`state.acting_turn`); the deny of its `leaf`
  commands goes.
- A working fork's PostToolBatch, which every other subagent's still skips
  (`state.worker_tool_batch`), renews its turn and hands it the user's further messages
  in the threads it took (`hook_transport.carry_agent_turn`); the module's
  `$.session.send` of them (`forks.extend`) goes.
- As any agent's run ends the module tells Leaf (`forks.ended`), which closes a fork's
  turn: an answered thread is done, and one still owed, given back with "HAND BACK:",
  failed or left, is the main thread's like any update it received, with the fork's
  answer reference. Leaf no longer posts the fork's last message as its answer.

The clerk's lines to the main thread and the hold for the line's fork are unchanged.
Tests: the take is a pickup a later receipt doesn't undo, the fork's own reply settles
the message and its start reads as work, a follow-up reaches the fork at its boundary
only, and a fork turn that ends unanswered, never starts or goes quiet gives the thread
to the main thread; the module test drives a fork through spawn, marked commands and
its end. The prototype tests that asserted TAKEN and the posted-for-it answer are
replaced.
…e module

The first live run of `leaf-dev journey claude-code --fork-work` after the previous
commit had the fork answer with `leaf thread reply` unmarked: Claude Code 2.1.322 runs
none of a plugin's `tool.call` hooks for the calls of an agent that plugin spawned
(`tool.call skipped: re-entry (the plugin's own code raised it; origin leaf)` in its
debug log), so the module's rewrite never ran and the reply acted in the main
thread's turn. The module's `turn.complete` hook does run for the fork, and so do
`hooks.json`'s command hooks, PreToolUse and PostToolBatch, whose payloads name the
agent.

So Leaf's PreToolUse hook, which runs before every `leaf` command, marks the command of
an agent running a turn of the session's own (`hooks.agent_command`): its answer is the
command with `export LEAF_AGENT_ID=<agent>; ` before it as `updatedInput`, with no
permission decision, so the call goes on through the permission flow. Leaf reads which
agents run turns from its own record, so a module reload loses nothing, and the
module keeps no list of forks to mark. The module's rewrite goes, and the test of it
becomes one of the hook's answer.
… fork note

`leaf-dev timeline` now names the fork whose turn a pickup or start is in, from the
take's and start's `fork_reply` records, and the agent that ran a `leaf` command, which
`leaf_command` records now carry where Leaf's PreToolUse hook marked the command
(`interaction_log.record_command`). A fork's own entries list the `leaf` commands it
ran and the deliveries its tool boundaries carried, and a command counts as running
`leaf` wherever the word stands in it, as after a heredoc.

`leaf-dev journey claude-code --fork-work` checks the new path: the fork's turn, not
the session's, picks the comment up, the fork answers it with its own reply, and its
turn ends answered. Two runs on 2026-11-10 passed: the line at 4.0 s and 3.2 s, the
fork's pickup in its own turn with it, its own `leaf thread reply` at 9.9 s and 9.6 s,
the session idle throughout and its next turn carrying the clerk's two lines without
answering again.

`session-lifetime.md` states the agent-turn contract the hooks and the fold rely on,
and the fork note's design and step 3 say what was built, including that Claude Code
runs none of a plugin's `tool.call` hooks for an agent it spawned.
Each renewal rewrites the session's agent-turns record, which every reading of the
session's pages stamps, so renewing at every tool batch of a fork moved those readings,
and woke each page's watchers and tabs, once per batch. A renewal now writes only once
the last is a minute old (`state.AGENT_RENEWAL`), well inside the fifteen-minute grace a
reader believes an unrenewed turn for.
leaf-agent
leaf-agent previously approved these changes Nov 14, 2026
# Conflicts:
#	.claude/skills/developing-leaf/SKILL.md
`leaf page state` now gives each answer the agent owes (`activity.obligations`)
and each thread a `holder`: `{"kind": "main"}` for the session's main thread, or
`{"kind": "fork", "name", "state"}` for the fork that took the thread, by the
name the main thread reaches it by, with whether its turn is running, closed or
lapsed. Before, page state listed a running fork's answer exactly like the main
thread's, so a main thread recovering what it owes after a compaction answered
it too, taking the thread from the fork and giving the user two answers.

`holding.holder` is the one place a turn is named; the activity fold computes it
for every owed answer and thread, and `activity.agent_holds`, which keeps the
Stop hook and the watch off a running fork's work, now reads the fold's holder
instead of recomputing the running set. `activity.agent_turn_state` is the one
reading of an agent turn as running, closed or lapsed, which `leaf-dev timeline`
uses too, and the taken_over diagnostics note names the taker through `holder`.
`fork_name` moves to `holding.py`. /api/state leaves the thread holders out.

The new `recovery-leaves-a-forks-answer` eval case scores it: with `holder`, the
agent left the fork's answer 3/3 on head and 3/3 on base; without it, 0/3. The
field alone does it, so `conversation-loop.md` gains no sentence.

TODOs mark the next steps: caller identity for unmarked `leaf` commands
(`state.acting_turn`), worker subagents' turns (`holding.holder`), and the panel
showing which fork works a thread (`served_state.page`).
leaf-agent
leaf-agent previously approved these changes Nov 14, 2026
The holder change added `holder` to each thread page state lists; this test of the
default snapshot's thread shape now expects it.
leaf-agent
leaf-agent previously approved these changes Nov 14, 2026
… read

An independent review of the holder change found two gaps. An owed answer's
`holder` named the turn that last took it, so an answer a fork gave back read
`fork … closed` while the Stop hook held the main thread over it; it now names who
owes the answer now, a fork while its turn runs and otherwise the main thread,
which is the rule `activity.agent_holds` keeps. And it sat in
`activity.obligations`, which the selection `conversation-loop.md` gives
(`{source, obligations, todos, queues}`) leaves out; it now sits on each
obligation record, null where the agent owes nothing. A thread's `holder` still
names the turn that last took it, with whether a fork's turn runs, closed or lapsed.

The eval case now reads that selection: the agent left the fork's answer 3/3 with
`holder` and answered it 0/3 without. The fold reads the log's rounds once for both
holder readings, `_taken_by` takes the stamps `ended` already read, and two locals
named `holder` in `holding.py` are renamed. The docs no longer claim every report
names a turn as `holder` does; a TODO in `holding.py` names the deeper step, one
reading of who answers each message that the hooks, delivery and page state share.
@max-sixty
max-sixty merged commit 0f1c401 into main Nov 14, 2026
5 checks passed
@max-sixty
max-sixty deleted the send-pickup-latency branch November 14, 2026 23:28
max-sixty added a commit that referenced this pull request Nov 15, 2026
main's fork work (#5961) reads an agent's own turn in `activity`. Its additions
are threaded through the branch's standing fold: `_stand_workflows` and
`_stand_todos` take the running agent turns, an update an agent's running turn
holds is in hand for that turn, and each owed answer names its `holder`. The
fork's ending reads a to-do by the obligation record it now is, and three fork
tests read `state` and `reason` where they read `stage`, `kind` and
`running`.
max-sixty added a commit that referenced this pull request Nov 15, 2026
…n session on a fork's thread say so (#6005)

Follow-up to #5961, which let a fork of a Claude Code conversation
answer a thread in a turn of its own. This PR lands three decisions from
that work.

## One reading of who holds each thread

Before this change, who held a waiting message was worked out in several
places. `holding_turns` and `last_taken` read pickups and answers.
`forks.kept_back` was a second reading of the messages kept back for the
line fork. Callers then built their own sets from those:
`kept_from_main`, `agent_held`, `left_by_agent_turns`, the fold's
`owes()` and `thread_holders`, and `forks.forked`, and each walked the
log again. They disagreed in page state. A thread kept back for the line
fork printed `main` as its holder, both on the thread and on its owed
answer, for up to 30 s.

The rule's unit is the thread: a turn that picks up any of a thread's
waiting messages takes the whole thread. So `holding.reading()` now
builds one `Holding` per page operation, with a `Hold(turn, picked,
holder)` for each thread. The holder is the line fork while the thread
is kept back, else the turn whose pickup of a waiting message is newest,
else the thread's last taker. Every rule asks that reading:
- delivery and admission;
- the fold, and through it the Stop hook (`activity.agent_holds`) and
page state, which prints the reading on each thread and, through
`owed_by`, on each owed answer;
- the fork rules: `pickable`, `to_carry`, `refuse_unheld`,
`taken_by_agent_turns` and `forks.ended`.

It replaces `holding_turns`, `last_taken`, `newest_pickups`,
`picked_in`, `turn_threads`, `running_threads`, `forks.forked`,
`forks.kept_from_main`, `holding.agent_held`,
`holding.left_by_agent_turns` and the fold's own composition.
`deliverable` reuses the fold's reading, so the Stop hook reads holders
once. In the served state, `kept_back` replaces `forked` as the one
presence field, and On it is `Holding.forked()`.

The only intended behavior change is that a kept-back thread's holder
reads `{"kind": "line"}`. An independent reviewer ran the merge base's
code beside this branch's at every call in the fork, hook, clerk,
delivery and stop-guard suites. The results matched in every call
(`deliverable` 372/372, `turn_obligations` 257/257, `pickable` 225/225,
refusals 309/309, among others), and holders differed only for the 127
kept-back threads.

## A command acting as the main thread on a fork's thread says so

A `leaf` command that no agent id marks acts as the main session: the
main session itself, a fork's own script, a worker subagent, or a
terminal. The question was what such a command should do with a thread a
running fork holds. The decision: let it go through, keeping the main
session's authority, and warn that the thread has a holder.
`holding.authorize` now returns an advice finding, `fork-holds-thread`,
which names the fork. On an answer, which takes the thread, it also says
the fork's later writes there are refused and how to stop the fork; on a
working message or a start, it says the command acted beside the fork.
It covers a writer outside the session and a start on a to-do in the
fork's thread. `leaf task end` takes nothing a fork holds, so it isn't
warned.

## Worker subagents no longer told to leave the page alone

The decision: shipped instructions should say only what we have a strong
view on. So `conversation-loop.md` no longer tells worker briefs to keep
off the page. The Claude Code contract keeps one line with a mechanism
behind it: a subagent's `leaf wait` takes the main thread's input. On
`parent-keeps-leaf-responsive` (Claude Code, 3 runs each), head and base
both passed 2/3, and both misses were the parent doing a setup check
itself. A TODO in `holding.holder` says what to build if workers come to
act on pages.

## Testing

- Focused model suites: forks, activity, hooks, delivery, todos,
threads, session lifecycle, interact, workflows, stop guard, clerk,
claims, Codex adapter, folds and obligations.
- New tests, each checked to fail without its behavior:
  - a kept-back thread reads `line` on the thread and on its answer;
  - a message beside a fork's answer leaves the fork holding the thread;
  - a command from outside the session is warned;
  - a start on a to-do in a fork's thread is warned;
  - Leaf's own line post prints no warning.
- `pnpm run check` passes.
- `leaf-dev journey claude-code --fork-work` passed all 8 steps on
Claude Code 2.1.325. Working messages came at 2.6–5.1 s, a running fork
answered a follow-up 13.2 s after it was sent, and a resumed fork
answered at 13.9 s.
- Reviews: Codex and three Fable passes.

## Size

Production files (`src`, `dev`) are +691/−529. Docstrings and comments
account for about +65 of the net, code for about +92, and the warning
for about 45 of those code lines. The change removes no lines overall.
What it removes is eleven helpers, each of which composed its own answer
to who holds a thread.

> _This was written by Claude Code on behalf of max-sixty_
leaf-agent added a commit that referenced this pull request Nov 15, 2026
…sure cards and acked undo (#6046)

Four tests in [scheduled run
43706054091](https://github.com/max-sixty/leaf/actions/runs/43706054091)
still expect what the page did before a merged change. Each change moved
the interface on purpose, and this PR updates the tests to the new
behavior. No product code changes.

- **`test_an_agent_reply_can_move_a_thread_to_its_revised_visual`,
`test_an_agent_reply_can_remove_a_subject_and_detach_its_open_thread`**
(both platforms): #5961 added `holder` to each thread record in `page
state` and updated the model tests, but not these journeys. They now
expect `"holder": {"kind": "main"}`.
-
**`test_complete_views_restore_native_values_in_wrapped_layouts[1200|390]`**
(both platforms): #6040 names a playground control with
`aria-labelledby` (the textbox is still named "Title"). The test looked
for `input[aria-label="Title"]`, which no longer matches, so it now uses
`get_by_role("textbox", name="Title")` like the rest of the test. #6038
moved this test into the journeys tier, so #6040's own CI didn't run it.
- **`test_strokes_join_one_drawing_until_it_is_sent_and_escape_leaves`**
(both platforms): `git bisect` names #5846 as the first bad commit. That
change opens the comment box and the sent thread's card at the full
measure. In the test's window, the box (x 382–946) and then the card (x
368–960) cover `#fig`, where the test's strokes began. The press landed
on the box's text field, so no second stroke was drawn. The two strokes
over the figure now start at its left edge (x ≈ 262–348). They still
begin over another element, which is what the test checks.
- **`test_resolving_a_thread_from_threads_moves_on_to_the_next`** (macOS
only): it pressed `z` as soon as the Resolve was acked, before the page
held the server's reading. That's #5941's mechanism, which #5943 fixed
in the sibling tests. It now uses `undo(page)`, which waits until the
dispatcher would take `z`.

**Verification.** On main (`9f3c6590b`), the five Linux-reproducible
cases fail before this change and pass after it. The edited modules pass
in full, along with `tests/model/test_suite_source.py`: 1,225 passed.
The resolve test passes on Linux both before and after. Its macOS
evidence is the next scheduled `test-macos` run.

This leaves the run's other new failures to their own owners. Those are
listed on #4225.

Refs #4225 — automated triage

Co-authored-by: leaf-agent <[email protected]>
max-sixty added a commit that referenced this pull request Nov 15, 2026
…ookup in a file the session just changed (#6018)

Two follow-ups from the forks work (#5961, #6005): Leaf now addresses
each Claude Code fork by its agent id rather than a per-thread name, and
the line-fork eval gains a case for a lookup in a file the session just
changed.

## Forks are addressed by agent id

Leaf used to spawn each working fork under the name `leaf-<thread>`. The
main session was told "to steer or stop it, SendMessage leaf-<thread>",
and page state and the `fork-holds-thread` advice named the fork that
way. Leaf spawns a second fork under the same name when the main session
answered a thread last and a later message there goes to a fresh fork,
and across a session's generations. A probe on Claude Code 2.1.325 found
what then happens:

- `$.agent.spawn` accepts the reused name, and the newest fork takes it.
- If the main session had already sent to the earlier fork, a send to
the name is refused ("now resolves to a different agent … Nothing was
sent").
- If it hadn't, a send meant to stop the older, still-running fork
reaches the newer one.
- After `claude --resume`, a send to the name reaches nothing.
- A send by raw agent id always reaches its agent: it resumes an ended
one, queues for a running one, and works across generations.

So the module now spawns forks unnamed, and `fork_name` is gone.
Everything that tells the main session how to reach a fork names the
agent id: the clerk's take line, the line at a fork's end, the advice
beside a main-session command on a fork's thread, and page state's
holder, which is now `{"kind": "fork", "agent": <id> | null, "state"}`.
A new fork's id exists only once its spawn returns, so its take line
moved from `forks._take` to `forks.started`; a resumed fork's id is
known at the take. `state.agent_turn_stamps` and the served
`agent_turns` carry the agent. The probe's findings are recorded in
`notes/fork-replies.md`, and `docs/forks.html` gives the reason in
"#telling-address".

One window is left: if the main session takes a thread over in the
second before a new fork starts, `started` still queues "leave that
thread to it". The fork's own writes are refused all the same.

## Line-fork eval: a lookup in the page's source

In 20 live journey runs, the line fork judged "How many cards does this
page's source hold? Count them in the file" `take` 17 times and
`hand_over` 3 times. Each time, the session had just recorded a release
in the page's source. The `line-fork` cases never had such an edit, so
they didn't measure this. There are two new variants:
- `source-edited` (label `take`): the session has finished its edit and
ended its turn. It scored 13/15.
- `source-editing` (label `hand_over`): the session is part way through
editing that file. It scored 14/14.

Read as written, the take criterion agrees with these labels, so the
prompt is unchanged.

## Testing

- Focused model suites: forks, hooks, clerk, activity, delivery, todos,
interact and workflows; the Claude Code module process test; the
timeline dev test; and the served-state schema test.
- Each changed test fails on the old addressing.
- `pnpm run check` passes.
- `recovery-leaves-a-forks-answer`, which reads the holder's new shape,
still leaves the fork's answer alone 3/3.
- `leaf-dev journey run claude-code --fork-work` passes every step:
`fork-idle`, `fork-follow-up`, `fork-resume` (the follow-up reached the
same fork by its agent id), `fork-todo` and `fork-work`. The journey now
reads a fork's agent from its start record, since a new fork's take no
longer carries it.

> _This was written by Claude Code on behalf of max-sixty_
max-sixty added a commit that referenced this pull request Nov 17, 2026
…e length (#6317)

The Forks page (`/forks/`) was a chapter of How it works at its own
address: it called itself "how it works", linked back there, and was
split off only because #5961 gave a half-built design a page of its own.
This moves it into How it works as a **Forks** tab between Threads and
Keys (`/how-it-works/#forks`), deletes `docs/forks.html`, and rewrites
the tab for a reader of that guide at half the length (about 4,900 words
to 2,500).

The tab describes only what is built. The planned parts (the three-way
sort, a to-do for significant work, a standing fork per session) move to
`notes/fork-replies.md`, step 4. It opens with why forks exist, as three
metric tiles: the session's own first words in a thread take a median 35
s and 140 s at p90 on real pages, against 2.4 s for a fork's working
line. Terms are defined where they first appear instead of in a
glossary. One diagram draws the three states a waiting thread is in, as
`forks.py`'s header names them, where two overlapped. Diagrams that
restated a table or paragraph are gone, the endings flowchart included,
whose table now reads in the order Leaf applies it. Function names move
to "Where each rule is written". The tab says "working line" as Workflow
and Progress do. It carries the summary fork that #6318 added to the old
page while this was open. The reasons for telling the main thread in
queued lines rather than the fork's SendMessage move into `forks.py`'s
header, which already held the tab's other decision records.

Worth a look: the tab strip, whose tenth tab puts Runtime behind the
overflow chevron at a 1440 px window, and whether the planned parts
belong in the note rather than on the site.

Max asked why Forks wasn't in How it works, then for the tab to be
improved. Two fresh reads of the rendered tab checked its claims against
`forks.py` and `holding.py`, and their findings are fixed.
`tests/model/test_product_page.py` and `tests/model/test_forks.py` pass;
the tab's three diagrams draw at 1440 and 390 px with no sideways
scroll, broken anchors or duplicate ids.

> _This was written by Claude Code on behalf of max-sixty_
max-sixty added a commit that referenced this pull request Nov 18, 2026
Main's #5961 already registers session.append on response and tool-result
and leaves out queue and notice. What this branch still changes is
dropping tool-message, whose Skill bodies cc-plugin-sec-default sends past
a person's plugins on a machine with managed settings. The test's fixture
now appends a tool-message row, which must not move the cut.
max-sixty added a commit that referenced this pull request Nov 18, 2026
…end doors (#5816)

The Claude Code module's `session.append` handler no longer registers on
the `tool-message` door. On a machine with managed settings, Claude Code
seats `cc-plugin-sec-default` outermost, and that plugin sends the body
a Skill call loads, a `tool-message` row, past a person's plugins.
Claude Code then told the user, at each session's first Skill call: `its
session.append hook was skipped. "cc-plugin-sec-default" runs first and
sent the event on past it.`

The rest of the door list stays as #5961 left it: `prompt`, `command`,
`response`, `tool-result`, `delivery` and `note`.

**Behavior change:** a fork's diagnostic `row` no longer advances on
`tool-message` rows, the messages a tool adds beside its result; it
stays on that result until the model next answers. `row` feeds only a
fork's diagnostics.

**Verification:**

- Claude Code 2.1.327's `cc-plugin-sec-default`, read from the binary,
sends past a person's plugins every `attachment`, `hook-context` and
`compaction` row, a `tool-message` row from the Skill tool, and a `note`
an organization's plugin appends.
- In a Linux container with `/etc/claude-code/managed-settings.json`,
where Claude Code 2.1.327 logged `cc-plugin-sec-default@builtin seated
outermost: this machine has managed settings`, a headless session
against a scripted Messages API called Skill once. With main's hooks
module loaded by `--plugin-dir`, the debug log had one `leaf:
session.append bypassed by cc-plugin-sec-default` and the session showed
the notice; with this branch's, neither. The container had no
organization plugin, so it doesn't exercise the `note` case; this wasn't
run on the managed machine where the notice was reported.
- `tests/process/test_interact_claude_code_module.py` passes (21 tests).
Its fixture now appends a `tool-message` row that must not move the cut,
and putting the door back fails 3 of them.

> _This was written by Claude Code on behalf of max-sixty_
max-sixty added a commit that referenced this pull request Nov 19, 2026
…context and request

The line fork's prompt now opens by saying what Leaf asks of the fork: the working
line it posts at once, and who should do the work, a fork beside the main session
or the main session itself. The order after that is the rule against tools and
deliberation, the running call and the thread's JSON, then the line, the take
criterion and the JSON answer.

The prompt used to say "The main session takes the message from here and does what
it asks", which was written before a fork could take work (#5961). It stood just
above the question of whether a fork should take it. It is gone, and the line now
stays "until the agent's answer replaces it", whoever answers.

Max asked for the reorder after reading the prompt as a fork receives it: "at no
point is it completely obvious ... that the goal is to give an ephemeral line and to
decide whether a fork or the main thread should own the subsequent work".

Scored with `leaf-dev eval run 'line-fork*' --harness claude-code --repeat 5 --base
HEAD`: base 43/45, head 44/45 ($4.52). Every `work` judgment was right on both
arms. All three misses were `line-right` on why-ci, where the line gave away the
cause ("with the -n 8 vs serial runs"): two on base, one on head.
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