Repository navigation
helios: tolerate omitted closing tags on complete tagged tool calls at EOS - #573
Merged
Merged
Conversation
…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]>
Collaborator
|
A case file has been opened for 573. Future reviews will auto-run 90 minutes after code updates are pushed.
|
…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]>
Collaborator
Autobot ReviewFull Review · Value Proposition · Case File Recommendation: Ready To MergePR #573 is sound and verified: flag-free EOS wrapper recovery preserves anti-fabrication and
Have your agent review the Full Review for complete verified findings, To opt out, include |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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::finishreturned an error for every unclosed tool call atTermination::Eos, failing requests whose arguments were structurally complete and unambiguous. Naive lenient parsing that simply callsToolDialect::parseon whatever remains at EOS is unsafe:TaggedOutput::finishpromotes undecided openers such as<tool_call>run,<tool_call>run<arg, or<tool_call>run</toolintoMode::Tool, which would fabricate zero-argument calls or garbled tool names and bypass thestrict: trueEOS 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
EOSmatches 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, andstarts_callprefix monotonicity), this PR pairs the implementation with Kani proofs, exhaustive opener-monotonicity tests, andcargo-mutantsmutation coverage.What changed
TaggedTools::finish(Termination::Eos)unconditionally returnedErr("incomplete <dialect> tool call");TaggedOutput::finishdid not incrementself.callsfor calls emitted during finish;TaggedOutputunconditionally setouter_tool_pending = D::FUNCTION.is_some()after closing a call even ifD::CLOSEhad already been consumed; andcargo kaniverified onlyexecution/transport.rs,model.rs, andmodel/contract.rs.helios/src/output/tagged.rs: Added a sharedToolDialect::parse_eos(body, tools)method (no per-model boolean flags).TaggedToolstracksconfirmed(true only whenD::starts_callconfirmed the opener rather thanfinishingpromoting an undecided tag, or onOutputStart::ToolBody) andouter_closed(computed byselect_candidatesoouter_tool_pendingis only set when the inner function closer ended the call).TaggedTools::eos_decisionclassifies termination/confirmation/in_json_stringstate cleanly (Omit,Reject,ParseEos);TaggedOutput::finishadvancesself.callsfor emittedToolBeginevents; andpartial_markeruses 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): Implementedparse_eosrequiring 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): Implementedparse_eosand updatedparse_candidateto allow non-strict calls missing</function>at</tool_call>or naturalEOSwhen at least one closed<parameter=...>...</parameter>is present with no trailing non-whitespace. Bare<function=name>, unclosed parameter values, trailing text, andstrict: truetools still fail.helios/src/output/gemma_call.rs(GemmaCall): Implementedparse_eosusing the brace-balanced parser (call:name{...}, including explicit{}) with no trailing non-whitespace, while rejectingstrict: truetools, bare headers, and unclosed braces or<|"|>strings.helios/src/output/minimax_xml.rs(MiniMaxXml): Implementedparse_eosand updatedparse_candidateto allow non-strict invocations missing</invoke>at</tool_call>or naturalEOSwhen 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, andstrict: truetools.helios/src/output/pipeline.rs(PlainText): Implementedparse_eosreturning the existing unconfigured-parser error.helios/src/output/tagged/kani_proofs.rs,helios/src/lib.rs,.github/workflows/helios-lifecycle.yml, andverification/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, andselect_candidate_picks_earliest_closer_and_tracks_outer_closed), enabledpub mod outputunderany(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: truecalls 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-verifier 0.68.0, CBMC6.11.0):cargo kani --manifest-path helios/Cargo.toml --features runtime-core --libtagged_tools_eos_decision_enforces_termination_and_confirmation:0/106failed,4/4cover properties satisfied (0.09s).select_candidate_picks_earliest_closer_and_tracks_outer_closed:0/149failed,2/2cover properties satisfied (0.38s).partial_marker_utf8_preserves_char_boundaries:0/233failed,1/1cover property satisfied (10.27s).partial_marker_ascii_matches_oracle:0/235failed,2/2cover properties satisfied (14.76s).cargo-mutants 27.1.0):Scoped via
--in-diffto all changes inhelios/src/output/against--lib -- output:: server::protocol::tests::unclosed_tagged server::protocol::tests::strict_tool_wires server::logprobs::tests::unclosed_glm:len -= 1->len /= 1andlen += 1inpartial_marker).at < end->at <= endinselect_candidate(distinct non-prefix outer/inner closing tags cannot start at the same byte) andlen > best->len >= bestinpartial_marker(matchinglen == bestonly reassignsbest = bestand breaks).</>in GLMToolBodynames,in_json_stringat EOS,outer_tool_pendingafterouter_closed, bare<function=lookup>inparse_eos,TaggedOutput.callspost-finish, multi-callemit_callsindexing,FunctionTags::parse,PlainText::parse_eos), all now covered and killed in pass 2.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-gatedgrammar::tests). Includes exhaustive prefix-monotonicity checks forArgumentTags::starts_callandFunctionTags::starts_call, prefix completeness forpossible_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-runandcargo 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_templatesrequiresFLARE_TEST_TEMPLATE_DIRand was not run.FLARE_*_TEST_SNAPSHOT:reasoning_delimiters,glm_frontend,parser_start, and the 4 ignoredgrammar::unit tests) and live GPU serving runs were not executed.partial_marker,select_candidate, andTaggedTools::eos_decisionwithin their stated bounded domains (andeos_decisionusesmem::forgeton its emptyArc<[serde_json::Value]>fixture to isolate the borrowed method fromserde_json::Value::drop); fullserde_jsonargument decoding and dialect tag grammars are verified by unit, protocol, exhaustive prefix-monotonicity, and mutation tests rather than CBMC.Discord: no username provided