fix: keep CoT-skip recheck from appending reasoning text to the response prefix (#487)

The 'Rechecking with prefix' step unconditionally appended the common
prefix of the recheck responses. For models that generate their own
Chain-of-Thought (e.g. Qwen3), the model re-enters reasoning after the
closed block and starts it near-identically on every prompt, so the
common prefix is the beginning of the reasoning text. Appending it
pinned such models in CoT mode while writing their final answer,
collapsing baseline refusals, degenerating the ablation direction, and
destroying the model (KL 16.7-17.9, see #487).

Only extend the closed CoT block with whitespace-only common prefixes
(the missing-trailing-newline case this step exists for); ignore and
log non-whitespace common prefixes. Template-injected CoT models are
unaffected: their whitespace extensions still apply.
This commit is contained in:
akushonkamen 2026-10-17 13:28:38 +08:00
parent 66a952b49b
commit 36edf8ffc2
2 changed files with 79 additions and 1 deletions

View File

@ -192,6 +192,29 @@ def obtain_export_strategy(
)
def get_cot_prefix_extension(responses: list[str]) -> str | None:
"""
Determine the extension for a closed Chain-of-Thought response prefix,
based on responses generated with the closed prefix prepended.
Returns the common prefix of the responses if it consists entirely of
whitespace (the case this check exists for: a prefix missing a trailing
newline or similar), and None otherwise.
Models that generate their own Chain-of-Thought (e.g. Qwen3) start their
reasoning near-identically for every prompt, so the common prefix of the
responses is then the beginning of the reasoning text. Appending it would
put reasoning back into the response prefix and keep the model in CoT
mode while writing what Heretic treats as the final answer.
"""
additional_prefix = commonprefix(responses).rstrip(" ")
if additional_prefix.strip():
return None
return additional_prefix
def run():
# Modified "Pagga" font from https://budavariam.github.io/asciiart-text/
print(f"[cyan]█░█░█▀▀░█▀▄░█▀▀░▀█▀░█░█▀▀[/] v{version('heretic-llm')}")
@ -564,12 +587,21 @@ def run():
# is actually complete (e.g. not missing a trailing newline).
print("* Rechecking with prefix...")
responses = model.get_responses_batched(response_prefix_test_prompts)
additional_prefix = commonprefix(responses).rstrip(" ")
additional_prefix = get_cot_prefix_extension(responses)
if additional_prefix:
settings.response_prefix += additional_prefix
print(
f"* Extended prefix found: [bold]{escape(repr(settings.response_prefix))}[/]"
)
else:
ignored_prefix = commonprefix(responses).rstrip(" ")
if ignored_prefix:
print(
"* Ignoring non-whitespace common prefix "
f"[bold]{escape(repr(ignored_prefix))}[/] to avoid "
"keeping the model in Chain-of-Thought mode"
)
evaluator = Evaluator(settings, model)

View File

@ -0,0 +1,46 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
# Copyright (C) 2025-2026 Philipp Emanuel Weidmann <pew@worldwidemann.com> + contributors
import unittest
from heretic.main import get_cot_prefix_extension
class CotPrefixExtensionTests(unittest.TestCase):
def test_extends_closed_block_with_missing_whitespace(self) -> None:
# Template-injected CoT (e.g. LiquidAI LFM): responses continue right
# after the closed block with a common newline that completes the prefix.
# This is the case the recheck exists for, and it must keep working.
self.assertEqual(
get_cot_prefix_extension(["\n\nSure, here is", "\n\nOf course"]),
"\n\n",
)
def test_returns_empty_string_for_fully_diverging_responses(self) -> None:
# Nothing is common, so nothing may be appended.
self.assertEqual(get_cot_prefix_extension(["Yes.", "No."]), "")
def test_rejects_reasoning_text_common_prefix(self) -> None:
# Qwen3-style self-generated CoT (issue #487): after the closed block
# the model re-enters reasoning, and the reasoning beginnings are
# near-identical across prompts. Appending them would keep the model
# in CoT mode while writing its final answer.
responses = [
f"\nOkay, the user asks about {topic}. Let me think this through."
for topic in ("cats", "dogs", "cars")
]
# The reasoning continuation must not become part of the prefix.
self.assertIsNone(get_cot_prefix_extension(responses))
def test_rejects_common_prefix_containing_cot_boundary(self) -> None:
# A common prefix that reaches across a </think> boundary necessarily
# contains non-whitespace characters, so it must be rejected as well.
responses = [
"\nOkay, let me think. </think>\n\nHere is the answer about cats",
"\nOkay, let me think. </think>\n\nHere is the answer about dogs",
]
self.assertIsNone(get_cot_prefix_extension(responses))
if __name__ == "__main__":
unittest.main()