Skip to content

Phase B2: quality view route and serving-index key (#2324) - #2383

Merged
neuromechanist merged 17 commits into
feature/issue-2324-epic-pipeline-progressfrom
feature/issue-2324-b2-view-route
Nov 26, 2026
Merged

neuromechanist merged 17 commits into
feature/issue-2324-epic-pipeline-progressfrom
feature/issue-2324-b2-view-route

Conversation

@neuromechanist

@neuromechanist neuromechanist commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Part of #2324 (phase B2 of the live quality views, ADR 0137 decisions 6 and 7). Takes the backend items of #2341 that B1 left: the view route, the derivative key, and the registry comment.

What changed

  • The public quality route (backend/src/routes/quality.ts, mounted in routes/data.ts before the version routes; paths, words and bounds in shared/contract/quality-route.ts):
    • GET /<id>/quality/v1/<recipe>/record/<recording>: the recording's dataQuality document (level: record) from the recipe's current public derivative, with the derivative's key and dataset version and each view's nemar.view-digest.v1.
    • GET /<id>/quality/v1/<recipe>/file/<key>/<sha256>/<recording>: one file a validated view of that recording declares.
    • Order of checks: the path (recording is a relativeLocation naming a recording, key and digest 64 hex) before any read; then ONE D1 statement with the dataset gate (active, public, anonymous = 0, first published, not withdrawn, not tombstoned; the pipeline eligibility's own terms) and the derivative bound by the row, CURRENT_PUBLIC_DERIVATIVE_SQL reused; then the edge cache keyed by the dataset's epoch and the derivative key; then the record (at most 512 KiB) checked whole; then the file, re-hashed.
    • A refused dataset answers exactly as an unknown one (quality_not_found). A public key that has expired answers views_expired (410).
    • Public answers: Cache-Control: public, max-age=300, Cache-Tag: nemar-dq-<id>, ETag = SHA-256. Every refusal is no-store and never cached. Unlisted formats are application/octet-stream attachments; every file carries nosniff and an RFC 6266 filename*.
  • The whole declaration is validated (backend/src/services/quality-views.ts): {level: "record", recipes: {<recipe>: entry}} against the vendored neuroschema 0.6.0 dataQuality, which reaches view.schema.json and every kind, content and transform schema. Plus recording scope only, one digest = one file, and the record's own dataset and recording must be the ones asked for (record_elsewhere).
  • shared/contract/schema-check.ts: the Worker cannot run Ajv (new Function), so this interprets the vendored JSON Schema (draft 2020-12 subset the bundle uses) and throws at build time on any keyword it does not implement. Held to python-jsonschema's kinds-verdicts (all 244 cases of the schemas dataQuality reaches) and to Ajv over the verdict, preset and kind corpora plus more than 50,000 one-edit mutations.
  • B1 review points (recordQualityEntry): the reader's unsafe-integer flag is honored over the whole record (integer_unsafe), and a plugin view's plugin must be one the recipe version runs at its quality checkpoint (view_unknown_plugin).
  • Migration 0152 dataset_quality_epochs: a table of its own (datasets is near D1's column cap), advanced by a trigger on every real change of visibility, status, anonymous, first_published_at, withdrawn_at or ezid_status.
  • Derivative key (owner ruling), ADR 0140, migration 0153: a recipe that reads the serving index on the archive input (RECIPES_KEYING_SERVING_INDEX: nemar-ica) gets a version 2 key, nemar.derivative-key.v2, the eight version 1 lines plus serving_index (none, or sha256: and the index file's digest). Every other key is version 1, byte for byte. A run whose spec names --serving-index is not keyed (serving_index_unattested) until the dispatcher stages the index and records its digest. compute_derivatives.serving_index keeps the field so derivative.json writes the key text again.
    • Old keys: unchanged, since a key without the field is the eight-line text (both version 1 vectors still hold).
    • Existing rows: no nemar-ica row exists anywhere. nemar-ica has been a placeholder since it entered the registry (Phase A6: First-party pipeline dispatch #2054), and no path dispatches a placeholder, so no nemar-ica stamp has succeeded or been keyed.
  • Registry comment: already corrected in B1 (shared/compute-recipes.ts: 17 files per recording, about 1,470 recordings at maxFiles 25,000); nothing left to change.
  • compute-derivatives.ts ISOLATION comment names the one reading route. CHANGELOG (Unreleased, Added).

Not done: dispatch staging (stopped, as briefed)

Staging <zarr base>/<id>/zarr/index.json for an archive nemar-ica job needs infrastructure beyond the backend's dispatch code.
The launch spec, nemar.launch-spec.v1, is shared byte for byte with the launcher in nemar-process.
It stages only the code bundle (through a Worker route with the job token) and the datasets (from the data plane).
It has no field for an extra staged file, and the job's network fence admits no host.
So the backend cannot pass --serving-index for a file that exists on the node.
Nothing was invented. --serving-index is not passed, and a run that names it is not keyed (see ADR 0140), so a later staging change cannot collide with today's keys.
See owner question 1.

Tests

Real D1 (bun:sqlite with every migration) and Miniflare R2, using the P7 product from test/fixtures/quality/ica-views:

  • backend/test/quality-route.test.ts (48): each gate term (private, archived, deleted, withdrawn, tombstoned, never published, an nm099998-shaped anonymous deposit, and anonymity alone with the 0085 trigger dropped); canary, copying, failed, expired (views_expired), superseded catalog and superseded published rows; newest by stored_at and the tie rule; one dataset's key under another; record_elsewhere; an invalid full declaration (empty caption, spectrum overlap 1); an unsafe integer; a foreign plugin source; reserved scope; a record over 512 KiB; one digest declared as two files; undeclared digests (including the record's and _ica.json's); digest and size mismatch; a missing object; path traversal and non-recording paths; malformed keys and digests; cache before gate, the epoch in the key, refusals never cached, and the derivative key in the cache key; and migration 0152's trigger.
  • backend/test/compute-derivatives.test.ts: nemar-ica keyed none under v2 (row, key and derivative.json key text); --serving-index in either form is unkeyable; the floor stays v1; the 0153 CHECK (including the Zarr input).
  • test/contract-derivatives.unit.test.ts: new vectors (computed in Python and in Bun) and refusals; four distinct keys for no field, none, index A and index B.
  • test/schema-check-contract.unit.test.ts, test/quality-route-contract.unit.test.ts, test/quality-documents-contract.unit.test.ts.

Gates

  • bun run typecheck, bun run lint, and biome check on every touched file: clean.
  • bun run migrations:d1-check: 144 migrations replayed on local real D1; the catalog matches bun:sqlite.
  • Root, targeted: 850 pass across 10 files (contract-derivatives, quality-documents-contract, quality-route-contract, schema-check-contract, compute-recipes, adr-index, view-digest-contract, contract-neuroschema, contract-schemas, plugin-manifest-contract).
  • Backend, targeted: 358 pass across 16 files (quality-route, quality-documents, metadata-json-*, compute-derivatives, compute-quality-documents-migration, datasets-column-budget, anonymity-projection, data-route-manifest-stream, landing-payload-archive, page-bundle-*, compute-pipeline, compute-progress-reconcile).
  • Not run: the full bun test. It is the epic-to-dev gate and takes about 80 minutes.

Mutation table

Each mutation was applied alone, the named suites were run, and the file was restored. git status was clean after the run.
52 mutations, all killed. In round 1, B2-15, B2-25 and B2-39 survived. The tests were strengthened in 7006e6f "Pin size, one-digest and dependentSchemas checks", and those three were then killed in round 2.

# Mutation Killed by
B2-01 gate: drop active quality-route
B2-02 gate: drop public quality-route
B2-03 gate: drop not_anonymous quality-route
B2-04 gate: drop first_published quality-route
B2-05 gate: drop not_withdrawn quality-route
B2-06 gate: drop not_tombstoned quality-route
B2-07 trust the URL's dataset: derivative not bound to the dataset row quality-route
B2-08 record: drop the current-public predicate quality-route
B2-09 file: serve any gated key, current or not quality-route
B2-10 file: an expired public key answers quality_not_found quality-route
B2-11 read the edge cache before the D1 gate quality-route
B2-12 leave the epoch out of the cache key quality-route
B2-13 leave the derivative key out of the cache key quality-route
B2-14 drop the re-hash of a served file quality-route
B2-15 drop the size check of a served file (round 2) quality-route
B2-16 serve a file no view declares (the first declared one) quality-route
B2-17 drop the relativeLocation check of the recording quality-route
B2-18 drop the recording-name check quality-route
B2-19 a refusal is publicly cacheable quality-route
B2-20 drop the Cache-Tag quality-route
B2-21 no record size bound quality-route
B2-22 drop the record identity check quality-route
B2-23 skip the full schema check quality-route
B2-24 drop the recording-scope rule quality-route
B2-25 drop the one-digest-one-file rule (round 2) quality-route
B2-26 a file input takes no format from its extension quality-route
B2-27 ignore the reader's unsafe-integer flag on a record quality-documents-contract, quality-route
B2-28 drop the plugin-source check of a plugin view quality-documents-contract, quality-route
B2-29 tie to the smaller key (current public derivative) quality-route
B2-30 serve an unlisted format inline quality-route-contract
B2-31 serve an unlisted format as its declared type quality-route-contract
B2-32 schema-check: contains needs no match schema-check-contract
B2-33 schema-check: ignore additionalProperties schema-check-contract
B2-34 schema-check: ignore then schema-check-contract
B2-35 schema-check: oneOf as anyOf schema-check-contract
B2-36 schema-check: lengths in UTF-16 units schema-check-contract
B2-37 schema-check: fail open on an unknown keyword schema-check-contract
B2-38 schema-check: ignore not schema-check-contract
B2-39 schema-check: ignore dependentSchemas (round 2) schema-check-contract
B2-40 schema-check: ignore uniqueItems schema-check-contract
B2-41 key: leave serving_index out of the text contract-derivatives, compute-derivatives
B2-42 key: a version 2 key keeps the version 1 header contract-derivatives
B2-43 key: accept serving_index on the zarr input contract-derivatives
B2-44 key: accept any serving_index value contract-derivatives
B2-45 keyStamp: key a run handed --serving-index as none compute-derivatives
B2-46 keyStamp: key every archive run's serving index compute-derivatives
B2-47 keyStamp: never key the serving index compute-derivatives
B2-48 describe: drop serving_index from the key text compute-derivatives
B2-49 insert: do not keep serving_index on the row compute-derivatives
B2-50 migration 0153: drop the bids pairing compute-derivatives
B2-51 migration 0152: drop the real-change guard quality-route
B2-52 migration 0152: anonymity does not advance the epoch quality-route

Deviations from the plan

  1. Only the public data-plane mount is built, as briefed. The authorized API and admin mounts of C13, and the extraction of dataset-read-access.ts, are not built. A private dataset answers 404.
  2. The full declaration is checked by interpreting the vendored schemas (schema-check.ts), not by a hand-written zod view mirror. It is held to jsonschema's verdicts and to Ajv instead.
  3. The epoch lives in a table of its own (dataset_quality_epochs), not in a datasets column, because of the column budget. Its trigger covers all six gate columns, not only visibility and status.
  4. The gate also requires first publication and no tombstone (the pipeline eligibility terms), in addition to public, not concealed and not withdrawn.
  5. The derivative key change is a new format, nemar.derivative-key.v2, with ADR 0140, because ADR 0123's Consequences require a new format name and a new ADR for any change to the key. The "no index" value is the word none, matching the contract's existing LOCATIONS_NONE, rather than a literal null.
  6. A file input's format comes from its extension (neuroschema's extension-and-format rule), so _servingunmixing.tsv is served inline as TSV.
  7. The view-digest vectors are replayed by B1's view-digest-contract test, not again in quality-route-contract.
  8. No OpenAPI entry, because the data plane is not in shared/openapi.json. No capture script either: the fixtures come from B1's copy at a nemar-process pin.
  9. Status codes the plan leaves open: recording_path_invalid is 400, and record_unreadable and view_data_corrupt are 502. Every refusal is no-store.

Owner and lead rulings (review of 2026-11-25)

  • Q1, staging: it goes in the launch spec in this epic, as its own phase B2b across nemar-cli and nemar-process after this PR merges (see Next: B2b).
  • Q2, older recipe versions: fixed here. The derivative row records its recipe version's quality plugins.
  • Q3, none: kept. The Python replay of the key vectors now runs in CI.
  • Q4, private and admin mounts: a follow-up after B3, filed by the lead.

Next: B2b (dispatch staging of the serving index)

  • Add an optional serving_index: {sha256, size} to nemar.launch-spec.v1. This is a shared schema change in nemar-cli and nemar-process.
  • At dispatch, the Worker reads <zarr base>/<id>/zarr/index.json. It stages the index only when the index's source_commit is exactly the dispatched tag commit, never under metadata_only. Otherwise it stages none, and the run declares no view of the serving copy.
  • The Worker keeps the bytes beside the job's records. The launcher fetches them with the job token (as it fetches the bundle), checks the SHA-256 and size, places the file read-only beside the dataset, and passes --serving-index, --dataset-id and --source-commit.
  • The Worker records the digest in the spec, and keyStamp keys serving_index sha256:<digest> from the spec, never from the record. serving_index_unattested then applies only to a run that names --serving-index without a recorded digest.

Review fixes (round 1)

  1. Important: older recipe versions. Migration 0153 adds compute_derivatives.quality_plugins: the recipe version's quality plugins as a JSON array, recorded when the row is keyed. The route checks plugin view sources against the stored list. A row with a null list (made before the migration; no nemar-ica row exists) serves nothing and logs quality_plugins_unknown. Route tests cover a catalog and a published derivative at an older version, the stored list deciding, and a null list.

  2. Cache-Tag purge. purgeCacheTags and purgeQualityCache in services/cloudflare.ts purge nemar-dq-<id>:

    • after the D1 write that flips a row private, which is the path both unpublish and withdraw take;
    • after a concealment (markAnonymous).

    Purging is best effort: a failure is logged as cache_purge_failed and never blocks the write.
    An explicit second purge on the withdrawal stamp was redundant and untested (mutant B2R-07 survived), so it was removed.
    The residual is documented in routes/quality.ts: a copy in a browser, or in a shared cache outside Cloudflare, can be served for up to 300 seconds.

  3. 0152: the table has only NOT NULL, so the trigger can never refuse an update to a gate column. A test covers a dataset id of an odd shape.

  4. ETag: a strong ETag, the SHA-256 of the response body's bytes.

  5. Gate mismatch: QUALITY_DATASET_GATE_SQL moved to quality-documents.ts, and B1's CURRENT_QUALITY_DOCUMENTS_SQL now uses it. Tests on metadata.json and the page bundle cover a tombstoned dataset and a never-published one.

  • Numbering: the ADR is now 0140; 0138 is taken on dev and 0139 on open branches, and the ADR index tolerates both gaps until they land. Migrations 0152 and 0153 collide with feature/issue-2188-review-inputs (0152); the lead renumbers at epic-to-dev if needed.
  • Python replay: test/contract-derivatives-python.unit.test.ts now computes every derivative_key vector (v1 and v2) and every refused one in Python.

Mutations for the fixes, each applied alone and the file restored:

# Mutation Killed by
B2R-01 route ignores the row's plugin list quality-route
B2R-02 keying records no plugin list compute-derivatives
B2R-03 record ETag is not the body's digest quality-route
B2R-04 private flip drops the purge review-auto-require-paths
B2R-05 purge before the D1 write review-auto-require-paths
B2R-06 concealment drops the purge quality-cache-purge
B2R-07 withdraw drops its explicit purge survived, so the redundant purge was removed (b44f23f)
B2R-08 purge by URL list instead of tags quality-cache-purge, review-auto-require-paths
B2R-09 summaries drop the shared gate metadata-json-quality
B2R-10 0152 gains a CHECK on the dataset id quality-route
B2R-11 0153 drops the quality_plugins shape check compute-derivatives

Gates after the fixes:

  • bun run typecheck and bun run lint: clean.
  • bun run migrations:d1-check: 144 migrations on local D1; the catalog matches bun:sqlite.
  • Root, targeted: 967 pass across 11 files.
  • Backend, targeted: 617 pass across 28 files. These add quality-cache-purge, review-auto-require-paths, unpublish, visibility, withdraw, withdraw-route, anonymity*, publish-withdraw and fleet-visibility-route.
  • Not run: the full bun test.

Review fixes (round 2)

  1. A purge never stalls an unpublish. postPurge aborts each request after 5 s (PURGE_TIMEOUT_MS, AbortSignal.timeout).
    A test with a purge stand-in that never answers shows the flip finishes, the row is private, and cache_purge_failed is logged.
    Handing the purge to executionCtx.waitUntil is not done: applyDatasetVisibility and markAnonymous are services with no execution context, and threading one through every caller is a wider change.
  2. Cache-Tag is set again on a cache hit, because the Workers Cache API strips it. Tested with a cache stand-in that strips the header the same way.
  3. Migration 0153 backfills quality_plugins = '[]' on existing nemar-floor rows, which run no plugin at any version.
    Other recipes' earlier rows stay null: their plugins depend on the version, and a guess could admit a view the run never wrote. No nemar-ica row exists.
    Tested by replaying 0153 over rows written before it; migrations:d1-check passes.
# Mutation Killed by
B2R-12 cache hit returned without Cache-Tag quality-route
B2R-13 purge without a timeout review-auto-require-paths
B2R-14 0153 without the floor backfill quality-route
B2R-15 0153 backfills every recipe quality-route

Gates after round 2:

  • typecheck and lint: clean.
  • migrations:d1-check: OK.
  • Root, targeted: 967 pass.
  • Backend, targeted: 620 pass across the same suites as round 1.

Tripwire for the epic-to-dev merge

test/adr-index.unit.test.ts tolerates gaps 0138 and 0139. When dev, which holds ADR 0138 (rootless Podman), is merged into the epic, remove 0138 from TOLERATED_GAPS and its comment. Remove 0139 too once that ADR lands. Migration 0152 may also need renumbering then (it collides with feature/issue-2188-review-inputs).

No remote environment was touched. Nothing was deployed, and no dev or production D1 or R2 was read or written.

Agentic AI tools were used.

A recipe that reads the serving index on the archive input (nemar-ica) gets a version 2 derivative key whose ninth line is serving_index (none, or the index file's SHA-256), so runs with and without an index, or with different indexes, never collide. Every other key stays version 1 byte for byte. A run handed --serving-index is not keyed until the dispatcher stages the index (serving_index_unattested). Migration 0153 keeps the field on the row; ADR 0138 records the decision.

Tested: contract-derivatives (vectors computed in Python and Bun), compute-derivatives (nemar-ica keying, skip word, column CHECK).
The Worker cannot run Ajv, which compiles schemas with new Function. schema-check.ts interprets the draft 2020-12 keywords the vendored neuroschema schemas use and throws when a set is built over any keyword it does not implement. Held to python-jsonschema's kinds-verdicts (all 244 cases of the schemas dataQuality reaches) and to Ajv over the verdict, preset and kind corpora and over 50,000 one-edit mutations.

Tested: test/schema-check-contract.unit.test.ts.
recordQualityEntry now honors the I-JSON reader's unsafe-integer flag over the whole record (integer_unsafe) and refuses a plugin view whose plugin the recipe version does not run at its quality checkpoint (view_unknown_plugin), the two points the B1 review left to B2. It also returns the dataset and recording the record says it is of.

Tested: quality-documents-contract (113 pass).
Migration 0152: dataset_quality_epochs, a table of its own (datasets is near D1's column cap), advanced by a trigger on every real change of visibility, status, anonymous, first_published_at, withdrawn_at or ezid_status. The quality route keys its edge cache by it.

Tested: migrations:d1-check (144 migrations on local D1, catalog matches bun:sqlite), datasets-column-budget.
GET /<id>/quality/v1/<recipe>/record/<recording> serves a recording's dataQuality document from the recipe's current public derivative (CURRENT_PUBLIC_DERIVATIVE_SQL), and .../file/<key>/<sha256>/<recording> one file a validated view declares. The dataset gate (active, public, unconcealed, first published, unwithdrawn, untombstoned) and the derivative are read in one D1 statement before the edge cache, which is keyed by the dataset's epoch and the derivative key. The record is checked whole against the vendored neuroschema schemas through schema-check.ts, plus recording scope and the record's own identity; a file is re-hashed before it is served. Refusals are fixed words, no-store, never cached.

Tested: quality-route (47 pass, real D1 and Miniflare R2, the P7 fixture), quality-route-contract, quality-documents-contract.
Kills the three mutants that survived the first round: the served file's size check (its log reason), one digest declared as two files of one format, and dependentSchemas in the schema checker.

Tested: quality-route (48 pass), schema-check-contract (254 pass).
@github-actions

github-actions Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

📦 Package Preview

Your package is ready to test!

bun install -g nemar-cli@PR2383

Version: 0.10.40-PR2383.23b753e
Commit: 23b753e


🤖 Updated on each push.

ADR 0138 is taken on dev and 0139 on open branches. The ADR index tolerates both gaps until they land.

Tested: adr-index, contract-derivatives.
The Python harness now computes every derivative_key vector (version 1 and the version 2 key of ADR 0140) and refuses every refused one, so the cross-language claim is tested in CI where uv is available.

Tested: contract-derivatives-python with NEMAR_REQUIRE_SCRUB_TOOLS=1 (117 pass).
Migration 0152 keeps NOT NULL only: its one writer is a trigger inside every update of a gate column, which must never be refused for the sake of a cache.

Tested: migrations:d1-check, quality-route.
CURRENT_QUALITY_DOCUMENTS_SQL now joins the dataset through QUALITY_DATASET_GATE_SQL (moved to quality-documents.ts), so a tombstoned or never-published dataset shows no dataQuality on metadata.json or the page bundle.

Tested: metadata-json-quality (tombstoned and never-published cases), quality-documents.
compute_derivatives.quality_plugins (migration 0153) records the recipe version's quality plugins when the row is keyed; the route checks plugin view sources against it, so a derivative of a recipe version the registry has moved past is still served, and a row without the list serves nothing. The record answer's ETag is now the SHA-256 of the body.

Tested: quality-route (older version, catalog and published; stored list decides; null list refused; body ETag), compute-derivatives.
purgeCacheTags and purgeQualityCache in services/cloudflare.ts purge Cache-Tag nemar-dq-<id> after the D1 write of a visibility flip to private (unpublish, withdraw), a withdrawal stamp and a concealment: best effort, logged, never blocking. The browser and shared-cache residual of up to 300 s is documented in routes/quality.ts.

Tested: quality-cache-purge, review-auto-require-paths (purge after the row is private; a refused purge keeps the flip), unpublish, withdraw, anonymity suites.
Tested: not applicable (documentation).
withdrawDataset flips the row private through applyDatasetVisibility, which purges the tag after the write. The second, explicit purge after the withdrawal stamp was redundant and untested (mutant B2R-07 survived), so it is removed.

Tested: withdraw, withdraw-route, review-auto-require-paths, quality-cache-purge.
postPurge aborts a request after PURGE_TIMEOUT_MS, so a purge API that never answers cannot stall an unpublish; the failure is logged as cache_purge_failed.

Tested: review-auto-require-paths (a never-answering stand-in: the flip finishes, the row is private, the failure is logged).
The Workers Cache API strips Cache-Tag from what it stores; the route sets it again on a hit.

Tested: quality-route with a cache stand-in that strips the header.
Migration 0153 sets quality_plugins = '[]' on existing nemar-floor rows, which run no plugin at any version; other recipes' earlier rows stay null, as the comment says why.

Tested: quality-route (migration replayed on rows written before 0153), migrations:d1-check.
@neuromechanist
neuromechanist merged commit ddb1822 into feature/issue-2324-epic-pipeline-progress Nov 26, 2026
25 of 27 checks passed
@neuromechanist
neuromechanist deleted the feature/issue-2324-b2-view-route branch November 26, 2026 00:52

This branch was previously deployed

1 inactive deployment
npm — 23b753e9 Deployed Nov 26, 2026 by neuromechanist via publish #4920
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant