Correctness review — PR #548 "Add request latency histograms and process metrics"
Head ebecef1, base main, 8 files (+545/−20). Lens: correctness — does the change work everywhere it reaches, across every caller, configuration, platform and edge path.
Key findings
Two should-fix issues, both about flare_request_e2e_seconds membership versus the runtime histograms and the new documentation, plus three nits in the new Linux process module. Nothing blocking: the counter/histogram coupling, the histogram arithmetic, the bucket rendering, the JSON snapshot and the non-Linux compilation all check out.
- Should fix —
flare_request_e2e_secondsincludes responses the runtime counts as cancelled, contradicting the invariant the new docs paragraph states (docs/api.md:312-313) and the PR description ("Cancelled and failed requests stay out of these histograms"). - Should fix — streaming e2e requires the client to drain the SSE body while non-streaming records before the body is polled, an operator-visible asymmetry the docs do not state.
Findings
Should fix 1 — e2e histogram observes cancelled (tool-limit) responses
Where: helios/src/server/http.rs:614-617 (stream) and helios/src/server/http.rs:631-635 (non-stream); cause at http.rs:471-476, http.rs:526, http.rs:563-567.
What the code does. When the output encoder hits the parallel-tool limit, the delivery loop marks the generation cancelled:
http.rs:471-476— onceencoder.tool_limit_reached, every event exceptFinished/AdmissionRejected/Erroris dropped (continue).http.rs:526— the decoder is finished withcrate::output::Termination::Cancelledinstead of the runtime's termination.http.rs:563-567—if encoder.tool_limit_reached { … generation.cancel(); }.
The terminal GenerationEvent::Finished still flows through the encoder and the loop ends with yield Delivery::Complete(response); (http.rs:598). Both delivery arms then observe e2e unconditionally inside the Complete arm, guarded only by !count (the count_tokens exclusion):
Delivery::Complete(_) => {
if !count { metrics.request_e2e.observe(&super::metrics::REQUEST_SECONDS, admitted_at.elapsed().as_nanos() as u64); }
Why it is wrong under this PR's own rule. On the cancelled-by-tool-limit path the runtime counts the request in metrics.cancelled, never in request_completed: the parked path increments self.metrics.cancelled and calls incoming.finish_cancelled(...) at runtime.rs:1334-1342, the active path at runtime.rs:1343-1364, and neither reaches Metrics::request_completed (metrics.rs:452-480). So the request lands in flare_requests_cancelled_total and in flare_request_e2e_seconds, but in none of flare_request_queue_seconds, flare_request_prefill_seconds, flare_request_decode_seconds, flare_time_per_output_token_seconds, flare_request_prompt_tokens, and not in flare_requests_completed_total.
The new docs paragraph states the opposite as an absolute: docs/api.md:312-313 — "Cancelled and failed requests enter only flare_requests_cancelled_total and flare_requests_failed_total." An operator comparing flare_request_e2e_seconds_count against flare_requests_completed_total or against the other request histograms is therefore comparing different populations. This also bears on the repository rule in AGENTS.md:182-186 about not weakening guarantees: the docs claim an invariant the code does not hold.
Nuance found while verifying. Because generation.cancel() at http.rs:565 is inherently racy against the runtime's own natural-finish path, a tool-limit request may additionally be counted by request_completed (runtime.rs:1993-1998) if the runtime finishes before it observes the cancel flag. In that case the same request appears as completed and the client still sees Termination::Cancelled. Membership of these requests in the histograms is thus timing-dependent, which is worse for dashboards than a fixed rule.
Fix. Either (a) gate the e2e observation on the response not being cancelled — e.g. skip the observation in the Complete arm when the encoder reported tool_limit_reached, or thread the termination through Delivery::Complete and observe only for successful terminations — or (b) narrow docs/api.md:312-313 so the "only cancelled/failed counters" sentence covers the five runtime histograms and say explicitly that e2e measures every response delivered to the client. Option (a) keeps the stated invariant; option (b) is a docs-only change but must then also explain the e2e/count divergence. A test that drives a request past the parallel tool limit and asserts flare_request_e2e_seconds_count versus flare_requests_cancelled_total would settle which rule is intended.
Should fix 2 — streaming e2e depends on the client draining the body; non-streaming does not
Where: helios/src/server/http.rs:600-620 (stream) versus helios/src/server/http.rs:629-641 (non-stream).
What the code does. For streaming, the Delivery::Complete arm lives inside the filter_map closure that backs Sse::new(events) (http.rs:604-620), so flare_request_e2e_seconds is observed only if the client polls the event stream to its terminal element. A client that disconnects mid-stream — the ordinary cancel path for chat UIs — never reaches it, even though the generation may have completed server-side.
For non-streaming, the observation happens in the handler (http.rs:631-632) before the JSON body is handed to axum, so it records even if the client never reads the response. The PR's own test asserts this: first_response_http_nonstream_records_only_when_completed_body_is_offered (phase_tests.rs:4461-4465) expects flare_request_e2e_seconds_count 1 for the poll_body == false case.
Why it matters. flare_request_e2e_seconds_count diverges from flare_requests_completed_total in opposite directions for streamed and non-streamed requests, and streaming is the majority traffic for chat. Docs api.md:301-302 says only "counts each successful generation from admission to the completed response", with no mention of body drain — while the adjacent first-response sentence (api.md:303-304) does say "once the client reads it". That inconsistency in the same paragraph will read as a guarantee for e2e that the code does not give.
Fix. State the asymmetry in docs/api.md (streaming observations require the client to read to the end; non-streaming records when the handler produces the response), or record e2e on the runtime completion path so both transports behave alike and cancelled/abandoned requests are excluded uniformly. The streaming early-disconnect case needs a test (phase_tests.rs) to pin the chosen behaviour.
Nits
Nit 1 — vision steps end queue time (runtime.rs:2187-2208)
reserve_and_pack stamps first_scheduled_ns in a loop over all step.lanes (runtime.rs:2192-2203) before the if step.kind == GraphKind::Vision split at runtime.rs:2208. For an image request the queue observation therefore ends at the vision step and flare_request_prefill_seconds absorbs the vision work. docs/api.md:306-307 says "the first model step that includes the request", and a vision step arguably qualifies, so this may be intended — worth confirming rather than fixing. If vision is meant to be the queue boundary, a comment at runtime.rs:2192 would save the next reader the analysis; if not, restrict the stamp loop to generation kinds.
Nit 2 — parse_stat returns Some on ambiguous /proc/self/stat (metrics/process.rs:19-32)
The parser splits the stat line at the last ), the conventional heuristic for a parenthesized comm that may itself contain ) (proc(5) recommends it). Because it returns Some on any line with a ), a malformed or shifted line yields systematically wrong CPU/memory/start-time values rather than omitting the metrics, which the module doc (metrics/process.rs:1-6) says is the behaviour for sources that cannot be parsed. Practical impact is nil for flare's own comm, so this is a nit; an alternative is to validate that the tail has exactly the expected field count and otherwise return None.
Nit 3 — process_open_fds counts its own directory read (metrics/process.rs:65-67)
Iterating /proc/self/fd includes the handle doing the counting, so the value is +1 relative to Prometheus' client_golang, which excludes it. The HELP string for the series says so ("including the directory read that counts them" at metrics/process.rs:115-123), which makes it a documented deviation rather than a defect — noted only because operators comparing against stock process dashboards (the stated motivation in #536) will see the offset.
Verified correct (checked in code, no finding)
- Counter and histograms cannot diverge.
Metrics::request_completed(metrics.rs:452-480) is the only increment ofcompleted. The two former directfetch_addsites onmain(runtime.rs:1832,runtime.rs:1984) are now the call atruntime.rs:1841-1846(user/text stop inWorker::complete) andruntime.rs:1993-1998(natural finish inWorker::publish); no directcompleted.fetch_addremains underhelios/src/server. RequestTimessurvives preemption.parkmovesIncominginto the parked list and readmission reuses it, sofirst_scheduled_ns/first_token_nsare preserved;get_or_insertsemantics atruntime.rs:1774andruntime.rs:2201keep the earliest stamp. Thereasoning_budget_cuts_or_plainly_continues_with_exact_history_and_usagerun in the PR description exercises this.- Arithmetic is total.
generated_tokens > 1guards the decode/TPOT division (metrics.rs:470-478), so no divide-by-zero;saturating_subeverywhere; queue/prefill are only observed when their stamps exist. - Token stamping is skipped for failed/incomplete requests. Stamps are updated per emitted token at
runtime.rs:1773-1775, but a mid-step stop (runtime.rs:1780-1795) or abandon (runtime.rs:1951-1956) never reachesrequest_completed, so no partial observation is published. - Histogram rendering.
position(|bound| exposed <= bound)always finds an index because the last bound is infinite;+Infequals_countbecausecumulativeis reused from the same load;# HELP/# TYPEemitted with ascending buckets. flare_time_to_first_response_secondslives inFirstResponseSamples(latency.rs:34-37) and is rendered fromserver.rs:671after the median gauge; the JSON snapshot returns atserver.rs:645-658before any histogram rendering, soformat=jsonis unchanged as documented.- Prompt-token histogram semantics match the docs:
runtime.rs:1996usescarried.prompt_tokens.unwrap_or(prompt_tokens), the full prompt length including prefix-cache reuse, asdocs/api.md:311-312states. - Non-Linux build. Every
libcuse inmetrics/process.rs(getrlimit,RLIMIT_NOFILE,rlimit) is insidecfg(target_os = "linux"); the#[cfg(not(target_os = "linux"))]stub only discards its argument (metrics/process.rs:9-14) and thelibcdependency (helios/Cargo.toml:50) is already used byhelios/src/os.rs(not opened) under unix cfgs. No new non-Linux compile risk is visible in this file, though the Windows build was not run (disclosed). - Read failures degrade gracefully. Each metric is rendered from an independently read source, so one unreadable
/procfile omits only its own series and never fails the scrape.
Rules applied
- Charter: "Supported matrix & compatibility — Check interactions across every supported model, cache scheme, parallel layout, multi-host setup, and hardware target named in the docs" and the four-part PR-description expectation (
.autobot/charter-sourced; seeread_charterline 42). AGENTS.md:182-186— reject weakened guarantees;AGENTS.md:194-199— require focused validation and disclose unrun checks.docs/design-review.mdprinciple 12 — validate the real path, keep raw measurements, disclose unrun checks and material limitations.
Evidence limitations (as disclosed, and as I confirmed)
- The live scrape was taken from a cherry-pick of this diff onto a fork commit (AzallazA
5ffe4d54, because the served model needs the Engram encoding from PR #482), not onmain; the conflict resolution kept only the added function. The stated numbers are therefore evidence about this code, not about the branch's merge withmain. - No CI checks exist on head
ebecef1; the Windows build and the full helios suite were not run by the author (disclosed). The claim that this PR and #537 "merge in either order" is asserted, not tested — they touch different functions ofmetrics.rs(request histograms beside the completion counter here, page gauges besidecached_pagesthere), so it is plausible, but unverified. - The scrape window includes the authoring agent's own session requests; the description says so and the bucket deltas (
le="64"+1,le="2048"+4) are consistent with that explanation. Nothing in that evidence contradicts the code reading.
Open questions
- Is the tool-limit path supposed to appear in e2e (a client did receive a response), in which case
docs/api.md:312-313needs narrowing rather than the code changing? Finding 1's fix depends on that call. - Is a vision step intended to terminate queue time (
runtime.rs:2192-2203), or shouldfirst_scheduled_nsbe stamped only for generation lanes? - Should e2e be recorded at runtime completion for both transports so streaming and non-streaming agree (
http.rs:600-641)? That would also make cancelled/abandoned streamed requests consistently absent.