Skip to content

helios: tolerate omitted closing tags on complete tagged tool calls at EOS - #573

Merged
lukealonso merged 2 commits into
mainfrom
fix/tool-call-eos-tolerance
Nov 28, 2026
Merged

lukealonso merged 2 commits into
mainfrom
fix/tool-call-eos-tolerance

Conversation

@lukealonso

@lukealonso lukealonso commented Nov 28, 2026 •

Copy link
Copy Markdown
Collaborator

Why

Models using tagged tool-call syntaxes occasionally finish generating a complete, self-delimited argument payload and emit end-of-sequence (EOS) without the final closing wrapper tag—for example, GLM omitting </tool_call> after </arg_value>, Qwen/MiMo omitting </function></tool_call> after </parameter>, Gemma 4 omitting <tool_call|> after }, or MiniMax XML omitting </invoke></tool_call> after a closed parameter element.

Previously, TaggedTools::finish returned an error for every unclosed tool call at Termination::Eos, failing requests whose arguments were structurally complete and unambiguous. Naive lenient parsing that simply calls ToolDialect::parse on whatever remains at EOS is unsafe: TaggedOutput::finish promotes undecided openers such as <tool_call>run, <tool_call>run<arg, or <tool_call>run</tool into Mode::Tool, which would fabricate zero-argument calls or garbled tool names and bypass the strict: true EOS contract.

Recovering unclosed tagged calls only when the opener was already confirmed, the inner arguments are structurally complete and self-delimited with no trailing non-whitespace, the target tool is non-strict, and termination is natural EOS matches practical serving expectations while preserving Flare's anti-fabrication, strict-tool, and truncation guarantees. Because tagged framing also relies on sequential invariants (longest partial-marker prefix on UTF-8 boundaries, earliest-closer selection, termination/confirmation gating, and starts_call prefix monotonicity), this PR pairs the implementation with Kani proofs, exhaustive opener-monotonicity tests, and cargo-mutants mutation coverage.

What changed

  • Before: TaggedTools::finish(Termination::Eos) unconditionally returned Err("incomplete <dialect> tool call"); TaggedOutput::finish did not increment self.calls for calls emitted during finish; TaggedOutput unconditionally set outer_tool_pending = D::FUNCTION.is_some() after closing a call even if D::CLOSE had already been consumed; and cargo kani verified only execution/transport.rs, model.rs, and model/contract.rs.
  • After:
    • helios/src/output/tagged.rs: Added a shared ToolDialect::parse_eos(body, tools) method (no per-model boolean flags). TaggedTools tracks confirmed (true only when D::starts_call confirmed the opener rather than finishing promoting an undecided tag, or on OutputStart::ToolBody) and outer_closed (computed by select_candidate so outer_tool_pending is only set when the inner function closer ended the call). TaggedTools::eos_decision classifies termination/confirmation/in_json_string state cleanly (Omit, Reject, ParseEos); TaggedOutput::finish advances self.calls for emitted ToolBegin events; and partial_marker uses a direct reverse scan preserving UTF-8 char boundaries. Non-EOS terminations (OutputLimit, UserStop, Cancelled, ExecutionError) remain unchanged and omit unclosed calls.
    • helios/src/output/argument_tags.rs (ArgumentTags): Implemented parse_eos requiring a valid function name (nonempty, no <, >, or whitespace), at least one closed <arg_key>...</arg_key><arg_value>...</arg_value> pair, no trailing non-whitespace, and a non-strict tool.
    • helios/src/output/chatml.rs (FunctionTags): Implemented parse_eos and updated parse_candidate to allow non-strict calls missing </function> at </tool_call> or natural EOS when at least one closed <parameter=...>...</parameter> is present with no trailing non-whitespace. Bare <function=name>, unclosed parameter values, trailing text, and strict: true tools still fail.
    • helios/src/output/gemma_call.rs (GemmaCall): Implemented parse_eos using the brace-balanced parser (call:name{...}, including explicit {}) with no trailing non-whitespace, while rejecting strict: true tools, bare headers, and unclosed braces or <|"|> strings.
    • helios/src/output/minimax_xml.rs (MiniMaxXml): Implemented parse_eos and updated parse_candidate to allow non-strict invocations missing </invoke> at </tool_call> or natural EOS when at least one closed parameter element is present with no trailing unclosed elements or text, while preserving existing parsing when </invoke> is present and rejecting empty blocks, bare headers, partial elements, and strict: true tools.
    • helios/src/output/pipeline.rs (PlainText): Implemented parse_eos returning the existing unconfigured-parser error.
    • helios/src/output/tagged/kani_proofs.rs, helios/src/lib.rs, .github/workflows/helios-lifecycle.yml, and verification/helios-lifecycle/README.md: Added four Kani harnesses (partial_marker_ascii_matches_oracle, partial_marker_utf8_preserves_char_boundaries, tagged_tools_eos_decision_enforces_termination_and_confirmation, and select_candidate_picks_earliest_closer_and_tracks_outer_closed), enabled pub mod output under any(feature = "server", all(kani, feature = "runtime-core")), updated CI Kani to pass --features runtime-core, and documented the proofs and their domains/limits.
    • docs/api.md: Updated the output-parser specification to document the natural-completion recovery rules per tagged dialect and the cases that remain errors or omissions.

Claimed result

Non-strict tagged tool calls whose argument bodies are structurally complete and self-delimited now succeed when only their closing wrapper tag is omitted at natural EOS (and without </function> / </invoke> when closed by </tool_call> for function-tag and MiniMax XML dialects) across buffered and streaming Chat Completions, Messages, and Responses APIs. Undecided openers, bare headers without a closing argument delimiter, unclosed arguments, trailing text, strict: true calls missing their wire closer, and all non-EOS terminations (OutputLimit, UserStop, Cancelled, ExecutionError) continue to fail or omit without fabricating calls. Core framing helpers (partial_marker, select_candidate, TaggedTools::eos_decision) are formally verified by Kani on every PR.

Evidence

  • Kani formal verification (kani-verifier 0.68.0, CBMC 6.11.0):
    cargo kani --manifest-path helios/Cargo.toml --features runtime-core --lib
    • 11/11 harnesses passed, 0 failures (7 existing transport/model/contract harnesses + 4 new output-parser harnesses):
      • tagged_tools_eos_decision_enforces_termination_and_confirmation: 0/106 failed, 4/4 cover properties satisfied (0.09s).
      • select_candidate_picks_earliest_closer_and_tracks_outer_closed: 0/149 failed, 2/2 cover properties satisfied (0.38s).
      • partial_marker_utf8_preserves_char_boundaries: 0/233 failed, 1/1 cover property satisfied (10.27s).
      • partial_marker_ascii_matches_oracle: 0/235 failed, 2/2 cover properties satisfied (14.76s).
  • Mutation testing (cargo-mutants 27.1.0):
    Scoped via --in-diff to all changes in helios/src/output/ against --lib -- output:: server::protocol::tests::unclosed_tagged server::protocol::tests::strict_tool_wires server::logprobs::tests::unclosed_glm:
    • 168 mutants tested: 157 caught, 7 unviable, 2 timeouts, 2 missed.
    • Both timeouts are infinite-loop mutations (len -= 1 -> len /= 1 and len += 1 in partial_marker).
    • Both missed mutants are mathematically equivalent: at < end -> at <= end in select_candidate (distinct non-prefix outer/inner closing tags cannot start at the same byte) and len > best -> len >= best in partial_marker (matching len == best only reassigns best = best and breaks).
    • Pass 1 caught 16 real test gaps (</> in GLM ToolBody names, in_json_string at EOS, outer_tool_pending after outer_closed, bare <function=lookup> in parse_eos, TaggedOutput.calls post-finish, multi-call emit_calls indexing, FunctionTags::parse, PlainText::parse_eos), all now covered and killed in pass 2.
  • Release unit, protocol, logprob, and grammar tests:
    • cargo test --locked --release --manifest-path helios/Cargo.toml --features runtime-core,server --lib -- output:: server::protocol::tests:: server::logprobs:: server::output:: grammar::: 206 passed, 0 failed, 4 ignored (checkpoint-gated grammar:: tests). Includes exhaustive prefix-monotonicity checks for ArgumentTags::starts_call and FunctionTags::starts_call, prefix completeness for possible_parameter_start, every-UTF-8-split tests across all four tagged dialects, OutputStart::ToolBody, token-gated reasoning delimiters, strict: true, parallel_tool_calls: false, tool_choice (auto/required/named), OutputLimit/UserStop/Cancelled/ExecutionError, buffered/streamed parity across Chat/Messages/Responses, and Responses content logprobs + reasoning token accounting.
    • cargo test --locked --release --manifest-path helios/Cargo.toml --features runtime-core,server --test deepseek_output: 14 passed, 0 failed.
    • cargo test --locked --release --manifest-path flare/Cargo.toml --no-run and cargo test --locked --release --manifest-path flare-cli/Cargo.toml --no-run: compiled cleanly.

Unrun checks / limitations:

  • server::protocol::decode::native_prompt_tests::messages_image_hints_end_after_expansion_for_text_and_segmented_templates requires FLARE_TEST_TEMPLATE_DIR and was not run.
  • Checkpoint-gated tokenizer/grammar integration tests (FLARE_*_TEST_SNAPSHOT: reasoning_delimiters, glm_frontend, parser_start, and the 4 ignored grammar:: unit tests) and live GPU serving runs were not executed.
  • Kani proves partial_marker, select_candidate, and TaggedTools::eos_decision within their stated bounded domains (and eos_decision uses mem::forget on its empty Arc<[serde_json::Value]> fixture to isolate the borrowed method from serde_json::Value::drop); full serde_json argument decoding and dialect tag grammars are verified by unit, protocol, exhaustive prefix-monotonicity, and mutation tests rather than CBMC.

Discord: no username provided

…t EOS

Models using tagged tool-call syntaxes occasionally finish a complete,
self-delimited argument payload and terminate with natural end-of-sequence
(EOS) without emitting the final closing wrapper tag (such as `</tool_call>`
in GLM argument-tag calls, `</function></tool_call>` in Qwen/MiMo function
tags, `<tool_call|>` in Gemma 4 calls, or `</invoke></tool_call>` in MiniMax
XML). Previously `TaggedTools::finish` rejected every unclosed call at
`Termination::Eos` unconditionally, failing the entire request even when all
arguments were closed and unambiguous.

Before this change, `TaggedTools::finish(Termination::Eos)` always returned
an incomplete-call error, `TaggedOutput::finish` did not increment `self.calls`
for tool calls emitted on finish, and `TaggedOutput` always set
`outer_tool_pending = D::FUNCTION.is_some()` after closing a tool even when
`D::CLOSE` had already been consumed.

This change adds a shared `ToolDialect::parse_eos(body, tools)` hook with no
per-model boolean flags and wires EOS recovery across all tagged dialects:

- `helios/src/output/tagged.rs`: Tracks whether `TaggedTools` entered
  `Mode::Tool` from a confirmed opener (`confirmed`) rather than an undecided
  tag promoted during `finish()`, tracks `outer_closed` so `outer_tool_pending`
  is only set when the inner closer ended the call, rejects unconfirmed calls
  and unfinished JSON strings on `Termination::Eos`, delegates confirmed EOS
  bodies to `D::parse_eos`, and advances `TaggedOutput.calls` when `finish()`
  emits `ToolBegin` events.
- `helios/src/output/argument_tags.rs`: Implements `ArgumentTags::parse_eos`
  requiring a valid function name (no `<`, `>`, or whitespace), at least one
  closed `<arg_key>...</arg_key><arg_value>...</arg_value>` pair, no trailing
  non-whitespace, and a non-strict tool.
- `helios/src/output/chatml.rs`: Implements `FunctionTags::parse_eos` and
  allows `parse_candidate` to close non-strict calls missing `</function>` at
  `</tool_call>` or EOS when at least one closed `<parameter=...>...</parameter>`
  is present with no trailing non-whitespace.
- `helios/src/output/gemma_call.rs`: Implements `GemmaCall::parse_eos` for
  non-strict brace-balanced `call:name{...}` payloads (including explicit `{}`)
  with no trailing non-whitespace.
- `helios/src/output/minimax_xml.rs`: Implements `MiniMaxXml::parse_eos` and
  allows `parse_candidate` to close non-strict invocations missing `</invoke>`
  at `</tool_call>` or EOS when at least one closed parameter element is
  present with no trailing unclosed elements or text.
- `helios/src/output/pipeline.rs`: Implements `PlainText::parse_eos` returning
  the existing unconfigured-parser error.
- `docs/api.md`: Documents the natural-completion recovery rules and the
  rejected/omitted cases.

Bare headers without a closing argument delimiter (such as `<tool_call>run` or
`<tool_call><function=lookup>`), undecided openers, partial arguments, trailing
non-whitespace, `strict: true` calls missing their wire closer, and all non-EOS
terminations (`OutputLimit`, `UserStop`, `Cancelled`, `ExecutionError`) remain
rejected or omitted.

Verified with Helios unit tests across all tagged dialects, protocol encoder
tests across Chat/Messages/Responses (buffered and streamed), logprob and
reasoning-token accounting tests, and DeepSeek output tests.

Design-Signed-Off-By: Luke Alonso <[email protected]>; design-review=c3a9
Signed-off-by: Luke Alonso <[email protected]>
@flare-autobot

Copy link
Copy Markdown
Collaborator

A case file has been opened for 573.
Case File Intake Report

Future reviews will auto-run 90 minutes after code updates are pushed.

  • Re-review sooner: Call @flare-autobot after push.
  • Opt-out: Include #autobot:ignore in the PR or any comment.

…sing

Tagged tool-call framing relies on sequential invariants that example tests
alone do not exhaustively establish: `partial_marker` must return the exact
longest proper prefix on a UTF-8 char boundary, `select_candidate` must pick
the earliest closing tag without overflow and track `outer_closed`, and
`TaggedTools::eos_decision` must omit non-EOS terminations and reject
unconfirmed or mid-JSON-string EOS states. Mutation testing against the EOS
recovery diff also surfaced 16 untested boundary paths across the tagged
dialects.

Before this change, `cargo kani` covered only `execution/transport.rs`,
`model.rs`, and `model/contract.rs` under default features, and `helios/src/output/`
had no Kani proofs or mutation-calibrated boundary tests for `starts_call`
prefix monotonicity, `<`/`>` in GLM `ToolBody` names, `in_json_string` at EOS,
`outer_tool_pending` after `outer_closed`, bare `<function=lookup>` in
`parse_eos`, `TaggedOutput.calls` post-finish, or multi-call `emit_calls`
indexing.

This change adds four Kani proofs in `helios/src/output/tagged/kani_proofs.rs`,
enables them in CI under `cargo kani --manifest-path helios/Cargo.toml --features runtime-core --lib`,
and closes the mutation-testing gaps:

- `helios/src/output/tagged.rs` and `helios/src/output/tagged/kani_proofs.rs`:
  Extracts `TaggedTools::eos_decision` and `select_candidate`, simplifies
  `partial_marker` from a nested iterator chain to an explicit reverse byte
  scan preserving UTF-8 char boundaries, and adds four Kani harnesses
  (`partial_marker_ascii_matches_oracle`, `partial_marker_utf8_preserves_char_boundaries`,
  `tagged_tools_eos_decision_enforces_termination_and_confirmation`, and
  `select_candidate_picks_earliest_closer_and_tracks_outer_closed`) with
  `kani::cover!` reachability checks.
- `helios/src/lib.rs`, `.github/workflows/helios-lifecycle.yml`, and
  `verification/helios-lifecycle/README.md`: Gates `pub mod output` on
  `any(feature = "server", all(kani, feature = "runtime-core"))`, runs CI
  Kani with `--features runtime-core`, and documents the four proofs and their
  domains and limits.
- `helios/src/output/{tagged,argument_tags,chatml,pipeline}.rs`: Adds
  exhaustive prefix-monotonicity tests for `ArgumentTags::starts_call` and
  `FunctionTags::starts_call` (plus `possible_parameter_start` prefix
  completeness) and targeted unit tests killing all 16 real surviving mutants.

Verified with `cargo kani --manifest-path helios/Cargo.toml --features runtime-core --lib`
(11/11 harnesses passing, 0 failures), `cargo-mutants` across all 168 mutants
in the `helios/src/output/` diff (157 caught, 7 unviable, 2 timeouts on
infinite-loop mutations, 2 mathematically equivalent survivors), and release
Helios unit/protocol/logprob/grammar/DeepSeek tests (206 + 14 passing).

Design-Signed-Off-By: Luke Alonso <[email protected]>; design-review=c3a9
Signed-off-by: Luke Alonso <[email protected]>
@lukealonso
lukealonso merged commit 2bdc847 into main Nov 28, 2026
2 checks passed
@flare-autobot

Copy link
Copy Markdown
Collaborator

Autobot Review

Full Review · Value Proposition · Case File

Recommendation: Ready To Merge

PR #573 is sound and verified: flag-free EOS wrapper recovery preserves anti-fabrication and strict: true gates, with no correctness, safety, or contract regressions. Only optional follow-ups remain.

  • Blockers: 0
  • Should fix: 0
  • Subtractive suggestions: 1
  • Nits: 2

Have your agent review the Full Review for complete verified findings, file:line citations, and suggested changes.

To opt out, include #autobot:ignore in your post or comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants