diff --git a/src/fi/alk/harness/model_citation.py b/src/fi/alk/harness/model_citation.py index fe274a647..755e5315e 100644 --- a/src/fi/alk/harness/model_citation.py +++ b/src/fi/alk/harness/model_citation.py @@ -119,6 +119,13 @@ ".gradle", ".java", ".cs", ".jl", ".dart", ".ex", ".exs", } ) +# Code languages where . +_NO_MARKUP_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,}|…") # How many letters of code may sit between two words of a quote in the lines: a string prefix @@ -175,6 +182,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, @@ -184,21 +195,29 @@ def _line_scan( triple: bool = False, hash_comments: str = _HASH_ANYWHERE, block_comments: bool = True, + markup: bool = False, ) -> 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; --> only in + ``markup``, where ", at) + closer = "*/" if token == "/*" else "-->" + close = line.find(closer, at) if close < 0: - return start, inside, None + if token == " 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: 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("/*") + if markup: + openers.append(", + 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 +662,23 @@ 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 + # A block open from an earlier line: a triple quote, /* where block comments are, \n', + 2, + id="a-markup-block-over-lines", + ), + pytest.param( + "page.xml", + '\n', + 2, + id="a-markup-block-before-its-closer", + ), + pytest.param( + "page.vue", + '\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", + ), + pytest.param( + "a.js", + '/* a */\nx\n/* real\nconst model = "gpt-4o";\n*/\n', + 4, + id="a-comment-after-an-earlier-closed-one", + ), + # \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", + ), + 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( + 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 = '/;\n', + 2, + id="a-markup-opener-outside-markup-with-a-closer-later", + ), + pytest.param( + "c.yaml", + "banner: \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): + (root / file).write_text(written, encoding="utf-8") + + problems, contract, _ = checked( + root, {"name": "gpt-4o", "path": file, "line": line} + ) + + assert problems == [] + assert contract.model is not None and contract.model.name == "gpt-4o" + + +@pytest.mark.parametrize( + "written", + [ + pytest.param( + "/*\n" * (MIB // 3 - 20) + 'const model = "gpt-4o";\n', + id="a-file-of-openers", + ), + pytest.param( + "*/ /*\n" * (MIB // 6 - 20) + 'const model = "gpt-4o";\n', + id="a-file-of-closers-and-openers", + ), + pytest.param( + "/* */ " * (MIB // 6 - 20) + '"gpt-4o"\n', id="a-line-of-closed-blocks" + ), + pytest.param( + "