# Cloudflare caching of the bead, apps and frames (R2) for the Oct 31 demo

2026-10-25, Claude Code chat `ef1a66e3`. Stephen: *"as we make this boston landing demo, let consider cloudflare cacheing of the bead, the apps and the data frames. maybe using r2?"*

## Why

Everything in this bead is served from Stephen's laptop through the Cloudflare tunnel (`x-co-host: tunnel`). For a demo to Google that is the single point of failure (laptop asleep, home uplink, 8K videos streamed over a residential line). R2 makes the bead durable at the edge, independent of the laptop, with native byte ranges for video.

## Measured (2026-10-25)

- R2: **not enabled** on account `46fba016…` (`10042 Please enable R2 through the Cloudflare Dashboard`) — the same blocker as 2026-10-19 (hub v0.3.0 used KV, 25 MB/value, instead). Enabling is a dashboard step with billing (Stephen); free tier: 10 GB-month storage, 1M class-A and 10M class-B ops/month, **zero egress fees**.
- Bead size 3.5 GB: media 1.5 GB (8K originals), webcams/youtube 1.2 GB, calibration-8k/frames 718 MB (1 fps 8K stills), proxy-960 66 MB, frame-service cache 52 MB, apps/poses/adsb/rop ~70 MB. Fits the free tier.

## Design: four tiers, read in order

1. **Browser** (and its OPFS/service worker as a co-host).
2. **Cloudflare edge cache** (per data centre): immutable objects cached for a year, mutable files short TTL.
3. **R2 bucket** (durable, one global copy, Range-capable): the published bead.
4. **Origin through the tunnel** (the laptop, and later browser co-hosts): authoring, the frame service's cold renders, private paths.

- **What goes to R2:** everything public in the bead, versioned or content-addressed where possible: apps (takeoff-player, takeoff-3d, the ROP viewer), proxies, 8K stills, poses, ADS-B, webcam clips, ROP time-tiles and meshes, and the 8K originals (so they are seekable without the laptop). Private paths (sign-in gated) stay origin-only, as the hub already enforces for `/private/`.
- **Frame service write-through:** each rendered window (`frame/?clip&f&rect&out`, immutable by key) is also PUT to R2 by the handler, so the second request from anywhere never reaches the laptop. Before the demo, **pre-warm**: render the windows the demo uses (fixed views, ROP AOI crops) so the whole demo runs from R2/edge with the laptop off.
- **Worker (hub, stephenguerin.live bead `01a14247`):** add the bead prefix to `COHOSTED_PREFIXES` with an R2 step: edge cache → R2 (pass `Range` to `R2Bucket.get(key, { range })`, answer 206) → tunnel origin. Cache-Control: `immutable, max-age=31536000` for versioned/content-addressed keys; `max-age=60, stale-while-revalidate` for notes/STATE/index.html. This also removes the Range relay workaround for these paths (`x-sg-range`, v0.4.2) since R2 serves ranges itself.
- **Mirror tool:** extend `repo/cohost-hub/tools/mirror-to-edge.mjs` from KV to R2 (S3-compatible API with an R2 token, multipart for files over 100 MB; sha256 in object metadata; skip unchanged).
- **Browser co-host job results** (§7 of the ROP note): a co-host that renders a frame or time-tile uploads it to R2 through a short-lived, Worker-signed PUT URL bound to that key (an MCP-style ephemeral capability), so the shared cache grows from every participant.

## To do

1. **Stephen:** enable R2 in the Cloudflare dashboard (R2 → Purchase/Enable; free tier).
2. Create bucket `stephenguerin-live-beads` (wrangler), bind it to the hub Worker as `BEADS`.
3. Mirror tool → R2; mirror BOS-Takeoff-Veillance (3.5 GB, ~20-40 min upload).
4. Worker v0.5.0: R2 tier with Range; deploy; verify a 690 MB 8K original seeks from R2 with the laptop's tunnel stopped.
5. Frame-service write-through + pre-warm list for the demo.
6. Point `pleaserender.me` at the ROP pages through the same Worker once its NS is on Cloudflare.

## Cloudflare as one co-host among many (Stephen, same session)

*"and cloudflare can be part of the co-hosting (multi-origin with my edge tunnel and other browsers)"*

R2 is not "the CDN in front of the laptop"; it is **one co-host in a multi-origin set**, beside Stephen's edge tunnel(s) and the participating browsers, per the [origin-ladder frame](https://redfish.acequia.io/guerin/.agents/01a14247-c5ef-7914-a3d2-d1eb81c13426/2026-10-15/notes/origin-ladder-design-frame_58511e5a.md) and AGENTS.md (browser hosts primary; everything else augments).

- **Roles, not hierarchy.** Each co-host declares what it holds (prefixes, or content hashes) and its role: *custody* (the person's own browser/device: authoritative, private data), *replica* (R2: durable, public data only, always on, global, Range-native), *reach/uptime* (the laptop tunnel: the frame service, authoring), *peer* (other browsers: OPFS replicas, render jobs). The hub already keeps a registry of browser co-hosts and their prefixes; R2 and the tunnel become entries in the same registry.
- **Routing.** For a read, the Worker picks among the holders by freshness, availability and cost: edge cache → the nearest live holder (R2 for public immutable objects; a registered browser co-host for its prefix; the tunnel) → the next. A write or a cold render goes to whoever can do it (the laptop's ffmpeg, a browser's WebCodecs) and is then replicated: write-through to R2, offered to peers.
- **Content addressing makes holders interchangeable.** Immutable objects are keyed by sha256 (with path aliases), so any holder's copy is verifiably the same bytes; this is what lets a browser, R2 and the laptop answer the same request.
- **Privacy follows the roles.** R2 (a third party's storage) is a replica for **public** paths only; private and custody data (Google Photos, the custodian's index) never go to R2 and travel only between trusted peers over WebRTC (custody note in the ROP bead).
- **Failure modes become graceful:** laptop off → R2 and browsers serve; R2 or Cloudflare unavailable → tunnel or browsers serve (with DNS/Worker as the remaining single point, the subject of the origin ladder's later rungs).
