Skip to content

Move the cursor and status into the log, fold small records, and retire state-home records by their pages - #3384

Merged
max-sixty merged 24 commits into
mainfrom
state-json-files
Oct 20, 2026
Merged

max-sixty merged 24 commits into
mainfrom
state-json-files

Conversation

@max-sixty

@max-sixty max-sixty commented Oct 20, 2026 •

Copy link
Copy Markdown
Owner

Leaf kept several small JSON files per page and per session, and the files of the state home it shares across installs were never cleaned up once their format changed: on one machine 1,295 of 1,311 delivery envelopes, 162 of 172 rows and 581 claim payloads were at formats no current Leaf reads, and 207 of 237 sessions' claim partitions pointed only at claims that were gone. This moves the facts that belong to the page's log into the log, folds the records that share a writer and lifetime, and retires state-home records by their pages whatever Leaf wrote them. It bumps the page protocol to 16.

What moves into the log

  • The acknowledgement cursor is now a kernel receipt event carrying through, written by delivery.receive_batch (record_receipt); the cursor is the greatest through (service.acknowledged). cursor.json, its clamp and the page init unlink are gone: a fresh log has no cursor by construction. A receipt is a separate kind from pickup because neither implies the other.
  • The agent's status declaration is now a kernel status event (state, detail), replacing both status.json's declaration and put_down, whose only writer was leaf status. A declaration puts down every start before it. The Codex App Server stream stays a file, renamed stream.json (service.STREAM).

Both are page-authored or agent-authored, so admission gives them no attention: they are never input, unread news or deliverable, and leaf wait doesn't wake on them.

What folds

  • data.json into each data/<source>.json, which now carries its contract; leaf data clear removes the source entirely.
  • viewed.json into user-views.json's viewed stamp; the browser never read it, so it leaves the served state.
  • sessions/<key>.titles.log, which nothing in Leaf read, into the page's observations.jsonl (the journey reads it there).
  • Reconnect notices are named sessions/<key>.reconnect, without the harness.

Retirement

  • state.retire_if_gone reads a record's pages at page or each batch's page, which every format names, and removes the record once they are gone: envelopes at each freeze, Codex task records, and rows by their page's server lease (presence._row_reading).
  • A claim on a gone page retires with its locator in any scan (state.held_claims, standing_claims, retire_claim). Publication, removal and retirement now change a claim's payload and locator under one claims lock, so a retirement never removes a claim published meanwhile. page init removes the old claimant's locator too. holds_claims, which the hook gate reads, counts only claims naming the session on a page that stands, so a session whose pages are gone stops running its --if-claimed hooks.
  • A page counts as gone only when stat finds nothing there, so a page behind an unreadable folder keeps its claim. A page on a volume that is unmounted reads as gone, since nothing tells the two apart, and its session takes it again with leaf server start once the volume is back.
  • A session that ends holding no claim loses all its files, its lifecycle, lock and reconnect notices included.

Not changed

  • The hooks' empty marker files and the server_id copy in server.lock stay: folding either would add a locked write to every hook, or a stale-incarnation window at server start.
  • The code that stops a server whose service.json an older Leaf wrote stays, since a later change to that record's shape would otherwise leave every running server unstoppable and the session's automatic re-vendor refusing.

The journeys-tier tests this edits ran in this PR's test job; leaf-dev journey was not run against a live harness.

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

max-sixty and others added 21 commits October 20, 2026 14:25
Since #3244 each state.Record's format names the protocol at which its own
shape last changed, so a delivery's format is not `leaf-delivery/<protocol>`
and the cold SessionEnd path reads no registry. The delivery reference now
leaves the number unstated, and the cold-install test stops copying the
registry into its plugin, so it proves SessionEnd runs without one.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
A page server recorded each title request it made in the claimant
session's `sessions/<key>.titles.log`, under a page transaction, and the
session's end removed it. Its only reader was the developer journey,
which proves the page server titled a thread. The page's
`observations.jsonl` is the stated home for page-local diagnostics,
including the server's own request rows, so each title request is now a
server row there (`thread_title_generated`, `_failed`, `_skipped`), read
through `interaction_log.read_interactions`.

This deletes `TITLES_SUFFIX`, `leases.titles_log`, the log's retirement
at SessionEnd and the claim gate the session-scoped log needed. A request
that outlives its session still writes no title; its row now says
`written: false` where the old test asserted the log's absence.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
`cmd_stop` discarded a `service.json` this Leaf could not read, and
reported a server stopped when the lease was held over one;
`restarting_server` stopped such a server before a re-vendor. No current
Leaf writes such a record: within one protocol every Leaf reads the same
`server.SERVICE` format, so only a server an earlier runtime started
leaves one. Root `AGENTS.md`, "Stage", spends no code on earlier
runtimes.

A stop that finds no record this Leaf can read now returns at once,
since it has no service to disable and no server of this Leaf to wait
for; before, it would wait on the lease. This deletes the two tests that
built an older Leaf's server. Behavior that changes: after a future
change to `SERVICE`'s shape, a server started before the upgrade keeps
running until its own lifetime ends it, `server stop` reports none
running, and a re-vendor refuses while it holds the lease.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Every retirement read through `Record.read`, which takes only its own
format, so each format change left the previous generation in the state
home for good: on one machine 1,295 of 1,311 envelopes, 162 of 172 rows
and 581 claim payloads were at formats no current Leaf reads. Every kind
names its pages at `page` or each batch's `page` in every format, so
`state.retire_if_gone` now reads those keys raw and removes a record
whose pages are gone. Envelopes, Codex task records and rows (by their
page's server lease) retire this way.

A claim's locator outlived its payload: `page init` deleted the payload
and left the old claimant's locator, and `holds_claims` counted locators
unchecked, so 207 of 237 sessions' partitions pointed only at claims that
were gone. Their hook gate never skipped and their files never ended.
`state.held_claims` now admits a locator only when its payload names the
session on a page that stands, and retires the claim of a page that is
gone with its locator; `retire_claim` moves the payload aside first and
puts back a successor's. `page init` removes the old claimant's locator
too (`remove_claim`).

A session that ends holding no claim now loses its lifecycle, lock and
reconnect notices as well: nothing reads them once no claim names it.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
`data.json` (`data.CONTRACTS`) recorded the contract each source was set
under, beside the per-source `data/<source>.json` publications that
`write_data` already replaced with it atomically, under the same page
transaction, read together by every reader. Each publication now
carries its `contract` (`data.PUBLICATION`, since 16), and the page holds
a source exactly while its file exists. `read_contracts` reads the
publications through the same stamp-keyed memo `read_data` uses.

The recorded contract stays rather than being derived: binding a source
takes `working_data_bindings` over every revision and markup event,
which a state read would otherwise pay on every browser poll. The module
header said readers judge a value "against the contract the source is
bound to now"; they judge it against the recorded contract, and the
header now says so.

Behavior that changes: `leaf data clear` removes the source whole. It
used to delete the value and keep a contract-only entry, which read as
`{"contract": ...}` with no value; the browser treated that and an
absent source alike (both draw `null`), and the next `data set` takes
the current binding either way. A cleared source now counts as
unsupplied for a datum comment, and a sample copies only sources with
a valid value. `page state`'s `data` drops its `file` key.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
`viewed.json` (`presence.VIEWED`) held one timestamp the server renewed,
at most every 30 s, whenever a visible tab asked `/api/news`. Beside it,
`user-views.json` already records each tab's view: a visible document
reports at least every 10 s (`runtime/user-view.js`), and a hidden one
sends no heartbeat. Both are disposable, unwatched browser observations
the page server writes for the agent.

The user-views record now keeps `viewed`, stamped by each visible report
(`user_views.viewed_at`, since 16). It is a top-level field rather than a
reading of the per-session entries, since those are pruned after a day
and the agent reads `null` as "nobody has opened it". The news door no
longer writes anything.

Nothing in the browser read `viewed`, so the served state stops carrying
it (`state.schema.json`); `page state` reads it from the user-views
record. Deleted with it: `VIEWED`, `VIEWED_FILE`, `renew_viewed`, its
throttle, and two tests of the news door's write (the process test and
the shared-website-server one). Behavior that changes: `viewed` now
renews about every 10 s rather than 30 s while a tab shows the page, and
a public leaf.page visitor who has not acted, who sends no view reports,
no longer moves it.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
The acknowledged position in a page's log lived in `cursor.json`, beside the
log it named. Two mechanisms kept it from outliving that log: `read_cursor`
clamped a seq past the log's end to 0 (re-reading the whole log from disk on
every call, even inside a transaction holding the events), and a fresh
`page init` deleted the file.

Receipt now appends a kernel `receipt` event carrying `through`, the seq it
confirms, and the cursor is the greatest `through` the log holds
(`service.acknowledged`), computed from the events the caller already has.
`delivery.receive_batch` is the one writer, through `record_receipt`, so every
acknowledging path (Claude Code and Pi inline hooks, `leaf delivery ack`,
`leaf wait --ack`, the Codex adapter and App Server receipts, the abandoned
Codex batch) records it. A receipt the cursor has already passed writes
nothing, so the cursor only moves forward and a retry appends nothing. A fresh
log has no cursor by construction.

A new kind rather than a `through` on `pickup`, because neither implies the
other: queued and failed pickups confirm no receipt, and a receipt whose
inputs were already picked up, or that the harness gave up on, records no
opened pickup. Extending `pickup` would have meant an empty `events` list,
a second meaning per phase, and changing its dedupe rule. The receipt is
authored by `page`, so admission gives it no attention: it is never input,
pending news, or deliverable, and `leaf wait` does not wake on it.

Deleted: `cursor.json`, `event_log.CURSOR`, `read_cursor`, `write_cursor`,
the clamp, the vendoring unlink, `schema.CURSOR_FILE` and `LOG_STATE_FILES`,
and the tests that only protected them (the clamp, the fresh-log unlink, and a
Codex recovery over a cursor reset within one log, a state no writer now
produces). The seeded page fixture acknowledges its seed with a receipt.
Bumps `$events.protocol` to 16.

Behavior tests changed: assertions on `cursor.json` read the derived cursor;
tests that took the log's last event as the pickup now select the pickup by
kind; two regtest snapshots and one printed seq move by one for the receipt.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
The served state stops carrying `viewed`, and the `data-source` and
`user-views` records change shape (each now `since` 16), so a page an
older Leaf vendored would misread what this one serves and keeps. The
baseline drops the `data` and `viewed` records with their files.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
`reconnect.NOTICES` lived at `sessions/<key>.<harness>.reconnect`, and
every helper took the harness to build that path. The key already names
one harness's session: the lifecycle beside it, `sessions/<key>.lifecycle`,
carries no harness. The notices now live at `sessions/<key>.reconnect`
(`state.RECONNECT_SUFFIX`), and `_retire` keeps that path by name beside
the lifecycle and lock rather than by a suffix test.

The notices stay a record of their own rather than fields of the
lifecycle record. Every lifecycle write bumps `revision`, and Codex's
adapter, delivery receipt and `carry_turn` compare whole lifecycle
records as their compare-and-swap token; a notice written into it, bumped
or not, would invalidate the reading the same prompt hook confirms its
delivery under. The lifecycle is also a page input
(`served_state.reading._outside`), so each notice would wake every
watcher of the session's pages.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
# Conflicts:
#	skills/leaf/scripts/leaf/delivery.py
`test_a_later_version_can_reuse_a_source[True]` asserted that a cleared
source kept its contract, the behavior the data-contract fold changed.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
`how-it-works.html`'s table of bookkeeping files still named the file
the viewed-stamp fold deleted.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
# Conflicts:
#	build/browser/protocol-baseline.json
#	skills/leaf/scripts/leaf/page-storage.md
#	skills/leaf/scripts/leaf/presence.py
#	skills/leaf/scripts/leaf/state.py
#	tests/interact_support.py
#	tests/journeys/test_site.py
#	tests/model/test_session_lifecycle.py
Reverts 13a6589. The handling it deleted is how `server stop` and a
re-vendor end a server an earlier Leaf started once `server.SERVICE`'s
shape moves: it removes the unreadable record so that server's own
supervisor exits. Without it, after such a change, every server running
at upgrade outlives `server stop`, and the session's automatic re-vendor
refuses on every attempt while that server holds the lease. That is a
live process across a Leaf update, which "Pages across Leaf updates"
keeps working, rather than an old record to read.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
The agent's `waiting` or `idle` declaration was a field of `status.json`,
beside the harness's live stream, while the work it puts down was a
`put_down` event in the log. The declaration is now a `status` event in
`events.jsonl`, admitted through `append_admitted` like every event, and
the file keeps only the stream, renamed to `stream.json`
(`service.STREAM`, since 16). Protocol 16.

- `status` replaces `put_down`: every `put_down` writer was a status
  declaration, so a declaration itself ends every start before it
  (`todos.item_starts`). The event is the agent's: attention false, no
  unread, not delivered.
- `activity.declaration` reads the newest `status` event; every reader of
  `read_status` moved to it or to `service.read_stream`. A start after an
  `idle` reopens the page as `waiting` in that reading, which deletes the
  reopening writes in `todos.take_in_hand` and `thread.post_reply` (a
  declaration written after the start would put it down). A copy a host
  serves without a process still reads no declaration.
- The watch's pre-lock status read is gone; `tick` reads the declaration
  under the page's lease.
- Behavior changes: `leaf status` prints the appended event record instead
  of `{state, detail, ts}`. The website harness's turn-end `waiting` now
  puts down the turn's starts, as an agent's own `leaf status waiting`
  does, so an update left in hand reads as an ended pickup rather than a
  stalled start. A status write moves the log, so a page whose source
  failed to activate is checked again.
- `stream.json` keeps `status.json`'s place in `SESSION_FILES`, so its
  watch, freshness and record-reading treatment is unchanged.
- Contracts updated: events.md, session-lifetime.md ("Page activity"),
  page-storage.md, the module headers, the served state's `status`
  description and the how-it-works page.

Tests: declarations are seeded as `status` events (`declare`) and read
back with `declared`; stream assertions read `stream.json`. The five race
tests that paused a wait at its pre-lock status FIFO now pause it at its
first page transaction (`held_selection`). Tests that hardcoded log
sequence numbers after a `leaf status` take them from the records they
append. The idle-with-a-start-in-force case of the closed-page deadline
test is gone, since that state can no longer arise; the test keeps the
deadline. New: a start reopens an idle page.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
# Conflicts:
#	build/browser/protocol-baseline.json
#	skills/leaf/scripts/leaf/events.md
#	skills/leaf/scripts/leaf/presence.py
#	skills/leaf/scripts/leaf/schema.py
#	skills/leaf/scripts/leaf/served_state/reading.py
#	tests/conftest.py
#	tests/journeys/test_site.py
#	tests/model/test_hooks.py
#	tests/model/test_receipts.py
#	tests/model/test_wait.py
… leases

A retirement that moved a gone page's payload aside could still delete the
locator of a claim the same session published meanwhile, losing it to that
session's discovery and hook gate. Publication, removal and retirement now
change a claim's payload and locator under one claims lock, taken after any
page or session lock, so retirement removes a claim only if it is still the
one read.

A row at a format this Leaf can't read was checked for retirement only when
first scanned, so one whose server was alive then stayed after the server
ended. Such a row is now a neighbour candidate without a record, by the page
it names, and `other_leaves` checks its lease on every reading as it does for
readable rows.

A visual-review journey still read the removed `data.json`.

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

`pages_gone` read `Path.is_dir`, which is false for a page this process
can't stat, so a claim on a page behind an unreadable folder or on an
unmounted volume was retired; under Python 3.9 the same input raised out of
the hook gate. A page is gone now only when stat finds nothing there.

Retiring a session's last claim leaves its locator directory empty, so
`_drop_claim` removes it under the claims lock, under which publication now
creates it.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
leaf-agent
leaf-agent previously approved these changes Oct 20, 2026
A seeded page's log now ends with the receipt that acknowledges the seed and
then the fixture's closing `status` event, so the site journey checks the
receipt covers everything before it rather than all but the last event. The
Codex adapter journey waited for the cursor to reach seq 1, where its first
comment now sits at a later seq.

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

@leaf-agent leaf-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The claim retirement path treats a missing page as permanently deleted. If a page is inside a mounted volume and that volume is unmounted, the mountpoint can remain while the page path becomes absent; page_stands() then returns False, and held_claims() or standing_claims() permanently removes the claim and its locator. Remounting the volume cannot restore that ownership. The PR explicitly promises to preserve claims on an unmounted volume, so this needs a way to distinguish that case before irreversible retirement, or a decision to narrow the promised behavior.

max-sixty and others added 2 commits October 20, 2026 16:11
The retirement docstring promised a page on an unmounted volume would keep
its claim, but its path is missing then, which nothing tells from a deleted
page. Its session takes the page again with `leaf server start`.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
# Conflicts:
#	skills/leaf/assets/registry.json
@max-sixty
max-sixty merged commit be98116 into main Oct 20, 2026
3 checks passed
@max-sixty
max-sixty deleted the state-json-files branch October 20, 2026 23:32
max-sixty added a commit that referenced this pull request Oct 21, 2026
Applies #3256's Stop rule to Codex. #3256 made Claude Code's and Pi's
Stop hook hand no late page input into a finishing turn where a watch
follows it, so the turn ends on its answer and the input gets a turn of
its own. Codex's Stop hook still blocked the turn's end and handed a
late comment in, so the agent wrote one more message after its answer,
and that reply became the turn's final message.

### What changes

- **One predicate says whether a later turn takes late input**
(`Harness.later_turn_takes_input`). It is the watch between turns for
Claude Code and Pi (`watches_between_turns`), and the live delivery
adapter for Codex (`adapter_is_live`). `hook_transport.carry_turn` and
`CodexHarness.offer_at_step` both read it, so Codex's Stop offers
nothing while the adapter runs. The turn closes, and the adapter offers
the input in a turn of its own: a `turn/start` with a `leaf_delivery`
tool output over App Server, or the queue's next turn through `codex
queue`. A Codex task that waits with `leaf wait` has no adapter, so its
Stop still hands input over, as under plain `claude -p`.
- **An owed update this owner holds no usable reference for counts as
input, not as a debt** (`hook_transport.read_plans`). Such an update,
received by another session before the page changed hands, is already
deliverable again (`delivery.unreferenced`). Stop used to remind the
agent about it as well, which with Codex's adapter running continued the
turn with a reminder naming the `leaf wait` that would deliver its
reference. Now Stop hands it over with the other input or leaves it to
the later turn. The same applies to Claude Code and Pi with a watch.
- Prose in the `codex.py` and `hook_transport.py` headers,
`session-lifetime.md`, `harness-codex.md` ("Delivery"), the site's "Stop
hook checks" and `TODO.md` now says Stop hands input over only where
nothing opens a later turn for it.

### Codex's TUI shows every message of a turn

The question was whether replacing the final message matters in Codex
the way it does in Claude Code's focus view. In codex-cli 0.160.0's TUI,
a turn asked to send a progress message, run a command and then answer
showed all three in the transcript (`• ALPHAONE`, `• Ran echo hi`, `•
OMEGATWO`). So the TUI never hid the answer. What this change keeps in
Codex is that a turn's final message is its answer, and that late input
gets a turn of its own; surfaces that show a turn's final message alone
were not checked.

### Tests

- New `test_a_codex_stop_leaves_late_input_to_the_adapters_own_turn`:
with the adapter live, a comment collected as the turn ends gets no Stop
output, the turn closes, and the adapter queues it once. It fails with
the Stop early return removed (Stop blocks with the envelope).
- `test_a_stop_reminder_recovers_the_exact_reply_address`: the handoff
cases now reach the new owner through the adapter's offer after Stop.
They fail without the `read_plans` change (Stop continues with the
reminder).
-
`test_codex_stop_leaves_quiet_input_but_delivers_the_whole_batch_with_owed_input`
is deleted: it asserted that an owed comment keeps the turn open with
the adapter running, the behavior this PR reverses.
`test_a_codex_stop_keeps_the_revision_returned_by_its_offer` now runs
without an adapter, where Stop still offers.
- The `codex-app-server` and `codex-queue` journeys pass. Their `resume`
step now runs on both transports and requires that a comment sent during
an empty-input resumed turn, whose first boundary is its Stop, is
answered in a turn of its own rather than handed into the resumed turn.
- Focused suites pass, except
`test_codex_app_server.py::test_a_turn_that_shares_a_user_message_leaves_the_delivery_owed`,
which fails the same way on `main` (`KeyError: 'status.json'`, from
#3380 meeting #3384).

> _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