Files
leleka/docs/tool-call-corpus.md
T
Pavlo RatushnyiandClaude Sonnet 5.5 ab54ae208f feat: experimental tool_call_recovery for drifted tool-call forms (default OFF)
Adds Options::recovery to tool_output and an opt-in setting, `tool_call_recovery` (config key) / STRATA_TOOL_CALL_RECOVERY=1
(environment, wins over the file; 0/false turns it off; anything else stops the start). It is off unless set, is read once at
start, logs "tool_call_recovery is ON (experimental)" at start, and logs every call it accepts. It is NOT part of the default
contract and has not passed a quality gate (docs/api-contract.md, "Tool-call recovery (experimental)").

With it on, a call for a DECLARED tool written next to the template form is read as the call (upstream 3fcb2106, #3441):
* <parameter=NAME> where <function=NAME> belongs; <=NAME>, <function:NAME>, <function =NAME> or <NAME> after <tool_call>;
* {"name": ..., "arguments": {...}} inside <tool_call>, also in a Markdown fence, arguments possibly a JSON string;
* a bare <function=NAME> ... </function> at the start of a line outside a code fence and inline code (a stray </tool_call>
  after it is dropped); the Markdown state is tracked over the text that has left the stream buffer, so streamed and whole
  readings agree;
* a second call in the same <tool_call>;
* for array/object arguments, brackets left open at the end and a bare string where an object member belongs (dropped).
An undeclared name stays an error in every form. Streaming: a batch is delivered when its </tool_call> arrives, a bare call when
the text after it shows it is over or at the end.

With the option off the reading is byte for byte the step-3 reading. The corpus test now reads every specimen twice (off and on)
and lists the failures of each; every streamed cut agrees with the whole reading in both. Recovery rate on the 41 Qwen specimens:
off 24/41 (58.5 %), on 30/41 (73.2 %); the 6 specimens that need recovery: off 0/6, on 6/6. That is code reach on upstream's
failure corpus, not a rate for real sessions. strata-control lib 339 passed (new: 3 recovery tests, 1 config test), corpus and the
quoted-value shapes pass with the option on and off.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-30 03:54:39 +01:00

14 KiB

Tool-call corpus: which specimens the parser reads

The Strata upstream keeps a corpus of 49 model outputs for its tool-call and reasoning parser (serve/test_tool_corpus.py) and a file of quoted-call value shapes (serve/test_quoted_call_values.py), both MIT licensed. They are ported as control-plane/tests/fixtures/tool_corpus/specimens.json (provenance and licence in NOTICE.md next to it) and run by control-plane/tests/tool_corpus.rs against control-plane/src/tool_output.rs. This page records which of them the parser reads as upstream expects, stage by stage, so a fix can be shown to change the result it claims to change.

Origin of the question: upstream issue #4078 (a string argument that quotes a tool call is cut at the quoted text), and #4427, #4077, #3796 and #3972 (calls that upstream now recovers instead of failing the turn). The probe that confirmed the truncation is in docs/upstream-review/cpu-and-platform.md on branch claude/upstream-review-cpu, section 8.

How the specimens are read

  • A specimen is the text a model wrote, the tools the request declared, and what a client must get back: the calls (name and arguments), the answer text and the reasoning text. The 8 dsml-* specimens are DeepSeek-V4.1's DSML form, which this repository does not parse; they are skipped (41 of 49 are run).
  • Whole: tool_output::parse_complete on the answer. This is the path of every request that reasons (the reasoning split of responses.rs::decode_generation is reproduced by hand in the test) and of non-streamed requests.
  • Streamed: tool_output::Incremental fed in pieces of 1, 3 and 7 characters and cut in two at every character boundary. This is the path of a streamed request that does not reason. Every cut must give the same calls and text.
  • Not compared: upstream's counters (unread, notes) and the ids of calls. Text is compared after trimming.
  • A specimen marked length ended at the token limit; the test treats it like the others (our code parses the answer whatever the finish reason).
  • The test is strict both ways: a specimen that fails and is not listed in KNOWN_FAILURES fails the test, and so does a listed one that passes. Run TOOL_CORPUS_TABLE=1 cargo test --release -p strata-control --test tool_corpus -- --nocapture to print the table.
  • From step 4 on every specimen is read twice, with the default options and with the experimental tool_call_recovery on (Options::recovery); the test lists the failures of each reading separately (KNOWN_FAILURES, KNOWN_FAILURES_WITH_RECOVERY).

Result by stage

stage specimens read as upstream expects (of 41)
main (before) 11
after 2 13
after 3 24
after 4, recovery off 24
after 4, recovery on 30

Specimens

ok: the calls, the answer text and the reasoning text are what upstream expects, whole and in every streamed cut. A reason is the first difference found at the final stage shown. recover marks specimens upstream reads only with tool_call_recovery on.

specimen what it is recover main (before) after 2 after 3 after 4, recovery off after 4, recovery on first difference at the last stage
4427-todos-valid #4427 first example (the Harness todo_write call) ok ok ok ok ok
4427-todos-stray-items #4427 second example: "status": "pending", "in_progress", "pending": false FAIL FAIL FAIL FAIL ok
4427-todos-single-quotes #4427 shape: Python quotes FAIL FAIL ok ok ok
4427-todos-trailing-comma-and-open-bracket #4427 shape: a cut array FAIL FAIL FAIL FAIL ok
4427-todos-json-string-of-an-array #4427 shape: the array written as a JSON string FAIL FAIL ok ok ok
4427-todos-not-json-stays-text #4427: text that is no array stays the text it is ok ok ok ok ok
4427-one-line #4427: the tags on one line ok ok ok ok ok
4427-one-line-in-thinking #4427: written inside the thinking, the turn ends on it FAIL FAIL ok ok ok
4078-nested-call #4078: a value holding a whole call FAIL ok ok ok ok
4078-reopened-parameter #4078: a value that reopens its own parameter FAIL FAIL FAIL FAIL FAIL whole: error: duplicate tool parameter: content
4078-heredoc #4078: a heredoc that prints a quoted call FAIL ok ok ok ok
4078-call-format-in-a-file #210: a file that documents the format ok ok ok ok ok
4077-stray-think-line #4077: the model quoted the closing tag at a line start FAIL FAIL FAIL FAIL FAIL whole: content differs
4077-stray-think-in-a-fence #4077: the quote sat in a code fence, so the fence closes in the 'answer' FAIL FAIL FAIL FAIL FAIL whole: content differs
4077-stray-think-after-a-backtick #4077: an open backtick FAIL FAIL FAIL FAIL FAIL whole: content differs
4077-quoted-think-stays-reasoning #537: a closing tag after a backtick is still reasoning (control) FAIL FAIL FAIL FAIL FAIL whole: content differs
4077-closer-in-the-answer-text-is-kept-mid-line a closing tag inside a sentence of the answer is text (control) ok ok ok ok ok
4077-no-second-closer-is-the-normal-case control ok ok ok ok ok
qwen-one-call the template's own form ok ok ok ok ok
qwen-int-argument a declared integer ok ok ok ok ok
qwen-after-thinking the normal agent turn ok ok ok ok ok
qwen-call-in-the-thinking #804: the turn ends inside the thinking, on the call FAIL FAIL ok ok ok
qwen-three-calls parallel calls, one wrapper each ok ok ok ok ok
qwen-calls-with-prose-between prose between calls ok ok ok ok ok
qwen-three-calls-in-the-thinking #804: two calls at the end of the thinking FAIL FAIL ok ok ok
qwen-function-close-left-out #4427 shape: no (PR #3960's Coder-L slip) FAIL FAIL ok ok ok
qwen-function-close-left-out-in-thinking the same inside the thinking FAIL FAIL ok ok ok
qwen-wrapper-close-left-out the output ends on (the server always streams) FAIL FAIL ok ok ok
qwen-wrapper-close-left-out-in-thinking the same inside the thinking FAIL FAIL ok ok ok
qwen-wrapper-close-left-out-two-calls a following opener confirms the missing closer FAIL FAIL ok ok ok
qwen-wrapper-close-left-out-two-calls-in-thinking the same inside the thinking FAIL FAIL ok ok ok
qwen-undeclared-name-is-text by design: a call for a tool the request did not declare comes back as text (and is counted) FAIL FAIL FAIL FAIL FAIL whole: error: undeclared function in tool call: format_disk
qwen-cut-inside-a-value an unterminated tag: the output stopped at the token limit; no call FAIL FAIL FAIL FAIL FAIL whole: error: tool parameter is incomplete
qwen-cut-inside-the-opener cut inside the name FAIL FAIL FAIL FAIL FAIL whole: error: tool call function name is incomplete
qwen-cut-after-function-close-on-length a turn cut by the token limit is not a natural stop: no call FAIL FAIL FAIL FAIL FAIL whole: error: tool call is missing </tool_call>
qwen-quoted-in-a-fence #1058: a call in a code fence is a quote FAIL FAIL FAIL FAIL FAIL whole: calls: got read want
qwen-prose-names-the-format prose that names the tag FAIL FAIL FAIL FAIL FAIL whole: error: tool call needs <function=NAME>
drift-parameter-as-opener corpus 300dfba2 (x11) yes FAIL FAIL FAIL FAIL ok
drift-json-in-the-wrapper corpus 5e847539 (x6) yes FAIL FAIL FAIL FAIL ok
drift-bare-function corpus 395efd4c / 6e71eed4 (x5 each) yes FAIL FAIL FAIL FAIL ok
drift-batch-in-one-wrapper corpus 0912870b / 51b155c9 yes FAIL FAIL FAIL FAIL ok

Quoted-call value shapes (serve/test_quoted_call_values.py)

Six string-argument shapes, each read whole and streamed in pieces of 1, 7 and everything; the argument must come back exact and no text may leak.

shape main (before) after 2 after 3 after 4, recovery off after 4, recovery on
quoted call FAIL ok ok ok ok
quoted call at the end FAIL ok ok ok ok
heredoc FAIL ok ok ok ok
repeated parameter FAIL FAIL FAIL FAIL FAIL
unclosed opener in prose ok ok ok ok ok
unclosed opener in code ok ok ok ok ok

What each stage changed

  • after 2 (truncation fix). A string argument that quotes a whole tool call is no longer cut at the quoted text. find_parameter_end ends a value at the first </parameter> that is followed by the next <parameter= or </function> and that is not inside a call the value quotes: the quoted openers (<function=NAME> then <parameter= or </function>) before it must not outnumber the </function> closers before it. A bare <function= in prose or code does not count. Whole and streamed reading agree. A call whose value quotes an opener and never closes it is now treated as unterminated (an error) instead of being cut at the first plausible close; the previous behaviour would have published a truncated call whenever a long write that quotes a call was itself cut by the token limit. Not changed, on purpose: a value that reopens its own parameter (4078-reopened-parameter, the repeated parameter shape). Upstream reads </parameter><parameter=content> inside a content value as text. Here it is the same bytes as a parameter written twice, which native_tools_sdet::unknown_incomplete_duplicate_or_late_invalid_call_refuses_entire_output requires to be refused; reading it as one value would merge two arguments of a malformed call into a corrupt one without an error. It stays an error (duplicate tool parameter).

  • after 3 (recoverable cases). A call whose </function> was left out (</parameter> then </tool_call>), whose </tool_call> was left out (next call's opener, or the end of a generation that stopped by itself), a call the reasoning ends on instead of </think>, and a damaged array argument (trailing or doubled comma, single quotes, Python literals, extra closers, an array in a JSON string) are now calls; each is logged. See docs/api-contract.md, "Tool-call parsing", for what a client sees.

  • after 4 (tool_call_recovery, experimental, off by default). With the option off the result is exactly that of step 3 (24 of 41; the same specimens fail for the same reasons). With it on, 30 of 41 are read as upstream expects: the four drift-* specimens (a <parameter=NAME> opener, a JSON call in the wrapper, a bare <function=NAME> block, a batch in one wrapper) and the two damaged-array specimens that need a guess (a bare string inside an object, brackets left open). Recovery rate on this corpus: off 24/41 = 58.5 %, on 30/41 = 73.2 %; of the 6 specimens that need recovery, off 0/6, on 6/6. These are 41 specimens chosen by upstream to show its own failures, not a sample of agent turns, so the rates say what the code can read and nothing about how often a real session drifts. Upstream's own figure for its 1,462-turn corpus (98.3 % read as intended with recovery, 92.9 % without; Qwen3.8 under Claude Code, not Codex) is its claim and is not reproduced here. Every streamed cut of every specimen gives the same result as the whole reading, with the option on and off.

Specimens that stay unread, and why

specimen(s) why this parser reads them differently
qwen-undeclared-name-is-text Deliberate. A call for an undeclared tool is an error (failed response), never text and never a call. Upstream returns the markup as text and counts it; text ends an agent's turn as if it were the answer, a failed response is retried. Pinned by an_invalid_tool_call_never_becomes_a_successful_function_call and other_undeclared_functions_still_fail.
4078-reopened-parameter (and the repeated parameter value shape) Deliberate. </parameter><parameter=content> inside a content value is the same bytes as a parameter written twice. native_tools_sdet::unknown_incomplete_duplicate_or_late_invalid_call_refuses_entire_output requires that to be refused, and merging the two into one argument would hide a malformed call. It stays an error (duplicate tool parameter); upstream reads it as text.
qwen-cut-inside-a-value, qwen-cut-inside-the-opener, qwen-cut-after-function-close-on-length A generation cut by the token limit inside a call is an error here (generation ended inside a tool call), where upstream returns the text with an incomplete status. Never a published call, so nothing is run. Pinned by unfinished_live_tool_call_fails_without_publishing_arguments; changing it is a separate decision.
4427-todos-stray-items, 4427-todos-trailing-comma-and-open-bracket The repairs that drop a bare string from an object or close brackets the model left open decide what the model meant, so they are not on by default. Read with tool_call_recovery on.
4077-stray-think-line, -in-a-fence, -after-a-backtick, 4077-quoted-think-stays-reasoning Not a tool-call matter: the reasoning ends at the first literal </think>, even one the model quoted in backticks or wrote twice. A second </think> stays in the answer text. Unchanged; a reasoning-boundary decision.
qwen-quoted-in-a-fence A call written inside a Markdown code fence of the answer is run. Upstream treats it as a quote. Not changed here: it alters which well-formed calls run. Worth a decision.
qwen-prose-names-the-format <tool_call> followed by something other than <function= is an error, so a sentence that names the tag fails the response. Upstream returns it as text. Not changed here.
drift-* (4) Need tool_call_recovery; read when it is on.