Skip to content

Give every record kind a verified window of shapes it reads through its kept keys, and keep drafts, grants, the page key and a page's address across a shape change - #7114

Closed
max-sixty wants to merge 32 commits into
mainfrom
agent-a48dd0c67af58cb77
Closed

max-sixty wants to merge 32 commits into
mainfrom
agent-a48dd0c67af58cb77

Conversation

@max-sixty

@max-sixty max-sixty commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Builds on #7061, which added state.Record.kept and Record.read_any.

After #7061, only seven record kinds declared kept keys, and every other kind read a record of another shape as absent and replaced it at the next write. A Leaf update that changed a shape, or an installed copy and a checkout sharing the state home, so deleted unsent drafts, dropped all but the newest connection grant, minted a new page key that signed out every tab, and moved a restarted page to a new port.

What each kind reads. Every state.Record now declares kept and kept_since. read_any reads a record of a shape from kept_since on through the kept keys, and an earlier one as none. The writers below start from it.

Kind Keeps From since
drafts store, version; each draft's attempt, text, version, payload, held 51
http-connections each grant's five fields 19
access token 13
service every required field 13
clerk each line and its wake 52
data-source the publication's four fields 16
user-views viewed 13
session, claim, codex-adapter, file-bindings, layer, delivery, codex-delivery as #7061 declared 13, or 36 for layer
bridge, bridge-session, completion, pointed, preview, render-reading, row, stream, website-publication nothing their earliest verified shape

A carried drafts store keeps its identity and moves its version on, so tabs hear the same store continue.

How the window is derived. protocol-baseline.json holds each kind's shapes from kept_since on, without annotations, seeded from its own git history back to the earliest shape whose kept keys mean what they mean now: the build checks that the shapes agree in structure, and the author judges meaning, as clerk's window starts at 52 because a since-44 line could name a turn it left the queue with. The build refuses a kept_since that names a shape the history doesn't hold, and checks each kept path against every shape in the window: each step's types, key and required flag, and the kept key's schema. A failing path's refusal names the kept_since that would leave the failing shape out. leaf.page's store-format test pins the drafts since.

What "Page protocol" now promises. A reader reads kept keys from every shape in a kind's window and nothing earlier. Every shape in the window agrees on each kept path. A kept key keeps its meaning, which no schema comparison sees. A kept key's schema says what every reader accepts: the session record's lifetime is now any object, so the suite no longer checks the five lifetime forms. A new shape adds no field whose loss would change what a record grants.

The reviewer's probe (format rewritten to /999): on #7061's head, drafts read {}, grants ['second'], and the key and port change; here both drafts, both grants, the same key and the same port survive. Max asked for this change; independent agents reviewed each round.

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

A checkout's `bin/leaf` no longer makes itself the session's Leaf when it
claims, serves or initializes a page the session holds, and
`leaf-dev preview --user` no longer selects the launching checkout. The
session's hooks, pages follower, bare `leaf` and Codex adapter run the copy
the harness loaded; a checkout runs only the command it was given and any
page server that command starts. The maintainer asked for this on the page
comparing ways for a Leaf-dev session to run Leaf, since sessions should
start normally on the installed plugin rather than with --plugin-dir.

Removed: `session_runtime.py` and every hand-off to it (`hooks.main`'s
exec, the follower's selection check, `cli`'s `hand_off_command`,
`vendoring._deliver_from_here`, the selection calls before a claim in
`service` and `hosting`, the Codex adapter's exit on selection and the
claim's wait for it, preview's `launching_checkout`), and the `launcher`
field of the session lifecycle and Codex adapter records. `renew_layer`
loses its `command` flag. `bin/leaf` already ran only its own tree since
#6758, so it needs no change beyond its comment.

Kept as the one guard: a Leaf at a later page protocol than the Leaf a
live session's hooks run refuses to take a page that session holds to its
own protocol, by renewal or `page init` (`vendoring._refuse_unheard`,
finding `unheard-protocol`); a re-init that moves no protocol passes. It
reads the hooks' Leaf from the root each hook already records
(`state.hooks_root`) and its protocol from that tree's kernel registry, so
hooks write nothing new. Such a move can't be undone, since no Leaf takes
a page back down. A claim gets no check:
since #6954 the hooks refuse a page at another protocol and say so, and
the agent can hear such a page with the checkout's `leaf wait`. The
`newer-layer` refusal now names the page's protocol, so that report tells
a checkout ahead of the plugin from a plugin update.

A Codex task's new adapter starts through the launcher of the copy the
task's hooks run (`state.hooks_launcher`), whichever Leaf claims, so a
checkout's claim no longer puts the task's delivery on the checkout.

Protocol: the session lifecycle and Codex adapter records change shape,
so each moves `since` from 13 to 56, the current protocol; per
`layer-registry.md`, "Page protocol", that needs no bump, since neither
`since` named 56 already and a reader treats a record at another format as
absent rather than misreading it. No logged event changes. A bump would
not help the one cost this leaves: a checkout and the session's own Leaf
now write the session's records side by side, so until both keep them at
the same `since`, each reads the other's lifecycle as absent and replaces
it. `session-lifetime.md` now says so.

Behaviour changes in tests: the selection tests go (bare command and hook
hand-off, the bridge before hand-off, selection on claim, init and
renewal, the Codex adapter's replacement on selection); the follower's
"selected" case goes; new tests cover the rewrite refusal and the adapter
starting from the hooks' copy.
A Leaf whose page protocol differs from that of the Leaf the session's
hooks run now refuses a claim for the session (`service.refuse_unheard`,
which every claim prepares through, `leaf-dev preview --user` included),
in both directions, as well as a renewal or `page init` that takes a held
page from an older protocol to its own. A re-init that moves no protocol
passes; the check before refused any re-init of a held page while the
hooks ran an older Leaf, whose advice then named a Leaf that couldn't read
the page.

Why: a worktree is usually behind the plugin's protocol, and from there a
claim let the session's hooks renew the page up to their protocol and
restart its server under the plugin, so the user reviewed the plugin's
runtime in place of the change. A probe of the prompt, tool-batch and Stop
hooks renewing such a page printed nothing at all. Ahead, the hooks refuse
the page, but only at the next prompt. The refusal now arrives at the
command and names the route: merge main when behind; serve the page
unclaimed and hear it with that checkout's `bin/leaf wait PAGE` when ahead,
or `/reload-plugins` once the installed copy has caught up. A preview
--user from such a checkout gets the same refusal rather than a fallback
that serves it unclaimed, which would need code of its own.

`state.protocol_at` takes over `registry.kernel.installed_protocol`, so
`service`, below `registry` in the import order, can read a tree's
protocol. Tests: the claim refusal in both directions, and a re-init that
moves no protocol.
…g-leaf

/developing-leaf told agents to run every `leaf` command through the
checkout's launcher, conversation pages included. With checkout selection
gone, a page the session holds stands at the protocol of the copy its
hooks run, so a worktree behind that copy, the usual case, is refused on
the session's own page. "Run `leaf`" now splits the two cases: a page the
session holds goes to the session's own `leaf`, the copy its hooks run;
this checkout's runtime (previews, fixtures, pages the session doesn't
hold, the tests and `leaf-dev`) goes to `<root>/bin/leaf` and `uv run`.
"Author or revise a page" reads the leaf skill of the Leaf that runs the
page. No other guidance sent every command to the checkout.

The ahead refusal now says what running the session's launcher is for.
The user decided that developing Leaf works exactly as developing any
other project does, so a checkout's `bin/leaf` run inside a session should
matter to the session no more than a project's dev server, and its
version, page protocol included, should never reach the session.

`harness.session_harness`, the one place a command learns which session it
acts for, now finds no harness where the session's hooks last ran another
Leaf (`state.runs_session`, from the root each hook already records). Such
a Leaf acts as a command from a bare shell does: it claims nothing, writes
none of the session's records, and serves a page unclaimed, which its own
`leaf wait` hears. A `leaf-dev preview --user` from a checkout therefore
serves its durable page unclaimed in a session started normally, and
claims only where the session's hooks run that checkout, as in
`leaf-dev journey session`.

Removed with it: the claim refusal at another protocol
(`service.refuse_unheard`, its two refusal texts and its tests),
`state.protocol_at` (`registry.kernel.installed_protocol` takes a root
again), `state.hooks_launcher`, and starting a Codex task's adapter through
the hooks' launcher, since only the Leaf the hooks run now claims.

Kept, as the one guard, for any Leaf: a page a live session holds isn't
taken to a later protocol than the one that session's hooks run
(`vendoring._refuse_unheard`). A bare-shell Leaf's `page init` on a held
page whose server runs is refused already by the restart
(`hosting.restarting_server`), and its renewal by `renew_layer`'s
ownership rule; the guard covers the case where the held page's server is
down, which would otherwise move the page irreversibly onto a protocol its
holder can't read. A checkout's `page claim` now says only the Leaf an
agent session's hooks run can claim for it.

Prose: `session-lifetime.md`, `how-it-works.html`, `/developing-leaf`
("Run `leaf`": a page the checkout serves, `--user` previews included, is
heard with `<root>/bin/leaf wait`), `previews.md` (a checkout's preview
connects to no chat), and the preview's header and `--user` help.

Tests: the two refusal tests become one that a Leaf other than the
hooks' acts for no session at any protocol, and one that no Leaf takes a
held page past its hooks' protocol; the adapter-launcher test goes.
…views

A page a live session holds is now served and re-initialized only by the
Leaf that session's hooks run, whatever the protocols and with its server
up or down (`service.refuse_held`): `page init`, a first serve that takes
no claim, and a restart for another session all refuse with one message.
Another session takes the page over first by claiming it. Since only the
hooks' Leaf acts for a session, no other Leaf can move a held page to a
protocol those hooks can't read, so the protocol comparison that guarded
that move (`vendoring._refuse_unheard`, finding `unheard-protocol`) goes,
and `registry.kernel.installed_protocol` takes no root again. The
`newer-layer` text returns to main's, since it no longer reports a
checkout's claim. Behaviour change: another session's or a terminal's
`page init` of a page a live session holds is refused with its server
stopped too, where it used to rewrite the layer.

A probe of a checkout behind, at and ahead of the session's protocol, with
the page's server up and down, found every `server start` and `page init`
refused and nothing changed; the two `server start` cells with a running
server at another protocol answer with the existing protocol refusal,
which `start_page` reaches first.

With a worktree's preview unable to reach a Codex chat, as the user
decided, the paths that served one go: the codex journeys' `--preview`
mode and its guidance in `agent-journey.md`, and the preview's desktop
detach (`start_desktop_preview`, the worker's `--prepared-claim` and
`--handshake`, and `--base`'s wait for detached arms), with their journey
tests. Nothing else reached them; `claim_and_start`'s prepared claim stays
for the prompt hook's reconnect.
…kout act only once the hooks run it

An independent review found two defects, both confirmed in temporary
state homes.

Held pages. `refuse_held` read a session record another Leaf wrote at
another `since` as absent, so it took a page the session's own Leaf served
for unheld: the review's probe had this Leaf stop the live server, move the
page to its protocol and leave it disabled. `renewable()` already counted
such a page held. Both now ask one predicate, `service.held_elsewhere`: a
live claim another session holds, or a claim or lifecycle record this Leaf
can't read. The claim carries no lifetime of its own (`claim_reading` takes
it from that record), so nothing this Leaf reads proves such an owner ended;
a session takes the page over by claiming it, which reads no earlier claim,
or the Leaf that wrote the records retires them. `_restarts_for_session`
asks it first, so an unreadable record no longer reads as an ended session
whose service the restart stops.

Which Leaf acts for a session. `runs_session` let any Leaf act for a
session no hook had marked yet, so a checkout's `leaf-dev preview --user`,
which no PreToolUse rule matches, claimed the page in a fresh session; and
a stale mark from an earlier copy left the session's own installed copy
acting for no session. It now decides from facts a process has at start:
an installed copy acts for the session, and a checkout only once the
session's hooks run it (`state.hooks_run_here`, which `renew_held` uses
too). `harness.unserved_session` names the session a Leaf doesn't act for,
and `server start` and a `--user` preview from such a Leaf say they serve
for no session and that its own `leaf wait` hears the page
(`serves-for-no-session`). The held-page refusal's remedy now reads for a
person in a shell as for an agent.

Tests: the suite runs this tree, a checkout, as the Leaf of every session
it sets up, so each session now records that its hooks run this tree, as a
real first hook would (`conftest.hooks_run_this_tree`): the conftest's own
sessions and Codex's `codex-thread`, the sessions `start_server_command` and
`under_codex` start, the Claude Code and Pi module helpers, and each
`monkeypatch.setenv` of a session id. New tests cover a checkout acting
only once the hooks run it (with an installed copy acting whatever the
mark), and a page whose lifecycle record another Leaf wrote staying held
until another session claims it.
…n's hold whichever Leaf recorded it

A restart for anyone but the holding session now refuses a held page before it
stops anything, whatever the service's lifetime (`hosting.restarting_server`).
Before, a persistent service skipped the check, so the stop ran first and the
new server refused, leaving the page down: the review's probe12 had NEW's
`restarting_server(page, None)`, the step `follow_copy` takes, stop OLD's server
and then refuse. A restart with nothing to stop, as a fresh `page init` at a
path whose earlier page a session held, refuses nothing.

Whether another session holds a page is read from that session's lifecycle
record whichever Leaf wrote it, through the keys every shape of the record keeps
(`state.session_lifecycle`: generation, ended, lifetime, transcript_path, which
mean the same at since 13 and 56). A page whose session ended under another
Leaf is free to a terminal; one whose session another Leaf runs stays held.
Before, a record in another shape counted as held for good, so the review's
probe10 (B) refused NEW's bare-shell `page init` and `server start` on a page
whose session ended under OLD.

`claim_is_active` still reads only this Leaf's shape. The session's own hooks
begin a new generation where they can't read their record, and their prompt
serves the page again only because the old claim reads as ended; reading the
lifecycle across shapes there kept the prompt from serving the page, which then
stayed down until the next prompt (probed). A page server's orphan check stays
on `claim_is_active` too. Read across shapes, it kept a server serving through
a later bump under Claude Code (probed, with a copy at since 57), but after an
in-place update the server declines the follow once, never asks again, and
would keep running the replaced copy's modules (from reading).

`leaf server run` gives the `serves-for-no-session` notice `server start` gives.
A wait acting for no session on a page a session holds says so
(`held-for-a-session`) instead of "this session no longer owns it".

`leaf-dev site demo` records that its session's hooks run this checkout, so the
recorder acts for its session under the rule that a checkout acts only for a
session whose hooks run it.

Tests: a new generation's first hook marks the tree again
(test_private_startup_keeps_previous_owner_until_acceptance, the CI failure).
Sessions the codemod missed are marked: new-owner, harness-owner, s2, and the
managed_server fixture's. A `--user` preview's tests record that the session's
hooks run the preview's runtime (`conftest.hooks_run_leaf`), and its wait is that
runtime's. A persistent server on a held page is started by the holding session.
The old plugin in the init-restart journey sits at an installed-copy path. The
journey model test drops the `preview` argument the `--preview` removal left.
New tests cover a held page whose session lives or ended under another Leaf, and
a restart for another session refused before it stops.
…eps its generation and input

One fact, whether a live session holds a claim, had two readings:
`held_elsewhere` read the session's lifecycle across shapes for refusals, and
`claim_is_active` read only this Leaf's shape for receipt, the server's orphan
check, follow, presence and restarts. The third review's probe13 (3) showed the
cost: a live OLD session held a page, N2's `held_elsewhere` said held, but
`claim_is_active` said no, so `may_receive(None)` let a terminal's
`leaf wait PAGE` print the user's comment and `delivery ack` record a pickup and
a receipt. OLD still read its claim as live, so its hooks never handed the
comment over: the user's comment was received and never reached the agent. It
would recur at every session-record bump, for any checkout's or terminal's wait
on a page an older installed copy holds.

`state.session_record` is now the one reading. It reads a record of another
shape through the keys every shape keeps (`generation`, `ended`, `lifetime`,
`transcript_path`, the same at since 13 and 56), with no turn yet, so every
reader gets the cross-shape answer, and the session's own hooks carry the
generation and lifetime on and rewrite the record in their own shape at their
next write instead of starting a new generation. `session_lifecycle` goes;
`held_elsewhere` is a live claim of another session, or a claim this Leaf can't
read. `layer-registry.md` says a record at another format reads as absent except
through those keys.

With it, a later session-record bump keeps a live session's server serving
(probe16: before, N2's server stopped within 5 s of the record changing shape;
after, it keeps serving), the server follows the session to the newer copy (a
copy at since 57 took the page over within 8 s), and a copy's follow no longer
gives up on a persistent server for a live session in another shape. This PR's
own landing still drops OLD's servers once, since OLD's code reads the new
record as absent: probe10 (A) keeps the page down until the second prompt, as
before. A prompt hook already serves a held page whose server is down whatever
the claim's liveness (`reconnect.to_serve`); it skipped in that probe because
OLD's server was still running when the hook looked, and retired after.

`serves-for-no-session` now prints only where this Leaf has just bound a server
for no session: a start that reuses a server says nothing (probe14, probe15),
and a refused one prints only the refusal. The detached child reports whether
it bound one (`PageStart.fresh`).

Tests: a wait for no session leaves the input of a session another Leaf
recorded alone; a session goes on in its generation when its record changes
shape; a checkout's `--user` preview in a session whose hooks run another Leaf
serves unclaimed, says so, and the checkout's own wait hears the user.
…ough one method, and refuse in the build a shape that drops one

A `Record` read any other shape as absent, so each record other Leaves must read
grew its own hand-written cross-shape reader (`named_pages` over raw JSON,
`registry.storage.layer_packages`, the carry in `session_record`), each resting
on a prose rule nothing checked, and the claim and Codex adapter records had
none. The fourth review's probe18 moved a claim file to `leaf-claim/999`, as a
claim-record bump would: the session's own server retired within 5 s, its own
`page init` was refused as "held by a session another Leaf recorded", and its
next prompt hook found no page to serve. At an adapter-record bump,
`running_adapter` saw no adapter (probed: a live lease with a record at
`leaf-codex-adapter/999` read as None), so a second adapter would start and be
refused. And a future session shape that dropped `lifetime` made
`session_record` raise `KeyError` in `page init`, the hooks and the claim scans.

Each `Record` now declares `kept`: the keys, or paths such as `batches/*/page`,
every shape of the kind keeps. `Record.read_any` returns a record whole at this
Leaf's shape and its kept keys at any other, and is the one cross-shape reading.
The build (`build/browser/protocol.mjs`) refuses a kept key that is dropped,
changes its schema, stops being required, or isn't in the schema, whatever the
`since`; the baseline records each kind's kept keys. Kinds that declare them:
session (generation, ended, lifetime, transcript_path), claim (every key),
codex-adapter (app_server), layer (packages), delivery and codex-delivery
(batches/*/page), file-bindings (bindings). The prose rules for every record's
`page` and the layer record's `packages` give way to the build rule in
`layer-registry.md`, "Page protocol".

The readers move onto it: `session_record` fills a record of another shape from
its kept keys, `page_claim` and the claim scans read claims through
`CLAIM.read_any` (the claim record moves beside those scans in `state.py`, since
`state` can't import `service`), `retire_if_gone` takes the record's kind,
`layer_packages` and `running_adapter` read through `read_any`, and the file
grants keep those another Leaf recorded. `held_elsewhere` still counts a claim
naming a harness this Leaf doesn't know as held, as a later Leaf's may; that case
is a value, not a shape. probe18 after: the claim at another shape stays readable
and active, the server keeps serving, the session's `page init` succeeds, and the
session record missing `lifetime` raises nothing.

The drafts record is left: its kept keys would be the whole drafts map, which
would freeze the draft entries' schema for good.

`ClaudeCodeHarness.lifetime` refuses, naming CLAUDE_PID, where
CLAUDE_CODE_SESSION_ID names a session but CLAUDE_PID is unset, instead of a
`KeyError` traceback (probe17; on main too).

The agent journey's check between steps (`between`) waits as `settle` does: what
holds between steps must hold through a quiet spell, so a watch's wake that
opens a turn just after the session goes idle no longer fails it.

Tests: a claim in another shape reads as the same claim; the build refuses a
dropped, changed, loosened or missing kept key; a grant another shape recorded
survives the next grant; a Claude Code session without CLAUDE_PID is refused by
name. The older-claim test now expects the claim to read as that claim rather
than be dropped.
…pes, and read a record lacking a kept key as absent

A claim holds its page only while its session lives, by the session's record
alone: `held_elsewhere` reads the claim whichever Leaf wrote it and returns it
where it names another live session, and its branch counting any claim file it
couldn't take as a claim as held goes. That branch kept a page held for good
once its claim named a harness this Leaf doesn't know, even after the session
ended, and it held every page whose claim an earlier Leaf wrote without a
format: the fifth review's probe21 had a terminal's `page init` refused for
both, and the maintainer's state home holds 319 such format-less claims, all on
pages that still stand. Liveness never needed the harness: `claim_is_active`
now takes the lifetime from the session's record with the claim's id, so it
reads a raw claim as a claim reading. A format-less claim file reads as no claim,
as `CLAIM.read_any` reads any file no claim shape describes. A new session's own
claim of such a page was never refused, and still isn't (probed).

The session record's `lifetime` schema names each shape `lifetime_is_live`
reads (none yet, `pid`, `job`, `app`, `chat`), each closed to other keys, so a
lifetime respelled or given a new key changes the kept key's schema and the
build refuses it; with `{"type": "object"}`, a newer Leaf could have respelled
it and every older Leaf would have read its sessions as dead. The other kept
keys are plain values whose schema already carries their meaning, apart from
the claim's `harness`, which stays an open string: a later Leaf may add a
harness, which an older one reads as a claim it can't dispatch on.

A record of another shape that lacks a kept key its kind requires reads as
absent (`Record.read_any`): the build holds every shape to its kept keys, so
such a record predates them. Before, a claim lacking `harness` raised KeyError
in `claim_reading`, and a session record lacking `ended` read as alive.

The protocol baseline is rebuilt from main's, since the session record's shape
moves to since 56 in this change as a whole.

Tests: a format-less claim and an unknown harness's claim with its session
ended leave the page free to a terminal; a session record lacking `ended` or
`lifetime` reads as absent; a claim lacking `harness` reads as absent.
One rule, applied in only some readers before: a value a Leaf doesn't know in a
kept key, such as a later Leaf's harness or kind of lifetime, reads as the
session still live until its record says it ended. `layer-registry.md`, "Page
protocol", states it once.

Unknown harness. `held_elsewhere` read a claim whatever harness it named, but
`claim_reading` dropped one naming a harness this Leaf doesn't know, and every
other reader goes through it. The sixth review's probe23 had a live session of
an unknown harness hold a page while `page_claim` read None, so `may_receive`
let a terminal's `leaf wait PAGE` print the user's comment and ack it. Now
`claim_reading` keeps every claim, and only `harness.claim_harness`, which code
acting through the claimant's harness calls, returns None for a harness this
Leaf doesn't know; its callers (presence, titles, inference, the hooks, follow)
take that as no harness to act through. probe23 after: `page_claim` names the
claim, and the wait exits 2 with `held-for-a-session`, leaving the comment
unread.

Unknown lifetime kind. `lifetime_is_live` read a kind it doesn't know as ended,
so probe22's session with a `{"window": ...}` lifetime let an older Leaf's
terminal `page init` and `server start` take over its page. Now a kind this Leaf
doesn't know reads as live; a lifetime naming nothing yet still has no owner.
probe22 after: both refused, as for a known lifetime.

`protocol.mjs` lets a kept key's `anyOf` gain variants, and still refuses
changing or dropping one, so a later Leaf can add a kind of lifetime, as `job`,
`app` and `chat` were each added (the review's variant.mjs: refused at any
protocol before; at a bumped protocol, passes now). `lifetime_is_live` and the
`Record` header say how a new kind of lifetime is added.

Searching the readers of kept keys for other decisions by a value list found one
more: `restore_claim` stripped the lifetime keys it knew from a claim reading
before republishing it, so a lifetime kind it didn't know would have been
written into the claim. It now keeps the claim schema's own keys, and
`CLAIM_LIFETIMES` goes.

The accepted cost: a newer session that crashes without recording `ended` stays
held for older Leaves until a session claims its page.

Tests: a no-session wait leaves an unknown harness's session its input; an
unknown lifetime kind holds the page until the session ends; the build lets a
kept anyOf gain a variant but refuses changing or dropping one. The older-claim
test now expects an unknown harness's claim to read as that claim.
…rded end first

Two places decided whether a session lives, by different rules:
`claim_is_active` checked the record's `ended`, then the generation, then the
lifetime, while the sandbox bridge's sweep checked the lifetime alone. With a
lifetime kind this Leaf doesn't know reading as live, the seventh review's
probe25 had a newer copy's session end with its record saying so, and this
Leaf's bridge still count it live and keep its registration, so the bridge never
retired and held its localhost port. The "until its record says it ended" half
of the rule lived only in `claim_is_active`.

`service.session_lives` (formerly `lifetime_is_live`) now answers it whole: a
session whose record, where it has one, records its end doesn't live, and
otherwise its lifetime decides. `claim_is_active` checks the generation and asks
it; the bridge's sweep asks it with the registration's lifetime. A registration
of a session with no record still lives by its lifetime alone, as a sandboxed
session that has claimed nothing does. The other liveness checks in the tree ask
whether a harness's own process runs, which is not this question. probe25
after: the bridge lets the ended session go and drops its registration.

Tests: the bridge keeps a later Leaf's session with an unknown lifetime kind
while its record says it lives, and lets it go once the record says it ended.
…fts, grants, the page key and a page's address across a shape change

A record of another shape read as absent, and the next write replaced it, for
every kind but the seven #7061 gave kept keys. A Leaf update that changed a
kind's shape, or two Leaves of different shapes sharing the state home, so
deleted unsent drafts, dropped connection grants, and minted a new page key
that signed out every open tab.

`kept` is now a required argument of `state.Record`, and the protocol build
refuses a record in its baseline without a declaration (the composer's draft
declares none, since the composer reads a draft only at its own format). A
kind whose loss costs the user nothing declares an empty `kept`.

A `*` on a kept path now walks a map's values as well as a list's items, in
`state._keep` and in `protocol.mjs`, which also refuses a shape where a `*`
changes from walking one to the other, and a path that ends at a `*`, which
`_keep` can't read.

Kinds now keeping keys, and their readers:
- drafts: each draft's attempt, text, payload and held reply. A store of
  another shape gives way to a new store holding those, at version 1 since a
  tab reads no drafts from a version-0 store. The payload is kept only as an
  object; its owner versions it and reads it only at its own format.
- http-connections: every field of each grant; `_read` seeds from read_any.
- access: the token; `host_key` reads any shape and leaves another Leaf's
  file as written, so two Leaves no longer replace each other's key.
- service: every required field, so a restart keeps the page's port, host
  and lifetime, and another Leaf still finds the running server.
- clerk: each queued line and its wake flag.
- data-source: the publication's four fields.
- user-views: `viewed`.
Bridge, completion, pointed, preview, render-reading, row, stream and
website-publication keep none, for the reason their declaration or
description gives.

leaf.page's Durable Object keeps a page's drafts store without its `format`,
and the container labels it with the running Leaf's on restore, so a drafts
shape change under the same STORE_FORMAT would make hosted drafts readings
misread or fail. The worker's store-format test now pins the drafts record's
`since` beside the tables' digest, so such a change moves STORE_FORMAT, as
the store's contract already says an encoding change must.

Behavior changes in tests: a key file at another ACCESS format no longer gives
way to a fresh key (only a file holding no key record does); the address and
key test now also covers a shape change of both records.

Builds on #7061.
`state.Record` requires `kept`, so a record without a declaration can't be
constructed, and the build's own check of it could never fire. One mechanism
holds the guarantee now; the composer's draft keeps its declared `[]`. The
test of the refusal goes with it.
…on's end removes it

The bridge read its registrations from a directory of its own, so a session's
end never reached them: `state.end_session` on a session holding no claim
retires the session's files, its lifecycle record included, and the bridge's
sweep then read the registration with no record. With a lifetime kind this Leaf
doesn't know reading as live, the eighth review's probe26 had such a session
keep the bridge registered for good, so the bridge never retired and held its
localhost port. The sweep can't tell a session that ended and was retired from
one that never had a record.

A registration is now one of its session's own files
(`sessions/<session>.<key>-<digest>.bridge`, `bridge.registrations`), so the
session's end retires it with the rest, whether or not the session's record
stays; where a claim keeps the record, the record says the session ended
(`service.session_lives`). The end stays where it was: Claude Code's SessionEnd
runs `state.main` with no package imports, and `state` can't reach `bridge`. The
reviewer's alternative, keeping the lifecycle record while a registration names
the session, would have added a second retention rule to `state._retire`.
probe26, adapted to the new location: the end of a session holding no claim
leaves neither its record nor its registration, and the sweep finds nothing.

Tests: the bridge keeps a session registered with an unknown lifetime kind
until it ends, by its record or by its end retiring its files; the late-session
test now ends its session for real rather than deleting the registration by
hand.
# Conflicts:
#	build/browser/protocol-baseline.json

@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 description is now out of date after d3ea259. Two sentences still say the build refuses a record with no kept declaration: "the protocol build refuses a record in its baseline with no declaration" and "Tests cover … a new kind with no kept". Merging squashes with the description, so both claims would land in main's commit message. After this commit the requirement comes only from state.Record.__init__, whose keyword-only kept has no default.

…nds every process's registration

"Each process of a session that lives as its own pid keeps its own registration"
read as if one process's end left the others' registrations standing. It
doesn't: the first process to end the session (`state.end_harness_instance`)
ends it for all of them, which removes every registration along with their
claims, as ending the session already made every process's claims inactive. The
header now says so. The surviving process registers again at its next `leaf`
command's hook; the path takes two live processes on one session id, so it
stays as it is (the ninth review's probe27, case b).
… counting before then

The header said a session's end removes its registrations, and that the first
process to end a session removes every registration along with the claims.
Neither holds while a claim names the session: `state._retire` then removes
only the hooks file, and the registrations and claims stay. A registration goes
with its session's files once no claim keeps the session's records
(`state._retire`); until then the session's ended record stops it counting, and
the bridge's sweep forgets it (`live_sessions`, `service.session_lives`). Each
process of a pid-lifetime session keeps its own registration until the first to
end the session ends it for all of them (`state.end_harness_instance`). The
header now says that (Leaf's CI reviewer on 8d99c0b).
…use a shape that requires a kept key its last shape didn't

A carried drafts store took a new identity at version 1, so every tab met it as
another store and wrote back each copy it held that the store lacked: a draft
cancelled on another device came back, and a draft rewritten there gained the
tab's stale words after the new ones. Two Leaves of different drafts shapes
serving a page in turn minted a new store at each switch.

Every drafts shape now keeps the store's identity and version and each draft's
version. The first reading or write takes a store of another shape over as a
write of its own, rewriting it in this shape at its next version, so a tab
hears the same store go on and writes nothing. A host now takes that write
over the store it holds, since the carried store names it. The test asserts
the carried store's identity and its moved version.

The protocol build accepted a shape that requires a key on a kept path that
its last shape lacked or left optional; a newer Leaf's `read_any` then read
every older record as absent. It now refuses one, beside a key that stops
being required.

Every write replaces a record whole, so an older Leaf's write drops the fields
a newer shape added. "Page protocol" now says so, and that a new shape adds
no field whose loss would change what the record grants or means; the
connections header names the fields each grant keeps rather than "whole".
hosting.py's comments on a service record no Leaf reads now say that's one
with no `format`.
…'t require

The refusal compared a kept path the last shape didn't hold to an empty list,
so a parent every shape requires, as `drafts` is, counted as newly required,
and no shape could keep a new optional key of each draft, grant or queued
line. The build now follows the path through the last shape as far as that
shape goes, and refuses only a key it lacked or left optional that the new
shape requires. protocol.test.mjs keeps a new optional key of each map value
under a required parent, and refuses the same key made required.
leaf-agent
leaf-agent previously approved these changes Nov 25, 2026
…pe held each line as a string

Checked against every shape in its history, the clerk queue's kept paths
`lines/*/line` and `lines/*/wake` fail its shape at since 36, which held each
line as a plain string. A queue of that shape read through them came back as
a list of strings, and `clerk.queued`, which the hook carrying the lines
calls, raised TypeError on it. No key of a line is in every shape, so the
queue keeps nothing: a queue of another shape reads as empty again, and the
next line queued replaces it. Its history is now seeded too.

Test: a queue at since 36's shape reads as empty and the next line replaces
it, in place of the test that lines carried over.

@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.

Checking kept paths against earlier shapes is a sound addition, and the clerk change follows from it. But the PR description leaves one design question open: what to do about three kinds whose kept keys fail against their own earlier shapes. Meanwhile the contract states the guarantee as though that question were settled. I'm leaving this as a comment so the maintainer can make that call before merging (see inline).

Comment thread src/leaf/layer-registry.md Resolved
@leaf-agent
leaf-agent dismissed their stale review November 25, 2026 06:29

Superseded by the review on a later commit.

max-sixty added a commit that referenced this pull request Nov 25, 2026
…h and stamp them (#7122)

`served_state/reading.py` declared which writes count as a page's inputs
(`page_input`, `page_roots`, `outside_inputs`) and took the stamps over
them (`page_reading`, `source_readings`, `record_reading`,
`join_reading`). Its callers are the file watchers, activation,
publishing, the row publisher and the hooks, none of which projects
state, and it imported nothing from the projection. It becomes the
top-level module `page_inputs`, below its callers, and `served_state`
holds only the served state: the page read, its projection, and the
patch sent to a tab holding an earlier reading. `version_written`, which
names when the files a version is built from last changed, moves to
`revision_artifact` beside `CAPTURED_TREES`, ending its deferred import.

`session`, `hosting`, `server_rows`, `http` and `agent_state` now stand
just above `served_state` in `.config/importlinter.ini`, since each
reads the served state and `served_state` imports none of them. Only the
order changes for those, in a commit of its own.

`src/leaf`'s allowed backward imports go from 64 to 51. Behavior is
unchanged.

`.config/importlinter.ini` will conflict with #7061 and #7114 on one
hunk: this branch deletes the `leaf.session -> leaf.served_state.*`
allowances, and both PRs delete the adjacent `leaf.session_runtime ->
leaf.harness`. Deleting all three lines resolves it, and `lint-imports`
checks the result. Every other file merges cleanly with both. From their
files, this touches import lines in `hooks.py`, `session.py`,
`render_gate/handover_pass.py` and `worker/leaf_website/storage.py`, and
one docstring line in `presence.py`.

max-sixty asked for refactoring to continue while each change improves
the code.

Tests: `lint-imports` and pre-commit pass, as do the model tests for
file bindings, native watches, the handover pass, seen, hook
registration, model folds, the render gate and render export, the
website's saved records and the revendoring contract (163).

> _This was written by Claude Code on behalf of max-sixty_
@leaf-agent
leaf-agent dismissed their stale review November 25, 2026 06:33

Superseded by the review on a later commit.

…ys, verified shape by shape

`read_any` read every older record through the kept keys, including records of
shapes no check ever verified: a since-36 clerk queue held each line as a
string and crashed the hook that carried it, and older drafts, data-source
and session shapes failed the kept-path check. Each `state.Record` now
declares `kept_since`, the earliest shape whose records `read_any` reads
through its kept keys; a record of an earlier shape reads as none.

The baseline holds each kind's shapes from `kept_since` on, without their
annotations, and every kept path must hold against each of them. The build
refuses a `kept_since` that names none of the shapes the history holds, so a
window never reaches a shape no check saw nor leaves out the last shape, and
a new kind's is its own since. A path that fails against an earlier shape is
refused with the `kept_since` that would leave that shape out.

Every kind's history is seeded from protocol-baseline.json's git history
back to the earliest shape from which every later one holds its kept paths:
clerk from 44, delivery and codex-delivery from 13 (main retires envelopes of
any shape), layer from 36, drafts from 51, data-source from 16, session from
13. The clerk queue keeps its lines again. No page on the maintainer's
machine has a layer record below 36, the earliest shape the baseline held.

Shapes in a window agree on every kept path, required keys included, so
`state._keep` no longer checks required keys or takes the schema.

The session record's `lifetime` is any object, what every reader accepts, so
its since-13 shape holds and a session live during the update from main goes
on; `service.session_lives` reads a lifetime it doesn't know as live. No
Python type describes the five forms (`Harness.lifetime` returns a plain
dict), so the suite's schema check of that one field goes: a harness that
writes a malformed lifetime no longer fails the suite's record check.

With nothing kept as an `anyOf`, the build's allowance for a kept `anyOf` to
gain variants (`widens`) goes with its test and its prose; a kept key's
schema must equal every shape's in its window, and says what every reader
accepts.

"Page protocol" says a kept key keeps its meaning across shapes, which no
comparison of schemas can see.

Tests: a session record older than `kept_since` reads as none; case d is
refused with the `kept_since` that passes; the window test replaces the
history-append test; the clerk test covers a since-36 queue; the claim test
reads a newer Leaf's claim, and the impossible lacking-key cases go.
@max-sixty max-sixty changed the title Have every record kind declare the keys it keeps, and keep unsent drafts, grants, the page key and a page's address across a shape change Give every record kind a verified window of shapes it reads through its kept keys, and keep drafts, grants, the page key and a page's address across a shape change Nov 25, 2026
…pt_since is chosen

At since 44 a queued line could name a turn, and left the queue unheard once
that turn ended, as a fork's "leave it to the fork" did. No later shape
records the turn, so a since-44 line read through the kept keys reached the
agent whatever became of the fork. The queue's `kept_since` is now 52, and
its since-44 shape leaves the baseline; a since-44 queue reads as empty.

The layer record keeps its window from 36: a since-36 record keeps an
explicit theme in its own key rather than in `packages`, so renewal drops
that theme, as on main, where a later window would drop the page's whole
selection. Its declaration says so.

The `Record` docstring now says how a `kept_since` is chosen: the earliest
shape whose kept keys mean what they mean now. The build checks only that
the shapes agree in structure.

Test: the clerk queue's shape test reads a since-44 queue, a line naming a
turn, as empty.
Comment thread src/leaf/registry/storage.py Resolved
…hapes

The comment at LAYER named this branch's history ("as on main"), and its
last clause read as describing main rather than the alternative window. It
now says a since-36 record holds its theme in a key of its own, which
renewal drops, and that a window from 53 would drop the whole selection
instead. The clerk header states the since-44 line's meaning in the present
tense.
# Conflicts:
#	.config/importlinter.ini
# Conflicts:
#	build/browser/protocol-baseline.json
#	src/leaf/render_gate/handover_pass.py
leaf-agent
leaf-agent previously approved these changes Nov 25, 2026
Base automatically changed from agent-a6f94928fcd4286e2 to main November 25, 2026 08:43
@max-sixty
max-sixty dismissed leaf-agent’s stale review November 25, 2026 08:43

The base branch was changed.

# Conflicts:
#	build/browser/contract-types.mjs
#	build/browser/protocol-baseline.json
#	build/browser/protocol.mjs
#	build/browser/protocol.test.mjs
#	src/leaf/codex.py
#	src/leaf/codex_adapter.py
#	src/leaf/delivery.py
#	src/leaf/file_bindings.py
#	src/leaf/hosting.py
#	src/leaf/layer-registry.md
#	src/leaf/registry/storage.py
#	src/leaf/service.py
#	src/leaf/state.py
#	src/leaf/user_views.py
#	tests/model/test_claims.py
#	tests/model/test_hooks.py

@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 description's opening paragraph went stale when #7061 merged and this PR moved onto main. "This PR's base is that branch, so its diff is only this change; GitHub retargets it to main when #7061 merges" no longer describes the PR. Merging squashes the description into main's commit message, so that sentence would land as a false claim. Keeping only "Builds on #7061, which added state.Record.kept and Record.read_any." would fix it.

max-sixty added a commit that referenced this pull request Nov 25, 2026
#7149)

This PR moves the handover pass, which reads each version a page's
claimant hands over and tells the agent what the render check newly
found, from `render_gate/handover_pass.py` to `leaf/handover_pass.py`.
In import-linter's order the module now sits directly below `hosting`.
That ends two of `src/leaf`'s allowed backward imports, so the count
goes from 50 to 48. `hosting` imports the pass to start a page's server,
and the pass's child reading imported `validation.command` for
`check_page`. The pass imports nothing from `render_gate`, so nothing
held it in that package.

The reading now takes the check it runs as an argument. `leaf page
_read` in `cli` passes `check_page` in, and the tests hand their
stand-in checks to `read_and_tell` directly instead of monkeypatching
`validation.command`. Since `validation` no longer sits in a cycle with
the pass, it now imports `remember_reading` at the top of the module.

One reference to the old path is left: the render-reading record's
schema description in `render_reading.py`. Changing it would change that
record's line in `build/browser/protocol-baseline.json`, and #7114,
another session's open PR, changes the same line. I'll update it once
#7114 lands.

Checks: pre-commit passes, import-linter included.
`tests/model/test_handover_pass.py`, `tests/model/test_render_gate.py`
and `tests/process/test_interact_server.py` pass (179 tests). `git
merge-tree` against #7114's head reports no conflicts.

> _This was written by Claude Code on behalf of max-sixty_
max-sixty added a commit that referenced this pull request Nov 25, 2026
This PR stops `findings` from importing anything in `registry`, which
ends two of `src/leaf`'s allowed backward imports (48 to 46). `registry`
imports `findings` in five modules. But `findings` also rendered two
kinds of words through `registry` modules ordered above it, importing
each inside a function: a JSON value written inline in a sentence
(`registry.schema.json_value`), and the part ids a visual admits (the
`str` of `registry.contract.VisualParts`).

- `json_value` moves to `json_block`, which already described Leaf's
inline and fenced forms of a JSON value. `findings`, `registry.schema`,
`data`, `data_contracts` and `codex` now import it from there. The one
in `codex` stays a function-level import, as it was.
- `findings` owns the words for a visual's admitted parts
(`admitted_parts`). `VisualParts.__str__` renders through it, so the
`undeclared-visual` refusal and anchor capture's error share one
wording.

Checks: pre-commit passes, import-linter included. The model tests for
the interact contract, documents, layers, render gate, Codex adapter and
chrome contracts all pass (876). `git merge-tree` against the head of
#7114, another session's open PR, reports no conflicts.

> _This was written by Claude Code on behalf of max-sixty_
max-sixty added a commit that referenced this pull request Nov 25, 2026
…ix backward imports (#7153)

`harness` stands low in `src/leaf`'s import order, because the event
log, admission and claims all read a harness. Several of its methods
called up into modules ordered above it, importing each inside the
method, which import-linter allowed as six backward imports. This PR
keeps in `harness` the identity, the declared facts and the methods that
call only modules below it. Each behaviour that needed a higher module
now lives above that module and dispatches on a fact the harness
declares, so none of them compares a harness's name. Allowances: 48 to
42.

Two facts are new:
- `Harness.adapter`: the session's input goes through Codex's adapter
and its delivery record.
- `Harness.inference`: how Leaf asks the harness's model, one of
`completion`, `app-server` or `print`.

Where each behaviour went:
- **Inference and thread titles:**
`inference.harness_generator(harness)` reads `inference`.
`event_endpoint` builds the title generator from it, replacing
`Harness.inference_generator` and `Harness.title_generator`.
- **Claims:** `hosting.preparing_delivery` starts or joins the adapter
where `adapter` is set, replacing `Harness.preparing_delivery`.
`codex_adapter.preparing_adapter` now holds the check that a running
`leaf wait` stays the watcher. `cmd_claim` moved from `service` to
`hosting`, because `service` sits below the adapter it starts.
- **Codex's tool-hook and Stop offer:** this is now
`hooks.offer_at_step`, called where `adapter` is set. Codex's rule that
a later turn takes the input moved to
`codex_adapter.later_turn_takes_input`, beside the record it reads.
`hook_transport.carry_turn` now asks `watches_between_turns()`, which
gives the same answer there, because Codex's hooks never put input into
a delivery.
- **Delivery receipt:** `Harness.receive_pointer` returns None by
default, and `delivery.receive_delivery` then takes the ordinary
receipt. `Harness.entered_context` returns the hook contexts it finds in
the transcript, and `delivery.confirm_entered` reads the inputs out of
them, so `harness` only reads its own transcript format.

To weigh: `adapter` and `inference` each match one harness today, so
they can read as harness names under another name. They state what Leaf
dispatches on, and a new harness sets them without any module outside
`harness` changing. The Codex hook offer stays in `hooks` rather than
moving into `codex_adapter`, because importing `codex_adapter` costs
about 50 ms, and Codex runs that hook after every tool call.

Checks: pre-commit passes, import-linter included. About 600 focused
tests pass, covering hooks, thread titles, the Codex adapter, receipts,
delivery, read state, claims, the Stop guard, waits, responses, hook
registration, activity, session lifecycle, App Server, native watch,
server lifetime, the Claude Code module, Pi and LLM. `test_forks`' stub
harness now declares `inference = None` in place of the removed
`title_generator`. The two journey tests this PR edits weren't run
locally. `git merge-tree` against the head of #7114, another session's
open PR, reports no conflicts.

> _This was written by Claude Code on behalf of max-sixty_
max-sixty added a commit that referenced this pull request Nov 25, 2026
…t arbitrates (#7157)

Each `state.Record` shape now lives in a file named with its `since`
(`drafts.json` is kept at `[email protected]`), and a Leaf reads and
replaces only its own shapes' files. This takes out #7061's
`Record.kept`, `read_any` and the build's kept-key checks: root
`AGENTS.md` "Stage" forbids fallback reads, and in the last five weeks
the records #7114 would carry across changed shape only during the
drafts store's first two days.

A record that arbitrates between Leaves (a page's claim with its
session's lifecycle, a page's service, a grant, the Codex adapter's)
holds while a file of any shape of it stands, without being read, and a
session's end, `leaf server stop`, a grant's withdrawal and a takeover
of the page end it at every shape. Who holds a page is one reading,
`service.page_holder`, which every reader deciding whether it may act on
a page asks, and a rolled-back takeover puts back what it removed. The
cost: after a `SESSION` shape change, which comes with a protocol bump,
a page whose session ended under the older Leaf stays held for the newer
one until an agent takes it over with `leaf server start`, which every
refusal of a held page names; the takeover leaves one owner.
`layer.json` stays one file, and renewal keeps its `packages`. `page
check` names a drafts store of another shape, the user's unsent words,
and says to point the user at it rather than read it.

Max chose this over #7114 on his Leaf page; it supersedes #7114.

At a shape change, against main: drafts, connection grants, the sign-in
key and the server address are gone from the page as on main, their
files kept; file grants, which main carries across, are gone too; a
claim-shape change leaves the page held until the agent runs `leaf
server start`. Session records, delivery envelopes and the layer behave
as on main.

Net lines: production code +83, comments and docstrings +57, tests +96,
prose +22, baseline data −7.

Tests: `tests/model/test_record_shapes.py` (a page held at another
shape, a session-less wait stopping there, a takeover leaving one owner
and its rollback restoring the other holder, a withdrawal at every
shape, the `page check` note) and a stop beside another shape's server
in `test_server_lifetime.py`; the model and process files that touch
records pass on Python 3.14 and 3.12. CI also runs the journey and
browser tests this branch edits; the four server-row journeys in
`test_interact_server` and `test_render_controls` fail on main at
118f31f too, since #7061 refuses to serve a held page for no session.
Edited but not run locally: `tests/journeys` (test_activity,
test_claims, test_codex_adapter, test_hook_registration,
test_interact_document, test_interact_layer, test_interact_server,
test_native_watch, test_render_controls, test_session_lifecycle,
test_site), `tests/browser` (drafts/test_shared_drafts,
startup/test_projected_data, projection/test_code_review) and
`dev/tests/journeys` (test_render_export, test_interact_preview).

> _This was written by Claude Code on behalf of max-sixty_
@max-sixty

Copy link
Copy Markdown
Owner Author

Superseded by #7157, which landed instead: each record shape now lives in its own file, a Leaf reads only its own shapes, and a record that arbitrates between Leaves holds while a file of any shape of it stands. Records aren't carried across a shape change, so this PR's windows of kept keys aren't needed.

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

@max-sixty max-sixty closed this Nov 25, 2026
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