Review — PR #540, lens: conventions and duplication
Key findings
The change is well-organized and mostly follows the repository's own patterns, but it introduces two genuine duplication defects in the new scheduler code, and it leaves the two documents that own copy-draft semantics describing the pre-PR mechanism.
- Should fix — two parallel SAM-vs-shared-history selection paths.
RequestScheduler::request_copy_match_stats(helios/src/scheduler.rs:1163-1205) reproducesrequest_history_proposal_with_match_len(helios/src/scheduler.rs:1112-1159) almost exactly, and the copies have already drifted on the zero-count shared-ring case. - Should fix — re-derived copy prior in
wide_copy_curve.helios/src/scheduler/rounds.rs:541-553recomputes the same geometric prior thatCopyCalibration::curvealready owns, in a branch that cannot be reached. - Should fix — stale copy-draft design prose.
docs/mtp-design.md:22-40anddocs/cli-design.md:742still describe copy proposals as only the shared ring, anddocs/scheduling-runtime-design.md:7-12omits the newsam.rsmodule from the index of modules that implement the documented design. - Nits (4): repeated copy-provenance predicate; unexplained
refresh_orderreturn type; a test oracle that shares production shapes and logic; unconsumedpubSAM API and a duplicated6.
Nothing blocking. All findings were checked in the code at head 7ca5430.
Should fix
1. request_copy_match_stats duplicates request_history_proposal_with_match_len
File: helios/src/scheduler.rs:1163-1205 (duplicating 1112-1159).
Both functions perform the identical sequence:
| Step | request_history_proposal_with_match_len |
request_copy_match_stats |
|---|---|---|
| Clamp | 1117-1119: maximum.min(completion_token_capacity.saturating_sub(1)).min(COPY_DRAFT_CAPACITY) |
1168-1170: identical |
| Early exit | 1120-1122: maximum == 0 || decoded_history.is_none() |
1171-1173: identical |
| Request lookup | 1123-1125: self.request(handle) with vec![-1; maximum] fallback |
1174-1176: identical, (0, 0) fallback |
| SAM query | 1127-1138: sam.propose(maximum), truncate at first (token as usize) >= vocabulary |
1178-1186: same sam.propose(maximum), same position(... >= vocabulary) clip |
| Shared query | 1139-1142: DecodedHistory::lookup_with_match_len(request.tokens(), maximum) |
1187-1193: same call |
| Tie-break | 1144-1148: shared wins only when shared_match_len > sam.match_len (plus shared_count > 0) |
1196-1200: shared wins only when shared_match > sam_match |
The only intended difference is the return shape ((padded_tokens, count, match_len) versus (valid_len, match_len)), stated in the doc comment at 1161-1162 ("without building a padded upload vector").
Precedent inside this same PR. helios/src/scheduler/decoded_history.rs:180-183 shows the pattern the author follows everywhere else: lookup is a three-line wrapper over lookup_with_match_len, differing only in the returned tuple. helios/src/scheduler/rounds.rs:555-557 likewise wraps wide_copy_curve as expected_copy_tokens rather than copying the curve construction.
Why it matters beyond tidiness. The two copies are no longer equivalent. In request_history_proposal_with_match_len the shared result is consumed unconditionally in the (None, Some(...)) arm (1154-1156) — a zero-count shared result yields (tokens, 0, 0). In request_copy_match_stats the shared result is filtered first (.filter(|&(count, _)| count > 0), 1194), so a zero-count result falls through to (None, None) => (0, 0) (1203). The two entry points therefore disagree about whether a zero-count shared match is a match. The wide-copy gate at rounds.rs:774-776 reads match_len from the filtered path while the packing path reads count from the unfiltered one, so a fix to that semantic has to be made in two places and can silently diverge.
Fix. Keep one implementation. Have request_copy_match_stats call a single private helper that returns (match_len, valid_len, provider) (or an Option<&MatchProposal>-style borrow), and let both public methods shape that result. Then the vocabulary clip (1130-1136), the count > 0 rule, and the strictly-longer tie-break exist once — the same factoring decoded_history.rs:180-183 already demonstrates.
2. wide_copy_curve's fallback re-implements CopyCalibration's own prior, and cannot run
File: helios/src/scheduler/rounds.rs:541-553.
let curve = if let Some(calibration) = request.copy_calibration.as_ref() {
calibration.curve(request.generated_tokens, optimistic)
} else {
(0..self.capabilities.completion_token_capacity.saturating_sub(1))
.map(|depth| speculation::WIDE_COPY_PRIOR_RATE.powi(depth as i32 + 1))
.collect()
};
The else arm reproduces, by hand, the exact curve CopyCalibration produces when nothing has been observed. In helios/src/scheduler/speculation.rs:110-151, CopyCalibration::new seeds step_rate.mean with WIDE_COPY_PRIOR_RATE (113), and curve computes prior = rate.powi(depth as i32 + 1) (132) for every None depth (145), then clamps monotonically (147-148). With all depths None and optimistic == false, curve returns precisely the fallback's sequence. The doc comment at speculation.rs:100-102 states this outright: "Unobserved depths fall back to a geometric curve parameterized by the calibrated per-step copy acceptance rate (seeded with WIDE_COPY_PRIOR_RATE)."
Two owners for one formula means the optimistic path is not honored in the fallback (the fallback ignores optimistic entirely, while curve applies step_rate.upper(generated) at 122-123), so the two curves differ whenever a caller asks optimistically. The codebase already carries the counter-example: the structural part of this refactor — wide_copy_curve/expected_copy_tokens wrapping powered_curve/expected_speculative_tokens — is right, but the prior belongs with its owner, not with the wrapper.
The branch is also dead. LiveRequest.sam and LiveRequest.copy_calibration are constructed from the same predicate at admission: helios/src/scheduler.rs:1849-1856 sets both only under .decoded_history.is_some(). enable_history_drafts (1086-1092) refuses once any request has been submitted or admitted, so decoded_history cannot become Some after a request exists without one. Every caller of wide_copy_curve is reached from the wide-copy block in rounds.rs:752-800, which is itself only entered when self.decoded_history.is_none() is false (759). copy_calibration is therefore Some wherever this arm executes — which is why no test covers it.
Fix. Delete the else arm and take the calibration unconditionally (request.copy_calibration.as_ref().expect("copy calibration accompanies the per-request SAM")), or better, give CopyCalibration a prior(capacity, optimistic) constructor so no caller re-derives the geometric series. Either way, WIDE_COPY_PRIOR_RATE then has exactly one use site — speculation.rs:113 — instead of leaking into rounds.rs.
3. Copy-draft design prose and the scheduler module index were not updated
Files: docs/mtp-design.md:22-40, docs/cli-design.md:742, docs/scheduling-runtime-design.md:7-12.
docs/mtp-design.md is the document that owns copy-draft semantics. After this PR it states, at 31-34: "With copying enabled, each loaded model retains the most recent 65,536 committed generated tokens across requests in a host-memory ring. Exact eight-token and four-token indexes match the active request's suffix, preferring eight-token matches and then the most recent occurrence. They return up to seven observed continuation tokens." And at 22-26: "--history-drafts augments Qwen's learned MTP proposals with continuations from shared decoded history."
Neither statement is now the primary path. Per helios/src/scheduler.rs:1099-1159, proposals come first from the request's own suffix automaton: a 131,072-token trailing prompt (sam.rs:25, DEFAULT_MAX_PROMPT_TOKENS), a 131,072-token output automaton (sam.rs:28), a 512-token prompt/output overlap (sam.rs:32), and a minimum match of six tokens (sam.rs:22) — raised from three by this PR. The shared ring is now the fallback, used only when its match is strictly longer (scheduler.rs:1144-1148). "Return up to seven observed continuation tokens" is a learned-draft-width statement that no longer bounds the SAM path, which drafts up to COPY_DRAFT_CAPACITY (31, speculation.rs:12) subject to graph width.
docs/cli-design.md:742 says only "Augment learned drafts with shared decoded-history continuations". docs/scheduling-runtime-design.md:39-41 does discuss copy proposals generically, and lines 435-444 do describe the new refresh ordering, so that file is otherwise current — but its module index at 7-12 enumerates the modules that specify the design (policy.rs, utility.rs, service.rs (not opened), cost.rs, refresh.rs, learning.rs (not opened), rounds.rs, speculation.rs) without sam.rs, even though the document specifies the copy-proposal mechanism that sam.rs implements. A reader following the document's own pointer cannot reach the implementation.
Rule cited. .agents/skills/repo-prose/SKILL.md:26-29 (not opened): "Documentation is a present-state specification, not a changelog. Replace stale claims instead of layering history on top." And AGENTS.md:52-56: "State support as present qualification: what is implemented, the hardware and configurations on which it is qualified". The current text qualifies the ring path as the mechanism.
Fix. Rewrite docs/mtp-design.md:22-41 to describe the per-request prompt+output SAM first and the shared ring as its fallback, with the caps and the six-token minimum; update the docs/cli-design.md:742 row; add sam.rs to the docs/scheduling-runtime-design.md:7-12 index. Also state why the two thresholds are six (sam.rs:22, speculation.rs:18) — neither is flag-exposed, so the value has to be argued in prose to satisfy AGENTS.md:446-456 on where settings live.
Nits
4. Copy-provenance predicate spelled out five times
helios/src/scheduler/cost.rs:195, 199, 229-230, 239 and rounds.rs:756 each write estimate.draft_source != Some(super::speculation::ProposalSource::Copy) (or the == form) inline. Estimate already carries a small accessor in this file (Estimate::fresh, cost.rs:111), so an is_copy_only() accessor would match local style and keep the meaning — "this graph is a copy-only wide graph" — in one place. refresh_packed_durations (192-207) and max_mtp_draft_tokens (234-244) both lean on the predicate, and is_copy_only_step (223-232) repeats it a fourth time in the same file.
5. refresh_order lost its contract comment
helios/src/scheduler/cost.rs:287-296 changed the return type to (bool, u64) — (true, attempt_ns) meaning "attempted while stale with no valid observation", (false, 0) otherwise — and the ordering rule now lives only in docs/scheduling-runtime-design.md:435-441. Neighbouring helpers in the same file carry their invariants inline (cost.rs:21-48, 210-213, 282-285). Per .agents/skills/repo-prose/SKILL.md:38-39 (not opened), comments should state invariants; this one no longer does, and (bool, u64) is not self-describing.
6. The test oracle is not independent of production code
helios/src/scheduler/sam.rs:1236-1273: brute_force_oracle returns the production MatchProposal and re-derives build_proposal's logic — the same count = max_draft_len.min(n - 1 - endpos) bound (1260 vs 1208) and the same source selection by endpos >= prompt_len (1262-1266 vs 1216-1220). A change to build_proposal that is mirrored by reflex in the oracle would not be caught; the 120-trial randomized comparison at sam.rs:1402-1456 is only as strong as that independence. Precedent: the oracle helper in helios/src/scheduler/decoded_history.rs:228-252 returns the same plain (Vec<i64>, usize) the production lookup returns, so the comparison is of values, not of shared structs.
7. pub SAM API with no caller outside tests; threshold duplicated
helios/src/scheduler/sam.rs:1070 (propose_for_context) and helios/src/scheduler/sam.rs:1118 (propose_conditional_continuations) are pub but have no non-test caller — propose_for_context is exercised only at sam.rs:1463, the conditional variant only at sam.rs:1505. AGENTS.md:188-193 asks that frameworks and extension points not be added "without a concrete requirement"; either the pipelined path that consumes them lands here, or they should not ship as pub. Relatedly, DEFAULT_MIN_MATCH_LEN = 6 (sam.rs:22) and WIDE_COPY_MIN_MATCH_LEN = 6 (speculation.rs:18) are two constants for one threshold, both required at rounds.rs:776; the SAM default also governs the plain --history-drafts path (packing.rs:395-407 → request_history_proposal → SamConfig::default(), sam.rs:53-62).
Checked and found conforming
- SPDX header and second copyright line in
sam.rs:1-5matchspeculation.rs:1-5, the established pattern inhelios/src/scheduler/. - The
wide_copy_curve/expected_copy_tokenswrapper structure atrounds.rs:555-557correctly mirrorspowered_curve/expected_speculative_tokens(537-539,528-534); the defect is only the fallback inside it (finding 2). - The per-request forecast caching in the wide-copy block (
rounds.rs:747-748,get_or_insert_withat794-795) follows the existing lane-forecast cache pattern in the same function ("Lane forecasts stay fixed while shapes compete; evaluate each at most once",746). docs/scheduling-runtime-design.md:425-433and446-450were updated for the readiness-wait exclusion and episode charging, and435-441for the two-tier refresh ordering — the prose matches the implementation, and the aggregate bounds quoted in the PR description are stated there consistent withrefresh.rs.- No Protobuf/wire changes; the
draft_sourcefield defaults to MTP, matchingAGENTS.md:437-440("Protobuf additions must use defaults that preserve existing core behavior").
Open questions
- Finding 2 assumes
copy_calibrationis neverNonewherewide_copy_curveruns. The admission construction (scheduler.rs:1849-1856) and theenable_history_draftsfence (1086-1092) make that airtight for current code; a test that constructs aLiveRequestwithsam: Someandcopy_calibration: None(or vice versa) would settle whether the invariant should be enforced structurally — for example by holding both behind oneOption<PerRequestCopyState>, which would also remove the two-copy construction at1849-1856. - Finding 3 needs an author statement on intended scope: if copy proposals are meant to remain documented as "shared history" for the ring path only, then
sam.rsis an undocumented second mechanism, whichAGENTS.md:52-56does not permit. Only the author can say which document should own the new description. - Findings 1 and 2 are checkable without hardware. Findings 6 and 7 are judgments about test strength and API surface; a Kani or property test over
build_proposalversus an oracle returning plain tuples would settle 6, and a grep of downstream crates (flare/,flare-cli/) for the twopubSAM methods would settle 7 — I found no caller inhelios/srcbut did not read those crates.