Subtractive scratchpad · NVIDIA/hyperflare-inference #548

Raw Markdown for agents: https://autobot.eidonix.com/p/88474981-0f3f-4b76-9a8f-3a6bb084ba69.md

#1 [high] Should fix: process.rs re-implements existing helpers and keeps fallbacks for sources that cannot fail (~70 lines)

File: helios/src/server/metrics/process.rs (new, +184).

(a) Duplicate metric writer. process.rs:42-49 defines fn metric(text,name,kind,help,value: impl Display) that is metrics::metric (helios/src/server/metrics.rs:1597-1602) minus the flare_ prefix and with a Display value. Call it from metrics.rs/server.rs with full names and one helper disappears (~9 lines). Same shape is also open-coded at metrics.rs:1626-1636 and server.rs:669, so the repo already tolerates one writer.

(b) Duplicate procfs/resource-limit readers:

(c) Fallback chains for sources that cannot fail. render_linux (process.rs:51-133, 83 lines) reads /proc/self/stat, /proc/stat btime, /proc/self/fd, RLIMIT_NOFILE as Options and emits each metric behind if let (Some(..), Some(..)) (6 blocks, ~54 lines). On Linux /proc/self/stat, /proc/stat and /proc/self/fd always exist and getrlimit(RLIMIT_NOFILE) always succeeds, so 6 of the 8 fallbacks are dead. Smaller version: one early return / unwrap_or_default per source and 6 straight metrics::metric(...) calls:

let Some(stat) = ... parse_stat(...) else { return };
metrics::metric(text, "process_cpu_seconds_total", "counter", "...", (stat[0]+stat[1]) as f64/ticks as f64);
... (5 more, one line each)

Saves ~30 lines of scaffolding plus the -> Option plumbing in parse_btime (process.rs:34-40).

(d) Test that proves the OS, not the code: render_reports_process_memory_descriptors_and_start_time (process.rs:157-183, ~27 lines) asserts live /proc values and that six # TYPE lines exist; the phase test metrics_route_serves_prometheus_or_json_without_the_api_key (runtime/phase_tests.rs:5387-5388) already asserts a process_* series reaches a scrape. Keeping only parse_stat_reads_fields_after_a_parenthesized_command_with_a_paren (process.rs:148-155) loses nothing. ~25 lines.

Total process.rs: ~70 of 184 lines (38%), no behaviour that a Linux run can observe.

Scope note: #536's missing-metrics table asks for process_cpu_seconds_total, process_resident_memory_bytes, process_open_fds, process_start_time_seconds only. process_virtual_memory_bytes (process.rs:88-96) and process_max_fds (process.rs:124-132) are not requested and virtual size is uninformative for a GPU server; dropping both removes another ~18 lines plus two doc lines (docs/api.md:316-317) and two test assertions (process.rs:163-164,178-179).

#2 [high] Should fix: request_completed keeps a per-request state that cannot occur and publish() recomputes two values

File: helios/src/server/metrics.rs:452-480 and helios/src/server/runtime.rs:1993-2019.

(a) metrics.rs:462-469 — the inner if let Some(first) = times.first_token_ns inside if let Some(scheduled) = times.first_scheduled_ns cannot fail. first_scheduled_ns is stamped in reserve_and_pack (runtime.rs:2189-2203) for every lane of every committed step, and first_token_ns is only stamped in Worker::complete (runtime.rs:1774-1775) for tokens of an active request whose step went through reserve_and_pack; so first_token_ns.is_some() ⇒ first_scheduled_ns.is_some(). Flatten to one if let:

if let Some(scheduled) = times.first_scheduled_ns {
    self.request_queue.observe(&REQUEST_SECONDS, scheduled.saturating_sub(arrival_ns));
}
if let (Some(scheduled), Some(first)) = (times.first_scheduled_ns, times.first_token_ns) {
    self.request_prefill.observe(&REQUEST_SECONDS, first.saturating_sub(scheduled));
}

(or one block using (Some(s), Some(f)) with the queue observation outside). The test at metrics.rs:1684-1697 constructs the impossible state (first_scheduled_ns: None, first_token_ns: Some(..)) and asserts on it; that half of the test goes too. ~5-8 lines including the test case.

(b) runtime.rs:1993-1998 and 2015-2019 compute the same two expressions twice: request.carried.prompt_tokens.unwrap_or(prompt_tokens) and request.carried.generated_tokens + generated. Worker::complete already hoists them into locals (runtime.rs:1838-1840) before calling request_completed and building the event. Do the same in publish:

let prompt_tokens = request.carried.prompt_tokens.unwrap_or(prompt_tokens);
let generated_tokens = request.carried.generated_tokens + generated;
self.metrics.request_completed(request.incoming.arrival_ns, request.incoming.times, prompt_tokens, generated_tokens);
... GenerationEvent::Finished { prompt_tokens, generated_tokens, .. }

~4 lines, and the two call sites become identical in shape.

#6 [high] Research summary

Reviewed PR #548 (8 files, +545/−20) against the charter, AGENTS.md and docs/design-review.md, strictly for subtraction. The request histograms themselves are the #536 deliverable and are minimal; the removable weight is concentrated in the new process.rs (184 lines) and in the fact that the new Histogram type was added beside the hand-rolled host histograms already in metrics.rs.

Findings, ranked by lines saved (all in the scratchpad with citations):

  1. Should fix, ~70 of 184 lines in helios/src/server/metrics/process.rs. A second metric writer duplicating metrics::metric (metrics.rs:1597), duplicate procfs readers (open_descriptors protection.rs:291, flare_eos::raise_open_file_limit(0), flare_eos::page_size()), and six dead if let (Some, Some) fallbacks around /proc sources that always exist on Linux. ~25 more lines in a test that asserts live /proc values already covered by the scrape test.
  2. Should fix, ~45 lines. The new Buckets/Histogram coexists with the hand-rolled HOST_*_BUCKETS + host_cost_sums + 37-line render loop (metrics.rs:200-210, 382-385, 1029-1065); converting the three host histograms deletes the old machinery. The e2e observation is also written twice (http.rs:614, 631) plus a needless Arc clone, and can be one call before the single yield Delivery::Complete (http.rs:598).
  3. Should fix, ~13 lines. request_completed nests a first_token_ns check that cannot fail (metrics.rs:462-469) and its test constructs the impossible state; Worker::publish recomputes the prompt/generated expressions twice (runtime.rs:1993-2019) where Worker::complete already hoists them.
  4. Nits, ~19 lines. Non-Linux shim for a function that writes nothing, second impl block in latency.rs, docs/api.md restating the HELP strings verbatim, duplicated histogram assertions in phase_tests.

Total: about 145 of the 545 added lines (~27%) removable with no behaviour lost; the change is not blocking on size.

#7 [high] Tally and framing for the final report (change size, per-finding savings, verdict)

Change size (from pr_files / diff): 8 files, +545/−20.

Ranked savings (details in notes 1-5, each title starts with its severity):

  1. Should fix — helios/src/server/metrics/process.rs: ~70 of 184 lines (38%).
  2. Should fix — helios/src/server/metrics.rs:212-298 + metrics.rs:1029-1065 + http.rs:603,614-617,631-632: ~45 lines.
  3. Should fix — metrics.rs:452-480, runtime.rs:1993-2019, metrics.rs:1684-1697: ~13 lines.
  4. Nit — process.rs:8-14 + server.rs:709 (shim, ~6), latency.rs:122-131 (2), docs/api.md:300-313 (~6), phase_tests.rs:4466-4470 (~5): ~19 lines. Total ~147 of +545 (~27%); ~100 of that is in the one new file. Verdict: not blocking on size; no finding asks for a new feature/check/test/option.

What is already minimal (say so plainly in the report): the five request histograms are exactly the #536 "Missing metrics" table (issue #536, opened 2026-11-26); the RequestTimes fields plus the two stamps (runtime.rs:1774-1775, 2189-2203) are the minimum that yields queue/prefill/decode intervals, and preemption preservation is free from Incoming moving into the parked list, not added code. Everything is observability-only; the description claims no performance change, which matches — but charter line 35 / AGENTS.md:165-169 ("what gets merged") means the case rests entirely on #536 being the maintainer-visible request it references, and #536 is a community issue, not a maintainer request. Worth one sentence, not a finding.

Charter/guidance citations to use: charter "Solve the right problem in the right place" (smallest footprint, way to solve without new code at all) and "Keep the runtime small"; AGENTS.md:188-193 ("Reject unsupported abstractions and churn… do not add frameworks… fallback layers"); AGENTS.md:194-199 ("Tests must exercise the affected behavior… source-text checks… cannot establish runtime correctness"); docs/design-review.md principle 1 ("Keep Helios as simple as possible"). Also AGENTS.md:448-451 (no env switches — not violated here; don't cite).

Note on the PR's own evidence: the author's live scrape and 109-test run are credible but include the agent session's own requests (stated in the description), so the histogram counts exceed the three test requests; not a subtraction finding, only context if the report mentions the evidence.

#3 [medium] Should fix: new Histogram type coexists with the hand-rolled host histograms; e2e observation written twice (~45 lines)

File: helios/src/server/metrics.rs:212-298 (new Buckets/Histogram, ~87 lines) and helios/src/server/server.rs-adjacent call sites.

(a) The new generic histogram duplicates machinery that already exists in the same file. HOST_TIME_BUCKETS/HOST_TOKEN_BUCKETS plus the parallel arrays host_missing_tokens/host_transfer_ns/host_prefill_ns and host_cost_sums (metrics.rs:200-210, 382-385) and the 37-line render loop (metrics.rs:1029-1065) do exactly what Histogram<N> + Buckets<N> do. Smaller version: convert the three host histograms to Histogram<8> and delete HOST_TOKEN_BUCKETS, HOST_TIME_BUCKETS, host_cost_sums, the .zip(limits) bucket loop and the .position(...).unwrap() in host_lookup_cost (metrics.rs:670-692) — the existing host_outcomes_and_capacity_have_bounded_labels_and_cost_histograms test (metrics.rs:1740-1796) keeps passing unchanged because it only reads the rendered text. ~40 lines net saving after subtracting what the PR adds (the PR currently adds the type alongside the hand-rolled version, so both exist).

(b) The e2e observation is written twice, once per delivery shape (helios/src/server/http.rs:614-617 and 631-632), and needs a second Arc clone only for the streaming arm (let metrics = state.model.metrics_handle(); at http.rs:603). Every successful generation yields exactly one Delivery::Complete from the generator body (http.rs:598, the only such yield); failures return after Delivery::Failed. Recording once in the generator immediately before that yield replaces both match-arm copies:

if !count {
    state.model.metrics().request_e2e.observe(&super::metrics::REQUEST_SECONDS, admitted_at.elapsed().as_nanos() as u64);
}
yield Delivery::Complete(response);

Deletes the metrics_handle() line, both if !count {...} copies, and keeps the same timing (the generator reaches that point only when the client polls to completion, which is what the two arms test today). ~5 lines and one Arc clone removed.

#4 [medium] Nit: thin platform shim, second impl block, docs restating HELP text, duplicated test assertions (~20 lines)

Smaller items, all verified:

(a) Nit — non-Linux shim. helios/src/server/metrics/process.rs:8-14 wraps render_linux so other platforms compile a function that writes nothing (#[cfg(not(target_os="linux"))] let _ = text;), and metrics.rs:12 pub(super) mod process; is unconditional. The repo already cfg-gates platform modules (helios/src/runtime_core/protection.rs:291-302, eos crate). Smaller: #[cfg(target_os = "linux")] pub(super) mod process; in metrics.rs:12 and #[cfg(target_os = "linux")] metrics::process::render(&mut text); at server.rs:709, dropping the wrapper and the let _ = text arm. ~6 lines, and one less dead path to reason about on Windows builds (which the description says were not run).

(b) Nit — latency.rs:122-131 adds a second impl FirstResponseSamples block for render_histogram instead of putting it in the existing impl at latency.rs:63-101. 2 lines of churn.

(c) Nit — docs/api.md:300-313 restates, sentence for sentence, the HELP strings added at metrics.rs:1387-1425 ("from model-scheduler arrival to the first model step...", "decode time divided by generated tokens after the first", etc.). One of the two is enough for an operator; the API doc could keep the names and the one non-obvious rule (cancelled/failed excluded, prefix-reused tokens counted) and drop the four start/end clauses, ~6 lines.

(d) Nit — duplicated assertion. phase_tests.rs:4429-4432 and 4464-4470 check e2e/ttfr counts in two separate tests and metrics_route_serves_prometheus_or_json_without_the_api_key (phase_tests.rs:5379-5388) checks the same series through a real scrape. The scrape test covers the rendering; the two per-test assertions could keep only the behavioural bit each proves (a failed stream leaves e2e at 0; one offered body leaves it at 1) and drop the re-render of the histogram at 4466-4470, ~5 lines.

Verified-minimal parts (do not ask to cut): the five histogram definitions themselves are the #536 deliverable (issue #536 "Missing metrics" table); the RequestTimes field additions and the two reserve_and_pack/complete stamps are the minimum that produces queue/prefill/decode intervals, and the parked-request preservation falls out of moving Incoming into the parked list (runtime.rs:2058+ park path) rather than any added code; RequestTimes derives Debug unused, 0 lines. The state.first_response.render_histogram split (latency.rs:123-131, server.rs:671) is needed because the first-response histogram lives outside Metrics.

#5 [medium] Correction: open_descriptors lives behind the runtime-core feature; move it to eos

Correction/adjustment to entry 1(b): open_descriptors() (helios/src/runtime_core/protection.rs:291-302) is not directly callable from the server — runtime_core is a separate cargo feature (helios/Cargo.toml:21,25; server does not enable it). So reuse needs one of: move that 8-line fn into flare_eos (eos/src/unix.rs already holds the platform split, and helios/src/lib.rs:36 already re-exports from it), or keep the duplication. Prefer moving it: flare_eos is an unconditional dependency of helios (Cargo.toml:34) and already owns /proc and rlimit access (page_size unix.rs:73-75, windows.rs:152; raise_open_file_limit unix.rs:138-165). Either way the process.rs copy at lines 65-67, 68-77 and 55-58 collapses onto eos calls, saving ~15 lines and three unsafe blocks in the new file.

flare_eos::page_size() and flare_eos::raise_open_file_limit(0) are both reachable from server code today (helios/src/lib.rs:36, eos is not feature-gated), so 1(b)'s max_fds and page parts stand as written.

Also confirmed for the report: the hand-rolled metric in process.rs (43-49) is fine to fold into metrics::metric (metrics.rs:1597) — both live in helios::server, and metrics::metric is pub(super) within server.