Skip to content

fix(cli): dedupe bundle-pull entries by hash + guard part dir cross-process (#1306, #1303) - #1336

Merged
thiras merged 2 commits into
mainfrom
fix/bundle-pull-dedup-flock
Jul 21, 2026
Merged

thiras merged 2 commits into
mainfrom
fix/bundle-pull-dedup-flock

Conversation

@thiras

@thiras thiras commented Jul 20, 2026 •

Copy link
Copy Markdown
Contributor

Two bundle-pull correctness bugs surfaced by the #1301 review, batched into one PR.

#1306 — --no-keep-blobs re-pays for a file duplicated at two bundle paths

A bundle may hold one file's content at two paths (ManifestEntry carries path + hash; nothing dedupes by hash). Under --no-keep-blobs the first entry deleted its parts before the second ran, so the second re-fetched — and re-paid for — every chunk (a 100 GB file at two paths cost 200 GB), with nothing in the output saying so.

Fix: bundle pull now fans out over distinct blob hashes (group_by_hash → HashGroup). fetch_group fetches/pays once; the first writable destination gets the materialized bytes and every other is a hard link (or a copy on EXDEV) via link_or_copy_atomic. Covers raw and DECDNMAN entries alike, in both retention modes. Skip-existing/--overwrite stay per-destination (plan_slots). Deduped duplicates report a new Linked outcome so fetched/total_bytes count only paid pulls.

#1303 — chunk part directories unguarded across processes

The per-manifest lock #1301 added was process-local, so two decdn invocations sharing a data dir still raced the shared part directory. file_manifest::reconstruct now holds a rustix exclusive flock on a persistent <downloads_root>/<hex>.lock — a sibling of the part dir, never inside it, so --no-keep-blobs cleanup can't take the lock target and reopen the race. The part dir is (re)created inside the locked region so a concurrent holder's cleanup can't strand a waiter on ENOENT mid-fetch.

Notes

  • Dedup (in-process) + flock (cross-process) jointly retire the process-local manifest_locks — a net simplification.
  • New workspace dep: rustix (features fs), already present transitively.
  • Docs: appendix-bundles.md § Pull gains a dedup note; ADR 012 § Blob retention is unchanged (dedup reconstructs once).

Review

This branch went through a 5-agent review (code, tests, silent-failure, types, comments). Key items fixed before opening:

  • High: flock was acquired after create_dir_all(part_dir) — a concurrent --no-keep-blobs holder could remove the dir mid-window, causing a spurious ENOENT after already paying for a chunk. Now created inside the lock.
  • Important: added the Linked outcome so the summary never implies a double payment; extracted plan_slots + injectable materialize_group and added direct tests for the "fetched once / linked rest" invariant, the retry path, and the per-destination skip matrix.
  • Type/doc polish: Slot::Write { label, dest }, HashGroup carrying the shared hash, precise per-distinct-blob wording.

Verification

351 tests pass (10 new/updated), clippy -D warnings, fmt --check, rustdoc -D warnings, and cargo deny check all clean.

Closes #1306
Closes #1303

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features

    • decdn bundle pull now fetches shared blobs only once and reuses them across destinations.
    • Duplicate destinations are materialized using hard links when possible, or copies when necessary.
    • Added linked-destination counts to human-readable and JSON reports.
  • Bug Fixes

    • Improved atomic handling when creating linked or copied files, preventing partially written destinations.
    • Skip and overwrite behavior is now evaluated independently for each destination.
    • Improved reliability when reconstructing files concurrently.
  • Documentation

    • Updated bundle pull behavior and coverage descriptions to reflect blob-level deduplication.

…rocess (#1306, #1303)

Two bundle-pull correctness bugs from the #1301 review, batched.

- **#1306** — a bundle may hold one file's content at two paths (`ManifestEntry`
  carries `path` + `hash`, nothing dedupes by hash). Under `--no-keep-blobs` the
  first entry deleted its parts before the second ran, so the second re-fetched —
  and **re-paid for** — every chunk (a 100 GB file at two paths cost 200 GB), with
  nothing in the output saying so. `bundle pull` now fans out over *distinct blob
  hashes* (`group_by_hash` → `HashGroup`): `fetch_group` fetches/pays once, the
  first writable destination gets the materialized bytes, and every other is a
  hard link (or copy, on `EXDEV`) via `link_or_copy_atomic`. Covers raw and
  `DECDNMAN` entries alike, in both retention modes. Skip-existing/`--overwrite`
  stay per-destination (`plan_slots`). Deduped duplicates report a new `Linked`
  outcome so `fetched`/`total_bytes` count only paid pulls.

- **#1303** — the per-manifest lock #1301 added was process-local, so two `decdn`
  invocations sharing a data dir still raced the shared part directory.
  `file_manifest::reconstruct` now holds a `rustix` exclusive `flock` on a
  persistent `<downloads_root>/<hex>.lock` (a sibling of the part dir, never
  inside it, so `--no-keep-blobs` cleanup can't take the lock target and reopen
  the race). The part dir is (re)created *inside* the locked region so a
  concurrent holder's cleanup can't strand a waiter on `ENOENT` mid-fetch.

Dedup (in-process) + flock (cross-process) jointly retire the process-local
`manifest_locks`. New workspace dep: `rustix` (features `fs`), already transitive.
Docs: `appendix-bundles.md` § Pull dedup note; ADR 012 retention unchanged.

Verified: 351 tests pass (10 new/updated), clippy `-D warnings`, fmt, rustdoc
`-D warnings`, and `cargo deny` all clean.

Closes #1306
Closes #1303

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Copilot AI review requested due to automatic review settings July 20, 2026 22:04
@coderabbitai

coderabbitai Bot commented Jul 20, 2026 •

Copy link
Copy Markdown

Review Change Stack

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Free

Run ID: d70b754a-2c8e-4c0a-9455-8abf985f391f

📥 Commits

Reviewing files that changed from the base of the PR and between a07954f and aa43988.

📒 Files selected for processing (1)
  • crates/cli/src/commands/file_manifest.rs

Walkthrough

bundle pull now groups entries by distinct blob hash, fetches each blob once, and links or copies it to duplicate destinations with per-destination overwrite handling. Reporting tracks linked outcomes, while manifest reconstruction uses persistent cross-process locking.

Changes

Bundle pull deduplication and reconstruction locking

Layer / File(s) Summary
Cross-process reconstruction lock
crates/cli/src/commands/file_manifest.rs
Reconstruction creates the downloads root, acquires a persistent exclusive lock, creates parts inside the critical section, and verifies lockfile persistence during cleanup.
Deduplicated materialization and destination planning
crates/cli/src/commands/bundle_pull.rs, adr/appendix-bundles.md
Entries sharing a hash are grouped; destinations are planned independently; fetched bytes are atomically written, hard-linked, or copied; ADR behavior and coverage language reflect distinct-blob processing.
Grouped execution and reporting
crates/cli/src/commands/bundle_pull.rs
Bounded execution processes hash groups, removes manifest-level locking, preserves provider serialization, and reports linked destinations separately from fetched bytes.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant User
  participant bundle_pull
  participant fetch_group
  participant file_manifest
  participant Filesystem
  User->>bundle_pull: run bundle pull
  bundle_pull->>fetch_group: process ordered distinct-hash groups
  fetch_group->>fetch_group: plan writable and skipped destinations
  fetch_group->>file_manifest: reconstruct manifest blob when needed
  file_manifest->>Filesystem: acquire persistent reconstruction lock
  fetch_group->>Filesystem: fetch and write canonical bytes once
  fetch_group->>Filesystem: link or copy duplicate destinations
  bundle_pull-->>User: report fetched, linked, skipped, and failed outcomes
Loading

Note

🎁 Summarized by CodeRabbit Free

Your organization is on the Free plan. CodeRabbit will generate a high-level summary and a walkthrough for each pull request. For a comprehensive line-by-line review, please upgrade your subscription to CodeRabbit Pro by visiting https://app.coderabbit.ai/login.

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

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request optimizes bundle pulling by grouping manifest entries by their blob hash, ensuring duplicate blobs are fetched and paid for only once and then hard-linked or copied locally. It also introduces cross-process advisory locking using rustix to serialize concurrent reconstructions sharing a data directory. The review feedback correctly identifies that the use of Unix-specific imports (std::os::fd::OwnedFd) and APIs in file_manifest.rs will break compilation on Windows, and suggests using cross-platform alternatives like std::fs::File alongside target-gating for the locking logic.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes two correctness issues in decdn’s bundle pull path: (1) duplicate bundle entries that reference the same blob hash no longer trigger duplicate paid fetches, and (2) chunk-reconstruction part directories are now guarded across processes (not just within one invocation) to prevent cross-process races.

Changes:

  • Group bundle manifest entries by distinct blob hash so each blob is fetched/paid once, then materialized to multiple destinations via hard-link (or copy fallback), with reporting that distinguishes Fetched vs Linked.
  • Add a cross-process lock around DECDNMAN reconstruction using a persistent lockfile in the downloads root, and move part-dir creation inside the locked region.
  • Update documentation to reflect per-distinct-blob behavior and dedup semantics.

Reviewed changes

Copilot reviewed 5 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
crates/cli/src/commands/bundle_pull.rs Dedup by hash, add link/copy materialization, new Linked outcome and reporting updates
crates/cli/src/commands/file_manifest.rs Add cross-process lockfile + flock guarding reconstruction and part-dir lifecycle
crates/cli/Cargo.toml Add rustix dependency for filesystem locking
Cargo.toml Add workspace rustix dependency (fs feature)
Cargo.lock Lockfile updates for rustix inclusion
adr/appendix-bundles.md Document dedup-by-hash semantics and per-distinct-blob node selection/concurrency

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@github-actions

github-actions Bot commented Jul 20, 2026 •

Copy link
Copy Markdown

Coverage report

Total: 81.72% (was 81.72%, ▲ +0.01%) — 51373/62862 lines

File Coverage Lines Δ
crates/cli/src/commands/bundle_pull.rs 57.07% 545/955 ▲ +10.49%
crates/cli/src/commands/file_manifest.rs 93.25% 539/578 ▼ 0.12%
History
Commit Total Δ vs base
a07954f 81.72% ▲ +0.01%
aa43988 (HEAD) 81.72% ▲ +0.01%

Review (Gemini, Copilot): the #1303 lock used `std::os::fd::OwnedFd` +
`rustix::fs` flock, which is Unix-only and would break the
`x86_64-pc-windows-msvc` release build (release.yml builds decdn-cli →
decdn.exe on windows-latest). PR CI is Linux-only, so it passed.

Switch `acquire_part_lock` to `std::fs::File::lock` — stable since Rust
1.89 (MSRV here is 1.95), a cross-platform OS advisory lock (flock on
Unix, LockFileEx on Windows). This gives a *real* lock on every release
target rather than a Windows stub, and needs **no** third-party crate:
the `rustix` direct dep is dropped entirely (workspace + cli), leaving
the Cargo.lock change a net no-op vs main. The returned File is the lock
guard; the OS frees it on drop/exit, so no staleness bookkeeping.

Verified: 351 tests pass, clippy -D warnings, fmt, rustdoc -D warnings,
cargo deny all clean; cli no longer depends on rustix/fs4.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@thiras
thiras merged commit b84a9d0 into main Jul 21, 2026
23 checks passed
@thiras
thiras deleted the fix/bundle-pull-dedup-flock branch July 21, 2026 00:03
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.

--no-keep-blobs re-pays for a file duplicated at two bundle paths Chunk part directories are unguarded across processes

2 participants