Skip to content

Answer a held thread message from a fork of the Claude Code conversation - #4892

Merged
max-sixty merged 5 commits into
mainfrom
forked-replies
Nov 1, 2026
Merged

max-sixty merged 5 commits into
mainfrom
forked-replies

Conversation

@max-sixty

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

Copy link
Copy Markdown
Owner

In Claude Code, a user message in a thread the session hasn't picked up is now held from delivery while a fork of the session's conversation decides: the fork answers from the conversation, and its reply is posted as the agent's, or it hands the thread to the session, which receives it as any input. A question the conversation already answers gets its answer in a median 12 s on real thread messages, against 48 s for an idle session and 100 s for a busy one today; everything else reaches the session about 6 s later than today. LEAF_FORK_REPLIES=off in Claude Code's environment turns forks off.

How it works

src/leaf/forks.py owns the state machine, and its header states it. A thread with waiting messages is held, answered or with the session, computed by forks.held from the log, the session's hand-over record (sessions/<session>.forks, a new state.Record at protocol 37) and its forks lane; nothing stores the state. The lane is the session's pages follower (hooks.cmd_pages): it lists held threads for the module to fork (forks.look), and hands a thread over itself once 30 s have passed since its newest message. A lane holds only messages admitted after it took its lease, so a restarted follower never holds again what an earlier one let go.

Every way out of the hold but a killed follower is a write the session's waiters already read: the reply in the log, or a hand-over in the record the watch's reading stamps (served_state.reading), which also nudges a session no watch follows, as admission would have. The reply is fenced to the hold under the lock its append takes (thread.Receipt.fence), so it writes nothing once the session has the message. A follower that stops with a thread held nudges what it leaves, as a watch does.

hooks/claude-code.ts (forkReply) forks with $.model.fork, names the tool calls the session is running (which the fork's copy of the last request predates), and hands the answer back through leaf hook --fork. Python writes the prompt (forks.reply_prompt) and reads the answer. The session hears of a fork's reply at its next prompt or tool boundary: the settled message arrives with the reply in the thread's digest, and the clerk's queue adds a line asking the session to correct the reply where what it knows shows it wrong.

Behavior changes elsewhere: delivery.deliverable leaves held messages out, and admission doesn't nudge for a held message, since its hand-over nudges instead.

Evidence

  • tests/model/test_forks.py walks each transition, and each of eight races Codex found over three review rounds has a test that fails without its fix. tests/process/test_interact_claude_code_module.py drives the hooks module with a scripted fork.
  • leaf-dev journey claude-code passed against a real Claude Code session both ways. With forks off it ran all 13 delivery steps. With --forks (new here, and charted in its own row by leaf-dev journey-chart), forks answered a question while the session was idle in 3.4 s and one during a 25 s command in 2.4 s, and handed both work requests to the session in about 2 s. About 2 s of each forked reply is Leaf's own: the follower's poll, two leaf hook --fork processes and the post.
  • Replays of real thread messages scored the fork's judgment (notes/fork-replies.md, "How well the fork judges"). On 15 fresh messages the shipped prompt answered 8, one of them wrongly, and handed 7 over correctly; the 95% interval on its wrong-answer rate runs from under 1% to about 50%.

Known limits

A follower killed outright while it holds a message, after the user pressed Escape, leaves that message waiting for the page's next change. Admission's nudge can still race a starting Stop watch into waking a session twice; that race predates this change, and the follower now adds a nudge only when it held a thread.

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

…read messages from one

notes/fork-replies.md records what Leaf's hooks module can fork, measured on
Claude Code 2.1.316 with Opus 5.5 by a probe module run under `claude -p`:
`$.model.fork` answered mid-command in 1.9 s at 44k tokens and 4.5–6.0 s at
225–274k when the main thread's cache entry lived, and in 13.9 s when it
didn't; a forked subagent answered in 3.7–8.6 s. Mid-turn the fork misses the
tool call now running; at a turn's end it sees the final reply.

It proposes, unbuilt, a fork for each thread's waiting messages that answers
or hands them to the session, with Leaf holding them from delivery while a
live fork lane decides, the reply posted as the session's, and the session
reading the exchange at its next step through the delivery that exists.

notes/leaf-run-sessions.md drops its `--fork-session` side-lane measurement,
which the module's primitives supersede, and points its side-lane option and
table cell at the new note.
In Claude Code, a user message in a thread the session hasn't picked up is now
held from delivery while a fork of the session's conversation decides: the fork
answers from the conversation, posted as the agent's reply, or hands the thread
to the session, which receives it as any input. `src/leaf/forks.py` owns the
state machine: a thread with waiting messages is held, answered or with the
session, computed by `forks.held` from the log, the session's hand-over record
(`sessions/<session>.forks`, protocol 36) and its forks lane, with nothing
stored. The lane is the session's pages follower (`hooks.cmd_pages`), which
lists held threads for the module to fork, and hands a thread over itself once
30 s have passed since its newest message. A lane holds only messages admitted
after it took its lease, so a restarted follower never re-holds what an earlier
one let go.

Every way out of the hold but a killed follower is a write the session's
waiters read: the reply in the log, a hand-over in the record the watch's
reading stamps, which also nudges a session no watch follows. The reply is
fenced to the hold under the lock its append takes (`thread.Receipt.fence`),
so it writes nothing once the session has the message. A follower that stops
with a thread held nudges what it leaves, as a watch does.

The session hears of a fork's reply at its next prompt or tool boundary, with
the reply in the thread's digest and a queued line asking it to correct the
reply where its context shows it wrong. `LEAF_FORK_REPLIES=off` in Claude
Code's environment turns forks off; a module or Leaf without the lane holds
nothing and delivers as before.

The fork's prompt hands over whenever it would have to work an answer out, and
whenever the user answers or decides something the session asked. Replayed on
15 fresh real thread messages it answered 8, one wrongly, and handed over 7
correctly; `notes/fork-replies.md` has the replays, timings and limits.

Behavior changes: admission no longer nudges for a held message (its hand-over
nudges instead), and `delivery.deliverable` leaves held messages out.
`leaf-dev journey claude-code --forks` runs the session with forked replies on
and, in place of the delivery steps, asks a question while the session is idle,
one during a 25 s command, and a request for work during one, checking that a
fork answered each question, the session answered each request, and every
comment still has one reply. Each comment a fork decided on carries the fork's
outcome, time and tokens in the sample. Without `--forks` the journey runs with
`LEAF_FORK_REPLIES=off`, so its delivery steps keep testing the session's own
answers.
# Conflicts:
#	build/browser/protocol-baseline.json
#	src/leaf/clerk.py
#	src/leaf/hook_transport.py
leaf-dev journey-chart named a forks run's samples as it named the rest, so
their release replies pooled with the session's own; the row now says
"with forks". The developing-leaf skill says to add --forks to the
claude-code journey after a change to forks.py or the module's forkReply.
@max-sixty
max-sixty merged commit 410621c into main Nov 1, 2026
4 checks passed
@max-sixty
max-sixty deleted the forked-replies branch November 1, 2026 06:24
leaf-agent added a commit that referenced this pull request Nov 7, 2026
Comments, docstrings and one note that point at names the code no longer
has. Each now names the rule or function it means. Nothing that runs
changes.

- **Renamed `browser/AGENTS.md` sections.** `floating-panel.js` cited
"News never moves what the reader is reading", and
`composing/text-field.js` cited "Editing in place" and "Stability". None
of these is a heading or rule any more. They now name "News moves what
the reader reads only in the thread they are in", "Editing something
keeps it where it stood and as it read" and "The page holds still under
the user's aim".
- **`keyboard/layer-stack.js`, `nativeModes`.** The comment counted hint
popovers among the modes, but since #5614 `pushNativeLayer` returns
`null` for `popover="hint"`, so no hint popover ever joins the stack.
The module header already says so.
- **Functions that were removed or never existed:**
- `floating-panel.css` cited `rowNotice`, which #5613 deleted. It now
cites `rowNoticeTemplate`.
- `thread/messages.js` cited `preview.js`'s `fitList`, which has never
existed. The fitter is `fit`.
- **`packages/diff/widget-shadow.css`.** Two z-index comments gave the
full-name path box as their reason, and #5614 removed that box. The
values stay. The comments now give the stacking they keep: the line
numbers sit at 2, the pinned name at 3 and the file's actions at 4.
- **`notes/fork-replies.md`.** It said a `forked-replies` branch builds
the design, and that the fork posts "the reply". `forks.py` has been on
main since #4892, and since #5552 the fork posts only the working line.
The rest of the note, the research and the unbuilt "fork that takes the
work", stays.
- **Smaller fixes:**
  - The `plans.py` docstrings now name the `items` parameter.
- The `structure.py` allocation comment now points at
`AUTHORED_ALLOCATIONS`, which also holds `data-height` and the page-wide
allocations.
  - A runtime test now names `tokens.css`, where the card widths live.
- A test comment now names `test_admission.py`, where #3089 moved the
named-dependency test.
- **`packages/default/styles/sample.css`.** A trailing comment governing
no rule repeated the exhibit rule that `options.css` states and applies,
so it is deleted.

Verification: pre-commit (ruff, prettier, eslint, ty, knip, typos)
passes on the changed files, and
`tests/runtime/comment-placement.test.mjs` passes. No test is added,
since nothing that runs changes.

Co-authored-by: leaf-agent <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants