Skip to content

Cross-invocation retry for approve, drop subrequest blowout - #383

Merged
neuromechanist merged 2 commits into
devfrom
feature/issue-382-approve-cli-retry
Apr 28, 2026
Merged

neuromechanist merged 2 commits into
devfrom
feature/issue-382-approve-cli-retry

Conversation

@neuromechanist

Copy link
Copy Markdown
Contributor

Closes #382 (follow-up to #340 / #369).

Summary

PR #369 made the publish orchestrator retry transient failures inside a single Worker invocation. That fixed some GitHub propagation 5xx, but introduced three new failure modes:

  1. s3_lock blew the CF subrequest limit. Each applyObjectLock batch fires ~40 S3 PUTs + listObjectKeys pagination. The 3-attempt inline retry tripled that and reliably hit "Too many subrequests by single Worker invocation" — every re-run failed identically.
  2. tag_protect halted on "Repository has been locked". GitHub briefly locks a repo right after a visibility flip; 1s × 3 inline retries never outlasted the lock window.
  3. publish_doi saw a stale ezid_identifier. doi_create updated D1 but the in-memory dataset snapshot loaded at orchestrator start stayed null, so publish_doi in the same invocation threw "No EZID identifier found".

Original goal ("failures don't stop the process") was right; the implementation was wrong. Move retry across invocations, where each attempt gets a fresh subrequest budget and a real propagation window — exactly what an admin doing it by hand was already doing.

Changes

Backend (backend/src/routes/admin.ts)

  • Drop step-level withRetry on s3_lock and tag_protect. s3_public_read, doi_create, create_tag, create_release keep their wrappers — their per-call subrequest cost is small and idempotent.
  • Sync the in-memory dataset (concept_doi, doi_provider, ezid_identifier/ezid_status, zenodo_concept_id, is_sandbox) right after doi_create writes to D1, so subsequent steps in the same invocation see fresh values.

CLI (src/lib/api.ts, src/commands/admin.ts)

  • approvePublication now wraps the existing request loop in a 5-attempt × 10s retry gated on transient errors:
    • retried: status 0 (network), 429, 5xx, 403 with body matching repository has been locked
    • not retried: CI failure 422, validation 400, auth 401/403, 404 — these don't fix themselves with time
  • s3_lock_offset is preserved across retries; useResume flips to true after the first attempt so already-completed steps stay skipped.
  • New onRetry callback surfaces [!] <step> failed: <err> and Retrying in 10s (attempt N/5)… through the CLI spinner.

Test plan

Out of scope

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
… 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.
@github-actions

github-actions Bot commented Apr 28, 2026 •

Copy link
Copy Markdown

📦 Package Preview

Your package is ready to test!

bun install -g nemar-cli@PR383

Version: 0.8.4-PR383.cbc282d
Commit: cbc282d


🤖 Updated on each push.

@neuromechanist

Copy link
Copy Markdown
Contributor Author

/review-pr findings + responses

Ran pr-review-toolkit:review-pr with four agents in parallel. Pushed cbc282d to address actionable findings.

Addressed (cbc282d)

code-reviewer #1 (important): step in onRetry always undefined.
Confirmed: orchestrator returns step at the top level of the error JSON, but request() only attached data.details to ApiError, so apiErr.details was undefined for every step failure and the spinner always fell back to the literal string "step". Fix: added optional step?: string to ApiError, wired request() to populate it from data.step, and approvePublication now reads apiErr.step directly.

silent-failure-hunter #2 (high): Final-failure context loss.
After 5 retries, only lastError was thrown; the accumulated step timeline was discarded. Now attached to the thrown ApiError as stepResults. The CLI handler in src/commands/admin.ts prints "Step timeline (last attempt):" with the per-step status/duration on final failure.

silent-failure-hunter #3 (medium): Duplicated step_results on success.
A step that failed-then-succeeded across a retry boundary appeared twice in the success summary. Added dedupeStepResults() that keeps the latest entry per step. Applied on both the success and final-failure paths.

pr-test-analyzer #1 (criticality 9): Classifier needs unit tests.
Exported isRetryablePublishError as a pure predicate. Added test/publish-retry-classifier.test.ts with 23 cases: 5xx/429/network retried, "Repository has been locked" 403 retried (case-insensitive), plain 4xx/auth 403 not retried, non-ApiError inputs not retried, boundary checks at 499/600. No mocks. All pass.

Discussed but not changed

silent-failure-hunter #6 (claimed CRITICAL): D1 UPDATE failure in doi_create could leak DOIs.
Real risk for Zenodo (createDeposition is not idempotent), but Zenodo upload is currently disabled in the orchestrator (upload_to_zenodo step is a no-op). EZID is the active provider and createEzidConceptDoi is idempotent on "already exists" — re-runs fetch the existing reservation rather than minting a new one. Pre-existing concern, not introduced by this PR. Worth a separate issue if Zenodo is ever re-enabled, but blocking this PR on it would scope-creep beyond the original retry/subrequest fix.

code-reviewer #2 (important): repo_public budget verification.
Verified: s3_public_read keeps withRetry (1 GET + 1 PUT = max 6 subrequests), create_tag/create_release/doi_create each keep withRetry and are individually cheap. s3_lock (~40 PUTs + paginated LIST × 3) was the only blowout. The set is internally consistent.

comment-analyzer: All 6 added comments verified accurate against the code (githubFetchWithRetry does retry 5xx + 404-on-flag, applyObjectLock does count 403 as success, classifier JSDoc matches predicate exactly). No drift.

Test results

bun test: 811 pass / 36 skip / 4 fail. The 4 failures are pre-existing unrelated tests (#366, #367, #368) plus one flaky network timeout in manifest.test.ts that passes on re-run.

@neuromechanist
neuromechanist merged commit 868c98a into dev Apr 28, 2026
8 checks passed
@neuromechanist
neuromechanist deleted the feature/issue-382-approve-cli-retry branch May 11, 2026 05:15

This branch was previously deployed

1 inactive deployment
npm — cbc282dd Deployed Apr 28, 2026 by neuromechanist via publish #671
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