Skip to content

feat(node): window-paced pull-through for regional proxy warming (#856) - #893

Merged
thiras merged 5 commits into
mainfrom
fix/856-window-paced-pull-through
Jun 14, 2026
Merged

thiras merged 5 commits into
mainfrom
fix/856-window-paced-pull-through

Conversation

@thiras

@thiras thiras commented Jun 13, 2026

Copy link
Copy Markdown
Contributor

Summary

Node-side window-paced pull-through (ADR 037 §Node serving, issue #856). A bonded node serves a cache-miss cdn/client/v1 request by fusing a progressive upstream pull with downstream delivery: each verified upstream chunk is forwarded to the paying client and tee'd into the local cache, and the pull pauses whenever the per-request unrecouped frontier (bytes_pulled − bytes_paid) reaches pull_ahead_bytes, resuming as vouchers clear. The node thereby becomes the first regional copy of the blob with per-request speculative exposure bounded to one window instead of the whole blob.

Three operator-configurable [cache] caps ship from day one:

  • pull_ahead_bytes — per-request pipeline window
  • max_unrecouped_leech_bytes — node-wide unrecouped-leech budget
  • pull_share_ratio_percent — per-peer share ratio

The seed-leech caps live in the new crate::leech_governor::LeechGovernor. A pre-flight deposit guard refuses a pull outright (signed NotFound) when the channel's remaining deposit cannot cover the worst-case cost.

Review hardening (this branch)

  • Critical: fixed a busy-spin in window_forward_loop that starved the tokio runtime when a seed-leech cap denied the speculative pull mid-stream with no interval to recoup — the loop now refuses (abandons the partial fill) rather than looping with no await point. Regression-guarded by a handler↔governor test that previously hung the runtime.
  • Governor now records pulled bytes the instant the upstream is paid (before the fallible tee/forward) so abandon paths cannot under-count speculative spend.
  • Deposit guard no longer fails open when max_blob_size_bytes == 0 (falls back to the per-request window cost).
  • Added node_pull_through_tee_finalize_failed and node_pull_through_upstream_verify_failed metrics on the two spend-bearing finalize arms.
  • Config rejects pull_ahead_bytes > max_unrecouped_leech_bytes when the global cap is enabled.

Testing

  • cargo clippy --workspace --all-targets — clean
  • cargo fmt --check — clean
  • cargo nextest run — 1159 passed, 0 failed

New behavioral tests: handler↔governor in-loop pause (busy-spin regression guard) and admission refuse, fused-path corrupt-upstream (not cached), and mid-window underpayment abandonment.

🤖 Generated with Claude Code

Implements the node-side serving behavior of ADR 037: a node serving a
cdn/client/v1 request for a blob it does not hold fills it by a fused
window-paced pull — forwarding each verified upstream chunk to the paying
client and teeing it into the cache — pacing upstream spend by the client's
vouchers so per-request speculative exposure is bounded to pull_ahead_bytes
rather than the whole blob. Node-wide abuse is bounded by the LeechGovernor
seed-leech caps (global unrecouped budget + per-peer share ratio), and a
pre-flight deposit guard refuses pulls a channel provably cannot pay for.

Includes review hardening:
- Fix a runtime-starving busy-spin when a seed-leech cap denies mid-stream
  with no interval to recoup: the loop now abandons rather than spinning
  with no await point.
- Charge pulled bytes to the governor the instant the upstream is paid,
  before the fallible tee/forward, so abandon paths cannot hide spend.
- Close the deposit-guard fail-open when max_blob_size_bytes is unbounded
  (fall back to the per-request window cost).
- Add tee-finalize-failed and upstream-verify-failed metrics on the two
  spend-bearing finalize arms.
- Reject pull_ahead_bytes > max_unrecouped_leech_bytes at config load.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Copilot AI review requested due to automatic review settings June 13, 2026 16:14

@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 implements the window-paced regional proxy warming and pull-through mechanism specified in ADR 037. It introduces a TeeSink to stream and incrementally verify chunks into the cache while forwarding them to clients, and adds a LeechGovernor to enforce global and per-peer speculative pull caps. Feedback on the changes highlights a potential Denial of Service (DoS) vulnerability in the LeechGovernor's eviction logic, where a linear scan of up to 65,536 peers to find the oldest entry runs in O(N) time and could lead to CPU exhaustion under high peer churn.

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.

Comment thread crates/node/src/leech_governor.rs Resolved

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

Implements node-side window-paced pull-through for cache-miss cdn/client/v1 requests (#856 / ADR 037), fusing progressive upstream pull with downstream delivery while bounding speculative exposure via a per-request window, a pre-flight deposit guard, and node-wide “seed-leech” caps.

Changes:

  • Add progressive upstream pull plumbing (open_progressive_pull / UpstreamPull) and a fused serve path that forwards verified chunks while teeing into the cache.
  • Introduce seed-leech governance (LeechGovernor) and operator-configurable caps (pull_ahead_bytes, max_unrecouped_leech_bytes, pull_share_ratio_percent) with validation and CLI visibility.
  • Add new metrics plus extensive end-to-end tests covering drop/underpay, insufficient deposit refusal, leech stall regression (busy-spin), and corrupt upstream behavior.

Reviewed changes

Copilot reviewed 21 out of 22 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
crates/node/src/handlers/client.rs Adds window-paced pull-through serve path, deposit guard, leech-governor integration, and related metrics wiring.
crates/node/src/node_origin.rs Adds progressive pull open path and NodeProgressivePull wrapper for bookkeeping + persistence/scoring.
crates/node/src/client_requester.rs Introduces progressive upstream pull (UpstreamPull) and open helper for header-first streaming.
crates/cache/src/engine.rs Adds tee-based cache fill (open_tee_sink / TeeSink) and shared stream-import/verify path.
crates/cache/src/lib.rs Re-exports tee-related types for node use.
crates/node/src/leech_governor.rs New in-memory governor enforcing global unrecouped budget + per-peer share ratio caps.
crates/node/src/metrics.rs Adds new counters for window pacing, leech caps, abandon paths, and finalize failures.
crates/node/src/runtime/mod.rs Wires window pull-through + leech governor into runtime when origin is available.
crates/node/src/runtime/reload.rs Updates resolved-config defaults for newly added cache cap fields.
crates/common/src/config/types.rs Adds new cache config knobs and docs.
crates/common/src/config/resolved.rs Adds resolved fields for new cache cap knobs.
crates/common/src/config/mod.rs Adds defaults + config validation (window must not exceed enabled global budget).
crates/cli/src/commands/config.rs Surfaces new cap values in config validation output and default config template.
crates/cli/tests/config_validate.rs Updates resolved-config fixture for new cache cap fields.
crates/node/tests/node_origin_pull.rs Adds end-to-end tests for window-paced pull-through behavior and regressions.
crates/node/tests/sighup_signal.rs Updates resolved-config fixture for new cache cap fields.
crates/node/src/lib.rs Exposes leech_governor module.
crates/node/Cargo.toml Adds blake3 dependency for incremental verification in progressive pulls.
crates/incentive/src/rate.rs Adds min_payment helper (ceil pricing) plus tests.
crates/incentive/src/lib.rs Re-exports min_payment.
Cargo.lock Locks new blake3 dependency.
adr/037-regional-proxy-warming.md Documents that #856 implements the ADR’s node-side window-paced pull-through.

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

Comment thread crates/node/src/handlers/client.rs Resolved
Comment thread crates/cache/src/engine.rs Resolved
Comment thread crates/cache/src/engine.rs Resolved
Comment thread crates/node/src/client_requester.rs Resolved
@github-actions

github-actions Bot commented Jun 13, 2026 •

Copy link
Copy Markdown

Coverage report

Total: 85.74% (was 85.74%, ▼ 0.01%) — 36982/43135 lines

File Coverage Lines Δ
crates/cache/src/engine.rs 91.80% 1869/2036 ▼ 0.74%
crates/cache/src/lib.rs 100.00% 28/28 ● 0.00%
crates/cli/src/commands/config.rs 66.38% 154/232 ▼ 0.87%
crates/common/src/config/mod.rs 96.84% 6065/6263 ▲ +0.01%
crates/common/src/config/resolved.rs 100.00% 35/35 ● 0.00%
crates/incentive/src/rate.rs 95.36% 144/151 ▲ +1.45%
crates/node/src/client_requester.rs 83.73% 427/510 ▼ 2.69%
crates/node/src/handlers/client.rs 76.67% 815/1063 ▲ +2.46%
crates/node/src/leech_governor.rs 94.59% 175/185 new
crates/node/src/metrics.rs 83.14% 789/949 ▼ 0.45%
crates/node/src/node_origin.rs 89.94% 456/507 ▼ 0.39%
crates/node/src/runtime/mod.rs 49.00% 882/1800 ▼ 0.16%
crates/node/src/runtime/reload.rs 95.19% 1444/1517 ▲ +0.03%
History
Commit Total Δ vs base
6cc2009 85.98% ▼ 0.04%
13a3367 86.00% ▼ 0.01%
f368a2b 85.99% ▼ 0.03%
be85e37 85.73% ▼ 0.02%
18e8744 (HEAD) 85.74% ▼ 0.01%

- leech_governor: replace the O(n) full-table eviction scan with an
  O(log n) BTreeMap LRU index (per gemini-code-assist) — preserves true
  LRU so an active peer is never the eviction victim, removing the CPU
  amplification surface under Sybil peer-churn once the table is full.
- client_requester: correct the `expected()` doc — it returns the
  remaining bytes (`total_bytes - byte_offset`), not `total_bytes`.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…urfacing, coverage)

Follow-up review fixes for window-paced pull-through (#856):

- ADR 037: correct the deposit-guard description — when max_blob_size_bytes
  is unbounded the guard falls back to the per-request window cost, it is NOT
  skipped (the code never fails open); clarify the look-ahead deferral wording.
- client.rs: surface a coalesced-fill `has()` store fault instead of silently
  reclassifying it as a miss; split a tee HashMismatch (forwarded/teed stream
  divergence — error) from a routine store fault (warn); log a failed
  success-path finish(); add channel/byte context to the verify-failure and
  seed-leech-throttle teardown logs (no in-band frame: the protocol's frozen
  StreamError domain split forbids a mid-stream delivery-fault code).
- leech_governor.rs: document that may_pull mutates LRU state despite its
  predicate-style name.
- tests: add window_pull_through_resumed_offset_falls_back_not_fused (the
  byte_offset>0 fused-path guard was untested); assert the window pause/resume
  actually fires in the full-blob test; tighten the upstream-spend bound to
  +2 chunks to match the documented "window + one chunk".

Deferred items tracked as #894 (Bytes/Percent newtypes), #895 (concurrent
coalescing E2E), #896 (tee_finalize_failed coverage).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
thiras and others added 2 commits June 14, 2026 18:14
Resolve conflicts from #899 (#820 prefetch ledger) landing alongside the
#856 window-paced pull-through work:

- node_origin.rs: keep the PR's persist_buyer_progress() helper refactor
  for the buyer voucher-watermark write AND main's #820 acquisition-observer
  block that surfaces per-pull spend/bytes to the prefetch ledger.
- handlers/client.rs: keep both additive sets of post-construction fields
  (window pull-through: pull_through_origin/pull_ahead_bytes/leech_governor;
  prefetch: prefetch_engine) and both serve-path credits (leech_governor
  record_served + prefetch note_served_if_prefetched).
- tests/node_origin_pull.rs: drop the duplicate decdn_cache::Hash import
  (main folds it into {CacheEngine, Hash}) and the duplicate counter_value
  helper (keep main's absent-counter-as-0 variant; satisfies both branches'
  callers).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Apply the multi-agent review findings on the window-paced pull-through:

- node_origin: wire the #820 prefetch acquisition observer into the
  window path's NodeProgressivePull::finish/abandon (was buffered-path
  only) via a shared feed_acquisition_observer helper, so per-pull spend
  can never silently escape the rolling budget if a prefetch pull is ever
  routed onto the fused path; add a node_pull_delta_overflow metric to the
  narrow_pull_delta clamp.
- leech_governor: private LeechCaps fields behind a validated
  LeechCaps::new() -> Result<_, WindowExceedsBudget> (with an explicit
  new_unchecked for intentional edge-config tests); rename may_pull ->
  poll_admission and document it as a stateful, advisory (non-reserving)
  poll; correct the MAX_TRACKED_PEERS memory estimate and new() doc.
- client_requester: add a Drop safety net on UpstreamPull to close the
  upstream connection if no terminal method ran; fix the deadline doc to
  describe what actually bounds a mid-stream stall.
- client.rs: distinct node_pull_through_local_tee_failed metric for a
  local tee-write fault (vs client drop); fix the pull_chunk_bytes ->
  CHUNK_SIZE comment and scope the abandon_window_serve doc.
- runtime: build LeechCaps via the validated constructor.
- tests: LeechCaps::new validation; window BlobTooLarge abort arm (+
  tee-not-stranded repeat request); per-peer share-ratio admission refusal.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@thiras
thiras merged commit f2b4a61 into main Jun 14, 2026
22 checks passed
@thiras
thiras deleted the fix/856-window-paced-pull-through branch June 14, 2026 16:26
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.

node: cache-miss pull-through pays upstream for the entire blob before collecting any downstream voucher

2 participants