Skip to content

Keep a started message out of the reminders while the session's background agents work - #6593

Merged
max-sixty merged 14 commits into
mainfrom
mark-holds-while-background
Nov 20, 2026
Merged

max-sixty merged 14 commits into
mainfrom
mark-holds-while-background

Conversation

@max-sixty

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

Copy link
Copy Markdown
Owner

A start on a message now keeps it out of Leaf's reminders for as long as the page shows it in hand: through the turn that wrote it, and after that while the harness reports the session busy without a break since the start. Claude Code does so while any background agent of the session runs (a fork, another subagent, a teammate or a workflow); Codex and Pi report nothing, so there a start still lapses with its turn. Once nothing holds the message and it is still owed, the next turn is reminded of it. Before, a start counted only within its own turn, so the session's next turn, opened for a message in another thread, was told to start a message its fork was still answering, and the session started it again or did the work itself.

activity.start_holds is now the one rule the page's aging, the prompt's and Stop's owed answers and the watch's wake all read. To weigh: a start whose turn ends with no background agent is reminded right after that turn (the page already showed it dropped then); a background Bash command alone doesn't count, since Claude Code reports it as shell, which Leaf reads as idle; and because Claude Code dates busy by its last change, a dialog after the start (waiting, then busy) ends the hold as a break would, so the reminder comes as it did before. Fixing that needs a mark of unbroken work Leaf keeps at each turn's opening, which the start_holds docstring describes and this PR leaves for a decision. The worker rule in conversation-loop.md now says the start holds through later turns where the harness reports background agents, and otherwise to answer the update or start it again.

Max decided this on a Leaf page. Scored on beside-work-goes-to-a-fork's new next-turn variant: base 3/6, the sentence deleted alone 2/6, the shipped text 14/14; the fork-ended guardrail 3/3 on both. Focused model tests, a new hook test that fails on the old rule, and the claude-code journey pass.

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.
…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.
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.
…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.
…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.
Main moved the page protocol to 52 (#6581), so the agent-turns and clerk
records this branch reshapes move their since to 52.
…round agents work

Max's decision, picked on a Leaf page: stop reminding the session of a message
while a background agent runs. A start on a message now counts for the reminders
for as long as the page shows it in hand, until Claude Code reports no agent of
the session running in the background, whatever kind of worker it is; a message
the session started and then dropped gets its reminder once that work ends.

Before, the reminders counted a start only within the turn that wrote it. A
session that started a message and gave it to a fork ended its turn over it, and
its next turn, opened for a message in another thread, named the first message
as owed and told the session to start it again, though the fork was still
answering it. The session then started it again or did the work itself.

What changes:

- `activity.start_holds` is the one rule for whether a start of the claimant's
  session still holds its update: while its turn is current and no end of it was
  seen before the start, and after that while Claude Code's record says `busy`
  dated at or before the start. The page's aging (`_start_age`), the prompt's
  and Stop's owed answers (`turn_obligations`) and the watch's wake
  (`left_owed`) all read it through `in_hand`, so the page and the reminders
  agree. A start left with no background agent now gets its reminder as its
  turn ends, as the page already showed it dropped then; and a later turn no
  longer shows an earlier turn's start as in hand on the page unless background
  work carried it.
- The prompt hook records no pickup of an update a start holds, which would have
  put the update past its start and counted as a reminder it never gave.
- The watch keys its cached reminders on Claude Code's word on the session, so
  the background work ending wakes the reminder without a log write.
- `started_by` entries carry the start's `ts`, which the dated rule needs.
- The Stop/prompt reminder's restart note says why a start lapsed.
- `conversation-loop.md`, "Long-running work", drops "In the next turn, start
  the item again or answer it" and says the start holds the update through later
  turns while the session's agents work in the background, so the session leaves
  it to the worker; `task start`'s help, `session-lifetime.md` and How it works
  say the same.

What `busy` means, checked in Claude Code 2.1.329's source and on a live
session: the registry record says `busy` while a turn runs or while any
background task of kind local_agent (forks and other subagents), remote_agent,
in_process_teammate or local_workflow runs; `shell` (which Leaf reads as idle)
when only a background Bash command runs. A haiku `claude --bg` probe that ended
its turn with a background agent running stayed `busy` from the turn's end until
the agent finished, went `shell` while a command the agent left ran, and was
`busy` without a break through the turn the agent's completion opened. So `busy`
is dated against the start: an undated `busy` would hold through every turn the
session runs.

Eval, `beside-work-goes-to-a-fork` with two new variants, claude-code, author
spend $7.63 over 32 trials:

- `next-turn` (a later turn while the fork still works), leaves-the-fork-its-
  message: base 3/6 (1/3 of runs that read conversation-loop.md); deleting the
  sentence alone 2/6 (2/6 readers, which started the message again to keep it in
  the panel's status); the shipped text 14/14 (3/3 readers).
- `fork-ended` (Leaf's reminder after the fork ended without answering),
  answers-or-restarts: base 3/3, head 3/3.

Tests: `test_a_start_holds_its_message_while_background_work_runs` walks a start
through a later turn and the background work's end (it fails on the old rule at
the later turn's prompt), and the coordinator test checks that `busy` starting
after a break brings no dropped start back.
…ound work

Codex and Pi say nothing of a session's background work (`Harness.live_turn`), so
there a start still lapses with its turn and the next turn is reminded of the
update. The worker rule in conversation-loop.md, `task start`'s help, the reminder's
restart note, session-lifetime.md and How it works said the start holds while the
session's agents work, which is true only where the harness reports them, as
Claude Code does. Each now says so, and the worker rule again tells the agent to
answer the update or start it again once nothing holds it. A factual correction of
the text #6593 added, so no new eval run.

@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.

One gap in the hold rule, inline on start_holds: a dialog after the start breaks the hold even though the session's background agents keep working.

Comment thread src/leaf/activity.py Resolved
The registry dates `busy` by the record's last change of status, so a permission
prompt or question after the start (`busy`, `waiting`, `busy`) re-dates it and the
hold lapses as if the session had gone idle, which brings back the reminder this
rule removes. The review on #6593 found it; the docstring now states it and the
mark of unbroken work a fix would need, which is left for Max to decide.
@max-sixty
max-sixty merged commit a1e119d into main Nov 20, 2026
7 checks passed
@max-sixty
max-sixty deleted the mark-holds-while-background branch November 20, 2026 17:23
max-sixty pushed a commit that referenced this pull request Nov 20, 2026
…livery 52 in the fork eval (#6621)

Both failures in `ci` run
[44799579467](https://github.com/max-sixty/leaf/actions/runs/44799579467)
on main come from #6573. The tests were left behind by that change. The
product behavior is fine.

- **Eleven browser tests in `test_render_anchors.py` and
`test_render_aim.py`.** #6573 gave the margin card its own thread quote
("Every thread view now quotes its words"), so an open thread now draws
`.lf-thread-quote-head` / `.lf-quote-label` in both the panel and the
card. These tests read the panel's quote with page-wide locators, and
Playwright's strict mode refuses the two matches. The `thread_head`
helper and the flowchart test now read inside the open panel view
(`PANEL_THREAD`), which is the view the helper's docstring already
names. Each test asserts what it did before.
- **`test_every_delivery_an_eval_pastes_is_the_envelope_leaf_writes`.**
#6573 moved deliveries to `leaf-delivery/52` with a `rewritten_from`
field on each thread digest and updated two of the three envelopes in
`evals/beside-work-goes-to-a-fork`. The `next-turn` variant came from
#6593, which merged first, and it kept `/51` (`'leaf-delivery/51' ==
'leaf-delivery/52'`). It now carries the format and the null field Leaf
writes.

Verification: all 12 tests fail on current main (`origin/main` at
da4e538) and pass on this branch, run locally with Chrome. `rg
'page.locator\(".lf-(thread-quote-head|quote-label|thread-quote)"'` over
`tests/` and `dev/` finds one other unscoped read, at
`test_render_aim.py:2195`. It passed in the failing run, because design
mode retires the margin card there.

Refs #6620 — automated triage

Co-authored-by: leaf-agent <[email protected]>
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 24, 2026
…the background (#6940)

A message the agent starts now stays in hand after its turn ends while a
command that turn left running in the background still runs, as it
already does while a background agent works: the page reads it Working
and no reminder names it until the command exits or the session's next
turn opens. Before, Claude Code's `shell` status (no turn or agent
running, a command still running) read as plain `idle`, so the message
dropped to Stopped and the watch woke the session to remind it of work
its command was still doing.

Mapping `shell` to `busy` doesn't fix it: Claude Code dates each status
by its last change, so `shell` is dated from the turn's end, after the
start. `Harness.live_turn` now reads `shell` as `idle` (which interrupt
detection relies on) with `command: true`, and `activity.start_holds`
holds a start from the claimant's current turn while the record says the
session works: `idle` with `command`, or a `busy` that began before the
turn's end. The second half also keeps the start through a dialog after
it, such as approving the command, which re-dates `busy`.

To weigh:
- A command alone doesn't make the page read Working "in the background"
the way a background agent does: a dev server or a Monitor reports the
same `shell`.
- A later turn opened while the command still runs is reminded of the
message, since that turn's `busy` hides the command; the agent starts it
again.
- A start the agent leaves while an unrelated server runs (a `leaf-dev
preview`, say) is reminded at the next turn rather than at once; the
page shows it Working, then Gone quiet after 15 minutes.

The restart line in Leaf's reminder and `conversation-loop.md` now say
what holds a start; neither changes a rule.

This extends Max's decision for background agents (#6593) to commands;
nobody chose the difference, and Max hasn't decided the command case
himself. Tests: a new hook test against Claude Code registry records
fails on `main`; activity tests cover `shell` after a background agent
and an Escape that leaves a command running. The claude-code journey
passes all 14 steps.

> _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