Skip to content

serve: a forced tool call follows the model's own close of an open think span - #1023

Merged
juliankang4 merged 1 commit into
midagedev:mainfrom
juliankang4:dgx-spark-q38-fix-q38-thinking-tool-choice
Oct 30, 2026
Merged

juliankang4 merged 1 commit into
midagedev:mainfrom
juliankang4:dgx-spark-q38-fix-q38-thinking-tool-choice

Conversation

@juliankang4

Copy link
Copy Markdown
Collaborator

thinking이 켜진 요청에서 tool_choice가 도구 호출을 강제할 때, 모델이 reasoning을 끝낸 뒤에 호출이 시작되도록 고친 수정입니다.

What and why

With thinking on and tool_choice set to required or a named function, the prompt leaves the think span open. The server closed that span at once and started the forced call, so the model never reasoned before it chose the arguments. On Qwen3.8 a simple arithmetic request came back with a well-formed call and a wrong answer ({"answer":400} for 17 x 23 + 19, where 410 is right).

A forced call now lets the model reason freely until it writes the span's close itself, and the call's program starts after that close. Only the close tag ends the span in that case, on chat, responses and messages, whole and stream. Raw /completion and /v1/completions read an open span in the prompt the same way. Text the model writes after its close must be the parser's ASCII whitespace; anything else, including a UTF-8 byte the decoder still holds at the close, is refused with the existing error before the close is emitted. A finite reasoning_budget beside a forced call is a 400, since the budget would compete with the call for the span's end. docs/serving.md describes the behaviour.

Release notes: Fixed: a forced tool_choice with thinking on now lets the model finish reasoning before the call, instead of closing the think span at once.

Code shape

The review reads these first. A box left unticked needs one line of why.

  • No second copy. Logic two models (or two call sites) share has one common owner; I lifted it there instead of
    copying it (AGENTS.md "Common code first", docs/rust-quality.md R14). Where I found an existing copy, I
    named it below.
  • Model code holds only architecture facts. A model's own files carry only what its architecture forces
    (tensor shapes, the attention kind, the quant). A common owner gained no per-model branch, flag or trait method.
  • Dead code is gone. Nothing I made unused is left behind (functions, flags, gate clauses, docs).
  • No silent failure. Undefined input is a named error or panic, never a defined-looking output.
  • Comments state what is true now. No dates, issue numbers or how a bug was found (tools/check-comments.sh).
  • No relaxed gate. No band, tolerance or lint moved to make a check pass; a re-pin carries PIN(YYYY-MM-DD):
    and its reason.
  • cargo fmt and cargo clippy are clean on what I touched; the lint count did not go up.

Duplication found or removed (path:line), or "none": none. The close-boundary check lives in Gen::spend (crates/serve/src/genloop.rs) and reuses the parser's reasoning::is_space and the existing StepError::NoAllowed.

What I ran

Card run on a GB10 with the Qwen3.8 FP4-Local checkpoint, base 8a59241b and head c2624dab. Both binaries come from cargo oxide build --arch sm_121a -- -p bloomery-gpu-gates --features glm5next,clef,vision,mimo2,solar2,minimaxm3,flint,gemma-embedding2,hf,checkpoint --release --bin bloomery-serve. Each ran as:

BLOOMERY_DRAFT=off bloomery-serve --model qwen38 --host 127.0.0.1 --port 18739 --parallel 1 --ctx-size 4096 --cache-ram 0 -ub 256 --no-mmproj

The request asks for a x b + c with one report_answer tool ({"answer": integer}), enable_thinking true, temperature 0, seed 7, max_tokens 512, reasoning_format deepseek. It ran for 12 arithmetic prompts, each with tool_choice auto, required and a named function, as a whole reply and as a stream. A reply counts as correct when the call is report_answer and its answer equals a x b + c.

Metric (correct answers) Base Head
auto, whole 12/12 12/12
auto, stream 12/12 12/12
required, whole 10/12 12/12
required, stream 10/12 12/12
named, whole 10/12 12/12
named, stream 10/12 12/12

Base, required, 17 x 23 + 19: the first generated token is </think>, reasoning_chars is 0 and the call is {"answer":400}. Head: the model reasons ("17*23=391, +19=410") and the call is {"answer":410}. No request on either side returned an HTTP error, and both servers exited 0 (SERVER_EXIT base 0, SERVER_EXIT head 0). The run verdict is PASS.

Close-boundary unit tests (CPU), at the tested head:

cargo test -p bloomery-serve --lib a_merged_close_writes_nothing_outside_the_held_program -- --nocapture
test result: ok. 1 passed; 0 failed; 471 filtered out
GREEN merged-close boundary: 78 cases
cargo test -p bloomery-serve --test qwenxml --test hermes a_forced_call -- --include-ignored --nocapture
test hw_a_forced_call_is_served_on_every_template ... ok
test hw_a_forced_call_follows_the_models_own_close ... ok

The same boundary test cases, run against the previous genloop.rs, return ab<|think:end|>\u{a0} and ab<|think:end|> followed by U+FFFD as successful replies. They are rejected with the fix.

Full serve suite:

cargo test -p bloomery-serve --lib --test serve --test dsml --test glmxml --test hermes --test qwenxml --test callstream --test limits --test anthropic --test penalty --test sampdraft --test contract -- --include-ignored

test result: ok for every target, 0 failed (lib 472, serve 111, qwenxml 22, anthropic 15, glmxml 14, dsml 13, hermes 13, sampdraft 12, callstream 6, penalty 6, contract 4, limits 3).

Hardware (if the change is for a machine)

Card Driver CUDA
GB10 595.84 13.3

AI help

AI 코딩 에이전트가 코드를 작성한 후 설명을 썼고, 테스트를 마쳤습니다. diff 리뷰와 PR 초안 리뷰는 제가 직접 했습니다.

…ink span

With thinking on, tool_choice "required" or a named function started its
program at the first generated id, which forced the span's close and left
the model no reasoning. The call's program is now held while the model
reasons in the span the prompt left open: the reasoning is drawn as free
text (the request's sampling and logit_bias), and the program starts on
the id after the model's own close, which the engine is fed once. Only the
close tag ends that span, in the generation and in the chat parser alike,
so a call tag inside the reasoning stays reasoning. A model that ends its
generation inside the span is a named error, not a call, and so is a
close read by its text whose last token also writes text past it; that
text is refused before it goes out.

A JSON response_format still closes an open span at once, and a finite
reasoning_budget beside structured output is still a 400. The raw
completion routes read an open span in the prompt for a forced call too.
@github-actions github-actions Bot added documentation Improvements or additions to documentation serve The HTTP server: crates/serve tools Tools, recipes, CI and the toolchain: tools/, justfile, .github/, Cargo labels Oct 30, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: CLEAN: No reachable behavior defect or proven quality defect remains in the changed code.

Optimization headroom

  • E. Less work: crates/serve/src/genloop.rs:2395, the held reasoning phase disables speculative passes. For R reasoning IDs, reusing the existing pass path with verification at the close boundary could reduce R−1 steps to about (R−1)/a target passes, where a is accepted reasoning IDs per pass [derived].

Checked

Duplicates: 12 new functions/constants checked; none duplicate an existing owner. Dryer reported no candidate pairs.

Complexity: answer 14→19 adds held-phase bias decisions; spend 9→14 adds handoff validation; pump 33→34 adds an EOS refusal; schema::request 16→17 adds a small mode selection. The new merged-close test is 21, covering vocabulary setup, a case matrix and assertions.

Silent failure: 2 fallback sites followed. A missing splitter builds the equivalent tracker. Saturating the retained length handles a stop that starts in earlier text; the caller receives the stop result.

Tests (static mutation review; not executed): removing holding at genloop.rs:2092 fails the own-close unit and API assertions. Dropping bias at 2253 or the EOS refusal at 2649 fails the held-call test. Removing suffix or decoder-tail checks at 2318/2322 fails the merged-close matrix. Dropping named-tool filtering at schema.rs:837 fails the modified Hermes gate.

Gates: no tolerance, threshold or lint moved. The body supplies FAIL-first results for the close-boundary test.

Adversarial checks: special versus ordinary close tags, merged suffixes, incomplete UTF-8, stops spanning tokens, and token/context limits are guarded. Responses and messages reuse chat_plan. A prompt with a closed span still starts the program immediately.

Escape catalog: C1–C4 do not match the changed script; its Python heredoc is quoted.

Notes

GPU evidence is incomplete: the body has no nvidia-smi line or just gate-* commands with output. Reported runs use head c2624dab, rather than the reviewed head a3ae6010. The reported serve-suite results were not rerun during this read-only review.

🔨 Shingler, bloomery's review bot: approves a clean pull request and requests changes on a must-fix (how it works)

@juliankang4

Copy link
Copy Markdown
Collaborator Author

The review is done and the contribution check passed, so I'm merging this.

@juliankang4
juliankang4 merged commit 744e178 into midagedev:main Oct 30, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation serve The HTTP server: crates/serve tools Tools, recipes, CI and the toolchain: tools/, justfile, .github/, Cargo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant