mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[GH-ISSUE #3075] [Engine] a tool call the request never offered still becomes tool_calls on the content path (frontend.py:1285, 0.2.1) #1232
Labels
No labels
needs-format
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Niko1221/Strata#1232
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @riverhh76 on GitHub (Oct 18, 2026).
Original GitHub issue: https://github.com/Niko1221/Strata/issues/3075
Environment
main@477ac13c(fetched 2026-10-19),--engine strata, OpenAI-compatible serveswift-1.5-iq3_xxsand theqwen3.8-flash-nextfamily — model-independent (see"Another observation, not part of the fix")
{"temperature": 0, "top_p": 1.0, "presence_penalty": 0}— greedy0.1.45container imageSummary
OutputParserscans the answer for<tool_call>in two states. The scan in thereasoning state is gated on whether the request declared tools; the scan in the
content state is not. So a request with no
toolsfield at all still yieldsmessage.tool_callsandfinish_reason: "tool_calls"whenever the model writes thatXML — and the function name is never checked against the declared set (there is no
declared set).
A client that does the normal thing —
if (msg.tool_calls): run(msg.tool_calls)— thenexecutes calls for tools it never offered. When the client instead ignores them because
it offered none, it gets
content: ""back, i.e. an empty answer.Minimal reproduction
No GPU, no model, no server — the parser alone.
Actual output —
tools=Nonewas passed:tools=[]behaves the same. The streaming path reproduces it too: over HTTP withstream: trueandstream_toolsalways on (server.py:4216), all four subtests of theregression test below fail on the unpatched tree.
Observed behaviour matrix
Same parser, same model output, varying only what the request declared:
tool_callsemittedcontenttoolsabsent)tools: []tools: [get_indicators]tools: [some_other_tool]get_indicatorsThe last row is a second, independent gap: a request that does declare tools still
gets a call to a function that is not in the set. The guard proposed below does not cover
it; it is written up separately under "A second, related gap" because the obvious patch
for it does not work.
Root cause
One line, sitting next to the guarded twin of it:
With no tools,
self.schemas == {}(built at:887), yet the content state stilltransitions into
"call"and runs_finish_call, which builds theToolCallat:872without ever comparing
namewith the declared names (:1448-1451doesself.schemas.get(name), which isNoneand is accepted).Why this reads as an oversight rather than a decision. Every neighbouring path in the
same parser checks something the content path does not:
rcallstate (a call quoted in the reasoning):1203if name in self.schemas:1246,:1258self.schemasjson_tool_call(therecoverpath):829-830obj["name"] not in schemas → None:1285The JSON sibling of the same feature validates the name; the XML path does not.
This also runs against the principle the parser has already been fixed to hold elsewhere:
a
<tool_call>that is quoted is text, not a call — #1058, #2039, and #700 ("serve: a<tool_call>named in prose before the real call is content, not a malformed call"). Therule those fixes established is that text which looks like a tool call is text. Here the
inverse holds: a tool call naming a function that was never declared is still a tool call.
Real-world impact
Production agent (
TradingAgents) on 600076.SH, 2026-10-15. The technical analyst ranfour tool rounds, then sent a convergence turn that deliberately omits
toolsandappends "tool call rounds are exhausted, you must not call any more tools". The
response was:
The agent writes
contentonly whentool_callsis empty, so the analyst's report becamethe empty string, the data-quality gate graded it F, and the final research document
shipped a section reading (empty). A production research pipeline produced a report with
a silent hole in it.
This is not rare, and it predates the current release: our own bench logs from 2026-10-04
(Strata 0.1.38/0.1.39) already recorded
finish=tool_callson requests that carried notools, twice, noted then as "occasional tool_calls hallucination" and attributed to themodel. It is the parser.
Anyone whose convergence/final-turn pattern is "stop calling tools" is exposed — that is a
common and reasonable agent design.
Test gap
serve/test_server.py:4313 test_auto_absent_and_none_leave_the_tools_to_the_modelis thetest for this case, and it only checks the prompt side:
Its fake engine never emits tool-call XML, so the answer side is never exercised. There is
no test anywhere asserting that a request which declared no tools cannot come back with
tool_calls.Suggested direction
Mirror the guard that already exists at
:1246:That one line is the whole fix for this report. Measured with the repo's own
serve/test_server.pyinside the 0.1.45 image (jinja2 3.1.6):477ac13c, with the new regression testnothing declaredandtool_choice none, streaming and not)The new test is
ForcedToolChoice.test_a_call_the_request_never_offered_stays_text: a modelthat, after
</think>, writes a complete well-formed call naming a tool the request neveroffered. Today that comes back as
tool_callswith an empty content; with the guard thewhole answer comes back as text, call and all.
The test also pins the case where the request declared other tools and
tool_choice: none— both must produce text, and both do today only by accident of the first guard being absent.
A second, related gap: an undeclared name, with tools declared
When a request does declare tools but the model names a function that is not among them,
the call is emitted anyway:
[some_other_tool]get_indicatorstool_calls: [get_indicators], content emptyThis one is not fixed by the guard above, and I want to be precise about why, because the
obvious patch does not work.
Service.runalways constructs the parser with streaming on (server.py:4216,stream_tools=True). With streaming on, the call is consumed by_scan()before_finish_call()runs (frontend.py:1395-1408):_scan()hits<function=NAME>atfrontend.py:1078-1087, buildsToolCall(name=name, arguments={})with no membership check —self.schemas.get(name)at
:1085is used only to read declared parameter types — and immediately emitstool_start, so the client is already told about the call.finish()(:1428-1431) emits the finaltool_callfor that already-announced call.Adding a name check inside
_finish_calltherefore changes nothing observable over the wire:by the time it runs, the
tool_start/tool_argschunks are already on the wire and theclient has assembled the call. I confirmed this by measurement — with such a check added, the
response carried both the text and a
tool_callsentry.The right place is
_scan()at thess == "name"step (:1083): the name is complete thereand nothing has been emitted yet, so an undeclared name can still be refused and the raw text
returned as content. That is a real change to the streaming state machine, so I would rather
raise it than guess at it. Flagging it as a separate item rather than folding it into the
one-line fix above — happy to work on it if you want it in the same PR.
Another observation, not part of the fix
serve/chat_template.jinja:130-157renders an assistant message'stool_callsunconditionally — that loop sits outside the
{%- if tools ... %}block at:72. So arequest that declares no tools, but whose history contains tool-calling turns, puts verbatim
tool-call demonstrations into the prompt:
Measured on
swift-1.5-iq3_xxswith the repo's own tokenizer(
tools/strata_tokenizer.pyover/data/packs/swift-iq3_xxs/tokenizer):tools=Nonetools=[one tool]The prompt then ends with an already-opened assistant turn (
<|im_start|>assistant\n<think>\n\n),after a final user turn that reads "Tool call rounds are exhausted. You must not call any
more tools. Write the final report now."
Greedy decoding on that context continues the demonstrated pattern. That is expected, and it
is not the bug — a model writing
<tool_call>after being shown four examples is behavingreasonably. The bug is that the parser treats the result as a real call.
Worth noting that the reasoning-state equivalent is already handled: a call quoted inside the
thinking stays reasoning text (
rcallat:1194-1212, guard at:1203/:1246). The answerof a non-thinking turn, or the part of a thinking turn after
</think>, was not. I confirmedthat split by measurement: the same XML before
</think>stays text today; the same XML afterit becomes a call.
I am not proposing a change here — the history rendering cannot simply be dropped without
corrupting the conversation — but the trigger is prompt hygiene, not the weights, so
swapping models does not remove it.
What I did not test
Content→callpath was exercised directly. Thercall(reasoning) pathalready gates correctly and was left alone.
parser plus the full serve suite, not a served request.
@riverhh76 commented on GitHub (Oct 18, 2026):
A one-line fix for the first of the three items, in case it saves you the round trip.
The content state simply gates on the declared tools, the way the reasoning state three lines above already does:
Regression test, next to
test_auto_absent_and_none_leave_the_tools_to_the_model:Measured in the released 0.1.45 container (jinja2 3.1.6), patch and baseline each in its own
scratch tree,
/appuntouched:477ac13c, test addedserve/test_server.py, baselineserve/test_server.py, patched + testThe test drives the HTTP path, so
stream_tools=True— the server's only mode — is covered.Scope: this touches only the
:1285gate. It leaves the two other items alone — the_scan()streaming gap, where a name check in_finish_callprovably does not work becausetool_startis already on the wire by then, and the template rendering. No existing test neededchanging, no snapshot or timing expectation moved.
Behaviour change for the changelog: a tools-less request can now return the
<tool_call>XML astext in
contentwhere it returnedtool_callswith an emptycontent.Happy to open a PR if you want it — I did not send one, since the few I saw on recent
serve/changes seemed to get closed.
@ctr-137 commented on GitHub (Oct 18, 2026):
Measured on a working checkout of
main(Windows, Python 3.14.7, CPU only, no GPU, no model files).The test. I wrote the missing answer-side test as
ForcedToolChoice.test_a_call_the_request_never_offered_stays_textinserve/test_server.py: after</think>the model writes a complete, well-formed call forget_indicators, and the request declares nothing —toolsabsent,tools: [], andtool_choice: "none", each streamed and not, so 6 subtests. On the unpatched tree all 6 fail withfinish_reason: "tool_calls"and empty content; with the guard atserve/frontend.py:1285all 6 pass.What the guard does not leave alone. Two existing tests assert the opposite, so the one line is a behaviour change, not a fix:
serve/test_mcp.py:329(test_plain_requests_get_no_mcp_tools) — a request that declares no tools, the model writes a call, and the test assertsfinish_reason == "tool_calls", with the comment "returned to the client as always". With the guard it becomesstop.serve/test_parser_call_scan.py:56(test_no_structural_rescans_until_closer_and_reset_for_next_call) — a parser built with no tools is expected to enter the call state and reachcall_endonce. With the guard the scan never starts, so the count is 0 instead of 1.Full
servesuite on this machine:test_winjob,test_keepawake— Windows/timing, unrelated to this)So I cannot reproduce the clean run you measured in the 0.1.45 image. Which behaviour is intended here? If the guard is the rule, those two tests have to be rewritten, and the change stops being a one-liner: it decides what the content state does with a call for a tool the request never offered. I would rather ask than quietly flip those two expectations. The test is ready in my working tree — with a decision I can open the PR with the guard, the test and the two adjusted expectations in one change.
@riverhh76 commented on GitHub (Oct 19, 2026):
You are right, and my verification was too narrow. Thank you for catching it before this became a PR.
What I got wrong. I ran
serve/test_server.pyalone (337 passed) and called it the suite.serve/holds 49 test files and 848 tests. Re-run on a checkout ofmain@477ac13c, CPU only:servesuite:1285guard_finish_callonly (no gate)The baseline failure is
test_server.py::SilentEngine::test_a_busy_gpu_is_not_a_frozen_engine; it passes when run alone and failed only while the machine was busy. It is not related. (The third row's single failure is not that one — see the last section.)Your two, and a third you did not list. The guard fails three tests, not two:
serve/test_mcp.py:329test_plain_requests_get_no_mcp_tools— yoursserve/test_parser_call_scan.py:56test_no_structural_rescans_until_closer_and_reset_for_next_call— yoursserve/test_responses.py:687test_the_kept_tools_shape_only_the_prompt— not in your list, and its comment is the most explicit of the three: "the calls come back as for any request without tools: no namespace, no custom tool"So the pass-through is written down in two protocols (Chat Completions' MCP path and the Responses/Codex path) and three tests, one dating to 2026-09-28. I am withdrawing the guard. Asking you to flip three written expectations to land a one-liner is the wrong trade, and I no longer think this is an oversight.
One distinction on your second test.
test_parser_call_scan.pyis a re-scan test: it countscall_endwalks to prove a long body is not rescanned. It buildsOutputParser(thinking=False)with no tools because that is the shortest constructor, not because it asserts anything about tools. Give it the tool it actually writes and it passes under the guard:2 passed. So that one is a test that never declared the tool it uses — onlytest_mcp.py:329andtest_responses.py:687are behaviour assertions.Which behaviour is intended — what I can measure. The two states are exactly opposite today, and each side has its own tests. Measured on
OutputParserat477ac13c, streamed and whole:</think>(content)maintool_callstest_mcp.py:329,test_responses.py:687tool_callsmaintest_reasoning_tools.py:62,test_reasoning_rescue.py:155test_reasoning_tools.py:53,test_reasoning_rescue.py:162The content JSON path (
:927/:934,test_server.py:1105) and the content recovery path (:1286,:1305,test_frontend.py:336) also keep an undeclared name as text. So #804's rule — a tool call counts only for a declared tool — holds in the reasoning state (the:1246gate plus the:1203membership check), in the content JSON path, and in the content recovery path. The content structural path (:1285,_finish_callat:1451) is the only one where neither the gate nor the name is checked.If the answer is the content state hands back whatever the model wrote, that is coherent, my issue's headline is wrong, and I will close it — the two states would simply be meant to differ. If the answer is #804's rule, the undeclared-name row is the part to change, and the no-tools rows above stay exactly as they are.
Two things to know before choosing the second. I tried the narrow version — a name check in
_finish_call(:1451) only, no gate at:1285— and it is neither sufficient nor free.It is not sufficient.
server.py:4216builds every parser withstream_tools=True, so_scan()has already emittedtool_startat:1087by the time_finish_callruns. Measured, tools declared and a call for another name:tool_start/tool_argsevents and then the same body as textThe client is told a call started, is given its deltas, and then the same body arrives as content — worse than either answer on its own.
_scanhas to be gated too.And it is not free.
name in self.schemasis the wrong test, because the name on the wire is not always aschemaskey. Seetest_responses.py:520test_namespace_call_written_with_double_underscore: Codex declares a namespace tool namedmcp__websearchholdingweb_search, and Qwen writes the call as the flat<function=mcp__websearch__web_search>.responses.py:254offers the templatemcp__websearch.web_search(dot form) and maps that spelling back, then:271deliberately adds the underscore spelling to the name map without adding it to the tools — with the comment "Qwen writes Codex's MCP tools asmcp__server__tool(the flat name it learned), notmcp__server.tool: map that spelling back as well". So the underscore form is innamesand by design not inschemas.With the check added, the full
servesuite still reports one failure — but it is a different one: the busy-machine timing test passes, andtest_responses.py:520fails instead. The check rejects a call that path exists to accept. Measured in both trees on the same machine.So the check cannot be
schemasmembership; it has to go through the resolution the protocol itself uses. Which is your point exactly: this decides what the content state does with a call for a tool the request never offered, and it is a decision rather than a one-liner.Offered: whichever you pick, the undeclared-name row has no coverage in any path today, and it is worth having either way — as a "stays text" test or as a "still comes back" one. Say which and I will write it.
@Niko1221 commented on GitHub (Oct 19, 2026):
Thanks for taking the time to report this.
We're resetting the tracker for 0.2.0-beta. There are more than 1,300 open issues and pull requests, too many for us to read properly, so we're closing everything that isn't being worked on right now. Closed here does not mean rejected.
To reopen it takes about two minutes:
/reopen. A check runs and either reopens the issue or lists what is still missing.The rules are in CONTRIBUTING.md.
@riverhh76 commented on GitHub (Oct 20, 2026):
Re-test on 0.2.1, per the tracker reset
Re-tested on 0.2.1 (engine 0.1.46) on 2× RTX A5000 (sm_86), as step 1 of the reset asks. It is still there, and the local sources show why: the rule that #804 added to the reasoning state is missing from the content state.
What happens
A request that declares no tools (
"tools": []) sends back atool_callsarray when the model writes a call in its answer. The answer text is lost:contentcomes back as"". The same call written inside<think>is left alone as text.toolsdeclaredfinish_reasontool_callscontent<think>tools: [])stop<think>tool_calls""✅tools: [])tool_calls""❌tool_calls""✅So one and the same undeclared call is text or a call depending only on whether it lands before or after
</think>."tool_choice": "none"behaves the same way (the request is rewritten totools = Noneatserve/server.py:6695): 4 of 4 combinations (two cases × streaming and not) come backtool_calls.Where
serve/frontend.py,OutputParser.feed()— two states, each scanning for<tool_call>:reasoningtool = self.buf.find(CALL_START) if self.schemas else -1contenti = self.buf.find(CALL_START)— noschemasgate1246 was added by
8d0bb446(2026-10-05) for #804, "a tool call inside the reasoning counts only for a declared tool".serve/frontend.pyhas no changes between477ac13c(0.1.45) andv0.2.1, which is why this reproduces on both.Smallest reproduction
One request, no tools. The model keeps writing the call format it was shown in its history — the same thing the test's
CallingEnginefakes, only through the ordinary HTTP path:Reply:
finish_reason: "tool_calls", one call,content: "". Expected:finish_reason: "stop"with the call left incontentas text — which is what the reasoning state already does.A note on what does not reproduce it: asking the model to "search the web for 2+2" gives
2 + 2 = 4as text and no call at all (0 of 4) — the format has to be in its history, or the request has to ask for the call in the thinking block. Assistant prefill is ignored by the engine. So the history above is not decoration; without it there is nothing to mis-parse.Reproducible?
Yes — 4 of 4 (the two no-tools cases, streaming and not) on 0.2.1, and the same on 0.1.45. The "written in the answer / written in the thinking" pair above is the one that shows the inconsistency.
Version, hardware, model
0.2.1-cuda13image.--layer-split 28,--kv int8 --kv-resident 32768,--spec 6with MTP.The question
Is the content state meant to follow #804?
I am not asking for a particular policy, only that the two paths agree. Happy to open a PR with the one-line gate plus the test from my earlier comment if you want it.
@riverhh76 commented on GitHub (Oct 20, 2026):
/reopen
@github-actions[bot] commented on GitHub (Oct 20, 2026):
Not reopened yet. Still missing:
Edit the issue and comment
/reopenagain. Details: CONTRIBUTING.md@riverhh76 commented on GitHub (Oct 20, 2026):
/reopen
@github-actions[bot] commented on GitHub (Oct 20, 2026):
Reopened, thank you. A maintainer will pick it up from here.
@Niko1221 commented on GitHub (Oct 21, 2026):
Fixed for 0.2.2, the next release (not out yet). For Chat Completions and /v1/messages a call counts only for a tool the request declared, with or without tools, before or after
</think>. The Responses route keeps returning calls from a request without tools because Codex's compaction request relies on it.@riverhh76 commented on GitHub (Oct 21, 2026):
Thanks — that answers the question. We'll re-run the same four cases on 0.2.2 and report back here, then close it if the content path keeps the undeclared call as text.
@riverhh76 commented on GitHub (Oct 21, 2026):
Tested on 0.2.2 (image
strata:0.2.2-cuda13, engine 0.1.47) — same requests, only the version changed.<tool_call>is writtentoolsdeclaredstop, 0 calls, kept as textstop, 0 calls, kept inreasoning_contenttool_calls, 1tool_calls, 1tool_calls, 1stop, 0 calls, kept as texttool_calls, 1tool_calls, 1Row 3 is the case from the report, and it now behaves as expected — 5/5 runs, the call stays
plain text and nothing is synthesised.
tool_choice: "none"(server rewrites totools = None) on the same content-path prompt:stopwith the call left as text, bothstream: falseandstream: true(0.2.1 returned
tool_callsfor both).No collateral damage here: 19/19 tool-call cases still pass with zero direction changes vs
0.2.1, including the two "must not call" cases (chit-chat,
1+1) which still returntool_calls: []; a "quoted tool call in prose" probe is still 5/5.Thanks for the quick fix — closing.
@achanbaka commented on GitHub (Oct 25, 2026):
Same class, fourth client: DeepSeek Harness (DSH) PTC / non-PTC — with before/after evidence that returning the call was the client's recovery mechanism
Adding data from a fourth client, plus run evidence across the upgrade that explains why this is not just "prompt the model better": for this client class the pre-0.2.2 behavior was the recovery mechanism, and we verified that delivering the same block as
tool_callsrestores it.Setup
DeepSeek Harness (DSH) has an agent preset ("PTC mode") whose request declares exactly one function,
run_code; the real tools are described in the system prompt and are supposed to be called from inside that program. Model: Qwen3.8-Flash-Next IQ3_XXS. Strata: 0.2.6 line / engine 0.1.50 (Qwen engine),/v1/chat/completions, streaming and non-streaming. Intermittently the model calls those tools directly by name (web_search,glob,pwsh, …), which lands exactly in the undeclared-name path.DSH's tool layer already treats a call to a "visible but collapsed" tool as a deterministic error result (it is not executed):
A genuinely unknown name gets
unknown tool "…". Either way the agent loop continues and the model corrects itself — but only if the engine hands the call over.Before / after the upgrade (same client, model and endpoint)
tool_calls(web_search,glob,pwsh, …)finish_reason: "stop"After the upgrade the leaked block stayed in history and the model imitated it on the next turn (same self-reinforcing pattern as #3441).
Same failure in non-PTC mode, with all real tools declared
While an MCP server was being set up but was not yet mounted, a request declared its 79 native tools and none from that server (no
mcp__*intools; no--mcpchild process). The model wrote a well-formed call for a name it invented from documentation it had been reading:That name appears nowhere in the session or its configuration — it is a hallucinated tool name, which is the same undeclared-name path. Result: the block as content,
finish_reason: "stop", turn over. Had it been delivered, the client would have answered "unknown tool" and the model would have continued.Deterministic reproduction on engine 0.1.50
Force the exact block for
web_searchwhile onlyrun_codeis declared (same technique as the #3441 repro, no model drift needed):Actual:
content= the whole block, notool_calls,finish_reason: "stop", plus the server line[strata] 1 <tool_call> block(s) in the answer did not read as a call of a declared tool and went back as text …; the names the model wrote: web_search.Control: adding
web_searchtotoolsin the same request returnsfinish_reason: "tool_calls", one correcttool_callsentry, empty content. So the block itself is well formed — this is not the #3441 malformed-opener case; only the declared-name check diverts it. The same rule serves/v1/messages(declared_only=Truein the server);/v1/responsesdiffers only for requests that declare no tools.Client-side recovery validated
We delivered the identical block as a normal
tool_callsentry (temporary client-side shim): the client answeredError: unknown tool "mcp__livecheck__ping"and the model continued to a normal final answer in the same turn — no raw XML. That is exactly what pre-0.2.2 did, and it confirms that the missing piece is delivery, not a client capability. We removed the shim; it was only to validate the recovery path.Proposal (same as the OP; one concrete shape)
"strata_tool_call_compat": true(name TBD; matchesstrata_mcp/return_progress). When the request declares at least one tool, a well-formed<tool_call><function=NAME>…whose NAME is not declared is returned as a normaltool_callsentry (arguments parsed as strings when no schema exists). Defaultfalse= exactly today's behavior, so #3075 stays the default; requests with notoolskeep returning text; the Responses route unchanged.rejected_tool_callsfield in the response / final SSE chunk (names only), so a client can drive its own retry. For DSH that needs an extra client-side loop; the call-as-tool_callspath needs none.Scope / safety
tool_callsshould not enable it.Reproduction status: reproduced locally (deterministic API repro on engine 0.1.50, stream and non-stream), observed in real sessions before and after the upgrade, observed in non-PTC mode with all real tools declared, and client-side recovery validated.
Related: #3883, #4071, #3441, #3075.
@cjmckenna commented on GitHub (Nov 1, 2026):
Another DSH data point on Strata 0.2.9, using Qwen3.8-Flash-Next IQ3_S with the
openai-completionsstreaming route: the model returned a complete<tool_call>for an undeclaredlinear_comment_get. Strata returned it as text withfinish_reason: stop, and DSH ended the turn. After a manual “continue,” the model called the availablelinear_comment_listtool successfully.We tested a DSH-side workaround using the
agent/turn-stoppinghook. When the whole response is a narrowly recognized, standalone call to an unavailable tool, it queues one corrective notice for the same turn. It does not execute the named tool. A scripted test using DSH 0.2.1-rc.5’s agent loop continued to the next model step with no tool execution; a clean-profile installation and focused checks for fenced examples, native tool calls, aborted turns, and max-token stops also passed.Limitations: the current pi-ai adapter does not surface
strata_rejected_tool_callsto this hook, so the workaround uses a deliberately narrow text heuristic, restricted to our configured Strata provider. It can retry once for an intentionally requested standalone XML example, and does not cover other malformed shapes. Live Desktop activation and end-to-end model recovery have not yet been verified. Using the explicit rejection marker in DSH would be a better general solution.