Skip to content

Phase 2: record the upstream snapshot per version - #1920

Merged
neuromechanist merged 18 commits into
feature/issue-1046-epic-upstream-freshnessfrom
feature/issue-1908-phase2-source-version
Oct 24, 2026
Merged

neuromechanist merged 18 commits into
feature/issue-1046-epic-upstream-freshnessfrom
feature/issue-1908-phase2-source-version

Conversation

@neuromechanist

@neuromechanist neuromechanist commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Phase 2 of epic #1046 (ADR 0111): record which upstream snapshot each NEMAR version mirrors.

Adds dataset_versions.source_version and source_version_basis (doi, lineage, unknown; NULL means never examined) in migration 0104, a shared pure parser (shared/source-snapshot.ts), a fail-open recorder at all five dataset_versions insert sites, the dry-run-first POST /admin/datasets/upstream-backfill route with nemar admin upstream-backfill, and the two fields on GET /datasets/:id/versions rows. ADR 0111 gets a dated append-only amendment (the "central flow already reads the description" premise was wrong; the callback now fetches it with a 4 s timeout and fails open) and the design note is updated.

The migration is numbered 0104 because origin/dev holds 0100 and 0101 and open branches reach 0103. Re-check before the epic merges. A cross-column CHECK inside ADD COLUMN was verified on SQLite only, not on D1. Nothing was deployed or run against production or dev data.

Test plan

  • Phase 2 files together: 132 pass, 0 fail
  • bun run typecheck and bun run lint clean on touched files
  • Full bun test: 18738 pass, 231 skip, 2 fail (one fixed in c4df1d1; test/account-copy-parity.test.ts website parity drift, being checked against the epic base)
  • Mutations caught: migration CHECK widened or term dropped; parser unique-tag, own-entry, two-dataset, numeric sort, zero-padding; insert absent-mapped-to-unknown, timeouts widened, warning check, id check; backfill IS NULL guard, fetch-error-to-unknown, cursor, age filter, suspect-credential guard, limit clamp, withdrawn and repo filters; CLI execute always sent, loop break, exit conditions

Closes #1908

Part of epic #1046

Migration 0100 adds source_version and source_version_basis with a CHECK that keeps never-examined, unknown and known-tag apart. Tested on real SQLite, including the NULL-safe CHECK terms; datasets is untouched (budget test still 84).
shared/source-snapshot.ts: sourceSnapshotOf, isOwnSourceEntry, openNeuroSourceId, compareSourceTags, mirroredTagOf. Reads only the dataset's own SourceDatasets entry; any disagreement is unknown. Unit tested (36 cases); each rule mutated and caught.
One INSERT_DATASET_VERSION_SQL for all five dataset_versions insert sites. The manifest-ready callback and admin manifest regeneration read the tagged dataset_description.json (bounded, never throws); the inline sites use the description they hold. An absent file or failed read stays NULL/NULL. Tested through the real worker and admin route plus a source scan; mutations caught.
POST /admin/datasets/upstream-backfill examines never-examined on###### version rows in keyset pages (default 25, max 50), dry run by default. A fetch error writes nothing, a 404 is unknown only past one hour, an all-absent batch is a suspect credential, and the write never overwrites. 23 route tests on real SQLite; mutations caught. Route inventory pin updated.
Loops POST /admin/datasets/upstream-backfill over next_cursor (250 ms pause, --max-batches), dry run unless --execute, reads the 502 body so the resume cursor is printed, exits 1 on any error or unfinished pass. Tested through the real CLI against a local server; mutations caught. API export pin updated.
GET /datasets/:id/versions rows gain source_version and source_version_basis (always present; nulls mean never examined). The route is outside the OpenAPI document by design, so the CLI VersionInfo type is the contract. Tested through the real route; mutation caught.
Dated append-only amendment to ADR 0111: the insert-site table (A and B live, C and D dev-only, E unreachable) corrects the callback-already-reads claim; the entry-level Version reading, the meaning of doi, and the insert-versus-backfill rule for an absent file. Design note section 4 updated to match. Docs only; the ADR index test passes.
The email epic (#1893) holds 0100_broadcast_queue on its pushed branch; this takes 0101 so the two never share a number. Renames the file and updates the references in code, tests and docs; the migration test, column budget test and route tests pass.
The test compared the schema after ALL migrations with the schema before 0098, so any later column on dataset_versions (0101 adds two) failed it. It now builds the after-schema through 0098 only. Test passes (30).
INSERT_DATASET_VERSION_SQL uses ON CONFLICT (dataset_id, version) DO NOTHING instead of INSERT OR IGNORE, so a snapshot pair the CHECK refuses fails the insert (callback 500, job stays dispatched) while a replayed version stays a silent no-op. Tested on real SQLite and through the callback; reverting to OR IGNORE fails both new tests.
Normalize the ref with toVersionTag (no vv1.0.0), cap the body at 1 MiB (over-cap is a read error), strip a byte order mark, warn when an insert-time read is absent, and document the 5 s worst-case latency. Removes the unused readMirroredTag and readSourceVersionRows. New tests: stalled App token mint is cut off at the deadline, the legacy handler (site D) through its route, cap boundary with and without a declared length. Mutations caught: ref, deadline race, cap (stream and exact), BOM, site D bind, absent warning.
An absent description becomes unknown (final) only when a control read of the same repository at main succeeds with the same credential; otherwise the row is not_examined and retried. The suspect-credential guard no longer depends on batch size or on other errors. Only N.N.N versions are candidates. A write result without a change count is an error, and execute= is parsed strictly (400). Tests: lone and mixed batches, shared control read, keyset tie-break inside one dataset, v-prefixed and non-semver rows; 9 mutations caught.
nemar admin upstream-backfill says a recorded unknown is final, and a finished pass with rows not examined prints one line to rerun without --after (an unfinished pass prints the resume cursor instead). Tested through the real CLI; three mutations caught.
Migration comments: TRUE-or-FALSE-never-NULL wording, the full unknown definition, and the cross-column CHECK marked unverified on D1. ADR 0111 amendment: one correction and three readings, names the decision 4 sentence it corrects, control read and N.N.N rules, ON CONFLICT insert; README entry notes decision 4 is built. Design note: dated migration wording.
In the full Bun process an earlier file leaves the GitHub rate-limit bucket drained, which made the legacy-handler description read fail fast and the site D tests see NULL/NULL. The tests now reset the rate-limit state and the installation-token cache before each test; the file passes alone (38) and the full-suite check follows.
In a full Bun process an earlier file installs a permanent mock.module of services/github whose getTreeAtRef returns an empty tree, so the legacy version-DOI handler (site D) read NULL/NULL and the tests failed in the full run only. The four assertions now live in source-snapshot-legacy-handler.test.ts and run in a child process via a wrapper test; the stand-in is shared through helpers/source-snapshot-standin.ts. Mutating the site D bind fails the wrapper.
@github-actions

github-actions Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

📦 Package Preview

Your package is ready to test!

bun install -g nemar-cli@PR1920

Version: 0.10.24-PR1920.3ae2b25
Commit: 3ae2b25


🤖 Updated on each push.

origin/dev holds 0100 and 0101 (email epic) and its branches reach 0103. Renames the file and updates the references in code, tests, the ADR 0111 amendment and the design note. Migration, column budget and route tests pass; no duplicate migration prefixes.
errors[] carries kind: transient (a rerun fixes it) or needs_admin (absent at the tag and at main, which a rerun never fixes). The CLI states the two causes separately and marks needs-admin rows; the ADR amendment says the same. Also rewraps the route header comment. Route and CLI tests pass; the cause mutation is caught.
@neuromechanist
neuromechanist merged commit 535dd84 into feature/issue-1046-epic-upstream-freshness Oct 24, 2026
14 checks passed
@neuromechanist
neuromechanist deleted the feature/issue-1908-phase2-source-version branch October 24, 2026 10:03

This branch was previously deployed

1 inactive deployment
npm — 3ae2b250 Deployed Oct 24, 2026 by neuromechanist via publish #3860
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