Skip to content

Serve Leaf website shells from the edge - #414

Merged
max-sixty merged 4 commits into
mainfrom
codex/edge-live-shell
Sep 7, 2026
Merged

max-sixty merged 4 commits into
mainfrom
codex/edge-live-shell

Conversation

@max-sixty

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

Copy link
Copy Markdown
Owner

The public site currently waits for a per-reader container before returning even its immutable HTML, so a cold request can spend several seconds before the browser receives its first byte. This serves the complete Leaf shell—documents, version and revision views, runtime, widgets, theme, and media—from Cloudflare's asset edge while leaving /api/* on the canonical Python server and per-reader event log.

leaf.live_shell is the reusable materialization boundary: it derives public bytes from a complete page directory using the same supervision and route-scoping functions as live HTTP delivery, without copying mutable session state. The website build emits that derived asset tree beside the private container tree. The Worker routes non-API GET/HEAD requests to the edge, falling through to the canonical container only when reader-uploaded media is absent there. The shared startup supervisor can also recover from a static registry generation, and the deployment smoke test waits until each page's edge shell and container report the same layer.

Tests compare materialized files byte-for-byte with canonical server responses, assert that private state never enters the asset tree, and prove that published assets do not allocate a container while uploaded media remains available. The full 907-test Python/browser suite, 27 Worker tests, TypeScript checks, pre-commit, site build, and Wrangler deployment dry-run pass.

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.

Two consequences of moving the delivery boundary look unhandled.

The deploy verification can never pass. $layer.generation is a fresh secrets.token_hex(16) per leaf page init — see _stamp_layer in skills/leaf/scripts/leaf/vendoring.py, whose comment calls it the epoch of a contract transition — and the site build runs one init per example (prepare in scripts/preview.py) plus one shared init copied to every product page. So the layer read off /examples/feature-gallery/ is feature-gallery's own generation and never equals the Leaf-Layer that the root product page or design-decision reports, and both verify_state calls exhaust their 20 attempts before failing the step. I confirmed the values diverge by running two bin/leaf page init with identical selections: same $layer.fingerprint, different $layer.generation. The deploy itself has already landed by then, so the effect is a red publish-site on every push to main rather than a blocked rollout.

Reader-uploaded media stops resolving. POST /api/media still reaches the container, store_uploaded_media writes into that reader's page directory, and _json's scope_page_urls hands back /examples/<name>/media/<digest>.png. That path is a non-API page route, so the new branch sends the follow-up GET to env.ASSETS, which only holds the media the build materialized — the reader's own upload 404s. Pasting an image into a composer on any published example (chrome.js wires uploadMedia into configureInput unconditionally, and the published handler applies no publication gate to /api/media) now leaves a broken image where the container used to serve it.

Comment thread .github/workflows/publish-site.yaml Resolved
Comment thread worker/src/index.ts Resolved
@max-sixty
max-sixty merged commit cb02617 into main Sep 7, 2026
19 checks passed
@max-sixty
max-sixty deleted the codex/edge-live-shell branch September 7, 2026 21:28
max-sixty pushed a commit that referenced this pull request Sep 7, 2026
## Problem

`publish-site` has gone red on `main` twice since #414 introduced the
layer-matching verification — [run
34163256368](https://github.com/max-sixty/leaf/actions/runs/34163256368)
(the build of #414 itself) and [run
34163695118](https://github.com/max-sixty/leaf/actions/runs/34163695118)
(the build of #411). Both times the deploy succeeded and `Verify public
Leaf pages` spent its whole budget without the edge shell's layer and
`/api/state`'s layer ever agreeing. In the second run the state body
carried `"producer": {"commit": "cb02617a7704"}` — the container was
still serving the *previous* deploy's image while the edge served the
new shell.

The step asks the same session cookie on all twenty attempts. That
cookie picks one Durable Object, and `sleepAfter = "10m"` on
`LeafWebsiteSession` keeps a container Cloudflare has already started
alive for ten minutes. So the first attempt seats a container on
whatever image the rollout has reached at that instant, and the
remaining nineteen attempts re-read that one instance. What the step
then measures is the image one session began on — not the image the
fleet hands a reader arriving now, which is the property it exists to
prove.

I measured both against the live site while run 34163695118's rollout
was still in flight, polling a fresh session and one fixed session side
by side against the edge shell's layer `6260affc`:

```
edge shell layer: 6260affc432de909ef10a47d5552bb36
t+7s    fresh=017bc7bd  fixed=017bc7bd
t+45s   fresh=6260affc  fixed=017bc7bd
t+140s  fresh=017bc7bd  fixed=017bc7bd
t+217s  fresh=6260affc  fixed=017bc7bd
...
t+505s  fresh=6260affc  fixed=017bc7bd
fresh converged at: 45s ; fixed converged at: never
```

A new session reached the new image in 45 seconds. The fixed session
never did, across 8.7 minutes of polling — consistent with the idle
timer, and the reason the twenty attempts could not have converged
however many of them ran.

## Solution

Mint a session cookie per attempt, so each one asks as an arriving
reader, and bound the loop by one fifteen-minute deadline shared across
both pages rather than a fixed twenty attempts. The deadline is what
covers the rest of what the measurement shows: a gradual rollout leaves
the fleet mixed for a while, so fresh sessions still landed on the old
image intermittently out to `t+198s`, well past the ~145s the old loop
actually spent. Fifteen minutes is entered about 2 minutes into the job
and leaves margin under `timeout-minutes: 30`. Sharing it between the
two pages is right because they are waiting on the same rollout, but
each page keeps five attempts past the deadline: the two pages mint
separate cookies and so seat separate containers, and a rollout slow
enough to run the first page to the deadline must not leave the second a
single probe before it reports a healthy deploy red.

The failure line now names both layers, and each attempt clears the
state layer and body it is about to overwrite so the line reports what
this attempt observed rather than an earlier one's reading. Diagnosing
these runs meant inferring the mismatch from `producer.commit` buried in
the dumped state body, because the step printed the body and not the two
values it had just compared.

## What this does not address

During a rollout a real reader can be served a new edge shell against an
old container, and `sameLayer` in `layer-client.js` reloads the tab on
that mismatch — with the session cookie sticky, into a reload loop until
the container's idle timer retires it. This PR makes CI observe that
window honestly rather than closing it; narrowing it (a faster container
rollout, or a reader-facing answer to the mismatch) is a deployment and
reader-experience decision I have left to you.

## Testing

`publish-site` runs only on pushes to `main`, so this branch's CI cannot
exercise the changed step. I ran the step's script as CI runs it,
extracted from the workflow with the same `bash -e`:

<details><summary>Extracted-step runs against the live site</summary>

Against the settled site, both pages pass on their first attempt:

```
$ time bash -e /tmp/verify.sh
true
true
real	0m7.165s
exit=0
```

The deadline path, with the deadline cut to 12s and the product
predicate falsified so it can never pass — it breaks out, names both
layers, dumps the body, and returns 1:

```
$ bash -e /tmp/verify-fail.sh
https://leaf.page/ served layer 6260affc432de909ef10a47d5552bb36; https://leaf.page/api/state served layer 017bc7bd82e042ede192a56a1e1b41de
{"layer": {"generation": "017bc7bd82e042ede192a56a1e1b41de", ...
exit=1
real	0m16.200s
```

That run also caught the live failure in the act: the edge was serving
#411's shell while the container still served #414's image, the same
disagreement both red runs reported.

Re-running the extracted step against the settled site after the review
fixes still passes both pages on their first attempt (7.2s). With the
deadline set to 0 so it is already spent, the first page passes and the
second still probes five times instead of once. Stubbing `curl` so the
shell fetch succeeds once and then fails: before the per-attempt reset
the failure line reported `served layer BBB` and dumped attempt 1's body
for a probe that never ran; after it, both read `none`.

The convergence measurement above polled `https://leaf.page/api/state`
every 15s, reading `Leaf-Layer` from the response headers, with a new
`openssl rand -hex 16` cookie for the `fresh` column and one cookie held
for the whole run for the `fixed` column.

`pre-commit run --files .github/workflows/publish-site.yaml
worker/README.md` passes, actionlint — which shellchecks the `run:`
block — included.

</details>

---

Automated fix for [failed run
34163256368](https://github.com/max-sixty/leaf/actions/runs/34163256368).

---------

Co-authored-by: leaf-agent <[email protected]>
max-sixty added a commit that referenced this pull request Oct 11, 2026
The runtime's two probes of a page's registry, startup recovery in
`bootstrap.js` and `sourceMovedOn` in `layer-client.js`, read the
delivery's layer from the `Leaf-Layer` header and, when it was absent,
parsed the registry body for `$layer.generation`. That fallback came
with #414, when edge-served shells lacked Leaf headers. Every host Leaf
supports now stamps the header on what a probe fetches: the local server
on `/registry.json` and sample routes (`http.py`, `_delivery_headers`),
the Worker on page routes and released assets (`stampDelivery`), and
`leaf page --export` never probes (its probe is empty and it boots
through `offline-delivery.js`).

Behavior change: a host that serves the tree without `Leaf-Layer` can no
longer supervise startup. Its probe settles nothing, so a failed start
waits rather than reloading onto a replaced layer.

- Delete the body fallback in both probes.
- Delete `test_a_failed_bootstrap_hears_a_static_registry_generation`,
  which held the header-less static host.
- `write_live_shell`'s docstring now states that its host stamps the
  delivery headers, in place of saying a static host may serve the tree.
- `tests/test_site.py`'s stand-in for the edge stamps `Leaf-Layer` and
  `Leaf-Release` on released assets, as the Worker does.



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

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants