Repository navigation
Follow a page's event log with leaf events --follow - #1075
Merged
Merged
Conversation
Other programs can now follow a page as it changes: `leaf events PAGE --follow [--after SEQ]` prints each stored record after SEQ, then each event the append door admits, one flushed JSON line apiece. `seq` is the resume cursor. SIGINT, SIGTERM and a closed stdout exit 0. The feed wakes on the log's file stamp at the news stream's look cadence, so LOOK_S moves beside file_stamp in files.py and both readers share it. A moved stamp reads only the complete lines past what was already read, counted the way _parse_events counts them, which now takes the line it starts after. events.md gains "Following the log", which also states that stored records are a public format whose readers drop what they do not recognise; the datastore note's paragraph asking for this goes. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
leaf-agent
approved these changes
Sep 24, 2026
max-sixty
added a commit
that referenced
this pull request
Sep 24, 2026
#1086) 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](https://claude.com/claude-code) Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
max-sixty
added a commit
that referenced
this pull request
Sep 24, 2026
…its explainer role (#1095) Refinements from a review of #1075 (`leaf events --follow`) and #1091 (the event-log explainer). **The follower ends when its log is replaced.** - Before this, `follow_events` kept its byte offset across an inode change. A log renamed into place stalled the follower silently, and a longer replacement would have been printed from the middle under the wrong seqs. - It now remembers the inode of the file it first opened. It ends with the existing `<log> is gone` error (exit 1) when the inode changes, when the file is shorter than what was read, or when the file vanishes between the stat and the open. - `test_events_follow_ends_when_its_log_is_replaced` covers it and times out without the guard. **Startup skips parsing what `--after` excludes.** The lines at or before `--after` are counted, not parsed. With `--after 199999` on a log of 200k events, the first event arrives in 0.04 s at 82 MB, down from 0.43 s at 390 MB. **`events.md`.** - "Following the log" moves after the paragraph on dependency identities and `meaning.creates`, which #1075 had cut off from Admission. - It drops the "public format" promise, which AGENTS.md "Stage" contradicts, and keeps the rule that a reader drops fields and kinds it does not recognise. - It states the exit-1 case. **Page tests.** - The captured log's `lf-note` anchors are now derived from the record layout and compared with the page. When regenerated records shift lines, the test fails and prints the values to use. - The verb-table test now compares the Unit and Record columns as well as tag and verb. **Page prose.** - The reports and follow sections are cut to one sentence of mechanism each, plus a link to the rule, so they no longer copy `packages.md` and `events.md`. - The eyebrow says "how it works". - The Layers table no longer uses "envelope". It names what the door adds, and it lists the log's kinds by group. **Tests.** Everyday suite: 1359 passed. `scripts/site.py` builds 84 pages. `leaf version check --render` is clean in light and dark. 🤖 Generated with [Claude Code](https://claude.com/claude-code) > _This was written by Claude Code on behalf of max-sixty_ Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
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.
leaf events PAGE --follow [--after SEQ]lets a program other than the agent loop follow a page's event log. It prints every stored record afterSEQ, then each event the append door admits, one JSON line per event as stored, includingseqand the server-stampedmeaning.seqis the resume cursor: restarting with--afterthe last seq printed misses nothing and repeats nothing. SIGINT, SIGTERM and a closed stdout exit 0.--followwith--conversationis refused.Trigger. The follower wakes on the log file's stamp every
LOOK_S, the same look the browser's/api/newsstream makes over the whole page, narrowed toevents.jsonl.LOOK_Smoved fromhttp.pytofiles.pybesidefile_stampso both readers share it. A moved stamp reads only the complete lines past what was already read._parse_eventstakes the line number to start after, so seq is counted as a full read counts it.Data changes are not in the feed. Files under
data/are replaced in place and have no sequence to resume from; a reader that needs them readsleaf page stateor the value files.Contract. A new section in
events.md, "Following the log", holds the feed and says stored event records are a public format whose readers drop what they do not recognise.packages.mdpoints a worker there. The matching paragraph leavesnotes/datastore-review.md.Tests. Two integration tests in
tests/test_interact_document.pyrun a real follower subprocess against writes through the append door: one reads existing records then new ones and stops on SIGTERM; one resumes with--afterand stops on SIGINT. Both fail when the follower stops after its first read. Everyday suite: 1352 passed.npm run test:runtime: 84 passed.🤖 Generated with Claude Code