# Delivery: stores, catalog, API, uploads

LLMS index: [llms.txt](/docs/llms.txt)

---

The pointer/manifest contract is in
[architecture.md](architecture.md#delivery-contract); running uploads is in
[publishing.md](publishing.md).

## Zarr store

Every bundle of one resolution tier of a run or round is published in one
shared store, `bundles.zarr/` / `bundles.<tier>.zarr/` (profile 2; a series
store and every run published earlier are one store per bundle, profile 1);
[zarr-profile.md](../zarr-profile.md) is normative. Choices the spec does
not explain:

- **One store per tier, not per bundle.** A per-bundle store repeated its
  group document and three coordinate arrays (seven objects) beside every
  bundle's few shards: 88 % of the objects a run wrote, at a Class A
  operation each. Shared, they are written once per tier; the group
  document is then the one object several bundles share, so it is a merge
  of per-bundle parts written last (`assemble-run` in a fan-out) and its
  CRC-32 is every entry's `?v=`.
- **One shard per array** (whole axis, whole grid). A bucket bills per
  object: per-time-chunk shards made a GFS run ~5 000 objects. The shard
  index, one suffix range, is the whole index. Readers take the shard length
  from the chunk shape, so earlier per-time-chunk stores still read.
- **Inner chunks are regular six-frame chunks** of the bundle's tiles. They
  match the container's temporal groups only up to the first step change,
  so every chunk is encoded from the codes, never copied out of a
  container.

Writing:

- A build writes each bundle's arrays straight from its codes: `binconvert`
  per bundle job through `zarrstore.write_bundle_arrays` over a
  `CodesSource` (NumPy only), the native encoder through
  `encode/zarrstore.rs`, both with up to four tiers in flight over one
  compression pool, then the store's part and group document
  (`write_part`, `finalize_store`; the native path's group document is
  written by `xuebuild/native.py`, [encoder.md](../encoder.md)). A
  bundle's arrays are a function of the metadata, the codes, the tile, the
  predictors and its dimension names, so no container is read; `tests/test_native.py` and `tests/test_zarr.py` hold the two
  writers byte-identical. Every chunk is decompressed again and compared
  after compression, and the native writer parses each document and index
  back with the decoder's own validators.
- A tier's manifest `bandwidth` hint is computed from the delivery it
  ships: the container's size where one is written, the store's otherwise.
- `build-bin` and `showcase build` write the stores alone
  and the manifest names nothing else; `--xue` (`container=True` in the
  Python and wheel APIs) writes each bundle's `.xue` beside its store and
  names both. A store is never derived from a finished `.xue`.
- The `zarr` dependency group is for tests and for reading delta stores
  with zarr-python (`xuebuild/zarrcodec.py`).

Manifest: a bundle entry and each variant may carry `zarr: {path,
byteLength, crc32}` (`crc32` of the group `zarr.json`, also the store's
`?v=`, the same on every entry naming a shared store; `byteLength` the
bundle's share), validated by all three validators and stamped by
`assemble-run`. Entries of one tier share the store's path, the one
exception to the validators' unique-path rule. The container fields are present whole or absent whole; an
entry with neither delivery is refused.

Browser (`web/src/decode/zarr/`, the default channel unless `?backend=xue`):

- Fallback order: store by ranges → `.xue` by ranges → `.xue` whole → store
  by whole objects (only when there is no container). Probes stream or fail.
- `decode/zarr/worker.ts` speaks the `worker.ts` protocol. The format is read in
  wasm (`rust/xue/src/zarr.rs` through `rust/xue-wasm`): the group and array
  documents, the CRC-32C-checked index once per shard, the span of a
  frame's tile (a chunk may straddle container groups), the decoded tiles of
  the time chunk being read, the frame assembled from them and a cell's
  series. `store.ts`
  coalesces one shard's ranges per microtask (gap ≤ 64 KB), keeps at most
  sixteen of a worker's requests on the wire, and asks again (after 250 ms,
  then 1 s) for one that died there: every request to the origin rides one
  HTTP/2 connection, and an edge that closes it takes all of them at once.
  A request that goes quiet — no response, or no further piece of its body,
  for ten seconds — is aborted and asked again the same way. A request on a
  connection the edge closed is not always answered or failed (in Safari a
  pinned point's rows were seen to stay empty with no error after one), and
  one that never settles would keep its place under the limit for good,
  sixteen of them leaving a worker that reads nothing.
  Range support is probed once per object (`sessions.ts`), and the probe's
  byte is read rather than cancelled.
- A layer's session has a worker to itself. A pinned point's sessions —
  one per meteogram row, and four per pressure level with a model column on
  the chart — share at most four hub workers (`decode/zarr/hub.worker.ts`), each
  hosting many channels: the loop is the same one (`decode/zarr/endpoint.ts`), the
  messages travel in an envelope naming the channel, and `channel.ts` hands
  the session a `DecodeChannel` that hides it. `probepanel.ts` opens them
  six at a time. A hub outlives its channels, so closing one frees its
  reader's WASM memory (`ZarrReader.dispose`) once its reads in flight have
  returned; a dedicated worker frees everything by being terminated.
- Compressed chunks sit in an LRU under `payloadBudgetBytes`
  (`budget.ts::zarrPayloadBudgetBytes`), bounding memory by the prefetch
  window rather than the axis; a decode re-fetches what eviction took.
- A tab that refuses a live manifest or cannot open a live store reloads
  once per manifest (`primaryswitch.ts::reloadForNewerShell`), the insurance behind
  shell-first deploys.
- `tests/web/zarr.test.ts` holds frames and series byte-identical to the
  `.xue` path; `npm run measure:backends` compares backends.

## STAC catalog (`xuebuild/stac.py`)

A static STAC 1.1.0 catalog beside the shell's JSON; the frontend never
reads it. [stac.md](../stac.md) defines the documents, including the point
products'.

- Every document is a **pure function** of the manifest (or a point
  product's `index.json`), the catalog row and `sources.py`: no timestamps,
  no hosts, relative links. So the split-build and top-up identity tests
  cover them.
- Written by the CLI, not the converter: `build-bin` (whole run),
  `assemble-run`, `xue stac`. A `--bundles` piece writes none. Point
  products write theirs in their own build, after the index and the
  pointer (`write_point_product_documents`).
- The live Item `<source>/item.json` is the run's Item relocated
  (`relocate_item`) so its path outlives the run.
- Licence and provider prose: `xuebuild/stac_prose.json`, held
  to the registry by `tests/test_stac.py`. `stacindex.py` renders the root
  `index.html` from the same prose; its hostnames mirror `web/src/site/site.ts`.
- Uploads: the run Item no-cache with the manifest; Collection, live Item
  and root through `upload-r2-stac-collection` (run by
  `upload-r2-pointer`); showcase documents by `upload-r2-showcase`; a point
  product's by `upload-r2-<product>`.

## Data API (`rust/xue-worker`)

The endpoint contract is [api.md](../api.md). Production runs the crate as
the native `xue-api-server` on a self-hosted origin behind a Cloudflare
Tunnel ([api-origin.md](api-origin.md)); no Worker is deployed, and the
Workers build runs only under `wrangler dev`. What follows is how the crate
is built; the Worker-specific parts (R2 binding, `wrangler dev`, upload
size) apply to that build only.

- Resolves Collection → pointer → manifest, reads stores by R2 range
  (`DATA` binding) and decodes with the crate's `decode_chunk`.
- The point products go through `products.rs` (pointer → index, the
  `issue=` pin, the spatial selection, one cached range read per station)
  and a module per product (`sounding.rs`, `airport.rs`, `synop.rs`,
  `tc.rs`) that only reshapes the product's JSON: compact rows to objects,
  fixed point to units, epoch seconds to ISO. The products' goldens under
  `tests/fixtures/<product>/expected/` are their test bucket, so a change
  to a product's schema that regenerates a golden also runs these tests.
  Adding a product is a `PRODUCTS` row plus a module; the routes are
  listed in `route.rs`.
- Read geometry lives IO-free in `rust/xue/src/zarr.rs`, held to
  [zarr-profile.md](../zarr-profile.md). The browser reads through the same
  code (`rust/xue-wasm/src/zarr.rs`); it keeps the requests and the cache of
  compressed chunks. A point read and the browser's series share the cell
  arithmetic (`tile_cell`) and the chunk decode (`decode_inner_chunk`).
- The binding is `remote: true` (honoured by `wrangler dev` only), so
  `make api-dev` runs locally against the real bucket; `make api-dev-cdn`
  (`DATA_SOURCE=cdn`) reads the public origin when the network blocks that.
- `timezone.rs` answers the point's IANA zone from `tzf-rs` with its
  bundled polygon file compiled into the wasm. That file is most of the
  upload (4.8 MiB uncompressed per `wrangler deploy --dry-run`, against a
  64 MiB limit on every plan; only the uncompressed size counts). The
  finder opens on the first point request of an isolate, not at startup,
  so the 1 s startup limit is untouched.
- Only `runtime.rs` and the framing half of `error.rs` touch the Workers
  runtime; `route.rs` and the handlers return plain values, so `cargo test
  -p xue-worker` runs them natively over the web fixtures through an
  in-memory `Data` that logs every read.

## Uploads to R2 (`scripts/aws-config`)

All uploads go through the AWS CLI (`$(S3)` in `mk/r2.mk`).

- **One operation per object.** R2 bills each multipart step as Class A, and
  the CLI's 8 MiB default split large shards into ~7. `AWS_CONFIG_FILE`
  points at `scripts/aws-config`, which sets `s3.multipart_threshold` to
  5 GB. It names no profile; credentials stay in the environment. The
  workflows' credential probe is a `head-object` (Class B), not a listing.
- **Order:** `upload-r2-bundles` (artifacts), then `upload-r2-manifest`
  (`check-pointer`, manifest + run Item, then `upload-r2-pointer`);
  `upload-r2` is both. The pointer goes last because it takes the run live;
  `check-pointer` refuses a pointer that names another run or whose CRC32
  disagrees with the manifest on disk.
- **`columns.zarr`** goes up by `make cut-columns` in the run's `columns` job,
  before finalize: the store's objects (immutable; the group document carries
  every shard's CRC-32, so a changed cut is a new `?v=`) and their edge
  warm-up, with the descriptor handed to finalize, which names it in the one
  manifest it writes, only for a run R2 does not serve yet (a top-up or forced
  rebuild of the live run never cuts before the flip). A run already live
  without its store is attached by
  `make publish-columns`: the manifest and Item, then the pointer last, each
  behind a check that the live pointer still names the run and manifest the job
  started from (the manifest is rewritten at its own key, as a top-up does; its
  `?v=` changes with the pointer's `manifestCrc32`). Shards stay under
  Cloudflare's 512 MB cache limit per object (up to ~140 MB, a few GB per run)
  by sharding along latitude.
- **`prune-r2`** runs in its own job after a successful finalize, six minutes
  late: open pages poll the pointer every five minutes, so one poll has happened
  by then; a page that missed it keeps reading the old run until its next one.
- Nothing is overwritten under an unchanged `?v=` (see
  [`publishing.md`](publishing.md)).
