name: cohort-sharding
description: Shard archives in depth — strict structural cohesion, dictionary union-merge, set-rung auto-widen on create and add, grouped shards, the anchor syntax, union-wide memory for materializing operations, single-writer concurrency, and transport-only compression for moving a cohort.
type: guide
kind: design
applies_to: inspect, predict, process, compose, sample, facet
covers: [shard archives, cohesion, auto-widen, anchor syntax, transfer, zstd]
Sharded cohorts in depth
Part of the .pulse schema surface; entry skill cohort-schema-design (Sharded cohorts — the two on-disk shapes). Operations guide: docs/src/internals/managing-shard-archives.md.
Sharded cohorts — cohesion
- Structural: strict at insert except the
set_*RUNG (auto-widen below). Field count, names, type bytes, field widths, bit positions must match canonical, elsePULSE_SHARD_SCHEMA_MISMATCH. Stored byte offsets (a cache) are never compared. - At most 65535 shards (
shard_countis a u16); more →PULSE_SHARD_COUNT_LIMIT, nothing written. - Descriptions: tolerant. Divergence →
PULSE_SHARD_DESCRIPTION_DIVERGENCE(warning); canonical wins. - Nullability (
details.dimension: nullable): never widened. A shard nullable where the archive is not →PULSE_SHARD_SCHEMA_MISMATCH; the reverse only with equal bitmap widths, outside groups. - Default zone field (
details.dimension: zone_field): strict, one per archive (or none);pulse cohort zone set/clearrewrites_schema.pulse+ every shard (PULSE_SHARD_ZONE_REWRITTEN). - Vectors (
vec_f32/vec_f64): the dim is strict too (details.dimension: vector_dim) — it is not in the type byte, so a trailing vector’s dim mismatch is caught nowhere else. Labels / kind / scale range are description-class:PULSE_SHARD_DESCRIPTION_DIVERGENCEnamingvector_labels/vector_kind/vector_scale; canonical wins. - Categorical / set dictionaries: union-merge. Canonical entries first; new entries appended; incoming records byte-rewritten with remapped indices.
- Prefix-only validator raises
PULSE_SHARD_DICT_DIVERGENCEwhen embedders coordinate dicts upstream. set_*auto-widen. Two forces, composable: (1) a dictionary union that outgrows a set field’s bitmask; (2) the arriving shard declaring a different rung. Either way the field is promoted to the narrowest rung holding every member, across_schema.pulseand every shard payload, instead of refusing the shard.- Only ever wider. A NARROWER arriving shard never narrows the archive (that would silently drop every selection above the ceiling); it is promoted to the archive’s rung, the only shard rewritten.
- Create widens too, on
shard add’s conditions; the warning ridesCreateShardArchiveResult. - The whole-archive rewrite is atomic (see Concurrency) and emits a mandatory
PULSE_SHARD_SET_WIDENEDwarning naming field, old rung, new rung, the arriving shard’s rung and the shards rewritten.details.archive_widenedtells the directions apart. Pastset_u256there is nowhere to widen to andPULSE_SHARD_DICT_WIDTH_OVERFLOWstands. Categorical widths never widen. pulse shard verifyreports set-width headroom per set field (entries used, capacity, headroom, next rung).- Grouped (
0x02) shards. An archive has ONE parent-group layout — part of strict cohesion, soshard verifyrefuses a0x01shard in a0x02archive. On create/add the ARCHIVE’s layout wins: a grouped arrival into an ungrouped archive is stored flattened; an arrival with other groups (or none) is re-encoded into the archive’s. Group dictionaries union-merge canonical-first (stored shards untouched, only the arrival renumbered); past the u32 index space →PULSE_SHARD_DICT_WIDTH_OVERFLOW. An arrival that disagrees with a CONSTANT group promotes it to indexed across every shard; one that violates a declared key is refused (PULSE_GROUP_MEMBER_NOT_CONSTANT). Layout changes emit the mandatoryPULSE_SHARD_GROUPS_REWRITTEN(details.reason,archive_rewritten);shard verifyaddsgroup_index_headroom(a constant group always shows 0). Cheap appends: import every period with the same--groupflags; never elide a field varying between periods.pulse deduprefuses archives — dedup each shard, thenshard create.
Anchor syntax
respondents/Q1.pulse → full sharded cohort (union semantics)
respondents/Q1.pulse#20190101.pulse → named shard, as one-shard cohort
Anchor on a single file: PULSE_ARCHIVE_MAGIC_INVALID; missing: PULSE_SHARD_MISSING.
Memory shape
Materializing ops (percentile / median, quantile and date groupers, windows, decimal paths, tier-1 tests / groupers / features, two-pass attrs, tier-2 post tests) materialize across the union of shards; median-of-medians is not the median. Memory scales with shard count, except on a mergeable ShardWorkers run (partials merge in order, then free; <=2x workers live). Archives are random-access, never read whole: open / inspect / count read the zip directory + headers, a scan one window per shard. Deflated entries: PULSE_ARCHIVE_CORRUPT.
Concurrency
A plain add appends in place (old _schema.pulse + directory go dead: verify counts, compact reclaims); past half dead it rewrites (PULSE_SHARD_DEAD_BYTES_RECLAIMED); --rewrite forces that (refused write-open: PULSE_SHARD_APPEND_REFUSED). A crash mid-append leaves <archive>.append: opens refuse (PULSE_SHARD_APPEND_INCOMPLETE) until pulse shard verify --repair (truncating warns PULSE_SHARD_APPEND_ROLLED_BACK). Other mutations (and cohort filter output) stage <archive>.pulse.tmp-staged, fsync + rename; the next sweeps crashed stages. cohort filter --flatten writes surviving rows as ONE file (canonical schema, archive order). No writer lock: callers own single-writer. add / remove keep archive indexes in step (cohort-sidecar-index).
Moving a cohort (transport-only compression)
Compress only to move it: pulse export transfer writes <cohort>.zst (one zstd stream of the exact bytes, single-file or archive); pulse import transfer restores a byte-identical .pulse (sha256 in both reports). Never at rest — random access, mmap, lookup and parallel decode need the raw bytes, so any read surface refuses an artifact with PULSE_COHORT_COMPRESSED. CLI/library only, no MCP tool. Detail: docs/src/format/transfer.md.