Subtraction and simplicity scratchpad · NVIDIA/hyperflare-inference #685

Raw Markdown for agents: https://autobot.eidonix.com/p/ae3bb724-7ce7-432e-9c58-2d9037b93d0a.md

#1 [high] A (high): finish(cause) parameter sweep — collapse to finish() + explicit drop at the one real call site

helios/src/server/output.rs:476-480

/// Whether a termination cause drops a tool call that was announced but never
/// completed, instead of reporting it as an incomplete call.
pub(crate) fn drops_unfinished_call(cause: Termination) -> bool {
    cause != Termination::Eos
}

Two uses only: output.rs:164 and output.rs:1731. The new parameter cause: Termination on Accumulator::finish (output.rs:163) and ResponseOutput::finish (output.rs:1730) is an implicit mode flag: every caller in the repo passes Termination::Eos except Encoder::finish (encode.rs:675, :677). Mechanical churn sites (all finish() -> finish(Termination::Eos)): output.rs:721,723,1346,1379,1431,1432,1648,1676; phase_tests.rs:765,973; argument_tags.rs:209/287/334/435/490; chatml.rs:415,647,830,1094; gemma_call.rs:265; minimax_xml.rs:; xtml.rs:461,505; pipeline.rs:342,489,577,682,735; protobuf.rs:1157. That is 8 files touched for one call site's decision.

Simplification: keep fn finish(&mut self) -> Result<()> (Accumulator) and fn finish(&mut self) -> Result<Vec<Change>> (ResponseOutput) unchanged, and add one explicit method, e.g. pub(crate) fn drop_unfinished(&mut self) on each, called from Encoder::finish before finish() when cause != Termination::Eos:

Note: on main Accumulator::finish/ResponseOutput::finish are also called from the Chat path (encode.rs:675) and unconditionally (encode.rs:677); a drop_unfinished on both preserves today's exact behaviour, and strict-tool parking (encode.rs:567-621) is unaffected.

#2 [high] B (high): duplicated truncation tests — same two test names in dsml.rs and protocol/tests.rs

+285 lines in helios/src/output/dsml.rs (mods streaming_tests 749-985 and rejection_tests 987-1032) and +266 lines in helios/src/server/protocol/tests.rs (3808-4074).

Duplicate test names across the two files:

The protocol copies use hand-built Event:: vectors (no parser), so the DSML-side copies are only adding "the parser produced these events", which is already covered by a_call_opens_and_publishes_arguments_before_its_envelope_closes (dsml.rs:793) and a_truncated_call_keeps_its_published_fragments_and_omits_the_stored_call (dsml.rs:917). Recommend keeping one per name (the protocol-level one, since the buffered body + finish_reason is the user-visible contract) and deleting the dsml.rs duplicate; ~90 lines.

Also removable: rejection_tests::end_of_sequence_inside_an_announced_call_is_an_error (dsml.rs:1002-1011)

    #[test]
    fn end_of_sequence_inside_an_announced_call_is_an_error() {
        let mut parser = parser();
        let events = parser.push(Input::Text(
            "<|DSML|tool_calls><|DSML|invoke name=\"write\"><|DSML|parameter name=\"q\" string=\"true\">par",
        )).unwrap();
        assert!(events.iter().any(|event| matches!(event, Event::ToolBegin { .. })));
        assert!(events.iter().any(|event| matches!(event, Event::ToolArguments { .. })));
        assert!(parser.finish(Termination::Eos).is_err());
    }

The Eos-is-an-error half is already asserted by the existing integration test incomplete_calls_do_not_finish_on_eos_and_can_be_cancelled_or_truncated (helios/tests/deepseek_output.rs:387-421, which loops Eos -> is_err and the four other causes -> Ok), and the two event assertions are asserted by the streaming tests above. Whole rejection_tests module (~45 lines incl. its own parser() fixture duplicating streaming_tests::config()) can go apart from the publish-before-reject assertion.

#5 [high] Research summary

Subtraction/simplicity review of #685 complete; findings saved as scratchpad notes 1–4.

The substantive streaming change is genuinely lean: Call::publish is 10 lines, guarded by one predicate (literal.is_some()), and needs only one new field (published_argument_bytes). DsmlOutput loses 9 lines at ToolCallArgumentsEnd. Most of the +662 arrives as tests and the finish(cause) parameter sweep, and that sweep is where the removability is.

Four suggestions, all with exact quotes and concrete replacements:

  1. High — Accumulator::finish(cause) / ResponseOutput::finish(cause) plus the drops_unfinished_call(cause) helper (server/output.rs:163, 476-480, 1730) encode a single boolean, and ~20 of ~21 call sites pass Termination::Eos. Replace with an explicit drop_unfinished() called once from Encoder::finish: deletes the helper, the parameter, and ~30 mechanical edits across 8 files (argument_tags.rs, chatml.rs, gemma_call.rs, minimax_xml.rs (not opened), xtml.rs, pipeline.rs, protobuf.rs, phase_tests.rs, plus output.rs's own tests).
  2. High — two new tests are duplicated under identical names in both output/dsml.rs and server/protocol/tests.rs (a_truncated_second_call_keeps_the_completed_first_call, a_header_only_call_is_omitted_when_the_response_truncates), and the whole rejection_tests module (dsml.rs:987-1032) re-asserts what helios/tests/deepseek_output.rs:387-421 and the streaming tests already cover, with its own duplicate parser() fixture. ≈135 removable lines.
  3. Medium — the PR adds call.check_size()? at dsml.rs:191 while the pre-existing post-match check_size at dsml.rs:218-220 still runs on the same state; drop one, and note that the new docs/api.md:570-572 sentence documents the 218 behaviour while the new code makes 191 load-bearing first.
  4. Medium — docs/api.md:543-551 adds two sentences that are not this PR's subject: a tagged-parser justification that is already stale on main (tagged_json.rs (not opened) coerces nothing) and an editorial line about clients seeing fragments. Replace with the one-clause main sentence, inverted.

No architectural/convention violation of the "no flags, no speculative abstraction" kind: no new config knob, no fallback layer, no dead code left behind. The finish(cause) sweep is the one instance of a flag-like parameter, and it is fixable as described. The deletion of Err("incomplete emitted tool call") for non-Eos causes is a Correctness/Contracts matter already recorded in the case file, not restated here; if that behaviour is reversed, suggestion 1 evaporates entirely, which is a further argument for not threading the cause through the accumulator API.

#6 [high] E (high): overall verdict, inventory, and citation caveats for the subtraction lens

VERDICT: the substantive streaming change is lean and idiomatic. Do not dress this up as a style problem. Removability is concentrated in two places — the finish(cause) parameter sweep (note #1) and duplicated/overlapping new tests (note #2) — plus two small ones (notes #3, #4).

What is lean and should be said so plainly:

INVENTORY of the 13 changed files for the report:

CITATION CAVEATS to state honestly in the report:

INTERACTION with the correctness lens (do not duplicate it, but the report should note the dependency): if the reviewer decides the non-Eos drop is wrong and the Err("incomplete emitted tool call") branch must be restored for non-Eos causes, suggestion #1 becomes trivially true — the accumulator API returns to finish() with no argument at all. Recommend the simplification in those terms: it is strictly better under either correctness outcome.

#3 [medium] C (medium): redundant check_size inside the argument-text action

helios/src/output/dsml.rs:188-192 (added)

                    }
                    call.check_size()?;
                    call.publish(self.calls, events);
                }

and the pre-existing, unchanged lines dsml.rs:218-220

            if let Some(call) = &self.call {
                call.check_size()?;
            }

Both run for every ToolCallArgumentsText action; the second re-checks state the first just validated (nothing appends between them except publish, which only reads). One of the two should go.

Which to remove: keeping only 218-220 is the smaller diff and preserves all existing guarantee tests (helios/tests/deepseek_output.rs:530-566, 568-593 rely on the post-match check), at the cost that the fragment which crosses 1 MiB is published before the error. Keeping only 191 means the crossing fragment is never published — but then note that docs/api.md:570-572's new wording ("The bound is enforced on the append that crosses it, after the call name and earlier argument fragments were published") describes the 218-only behaviour, so code and doc currently disagree about which of the two checks is load-bearing. Pick one and align the sentence.

Not a validation-removal concern: the bound is still enforced on every append either way (AGENTS.md:182-187 satisfied); only the duplicate disappears.

#4 [medium] D (medium): docs paragraph overreach — trim out-of-scope/stale sentences

docs/api.md:543-551 (added)

byte for byte. 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: values are coerced to
declared types, strict container parameters are parsed, and wrapping newlines
are trimmed. 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. A client that received fragments of such a
call has already seen a call that cannot complete.

Two removals:

  1. The tagged-parser justification (543-547) is not this PR's change (tagged parsers are untouched) and is already stale: TAGGED_JSON on main parses the model's own {"name":…,"arguments":…} object and re-serializes it (helios/src/output/tagged_json.rs:14-57) rather than coercing to declared schemas. The pre-existing main sentence it derives from was "The model parsers buffer tool envelopes until they are complete, so argument events do not imply incremental generation of an unfinished call." (docs/api.md:540-543 on main) — one clause, correct for every parser. Replace 543-547 with that clause, inverted for DSML only: "The tagged tool parsers publish a call only when its envelope is complete, so their argument events do not imply incremental generation."
  2. A client that received fragments of such a call has already seen a call that cannot complete. (550-551) is commentary, not contract, and describes precisely the unresolved framing gap (no content_block_stop / response.output_item.done for the dropped call). Delete.

Keeps the delta honest and shrinks the docs hunk from ~12 new lines to ~4.