moczarr.stats
D20 stats sidecars + D22 stats rollups (read side), and O11 verification.
zagg's writer (englacial/zagg PR #307) PUTs one small JSON stats sidecar
per successful shard as a SIBLING object next to the leaf .zarr (never
inside it — the leaf stays vanilla zarr v3 and the D4 commit stamp stays its
final write): timings, counts, memory, cost, catalog identity
(granules_sha256), the D19 semantic_hash, and — when the writer
records them — the O11 per-array content hashes. The unified second-pass
sweep (englacial/zagg#300, D22) folds those sidecars up-tree into
stats.rollup.json objects at digit nodes.
Sweep-built overviews (the zagg spec §4 pyramid family) get the same D20
record, so they have the same two readers — :func:read_overview_stats and
:func:verify_overview_arrays — differing only in addressing: an overview
lives at an ancestor NODE under a {window}.zarr basename, and its sidecar
name is the §5.3 stem grammar at every spec revision
(:func:overview_sidecar_key), never the store's own.
Everything here is read-side and telemetry-class: sidecars and rollups are
never load-bearing (D9 — deleting every one leaves leaf reads intact), so
the readers are tolerant — absent or malformed objects read as None with
a debug log, never a raise. The one loud surface is name arithmetic
(:func:stats_sidecar_key), which mirrors zagg's spec-keyed seam exactly: a
writer/reader spec mismatch must fail, not silently key the wrong name.
O11 (englacial/zagg design registry, resolved 2026-07-20): the logical
content hash of a leaf is per-array sha256 over decoded values — each
named zarr array's full decoded contents as raw C-order little-endian bytes
at the declared dtype, after decompression — never stored object bytes,
which churn on codec/library upgrades. :func:hash_arrays recomputes it;
:func:verify_arrays compares against the sidecar's recorded hashes
("intended identical" — the semantic hash — vs "actually byte-identical").
O11's scope includes the ragged (vlen-bytes) arrays, whose decoded elements
are Python objects rather than a flat buffer — see :func:hash_arrays for
the length-prefixed vlen recipe this reader defines (there is no zagg O11
writer yet, so the recipe is pinned here and by the committed fixture).
combined_hash(hashes)
The O11 combined hash: sha256 of the sorted per-array hex digests.
Serialization pinned here (and by the committed fixture's golden): the
digests sorted lexically and joined with "\n", hashed as ASCII —
array names deliberately excluded, per O11's definition ("hash of the
sorted per-array hashes").
Source code in src/moczarr/stats.py
573 574 575 576 577 578 579 580 581 | |
hash_arrays(store_root, leaf, *, store=None, **store_kwargs)
O11 per-array content hashes of one leaf, recomputed from decoded values.
Opens the leaf as vanilla zarr v3 and hashes EVERY array beneath it
(data fields, the ragged vlen arrays, morton, every coordinate — the
resolved O11 scope), keyed by the array's path relative to the leaf root
(e.g. "8/morton"). Each hash is sha256 over the array's full decoded
contents as raw C-order little-endian bytes at the declared dtype —
decoded values, never stored object bytes, so codec/packaging changes
(ShardingCodec inner chunks, compressor upgrades) are invisible by
construction while any value change flips the hash (exact bytes, no
float tolerance — the PR #282 class is meant to flip it).
Vlen (ragged) recipe. An object-dtype array (zagg's
variable_length_bytes ragged payloads, issue #209, and their
{field}_locations sibling, issue #87) has no flat buffer to hash —
ndarray.tobytes() on it serializes pointer addresses, which are
per-process garbage. Such an array is instead hashed as, in flat C order
over cells::
sha256( for each cell: uint64_le(len(payload)) || payload )
where payload is that cell's decoded bytes (:func:_element_bytes)
— for zagg that is exactly the little-endian buffer
write._ragged_payload_bytes stores. The length prefix is what makes
the digest injective (without it [b"ab", b"c"] and [b"a", b"bc"]
collide), and an empty cell contributes its zero length, so a b""
fill is distinguishable from a missing cell only by position — which is
the intent: the digest covers the cell grid, not just the payloads.
zagg has no O11 writer yet, so this recipe (and the committed fixture's golden) is the only artifact of the choice: zagg's writer must adopt it verbatim when it lands (spec home: englacial/zagg#340).
Versioned leaves (zagg spec §1.5). leaf is resolved through its
root stamp first (:func:moczarr.store.leaf_data_prefix): on a pointer
root the arrays hashed are the CURRENT version's, keyed relative to the
version ("8/morton", never "run-…/8/morton") — the form §5.3
records, identical to a legacy leaf's. Without that, the walk below
would sweep every retained version, and a converted legacy leaf's
superseded root arrays, into one mapping. A pointer naming a missing or
unstamped version is a corrupted leaf, read as debris: {}, the same
answer as a leaf that is not there. A prefix whose root carries no
current — a legacy leaf, an overview, a column, a version addressed
directly — hashes in place, as before, minus any run-… child: on a
legacy root that is a version whose pointer swap has not landed (a write
in flight, or one that died before it), which §1.5 makes invisible to a
path reader until the swap — and §4.2 tells it from a cell-order group
by name.
Source code in src/moczarr/stats.py
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 | |
overview_sidecar_key(basename)
Sidecar object name for an OVERVIEW leaf basename — stem grammar, always.
zagg spec §5.3, normative: a sweep-built overview's sidecar is named from
the overview's own basename — the stem plus .stats.json
(all.zarr -> all.stats.json, 2019.zarr ->
2019.stats.json) — unconditionally, at every store spec revision,
unlike a source leaf's sidecar name (:func:stats_sidecar_key), which is
keyed to the spec. One ancestor node holds every window's overview
(§4.2), so the legacy revision's bare name would resolve all of them to a
single stats.json at that node.
Implemented by asking :func:stats_sidecar_key for the /3 grammar
rather than restating it: the point of this function is the pin — that
the store's own spec is not consulted here — and a second copy of the
stem rule could drift from the one the /3 stores will use.
Source code in src/moczarr/stats.py
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | |
overview_sidecar_path(leaf)
Store-relative path of an overview leaf's stats sidecar (its sibling).
Source code in src/moczarr/stats.py
213 214 215 | |
read_overview_order_stats(store_root, manifest, order, *, window=None, store=None, **store_kwargs)
Every materialized node's D20 record at one declared overview order.
:func:read_overview_stats swept over an order, taking
:func:moczarr.pyramid.open_overview_order's signature —
(store_root, manifest, order, *, window=…) — so the dataset and
telemetry surfaces read side by side. order is the declared
ancestor order k, not the §4.4 cell order the node is named by.
The one place the two surfaces deliberately differ is window=None
on a windowed (morton-hive/2) store. open_overview_order
refuses it (pass window=...): a dataset node has to say which window
its cells are, and the all-time folds have no counterpart on the source
axis (espg/moczarr#30, #31). This accepts it, addressing the all.zarr
fold — the same meaning window=None has in :func:read_overview_stats,
which is the primitive this sweeps and which has no order-level tree to
mislead about. The consequence is real and is the price of that
symmetry: on a product declaring all_time: false, or one whose folds
were deleted (legal — §4.1 regenerable caches), the omitted-window=
sweep returns {} where the dataset surface would have raised. Pass
window= explicitly whenever per-window records are what you meant;
the mapping does not record which object family it read.
Returns {node decimal: record} in packed-word order, over the nodes
:func:moczarr.pyramid.overview_nodes names (the root MOC's source
shards coarsened to the order-k prefix — shared with
open_overview_order, so the two cannot disagree about which objects
the order has). A node with no readable sidecar is simply absent from
the mapping: a sidecar exists on success only and its PUT is fail-open
writer-side, so absence means "no telemetry", never "no data" — and
absence here does not even mean "no overview", since the object can be
perfectly present with its sidecar deleted (D9). Compare the returned
keys against open_overview_order's zagg_objects entries to tell
the two apart.
Enumeration needs the root coverage.moc: an unusable one warns and
returns {} (as open_overview_order omits the order), because the
alternative — an empty mapping indistinguishable from "no telemetry
anywhere" — is the silence this reader's D9 posture is meant to avoid.
Source code in src/moczarr/stats.py
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 | |
read_overview_stats(store_root, node, *, window=None, store=None, **store_kwargs)
One OVERVIEW leaf's D20 stats record, or None when absent/unusable.
The overview counterpart of :func:read_stats, differing in the two
places zagg's overview writer differs (englacial/zagg PR #356):
- Addressing is per node. A sweep-built overview lives at an
ancestor node — order
k < shard_order— so there is no shard id at the store's shard order to name it with.nodeis any digit-tree prefix, exactly as :func:read_stats_rolluptakes one (packed word or decimal string). - The sidecar name is spec-independent — :func:
overview_sidecar_key(§5.3). On a/1or/2store the spec-keyed name would bestats.json, and the object would read as absent.
window selects the overview basename: a declared label, or None
for the all.zarr object — which is both the unwindowed store's only
overview and, on a windowed store, the all-time fold
(pyramid.overview.all_time, §4.5). The reserved token is spellable
here, unlike at the dataset openers' window= (espg/moczarr#30): this
returns one object's telemetry record, not a coverage view.
Same D9 posture as :func:read_stats — absent, unparsable, or
non-mapping all read None with a debug log, and an overview that was
never swept (or was deleted, which is legal — overviews are regenerable
caches, §4.1) is simply absent. Name arithmetic stays the loud surface: a
path_grouping > 1 store RAISES rather than composing a key the writer
never wrote.
For every node at an order in one call see
:func:read_overview_order_stats.
Source code in src/moczarr/stats.py
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 | |
read_stats(store_root, shard, *, window=None, store=None, **store_kwargs)
One leaf's D20 stats record, or None when absent/unusable.
shard is the leaf's shard id (packed uint64 word or decimal
string) at the store's shard order; window selects the windowed
leaf's sidecar (stats_{window}.json) exactly as it selects the leaf.
The sidecar name and hive path are arithmetic off the manifest (spec,
path_grouping) — no LIST. Sidecars exist on success only and their
PUT is fail-open writer-side, so absence means "no telemetry", never
"no data" — completeness stays the commit stamp's job (D4).
For folded stats above the leaf level see :func:read_stats_rollup.
Source code in src/moczarr/stats.py
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | |
read_stats_rollup(store_root, node, *, store=None, **store_kwargs)
The D22 stats rollup envelope at a digit node, or None.
node is any digit-tree prefix — a base component, an interior
prefix, or a shard id (packed word or decimal string; shard-node rollups
fold that shard's windows). Returns the sweep envelope as written
(spec/family/node/order/generation/payload,
plus windows at shard nodes); envelope["payload"] is a merged
D20 stats record — the same shape :func:read_stats returns, folded
over every leaf beneath the node.
Cache posture (mirroring the sweep's own reader, zagg.sweep._read_rollup):
missing, unparsable, wrong-spec/family, or stamp-less objects all read as
None with a debug log — a rollup is a regenerable derived artifact
(D9), and its generation stamp makes staleness detectable, not
prevented; consumers needing exactness fold leaf records themselves.
"Stamp-less" is checked to the same depth as the sweep (a generation
dict with an integer n_leaves), with one deliberate divergence: the
sweep requires only that payload be present, this requires it to be
a mapping — the documented envelope["payload"] contract, and cheaper
to enforce here than to guard at every consumer.
Source code in src/moczarr/stats.py
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 | |
stats_sidecar_key(leaf_name, spec=None)
Sidecar object name for a leaf zarr basename, keyed by store spec.
Mirrors zagg's writer seam (zagg.telemetry.sidecar_key, PR #307) so
the two sides cannot drift silently:
- Legacy (
specabsent /morton-hive/1//2): bare leaves getstats.json; windowed leaves getstats_{window}.json(a hive node holds every window's leaf of its one shard, so a bare name would self-clobber across windows). Frozen grammar. morton-hive/3(D23 window-basename naming): the leaf stem +.stats.json—{window}.stats.json, andall.stats.jsonfor theschedule: nonereserved token (allsatisfies the label grammar by design and is excluded from explicit window labels spec-side, so it needs no special case here).
An unrecognized spec RAISES rather than defaulting to the legacy grammar: a versioned naming bump must be a loud, deliberate change, or a writer/reader spec mismatch would key the wrong name and read as absent instead of failing.
Source code in src/moczarr/stats.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | |
stats_sidecar_path(leaf, spec=None)
Store-relative path of a leaf's stats sidecar (sibling of the .zarr).
Source code in src/moczarr/stats.py
131 132 133 134 | |
verify_arrays(store_root, shard, *, window=None, store=None, **store_kwargs)
Verify one leaf's decoded contents against its sidecar's O11 hashes.
The verification half of the D19 identity story: the semantic_hash
says two leaves were intended identical; the O11 content hashes say
they are byte-identical — and localize a mismatch to the array. Also
the detection mechanism for stamped-but-torn leaves under the
concurrency contract's out-of-contract case.
Returns::
{
"leaf": ..., # store-relative leaf path
"computed": {name: hash}, # recomputed per-array hashes
"combined": ..., # recomputed combined hash
"recorded": ..., # sidecar's per-array hashes (or None)
"recorded_combined": ., # sidecar's combined hash (or None)
"match": ..., # True/False; None = nothing recorded
"combined_match": ..., # True/False; None = none recorded
"mismatched": [...], # array names differing (either side)
}
match is None when the sidecar is absent or records no
content_hashes — "nothing to verify against" is a distinct answer
from "verified" (the conservative posture zagg's dedup takes: an
unverifiable leaf is never a hit).
combined_match is the recorded-vs-recomputed combined-hash verdict,
kept as its own greppable signal because it fails for a DIFFERENT reason
than a per-array mismatch: the combined hash is derived from the
per-array digests, so "every array matches but combined differs" is not
a data problem at all — it is proof the writer's combined serialization
diverges from :func:combined_hash (the choice this PR asks zagg to
freeze). A False there also forces match to False: a leaf
whose recorded combined hash disagrees is never reported verified.
Source code in src/moczarr/stats.py
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 | |
verify_overview_arrays(store_root, node, *, window=None, store=None, **store_kwargs)
Verify one OVERVIEW leaf against its sidecar's O11 hashes.
:func:verify_arrays with :func:read_overview_stats's addressing —
ancestor node plus the overview basename, and the §5.3
spec-independent sidecar name. The return dict is
:func:verify_arrays's, verbatim, including the posture that matters
most here: match is None means unverifiable, not tampered.
Two different things read that way, and computed is the
discriminator. A non-empty computed is an overview that exists but
records no content_hashes (an older sweep, or a fail-open sidecar
PUT). An empty computed — equivalently a combined equal to
the sha256 of the empty byte string — means no arrays were found under
leaf at all: there is no overview object at that node. The node path
is composed arithmetically from any digit string, so a mistyped node, an
order the sweep never ran, and a deleted overview all land there. Check
computed (or open_overview_order's zagg_objects) before
reading match is None as a statement about a present object.
Overviews are regenerable caches (§4.1), so a mismatch indicts the cache,
not the store: the fix is to re-run the sweep. What it localizes is
which folded array diverged — the same signal a source leaf's mismatch
gives, which is why this shares :func:hash_arrays rather than
approximating it.
Source code in src/moczarr/stats.py
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 | |