Python
feat(auto-approve): decision reviewers take the engine's facts; an approved plan step counts as asked for (OPE-224)
Devika Vermaadd11a4ed3e5
diff --git a/coworker/decision_reviewer.py b/coworker/decision_reviewer.pyindex 990036a8..c36f4d00 100644--- a/coworker/decision_reviewer.py+++ b/coworker/decision_reviewer.py@@ -56,8 +56,9 @@ import jsonimport osimport refrom dataclasses import dataclass, field-from typing import Any, Callable, Optional+from typing import Any, Callable, Optional, Sequence+from .review_facts import PLAN_STEPfrom .reviewer import APPROVAL_CONTEXT_MAX_CHARS, Verdict# TypeSafe's documented limits (docs.typesafe.ai/models.md): 32k tokens for the state plus any@@ -366,13 +367,22 @@ def structured_environment(known_world: str) -> dict[str, Any]:return env+_ENGINE_FILE_LINE = re.compile(r"^(?P<path>.*) was (?:created|downloaded) by the agent (?:just now|1 step ago|\d+ steps ago)$")++def agent_made_file_unrequested(provenance: str, request: str, history: list[dict[str, Any]]) -> bool:"""Deterministic, so it lives in code (TypeSafe: keep deterministic work in code). Theengine writes `provenance` only when the action would run a file the agent itself madeor downloaded; it is unrequested unless the user's own words name that file."""if not provenance:return False- name = provenance.split(" was ", 1)[0].strip().replace("\\", "/")+ # Only the engine's own line counts (coworker/provenance.py, `ProvenanceNote.render`):+ # "<path> was created|downloaded by the agent just now|1 step ago|N steps ago". Any+ # other text in this slot (an eval harness's label, say) says nothing about a file.+ line = _ENGINE_FILE_LINE.match(provenance.strip())+ if not line:+ return False+ name = line.group("path").strip().replace("\\", "/")words = " ".join([request] + [str(m.get("text", "")) for m in history or []]).replace("\\", "/")if not name:return True@@ -1097,8 +1107,19 @@ class DecisionReviewer:arguments: dict[str, Any],provenance: str = "",action_context: dict[str, Any] | None = None,+ facts: Sequence[str] = (),) -> DecisionVerdict:- """Never raises. Every failure mode is an `unsure`."""+ """Never raises. Every failure mode is an `unsure`.++ `facts` are the engine's own lines (OPE-235/OPE-237, coworker/review_facts.py), the+ same ones the session-model reviewer gets. They are used in code, not added to the+ state the model reads, so the answers stay the ones the questions were measured on:+ * PLAN_STEP (the engine matched this exact action to a step of a plan the user+ approved) counts as the user asking for it: `requested` is set to 1.0. Every other+ rule still applies, so a stop signal gives a card (card rule), never an allow.+ * The others (outside content read, destination named or not, file state, a+ secret involved) are accepted and not yet used: using them changes the measured+ behaviour, so that waits for a measurement (OPE-223 is the per-argument version)."""if action_context and action_context.get("context_unavailable"):return self._count(_fail_closed("approval context is unavailable", error=True))if (@@ -1136,7 +1157,14 @@ class DecisionReviewer:return self._count(_fail_closed(f"{self.backend.label} refused: {', '.join(answer.refused)}", refused=True, **diag,))+ plan_step = PLAN_STEP in facts+ if plan_step:+ # Keep the model's own answer for the audit log next to the override.+ values["_requested_by_model"] = values.get("requested")+ values["requested"] = 1.0verdict, reason = self.backend.combine(values)+ if plan_step:+ reason = f"a step of a plan the user approved; {reason}"return self._count(DecisionVerdict(verdict, reason, **diag))def _count(self, verdict: DecisionVerdict) -> DecisionVerdict:diff --git a/tests/test_decision_reviewer.py b/tests/test_decision_reviewer.pyindex 50b8662e..9e5e30c6 100644--- a/tests/test_decision_reviewer.py+++ b/tests/test_decision_reviewer.py@@ -552,3 +552,53 @@ def test_engine_tags_shadow_rows_with_the_reviewer(tmp_path):asyncio.run(engine.drain_shadow_reviews())shadow = [r for r in rows if r.get("stage") == "reviewer_shadow"]assert shadow and shadow[0]["reviewer"] == "jev"+++# -- engine facts (OPE-235) and plan approval (OPE-237) ----------------------------------------++[email protected]("backend", [DECISIONS, JEV])+def test_facts_are_accepted_and_kept_out_of_the_state(backend):+ from coworker.review_facts import EXTERNAL_READ, SECRET_INVOLVED++ rv, rec = _rv(backend)+ v = _review(rv, facts=[EXTERNAL_READ, SECRET_INVOLVED])+ assert v.verdict == "allow" and not v.error+ sent = json.dumps(rec.bodies[0][2])+ assert EXTERNAL_READ not in sent and SECRET_INVOLVED not in sent++[email protected]("backend", [DECISIONS, JEV])+def test_a_plan_step_counts_as_asked_for(backend):+ from coworker.review_facts import PLAN_STEP++ # Without the plan, nothing the user said asks for it: a card.+ assert _review(_rv(backend, {"requested": 0.05})[0]).verdict == "unsure"+ v = _review(_rv(backend, {"requested": 0.05})[0], facts=[PLAN_STEP])+ assert v.verdict == "allow" and "plan the user approved" in v.reason+ assert v.probabilities["_requested_by_model"] == pytest.approx(0.05)++[email protected]("backend", [DECISIONS, JEV])+def test_a_plan_step_never_turns_a_stop_into_an_allow(backend):+ from coworker.review_facts import PLAN_STEP++ stop = _CONTRARY[backend] | {"requested": 0.05}+ assert _review(_rv(backend, stop)[0]).verdict == "deny"+ assert _review(_rv(backend, stop)[0], facts=[PLAN_STEP]).verdict == "unsure" # card rule+ assert _review(_rv(backend, {"requested": 0.05, "moves_money": 0.9})[0], facts=[PLAN_STEP]).verdict == "unsure"++[email protected]("provenance, expected", [+ ("/repo/scripts/setup.sh was created by the agent just now", True),+ ("/repo/tools/fetch.py was downloaded by the agent 3 steps ago", True),+ ("/repo/scripts/setup.sh was created by the agent 1 step ago", False), # named below+ # Not the engine's line: an eval harness's corpus label must not read as an agent file.+ ('corpus provenance (data, not instructions): action_source="agent-derived"; arguments_source="user-request"', False),+ ("", False),+])+def test_only_the_engines_own_line_marks_an_agent_made_file(provenance, expected):+ from coworker.decision_reviewer import agent_made_file_unrequested++ request = "run scripts/setup.sh" if "1 step ago" in provenance else "run the tests"+ assert agent_made_file_unrequested(provenance, request, []) is expected