edge-held-streams: design rev 1 and Astra review brief (#432)
Prompt
Can we take #432 if we don't have it yet? / Let's go / Recommendation seems good (the carlos_hold=1 URL marker)
From this commit's own message. The prompt ledger has no entries for it — which is what a squashed, rebased or amended commit looks like, and what every commit looks like in a repo without a ledger.
Summary
Design for holding a browser's SSE stream at the edge while its instance sleeps and resuming it on wake, activity-invisible and non-waking in every outcome.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
docs/superpowers/specs/2026-10-28-edge-held-streams-design.md+306 −0docs/superpowers/specs/CODEX-REVIEW-BRIEF-edge-held-streams.md+88 −0
CI failed — run details
Intent
Attributed to this commit: 90.0 g CO₂e (9,801,383 tokens). This is an attribution, not a measurement — usage counted from this commit's own marker forward to the next one on edge-held-streams-432, never backward from it. A turn that produced more than one commit books all of its tokens to the last one, and a concurrent session on this branch can add to the same figure. Some of it was priced using the shared default rate, because no model-specific factor row exists yet for at least one model involved. The Carbon view has the repo total, which is never derived from this attribution.
diff --git a/docs/superpowers/specs/2026-10-28-edge-held-streams-design.md b/docs/superpowers/specs/2026-10-28-edge-held-streams-design.md| new file mode 100644 |
| index 000000000..ccff3fa36 |
| --- /dev/null |
| +++ b/docs/superpowers/specs/2026-10-28-edge-held-streams-design.md |
| @@ -0,0 +1,306 @@ |
| +# 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. |
| + |
| +## 1. Intent |
| + |
| +A browser that leaves a page with a live stream open (`EventSource`) must not |
| +keep a hibernating instance awake. Today it does: the proxied stream is one |
| +request in flight for its whole life (`activator.begin` holds an in-flight |
| +count, and the idle sweep only sleeps an entry with `inflight == 0`), so an |
| +editor who leaves `/admin` open overnight keeps the instance awake and billed |
| +all night, ended only by the plan's awake ceiling. |
| + |
| +Paul's ruling (#432): the stream lets the instance sleep, and resumes when the |
| +instance wakes for any other reason. The edge does the holding, not the |
| +browser: it works for every CARLOS app with a stream, it resumes at once, and a |
| +sleeping site gets no retry traffic. |
| + |
| +The first user is Rastrillo's live fragments (Patrix's `/admin` dashboard). |
| +Rastrillo puts an `id:` on every event and honours `Last-Event-ID` on connect, |
| +so a resumed upstream sends only what was missed. |
| + |
| +## 2. Goals, non-goals, anti-goals |
| + |
| +Goals: |
| + |
| +- G1. An opted-in stream is **activity-invisible** in every outcome: held, |
| + relayed, refused or closed. It earns no in-flight count and no `lastActive` |
| + stamp. The instance sleeps after its normal idle window as if the stream |
| + were not there. |
| +- G2. An opted-in stream **never wakes** an instance. |
| +- 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 |
| + costs no bucket reads and no S3 requests. |
| + |
| +Non-goals: |
| + |
| +- Buffering or replaying events. The edge relays events as they arrive. |
| + Missed events are the app's to resend from `Last-Event-ID`. |
| +- 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. |
| +- 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. |
| +- Surviving an edge restart. A held stream lives in the edge process; a |
| + restart closes it and the browser's own reconnect is held again (§5.6). |
| + |
| +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. |
| +- 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. |
| + |
| +## 3. Decided |
| + |
| +### 3.1 Opt-in: a URL marker (Paul, 2026-10-28) |
| + |
| +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: |
| + |
| +- 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. |
| + |
| +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). |
| + |
| +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 |
| +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.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. |
| + |
| +### 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. |
| + |
| +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.6 Bounds |
| + |
| +- **Per host:** 64 held streams. **Per box:** 4,096. |
| +- **Maximum hold:** 1 hour per browser connection, then the edge closes it |
| + (at an event boundary). The browser reconnects and is held again; the |
| + bound is a recycle, not a cut-off, and it limits how stale a held |
| + stream's cookies can get. |
| +- **Over a cap:** the edge answers `200 text/event-stream` with the single |
| + event `retry: 30000` and closes. A native `EventSource` treats a 503 as |
| + fatal and never reconnects, so a 503 would silently end live updates for |
| + 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. |
| + |
| +The constants are the builder's to tune; the shapes are not. |
| + |
| +### 3.7 Heartbeats and the tick |
| + |
| +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. |
| + |
| +Usage metering counts the downstream bytes as it counts any response's; |
| +heartbeats are a few bytes a minute. |
| + |
| +### 3.9 Counters |
| + |
| +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). |
| + |
| +## 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. |
| +- 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 |
| + a real child process, not a fake activator (production-wired witnesses |
| + rule). |
| + |
| +## 5. Cases the design has to be right about |
| + |
| +1. **Asleep, browser connects:** 200 from the edge, heartbeats, no wake, no |
| + 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. |
| +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 |
| + set at reload; its held streams close and reconnect to the new owner. |
| + |
| +## 6. Left to the builder |
| + |
| +- The exact shape of the held set, the hook's channel and its buffer size. |
| +- 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. |
| + |
| +## 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. |
diff --git a/docs/superpowers/specs/CODEX-REVIEW-BRIEF-edge-held-streams.md b/docs/superpowers/specs/CODEX-REVIEW-BRIEF-edge-held-streams.md| new file mode 100644 |
| index 000000000..830c3c7f3 |
| --- /dev/null |
| +++ b/docs/superpowers/specs/CODEX-REVIEW-BRIEF-edge-held-streams.md |
| @@ -0,0 +1,88 @@ |
| +# Review brief: edge-held event streams design, round 1 of 3 |
| + |
| +Repo carlos/platform, branch `edge-held-streams-432`, commit: the tip of |
| +that branch when you start (`git rev-parse HEAD`; name it in your output). |
| +Under review: `docs/superpowers/specs/2026-10-28-edge-held-streams-design.md`. |
| +Authorities: the code on this branch (it is `main` plus this spec), chiefly |
| +`internal/activator/activator.go` (`serve`, `BeginSidecarRequest`, `begin`, |
| +`beginStopLocked`, the idle sweep), `internal/edge/edge.go` (`Reload`'s |
| +`hibernating` upstream flag, `ServeHTTP`, the backhaul hop), |
| +`internal/edge/ratelimit.go` (`slotWriter`), `internal/edge/compress.go`, |
| +and the zero-downtime deploy specs under `docs/superpowers/specs/`. |
| +The request is amadan discussion carlos/platform #432 and its post 835; the |
| +spec's §1-§2 restate it. Do not review against unmerged branches; if this |
| +work needs one, say so as a P1 and stop. |
| + |
| +You are an independent reviewer. Read the document, then the code it |
| +names, and check every claim against that code. Read it also as the |
| +engineer who must write the implementation plan from it alone: report the |
| +places where you would have to guess and a wrong guess would matter. |
| + |
| +## Fixed: do not re-argue |
| + |
| +- Paul's ruling (#432): the edge holds the stream; the browser does not |
| + retry on its own. Report only mechanisms that fail to honour it. |
| +- Paul's ruling (2026-10-28): the opt-in is the URL marker `carlos_hold=1` |
| + (spec §3.1), not a list of paths in app config. |
| +- Post 835: an opted-in stream is activity-invisible and non-waking in every |
| + outcome; no proxied fallback when a cap is reached. |
| + |
| +## What to check |
| + |
| +1. G1/G2: is there any path (first leg, reopen, over-cap, error, swap, |
| + reload, hop, tunnel) where an opted-in stream reaches `begin`, earns |
| + in-flight or `lastActive`, or causes a wake? |
| +2. G4: can a held downstream sit with no upstream while the host is awake |
| + for longer than one tick? Check the register-then-check order, the hook |
| + (`Awake`/`Stopping`) and where those transitions really happen in the |
| + activator, including waking-then-failed, generation swaps, fences, |
| + probes and shutdown. |
| +3. Stopping: does closing upstream legs on `Stopping` interact badly with |
| + the stop path, the SIGTERM drain, `actStopTimeout`, or the sidecar |
| + rules? Is `beginStopLocked` the only path to a stop that ends a |
| + process's streams? |
| +4. Zero-downtime overlap swaps: does §3.5's reload rule match how the edge |
| + actually picks a generation's upstream (`genManaged`, `e.gens`)? Would a |
| + drain wait on a held leg? |
| +5. Edge placement (§3.8): is the proposed position in `Edge.ServeHTTP` real |
| + (gate, rate limit, analytics, usage meter, cache, activator call), and |
| + does the writer it gets support Flush there? |
| +6. The hop and tunnel (§3.4): does a forwarding edge pass the marker and |
| + the stream through untouched, and do any of its timeouts cut a quiet |
| + stream before a 25 s heartbeat? |
| +7. SSE framing (§3.3): is per-event relay with `id:` tracking correct |
| + against the SSE spec, and does the over-cap `retry:` answer actually make |
| + a native `EventSource` reconnect? |
| +8. Anything an outsider can do with the marker that the spec does not |
| + bound. |
| + |
| +## Severity |
| + |
| +- **P1**: unsafe, would not compile, a test fails, or it would not work. |
| + Blocks merge. |
| +- **P2**: wrong or missing. Fixed without reopening this review. (The |
| + census, and review of any newly changed security, root, termination |
| + or production-storage behaviour, still check the fix; see AGENTS.md, |
| + "Stopping rules".) |
| +- **P3**: clarity or polish. Goes on a follow-up list; never a reason for |
| + another round. |
| + |
| +A P1 states its failure scenario and the evidence (file:line). Do not |
| +raise a P2 to P1 to keep the review open. |
| + |
| +## Output |
| + |
| +Findings ranked P1, P2, P3. Each gives the section, the problem, the |
| +evidence (file:line, verified), the failure scenario, a concrete fix and |
| +the invariant or trust boundary it breaks (that is its fault family). |
| +Then a short list headed "Builder questions": decisions the spec leaves |
| +open that a planner could not settle alone, most important first. End |
| +with exactly one verdict: |
| + |
| +- **READY**: no P1 and no P2. |
| +- **READY AFTER FIXES**: no P1. The listed P2s are fixed without another |
| + round. |
| +- **NOT READY**: one or more P1s, listed. |
| + |
| +Do not edit files. Do not spawn Anthropic-model reviewers or subagents of |
| +your own. Do not run the full test suites. |