Skip to content

Declare each agent harness once, and dispatch on what it states - #811

Merged
max-sixty merged 12 commits into
mainfrom
harness-modularity
Sep 18, 2026
Merged

max-sixty merged 12 commits into
mainfrom
harness-modularity

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

Leaf reaches a session through whichever harness runs it: Claude Code, the Codex CLI, or the hosted website's embedded App Server host. The harness was already resolved once for identity and lifetime, but the return path was chosen by comparing a two-valued host string in hooks.py, event_endpoint.py, codex.py and worker/server.py, and the website claimed its pages as codex to enter those arms. Each harness now has one declaration, and the harness-neutral modules dispatch on what it states.

Harness declarations

host.py defines Harness, with ClaudeCodeHarness, CodexHarness and EmbeddedHarness. Each states its name, its lifetime, what proves its carrier live, the two remedies the Stop hook prints, and a nudge where the session can be reached between turns; the two the environment implies also state their default display name and the variables their session id arrives in. A claim records harness in place of host, so the hook, the event door and the page server rebuild the claimant's declaration and ask it, rather than reading their own environment. No harness name is compared outside host.py and codex_adapter.py.

The website declares itself embedded, with its own display name and the App Server process its session lives with. host leaves browser-facing state, where nothing read it. The claim key no longer shares a name with the network host in hosting.py, server.py and --host.

A claim naming a harness this install cannot rebuild, including one written before this change, reads as no claim, so the page stands unheld until the next server start or named leaf wait writes a current one.

Codex modules named for what they hold

codex.py is Leaf's side of one Codex task: the App Server connection every carrier speaks, the activity and reply readings, and the durable delivery records a carrier prepares, accepts, opens and abandons. codex_adapter.py is the detached carrier behind leaf codex start, with its codex queue fallback, page receipts and launcher. The website imported three underscore-private names across that line and now imports public ones. The request loop, the handshake and terminate-then-kill, each written in both the adapter and the website, are shared.

The retry ladder was written four times with two exponent rules. It is retry_delay now: one second after a first failure, doubling to thirty. The two adapter sites waited two seconds first and reached the ceiling one failure earlier.

The author token is a role

Events carried author=claude for every agent message, including a Codex or website reply. The token is agent in the event contract, the registry schema, the browser runtime and its CSS classes, the rebuilt browser bundle, the example logs and the tests. author is user, agent or page. A log written before the rename decodes the old token as agent at the one event parser, dated to come out; a page vendored before it still needs page init to pick up the runtime that reads the new one.

One display default

A page nothing has claimed calls its agent "Agent", in Python (UNCLAIMED_AGENT) and in the browser. Five per-site "Claude" and "Codex" fallbacks are gone, because a claim always carries its name.

Host-named rules live in the host contracts

conversation-loop.md, event-batches.md and SKILL.md state the neutral rule and point at the host contract. The Claude Code and Codex clauses moved into host-claude-code.md and host-codex.md, which SKILL.md already routes each agent to.

Tests

Claims and identities built by hand use the harness record, through the support helper where one existed. The tests that observed stream activity through project_app_server_activity's callback seam patch the two writers instead, since the seam is gone. Two test names that spelled the old author token were renamed.

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

max-sixty and others added 12 commits September 18, 2026 12:15
conversation-loop.md and event-batches.md are routed for either host but stated
several rules by harness name (Codex App Server banner step, queued-pointer
semantics, wait/ack ownership, and when new input arrives). SKILL.md did the
same for the launcher path and the delivery envelope's transport. Each fact
either already existed in more detail in host-codex.md (App Server banner
step, queued-pointer confirmation, adapter-owned wait/ack, turn-boundary
delivery) or is now stated there for the first time (host-claude-code.md gains
a Launcher note and a "next tool result" note in Wait loop). The neutral docs
now state the host-agnostic rule and point at "your host contract" for the
specifics, so they read correctly for a third host's contract too.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
The Claude Code contract compared its envelope to what App Server and a
queued Codex task carry; the neutral references now leave that to each
host contract, so the comparison had no reader left.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
The author field on every event, the registry contract, and the CSS class on
agent-authored messages all used the vendor name claude for the agent side of
a conversation. That token is now a role, not a vendor: author is user, agent,
or page, so a reply written from Codex or from the hosted website is stamped
author=agent rather than author=claude. The .lf-msg/.lf-conversation-msg class
that carries the same value follows the same rename. Rebuilt the compiled
browser-runtime.js bundle from the updated scripts/browser/application.ts and
regenerated examples/corpus.html (no byte change, since the corpus does not
embed conversation-log JSON). Left the claude-code host token, CLAUDE_CONFIG_DIR,
the agent display-name field, and notes/mcp-apps/experiments/** untouched.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
The website's embedded host is a second consumer of this code, and it was
reaching into `leaf.codex` for six private names, the connection itself among
them. The protocol client, the readings Leaf takes from a task's notifications,
the writers that put those readings on a claimed page, and the durable records a
delivery passes through are now `leaf/app_server.py`. `codex.py` keeps the
detached adapter `leaf codex start` leaves running, and imports the rest.

The line is the consumer rather than the subject, so it falls in a different
place than the module's two halves suggest. `prepare_codex_delivery`,
`accept_codex_delivery`, `open_app_server_delivery` and `delivery_queue_state`
are the website's own API and are built on the `leaf-codex-queue-v1` records, so
the records and their offer lifecycle moved with them, while the receipt
reconciliation only the adapter's loop runs stayed behind. `AppServerClient` is
the adapter's carrier, so `AppServerDeliveryUncertain`, which only it raises,
stayed with it.

Both hosts had written the same JSON-RPC request loop, the same
initialize/initialized handshake, and the same terminate-then-kill: they now
share `app_server_request`, `app_server_handshake` and `stop_app_server`. Each
caller still says what to do with a notification that arrives behind its
request, because buffering it to replay and folding it immediately are what
separate a stream a caller is following from a snapshot whose answer outranks
everything before it.

The synthetic id on a streamed reply is `stream:<turn>` rather than
`codex-stream:<turn>`; nothing about that message is Codex's.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
`app_server.py` held more than an App Server client. The durable delivery records
and the prepare, accept, open and abandon operations over them are Leaf's side of
one Codex task whichever transport carries it, including the `codex queue` command,
which is not App Server at all. That module is now `codex.py`, and the detached
carrier process that was `codex.py` — its observing client, its queue fallback, its
receipts and leases, and `leaf codex launch` — is `codex_adapter.py`. The
`leaf codex …` CLI group is unchanged.

The retry ladder was written four times with two different first steps. It is now
`retry_delay(failures)`: a second after the first failure, doubling to a half-minute
ceiling. The two carrier sites, `AppServerClient._run` and `run_adapter`, moved one
step — their first retry waits 1s rather than 2s, and the ceiling arrives one
failure later. The website's `TurnStream._wait_for_another` and
`HostedTurn._restart_turn` keep the ladder they had.

`project_app_server_activity` no longer takes the two writers as arguments. Both
callers filled them with the same pair from the module the projection now lives in,
so it calls them directly. What the callbacks were doing for tests, one helper per
suite does explicitly: a reading is written through two bindings of those functions
— a carrier's own import and `leaf.codex` — and a test that wants the readings, or
wants them to touch nothing, says so at both.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
`host.py` now owns a `Harness` per agent host — Claude Code, Codex, and the
embedded App Server host the website runs — carrying the session id and display
name, the lifetime resolver, the delivery carrier, the evidence that the carrier
is live, the two remedies the Stop hook prints, and the nudge that reaches a
session with nothing watching. Every harness-neutral module asks the declaration
instead of comparing a host string: no harness name appears in `hooks.py` or
`event_endpoint.py`, and `codex.py`'s page lock is by ownership alone, since the
claim's session id already says whose pages those are.

The claim records `harness` and `carrier` in place of `host`, which collided
with `host` meaning hostname, so a reader meeting the claim later dispatches
without re-deriving an environment that need not be the claimant's.
`waiter_lease_path` and `wait_is_live` take a session id rather than a claim or
identity record. `scripts/site.py` scrubs the session variables the harnesses
declare instead of keeping its own list.

Behaviour changed:

- The website declares its own embedded harness, so its claims read `embedded`
  rather than `codex`.
- The hook's remedy text and its watch question come from the harness
  declaration.
- A Codex claim with no process to name records `activity: "multiplexed"`
  instead of the harness-shape token `codex-app-server`.
- Browser-facing state no longer carries `host`; nothing in `assets/` read it.
- A page nothing has claimed calls its agent "Agent", one constant shared by
  `presence.py` and `context.js`, rather than "Claude" or "Codex" guessed at
  each site. Where a claim exists its own name is used directly.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Every claim reader now dispatches on the record's `harness`, and a file an
older install wrote carries `host` instead. Reading one raised KeyError in
the event door, the Stop hook, and `server start` on any machine whose
sessions span the upgrade. Such a record now reads as no claim: the page
stands unheld and the next start or named wait writes a current one. This
decodes data already on disk and is dated to come out.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
The claim recorded `carrier` beside `harness`, a second copy of a fact
`claim_harness` already rebuilds from the name, and two contracts
described readers dispatching on it that did not exist; the field is
gone, the website compares the harness name in its own module, and the
contracts describe what the code does. A claim naming a harness this
install cannot rebuild reads as no claim, which subsumes the earlier
pre-field test and covers a state home shared across installs. An event
log written before the author token became a role decodes `claude` as
`agent` at the one parser, dated to come out. `EnvironmentHarness` holds
the two class facts only an environment-implied harness has, so the
embedded one no longer promises attributes it lacks. The Codex contract
now says when reader input arrives, which the neutral reference sends
its reader there for. Tests that assumed "Claude" on an unclaimed page
now expect the neutral word, the lease helper passes a session id, and
the test support reads Leaf's harness table instead of keeping one.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
# Conflicts:
#	scripts/browser/application.test.mjs
#	scripts/browser/application.ts
#	scripts/browser/generated/browser-runtime.js.map
#	scripts/browser/generated/browser-runtime.manifest.json
#	skills/leaf/assets/vendor/browser-runtime.js
#	tests/test_render_conversations.py
Two phases in the banner tests write a claim naming Claude before they
read the banner, so the neutral word belongs only to the unclaimed
phases beside them.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@max-sixty
max-sixty merged commit 9e5e0d3 into main Sep 18, 2026
5 checks passed
@max-sixty
max-sixty deleted the harness-modularity branch September 18, 2026 22:16
max-sixty added a commit that referenced this pull request Sep 18, 2026
…wrote (#813)

Two follow-ons from #811, which gave each agent harness one declaration.

## `host.py` held four things

It carried this machine's state-home paths and its psutil process
readings alongside the harness declarations and the Claude Code
messaging socket. Because `leases.py` imports `state_home` from it, it
could not import `leases` at module scope, and
`CodexHarness.carrier_live` worked around that with a local import of
the adapter lease.

`machine.py` now owns the state home and the process readings, `host.py`
owns the harnesses and the socket, and the local import is gone. Each
moved function keeps its docstring, including the psutil rationale,
which is now that module's header. One test patched a moved reading and
was retargeted to the binding the code under test actually reads; a
control run confirms it fails loudly when pointed at the old one.

## Nothing is owed to what an older version wrote

Stage already said leaf has no persisted state that constrains new code,
and that stale state is deleted and regenerated. It did not say what
follows for a change that strands some, so #811 shipped two shims for
records written before it, each with a dated note promising its own
removal, and its handoff told the user how to re-vendor standing pages.

Stage now rules out all three: no migration, no shim that reads the old
shape, no dated note, and no revival steps in the reply. Both shims go
with it. A claim written before claims named a harness reads as what it
is again, and the event parser stops rewriting the old author token.
Neither condition is reachable from code that runs, since every claim
and every event is written by the running version.

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

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Fable 5.1 <[email protected]>
leaf-agent added a commit that referenced this pull request Sep 19, 2026
A plugin update swaps the running Leaf under a standing page, and #820
found that the page could then be neither used nor repaired.

`$events.kinds` is Leaf's fixed kernel transport contract. A page's
vendored `registry.json` holds the one the Leaf that wrote it had, so
after an update it cannot equal the running kernel's, and `load_registry`
refused every read of the page with a contract the reader had no way to
meet. Re-vendoring is the only move that resolves it, so the refusal now
names `leaf page init` — the shape `layer_metadata` already uses for a
registry missing its `$layer.generation`.

`page init` then has to take that move, and it refused too.
`candidate_vocabulary_gaps` was checking each logged event against the
incoming per-kind `record` schema, which is a gap a layer selection can
never own: the kinds are fixed per version, so no selection restores the
shape an earlier Leaf wrote a log in, and admission is the only reader of
that schema — an event already in the log replays identically whichever
layer is vendored over it. The guard keeps what a selection does own: a
dropped reaction token, a retired verb, a kind the contract no longer
declares. The complete stored record now has no validator at all; the
browser door checks the client's half of it and the CLI writers check
none, so restoring that belongs at the append boundary rather than here.

`test_init_refuses_a_historical_event_record_outside_its_declared_schema`
held the refusal and moved with it: it now asserts the re-vendor runs and
that the event still replays afterwards. The new case in
test_interact_layer.py runs the reported journey — a log and a vendored
layer spelling the agent's role with the token #811 renamed, a refused
`version check` that names the way out, a `page init` that takes it, and
the same reply reading back through `leaf transcript`.

Closes #820
max-sixty pushed a commit that referenced this pull request Sep 19, 2026
A plugin update swaps the running Leaf under a standing page. #820
reports what that leaves: a live page that cannot be validated, replied
to, or re-vendored, with readers still commenting on threads nobody can
answer. Two refusals produced that, and this removes both.

`$events.kinds` is Leaf's fixed kernel transport contract, and a page's
vendored `registry.json` holds the one the Leaf that wrote it had. After
an update the two cannot be equal, so `load_registry` refused every read
of the page — `version check`, `version stamp`, `reply` — with a
contract the reader had no way to meet. Re-vendoring is the only move
that resolves it, so the refusal gains a one-line hint naming `leaf page
init`, the shape `layer_metadata` already uses for a registry missing
its `$layer.generation`.

`page init` then has to take that move, and it refused too.
`candidate_vocabulary_gaps` was checking each logged event against the
incoming per-kind `record` schema, which is a gap a layer selection can
never own: admission is the only reader of that schema, and an event
already in the log replays identically whichever layer is vendored over
it. That check is deleted rather than relaxed. The guard keeps what a
selection does own: a dropped reaction token, a retired verb, a kind the
contract no longer declares.

No shim reads the old shape and no guidance explains one — the page's
vocabulary is regenerated, and the refusal says so where the reader
meets it. Re-vendoring does not undo what the update itself changed:
#813 dropped the shim reading the old `claude` author token as `agent`,
so the reported page's 13 replies read as user turns under the running
Leaf, before and after the re-vendor alike. That was a deliberate call
under **Stage**, and this PR leaves it there.

## Verification

The new case in `tests/test_interact_layer.py` runs the reported journey
against a page whose log and vendored layer spell the agent's role with
the token #811 renamed: `version check` refuses and names the way out,
`page init` takes it, the vendored kinds come back equal to the
kernel's, `version check` passes, and the standing reply reads back
through `leaf transcript`. Before this change it fails at the refusal,
with the issue's two messages verbatim.


`test_init_refuses_a_historical_event_record_outside_its_declared_schema`
held the removed refusal and moved with it — it now asserts the
re-vendor runs and that the event still replays afterwards, next to the
sibling cases that pin the selection-owned gaps.

`tests/test_interact_layer.py`, `test_interact_contract.py`,
`test_interact_server.py` and `test_interact_document.py` pass together:
705 passed, with
`test_staged_writes_honor_umask_without_copying_a_replaced_symlink_mode`
failing identically on an unmodified checkout of its merge base in the
same sandbox.

<details><summary>One boundary this deliberately leaves open</summary>

Removing the record check leaves the complete stored record with no
validator. The browser door validates with `browser=True`, which drops
`meaning` and `generated` from `required` and strips the fields the
server stamps afterwards, so it only ever sees the client's half;
`append_event`, which the CLI writers use, validates nothing. `page
init` was the one place the full record was checked, several commands
after the write, with an unremediable refusal as its only response —
which is what this PR removes. Restoring the invariant belongs at the
append boundary, where a violation is still the writer's to fix, and
that is a change of its own.

</details>

Closes #820 — automated triage

---------

Co-authored-by: leaf-agent <[email protected]>
max-sixty added a commit that referenced this pull request Sep 19, 2026
Three changes from a survey session: one code fix found by hitting it,
and the list and note edits that follow from the survey's own results.
Item numbers below are `TODO.md` ids, written without a hash so they do
not read as issue references.

## A claim this version cannot read is dropped, not raised on

The claims directory is one per machine and every worktree, host and
session on it writes there, each running the leaf it was built from — so
a record older than the code reading it is ordinary rather than
exceptional. #813 removed the last shim on the premise that "every claim
is written by the running version", which a shared state home makes
false.

Found by hitting it an hour ago: a session held six preview pages
claimed before #811 named a harness, merged a main that had landed it,
and every `leaf wait` afterwards died in `owned_by` on
`claim["harness"]` — taking the watcher off the unrelated page the
reader was on. The page's own loop guard then fired on every turn, and
nothing in the message said why.

This is the third refusal in the family #822 named an hour earlier, from
the same cause: an update swaps the running Leaf under standing state.
#822 fixed the registry read and the re-vendor; the claim read was still
crashing.

Stage still owes nothing to what an older version wrote, so nothing here
migrates a record or reads its old shape. `readable_claim` is one
predicate on the two file readers every other reader goes through: a
record the readings above can take their answers from is a claim, and
any other record is not one this version can read, so its page reads as
unclaimed until something claims it again. That is what deleting the
stale state would have done, without the deletion, and no session is
taken down by state it does not own.

The harness *name* is decided at that same door, which review caught:
`host.claim_harness` does `HARNESSES[claim["harness"]]`, so carrying the
field was not enough — a record naming a harness outside this version's
table passed the predicate and raised two calls further in, in the Stop
hook and in the nudge the append door takes under its lock. Renaming a
harness is reachable by exactly the mechanism that made the field rename
reachable. `turn_closed` joins the field set for the same reason:
`event_endpoint` brackets it, and it was covered only by the accident
that it predates `turn` in the schema.

`CLAUDE.md`'s Stage section gains the missing half of its own rule — it
said stale state is thrown away without saying who throws it.

### Verification


`test_a_claim_an_older_leaf_wrote_is_dropped_rather_than_read_or_raised_on`
runs the reported journey: two pages held by one session, the first
claimed in the shape written before #811, and `leaf wait` on the second.
Bug-back with `readable_claim` removed reproduces the exact production
error, `KeyError('harness')` out of `leaf wait`, rather than a weaker
assertion failure — which needed the stale page to sort ahead of the
reader's page in the watcher's walk, as it did in the wild.

The two review cases go through the Stop hook, which is where their
consequence is. Bug-back on the harness-name guard raises
`KeyError('some-host-a-later-leaf-named')` out of the hook; bug-back on
`turn_closed` has the hook name a page nothing owns, which is the flood
this PR is fixing rather than a raise.

Everyday suite green on the merged head: 1,172 passed.

## `TODO.md`: item 25 retired, items 5 and 7 re-measured

Item 25 asked for one App Server client instead of two. #811, #813 and
#834 dissolved what it named: `codex.py` exports `app_server_connect`,
`app_server_handshake` and `app_server_request`, both callers use them,
`AppServerClient` moved out to `codex_adapter.py`, and
`worker/server.py` imports 17 public names and no private one. The
reconnect backoff, written four ways when the item was raised, is
written once.

What survived it is two turn supervisors over that one protocol —
`HostedTurn` and `WebsiteCodexHost` in `worker/server.py` at about 900
lines, against `TaskObserver` and `DeliveryTurn` in `codex_adapter.py`
at about 1,000. They supervise different things, a local Codex process
with a queue and a lease against a hosted per-reader container turn, so
merging them is a restructure with no deletion. That is item 6's
question, under the product condition item 6 already carries, so item 25
retires and item 6 states what is left of it.

Items 5 and 7 carried figures from before #808, #815 and #797 landed;
both are re-measured against `main` at `76252900` and dated.

## `notes/dependency-survey.md`

One line pointed at a `TODO.md` item `b7e1efd0` had already removed. It
now points at the work that landed.

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

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 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