Skip to content

Retry transient/propagation failures in approve pipeline - #369

Merged
neuromechanist merged 2 commits into
devfrom
feature/issue-340-resilient-approve
Apr 27, 2026
Merged

neuromechanist merged 2 commits into
devfrom
feature/issue-340-resilient-approve

Conversation

@neuromechanist

Copy link
Copy Markdown
Contributor

Closes #340

Summary

The approval pipeline aborts on transient propagation failures from GitHub (tag protection, ref/tree/blob reads after a write) and from EZID/DataCite (5xx during DOI minting). These are recoverable: a retry seconds later succeeds. Current behavior forces an admin to babysit each approval and re-run resume.

This PR adds retry at two layers:

1. githubFetchWithRetry in backend/src/services/github.ts

Internal helper that retries 5xx/429/network always. Optionally retries 404 (retryOn404: true) only where the caller knows the resource exists — i.e. propagation lag is the only plausible cause. Wired into:

  • getTreeAtRef (ref + tree fetch) — propagation 404 retried
  • getBlobContent — propagation 404 retried (blob SHA came from a tree we just resolved)
  • getMainBranchSha — propagation 404 retried
  • getFileContent — no 404 retry (404 is a valid 'file missing' signal returned as null)
  • applyTagProtection — propagation 404 retried (rulesets endpoint can briefly 404 right after a repo visibility flip)

2. Step-level retry in backend/src/routes/admin.ts

  • tag_protect wrapped with withRetry. applyTagProtection already treats 422 (rule exists) as success, so retry is naturally idempotent.
  • doi_create wrapped with withRetry, gated on provider === \"ezid\". EZID DOIs are deterministic (buildConceptIdentifier is a pure function of datasetId), and createEzidConceptDoi already handles 'already exists' by fetching the existing record — so retry is safe. Zenodo's createDeposition is not idempotent (each call mints a fresh deposition); it stays unwrapped.
  • Replaced the misleading 'DOI creation is NOT idempotent' comment with one that explains the EZID-vs-Zenodo split.
  • Widened isRetryable regex from `/(?:http|status)\s*(5\d\d|429)\b/i` to `/(?:http|status)[\s(](?:error\s\()?\s*(5\d\d|429)\b/i` so it also matches EZID-style "EZID HTTP error (503 ...)" messages, not just GitHub-style "HTTP 503". Without this, EZID 5xx propagation errors silently bypassed retry.

Out of scope

BIDS validation CI failures still surface as 4xx and are not retried — they are real dataset problems that don't resolve with time, matching the issue's distinction.

Test plan

Pre-existing test failures filed as separate issues

Closes #340

EZID DOI minting, GitHub ref reads, and tag-protection rule application
all suffer from short propagation windows during the approval pipeline.
These are not real failures, but they currently abort approval and force
the admin to re-run the resume flow.

- backend/src/services/github.ts: add githubFetchWithRetry (3 attempts,
  1s delay). Retry 5xx/429/network always; retry 404 only when the
  caller knows the resource exists (just-written ref, blob from a
  resolved tree, ruleset endpoint after a repo flip). Wire into
  getTreeAtRef, getBlobContent, getMainBranchSha, getFileContent
  (no 404 retry; 404=null is intended), applyTagProtection.
- backend/src/routes/admin.ts: wrap tag_protect step with withRetry.
  Wrap doi_create with withRetry, gated on provider==='ezid' because
  EZID IDs are deterministic and createEzidConceptDoi already handles
  'already exists' by fetching the existing record. Zenodo's
  createDeposition is not idempotent and stays unwrapped.
- backend/src/routes/admin.ts: widen isRetryable regex to also match
  EZID-style 'EZID HTTP error (503 ...)' messages, not just GitHub-style
  'HTTP 503'. Without this, EZID 5xx propagation errors silently
  bypassed the retry path.
@github-actions

github-actions Bot commented Apr 27, 2026 •

Copy link
Copy Markdown

📦 Package Preview

Your package is ready to test!

bun install -g nemar-cli@PR369

Version: 0.8.2-PR369.d00136a
Commit: d00136a


🤖 Updated on each push.

Reviewer findings:
- Regex match on '5xx anywhere in body' could mis-retry a 4xx whose body
  text happens to contain a 5xx code.
- applyTagProtection swallowed terminal failures by returning false, so
  outer withRetry never saw them and the operator-facing error lost the
  status + body context.
- doi_create did not plumb the retry attempt count to step telemetry.
- Retry helper was untestable in isolation.

Changes:
- backend/src/services/retry.ts: extract withRetry + isRetryable into
  their own module. Add HttpError class with numeric .status. isRetryable
  classifies HttpError / Error.status numerically first; the message
  regex is now a fallback for legacy errors only and never overrides a
  4xx that has been explicitly tagged.
- backend/src/services/github.ts: read helpers (getTreeAtRef,
  getBlobContent, getFileContent, getMainBranchSha) and applyTagProtection
  throw HttpError with the response status. applyTagProtection now
  returns void and throws on terminal failure with status + body
  snippet, surfacing operator-actionable diagnostics.
- backend/src/services/ezid.ts: ezidRequest throws HttpError on
  non-ANVL HTTP failures. EZID 5xx is now classified by status, not
  by string-matching 'EZID HTTP error (503 ...)'.
- backend/src/routes/admin.ts: import withRetry from the new module,
  drop the in-file copy. tag_protect step no longer checks a boolean
  return — relies on throw. doi_create plumbs attempts to updateProgress.
- test/retry.test.ts: 27 unit tests covering HttpError, status-tagged
  Error, network message classification, GitHub/EZID legacy regex
  fallback, dataset-id false-positive guard, retry control flow, and
  custom isRetryable. No mocks; pure functions only.
@neuromechanist

Copy link
Copy Markdown
Contributor Author

Review fix-up (commit d00136a)

Addressed all critical and important findings from the multi-agent review.

Fixed (critical/important):

  • Regex false positive (silent-failure-hunter Add proper test coverage measurement (90%+ target) #1): A 4xx whose body text contained a 5xx code could be mis-classified as retryable. Replaced with status-first classification: HttpError and Error.status are inspected numerically; the message-regex is now a fallback for legacy errors only and never overrides a tagged 4xx. Verified by does not retry when .status is 422 even if message contains 'HTTP 500' test.
  • applyTagProtection swallowed failures (silent-failure-hunter Optimize KV usage to stay within free tier limits #2): Now returns void and throws HttpError with status + body snippet on terminal failure, so outer withRetry actually retries it and the operator-facing error message carries diagnostic context.
  • doi_create attempt count dropped (code-reviewer Add proper test coverage measurement (90%+ target) #1): Plumbed attempts from withRetry to updateProgress, matching tag_protect. Step telemetry now reflects real retry counts on EZID 5xx propagation.
  • getTreeAtRef comment misleading (comment-analyzer): Softened "a tag we just wrote" to "callers that pass a ref they just created."

Added (test gap):

  • Extracted withRetry + isRetryable to backend/src/services/retry.ts with a new HttpError class.
  • 27 new unit tests in test/retry.test.ts. No mocks. Covers: HttpError classification, Error.status classification, network message detection, GitHub-style and EZID-style HTTP regex fallback, the nm000500 dataset-id false-positive guard, control flow (success-first, retry-then-success, no-retry-on-non-retryable, last-error-rethrow), and custom isRetryable override.

Wired through:

  • github.ts read helpers + applyTagProtection throw HttpError.
  • ezid.ts ezidRequest throws HttpError on non-ANVL HTTP failures.

Knowingly skipped (out of scope for #340):

Verified:

@neuromechanist
neuromechanist merged commit d0e4a8c into dev Apr 27, 2026
8 checks passed
@neuromechanist
neuromechanist deleted the feature/issue-340-resilient-approve branch April 27, 2026 21:26
neuromechanist added a commit that referenced this pull request Apr 28, 2026
Move the publish orchestrator's transient-failure retry from inside one
Worker invocation to across invocations driven by the CLI. PR #369's
inline withRetry on s3_lock made each call issue ~120 S3 PUTs in a
single invocation and reliably tripped Cloudflare's per-invocation
subrequest limit; on tag_protect the 1s x 3 inline delay never
outlasted GitHub's "Repository has been locked" window.

Backend (admin.ts):
- Drop withRetry on s3_lock and tag_protect; surface 500 immediately
- Sync the in-memory dataset after doi_create so publish_doi in the same
  invocation sees the freshly minted ezid_identifier instead of null
  ("No EZID identifier found" after a re-run)

CLI (api.ts, commands/admin.ts):
- approvePublication wraps the request loop in 5-attempt x 10s retry
  across fresh Worker invocations: each retry gets a fresh subrequest
  budget and a real propagation window. Retries are gated on transient
  errors (5xx/429, network, GitHub 403 "Repository has been locked");
  CI 422, validation 400, auth 401/403, 404 surface immediately.
- onRetry callback surfaces "[!] step failed... retrying in 10s
  (attempt N/5)" through the existing spinner.

Closes #382
neuromechanist added a commit that referenced this pull request Apr 28, 2026
* fix(approve): cross-invocation retry, drop subrequest blowout

Move the publish orchestrator's transient-failure retry from inside one
Worker invocation to across invocations driven by the CLI. PR #369's
inline withRetry on s3_lock made each call issue ~120 S3 PUTs in a
single invocation and reliably tripped Cloudflare's per-invocation
subrequest limit; on tag_protect the 1s x 3 inline delay never
outlasted GitHub's "Repository has been locked" window.

Backend (admin.ts):
- Drop withRetry on s3_lock and tag_protect; surface 500 immediately
- Sync the in-memory dataset after doi_create so publish_doi in the same
  invocation sees the freshly minted ezid_identifier instead of null
  ("No EZID identifier found" after a re-run)

CLI (api.ts, commands/admin.ts):
- approvePublication wraps the request loop in 5-attempt x 10s retry
  across fresh Worker invocations: each retry gets a fresh subrequest
  budget and a real propagation window. Retries are gated on transient
  errors (5xx/429, network, GitHub 403 "Repository has been locked");
  CI 422, validation 400, auth 401/403, 404 surface immediately.
- onRetry callback surfaces "[!] step failed... retrying in 10s
  (attempt N/5)" through the existing spinner.

Closes #382

* fix(approve): wire step into ApiError, dedupe results, add classifier tests

Address pr-review-toolkit findings on PR #383:

- ApiError now carries an optional `step` field, populated by request()
  from the orchestrator's top-level `step` JSON field. Previously the
  onRetry callback read step from `details`, which is `undefined` for
  every orchestrator failure (details is only populated by the s3_lock
  partial-fail path), so the spinner always printed the literal "step"
  instead of the actual step name.
- Dedupe accumulated step_results by step name on both success and
  final failure. Without this, a step that failed-then-succeeded across
  a retry boundary appears twice in the post-publication summary.
- On final failure (after all 5 attempts exhausted), attach the
  per-attempt step timeline to the thrown error so the CLI handler can
  show "which step failed when, and how many attempts each took"
  instead of just the final 500 message.
- Export isRetryablePublishError as a pure predicate and add 23 unit
  tests covering: 5xx/429/network retried, "Repository has been
  locked" 403 retried, plain 4xx/auth 403 not retried, non-ApiError
  inputs not retried, boundary checks at 499/600. No mocks, mirrors
  the test/retry.test.ts pattern.

This branch was previously deployed

1 inactive deployment
npm — d00136ae Deployed Apr 27, 2026 by neuromechanist via publish #643
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