Skip to content

serve: a think or Qwen call tag is markup only where its special token was generated, so the same text in ordinary tokens stays content - #1020

Merged
juliankang4 merged 10 commits into
midagedev:mainfrom
juliankang4:dgx-spark-q38-fix-q38-control-markers
Oct 30, 2026
Merged

juliankang4 merged 10 commits into
midagedev:mainfrom
juliankang4:dgx-spark-q38-fix-q38-control-markers

Conversation

@juliankang4

Copy link
Copy Markdown
Collaborator

Qwen3.8 출력에서 think와 tool_call 태그를 그 special 토큰이 생성된 자리에서만 경계로 읽고, 일반 토큰으로 쓴 같은 글자는 content로 두는 수정입니다.

What and why

Qwen3.8 can write <think>, </think> and <tool_call> as ordinary text tokens, for example when it quotes or copies them. The parsers matched those characters in the decoded text, so ordinary text was read as markup: a quoted </think> closed the reasoning early, and a copied <tool_call> XML example became a real tool call.

The generation loop now records where each special token's text starts. The think split and the Qwen call scan treat a tag as markup only at those offsets. A model whose vocabulary does not hold a tag as one special token keeps the previous text match. Chat, responses and messages hand the offsets to the parser in both whole and stream replies. Raw completions are unchanged.

Release notes: Fixed: Qwen3.8 replies that quote or copy </think> or <tool_call> as ordinary text are no longer cut into reasoning or turned into tool calls.

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.
  • 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": crates/serve/src/qwenxml.rs:373 and crates/serve/src/glmxml.rs:321 both keep a three-line push wrapper over their own push_stream and error type. Both existed before; this change only hands &[] specials to the Qwen one. Merging them needs a shared trait outside this fix.

What I ran

Card runs used base 9540dc3f and head 65cd45a3; the PR head is 2102e9e6c. 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 --host 127.0.0.1 --port 18739 --parallel 1 --ctx-size 4096 --cache-ram 0 -ub 256 --no-mmproj on the Qwen3.8 FP4-Local checkpoint, and each request ran as a whole reply and as a stream. Both used temperature 0, seed 7, reasoning_format deepseek.

Request 1 (enable_thinking false, a report_answer tool, tool_choice auto) asks the model to copy this XML as plain text:

<tool_call>
<function=report_answer>
<parameter=answer>
410
</parameter>
</function>
</tool_call>

Request 2 (enable_thinking true) asks the model to first copy the literal text </think> as a quoted string inside its reasoning, then explain why 2+2=4, and finally answer OK.

  • Request 1, whole and stream: base finished with tool_calls, 0 content chars and the call report_answer({"answer":410}); head finished with stop, 97 content chars (the XML) and no tool call.
  • Request 2, whole and stream: base gave 87 reasoning chars and 874 content chars; head gave 958 reasoning chars and content OK.

Base, request 1: "finish": "tool_calls", "reasoning_chars": 0, "content_chars": 0, "tool_calls": [["report_answer", "{\"answer\":410}"]].

Head, request 1: "finish": "stop", "reasoning_chars": 0, "content_chars": 97, "tool_calls": [].

Base, request 2: "finish": "stop", "reasoning_chars": 87, "content_chars": 874.

Head, request 2: "finish": "stop", "reasoning_chars": 958, "content_chars": 2.

Both servers exited with SERVER_EXIT base 0 and SERVER_EXIT head 0.

Unit test constrain::tests::a_tag_spelled_in_ordinary_ids_is_text drives the real generation loop with a scripted engine that emits ordinary and special ids. It checks that whole and stream replies give the same message, that a copied call stays content, and that a think budget of 20 ignores an ordinary </think> and forces the special one. With the special-token check off it failed with Qwen(NoFunction { .. "</think>\n\n<tool_call>..." }); with it on it passes.

Host gate on 65cd45a3:

just gate-serve

12 test binaries ran, 683 tests passed, 0 failed, 0 ignored. The bloomery-serve lib reported test result: ok. 465 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out. After the last commit cargo test -p bloomery-serve --lib reported 466 passed.

Hardware (if the change is for a machine)

One GB10, driver 595.84, CUDA compilation tools 13.3, running the Qwen3.8 FP4-Local checkpoint.

AI help

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

…n was generated, so the same text in ordinary tokens stays content
Keep string and number endings from consuming the prefix of a forced special token across grammar frames. Cover whole and streamed required calls, and a model-ended reasoning span inside its budget.
Carry each special token's byte length beside its offset through the
generation events, the outcome and the parsers. A vocabulary may hold a
special token that spells only the start of a tag; such a token followed by
ordinary pieces that complete the tag's text no longer opens or closes a
think span or a tool call.
@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 merge-blocking defect found in special-token tracking, reasoning boundaries or tool-call parsing.

Non-blocking

⚠ Supply dispatch timing evidence: crates/serve/src/genloop.rs:2598 decodes each emitted special token again. With Vocab, this adds a byte buffer and an owned String per nonempty special token; sending also allocates metadata for each event carrying one. The body supplies correctness results but no timing comparison. Attach the same-lease base/head A/B required by AGENTS.md's Measuring contract.

Checked

Duplicates: The existing Qwen/GLM push wrappers delegate to separate scanners. Sharing these short wrappers would add an abstraction.
Complexity: Gen::pump adds a special-offset branch to token emission.
Silent failure: SpecialTags::find preserves text matching for vocabularies without a special marker.
Tests: Static mutation analysis, not execution. Bypass the special checks at crates/serve/src/reasoning.rs:57 and line 66: the ordinary-id and ordinary-close assertions fail.
Tests: Drop the guard at crates/serve/src/constrain/program.rs:1119: constrained_calls_keep_special_markers_whole fails its closing-token assertion.
Tests: Remove the upper bound at crates/serve/src/genloop.rs:2254: a_held_special_is_reported_only_when_its_text_is_sent fails its event-offset assertion.
Gates: No tolerance relaxed or test removed; model-tax limits decrease. The body supplies FAIL-first evidence for ordinary-token tag handling.
Escapes: No C1–C4 pattern matches. The diagnostic uses a quoted heredoc and propagates Python failures. Shell and embedded Python syntax checks pass.

Notes

Rust tests and hardware runs were not executed in this review. The body lacks the required nvidia-smi output and exact model file path.

🔨 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 989d010 into midagedev:main Oct 30, 2026
8 of 9 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