Skip to content

Keep every persisted record through one primitive, and retire dead session files - #3202

Merged
max-sixty merged 5 commits into
mainfrom
record-cutover
Oct 20, 2026
Merged

max-sixty merged 5 commits into
mainfrom
record-cutover

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

Leaf keeps about twenty JSON records outside a page's log and document. Three carried a format and a schema; the rest were dict literals written at scattered sites and read with key-presence checks, .get(...) or {} chains or raw indexing, each reader deciding for itself what an unreadable record means. This change gives every record one form, and stops the state home growing by a file pair for every session of every harness Leaf is installed in.

The primitive

state.Record(kind, schema) is the one form of every such record. Each kind is declared once, with its JSON Schema, beside the module that owns it, and that module's functions are its only writers. write stamps format (leaf-<kind>/<protocol>) and replaces the file atomically. read returns the record less its format, or None for a record this Leaf does not read: a missing file, one at another protocol or with no format, or one lacking a field its schema requires at any declared depth, conditional allOf/if/then clauses included, as a record another checkout wrote before a field was added. A file that is not JSON raises, naming it. state.py owns the primitive because the SessionEnd entry runs it on system Python 3.9.

Nothing validates a record whole while Leaf runs, since only Leaf writes them and the hooks that read the lifecycle and claims must not load the schema engine (test_hook_registration and test_codex_adapter hold that). The suite checks every record a test writes against its schema instead (tests/conftest.py, declared_records), as #3185 does for the served state.

Records cut over

  • Page directory: status.json (service.STATUS), cursor.json (event_log.CURSOR), viewed.json (presence.VIEWED), service.json (server.SERVICE, written only through server.write_service), preview.json (server.PREVIEW, whose shape is the served state's preview), data.json and data/<source>.json (data.CONTRACTS, data.PUBLICATION, written only through data.write_data), files.json (file_bindings.BINDINGS), user-views.json (user_views.USER_VIEWS).
  • State home: claims (service.CLAIM, written only by publish_claim), session lifecycles (state.SESSION), reconnect notices (reconnect.NOTICES, now an object), neighbor rows (server_rows.ROW), delivery envelopes (delivery.DELIVERY), Codex task records (codex.RECORD), the Codex adapter's lease contents (codex_adapter.ADAPTER), the bridge's address and registrations (bridge.ADDRESS, bridge.REGISTRATION), and leaf.page's publication fingerprints (leaf_website.storage.PUBLISHED).
  • Left out: access.json, whose one field never changes meaning; a format would rotate the machine's key, signing every open tab out, at each protocol bump. Claude Code's own job records are not Leaf's. Two dev tools read records raw on purpose: leaf_dev/arms.py runs inside another payload's interpreter, which may predate Record, and usability_eval.py copies files verbatim as judging evidence.

Deleted: registry.kernel.record_format, SESSION_FIELDS, CLAIM_IDENTITY, readable_claim (now claim_reading), readable_delivery, SERVICE_KEYS/LIFETIMES, the read-time jsonschema checks of Codex task records, neighbor rows and user views, and retire_if_gone's shape checks. status.json always carries its stream with activity, reply and reply_reservations, so its readers index it. samples.py writes a child page's data through data.write_data and its starting history through the new event_log.start_log, which page init also uses, and writes no status. Its events were already admitted against the child's view, and a copied thread keeps its parent's ids, so append_admitted would admit them twice.

Session files

The SessionEnd hook used to write a lifecycle record and a lock for every session of every harness Leaf is installed in, used or not: 4,647 of 4,961 lifecycle files on one machine. A session that left no file now ends without one. At its end, its lifecycle, session lock and reconnect notices stay, since a failed takeover can restore a claim naming it: the page shows that session's turn from the lifecycle, and its resumed hooks read the notices. A session holding no claim loses every other file no process holds a lock on, so a running watch keeps its lease; one holding a claim loses only the observations that end with it. session-lifetime.md, "A session's files", owns the rule.

Behavior changes

  • A record another Leaf wrote reads as absent wherever it is read, including a Codex task record or envelope of the current format that lacks a required field. A wrong-typed value is no longer rejected on read.
  • A malformed files.json reads as no bindings rather than refusing.
  • leaf status prints the declaration without the stream.
  • A delivery this Leaf does not read is refused as "written by another Leaf".
  • Records on disk from before this change read as absent once, as at a protocol bump; the next claim or serve writes them again.

Size

Against main the branch adds 858 and deletes 582 product lines (+276), and adds 499 and deletes 327 test lines. Of the product lines, about 200 are the new schema declarations, which replace shapes that were implicit, about 60 are the session-file retirement, and about 100 are the primitive with its docstring; the code it replaced shrank. The cutover replaces a dozen ad hoc read checks with one.

Tests

test_a_session_keeps_its_files_only_while_something_can_read_them holds the retirement. The drift tests in test_claims, test_concurrent_deliveries, test_user_views, test_session_lifecycle and process/test_interact_server hold absent-on-drift, and test_the_registered_session_end_releases_shared_claims_without_an_environment holds the system-Python SessionEnd. Fixtures write records through interact_support.write_record and write_row; a test of an earlier Leaf's record writes it raw.

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

max-sixty and others added 5 commits October 19, 2026 18:07
…ssion files

Leaf keeps about twenty JSON records outside a page's log and document.
Three carried a `format` and a schema; the rest were dict literals read
with key-presence checks, `.get(...) or {}` chains or raw indexing, each
reader deciding for itself what an unreadable record means.

## The primitive

`state.Record(kind, schema)` is the one form of every such record. Each
kind is declared once beside its owner, whose functions are its only
writers. `write` stamps `format` (`leaf-<kind>/<protocol>`) and replaces
the file atomically; `read` returns the record less its `format`, or
None for one this Leaf does not read: missing, at another protocol or
with no format, or lacking a field the schema requires at any declared
depth (a record another checkout wrote before a field was added). A file
that is not JSON raises, naming it. `state.py` owns it because the
SessionEnd entry runs it on system Python 3.9.

Nothing validates a record whole while Leaf runs: only Leaf writes them,
and hooks that read the lifecycle and claims must not load the schema
engine (`test_hook_registration`, `test_codex_adapter` hold that). The
suite checks every record a test writes against its schema instead
(`tests/conftest.py`, `declared_records`), as #3185 does for the served
state.

## Records cut over

Page directory: `status.json` (`service.STATUS`), `cursor.json`
(`event_log.CURSOR`), `viewed.json` (`presence.VIEWED`), `service.json`
(`server.SERVICE`, written only through `server.write_service`),
`preview.json` (`server.PREVIEW`), `data.json` and `data/<source>.json`
(`data.CONTRACTS`, `data.PUBLICATION`, written only through
`data.write_data`), `files.json` (`file_bindings.BINDINGS`),
`user-views.json` (`user_views.USER_VIEWS`).

State home: claims (`service.CLAIM`, written only by `publish_claim`),
session lifecycles (`state.SESSION`), reconnect notices
(`reconnect.NOTICES`, now an object), neighbor rows (`server_rows.ROW`),
delivery envelopes (`delivery.DELIVERY`), Codex task records
(`codex.RECORD`), the Codex adapter's lease contents
(`codex_adapter.ADAPTER`), the bridge's address and registrations
(`bridge.ADDRESS`, `bridge.REGISTRATION`), and leaf.page's publication
fingerprints (`leaf_website.storage.PUBLISHED`).

`access.json` stays unversioned: its one field never changes meaning, and
a format would rotate the machine's key, signing every open tab out, at
each protocol bump.

Deleted: `registry.kernel.record_format`, `SESSION_FIELDS`,
`CLAIM_IDENTITY`, `readable_claim` (now `claim_reading`),
`readable_delivery`, `SERVICE_KEYS`/`LIFETIMES`, the read-time jsonschema
checks of Codex task records, neighbor rows and user views,
`retire_if_gone`'s shape checks, and the pre-publication data special case.

`status.json` now always carries its `stream` with `activity`, `reply`
and `reply_reservations`, so its readers index it. `samples.py` writes a
child's data through `data.write_data` and its seeded history through
the new `event_log.start_log` (which `page init` also uses), and no
longer writes a status.

## Session files

The SessionEnd hook used to write a lifecycle record and a lock for every
session of every harness Leaf is installed in, used or not: 4,647 of
4,961 lifecycle files on one machine. Now a session that left no file
ends without one. At a session's end, while a claim names it, its
lifecycle and reconnect notices stay, since its pages show its turn from
the lifecycle; once none does, every file of it that no process holds a
lock on goes, the session lock last. A claim transfer retires the
previous session the same way once it has ended
(`state.retire_session`). `session-lifetime.md`, "A session's files",
owns the rule.

## Behavior changes

- A record another Leaf wrote reads as absent wherever it is read,
  including a Codex task record or envelope of the current format that
  lacks a required field; a wrong-typed value in one is no longer
  rejected on read.
- A malformed `files.json` reads as no bindings rather than refusing.
- `leaf status` prints the declaration without the stream.
- A delivery that this Leaf does not read is refused with "written by
  another Leaf".
- Records on disk from before this change read as absent once, as at a
  protocol bump: claims, lifecycles and services are written again by the
  next claim or serve.

Tests: `test_a_session_keeps_its_files_only_while_something_can_read_them`
holds the retirement; the drift tests in `test_claims`,
`test_concurrent_deliveries`, `test_user_views`, `test_session_lifecycle`
and `process/test_interact_server` hold absent-on-drift;
`test_the_registered_session_end_releases_shared_claims_without_an_environment`
holds the system-Python SessionEnd. Fixtures write records through
`interact_support.write_record` and `write_row`; a test of an earlier
Leaf's record writes it raw.

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

Fixes what the independent review of the record cutover found.

A record's read now finds the fields required inside an `allOf` clause,
and inside a conditional one when its `if` names, by `enum` or `const`,
values the record holds, so a Codex task record whose state or answer
kind requires a field it lacks reads as absent instead of raising later.
`codex.read_record` drops its own variant-field check, which the schema
now covers. A user view's `checks` and the status stream's `activity` and
`reply` declare their full shapes, and `preview.json` takes its shape
from the served state's `preview`, so a record missing a field its
readers index reads as absent.

A claim transfer no longer retires the previous owner's files: a failed
takeover restores the claim it replaced, which needs that session's
lifecycle. A session ending while it holds a claim keeps its lifecycle,
lock and reconnect notices for good; one ending without a claim keeps
only leases another process holds. `session-lifetime.md`, "A session's
files", says so.

Tests: `test_concurrent_deliveries` gains the collecting and answer
missing-field cases back; `test_a_session_keeps_its_files_only_while_something_can_read_them`
covers the end-time rule without the transfer; `tests/conftest.py`
checks a record less its `format` against its schema, which closed
schemas such as `preview` need.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
# Conflicts:
#	skills/leaf/scripts/leaf/bridge.py
#	skills/leaf/scripts/leaf/data.py
#	skills/leaf/scripts/leaf/delivery.py
#	skills/leaf/scripts/leaf/file_bindings.py
#	skills/leaf/scripts/leaf/presence.py
#	skills/leaf/scripts/leaf/server.py
#	skills/leaf/scripts/leaf/server_rows.py
#	skills/leaf/scripts/leaf/service.py
#	tests/conftest.py
#	tests/model/test_interact_document.py
#	tests/process/test_interact_server.py
#	worker/leaf_website/storage.py
The second review found that a session ending between another session's
takeover of its last page and that takeover's failure lost its lifecycle,
so the restored claim showed no turn and its unanswered work read stale
rather than ended and overdue. A session's end now keeps its lifecycle
and lock whether or not it holds a claim, and removes its other files
that no process holds a lock on once it holds none. Sessions that never
used Leaf still end without a file, which was 94% of the lifecycle files
on one machine.

`test_a_session_keeps_its_files_only_while_something_can_read_them`
holds the new rule; `session-lifetime.md`, "A session's files", states it.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
The third review found that a session whose last claim a takeover took
lost its reconnect notices at its end, so a failed takeover that
restored its claim would let its resumed hooks repeat a notice for the
same outage. A session's end now keeps its notices beside its lifecycle
and lock, which restores what `session-lifetime.md` already said: the
notification record survives SessionEnd.

`test_a_session_keeps_its_files_only_while_something_can_read_them`
now holds a kept notice.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@max-sixty
max-sixty merged commit fd46034 into main Oct 20, 2026
3 checks passed
@max-sixty
max-sixty deleted the record-cutover branch October 20, 2026 01:57
leaf-agent added a commit that referenced this pull request Oct 20, 2026
The [main CI
run](https://github.com/max-sixty/leaf/actions/runs/39349858182) failed
in two browser tests. Current `main` passes the theme-height test after
#3200. The data-publication test's original server 500 is also gone
after #3202 made an unreadable stored publication read as absent, but
#3202 added a `format` field to publication files and left this test
comparing the whole stored record to three browser fields.

Compare `run`, `updated`, and `value` on both sides. This keeps the
test's checks that each rerun reaches the widget and that corruption,
clearing, and recovery publish the expected states. On current `main`,
the focused data test failed at the mismatched `format` field before
this change; with this change, both tests from the failed run pass (`2
passed`).

---------

Co-authored-by: leaf-agent <[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