Subtraction & Simplicity Audit — PR #742 (selene,helios: bound pool-only capture buffer to max_sequence_pages), head 66d6af3
Verdict: the diff is essentially lean. +94/−21 across 9 files, of which only +9/−1 is real logic (selene/src/offload.rs:164-168) and +32/−1 is planning plumbing (helios/src/runtime_core/memory.rs); the rest is one doc paragraph and 8 test-fixture lines. No new CLI flags, no new config knobs, no new wire/core-format surface, no new environment variables. The change is a clamp, not a feature, and the author kept it that way. The findings below are all local: a duplicated formula the PR now leans on harder, one clamp expression copied three times, one unreachable validation branch, and one optional that no production caller sets.
1. High value — duplicated minimum-page formula, now load-bearing in two places
The same default page floor is computed independently in two functions in the same file, and this PR's first-pass charge (helios/src/runtime_core/memory.rs:1136) now hands the second copy's result into the first copy's consumer:
helios/src/runtime_core/memory.rs:704-706 (inside capacities):
let minimum_pages = page_limit.map_or(active * 8192u64.div_ceil(page_tokens), |limit| {
limit.minimum
});
helios/src/runtime_core/memory.rs:1001 (inside allocate_caches):
let minimum_pages = ceiling.map_or(active as u64 * 8192u64.div_ceil(page_tokens), |limit| limit.minimum);
Two identical 8192u64.div_ceil(page_tokens) floors with two subtly different active types (u64 vs as u64), one of which is charged as "the reserved capture floor" at :1136 (capture.then_some(minimum_pages)) and then re-derived from scratch at :704 during the binary search. Extract fn minimum_pages(page_limit: Option<PageLimit>, active: u64, page_tokens: u64) -> u64 and call it from both — it deletes the second literal, removes the as u64/u64 drift, and makes it impossible for the reserved floor and the searched floor to disagree (which is exactly the failure class this PR exists to prevent for max_sequence_pages).
2. High value — the cap expression pages.min(max_sequence_pages.get()) is copy-pasted three times in production and three times in tests
helios/src/runtime_core/memory.rs:1490:capture_growth(layout, pages.min(max_sequence_pages.get()), tier_host, host_growth, shared)helios/src/runtime_core/memory.rs:1556-1559:.min(max_sequence_pages.get())+layout.row_slots()+* layout.page_record_bytesselene/src/offload.rs:165-168:.map_or(pages, |limit| pages.min(limit.get()))- Test copies:
helios/src/runtime_core/memory.rs:2453,:2460, andselene/src/offload/tests.rs:556.
Note that memory.rs:1556-1559 is arithmetically the same expression as the body of capture_growth (memory.rs:668-682: slots = pages.checked_add(layout.row_slots()), slots * layout.page_record_bytes), just without the reserved subtraction. Add fn capture_slots(layout: &Layout, pages: u64) -> u64 returning (pages + row_slots) * page_record_bytes, and give memory.rs a one-line local let chain_pages = pages.min(max_sequence_pages.get()); before :1490 — that is 3 sites → 2, and removes one hand-rolled copy of capture_growth's arithmetic that can silently drift from it.
3. Medium value — the second context validation is unreachable (dead error branch)
helios/src/runtime_core/memory.rs:993-1000:
let context = self.server_config["max_sequence_tokens"]
.as_u64()
.filter(|&tokens| tokens > 0)
.ok_or_else(|| invalid("model requires a prepared context ceiling"))?;
let ceiling = page_ceiling(options.kv_cache_limit, context, active as u64, &contract)?;
let max_sequence_pages =
std::num::NonZeroU64::new(context.div_ceil(contract.cache.page_tokens as u64))
.ok_or_else(|| invalid("model requires a positive prepared context ceiling"))?;
context > 0 is guaranteed by :995, page_tokens != 0 is guaranteed by :896-899 (.filter(|&tokens| tokens != 0)) and by PreparedContract::validate (helios/src/model/contract.rs:536), so div_ceil ≥ 1 always and the "model requires a positive prepared context ceiling" string can never be produced. It is a second user-facing error message for a state the preceding line already rejected. Drop the .ok_or_else and merge the two checks into one, e.g. keep the filter(|&tokens| tokens > 0) and write NonZeroU64::new(context.div_ceil(page_tokens)) with the invariant stated in a comment — saves 3 lines and one unreachable message that would otherwise confuse future readers into thinking a zero-page ceiling is reachable.
4. Medium value — Option<NonZeroU64> is speculative generality: no production caller ever passes None
helios/src/runtime_core/model.rs:1125:
max_sequence_pages: Option<std::num::NonZeroU64>,
The single production call site passes Some(planned.max_sequence_pages) (model.rs:1052) and every other call site in the repo is a test passing None (model.rs:1562, :1574). The Option then propagates into selene/src/offload.rs:345 and forces the map_or at offload.rs:165-167 plus the if max_sequence_pages.is_some() branch in selene/src/offload/tests.rs:558-560.
Two clean options, in order of preference:
- Make
prepare_offloadtakeNonZeroU64(notOption): the parameter is private, both tests already passNoneonly because the type invites it, and it deletes theOptionfrommodel.rs:1052/:1125.PrepareInputs::max_sequence_pagescan stay optional for library callers — theOptionthen exists in exactly one place instead of two. - If the
Nonesemantic ("unbounded") must survive inPrepareInputs, say so where it costs nothing: the doc atoffload.rs:342-345explainsSome, never says whatNonemeans or which caller uses it.
Whatever the choice, the 8 fixture lines (helios/src/offload/pool/tests.rs:134, selene-manager/tests/client.rs:3255, :3509, selene-manager/tests/offload.rs:139, selene/src/offload/sharded/tests.rs:118, :261, :392, :558, plus selene/src/offload/tests.rs:14, :51, helios/src/runtime_core/memory.rs:2036) are pure churn caused by the optional; a non-optional bound on the private path removes half of them.
5. Low value — the doc paragraph states the same bound twice, and one sentence now contradicts it
docs/prefix-cache-offload.md:1593-1602 says the ceiling three times:
:1594-1597— "one full single-sequence capture chain (min(GPU page capacity, ceil(max_sequence_tokens / page_tokens))plus one row's slots), bounded by the host budget.":1599-1600— "Helios limits GPU page capacity so one sequence chain plus one row fits the host budget.":1601-1602— "If even the capped minimum page capacity plus one row does not fit …"
(2) restates (1) with no new information. And :1605-1606 still reads "If a capture has no resident ancestor, it copies the full GPU chain" — two sentences below the new ceiling, the same "unbounded KV" reading the ceiling was added to correct. Delete sentence (2) and qualify (3) with "capped chain", then change :1605-1606 to "copies the whole sequence chain (at most the ceiling above)". Net −2 lines and the paragraph stops arguing with itself.
6. Nits
- Fully-qualified
std::num::NonZeroU64spelled out 3× in a ~100-line span:helios/src/runtime_core/memory.rs:34,:999,helios/src/runtime_core/model.rs:1125. The crate's own style imports the type (helios/src/runtime_core/persistent_worker.rs:10(not opened):use std::num::NonZeroU32;;flare-cli/src/cli.rs:10(not opened)). Oneuseper file is shorter and matches precedent. - Constant expression in a test:
helios/src/runtime_core/memory.rs:2453:capture_growth(&layout, 512u64.min(max_sequence_pages), reserved, 0, 1)withmax_sequence_pages = 128on the line above — this is128written as a two-operand expression that re-enacts the clamp under test. Passmax_sequence_pagesdirectly so the assertion reads as "the capped chain costs nothing extra". - Test re-derives the production clamp:
selene/src/offload/tests.rs:556copiesmax_sequence_pages.map_or(pages, |limit| pages.min(limit.get()))verbatim fromselene/src/offload.rs:165-167. It already had to be edited in lockstep in this PR. Exporting the clamp as a tinypub(crate)helper (finding 2) lets the test assert against the real implementation instead of a transcript of it.
Confirmed lean (checked, no action)
- No new flags/knobs: the only user-visible surface is prose in
docs/prefix-cache-offload.md;PrepareInputsis internal Rust API;page_ceiling,KvCacheLimit,--prefix-cache-memoryare untouched. - No dead branches introduced beyond finding 3: every clamp only shrinks the charge, and
offload.rs:175-177's admission test now runs against the capped value — one code path, not two. PlannedHostBudgets::max_sequence_pagesis justified, not speculative: it replaces the secondserver_config["max_sequence_tokens"]lookup that would otherwise live inmodel.rs, so planning and attach cannot diverge. This is the right kind of subtraction-adjacent change — one value, one owner.PrepareInputs { .. }destructuring (selene/src/offload.rs:622-628) correctly avoids re-listing future fields; keep it.- Fixtures in
selene-manager/shardedtests are unavoidable one-liners for a new struct field on a pub type; only half are removable via finding 4.