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

Raw Markdown for agents: https://autobot.eidonix.com/p/a7b98d92-0701-427a-9d7a-0f7f553e31e0.md

#1 [high] PR #615 overview + files inspected

PR #615 (newjordan) head 2652c06, base main@2147cd4. +562/-31 over 6 files.

#2 [high] Findings: PR #615 correctness/contracts/system lens

FINDING 1 (Major): UTF-8 trim is format- and grammar-agnostic; it silently drops up to 3 already-published tokens from the re-prefilled prompt for EVERY reasoning format and even under an active grammar.

FINDING 2 (Minor->Major): literal U+FFFD is indistinguishable from a provisional one, so a legitimately generated replacement character is silently deleted.

FINDING 3 (Minor): trim silently gives up in the documented-certain cases it claims to fix.

FINDING 4 (Minor): the injected tail is not all-or-nothing on a user token stop.

FINDING 5 (Minor, accounting/metrics): per-token accounting triples the hidden-token charge per cut.

FINDING 6 (Minor, architecture/charter): hardcoded marker literals in the request layer.

FINDING 7 (evidence): no CI on head; no live Inkling 2 evidence; #[ignore] real-checkpoint TML tests unrun; the synthetic phase test drives a scripted token stream, so "the injected pair keeps a real model in a text frame" is unverified (PR "Limitations" acknowledges the run gaps but not that the central behavioral claim rests on a fixture). Also: PR text "no visible transition text is inserted" is correct (docs/api.md:825) — verified via TmlOutput (Header->Body{Text} emits only subsequent text).

SOUND (verified, do not report): output_event_capacity (runtime.rs:68-88) still bounds publications because budget+after == original max_new_tokens and each injected token emits <=1 event; after reduced by injected.len() at runtime.rs:2011 keeps prompt+remaining == original limit so validate_prompt's context check (admission.rs:154-169) is unaffected; extend_text monotonicity (admission.rs:25-56) holds because at least one generated token survives; preemption passes &[] (runtime.rs:2121) and never trims; no platform-specific (Unix/Windows) code touched; no wire/proto/captured-graph contract change (ReasoningCut is in-process).

#3 [high] Research summary

I reviewed all six changed files plus their direct callees (TmlOutput, OutputPipeline, IncrementalDecoder, TokenStops/TextStops, Admission/PromptMetadataMut, scheduler decoded-history, metrics, http decoder) and the docs they touch.

Headline: the change is largely sound in its accounting. I verified that output_event_capacity still bounds publications (budget + after == the original limit, and after is reduced by injected.len() at runtime.rs:2011), that the context-length invariant in admission.rs:154-169 is preserved by construction, that extend_text monotonicity holds because at least one generated token always survives, that preemption still passes &[] and never trims, that the injected <|message_model|><|content_text|> legally re-enters Body::Text in TmlOutput (so the client pipeline publishes it as content, not reasoning), and that no wire/proto/captured-graph or platform-specific code is touched.

The main issue is that the UTF-8 trim in Worker::park() (runtime.rs:2129-2155) is gated only on !appended.is_empty(), so it fires for every forced reasoning cut in every format — including grammar-constrained requests, where the tail injection itself is gated on grammar.is_none() (runtime.rs:1962-1965). That silently deletes already-grammar-advanced, already-published tokens from the re-prefilled prompt, diverging grammar state from prompt history, while docs/api.md:817-823 documents the behavior only for TMLv0/ContentHeaders. Related: the U+FFFD test cannot distinguish a legitimate generated replacement character from a provisional one (the repo notes this distinction explicitly at token_spans.rs:92-93), the trim silently no-ops when the partial sequence reaches the 8-token window start, and the injected tail is not actually all-or-nothing on a user token stop (runtime.rs:1973-1989 breaks mid-sequence, leaving TML in State::Header), contradicting the PR's "all-or-nothing" wording. Minor items: per-cut hidden-token accounting went from 1 to up to 3 in generated_tokens/usage while prompt_tokens does not reflect it, and the new helper hardcodes <|message_model|>/<|content_text|> in the request layer even though OutputPipeline::envelope_markers(TmlJson) / declared_markers already expose exactly those literals (charter's model-agnostic rule). Evidence is CPU-only with a synthetic tokenizer; the central claim that the injected pair keeps a real Inkling 2 model in a text frame is unverified, and no CI exists on the head commit.

Full findings with verbatim quotes, severities, impact and fixes are in scratchpad note 2.

#4 [high] Verbatim quotes + coverage gaps backing each finding

Companion to note 2. Every quote below was read directly from the PR head (2652c06).

F1 (trim is format/grammar-agnostic). The trim gate, helios/src/server/runtime.rs:2133: if !appended.is_empty() && tokens.len() > live.prompt_tokens { versus the tail injection, which is grammar-gated, runtime.rs:1962-1965: let include_tail = !cut.close_tail.is_empty() && request.incoming.prompt.grammar.is_none() && u64::from(1 + cut.close_tail.len() as u32) < u64::from(cut.after); appended is &injected for any forced cut (runtime.rs:2013: let appended: &[u32] = if forced { &injected } else { &[] };), so DeepSeek V4.1 / XTML / ThinkTags / Recipient / DSML cuts (which have empty close_tail, injected == [close]) trim too. Docs scope it to TML, docs/api.md:817-821: reasoning (ContentHeaders), when no tools or grammar are active and output capacity remains beyond the frame opener, Flare also emits the silent <|message_model|><|content_text|> control tokens after <|end_message|> so continuation enters a text frame directly; when tools or a grammar are active, only <|end_message|> is emitted. Any incomplete multi-byte UTF-8 tail tokens at the cut boundary are trimmed from the re-prefilled continuation context before the close token is appended. Grammar/state divergence: the worker advanced the grammar over those tokens (runtime.rs:1822-1838 open grammar.advance, and Active::close_reasoning runtime.rs:432-438), and the client decoder already emitted their text, yet the token disappears from the parking prompt (runtime.rs:2150 tokens.truncate(end);) so the re-admitted continuation's mask no longer matches what was produced. Fix options: put a trim_utf8 (or keep_tail_tokens) flag on ReasoningCut and set it only in the TML path of request.rs:384-393; or add && request.incoming.prompt.grammar.is_none(); then scope docs/api.md:821-823 to what remains.

F2 (literal U+FFFD cannot be distinguished). runtime.rs:2136-2139: if self .frontend .decode(&tokens[window_start..], false) .is_ok_and(|text| text.ends_with('\u{FFFD}')) { The repo states the distinction is only available with bytes — helios/src/server/token_spans.rs:92-93: // Exact byte ranges distinguish a literal U+FFFD from provisional UTF-8 // replacement, and map every byte token of one character to its start. A byte-fallback tokenizer really can emit U+FFFD content — helios/src/server/stops.rs:340-341 vocab contains "<0xEF>", "<0xBF>", "<0xBD>" and stops.rs:454 tests TextStops::new(&frontend, vec!["\u{fffd}".into()]). Such a tail is silently truncated instead of kept. Fix: require the trailing tokens' raw bytes (frontend.token_bytes, used at token_spans.rs:56) to be an incomplete UTF-8 prefix before trimming, or reuse byte_starts' exact-range logic.

F3 (silent no-op). runtime.rs:2141-2153: let max_trim = 3usize.min(generated_len.saturating_sub(1)); for trim in 1..=max_trim { let end = tokens.len() - trim; if end > window_start && self .frontend .decode(&tokens[window_start..end], false) .is_ok_and(|text| !text.ends_with('\u{FFFD}')) { tokens.truncate(end); break; } } If the partial sequence straddles window_start (runtime.rs:2135: let window_start = tokens.len().saturating_sub(8).max(live.prompt_tokens);) every candidate fails end > window_start, so nothing is trimmed and nothing is reported — while docs/api.md:821-823 assert the trimming happens. Fix: bound the scan by incoming.prompt.tokens.len() plus a wider window, or document the bound.

F4 (tail not all-or-nothing). runtime.rs:1973-1982: for token in sequence { match request.close_reasoning(token, &self.config.eos_token_ids) { Ok(stopped) => { injected.push(token); if stopped { budget_stop = true; break; } } close_reasoning returns a user token-stop match via Active::emit_output (runtime.rs:431-444 -> 466-478 TokenStops::push), so <|message_model|> can be injected without <|content_text|>, leaving TmlOutput in State::Header { model: true } (tml.rs:272-284: MODEL => State::Header { model: true }) and finishing with reason "length" (runtime.rs:2055). The PR body claims "(all-or-nothing when grammar.is_none() and 1 + cut.close_tail.len() < cut.after)".

F5 (accounting). runtime.rs:1991-1999: if forced && !abandon { for &token in &injected { self.scheduler .record_decoded_token(request.request, token)?; } self.metrics .reasoning_budget_cuts .fetch_add(1, Ordering::Relaxed); self.metrics.progress(0, injected.len(), 0, 0, 0, 0); } plus runtime.rs:2063 generated_tokens: request.carried.generated_tokens + generated, and metrics.rs:1055-1056 ("Committed generated tokens, including host-forced tokens, EOS and tokens subsequently withheld by text stops."). Main charged 1 token per cut; this charges up to 3 into usage/flare_generated_tokens_total while prompt_tokens (runtime.rs:2059-2062) does not reflect the 3 tokens added to the prompt. docs/api.md:823-824 only says forced tokens count as generated tokens, not that the per-cut count tripled.

F6 (hardcoded literals in the request layer). request.rs:543-555: fn tml_text_transition_tokens(frontend: &Frontend, config: &ModelConfig) -> Vec { if frontend.prompt_encoder() != crate::frontend::PromptEncoder::TmlV0 || config.contract.frontend.reasoning.as_ref().map(|r| r.kind) != Some(crate::model::contract::ReasoningParserKind::ContentHeaders) { return Vec::new(); } let vocab = frontend.tokenizer().get_added_vocabulary().get_vocab(); match (vocab.get("<|message_model|>"), vocab.get("<|content_text|>")) { The same literals already flow through pipeline.rs:135 (ToolParserKind::TmlJson => &super::tml::CONTROL_MARKERS) and pipeline.rs:147-177 declared_markers, and prepare_output resolves control tokens with frontend.encode (model/prompt.rs:319-328). Charter: model-agnostic runtime, no per-model branches (#267/#334).

F7 (evidence). PR body's own limitations: TLA+ runner not executed; #[ignore] real-checkpoint tml_tests.rs (needs FLARE_INKLING2_TEST_SNAPSHOT) and live GPU flare run not re-run. All new runtime tests use Fixture::with_template synthetic tokenizers (phase_tests.rs:680-900), i.e. a scripted token stream, so the claim that <|message_model|><|content_text|> keeps a real Inkling 2 model in a text frame is unproven. No CI checks on head 2652c06.

TEST GAPS (concrete): no test drives the trim on a non-TML cut (e.g. budget_cut with close_tail empty on a </think> syntax), no test with prompt.grammar = Some(..) and a brace cut (the case where F1 bites hardest), no test for a literal-U+FFFD tail (F2), none for continuation with a user token stop mid-tail (F4), and none where the partial sequence reaches window_start (F3).

DOCS NOT UPDATED (search hits only, files not opened in full): docs/logprobs-design.md:19 ("Forced tokens, such as the thinking_token_budget close, have no logprob entry." — singular, still accurate in spirit), docs/cli-design.md:1337/1367/1417 describe [server] thinking_token_budget without the TMLv0/ContentHeaders case.

VERIFIED-SOUND (do not report as issues): event-capacity bound holds (runtime.rs:68-88; budget + after unchanged, after reduced by injected.len() at 2011); context invariant holds (admission.rs:154-169) because prompt grows by exactly injected.len(); extend_text monotonicity holds (admission.rs:25-56) since generated_len >= 1 survives; preemption path passes &[] (runtime.rs:2121) so it never trims; prompt_start (tml.rs:79-93) reports Content for the re-prefilled prompt ending in <|content_text|>, so the continuation parses as a text frame; the phase test's OutputPipeline assertion (reasoning "a", content "bc", phase_tests.rs:760-775) confirms no visible transition text; no Unix/Windows divergence, no wire/proto/core-ABI change (ReasoningCut is in-process).