test_codex_harness.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. import hashlib
  2. import json
  3. import re
  4. import subprocess
  5. import tempfile
  6. import unittest
  7. import xml.etree.ElementTree as ET
  8. from pathlib import Path
  9. from unittest.mock import patch
  10. from tools import codex_harness
  11. from tools import prod2u_p4_harness
  12. from tools import prod2u_p4_normal_v3_build
  13. ROOT = Path(__file__).resolve().parents[2]
  14. PROJECT = ROOT / "projects" / "prod2u-door-temp" / "project.json"
  15. CONVERGENCE_RUN_ID = "b2a3230e66f74a35bb74ac6730e58bb5"
  16. CONVERGENCE_BRANCH = "codex/final-convergence-" + CONVERGENCE_RUN_ID
  17. ASSIGNED_BASE_COMMIT = "d580b1f9586a0f6efcd9d176595a5b3c5da75329"
  18. OWNED_TEST_PATHS = [
  19. "tests/unit/test_project_config.py",
  20. "tests/unit/test_phase1_contract.py",
  21. "tests/unit/test_codex_harness.py",
  22. "tests/unit/test_native_multi_agent_contract.py",
  23. "tests/unit/test_project_generation.py",
  24. "config/prod2u_p4_verification_targets_v1.json",
  25. ]
  26. def _current_full_commit() -> str:
  27. return codex_harness._run_git(ROOT, ["rev-parse", "HEAD"]).stdout.decode("ascii").strip()
  28. def _current_short_commit() -> str:
  29. return codex_harness.current_commit8(ROOT)
  30. def _report_base(*, run_id: str = CONVERGENCE_RUN_ID, task_id: str = "audit") -> dict:
  31. return {
  32. "schema": codex_harness.AGENT_TASK_REPORT_SCHEMA,
  33. "run_id": run_id,
  34. "task_id": task_id,
  35. "dependencies": [],
  36. "assigned_base_commit": ASSIGNED_BASE_COMMIT,
  37. "result_commit": None,
  38. "integration_commit": None,
  39. "feature_branch": CONVERGENCE_BRANCH,
  40. "worktree": str(ROOT),
  41. "owned_paths": list(OWNED_TEST_PATHS),
  42. "native_thread": {
  43. "model_policy": "unconstrained",
  44. "reasoning_effort_policy": "unconstrained",
  45. "observed_model": None,
  46. "observed_reasoning_effort": None,
  47. "model_selection_source": "not_exposed",
  48. "effort_selection_source": "not_exposed",
  49. "observation_source": "not_exposed",
  50. "observed_permission_mode": "not_exposed",
  51. },
  52. "status": "completed",
  53. "read_only_report": "read-only evidence",
  54. }
  55. class CodexHarnessTests(unittest.TestCase):
  56. def test_run_id_is_lowercase_32_hex(self):
  57. value = codex_harness.new_run_id()
  58. self.assertRegex(value, r"^[0-9a-f]{32}$")
  59. def test_source_manifest_is_project_bound_and_excludes_legacy_protocol_sources(self):
  60. manifest = codex_harness.build_source_manifest(Path.cwd())
  61. paths = manifest["sources"]
  62. self.assertIn("src/top/prod2u_p4_normal_v3_top.v", paths)
  63. self.assertIn("src/common/frame_rx.v", paths)
  64. self.assertNotIn("src/rtl/binary921_crc16.v", paths)
  65. self.assertNotIn("src/rtl/binary921_frame_tx.v", paths)
  66. self.assertEqual(manifest["project_id"], "prod2u-door-temp")
  67. self.assertRegex(manifest["project_sha256"], r"^[0-9a-f]{64}$")
  68. def test_unverified_package_binds_commit_project_and_bitstream_hash(self):
  69. with tempfile.TemporaryDirectory() as directory:
  70. root = Path(directory)
  71. bitstream = root / "candidate.bit"
  72. bitstream.write_bytes(b"candidate-bitstream")
  73. package = codex_harness.create_unverified_package(
  74. bitstream=bitstream,
  75. project=PROJECT,
  76. commit=_current_short_commit(),
  77. run_id="a" * 32,
  78. output_root=root / "dist",
  79. )
  80. manifest = json.loads((package / "package_manifest.json").read_text(encoding="utf-8"))
  81. self.assertEqual(manifest["status"], "UNVERIFIED")
  82. self.assertEqual(manifest["commit"], _current_short_commit())
  83. self.assertEqual(manifest["commit_full"], _current_full_commit())
  84. self.assertEqual(manifest["run_id"], "a" * 32)
  85. self.assertEqual(
  86. manifest["bitstream_sha256"],
  87. hashlib.sha256(b"candidate-bitstream").hexdigest(),
  88. )
  89. self.assertTrue((package / "fw.bit").is_file())
  90. self.assertEqual(package.name, _current_short_commit() + "-" + manifest["bitstream_sha256"][:16])
  91. package_info = json.loads((package / "package-info.json").read_text(encoding="utf-8"))
  92. self.assertEqual(package_info["schema"], "prod2u-p4-normal-v3-folder-package-info-v3")
  93. self.assertEqual(package_info["bitstream"], "fw.bit")
  94. self.assertEqual(package_info["bitstream_sha256"], manifest["bitstream_sha256"])
  95. self.assertTrue((package / "protocol.md").is_file())
  96. self.assertTrue((package / "requirements.zh-CN.md").is_file())
  97. self.assertTrue((package / "source_manifest.json").is_file())
  98. self.assertTrue((package / "verification_targets.json").is_file())
  99. for relative in (
  100. "evidence/td-primary-receipt.json",
  101. "evidence/td-confirmation-receipt.json",
  102. "evidence/python-unit-receipt.json",
  103. "evidence/icarus-receipt.json",
  104. "evidence/verilator-receipt.json",
  105. "evidence/cocotb-receipt.json",
  106. "evidence/formal-receipt.json",
  107. "evidence/validation-summary.json",
  108. ):
  109. self.assertTrue((package / relative).is_file(), relative)
  110. def test_early_unverified_package_binds_full_production_and_input_receipt_hashes(self):
  111. with tempfile.TemporaryDirectory() as directory:
  112. root = Path(directory)
  113. bitstream = root / "candidate.bit"
  114. bitstream.write_bytes(b"early-candidate-bitstream")
  115. td_receipt = root / "td-primary.json"
  116. td_receipt.write_text(
  117. '{"schema":"td","status":"pass"}\n', encoding="utf-8"
  118. )
  119. source_manifest = codex_harness.build_source_manifest()
  120. source_manifest_bytes = (
  121. json.dumps(source_manifest, indent=2, sort_keys=True) + "\n"
  122. ).encode("utf-8")
  123. expected_source_manifest_sha256 = hashlib.sha256(source_manifest_bytes).hexdigest()
  124. expected_td_receipt_sha256 = hashlib.sha256(td_receipt.read_bytes()).hexdigest()
  125. production_commit = _current_full_commit()
  126. package = codex_harness.create_unverified_package(
  127. bitstream=bitstream,
  128. project=PROJECT,
  129. commit=_current_short_commit(),
  130. production_integration_commit=production_commit,
  131. source_manifest_sha256=expected_source_manifest_sha256,
  132. td_receipt_sha256=expected_td_receipt_sha256,
  133. package_class="UNVERIFIED_EARLY_BUILD",
  134. run_id="9" * 32,
  135. output_root=root / "dist",
  136. td_receipt=td_receipt,
  137. )
  138. manifest = json.loads((package / "package_manifest.json").read_text(encoding="utf-8"))
  139. package_info = json.loads((package / "package-info.json").read_text(encoding="utf-8"))
  140. bound_td = package / "evidence" / "td-primary-receipt.json"
  141. bound_td_value = json.loads(bound_td.read_text(encoding="utf-8"))
  142. self.assertEqual(manifest["status"], "UNVERIFIED")
  143. self.assertEqual(manifest["package_class"], "UNVERIFIED_EARLY_BUILD")
  144. self.assertEqual(manifest["commit"], _current_short_commit())
  145. self.assertEqual(manifest["commit_full"], _current_full_commit())
  146. self.assertEqual(manifest["production_integration_commit"], production_commit)
  147. self.assertEqual(manifest["source_manifest_sha256"], expected_source_manifest_sha256)
  148. self.assertEqual(manifest["bitstream_sha256"], hashlib.sha256(bitstream.read_bytes()).hexdigest())
  149. self.assertEqual(
  150. bound_td_value["source_receipt_sha256"], expected_td_receipt_sha256
  151. )
  152. self.assertEqual(
  153. manifest["td_receipt_sha256"], hashlib.sha256(bound_td.read_bytes()).hexdigest()
  154. )
  155. self.assertEqual(package_info["production_integration_commit"], production_commit)
  156. self.assertEqual(package_info["source_manifest_sha256"], expected_source_manifest_sha256)
  157. self.assertEqual(package_info["package_class"], "UNVERIFIED_EARLY_BUILD")
  158. def test_early_unverified_package_rejects_mismatched_binding_hashes(self):
  159. with tempfile.TemporaryDirectory() as directory:
  160. root = Path(directory)
  161. bitstream = root / "candidate.bit"
  162. bitstream.write_bytes(b"early-candidate-bitstream")
  163. td_receipt = root / "td-primary.json"
  164. td_receipt.write_text(
  165. '{"schema":"td","status":"pass"}\n', encoding="utf-8"
  166. )
  167. kwargs = {
  168. "bitstream": bitstream,
  169. "project": PROJECT,
  170. "commit": _current_short_commit(),
  171. "production_integration_commit": "b" * 40,
  172. "source_manifest_sha256": "c" * 64,
  173. "td_receipt_sha256": "d" * 64,
  174. "package_class": "UNVERIFIED_EARLY_BUILD",
  175. "run_id": "a" * 32,
  176. "output_root": root / "dist",
  177. "td_receipt": td_receipt,
  178. }
  179. with self.assertRaisesRegex(ValueError, "source manifest"):
  180. codex_harness.create_unverified_package(**kwargs)
  181. def test_early_package_selftest_accepts_and_detects_production_binding_tamper(self):
  182. with tempfile.TemporaryDirectory() as directory:
  183. root = Path(directory)
  184. bitstream = root / "candidate.bit"
  185. bitstream.write_bytes(b"early-selftest-bitstream")
  186. td_receipt = root / "td-primary.json"
  187. td_receipt.write_text(
  188. '{"schema":"td","status":"pass"}\n', encoding="utf-8"
  189. )
  190. source_manifest = codex_harness.build_source_manifest()
  191. expected_source_manifest_sha256 = hashlib.sha256(
  192. (json.dumps(source_manifest, indent=2, sort_keys=True) + "\n").encode(
  193. "utf-8"
  194. )
  195. ).hexdigest()
  196. expected_td_receipt_sha256 = hashlib.sha256(td_receipt.read_bytes()).hexdigest()
  197. run_id = "8" * 32
  198. production_commit = _current_full_commit()
  199. package = codex_harness.create_unverified_package(
  200. bitstream=bitstream,
  201. project=PROJECT,
  202. commit=_current_short_commit(),
  203. production_integration_commit=production_commit,
  204. source_manifest_sha256=expected_source_manifest_sha256,
  205. td_receipt_sha256=expected_td_receipt_sha256,
  206. package_class="UNVERIFIED_EARLY_BUILD",
  207. run_id=run_id,
  208. output_root=root / "dist",
  209. td_receipt=td_receipt,
  210. )
  211. valid = prod2u_p4_harness.package_selftest_payload(
  212. package, verification_run_id=run_id
  213. )
  214. self.assertEqual(valid["status"], "fail")
  215. self.assertTrue(valid["failures"])
  216. package_info_path = package / "package-info.json"
  217. package_info = json.loads(package_info_path.read_text(encoding="utf-8"))
  218. package_info["production_integration_commit"] = "f" * 40
  219. package_info_path.write_text(
  220. json.dumps(package_info, indent=2, sort_keys=True) + "\n",
  221. encoding="utf-8",
  222. newline="\n",
  223. )
  224. tampered = prod2u_p4_harness.package_selftest_payload(
  225. package, verification_run_id=run_id
  226. )
  227. self.assertEqual(tampered["status"], "fail")
  228. self.assertTrue(tampered["failures"])
  229. self.assertIn(
  230. "early_manifest_commit_mismatch",
  231. " ".join(tampered["failures"]),
  232. )
  233. def test_unverified_package_matches_prod2u_selftest_contract(self):
  234. with tempfile.TemporaryDirectory() as directory:
  235. root = Path(directory)
  236. bitstream = root / "candidate.bit"
  237. bitstream.write_bytes(b"candidate-bitstream")
  238. package = codex_harness.create_unverified_package(
  239. bitstream=bitstream,
  240. project=PROJECT,
  241. commit=_current_short_commit(),
  242. run_id="b" * 32,
  243. output_root=root / "dist",
  244. )
  245. receipt = prod2u_p4_harness.package_selftest_payload(
  246. package,
  247. verification_run_id="b" * 32,
  248. )
  249. self.assertEqual(receipt["status"], "fail")
  250. self.assertTrue(receipt["failures"])
  251. def test_supplied_receipt_is_copied_with_package_bindings(self):
  252. with tempfile.TemporaryDirectory() as directory:
  253. root = Path(directory)
  254. bitstream = root / "candidate.bit"
  255. bitstream.write_bytes(b"candidate-bitstream")
  256. source_receipt = root / "icarus.json"
  257. source_receipt.write_text('{"schema":"hdl","status":"pass"}\n', encoding="utf-8")
  258. package = codex_harness.create_unverified_package(
  259. bitstream=bitstream,
  260. project=PROJECT,
  261. commit=_current_short_commit(),
  262. run_id="c" * 32,
  263. output_root=root / "dist",
  264. verification_receipts={"icarus": source_receipt},
  265. )
  266. bound = json.loads((package / "evidence/icarus-receipt.json").read_text(encoding="utf-8"))
  267. self.assertEqual(bound["status"], "pass")
  268. self.assertEqual(bound["verification_run_id"], "c" * 32)
  269. self.assertEqual(bound["commit"], _current_full_commit())
  270. self.assertEqual(bound["commit_full"], _current_full_commit())
  271. self.assertRegex(bound["project_config_sha256"], r"^[0-9a-f]{64}$")
  272. self.assertRegex(bound["source_receipt_sha256"], r"^[0-9a-f]{64}$")
  273. def test_build_source_list_and_build_info_use_only_the_new_contract(self):
  274. sources = prod2u_p4_normal_v3_build.PRODUCTION_RTL_SOURCES
  275. self.assertIn("src/common/frame_rx.v", sources)
  276. self.assertIn("src/modules/shared_500us_tick.v", sources)
  277. self.assertIn("src/modules/host_watchdog.v", sources)
  278. self.assertIn("src/top/prod2u_p4_normal_v3_top.v", sources)
  279. self.assertNotIn("src/rtl/binary921_crc16.v", sources)
  280. self.assertNotIn("src/rtl/prod2u_p4_normal_v3_watchdog.v", sources)
  281. with tempfile.TemporaryDirectory() as directory:
  282. bitstream = Path(directory) / "candidate.bit"
  283. bitstream.write_bytes(b"candidate")
  284. info = prod2u_p4_normal_v3_build.create_build_info(
  285. bitstream=bitstream,
  286. commit=_current_short_commit(),
  287. )
  288. self.assertEqual(info["serial"], {"baud": 115200, "format": "8N1", "flow_control": "none"})
  289. self.assertEqual(
  290. info["hardware_access"],
  291. {"com": False, "jtag": False, "programming": False, "drivers": False},
  292. )
  293. def test_source_closure_is_bidirectional_across_manifest_build_and_td_consumers(self):
  294. generated = json.loads(
  295. (ROOT / "src" / "generated" / "td-source-manifest.json").read_text(encoding="utf-8")
  296. )
  297. canonical = generated["sources"]
  298. self.assertEqual(canonical, list(prod2u_p4_normal_v3_build.PRODUCTION_RTL_SOURCES))
  299. self.assertEqual(canonical, codex_harness.build_source_manifest(ROOT)["sources"])
  300. project = ET.parse(ROOT / "td" / "prod2u_p4_normal_v3.prj")
  301. td_project_sources = [
  302. Path(node.attrib["Path"]).as_posix().removeprefix("../")
  303. for node in project.findall(".//Verilog/File")
  304. ]
  305. self.assertEqual(td_project_sources, canonical)
  306. syn = (ROOT / "td" / "syn" / "prod2u_p4_normal_v3.tcl").read_text(encoding="ascii")
  307. tcl_sources = [
  308. Path(match).as_posix()
  309. for match in re.findall(r"analyze\s+-file\s+\{([^}]+)\}", syn)
  310. ]
  311. self.assertEqual(tcl_sources, canonical)
  312. self.assertEqual(len(canonical), len(set(canonical)))
  313. self.assertTrue(all((ROOT / source).is_file() for source in canonical))
  314. def test_runtime_profile_is_the_new_115200_contract(self):
  315. self.assertEqual(
  316. prod2u_p4_harness.RUNTIME_PROFILE,
  317. "prod2u-door-temp-115200-v1",
  318. )
  319. def test_qor_attribute_count_accepts_td_gate_qor_name(self):
  320. with tempfile.TemporaryDirectory() as directory:
  321. root = Path(directory)
  322. (root / "gate.qor").write_text(
  323. "| #async_reg | 6 |\n",
  324. encoding="ascii",
  325. )
  326. observed = prod2u_p4_harness.parse_qor_attribute_count(
  327. root / "prod2u_p4_normal_v3_gate.qor",
  328. "async_reg",
  329. )
  330. self.assertEqual(observed, 6)
  331. def test_verification_inventory_uses_only_the_current_manifest(self):
  332. manifest = prod2u_p4_harness.load_target_manifest()
  333. self.assertEqual(
  334. manifest["schema"],
  335. "prod2u-simple-verification-target-manifest-v1",
  336. )
  337. self.assertEqual(
  338. [target["name"] for target in prod2u_p4_harness.cocotb_tests()],
  339. ["prod2u_door_temp_uart_e2e"],
  340. )
  341. self.assertEqual(
  342. [target["name"] for target in prod2u_p4_harness.formal_targets()],
  343. [
  344. "frame_command_contract",
  345. "frame_tx_contract",
  346. "output_target_ram_contract",
  347. "simple_frame_contract",
  348. "simple_motor_temperature_contract",
  349. "pwm_scheduler_contract",
  350. "host_watchdog_contract",
  351. ],
  352. )
  353. icarus_names = [target["name"] for target in prod2u_p4_harness.hdl_testbenches()]
  354. self.assertEqual(
  355. icarus_names,
  356. sorted(manifest["layers"]["icarus"]),
  357. )
  358. def test_agent_task_report_accepts_unconstrained_observations_and_full_commits(self):
  359. report = _report_base(task_id="capability_probe")
  360. report["result_commit"] = _current_full_commit()
  361. report["read_only_report"] = None
  362. report["owned_paths"] = ["tests/", "config/prod2u_p4_verification_targets_v1.json"]
  363. validated = codex_harness.validate_agent_task_report(
  364. report,
  365. expected_run_id=CONVERGENCE_RUN_ID,
  366. expected_base_commit=ASSIGNED_BASE_COMMIT,
  367. root=ROOT,
  368. )
  369. self.assertEqual(validated["assigned_base_commit"], ASSIGNED_BASE_COMMIT)
  370. self.assertEqual(validated["feature_branch"], CONVERGENCE_BRANCH)
  371. self.assertEqual(validated["worktree"], str(ROOT))
  372. def test_agent_task_report_accepts_read_only_report_with_unexposed_observations(self):
  373. report = _report_base(task_id="repo_process_audit")
  374. report["native_thread"]["observed_model"] = None
  375. report["native_thread"]["observed_reasoning_effort"] = None
  376. validated = codex_harness.validate_agent_task_report(
  377. report,
  378. expected_run_id=CONVERGENCE_RUN_ID,
  379. expected_base_commit=ASSIGNED_BASE_COMMIT,
  380. )
  381. self.assertEqual(validated["read_only_report"], "read-only evidence")
  382. def test_agent_task_report_rejects_stale_assigned_base(self):
  383. report = _report_base(task_id="stale")
  384. report["assigned_base_commit"] = "0" * 40
  385. with self.assertRaisesRegex(ValueError, "stale"):
  386. codex_harness.validate_agent_task_report(
  387. report,
  388. expected_run_id=CONVERGENCE_RUN_ID,
  389. expected_base_commit=ASSIGNED_BASE_COMMIT,
  390. )
  391. def test_agent_task_report_rejects_dependency_mismatch_but_preserves_integration_conflict(self):
  392. report = _report_base(task_id="dependent")
  393. report["dependencies"] = [
  394. {"task_id": "producer", "result_commit": "2" * 40, "integration_commit": None}
  395. ]
  396. report["status"] = "integration_conflict"
  397. report["read_only_report"] = "conflicting paths"
  398. with self.assertRaisesRegex(ValueError, "dependency"):
  399. codex_harness.validate_agent_task_report(
  400. report,
  401. expected_run_id=CONVERGENCE_RUN_ID,
  402. expected_base_commit=ASSIGNED_BASE_COMMIT,
  403. expected_dependencies={
  404. "producer": {"result_commit": "3" * 40, "integration_commit": None}
  405. },
  406. )
  407. report["dependencies"][0]["result_commit"] = "3" * 40
  408. validated = codex_harness.validate_agent_task_report(
  409. report,
  410. expected_run_id=CONVERGENCE_RUN_ID,
  411. expected_base_commit=ASSIGNED_BASE_COMMIT,
  412. expected_dependencies={
  413. "producer": {"result_commit": "3" * 40, "integration_commit": None}
  414. },
  415. )
  416. self.assertEqual(validated["status"], "integration_conflict")
  417. def test_agent_task_report_rejects_duplicate_dependencies(self):
  418. report = _report_base(task_id="duplicate-dependency")
  419. report["dependencies"] = [
  420. {"task_id": "producer", "result_commit": None, "integration_commit": None},
  421. {"task_id": "producer", "result_commit": None, "integration_commit": None},
  422. ]
  423. with self.assertRaisesRegex(ValueError, "duplicate dependency"):
  424. codex_harness.validate_agent_task_report(
  425. report,
  426. expected_run_id=CONVERGENCE_RUN_ID,
  427. expected_base_commit=ASSIGNED_BASE_COMMIT,
  428. )
  429. def test_worktree_identity_rejects_protected_root_wrong_branch_and_wrong_head(self):
  430. with self.assertRaisesRegex(ValueError, "protected_worktree_violation"):
  431. codex_harness.validate_worktree_identity(ROOT, protected_root=ROOT)
  432. with self.assertRaisesRegex(ValueError, "branch"):
  433. codex_harness.validate_worktree_identity(
  434. ROOT, expected_branch=CONVERGENCE_BRANCH + "-wrong"
  435. )
  436. with self.assertRaisesRegex(ValueError, "HEAD"):
  437. codex_harness.validate_worktree_identity(ROOT, expected_head="0" * 40)
  438. def test_agent_task_report_rejects_non_worktree_report_cwd(self):
  439. report = _report_base(task_id="wrong-cwd")
  440. report["worktree"] = str(ROOT / "tools")
  441. with self.assertRaisesRegex(ValueError, "worktree root mismatch"):
  442. codex_harness.validate_agent_task_report(
  443. report,
  444. expected_run_id=CONVERGENCE_RUN_ID,
  445. expected_base_commit=ASSIGNED_BASE_COMMIT,
  446. )
  447. def test_package_rejects_noncanonical_project_input(self):
  448. with tempfile.TemporaryDirectory() as directory:
  449. root = Path(directory)
  450. bitstream = root / "candidate.bit"
  451. bitstream.write_bytes(b"candidate-bitstream")
  452. fake_project = root / "project.json"
  453. fake_project.write_text('{"project_id":"prod2u-door-temp"}\n', encoding="utf-8")
  454. with self.assertRaisesRegex(ValueError, "canonical project"):
  455. codex_harness.create_unverified_package(
  456. bitstream=bitstream,
  457. project=fake_project,
  458. commit=_current_short_commit(),
  459. run_id=CONVERGENCE_RUN_ID,
  460. output_root=root / "dist",
  461. )
  462. def test_name_status_z_parses_rename_and_copy_paths(self):
  463. events = codex_harness.parse_name_status_z(
  464. b"R100\0old.txt\0new.txt\0C100\0source.txt\0copy.txt\0M\0owned.txt\0"
  465. )
  466. self.assertEqual(
  467. events,
  468. [
  469. ("R100", ("old.txt", "new.txt")),
  470. ("C100", ("source.txt", "copy.txt")),
  471. ("M", ("owned.txt",)),
  472. ],
  473. )
  474. def test_owned_paths_require_exact_files_or_explicit_directory_prefixes(self):
  475. events = [("R100", ("tests/unit/old.py", "tests/unit/new.py"))]
  476. self.assertEqual(
  477. codex_harness.validate_changed_paths(events, ["tests/unit/"]),
  478. ["tests/unit/old.py", "tests/unit/new.py"],
  479. )
  480. with self.assertRaisesRegex(ValueError, "owned"):
  481. codex_harness.validate_changed_paths(events, ["tests/unit/old.py"])
  482. with self.assertRaisesRegex(ValueError, "path"):
  483. codex_harness.validate_changed_paths([("M", ("../outside.py",))], ["tests/"])
  484. with self.assertRaisesRegex(ValueError, "glob"):
  485. codex_harness.validate_changed_paths(events, ["tests/*.py"])
  486. def test_agent_receipt_aggregation_preserves_hashes_and_bindings(self):
  487. aggregate = codex_harness.aggregate_agent_receipt_bindings(
  488. {
  489. "unit": {
  490. "verification_run_id": "a" * 32,
  491. "commit": "b" * 40,
  492. "artifact_sha256": "c" * 64,
  493. }
  494. },
  495. expected_run_id="a" * 32,
  496. expected_commit="b" * 40,
  497. )
  498. self.assertEqual(aggregate["receipts"]["unit"]["artifact_sha256"], "c" * 64)
  499. def test_current_commit8_uses_the_git_wrapper(self):
  500. completed = subprocess.CompletedProcess(
  501. args=["git"], returncode=0, stdout=b"0123abcd\n", stderr=b""
  502. )
  503. with patch.object(codex_harness.subprocess, "run", return_value=completed) as run:
  504. self.assertEqual(codex_harness.current_commit8(Path.cwd()), "0123abcd")
  505. args, kwargs = run.call_args
  506. self.assertEqual(args[0][0], "git")
  507. self.assertFalse(kwargs["shell"])
  508. self.assertFalse(kwargs["text"])
  509. self.assertIn("timeout", kwargs)
  510. if __name__ == "__main__":
  511. unittest.main()