from pathlib import Path from git import Repo from afk_developer.event_store import DurableEventStore from afk_developer.scheduler.quarantine import ( quarantine_unreported_untracked, reconcile_completed_quarantine_effects, ) def _repo_with_commit(path: Path) -> Repo: repo = Repo.init(path, initial_branch="main") with repo.config_writer() as config: config.set_value("user", "name", "AFK Test") config.set_value("user", "email", "afk-test@example.invalid") (path / "tracked.py").write_text("value = 1\n", encoding="utf-8") repo.index.add(["tracked.py"]) repo.index.commit("base") return repo def test_quarantine_moves_only_unreported_untracked_files_with_hashes(tmp_path) -> None: _repo_with_commit(tmp_path) reported = tmp_path / "feature.py" scratch = tmp_path / "scratch_probe.py" reported.write_text("feature = True\n", encoding="utf-8") scratch.write_text("print('probe')\n", encoding="utf-8") result = quarantine_unreported_untracked( tmp_path, ["feature.py"], tmp_path.parent / "quarantine" / "task-move" ) assert result.passed assert [item.project_path for item in result.moved] == ["scratch_probe.py"] assert not scratch.exists() assert reported.exists() artifact = result.moved[0] assert artifact.quarantine_path.read_text(encoding="utf-8") == "print('probe')\n" assert artifact.sha256 in artifact.quarantine_path.name def test_existing_mismatched_quarantine_target_blocks_without_removing_source(tmp_path) -> None: _repo_with_commit(tmp_path) scratch = tmp_path / "scratch_probe.py" scratch.write_text("print('probe')\n", encoding="utf-8") quarantine = tmp_path.parent / "quarantine" / "task-collision" quarantine.mkdir(parents=True, exist_ok=True) import hashlib digest = hashlib.sha256(scratch.read_bytes()).hexdigest() (quarantine / f"{digest}-scratch_probe.py").write_text("wrong\n", encoding="utf-8") result = quarantine_unreported_untracked(tmp_path, [], quarantine) assert not result.passed assert result.blocked_paths == ["scratch_probe.py"] assert scratch.exists() def test_quarantine_emits_intent_before_and_result_after_removal(tmp_path) -> None: _repo_with_commit(tmp_path) (tmp_path / "scratch_probe.py").write_text("print('probe')\n", encoding="utf-8") calls: list[tuple[str, str]] = [] result = quarantine_unreported_untracked( tmp_path, [], tmp_path.parent / "quarantine" / "task-hooks", before_move=lambda item: calls.append(("intent", item.project_path)), after_move=lambda item: calls.append(("result", item.project_path)), ) assert result.passed assert calls == [("intent", "scratch_probe.py"), ("result", "scratch_probe.py")] def test_reconciliation_only_recognizes_a_proven_completed_quarantine(tmp_path) -> None: source = tmp_path / "scratch_probe.py" source.write_text("print('probe')\n", encoding="utf-8") quarantine = tmp_path / "quarantine.py" quarantine.write_bytes(source.read_bytes()) source.unlink() import hashlib store = DurableEventStore(tmp_path / "governance-events.jsonl") store.append( "effect_intended", { "effect_id": "quarantine:task:hash", "operation": "quarantine", "source_path": str(source), "quarantine_path": str(quarantine), "sha256": hashlib.sha256(quarantine.read_bytes()).hexdigest(), }, ) assert reconcile_completed_quarantine_effects(store) == ["quarantine:task:hash"] def test_reconciliation_never_removes_an_ambiguous_remaining_source(tmp_path) -> None: source = tmp_path / "scratch_probe.py" source.write_text("print('probe')\n", encoding="utf-8") store = DurableEventStore(tmp_path / "governance-events.jsonl") store.append( "effect_intended", { "effect_id": "quarantine:task:hash", "operation": "quarantine", "source_path": str(source), "quarantine_path": str(tmp_path / "missing-copy.py"), "sha256": "a" * 64, }, ) assert reconcile_completed_quarantine_effects(store) == [] assert source.exists()