Skip to content

fix(cli): don't re-pay for an already-complete chunk-aligned partial - #1622

Merged
alpergundogdu merged 1 commit into
mainfrom
claude/focused-shamir-ce2fb3
Aug 5, 2026
Merged

alpergundogdu merged 1 commit into
mainfrom
claude/focused-shamir-ce2fb3

Conversation

@alpergundogdu

@alpergundogdu alpergundogdu commented Aug 5, 2026 •

Copy link
Copy Markdown
Contributor

Problem

A money bug in the CLI paid-pull resume loop (fetch_blob_streaming), affecting decdn fetch — and decdn bundle pull once it shares that path.

When a .partial is exactly complete and the blob's total_bytes is an exact multiple of the 16 KiB chunk-group size, a resume re-fetches and re-pays for the entire blob it already holds:

  1. resume_offset(existing_len) rounds down to a chunk-group boundary. For an exact-multiple size there is no ragged final group, so byte_offset == total_bytes.
  2. The node's serve range gate refuses byte_offset >= total_bytes with RangeNotSatisfiable, which deliberately collapses to NotFound on the wire (indistinguishable from a foreign/absent blob).
  3. The client reads that NotFound as a stale partial, rewinds to byte_offset = 0, and set_len(0) truncates the complete, already-paid file — then re-fetches and re-pays for the whole blob.

Non-exact-multiple sizes are fine: they resume near the end and pay only for the trailing partial group.

Fix

In the restart branch, before rewinding to zero, hash the on-disk .partial against the target hash H. If it is the blob, sync and promote it for free (0 bytes, 0 pay); only a genuine miss falls through to the rewind. The whole-file hash resolves exactly the "your offset is past the end" vs "I don't have this blob" ambiguity the restart path could not otherwise distinguish.

Placement note

The reviewer suggested checking before opening the pull. This PR places the check inside the restart branch instead. A pre-open check would hash the whole .partial on every resume, including ordinary incomplete ones — doubling hashing cost on large-file resumes (e.g. a 700 MB resume would hash ~1.2 GB). The restart-branch placement fires only on the rare ambiguous NotFound; an ordinary incomplete resume's open succeeds and never reaches it, so it adds zero hashing to the common path. The only trade is one already-failed (no-money) round-trip in the rare complete-partial case.

Test

Adds journey 5 to cli_fetch_resume.rs: a complete .partial for a chunk-group-aligned blob must finish with zero further payment.

  • Before fix: billed 131520 extra bytes for a blob already fully on disk (the aligned-size re-pay bug) — the full wire cost of re-fetching the 128 KiB aligned blob.
  • After fix: all 5 journeys pass (13.9s).

Verification

  • e2e cli_fetch_resume (anvil-backed): RED → GREEN, journeys 1–4 unchanged
  • cargo clippy -p decdn-cli and -p decdn-e2e --features anvil-e2e --tests: clean
  • cargo fmt --check: clean
  • 549 unit tests (decdn-cli + decdn-client-pull): pass

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes

    • Improved resumable downloads when an existing partial file is already complete.
    • Complete files are now verified and promoted without unnecessary downloading or additional charges.
    • Existing behavior for incomplete or invalid partial files remains unchanged.
  • Tests

    • Added coverage for complete partial files, including files aligned to resume boundaries.

A complete `.partial` whose blob size is an exact multiple of the 16 KiB
chunk-group size makes `resume_offset` snap to `byte_offset ==
total_bytes` (it rounds down to a group boundary and there is no ragged
final group to land below it). The node's range gate refuses `byte_offset
>= total_bytes` with `RangeNotSatisfiable`, which collapses to the same
wire `NotFound` a foreign/absent partial draws. `fetch_blob_streaming`
read that as a stale partial, rewound to zero, and `set_len(0)`
truncated the complete, already-paid file — re-fetching and re-paying for
the whole blob. Only exact-multiple-of-16-KiB sizes hit it; a ragged
final group resumes fine.

Before rewinding, hash the on-disk `.partial` against the target: if it
is already the blob, sync and promote it for free (0 bytes, 0 pay); only
a genuine miss falls through to the rewind. The whole-file hash resolves
exactly the "past the end" vs "don't have it" ambiguity the restart path
could not otherwise tell apart. Placed in the restart branch (not before
the open) so it fires only on this rare ambiguous refusal and adds no
hashing to an ordinary incomplete resume, whose open succeeds and never
reaches it.

`decdn fetch` benefits now; `decdn bundle pull` inherits the fix once it
shares `fetch_blob_streaming`.

Adds journey 5 to the resume e2e: a complete partial for a
chunk-group-aligned blob must finish with zero further payment.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Copilot AI lite review requested due to automatic review settings August 5, 2026 16:38
@coderabbitai

coderabbitai Bot commented Aug 5, 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: 60697477-d3a0-431c-8aeb-da9a800c8dbf

📥 Commits

Reviewing files that changed from the base of the PR and between 2bd4878 and c30837f.

📒 Files selected for processing (2)
  • crates/cli/src/commands/fetch.rs
  • crates/e2e/tests/cli_fetch_resume.rs

Walkthrough

The fetch command now detects complete partial files after ambiguous resume refusals, verifies their hash, and promotes them without refetching. The end-to-end test covers an aligned blob whose complete partial file requires a zero-offset completion.

Changes

Aligned partial fetch

Layer / File(s) Summary
Verify and promote complete partial files
crates/cli/src/commands/fetch.rs
The fetch command reuses the partial length for the resume offset. It hashes ambiguous partial files and promotes matching complete files without refetching.
Test aligned complete partial promotion
crates/e2e/tests/cli_fetch_resume.rs
The test creates and registers an aligned blob, seeds a complete partial file, verifies promotion and output contents, and confirms that no additional bytes are billed.

Estimated code review effort: 3 (Moderate) | ~20 minutes


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.

@github-actions

github-actions Bot commented Aug 5, 2026

Copy link
Copy Markdown

Coverage report

Total: 80.30% (was 80.33%, ▼ 0.03%) — 57160/71180 lines

File Coverage Lines Δ
crates/cli/src/commands/fetch.rs 39.38% 584/1483 ▼ 0.59%
History
Commit Total Δ vs base
c30837f (HEAD) 80.30% ▼ 0.03%

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

Fixes a paid-pull resume edge case in the CLI streaming fetch path (fetch_blob_streaming) where a .partial file that is already complete and chunk-group-aligned could be misclassified as stale, truncated, and re-downloaded (re-paying for already-held bytes). The fix adds a targeted whole-file hash verification only on the rare ambiguous NotFound restart path, and the e2e suite gains a regression journey to ensure zero additional billing for this scenario.

Changes:

  • In fetch_blob_streaming, when a resumed open fails with an ambiguous “stale partial” signal, verify whether the on-disk .partial already hashes to the target; if so, sync and promote without rewinding or paying.
  • Add an anvil-backed e2e regression (“journey 5”) covering the exact-multiple-of-chunk-group complete-partial case and asserting zero additional billed bytes.

Reviewed changes

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

File Description
crates/cli/src/commands/fetch.rs Adds a restart-branch whole-file verification to avoid truncating/promoting re-fetch for already-complete, aligned .partial files.
crates/e2e/tests/cli_fetch_resume.rs Adds a new end-to-end journey to reproduce and prevent the aligned-size re-pay bug by asserting 0 additional billed bytes.

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

@alpergundogdu
alpergundogdu merged commit aecf783 into main Aug 5, 2026
26 checks passed
@alpergundogdu
alpergundogdu deleted the claude/focused-shamir-ce2fb3 branch August 5, 2026 16:45
alpergundogdu added a commit that referenced this pull request Aug 6, 2026
…edStore

The resume journeys seeded a raw .partial (old model); the gap-driven fetch
resumes only from its sidecar record. Add ClientRangedStore::seed_checkpointed_prefix
(test-util) and rewrite the journeys to construct a real checkpointed on-disk
state: resume-saves, corrupt-caught-by-finalize-sweep, foreign-no-wedge, and the
#1622 complete-partial free-finish, all under new-model semantics.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
alpergundogdu added a commit that referenced this pull request Aug 7, 2026
…ver (P3-client of #1621) (#1631)

* feat(client-pull): BlobSource + Funder + Pacer axes for the gap-driven driver

Defines the substrate-agnostic driver axes (#1608): BlobSource (dumb raw-bao
source), Funder (injected top-up seam), Pacer/BudgetPacer (pure draw/top-up/wait
decision). Scripted test doubles + pure BudgetPacer unit tests. No PeerSource or
driver yet.

Co-Authored-By: Claude Opus 4.8 <[email protected]>

* feat(client-pull): PeerSource BlobSource + bounded byte_len pull requests

open_progressive_pull now threads byte_len into StreamRequest so a middle gap
[offset,len) can be requested (wire already carried the field). PeerSource wraps
the paid progressive pull behind BlobSource (Reader=PullReader, finish drains to
the voucher watermark). Existing callers pass byte_len=0 (to-end, unchanged).

Co-Authored-By: Claude Opus 4.8 <[email protected]>

* feat(client-pull): ClientRangedStore streaming ingest for the gap driver

Consumes a gap's raw bao stream into positioned .partial + accumulating .obao4 at
O(chunk-group) memory, checkpointing present-ranges durably (fsync-before-record)
so a mid-gap fault resumes without re-paying the received prefix. Evolves
decode_to_sink: Leaf written positioned, Parent persisted to the outboard.

Co-Authored-By: Claude Opus 4.8 <[email protected]>

* feat(client-pull): gap-driven drive() — pull and pay for only missing_ranges

drive(store, source, pacer, funder, ...) fills each gap of missing_ranges(R)
via a paced PeerSource pull streamed through ingest_stream, folding the resume/
top-up/settle-wait/reseed loop behind the Pacer+Funder axes. Held ranges are
read locally, never pulled, never re-paid. Money-assertion test: opened ranges
== the gaps exactly.

Co-Authored-By: Claude Opus 4.8 <[email protected]>

* feat(cli): route decdn fetch through the gap-driven driver over ClientRangedStore

decdn fetch now pulls only missing_ranges(R): the single-cursor stream loop is
replaced by drive() over a ClientRangedStore + PeerSource + BudgetPacer + a CLI
Funder. Channel context is shared (interior-mutable) so the driver's top-up/reseed
and the source's pull see one state. Whole-file resume re-hash dropped (the store
bao-verifies every byte + valid_ranges sweep). Held ranges never re-paid.

Co-Authored-By: Claude Opus 4.8 <[email protected]>

* docs(client-pull): correct ingest checkpoint re-pay-window doc; dedup settle-wait constants

The mid-gap-fault re-pay window is up to one 4 MiB checkpoint interval (~4 default
voucher intervals) x MAX_RESUME_ATTEMPTS, not "under one interval" — checkpointed
bytes are still never re-paid. Relocate MAX_TOPUP_SETTLE_WAITS/TOPUP_SETTLE_BACKOFF
to client-pull so drive() and bundle pull's legacy loop share one definition.

Co-Authored-By: Claude Opus 4.8 <[email protected]>

* fix(client-pull): don't force a 15s settle-wait after every top-up

BudgetPacer returned Wait whenever awaiting_settle, and fill_gap set it after
every top-up, so the driver slept the full settle budget (30x500ms) before
retrying. Retry the open immediately after a top-up; the bounded settle-wait now
fires only when a re-open actually fails resume_may_be_stale, matching the legacy
CLI loop.

Co-Authored-By: Claude Opus 4.8 <[email protected]>

* test(e2e): adapt cli_fetch_resume to the new sidecar-based ClientRangedStore

The resume journeys seeded a raw .partial (old model); the gap-driven fetch
resumes only from its sidecar record. Add ClientRangedStore::seed_checkpointed_prefix
(test-util) and rewrite the journeys to construct a real checkpointed on-disk
state: resume-saves, corrupt-caught-by-finalize-sweep, foreign-no-wedge, and the
#1622 complete-partial free-finish, all under new-model semantics.

Co-Authored-By: Claude Opus 4.8 <[email protected]>

* fix(client-pull): bill the credit-window tail — payment-aware completion + paid-frontier resume

The node delivers the whole blob within its credit window (ADR 003/037), so
ingest_stream checkpoints bytes the vouchers haven't paid for yet. BudgetPacer
returned Done on delivered>=requested, stopping before the delivered-but-unpaid
tail was billed (under-pay). Done now requires PAID(wire) >= the range's wire
cost; an unpaid tail tops up and re-opens at content_paid_frontier(leg_start,
total, paid_wire) to re-deliver and bill it, terminating when paid catches up.
A fresh/cross-invocation resume (paid_wire==0) still resumes from the disk
frontier with no re-pay.

Co-Authored-By: Claude Opus 4.8 <[email protected]>

---------

Co-authored-by: Claude Opus 4.8 <[email protected]>
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.

2 participants