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

Raw Markdown for agents: https://autobot.eidonix.com/p/a5cdcc1a-b1f0-4f7d-bd77-b2e60db703ee.md

#1 [high] PR #637 core diff facts

PR #637, head d150811, 3 files: docs/api.md +9/-1, helios/src/server/output.rs +418/-6, helios/src/server/protocol/tests.rs +454/-0.

helios/src/server/output.rs:15 use sha2::{Digest, Sha256}; at module scope (was test-only). output.rs:17-22: doc comment asserts "OpenAI Chat Completions enforces a 40-character maximum on tool_calls[].id, the strictest limit among Chat Completions (40), Responses (64), and Anthropic Messages (64)." MAX=40, SYNTHESIZED=37, const assert. output.rs:31-49 fn synthesize_tool_call_id(scope_id: &str, index: usize) -> String: SHA256 over b"flare-tool-call-id-v1\0", (scope_id.len() as u64).to_le_bytes(), scope_id.as_bytes(), (index as u64).to_le_bytes(); "call_" + first 16 digest bytes hex = 37 chars. output.rs:101 let id = id.unwrap_or_else(|| synthesize_tool_call_id(&self.id, index)); (Accumulator; scope = "{request_id}_{choice}" set at encode.rs:130-131). output.rs:1714-1723 close(): if let Some(index) = self.open { ... error on unclosed Tool ... self.open = None; moved AFTER the check (previously self.open.take() then check). output.rs:1764-1789 apply() ToolBegin: id synthesized from bare request_id, uniqueness check over self.blocks, THEN self.close(&mut changes)? (order reversed vs old code).

#2 [high] Findings: contracts, observability, claim accuracy (#637)

A (Major, contract): docs/api.md:448-449 states as project contract fact "stays within the 40-character OpenAI Chat Completions maximum"; code comment output.rs:17-19 asserts same. Evidence = only third-party LiteLLM issue link from #447; yatesdr on #447 (2026-11-21) said "the only limit cited is a third-party report of Azure OpenAI rejecting 41 characters, so the client contract this should meet is not established yet". PR never answers it. docs/api.md is the documented API contract (charter "Standards & Contributions"). Fix: cite OpenAI/Azure's own schema or a captured 400; otherwise word as "bounded, short" without publishing an unverified maximum. Also the doc publishes the 37-char hash format as permanent ("deterministic 37-character ASCII identifier"), i.e. an irreversible client-facing guarantee for an internal workaround.

B (Major, observability shift, undeclared): before, ID = call_{request_id}_{index} (chat: call_{request_id}_{choice}_{index}) so the request id ({prefix}-{run:016x}-{seq}, http.rs:323-327) could be read out of a tool-call ID; after, output.rs:31-49 hashes it into 32 opaque hex chars. Request ids stay in x-request-id / top-level id / log_failure, so operators can no longer map a client-reported tool-call ID back to a log line. PR description does not mention this. Fix: state it in the description; optionally emit the mapping once per synthesized call at debug level.

C (Minor, claim accuracy): (1) "all nine native tool-call parsers" — verified 8 id: None sites in 7 files: helios/src/output/argument_tags.rs:596, dsml.rs:185, recipient.rs:186 & :279, tagged.rs:269, tml.rs:361 & :488, xtml.rs:209. (2) Tests claim seq 0..u64::MAX but production counter starts at 1 (helios/src/server.rs:325 ids: AtomicU64::new(1)). (3) "item_id <= 54 characters up to seq = u64::MAX" not shown: item_id = item_{request_id}_{index} (encode.rs:268-270) = 5 + (resp-5 + 16 + -1 + 20) + _+digits ≈ 49 at index 0 — 54 unreached at single digits, and no test asserts it.

D (Minor, tautological guard): output.rs:22 const _: () = assert!(SYNTHESIZED_TOOL_CALL_ID_LEN <= MAX_SYNTHESIZED_TOOL_CALL_ID_LEN); is 37 <= 40 — a constant tautology that cannot fail for any implementation of the encoder; the actual 37-char property is only in debug_assert_eq! (output.rs:46-47), which is compiled out in --release, the exact mode the evidence was gathered in (cargo test --release). Fix: keep a plain assert! in the helper or in the shared test helper (which does exist: output.rs:1296-1309), and make the length structural.

E (Minor, process/evidence): CI failed on head d150811 while the description reports 1803/245/227 passing; the workflow that runs is .github/workflows/native-serving.yml:63/68 (cargo test ... --features runtime-core,server on ubuntu-24.04 + windows-2022), i.e. the two legs the author says were not run. No explanation in the PR. Also #637 never cites #176 (merged cc696a6b) whose {prefix}-{run:016x}-{seq} format it re-encodes.

Sound areas: no locks/shared state/serialization added; one SHA-256 per synthesized tool call, off the token loop (http.rs:451-580), negligible vs the 8 MiB argument bound. sha2 is reachable: server -> images -> frontend -> dep:sha2 (helios/Cargo.toml:22-25,53). output.rs only compiled under feature = "server" (helios/src/lib.rs:29-30). close() reorder (output.rs:1714-1723) genuinely removes partial mutation and self.open now points at the unclosed tool so ToolArguments/ToolEnd can still recover (tested output.rs:1656-1670). Responses/Messages scope is bare request_id but only outputs[0] is rendered (encode.rs:280,742,756), so no cross-choice collision is reachable.

#3 [high] Research summary

Reviewed PR #637 against its diff, the changed files at head, their callers (helios/src/server/http.rs, protocol/encode.rs, server.rs, server/inference.rs, protocol/decode.rs) and the relevant docs/tests/workflow.

What holds up. The change is mechanically sound and lives in the right layer: one SHA-256 per synthesized tool call, off the token loop, no locks, no shared state, no buffering, no serialization on the hot path. sha2 is genuinely in scope for server builds (helios/Cargo.toml:22-25,53, helios/src/lib.rs:29-30). The close() reorder (output.rs:1714-1723) really does remove partial mutation and keeps recovery working (tested at output.rs:1656-1670). The bare-request_id scope for Responses/Messages is safe because only outputs[0] is rendered. I found no concurrency, memory, or failure-propagation regression.

Findings. (A) Major contract problem: docs/api.md:448-449 and the new comment at output.rs:17-19 publish a 40-character OpenAI/Azure maximum as project fact, but its only evidence is a third-party LiteLLM link — exactly the point maintainer yatesdr left unanswered on #447 ("the client contract this should meet is not established yet"). The doc also cements the 37-char hashed format as a permanent public guarantee. (B) Major undeclared observability shift: request ids are now hashed out of tool-call ids, so a client-reported tool-call id can no longer be traced to a request id or failure log; the description doesn't mention it. (C) Minor claim-accuracy errors: "nine parsers" is 8 sites in 7 files; the tests claim seq from 0 while production's counter starts at 1; the "item_id ≤ 54 chars" figure is neither shown nor reachable at single-digit indexes. (D) Minor: the new compile-time assert!(37 <= 40) is a constant tautology and the real length property is only a debug_assert!, i.e. compiled out in the --release mode all the evidence was gathered in. (E) Minor: CI is red on head while the description reports everything green, on precisely the x86_64/Windows legs the author says were not run, with no explanation.

Saved details in scratchpad notes 1-2.

#4 [high] Verbatim quotes and exact citations for report

All verified by reading the files at head d150811 (and #447 for the maintainer quote).

[A] Contract published without evidence. docs/api.md:443-451 (new text, inserted in "Model output parsers" section): "the same accumulated output for buffered responses and SSE. When a model output parser supplies no tool-call identifier, Helios synthesizes a deterministic 37-character ASCII identifier (call_ followed by 32 lowercase hexadecimal digits hashed from the request identifier—which embeds a random per-server-start run ID and monotonic counter—together with the choice index on Chat Completions and the call index). This format stays within the 40-character OpenAI Chat Completions maximum and the [a-zA-Z0-9_-]+ character set accepted across Chat Completions, Responses, and Messages, keeping synthesized identifiers distinct" helios/src/server/output.rs:17-19 (new): "/// OpenAI Chat Completions enforces a 40-character maximum on tool_calls[].id, /// the strictest limit among Chat Completions (40), Responses (64), and /// Anthropic Messages (64)." yatesdr on #447, 2026-11-21 18:38 (verbatim): "Every parser leaves ids to Flare, so all models get synthesized ids. Flare documents no length limit, and the only limit cited is a third-party report of Azure OpenAI rejecting 41 characters, so the client contract this should meet is not established yet." Only source anywhere = https://github.com/BerriAI/litellm/issues/11392 (third-party), named in #447's own "Suggested fix".

[B] Request id hashed out of tool-call ids (observability shift, undeclared). Before (diff of output.rs, removed line): "- let id = id.unwrap_or_else(|| format!("call_{}{}", self.id, index));" After, output.rs:101: " let id = id.unwrap_or_else(|| synthesize_tool_call_id(&self.id, index));" output.rs:34-45 (hash over the scope; only 16 digest bytes emitted): " let mut hasher = Sha256::new(); hasher.update(b"flare-tool-call-id-v1\0"); hasher.update((scope_id.len() as u64).to_le_bytes()); hasher.update(scope_id.as_bytes()); hasher.update((index as u64).to_le_bytes()); let digest = hasher.finalize(); let mut id = String::with_capacity(SYNTHESIZED_TOOL_CALL_ID_LEN); id.push_str("call"); for byte in &digest[..16] {" Request id source still human-readable, http.rs:323-327: " let id = format!( "{prefix}-{:016x}-{}", state.run, state.ids.fetch_add(1, Ordering::Relaxed) );" Note: 128 bits of digest are kept, not the run value or counter; scope hash includes the whole request id and index.

[C] Claim accuracy.

[D] Tautological compile-time guard. output.rs:20-22: "const MAX_SYNTHESIZED_TOOL_CALL_ID_LEN: usize = 40; const SYNTHESIZED_TOOL_CALL_ID_LEN: usize = 37; const _: () = assert!(SYNTHESIZED_TOOL_CALL_ID_LEN <= MAX_SYNTHESIZED_TOOL_CALL_ID_LEN);" The actual emitted-length invariant is only debug-checked, output.rs:46-47: " debug_assert_eq!(id.len(), SYNTHESIZED_TOOL_CALL_ID_LEN); debug_assert!(id.len() <= MAX_SYNTHESIZED_TOOL_CALL_ID_LEN);" All evidence was gathered with cargo test --release (PR "Supporting Evidence"), where debug_assertions are off. A test-side helper does exist (output.rs:1296-1309 assert_synthesized_tool_call_id, tests.rs:2666-2681 assert_synthesized_protocol_tool_call_id), so the property is covered by tests but not by the production binary.

[E] CI/evidence. .github/workflows/native-serving.yml:63: " cargo test --locked --release --manifest-path helios/Cargo.toml --target-dir flare-cli/target --features runtime-core,server" (ubuntu-24.04 leg) same command at :68 for the windows-2022 leg (matrix :18-21). These are exactly the legs the PR says were "not executed locally", and CI failed on head d150811. PR cites no #176 (merged cc696a6b) even though it re-encodes #176's {prefix}-{run:016x}-{seq} uniqueness scheme into fixed width.

[Sound] close() reorder, output.rs:1714-1723: " fn close(&mut self, changes: &mut Vec) -> Result<()> { if let Some(index) = self.open { if let Block::Tool { ended: false, .. } = &self.blocks[index] { return Err("incomplete emitted tool call".into()); } self.open = None; changes.push(Change::End(index)); }" apply() now validates before closing, output.rs:1768-1777: " let id = id.unwrap_or_else(|| synthesize_tool_call_id(request_id, index)); if id.is_empty() || self .blocks .iter() .any(|b| matches!(b, Block::Tool { id: existing, .. } if existing == &id)) { return Err("tool IDs must be unique and nonempty".into()); } self.close(&mut changes)?;" Responses/Messages render only outputs[0]: encode.rs:280 (self.outputs[0].blocks[index]), :742, :756. Chat scope {id}_{choice} at encode.rs:130-131.