Skip to content

Sync to nemar.org on version DOI publish - #463

Merged
neuromechanist merged 2 commits into
devfrom
feature/issue-339-version-sync
May 14, 2026
Merged

neuromechanist merged 2 commits into
devfrom
feature/issue-339-version-sync

Conversation

@neuromechanist

Copy link
Copy Markdown
Contributor

Closes #339.

Summary

The version-DOI webhook (POST /webhooks/publish-version-doi) splits
into an EZID branch and a Zenodo branch. The EZID branch was already
triggering an nemar.org sync via c.executionCtx.waitUntil(...), but
the Zenodo branch wasn't — so any dataset on the Zenodo provider would
mint a new version DOI and leave nemar.org showing the previous
version's metadata indefinitely.

Changes:

  • Extract shouldSyncToNemarAfterVersionDoi() from the EZID inline
    predicate into a pure helper. Returns either { trigger: true } or
    { trigger: false, reason } so logging can stay specific.
  • Apply the helper to both branches. The Zenodo branch now triggers
    the same syncToNemarAfterVersionDoi(c.env, dataset, version, published.doi) background call as EZID, matching the non-fatal,
    background-waitUntil pattern.
  • Zenodo also picks up a no_doi skip reason for the edge case where
    Zenodo publish succeeds but returns no DOI string (EZID can't hit
    this path).

The downstream syncToNemarAfterVersionDoi() helper was already
non-fatal: it catches errors, writes them to datasets.nemar_sync_error

  • nemar_sync_status='failed', and never throws. Triggering it from
    the Zenodo branch inherits that behavior unchanged.

Test plan

  • bun test test/version-doi-sync-trigger.test.ts (7/7 pass)
    exercises every branch of the gating predicate: trigger, missing
    username, missing password, on-prefix, null DOI, empty DOI string,
    and ordering (credentials check beats provenance check).
  • cd backend && bun run typecheck
  • bun run build
  • Manual: publish a v1.0.0 of a Zenodo-provider dataset on the
    SCCN dev backend and confirm a row appears in dataexplorer_dataset
    with the new version DOI.

@github-actions

github-actions Bot commented May 14, 2026 •

Copy link
Copy Markdown

📦 Package Preview

Your package is ready to test!

bun install -g nemar-cli@PR463

Version: 0.8.14-PR463.a6aa426
Commit: a6aa426


🤖 Updated on each push.

@neuromechanist

Copy link
Copy Markdown
Contributor Author

Code Review: PR #463 — Sync to nemar.org on version DOI publish

Reviewed with Sonnet. Checked: waitUntil semantics, gating predicate correctness, double-sync risk, error persistence, and test quality.


Critical

None.


Important

shouldSyncToNemarAfterVersionDoi misses xx-prefix sandbox datasets (webhooks.ts:147)

The predicate only blocks on-prefix datasets. Sandbox datasets (xx-prefix) pass the predicate and get { trigger: true }. runDatasetSync inside syncToNemarAfterVersionDoi throws DatasetReindexError("Sandbox dataset ... is not eligible for nemar.org sync") for xx prefixes (dataset-reindex.ts:128-132). The catch block at webhooks.ts:555-571 then writes nemar_sync_status = 'failed' and a runDatasetSync threw... error into D1 for every sandbox version-DOI publish. Not fatal, but every xx publish generates a false-alarm failure row in D1. The on-prefix guard is explicit; xx should be too.

Suggested fix — add to shouldSyncToNemarAfterVersionDoi before the no_doi check:

if (input.datasetId.startsWith("xx")) {
  return { trigger: false, reason: "sandbox" };
}

And add "sandbox" to NemarSyncDecision's reason union.


Redundant guard && published.doi at Zenodo trigger line (webhooks.ts:721)

if (zenodoSyncDecision.trigger && published.doi) {

shouldSyncToNemarAfterVersionDoi returns trigger: true only when versionDoi is truthy. versionDoi was set from published.doi ?? null, so trigger: true implies published.doi is already non-empty. The && published.doi check is unreachable dead code. The EZID caller (line 483-484) doesn't have this redundant guard — so the Zenodo caller is inconsistent. If trigger is somehow true but published.doi is falsy (impossible by invariant), the sync would silently skip with no log. The guard should either be removed (trusting the invariant, consistent with EZID) or pushed into the predicate itself.


Suggestions

waitUntil is correct — confirming the specific scrutiny question

c.executionCtx.waitUntil() is the canonical Cloudflare Workers pattern for running work after the HTTP response is flushed. The Workers runtime keeps the isolate alive until all registered promises settle. Both branches correctly use waitUntil — neither uses await, which would block the response. This matches the pre-existing EZID pattern and ctx.waitUntil(scheduledCleanup(env)) in index.ts. No issue here.

Error persistence confirmed correct

syncToNemarAfterVersionDoi at lines 555-571 catches any exception thrown by runDatasetSync and writes nemar_sync_status = 'failed' + nemar_sync_error to D1 via a second try/catch. The D1 write failure is only warn-logged, not re-thrown. The promise passed to waitUntil never rejects — Workers wouldn't surface an unhandled rejection from waitUntil in any useful way. Pattern is sound.

Double-sync risk: none identified

The admin publish-approve step 14 (sync_nemar) calls syncDatasetToNemar directly and runs synchronously in-request. The POST /webhooks/publish-version-doi endpoint is a separate webhook (triggered by GitHub Actions CI). No workflow file in the repo fires this webhook as part of the publish pipeline — it's documented as a "fallback for PR/patch scenarios." The two paths cannot race for the same dataset+version event.

no_doi log branch absent from EZID handler (webhooks.ts:483-489)

The EZID decision handler has if ... no_credentials and else if ... openneuro branches but no else if ... no_doi branch. Comment correctly notes EZID always returns a non-null DOI on success, so no_doi is impossible for EZID. But the Zenodo handler has all three branches for consistency. Minor asymmetry — if the predicate's return type is ever extended or the EZID return type loosened, the missing branch would silently drop the log. Low priority.

Test NEMAR_USERNAME "missing" actually tests empty string (test/version-doi-sync-trigger.test.ts:27-35)

The test description says "skips when NEMAR_USERNAME is missing" but the test value is "" (empty string). The !input.nemarUsername falsy check covers both null, undefined, and "", so the behavior is correct. But the test description is slightly misleading — the "missing password" test (line 37-45) tests null, which is the more common production scenario for an unconfigured env var. Suggest renaming the username test to "skips when NEMAR_USERNAME is empty string" or testing undefined instead.

Test coverage is real — no mocks used

All 7 tests exercise the exported shouldSyncToNemarAfterVersionDoi pure function directly with real inputs. No mocking, no stubs, no fake Bun.serve harness. The comment is honest that waitUntil effects cannot be integration-tested without a live Worker runtime — this is accurate and not a shortcut. Passes the project "no mocks" rule.


False positives / Intentional behaviors

  • no_doi only in Zenodo handler: Intentional — EZID always mints a DOI on success; the type DoiResult guarantees doi: string. The asymmetry is correct.
  • Sync placed outside if (zenodoRepoName) block: Intentional — the Zenodo sync correctly runs unconditionally (not gated on zenodoRepoName), since runDatasetSync does its own repo checks.

Mergeability

Merge after addressing the xx-prefix gap and the redundant && published.doi guard. The waitUntil pattern is sound, error persistence is correct, tests are real, and the core bug fix (Zenodo branch not syncing) is accurate. The xx-prefix issue causes noisy D1 failure records for sandbox publishes but is non-fatal; the redundant guard is dead code. Both are worth fixing before merge.

- Add xx-prefix (sandbox) to shouldSyncToNemarAfterVersionDoi skip list;
  sandbox datasets pass the gate, causing false-alarm failed sync rows
- Remove redundant && published.doi guard on the Zenodo trigger: true
  already guarantees versionDoi is non-null via the no_doi predicate;
  align with EZID caller which doesn't repeat this check
- Add sandbox log line to the Zenodo else branch for consistency
- Add backend/test/version-doi-sync-trigger.test.ts covering all
  decision paths including the new sandbox case (10 tests)
@neuromechanist

Copy link
Copy Markdown
Contributor Author

Review findings addressed (commit a6aa426)

Important #1: xx-prefix gap in shouldSyncToNemarAfterVersionDoi

Added xx-prefix to the skip list in shouldSyncToNemarAfterVersionDoi. Sandbox datasets now return { trigger: false, reason: "sandbox" } before reaching the DOI check, preventing them from reaching syncToNemarAfterVersionDoi and writing false-alarm nemar_sync_status='failed' rows. Also added a matching log line in the Zenodo else branch.

New test file: backend/test/version-doi-sync-trigger.test.ts — 10 tests covering all decision paths (normal nm, missing creds, OpenNeuro, sandbox, missing DOI) including the credentials-before-prefix priority ordering.

Important #2: Redundant && published.doi guard on Zenodo trigger

Removed the redundant && published.doi guard. When trigger: true, the no_doi predicate in shouldSyncToNemarAfterVersionDoi already guarantees versionDoi is non-null. Used non-null assertion (published.doi!) to satisfy TypeScript and added a comment explaining the guarantee. Now consistent with the EZID path which does if (ezidSyncDecision.trigger) without a second guard. Also simplified else if (!zenodoSyncDecision.trigger) to a plain else since it is the only remaining branch.

All 46 backend tests pass.

@neuromechanist
neuromechanist merged commit 9706067 into dev May 14, 2026
8 checks passed
@neuromechanist
neuromechanist deleted the feature/issue-339-version-sync branch May 14, 2026 04:45

This branch was previously deployed

1 inactive deployment
npm — a6aa426d Deployed May 14, 2026 by neuromechanist via publish #805
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