mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[GH-ISSUE #1058] 0.1.40: a tool call quoted in the thinking is delivered as a real call (fires on a max_tokens cut and inside code fences) #452
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#452
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 @47Hunter47 on GitHub (Oct 6, 2026).
Original GitHub issue: https://github.com/Niko1221/Strata/issues/1058
Summary. 0.1.40 (
9fb1b9f) fixes #804 and on real traffic it recovers more than the opt-in PR did — on a second harness here it turned 11 of 12 recorded strandings into real calls, where 0.1.39 delivered none. But the rescue's only condition is "the called name is one the request declares", so it also acts on a call the model wrote inside its thinking as an example. On a reply that then runs intomax_tokens, that means the client is handed a real tool call for something the model was only documenting — and on a client that auto-approves, it runs.The example everyone writes quotes a destructive command, so this is worth a look before it spreads:
That is a real response from a live 0.1.40 server, not a parser unit test.
Environment. 0.1.40, tag
1735d64, engine compiled locally on this machine (BUILD.json:source: local). Linux, 1× RTX 3090 24 GB, 61 GB RAM, driver 580.178.04. Qwen3.8-Flash-Next GSQ-RCO IQ3_XXS,--max-context 230000,--kv int8, images on (CPU encoder), thinking on, sampling temperature 1.0 / top_p 0.95 / top_k 20,reasoning_budget_tokens16384. Requests over/v1/chat/completions, non-streaming and streaming (they agree). Nothing was executed: the client read the response fields only, and the tool named in the request exists nowhere.Which conditions it fires under. Same dictated example, one variable changed per row, two attempts each:
length(cut mid-thought)length/stoplengthfinish=tool_calls, and two calls in one reply)```textfencefinish=tool_calls)So, in order: tools must be declared (V2 confirms the guard the maintainer described), the name must be one of them (V3 confirms the gate), and once those hold the rescue ignores where the call sits — not the start of a line, and not kept out by a code fence (V5) — and it does not care how the turn ended: a
max_tokenscut (V1, the dangerous one) and a natural stop (V4) both fire.Rate, to be fair about it: with the model asked to copy the exact block, ~50-100% of attempts fire; with a free-form prompt ("give an example of the call format, then keep thinking") it was 1 of 11 attempts — the model often writes the format loosely enough not to parse. It is stochastic, temperature 1.0.
The same result offline and deterministically. The corpus on PR #525 (
serve/fixtures/format_fix_specimens.jsonat4111c85, 25 entries with stated expectations) run against a 0.1.40 tree: the 5 "act" entries are correct, and 10 of the 20 negative controls now deliver a call — a fenced example, a mid-sentence quote, prose between two blocks (two calls), a call with no parameters, andcut-max-tokens, which is exactly the shape above. No model needed, justOutputParser:Mechanism. In
feed(), the reasoning branch enters a newrcallstate on any<tool_call>onceself.schemasis non-empty (tool = self.buf.find(CALL_START) if self.schemas else -1— no line or fence context), holds the block whole, and then:The declared-name condition is doing the work the content channel already does — that part is right, and it is why #970's two bogus
<function=tool_call>deliveries in #804 do not happen here. What is missing is that the content channel distinguishes a call the model is making from text about the format by position and by where the turn ended, and this path does not.Suggested gates, both cheap and both already in #525 (I am not arguing for the flag, only for the two conditions):
\n\n<tool_call>\n<function=NAME>— opener on its own line — and so do all four in #804, so this costs nothing in recall on the recorded data. It removes the fenced documentation case, the mid-sentence quote, and prose-between-blocks.length. A budget cut is when a thinking span is most often open, and a complete call in that reasoning was written as an example far more often than as an act. This is the case with a destructive command in it.One request: make it observable.
rcalllogs nothing and adds nothing toGET /metrics(totalsstill holds only the request/token/draft keys). So an operator cannot tell whether this class is firing on 1 reply in 100 or 1 in 100 000, and cannot tell when it acts on the wrong thing — the parse-level shape that #525 calls a hit is invisible here. A single counter (totals.tool_calls_from_reasoningor similar) would make the rate measurable per model and per quant, which is the question every harness will ask before turning this on.Corpora, both public. PR #525's
serve/fixtures/format_fix_specimens.json(25 entries, expectations included) is the deterministic half. Mine — 12 live strandings from this machine, genericised, in that same schema, 11/12 recovered by 0.1.40: https://gist.github.com/47Hunter47/4abcd3a80701f5634f85b96e4c067cc3 . Happy to re-run either against any head or to add the variants above as corpus entries.@47Hunter47 commented on GitHub (Oct 6, 2026):
We hit this class on a second harness and ended up carrying a local fix, so here is what we run, what it measures, and what it costs — posted as information for this decision, not as a request for anything.
The two conditions we added (nothing else changes; the rescue still fires only for a name the request declares):
length/cancel/disconnectend returns it as reasoning text.Plus one rule borrowed from #525's contract: a
</think>inside the reasoning clears the held block (a call inside a closed span was a mention, however well it parses).Measured offline, both corpora, same machine:
format_fix_specimens.json, 20 of them)max_tokens)stream_toolsoff)Already-streamed text is byte-identical before and after; only the reasoning-block case moves, and it moves to end-of-turn.
What it costs, honestly. One live stranding — a client cancel whose reasoning ended on a genuine call — is no longer delivered; that is the gate doing its job. And it turns 6 tests red in
serve/test_reasoning_tools.py, every one of them asserting the previous policy:test_a_declared_call_in_the_reasoning_is_a_call(a call opened mid-sentence,"before " + CALL, counted as a call),test_finish_reasons_and_block_order, and theRCALL_MAXtest whose assertion assumes the reasoning is held rather than streamed.That is exactly why we did not open a PR for it. Those tests are design statements — whether a
<tool_call>woven into a sentence inside the thinking is an act is your call, not ours, and a patch that flips them red is not a patch we should land on someone else's behalf.What it does not solve. A line-start block followed by more prose still fires (3 of #525's 20 controls). We kept that shape on purpose: it is the same shape as every real stranding we have —
\n\n<tool_call>\n<function=NAME>closing the span — so position alone cannot tell a quote from an act there.The change is a 155-line diff over
serve/frontend.pyandserve/server.py(the serve now passes its finish reason intofinish()). Happy to paste it here, attach it, or turn it into a PR if you would rather have one.Either way our earlier request stands: one counter in
GET /metricswould let an operator see whether this class fires at all, and how often it acts on the wrong thing, before deciding anything about the gates.@sergqwer commented on GitHub (Oct 6, 2026):
Thanks for the numbers. #1068 (opened 14 minutes before your comment) does the same two things you describe, plus a third condition that targets your remaining 3:
stop).length, cancel, disconnect and error return it as reasoning text.Condition 2 separates the shape you kept on purpose (a line-start block followed by prose) from a stranding. A stranding closes the span, so nothing follows it. A quote is followed by something: prose, a closing fence, or
</think>and an answer. Your</think>rule falls out of it, because</think>is not whitespace.The same corpora, run through
OutputParserwhole, one character at a time and in random 1-9 character chunks (every mode agrees):live-02,live-06)The two it no longer delivers:
live-02: the cancelled one, refused as you say.live-06: a declaredtodo_listblock followed by an undeclared<function=tool_call>envelope block. The second block breaks condition 2, so both stay text, as your corpus expects. Stock delivered the first half.What #1068 does not cover, and your fence rule does: an opener inside a ``` fence that the turn never closes, where the turn ends right after the block. A closing fence after the block already releases it. None of the live strandings sits in a fence, so adding your fence check would cost no recall on this data. I can add it to #1068 if the maintainer wants it.
Tests. #1068 rewrites
serve/test_reasoning_tools.pyto the new contract.test_a_declared_call_in_the_reasoning_is_a_callbecomes a call that ends the turn (one, two adjacent, at the start of the reasoning). The mid-sentence call is the first of the quoted shapes that must stay text. The others:</think>after it;max_tokenscut;An HTTP test checks that
max_tokenscutting the turn right after the call gives finishlengthand no call. It fails iffinish()ignores how the turn ended. TheRCALL_MAXtest passes unchanged: the reasoning before an opener still streams, and only the block itself and the whitespace after it are held. The serve suite passes (423).The counter:
GET /metricstotals getreasoning_calls_deliveredandreasoning_calls_kept_as_text(complete calls to a declared tool that stayed text).@47Hunter47 commented on GitHub (Oct 6, 2026):
Closing our side of this: #1068 is what we now run.
We applied it on this machine (cherry-pick of
72ff2d65onto v0.1.40, so the tree is byte-identical to the PR head) and re-ran both corpora against it, then the live server:stream_toolsoff).serve/on the PR head: 415 passed, 8 skipped.reasoning_calls_kept_as_text: 1inGET /metrics— which is the part that lets anyone see whether this class fires at all before deciding anything.Our own two-condition stopgap is dropped in its favour: it left 3 of #525's controls firing (a line-start block followed by prose), and #1068's second condition closes exactly those.
The only shape the three conditions do not cover, confirmed with a minimal pair on the PR: an opener inside a fence that never closes, with the turn ending right after the block. Nothing in our live data sits in a fence (all 12 are
\n\n<tool_call>\n<function=NAME>with the span ending on the block), so that is a defensive gap rather than an observed one — noted on #1068, not a request here.@Niko1221 commented on GitHub (Oct 6, 2026):
Fixed in 0.1.40.1, thank you for the corpus and the live runs. The rescue now needs a line-start opener outside code fences and inline code, a closed call with nothing but whitespace or more calls after it, and a normal end of the turn; a reply cut by max tokens keeps the call as reasoning text. Both gates you suggested are in, plus the counter (totals.tool_calls_from_reasoning) and a log line. A call quoted in a code fence or inline code in the visible answer (after ) is now text too, so an example in an answer never runs.
@47Hunter47 commented on GitHub (Oct 6, 2026):
Verified on 0.1.40.1 (
82f46a8c) before switching to it — offline first, then live:serve/fixtures/rcall_specimens.json, 37/37 entries match their expectations, the 16 that expect a call get it, and every feeding mode agrees (whole / 1-char / random 1-9).live-06, the mixed tail: this delivers the declaredtodo_listblock where our older expectation (inherited from #525's all-or-nothing tail contract) said nothing. Your entry for the same shape expects the declared call, so per-block is the consistent reading — our expectation was the stale one, not your behaviour.serve/on this head: 444 passed, 8 skipped.max_tokenswith ~18k characters of thinking still returns no call; ordinary tool calls and streaming are untouched.totals.tool_calls_from_reasoning, created on the first rescue — it reads 0 until one actually fires) and the cut case logs the max-tokens line.One small operational note:
finish()takingreason: str | Noneis friendlier than the boolean a half-way PR used, because a harness that drives the parser itself can still tell a cancel from a length cut.We switched to 0.1.40.1 and dropped the two-condition patch we had been carrying. The fence/inline-code side is the part we could not have written as confidently — our own gate only handled fences, and none of our 12 live cases sat in one, so it had no evidence behind it. Good to have it in for real.