carlos / platform Public

edge-held-streams: design rev 3, Astra round 2 P2 fixes (#432)

Summary

Round 2 was READY AFTER FIXES (no P1). Rev 3 fixes its five P2s without another round: classification before login handling, admissibility includes lease and startup recovery, no shared caching of marked requests on any edge, legs strip X-Carlos-Sidecar, heartbeats are bare comment lines and the held response seeds the cursor.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev e7dbd5657f1d5316fac678298dbb5556b39c2e1d parent 538fdfe
2 files changed, +184 −27
  • docs/superpowers/specs/2026-10-28-edge-held-streams-design.md +75 −27
  • docs/superpowers/specs/CODEX-FINDINGS-edge-held-streams-R2.md +109 −0

CI passed — run details

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
index e72a2fbb7..f6efd7e7c 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,11 +1,14 @@
# Edge-held event streams — design
Discussion: carlos/platform #432 (feature request from Rastrillo, with the
-amendment in post 835). Status: draft, rev 2, 2026-10-28.
+amendment in post 835). Status: draft, rev 3, 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.
+**(r1 P1-n / P2-n)** where they land. None declined. Round 2 (Astra,
+`CODEX-FINDINGS-edge-held-streams-R2.md`): READY AFTER FIXES, no P1, five
+P2 (P2-1 and P2-5 carried, P2-7 to P2-9 new). All five are fixed in rev 3,
+marked **(r2 P2-n)**, without another round (AGENTS.md stopping rule 1).
## 1. Intent
@@ -77,7 +80,9 @@ string: `new EventSource("/live?carlos_hold=1")`. Paul accepted this over a
list of stream paths in the app's config.
**Classification** happens once, on the edge whose activator manages the
-host, immediately after the upstream lookup (§3.8). A request is **marked**
+host, immediately after the upstream lookup and before anything else can
+answer the request, the login POST handling and the password gate included
+(§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:
@@ -86,7 +91,8 @@ and whose row has `Hibernate` set, a marked request is one of:
(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)**.
+ answered `400` by the edge at the classification point, before login
+ handling or the gate can answer or redirect it **(r1 P2-1, r2 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.**
@@ -132,7 +138,11 @@ is `hibernating`. There is no "holding unsupported" fallback to proxy through
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)**.
+ written once and used by both **(r1 P2-5, r2 P2-5)**: the predicate
+ function is what `AdmitHeldLeg` evaluates, and its stop-initiating side
+ effects (a lost lease or a dead child starts a stop, as
+ `BeginSidecarRequest` does today) stay in `AdmitHeldLeg`, outside the
+ predicate.
- `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
@@ -193,8 +203,9 @@ cancellation or the maximum hold (§3.6) **(r1 P2-3)**.
- `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;
+ `Transfer-Encoding` and hop-by-hop headers **(r1 P2-2)**, with
+ `Cache-Control: no-store` in place of the app's **(r2 P2-7)**, 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
@@ -208,8 +219,11 @@ cancellation or the maximum hold (§3.6) **(r1 P2-3)**.
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
+ `Cache-Control: no-store`, and flushes. These are the edge's own headers;
+ none of the app's are sent. If the edge holds a cursor (the browser's
+ `Last-Event-ID`), it then writes the ID-only block `id: <cursor>` and a
+ blank line, so the browser's parser holds the same cursor as the edge
+ **(r2 P2-9)**. 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
@@ -228,7 +242,9 @@ cancellation or the maximum hold (§3.6) **(r1 P2-3)**.
**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
+headers and minus `X-Carlos-Sidecar` (the edge-only marker the activator
+path strips today, `activator.go`'s `serve`) **(r2 P2-8)**,
+`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
@@ -282,9 +298,13 @@ are closed. Each browser reconnects and is routed afresh.
### 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
+A host is **admissible** when `HeldAdmissible(host)` is true: startup
+recovery has finished (`awaitStartup` would return at once without error),
+an entry exists, is `actAwake`, its child is alive, its lease replica
+reports no error (`e.rep.Err() == nil`), it is not fenced, not
+`probeHeld`, and the service is not shutting down **(r2 P2-5)**. A test
+pins that `HeldAdmissible` is never true in a state where `AdmitHeldLeg`
+would wait or refuse. 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.
@@ -321,9 +341,22 @@ 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 a leg is open, so an
-app that is silent while awake is covered too.
+downstream, the edge writes the comment line `: ping`, between events, and
+flushes. This runs whether or not a leg is open, so an app that is silent
+while awake is covered too.
+
+**No blank line after a heartbeat (r2 P2-9).** In the SSE parser a blank
+line dispatches, and dispatch copies the parser's last-event-ID buffer to
+the `EventSource`'s cursor even when the block had no data. A browser's
+buffer starts empty on every new connection, so a comment-and-blank-line
+heartbeat on a fresh connection would erase the cursor the browser
+reconnected with. A bare comment line dispatches nothing. For the same
+reason the edge drops comment-only blocks that arrive from a leg (the edge
+does its own heartbeats) and relays every other block whole, so the
+browser's cursor only ever changes with a block that carries the app's own
+`id:` field, or the edge's seed in §3.3 step 4. A witness covers two
+reconnects with a sleep and heartbeats between them, and checks the second
+reconnect still sends the right `Last-Event-ID`.
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
@@ -335,23 +368,34 @@ never depends on it. Cancellation never depends on a signal at all (§3.2).
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`
+1. host lookup, as today;
+2. **classification (§3.1)**, immediately after it: a marked request that
+ is not a held-stream request is answered `400` here, before the login
+ POST handling (whose successful answer is a redirect to an unmarked
+ `next`) or anything else can answer it **(r2 P2-1)**. A held-stream
+ request is flagged and continues;
+3. the existing order, unchanged: login POST handling, door scope, import
+ gate, plan-disabled, the observing writer (usage meter and analytics),
+ the edge guard, the hop asset-fill answer (which passes a flagged
+ request through), and the password gate;
+4. **the hold branch**, immediately after the password gate and before the
+ upgrade branch, the local asset catch, compression, the plan throttle,
+ the shared cache, the rate limit and the activator call. A flagged
+ request never goes past it. 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.
+5. 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.
+classification. **Every** edge, forwarding ones included, skips the shared
+cache, lookup and storage both, for any request carrying the marker, on
+any route **(r2 P2-7)**: a tunnel hub or a hop that recorded a held stream
+when it ended would replay a finished stream to the next browser instead of
+connecting it to the holding edge. Held responses also carry
+`Cache-Control: no-store` (§3.3).
The hold branch is handed the observing writer directly. It must support
`Flush` and `http.ResponseController` write deadlines (`Unwrap`), which the
@@ -442,8 +486,12 @@ deadline).
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
+ not the client's forged ones, never carries `X-Carlos-Sidecar`, and that a first-leg `Set-Cookie` is scoped
as a proxied response's is.
+- A tunnel-hub witness: a marked stream whose app sends a cacheable
+ `Cache-Control` is never stored by the forwarding edge's shared cache.
+- A marked login POST on a gated route gets `400`, never the gate's
+ redirect.
- 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.
diff --git a/docs/superpowers/specs/CODEX-FINDINGS-edge-held-streams-R2.md b/docs/superpowers/specs/CODEX-FINDINGS-edge-held-streams-R2.md
new file mode 100644
index 000000000..7b50d6a26
--- /dev/null
+++ b/docs/superpowers/specs/CODEX-FINDINGS-edge-held-streams-R2.md
@@ -0,0 +1,109 @@
+# Edge-held event streams — round 2 findings
+
+Commit reviewed: `538fdfe33cac6291e3d377b9087d6f1b33bdb92b` (verified `HEAD`).
+Round: **2 of 3**.
+
+Read-only review of the design and its code authorities. No files changed, subagents spawned, or test suites run.
+
+## Round 1 disposition
+
+- **P1-1 — Resolved:** cancellation moves into the generation-switch critical section; it no longer depends on reload.
+- **P1-2 — Resolved:** admission, registration and cancellation share `a.mu`; stop cancellation no longer depends on notifications.
+- **P1-3 — Resolved:** upstream redirects become terminal 204 responses.
+- **P1-4 — Resolved:** holding support is required; the waking fallback is removed.
+- **P2-1 — Partly resolved:** the dedicated branch avoids ordinary dispatch, but its stated position conflicts with existing gate ordering.
+- **P2-2 — Resolved:** identity encoding and removal of upstream framing headers are specified.
+- **P2-3 — Resolved:** independent leg I/O, initial-response and header bounds, and downstream write deadlines are specified.
+- **P2-4 — Resolved:** reopen spacing and short-leg backoff apply regardless of emitted events.
+- **P2-5 — Partly resolved:** probes and fences are covered, but the explicit admissibility predicate still omits existing admission conditions.
+- **P2-6 — Resolved:** reusing `up.proxy` preserves the cited forwarded-header, signing, purge and cookie transformations. A separate activator-side transformation remains missing, identified below.
+
+## P1 findings
+
+None.
+
+## P2 findings
+
+### P2-1 — The classification position still conflicts with existing gate handling
+
+**Sections:** §§3.1 and 3.8.
+
+**Problem and evidence:** The design places classification after the password gate but before the hop asset-fill handler, while saying existing ordering stays unchanged: [design:338](/home/paulca/amadan.net/carlos/platform/.claude/worktrees/edge-held-streams-432/docs/superpowers/specs/2026-10-28-edge-held-streams-design.md:338). The actual order is hop asset fill at [edge.go:1903](/home/paulca/amadan.net/carlos/platform/.claude/worktrees/edge-held-streams-432/internal/edge/edge.go:1903), then password admission at `edge.go:1926`.
+
+There is also an executable exception to §3.1’s marked-invalid-request rule. `Gate.Admit` consumes login POSTs before returning: [gate.go:110](/home/paulca/amadan.net/carlos/platform/.claude/worktrees/edge-held-streams-432/internal/gate/gate.go:110). Successful login redirects to the submitted `next` path at `gate.go:171`.
+
+**Failure scenario:** A marked login POST with valid credentials returns 303 instead of the specified 400. Following an unmarked `next` URL reaches ordinary activity admission. A planner following the stated insertion position must also guess whether to reorder the gate and asset-fill handler.
+
+**Concrete fix:** Specify early marker classification and invalid-request refusal before login handling can answer or redirect. Keep eligible stream admission behind the security gates. State the actual order explicitly, retaining the prescribed marker bypass in hop asset fill.
+
+**Fault family:** Every dispatch and response-driven follow-up must obey the activity-exclusion decision. Carries forward round 1 P2-1.
+
+### P2-5 — The explicit admissibility predicate is not equivalent to admission
+
+**Sections:** §§3.2 and 3.5.
+
+**Problem and evidence:** §3.2 requires the decisions of `BeginSidecarRequest`, but §3.5’s exhaustive predicate checks state, child liveness, fences, probe holds and shutdown only: [design:285](/home/paulca/amadan.net/carlos/platform/.claude/worktrees/edge-held-streams-432/docs/superpowers/specs/2026-10-28-edge-held-streams-design.md:285).
+
+Existing admission also detects a lost lease through `e.rep.Err()` and starts a stop instead of admitting: [activator.go:1838](/home/paulca/amadan.net/carlos/platform/.claude/worktrees/edge-held-streams-432/internal/activator/activator.go:1838). It first waits for startup recovery at `activator.go:1812`; that wait can fail or remain pending.
+
+**Failure scenario:** An awake child with a failed replica satisfies §3.5 but fails the required admission operation. Implementing the explicit predicate literally either admits traffic after lease loss or makes the tick report admissibility while admission refuses it.
+
+**Concrete fix:** Include lease validity and completed startup recovery in the shared predicate. Keep stop initiation as a separate admission-side action when lease loss or child death is detected. Test that `HeldAdmissible` cannot report true when immediate admission would wait or refuse.
+
+**Fault family:** Liveness checks must use the same availability conditions as admission. Carries forward round 1 P2-5.
+
+### P2-7 — A forwarding tunnel edge can cache the held response
+
+**Sections:** §§3.3, 3.4 and 3.8.
+
+**Problem and evidence:** The holding edge bypasses its shared cache, but forwarding edges are instructed to proxy normally. Only asset-cache bypasses are specified: [design:350](/home/paulca/amadan.net/carlos/platform/.claude/worktrees/edge-held-streams-432/docs/superpowers/specs/2026-10-28-edge-held-streams-design.md:350).
+
+A tunnel route sets `viaTunnel` and uses ordinary dispatch: [edge.go:789](/home/paulca/amadan.net/carlos/platform/.claude/worktrees/edge-held-streams-432/internal/edge/edge.go:789). Its GET remains eligible for shared-cache lookup and recording at `edge.go:2028` and `edge.go:2078`. `maybeStore` accepts a small successful response with positive cache lifetime; it has no SSE exclusion: [edge.go:2140](/home/paulca/amadan.net/carlos/platform/.claude/worktrees/edge-held-streams-432/internal/edge/edge.go:2140). Flushing does not disable recording (`edge.go:2513`).
+
+**Failure scenario:** An app’s first SSE response includes `Cache-Control: public, max-age=60`. The holding edge preserves that header. When its downstream closes normally—after the maximum hold or repeated short legs—the tunnel hub can cache the captured stream. Subsequent marked requests receive a finite cached replay without reaching the holding edge, losing live updates until expiry.
+
+**Concrete fix:** Bypass shared-cache lookup and storage for marked forwarding requests as well as held requests. Give edge-owned held responses an explicit non-cacheable policy, including successful first-leg responses.
+
+**Fault family:** A forwarding edge must preserve the live connection to the holding edge.
+
+### P2-8 — Calling `up.proxy` skips removal of the sidecar marker
+
+**Sections:** §§3.3 and 3.3a.
+
+**Problem and evidence:** Legs copy original headers, excluding hop-by-hop headers, and call `up.proxy` directly: [design:229](/home/paulca/amadan.net/carlos/platform/.claude/worktrees/edge-held-streams-432/docs/superpowers/specs/2026-10-28-edge-held-streams-design.md:229).
+
+Removal of `X-Carlos-Sidecar` occurs in the bypassed activator handler: [activator.go:5854](/home/paulca/amadan.net/carlos/platform/.claude/worktrees/edge-held-streams-432/internal/activator/activator.go:5854). Neither the generation proxy’s rewrite at [genproxy.go:369](/home/paulca/amadan.net/carlos/platform/.claude/worktrees/edge-held-streams-432/internal/edge/genproxy.go:369) nor the Unix proxy’s rewrite at `edge.go:2708` performs that removal.
+
+**Failure scenario:** An outsider sends an otherwise eligible held request with `X-Carlos-Sidecar`. The app receives this edge-only signal on every leg, although the ordinary activator path strips it expressly to prevent downstream interpretation of a spoofable marker.
+
+**Concrete fix:** Explicitly remove the sidecar header when constructing every held leg. Add it to the production-wired request-header witness alongside forwarded-header and purge-header checks.
+
+**Fault family:** Bypassing activity accounting must retain existing client-to-app header boundaries.
+
+### P2-9 — Heartbeats can erase a reconnecting browser’s resume cursor
+
+**Sections:** §§3.3 and 3.7.
+
+**Problem and evidence:** The edge seeds its cursor from the browser’s `Last-Event-ID`, but its synthetic held response sends no corresponding ID field: [design:229](/home/paulca/amadan.net/carlos/platform/.claude/worktrees/edge-held-streams-432/docs/superpowers/specs/2026-10-28-edge-held-streams-design.md:229). Heartbeats are comment lines followed by a blank line: [design:323](/home/paulca/amadan.net/carlos/platform/.claude/worktrees/edge-held-streams-432/docs/superpowers/specs/2026-10-28-edge-held-streams-design.md:323).
+
+For each new response, the browser initializes its parser’s ID buffer to empty. Processing a blank line copies that buffer to the EventSource’s last-event-ID string **before** checking whether the block contains data. A comment-only heartbeat can therefore reset the cursor. [https://html.spec.whatwg.org/multipage/server-sent-events.html#event-stream-interpretation](https://html.spec.whatwg.org/multipage/server-sent-events.html#event-stream-interpretation)
+
+**Failure scenario:** A browser reconnects with `Last-Event-ID: 42` while the instance sleeps. The edge remembers `42`; its first heartbeat clears the browser’s cursor. If that downstream disconnects before the app supplies another ID, the next browser request omits `Last-Event-ID`, losing the resume position.
+
+**Concrete fix:** Initialize downstream parser state from the incoming cursor before emitting heartbeat-only blocks—for example, with a correctly framed ID-only block. Keep subsequent cursor updates synchronized with completed downstream blocks, including empty-ID resets. Add a witness covering two reconnects with sleep and heartbeats between them.
+
+**Fault family:** Edge-generated SSE framing must preserve the browser’s resume cursor.
+
+## P3 findings
+
+None.
+
+## Builder questions
+
+1. Where does early marked-invalid-request refusal sit relative to login POST routing and password admission? The current ordering promises incompatible behavior.
+2. Will marked requests bypass shared caching on every forwarding edge, with a non-cacheable downstream response policy?
+3. How will the initial downstream ID state be established before the first heartbeat?
+
+This round has no P1, so numbered review stops under the repository’s stopping rule. Apply the P2 fixes without reopening review, retaining the required checks for newly changed security behavior. The repeated P2-1 and P2-5 fault families should be addressed through their central classification and admission invariants, with guard tests.
+
+**READY AFTER FIXES**
\ No newline at end of file