|
@@ -0,0 +1,172 @@
|
|
|
|
|
+import ast
|
|
|
|
|
+import re
|
|
|
|
|
+import subprocess
|
|
|
|
|
+import tomllib
|
|
|
|
|
+import unittest
|
|
|
|
|
+from pathlib import Path
|
|
|
|
|
+from unittest.mock import patch
|
|
|
|
|
+
|
|
|
|
|
+from tools import codex_harness
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ROOT = Path(__file__).resolve().parents[2]
|
|
|
|
|
+BASE_COMMIT = "aae04014eb520a815438de051950c014bd9ea9c6"
|
|
|
|
|
+AGENT_DIR = ROOT / ".codex" / "agents"
|
|
|
|
|
+AGENT_FILES = {
|
|
|
|
|
+ "cpld_explorer.toml": "read-only",
|
|
|
|
|
+ "cpld_engineer.toml": "workspace-write",
|
|
|
|
|
+ "cpld_test_engineer.toml": "workspace-write",
|
|
|
|
|
+ "cpld_reviewer.toml": "read-only",
|
|
|
|
|
+}
|
|
|
|
|
+FORBIDDEN_ASSIGNMENTS = {
|
|
|
|
|
+ "model",
|
|
|
|
|
+ "model_reasoning_effort",
|
|
|
|
|
+ "default_subagent_model",
|
|
|
|
|
+ "default_subagent_reasoning_effort",
|
|
|
|
|
+}
|
|
|
|
|
+PHASE0_PATH_ALLOWLIST = {
|
|
|
|
|
+ "AGENTS.md",
|
|
|
|
|
+ ".gitignore",
|
|
|
|
|
+ ".codex/config.toml",
|
|
|
|
|
+ ".codex/agents/cpld_explorer.toml",
|
|
|
|
|
+ ".codex/agents/cpld_engineer.toml",
|
|
|
|
|
+ ".codex/agents/cpld_test_engineer.toml",
|
|
|
|
|
+ ".codex/agents/cpld_reviewer.toml",
|
|
|
|
|
+ "tools/codex_harness.py",
|
|
|
|
|
+ "tests/unit/test_codex_harness.py",
|
|
|
|
|
+ "tests/unit/test_native_multi_agent_contract.py",
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def _git_check_ignore(path: str) -> subprocess.CompletedProcess[str]:
|
|
|
|
|
+ return subprocess.run(
|
|
|
|
|
+ ["git", "check-ignore", "-v", "--no-index", path],
|
|
|
|
|
+ cwd=ROOT,
|
|
|
|
|
+ capture_output=True,
|
|
|
|
|
+ check=False,
|
|
|
|
|
+ shell=False,
|
|
|
|
|
+ text=True,
|
|
|
|
|
+ timeout=10,
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+class NativeMultiAgentContractTests(unittest.TestCase):
|
|
|
|
|
+ def test_project_config_enables_native_multi_agent_without_runtime_selection_pin(self):
|
|
|
|
|
+ config = tomllib.loads((ROOT / ".codex" / "config.toml").read_text(encoding="utf-8"))
|
|
|
|
|
+ self.assertTrue(config["features"]["multi_agent"])
|
|
|
|
|
+ self.assertEqual(
|
|
|
|
|
+ config["agents"],
|
|
|
|
|
+ {"enabled": True, "max_concurrent_threads_per_session": 6},
|
|
|
|
|
+ )
|
|
|
|
|
+ self.assertFalse(FORBIDDEN_ASSIGNMENTS.intersection(config))
|
|
|
|
|
+
|
|
|
|
|
+ def test_custom_agents_have_required_fields_permissions_and_no_assignment_pin(self):
|
|
|
|
|
+ expected_names = {path.removesuffix(".toml") for path in AGENT_FILES}
|
|
|
|
|
+ observed_names = set()
|
|
|
|
|
+ for filename, expected_sandbox in AGENT_FILES.items():
|
|
|
|
|
+ path = AGENT_DIR / filename
|
|
|
|
|
+ raw = path.read_text(encoding="utf-8")
|
|
|
|
|
+ parsed = tomllib.loads(raw)
|
|
|
|
|
+ self.assertEqual(
|
|
|
|
|
+ set(parsed), {"name", "description", "sandbox_mode", "developer_instructions"}
|
|
|
|
|
+ )
|
|
|
|
|
+ self.assertIn(parsed["name"], expected_names)
|
|
|
|
|
+ self.assertEqual(parsed["sandbox_mode"], expected_sandbox)
|
|
|
|
|
+ self.assertTrue(parsed["developer_instructions"].strip())
|
|
|
|
|
+ self.assertIsNone(
|
|
|
|
|
+ re.search(
|
|
|
|
|
+ r"(?m)^\s*(?:model|model_reasoning_effort|default_subagent_model|default_subagent_reasoning_effort)\s*=",
|
|
|
|
|
+ raw,
|
|
|
|
|
+ )
|
|
|
|
|
+ )
|
|
|
|
|
+ observed_names.add(parsed["name"])
|
|
|
|
|
+ self.assertEqual(observed_names, expected_names)
|
|
|
|
|
+
|
|
|
|
|
+ def test_governance_contract_is_phase_aware_and_root_owned(self):
|
|
|
|
|
+ raw = (ROOT / "AGENTS.md").read_text(encoding="utf-8")
|
|
|
|
|
+ for required in (
|
|
|
|
|
+ "[DYNAMIC-NATIVE-MA]",
|
|
|
|
|
+ "dependency graph",
|
|
|
|
|
+ "Phase 0 must not implement product functionality",
|
|
|
|
|
+ "Phase 1 must review every nested",
|
|
|
|
|
+ "The Harness must not start Codex",
|
|
|
|
|
+ "Root owns interface freezes",
|
|
|
|
|
+ ):
|
|
|
|
|
+ self.assertIn(required, raw)
|
|
|
|
|
+ self.assertIsNone(
|
|
|
|
|
+ re.search(
|
|
|
|
|
+ r"(?m)^\s*(?:model|model_reasoning_effort|default_subagent_model|default_subagent_reasoning_effort)\s*=",
|
|
|
|
|
+ raw,
|
|
|
|
|
+ )
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+ def test_gitignore_reincludes_only_approved_project_agent_files(self):
|
|
|
|
|
+ ignored = [
|
|
|
|
|
+ ".codex/sessions/example.json",
|
|
|
|
|
+ ".codex/transcripts/example.json",
|
|
|
|
|
+ ".codex/agents/other.toml",
|
|
|
|
|
+ ]
|
|
|
|
|
+ tracked_exceptions = [
|
|
|
|
|
+ ".codex/config.toml",
|
|
|
|
|
+ ".codex/agents/cpld_explorer.toml",
|
|
|
|
|
+ ".codex/agents/cpld_engineer.toml",
|
|
|
|
|
+ ".codex/agents/cpld_test_engineer.toml",
|
|
|
|
|
+ ".codex/agents/cpld_reviewer.toml",
|
|
|
|
|
+ ]
|
|
|
|
|
+ for path in ignored:
|
|
|
|
|
+ result = _git_check_ignore(path)
|
|
|
|
|
+ self.assertEqual(result.returncode, 0, path)
|
|
|
|
|
+ pattern = result.stdout.split("\t", 1)[0].rsplit(":", 1)[-1]
|
|
|
|
|
+ self.assertFalse(pattern.startswith("!"), path)
|
|
|
|
|
+ for path in tracked_exceptions:
|
|
|
|
|
+ result = _git_check_ignore(path)
|
|
|
|
|
+ self.assertEqual(result.returncode, 0, path)
|
|
|
|
|
+ pattern = result.stdout.split("\t", 1)[0].rsplit(":", 1)[-1]
|
|
|
|
|
+ self.assertTrue(pattern.startswith("!"), path)
|
|
|
|
|
+
|
|
|
|
|
+ def test_harness_git_process_boundary_is_ast_and_runtime_enforced(self):
|
|
|
|
|
+ tree = ast.parse((ROOT / "tools" / "codex_harness.py").read_text(encoding="utf-8"))
|
|
|
|
|
+ run_calls = []
|
|
|
|
|
+ forbidden_calls = []
|
|
|
|
|
+ for node in ast.walk(tree):
|
|
|
|
|
+ if isinstance(node, ast.Call) and isinstance(node.func, ast.Attribute):
|
|
|
|
|
+ if isinstance(node.func.value, ast.Name) and node.func.value.id == "subprocess":
|
|
|
|
|
+ if node.func.attr == "run":
|
|
|
|
|
+ run_calls.append(node)
|
|
|
|
|
+ else:
|
|
|
|
|
+ forbidden_calls.append(node.func.attr)
|
|
|
|
|
+ if isinstance(node.func.value, ast.Name) and node.func.value.id == "os":
|
|
|
|
|
+ forbidden_calls.append(node.func.attr)
|
|
|
|
|
+ self.assertEqual(len(run_calls), 1)
|
|
|
|
|
+ self.assertFalse(forbidden_calls)
|
|
|
|
|
+ source = (ROOT / "tools" / "codex_harness.py").read_text(encoding="utf-8")
|
|
|
|
|
+ self.assertNotIn("Popen(", source)
|
|
|
|
|
+ self.assertNotIn("shell=True", source)
|
|
|
|
|
+
|
|
|
|
|
+ completed = subprocess.CompletedProcess(
|
|
|
|
|
+ args=["git"], returncode=0, stdout=b"observed\n", stderr=b""
|
|
|
|
|
+ )
|
|
|
|
|
+ with patch.object(codex_harness.subprocess, "run", return_value=completed) as run:
|
|
|
|
|
+ codex_harness._run_git(ROOT, ["rev-parse", "HEAD"])
|
|
|
|
|
+ args, kwargs = run.call_args
|
|
|
|
|
+ self.assertEqual(args[0][0], "git")
|
|
|
|
|
+ self.assertFalse(kwargs["shell"])
|
|
|
|
|
+ self.assertFalse(kwargs["text"])
|
|
|
|
|
+ self.assertEqual(kwargs["timeout"], 10.0)
|
|
|
|
|
+
|
|
|
|
|
+ def test_phase0_changed_paths_are_strictly_allowlisted(self):
|
|
|
|
|
+ result = subprocess.run(
|
|
|
|
|
+ ["git", "diff", "--name-only", BASE_COMMIT, "HEAD"],
|
|
|
|
|
+ cwd=ROOT,
|
|
|
|
|
+ capture_output=True,
|
|
|
|
|
+ check=True,
|
|
|
|
|
+ shell=False,
|
|
|
|
|
+ text=True,
|
|
|
|
|
+ timeout=10,
|
|
|
|
|
+ )
|
|
|
|
|
+ changed = {line.replace("\\", "/") for line in result.stdout.splitlines() if line}
|
|
|
|
|
+ self.assertTrue(changed <= PHASE0_PATH_ALLOWLIST, sorted(changed - PHASE0_PATH_ALLOWLIST))
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+if __name__ == "__main__":
|
|
|
|
|
+ unittest.main()
|