From 91ab8f759f5c2992ca506c36a5bc8a29d26c89c2 Mon Sep 17 00:00:00 2001 From: khushalsonawat Date: Wed, 11 Nov 2026 11:38:40 +0530 Subject: [PATCH 1/5] fix(harness): keep a block comment open across lines, and save no prompt from unconfirmed lines A model written inside a multi-line /* */ or block was accepted, because the scan closed the comment at the end of its first line. The open block is now carried across lines until its closer. When lines named twice don't hold a quote too short to stand for the prompt, no prompt is saved; the lines are kept only as unconfirmed, since the platform would otherwise show them as the agent's prompt. Refs TH-8953 Signed-off-by: khushalsonawat --- src/fi/alk/harness/model_citation.py | 75 ++++-- .../harness/skills/understand-agent/SKILL.md | 4 +- src/fi/alk/harness/tools.py | 23 +- tests/harness/test_contract_model.py | 243 +++++++++++++++++- 4 files changed, 299 insertions(+), 46 deletions(-) diff --git a/src/fi/alk/harness/model_citation.py b/src/fi/alk/harness/model_citation.py index fe274a647..2ef5ddf24 100644 --- a/src/fi/alk/harness/model_citation.py +++ b/src/fi/alk/harness/model_citation.py @@ -175,6 +175,10 @@ def _opens_a_string(line: str, at: int) -> bool: return line[start:at].lower() in _STRING_PREFIXES +# What closes a block comment left open at a line's end. +_COMMENT_CLOSERS = frozenset({"*/", "-->"}) + + def _line_scan( line: str, block: str | None, @@ -187,18 +191,25 @@ def _line_scan( ) -> tuple[int, list[tuple[int, int]], str | None]: """One pass over a line: where its comment starts (or its length), the stretches of it that are text (inside a triple-quoted string, or a block comment closed on the line), and the - triple quote still open at its end. ``block`` is the one open where the line starts. A line - is a comment when it is the `* ` middle of a block comment or, - in INI, starts with ;. Strings are passed over whole, so a marker or a triple quote inside one - doesn't count; an apostrophe opens a string only when another closes it on the line.""" + block still open at its end (a triple quote, or the */ or --> a comment ends at). ``block`` is + the one open where the line starts. A line is a comment when it is the `* ` middle of a + block comment or, in INI, starts with ;. Strings are passed over whole, so a marker or a + triple quote inside one doesn't count; an apostrophe opens a string only when another closes + it on the line.""" inside: list[tuple[int, int]] = [] at = 0 if block is not None: close = line.find(block) - if close < 0: + if block in _COMMENT_CLOSERS: + if close < 0: + return 0, inside, block + # From the line's start, so the stretch can never be a quoted value. + inside.append((0, close + len(block))) + elif close < 0: return len(line), [(0, len(line))], block - inside.append((0, close)) - at = close + 3 + else: + inside.append((0, close)) + at = close + len(block) else: stripped = line.lstrip() if (block_comments and stripped.startswith(("* ", "*\t"))) or ( @@ -227,33 +238,46 @@ def _line_scan( at = closed.end() elif token in ("/*", "", at) + closer = "*/" if token == "/*" else "-->" + close = line.find(closer, at) if close < 0: - return start, inside, None + # The comment runs on to the line that closes it. + return start, inside, closer # From the marker itself, so the stretch can never be a quoted value. inside.append((start, close)) - at = close + (2 if token == "/*" else 3) + at = close + len(closer) elif token[0] not in "\"`": return start, inside, None return len(line), inside, None def block_open_before( - lines: list[str], *, hash_comments: str = _HASH_ANYWHERE, block_comments: bool = True + lines: list[str], + *, + triple: bool = False, + dashes: bool = False, + ini: bool = False, + hash_comments: str = _HASH_ANYWHERE, + block_comments: bool = True, ) -> str | None: - """The triple quote open where the line after ``lines`` starts, the lines read with the same - scan as the cited one. A line with no triple quote, outside a block, is passed over without - a scan; inside one, only the closing quote is looked for.""" + """The block open where the line after ``lines`` starts (a triple quote, or the */ or --> + of a block comment), the lines read with the same scan as the cited one. A line that could + open none, outside a block, is passed over without a scan; inside one, only its closer is + looked for.""" + openers = ['"""', "'''"] if triple else [] + openers += ["/*", ", + open where the line starts), and between matching quotes (not a key, not a lookup), or in a configuration file written bare after ``:`` or ``=`` or as a shell default, ``${MODEL:-gpt-4o}``. ``name`` is made only of MODEL_NAME_CHARS, so an occurrence starting inside one that failed has a name character just @@ -608,14 +633,14 @@ def model_problems(contract: AgentContract, source_root: Path) -> list[str]: triple=triple, hash_comments=hash_comments, block_comments=block_comments, - block=( - block_open_before( - lines[: model.line - 1], - hash_comments=hash_comments, - block_comments=block_comments, - ) - if triple - else None + # Every file can hold a block open from an earlier line: \n', + 2, + id="a-markup-block-over-lines", + ), + pytest.param( + "page.xml", + '\n', + 2, + id="a-markup-block-before-its-closer", + ), + ], +) +def test_a_value_inside_a_block_comment_over_lines_is_not_the_model( + root, file, written, line +): + (root / file).write_text(written, encoding="utf-8") + + problems, _, _ = checked(root, {"name": "gpt-4o", "path": file, "line": line}) + + assert problems == [not_named("gpt-4o", line, file)] + + +@pytest.mark.parametrize( + "file, written, line", + [ + pytest.param( + "a.js", + '/*\nold\n*/\nconst model = "gpt-4o";\n', + 4, + id="after-the-closing-line", + ), + pytest.param( + "a.js", + '/*\nold */ const model = "gpt-4o";\n', + 2, + id="after-the-closer-on-its-line", + ), + pytest.param( + "a.js", + '/* old */\nconst model = "gpt-4o";\n', + 2, + id="closed-on-an-earlier-line", + ), + pytest.param( + "a.js", + '/* old */ const model = "gpt-4o";\n', + 1, + id="closed-on-the-same-line", + ), + pytest.param( + "page.html", + '\n\n', + 4, + id="after-a-markup-block", + ), + pytest.param( + "a.js", + 'const open = "/*";\nconst model = "gpt-4o";\n', + 2, + id="an-opener-in-a-string", + ), + pytest.param( + "a.js", + "const open = ' a comment ends at). ``block`` is - the one open where the line starts. A line is a comment when it is the `* ` middle of a + block still open at its end (a triple quote, or the */ or --> a comment ends at; --> only in + ``markup``, where " close = line.find(closer, at) if close < 0: + if token == " - of a block comment), the lines read with the same scan as the cited one. A line that could - open none, outside a block, is passed over without a scan; inside one, only its closer is - looked for.""" + """The block open where ``lines[before]`` starts (a triple quote, or the */ or --> of a + block comment), the lines before it read with the same scan as the cited one. A comment is + carried to later lines only when its closer is on a later line of the file: one that never + closes doesn't compile, so its opener is text (a regex, a template). A line that could open + none, outside a block, is passed over without a scan; inside one, only its closer is looked + for.""" openers = ['"""', "'''"] if triple else [] - openers += ["/*", "\n\n', + 3, + id="a-markup-block-in-a-component", + ), + pytest.param( + "a.js", + '/* was\nconst model = "gpt-4o";\nmore\n*/\n', + 2, + id="a-closer-after-the-cited-line", + ), ], ) def test_a_value_inside_a_block_comment_over_lines_is_not_the_model( @@ -1994,6 +2006,42 @@ def test_a_value_inside_a_block_comment_over_lines_is_not_the_model( 4, id="a-triple-quote-inside-a-comment", ), + # An opener whose closer is on no later line is text: a comment that never closes + # doesn't compile. And /;\n', + 2, + id="a-markup-opener-outside-markup-with-a-closer-later", + ), ], ) def test_a_value_outside_a_block_comment_is_the_model(root, file, written, line): From e473069bfc7405d453e355e1ebd325871ee38ca0 Mon Sep 17 00:00:00 2001 From: khushalsonawat Date: Wed, 11 Nov 2026 17:29:46 +0530 Subject: [PATCH 3/5] fix(harness): carry comment through in XML under other suffixes (.config, .csproj, .xaml) and in PHP or JSP. Now everywhere else. Adds tests for those files and for a comment after an earlier closed one. Review round 1 of the P3 fix on #289 (TH-8953). Signed-off-by: khushalsonawat --- src/fi/alk/harness/model_citation.py | 33 +++++++++++++++++----------- tests/harness/test_contract_model.py | 26 ++++++++++++++++++++++ 2 files changed, 46 insertions(+), 13 deletions(-) diff --git a/src/fi/alk/harness/model_citation.py b/src/fi/alk/harness/model_citation.py index 492a5654c..3d8033819 100644 --- a/src/fi/alk/harness/model_citation.py +++ b/src/fi/alk/harness/model_citation.py @@ -64,9 +64,6 @@ {".py", ".pyi", ".pyw", ".rb", ".pl", ".r", ".jl", ".ex", ".exs", ".nim", ".lua"} ) _YAML_SUFFIXES = frozenset({".yaml", ".yml"}) -# Markup: only there is . +_NO_MARKUP_COMMENT_SUFFIXES = ( + _NO_HASH_COMMENT_SUFFIXES + | _NO_BLOCK_COMMENT_SUFFIXES + | _TRIPLE_SUFFIXES + | _DASH_COMMENT_SUFFIXES +) # Where a quote leaves text out: ... or …, also inside brackets, since only letters are compared. _ELISION = re.compile(r"\.{3,}|…") # How many letters of code may sit between two words of a quote in the lines: a string prefix @@ -196,10 +203,10 @@ def _line_scan( """One pass over a line: where its comment starts (or its length), the stretches of it that are text (inside a triple-quoted string, or a block comment closed on the line), and the block still open at its end (a triple quote, or the */ or --> a comment ends at; --> only in - ``markup``, where of a block comment), the lines before it read with the same scan as the cited one. A comment is - carried to later lines only when its closer is on a later line of the file: one that never - closes doesn't compile, so its opener is text (a regex, a template). A line that could open - none, outside a block, is passed over without a scan; inside one, only its closer is looked - for.""" + carried to later lines only when its closer is on a later line of the file: an opener that + never closes is text (a regex, a template), not a comment over the rest of the file. A line + that could open none, outside a block, is passed over without a scan; inside one, only its + closer is looked for.""" openers = ['"""', "'''"] if triple else [] if block_comments: openers.append("/*") @@ -659,7 +666,7 @@ def model_problems(contract: AgentContract, source_root: Path) -> list[str]: hash_comments=hash_comments, block_comments=block_comments, # A block open from an earlier line: a triple quote, /* where block comments are, \n', + 2, + id="a-markup-block-in-dotnet-config", + ), + pytest.param( + "a.csproj", + '\n', + 2, + id="a-markup-block-in-a-project-file", + ), + pytest.param( + "page.php", + '\n', + 2, + id="a-markup-block-in-php", + ), ], ) def test_a_value_inside_a_block_comment_over_lines_is_not_the_model( From 05875d278b48ea2569416ef0772546a0d12a8d73 Mon Sep 17 00:00:00 2001 From: khushalsonawat Date: Wed, 11 Nov 2026 17:39:01 +0530 Subject: [PATCH 4/5] fix(harness): read markup named like configuration as markup env.xml and Dockerfile.xml took the configuration rule and accepted a model inside a real multi-line comment. Configuration is now decided by the suffix or the whole file name, so they carry . _NO_MARKUP_COMMENT_SUFFIXES = ( - _NO_HASH_COMMENT_SUFFIXES - | _NO_BLOCK_COMMENT_SUFFIXES - | _TRIPLE_SUFFIXES - | _DASH_COMMENT_SUFFIXES + _NO_HASH_COMMENT_SUFFIXES | _NO_BLOCK_COMMENT_SUFFIXES | _DASH_COMMENT_SUFFIXES ) # Where a quote leaves text out: ... or …, also inside brackets, since only letters are compared. _ELISION = re.compile(r"\.{3,}|…") @@ -675,7 +672,13 @@ def model_problems(contract: AgentContract, source_root: Path) -> list[str]: ini=ini, hash_comments=hash_comments, block_comments=block_comments, - markup=not (config or suffix in _NO_MARKUP_COMMENT_SUFFIXES), + # Configuration by its suffix or its whole name: env.xml and Dockerfile.xml are + # markup. + markup=not ( + suffix in CONFIG_SUFFIXES + or _names_configuration(name_lower) + or suffix in _NO_MARKUP_COMMENT_SUFFIXES + ), ), ): return [ diff --git a/tests/harness/test_contract_model.py b/tests/harness/test_contract_model.py index 0c9d8d22f..5cf7e43b2 100644 --- a/tests/harness/test_contract_model.py +++ b/tests/harness/test_contract_model.py @@ -1912,6 +1912,12 @@ def test_a_value_in_a_block_comment_is_not_the_model(root, file, written, name): 2, id="a-markup-block-in-php", ), + pytest.param( + "env.xml", + '\n', + 2, + id="a-markup-block-in-markup-named-like-configuration", + ), ], ) def test_a_value_inside_a_block_comment_over_lines_is_not_the_model( @@ -2032,8 +2038,8 @@ def test_a_value_inside_a_block_comment_over_lines_is_not_the_model( 4, id="a-triple-quote-inside-a-comment", ), - # An opener whose closer is on no later line is text: a comment that never closes - # doesn't compile. And \n", + 2, + id="a-markup-opener-in-configuration-with-a-closer-later", + ), + pytest.param( + ".env", + "BANNER=\n", + 2, + id="a-markup-opener-in-configuration-by-name-with-a-closer-later", + ), ], ) def test_a_value_outside_a_block_comment_is_the_model(root, file, written, line): From 29982d6dcd6e0f97692be725f10b12e356183020 Mon Sep 17 00:00:00 2001 From: khushalsonawat Date: Wed, 11 Nov 2026 17:44:39 +0530 Subject: [PATCH 5/5] docs(harness): say which configuration names stay configuration Review round 3 of the P3 fix on #289 (TH-8953). Signed-off-by: khushalsonawat --- src/fi/alk/harness/model_citation.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/fi/alk/harness/model_citation.py b/src/fi/alk/harness/model_citation.py index d6c69d8e5..755e5315e 100644 --- a/src/fi/alk/harness/model_citation.py +++ b/src/fi/alk/harness/model_citation.py @@ -672,8 +672,8 @@ def model_problems(contract: AgentContract, source_root: Path) -> list[str]: ini=ini, hash_comments=hash_comments, block_comments=block_comments, - # Configuration by its suffix or its whole name: env.xml and Dockerfile.xml are - # markup. + # Configuration by its suffix or its whole name: env.xml is markup. A name that + # is configuration by itself stays so whatever its suffix (Dockerfile.xml, .env.xml). markup=not ( suffix in CONFIG_SUFFIXES or _names_configuration(name_lower)