#!/usr/bin/env python3 from __future__ import annotations import argparse import json import pathlib import tempfile import unittest from unittest import mock import persona_limb_eye import persona_reflex_arc class PersonaReflexArcTest(unittest.TestCase): def args(self, root: pathlib.Path, ground: pathlib.Path, spec: pathlib.Path, task_id: str) -> argparse.Namespace: return argparse.Namespace( persona="ICE-P-ZY001", task_id=task_id, task="create the exact expected result without changing target.json", spec=str(spec), ground=str(ground), case_root=str(root / "cases"), host="codex", runtime="auto", access="read-only", attempt=1, ) @staticmethod def methods() -> dict: return { "codex": {"available": True}, "zcode": {"available": True}, "qwen": {"available": True}, } def test_reflex_returns_to_parent_brain_and_only_brain_closes_done(self): with tempfile.TemporaryDirectory() as directory: root = pathlib.Path(directory) ground = root / "ground" ground.mkdir() (ground / "target.json").write_text('{"keep": true}\n', encoding="utf-8") spec = root / "spec.json" spec.write_text(json.dumps({ "unchanged": ["target.json"], "expect": [{"file": "result.json", "field": "arc", "equals": "PASS"}], }), encoding="utf-8") def fake_hand(*_args, **_kwargs): (ground / "result.json").write_text('{"arc": "PASS"}\n', encoding="utf-8") receipt = {"schema": "guanghu.persona-execution-limb-runtime-receipt/v1", "outcome": "PASS"} return {"exit_code": 0, "stdout": json.dumps(receipt), "stderr": "", "elapsed_ms": 1} with mock.patch.object(persona_reflex_arc, "admit"), \ mock.patch.object(persona_reflex_arc, "probe_methods", return_value=self.methods()), \ mock.patch.object(persona_reflex_arc, "hand", side_effect=fake_hand): returned = persona_reflex_arc.run_arc(self.args(root, ground, spec, "ARC-PASS")) self.assertEqual(returned["state"], "AWAITING_PARENT_BRAIN_VERDICT") self.assertEqual(returned["recommended_decision"], "TASK_DONE") case = pathlib.Path(returned["case_dir"]) before = persona_reflex_arc.status(case) self.assertEqual(before["state"], "AWAITING_PARENT_BRAIN_VERDICT") verdict = persona_reflex_arc.finalize(argparse.Namespace( case=str(case), persona="ICE-P-ZY001", decision="TASK_DONE", reason="parent brain accepts hand and eye evidence", )) self.assertEqual(verdict["decision"], "TASK_DONE") self.assertEqual(persona_reflex_arc.status(case)["state"], "TASK_DONE") def test_failed_world_witness_cannot_be_closed_as_done(self): with tempfile.TemporaryDirectory() as directory: root = pathlib.Path(directory) ground = root / "ground" ground.mkdir() (ground / "target.json").write_text('{"keep": true}\n', encoding="utf-8") spec = root / "spec.json" spec.write_text(json.dumps({ "unchanged": ["target.json"], "expect": [{"file": "missing.json", "field": "arc", "equals": "PASS"}], }), encoding="utf-8") receipt = {"schema": "guanghu.persona-execution-limb-runtime-receipt/v1", "outcome": "PASS"} fake = {"exit_code": 0, "stdout": json.dumps(receipt), "stderr": "", "elapsed_ms": 1} with mock.patch.object(persona_reflex_arc, "admit"), \ mock.patch.object(persona_reflex_arc, "probe_methods", return_value=self.methods()), \ mock.patch.object(persona_reflex_arc, "hand", return_value=fake): returned = persona_reflex_arc.run_arc(self.args(root, ground, spec, "ARC-REPAIR")) self.assertEqual(returned["recommended_decision"], "REPAIR_NEEDED") case = pathlib.Path(returned["case_dir"]) with self.assertRaisesRegex(ValueError, "CANNOT_CLOSE"): persona_reflex_arc.finalize(argparse.Namespace( case=str(case), persona="ICE-P-ZY001", decision="TASK_DONE", reason="must be rejected", )) repaired = persona_reflex_arc.finalize(argparse.Namespace( case=str(case), persona="ICE-P-ZY001", decision="REPAIR_NEEDED", reason="expected file was not seen", )) self.assertEqual(repaired["decision"], "REPAIR_NEEDED") def test_room_eye_observes_shared_organs_without_claiming_identity(self): value = persona_limb_eye.room() self.assertFalse(value["reads_hand_stdout"]) self.assertFalse(value["ground_write"]) self.assertEqual(value["identity_decision"], "NOT_PERFORMED_BY_EYE") self.assertIn("reflex_arc", value["room_report"]["container_paths"]) self.assertIn("persona_time_system", value["room_report"]["container_paths"]) if __name__ == "__main__": unittest.main()