Skip to content

fix(node): top up reused buyer channels + drop redundant progressive-open timeout (#1146, #1147) - #1211

Merged
thiras merged 2 commits into
mainfrom
fix/node-pull-path-topup-and-open-timeout
Jul 15, 2026
Merged

thiras merged 2 commits into
mainfrom
fix/node-pull-path-topup-and-open-timeout

Conversation

@thiras

@thiras thiras commented Jul 15, 2026 •

Copy link
Copy Markdown
Contributor

Batch fix for two contained correctness bugs in the node's node-to-node cache-miss pull path (same NodeOrigin flow), following the repo's "fix batch" pattern. No contract changes. Clear of in-flight PRs #1204 (PaymentChannel contract) and #1209 (origin-replay/zero-address).

#1146 — reused buyer channels are never topped up

A buyer channel opened for a provider was reused verbatim on every miss pull and never re-funded, so once its deposit spent down, pull-through to that provider silently degraded forever.

  • On reuse below a 20% low-water mark, the node fires a detached, per-provider-singleflighted topUp (plan_refill → spawn_refill_if_low, deduped via RefillSlot/topups_in_flight) and hands the current pull its channel immediately — the refill never blocks the non-serializing reuse fast path. A fully-drained channel heals on the next pull.
  • refill_amount / LOW_WATER_DIVISOR promoted into decdn-client-pull as the single source of truth shared with the CLI fetch auto-refill.
  • New metrics decdn_buyer_topup_ok_total / _failure_total. top_up now returns DepositOutcome; the escrowed-but-untracked outcomes (UnknownProvider/ChannelMismatch) are metered as failures, so stranded deposits are visible to an operator alert.

#1147 — progressive-open stage was bounded twice

The open was wrapped in a redundant outer tokio::time::timeout(pull_timeout, …) and bounded internally by deadlines.open inside open_stream. Both sourced from pull_timeout, so the double-bound only risked drift — a future open-specific knob could make one candidate cost up to 2× pull_timeout while outer_pull_deadline budgets 1×, starving the MAX_PROVIDER_ATTEMPTS fallback loop.

  • Dropped the outer wrap; the open is now bounded solely by deadlines.open, matching the buffered path (stream_fetch_shared).
  • Classification/metering preserved: the inner path emits the same PullTimeout { after: deadlines.open } sentinel → classify_pull_failure exonerates the peer and meters node_pull_timeout.

Tests / verification

  • cargo clippy --workspace --all-targets → clean (-D warnings)
  • cargo nextest (node + client-pull + cli) → 1126 passed
  • New: plan_refill branch tests (trigger / headroom / already-expired / no-channel / dedup), refill_decision target-clamping incl. min_deposit-wins, claim_refill_slot dedup + free-on-drop. #1147 single-bound behavior remains covered by node_origin_pull.rs.

Applied fixes from a multi-agent PR review (metric misclassification, poisoned-lock self-heal, branch-test coverage, comment accuracy). The full on-chain success path is exercised by the existing anvil_settlement_e2e harness.

Closes #1146
Closes #1147

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features

    • Reused buyer payment channels now receive automatic background top-ups when deposits fall below the low-water threshold.
    • Refill activity is tracked with success and failure metrics.
    • Concurrent refill requests are coordinated to avoid duplicate top-ups.
  • Bug Fixes

    • Deposit operations now report their persisted outcome for clearer handling.
    • Pull opening now uses the configured operation deadline consistently, improving timeout behavior.

…open timeout (#1146, #1147)

Two contained correctness fixes in the node's node-to-node pull path.

#1146: a buyer channel opened for a provider was reused verbatim on every
cache-miss pull and never topped up, so once its deposit spent down,
pull-through to that provider silently degraded forever. On reuse below a 20%
low-water mark, the node now fires a detached, per-provider-singleflighted
`topUp` (via `plan_refill`/`spawn_refill_if_low` + `RefillSlot`) and hands the
current pull its channel immediately — the refill never blocks the
non-serializing reuse fast path. `refill_amount`/`LOW_WATER_DIVISOR` are promoted
into `decdn-client-pull` as the single source of truth shared with the CLI
auto-refill. Adds `decdn_buyer_topup_ok_total` / `_failure_total`; the escrowed-
but-untracked `top_up` outcomes are metered as failures so stranded deposits are
visible.

#1147: the progressive-pull open stage was bounded twice — a redundant outer
`tokio::time::timeout(pull_timeout, …)` wrap plus the inner `deadlines.open`
bound inside `open_stream`. Both were sourced from `pull_timeout`, so the
double-bound only risked drift (an open-specific knob could make one candidate
cost up to 2x while `outer_pull_deadline` budgets 1x, starving the fallback
loop). Dropped the outer wrap; the open is now bounded solely by `deadlines.open`,
matching the buffered path. Classification/metering (`PullTimeout` →
`node_pull_timeout`, peer exonerated) is preserved by the inner sentinel.

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

coderabbitai Bot commented Jul 15, 2026 •

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@thiras, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 46 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Free

Run ID: 5442a306-2fd1-4dd6-bae9-cb5a481c3004

📥 Commits

Reviewing files that changed from the base of the PR and between a903a15 and 8b6f43a.

📒 Files selected for processing (2)
  • crates/node/src/buyer_channel.rs
  • crates/node/src/metrics.rs

Walkthrough

The change centralizes buyer-channel refill policy, returns persisted deposit outcomes, and adds non-blocking deduplicated low-water refills with metrics and tests. CLI integrations consume the shared policy. Progressive OPEN now uses its typed deadline without an outer timeout wrapper.

Changes

Buyer channel low-water refill

Layer / File(s) Summary
Top-up outcome and refill policy
crates/client-pull/src/buyer_channel.rs
top_up returns DepositOutcome; shared low-water and saturating refill helpers replace the CLI-local policy and are covered by unit tests.
Background refill orchestration
crates/node/src/buyer_channel.rs, crates/node/src/metrics.rs
Reused channels schedule deduplicated detached refills, track outcomes with metrics, and validate planning, slot ownership, and concurrency behavior.
CLI and end-to-end integration
crates/cli/src/commands/fetch.rs, crates/e2e/tests/cli_fetch_topup.rs
CLI fetch uses the shared refill helpers and explicitly ignores the returned top-up outcome in the end-to-end flow.

Progressive OPEN timeout

Layer / File(s) Summary
Direct progressive OPEN timeout
crates/node/src/node_origin.rs
Candidate opening awaits open_progressive_upstream directly, using its typed deadline for existing failure classification.

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

Sequence Diagram(s)

sequenceDiagram
  participant Reuse as Channel reuse
  participant Service as BuyerChannelService
  participant Store as BuyerChannelStore
  participant TopUp as top_up
  participant Metrics
  Reuse->>Service: reuse live buyer channel
  Service->>Store: read deposit state
  Service->>Service: plan and claim low-water refill
  Service->>TopUp: ensure allowance and submit refill
  TopUp-->>Service: DepositOutcome
  Service->>Metrics: record success or failure
  Service-->>Reuse: return channel context without waiting
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.

@github-actions

Copy link
Copy Markdown

Gas snapshot

No gas changes vs base.

@github-actions

github-actions Bot commented Jul 15, 2026 •

Copy link
Copy Markdown

Contracts coverage

Line coverage: 88.97% (2500/2810 lines)
▲ +0.00pp vs base (88.97% → 88.97%)
Run: 29447751003

@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 centralizes the channel auto-refill logic (the refill_amount function and LOW_WATER_DIVISOR constant) into the shared decdn-client-pull crate, allowing it to be reused by both the CLI and the node. It introduces a background, non-blocking low-water top-up mechanism for reused buyer channels in the node's fast-path reuse flow, complete with per-provider task deduplication, RAII-based slot management, and dedicated metrics tracking. Additionally, it removes a redundant timeout wrapper around progressive upstream opens in node_origin.rs. There are no review comments, so I have no feedback to provide.

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

Fixes two correctness issues in the node-to-node cache-miss pull path: (1) reused buyer channels now get best-effort low-water refills so pull-through doesn’t silently degrade once deposits are spent down, and (2) removes a redundant progressive-open timeout so per-candidate budgeting can’t drift/double-bound.

Changes:

  • Add detached, per-provider-deduped low-water buyer-channel top-ups on reuse, plus success/failure metrics.
  • Promote the refill policy (low-water divisor + refill decision kernel) into decdn-client-pull as shared source of truth, and adapt CLI/e2e call sites to top_up returning DepositOutcome.
  • Drop the outer progressive-open tokio::time::timeout, relying on deadlines.open inside the shared open_stream path.

Reviewed changes

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

Show a summary per file
File Description
crates/node/src/node_origin.rs Removes redundant progressive-open timeout wrapper; relies on deadlines.open internal bound.
crates/node/src/metrics.rs Adds buyer top-up ok/failure counters and increment helpers for the new background refill path.
crates/node/src/buyer_channel.rs Implements low-water refill planning + per-provider singleflighted detached top-ups on channel reuse.
crates/e2e/tests/cli_fetch_topup.rs Updates test call site to ignore top_up’s returned DepositOutcome.
crates/client-pull/src/buyer_channel.rs Changes top_up to return DepositOutcome; adds shared low-water refill constants/kernel + tests.
crates/cli/src/commands/fetch.rs Removes local refill policy duplication; imports shared refill kernel and adapts to top_up return type.

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

Comment thread crates/node/src/metrics.rs Resolved
Comment thread crates/node/src/buyer_channel.rs Resolved
@github-actions

github-actions Bot commented Jul 15, 2026 •

Copy link
Copy Markdown

Coverage report

Total: 80.69% (was 80.71%, ▼ 0.02%) — 47244/58548 lines

File Coverage Lines Δ
crates/cli/src/commands/fetch.rs 41.88% 258/616 ▼ 3.96%
crates/client-pull/src/buyer_channel.rs 45.87% 100/218 ▲ +16.29%
crates/node/src/buyer_channel.rs 53.19% 976/1835 ▲ +2.52%
crates/node/src/metrics.rs 86.15% 1120/1300 ▼ 0.40%
crates/node/src/node_origin.rs 89.38% 791/885 ▲ +0.53%
History
Commit Total Δ vs base
a903a15 80.72% ▲ +0.01%
8b6f43a (HEAD) 80.69% ▼ 0.02%

…en failure-metric doc

Copilot review follow-ups on #1211:

- Reuse fast path no longer does a second `store.get_by_provider`: the low-water
  decision reads `deposit`/`prior_amount` off the `ChannelContext` the reuse read
  already built. `plan_refill` now takes those values (and `refill_decision` is
  value-based), dropping its store-read + expiry branches — expiry / no-channel are
  already enforced upstream by the reuse gate (`try_reuse_live`), so
  `spawn_refill_if_low` is only reached for a live channel. Tests updated to drive
  the decision by value (removes the now-unreachable expired / no-channel cases).
- `buyer_topup_failure` method doc now notes it also covers the escrowed-but-
  untracked `DepositOutcome` outcomes, matching the field doc.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@thiras
thiras merged commit 5c2611f into main Jul 15, 2026
22 checks passed
@thiras
thiras deleted the fix/node-pull-path-topup-and-open-timeout branch July 15, 2026 20:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants