diff --git a/pyproject.toml b/pyproject.toml index 3808b69e..e336b6de 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -31,6 +31,7 @@ dependencies = [ "azure-identity>=1.15.0", "azure-core>=1.30.0", "httpx>=0.27.0", + "markdown-it-py>=4.3.0", ] [project.optional-dependencies] diff --git a/requirements.txt b/requirements.txt index 2c5f13ba..79869383 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,6 +6,7 @@ openpyxl>=3.1.0 azure-identity>=1.15.0 azure-core>=1.30.0 httpx>=0.27.0 +markdown-it-py>=4.3.0 # ── Optional: ALFWorld benchmark ────────────────── # alfworld>=0.4.0 diff --git a/skillopt_sleep/judges.py b/skillopt_sleep/judges.py index acaf0f93..2812d060 100644 --- a/skillopt_sleep/judges.py +++ b/skillopt_sleep/judges.py @@ -39,6 +39,10 @@ import re from typing import Any, Dict, List, Tuple +from markdown_it import MarkdownIt + +_MARKDOWN_BLOCKS = MarkdownIt("commonmark").disable("inline") + def _section_present(response: str, name: str) -> bool: # a markdown heading line (#, ##, ...) or bold line that contains `name` @@ -58,13 +62,22 @@ def _section_contains(response: str, name: str) -> bool: Unlike the legacy ``section_present`` check, this opt-in operator permits text before and after the requested name, such as numbering, translations, or subtitles. Only ATX heading lines with one to six markers and up to three - leading spaces are inspected, so body text and other Markdown constructs do - not satisfy the check. ``casefold`` gives literal, Unicode-aware, - case-insensitive matching without interpreting regex metacharacters. + leading spaces outside fenced code blocks are inspected. ``casefold`` gives + literal, Unicode-aware, case-insensitive matching without interpreting + regex metacharacters. """ needle = name.casefold() heading = re.compile(r"(?m)^ {0,3}#{1,6}(?:[ \t]+|$)([^\n]*)$") - return any(needle in match.group(1).casefold() for match in heading.finditer(response or "")) + text = response or "" + if "```" in text or "~~~" in text: + lines_and_endings = re.split(r"(\r\n?|\n)", text) + for token in _MARKDOWN_BLOCKS.parse(text): + if token.type == "fence" and token.map: + start, end = token.map + for line in range(start, end): + lines_and_endings[2 * line] = "" + text = "".join(lines_and_endings) + return any(needle in match.group(1).casefold() for match in heading.finditer(text)) _REFUSAL_PREFIXES = ( diff --git a/tests/test_judges.py b/tests/test_judges.py index c11aa439..18fb38b5 100644 --- a/tests/test_judges.py +++ b/tests/test_judges.py @@ -95,6 +95,30 @@ def test_section_contains_is_literal_and_case_insensitive(self) -> None: 0.0, ) + def test_section_contains_ignores_headings_in_fenced_code(self) -> None: + responses = ( + "```markdown\n## Key Risks\n```", + " ~~~python\r\n### Key Risks\r\n ~~~~ \r\n", + "````markdown\n```\n~~~~\n```` still code\n## Key Risks\n````", + "```markdown\n## Key Risks", + "- Example\n ```markdown\n ## Key Risks\n ```", + ) + for response in responses: + with self.subTest(response=response): + self.assertEqual(self._score("section_contains", "Key Risks", response)[0], 0.0) + + def test_section_contains_reads_headings_outside_fenced_code(self) -> None: + responses = ( + "### Key Risks\n```markdown\nexample", + "````markdown\nexample\n````` \t\n### Key Risks", + "```invalid ` info\n### Key Risks", + " ```markdown\n### Key Risks", + "- Example\n ```markdown\n sample\n\n### Key Risks", + ) + for response in responses: + with self.subTest(response=response): + self.assertEqual(self._score("section_contains", "Key Risks", response)[0], 1.0) + def test_tool_called_via_marker(self) -> None: self.assertEqual(self._score("tool_called", "search", "TOOL_CALL: search")[0], 1.0) self.assertEqual(self._score("tool_called", "search", "nope", ["search"])[0], 1.0)