# how it works Historical design notes. Summary storage moved from D1 to HeavyPad SQLite and R2 serving snapshots on 2026-10-11; see [the current architecture](snapshots.md). The footer format and witnessed-time caveat below still apply. ## the read every sealed `.jss` segment carries a footer whose layout is fixed by [jss-format-v1](https://tangled.org/zat.dev/stream/blob/main/docs/jss-format-v1.md). The 256-byte header at offset 0 holds `collection_index_offset`; the collection block index at that offset (to EOF) is a 16-byte header plus one zstd frame containing `(len u8, count u32, nsid)` per collection. Both are reachable with HTTP `Range` requests against `getSegment`, so a segment's per-collection counts cost about 1.3 KB and two requests, not its 270 MB body. Measured 2026-08-29 on segment 6000: header 256 B (0.56 s), collection index 1,033 B (0.46 s), 28 collections whose counts sum to the segment's event count minus its `$account/$identity/$sync` marker events, as the format specifies. ## who does what - **the flow** (`flows/strata.py` in my-prefect-server, scheduled there — the only cron): asks this worker for its progress, lists segments past it via `listSegments`, reads header + collection index per new segment with a bearer archive key rate-limited to 8 Mbps, and POSTs batches to `/ingest`. stateless; reruns are idempotent (segment rows are upserted, collection rows replaced). - **this worker**: `/ingest` (bearer `STRATA_INGEST_TOKEN`) writes D1; `/api/progress`, `/api/heat?step=N`, `/api/collections` read it. no schedule, no archive access. ## the axis caveat `min_witnessed_at`/`max_witnessed_at` are when *stream* ingested the events, not when they happened. the ~6,750 bootstrap segments were all witnessed inside the six-day backfill from 2026-07-28; only later segments are live-era (≈2 minutes each). true record time lives inside event payloads, which this project does not decode. plot by `idx` or `seq`; treat witnessed time as an ingest-era marker. ## secrets `STRATA_INGEST_TOKEN` and the archive key `stream-archive-key-strata` live in the sops store and reach the flow as prefect Secret blocks; the worker's copy of the ingest token is a wrangler secret set from the same store value. ## the drain (decided 2026-08-29) the long tail needs rows, not footer counts. `drain/drain.sh` runs on the stream box (`/opt/strata`, with the duckdb CLI and the jetstream SDK's `segment-export` example): for every sealed segment it decodes rows from local disk (`seq, time_us, did, kind, op, collection, rkey` — witnessed time, no record bodies), writes parquet partitioned by namespace with duckdb, and PUTs each partition to this worker's `/archive/` with the ingest bearer. keys: `events/ns=/seg=.parquet`, `agg/seg=.parquet` (per collection/kind/op: events, distinct dids, seq and time bounds), `manifest/seg=.json`. resumable via `done.txt`; the active segment is never read. measured: 16–17 s per segment at nice 19. placement policy: `events/ns=app.bsky/*` goes to the private bucket (`strata-archive-private`); every other partition, all aggregates and manifests are public at `pub-735e1688181b45e49c925eed81fe2da7.r2.dev` (bucket `strata-archive`, CORS open for range reads). the tail is what the page queries live with duckdb-wasm; `app.bsky` is served from aggregates. ## compaction moves the ground stream compacts sealed segments continuously (several passes per two days), rewriting a segment in place with rows dropped and a new checksum. two consequences, learned on the first full walk (2026-08-29): - a listing taken at the start of a run can disagree with a header read minutes later; the flow re-lists that one segment and reads again. - a segment summarised once is stale after its next rewrite. the flow therefore reconciles by checksum every run: `listSegments` for everything (seven requests), `/api/checksums` for what the worker holds, and it re-reads whatever is new or changed. there is no cursor. the drain's parquet has the same exposure: `events/ns=*/seg=N.parquet` and `agg/seg=N.parquet` reflect the segment as it was when drained, and deletes folded in later are not reflected. `manifest/seg=N.json` should carry the checksum so a re-drain can be selective; not yet done.