Skip to content

refactor(node): extract FrameQueue/FrameChunks so the serve framers stop hand-maintaining the byte-count invariant (#1834) - #1888

Merged
alpergundogdu merged 1 commit into
mainfrom
claude/issue-1834-spec-impl-6ddfd3
Aug 31, 2026
Merged

alpergundogdu merged 1 commit into
mainfrom
claude/issue-1834-spec-impl-6ddfd3

Conversation

@alpergundogdu

Copy link
Copy Markdown
Contributor

Closes #1834.

Both serve framers — ChunkFramer (cache-hit leg, delivery.rs) and CoherentFrameProducer (cache-miss leg, serve_encoder.rs) — held identical byte-queue state (queue: VecDeque<Bytes> + queued: usize) and maintained its invariants by hand, with the free function wire::drain_frame holding the seam open via two &mut out-params that had to move in lockstep. These are money-carrying invariants: a desync yields Ok(None), which the serve loop reads as done_delivering → StreamEnd over a truncated blob, with the client paying the closing voucher.

This extracts the shared state into two types in wire.rs, so the byte-count invariant is held by construction rather than by prose + runtime checks. Per #1834 this is the "refactor second" half of harden-first-refactor-second; it deletes the very guards #1828's follow-up added, which is why the two could not land together.

New types

  • FrameQueue { queue, queued } — new/push/len/clear/cut (is_empty is test-only). push drops empty Bytes, so queued == 0 iff the queue is empty by construction — the desync the framers guarded by hand is unrepresentable. cut is drain_frame's body moved onto the type, returning Option<FrameChunks>; None only on an empty queue.
  • FrameChunks { chunks, total } — private constructor; only cut builds it, computing total from the same items it moves into chunks. A frame whose header would misdeclare its own bytes cannot be built.

What it deletes (each now unrepresentable)

  • Both copies of the "cut no chunks from N queued bytes" desync bail and their two divergent error strings.
  • Both push-site if len > 0 filters (now inside FrameQueue::push).
  • drain_frame's front_len == 0 branch.
  • Both guards in chunk_frame_bufs — the empty-payload guard and the actual != total_len sum check.
  • The checked_sub and queue.is_empty() guards from the perf(node,protocol): zero-copy ChunkData framing via vectored QUIC writes (#1791) #1828 follow-up.

Out of scope (per issue)

  • faulted/hash stay duplicated in each producer (the two faults are semantically different; terminal-poison is a producer property, not a byte-queue one).
  • The two producers are not unified behind a trait.
  • frame_target → NonZeroUsize (its own commit) — the target == 0 guards stay in both next_frame_chunks.

Verification

  • cargo fmt -- --check — clean
  • cargo build / cargo clippy --all-targets — no warnings
  • cargo nextest run -p decdn-node — 1058 passed, 0 failed

Net: 4 files, +329 / −358.

🤖 Generated with Claude Code

…top hand-maintaining the byte-count invariant (#1834)

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

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 refactors the node’s serve-path framing logic by extracting the shared “queued Bytes + running byte count” bookkeeping into FrameQueue/FrameChunks in handlers/client/wire.rs, so the frame byte-count invariant is enforced structurally rather than by duplicated runtime checks in both serve framers (cache-hit ChunkFramer and cache-miss CoherentFrameProducer).

Changes:

  • Introduces FrameQueue (owning VecDeque<Bytes> plus queued) and FrameChunks (payload slices plus computed total) and moves frame-cutting logic onto the type.
  • Updates both serve framers and their call sites to pass around FrameChunks instead of (Vec<Bytes>, usize) and removes now-redundant invariant guards.
  • Adjusts chunk_frame_bufs and related tests to rely on the new construction invariants.

Reviewed changes

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

File Description
crates/node/src/handlers/client/wire.rs Adds FrameQueue/FrameChunks, refactors frame cutting and buffer assembly to rely on construction invariants, updates unit tests accordingly.
crates/node/src/handlers/client/serve_leg.rs Switches serve loop framing from (Vec<Bytes>, usize) to FrameChunks and uses FrameChunks::total() for billing/accounting.
crates/node/src/handlers/client/serve_encoder.rs Replaces manual VecDeque + queued bookkeeping with FrameQueue, updates framing APIs/tests to use FrameChunks.
crates/node/src/handlers/client/delivery.rs Replaces manual VecDeque + queued bookkeeping with FrameQueue, updates framing APIs/call sites/tests to use FrameChunks.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@github-actions

Copy link
Copy Markdown

Coverage report

Total: 82.40% (was 82.40%, ● 0.00%) — 69557/84415 lines

File Coverage Lines Δ
crates/node/src/handlers/client/delivery.rs 94.19% 324/344 ▲ +1.56%
crates/node/src/handlers/client/serve_encoder.rs 90.63% 445/491 ▲ +1.06%
crates/node/src/handlers/client/serve_leg.rs 81.82% 153/187 ▼ 0.10%
crates/node/src/handlers/client/wire.rs 94.56% 400/423 ▼ 1.07%
History
Commit Total Δ vs base
6d8c721 (HEAD) 82.40% ● 0.00%

@alpergundogdu
alpergundogdu merged commit 27d4d4d into main Aug 31, 2026
26 checks passed
@alpergundogdu
alpergundogdu deleted the claude/issue-1834-spec-impl-6ddfd3 branch August 31, 2026 17:39
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.

extract FrameQueue/FrameChunks so the serve framers stop maintaining their byte-count invariant by hand

2 participants