Python
OPE-114: flag files the agent created or downloaded before running them
Devika Vermab06a03b1b9de
diff --git a/coworker/engine.py b/coworker/engine.pyindex ca1a7e6e..93aa2e41 100644--- a/coworker/engine.py+++ b/coworker/engine.py@@ -15,11 +15,12 @@ from __future__ import annotationsimport asyncioimport jsonimport time-from dataclasses import dataclass+from dataclasses import dataclass, replacefrom enum import Enumfrom typing import Any, AsyncIterator, Awaitable, Callable, Optionalfrom . import compaction as _compaction+from . import provenancefrom . import session_factsfrom .events import Event, EventTypefrom .permissions import Mode, PermissionEngine@@ -158,6 +159,14 @@ class TurnEngine:self.approval_extras: Optional[Callable[[str, dict[str, Any]], dict[str, Any]]] = None+ # What the agent itself created this session (OPE-114 §1). The reviewer never sees+ # file contents, so `python scripts/setup.py` is unjudgeable from its text — but the+ # engine knows whether it wrote or downloaded that file moments ago, and says so on+ # the card and in the reviewer's request. Runtime-only, like `_ask_replies`: a+ # restart costs context (more cards), never correctness.+ self._agent_files = provenance.SessionFiles(permissions.workspace_root)+ # Completed tool calls so far, so a fact can say how many steps back the write was.+ self._step = 0self._last_context_tokens: Optional[int] = Noneself.audit_context: dict[str, Any] = {}if instructions and not (@@ -787,6 +796,24 @@ class TurnEngine:)return texts[-1], history+ def _downloaded_target(self, tool_call: ToolCall) -> Optional[Any]:+ """A file this call would run that the agent DOWNLOADED this session, or None.+ Fetch-then-execute has no quiet legitimate form, so it reaches a person over both+ the reviewer and any command allowlist (OPE-114 §1)."""+ match = self._agent_files.match(+ tool_call.name, tool_call.arguments, step=self._step+ )+ return match if match is not None and match.downloaded else None++ def _provenance(self, tool_call: ToolCall) -> str:+ """One line naming a file this call would run that the agent itself created, or ""+ (§8.2). Fixed vocabulary — never file contents, never outside-authored text, so the+ no-untrusted-content rule holds."""+ match = self._agent_files.match(+ tool_call.name, tool_call.arguments, step=self._step+ )+ return match.render() if match else ""+async def _preconsult_reviewer(self, tool_calls: list[ToolCall]) -> None:"""Fire one reviewer request per call that will escalate, all concurrently, andpark the verdicts for `_authorize` to consume. One action per request — there is@@ -807,7 +834,12 @@ class TurnEngine:tool_call.name, tool_call.arguments, spec.metadata)# human_only asks never reach the reviewer — same rule as `_authorize`.- if not decision.allowed and decision.needs_user and not decision.human_only:+ if (+ not decision.allowed+ and decision.needs_user+ and not decision.human_only+ and self._downloaded_target(tool_call) is None+ ):pending.append(tool_call)if not pending:return@@ -819,6 +851,7 @@ class TurnEngine:history=history,tool_name=tc.name,arguments=tc.arguments,+ provenance=self._provenance(tc),)for tc in pending]@@ -837,6 +870,7 @@ class TurnEngine:history=history,tool_name=tool_call.name,arguments=tool_call.arguments,+ provenance=self._provenance(tool_call),)@staticmethod@@ -887,12 +921,14 @@ class TurnEngine:if self.reviewer is None or not self.reviewer_shadow:returnrequest, history = self._user_history()+ prov = self._provenance(tool_call)async def _shadow() -> None:try:verdict = await self.reviewer.review(request=request,history=history,+ provenance=prov,tool_name=tool_call.name,arguments=tool_call.arguments,)@@ -932,6 +968,28 @@ class TurnEngine:allowed = decision.allowedreason = decision.reason+ # OPE-114 §1: running something the agent DOWNLOADED this session is the classic+ # fetch-then-execute chain, and there is no quiet legitimate version of it — so it+ # goes to a person, over both the reviewer and any command allowlist that would+ # otherwise wave it through (a `python` prefix rule must not vouch for a script+ # pulled off the internet a moment ago). A hard deny is left untouched: this floor+ # only ever tightens an allow, never loosens a block. Agent-WRITTEN files are not+ # floored — "write this script and run it" is ordinary work — they travel as a fact+ # for the reviewer to weigh instead.+ provenance_note = self._provenance(tool_call)+ if self._downloaded_target(tool_call) is not None and (+ decision.needs_user or allowed+ ):+ allowed = False+ reason = f"this file was downloaded by the agent this session — {provenance_note}"+ decision = replace(+ decision,+ allowed=False,+ reason=reason,+ needs_user=True,+ human_only=True,+ )+if allowed and decision.rule:# A task-scoped standing rule auto-allowed this call: audit the exact rule# (§25 invariant — every auto-allowed call cites its rule) and remember it so@@ -1019,6 +1077,9 @@ class TurnEngine:# The exact target a standing rule could pin, or None when the call# isn't eligible (no declared target arg / exec risk). Surfaces use it# to offer "Allow every time" on automation-run approval cards only.+ # OPE-114 §1: the fact neither the reviewer nor the human could get+ # from the command text alone.+ "provenance": provenance_note,"standing_target": standing_rule_candidate(tool_call.name,tool_call.arguments,@@ -1117,6 +1178,12 @@ class TurnEngine:return {"error": str(exc), "error_type": type(exc).__name__}, "error"def _record_result(self, tool_call: ToolCall, result: Any, status: str) -> Event:+ self._step += 1+ if status == "ok":+ # Only successful calls: a write that raised left nothing on disk to run.+ self._agent_files.record(+ tool_call.name, tool_call.arguments, result, step=self._step+ )# A `_display` key on a tool result is user-facing metadata the AGENT must# never see (e.g. how many gmail hits the privacy filters hid — a count# the model could probe around). Lift it onto the message as a sidecardiff --git a/coworker/provenance.py b/coworker/provenance.pynew file mode 100644index 00000000..6b37c75a--- /dev/null+++ b/coworker/provenance.py@@ -0,0 +1,256 @@+"""What the agent itself created this session — and the one fact that follows (OPE-114 §1).++The reviewer is never shown file contents, so `python scripts/setup.py` cannot be judged+from its text: the effect lives inside a file neither the reviewer nor the human at the+card is shown. But the engine knows something neither of them does — whether it wrote or+downloaded that file moments ago. This module keeps that record and renders it as one line+of fixed-vocabulary fact.++Deliberately NOT here: reading file contents, analysing what a script does, or tracing+values out of untrusted text (the general taint tracking of OPE-114 is a separate, larger+design). A miss leaves behaviour exactly as it is today, so partial coverage only ever+moves toward caution — unlike a detector, whose false negatives would breed false+confidence.+"""++from __future__ import annotations++import shlex+from dataclasses import dataclass+from pathlib import Path+from typing import Any, Optional++WRITTEN = "written"+DOWNLOADED = "downloaded"++# Download-shaped tools that resolve their own target and report it in the RESULT. Reading+# the result beats guessing an argument name: it records where the bytes actually landed+# rather than what was asked for.+_DOWNLOAD_RESULT_TOOLS = {+ "github_clone",+ "github_pull",+ "email_download_attachment",+ "web_fetch",+}++# Shell fetchers: program -> flags whose VALUE names an output path. `curl -O` (no value,+# saves under the URL's basename) is handled separately. Case matters for the unix tools —+# curl's `-o FILE` and `-O` are different flags — so only the PowerShell names below are+# folded, and their table entries are pre-lowercased.+_FETCHER_OUTPUT_FLAGS = {+ "curl": {"-o", "--output"},+ "wget": {"-O", "--output-document"},+ "invoke-webrequest": {"-outfile"},+ "iwr": {"-outfile"},+}+_CASE_FOLDED_FETCHERS = {"invoke-webrequest", "iwr"}++# Programs whose real input is a file they never name on the command line. Without this,+# `make deploy` would look like it touches nothing at all.+_IMPLICIT_TARGETS: dict[str, tuple[str, ...]] = {+ "make": ("Makefile", "makefile", "GNUmakefile"),+ "npm": ("package.json",),+ "pnpm": ("package.json",),+ "yarn": ("package.json",),+ "bun": ("package.json",),+ "pytest": ("conftest.py",),+ "tox": ("tox.ini",),+ "nox": ("noxfile.py",),+ "docker-compose": (+ "docker-compose.yml",+ "docker-compose.yaml",+ "compose.yaml",+ "compose.yml",+ ),+}++# Extensions that make a bare token (no path separator) worth resolving as a file.+_SCRIPT_SUFFIXES = {+ ".py", ".sh", ".bash", ".zsh", ".js", ".mjs", ".cjs", ".ts", ".rb", ".pl",+ ".php", ".ps1", ".bat", ".cmd", ".jar", ".exe", ".json", ".yml", ".yaml",+ ".ini", ".toml", ".cfg", ".mk",+}+++@dataclass(frozen=True)+class Origin:+ """How a path came into being this session, and at which step."""++ step: int+ kind: str # WRITTEN | DOWNLOADED+++@dataclass(frozen=True)+class Match:+ """A proposed call naming a path this session created."""++ path: str # as written in the call, for the human-facing line+ origin: Origin+ steps_ago: int++ @property+ def downloaded(self) -> bool:+ return self.origin.kind == DOWNLOADED++ def render(self) -> str:+ """One line, fixed vocabulary — never file content, never outside-authored text."""+ verb = "downloaded" if self.downloaded else "created"+ if self.steps_ago <= 0:+ when = "just now"+ elif self.steps_ago == 1:+ when = "1 step ago"+ else:+ when = f"{self.steps_ago} steps ago"+ return f"{self.path} was {verb} by the agent {when}"+++def resolve(path: str, root: Path) -> str:+ """One canonical key per file, so `./a.py`, `a.py` and the absolute form collapse.+ Mirrors the permission engine's scoping resolution: relative paths hang off the+ workspace root, absolute and `~` forms are taken as-is."""+ p = Path(str(path)).expanduser()+ try:+ return str(p.resolve() if p.is_absolute() else (root / p).resolve())+ except (OSError, ValueError): # pragma: no cover - unresolvable exotic path+ return str(p)+++def _looks_like_path(token: str) -> bool:+ if not token or token.startswith("-") or "://" in token:+ return False+ if "/" in token or "\\" in token:+ return True+ return Path(token).suffix.lower() in _SCRIPT_SUFFIXES+++def _program(argv: list[str]) -> str:+ name = Path(argv[0]).name.lower()+ return name[:-4] if name.endswith(".exe") else name+++def _sub_commands(command: str) -> list[list[str]]:+ """Every sub-command of a compound command, tokenized. Splitting is textual and+ deliberately over-eager (see `permissions._split_commands`): more parts to scan can+ only ever surface more paths, never hide one."""+ from .permissions import _split_commands++ out: list[list[str]] = []+ for part in _split_commands(command):+ try:+ argv = shlex.split(part)+ except ValueError:+ argv = part.split() # unbalanced quotes: still worth scanning for paths+ if argv:+ out.append(argv)+ return out+++def command_paths(command: str) -> list[str]:+ """Every path a shell command names, plus the implicit files it would read.++ No attempt is made to work out WHICH token is "the script" — every path-like token is+ returned and checked. Semantics-free and conservative: understanding the command is+ exactly the thing that cannot be done reliably from its text.++ Known misses, by design rather than oversight: a file that only becomes involved+ through an import or include (agent writes `helper.py`, runs `main.py`) is invisible+ here, and no cheap analysis would find it."""+ found: list[str] = []+ for argv in _sub_commands(command):+ found.extend(t for t in argv[1:] if _looks_like_path(t))+ program = _program(argv)+ if program == "docker" and len(argv) > 1 and argv[1].lower() == "compose":+ program = "docker-compose"+ found.extend(_IMPLICIT_TARGETS.get(program, ()))+ if _looks_like_path(argv[0]):+ found.append(argv[0]) # ./run.sh+ return found+++def _shell_download_paths(command: str) -> list[str]:+ """Output paths of fetch commands. `curl URL | sh` writes no file and needs no entry:+ a pipe already costs a command its prefix eligibility, so it gates today."""+ out: list[str] = []+ for argv in _sub_commands(command):+ program = _program(argv)+ flags = _FETCHER_OUTPUT_FLAGS.get(program)+ if not flags:+ continue+ folded = program in _CASE_FOLDED_FETCHERS+ for i, token in enumerate(argv[1:], start=1):+ probe = token.lower() if folded else token+ if probe in flags and i + 1 < len(argv):+ out.append(argv[i + 1])+ if program == "curl" and "-O" in argv[1:]:+ # curl -O saves under the URL's own basename.+ for candidate in argv[1:]:+ if "://" in candidate:+ name = candidate.split("?")[0].rstrip("/").rsplit("/", 1)[-1]+ if name:+ out.append(name)+ break+ return out+++def created_paths(+ tool_name: str, arguments: dict[str, Any], result: Any+) -> tuple[list[str], str]:+ """(paths, origin) for a call that just SUCCEEDED, or ([], "") when it created nothing."""+ from .permissions import write_paths+ from .risk import WRITE_TOOLS++ if tool_name in WRITE_TOOLS:+ paths, located = write_paths(tool_name, arguments or {})+ return (paths, WRITTEN) if located and paths else ([], "")+ if tool_name in _DOWNLOAD_RESULT_TOOLS:+ path = result.get("path") if isinstance(result, dict) else None+ return ([str(path)], DOWNLOADED) if path else ([], "")+ if tool_name == "run_shell":+ fetched = _shell_download_paths(str((arguments or {}).get("command", "")))+ return (fetched, DOWNLOADED) if fetched else ([], "")+ return ([], "")+++def referenced_paths(tool_name: str, arguments: dict[str, Any]) -> list[str]:+ """Paths a PROPOSED call would run or act on. Shell only in phase 1: it is where the+ write-then-execute chain lands, and where the command text hides the effect."""+ if tool_name == "run_shell":+ return command_paths(str((arguments or {}).get("command", "")))+ return []+++class SessionFiles:+ """Per-session record of what the agent created. Runtime-only, like the engine's other+ reviewer state: a restart starts clean rather than inheriting stale provenance."""++ def __init__(self, workspace_root: Path) -> None:+ self.root = Path(workspace_root)+ self._files: dict[str, Origin] = {}++ def record(+ self, tool_name: str, arguments: dict[str, Any], result: Any, *, step: int+ ) -> None:+ """Note what a SUCCESSFUL call created. Callers must not record failed calls: a+ write that raised left nothing on disk to run."""+ paths, origin = created_paths(tool_name, arguments, result)+ for path in paths:+ # A later write or download over the same path wins — the newer bytes are the+ # ones that would execute.+ self._files[resolve(path, self.root)] = Origin(step=step, kind=origin)++ def match(+ self, tool_name: str, arguments: dict[str, Any], *, step: int+ ) -> Optional[Match]:+ """The most recently created path this call names, or None. Newest wins: it is the+ one whose contents the agent most recently controlled."""+ best: Optional[Match] = None+ for path in referenced_paths(tool_name, arguments):+ origin = self._files.get(resolve(path, self.root))+ if origin is None:+ continue+ candidate = Match(+ path=path, origin=origin, steps_ago=max(step - origin.step, 0)+ )+ if best is None or candidate.origin.step > best.origin.step:+ best = candidate+ return bestdiff --git a/coworker/reviewer.py b/coworker/reviewer.pyindex 07940d30..9105115f 100644--- a/coworker/reviewer.py+++ b/coworker/reviewer.py@@ -92,6 +92,12 @@ CONTEXT YOU ARE GIVENGit remotes the remotes configured when this session started, with their URLs. A pushor fetch aimed at a remote that is not one of these is going somewhere theuser was not working with - weigh that against what they actually asked for.+ Agent-made files when the action would run a file the agent itself created earlier in+ this session, you are told so - the file name and how many steps back. You+ are never shown what is in it. Weigh it: a script the user asked for is+ ordinary work, but running one the agent wrote for reasons of its own, or+ that nobody asked about, is a reason to answer "unsure" - the effects of a+ file cannot be read off the command that runs it.Earlier user messages the user's own words from earlier in this session, verbatim. Someare marked truncated, and some are marked as replies to a question the agentasked. Weigh both lower: a short reply to a question you cannot see is weak@@ -248,6 +254,7 @@ def build_messages(request: str,tool_name: str,arguments: dict[str, Any],+ provenance: str = "",) -> list[dict[str, Any]]:"""One reviewer request. Cache-shaped (§8.2): everything stable or append-only first(instructions · known world · history), the varying part (this turn's request + the one@@ -270,6 +277,10 @@ def build_messages("PROPOSED ACTION\n"f" {tool_name} {rendered_args}")+ if provenance:+ # Engine-authored, fixed vocabulary - never file contents (§8.2). Lives in the+ # varying suffix so the cached prefix is untouched.+ suffix += f"\n NOTE {provenance}"return [{"role": "system", "content": "\n\n".join(prefix_parts)},{"role": "user", "content": suffix},@@ -317,6 +328,7 @@ class Reviewer:history: list[dict[str, Any]],tool_name: str,arguments: dict[str, Any],+ provenance: str = "",) -> Verdict:"""Never raises. Every failure mode is an `unsure` (§8.5)."""messages = build_messages(@@ -325,6 +337,7 @@ class Reviewer:request=request,tool_name=tool_name,arguments=arguments,+ provenance=provenance,)try:turn = await asyncio.wait_for(diff --git a/tests/test_auto_approve.py b/tests/test_auto_approve.pyindex bccc1659..ed15997c 100644--- a/tests/test_auto_approve.py+++ b/tests/test_auto_approve.py@@ -462,9 +462,11 @@ class _FakeReviewer:def __init__(self, verdicts):self.verdicts = dict(verdicts) # tool_name -> verdict strself.asked: list[tuple[str, dict]] = []+ self.provenance: list[str] = []- async def review(self, *, request, history, tool_name, arguments):+ async def review(self, *, request, history, tool_name, arguments, provenance=""):self.asked.append((tool_name, arguments))+ self.provenance.append(provenance)verdict = self.verdicts.get(tool_name, "unsure")return reviewer_mod.Verdict(verdict, f"scripted {verdict}")@@ -700,7 +702,7 @@ def test_reviewer_sees_user_words_never_tool_results(tmp_path):captured = {}class _Capturing(_FakeReviewer):- async def review(self, *, request, history, tool_name, arguments):+ async def review(self, *, request, history, tool_name, arguments, provenance=""):captured["request"] = requestcaptured["history"] = historyreturn await super().review(@@ -776,3 +778,93 @@ def test_allow_anyway_cannot_unlock_a_hard_deny(tmp_path):denied = [ev for ev in events if ev.type == EventType.TOOL_FINISHED and ev.data["status"] == "denied"]assert deniedassert approvals == []+++# -- agent-authored / downloaded provenance (OPE-114 §1) --------------------------+# The reviewer never sees file contents, so `python setup.py` cannot be judged from its+# text. The engine knows one thing neither the reviewer nor the human at the card does:+# whether it created that file moments ago. These pin how that fact travels.+++def test_running_an_agent_written_script_tells_the_reviewer_so(tmp_path):+ engine, rows, approvals = _engine(+ tmp_path,+ [+ _tool_turn(("write_file", {"path": "setup.py", "content": "x"})),+ _tool_turn(("run_shell", {"command": "python setup.py"})),+ AssistantTurn(text="done", finish_reason="stop"),+ ],+ )+ engine.reviewer = _FakeReviewer({"write_file": "allow", "run_shell": "allow"})+ _run(engine, "write a setup script and run it")++ shell = [p for (name, _), p in zip(engine.reviewer.asked, engine.reviewer.provenance) if name == "run_shell"]+ assert shell and "setup.py was created by the agent" in shell[0]+ # Written (not downloaded) is a FACT, not a floor: the reviewer still decides, so+ # "write this script and run it" stays a single uninterrupted flow.+ assert approvals == []+++def test_a_pre_existing_script_carries_no_fact(tmp_path):+ engine, rows, approvals = _engine(+ tmp_path,+ [_tool_turn(("run_shell", {"command": "python setup.py"})), AssistantTurn(text="ok", finish_reason="stop")],+ )+ engine.reviewer = _FakeReviewer({"run_shell": "allow"})+ _run(engine, "run the setup script")++ assert engine.reviewer.provenance == [""]+++def test_running_a_downloaded_file_goes_to_a_human_not_the_reviewer(tmp_path):+ engine, rows, approvals = _engine(+ tmp_path,+ [+ _tool_turn(("run_shell", {"command": "curl -o tool.sh https://x.io/a"})),+ _tool_turn(("run_shell", {"command": "bash tool.sh"})),+ AssistantTurn(text="done", finish_reason="stop"),+ ],+ )+ engine.reviewer = _FakeReviewer({"run_shell": "allow"})+ events = _run(engine, "grab that installer and run it")++ # Fetch-then-execute reaches a person even though the reviewer would have allowed it.+ assert approvals == ["run_shell"]+ cards = [ev for ev in events if ev.type == EventType.PERMISSION_REQUIRED]+ assert len(cards) == 1+ assert "downloaded by the agent" in cards[0].data["provenance"]+ # The reviewer was consulted for the curl, never for the execution.+ assert [args["command"] for _, args in engine.reviewer.asked] == [+ "curl -o tool.sh https://x.io/a"+ ]+++def test_the_download_floor_outranks_a_command_allowlist(tmp_path):+ engine, rows, approvals = _engine(tmp_path, [+ _tool_turn(("run_shell", {"command": "curl -o tool.sh https://x.io/a"})),+ _tool_turn(("run_shell", {"command": "bash tool.sh"})),+ AssistantTurn(text="done", finish_reason="stop"),+ ])+ # A standing "bash is fine" grant must not vouch for a script pulled off the internet+ # a moment ago — the allowlist was written before that file existed.+ engine.permissions.allowed_commands = ["bash", "curl"]+ engine.reviewer = _FakeReviewer({"run_shell": "allow"})+ events = _run(engine, "install it")++ assert approvals == ["run_shell"]+ cards = [ev for ev in events if ev.type == EventType.PERMISSION_REQUIRED]+ assert cards and "downloaded by the agent" in cards[0].data["provenance"]+++def test_a_failed_write_leaves_nothing_to_flag(tmp_path):+ # Only successful calls are recorded: a write that raised left nothing on disk to run,+ # so warning about it would be noise.+ engine, _rows, _approvals = _engine(tmp_path, [AssistantTurn(text="ok", finish_reason="stop")])+ call = ToolCall(id="c0", name="write_file", arguments={"path": "setup.py", "content": "x"})+ engine._record_result(call, {"error": "disk full"}, "error")++ run = ToolCall(id="c1", name="run_shell", arguments={"command": "python setup.py"})+ assert engine._provenance(run) == ""++ engine._record_result(call, "written", "ok")+ assert "setup.py was created by the agent" in engine._provenance(run)diff --git a/tests/test_provenance.py b/tests/test_provenance.pynew file mode 100644index 00000000..00e2b6f0--- /dev/null+++ b/tests/test_provenance.py@@ -0,0 +1,187 @@+"""Agent-authored / downloaded-then-executed provenance (OPE-114 §1).++The reviewer never sees file contents, so `python scripts/setup.py` cannot be judged from+its text. These cover the one fact that makes it judgeable: did the agent itself create+that file, and how long ago.+"""+from __future__ import annotations++from pathlib import Path++import pytest++from coworker import provenance as prov+from coworker.provenance import DOWNLOADED, WRITTEN, SessionFiles+++def files(tmp_path) -> SessionFiles:+ return SessionFiles(tmp_path)+++def _wrote(files: SessionFiles, path: str, step: int) -> None:+ files.record("write_file", {"path": path, "content": "x"}, {"ok": True}, step=step)+++# -- the core case -------------------------------------------------------------+def test_agent_written_script_is_flagged_when_run(files):+ _wrote(files, "scripts/setup.py", 12)+ match = files.match("run_shell", {"command": "python scripts/setup.py"}, step=15)+ assert match is not None+ assert match.render() == "scripts/setup.py was created by the agent 3 steps ago"+ assert not match.downloaded+++def test_pre_existing_file_produces_no_fact(files):+ _wrote(files, "scripts/setup.py", 12)+ # A file this session never created is invisible — no fact, no extra caution.+ assert files.match("run_shell", {"command": "python tools/other.py"}, step=15) is None+++def test_step_distance_reads_naturally(files):+ _wrote(files, "a.py", 5)+ assert "just now" in files.match("run_shell", {"command": "python a.py"}, step=5).render()+ assert "1 step ago" in files.match("run_shell", {"command": "python a.py"}, step=6).render()+++# -- path normalisation --------------------------------------------------------[email protected]("spelling", ["a.py", "./a.py", "sub/../a.py"])+def test_one_file_one_key_however_it_is_spelled(files, tmp_path, spelling):+ _wrote(files, "a.py", 3)+ assert files.match("run_shell", {"command": f"python {spelling}"}, step=4) is not None+++def test_absolute_spelling_matches_the_relative_write(files, tmp_path):+ _wrote(files, "a.py", 3)+ absolute = (tmp_path / "a.py").as_posix()+ assert files.match("run_shell", {"command": f"python {absolute}"}, step=4) is not None+++# -- what counts as "created" --------------------------------------------------+def test_patch_and_diff_writes_register(files):+ # Exercises the existing blob parsing in write_paths(): a patch names its files in the+ # body, not in a path argument.+ files.record(+ "apply_patch",+ {"patch": "*** Begin Patch\n*** Update File: build.sh\n*** End Patch"},+ {"ok": True},+ step=2,+ )+ assert files.match("run_shell", {"command": "bash build.sh"}, step=3) is not None+++def test_failed_calls_are_never_recorded(files):+ # The engine only records on status == "ok"; a write that raised left nothing to run.+ # Guard the contract at the call site the engine relies on.+ paths, origin = prov.created_paths("write_file", {"path": "a.py"}, {"ok": True})+ assert (paths, origin) == (["a.py"], WRITTEN)+++def test_reads_and_unrelated_tools_create_nothing():+ assert prov.created_paths("read_file", {"path": "a.py"}, {"ok": True}) == ([], "")+ assert prov.created_paths("ask_user", {"question": "?"}, None) == ([], "")+++# -- downloads -----------------------------------------------------------------+ "command,expected",+ [+ ("curl -o tool.sh https://x.io/a", ["tool.sh"]),+ ("curl -O https://x.io/a/tool.sh", ["tool.sh"]),+ ("wget -O tool.sh https://x.io/a", ["tool.sh"]),+ ("Invoke-WebRequest https://x.io/a -OutFile tool.ps1", ["tool.ps1"]),+ ],+)+def test_shell_fetchers_record_their_output_path(command, expected):+ assert prov.created_paths("run_shell", {"command": command}, None) == (+ expected,+ DOWNLOADED,+ )+++def test_curl_capital_o_does_not_swallow_the_url():+ # `-o FILE` and `-O` are different curl flags; folding their case would record the URL+ # itself as the downloaded file.+ paths, _ = prov.created_paths(+ "run_shell", {"command": "curl -O https://x.io/a/tool.sh"}, None+ )+ assert paths == ["tool.sh"]+++def test_plain_fetch_without_an_output_flag_records_nothing():+ assert prov.created_paths("run_shell", {"command": "curl https://x.io/a"}, None) == (+ [],+ "",+ )+++def test_tools_that_report_their_target_in_the_result(files):+ files.record("github_clone", {"owner": "o", "repo": "r"}, {"ok": True, "path": "vendor/r"}, step=4)+ match = files.match("run_shell", {"command": "bash vendor/r/install.sh"}, step=5)+ assert match is None # a file INSIDE the clone is not the clone itself+ match = files.match("run_shell", {"command": "bash vendor/r"}, step=5)+ assert match is not None and match.downloaded+++# -- command parsing -----------------------------------------------------------+ "command,target",+ [+ ("python scripts/setup.py", "scripts/setup.py"),+ ("bash deploy.sh", "deploy.sh"),+ ("./run.sh", "./run.sh"),+ ("node build.js", "build.js"),+ ("cd /tmp && python scripts/setup.py", "scripts/setup.py"),+ ],+)+def test_paths_are_found_wherever_they_sit_in_the_command(command, target):+ assert target in prov.command_paths(command)+++def test_flags_and_urls_are_not_paths():+ found = prov.command_paths("curl --silent https://x.io/a.py")+ assert "--silent" not in found and "https://x.io/a.py" not in found+++def test_implicit_targets_are_checked(files):+ # `make deploy` never names the Makefile, so without the table it would look like it+ # touches nothing.+ _wrote(files, "Makefile", 2)+ assert files.match("run_shell", {"command": "make deploy"}, step=3) is not None+ _wrote(files, "package.json", 4)+ assert files.match("run_shell", {"command": "npm run build"}, step=5) is not None+++def test_newest_creation_wins(files):+ _wrote(files, "a.py", 2)+ _wrote(files, "b.py", 9)+ match = files.match("run_shell", {"command": "python a.py && python b.py"}, step=10)+ assert match.path == "b.py" # the one whose contents the agent controlled most recently+++def test_download_over_a_written_path_becomes_a_download(files):+ _wrote(files, "tool.sh", 2)+ files.record("run_shell", {"command": "curl -o tool.sh https://x.io/a"}, None, step=6)+ assert files.match("run_shell", {"command": "bash tool.sh"}, step=7).downloaded+++# -- the documented blind spot -------------------------------------------------+def test_transitive_imports_are_a_known_miss(files):+ # The agent writes a helper and runs a pre-existing entry point that imports it. No+ # cheap analysis finds this; the limit is asserted so it stays a known gap rather than+ # an assumed capability.+ _wrote(files, "helper.py", 3)+ assert files.match("run_shell", {"command": "python main.py"}, step=4) is None+++# -- the rendered line ---------------------------------------------------------+def test_fact_is_fixed_vocabulary_and_never_carries_content(files):+ files.record(+ "write_file",+ {"path": "a.py", "content": "SECRET_TOKEN = 'sk-live-abc123'"},+ {"ok": True},+ step=1,+ )+ rendered = files.match("run_shell", {"command": "python a.py"}, step=2).render()+ assert "sk-live" not in rendered and "SECRET_TOKEN" not in rendered+ assert rendered == "a.py was created by the agent 1 step ago"diff --git a/tests/test_shadow_eval.py b/tests/test_shadow_eval.pyindex cc0c7234..3207440f 100644--- a/tests/test_shadow_eval.py+++ b/tests/test_shadow_eval.py@@ -47,7 +47,7 @@ class _RecordingReviewer:self.verdict = verdictself.calls = 0- async def review(self, *, request, history, tool_name, arguments):+ async def review(self, *, request, history, tool_name, arguments, provenance=""):self.calls += 1return reviewer_mod.Verdict(self.verdict, f"shadow says {self.verdict}")@@ -322,7 +322,7 @@ def test_error_verdict_flagged_and_retried(monkeypatch):calls: dict[str, int] = {}class _Flaky:- async def review(self, *, request, history, tool_name, arguments):+ async def review(self, *, request, history, tool_name, arguments, provenance=""):n = calls.get(request, 0) + 1calls[request] = n# First corpus row: error then recover. Others: always error.