Skip to content

perf: idempotent atomic parallel PMC downloads - #165

Merged
SkyeAv merged 1 commit into
net/agent-fetchfrom
net/parallel-downloads
Sep 15, 2026
Merged

SkyeAv merged 1 commit into
net/agent-fetchfrom
net/parallel-downloads

Conversation

@SkyeAv

@SkyeAv SkyeAv commented Sep 15, 2026

Copy link
Copy Markdown
Owner

Makes PMC article file downloads idempotent, atomic, and bounded-parallel, so reruns cost nothing for already-fetched files and a kill never leaves a torn file. Previously every file downloaded sequentially, single-attempt, on every rerun.

Download contract

  • Idempotency: a non-empty existing file is skipped without any request; a zero-length file (torn prior attempt) re-downloads.
  • Atomicity: downloads write a .part sibling and os.replace it into place on completion; the .part is removed even on KeyboardInterrupt, so no half-written file survives.
  • Parallelism: fetch_pmc_article fetches up to net.DEFAULT_FETCH_CONCURRENCY (8) files concurrently via a ThreadPoolExecutor, collecting results in submission order.
  • Fail-fast validation: concurrency < 1 raises ValueError at the top of fetch_pmc_article, before any network call (kept in _download_files too, as defense in depth).

Design

  • Worst-case wall clock (documented in fetch_pmc_article's docstring): 3 serial listing/metadata calls + ceil(N/8) waves, each paying ≤ 60 s of sleeps — ≤ 300 s at the observed N ≈ 15, about 6% of the 90-minute per-article budget; a healthy run pays zero added time.

Testing

  • uv run pytest tests/test_agent_net.py -q --no-cov -n 012 passed in 1.28s: skip-without-request, torn-file re-download, atomic replace + .part cleanup, deterministic submission-order under reverse completion (threading events, no sleeps), in-flight bound via simultaneous-worker counters, concurrency < 1, first-failure propagation (two scenarios separated by monkeypatch.undo())
  • All tests < 0.03 s — no wall-clock assertions anywhere
  • Full gate on the stack tip: uv run pytest -q1565 passed, 15 skipped; ruff/format/pyright clean; Tier-2 review passed all 11 criteria

@coderabbitai

coderabbitai Bot commented Sep 15, 2026

Copy link
Copy Markdown

Important

  • 🔍 Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Advanced

Run ID: 5fec2659-092c-4549-bbc8-e7348682301b


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@SkyeAv
SkyeAv added this pull request to stack #170 September 15, 2026 17:40
@SkyeAv SkyeAv changed the title perf: [US-003] idempotent atomic parallel PMC downloads perf: idempotent atomic parallel PMC downloads Sep 15, 2026
@SkyeAv
SkyeAv marked this pull request as ready for review September 15, 2026 18:07
@SkyeAv
SkyeAv force-pushed the net/parallel-downloads branch from 5fab0f0 to 9686eb9 Compare September 15, 2026 18:19
fetch_pmc_article now fetches files through a bounded ThreadPoolExecutor (concurrency=8, keyword-only) instead of a serial loop, skips non-empty destinations with no HTTP request so a rerun of a killed attempt costs only the missing files, re-downloads zero-length torn writes, and writes every file via .part + os.replace with .part cleanup on failure. Results keep exact S3 listing order and the first failing key's error propagates, so downstream table binding and existing offline tests are unchanged. Invalid pool sizes fail loudly before any network call.
@SkyeAv
SkyeAv force-pushed the net/parallel-downloads branch from 9686eb9 to 3d6ce81 Compare September 15, 2026 18:41
@SkyeAv
SkyeAv merged commit face509 into main Sep 15, 2026
5 checks passed
SkyeAv added a commit that referenced this pull request Sep 15, 2026
Cut 19.0.0 and bump the package version in pyproject.toml, CITATION.cff,
and uv.lock.

Major: five breaking changes ship. The `tablassert` console command now
requires the optional `[cli]` extra, since `cyclopts` and `rich` left the
base install (#187). Level-one normalization redefines fullmap keys as
cleaned Unicode-lowercase, Porter2-normalized, byte-ordered token sets and
moves the database to schema v6, so every schema-v5 fullmap is rejected and
must be rebuilt (#171, #172, #174, #175, #176). `build-fullmap --aria2c`
is gone -- aria2c is used automatically whenever the `[aria2]` extra is
installed (#178) -- and `--taxon-allowlist` is gone because the built-in
top-100 experimental-taxon allowlist now applies to every build, guarding
both reuse paths by `META.taxon_allowlist` identity (#153). Logging now
requires the `[log]` extra and is fully disabled without it (#152).

Features: `tablassert.net` is a new stdlib-only transient/permanent
classification and retry seam shared by the agent and the BABEL downloader
(#163, #164), with one bounded jittered retry layer across the inner agent,
reflexion, and judge (#167) and machine-readable `error_code` values on
skipped checkpoint records (#166). `build-kg` gained an automatic ephemeral
shared-prefix TCode cache keyed by content-addressed XXH64 op digests
(#155-#159, #161). The `--distill` corpus became schema-uniform v2 with a
sibling `outcomes.ndjson`, a deterministic `RewardConfig`-tunable reward,
and the new zero-dependency `tablassert distill-weigh` command (#180-#183).
`CLASS_FIELD_OVERRIDES` grants the DAKP sparse qualifier stack on the pinned
association classes (#188).

Fixes: `distill-export` partitions its corpus by content and unions the
schema across record files, so a mixed v1/v2 directory no longer CastErrors
or silently stringifies a column (#184); BABEL retry warnings survive the
loguru sink; PMC downloads are idempotent, atomic, and bounded-parallel
(#165); reflexion and judge prompts are hard-bounded (#168).

Changelog:
- versioned the Unreleased section as 19.0.0 - 2026-09-15
- merged three duplicate `### Added` blocks into one
- moved the two `BREAKING:` entries from `### Changed` into
  `### Breaking Changes` and added a `**Migration:**` note to all five
- added PR links to all 17 entries, which carried none
- added the missing entries: the distill v2 corpus/reward/distill-weigh
  stack (#180-#183), the distill-export schema-drift fix (#184), the
  TCode run-cache detail (#155-#161), and the README badge removal (#154)

Docs: none needed here. Every shipped doc change landed with its own PR,
and the docs source-of-truth gate passes against the bumped tree.

Testing:
- make check -> exit 0
- uv run ruff check . -> All checks passed!
- uv run pyright -> 0 errors, 0 warnings, 0 informations
- uv run pytest -> 1622 passed, 52 skipped
- cargo test --manifest-path rust/Cargo.toml -> 156 passed, 0 failed
- cargo clippy --all-targets -- -D warnings -> clean
- uv lock --check -> resolved 169 packages, lock current
- uv run mkdocs build --strict -> exit 0
- docs SSOT + CLI coverage after the changelog edit -> 204 passed
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