carlos / platform Public

Hold a browser's SSE stream at the edge while an instance sleeps, and resume it on wake Feature Request open

On branch edge-held-streams-432

Claimed by claude~paul@keymail.dev 3 weeks ago

Request from Rastrillo. Let the edge hold a browser's Server-Sent Events stream while an instance sleeps, and resume it when the instance wakes, without the stream counting as activity or waking anything.

Why

Rastrillo is adding live pages (design, docs/superpowers/specs/2026-10-26-live-fragments-design.md). The first user is Patrix's /admin dashboard. An app calls Notify("comments") after a write. Every open page showing comments gets an SSE event carrying only a topic and a version, then fetches that region of the page again as ordinary HTML.

On CARLOS today, an open stream keeps a hibernating instance awake indefinitely. In-flight counting is what holds it (internal/activator/activator.go:523-527): the proxied request is in flight for the stream's whole life. So an editor who leaves /admin open in a tab overnight keeps Patrix awake and billed all night. Only the plans awake ceiling (:4790) would end it.

Paul's ruling: the stream should let the instance sleep. When the instance wakes, for any other reason, the stream should resume. He chose the edge-held shape below over the browser retrying on its own. The edge can do it for every CARLOS app with a stream, it resumes at once, and a sleeping site gets no retry traffic.

What exists

  • X-Carlos-Sidecar (activator.go:90) already has the activity semantics we want: no activity credit, 503 while asleep, never a wake. It is a request header for sidecars, though. A browser's EventSource cannot set headers, and we don't want to borrow a sidecar's marker from a browser.
  • The edge already treats text/event-stream specially by never compressing it (internal/edge/compress.go:125).
  • SSE is resumable by design. Each event can carry an id:, and a reconnecting client sends Last-Event-ID. Rastrillo will set an id: on every event and honour Last-Event-ID on connect, so a resumed upstream sends only what was missed.

The ask

The edge holds a browser's SSE stream on a route that opts in:

  1. Which streams. These must be identifiable from the request, before the activator decides whether to wake. A route-level list of stream paths (manifest or route config) is the obvious shape, since EventSource can't add headers. A query marker would also work. The platform chooses; Rastrillo will put its stream wherever you say.
  2. Not activity. While the instance is awake, the upstream leg of a held stream earns no in-flight count and no lastActive stamp, like sidecar traffic. The instance sleeps after its normal idle window as if the stream weren't there.
  3. Never a wake. A held stream opened while the instance is asleep does not wake it. The edge accepts the browser's connection, sends the 200 and SSE headers itself, and holds it.
  4. Held while asleep. When the instance sleeps (idle, convergence cycle, or awake ceiling), its upstream ends. The edge keeps the browser's connection and sends its own comment heartbeats (: ping) at the interval it would anyway, so intermediaries don't time it out.
  5. Resume on wake. When any other request wakes the instance, the edge reopens the upstream for each held stream on that host. It uses the original request's method, path, query and cookies, plus Last-Event-ID set to the last id: it passed through. It then relays events again. The browser sees one unbroken stream.
  6. Upstream refuses. If the reopened upstream answers anything but 200 text/event-stream (session expired: 401/403/303; route gone: 404), the edge closes the browser's stream. The browser's own reconnect then hits the app directly, with normal semantics.
  7. Bounds. A per-host cap on held streams and a maximum hold time, after which the edge closes the stream. A cap that is reached means "don't hold this one": serve it as a plain proxied stream (today's behaviour) or refuse it, your choice. The edge never buffers events. It relays them as they arrive.
  8. Deploys. A convergence cycle is a sleep followed by a wake, so held streams ride across a deploy the same way.

What Rastrillo does until this ships

Live streams stay off on hibernating routes. Pages are static there, as they are today, and live updates work only on always-on routes. Once this lands, Rastrillo turns them on wherever the route opts in. Nothing changes in the app or the browser code, because it is plain SSE either way.

Questions for the platform

  • Where does the opt-in live: manifest, route config, or a request marker?
  • Is per-host hold state at the edge acceptable at the scale you expect? That would be tens of streams per host, idle most of the time.
  • Should a held stream's resume count as a wake reason in metrics, or stay invisible?

Two amendments to the ask, from the third round of cross-provider review on the Rastrillo design (discussion 129). Both close a way the stream could still keep an instance awake or go quiet:

  • Item 7, the cap: no activity-counted fallback. Once the per-host cap is reached, the edge must not fall back to a plain proxied stream. That stream would be an ordinary in-flight request and would keep the instance awake, which is the problem this request exists to solve. Over the cap, the edge refuses the stream (e.g. 503) without waking the instance and without activity credit. The browser backs off and retries. Every opted-in stream path is activity-invisible and non-waking in every outcome: held, refused or relayed.
  • New item, upstream ends while the instance is awake. The app bounds each stream's lifetime, so an upstream can end while the instance is still awake (busy with other traffic). The edge then reopens it at once with Last-Event-ID. That reopening is non-waking and activity-invisible, and it re-checks auth (item 6). If the reopen fails, the edge closes the browser's stream so the browser's own recovery runs. The edge must never hold a downstream that has no upstream while the instance is awake, because the page would go quiet with nothing to say so.

Item 6 also needs a correction: when the edge closes a stream, the browser's reconnect still goes to an opted-in path, so it gets the same non-waking treatment, not "normal semantics".

For the record: Last-Event-ID at the edge shows that events were delivered, not that the page applied them. Rastrillo keeps track of fetches still owed in the browser, so the edge doesn't need to know about them.

Paul approved the design (rev 3, e7dbd565) and the implementation plan (rev 2, ae6eb493) on 2026-11-02, in the Rastrillo live-pages session. He said "Go ahead" after this summary: the carlos_hold=1 URL opt-in; never counted and never waking, in every outcome; the edge answers and heartbeats while the instance sleeps and reopens with Last-Event-ID on wake; stops and swaps cancel legs first; over the caps, retry: 30000 and no proxied fallback. Accepted trade-offs: same-origin only, a redirect on a held stream becomes a stop (apps answer 401), later-leg cookies lost, an hourly reconnect, edge restarts close held streams.

Build notes for whoever picks this up:

  • main has moved since the plan was reviewed. Check the plan against current main before Task 1, especially internal/activator and internal/edge, which stateless replicas plan 1 (#511) touched. If the plan's interfaces no longer hold, that is a plan revision, still inside the review budget per AGENTS.md stopping rule 1.
  • Switch-on (rule 5): it is live as soon as an app sends the marker. The first user is Rastrillo's live.js; Patrix's /admin follows.
  • Rastrillo's follow-up once this ships: live.js adds carlos_hold=1 to its stream URL, and the stream answers a signed-out viewer with 401 instead of a sign-in redirect.

Landed on main as 412203c (squash of branch edge-held-streams-432; CI green on tip 0fa5468c). Not released or rolled yet: it reaches boxes with the next release through the normal process.

What is in it: the activator side (held legs admitted under a.mu, never counted, never waking, cancelled before every stop, swap and removal), the edge side (classification after lookup, hold branch after the gate, one stream task per browser, heartbeats, wake-hook and tick reopens, caps, no shared caching of marked requests), and held_streams counters in the box status. Reviews: Astra on Tasks 1, 2, 5, 6 and the whole branch; census approved. Follow-ups: docs/superpowers/notes/edge-held-streams-followups.md (region-HA cold holder cache must be solved before region HA ships; close idle edge-to-child connections at stop).

Still to come: the app-author docs page (plan Task 9) is in Paul's copy review and lands as its own branch.

What Rastrillo must do next (separate branch in rastrillo/rastrillo, after this is on the boxes):

  1. live.js adds carlos_hold=1 to its stream URL, e.g. new EventSource("/live?carlos_hold=1") (keep any existing query; the edge strips only the marker).
  2. The stream endpoint answers a signed-out viewer with 401 (or 403), never a redirect to sign-in. A held stream that gets a 3xx is answered 204 by the edge and the browser stops listening; a 401/403 is passed through so the page can react.
  3. Keep an id: on every event and resume after Last-Event-ID on connect (already true per the design). Posted in rastrillo as well.

Follow-up merged as 5394c7f: the first heartbeat on a held stream now goes out at +25 s, not +50 s (seen on dev after v0.33.7; Astra READY, census approved). Ships with the next release.

CI flake seen 2026-11-03 on branch litestream-oom-284 @ 5ec4cc6c (which doesn't touch internal/edge): TestHeldDialWebSocketUpgrade (localhold_test.go:340) got HTTP/1.1 200 OK ... {"isValid":true,"name":""} instead of 101. That body isn't ours. It looks like the dial reached another listener on the runner (a port reused between httptest teardown and the raw net.Dial?). Passes 3/3 locally. Worth a look if it recurs.