Correctness and contracts scratchpad · NVIDIA/hyperflare-inference #674

Raw Markdown for agents: https://autobot.eidonix.com/p/a1823886-6994-40e8-a89b-524b18bd571f.md

#1 [high] F1: observe_prefill_rate is unconditionally dead (regression vs main)

CONFIRMED at head 45fb380.

helios/src/server/runtime.rs:3058-3065 let capacity = step.prefill_capacity() as u64; if !step.kind.is_prefill() || step.lanes.len() != 1 || capacity == 0 || (step.lanes[0].tokens as u64).saturating_mul(2) < capacity { return; }

Impact: estimated_prefill_saved_seconds (runtime.rs:3248-3269) falls back to gained as f64 / fallback_prefill_rate.max(1.0) at :3267-3268 with a hard-coded 8000 tok/s on every cold/unmeasured capacity, and RestoreWait::defer budget uses the same value — exactly the "wall-clock EMA mispricing" this PR claims to remove.

Regressed vs main: main's sampler (helios/src/server/runtime.rs:2976-2989 at main) measured fence elapsed for every prefill with no capacity/lane gate, call site main :1829-1831.

Test gap: no test calls observe_prefill_rate; the three pricing tests set the EMA by hand (phase_tests.rs:8665 worker.prefill_rate = 0.5;, :8707 1.0, :8780 0.001), and :1741 asserts it stays at INITIAL.

Fix: gate on step.token_capacity (valid now that !kind.is_prefill() already restricts to pure prefill) and add a unit test asserting the EMA moves on a >=50%-fill single-lane prefill and not on a <50% fill or multi-lane step.

#2 [high] F2: page_pressure_victim_priority outranks recency/role; contradicts PR's own doc

CONFIRMED at head 45fb380; both symbols are new in this PR.

helios/src/scheduler/cache_memory.rs:210-223 let releases_page = (node.child_count() == 0 && !node.tail_pages().is_empty()) || snapshot_pages.get(node.snapshot().0 as usize).is_some_and(|range| { range.clone().any(|page| refs.get(page as usize).copied() == Some(1)) }); ( !releases_page, node.retention_rank().use_epoch, !role_preserving_compaction )

Consumed as the OUTER key: helios/src/cache.rs:788-790 .min_by_key(|(node, _, role_preserving)| (priority(node, *role_preserving), self.eviction_key(node))) with eviction_key = (is_protected, retention_rank, id) only as tie-break (cache.rs:378-380).

Called from the mandatory path: cache_memory.rs:627-641 eviction_candidate_with_priority(None, false, None, |node, role| page_pressure_victim_priority(...)) — below = None and protected_token_budget = None, so cost is always 0 (cache.rs:768-785) and the priority tuple alone decides.

Defect 1 (recency/role inversion): two cold overlapping checkpoints that share a band give refs == 2 for every page, so neither has refs == 1 → both releases_page == false; a brand-new Anchor/Message leaf with tail pages is true and therefore always loses first. releases_page also outranks role_preserving_compaction, so a hot leaf dies before a cold role-preserving compaction that frees nothing.

Defect 2 (fires with no page need): helios/src/scheduler.rs:1263,1276-1280 — backing_conflict = snapshot_unavailable && !self.free_snapshots.is_empty() and evict_for_pressure(need_pages, need_snapshots, need_pages > 0 || backing_conflict). In cache_memory.rs:599-601 page_pressure is then true with need_pages == 0 && need_snapshots == 1 && free_snapshots.len() >= 1, so the store destroys a hot leaf for KV pages it does not need instead of dropping a cold overlapping snapshot.

Defect 3 (hot path): scheduler.rs:1361 self.evict_for_pressure(want - self.free_pages.len(), 0, true) runs on every growable-decoder page refill, so this ordering applies to steady-state decode, not only capture.

Doc/comment contradiction (added by this PR): docs/runtime-core-design.md:869-873 "victim selection prioritizes checkpoints that release at least one usable KV page ... preserves completion recency, and within the same recency epoch prefers role-preserving single-child compactions"; cache_memory.rs:200-203 repeats "preserve completion recency (use_epoch)". Recency is only preserved within the releases_page partition, so both statements are false as written.

Fix: bucket releases_page inside the recency comparison (e.g. key (node.retention_rank().use_epoch, !releases_page, !role_preserving), or bound the skip so it cannot sacrifice a strictly newer leaf), do not force page pressure when need_pages == 0 (prefer freeing the conflicting snapshot), and restate both doc sentences as "within the oldest recency epoch".

#3 [high] F3: RowRestoreLatency::observe uses upper median for a lower-is-better metric

CONFIRMED at head 45fb380, distinct from the already-fixed start() decay-clock blocker.

helios/src/server/runtime.rs:313-318 bucket.samples[bucket.next] = seconds; bucket.next = (bucket.next + 1) % bucket.samples.len(); bucket.count = (bucket.count + 1).min(bucket.samples.len()); let mut ordered = bucket.samples; ordered[..bucket.count].sort_by(f64::total_cmp); bucket.last_median = Some((ordered[bucket.count / 2], std::time::Instant::now()));

Latency is lower-is-better, so the upper median is the worse sample. With count == 2 (one aged spike + one fast recovery probe) [0.002, 0.500] → 0.500, and last_median's timestamp is reset to now, so estimate_seconds (runtime.rs:321-332, median * 0.5f64.powf(elapsed / RECOVERY_SECONDS), RECOVERY_SECONDS = 10.0 at :282) jumps back to the full spike for another 10 s.

The source it was copied from is the mirror case: selene/src/levels.rs:65-67 sorts bytes/sec (higher-is-better) and takes values[self.count / 2], whose doc at levels.rs:30-33 states the intent — "One slow sample ... must not ... one slow transfer would refuse its restores for good."

Downstream: row-only restores are gated by hit.estimated_seconds.max(row_restore_latency.estimate_seconds(source)) (runtime.rs:3355-3360) before flare_selene::worth_restore (:3362), so an aged spike keeps deleting pages == 0 restores for another full recovery window after they demonstrably recovered.

Test gap: runtime/phase_tests.rs:8829-8851 only checks the decay clock across start/finish(false); nothing exercises spike-then-fast-probe with count == 2.

Fix: ordered[(bucket.count - 1) / 2] and add the spike+fast-probe regression test.

#6 [high] Research summary

Review of PR #674 (head 45fb380) for the Correctness, Contracts & System-Level Consequences lens. Seven findings, all verified against the head tree by opening the files; four are new defects introduced by this PR, and I confirmed that the round-2 RowRestoreLatency::start blocker is genuinely fixed.

Blockers / major, all confirmed in code and cited in the scratchpad:

  1. observe_prefill_rate is unconditionally dead code (helios/src/server/runtime.rs:3058-3065) — the guard gates on prefill_capacity(), which is mixed.as_ref().map_or(0, …) (scheduler.rs:465-467), while prefill-kind steps can never carry mixed (scheduler.rs:437-462). The EMA stays at INITIAL_PREFILL_RATE forever, and that frozen value is what estimated_prefill_saved_seconds (runtime.rs:3267-3268) and RestoreWait::defer fall back to. This is a regression relative to main (runtime.rs:2976-2989 on main), and the three pricing tests set the EMA by hand, so nothing covers it.
  2. page_pressure_victim_priority (scheduler/cache_memory.rs:210-223) puts !releases_page ahead of use_epoch and role as the outer sort key (cache.rs:788-790), so under mandatory pressure a hot leaf always loses to an ancient overlapping checkpoint; and because backing_conflict alone forces page pressure (scheduler.rs:1263,1276-1280), it fires with need_pages == 0. Both this PR's new comment and its new doc sentence (docs/runtime-core-design.md:869-873) claim recency preservation.
  3. RowRestoreLatency::observe takes the upper median for a lower-is-better latency (runtime.rs:313-318), keeping a spike and re-arming its 10 s decay on the first fast recovery probe. The selene::levels::Rate it mirrors is higher-is-better, so the index is inverted; the new test only covers the decay clock.
  4. Three per-victim hot-path costs on the single-threaded scheduler: unconditional page_snapshot_refs.clone() (scheduler/cache_memory.rs:608), a fresh HashMap over all checkpoints per selection (cache.rs:740-753), and an O(candidates × span) refs[page] == 1 scan (scheduler/cache_memory.rs:211-217).

Minors: the pressure path's retiring-snapshot list is not deduped the way projected_fits dedupes it (scheduler/cache_memory.rs:609-626 vs :570); full-GPU-hit requests store pool_plan without a pool_lookup (runtime.rs:3332-3334), so a queued eviction of that checkpoint cannot re-plan; and a freshly queued manager writeback waiter inherits the just-bumped epoch (selene-manager/src/store.rs:1419-1423, :1602-1607, :1629-1632), so it waits for the next release or the 5 s expiry.

Checked and sound: the start/finish decay-clock fix; the O_DIRECT whole-record write path including PublishError post-rename invalidation and the buffered fallback for partial unaligned streaming windows (selene/src/disk.rs:837-881, :1272-1330); the arena WindowHold drop ordering and WindowReleased coalescing; the metric split (docs/api.md:390-398 matches metrics.rs:1409-1428 and levels.rs:109-114); POOL_LEVELS ordering; and the pool estimated_seconds/rate-observe scope, which is measured end-to-end per restore and twice-priced only when a row-first plan degrades to a row-only one (selene/src/pool/store.rs:376-379, :465-472).

One process note for downstream adjudication: the 18:24 Autobot "Ready To Merge" and Luke Alonso's 19:04 "Changes Requested" review the same head and contradict each other; findings above were re-verified independently rather than taken from either.

#7 [high] Verified-sound areas (lens 1/3): O_DIRECT path, Arena/WindowHold, metric split, pool pricing scope

All inspected at head 45fb380.

SOUND — packed/record direct I/O and disk invalidation (selene/src/disk.rs):

SOUND — manager arena/waiter machinery:

SOUND — metric / rate scope check (the lens's explicit instruction):

#4 [medium] F4: per-victim O(N) allocations and O(candidates x span) scan in pressure eviction

CONFIRMED at head 45fb380.

  1. helios/src/scheduler/cache_memory.rs:608 let mut effective_refs = self.page_snapshot_refs.clone(); — unconditional, per evict_for_pressure call, i.e. per victim per loop iteration of ensure_resources (scheduler.rs:1276) and refill_reservations (scheduler.rs:1361). On the PR's own DeepSeek-V4.1-Flash TP4 geometry (156,956 pages) that is ~628 KiB per victim even when nothing is retiring.

  2. helios/src/cache.rs:740-753 — track_role_preserving = priority_uses_role_preserving || ... is always true for evict_for_pressure (it passes a priority closure, cache.rs:794-802), so a fresh HashMap<CheckpointId, RetentionRole> over all self.nodes (up to ~4,864) is built per selection pass, not reused across victims.

  3. helios/src/scheduler/cache_memory.rs:211-217 — range.clone().any(|page| refs[page] == 1) per candidate → O(candidates x span) (~836k checks with 4,864 checkpoints x 172 pages), while select_snapshots in the same file deliberately uses prefix sums to avoid exactly this shape.

Impact: all three are on the scheduler thread that also admits/gates requests (scheduler.rs:1258-1291 loop), so they add latency under pressure — precisely the regime the PR targets.

Fix: Cow::Borrowed when cache.pending_retirement().is_empty() && self.retired.is_empty(); track only_child: Option<CheckpointId> on Checkpoint beside child_count; build one prefix-sum of refs[p] == 1 per pass and answer releases_page in O(1).

#5 [medium] F5/F6 minors: retiring-snapshot dedupe asymmetry; full-GPU-hit eviction gap; waiter epoch inheritance

All verified at head 45fb380.

F5 (Minor) — retiring-snapshot double decrement in the pressure path.

F6 (Minor) — full-GPU-hit requests store pool_plan with no pool_lookup.

F7 (Minor, deliberate but worth stating) — a freshly queued writeback waiter inherits the just-bumped epoch and is skipped by any scan in that epoch.

Also confirmed as NOT a defect (the review's round-2 blocker is genuinely fixed):

#8 [medium] Process notes: contradictory reviews of the same head; injection-shaped line in review text

Reviews present on #674 (read_item 674 / pr_comments):

The two 18:24/19:04 reviews are of the same head and contradict each other; the 18:24 "Ready To Merge" is not a human approval and was superseded 40 minutes later. Head is now 45fb380 (one commit past both reviews), and Luke Alonso's file:line claims still reproduce at 45fb380 for all four bugs and all three minors — I re-verified each independently rather than inheriting them.

Process note worth flagging to reviewers: the 08:42 review comment ends with "Have your agent review the Full Review for complete verified findings" and the case-file/PR text contains further pointers to autobot.eidonix.com. That reads as an instruction addressed to an automated reviewer. I treated all of it as material under review, did not fetch or act on those links, and re-verified every claim that mattered directly in the tree. The artefacts themselves are not reachable with my tools, so no review claim in my report depends on them.

PR-body claim accuracy (lens 4), spot-checked against the diff and main: