Skip to content

perf(execution): an open patch keeps its process warm instead of reloading it - #775

Merged
allisonmahmood merged 4 commits into
mainfrom
perf/753-warm-and-touch
Oct 18, 2026
Merged

allisonmahmood merged 4 commits into
mainfrom
perf/753-warm-and-touch

Conversation

@allisonmahmood

@allisonmahmood allisonmahmood commented Oct 18, 2026 •

Copy link
Copy Markdown
Owner

The supervisor reaped a version's process 60 s (execution.process.idle) after its last call, whether or not anyone had the patch open. In production, 25 of 44 reaps in a week happened while a document for that version was open, so reading a page for a minute and then switching screens reloaded the process: first runs took p50 1.12 s against 0.50 s warm, and 3 of 44 hit the 3 s query deadline. The load also only started when the first query arrived, while the client was still booting.

Now a host keeps the version of each connected, ready tier 2 document warm by touching it before each idle window ends, and a document's first stream warms its version as it becomes ready.

How it works

Touch is a new private management operation, POST /touch { bindingEpoch, binding }. For a ready process of exactly that version and hash at the current epoch, it restarts the idle clock and answers touched. A loading process answers loading and keeps its initialization deadline from spawn; no live process answers absent. It never loads, reserves, evicts or changes the epoch. Every reply also carries idleMs, the idle window the supervisor reaps by: its last bind's setting. A different hash is binding_conflict; it checks wrong_task and the epoch like every other operation.

Where the bookkeeping lives: a new runtime module, ConnectedVersions, one per host, built beside the fleet in FleetInvocation and used by RuntimeStream (absent in no-pool dev, like ExecutionLifecycle). It keeps one keepalive per company + patch + version, not per document:

  • Open: when a tier 2 stream sends ready, it calls connect(version, { warm }) for the stream's lifetime. The first holder starts the keepalive, whose first touch is immediate.
  • Reconnect, reauthentication, resume: the shell sends X-Patchy-Generation on every stream after a document's first hello, so warm is true only when it is absent. A document warms once; its renewals only touch, so a version evicted under pressure reloads on demand, never on renewal. No state is needed for this, and it holds across hosts.
  • Last disconnect or hidden suspension: both close the stream, which releases its hold. The last release ends the keepalive, and the process idles out as before.
  • Binding changes (adoption, epoch, deployment replacement): each touch goes through ExecutionLifecycle.touch, which reads the company's current active binding and sends to that task at that epoch. It never admits, claims a task or binds; a company without an active binding gets no touch (unbound).
  • Pause: each touch reads the patch's breaker first (as admission does, off in dev). A paused patch gets no touch, and its warmup doesn't bind.
  • Retire and delete: the stream's existing authority checks close those documents, which ends their keepalive.
  • A new publish while open: the document keeps its loaded version, so its keepalive keeps that version. A reload opens a new document on the new version, which warms it; the old one idles out.

Cadence: the first touch is immediate. Each later touch starts at a random 30–40 % of the idle window after the previous one started, whatever that one cost, and gives up after 10 %. The window is the smaller of the company's configured execution.process.idle (read through StreamLimits) and the idleMs the supervisor last answered. A changed limit reaches the supervisor only with its next bind, so a raise spreads touches out only once the supervisor has it, and a cut applies from the next turn. A cut can still reap a process already idle longer than the new window, as it would without touches; demand reloads it. So a lost touch, even one that fails only at the provider's timeout, still leaves the next to land within 90 % of the window after the last that landed, and versions spread out. This is derived from the idle window, so there is no new registry tunable.

Warmups: on a first stream, the keepalive's touch decides. touched or loading is enough (resident, loading). On absent, it binds the version under a normal call admission (acquire, then the server bundle, then executor.bind, which binds by hash first), concurrent with client boot, without delaying ready. A paused or unbound touch tries nothing. Warmups are bounded:

  • one per logical document;
  • coalesced per company, version and host: a holder joins or creates its version's keepalive in one synchronous step, and warm requests set a flag that keepalive serves, so concurrent documents share one touch and at most one bind at a time. The bind runs beside the touches, so it never delays them;
  • only for documents whose company task is already bound, so they never claim a task or count against the fleet budget beyond what the first call would;
  • the supervisor's residency and memory caps and the provider's management timeout bound the bind like a call's, and its admission stays interruptible as a call's does, so a keepalive whose last document leaves mid-admission claims and binds nothing;
  • a failed warmup is silent apart from the wide event. It doesn't take a subscription run slot, so it never delays the first queries.

More open versions than the process cap (13 under 12): touches never reserve or load, so they can't cause evict/reload churn. Touched residents count as recently used in the eviction order, so versions without open documents go first. Under pressure, demand (a call or a first-stream warmup) still evicts the least recently used, and touches of the evicted version then answer absent until demand reloads it. Memory-pressure eviction is unchanged.

Observability: one new wide event, type: "keepalive", per company version per host when its last document leaves: peakDocuments, touches by outcome (touched, loading, absent, unsupported, paused, unbound, failed) and warmups by outcome (resident, loading, loaded, bound, paused, unbound, failed). This measures what the issue asks for:

  • touches.absent counts versions evicted or reaped while open;
  • process events with callsServed = 0 after a loaded warmup are unused warmups;
  • cause = "evicted" is residency churn;
  • the first call per process generation against warmup timing gives the cold calls avoided.

A touch per open version every ~25 s per host would be too noisy as its own event, so they are counted. OPERATIONS has a query.

Rollout

Touch is a new route, and supervisors' schemas are strict, so it follows #768/#773:

  • The ECS provider sends it only to tasks of its own release's task definition (sameRelease), and through the same verified endpoint cache.
  • A task of another release gets unsupported without a request. Its release is remembered, so later touches don't ask ECS again; a task's definition never changes, and the memory is forgotten on stop.
  • Older hosts never send it.
  • A local task owner that outlived its host's checkout answers 404, which counts as a failed touch: no retry before the next turn, and nothing reaches viewers.
  • A warmup for an older supervisor (unsupported) is a normal bind (bound).

No migrations.

Tests

  • Supervisor.test.ts (real workerd):
    • a touched process outlives its idle window, and a touch for a version it doesn't hold loads nothing;
    • hash and epoch mismatches are refused;
    • once untouched, the process idles out and a touch finds absent;
    • an unfinished load still expires at its deadline from spawn while touches arrive;
    • one more touched version than the process cap: no evictions and no reloads across two idle windows; a demand bind then evicts once, and that version's touches find absent without reloading it. This runs at a cap of 2 rather than 12: touches never reserve, so the property doesn't depend on the cap, and twelve workerd processes would dominate the suite.
  • Management.test.ts: /touch authenticates before reading the body and refuses wrong_task before any effect.
  • EcsTaskProvider.test.ts: an older-release supervisor is never sent a touch, and ECS is asked once about it; a same-release touch reuses the cached endpoint.
  • Fleet.test.ts: a touch claims nothing for an unbound company, follows adoption's new epoch, and sends nothing for a paused patch.
  • ConnectedVersions.test.ts (TestClock, injected ports):
    • warms once per document and coalesces documents;
    • a renewal of an evicted version only touches;
    • a paused version is neither loaded nor woken;
    • each version gets one touch per turn within 40 % of the idle window, however many documents hold it;
    • with the widest jitter and every other touch lost, failing only at the provider's 10 s timeout, each landing still follows the last within the idle window;
    • four concurrent first holders, interleaved by a small scheduler budget, share one keepalive;
    • touches keep to the supervisor's acknowledged window while a raised limit hasn't reached it, spread out once it has, and tighten from the next turn after a cut;
    • a warmup waiting for admission is cancelled when its last document leaves: nothing is admitted afterwards, and the keepalive reports once;
    • the last document leaving stops touches.
  • RuntimeStream.test.ts: a ready document holds its version until disconnect, with warm only on its first stream.

Numbers

I measured on the local fleet (pnpm dev up brightline, EXECUTION_PROVIDER=local-fleet via a temporary, uncommitted runner edit) with PATCHY_LIMITS_JSON={"execution.process.idle":20000}. Each pass, as a member:

  1. open the New Business board cold;
  2. keep it open 30 s;
  3. open a deal (a new deals.timeline query on the same version);
  4. close the page.

There were three passes each, on origin/main and this branch.

main this branch
Open → board shows deals (browser) 311, 313, 323 ms 215, 216, 221 ms
Stream open → first board answer (server) 160–168 ms 130–137 ms
Process reaped while the board was open yes, 20.1 s after the first call no
Deal opened after 30 s: its run 115–124 ms, new process generation 28–30 ms, same generation
Deal opened: click → timeline shows (browser) 217–231 ms 120–128 ms
After closing reaped 20 s after the last call reaped 20 s after close (lifetime 50.4 s = 30.3 s open + 20 s)
keepalive event per pass none touches {touched: 3, absent: 1}, warmups {loaded: 1}

A local load costs ~90 ms; in production the first run was p50 1.12 s, so the gap there should be correspondingly larger. The open-time gain is small locally because the client boots in ~100 ms here, against ~1 s in production.

pnpm verify passes.

Closes #753

Made by Claude Opus 5.5 via Claude Code; reviewed with GPT-6.1-Sol.

…ading it

The supervisor reaped a version's process 60 s after its last call even while
its patch was open, so the next screen reloaded it, and the first query after
open waited for a load that only started when it arrived.

A host now touches the version of each connected, ready tier 2 document before
its idle window ends: a new management operation that restarts a ready
process's idle clock and never loads, reserves or evicts. A document's first
stream also warms its version as it becomes ready. Touches go only to
supervisors of the host's own release.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added area:server Touches the server area area:docs Touches the docs area size:xl Over 1,000 changed lines; review in multiple passes labels Oct 18, 2026
allisonmahmood and others added 3 commits October 18, 2026 21:27
…ncurrent holders share one keepalive

Each touch now starts 30-40 % of the idle window after the previous one
started, whatever that one cost, and gives up after 10 %, so a lost touch
still leaves the next to land within 90 % of the window. Warmup binds run
beside the touches instead of in their way.

A holder now joins or creates its version's keepalive in one synchronous
step, and a keepalive removes only its own entry when its last holder leaves.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…a closing keepalive cancels its warmup's admission

Every touch reply now carries `idleMs`, the idle window the supervisor
enforces, and touches are spaced by the smaller of that and the configured
limit, so a raised limit the supervisor hasn't taken yet can't let an open
version idle out. A cut applies from the next turn.

A warmup's admission is interruptible, as a call's is, so a keepalive whose
last document leaves while it waits claims and binds nothing.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@allisonmahmood
allisonmahmood merged commit 50a0f4d into main Oct 18, 2026
13 checks passed
@allisonmahmood
allisonmahmood deleted the perf/753-warm-and-touch branch October 18, 2026 20:18
allisonmahmood added a commit that referenced this pull request Oct 18, 2026
…t once (#776)

When a subscription POST is refused as `409 invalid_request` but still
names the current stream generation and latest sequence, the stream has
been lost: a **current-generation loss**. The shell tears it down, then
waited its jittered backoff (250–500 ms right after a `hello`) before
reconnecting. In the traced production load on #754, that wait sat in
front of the first query.

Two refusals produce this loss:
- `StreamReplaced`, from a host that doesn't hold the stream, such as a
misrouted POST;
- `StaleSequence`, from a document that just closed.

## Fix

- **One immediate reconnect.** On a 409 the shell reads the body. If it
is `invalid_request` and the POST still names the current generation and
latest sequence, the stream reconnects at once instead of waiting for
the backoff.
- **The allowance comes back only when the document reconciles.** A new
`hello` doesn't restore it. A second loss before then waits for the
backoff, as on main, so a persistent misroute can't loop faster than it
does today.
- **Everything else keeps its handling:**
  - a stale-generation or stale-sequence refusal is ignored;
- `401`, a `409 principal_changed` and `429` tear the stream down and
wait for the backoff;
  - a pending tier 2 bind deadline still wins;
- the reconnect still sends `X-Patchy-Generation`, so #775 only touches
the version and doesn't warm it again.
- **ADR-0010 is updated.** The stickiness cookie lasts one day
(`duration_seconds=86400`), which is what `AppStack.ts` sets. The new
reconnect rule is recorded next to the backoff.

## What "reconciled" means

This uses the shell's existing `reconciled` state: the server has
admitted this stream's latest sequence, and every desired subscription
has been answered past its fence, by a snapshot, an up-to-date or a
refusal. It is the state that already clears the Reconnecting pill.

It's the first point where the shell has proof that its POSTs reach the
stream's host. A `hello` only proves the GET landed somewhere, and an
empty set still needs its replace admitted. So between any two immediate
reconnects there is at least one full, successful round trip through the
stream's host.

## Tests

`stream.spec.ts`, with the page clock paused so that no backoff timer
can fire unless the test runs the clock:

- **Allowance.**
  - A current-generation loss reconnects at once.
- The new stream admits its replace while its answers are held, so it
hasn't reconciled; a loss there waits for the backoff.
  - Once the answers arrive, the next loss reconnects at once again.
  - Every reconnect carries the generation it replaces.
  - **Fails on main**: no second `hello` arrives without the clock.
- **Other refusals.**
  - A stale-sequence `409` leaves the stream alone.
- `401 session_refresh_required`, `409 principal_changed` and `429
rate_limited` each tear the stream down and wait for the backoff.
- The unreconciled replacement's own current-generation loss then still
reconnects at once, so these refusals didn't spend the allowance.
  - Passes on main and here.

I checked that each test catches the bug it pins:
- restoring the allowance on `admitted` fails the first test;
- spending it on any refusal fails the second.

`pnpm verify` passes locally: check, postgres-concurrency, cli-smoke,
tier2-smoke and browser.

## Numbers

Setup:
- a local brightline instance, opening New Business (tier 2) as a
persona;
- a temporary, uncommitted server hook that refused every subscription
POST of a document's first generation as `StreamReplaced`;
- 10 loads each, timed in the shell from the `409` response.

| From the refusal to… | main (backoff) | this branch |
| --- | --- | --- |
| the new stream's request | median 358 ms (274–473) | 15 ms (14–18) |
| its `hello` | 368 ms (282–480) | 24 ms (21–30) |
| the first query answer | 410 ms (317–781) | 51 ms (49–59) |

With every POST refused, a persistent misroute, over 10 s:
- main reconnected 23 times, 300–570 ms apart;
- this branch reconnected 27 times: the first after 23 ms, then 294–498
ms apart.

The allowance adds one immediate reconnect per unreconciled streak.
Jitter alone moves the count over a 10 s window, so the totals don't
measure that exactly.

**Unchanged here.** On main, `hello` resets the backoff's failure count,
so a persistent misroute already reconnects every 0.25–0.5 s
indefinitely instead of backing off further.

Part of #749. Steps 1 and 2, diagnosing the cause from #758's wide
events and fixing it, need production data.

Made by Claude Opus 5.5 via Claude Code; reviewed with GPT-6.1-Sol.

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
allisonmahmood added a commit that referenced this pull request Oct 19, 2026
…ng Reconnecting (#783)

Release 14's What's new entry, written from \`7694bab\`. It covers
everything merged since release 13's \`through\` (\`c9b8fc4\`), and sets
the \`patchy\` release to \`0.14.0\` (\`packages/patchy/package.json\`
and \`CURRENT_RELEASE\`).

| Change | PRs |
| --- | --- |
| Improved: Tools with server code answer faster | #756, #764, #768,
#769, #771, #772, #773, #775, #778 |
| Improved: Your company's tools and pages open sooner | #759, #761,
#766, #770 |
| Fixed: Tools stop flashing Reconnecting while they load | #755, #776,
#780 |
| Improved: Publishing is quicker | #729 |
| Improved: New tools start with a tidy layout | #763 |
| Improved: Your usual is easier to scan | #721 |
| Fixed: A page that isn't there says what to do | #762 |
| Fixed: A logo Patchy can't read is refused, not a crash | #728 |
| Behind the scenes: richer logs for live data | #758 |

#720 is release 13's own entry, so it's left out.

Made by Claude Opus 5.5 via Claude Code.

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

Labels

area:docs Touches the docs area area:server Touches the server area size:xl Over 1,000 changed lines; review in multiple passes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A version's process is reaped while its patch is open

1 participant