Skip to content

Keep a Qwen3.5, QwQ or Cohere2 MoE forced-open thought as llama-server parses it - #1524

Merged
leehack merged 5 commits into
mainfrom
fix/750-reasoning-parity-families
Oct 28, 2026
Merged

leehack merged 5 commits into
mainfrom
fix/750-reasoning-parity-families

Conversation

@leehack

@leehack leehack commented Oct 28, 2026 •

Copy link
Copy Markdown
Owner

Summary

  • When the prompt opens a thought (Qwen3.5 / StepFun Qwen3-Coder XML, QwQ / Bielik / Reka / Spark / Ling Hermes-style, Cohere2 MoE), the parse and stream now take the reasoning as the text before the first end tag, as llama.cpp 620d2225 does: a repeated <think> and an escaped \n stay as generated, and thinking tags after the first end tag stay in the content instead of being parsed as more reasoning. This extends the DeepSeek V3.1 rule from #1512 to these formats through the same keepsForcedThoughtVerbatim trait.
  • A Qwen3.5 <tool_call> before </think> ends the thought; a Cohere2 MoE thought ends at <|START_TEXT|> or <|START_ACTION|> when one comes before <|END_THINKING|> (#1523). New trait forcedThoughtContentOpenings.
  • A thought that never ends drops a trailing partial end tag or opening (Plan it </thi -> Plan it), in parse and stream, for every verbatim format including DeepSeek V3.1.
  • Part of #750 (items 1, 3 and the Cohere half of 4).

Production-readiness scope

  • User-facing scope: engine.create / ChatSession reasoning and content for the formats above when the template opens the thought (thinking on). Stream chunks and the final message agree.
  • Supported platforms/paths: all; Dart-side template parsing shared by native and web.
  • Unsupported or intentionally unavailable paths: N/A, parse-only change. Recorded divergences stay as listed on #750: no-reasoning templates (Hermes-3, Qwen2.5, Granite) still extract <think>; a thought that does not start forced-open (Qwen3 with a leading <think>) keeps the existing extraction.
  • Out of scope / follow-ups: thinking-off <think> as content is #1525; Cohere tests/docs follow-ups are #1526. Remaining divergences are recorded on #750.

Completeness checklist

  • Declared scope is fully implemented, or explicitly reduced above.
  • Unsupported platform/option combinations fail loudly with actionable diagnostics or are clearly documented as unavailable. (N/A: no new option.)
  • Public API docs, README/website docs, examples, support matrices, and changelog entries are updated where relevant. (template-engine-internals.md, chat-template-and-parsing.md, both changelogs. README/examples/support matrix: N/A, no capability change.)
  • Regression coverage covers the original issue plus key negative/version-skew paths where relevant.
  • Public capability changes update the validation coverage registry with a pack or a reasoned, issue-linked exemption; inventory checks pass (or N/A with scope). N/A: parse parity fix, no public capability change.
  • Security/privacy review completed: no secrets, bearer tokens, signed URLs, or raw secret-bearing paths leak through logs, cache keys, metadata, errors, or snapshots.
  • Follow-up work that is useful but not required for this PR is tracked in GitHub Issues, or explicitly marked "None" above.

PR type guidance

  • Bugfix PR: root cause: Hermes, Qwen3-Coder XML and Cohere2 MoE parsed a forced-open thought with the generic extractThinking (drops repeated start tags, unescapes \n, recurses into later pairs) and the stream split only at the end tag, so no content opening ended the thought and a held partial tag was flushed into reasoning at EOF. Tests: upstream oracle fixture test/fixtures/forced_open_thought_upstream.json (new Qwen3.5, QwQ and Cohere2 MoE entries, a DeepSeek partial-tag case) through parse and whole/bytewise stream, plus thinking_utils_test.dart. Platforms: all (Dart parse).
  • Feature PR / Docs-only PR: N/A.

Test Plan

  • dart run tool/prepare_workspace.dart
  • dart format --output=none --set-exit-if-changed .
  • dart analyze
  • dart test -p vm -j 1 --exclude-tags local-only (+9213 ~5, all passed at 7bbcb4848)
  • dart test -p chrome --exclude-tags local-only (CI)
  • Other targeted/local-only validation: ./tool/testing/run_template_parity_suites.sh (all suites passed); upstream oracle, sweep, render matrix and mutation runs below.

Evidence (harnesses from #1494 / #1512, oracle built from llama.cpp 620d2225):

Check Base (22a0c4518) Head
Failing-first: new fixture/unit tests 19 fail all pass
Oracle, 640 outputs over Qwen3.5 x2, QwQ, Bielik, Reka, Spark, Ling, StepFun, DeepSeek V3.1 443 mismatch 264 mismatch; every change BAD->OK
Oracle, Cohere2 MoE (24) 19 mismatch 6 mismatch; every change BAD->OK
Parse/stream sweep, 94 templates x 14,504 cases changes only in forced-open thinking-on cases of hermes, qwen3CoderXml, deepseekV3, cohere2Moe; stream != parse for those formats 0 (base 345)
Render matrix, 15,792 renders 0 changed

Remaining oracle mismatches are upstream parse errors (text before a Cohere marker, trailing text after a call), tool-format differences and the recorded divergences; none come from this change.

Mutants (all killed): parse not verbatim per handler (Hermes, Qwen3-Coder XML, Cohere2 MoE, R7B openings ignored), registry traits off (hermes, qwen3CoderXml, cohere2Moe verbatim; cohere2Moe and qwen3CoderXml stream openings), content openings ignored / consumed / winning after the end tag in parse and stream, no partial-tag drop in parse, stream EOF keeps held prefix, stream does not hold an opening prefix, withoutTrailingTagPrefix keeps the shortest match or stops at the first tag, each Cohere opening dropped, XML verbatim branch skipped. 22 of 22.

Matrix Evidence

Matrix row Scope covered Platform / model / backend Result Evidence / notes
static-format-analyze format, analyzer, web/native import boundaries macOS, Flutter 3.47.1 PASS local + CI
root-vm native-safe unit/integration coverage on the Dart VM macOS VM PASS +9213 ~5, all passed at 7bbcb4848
root-chrome browser-compatible unit/integration coverage Chrome CI CI run on this head
coverage-lib >=70% line coverage VM CI CI run on this head
structured-output-adversarial partial/final streaming and rollback, thinking prefixes with tool choice, pinned upstream template/parser parity Dart parse vs llama.cpp 620d2225 PASS oracle, sweep (whole/bytewise/3-char chunks, auto/required/none, parallel), render matrix, parity suites above
high-risk-exact-head-independent-qa independent blocking review of exact head fresh auditor session, exact head b3f443a PASS CLEAR: audit CLEAR at 7bbcb48; merge of main verified by delta-b3f443a91.md; evidence /opt/UnitySrc/personal/llama/llamadart-evidence/pr-1524/
docs-site website build CI docs-only edits to two guides
gguf-chat-features-smoke real GGUF chat/thinking N/A N/A parse-only; behaviour pinned against upstream emissions, see below

Review Notes

  • Independent review status: CLEAR at exact head b3f443a by a fresh auditor session (0 P1, 0 P2).
  • CI status / head SHA: b3f443a91 (audited 7bbcb4848 + merge of main 16d4e72f3; delta check delta-b3f443a91.md), CI pending.

High-risk regression review

  • Classification: high-risk (structuredOutput, backendRuntime)
  • Implementation task: #750 lane of #922
  • Independent blocking QA task: fresh auditor session, CLEAR, 0 known PR-caused P1, 0 unresolved threads
  • Exact head / current base: b3f443a / 16d4e72 (merge-tree clean)
  • Production-branch deletion, bypass, or miswire proof: the mutants above remove each production branch (handler verbatim calls, registry traits, stream openings and EOF drop) and each is killed by the fixture or unit tests.
  • Affected-family real-model/artifact evidence: model weights not run; the fixture holds primary upstream emissions from llama.cpp 620d2225 (common_chat_templates_apply + common_chat_peg_parse, whole and bytewise) for the affected templates in test/fixtures/llama_cpp_templates/.
  • Explicit unavailable-family or other N/A evidence: Qwen3.5, QwQ, Cohere2 MoE and DeepSeek V3.1 GGUF weights not used; durable upstream fixtures instead.
  • Known PR-caused P1 regressions: 0
  • Unresolved review threads: 0

@leehack
leehack marked this pull request as ready for review October 28, 2026 11:56
@leehack
leehack merged commit c91b352 into main Oct 28, 2026
39 checks passed
@leehack
leehack deleted the fix/750-reasoning-parity-families branch October 28, 2026 11:56
@github-actions

Copy link
Copy Markdown
Contributor

Chat app preview removed for leehack/llamadart-chat-pr-1524.

leehack added a commit that referenced this pull request Oct 28, 2026
# Conflicts:
#	lib/src/core/template/chat_format_registry.dart
#	lib/src/core/template/handlers/hermes_handler.dart
#	lib/src/core/template/handlers/qwen3_coder_xml_handler.dart
#	lib/src/core/template/xml_tool_call_format.dart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant