Repository navigation
Follow the log in leaf wait, and learn a wait started from its lease - #1086
Merged
Merged
Conversation
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
previously approved these changes
Sep 24, 2026
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
leaf-agent
approved these changes
Sep 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Two fixes to
leaf wait. The watch now wakes when the page's files change rather than on a one-second timer. ThePostToolUsehook 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 waitslept 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 everyLOOK_S(50 ms). Both now usefiles.next_reading, which checks a stamp everyLOOK_Suntil it moves, optionally up to a timeout.follow_eventsuses it and still reads only the new bytes.Watch.mark()before each pass: the stamp of the claims directory pluspage_readingfor every page the last pass read.page_readingis 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.REVIVAL_CHECK_S(5 s). That is the existing server-revival cadence, and nothing else in a pass depends on the clock.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_waitparsedtool_input.commandand returned False for$LEAF wait p,"$LEAF" wait --ack xanduv run leaf wait. SKILL.md tells agents to use the$LEAFform. Now:leases.started_wait).leaf hookhandlesPostToolUseafter a background command. If the held lease carries a token no hook has announced yet, it adds the guidance and records the token insessions/<id>.told.hooks/scripts/wait-started.pyandhooks/wait-started.jsonare deleted. The registration runsloop-guard.pybehind the existing$CLAUDECODEgate andifprefilter, with the timeout raised from 5 s to 10 s.The lease isn't held yet when
PostToolUsefires, 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 ownuv runtakes about 300 ms, so the two race. That rules out a "held when first checked" rule. Instead the hook checks for up toWAIT_START_S(3 s), and the token tells it which wait it is looking at.I checked the
ifprefilter in headless Claude Code 2.1.281. It passes$LEAF wait p,"$LEAF" wait --ack xand any command containingleaf wait. It rejectsL=/bin/echo; $L waitandsleep 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 forecho leaf wait is a commanddoesn't.Behavior changes
leaf waitbut is refused because another wait holds the lease.$CLAUDECODEgate. 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 waitagainst a served page):8201a7e3)Tests
-k "wait or hook or follow or watch or lease or adapter or events"): 250 passed on the merge with mainnpm run test:runtime: 91 passeduv run pytest tests: 1357 passed on the merge with main🤖 Generated with Claude Code