Repository navigation
serve: reasoning_loop ends a think span that repeats a cycle, with finish_reason reasoning_loop - #1009
Conversation
There was a problem hiding this comment.
Verdict: MUST_FIX: Text completions can stop inside a Qwen tool call.
Must fix
1. Apply the Qwen span boundary on text completions
crates/serve/src/api.rs:4513 leaves think_split unset, so genloop.rs:2024 falls back to a splitter that only recognizes think tags. On a Qwen server, send /completion or /v1/completions a rendered tool prompt ending in <think> with reasoning_loop: true. The added gate's byte-tokenized script (r<tool_call> followed by a save_note call with 300 repeated a ids) remains inside the tracked span, so generation stops inside the argument with reasoning_loop. A budget of 12 also forces </think> immediately after the call opener.
Cost: Fix the shared completion planner before the new field ships with different span boundaries by route.
Fix: Reuse the parser's Qwen boundary in the completion planner. Extend the Qwen regression to both text-completion routes, asserting the complete script survives with the guard, budget, and both enabled.
fn completion_plan(state: &State, b: &Map<String, Value>) -> Result<CompletionPlan, ApiError> {
let mut p = gen_params(state, b)?;
let ids = completion_prompt(state, b.get("prompt"))?;
if p.reasoning_budget.is_some() || p.reasoning_loop {
let text = match b.get("prompt") {
Some(Value::String(s)) => s.clone(),
_ => state.tok.decode(&ids),
};
gate_think_span(&mut p, &text);
if let ToolFormat::QwenXml(framing) = state.tool_format {
p.think_split = ChatParser::with_tags(
&text,
ReasoningFormat::Deepseek,
Some(Tools::QwenXml(framing, ParamKinds::default())),
state.think,
)
.think;
}
}
Ok(CompletionPlan {
p,
input: Prompt::from(ids),
prompt: b.get("prompt").cloned().unwrap_or(Value::Null),
return_tokens: get_b(b, "return_tokens")?.unwrap_or(false),
})
}Checked
Duplicates: dryer's genloop.rs:316 / swap.rs:227 pair maps different enums and only shares structure.
Complexity: gen_params 43→44 adds the field read; Gen::pump 31→32 adds the loop-stop branch.
Silent failure: invalid booleans return 400; absent budgets and guards stay unrestricted/off. The unset splitter reaches the fallback in item 1.
Tests: the Qwen regression checks complete arguments with the budget, guard and both enabled, but calls only /v1/chat/completions. It does not protect the completion planner's missing handoff. Detector assertions cover cycle thresholds and the period limit.
Gates: no existing pins, tolerances or cases moved.
Escape catalog: C1–C4 do not match this diff.
Notes
Validation was source inspection; Rust gates were not run.
🔨 Shingler, bloomery's review bot: approves a clean pull request and requests changes on a must-fix (how it works)
…nish_reason reasoning_loop A request with reasoning_loop: true has its open think span read for a loop: for each cycle length up to 1,024 ids, the run of the span's last ids that each equal the id one cycle before. A run of 256 ids and one whole cycle ends the generation with the reasoning so far: finish_reason reasoning_loop on the OpenAI routes, stop_reason on /v1/messages, stop_type on /completion, and status incomplete with incomplete_details.reason reasoning_loop on /v1/responses. Text after the span closes is not read, the field is ignored where the template closed the span, as reasoning_budget is, and a value that is not a boolean is a 400 naming it. Off by default; a request without it runs as before. The guard and the think budget read the span with the chat parser's own splitter, so a Qwen3-Coder call that ends the span without </think> ends it for them too: the budget no longer forces a close into the call's arguments.
3c33377 to
adbd169
Compare
There was a problem hiding this comment.
Since the last review: must-fix 1 is fixed at crates/serve/src/api.rs:4521. Both text-completion routes now use the parser's Qwen span boundary. The expanded regression test checks complete tool calls with the budget, guard and both enabled, with and without drafting.
Verdict: CLEAN: The previous failure is fixed, and the interdiff introduces no remaining must-fix defect.
Checked
Duplicates: 0 new named definitions and 2 new test closures checked; no duplicate owner. Dryer's genloop.rs:316 / swap.rs:227 pair maps different enums and only shares structure.
Complexity: crapper's gen_params 43→44 adds a field read; Gen::pump 31→32 adds a loop-stop branch. Neither function changed in this interdiff.
Silent failure: 1 optional splitter handoff followed. Qwen constructs a splitter, and call content retires the tracker before argument tokens consume the budget or trigger the guard.
Tests: mutation checked by inspection. Removing the splitter assignment makes the complete-script assertions fail with the budget and the stop-reason assertions fail with the guard on both text routes. Draft resumption is also asserted.
Gates: no existing threshold, tolerance, pin or case changed. The expanded gate remains hw_-prefixed and ignored.
Adversarial checks: partial call tags, model-opened spans, closed prompts and drafted tokens follow the existing splitter and tracker guards. A repeating span without a call still reaches the loop detector.
Performance: the interdiff adds request planning work; no per-token launch, copy, allocation or synchronization is added.
Escape catalog: C1–C4 do not match.
Notes
Validation was source inspection. Rust tests were not run during this review.
🔨 Shingler, bloomery's review bot: approves a clean pull request and requests changes on a must-fix (how it works)
|
The review is done and the contribution check passed, so I'm merging this. |
요청에
reasoning_loop: true를 주면 생각 구간이 같은 순환을 반복할 때 생성을 끝내고, think 예산이 Qwen3-Coder 도구 호출 안에서 닫힘을 강제하던 문제도 함께 고칩니다.What and why
A model that falls into a loop inside its think span spends the whole token limit repeating itself and never answers. A request with
reasoning_loop: truenow ends the generation as soon as the open think span repeats a cycle: for a cycle of up to 1,024 ids, each of the span's last ids equals the id one cycle before it, over at least 256 ids and one whole cycle. The generation ends with the reasoning so far and no answer. The field is off by default, is ignored where the template already closed the span (asreasoning_budgetis), and a value that is not a boolean is a 400 naming the field.The reason is
finish_reasonreasoning_loopon/v1/chat/completionsand/v1/completions,stop_reasonon/v1/messages,stop_typeon/completion, andstatusincompletewithincomplete_details.reasonreasoning_loopon/v1/responses.docs/serving.mddescribes the field.The guard and
reasoning_budgetread the span with the chat parser's own splitter, on the chat routes and on/completionand/v1/completions. A Qwen3-Coder<tool_call>ends the span without</think>, so the span now counts as closed there and the budget no longer forces a</think>into the call's arguments.Release notes: Added:
reasoning_looprequest field that ends a think span stuck in a repeating cycle withfinish_reasonreasoning_loop;reasoning_budgetno longer cuts a Qwen3-Coder tool call.Code shape
genloop.rsbeside the budget's tracker; the five routes read oneStopKind::Loopmapping throughStopKind::finish_reasonandcause_word.completion_plannow also asks on a Qwen3-Coder server.reasoning_loopis a 400 naming the field.cargo fmtandcargo clippyare clean on what I touched; the lint count did not go up.Duplication found or removed (
path:line), or "none": none.What I ran
On the DGX Spark, the real
bloomery-servebuilt withcargo oxide buildatdf1e2a540ca63d2f9cb04c07d46dea0ad8608641, serving the Qwen3.8 FP4-Local checkpoint withBLOOMERY_DRAFT=mtp,--parallel 1 --ctx-size 16896. A logit bias of +100 on one id (13784, "Wait") makes every greedy pick that id, so the span repeats a one-id cycle, and the guard fires at 257 ids. Output of the client checks:The server's finish lines for the looping requests end
stop reasoning_loop, for examplegenerated 257 ids in 3.76 s ... stop reasoning_loop; ctx 320/16896.The Qwen tool-call boundary and the guard's unit tests ran on an x86_64 Linux host through the repository's gate script:
With the parser-split line removed from
genloop.rs, the Qwen test fails (HTTP 500,tool call 0 opens no function, and a forced</think>in the middle of the call):The cycle limit test,
bash tools/gate.sh -p bloomery-serve --lib -- genloop::think_tests::a_loop_holds_two_copies_of_its_cycle, pins the cycle limit at 1,024 ids:The text completion routes use the same boundary.
crates/serve/tests/qwenxml.rsruns every budget and guard case on/v1/chat/completions,/completionand/v1/completionsand asserts the whole tool-call script comes back withstop_typeeosorfinish_reasonstop. With the splitter line removed, the budget case fails and the guard case stops inside the call's argument:With the fix:
Hardware (if the change is for a machine)
AI help
AI 코딩 에이전트가 코드를 작성한 후 설명을 썼고, 테스트를 마쳤습니다. diff 리뷰와 PR 초안 리뷰는 제가 직접 했습니다.