Skip to content

Read the agent's turn one way, from Claude Code's own session record where it has one - #1331

Merged
max-sixty merged 12 commits into
mainfrom
status-state-machine
Sep 27, 2026
Merged

max-sixty merged 12 commits into
mainfrom
status-state-machine

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

Leaf decided what an agent was doing from several readings that each aged evidence their own way, so a page could say "Working — on your update" long after Claude was interrupted, sitting in a permission dialog, or done. This change folds them into one reading of the agent's turn, adds the one source of truth Claude Code keeps about itself, and removes the states the fold no longer produces. The "How it works" page now describes that state machine as the code runs it.

One turn reading

activity.claimant_turn / current_turn are now the only place a turn is judged. The hook stamps (turn_opened, turn_closed) are the spine; a turn stays running within WORKING_GRACE of its last renewal (its opening, a status write, or the claimant's stream). Page activity and per-input workflows both derive from that reading, so the banner, the margin, neighbouring-page rows, and the nudge can no longer disagree.

A prompt or delivery into a turn that is still open renews turn_opened (service.open_turn), so a new prompt after Escape is judged by its own start rather than the interrupted turn's.

Claude Code's own session record

ClaudeCodeHarness.live_turn() reads ~/.claude/sessions/<pid>.json, which Claude Code rewrites on Escape (idle, with no Stop hook) and on a permission or question dialog (waiting). An idle newer than the turn's renewals ends the turn; a waiting newer than its stamps is a dialog, shown as "Claude waiting for you" (awaiting_user) and dated by when it opened. busy is never read: it stays set across turn endings while a background job runs, so it cannot say a turn is live. Other harnesses return no record and are read from their hook stamps alone, which is what makes the Codex readings improve too: a Codex partial reply (a turn that completed without a final answer) now ends its workflow rather than reading as a reply still in progress, and an awaiting step no longer times out while the observer holds its lease.

Nudges and overdue input

The terminal nudge fires only after an ending Leaf saw (turn.ended) while nothing is taking input, once per ending: the mark is the claim's close stamp, or for an interrupt its last opening, and the wait lease is checked first. counts.overdue counts input whose next move is the agent's and whose turn ended, was interrupted, or was never delivered; stale work is not overdue. It alone drives the banner's "nudge it in the terminal" and the neighbouring-page badge.

Removed

unattended page activity, the page quiet flag, and the workflow quiet / dropped conditions: nothing in the new fold reaches them.

Where to look

  • skills/leaf/scripts/leaf/activity.py — the fold; claimant_turn's docstring states the recency rules.
  • skills/leaf/scripts/leaf/host.py — the registry reader.
  • skills/leaf/scripts/leaf/presence.py, event_endpoint.py, service.py — the live facts, the nudge, turn renewal.
  • skills/leaf/assets/runtime/banner.js, margin-*.js, live-leaves.js — the wording that follows the new readings; queued receipts get their own margin face.
  • docs/how-it-works.html (Loop and Updates tabs), session-lifetime.md, conversation-loop.md — the state machine as documented.
  • tests/test_interact_session.py — the Claude record, the dialog outlasting the grace, and the once-per-ending nudge, each checked by mutation to fail without its rule.

Three Codex App Server adapter defects remain for a separate change: _reconcile in codex_adapter.py can reopen a finished turn, a reply binding left after a disconnect can rewrite an answer, and claim.turn can hold either Leaf's id or the App Server's.

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

🤖 Generated with Claude Code

max-sixty and others added 12 commits September 27, 2026 00:11
…s one

Every rule in the activity fold that asked whether the claimant's turn is
running — whether the agent takes input between waits, whether a delivered
move is being handled, whether a pickup's turn ended, whether a working claim
outlived its turn, whether streamed activity is current — now reads one
answer, activity.claimant_turn. It takes the host's live record first
(Harness.live_turn; for Claude Code, the session's status in its session
registry, which turns idle the moment Escape interrupts a turn and waiting
while a permission dialog is open), and otherwise the claim's stamps, believed
open only while the opening, a status write, or the claimant's stream renewed
them within the working grace.

Behaviour that moved:
- "Working — on your update" no longer stands forever after an interrupted
  Claude turn or a dead Codex adapter; the pickup reads Turn ended (host saw
  the end) or Update stale (nothing renewed the turn).
- A Claude dialog shows as Claude waiting for input, announced like Codex's.
- Nudge Claude in terminal only once an owed move stayed Sent past the pickup
  grace (counts.overdue); a turn ending with no watcher alone reads away.
- Codex: a reply that completed without a final answer (partial) reads ended;
  an approval or input wait no longer ages out while its observer lives.
- Margin receipts read under their own label; queued takes the pickup face.
- Removed the unreachable activity kind unattended, workflow quiet/dropped,
  activity quiet, and the dead margin "Was working" row.

The How it works Updates tab now states the decision as a flowchart with
corrected readings and labels.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
# Conflicts:
#	tests/conftest.py
…s reading

A background job's session record stays busy across turn endings while its
background work runs, so busy never proved a turn was running. The record now
adds only what no hook sees, each fact dated and the newest deciding: an idle
newer than every renewal ends an open turn (an interrupt), and a waiting newer
than the turn's stamps is a dialog open now. Records whose process is gone are
passed over.

Browser-event admission nudges a Claude Code session by the same reading
(presence.claimant_takes_input) rather than by the Stop stamp alone, so input
after an interrupted turn messages the session.

counts.overdue now takes every owed move stalled with the agent to act, so a
move picked up by a turn that ended without answering asks for a nudge too.

How it works: the flowchart and readings agree, the turn fact no longer says
busy means running, and user input is called input throughout.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…nding

Review of the recency rule found two holes. With busy ignored outright, a long
foreground step that writes no status read as not running, so admission
could message a session mid-turn; a busy newer than the turn's opening now
holds the turn, while a busy older than it (a background job's, kept across
turn endings) still adds nothing. And the nudge was capped per turn id, which
an interrupt does not advance, so a second interrupt under the same id went
unmessaged; the cap is now per ending (messaged_ending: turn id and the moment
the fold read it ended).

A dialog after a Stop shows as a step without making the session take input.
The fold and admission read the turn through one function,
activity.current_turn.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude Code marks a session busy as it takes the prompt, before the prompt
hook starts and stamps turn_opened in whole seconds, so a turn whose busy
landed in the previous second read as not running and could be nudged
mid-turn. The comparison now allows HOST_LEAD (10s); a background job's busy
kept across turn endings precedes the opening by a whole turn.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…en ending

Two review rounds patched how the fold read Claude Code's busy (first ignored,
then counted when it began in the turn, then with a lead for the hook's stamp)
to stop admission messaging a session mid-turn once belief in a long, silent
turn expired. The cause was the nudge acting on expired belief at all, which
main never did. The nudge now needs an ending something saw — a Stop's close or
the record's idle — and the fold reads only the record's unambiguous states:
idle, which ends an open turn, and waiting, a dialog open now. busy and
HOST_LEAD are gone.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
The banner's Nudge counted every owed move with a condition, stale work
included, so a long silent step past the working grace read Nudge while
admission, which nudges only after a seen ending, held back. counts.overdue now
takes the same rule: Sent past the pickup grace, or left by a turn that ended
or was interrupted.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
The overdue comment, the session-lifetime contract and the banner's Nudge line
still described the earlier rule; Nudge now says nothing is answering the
input, which holds for moves an ended turn picked up as well as unpicked ones.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
# Conflicts:
#	skills/leaf/scripts/leaf/session-lifetime.md
main's #1291 records a served workflow for the Node tests to build on; this
branch removed workflow quiet and dropped from the wire, so the recording drops
them too (uv run tests/served_records.py).

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
- A prompt or delivery into a still-open turn renews turn_opened. Without it,
  Escape then a new prompt left the new turn judged by the interrupted one's
  opening, so the page read away while the agent worked.
- The nudge's one-per-ending mark is the claim's close stamp, or for an
  interrupt its last opening, so the host record flipping between idle and
  shell no longer sends a second message for one interrupt; it checks the wait
  lease before reading presence, as main did.
- Claude's dialog step is awaiting_user ("waiting for you"), since the record
  cannot tell an approval from a question, and is dated by when it opened.
- The registry listing is reused for a second while its directory stamp holds.
- Queued receipts group under a Queued face rather than Picked up.
- The tray test that set the removed activity.quiet now sets counts.overdue.

Not taken: moving live_turn out of the public presence reading. That reading
already carries the claim's turn stamps and the wait lease, the same kind of
fact, and live_turn names no harness.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@max-sixty
max-sixty merged commit ad29d53 into main Sep 27, 2026
5 checks passed
@max-sixty
max-sixty deleted the status-state-machine branch September 27, 2026 19:12
max-sixty added a commit that referenced this pull request Sep 28, 2026
…t, the phone row inset and the overdue reading (#1400)

Repairs the nightly failures in [run
36421522559](https://github.com/max-sixty/leaf/actions/runs/36421522559)
that #1391 and #1392 left: thirteen failing node ids, one of which
needed a product fix to the phone banner's menus.

## The phone banner's menus stand under their door

On a phone the banner's action row puts More (and Versions after it) at
the row's left inset, and More's menu and the Versions menu open under
that door. Both menus clamped their left edge with `left: max(calc(8px +
var(--lf-safe-left)), anchor(left))`. The `8px` restated the row's inset
(10px then). #1338 cut that inset to 3px to fit three controls in 320px,
so the clamp now binds and the menu stands 5px right of the control that
opened it, which
`test_the_versions_menu_uses_the_banner_panel_and_its_doors_edge`
caught.

The row's padding owns the inset, and the door already stands inside it,
so the clamp can never be tighter than `anchor(left)`. Both menus now
take `left: anchor(left)` in one rule; the second copy of the inset is
deleted. The far edge is still bounded by the menus' existing
`max-width: calc(100vw - 16px)`.

Measured with `leaf-dev probe alert-review` (left, right edges in px):

| viewport | surface | before (e730341) | after |
|---|---|---|---|
| 320×844 | More door | 3 – 27 | 3 – 27 |
| 320×844 | Versions menu | 8 – 147.4 | 3 – 142.4 |
| 320×844 | More menu | 8 – 206.1 | 3 – 183 |
| 390×844 | More menu | 8 – 206.1 | 3 – 183 |
| 1440×900 | More door | 1247.1 – 1283 | 1247.1 – 1283 |
| 1440×900 | Versions menu | 1143.6 – 1283 | 1143.6 – 1283 |
| 1440×900 | More menu, right edge | 1283 | 1283 |

The More menu's width differs between the arms only because its
build-identity row reads `Leaf sha256:…` on the base commit and `Leaf
e730341+ · 9m ago` on the working tree; desktop placement is unchanged,
since the change sits inside the `width <= 840px` block.

## Tests that read the address the bootstrap now rewrites (#1337)

The bootstrap takes the host key `t` out of the address with
`history.replaceState`.

- `test_a_failed_preview_bootstrap_hears_the_replacement_server` (6
params) and `test_a_failed_bootstrap_hears_a_static_registry_generation`
counted reloads with `framenavigated`, which also fires for that
in-place rewrite (CI: `assert 2 == 1`; in the static-registry test the
stub never answered because `len(navigations) > 1` held from the start).
They now count the main frame's document requests
(`requested_documents`), so a reload is exactly one more document and
the rewrite counts as none.
- `test_the_current_page_has_a_menu_local_key` read `pin` off the end of
the address, which the rewrite turns into `?pin=`. It now asserts the
address is the one it found before the press.
- `test_a_draft_explains_its_change_and_restores_history_as_an_edit`
opened its second tab at the keyless `page.url` and got a 403. It opens
the handover URL `serve` returned.

## Other moved behavior

- `test_a_thread_can_be_answered_in_the_margin_without_opening_threads`
[1440, 1920]: since #1324 the reply placeholder paints in the standing
paint on the next frame. The test read it in a microtask after
`focusin`; it now reads it on the animation frame the focus asks for,
after the runtime's own focus listener has queued that frame's paint.
- `test_a_thread_says_what_the_agent_is_doing_about_it`: since #1331 a
turn that ends with updates still the agent's to answer makes them
overdue, so the banner says "Nothing is answering them, so nudge it in
the terminal." The expectation follows.

## Checks

All 13 node ids pass locally, and `leaf-dev flake` ran each changed test
(one parameter of each parametrized one) as 18 concurrent copies: 126 of
126 passed. The banner and menu cases in `test_render_controls.py` also
pass beside the CSS change, as do 54 phone, banner, menu and version
cases across the anchors, aim, navigation, pages, widgets, threads and
projection files.

> _This was written by Claude Code on behalf of max-sixty_

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 5.5 (1M context) <[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