Skip to content

Follow the log in leaf wait, and learn a wait started from its lease - #1086

Merged
max-sixty merged 2 commits into
mainfrom
wait-follows-the-log
Sep 24, 2026
Merged

max-sixty merged 2 commits into
mainfrom
wait-follows-the-log

Conversation

@max-sixty

@max-sixty max-sixty commented Sep 24, 2026 •

Copy link
Copy Markdown
Owner

Two fixes to leaf wait. The watch now wakes when the page's files change rather than on a one-second timer. The PostToolUse hook now learns that a wait started from the wait's own lease, not by parsing the shell command.

Mechanism

One way to follow page files. leaf wait slept a second between full passes, and each pass took the log lock and parsed every watched page's whole log. leaf events --follow (#1075) instead checked the log's file stamp every LOOK_S (50 ms). Both now use files.next_reading, which checks a stamp every LOOK_S until it moves, optionally up to a timeout.

  • follow_events uses it and still reads only the new bytes.
  • The watch takes Watch.mark() before each pass: the stamp of the claims directory plus page_reading for every page the last pass read. page_reading is the stamp the browser's news stream already uses, so it covers the log, status, cursor, service file and the page's claim. Watch.await_news(mark) returns as soon as any of these move. If a pass finds a different set of pages, the next pass runs straight away, since the mark covered the old set.
  • If nothing moves, a pass still runs every REVIVAL_CHECK_S (5 s). That is the existing server-revival cadence, and nothing else in a pass depends on the clock.
  • The Codex adapter's loop wakes the same way and keeps its one-second tick, because its queued-offer and receipt-recovery steps depend on Codex, not on page files.

The wait's own pass still reads the whole log under the lock. It now runs only when something has changed, and it needs cursor, status and events read together, so it doesn't use an incremental read.

The wait says it started. The hook's starts_wait parsed tool_input.command and returned False for $LEAF wait p, "$LEAF" wait --ack x and uv run leaf wait. SKILL.md tells agents to use the $LEAF form. Now:

  • Whoever takes a wait lease writes a fresh start token into it (leases.started_wait).
  • leaf hook handles PostToolUse after a background command. If the held lease carries a token no hook has announced yet, it adds the guidance and records the token in sessions/<id>.told.
  • A token the hook has already announced means this command started nothing, because a second wait is refused while one holds the lease.
  • hooks/scripts/wait-started.py and hooks/wait-started.json are deleted. The registration runs loop-guard.py behind the existing $CLAUDECODE gate and if prefilter, with the timeout raised from 5 s to 10 s.

The lease isn't held yet when PostToolUse fires, because the host spawns the command and runs the hook right away. The measured time from spawn to held lease is a median of 286 ms warm (6 runs), and the hook's own uv run takes about 300 ms, so the two race. That rules out a "held when first checked" rule. Instead the hook checks for up to WAIT_START_S (3 s), and the token tells it which wait it is looking at.

I checked the if prefilter in headless Claude Code 2.1.281. It passes $LEAF wait p, "$LEAF" wait --ack x and any command containing leaf wait. It rejects L=/bin/echo; $L wait and sleep 1. End to end with a real served page, the session transcript for a background $LEAF wait "$XDG_STATE_HOME/page" contains the guidance, and the one for echo leaf wait is a command doesn't.

Behavior changes

  • A wait wakes within one 50 ms check of an append, a status or cursor write, or a claim change, instead of on the next one-second pass. A quiet page now gets a pass every 5 s instead of every 1 s.
  • The closing guidance now reaches a background wait launched with any spelling of the launcher. It no longer reaches a background command that runs leaf wait but is refused because another wait holds the lease.
  • A background command that passes the prefilter but starts no wait now holds its result for up to 3 s while the hook checks.
  • Tests: the parser-case hook test is now two tests. One covers the lease: spelling doesn't matter, an announced wait isn't this command's, and a lease taken after the hook starts checking still counts. The other covers the registration's $CLAUDECODE gate. A new test shows the watch wakes on an append rather than waiting for its timeout.

Wake latency

Time from an event being appended to the delivery line appearing on leaf wait's stdout, 12 samples each at a random phase (bin/leaf wait against a served page):

median max
before (8201a7e3) 320 ms 1035 ms
after 69 ms 118 ms

Tests

  • Focused run (-k "wait or hook or follow or watch or lease or adapter or events"): 250 passed on the merge with main
  • npm run test:runtime: 91 passed
  • Full uv run pytest tests: 1357 passed on the merge with main

🤖 Generated with Claude Code

Two readers of page files each had their own mechanism, and one hook
decided a fact only `leaf wait` knows.

`leaf wait` slept a second between full passes, each a locked read and
parse of every watched page's log, while `leaf events --follow` looked at
the log's stamp every `LOOK_S` (50 ms). `files.next_reading` is now the one
wait a synchronous follower makes: look every `LOOK_S` until a stamp moves,
optionally up to a timeout. `follow_events` runs on it, and so does the
watch: `Watch.mark` stamps the claims directory and each watched page
(`served_state.reading.page_reading`, the news stream's own reading) before
a pass, and `Watch.await_news` returns when any of them moves. A pass that
finds a new page set runs again at once. With nothing moved, a pass still
runs every `REVIVAL_CHECK_S` (5 s), the server-revival cadence and the only
thing a pass owes the clock. The Codex adapter's loop wakes the same way
and keeps its one-second tick for its Codex-side duties.

Wake latency, event appended to delivery line on stdout, 12 samples at a
random phase: median 320 ms, max 1035 ms before; median 69 ms, max 118 ms
after.

The `PostToolUse` hook decided "a background `leaf wait` started" by
parsing the shell command, which missed `$LEAF wait`, `"$LEAF" wait`, and
`uv run leaf wait`, the first being the form SKILL.md tells agents to use.
Each wait lease holder now writes a fresh start token into the lease
(`leases.started_wait`), and `leaf hook` answers `PostToolUse` after a
background command: it names a token the held lease carries that no hook
has named yet (`sessions/<id>.told`). The hook fires before the wait takes
the lease (about 0.3 s warm), so it looks for up to `WAIT_START_S` (3 s);
a background command that passes the `if` prefilter without starting a wait
pays that. `hooks/scripts/wait-started.py` and `hooks/wait-started.json` are
gone; the registration runs `loop-guard.py` like the other hooks.

Tests: the parser-case hook test becomes two, one on the lease (spelling
plays no part; a wait already named is not this command's; a lease taken
after the hook starts looking counts) and one on the registration's
`$CLAUDECODE` gate. A new test shows a watch wakes on an append rather
than its timeout.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
leaf-agent
leaf-agent previously approved these changes Sep 24, 2026
@max-sixty
max-sixty merged commit 29a7fa4 into main Sep 24, 2026
6 checks passed
@max-sixty
max-sixty deleted the wait-follows-the-log branch September 24, 2026 06:53
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