Skip to content

Route every thread message through the main session, which spawns the forks - #6574

Merged
max-sixty merged 9 commits into
mainfrom
agent-a1541fefc21d089a6
Nov 20, 2026
Merged

max-sixty merged 9 commits into
mainfrom
agent-a1541fefc21d089a6

Conversation

@max-sixty

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

Copy link
Copy Markdown
Owner

Max chose this on the forks decision page and in its thread: every thread message goes to the main session, and the session decides whether to answer it itself, give it to a new fork of its conversation (Agent tool, fork type), or SendMessage it to the fork already answering that thread. The line fork only writes the working line; its take/hand_over judgment and the queued line it fed (forks.beside_line) are gone, since that line only told a session that decides anyway. The routing guidance is in skills/leaf/references/harness-claude-code.md, "Subagents". This also deletes the Leaf-spawned working fork: the hooks module's spawn/send and $.state records, forks.py's take/resume/start/end steps, fork turns in state.AGENT_TURNS (now line turns only, since 51), leaf --turn, the subagent tool boundary, and the fork holder. Production code nets −1,293 lines and tests −1,749.

Worth weighing: nothing marks which messages could run beside the session, so how often it delegates rests on its judgment, the risk Max accepted. A message given to a fork waits for the session's next tool call or wake before the fork starts. While a fork works, a later main turn is reminded of the message as owed and told to start it again. Since a line turn holds a thread only while it runs, holding.owed_by folds into holder, and leaf page state names a line holder {"kind": "line"}. How it works' "Anatomy of a fork journey" shows a journey run recorded under this design, message by message: the session gives the question to a fork and passes the follow-up to the same fork by SendMessage.

Tests: focused model and process suites pass. evals/beside-work-goes-to-a-fork, now on a plain delivery, passes 6/6 on head for a busy session's research question (0/6 on #6574's first instructions) and 6/6 for a later message, each sent to the thread's fork. leaf-dev journey run claude-code passes all 14 steps: the session gave both the fork-busy question and its follow-up to a fork, the follow-up by SendMessage to the same one.

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

The line fork still writes the working line and judges each thread message. A
`take` now hands the thread to the main session by the same path as `hand_over`,
with one line queued before it (`forks.beside_line`) saying a fork of the session
could do the work beside it. The main session starts the message and spawns a
fork of itself in the background (Claude Code's Agent tool, `fork` type), which
answers in the thread with the delivery's `answer.ref`; the session passes the
thread's later messages on to that fork with SendMessage
(`skills/leaf/references/harness-claude-code.md`, "Subagents").

Max chose this on 19 November 2026 as the simpler approach for now. In Leaf's
records from the 20 hours after Leaf-spawned working forks became the default on
18 November, the line fork judged 135 messages and promoted 30. A working fork
answered a median 84 s after the message; a subagent spawned at the main
session's next step would answer about 21 s later. The distributed approach,
Leaf spawning the fork itself, stays the alternative if that latency matters:
`notes/fork-replies.md` at this commit's parent holds the measurements of what
`$.agent.spawn`, `$.session.send` and Claude Code's notifications give a module.

Removed with the working fork:
- the hooks module's `spawn`/`send` instructions, its `$.state` fork records and
  their end reports, and `hooks/leaf-state.d.ts`;
- `forks.py`'s take, resume, start and end steps and the working fork's prompts;
- fork turns (`role`, `agent`, renewal) from `state.AGENT_TURNS`, which now holds
  line turns only (since 51), with `leaf --turn`, `leaf_invocation`,
  `agent_turn_of`, `name_agent_turn` and `renew_agent_turn`;
- the subagent tool boundary that carried a fork's further messages
  (`hook_transport.carry_agent_turn`): every subagent's PostToolBatch is skipped;
- the fork holder and its states in `holding.py`: a thread's holder is the main
  thread or a running line turn, and `owed_by` folds into `holder`, since a line
  turn holds a thread only while it runs; the served reading's `agent_turns`
  carries only running line turns;
- the clerk queue's turn-bound lines (`unqueue`, since 51);
- the eval `recovery-leaves-a-forks-answer`, whose fork holder no longer exists.

Behavior changes users see: a promoted message reaches the main session at its
next tool call or wake, rather than a fork Leaf starts at once; `leaf page state`
names a line holder as `{"kind": "line"}`, with no `state`; `fork-holds-thread`
speaks only of a line fork.

Scored with `evals/beside-work-goes-to-a-fork` (new), head against the merge base,
3 runs each, on the final line text: the first message is started and given to a
background fork 3/3 on both arms; the thread's later message goes to the same
fork 3/3 on head and 0/3 on base. With the line's first wording, which didn't say
to start the message, base used the fork type 1/3 and passed the later message
on 2/3; head 3/3 and 3/3. A head rerun with the reference's goal clause passed
6/6. The `claude-code` journey's fork steps now run the new path while the
session runs the user's own turn: the session starts the message, spawns a fork
that answers it, gets Claude Code's notice of the fork's end, and passes the
follow-up to the same fork; an idle session asked for a one-step count did it
itself both times it was asked, faster than a fork would have.
The anatomy and `dev/leaf_dev/fork-journey.json` now come from the passing
`leaf-dev journey run claude-code` at 19:49 PT on 19 November 2026 (Claude Code
2.1.329): the line fork's line 4.3 s after the message, the hand-over at the
PostToolBatch after the user's 25 s command, the main thread's `leaf task start`
and Agent call, the fork's answer at 44.4 s, Claude Code's notice of its end
starting a turn, and the follow-up the main thread passed on with SendMessage,
answered 43.2 s after it was sent.

Also: the holder prose in `session-lifetime.md`, `agent_state.py` and
`holding.authorize` says a holder is the main thread or a running line turn, as
`holding.holder` now reports it. A probe on Claude Code 2.1.329 confirmed that a
fork-type subagent's PostToolBatch payload names it (`agent_id`, `agent_type:
"fork"`), which `state.worker_tool_batch` skips, so no delivery reaches the fork.
leaf-agent
leaf-agent previously approved these changes Nov 20, 2026

@leaf-agent leaf-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This changes the premise of backlog #6547 ("Run Leaf's forks in Codex and Pi"). Its "Both" bullet asks each harness for a way to start a working fork, and its summary lists the carrier's spawn and send. This PR removes both: the main session now spawns its own subagent. Should #6547 be narrowed to the line fork, summary fork and turn-end check, or named here as Part of #6547?

…ead's own

Review of #6574 found Leaf's queued line told the session to spawn a fork even for
a later message in a thread whose fork already answered, where the reference says
to send it to that fork; the line now says to give the message to a fork, and that
it doesn't start the message, in place of the undefined "takes nothing in hand".

The reference paragraph now says the fork answers the message itself unlike a
worker that reports back, and points the start at conversation-loop.md's "Long-running
work", which owns starting a worker's update.

Removed what the deleted working fork left: `_start_age`'s branch for a start an
agent turn wrote (a line turn's receipt writes none), the unused holder value on
`fork-holds-thread`, and "fork turn" in the timeline's docstring.

beside-work-goes-to-a-fork, both variants, 3 runs each on the new wording: 6/6.
leaf-agent
leaf-agent previously approved these changes Nov 20, 2026
The line fork answers only the working line now. Leaf posts it and hands the thread to
the main session, which decides whether to answer the message itself, give it to a new
fork of its conversation, or send it to the fork already answering that thread.

Max chose this over #6574's first design, where the line fork also judged `take` or
`hand_over` and Leaf queued a line telling the session a fork could do the work
(`forks.beside_line`). Under that design the judgment only added a line to what the
session read, and the session decided anyway; the session knows its own work better
than a fork judging from a snapshot. The risk he accepted: nothing marks which messages
could run beside the session, so how often it delegates rests on its judgment.

Removed: `ANSWER`'s `work`, the line prompt's paragraph on who does the work,
`beside_line` and the queue it fed in `_hand_over`, the `work` field in `fork_outcome`
records and every reader of it (the journey's checks and fork summary, `logs
timeline`, `logs response-times`' `fork_take`/`fork_hand_over`, now one `forked`,
`page_timings`), the fork eval's `judged` check and `work` labels, the
`source-editing` line-fork case that only contrasted a judgment, the How it works
prompt slot for the queued line, and the test that held How it works' sorting table to
the judgment enum.

The Claude Code reference's "Subagents" now states the goal (the user hears back soon
while the session's own work goes on) and the considerations the line prompt's `take`
test carried, for the session to weigh. How it works' Forks tab and the forks.py header
describe the session routing every message, with Leaf spawning the fork as the
alternative and its cost.

Behavior changed: the journey's `fork-follow-up` step accepts a new fork as well as
the thread's own, since the reference lets the session choose.

Scored with evals/beside-work-goes-to-a-fork on a plain delivery (no beside line),
base 87746b1 (#6574's instructions) against head, 24 trials, about $7:
- busy session, research question: base 0/6; head 2/3 on a first wording, then 6/6
  on the final one, which adds that the fork's answer in the thread carries what it
  found (the failing run kept the work because answering seemed to need a page edit);
- later message in a thread a fork answered: base 3/3, head 6/6, each by SendMessage
  to that fork.

@leaf-agent leaf-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The PR description still describes the design the last commit removed: it says the line fork judges take and Leaf queues a line saying so (forks.beside_line), and "the session passes the thread's later messages on to that fork with SendMessage". The head has no take, no beside_line, and lets a later message go to the same fork or a new one. Since merging squashes with the description, main's commit message would state a mechanism the code doesn't have; worth rewriting it to the current design (the main session routes every message, the line fork answers only the working line).

Comment thread docs/how-it-works.html Resolved
…st descriptions

The independent review of the routing change found:
- the journey's `delegated` accepted any subagent that answered, so a follow-up given
  to a fresh general-purpose agent passed `fork-follow-up`; it now requires a fork of
  the session's conversation;
- How it works, `review_scenario`, `notes/fork-replies.md`, `hook_transport`'s queue
  comment, `evals/README.md`'s fork_eval row and a `test_forks` docstring still had the
  line fork judging who does the work;
- How it works' anatomy shows a run recorded under the line fork's judgment; it now
  says which parts it leaves out.

`fork-follow-up`'s message now asks for its work beside the session's own and for a
card the first answer didn't rank. With "And which would they ask about next?" a run's
session answered it itself from the subagent's report, which the reference rightly
allows, so the step failed on the session's sound choice rather than on Leaf.

Journey (claude-code): all 14 steps pass. An earlier run on this branch failed
`boundary` (the comment Escape kept went unanswered in 150 s), the step's known flake
without the sandbox; it passed on the rerun.
@max-sixty max-sixty changed the title Send promoted thread messages to the main session, which spawns the fork Route every thread message through the main session, which spawns the forks Nov 20, 2026
leaf-agent
leaf-agent previously approved these changes Nov 20, 2026
…uting

"Anatomy of a fork journey" and `dev/leaf_dev/fork-journey.json` now come from
the passing `leaf-dev journey run claude-code` at 22:07 PT on 19 November 2026
(Claude Code 2.1.329, `.tmp/journey/run-20261119-220756-w5ofr6md`), recorded on
cfe5067 with 091beb9's journey changes in the working tree. Its line fork
answered only `{"line"}`, and the main thread routed both messages itself: for
`fork-busy`'s question, the line 1.7 s after the message, the hand-over at the
PostToolBatch after the user's 25 s command (25.1 s), `leaf task start` (29.4 s),
the Agent call with the `fork` type (35.6 s) and the fork's answer (44.2 s); for
the follow-up, now "which card would they ask about last, and why?", a line fork
again, the hand-over at 25.3 s, ToolSearch for SendMessage, SendMessage to the same
fork (35.4 s) and its answer (47.3 s). The lede drops the sentences saying the run
predated the routing change and what the section left out.

The times, Claude Code's text and the models' words are as the run recorded them.
Leaf's two messages are written by the current code from the run's values: the
hand-over delivery it renders is byte for byte the context the run's PostToolBatch
hook carried. The line fork's batch is that delivery's, less the thread's title and
working line, which the log shows were written after the line fork was asked.

The newer runs beside it were not this design's passing run: 21:51 recorded no
sample and sent the old follow-up ("And which would they ask about next?"), and
22:04 failed `fork-follow-up` when the session answered that follow-up itself.

The line fork's timeline events are renamed `journey-write-line`, since the line
fork no longer judges.
leaf-agent
leaf-agent previously approved these changes Nov 20, 2026
max-sixty added a commit that referenced this pull request Nov 20, 2026
Rewrites the root `AGENTS.md` so a reader who has only the file can
parse each sentence on the first read. Below "Repository map" no rule
changes: pronouns give way to their nouns, a rule is split from its
mechanism and its reason, and Tend and the server's "reading" are
defined where they first appear. "Keep the layer open" now defines a
content widget, says what core code may not do, and limits the rule
against naming widgets to Leaf's own instructions (`SKILL.md`, its
references, and the entries in `browser/registry.json` and the kernel
registry), leaving a package's guidance free to name any widget, as the
maintainer decided. The defects a review reports in instruction text
become a list, with a new item for a sentence its reader can parse only
on a second read, and Tend's review threshold names that list instead of
the label "public surface traps". In the maintainer's sections above
"Repository map", only the three readings the maintainer picked change:
when Leaf adds code to prevent a failure ("Leaf's role"), that contract
prose may be edited for clarity ("Stage"), and that the code making a
change records it ("Responsiveness").

**To weigh:** Fresh readers given only the file flagged 60 and 72 of
about 250 sentences as needing a second read on `main`; on this branch
two readers of the draft flagged 31 each and a reader of the final text
28, none unsettled. Below "Repository map" that is 41 and 53 down to 13,
13 and 8. Most of what remains sits in the maintainer's sections, whose
proposed rewrite (9 flags in one read) waits on the maintainer. Asked
ten questions about rules the rewrite touched, fresh agents given only
the old file said it left the question open in 6 of 30 answers, and
given the new file in none; every new-file answer matched the code.
Codex compared old and new for changed rules and false claims, and its
eight findings on the first draft are fixed. The file grows from 5,538
to about 6,270 words, since a sentence that names its noun and splits
off its reason is longer. A full-file rewrite conflicts with the open
drafts that edit this file (#6023, #6024, #5979, #5970); it leaves the
`hooks/` line that #6574 edits alone.

Asked by max-sixty, who picked the three readings on the survey page; he
hasn't read this diff. Prose only; pre-commit passes.

> _This was written by Claude Code on behalf of max-sixty_
Main moved the page protocol to 52 (#6581), so the agent-turns and clerk
records this branch reshapes move their since to 52.
@max-sixty
max-sixty merged commit b6ce17e into main Nov 20, 2026
6 checks passed
@max-sixty
max-sixty deleted the agent-a1541fefc21d089a6 branch November 20, 2026 07:27
max-sixty added a commit that referenced this pull request Nov 20, 2026
…e protocol 53

#6581 took protocol 52 and #6574 removed the working forks. forks.py takes main's
version, since the working-fork code this branch had edited is gone; events.md keeps
main's pickup row and this branch's receipt row.
max-sixty added a commit that referenced this pull request Nov 20, 2026
…line fork fares (#6592)

A thread message Leaf keeps back for a line fork goes to the main
session when the line turn ends. When the line fork answers within 30 s,
the hand-over nudges a session no watch follows; when it answers later
or never, the line turn lapses, which writes nothing, and nothing
nudged. A session whose turn its Stop hook closed and whose watch had
gone (failed, or past its hook's timeout) then kept the message until
its next prompt. An interrupted session was already woken at the lapse
by the page server's interrupt nudge.

The page server's row producer already wakes at each transition the
activity fold names, a line turn's lapse included. It now nudges at a
transition whether the main turn was interrupted or closed
(`presence.nudge_interrupted` becomes `presence.nudge_left`).
`nudge_unwatched` still sends nothing to a session a watch follows, and
at most one nudge per turn ending.

Worth weighing: this reverses a cost #6156 accepted ("A line turn that
lapses nudges no one"). A transition that falls between a Stop and its
watch taking the lease wakes the session twice, as input admitted in
that moment already does.

Asked for in Max's session after a Codex review of #6574 found the gap;
Codex also reviewed this diff.

Tests: a new model test fails on main and passes here; the in-time
hand-over still nudges once; focused model and process suites pass.

> _This was written by Claude Code on behalf of max-sixty_
max-sixty added a commit that referenced this pull request Nov 20, 2026
…de it (#6616)

A Claude Code thread message now reaches the main session at once,
delivered and nudged as any input is, as in Codex and Pi. The line fork
runs beside the delivery and posts the agent's working line when it
answers. Until now Leaf held the message from the session while the line
fork wrote, for up to 30 s.

Max chose this on 20 Nov 2026. The hold came with #6156, when the line
fork also judged whether a working fork should take the message, and the
session had to wait for that judgment. #6574 removed the judgment, which
left the hold buying only that the line went in before the session's own
words. This reverses the decision `forks.py`'s header recorded, and
deletes the hold and everything that existed only for it.

## What holds now

- **Which messages get a line fork.** The pages follower lists one for
each *round*: an open thread whose waiting messages have nothing of the
agent's after the oldest of them, read as the round stood when its
newest message arrived. A message that joins a round before its line
gets a line fork about all of them. One that joins after the agent spoke
in the thread gets none. The follower lists only rounds newer than its
first look at a page, and the module runs each id once, so a round gets
at most one line fork, across a module reload too.
- **When the line goes in.** Only while its round still awaits it: the
thread's waiting messages are the ones the line fork read, and the agent
has said nothing there since, by a message or a rewrite. Leaf checks
this under the lock the append takes (`thread.Receipt`'s `fence`), so a
line never lands after the session's words, nor speaks for a round a
later message joined. A line that fails the check is recorded as
`dropped`.
- **On it.** Waiting messages read On it once the line stands, until the
session answers, as before. Before the line lands they read as their
delivery does: Queued, then Delivered.
- **The summary fork** still runs beside each listed round, even where
the agent spoke first. A delivery leaves the summary hint to it by the
same reading.

## Deleted

`holding.py`, line turns (`state.AGENT_TURNS` and its helpers), the
`held` pickup phase, `activity.STARTING_GRACE` and the lapse, #6592's
lapse nudge, admission's keep-back, the hand-over, the follow-up that
replaced a line turn, delivery's branch for input a line turn kept past
the cursor, `fork-holds-thread`, and `leaf page state`'s `holder`.
Production code nets −1,079 lines, tests −874, `leaf-dev` −119.

## To weigh

- The page protocol moves to 53, because the pickup's phases lose
`held`.
- For the second or two before the line lands, a message reads Queued or
Delivered rather than On it.
- A message the user sends before the pages follower first reads the
page gets no line. As before, the session gets it anyway.
- Whether a delivery carries the summary hint now turns on whether the
follower holds the session's lane when the delivery is built
(`forks.lane_up`), rather than on a record in the log.
- `leaf-dev eval run 'line-fork*' --base` can't compare across this
change, since its scripts call the new `forks.look` under the base arm's
Leaf too.

How it works' Forks tab describes the new flow, and its "Anatomy of a
fork journey" is re-recorded from the journey run on this branch.

Also fixes two tests #6573 left failing on main: the third delivery that
`evals/beside-work-goes-to-a-fork` pastes moves to `leaf-delivery/52`,
and
`test_an_agent_reply_can_remove_a_subject_and_detach_its_open_thread`
expects the thread's new `rewritten_from`.

Tests: model and process suites for the touched modules pass. New tests
cover delivery at once, a joining message, the agent speaking first
(answer, message, progress, rewrite), the race under the lock, a
repeated step, and the summary fork's ownership. Each new guarantee has
a test that fails without its fix. `pnpm run check` passes. `leaf-dev
journey run claude-code` passed all 14 steps on the final code. The line
was in the thread 1.6–2.0 s after each message (the line fork itself
took 1.3–2.0 s), and an idle session's prompt took a message 0.2–0.4 s
after it was sent. Before this change it waited for the line. Codex
reviewed the change and then its fixes. It found three gaps, each fixed
with a test that fails without the fix: a rewrite of the agent's own
answer didn't stop a late line, the agent's progress could leave a long
thread's summary with no owner, and a second rewrite could list a round
again.

> _This was written by Claude Code on behalf of max-sixty_
max-sixty added a commit that referenced this pull request Nov 21, 2026
567 commits on main, 50 conflicted files. What the merge decided where the
two sides met:

- main ends a question the agent asked by any resolve of its thread
  (#6106, `closings`) and deleted the agent's question end. The branch keeps
  its own rule beside it, that the user's resolve settles nothing the agent
  owes (`answered_by`, `owed_turns`): one ends what the user owes, the other
  what the agent owes. `obligations` loses `end_question`, `ended_as` and
  the answered-elsewhere ending; a question a resolve ended stands `waiting`
  with no next actor, and a plan step naming it reads done.
- main deleted Leaf's working fork and `holding.py` (#6574). `activity`
  drops the fork holder, agent turns and `agent_holds`; the line fork's On it
  (`served_state.work.lined`) reaches the fold as `on_it`, so a waiting
  update a progress message came after reads `running`.
- main's `start_holds` (#6593) replaces the branch's turn-only start rule
  in `_start_age`, `in_hand` and the Stop hook, and `Turn.busy` (#6334)
  replaces `Turn.background`.
- The neighbor row moved to `LivePage` in the schema and keeps the branch's
  fields; its record format moves to 53, the current protocol.
- `threadRecord` (main's kept reading) takes the branch's obligation-record
  attention and to-dos as obligations.
- Tests take main's fixtures (`lf-group`, `selected`, thread markup in the
  message text) with the branch's fields. Scenario S12 ends its question by
  resolving the thread, and the liveness scenario no longer re-dates the
  harness's `busy` after a start, which `start_holds` reads.
max-sixty added a commit that referenced this pull request Nov 21, 2026
A fresh agent reviewed the merge with main (521ec2b).

- `dev/tests/process/test_website_server.py`: the journey profile's fixture still
  gave the old activity words, so the test failed once main's fix let it run
  that far.
- A waiting update a line fork's line stands after counted both as worked on
  and as awaiting delivery, so an All pages row said both of one message.
  `unseen` now counts what no turn has taken in and nothing is at work on.
- Prose the merge left wrong: `events.md` said a reply restores the questions a
  resolve ended; `session-lifetime.md` named a page state `working`; the
  glossary and `workflow.js` gave On it to a fork that takes an update on,
  which #6574 removed; How it works still listed Up next, Conditions and an
  obligation's status; `queues.js` and `todos.py` each cited something gone.
- The hooks module's process test and the on-lists eval fixture carry the
  fields the served state now has.
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