Skip to content

Show step and s3-lock progress in publish approve - #464

Merged
neuromechanist merged 4 commits into
devfrom
feature/issue-284-publish-progress
May 14, 2026
Merged

neuromechanist merged 4 commits into
devfrom
feature/issue-284-publish-progress

Conversation

@neuromechanist

Copy link
Copy Markdown
Contributor

Closes #284.

What changed

The nemar admin publish approve command no longer renders a static
"Running publication workflow..." spinner for the entire 17-step run.
Two new layers of visibility were added so admins can tell a long S3
lock from a stuck Worker:

  1. Step-level progress. The spinner now reads
    Step 14/17: s3 lock and advances as the orchestrator reports a
    new step in its response. The canonical step list is exported
    from src/lib/api.ts (PUBLICATION_STEPS) and mirrors the
    backend's allSteps array, so the CLI and the dataset publish status display can no longer drift out of sync. The previous
    status display claimed "all steps complete" while the backend was
    still running enrichment_check, version_doi, or sync_nemar —
    that gap is closed.

  2. S3-lock batch progress. On the first s3_lock call (no
    continuation token), the backend now counts the dataset's
    objects/ prefix once via getDatasetS3Stats, returns it as
    s3_lock_total, and the CLI threads it back on every follow-up
    request so the count sweep is bounded to one invocation. Each
    response also returns s3_lock_batch_count (objects locked in
    that batch). The CLI sums them and renders
    Locking S3 objects: 1240/4963 (25.0%). Batch size is unchanged
    (out of scope per the issue).

Test plan

  • Added test/publish-progress.test.ts exercising the new
    stepIndexFor helper, the PUBLICATION_STEPS contract, the
    multi-page s3_lock running total, and the s3_lock_total
    round-trip from response to the next request body. Tests run against
    a real Bun.serve fake backend (no mocks).
  • bun run typecheck (root + backend) clean.
  • bun run build clean (580 modules bundled).
  • Targeted suites pass:
    bun test test/publish-progress.test.ts test/publish-retry-classifier.test.ts test/publish-workflow.test.ts test/admin-restructure.test.ts
    (66 passes, 0 fails).

@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@PR464

Version: 0.8.14-PR464.7596e63
Commit: 7596e63


🤖 Updated on each push.

@neuromechanist

Copy link
Copy Markdown
Contributor Author

PR Review: #464 — Show step and S3-lock progress in publish approve

Overall the implementation is clean and well-reasoned. Comments are unusually thorough and the test fake-server approach correctly avoids mocks. The step-list drift issue it fixes is real and the one-count-per-stream design is correct in principle. A few concrete issues worth addressing before merge.


Critical

s3_lock_batch_count not returned in the final (non-paging) s3_lock batch (backend/src/routes/admin.ts):

The diff adds s3_lock_batch_count: lockResult.locked to the hasMore: true path and the 500-error path, but when lockResult.hasMore === false && lockResult.failed.length === 0 the code falls through to await updateProgress("s3_lock") and then continues to the next step. The final batch's locked-object count is never returned. The CLI accumulates s3_lock_batch_count from responses, so the last page never gets counted. For a 4963-object dataset locked in pages of 1000, the spinner would read 3963/4963 (79.8%) at "complete" — off by one full batch. The test in the diff masks this by including s3_lock_batch_count: 20 in the third (final, non-hasMore) fake response, but the real backend would not return it.

Fix: add s3_lock_total: s3LockTotal, s3_lock_batch_count: lockResult.locked to the fall-through path before (or inline in) the final overall success response. Alternatively, return an explicit final s3_lock response similar to the hasMore path with hasMore: false so the CLI always gets the count.


Important

s3LockLocked resets to 0 on outer retry (Worker timeout mid-s3_lock) (src/lib/api.ts):

s3LockLocked is initialised to 0 at function scope and accumulates within one pass of the outer for loop. If a Worker times out during s3_lock and the outer retry fires, the new attempt starts with s3LockLocked = 0 and the continuation token threaded back. The display will show 0/4963 (0.0%) → 1000/4963 (20.2%) instead of picking up where it left off. This is cosmetic for a one-time timeout but misleading when the dataset has e.g. 50 pages and the Worker budget is typically 3–4 pages. Given that the feature's primary goal is giving visibility into a long operation, showing a counter that resets is a regression vs. showing no counter.

A pragmatic fix: when s3_lock_total is known and s3_lock_continuation_token is being echoed back, initialise s3LockLocked to s3_lock_total - remaining_objects (not easily known) or, simpler, just note in the spinner text "Resumed — locked N more" after a retry so the display is honest rather than wrong.

getDatasetS3Stats runs inside the Cloudflare Worker subrequest budget (backend/src/routes/admin.ts):

getDatasetS3Stats issues one ListObjectsV2 per 1000 objects (ceil(N/1000) subrequests). For a 7000-object dataset this is 7 additional LISTs on top of the applyObjectLockBatch budget. Cloudflare Workers have a 50-subrequest cap per invocation (higher on paid plans). The PR comment correctly notes "~7 LISTs for 6500 objects", but this runs on the same Worker invocation as the first lock batch (~1000 PUTs). For datasets large enough that s3_lock is split across many pages — exactly the datasets this progress bar is for — the count sweep could push the first invocation over the limit. Consider: only count if body.s3_lock_total is absent AND no continuation token, and cap the counting LISTs at, say, 10 (with a note that the total is approximate if truncated).


Suggestions

stepIndexFor fallback does not reset across retries (src/lib/api.ts line ~1401):

The comment in stepIndexFor says "fall back to stepsCompleted.length + 1 for unknown steps". If a future backend adds a step the CLI doesn't know, the fallback index grows with steps_completed and may exceed PUBLICATION_STEPS.length. The clamping via Math.min handles the overflow but the step index display could jump around (e.g. 17/17 while only 10 steps done). This is unlikely but the test for "clamps fallback" would catch it. No action required — existing test covers it.

Comment at backend/src/routes/admin.ts line 2547 still lists only 15 steps (pre-existing, not introduced by this PR):

The JSDoc block above approveSchema lists steps 1–15 but allSteps now has 17. enrichment_check (step 2) and version_doi (step 13) are omitted. Minor but confusing when cross-referencing. Could be updated here.

Test "emits a progress event on completion with the final step" asserts events.length >= 1 but doesn't assert all expected steps (test/publish-progress.test.ts):

The test verifies the last event is notify_user at position 17, which is the important contract. However, with only 4 steps in step_results, an implementation that only emits one event (the last) would pass. Consider asserting that the unique steps emitted match the step_results list, or at least that events.length === 4 (one per step transition).


Nice-to-haves

  • renderProgress in src/commands/admin.ts could live in src/lib/api.ts alongside PublishProgressInfo since it's pure formatting logic over the public type. Keeping it in the command file is fine for now but a future second consumer would need to duplicate it.
  • The test beforeEach guard (if (fake) { void fake.stop(); fake = null; }) uses void to discard the promise instead of awaiting it. Since Bun.serve.stop(true) is synchronous with true (force), this is safe, but is worth a comment.

False positives / intended behaviour

  • The two-list approach (backend allSteps + CLI PUBLICATION_STEPS) is noted as "must stay in sync" — this is inherent given the Worker/CLI deployment split. A shared TS module would require the CLI to import backend code, which is structurally worse. The existing lockout test (PUBLICATION_STEPS list is the expected 17 backend steps) is the right mitigation.
  • upload_to_zenodo appears in PUBLICATION_STEPS as a legacy no-op; this is correct and intentional.

Backend was returning s3_lock_total and s3_lock_batch_count on the
hasMore=true and error paths but not on the final non-paging success
path. The CLI accumulates s3_lock_batch_count from every response so
the last batch was never counted — progress read short by one full
page (e.g. 3963/4963 at "complete" for a 4963-object dataset).

Fix: capture s3LockFinalTotal/s3LockFinalBatchCount before calling
updateProgress("s3_lock") and include them in the final c.json
response. Also update the test comment to make the contract explicit
so the fixture can no longer mask this class of bug.
Two Important review findings addressed:

1. Spinner reset on Worker retry: add s3LockResumed flag to
   PublishProgressInfo. When an outer retry fires while the continuation
   token is set (mid-s3_lock), subsequent progress events carry
   s3LockResumed=true and renderProgress appends "(resumed)" to the
   spinner line. The locked counter (s3LockLocked) is kept at function
   scope across retries so the display shows accumulated progress rather
   than appearing to restart from zero.

2. Subrequest budget for getDatasetS3Stats: add maxPages parameter that
   caps the LIST sweep at N pages. The s3_lock path in admin.ts passes
   MAX_COUNT_LISTS=20 (covers up to 20000 objects). If the dataset is
   larger, objectCount returns undefined and the CLI degrades to a
   running count without a denominator — correct behaviour rather than
   risking "Too many subrequests" on the first Worker invocation. All
   callers updated to handle objectCount: number | undefined.
@neuromechanist

Copy link
Copy Markdown
Contributor Author

Review findings addressed

Critical — fixed

s3_lock_batch_count not returned on final (non-paging) success path

Before: the hasMore=false/no-failure path fell through to updateProgress("s3_lock") and then the overall success c.json, which did not include s3_lock_total or s3_lock_batch_count. The CLI accumulates those fields from every response, so the last batch was never counted — for a 4963-object dataset the progress would read 3963/4963 at "complete."

After: s3LockFinalTotal and s3LockFinalBatchCount are captured in outer-scoped variables just before updateProgress("s3_lock"), then added to the final c.json response unconditionally. The test comment was updated to make the contract explicit (the fixture must include these fields because the real backend now returns them).

Commit: 028ec83

Important #1 — fixed

Counter misleading on Worker retry mid-s3_lock

Added s3LockResumed?: boolean to PublishProgressInfo. The outer retry loop sets s3LockIsResumed = true when s3_lock_continuation_token is set at the time of failure (indicating we were mid-stream). Subsequent s3_lock progress events carry s3LockResumed: true and renderProgress appends "(resumed)" to the spinner line. The locked counter (s3LockLocked) is already at function scope so it persists across retries — the display shows the true running total.

Important #2 — fixed

getDatasetS3Stats subrequest budget on large datasets

Added maxPages?: number parameter to getDatasetS3Stats. Returns objectCount: undefined when the cap is hit. The s3_lock path in admin.ts passes MAX_COUNT_LISTS=20 (covers up to ~20 000 objects). For larger datasets the CLI degrades to a running count without a denominator. All callers updated to handle objectCount: number | undefined — dataset-metadata-columns.ts, dataset-reindex.ts, nemar-sync.ts, webhooks.ts, and the two admin.ts call sites.

Commit: 7596e63

All 9 publish-progress tests pass

bun test test/publish-progress.test.ts — 9 pass, 0 fail

@neuromechanist
neuromechanist merged commit 172cd54 into dev May 14, 2026
2 checks passed
@neuromechanist
neuromechanist deleted the feature/issue-284-publish-progress branch May 14, 2026 05:15

This branch was previously deployed

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