Repository navigation
Expand file tree
/
Copy pathtest_codex_attribution.py
More file actions
91 lines (80 loc) · 5.12 KB
/
Copy pathtest_codex_attribution.py
File metadata and controls
91 lines (80 loc) · 5.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# Copyright (c) 2026 Firefly Software Solutions Inc.
# SPDX-License-Identifier: Apache-2.0
import json
import unittest
from tests.test_codex_launch import CodexCrew, REVIEWER, TANK
class CodexPatchAttributionTests(CodexCrew):
"""Normalized hook contracts; file effects occur between real pre/post handlers, not injected call records."""
def setup_lanes(self):
s = self.lead(plan="api=30m [backend]; review=20m [review]")
s.ok("progress", "assign", "review", "--to", "Seraph")
self.spawn(s, TANK, "tank", "api")
self.spawn(s, REVIEWER, "seraph", "review")
(self.api / "api").mkdir(exist_ok=True)
return s
def patch_call(self, s, path="api/x.py", content="value = 1"):
call = dict(tool_name="apply_patch", tool_use_id="patch-builder", agent_id=TANK, agent_type="tank",
tool_input={"input": f"*** Begin Patch\n*** Add File: {path}\n+{content}\n*** End Patch\n"})
out = s.hook("pre-tool", s.payload("PreToolUse", **call))
return call, (out.get("hookSpecificOutput") or {})
def writes(self, s):
return s.run_doc()["agents"][REVIEWER].get("scope_writes") or {}
def test_pending_patch_is_possible_only_for_its_declared_path_and_later_is_exact(self):
s = self.setup_lanes()
review = s.begin("git status --short", REVIEWER, "seraph")
self.assertFalse(review["denied"], review)
patch, verdict = self.patch_call(s)
self.assertNotEqual(verdict.get("permissionDecision"), "deny", verdict)
# The external file tool writes after its PreToolUse and before its PostToolUse, while the reader finishes.
(self.api / "api" / "x.py").write_text("value = 1\n")
(self.api / "api" / "unexplained.py").write_text("unknown = 1\n")
s.finish(s.execute(review))
self.assertEqual(self.writes(s).get("certain"), ["api/unexplained.py"])
self.assertEqual(self.writes(s).get("paths"), ["api/unexplained.py", "api/x.py"])
s.hook("post-tool", s.payload("PostToolUse", tool_response={"success": True}, **patch))
edits = json.loads((self.state / "calls" / s.sid / ".edits.json").read_text())["edits"]
self.assertEqual([(e["caller"], e["path"]) for e in edits], [(TANK, str(self.api / "api" / "x.py"))])
again = s.begin("git status --short", REVIEWER, "seraph")
s.finish(s.execute(again))
self.assertEqual(self.writes(s).get("certain"), ["api/unexplained.py"])
def test_an_out_of_scope_patch_gets_advice_and_a_failed_patch_supplies_no_exact_edit(self):
s = self.setup_lanes()
patch, verdict = self.patch_call(s, path="outside.py")
self.assertNotIn("permissionDecision", verdict)
self.assertIn("outside.py is outside Backend engineer (Tank)'s scope", verdict.get("additionalContext") or "")
patch, verdict = self.patch_call(s)
self.assertNotIn("permissionDecision", verdict)
s.hook("post-tool", s.payload("PostToolUse", tool_response={"isError": True, "error": "patch rejected"},
**patch))
path = self.state / "calls" / s.sid / ".edits.json"
self.assertEqual(json.loads(path.read_text()).get("edits", []) if path.exists() else [], [])
self.assertFalse((self.state / "calls" / s.sid / "patch-builder.json").exists())
def test_failed_closed_patch_keeps_only_its_targets_possible(self):
s = self.setup_lanes()
review = s.begin("git status --short", REVIEWER, "seraph")
patch, verdict = self.patch_call(s)
self.assertNotEqual(verdict.get("permissionDecision"), "deny", verdict)
(self.api / "api" / "x.py").write_text("partial = 1\n")
(self.api / "api" / "unexplained.py").write_text("unknown = 1\n")
s.hook("post-tool", s.payload("PostToolUse", tool_response={"success": False}, **patch))
s.finish(s.execute(review))
writes = self.writes(s)
self.assertEqual(writes.get("paths"), ["api/unexplained.py", "api/x.py"])
self.assertEqual(writes.get("certain"), ["api/unexplained.py"])
def test_a_successful_patch_in_another_repository_does_not_clear_an_unknown_writer(self):
s = self.setup_lanes()
review = s.begin("git status --short", REVIEWER, "seraph")
# A successful edit with the same relative path belongs to a different repository.
call = dict(tool_name="apply_patch", tool_use_id="foreign-patch",
tool_input={"input": "*** Begin Patch\n*** Add File: api/x.py\n+value = 1\n*** End Patch\n",
"workdir": str(self.kit)})
before = s.hook("pre-tool", s.payload("PreToolUse", **call))
self.assertNotEqual((before.get("hookSpecificOutput") or {}).get("permissionDecision"), "deny", before)
(self.kit / "api").mkdir(exist_ok=True)
(self.kit / "api" / "x.py").write_text("value = 1\n")
s.hook("post-tool", s.payload("PostToolUse", tool_response={"success": True}, **call))
(self.api / "api" / "x.py").write_text("unknown = 1\n")
s.finish(s.execute(review))
self.assertEqual(self.writes(s).get("certain"), ["api/x.py"])
if __name__ == "__main__":
unittest.main()