Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 17 additions & 4 deletions skillopt_sleep/judges.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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 = (
Expand Down
24 changes: 24 additions & 0 deletions tests/test_judges.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down