Conventions report · NVIDIA/hyperflare-inference #549

Raw Markdown for agents: https://autobot.eidonix.com/p/17bf6669-fe34-4d81-8be1-6c06488e5f69.md

Review of PR #549 (Report KV pages held by active requests) — Conventions and duplication lens

Reviewed at head 9cca87ac ("Keep page gauges out of the copy-out retry key"), 8 files, +246/−16, against the charter (read via read_charter), AGENTS.md, and the existing patterns in helios/src. All eight changed files were read in context and every finding below was re-verified in the code at head.

Key findings

Verdict: should-fix issues only. No blocking issues, no duplicated mechanism worth reverting.

  1. Should fix — Cache::pin_request/unpin_request (helios/src/cache.rs:551-580) wrap pin/unpin and then walk chain(id) a second time, splitting one invariant across two function pairs, where pin/unpin (cache.rs:519-546) each own their whole invariant in a single walk. Fix: private pin_with(id, request: bool) walking the chain once, thin wrappers on top.
  2. Should fix — the scheduler now carries two parallel forwarding pairs for the same call: bool-dispatched pin_prefix/unpin_prefix (helios/src/scheduler.rs:2025-2040) beside the pre-existing named pin_checkpoint/unpin_checkpoint (scheduler.rs:2042-2050), and four request-pin sites bypass both and call self.cache.pin_request/unpin_request directly (scheduler.rs:1889, 2977-2979, 2997-2998, 3155). Three ways to take a request pin.
  3. Nit — docs/api.md:311-312 now documents a #[cfg(test)] assertion to API readers, and docs/api.md:302-303 claims "every scheduler pass" although the worker publishes only on passes that make progress (helios/src/server/runtime.rs:1148-1153). AGENTS.md:52-60 governs what belongs in repository documents.
  4. Nit (carried over) — the two new gauges have no home in docs/runtime-core-design.md, where the neighbouring page gauge flare_snapshot_backing_pages is specified (docs/runtime-core-design.md:873-876).

Explicitly checked and dropped (no duplication, matches precedent): the new AtomicU64 fields and their bulk store and test fixture rows follow cached_pages/snapshot_backing_pages exactly; SchedulerStats::stats (scheduler.rs:1216-1228) iterates slots the way its neighbours active_requests/retiring_slots/checkpoint_anchor_guards (scheduler.rs:1203-1212) already do, and no existing helper computes those sums; the gauges' absence from the docs/api.md JSON field table is correct because that table maps JSON fields and the gauges are deliberately not in the snapshot; and the drift check is folded into the existing #[cfg(test)] assertion in Cache::stats rather than adding new machinery.


Should fix

1. pin_request/unpin_request duplicate pin/unpin's chain walk instead of extending it

Where: helios/src/cache.rs:551-563 (pin_request) and helios/src/cache.rs:565-580 (unpin_request).

Precedent in the repository:

pub fn pin(&mut self, id: CheckpointId) -> Result<()> {        // cache.rs:519-534
    if !self.checkpoint(id)?.ready { ... }
    let chain = self.chain(id)?;                                // one walk
    if self.nodes[&id].direct_pins == u64::MAX
        || chain.iter().any(|id| self.nodes[id].pins == u64::MAX) { return Err(...); }
    self.nodes.get_mut(&id).unwrap().direct_pins += 1;
    for ancestor in chain { self.nodes.get_mut(&ancestor).unwrap().pins += 1; }
    Ok(())
}
pub fn unpin(&mut self, id: CheckpointId) -> Result<()> {      // cache.rs:536-546
    ... let chain = self.chain(id)?; ...                        // one walk
}

pin and unpin each compute self.chain(id)? exactly once and own their complete invariant inside that walk, including the refcount-exhaustion guard (cache.rs:524-528). The same "one function is the single site for the derived counters" shape is visible in count/uncount (cache.rs:258-275), which are the sole mutators of ready_checkpoints/unready_checkpoints/tail_pages for a node entering or leaving nodes.

What the new code does: pin_request calls self.pin(id)? (which walks the chain) and then let chain = self.chain(id)?; again (cache.rs:552-554); unpin_request mirrors it (cache.rs:569-571). So a single logical operation — "request pins are a subset of pins; request_pinned_pages moves on the 0↔1 transition of request_pins" — is now expressed by two functions that must stay in agreement, and every admission, ticket hand-off (scheduler.rs:1889), boundary publication (scheduler.rs:2977, 2997) and retirement (scheduler.rs:3155) pays a second root-to-leaf walk plus Vec allocation.

The subset relationship that makes the split delicate is enforced only by prose and asserts: direct_request_pins/request_pins are incremented after pin succeeds, compact_ancestor asserts node.request_pins == child.request_pins (cache.rs:627) and node.pins == child.pins (cache.rs:625), and the test-only scan in Cache::stats (cache.rs:816-837) is what actually catches drift. Note that the missing u64::MAX guard on the new counters is not a separate bug — the new counters cannot exceed pins, which pin already guards — it is the reason the loops belong in one place rather than an argument for a second guard.

Why it matters: this is the one place the PR departs from the file's established "one walk owns the whole invariant" pattern, and it is the function pair with the most subtle invariant in the file. The measured cost is not claimed here (see Open questions), so the finding stands on structure alone.

Fix: add a private fn pin_with(&mut self, id: CheckpointId, request: bool) that computes chain once, runs the shared guards, updates direct_pins/pins and, when request, direct_request_pins/request_pins plus the 0↔1 request_pinned_pages adjustment; make pin, pin_request (and the unpin counterparts) thin wrappers over it, keeping the current error strings so the tests at helios/src/cache/tests.rs:329-340 stay meaningful.

2. pin_prefix/unpin_prefix is a second, bool-dispatched copy of the pin_checkpoint/unpin_checkpoint convention, and half the request pins bypass both

Where: helios/src/scheduler.rs:2025-2040 (new pin_prefix/unpin_prefix), against helios/src/scheduler.rs:2042-2050 (pin_checkpoint/unpin_checkpoint).

Precedent in the repository: the scheduler already expresses "forward to the cache with a named purpose" as a named pair, documented at the definition:

/// Pins a GPU-ready checkpoint for the worker's copy-out; eviction and
/// compaction skip it until `unpin_checkpoint`.
pub fn pin_checkpoint(&mut self, id: CheckpointId) -> Result<()> { self.cache.pin(id) }   // 2044-2046
pub fn unpin_checkpoint(&mut self, id: CheckpointId) -> Result<()> { self.cache.unpin(id) } // 2048-2050

Every copy-out site goes through it — helios/src/server/runtime.rs:2480, 2496, 2360, 2364, 2396, 2705, 2710 — with no direct cache.pin bypass in non-test code (verified by searching cache.pin/cache.unpin across helios/src). That is the convention the new code should match.

What the new code does: commit 9cca87ac removed the three inline if inbound.is_none() { pin_request } else { pin } branches from admit_planned and replaced them with a bool-dispatched pair:

/// Pins `id` for an active request, or for a host admission ticket's parent.
fn pin_prefix(&mut self, id: CheckpointId, request: bool) -> Result<()> {   // 2026-2032
    if request { self.cache.pin_request(id) } else { self.cache.pin(id) }
}
fn unpin_prefix(&mut self, id: CheckpointId, request: bool) -> Result<()> {  // 2034-2040
    ...
}

called as self.pin_prefix(id, request_pin)? (scheduler.rs:1645) and self.unpin_prefix(id, request_pin)? (scheduler.rs:1656, 1678), with let request_pin = inbound.is_none(); at scheduler.rs:1643.

Three consequences, all verifiable in the code:

Fix: pick one mechanism. Preferred, and it subsumes finding 1: fold the request accounting into the cache (pin_with) so pin/pin_request become named wrappers there, delete pin_prefix/unpin_prefix, and let the admission site bind the choice once in the existing pinned/request_pin pair. Alternative: keep the scheduler-level helper but give it a two-variant enum instead of a bare bool, and route 1889, 2977-2979, 2997-2998, 3155 through it too, so that request pins — like copy-out pins — have exactly one entry point.


Nits

3. docs/api.md documents a test hook and overstates the publish cadence

Where: docs/api.md:311-312 and docs/api.md:302-303.

Fix: drop the parenthetical about the drift assertion (or move it to the PR description / docs/TODO.txt) and replace "on every scheduler pass" with "on each scheduler pass that makes progress".

4. New gauges get no docs/runtime-core-design.md entry (carried over)

Where: docs/runtime-core-design.md:873-876 gives flare_snapshot_backing_pages a durable semantic definition ("counts page ids excluded by owned snapshots, including rows awaiting retirement. It counts overlapping exclusions once."). The two new gauges have only the docs/api.md paragraph and their HELP strings. The absence of rows in the docs/api.md JSON field table (docs/api.md:325-355) is correct and not a finding — that table maps JSON fields and the gauges are deliberately excluded from the snapshot, consistent with the "beyond the JSON fields below" sentence at docs/api.md:292-296. Only worth adding a design-doc sentence if the request-pin/request_pinned_pages 0↔1 bookkeeping is meant to be a durable specification beyond the exposition help text.


Checked and dropped (no convention or duplication problem)

Open questions / not settled

Link: https://github.com/NVIDIA/hyperflare-inference/pull/549