Skip to content

fix(download): pre-count files+bytes for accurate progress (#346) - #373

Merged
neuromechanist merged 2 commits into
devfrom
feature/issue-346-download-progress
Apr 27, 2026
Merged

neuromechanist merged 2 commits into
devfrom
feature/issue-346-download-progress

Conversation

@neuromechanist

Copy link
Copy Markdown
Contributor

Summary

Fixes #346 — nemar dataset download/get showed 100% N/N files from the first file completion and reported different totals run-to-run on the same dataset.

Root cause

DownloadProgressTracker was constructed with filesTotal=0 and pinned the denominator to filesCompleted on each completion event, guaranteeing 100% display. Byte totals accumulated only from per-file total-size events as git-annex queried the S3 remote, so they varied run-to-run depending on which files git-annex had inspected when the user looked.

Fix

  • Pre-count pending files + bytes via new countPendingDownload() (wraps git annex find --not --in=here --json, sums the bytesize field). Returns null on failure so callers degrade gracefully.
  • Bytes-based percent when bytesTotal > 0 (more stable than file count when sizes vary wildly); falls back to file-based when only filesTotal is known; degraded mode (no totals) shows file count without a misleading bar.
  • Remove the filesTotal = filesCompleted fallback in processLine.
  • nemar dataset get now uses the streaming tracker (was spinner only) and short-circuits when nothing is pending.

Files

  • src/lib/progress.ts — tracker math, new setBytesTotal / getPercent / getProgress
  • src/lib/git-annex.ts — new countPendingDownload()
  • src/commands/dataset.ts — wire precount into download and get
  • test/progress.test.ts — 5 tests / 40 assertions

Test plan

  • bun test test/progress.test.ts — 5/5 pass (monotonic 0→100%, filesTotal-pin regression, degraded-mode null, byte-vs-file divergence, parser)
  • tsc --noEmit clean
  • biome check clean
  • bun run build clean
  • Manual: nemar dataset download nm000132 shows bar progressing monotonically 0% → 100% with byte total matching du -sh of annexed files

Acceptance criteria (from #346)

  • Bar progresses monotonically 0 → 100%
  • File count climbs alongside (not pinned to total)
  • Final byte total matches du -sh within rounding (precount uses authoritative bytesize from annex metadata)
  • Unit test covers tracker with fixed input sequence

DownloadProgressTracker was constructed with filesTotal=0 and pinned the
denominator to filesCompleted on each completion event, guaranteeing the bar
showed 100% from the first file. Byte totals also drifted run-to-run because
they accumulated only from per-file 'total-size' events.

- Add countPendingDownload() wrapping 'git annex find --not --in=here --json'
  to seed authoritative file count and total bytes before 'git annex get'.
- Tracker now prefers byte-based percent when bytesTotal>0, falls back to
  file-based when filesTotal>0, and renders no bar in degraded mode.
- Remove the filesTotal=filesCompleted fallback in processLine.
- 'nemar dataset get' now uses the streaming tracker (was spinner only) and
  short-circuits when nothing is pending.
- Add test/progress.test.ts: monotonic 0->100%, regression test for the
  fillsTotal pin, degraded-mode null percent, byte-vs-file divergence on
  uneven sizes, parser sanity.
Three high-confidence findings from /review-pr addressed:

1. Parallel-mode (-J N) byte misattribution (CRITICAL, code-reviewer):
   The tracker stored per-file state in single shared fields. With
   concurrent transfers git-annex interleaves --json-progress events on
   stdout, so file A's 'ok' would credit whichever file most recently
   emitted byte-progress (often a still-in-flight file B), pinning
   percent to 100% mid-download. Replace with Map<file,{transferred,
   total}> keyed on parsed.file. Match by file key on completion to
   credit only the completing file's bytes.

2. Short-circuit conflated 'nothing pending' with 'parse failure'
   (HIGH, silent-failure-hunter):
   countPendingDownload returned {0,0} when stdout had only non-JSON
   lines (warnings, locale info). The 'dataset get' command then
   printed 'All data files already present' and returned, hiding a
   real download. Now distinguish empty stdout (authoritative zero)
   from non-empty-but-unparseable (returns null -> degraded mode ->
   actual 'git annex get' runs).

3. Stderr discarded on non-zero exit (MEDIUM, silent-failure-hunter):
   Surface git-annex stderr behind VERBOSE env (matches existing
   pattern in update-check.ts/notices.ts) so a stale lock or other
   underlying defect is debuggable.

Tests:
- progress.test.ts: interleaved -J events, ok-without-progress for
  tiny files, more-ok-than-precount clamps at 100, fewer-ok stays
  below 100. (4 new tests, 9 total)
- git-annex-precount.test.ts: real git-annex repos verifying empty/
  non-annex/local-only/scoped/missing-files cases (5 tests). Follows
  the no-mock policy in .rules/testing.md and the pattern in
  test/datalad-commands.test.ts.
@neuromechanist

Copy link
Copy Markdown
Contributor Author

/review-pr findings + responses

Ran pr-review-toolkit (code-reviewer, pr-test-analyzer, silent-failure-hunter) in parallel. All high-confidence findings addressed in 973ca99.

Critical — parallel-mode byte misattribution (code-reviewer, conf 90)

The original tracker held currentFile, currentFileBytesTransferred, currentFileBytesTotal as single shared fields. With -J N git-annex interleaves --json-progress events from up to N concurrent files on stdout — so file A's ok:true would commit whichever file most recently emitted byte-progress (often still-in-flight file B), pinning percent to 100% mid-download. This regression of the very bug #346 fixes wasn't covered by the original serial-only tests.

Fix: Map<file, {transferred, total}> keyed by parsed.file. On ok:true, credit only the matching file's bytes. New regression test interleaved -J events credit bytes to the correct file covers it.

High — short-circuit conflated "nothing pending" with "parse failure" (silent-failure-hunter)

countPendingDownload returned {0,0} when git annex find exited 0 but emitted only non-JSON (warnings, locale info). nemar dataset get then printed "All data files already present" and returned — hiding a real pending download.

Fix: Distinguish empty-stdout (authoritative zero) from non-empty-but-unparseable (returns null → degraded mode → actual git annex get runs).

Medium — stderr discarded on non-zero exit (silent-failure-hunter)

Fix: Surface git annex find stderr behind VERBOSE=1 env, matching existing pattern in src/lib/update-check.ts / src/lib/notices.ts. No-op for the happy path; debuggable when needed.

Test coverage gaps (pr-test-analyzer)

Addressed in this PR:

  • countPendingDownload zero coverage (rating 9) → new test/git-annex-precount.test.ts, 5 real-annex tests (no mocks, follows test/datalad-commands.test.ts pattern).
  • More/fewer ok events than precount (rating 8) → 2 new unit tests in progress.test.ts.
  • ok-without-byte-progress for tiny files (rating 8) → new unit test.

Deferred (low value vs. complexity, would need network/fixtures):

  • End-to-end integration test through nemar dataset get CLI against a real S3 remote.
  • Parser shape matrix for percent-progress strings, action:"copy" lines.

Verification

  • bun test test/progress.test.ts test/git-annex-precount.test.ts → 14/14 pass (66 assertions)
  • tsc --noEmit → 0 errors
  • biome check → clean (one pre-existing warning in runCommand unrelated to this PR)
  • bun run build → clean

@github-actions

Copy link
Copy Markdown

📦 Package Preview

Your package is ready to test!

bun install -g nemar-cli@PR373

Version: 0.8.2-PR373.973ca99
Commit: 973ca99


🤖 Updated on each push.

@neuromechanist
neuromechanist merged commit fde0d06 into dev Apr 27, 2026
8 checks passed
@neuromechanist
neuromechanist deleted the feature/issue-346-download-progress branch April 27, 2026 22:19

This branch was previously deployed

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