Skip to content

feat(cache): prefer staging over progressive chunks - #1355

Merged
kalbasit merged 2 commits into
mainfrom
feat-cache-prefer-staging-over
Jun 8, 2026
Merged

kalbasit merged 2 commits into
mainfrom
feat-cache-prefer-staging-over

Conversation

@kalbasit

@kalbasit kalbasit commented Jun 7, 2026

Copy link
Copy Markdown
Owner

Read-path precedence for in-flight staging during the eager-CDC chunking
window (#1289, group 7). getNarFromChunks now, when total_chunks == 0 and
staging is enabled, checks for available staging parts and serves the
complete whole-NAR representation from them instead of entering
streamProgressiveChunks. The staged parts are an ordered, complete copy, so
this avoids the fragile progressive reassembly that can truncate.

The precedence is strictly scoped to the chunking window: total_chunks > 0
never reaches the branch, so steady-state chunk serving is unchanged, and
when no staging parts are present the progressive path remains the fallback.
The shared putStagingParts test helper now takes the NarStore interface so the
precedence tests can stage via c.narStore.

@dosubot dosubot Bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Jun 7, 2026
@coderabbitai

coderabbitai Bot commented Jun 7, 2026 •

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@kalbasit, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 6 minutes and 38 seconds. Learn how PR review limits work.

Your organization has run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

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.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: ecfe4815-b16a-4b52-97fb-8369943d016e

📥 Commits

Reviewing files that changed from the base of the PR and between 4428ac9 and 1fb88f8.

📒 Files selected for processing (3)
  • pkg/cache/cache.go
  • pkg/cache/inflight_staging_precedence_internal_test.go
  • pkg/cache/inflight_staging_reader_internal_test.go
📝 Walkthrough

Walkthrough

The PR adds early-exit logic to streamProgressiveChunks to serve whole NARs directly from in-flight staging during the CDC chunking window instead of reassembling from progressive chunks, plus comprehensive test coverage validating this preference under different staging and chunking states.

Changes

In-flight staging precedence during chunking window

Layer / File(s) Summary
Streaming preference for in-flight staging during chunking window
pkg/cache/cache.go
streamProgressiveChunks now adds an early branch: when chunking is in progress (totalChunks == 0) and in-flight staging is enabled and has available ordered parts, the function returns serveNarFromStaging immediately. Otherwise, the existing progressive chunk-polling logic remains the fallback.
Test coverage and test infrastructure for staging precedence
pkg/cache/inflight_staging_precedence_internal_test.go, pkg/cache/inflight_staging_reader_internal_test.go
New test file adds three scenarios: (1) prefers staged bytes over progressive when chunking-window staging parts exist; (2) falls back to progressive-path timeout when chunking-window staging has no parts; (3) ignores staging entirely in steady state and serves chunked content. Test helper putStagingParts refactored to accept storage.NarStore interface.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related issues

  • kalbasit/ncps#1289: Both changes modify streamProgressiveChunks to prefer whole-NAR from in-flight staging during the chunking window (total_chunks==0) instead of reassembling from progressive chunks.

Possibly related PRs

  • kalbasit/ncps#1207: Both PRs modify pkg/cache/cache.go's streamProgressiveChunks read path to prioritize alternative NAR retrieval during the chunking window.

Poem

🐰 A rabbit hops through staging ground,
In chunking windows, glory's found—
No reassembly, no delay,
Whole NARs served the rabbit's way!
✨ 🎯

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately and concisely summarizes the main change: preferring in-flight staging over progressive chunks during the eager-CDC chunking window.
Description check ✅ Passed The description is directly related to the changeset, explaining the read-path precedence for in-flight staging, the conditions under which it applies, and the test helper updates.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


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

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

Copy link
Copy Markdown
Contributor

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 modifies the NAR retrieval logic to prefer serving from in-flight staging over progressive chunk streaming during the chunking window (when totalChunks == 0), and adds corresponding unit tests. A critical review comment highlights that TransparentZstd is not cleared when serving from staging, which could lead to protocol or decoding errors at the client, and suggests explicitly setting it to false before returning.

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 pkg/cache/cache.go Resolved
@dosubot

dosubot Bot commented Jun 7, 2026 •

Copy link
Copy Markdown

Documentation Updates

1 document(s) were updated by changes in this PR:

NAR File Download and Streaming
View Changes
@@ -104,6 +104,8 @@
 **Part availability detection**: On each 200ms poll tick, the waiter checks `stagingServeReady` to determine if parts are available. Once `parts_available > 0`, the function returns a staging directive (`downloadState.stagingServe`) instead of continuing to wait for the whole download.
 
 **Streaming from staging parts**: When `GetNar` receives a staging directive, it calls `serveNarFromStaging` to stream the NAR from staging part-objects rather than storage. The implementation creates a `stagingPartReader` that tails part-objects in order, polling `staging_state.parts_available` every 200ms and blocking until each part is durably available.
+
+**Precedence during the chunking window**: When `total_chunks == 0` (actively chunking state), `getNarFromChunks` first checks for in-flight staging parts if staging is enabled. If staging parts are available, it serves the complete NAR from them instead of entering the progressive chunk streaming path (`streamProgressiveChunks`). This precedence improves reliability: staged whole-NAR parts are a complete, ordered representation, so serving from them avoids the fragile progressive reassembly that can truncate. This precedence is strictly scoped to the chunking window—when `total_chunks > 0` (steady-state chunk serving), the new branch is not reached and behavior is unchanged. When serving from staging in this path, the `TransparentZstd` flag is cleared because `serveNarFromStaging` sets the compression based on what's actually in staging. If no staging parts are available, the system falls back to the progressive-chunk path as before.
 
 **Transcoding support**: The reader transcodes if needed—for example, decompressing staged bytes when the client requests uncompressed data—at parity with the same-pod streaming path. The returned `narURL.Compression` advertises the compression actually served.
 

How did I do? Any feedback?  Join Discord

@kalbasit

kalbasit commented Jun 7, 2026

Copy link
Copy Markdown
Owner Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jun 7, 2026 •

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@kalbasit
kalbasit force-pushed the feat-cache-serve-cross-pod-nar branch from 1c37969 to cdfc03f Compare June 8, 2026 00:11
kalbasit added a commit that referenced this pull request Jun 8, 2026
Address review feedback on PR #1355 (thread PRRT_kwDONV6tZs6Hr3B-): when the
chunking-window path prefers in-flight staging, clear narURL.TransparentZstd
before serveNarFromStaging. serveNarFromStaging sets narURL.Compression to the
bytes it actually serves (decompressing staged compression on the fly when the
client wants none), so leaving TransparentZstd=true would mislead the HTTP layer
into expecting a zstd stream and corrupt the response.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@kalbasit
kalbasit force-pushed the feat-cache-prefer-staging-over branch from 4428ac9 to e313438 Compare June 8, 2026 00:11
@kalbasit
kalbasit force-pushed the feat-cache-serve-cross-pod-nar branch from cdfc03f to 891de62 Compare June 8, 2026 00:19
kalbasit added a commit that referenced this pull request Jun 8, 2026
Address review feedback on PR #1355 (thread PRRT_kwDONV6tZs6Hr3B-): when the
chunking-window path prefers in-flight staging, clear narURL.TransparentZstd
before serveNarFromStaging. serveNarFromStaging sets narURL.Compression to the
bytes it actually serves (decompressing staged compression on the fly when the
client wants none), so leaving TransparentZstd=true would mislead the HTTP layer
into expecting a zstd stream and corrupt the response.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@kalbasit
kalbasit force-pushed the feat-cache-prefer-staging-over branch from e313438 to f51b255 Compare June 8, 2026 00:19
@kalbasit
kalbasit force-pushed the feat-cache-serve-cross-pod-nar branch from 891de62 to e9b0c73 Compare June 8, 2026 00:27
kalbasit added a commit that referenced this pull request Jun 8, 2026
Address review feedback on PR #1355 (thread PRRT_kwDONV6tZs6Hr3B-): when the
chunking-window path prefers in-flight staging, clear narURL.TransparentZstd
before serveNarFromStaging. serveNarFromStaging sets narURL.Compression to the
bytes it actually serves (decompressing staged compression on the fly when the
client wants none), so leaving TransparentZstd=true would mislead the HTTP layer
into expecting a zstd stream and corrupt the response.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@kalbasit
kalbasit force-pushed the feat-cache-prefer-staging-over branch from f51b255 to 2761b54 Compare June 8, 2026 00:27
@kalbasit
kalbasit force-pushed the feat-cache-serve-cross-pod-nar branch from e9b0c73 to 1926202 Compare June 8, 2026 00:42
kalbasit added a commit that referenced this pull request Jun 8, 2026
Address review feedback on PR #1355 (thread PRRT_kwDONV6tZs6Hr3B-): when the
chunking-window path prefers in-flight staging, clear narURL.TransparentZstd
before serveNarFromStaging. serveNarFromStaging sets narURL.Compression to the
bytes it actually serves (decompressing staged compression on the fly when the
client wants none), so leaving TransparentZstd=true would mislead the HTTP layer
into expecting a zstd stream and corrupt the response.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@kalbasit
kalbasit force-pushed the feat-cache-prefer-staging-over branch from 2761b54 to b09e53e Compare June 8, 2026 00:42
@kalbasit
kalbasit force-pushed the feat-cache-serve-cross-pod-nar branch from 1926202 to bf7af11 Compare June 8, 2026 00:49
kalbasit added a commit that referenced this pull request Jun 8, 2026
Address review feedback on PR #1355 (thread PRRT_kwDONV6tZs6Hr3B-): when the
chunking-window path prefers in-flight staging, clear narURL.TransparentZstd
before serveNarFromStaging. serveNarFromStaging sets narURL.Compression to the
bytes it actually serves (decompressing staged compression on the fly when the
client wants none), so leaving TransparentZstd=true would mislead the HTTP layer
into expecting a zstd stream and corrupt the response.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@kalbasit
kalbasit force-pushed the feat-cache-prefer-staging-over branch from b09e53e to 1d3c1d2 Compare June 8, 2026 00:49
Base automatically changed from feat-cache-serve-cross-pod-nar to main June 8, 2026 00:56
@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. and removed size:S This PR changes 10-29 lines, ignoring generated files. labels Jun 8, 2026
kalbasit and others added 2 commits June 8, 2026 00:56
Read-path precedence for in-flight staging during the eager-CDC chunking
window (#1289, group 7). getNarFromChunks now, when total_chunks == 0 and
staging is enabled, checks for available staging parts and serves the
complete whole-NAR representation from them instead of entering
streamProgressiveChunks. The staged parts are an ordered, complete copy, so
this avoids the fragile progressive reassembly that can truncate.

The precedence is strictly scoped to the chunking window: total_chunks > 0
never reaches the branch, so steady-state chunk serving is unchanged, and
when no staging parts are present the progressive path remains the fallback.
The shared putStagingParts test helper now takes the NarStore interface so the
precedence tests can stage via c.narStore.
Address review feedback on PR #1355 (thread PRRT_kwDONV6tZs6Hr3B-): when the
chunking-window path prefers in-flight staging, clear narURL.TransparentZstd
before serveNarFromStaging. serveNarFromStaging sets narURL.Compression to the
bytes it actually serves (decompressing staged compression on the fly when the
client wants none), so leaving TransparentZstd=true would mislead the HTTP layer
into expecting a zstd stream and corrupt the response.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@kalbasit
kalbasit force-pushed the feat-cache-prefer-staging-over branch from 1d3c1d2 to 1fb88f8 Compare June 8, 2026 00:56
@dosubot dosubot Bot added size:S This PR changes 10-29 lines, ignoring generated files. and removed size:L This PR changes 100-499 lines, ignoring generated files. labels Jun 8, 2026
@kalbasit
kalbasit enabled auto-merge (squash) June 8, 2026 00:56
@kalbasit
kalbasit merged commit 2c73119 into main Jun 8, 2026
39 checks passed
@kalbasit
kalbasit deleted the feat-cache-prefer-staging-over branch June 8, 2026 01:04
kalbasit added a commit that referenced this pull request Jun 10, 2026
…#1386)

## Summary

The `staging-contention` e2e's chunking window surfaced a real ncps gap:
in-flight NAR staging — documented (#1355/#1374/#1375/#1379, CHANGELOG)
as serving cross-pod reads during the eager-CDC chunking window and
*"superseding the fragile progressive-chunk reassembly"* — **never
actually engaged** for the **uncompressed `.nar`** request, which (since
predictive-`none`, #1380) is the *only* request clients make in that
window. The cross-pod reader served progressive chunks with zero
contention and zero staging. #1379 itself flagged this as unit-verified
but with "no green end-to-end run yet."

This PR closes the gap (ncps fix), then makes the e2e assert the
now-correct behavior.

### Root cause — two read-path sites

- **`GetNar` routing (`cache.go:~1320`):** the fall-through to download
coordination (the only path that records a staging request) was gated
`Compression != none`, so the uncompressed read short-circuited to
progressive chunk serving and never contended.
- **`pollForDownloadOrTakeOver` state (D) (`cache.go:~6815`):** even
once coordinating, an actively-chunking NAR with no staging parts *yet*
immediately returned "served-by-peer → progressive," pre-empting staging
on an early tick.

### Fix (both gated on `InflightStagingEnabled()`)

- Route uncompressed actively-chunking cross-pod reads through
coordination (new `shouldCoordinateInflight` predicate) so they record a
staging request and serve from staging.
- State (D) now waits, bounded (`maxStagingWaitTicks`), for staging
parts so the staging serve wins, before falling back to progressive
chunks (safety net for a stalled producer).
- Unchanged for: staging disabled, lazy CDC, single-replica/same-pod,
finished NARs.

### Harness + docs

- `staging-contention` chunking window now asserts **staging
activation** (with a bounded clean-restart retry — addresses the earlier
CodeRabbit review), reverting the interim progressive-chunk assertion.
- Corrected the CHANGELOG (the prior entry described
intended-but-unverified behavior) and synced the `inflight-nar-staging`
+ `unified-e2e-harness` specs.

## Verification

- [x] `go test -race ./pkg/cache/` green; full `task test` green — no
regression
- [x] New `pkg/cache` internal tests (red→green) for both sites + the
staging-disabled / stalled-producer guards
- [x] `task test:e2e ... staging-contention`: **both** windows activate
in-flight staging, twice (determinism). Chunking-window race **143s →
4.5s** as staging replaces progressive reassembly.
- [x] `task fmt` / `task lint` clean; `openspec validate --all` 41/0

> Note: the first commit on this branch was the interim harness-only
change; this PR's net diff is the ncps fix + the staging-activation
assertion.

---------

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

enhancement New feature or request go Pull requests that update go code merge-stack size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant