Lens D (simplicity) review of PR #548 — Add request latency histograms and process metrics
Key findings
The PR is observability-only and its behaviour claims check out; nothing in this lens blocks it. The simplicity cost is that it adds a third histogram mechanism to a file that already renders histograms two other ways, and a second metric writer to a module that has one. Both could be avoided, and doing so would shrink the diff by roughly the size of the new generic machinery (Buckets/Histogram, ~90 of the 545 added lines, plus the parallel render path in latency.rs).
Governing rules cited below: AGENTS.md L188-193 ("Reject unsupported abstractions and churn … Do not add frameworks, extension points, speculative compatibility, fallback layers, or configuration switches without a concrete requirement"), AGENTS.md L194-199 ("Require focused, meaningful validation … source-text checks … cannot establish runtime correctness"), AGENTS.md L338 and docs/design-review.md L16 ("Keep Helios as simple as possible"), docs/design-review.md L125-128 (scope), docs/design-review.md L138-151 with .agents/skills/repo-prose/SKILL.md (not opened) (prose).
Non-findings I checked and dropped: state.model.metrics_handle() (http.rs:603) is required by the filter_map closure; if !count correctly excludes /v1/messages/count_tokens because server.rs:584 routes that path to the same http::generate; RequestTimes survives preemption because park moves Incoming wholesale and readmission reuses it; request_prompt_tokens is a genuine requirement of #536, so it stays.
Should fix
1. Two new histogram mechanisms beside the two that already exist — metrics.rs:200-210, 212-298, 851-877, 1029-1065
metrics.rs already renders Prometheus histograms twice: the host prefix-store series from HOST_TOKEN_BUCKETS/HOST_TIME_BUCKETS ([u64; N], u64::MAX as +Inf) at 1029-1065, and flare_host_cache_retirement_seconds at 851-877. The PR adds a third, differently-shaped one — Buckets<const N> with f64 bounds ending at f64::INFINITY plus a scale divisor, and Histogram<N> whose render takes an &str name and formats its own # HELP.
Three implementations of one exposition means three places to fix a bug (e.g. +Inf spelling, cumulative counting, escaping) and two bucket vocabularies in one struct.
Fix: make Histogram<N> the one mechanism — convert the host and retirement series onto it and delete HOST_TOKEN_BUCKETS/HOST_TIME_BUCKETS, the 1029-1065 loop and the 851-877 block. If that migration is judged too large for this PR, the smaller version is to drop the new generic type and express the new seconds histograms as [u64; N] bucket arrays like the existing ones, since request_queue/request_prefill/request_decode/request_e2e/time_to_first_response all share one bound list.
2. The scale: f64 field is redundant — metrics.rs:214-241, 260-270, 291-295
Every observation the PR makes is already an integer nanosecond count (as_nanos() as u64 in http.rs:615/632 and latency.rs:115; saturating_sub differences in request_completed). scale exists only so observe can divide by it (metrics.rs:262: value as f64 / buckets.scale) and render can divide the sum by it (metrics.rs:295). It also forces the PROMPT_TOKENS struct literal to carry a pointless scale: 1.0 (metrics.rs:239) and forces the comment at 261-262 ("Division is correctly rounded, so an integer grid point lands exactly on a bound written as the same decimal") to justify an invariant the design should not have.
Fix: keep bounds in nanoseconds as [u64; N], bucket with integer comparison, and divide once when writing _sum. That removes the scale field, the PROMPT_TOKENS special case, and the rounding comment. It also makes the proposal in finding 1 trivial, since the host series are already ns integers.
3. A second metric() writer — helios/src/server/metrics/process.rs:42-49 vs metrics.rs:1597-1602
metrics::metric(text, name, kind, help, value: u64) already emits exactly this shape (with the flare_ prefix baked in). The new module defines another with the same name, same body modulo the prefix, and value: impl std::fmt::Display. Nothing calls it with a non-u64/f64 and nothing calls it with a flare_-prefixed name, so the widening buys nothing.
Fix: keep the existing writer and pass it pre-formatted values, or widen the single metrics::metric to impl Display and delete the copy. One writer, one prefix convention.
4. render_histogram is a third impl block whose only content is a hard-coded name — latency.rs:33-37, 122-131, server.rs:671
FirstResponseSamples grew a histogram field (latency.rs:36) and a second impl block (latency.rs:122-131) that forwards to Histogram::render with a literal name and help string; server.rs then has a one-line scrape hook at 671. This exists only because the histogram was attached to the first-response sampler rather than to Metrics, which already owns request_e2e (metrics.rs:327) and which the scrape renders anyway.
Fix: put time_to_first_response: Histogram<21> on Metrics, observe it from FirstResponse::output through the metrics handle http.rs:603 already fetches, and render it in the same loop as the other six at metrics.rs:1389-1424. That deletes the extra impl block, the second field, the pub(super) render_histogram API and server.rs:671. Related: http.rs:611-615 and 632 are the same if !count { …observe(REQUEST_SECONDS, admitted_at.elapsed()…) } expression written twice; one helper on the state (e.g. state.record_e2e(admitted_at)) would keep the exclusion rule in one place instead of two.
5. Four speculative per-source fallbacks cost more than the metrics they guard — metrics/process.rs:55-131
render_linux builds Options for sysconf(_SC_CLK_TCK), sysconf(_SC_PAGESIZE), getrlimit, the read_dir result and the parse_stat result, then re-tests them in five combinations (if let (Some(stat), Some(ticks)), if let Some(stat), if let (Some(stat), Some(page)), if let (Some(stat), Some(btime), Some(ticks)), …). On Linux sysconf for these two names cannot fail, and getrlimit(RLIMIT_NOFILE) is unconditional. The module doc (process.rs:1-6) elevates this to a promise — "a scrape never fails and never panics" — that nothing in the repository requires of process_*; AGENTS.md L188-193 asks for a concrete requirement for exactly this kind of fallback layer.
Also note the repo already has a precedent for the page size: eos/src/unix.rs:74 (not opened) uses unwrap_or(4096).
Fix: read both sysconf values once, default the page size as eos/src/unix.rs:74 (not opened) does, and keep only the fallbacks that a real failure mode justifies (/proc reads, which genuinely can be absent in a container). That collapses five if let combinations to two and the doc comment to one sentence.
6. let _ = text; exists only to silence an unused parameter — metrics/process.rs:11-15
The non-Linux branch is a no-op whose sole effect is to keep the parameter used. Other platform-gated code in helios does not do this: runtime_core/protection.rs:298-301 gives the non-Linux branch a real body (None), and core/cache.rs:4196 gates whole items.
Fix: gate the render item itself (#[cfg(target_os = "linux")]) and gate the call site at server.rs:709 the same way, deleting the empty branch. If the call site must stay unconditional on every platform, keep the non-Linux arm but let the compiler see the parameter as used by an actually-gated signature rather than a discard.
Nits
7. parse_stat skip arithmetic is harder to check than field names — metrics/process.rs:21-32
The chain value(11)? / value(0)? / value(6)? / value(0)? / value(0)? plus "stat field N is token N - 3" requires the reader to hold two offset conventions at once to confirm the test at process.rs:169-175.
Fix: let mut fields = tail.split_whitespace(); for _ in 0..11 { fields.next(); } then read the five fields in order with one comment naming them.
8. last_token_ns duplicates scheduler accounting — runtime.rs:1774-1775 vs scheduler/policy.rs:275
The scheduler already records the last committed output instant in accounting.last_output_ns (set at policy.rs:275, surfaced at runtime.rs:1173, consumed at policy.rs:478). Only the first token instant has no existing home. Stamping both in Worker::complete adds a u64 per in-flight request plus a store per committed token.
Fix: keep first_token_ns, read the last-token instant from accounting. If the accounting value is unsuitable because it advances on publication rather than token commitment, say so in a comment — that is the only fact that would settle it, and it is not currently stated.
9. Comment bloat around three fields — metrics.rs:212-213, 243-244, 261-262, 300; runtime.rs:193, 2189-2190; process.rs:1-6
Four doc comments cover three struct fields; metrics.rs:261-262 defends an invariant finding 2 removes; runtime.rs:2189-2190 ("Stamp here rather than at submission so a request that reaches a model step has one, including requests driven through reserve_and_pack directly") only parses if the reader already knows reserve_and_pack has a second, non-obvious caller. The prose skill asks comments to state the invariant, not to defend the placement.
Fix: one line each on Buckets, Histogram and RequestTimes; drop 261-262 with the scale change; reduce runtime.rs:2189-2190 to the fact ("first model step that includes this request"), or leave it out entirely if the name says it.
10. Half of request_histograms_bucket_completed_request_timings proves the helper, not the behaviour — metrics.rs:1674-1701
The second request_completed call (first_scheduled_ns: None) asserts the helper's branch by observing counts stay at 1. That is a unit check of request_completed, which is legitimate, but it is not evidence for the PR's claim (that the runtime stamps the fields). The runtime-side check is phase_tests.rs:576-583, and it asserts only _count 1 — a histogram with every observation in the +Inf bucket would pass. AGENTS.md L194-199.
Fix: have the phase test assert one concrete bucket bound as well as the count (e.g. the parked-and-readmitted request lands in the first flare_request_queue_seconds_bucket), or move the None case into a test named for request_completed so the two claims are not conflated.
11. render() string assertions bolted onto three unrelated tests — phase_tests.rs:4428-4432, 4461-4471, 5380-5389
These add HELP/bucket-text checks to first_response_http_stream_*, first_response_http_nonstream_* and metrics_route_*. The first two restate what the count assertions on the same tests already prove and what metrics.rs:1674-1701 covers; they also make unrelated tests fail on a HELP-string edit.
Fix: keep only the process_resident_memory_bytes presence check inside the route test, as the one end-to-end scrape check, and keep the _count assertions where they are. (This is a judgement call: three of the four added assert blocks are redundant, not all.)
12. docs/api.md:297-320 repeats the HELP strings verbatim — docs/api.md
Eleven series are enumerated with definitions that duplicate the # HELP text now in metrics.rs:1389-1424. The two facts a scrape cannot tell a reader are that histograms are absent from format=json and that process_* is Linux-only.
Fix: keep those two sentences, list the names in one compact run, and let the scrape carry the definitions. AGENTS.md L188-193 treats comment/prose expansion by itself as not a contribution; the same standard makes this list worth trimming.