Skip to content
Merged
115 changes: 89 additions & 26 deletions src/fi/alk/harness/model_citation.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@
".gradle", ".java", ".cs", ".jl", ".dart", ".ex", ".exs",
}
)
# Code languages where <!-- is never a comment over lines, only text (a regex, a string, a
# template): there, as in configuration, it is a comment only to its line's end. In every other
# file (HTML, XML and its many suffixes: .config, .csproj, .xaml; PHP, JSP, Vue, Svelte, and any
# suffix not known here) it runs on to its -->.
_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
Expand Down Expand Up @@ -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,
Expand All @@ -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 <!-- runs on over lines). ``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 (
Expand Down Expand Up @@ -227,36 +246,70 @@ def _line_scan(
at = closed.end()
elif token in ("/*", "<!--"):
# A block comment closed on the line is passed over: `/* x */ model = "gpt-4o"`.
close = line.find("*/" if token == "/*" else "-->", at)
closer = "*/" if token == "/*" else "-->"
close = line.find(closer, at)
if close < 0:
return start, inside, None
if token == "<!--" and not markup:
# Outside markup, <!-- is a comment only to the line's end.
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],
before: int,
*,
triple: bool = False,
dashes: bool = False,
ini: bool = False,
hash_comments: str = _HASH_ANYWHERE,
block_comments: bool = True,
markup: bool = False,
) -> 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 ``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: 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("<!--")
# The last line each comment closer is on, looked for once, from the file's end.
last_closer: dict[str, int] = {}
block: str | None = None
for line in lines:
if block is None and '"""' not in line and "'''" not in line:
for at in range(before):
line = lines[at]
if block is None and not any(opener in line for opener in openers):
continue
if block is not None and block not in line:
continue
_, _, block = _line_scan(
line,
block,
triple=True,
dashes=dashes,
ini=ini,
triple=triple,
hash_comments=hash_comments,
block_comments=block_comments,
markup=markup,
)
if block in _COMMENT_CLOSERS:
if block not in last_closer:
last_closer[block] = next(
(i for i in range(len(lines) - 1, -1, -1) if block in lines[i]), -1
)
if last_closer[block] <= at:
block = None
return block


Expand Down Expand Up @@ -406,7 +459,8 @@ def named_on(
block: str | None = None,
) -> bool:
"""Whether ``name`` is the value written on ``line``: whole, outside a comment and outside
a triple-quoted string (``block`` is the triple quote open where the line starts), and
a triple-quoted string or a block comment (``block`` is the triple quote, or the */ or -->,
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
Expand Down Expand Up @@ -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, <!--
# outside code and configuration.
block=block_open_before(
lines,
model.line - 1,
triple=triple,
dashes=dashes,
ini=ini,
hash_comments=hash_comments,
block_comments=block_comments,
# 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)
or suffix in _NO_MARKUP_COMMENT_SUFFIXES
),
),
):
return [
Expand Down
4 changes: 2 additions & 2 deletions src/fi/alk/harness/skills/understand-agent/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ Find, in roughly this order:
more, with ... where you leave text out. The lines you name are checked against your latest
such quote, given with them or before. Lines that don't hold it are refused, and asked about
once more if they come without a quote; after that, the quote is kept and the lines are
recorded as unconfirmed (with a quote of fewer than 16 letters, the lines are kept, also
recorded as unconfirmed).
recorded as unconfirmed (with a quote of fewer than 16 letters, no prompt is saved; the
lines are recorded as unconfirmed).

7. **The modality.** How this agent is run: a voice session, a text conversation, or work it
carries out on websites or a computer. This decides how it is later run, so getting it wrong
Expand Down
23 changes: 15 additions & 8 deletions src/fi/alk/harness/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ def _checked_against_source(
outcome = (
"the quote is kept and the lines are recorded as unconfirmed"
if quote_pieces(quote)
else "the lines are copied as the prompt and recorded as unconfirmed, "
else "the lines are recorded as unconfirmed and no prompt is saved, "
"since that quote is too short to stand for it"
)
problems.append(
Expand All @@ -340,20 +340,23 @@ def _checked_against_source(
"the quote is kept, since they don't hold it"
)
else:
# A quote of a few words says too little to stand for the prompt:
# the lines are copied.
# A quote of a few words says too little to stand for the prompt, and
# lines nobody confirmed would be shown as it.
contract.system_prompt_excerpt = ""
contract.system_prompt_from = []
set_aside.append(
f"system_prompt_from: the lines named ({named}) are copied "
"unconfirmed, since they don't hold the short quote"
f"system_prompt_from: the lines named ({named}) are recorded as "
"unconfirmed and no prompt is saved, since they don't hold the "
"quote and it is too short to stand for the prompt"
)
else:
problems.append(mismatch)
asked.add("prompt-quote")
problems.append(
"system_prompt_from: lines named later are checked against this "
"quote until you give another; a later mismatch records the lines as "
"unconfirmed and keeps the quote, or the lines if the quote has fewer "
"than 16 letters"
"unconfirmed and keeps the quote, or saves no prompt if the quote has "
"fewer than 16 letters"
)
if cited := model_problems(contract, source_root):
if asked is not None and "model" in asked:
Expand Down Expand Up @@ -435,7 +438,11 @@ def _save_contract(contract: AgentContract, destination: Path) -> dict[str, Any]
f"{where.path[:200]} {where.start_line}-{where.end_line}"
for where in contract.unconfirmed_prompt_from
)
kept = "the lines are the prompt" if contract.system_prompt_from else "the quote is the prompt"
kept = (
"the quote is the prompt"
if contract.system_prompt_excerpt
else "no prompt is saved, since the quote is too short to stand for it"
)
said += (
f"\nsystem_prompt_from: the lines named ({named}) are recorded as unconfirmed, since "
f"they don't hold the quote; {kept}."
Expand Down
Loading
Loading