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
39 changes: 39 additions & 0 deletions .claude/notes/contracts.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <type>`): 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 <<EOF | bash`) is not parsed, a known false pass for a
`max_count: 0` guard; the guide lists it.

The splitter is linear: each nesting level re-scans only its own substring, and nesting is
capped. Each simple command is then windowed like a long command, so the ReDoS bound still
holds. Quoted and escaped operator characters are swapped for private-use stand-ins before
`shlex` runs, because `shlex` with `punctuation_chars` splits `';'` and `\;` like a bare `;`.

## Recording a CLI invocation

### Evidence, not attestation
Expand Down
30 changes: 30 additions & 0 deletions docs/TASK_DEFINITION_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -1037,6 +1037,7 @@ Checks whether the agent executed specific tools/commands during evaluation. Ins
| `max_count` | `null` | Optional inclusive upper bound. When set, the criterion passes iff `min_count <= matches <= max_count`. |
| `require_success` | `false` | Only count commands that completed successfully. This is the reported success of the whole tool call, so `cmd \| tail` and `cmd; true` can pass when `cmd` fails. |
| `exclude_pattern` | `null` | Regex that must NOT match; a command matching both `command_pattern` and `exclude_pattern` is skipped. It searches the same text as `command_pattern`, whether or not a `command_pattern` is set. |
| `match_on` | `command_text` | `command_text` matches the text of each call. `simple_commands` matches each command the shell runs, one at a time (see **Matching simple commands**). Set it per criterion. |

**Which text is searched.** The shape of a call decides the text that `command_pattern` and `exclude_pattern` search:

Expand Down Expand Up @@ -1066,6 +1067,35 @@ So with `tool_name: null`, a `max_count: 0` guard or an `exclude_pattern` does n

With a pattern, `tool_name: null` searches the command of a shell call, not the parameters of every tool (see **Which text is searched**).

**Matching simple commands (`match_on: simple_commands`).** With the default `match_on: command_text`, a pattern searches the text of a shell call, so it cannot tell a command that runs from a mention of it. `match_on: simple_commands` splits each shell call into the simple commands the shell runs, and matches the pattern against each of them. A simple command is its argv with quotes resolved, joined by single spaces.

```yaml
- type: "command_executed"
description: "Agent did not run flow debug"
command_pattern: "^(\\S*/)?uip (maestro )?flow debug\\b"
match_on: simple_commands
min_count: 0
max_count: 0
```

How the call is split:

- **Not commands:** heredoc bodies, comments, and the words of a `for`/`select`/`case` header. A `<<'EOF'` body (quoted delimiter) is dropped whole. Of a `<<EOF` body (unquoted), only the `$(...)` and backtick contents are kept, because the shell runs them. A heredoc fed to a shell (`bash -s <<EOF`, also behind assignments or `env`: `X=1 sh <<EOF`) is a script, and its commands count. Stdin piped into a shell from another command (`cat <<EOF | bash`, `echo 'uip a' | sh`) is not parsed: its commands do not count. An `echo`/`printf` string is an argument of `echo`, so an anchored pattern (`^uip ...`) does not match it.
- **Commands:** each part between `;` `&&` `||` `|` `&` `(` `)` and line breaks; the contents of `$(...)`, backticks and `<(...)`; `sh -c`/`bash -lc` strings and `eval` arguments; the `CMD` of `find ... -exec CMD ;` (also `-exec sh -c '...' _ {} ;`). In the outer command, a substitution shows as `__SUBST__`. Redirections are removed.
- **Wrappers that are stripped:** `timeout [flags] N`, `sudo [flags]`, `nohup`, `nice [-n N]`, `exec`, `command`, `builtin`, `stdbuf [flags]`, `time [-p]`, `xargs [flags]`, and shell keywords (`if`, `then`, `do`, `!`, ...). So `timeout 60 uip flow debug x` matches `^uip flow debug`.
- **Kept:** leading assignments and `env` with its options, because a guard can need them (`env -u UIPATH_STUDIO_PID uip rpa build`, `X=1 uip codedapp deploy`). Wrapper stripping stops at the first assignment or `env`, so write `^(env \\S+ )*` or `^(\\w+=\\S* )*` before the program if those forms must also match. A shell after them is still parsed: `X=1 bash -c 'uip a'` gives `X=1 bash -c __SUBST__` and `uip a`.

What a pattern must take into account:

- **Quotes are resolved.** `uip 'flow' "debug"` is `uip flow debug`; do not put `["']` in the pattern.
- **A pattern cannot span two commands.** `X=1; uip a` is the two commands `X=1` and `uip a`. Use two criteria.
- **Not a shell.** Loop variables and other parameters are not expanded (`for c in "flow debug"; do uip $c; done` gives `uip $c`), and aliases and shell functions are not resolved. The body of a heredoc fed to python/node is dropped, so a pattern cannot match the script that body holds.
- **Counting is per simple command.** Each matching simple command counts once towards `min_count` and `max_count`, so `uip a && uip a` counts 2. With `command_text` the same call counts 1.
- **`exclude_pattern`** is matched against the same simple command, so it skips a command, not the whole call.
- **Fallback to `command_text`.** A call that runs PowerShell or cmd.exe anywhere (a `pwsh`/`powershell`/`cmd` program, as in the Windows Codex route `"C:\...\pwsh.exe" -Command '...'`; PowerShell text without that wrapper, seen as an `$env:NAME` reference or a `Verb-Noun` cmdlet such as `Get-Content`; or a tool whose name contains `powershell`), and text that cannot be split (an unterminated quote, `$(` or backtick, or one of the private-use code points the splitter uses as marks, U+E000-U+E007 and U+E0FF), is matched with `command_text` for that call. The criterion details give the number of these calls and the reason.
- **Other tools.** The JSON params of a named non-shell tool (Write, Edit, ...) are matched as with `command_text`. An argv-list `command` is re-quoted and split like a string.
- It reads the recorded command text only, so it works the same on every agent and driver (Claude Code, Codex `bash -lc` records, studio-web).

### `cli_called`

Checks whether a CLI invocation matching a **structured** pattern was recorded, by reading a JSON Lines invocation log the sandbox produced. **Binary scoring.**
Expand Down
5 changes: 3 additions & 2 deletions plugins/coder-eval/reference/criteria.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,12 @@ Optional:
| Field | What it is |
| --- | --- |
| `tool_name` | Tool name filter (e.g., 'Bash'). None = any tool. A pattern searches only the `command` param of a shell call: a Bash call, or any call with a `command` param, such as a shell tool under another name. A named tool without a `command` param is searched on its JSON params. With None, a call without a `command` param is not searched: a mention in a Write, Edit or TodoWrite body does not count, and a `max_count: 0` guard (also the legacy `command_not_executed` without a tool_name) does not see it. Name the tool to search its params. |
| `command_pattern` | Regex to match command parameters. None = any command. For a shell command (see tool_name) the pattern is matched against the raw command text OR its shell-normalized form — `shlex`-resolved quoting with a `bash`/`zsh -lc` wrapper stripped — whichever hits, so you need not hand-encode shell quoting/escaping (`'single'`, `"double"`, bare). |
| `command_pattern` | Regex to match command parameters. None = any command. For a shell command (see tool_name) the pattern is matched against the raw command text OR its shell-normalized form — `shlex`-resolved quoting with a `bash`/`zsh -lc` wrapper stripped — whichever hits, so you need not hand-encode shell quoting/escaping (`'single'`, `"double"`, bare). With `match_on: simple_commands` it is matched against each simple command instead (see match_on). |
| `min_count` | Minimum matching commands required. ``0`` allows the criterion to pass when no commands match (combine with ``max_count: 0`` to express ``must NOT match``). |
| `max_count` | Optional maximum matching commands allowed (inclusive). ``None`` means no upper bound — the current default. When set, the criterion passes iff ``min_count <= match_count <= max_count``. |
| `require_success` | If True, only count successful commands. This is the reported success of the whole tool call, so `cmd \| tail` and `cmd; true` can pass when cmd fails. |
| `exclude_pattern` | Regex that must NOT match. Commands matching both command_pattern and exclude_pattern are skipped. Like command_pattern, this is matched against the raw shell command OR its shell-normalized form, so it also excludes quote-obfuscated variants of the same call. |
| `exclude_pattern` | Regex that must NOT match. Commands matching both command_pattern and exclude_pattern are skipped. Like command_pattern, this is matched against the raw shell command OR its shell-normalized form, so it also excludes quote-obfuscated variants of the same call. With `match_on: simple_commands` it is matched per simple command: it skips that simple command, not the whole call. |
| `match_on` | What a pattern matches in a shell call. `command_text` (default): the command text and its shell-normalized form, once per call. `simple_commands`: each simple command the shell runs, as its argv with quotes resolved, joined by single spaces -- heredoc bodies, comments and echo strings are not commands, `$(...)`, `bash -c` strings and heredocs fed to a shell are, and leading shell keywords (`if`, `then`, `do`, `!`, ...) and timeout/sudo/nohup/nice/exec/command/builtin/xargs/stdbuf/time wrappers are stripped (assignments and `env` stay). Stdin piped into a shell (`cat <<EOF \| bash`) is not parsed. Each matching simple command counts towards min_count/max_count. A PowerShell/cmd call or text that cannot be split is matched as `command_text`, which the details say. Other tools' JSON params are matched as before. |

### `commands_efficiency`

Expand Down
Loading
Loading