from app.task_plane.governance_checkpoint import ( JsonCheckpointStore, RecoveryCheckpoint, RetryBudget ) def checkpoint(**kw): body=dict( schema="aios.governance-recovery-checkpoint.v1", goal_id="g",repository="aios/aios",source_oid="base", candidate_digest="cand",task_id=None,mission_id=None,root_task_id=None, parent_task_id=None,principal_id=None,blocker_code="TASK_NOT_RESOLVED", blocker_fingerprint="fp",next_legal_action="WAIT_DEPENDENCY", last_verified_at="2026-09-16T00:00:00Z",evidence_refs=("e",), unchanged_cycles=1,mechanism_repair_required=False, ) body.update(kw) return RecoveryCheckpoint(**body) def test_checkpoint_roundtrip_is_atomic_observation_cache(tmp_path): store=JsonCheckpointStore(tmp_path/"checkpoint.json") cp=checkpoint() store.save(cp) loaded=store.load() assert loaded==cp assert len(loaded.digest)==64 assert not (tmp_path/"checkpoint.json.tmp").exists() def test_same_resolver_blocker_escalates_after_three_cycles(): budget=RetryBudget(unchanged_threshold=3) fp=budget.blocker_fingerprint("TASK_NOT_RESOLVED",("same",)) cycles,repair=budget.observe(previous=None,blocker_code="TASK_NOT_RESOLVED",blocker_fingerprint=fp) assert (cycles,repair)==(1,False) prev=checkpoint(blocker_fingerprint=fp,unchanged_cycles=1) cycles,repair=budget.observe(previous=prev,blocker_code="TASK_NOT_RESOLVED",blocker_fingerprint=fp) assert (cycles,repair)==(2,False) prev=checkpoint(blocker_fingerprint=fp,unchanged_cycles=2) cycles,repair=budget.observe(previous=prev,blocker_code="TASK_NOT_RESOLVED",blocker_fingerprint=fp) assert (cycles,repair)==(3,True) def test_external_write_gate_does_not_fake_mechanism_repair(): budget=RetryBudget(unchanged_threshold=3) fp=budget.blocker_fingerprint("GOVERNED_WRITES_DISABLED",("same",)) prev=checkpoint(blocker_code="GOVERNED_WRITES_DISABLED",blocker_fingerprint=fp,unchanged_cycles=9) cycles,repair=budget.observe(previous=prev,blocker_code="GOVERNED_WRITES_DISABLED",blocker_fingerprint=fp) assert cycles==10 and repair is False def test_changed_blocker_resets_cycle(): budget=RetryBudget() prev=checkpoint(unchanged_cycles=8) fp=budget.blocker_fingerprint("CALLER_PRINCIPAL_UNRESOLVED",("new",)) assert budget.observe(previous=prev,blocker_code="CALLER_PRINCIPAL_UNRESOLVED",blocker_fingerprint=fp)==(1,False)