| index ccff3fa36..e72a2fbb7 100644 |
| --- a/docs/superpowers/specs/2026-10-28-edge-held-streams-design.md |
| +++ b/docs/superpowers/specs/2026-10-28-edge-held-streams-design.md |
| @@ -1,7 +1,11 @@ |
| # Edge-held event streams — design |
| |
| Discussion: carlos/platform #432 (feature request from Rastrillo, with the |
| -amendment in post 835). Status: draft, rev 1, 2026-10-28. |
| +amendment in post 835). Status: draft, rev 2, 2026-10-28. |
| + |
| +Review history: round 1 (Astra, `CODEX-FINDINGS-edge-held-streams.md`): |
| +NOT READY, 4 P1 and 6 P2. All ten are accepted in rev 2 and marked |
| +**(r1 P1-n / P2-n)** where they land. None declined. |
| |
| ## 1. Intent |
| |
| @@ -33,10 +37,12 @@ Goals: |
| - G3. While the instance sleeps, the browser's connection stays open and alive |
| (edge heartbeats). When the instance wakes, the edge reopens the upstream |
| with `Last-Event-ID` and the browser sees one unbroken stream. |
| -- G4. **No silent stream**: while the instance is awake, a held downstream |
| - always has an upstream, or is being reconnected to one within a bounded |
| - time, or is closed. The page never goes quiet with nothing to say so. |
| -- G5. Works for every app with no app change beyond the opt-in marker, and |
| +- G4. **No silent stream**: while the host is *admissible* (§3.5), a held |
| + downstream has an upstream leg, or is inside a bounded reopen delay |
| + (§3.6), or is closed. Its quiet time is bounded by one tick (§3.7). |
| +- G5. **No stream holds up a stop.** Every upstream leg is cancelled before |
| + the process (or generation) it is connected to is signalled to stop. |
| +- G6. Works for every app with no app change beyond the opt-in marker, and |
| costs no bucket reads and no S3 requests. |
| |
| Non-goals: |
| @@ -46,6 +52,7 @@ Non-goals: |
| - Knowing whether the page applied an event. `Last-Event-ID` at the edge says |
| delivered, not applied; Rastrillo tracks fetches owed in the browser. |
| - WebSockets, long-poll, or any non-SSE stream. |
| +- Redirects on a held stream (§3.3). |
| - Cross-origin streams (§5.8). |
| - Fanning events out between replicas or boxes. An app's `Notify` reaches the |
| streams its own process holds; that is the app's design, not the edge's. |
| @@ -55,7 +62,8 @@ Non-goals: |
| Anti-goals (a failure even if it works): |
| |
| - Any path by which an opted-in stream keeps an instance awake, including a |
| - fallback to a plain proxied stream when a bound is hit. |
| + fallback to a plain proxied stream when a bound is hit or a capability is |
| + missing. |
| - Any path by which an outsider's request wakes an instance it would not have |
| woken without the marker. |
| - A per-request bucket or registry read on the hold path. |
| @@ -68,22 +76,34 @@ An app opts a stream in by putting `carlos_hold=1` in the stream's query |
| string: `new EventSource("/live?carlos_hold=1")`. Paul accepted this over a |
| list of stream paths in the app's config. |
| |
| -A request is a **held-stream request** when all of these hold: |
| +**Classification** happens once, on the edge whose activator manages the |
| +host, immediately after the upstream lookup (§3.8). A request is **marked** |
| +when its query has the parameter `carlos_hold` with the exact value `1`. On |
| +a route whose upstream is `hibernating` (`ActivatorOwned() && !viaTunnel`) |
| +and whose row has `Hibernate` set, a marked request is one of: |
| + |
| +- a **held-stream request**: GET, `Accept` includes `text/event-stream` |
| + (every `EventSource` sends it), not an upgrade. It goes to the hold |
| + branch; |
| +- anything else marked (another method, no SSE `Accept`, an upgrade): |
| + answered `400` by the edge **(r1 P2-1)**. |
| + |
| +Either way, **a marked request to a hibernating route never reaches |
| +`act.ServeHTTP`, the asset catch, the shared cache, or an upgrade dispatch.** |
| +That is the invariant G1/G2 rest on, and it is enforced in one place: the |
| +classification sits ahead of every one of those dispatches, and nothing |
| +after it re-reads the marker. |
| |
| -- the method is GET; |
| -- the query has the parameter `carlos_hold` with the exact value `1`; |
| -- the `Accept` header includes `text/event-stream` (every `EventSource` |
| - sends it); |
| -- the route is a hibernating instance this box's activator manages (the |
| - upstream's `hibernating` flag, which is `ActivatorOwned() && !viaTunnel`, |
| - and `rt.Hibernate`). On any other route the marker is stripped and the |
| - request proxied exactly as today. |
| +On any other route (always-on, resident, a service, a static site) the |
| +marker is stripped and the request proxied exactly as today: there is no |
| +sleep there to protect. |
| |
| The holding edge strips `carlos_hold` from the query before any upstream |
| leg, leaving the rest of the raw query byte for byte (no re-encoding, no |
| reordering). The app never sees the marker. An edge that forwards the |
| -request elsewhere (a backhaul hop, a fleet tunnel) does **not** strip it: the |
| -marker has to reach the edge that will hold the stream (§3.4). |
| +request elsewhere (a backhaul hop, a fleet tunnel) does **not** strip it and |
| +does not classify: the marker has to reach the edge that will hold the |
| +stream (§3.4). |
| |
| Why the marker is safe to accept from anyone: like `X-Carlos-Sidecar`, it |
| only weakens the sender's own request (no activity credit, no wake). It |
| @@ -91,106 +111,183 @@ cannot suppress another client's activity and cannot wake anything. The one |
| thing an outsider can do with it is take up held-stream slots (§3.6), which |
| is equally true of any opt-in. |
| |
| -### 3.2 Activity and wake semantics |
| - |
| -Every upstream leg of a held stream (first open and every reopen) goes |
| -through the sidecar admission path, `activator.BeginSidecarRequest`, not |
| -`begin`: |
| - |
| -- awake: proceed, no in-flight count, no stamp; |
| -- waking (someone else's wake): join it and proceed when it finishes; |
| -- asleep, stopping, fenced, shutting down: no leg; the stream is held. |
| - |
| -Nothing on the hold path calls `begin`, so G1 and G2 hold by construction. |
| -The browser's request itself is not counted either: the holding handler |
| -answers it entirely outside `begin`. |
| +### 3.2 The activator owns leg admission and cancellation (r1 P1-1, P1-2, P1-4, P2-5) |
| + |
| +Every upstream leg (first open and every reopen) is admitted by the |
| +activator, which registers it against the entry and the generation it was |
| +admitted on, under `a.mu`, in the same step that decides it may run. The |
| +Activator seam (`edge.Activator`) gains these methods. They are part of the |
| +required interface, not an optional one: the edge and activator ship in one |
| +binary, test fakes get them too, and a nil activator already means no route |
| +is `hibernating`. There is no "holding unsupported" fallback to proxy through |
| +**(r1 P1-4)**. |
| + |
| +- `AdmitHeldLeg(ctx, rt) (HeldLeg, error)`. Same decisions as |
| + `BeginSidecarRequest` (awake: admit; waking: join, then decide; asleep, |
| + stopping, fenced, probe-held, shutting down, child dead: refuse with no |
| + wake), plus: on admit, under `a.mu`, it records the leg on the entry with |
| + the current generation number and a cancel function. It never calls |
| + `begin`, never touches `inflight` or `lastActive`. |
| +- `HeldLeg` carries `Context()` (derived from `ctx`, cancelled by the |
| + activator as below), and `Done()` to deregister when the leg ends. |
| +- `HeldAdmissible(host) bool`: in-memory, under `a.mu`; true exactly when |
| + `AdmitHeldLeg` would admit without waiting. It is the same predicate, |
| + written once and used by both **(r1 P2-5)**. |
| +- `SetHeldWakeHook(func(host string))`: called, outside `a.mu`, when a host |
| + becomes admissible: a wake completes, a recovery probe releases its hold, |
| + a fence lifts. The edge side is a non-blocking per-host channel send; the |
| + hook may drop a signal and nothing depends on it except latency (§3.7). |
| + |
| +**Cancellation is synchronous and lives with the stop, not with a |
| +notification.** One helper, `cancelHeldLegsLocked(e, gen)`, cancels every |
| +registered leg on the entry (or on one generation of it). It only calls |
| +context cancel functions, so it is safe under `a.mu` and never blocks. It |
| +is called: |
| + |
| +- from `beginStopLocked`, before the stop goroutine is started, for every |
| + generation of the entry. Every stop reason that goes through it (idle, |
| + lease lost, exited, fenced, restart, release, retired, shutdown) is |
| + covered; |
| +- in the swap's switch critical section (`swap.go`, beside |
| + `interruptCmdsLocked(e, retiring.gen.N, …)`), for the retiring |
| + generation, before `genRetired` and before the retiring generation's |
| + drain **(r1 P1-1)**. |
| + |
| +The plan enumerates the remaining direct process-stop calls that do not pass |
| +through `beginStopLocked` (for example `maintenance.go`'s and the restart |
| +path's `proc.Stop` calls) and routes each through the helper. A guard test, |
| +in the style of the swap simulation's markers, fails when a process-stop call |
| +site exists without it. |
| + |
| +Why this closes the races **(r1 P1-2)**: admission and registration are one |
| +step under `a.mu`, and so are the state change to stopping (or the |
| +generation switch) and cancellation. A leg is either registered before the |
| +stop, and cancelled by it, or admitted after it, and refused. A leg that |
| +was admitted and then cancelled before it dialled fails its dial at once. |
| +Through the generation proxy, a leg registered on generation N can only |
| +reach N: the switch that makes N+1 current cancels N's legs in the same |
| +critical section, so a leg whose dial happens after the switch already has |
| +a cancelled context. The process's SIGTERM drain therefore never has a held |
| +stream handler to wait for. |
| + |
| +When a leg's context is cancelled, the edge treats it exactly as "upstream |
| +ended" (§3.3 step 6): if the host is admissible (a swap: the successor is |
| +current), it reopens at once; if not (a stop), it holds. |
| |
| ### 3.3 Hold, relay, resume: the per-stream loop |
| |
| -Each held stream is one goroutine on the holding edge, owning the browser's |
| -connection, with this loop: |
| - |
| -1. **Register** the stream in the host's held set (§3.5) before checking the |
| - instance's state, so a wake that lands between the check and the |
| - registration still finds it. |
| -2. **Admit** an upstream leg (§3.2). If admitted, open it: the original |
| - method, path and raw query (marker stripped), the original headers minus |
| - hop-by-hop headers, and `Last-Event-ID` set to the last id the edge has |
| - relayed (or the browser's own `Last-Event-ID` before the first one). |
| -3. **First leg, instance awake** (the browser has had no response yet): the |
| - upstream's response becomes the browser's. A 200 `text/event-stream` |
| - is relayed and the loop continues; anything else is passed through as |
| - is (status, headers, body), and the stream ends. A session that expired |
| - answers 401 or 303, and the browser's `EventSource` fails with the app's |
| - real answer instead of looping. |
| -4. **Not admitted** (asleep): if the browser has had no response yet, the |
| - edge writes `200`, `Content-Type: text/event-stream`, |
| - `Cache-Control: no-cache`, and flushes. The stream is now held: the edge |
| - sends heartbeats (§3.7) and waits for a wake signal or a tick. |
| -5. **Reopen** (any leg after the browser has its response): the upstream |
| - must answer 200 `text/event-stream`. Anything else (401, 403, 303, 404, |
| - 5xx) closes the browser's stream. The browser reconnects to the same |
| - marked URL; that reconnect is a fresh held-stream request, so step 3 |
| - passes the app's real answer through and the browser stops (#432 item 6 |
| - as corrected in post 835). |
| -6. **Upstream ends.** Ask the activator whether the host is awake: |
| - - not awake (stopping or asleep): back to held (step 4's heartbeats); |
| - - awake: the app ended the stream itself (it bounds stream lifetime). |
| - Reopen at once (step 2). Rapid ends are backed off (§3.6). |
| -7. **Browser ends** (disconnect, context done): close any upstream leg and |
| - deregister. |
| - |
| -Relaying is per event, not per byte. The edge reads the upstream line by line |
| -and writes an event downstream, flushed, when its terminating blank line |
| -arrives. That is the only buffering: one event in progress, capped at |
| -64 KiB (an event over the cap closes the stream). Event framing is what |
| -makes the rest safe: a heartbeat is only ever written between events, and an |
| -upstream that dies mid-event leaves nothing half-written for the next leg's |
| -first event to merge into. The edge tracks `id:` exactly as the SSE spec |
| -defines the last event ID (an `id` field containing NUL is ignored; an empty |
| -`id` resets it). `retry:` and comment lines are relayed as part of the event |
| -they arrive in, or on their own if they arrive alone. |
| +Each held stream is owned by one **stream task** on the holding edge. It is |
| +the only writer of the browser's connection. Leg readers and timers feed it |
| +over channels, so no blocking read or admission can stall heartbeats, |
| +cancellation or the maximum hold (§3.6) **(r1 P2-3)**. |
| + |
| +1. **Register** the stream in the host's held set before anything else, so a |
| + wake signal that lands during the next step still finds it. |
| +2. **Admit** a leg (§3.2). Admission can wait up to `wakeWaitBound` when it |
| + joins another request's wake. The first response is never kept waiting |
| + for that: if the browser has had no response 2 seconds after the stream |
| + task started, the edge commits its own held response (step 4) and the |
| + admission carries on behind it. |
| +3. **First leg, browser has no response yet.** The leg's response becomes |
| + the browser's: |
| + - `200` with `Content-Type: text/event-stream` and identity encoding: |
| + the edge writes the leg's headers through the shared response hooks |
| + (§3.3a), minus `Content-Length`, `Content-Encoding`, |
| + `Transfer-Encoding` and hop-by-hop headers **(r1 P2-2)**, and starts |
| + relaying; |
| + - a `3xx`: **not followed and not passed through.** The browser's Fetch |
| + would follow it and the request it then makes carries no marker, so it |
| + would be counted and could wake the host **(r1 P1-3)**. The edge |
| + answers `204 No Content` instead, which a native `EventSource` treats |
| + as final (it stops and does not reconnect). Apps that want the page to |
| + react to an expired session answer 401 or 403; |
| + - any other status (401, 403, 404, 5xx…): passed through as is, through |
| + the shared response hooks. The browser's `EventSource` stops with the |
| + app's real answer; |
| + - `200` that is not identity-encoded `text/event-stream`: `502`, logged |
| + throttled per host. |
| +4. **Not admitted, or the 2-second commit.** If the browser has had no |
| + response yet, the edge writes `200`, `Content-Type: text/event-stream`, |
| + `Cache-Control: no-cache`, and flushes. These are the edge's own headers; |
| + none of the app's are sent. The stream is now held: heartbeats run |
| + (§3.7) and the task waits for a wake signal, a tick, or its admission |
| + finishing. |
| +5. **Reopen** (any leg after the browser has a response): the leg must |
| + answer `200` identity `text/event-stream`. Its response headers are |
| + discarded (the browser's headers went out with the first response; a |
| + `Set-Cookie` on a later leg is lost, which the app docs say). Anything |
| + else, including a `3xx` (not followed), closes the browser's stream. The |
| + browser reconnects to the same marked URL; that is a fresh held-stream |
| + request, so step 3 gives it the app's real answer and it stops (#432 |
| + item 6 as corrected in post 835). |
| +6. **Leg ends** (upstream EOF, transport error, or its context cancelled): |
| + if `HeldAdmissible(host)`, reopen through the reopen rule (§3.6); |
| + otherwise hold. |
| +7. **Browser ends** (disconnect, write deadline, context done): cancel the |
| + open leg, call its `Done()`, deregister. |
| + |
| +**Opening a leg.** Each leg is a GET built from the original request: same |
| +path and raw query (marker stripped), same headers minus hop-by-hop |
| +headers, `Accept-Encoding: identity` in place of the browser's |
| +**(r1 P2-2)**, and `Last-Event-ID` set to the last id the edge has relayed |
| +(or the browser's own `Last-Event-ID` before the first one). Its context is |
| +the `HeldLeg`'s. Response headers must arrive within 10 seconds, or the leg |
| +counts as failed. |
| + |
| +**3.3a The leg goes through the route's own proxy (r1 P2-6).** A leg is |
| +served by running the route's existing `up.proxy` (the same handler chain |
| +`act.ServeHTTP` would have been handed) into an edge-internal response |
| +writer that hands status, headers and body to the stream task. So every leg |
| +gets exactly what a proxied request gets today: forwarded-header |
| +reconstruction and edge signing, purge-header removal, generation |
| +selection and stamping, response cookie scoping and the other response |
| +hooks. The relay never opens its own transport. Two consequences for the |
| +builder: the reverse proxy aborts a failed body copy with |
| +`panic(http.ErrAbortHandler)` when it sees a server context, so the leg runs |
| +in its own goroutine and recovers that panic as "leg ended"; and terminal |
| +first-leg answers (step 3) are written from what that writer captured, so |
| +they too have been through the response hooks. |
| + |
| +**Relaying is per event.** The edge reads the leg line by line and hands the |
| +stream task one complete event at a time, on its terminating blank line. |
| +That is the only buffering: one event in progress, capped at 64 KiB (an |
| +event over the cap closes the stream). Event framing makes the rest safe: a |
| +heartbeat is only ever written between events, and a leg that dies |
| +mid-event leaves nothing half-written for the next leg's first event to |
| +merge into (a partial event is dropped). The edge tracks the last event ID |
| +exactly as the SSE spec defines it (an `id` field containing NUL is ignored; |
| +an empty `id` resets it). `retry:` and comment lines are relayed as part of |
| +the event they arrive in, or on their own if they arrive alone. A BOM at the |
| +start of a leg is stripped before parsing. |
| + |
| +**Downstream writes** have a 10-second write deadline |
| +(`http.ResponseController.SetWriteDeadline`). A browser that cannot take a |
| +heartbeat in 10 seconds is closed. |
| |
| ### 3.4 Which edge holds |
| |
| The edge whose activator manages the host holds the stream: the same edge |
| that would otherwise call `act.ServeHTTP` for it. Edges that only forward |
| (backhaul hop to the home box, fleet tunnel) proxy the marked request as an |
| -ordinary long request, unchanged, and see the home edge's heartbeats like any |
| -other bytes. A backhaul hop keeps its slot for the stream's life, by the |
| -existing rule (`backhaulAcquire`: a long-lived stream is sustained |
| -cross-region egress, which is what the cap bounds). That is unchanged. |
| +ordinary long request, unchanged, and see the home edge's heartbeats like |
| +any other bytes. The home edge answers within 2 seconds (§3.3 step 2), well |
| +inside the hop's 30-second header budget. A backhaul hop keeps its slot for |
| +the stream's life, by the existing rule (`backhaulAcquire`: a long-lived |
| +stream is sustained cross-region egress, which is what the cap bounds). |
| +That is unchanged. |
| |
| When a reload means this edge no longer manages the host (route removed, |
| moved, repointed to a tunnel or fleet box), its held streams for that host |
| are closed. Each browser reconnects and is routed afresh. |
| |
| -### 3.5 What the activator gives the edge |
| - |
| -The edge's `Activator` seam gains an optional interface (type-asserted, so a |
| -nil or older activator means no holding: the marker is stripped and the |
| -request proxied as today): |
| - |
| -- `BeginSidecarRequest(ctx, rt) error`: already exists (§3.2). |
| -- `Awake(host string) bool`: in-memory, under `a.mu`, true when the entry's |
| - state is `actAwake`. |
| -- `SetHostStateHook(func(host string, s HostState))`, with states `Awake` |
| - (the transition to `actAwake`) and `Stopping` (`beginStopLocked`). The hook |
| - is called without `a.mu` held, never blocks the activator (the edge side |
| - is a non-blocking channel send per host), and may drop a signal when its |
| - buffer is full; correctness never depends on it (§3.7's tick). |
| - |
| -On `Awake` the edge reopens every held stream on that host. On `Stopping` |
| -the edge closes the upstream legs of that host's held streams first. The |
| -instance's own SIGTERM drain then has no open stream handlers to wait for, |
| -so a stop is not held to `actStopTimeout` by streams the edge was going to |
| -lose anyway. |
| - |
| -Generation swaps (zero-downtime overlap): an awake instance can move to a new |
| -generation without a stop. On a reload where a host's upstream changes, the |
| -edge closes that host's open upstream legs and reopens them against the |
| -current upstream, non-waking, with `Last-Event-ID`. The old generation's |
| -drain therefore never waits on a held stream. |
| +### 3.5 Admissible |
| + |
| +A host is **admissible** when `HeldAdmissible(host)` is true: an entry |
| +exists, is `actAwake`, its child is alive, it is not fenced, not |
| +`probeHeld`, and the service is not shutting down. G4 and the simulation in |
| +§7 are stated against admissibility, not against `actAwake` **(r1 P2-5)**: |
| +a recovery probe that is awake but held is not admissible, and a held stream |
| +waits through it. |
| |
| ### 3.6 Bounds |
| |
| @@ -205,10 +302,19 @@ drain therefore never waits on a held stream. |
| that page; `retry:` makes it come back in 30 seconds instead. The request |
| never reaches the activator, so it neither wakes nor counts (post 835's |
| amendment to item 7: no proxied fallback, ever). |
| -- **Rapid ends while awake:** an upstream that ends within 2 seconds of |
| - opening, having relayed no event, counts as a quick end. Reopens after |
| - quick ends back off 1s, 2s, 4s, capped at 30s; five quick ends in a row |
| - close the browser's stream. Any relayed event resets the count. |
| +- **Reopen rule (r1 P2-4).** A stream never opens two legs less than 1 |
| + second apart, whatever they relayed. A leg that ends less than 30 seconds |
| + after it opened is a **short leg**, with or without events; after a short |
| + leg the next reopen waits 1s, 2s, 4s … capped at 30s, and five short legs |
| + in a row close the browser's stream. A leg that lasted 30 seconds or more |
| + resets the count. A reopen because the host became admissible after a |
| + sleep, or because a swap cancelled the leg, is not a short leg. Per host, |
| + reopens are therefore bounded by the held-stream cap at one per second |
| + each, and in practice by the backoff. |
| +- The browser's original request passes the edge's admission once, in the |
| + usual way: the plan throttle and the rate limit's `rateAdmit`, exactly |
| + once, in the hold branch (§3.8). Legs are edge-internal and are bounded by |
| + the reopen rule, not by those. |
| |
| The constants are the builder's to tune; the shapes are not. |
| |
| @@ -216,26 +322,45 @@ The constants are the builder's to tune; the shapes are not. |
| |
| Every held stream has one timer. When 25 seconds pass with nothing written |
| downstream, the edge writes the comment line `: ping` and a blank line, |
| -between events, and flushes. This runs whether or not an upstream leg is |
| -open, so an app that is silent while awake is covered too. |
| - |
| -The same tick is the backstop for G4: on each tick a held stream with no |
| -upstream leg calls `Awake(host)`, and reopens if true. A lost or dropped wake |
| -signal therefore costs at most one tick (25s) of quiet, never a silent |
| -stream for the rest of the night. |
| - |
| -### 3.8 Where it sits in the edge |
| - |
| -The held-stream branch runs after everything that judges the browser's |
| -request (host lookup, the password gate, the rate limit's admission, |
| -analytics and usage metering) and before the asset cache and the activator |
| -call, on the hibernating path only. It is handed a writer that flushes. The |
| -rate limit's slot comes back when the edge writes the response headers |
| -(`slotWriter`'s existing rule). Compression already skips |
| -`text/event-stream` (`compress.go`). The cache never sees the request. |
| +between events, and flushes. This runs whether or not a leg is open, so an |
| +app that is silent while awake is covered too. |
| + |
| +The same tick is the backstop for G4: on each tick a stream with no open |
| +leg and no pending reopen delay calls `HeldAdmissible(host)`, and starts a |
| +reopen if true. A lost or dropped wake signal therefore costs at most one |
| +tick (25s) of quiet. The wake hook only makes a resume faster; correctness |
| +never depends on it. Cancellation never depends on a signal at all (§3.2). |
| + |
| +### 3.8 Where it sits in the edge (r1 P2-1) |
| + |
| +In `Edge.ServeHTTP` on the holding edge, the order is: |
| + |
| +1. host lookup and the existing early refusals, unchanged (login POST |
| + handling, door scope, import gate, plan-disabled), then the observing |
| + writer (usage meter and analytics), the edge guard, and the password |
| + gate, all as today; |
| +2. **classification and the hold branch (§3.1)**, here: after the password |
| + gate, before the hop asset-fill answer, the upgrade branch, the local |
| + asset catch, compression, the plan throttle, the shared cache, the rate |
| + limit and the activator call. The hold branch runs `planThrottleAdmit` |
| + and `rateAdmit` itself, once each; the rate limit's slot comes back when |
| + the edge writes the response headers (`slotWriter`'s existing rule); |
| +3. everything else, unchanged, for unmarked requests. |
| + |
| +On a forwarding edge, the far asset answer and the backhaul run before |
| +lookup, as today. A marked request is not an asset request: the asset catch |
| +(local, far and the hop fill) passes any request carrying the marker |
| +through untouched, so it reaches the backhaul or the home edge's |
| +classification. |
| + |
| +The hold branch is handed the observing writer directly. It must support |
| +`Flush` and `http.ResponseController` write deadlines (`Unwrap`), which the |
| +plan verifies for every wrapper in that chain. No compression wrapper is |
| +involved: the hold branch sits before it, and it writes identity bytes. |
| |
| Usage metering counts the downstream bytes as it counts any response's; |
| -heartbeats are a few bytes a minute. |
| +heartbeats are a few bytes a minute. Analytics records one request per |
| +browser connection. |
| |
| ### 3.9 Counters |
| |
| @@ -243,14 +368,15 @@ A resume is not a wake reason: it never causes a wake, so counting it as one |
| would be wrong. The edge keeps its own counters, published in the box status |
| snapshot beside the edge's other counters: streams held now (per host and |
| total), resumes, refusals over a cap, closes by reason (upstream refused, |
| -maximum hold, quick ends, host no longer managed). |
| +redirect refused, maximum hold, short legs, host no longer managed, write |
| +deadline). |
| |
| ## 4. Constraints |
| |
| - The marker parameter name `carlos_hold` and value `1` are wire contract |
| once Rastrillo ships against them; changes are additive only. |
| - The hold path makes no bucket read, no registry read, and no S3 request. |
| - `Awake` and admission are in-memory. |
| + Admission, admissibility and cancellation are in-memory. |
| - Linux-only facilities are not needed; the code must cross-compile |
| (darwin, windows) like the rest of `internal/edge`. |
| - Witnesses go through production wiring: the edge with a real activator and |
| @@ -263,44 +389,61 @@ maximum hold, quick ends, host no longer managed). |
| activity. |
| 2. **Awake, browser connects:** app's response passed through; the leg is |
| activity-invisible, so the instance still sleeps after its idle window. |
| -3. **Instance sleeps under an open stream:** `Stopping` closes the leg; the |
| - stream is held; heartbeats continue. |
| -4. **Another request wakes it:** `Awake` reopens every held stream on the |
| - host with `Last-Event-ID`; the browser sees the missed events and carries |
| - on. |
| -5. **App ends the stream while awake:** immediate reopen, non-waking. |
| +3. **Instance sleeps under an open stream:** `beginStopLocked` cancels the |
| + leg before SIGTERM; the stream is held; heartbeats continue. |
| +4. **Another request wakes it:** the wake hook (or the next tick) reopens |
| + every held stream on the host with `Last-Event-ID`; the browser sees the |
| + missed events and carries on. |
| +5. **App ends the stream while awake:** reopen under the reopen rule, |
| + non-waking. |
| 6. **Edge restart or platform roll:** held streams close; browsers reconnect; |
| each is held (asleep) or relayed (awake). No wake from the reconnects. |
| -7. **Deploy (convergence cycle):** a sleep then a wake; cases 3 and 4. A |
| - zero-downtime overlap swap: §3.5's reload rule. |
| -8. **Cross-origin `EventSource`:** while awake the app's own CORS headers |
| - pass through on the first leg; while asleep the edge's own 200 carries |
| - none and the browser refuses it. Held streams are same-origin only, and |
| - the docs say so. |
| -9. **Wake race:** the instance wakes between a stream's state check and its |
| - registration. Register-then-check (§3.3 step 1) plus the tick (§3.7) |
| - cover it. |
| -10. **Region HA failover or a move:** the host leaves this edge's managed |
| +7. **Deploy:** a convergence cycle is a sleep then a wake (cases 3 and 4). A |
| + zero-downtime overlap swap cancels the retiring generation's legs in the |
| + switch's critical section; they reopen on the successor at once. |
| +8. **Cross-origin `EventSource`:** while admissible the app's own CORS |
| + headers pass through on the first leg; while asleep the edge's own 200 |
| + carries none and the browser refuses it. Held streams are same-origin |
| + only, and the docs say so. |
| +9. **Wake race:** register-then-admit (§3.3 step 1) plus the tick (§3.7). |
| +10. **Stop race:** a leg admitted just before a stop is registered and |
| + cancelled by it; one asked for just after is refused (§3.2). |
| +11. **Region HA failover or a move:** the host leaves this edge's managed |
| set at reload; its held streams close and reconnect to the new owner. |
| +12. **Recovery probe:** awake but held, so not admissible; streams stay held |
| + until the probe releases (wake hook) or the next tick. |
| +13. **Session expired while asleep:** held; on wake the reopen gets 401 (or |
| + a redirect) and the stream closes; the reconnect, now admissible, gets |
| + the 401 (or the 204) and the page stops. |
| |
| ## 6. Left to the builder |
| |
| -- The exact shape of the held set, the hook's channel and its buffer size. |
| +- The exact shape of the held set, the stream task and its channels, and |
| + the wake hook's buffer. |
| - Where the counters attach in the status snapshot. |
| -- Tuning the constants in §3.6 and §3.7. |
| -- Whether `BeginSidecarRequest` is reused under its own name or gains a |
| - neutral alias for this caller (behaviour must stay identical). |
| -- Docs: a short page section on holding streams for app authors (marker, |
| - same-origin, `Last-Event-ID`, the 30s over-cap retry), through the copy |
| - review. |
| +- Tuning the constants in §3.3 and §3.6, and the heartbeat interval. |
| +- Docs: a short section for app authors (the marker, same-origin only, |
| + `Last-Event-ID`, no redirects, later-leg cookies lost, the 30-second |
| + over-cap retry), through the copy review. |
| |
| ## 7. Testing |
| |
| -- A randomized simulation of one host's states (awake, stopping, asleep, |
| - waking, swap) against N held streams with lost and delayed hook signals, |
| - checking two invariants: no held stream ever contributes to `inflight` or |
| - `lastActive`, and no stream stays upstream-less for more than one tick |
| - while the host is awake. |
| -- Production-wired witnesses for cases 1–7 against a real child app that |
| - serves SSE with ids and honours `Last-Event-ID`. |
| -- The marker-strip test pins byte-for-byte query preservation. |
| +- A randomized simulation of one host's lifecycle (waking, awake, stopping, |
| + asleep, failed wake, overlap swap, fence, probe held and released, |
| + shutdown) against N held streams, with wake signals dropped and delayed |
| + at random, checking three invariants: no held stream ever contributes to |
| + `inflight` or `lastActive` or starts a wake; no process or generation is |
| + signalled to stop while a leg registered on it is uncancelled; and no |
| + stream goes more than one tick without a leg while its host is |
| + admissible, outside a reopen delay. |
| +- The guard test for process-stop call sites (§3.2). |
| +- Production-wired witnesses for cases 1–7, 10 and 13 against a real child |
| + app that serves SSE with ids and honours `Last-Event-ID`, including a |
| + witness that the app's SIGTERM drain finishes without waiting on a held |
| + stream. |
| +- Witnesses that a leg carries the edge's forwarded headers and signing and |
| + not the client's forged ones, and that a first-leg `Set-Cookie` is scoped |
| + as a proxied response's is. |
| +- The marker-strip test pins byte-for-byte query preservation; a test pins |
| + that a marked request to a hibernating route never reaches |
| + `act.ServeHTTP`, whatever its method or headers. |