from pathlib import Path import pytest from repo_manager.observe import RecordIdentityCollisionError, observe_repository from repo_manager.record_identity import classify_record_id, scan_record_identities from repo_manager.standards import check_repository def _write_mason_workplan(repo: Path, second_uuid: str) -> None: path = repo / "workplans" / "MASON-0001-statehub-bootstrap.md" path.parent.mkdir(parents=True) path.write_text( f"""--- id: MASON-0001 type: workplan title: Bootstrap status: finished state_hub_workstream_id: "11111111-1111-4111-8111-111111111111" --- ## First occurrence ```task id: MASON-0001-T01 status: done priority: high state_hub_task_id: "ede1aa62-fc42-494a-9984-3190a52481af" ``` ## Second occurrence ```task id: MASON-0001-T01 status: done priority: high state_hub_task_id: "{second_uuid}" ``` """, encoding="utf-8", ) def test_grandfathered_mason_identity_is_loaded_from_canon_registry() -> None: assert classify_record_id("workplan", "MASON-0001") == "grandfathered" assert classify_record_id("task", "MASON-0001-T01") == "grandfathered" def test_current_canon_accepts_repository_and_ad_hoc_workplan_forms() -> None: assert classify_record_id("workplan", "RAPP-OPENBAO-WP-0002") == "canonical" assert classify_record_id("task", "RAPP-OPENBAO-WP-0002-T01") == "canonical" assert classify_record_id("workplan", "ACTIVITY-WP-ADHOC-2026-08-23") == "canonical" assert classify_record_id("task", "ACTIVITY-WP-ADHOC-2026-08-23-T01") == "canonical" assert classify_record_id("workplan", "ADHOC-2026-08-23") == "grandfathered" assert classify_record_id("task", "ADHOC-2026-08-23-T01") == "grandfathered" assert classify_record_id("workplan", "RAPP--OPENBAO-WP-0002") is None def test_same_id_and_uuid_indexes_once_with_cleanup_diagnostic(tmp_path: Path) -> None: _write_mason_workplan(tmp_path, "ede1aa62-fc42-494a-9984-3190a52481af") report = scan_record_identities(tmp_path) assert report["identity_collisions"] == [] assert report["duplicate_source_occurrences"][0]["sources"] == [ "workplans/MASON-0001-statehub-bootstrap.md#task-block-1", "workplans/MASON-0001-statehub-bootstrap.md#task-block-2", ] snapshot, index = observe_repository(tmp_path, slug="ops-mason") tasks = [record for record in index.work_records if record.kind == "task"] assert snapshot["index"]["task_count"] == 1 assert len(tasks) == 1 assert ( tasks[0].extra["source_occurrences"] == report["duplicate_source_occurrences"][0]["sources"] ) assert index.events[0]["type"] == "repo.work_record.duplicate_source_occurrence" conformance = check_repository(tmp_path, slug="ops-mason") codes = [finding.code for finding in conformance.findings] assert "work-record-duplicate-source-occurrence" in codes assert not any( finding.code == "work-record-id-invalid" and finding.path.startswith("workplans/") for finding in conformance.findings ) def test_same_id_with_different_uuids_fails_closed(tmp_path: Path) -> None: _write_mason_workplan(tmp_path, "22222222-2222-4222-8222-222222222222") report = scan_record_identities(tmp_path) assert report["duplicate_source_occurrences"] == [] assert report["identity_collisions"][0]["id"] == "MASON-0001-T01" with pytest.raises(RecordIdentityCollisionError): observe_repository(tmp_path, slug="ops-mason") conformance = check_repository(tmp_path, slug="ops-mason") assert any(finding.code == "work-record-identity-collision" for finding in conformance.findings)