Skip to content

Follow a page's event log with leaf events --follow - #1075

Merged
max-sixty merged 1 commit into
mainfrom
events-follow
Sep 24, 2026
Merged

max-sixty merged 1 commit into
mainfrom
events-follow

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

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 after SEQ, then each event the append door admits, one JSON line per event as stored, including seq and the server-stamped meaning. seq is the resume cursor: restarting with --after the last seq printed misses nothing and repeats nothing. SIGINT, SIGTERM and a closed stdout exit 0. --follow with --conversation is refused.

Trigger. The follower wakes on the log file's stamp every LOOK_S, the same look the browser's /api/news stream makes over the whole page, narrowed to events.jsonl. LOOK_S moved from http.py to files.py beside file_stamp so both readers share it. A moved stamp reads only the complete lines past what was already read. _parse_events takes 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 reads leaf page state or 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.md points a worker there. The matching paragraph leaves notes/datastore-review.md.

Tests. Two integration tests in tests/test_interact_document.py run a real follower subprocess against writes through the append door: one reads existing records then new ones and stops on SIGTERM; one resumes with --after and 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

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

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]>
@max-sixty
max-sixty merged commit 9747a3e into main Sep 24, 2026
5 checks passed
@max-sixty
max-sixty deleted the events-follow branch September 24, 2026 03:04
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]>
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