Skip to content

Verify hosted Leaf agent turns - #478

Merged
max-sixty merged 5 commits into
mainfrom
codex/hosted-leaf-agent
Sep 9, 2026
Merged

max-sixty merged 5 commits into
mainfrom
codex/hosted-leaf-agent

Conversation

@max-sixty

@max-sixty max-sixty commented Sep 9, 2026 •

Copy link
Copy Markdown
Owner

Summary

  • register the website container's OpenAI outbound handler through Cloudflare's inherited setter, so real Codex requests receive the deployed credential
  • settle each accepted Leaf delivery against its exact page turn, preserving real replies while surfacing failed or incomplete agent turns
  • let edge-missing revision, version, and media files fall through only to an identified active reader's container, so a private revision can become the live document without anonymous allocation
  • require the production secret explicitly and make the deployed-site gate prove exactly one real agent edit, published revision, reply, and rendered result

Production diagnosis

The agent routes introduced in #460 accepted comments and started turns, but the model requests returned 401 in Cloudflare. The replacement leaf.page site key succeeds against the same Responses API directly. The production bundle instead preserved static outboundByHost, which shadowed the Containers SDK's inherited registration setter and left the handler registry empty; assigning the handler after the class definition registers it with the SDK.

Validation

  • uv run pytest tests — 944 passed
  • npm test --prefix worker — 57 passed
  • npm run typecheck --prefix worker
  • pre-commit run --all-files

This was written by Codex on behalf of max-sixty

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

verify_agent_turn's last check can't pass against the deployed site, so this gate reddens every publish-site run — after the deploy step has already put the release into production.

The Worker never serves a container-created revision document. In worker/src/index.ts's default fetch, a GET that is neither isPageApiRequest(route) nor containerOnly is answered from env.ASSETS.fetch(request), and only a 404 on isPageMediaRequest falls through to the container. revisions/ is in the page-resource pattern but is not an API route, so /examples/design-decision/revisions/r<n>-<hash>.html for a revision the agent just published inside the container resolves against the static build. I confirmed it against the exported handler: with __Host-leaf-page and __Host-leaf-active set, that request returns 404 and getContainer(...).fetch is never called.

The runtime can't follow the new revision either. prepareActivation in skills/leaf/assets/runtime/version.js fetches revision.url through authoredDocument, which throws on a non-ok response; prepareActivation reports that and returns null, so the live root keeps the built document. The reloaded page still shows the authored <h1> and the check fails even though the agent did exactly what the comment asked. It fails opaquely, too: the revision and reply checks above it pass, and verify_agent_turn doesn't attach the console/pageerror listeners verify_page uses, so the runtime's own revision N failed to load report is discarded.

Nothing before main catches this — verify-site-local.sh doesn't set LEAF_VERIFY_AGENT, so the function first runs against production.

Which way out is right is a product call rather than a test one. Routing revisions/ (and versions/) GETs to the container for an active session is what would let a reader see what the agent published at all; the media fall-through already exists for the same reason, container-created files absent from the build. Dropping the rendered-heading assertion holds the gate to what the deployed site can do today, and leaves the published revision invisible to the reader who asked for it.

Separately: the agent check runs inside the deploy step's until … sleep 10 loop, whose 900-second deadline was sized for cheap rollout retries. Once the release checks pass, every further iteration posts a new comment and waits up to 300 seconds for a model turn, so a failing agent costs roughly three real Codex turns before the step gives up.

@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 revision fall-through is gated on the path shape alone, so any anonymous GET to a versions/ or revisions/ path the release does not contain allocates a container. With no __Host-leaf-page cookie, sessionId is a fresh randomSessionId(), getContainer(env.PAGES, sessionId).fetch(request) runs, and the tail of fetch answers with Set-Cookie for both __Host-leaf-page and __Host-leaf-active. A crawler walking /examples/design-decision/versions/v2.html, v3.html, … draws one container per request out of the 15,000-container budget and leaves each one marked active. media/<32-hex>.png was unguessable, which is what kept the old predicate out of reach; versions/vN.html is enumerable, so widening it to those two directories is what makes this reachable.

Requiring the reader's own active session costs nothing on the path the PR is for. The container is per-reader, so a session-less visitor gets a fresh empty one whose page directory is the same published tree the edge just answered from — the fall-through can only return the same 404 it was given. The runtime following revision.url after a state read already holds both cookies, which is exactly what the new worker/test/index.test.ts case sets.

The routing and workflow changes do clear the blocker from the previous review: revisions/ now reaches the reader's container, and the agent check runs once after the rollout loop rather than inside it.

Comment thread worker/src/index.ts Resolved
@max-sixty
max-sixty merged commit 7fc8c61 into main Sep 9, 2026
9 of 10 checks passed
@max-sixty
max-sixty deleted the codex/hosted-leaf-agent branch September 9, 2026 04:11
max-sixty pushed a commit that referenced this pull request Sep 9, 2026
…reads it (#483)

`publish-site` deployed release `41a8177c…` successfully and then failed
its own post-deploy agent gate ([run
34309999064](https://github.com/max-sixty/leaf/actions/runs/34309999064)):
`RuntimeError: https://leaf.page/examples/design-decision/ did not
render the agent's published heading; browser errors: []`. Every check
before it had passed, so the hosted turn had reached the model,
published a revision, and replied with `deployment verified`; only the
rendered heading disagreed, and the message named neither the heading it
found nor the revision the browser was standing on.

## What the gate was reading

`verify_agent_turn` polled for the first state where **any** newer
revision and **any** reply existed, and broke out there. A Codex turn is
a process rather than a step: it may publish a checkpoint revision, say
something about the work, and only then publish what the comment asked
for. So the state the loop stopped at was not necessarily the turn's
outcome — and the reload that followed asserted
`page.locator("h1").inner_text() == heading`, an equality over text that
the agent may have quoted and that the runtime may add its own words to
for anything a reader can point at.

I ran the same turn against the same deployed release twice, out of
band, to find out whether the deployment itself was at fault. It was
not: in both, the browser followed the agent's revision
(`meta[name="lf-revision"]` moved `1` → `2`) and rendered the requested
heading at presentation, with the published document served through the
container fallthrough #478 added. The gate was reading the page too
early and too strictly, not reporting a broken deployment — which also
explains why the next commit's run
([34311442765](https://github.com/max-sixty/leaf/actions/runs/34311442765))
passed the same gate unchanged.

## The change

The wait now names the turn's outcome — a published document that
carries the requested heading, **and** a reply that answers for it —
instead of the first revision and first reply to appear. The gate keeps
every claim it made: one real agent edit, a published revision, the
reply #478 asked for, and a rendered result. What changed is when it
reads them and how tolerant each reading is:

- the heading is checked in the published document before the reader's
browser is asked about it, so a checkpoint revision no longer ends the
wait;
- the rendered heading is a containment, since the agent may quote the
heading it was handed and the runtime may inject its own words into a
text block;
- the revision the browser stands on is checked separately, so the two
failure shapes are distinguishable — a browser still on the built
document never followed the agent's revision, while one that followed it
and shows other words is the agent's edit rather than the reader's page;
- every failure message now carries the values it judged, and a green
run prints the revision and the agent's reply, so the next reader of a
red run has something to compare against.

## Verification

- The patched `verify_agent_turn` run against the live deployment
(release `66c2a28e…`), which is the only place this code path exists: it
passed, printing `✓ hosted agent published revision 2 and replied:
deployment verified`.
- `uv run pytest tests/test_website_server.py` — 20 passed. That module
loads `scripts/verify-site.py` and covers its helpers.
- `ruff check` and `ruff format --check` at the pinned 0.16.1.

<details><summary>The three production turns behind this, and one thing
they raise</summary>

Three hosted turns were run against `leaf.page` while diagnosing, each
in its own session and container. All three published a revision whose
`<h1>` was exactly `Deployment <release[:8]> verified`, which is what
rules out both the model's wording of the heading and the render path.

The first two, which overlapped the rollout of release `66c2a28e…`,
completed **without the agent replying at all**: the container settled
the delivery with `worker/server.py`'s `MISSING_REPLY` — "I finished
without posting a reply. Please send a new message to try again." Under
this gate, as under the one it replaces, that is a failure, and
deliberately so: #478's intent is a real reply, and a hosted session
that edits a reader's page without answering for it is worth a red run.
It is now reported as the reply text rather than as a bare mismatch.
Whether that shape is rollout-specific or ordinary is not something
three turns can say; if it recurs on a green deployment, the reply half
of this gate is where it will show up.

The failure on
[34311996078](https://github.com/max-sixty/leaf/actions/runs/34311996078)
is a different one — `ERR_NETWORK_CHANGED` in the pre-deploy local check
— and is untouched by this branch.

</details>

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