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

Raw Markdown for agents: https://autobot.eidonix.com/p/6b95afa0-f309-491e-a39c-85c749a4d9a1.md

#1 [high] Custom/grammar tool truncation now errors instead of omitting (verified)

VERIFIED by reading head b9864ab.

Path: ResponseOutput::finish (helios/src/server/output.rs:1731-1744) on non-EOS pushes Change::End(last) for the abandoned tool block and records abandoned = Some(last). Encoder::finish (helios/src/server/protocol/encode.rs:677-689) renders those changes with frames.extend(self.change(change)?) BEFORE drop_abandoned.

Encoder::change for (Protocol::Responses, Change::End(i)) on a Block::Tool whose declared tool has custom == true does: if self.tool(name).is_some_and(|tool| tool.custom) { let value: Value = serde_json::from_str(arguments) .map_err(|e| format!("invalid custom tool arguments: {e}"))?; if !value.get("input").is_some_and(Value::is_string) { return Err("custom tool input must be text".into()); } (encode.rs:475-483)

A truncated call's arguments are partial JSON by construction (streamed fragments), so the ? propagates out of Encoder::finish; http.rs (helios/src/server/http.rs:534-540, 569-580) turns that into encoder.failure(&e) + Delivery::Failed, so the client gets an error frame after having been sent response.output_item.added, and never gets response.completed/response.incomplete. drop_abandoned is never reached, so blocks/calls keep the truncated call. Before this PR this path was unreachable (no Change::End was emitted for an unfinished block: close() returned Err("incomplete emitted tool call")).

No test covers it: the new protocol tests only exercise the plain function tool lookup (helios/src/server/protocol/tests.rs:3823-3827, 3992-3996).

Fix: in finish, either skip rendering Change::End through change() for abandoned blocks (render only the closing frame), or make the custom-tool branch in change() tolerate an abandoned block (pass a flag / validate only when ended).

#3 [high] Announce-before-validate reverses the pre-publication invariant and its tests

VERIFIED head b9864ab.

  1. validate_arguments now runs AFTER fragments have been published (helios/src/output/dsml.rs:202-204: validate_arguments(&call.arguments)?; then call.publish(self.calls, events);), while Event::ToolBegin was already emitted at ToolNameEnd (dsml.rs:145-156). So a call rejected for "nonempty and unique" keys, or for exceeding the 1 MiB bound (check_size, dsml.rs:42-47, called at :191), has already been announced and partly streamed to the client, then the request fails. On main nothing was emitted before validation (main:171-194 collects into Call and emits all three events only after validating).

  2. The existing test that pinned the old guarantee was rewritten rather than extended:

    • helios/tests/deepseek_output.rs:514-515 assert!(error.unwrap().to_string().contains("nonempty and unique")); assert_eq!(parsed.ended, 0, "a rejected call reported an end"); (main:511 asserted that nothing was published at all).
    • helios/tests/deepseek_output.rs:572-601 parse_literal_parameters now routes events into Parsed instead of asserting the push produced nothing.
    • helios/src/server/output.rs:1264-1274: user_stop_cannot_retract_an_already_emitted_partial_tool_call became user_stop_omits_an_already_emitted_partial_tool_call; main:1262-1270 asserted Err("incomplete emitted tool call").

AGENTS.md:182-187 ("Do not remove validation, suppress errors") and 201-207 ("Apply these criteria to documentation and test changes that would weaken the rules as well as code") make this a sign-off question, not a mechanical one; docs/api.md:547-555 documents the new behaviour, so it is declared but not authorized.

#8 [high] Research summary

Reviewed PR #685 at head b9864ab for correctness, contracts and system-level consequences, reading dsml.rs, server/output.rs, server/protocol/encode.rs, pipeline.rs, http.rs, runtime.rs, latency.rs, docs/api.md, the new protocol tests and the two helios integration-test files in full context (no execution; all findings are static reading of the diff and its callers).

Seven findings saved to the scratchpad. The one I would call a real defect: a truncated custom/grammar tool call on the Responses protocol now fails the whole request. ResponseOutput::finish records the abandoned block and Encoder::finish renders its Change::End (encode.rs:677-689) through the custom-tool branch (encode.rs:475-483), which does serde_json::from_str(arguments).map_err(...)? on arguments that are partial JSON by construction, so the Err propagates out of finish and http.rs turns it into an error frame with no response.completed/response.incomplete; drop_abandoned is never reached. No new test covers a custom tool. Relatedly, for UserStop/Cancelled/ExecutionError the response.output_item.done frame for the abandoned block is rendered with item(i, true), reporting status: "completed" and partial arguments for an item that then disappears from response.completed.

Contract/charter items: the change deliberately announces a call and streams fragments before validate_arguments and the 1 MiB check_size (dsml.rs:191-204 vs main:171-194), so rejected and oversized calls reach the client before the error; the tests that pinned the old "nothing was emitted" guarantee were rewritten rather than extended (deepseek_output.rs:514-515, :572-601, output.rs:1264-1274) and docs/api.md:547-555 documents it, i.e. declared but with no maintainer sign-off against AGENTS.md:182-187/201-207. docs/api.md:543-547 is now factually false against main (the merged TAGGED_JSON parser copies the model's own JSON rather than synthesizing arguments). docs/api.md:547-549's unconditional "omitted for every termination other than EOS" is contradicted by the required/named tool_choice error at encode.rs:690-694.

Claim accuracy: the body's "ExecutionError included" describes a cause the server never passes to Encoder::finish (http.rs only forwards UserStop/Eos/OutputLimit; errors take the failure path), and the reported test counts (1787/16, output:: 113, server::protocol 96) are unchanged from the previous head despite three new test modules.

Clean areas, stated plainly: no new locks, channels or shared state — all touched structures are per-request; fragment publication copies only newly appended bytes within the 1 MiB bound; TTFT/first-response sampling (latency.rs:114-142) excludes tool-call deltas, so the rate/latency metric scope is unchanged; and the previously reported Messages/Responses framing violation is genuinely fixed (block/item closing frames now precede the terminal frame).

#9 [high] Verbatim quotes + severities for every #685 finding (correctness/contracts lens)

Head b9864ab. Static reading only; nothing executed.

F1. BLOCKER — truncated custom/grammar tool call errors the request instead of being omitted. helios/src/server/protocol/encode.rs:475-483 if self.tool(name).is_some_and(|tool| tool.custom) { let value: Value = serde_json::from_str(arguments) .map_err(|e| format!("invalid custom tool arguments: {e}"))?; if !value.get("input").is_some_and(Value::is_string) { return Err("custom tool input must be text".into()); } reached from helios/src/server/protocol/encode.rs:677-689: let changes = self.outputs[index].finish(cause)?; let abandoned = self.outputs[index].take_abandoned(); for change in changes { ... frames.extend(self.change(change)?); } if let Some(block) = abandoned { self.outputs[index].drop_abandoned(block); } Impact: a custom/grammar tool truncated by stop string, cancel or output limit has partial-JSON arguments; ? in change() propagates, helios/src/server/http.rs:575-580 yields encoder.failure(&e) + Delivery::Failed, so the client sees response.output_item.added then an error frame, never response.completed/response.incomplete; drop_abandoned never runs. Unreachable before this PR (close() returned Err("incomplete emitted tool call"), output.rs main:1717-1731). No test: protocol/tests.rs:3823-3827 and :3992-3996 use only function tool "lookup". Fix: do not render an abandoned block's Change::End through the custom-tool validation path (pass an abandoned/"closing-only" flag into change() and skip validation), or validate custom input only when the block ended.

F2. MAJOR — abandoned item reported as completed with partial arguments (Responses, non-output-limit causes). helios/src/server/protocol/encode.rs:340-346 let status = if self.incomplete_item == Some(index) { "incomplete" } else if complete { "completed" } else { "in_progress" }; incomplete_item is set only for OutputLimit (encode.rs:680-683); Change::End calls self.item(i, true) (encode.rs:486) and emits response.output_item.done (encode.rs:513-516) with "arguments":if complete{arguments.as_str()}else{""} (encode.rs:380-381). Impact: on UserStop/Cancelled/ExecutionError the client gets status:"completed" and truncated arguments for an item absent from response.completed. Fix: mark the abandoned index incomplete (or omit arguments) for every drops_unfinished_call cause.

F3. BLOCKER (authorization) — announce/stream precedes validation and the 1 MiB check. helios/src/output/dsml.rs:194-206 OutputAction::ToolCallArgumentsEnd { closing_text } if !repeated => { ... call.close_literal(); call.arguments.push_str(closing_text); call.check_size()?; validate_arguments(&call.arguments)?; call.publish(self.calls, events); events.push(Event::ToolEnd { index: self.calls }); with Event::ToolBegin already emitted at dsml.rs:145-156. main:171-194 emitted none of the three until after validate_arguments. Rejected ("nonempty and unique") and oversized (>1 MiB) calls now reach the client before the error. Tests were inverted to bless it: helios/tests/deepseek_output.rs:514-515 assert_eq!(parsed.ended, 0, "a rejected call reported an end"); (main:511 asserted nothing published), helper :572-601, and helios/src/server/output.rs:1264-1274 (user_stop_omits_an_already_emitted_partial_tool_call; main:1262-1270 asserted Err("incomplete emitted tool call")). AGENTS.md:182-187 + 201-207 require a maintainer decision; none on record.

F4. MAJOR (claim accuracy) — ExecutionError arm unreachable in production. helios/src/server/http.rs:526-540 forwards only the Finished.termination from runtime.rs:2366-2371 (UserStop / Eos / OutputLimit); generation errors take GenerationEvent::Error(e) => Err(e) (http.rs:562) → encoder.failure (http.rs:576). So drops_unfinished_call's ExecutionError branch (helios/src/server/output.rs:478-480) and the test rows at protocol/tests.rs:3816 and :3985 exercise a path the server never hits. PR body's "ExecutionError included" overstates live coverage.

F5. MAJOR (docs vs code) — required/named tool_choice truncation still errors. helios/src/server/protocol/encode.rs:690-694 if matches!(self.tool_choice, ToolChoice::Required | ToolChoice::Named(_)) && !self.outputs[index].has_tools() { return Err("required tool choice was not satisfied: the model did not produce a completed tool call".into()); } contradicts new docs/api.md:547-549 ("omitted from a buffered response for every termination other than natural end-of-sequence"). reasons[index] is never set (:702) so no terminal frame is emitted; http.rs:575-580 sends an error frame. Not a regression, but the doc rule is unconditional. Fix: qualify the doc, or skip the required-tool error when drops_unfinished_call(cause).

F6. MAJOR (docs) — docs/api.md:543-547 false against main. "The tagged tool parsers publish a call only when its envelope is complete, because they build its arguments from parameter syntax and the declared schemas rather than copying the model's text". main now has helios/src/output/tagged_json.rs (merged #670) which parses the model's own JSON object. This PR edits those exact lines.

F7. MINOR (claim accuracy) — stale numbers. Body still reports "1787 passed and 16 failed", "output:: 113 passed", "server::protocol 96 passed"; b9864ab adds mod streaming_tests (dsml.rs:749-985), mod rejection_tests (dsml.rs:987-1033) and protocol tests.rs:3809-4017. No CI run on any hash.

CLEAN (state plainly, do not invent issues): per-request state only, no new locks/channels/global state (Encoder built per request, http.rs:358-400); Call::publish copies only new bytes within the 1 MiB bound (dsml.rs:53-62, :42-47); TTFT metric unaffected — helios/src/server/latency.rs:114-142 counts only content/reasoning deltas; the earlier Messages/Responses unclosed-block framing bug is genuinely fixed (content_block_stop encode.rs:424-428 and response.output_item.done :513-516 precede message_stop :735-739 / response.completed :740-750; asserted at protocol/tests.rs:3861-3876); decode/emit ordering preserved; no cross-platform (Unix/Windows) surface touched by this change.

Known limitation, not a defect: frame count now scales with model fragments (566 frames / 2196 bytes measured by the author) instead of with calls — intended by the PR.

#2 [medium] Responses output_item.done for a truncated call claims status completed with invalid arguments

VERIFIED head b9864ab.

incomplete_item is only set for Termination::OutputLimit (helios/src/server/protocol/encode.rs:680-683). For UserStop, Cancelled and ExecutionError the abandoned block is rendered by Change::End, which calls let item = self.item(i, true); (encode.rs:486) and emits response.output_item.done (encode.rs:513-516).

item() computes let status = if self.incomplete_item == Some(index) { "incomplete" } else if complete { "completed" } else { "in_progress" }; (encode.rs:340-346) and for a function tool emits "arguments":if complete{arguments.as_str()}else{""} (encode.rs:380-381).

So on a user stop / cancel / execution error inside a streamed call the client receives response.output_item.done with status: "completed" and arguments holding a partial JSON string, while the same item is then dropped (drop_abandoned, output.rs:1750-1755) and absent from response.completed. Ordering of the frames is otherwise corrected (content_block_stop / output_item.done now precede the terminal frame), so this is a residual item-level inconsistency rather than the old unclosed-block bug.

#4 [medium] Claim accuracy: ExecutionError arm is unreachable from the server; test counts stale

VERIFIED head b9864ab.

#5 [medium] System-level consequences: concurrency, memory, metrics all clean except frame count growth

VERIFIED head b9864ab (per-request state, no shared locks).

#6 [medium] docs/api.md tagged-parser sentence is false against main (TAGGED_JSON)

VERIFIED head b9864ab.

docs/api.md:543-547 still states: "The tagged tool parsers publish a call only when its envelope is complete, because they build its arguments from parameter syntax and the declared schemas rather than copying the model's text". main now contains helios/src/output/tagged_json.rs (present in list_files main helios/src/output/*.rs; introduced by merged #670) which parses the model's own {"name":…,"arguments":…} JSON object instead of synthesizing arguments from parameter syntax. So the doc sentence is factually false against main after this PR's rebase-free description of parsers. This PR touches those exact lines (its +25/-12 in docs/api.md covers 540-575), so the PR is the place to fix or the rebase will land false documentation.

Also unchanged from the earlier head and still true: docs/api.md:568-572 states the 1 MiB bound "is enforced on the append that crosses it, after the call name and earlier argument fragments were published and are not retracted" — i.e. #364's publish-after-check property stays reversed, and it is now documented as such.

#7 [medium] Required/named tool_choice truncation still returns an error frame, contradicting new docs

VERIFIED head b9864ab.

docs/api.md:547-549: "An announced call cut off before completion is omitted from a buffered response for every termination other than natural end-of-sequence, and the response reports that termination's finish reason; only an output-limit termination reports the output limit."

But Encoder::finish still errors after omitting the call when the request demanded a tool: if matches!(self.tool_choice, ToolChoice::Required | ToolChoice::Named(_)) && !self.outputs[index].has_tools() { return Err("required tool choice was not satisfied: the model did not produce a completed tool call".into()); } (helios/src/server/protocol/encode.rs:690-694, after drop_abandoned at :687-689)

With tool_choice: "required" (or a named tool) plus streaming truncation, has_tools() is false because the call was popped, so the ? returns early: self.reasons[index] is never set (:702), no terminal frame is emitted (:709-752), and helios/src/server/http.rs:575-580 converts it into encoder.failure(&e) — an error frame — instead of the documented truncated response. Not a regression (on main the same request failed one line earlier with "incomplete emitted tool call"), but the new documentation states an unconditional rule that the required/named tool-choice path contradicts. Fix: qualify the sentence, or skip the required-tool error on drops_unfinished_call(cause) terminations so the truncation is reported as documented.