diff --git a/.claude/notes/contracts.md b/.claude/notes/contracts.md index c749413a..c02f1716 100644 --- a/.claude/notes/contracts.md +++ b/.claude/notes/contracts.md @@ -125,6 +125,45 @@ The cost: with a null `tool_name`, a `max_count: 0` guard (and the legacy `command_not_executed` without a `tool_name`) no longer sees a command written into a Write or TodoWrite body. A guard on those bodies names the tool, one criterion per tool. +### Matching on simple commands + +`match_on: simple_commands` exists because graders could not tell a command that ran from a +mention of it. A `max_count: 0` guard tripped on a README heredoc that named the command, and +suites grew ever longer prefix lists and heredoc-skipping regexes to compensate (UiPath/skills +#5069). `coder_eval/shell_commands.py` splits the recorded text into simple commands once, +so a one-line anchored pattern replaces those regexes. + +The shape follows a blast-radius replay of 16,936 stored criterion evaluations (18 nightlies, +lab ticket 526): + +- It is opt-in per criterion, never global. With the patterns unchanged, 98 evaluations got + worse against 23 that got better: patterns that match quote characters, span two commands, + or target a python heredoc stop matching. +- Leading assignments and `env` options stay in the command. Stripping them lost the + `env -u UIPATH_STUDIO_PID` guards (43 evaluations). +- PowerShell / cmd.exe calls fall back to raw text. Parsing the Windows Codex + `pwsh -Command '...'` record as POSIX broke 9 evaluations. +- Matches are counted per simple command, not per call. Per call, a `min_count: 2` "passed" + on one real run plus a mention in a report. + +Text that cannot be split falls back to raw matching for that call, and the details say so. +So a parse failure never turns a match into a miss without notice. Any other exception from +the splitter falls back the same way (logged, reason `splitter error `): a splitter +bug must not score a guard 0.0, nor disarm early stop for every criterion of the task. + +The splitter's own marks (operator stand-ins, heredoc positions) are private-use code points, +and a text that holds one of those is not split. Only those: Codex commands do hold other +private-use code points (citation marks; 3 calls in 18 nightlies). A mark written as ASCII (`__HEREDOC_0__`) was agent +text too: a literal one raised IndexError or fed a real heredoc body to the wrong command. + +Stdin piped into a shell (`cat < bool: - """True if argv[0]'s basename looks like a POSIX shell. - - A predicate rather than an enumerated allowlist: the set of shells is open - (``bash``/``sh`` on Linux, ``zsh`` on macOS — Codex shells through the host's - login shell, see codex_agent.py — plus ``dash``/``ksh``/…), and every common - shell basename ends in ``sh``. Matching is additive (the raw text stays a - haystack), so favouring recall over a hand-maintained list is safe. - """ - return arg0.rsplit("/", 1)[-1].endswith("sh") - - -def _is_command_flag(tok: str) -> bool: - """True for a short-option cluster carrying a ``-c`` command string. - - Covers ``-c``, ``-lc``, ``-ic``, ``-lic`` (login/interactive + command) in - any order — a single ``-``-prefixed token whose option letters are all - alphabetic and include ``c``. ``--long`` options and ``-o=val`` forms are - rejected, so this is the first flag that actually introduces the command - string. The combined and split (``bash -l -c``) forms both work: a non-``c`` - short flag like ``-l`` simply isn't the command flag and the scan continues. - """ - return len(tok) >= 2 and tok[0] == "-" and tok[1] != "-" and tok[1:].isalpha() and "c" in tok[1:] +# A command whose first word is a PowerShell or cmd.exe program: bare, with a path, or quoted +# with a path that holds spaces (`"C:\Program Files\PowerShell\7\pwsh.exe" -Command ...`). +_NON_POSIX_NAME = rf"(?:{'|'.join(sorted(NON_POSIX_SHELLS))})(?:\.exe)?" +_NON_POSIX_WORD = "|".join( + ( + rf'"(?:[^"\n]{{0,260}}[\\/])?{_NON_POSIX_NAME}"', + rf"'(?:[^'\n]{{0,260}}[\\/])?{_NON_POSIX_NAME}'", + rf"(?:\S{{0,260}}[\\/])?{_NON_POSIX_NAME}", + ) +) +_NON_POSIX_PREFIX = re.compile(rf"\s*(?:{_NON_POSIX_WORD})(?:\s|$)", re.IGNORECASE) +# PowerShell text without its wrapper (Codex on Windows records the script alone): an +# `$env:NAME` reference, or a `Verb-Noun` cmdlet as a command's program. +_POWERSHELL_ENV = re.compile(r"\$env:\w") +_POWERSHELL_CMDLET = re.compile(r"[A-Z][a-z]+-[A-Z][A-Za-z]+") +_NOT_POSIX = "not POSIX shell" +_UNPARSEABLE = "not splittable into simple commands" @lru_cache(maxsize=1024) @@ -76,26 +76,21 @@ def _normalize_shell(cmd_text: str) -> str | None: if not tokens: return None # Unwrap `bash -lc "