diff --git a/src/heretic/main.py b/src/heretic/main.py index dd5e521..fdc3dd6 100644 --- a/src/heretic/main.py +++ b/src/heretic/main.py @@ -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) diff --git a/tests/test_cot_prefix_extension.py b/tests/test_cot_prefix_extension.py new file mode 100644 index 0000000..74d0ba3 --- /dev/null +++ b/tests/test_cot_prefix_extension.py @@ -0,0 +1,46 @@ +# SPDX-License-Identifier: AGPL-3.0-or-later +# Copyright (C) 2025-2026 Philipp Emanuel Weidmann + 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 boundary necessarily + # contains non-whitespace characters, so it must be rejected as well. + responses = [ + "\nOkay, let me think. \n\nHere is the answer about cats", + "\nOkay, let me think. \n\nHere is the answer about dogs", + ] + self.assertIsNone(get_cot_prefix_extension(responses)) + + +if __name__ == "__main__": + unittest.main()