Skip to content

Run leaf.page sessions on one vCPU, and start a visitor's container when they focus a text box - #5802

Merged
leaf-agent merged 6 commits into
mainfrom
agent-launch-state
Nov 10, 2026
Merged

leaf-agent merged 6 commits into
mainfrom
agent-launch-state

Conversation

@max-sixty

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

Copy link
Copy Markdown
Owner

A leaf.page visitor's first write waits while their session's container boots and composes the page's layer (about 7 s in production on 9 Nov, after #5764). This tries two ways to cut that wait.

One vCPU per session. Both environments run a custom instance with 1 vCPU and 3 GiB, the smallest custom size Cloudflare allows. In a local Docker reproduction, a cold first write took 1.9 s on it, against 8.7 s on basic's 1/4 vCPU. Cloudflare bills CPU only as it is used and memory as provisioned, so this costs 2.8× basic per awake second, less than standard-1 (1/2 vCPU with 4 GiB). Cloudflare checks the account's 1,500-vCPU limit at rollout, counting max_instances times vCPU. It refused 90 one-vCPU dev containers beside production's 5,900 basic ones, so dev now reserves 20, which fit beside either production size, and production takes 1,470.

Start the container when a text box gets focus. The tab's interaction log already reports each focus to the Worker within a quarter second. It now marks a focus entry whose control is one of the runtime's text boxes (TEXT_BOX) with textBox, and when a batch carries one, the Worker starts the session's container (prewarmContainer), with a per-source rate limit. Reads still start nothing.

What dev showed. I deployed this to dev, then redeployed dev on basic with the same code, and sent first comments from fresh browsers:

Arm n POST median (range) Write after the container was ready
1 vCPU, no prewarm 8 12.2 s (7.5–19.3) 7.7 s
basic, no prewarm 5 13.9 s (10.7–41.7) 10.2 s
basic, prewarm, 10 s of typing 4 29.7 s (14.3–48.4) 30.3 s (ready 0.7–6.8 s before the POST)

Every test session landed on a container booting for the first time since its deploy re-placed it. Those first boots were slow on both sizes: the containers' startup steps took 2.3–2.8 s against production's 1.9 s, and the write after boot grew across the session. So dev shows that the prewarm hides the boot, but it can't show what 1 vCPU does on production's containers. Production had one visitor cold write last week, so the trial is better read a day after merging from steadier counts: the containers' startup steps (about 2,300 a week) and CI's cold page loads (about 770 successful a week).

The Worker tests cover the focus trigger and the config, and the interaction log's browser test checks that a textarea's focus carries textBox. pnpm run check passes.

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

…hen they focus a text box

A page's first write on leaf.page waits while its container boots and composes the
page's layer. On basic's 1/4 vCPU that took 8.7 s in a local reproduction; on a
custom 1 vCPU / 3 GiB instance it took 1.9 s. Cloudflare bills CPU only as it is
used and memory as provisioned, so this instance costs 2.8x basic per awake second,
less than standard-1's 1/2 vCPU with 4 GiB. The account's default limit of 1,500
concurrent vCPU caps production at 1,400 containers beside dev's 90.

The tab's interaction log already reports each focus to the Worker within a quarter
second. When a batch shows focus entering a text box (leaf-text or textarea), the
Worker now starts the session's container, rate limited per source address, so the
boot overlaps the visitor's typing. Reads still start nothing.
… first

Cloudflare counts each application's max_instances times its vCPU against the
account's 1,500 when it rolls out: ninety one-vCPU dev containers beside
production's 5,900 basic ones (1,475 vCPU) were refused. Twenty fit beside either
production size, and production takes the remaining 1,470.

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

One finding, inline: the Worker's focus trigger keeps its own copy of the runtime's text-box selector, and no check ties the two together, so the prewarm can stop silently.

Comment thread worker/src/interactions.ts Resolved
…warm reads it rather than matching tag names

The prewarm matched the focused control's locator against its own copy of the
runtime's TEXT_BOX, so renaming leaf-text would have stopped it with every check
passing. describe() now sets textBox on a focus entry whose control matches
TEXT_BOX, the Worker's projection keeps that field, and startsTyping reads it.
The interaction recording browser test checks a textarea's focusin carries it.
@max-sixty
max-sixty marked this pull request as ready for review November 10, 2026 21:14
@leaf-agent
leaf-agent merged commit f824072 into main Nov 10, 2026
7 checks passed
@leaf-agent
leaf-agent deleted the agent-launch-state branch November 10, 2026 21:20
max-sixty added a commit that referenced this pull request Nov 10, 2026
…ning a thread

Opening a thread focuses its comment box, which since #5802 starts the session's
container for the reply. verify-site opened the saved thread before checking that
the revision resource and document came from the Durable Object, so the prewarm
could wake the container first; publish-site for #5802's merge failed with
'private revision resource woke its container'. The thread check now runs after
the checks that reading left the container asleep.
max-sixty pushed a commit that referenced this pull request Nov 10, 2026
… box's focus now starts the container (#5810)

`publish-site` on `main` has failed since #5802 ([run
42904167365](https://github.com/max-sixty/leaf/actions/runs/42904167365),
`RuntimeError: private revision resource woke its container`). #5802
made the Worker start a visitor's container (`prewarmContainer`) when an
interaction batch reports a text box's focus. `verify_saved_state` in
`leaf-dev verify-site` clicks the saved thread, which focuses its reply
`leaf-text`, and only after that checks that the state, the revision
stylesheet and the revision document came from durable storage. The
run's log shows the sequence: a `focusin` on `leaf-text` with `textBox:
true`, then `container_prewarm_completed`, then the stylesheet GET
taking 4 s and being answered by the woken container.

This PR moves the thread step to the end, after every durable read, the
"reading saved content started a page container" comparison and the
media write. Each of those checks again finds the container stopped: the
reads still prove they are served from durable storage, and the write
still proves it wakes the container and that the next writer restores
the record. The thread's saved comment and the browser-error check still
run, after the thread opens. Nothing changes in the Worker, the runtime
or the prewarm.

**Verification:** this PR's `ci` runs `uv run leaf-dev verify-site
website-worker` ("Measure the bundled release in a browser"), the same
command as the failing step. The failing run above is the before.

**For the author of #5802 to weigh:** opening a thread puts focus in its
reply box, so every thread open on leaf.page now starts the session's
container, even when the visitor only reads. The rate limit bounds this.
Whether that counts as "about to write" is a product call, and this PR
doesn't make it.

Refs #5809 — automated triage

---------

Co-authored-by: leaf-agent <[email protected]>
max-sixty added a commit that referenced this pull request Nov 12, 2026
…ntainer (#5812)

When a leaf.page visitor focuses a text box, the Worker's prewarm
(#5802) now asks the session's container to compose the page's layer,
rather than only starting the container. The visitor's first write then
finds the composition done instead of paying for it.

**Why here.** A first write spends most of its admission composing the
page's layer (theme, package modules, merged registry), which
`leaf.layer` keeps only in process memory. Shipping it pre-built in the
image would need a saved form of those caches in main code, so this
computes it earlier instead: website code calls the public
`registry.storage.selected` for the focused page. Everything is in
`worker/`.

**Measured** in the website image with a stub page store, on 1 vCPU,
admission of the first write (median of 2–3 runs):

| Write sent after boot | Today | Every page composed at boot | This PR
(focused page, at focus) |
| --- | --- | --- | --- |
| 0 s, no focus (a first click) | 1.13 s | 1.47 s | 1.13 s |
| 2 s after focus | 0.93 s | 0.45 s | 0.17 s |
| 8 s after focus | 0.94 s | 0.18 s | 0.18 s |

Composing one page takes about 0.9 s on 1 vCPU. Composing all 47 at boot
took 2.0 s and slowed a write that arrived meanwhile, so the container
composes only what the focus names.

**Changes.** The container answers `POST <page>/_leaf/agent/prewarm` by
composing that page's layer and logging `layer_prewarm_completed`.
`prewarmContainer` posts there through the session's Durable Object,
which starts the container as before.
`tests/process/website_server/test_dispatch.py` and the Worker's prewarm
test cover each side.

> _This was written by Claude Code on behalf of max-sixty_
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