diff --git a/kits/profile-dev-kit/README.md b/kits/profile-dev-kit/README.md index 5379716..5ef7028 100644 --- a/kits/profile-dev-kit/README.md +++ b/kits/profile-dev-kit/README.md @@ -7,11 +7,11 @@ the required environment keys are present (key names only, never values). Status: Story 5.2. Entry workflows `verify-change`, `read-report`, `ratify-inventory`, `declare-non-applicability`, `record-approval-decision`, -`approve-change` and `read-approval`; tools `profile_preflight`, +`approve-change`, `read-approval` and `publish-reverification`; tools `profile_preflight`, `profile_ratify_inventory`, `profile_bind_inventory`, `profile_record_change`, `profile_derive_impact`, `profile_open_attempt`, `profile_run_gate`, `profile_run_review`, `profile_capture_evidence`, `profile_record_report`, -`profile_read_report`, `profile_declare_non_applicability`, +`profile_read_report`, `profile_read_reverification`, `profile_declare_non_applicability`, `profile_record_approval_decision`, `profile_approve_change` and `profile_read_approval`. @@ -24,8 +24,11 @@ python/beddel_profile_dev/tools/approval.py Approval verification, persistence and read-back python/beddel_profile_dev/tools/approval_decision.py Approval decision transport +python/beddel_profile_dev/tools/publication.py + Read-only Re-verification copy source workflows/ entry workflows, including - record-approval-decision and read-approval + record-approval-decision, read-approval and + publish-reverification tests/ kit tests (no network, no real credential) ``` @@ -401,3 +404,25 @@ then `decision.commit_invalid`, `decision.unreachable`, `decision.absent`, Every refusal raises `PROFILE_APPROVAL_REFUSED` and writes nothing. An invalid repository path or records root keeps `PROFILE_REPOSITORY_INVALID`. Ratification and Approval remain separate human acts. + +## Re-verification copy (Story 7.1b) + +`publish-reverification` takes `repo_path` and `change_id`. It first invokes +`profile_read_reverification`, which is read-only and refuses in this order: +`id.invalid`, `change.unrecorded`, `report.malformed`, `report.absent`, +`reverification.not_recorded` for an older schema, +`report.malformed` for an invalid latest record, `digest.mismatch`, and +`reverification.not_recorded` for a non-recorded status. Every refusal raises `PROFILE_PUBLICATION_REFUSED` and +leaves the records tree unchanged. The returned `reverification` value is the +whole object parsed from the latest stored Report; it is not rebuilt. + +The workflow's copy step is skip-on-error. A failed copy appears only in the +run's `_absorbed_errors` as its step identity and stable error code. It never +changes the Verification Report or creates a publication record. The tracked +workflow uses the neutral key `copy_destination`, the placeholder endpoint +`https://copy-destination.invalid/mcp`, the placeholder tool `write_copy` and +the `entry` argument. The operator copy, kept outside every repository, +replaces those placeholders, adds any literal arguments and the matching +qualified `allowed_tools` entry. Consent is obtained outside a run with +`python -m beddel_protocol_mcp.authorize`. Nothing returned by the copy is +read back or stored. diff --git a/kits/profile-dev-kit/kit.yaml b/kits/profile-dev-kit/kit.yaml index a00f901..65ff2c1 100644 --- a/kits/profile-dev-kit/kit.yaml +++ b/kits/profile-dev-kit/kit.yaml @@ -77,6 +77,10 @@ tools: target: "beddel_profile_dev.tools.report:profile_read_report" description: "Read the latest persisted Verification Report and its append-only history" category: profile + - name: profile_read_reverification + target: "beddel_profile_dev.tools.publication:profile_read_reverification" + description: "Read the recorded Re-verification entry of the latest stored Report without writing records" + category: profile - name: profile_declare_non_applicability target: "beddel_profile_dev.tools.determine:profile_declare_non_applicability" description: "Record the maintainer's determination that an obligation with an absent target is not applicable (transport only)" @@ -101,6 +105,9 @@ workflows: - name: read-report path: workflows/read-report.yaml description: Reads the latest persisted Verification Report and its history without running verification or writing records + - name: publish-reverification + path: workflows/publish-reverification.yaml + description: Publishes a one-way copy of the recorded Re-verification entry through the configured MCP destination; never blocks a verification - name: ratify-inventory path: workflows/ratify-inventory.yaml description: Records the ratification of an inventory version @@ -145,6 +152,8 @@ targets: target: "beddel_profile_dev.tools.report:profile_record_report" - name: profile_read_report target: "beddel_profile_dev.tools.report:profile_read_report" + - name: profile_read_reverification + target: "beddel_profile_dev.tools.publication:profile_read_reverification" - name: profile_declare_non_applicability target: "beddel_profile_dev.tools.determine:profile_declare_non_applicability" - name: profile_approve_change diff --git a/kits/profile-dev-kit/python/beddel_profile_dev/_errors.py b/kits/profile-dev-kit/python/beddel_profile_dev/_errors.py index 1f52754..32cbaab 100644 --- a/kits/profile-dev-kit/python/beddel_profile_dev/_errors.py +++ b/kits/profile-dev-kit/python/beddel_profile_dev/_errors.py @@ -26,6 +26,7 @@ "PROFILE_INVENTORY_INTEGRITY_BLOCK", "PROFILE_INVENTORY_UNRATIFIED", "PROFILE_PREFLIGHT_FAILED", + "PROFILE_PUBLICATION_REFUSED", "PROFILE_RATIFICATION_REFUSED", "PROFILE_REPOSITORY_INVALID", "PROFILE_REVIEW_UNSCOPED", @@ -47,6 +48,9 @@ PROFILE_PREFLIGHT_FAILED = "PROFILE_PREFLIGHT_FAILED" """Raised when one or more preflight checks fail.""" +PROFILE_PUBLICATION_REFUSED = "PROFILE_PUBLICATION_REFUSED" +"""Raised when no recorded Re-verification can be read for publication (nothing is written).""" + PROFILE_RATIFICATION_REFUSED = "PROFILE_RATIFICATION_REFUSED" """Raised when an inventory version cannot be ratified (nothing is written).""" diff --git a/kits/profile-dev-kit/python/beddel_profile_dev/tools/publication.py b/kits/profile-dev-kit/python/beddel_profile_dev/tools/publication.py new file mode 100644 index 0000000..87cacd6 --- /dev/null +++ b/kits/profile-dev-kit/python/beddel_profile_dev/tools/publication.py @@ -0,0 +1,110 @@ +"""Read-only access to a recorded Re-verification for publication.""" + +from __future__ import annotations + +import hashlib +from typing import Any + +from beddel.tools import beddel_tool + +from beddel_profile_dev._errors import PROFILE_PUBLICATION_REFUSED, ProfileError +from beddel_profile_dev._io import real_repo_root, records_root +from beddel_profile_dev.config import load_config +from beddel_profile_dev.inventory import is_valid_id +from beddel_profile_dev.tools.report import ( + _DIGEST, + REPORT_SCHEMA, + _Reader, + _report_history, + _valid_change, +) + +__all__ = ["profile_read_reverification"] + + +def _valid_latest_report(report: dict[str, Any]) -> bool: + """Check the fields of a stored Report that the digest re-read relies on. + + Kept local: the structural boundary test keeps every module but the + decision modules free of any import of them. + """ + if "attempt" not in report: + return False + attempt = report.get("attempt") + if attempt is not None and (type(attempt) is not int or attempt < 1): + return False + reads = report.get("reads") + if not isinstance(reads, list): + return False + if any( + not isinstance(item, dict) + or set(item) != {"ref", "sha256"} + or not isinstance(item["ref"], str) + or not isinstance(item["sha256"], str) + or _DIGEST.fullmatch(item["sha256"]) is None + for item in reads + ): + return False + return report.get("verdict") in {"consistent", "has pendency"} + + +def _refuse(reason: str) -> ProfileError: + return ProfileError( + PROFILE_PUBLICATION_REFUSED, + f"publication refused: {reason}", + {"reason": reason}, + ) + + +@beddel_tool( + name="profile_read_reverification", + description="Read the recorded Re-verification entry of the latest stored Report without writing records", + category="profile", +) +def profile_read_reverification(repo_path: str, change_id: str) -> dict[str, Any]: + """Read the latest recorded Re-verification without changing records.""" + if not is_valid_id(change_id): + raise _refuse("id.invalid") + + repo = real_repo_root(repo_path) + records = records_root(repo, load_config().records_dir) + reader = _Reader(records) + if not _valid_change(reader.json(f"changes/{change_id}/change.json"), change_id): + raise _refuse("change.unrecorded") + + try: + history = _report_history(reader, records, change_id) + except (ProfileError, OSError, ValueError, RecursionError): + raise _refuse("report.malformed") from None + if not history: + raise _refuse("report.absent") + + ordinal, latest = history[-1] + if latest.get("schema") != REPORT_SCHEMA: + raise _refuse("reverification.not_recorded") + if not _valid_latest_report(latest): + raise _refuse("report.malformed") + + for item in latest["reads"]: + try: + raw = reader.read(item["ref"]) + except (ProfileError, OSError, ValueError): + raise _refuse("digest.mismatch") from None + if raw is None or hashlib.sha256(raw).hexdigest() != item["sha256"]: + raise _refuse("digest.mismatch") + + reverification = latest.get("reverification") + if ( + not isinstance(reverification, dict) + or reverification.get("status") != "recorded" + ): + raise _refuse("reverification.not_recorded") + + report_ref = f"changes/{change_id}/report/{ordinal}.json" + return { + "ok": True, + "change_id": change_id, + "report": report_ref, + "report_sha256": reader.reads[report_ref], + "reverification": reverification, + } diff --git a/kits/profile-dev-kit/tests/test_manifest.py b/kits/profile-dev-kit/tests/test_manifest.py index 4a85a57..f4ccaea 100644 --- a/kits/profile-dev-kit/tests/test_manifest.py +++ b/kits/profile-dev-kit/tests/test_manifest.py @@ -25,6 +25,7 @@ "profile_capture_evidence", "profile_record_report", "profile_read_report", + "profile_read_reverification", "profile_declare_non_applicability", "profile_approve_change", "profile_record_approval_decision", @@ -96,6 +97,7 @@ def test_manifest_declares_all_workflows_and_python_targets() -> None: assert [w["name"] for w in manifest["workflows"]] == [ "verify-change", "read-report", + "publish-reverification", "ratify-inventory", "declare-non-applicability", "approve-change", diff --git a/kits/profile-dev-kit/tests/test_neutrality.py b/kits/profile-dev-kit/tests/test_neutrality.py new file mode 100644 index 0000000..8a80591 --- /dev/null +++ b/kits/profile-dev-kit/tests/test_neutrality.py @@ -0,0 +1,63 @@ +"""Neutral tracked workflow and configured-term checks.""" + +from __future__ import annotations + +import os +import subprocess +from pathlib import Path +from urllib.parse import urlsplit + +import pytest +import yaml + +KIT_ROOT = Path(__file__).resolve().parents[1] + + +def _tracked_text_files(root: Path) -> list[Path]: + output = subprocess.check_output( + ["git", "-C", str(root), "ls-files", "-z"], text=False + ) + return [ + root / relative + for relative in output.decode().split("\0") + if relative and (root / relative).is_file() + ] + + +def test_untracked_destination_terms_are_absent() -> None: + terms_path = os.environ.get("BEDDEL_GUARD_TERMS_FILE") + if not terms_path or not Path(terms_path).is_file(): + pytest.skip("BEDDEL_GUARD_TERMS_FILE is not configured") + terms = [ + line.strip() + for line in Path(terms_path).read_text().splitlines() + if line.strip() + ] + if not terms: + pytest.fail("BEDDEL_GUARD_TERMS_FILE contains no terms") + root = Path( + subprocess.check_output( + ["git", "-C", str(KIT_ROOT), "rev-parse", "--show-toplevel"], + text=True, + ).strip() + ) + hits: list[str] = [] + for path in _tracked_text_files(root): + try: + text = path.read_text() + except UnicodeDecodeError: + continue + folded = text.casefold() + for index, term in enumerate(terms): + if term.casefold() in folded: + hits.append(f"{path}: listed term #{index}") + assert hits == [] + + +def test_tracked_workflow_uses_a_non_routable_placeholder() -> None: + workflow = yaml.safe_load( + (KIT_ROOT / "workflows" / "publish-reverification.yaml").read_text() + ) + endpoint = workflow["mcp_servers"]["copy_destination"]["endpoint"] + host = urlsplit(endpoint).hostname + assert host is not None and host.endswith(".invalid") diff --git a/kits/profile-dev-kit/tests/test_publication.py b/kits/profile-dev-kit/tests/test_publication.py new file mode 100644 index 0000000..ff918c8 --- /dev/null +++ b/kits/profile-dev-kit/tests/test_publication.py @@ -0,0 +1,176 @@ +"""Read-only publication source tests for Story 7.1b.""" + +from __future__ import annotations + +import hashlib +import json +from pathlib import Path + +import pytest +from beddel_profile_dev._errors import PROFILE_PUBLICATION_REFUSED, ProfileError +from beddel_profile_dev._io import canonical_json, records_root +from beddel_profile_dev.tools.publication import profile_read_reverification +from beddel_profile_dev.tools.report import profile_read_report, profile_record_report + +from ._helpers import change_fixture +from .test_report import ( + _prepare, + _prepare_next_revision, + _review_divergence_output, + _run_results, +) + + +def _records_snapshot(root: Path) -> dict[str, bytes]: + if not root.exists(): + return {} + return { + str(path.relative_to(root)): path.read_bytes() + for path in root.rglob("*") + if path.is_file() + } + + +def _recorded_publication_fixture( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> tuple[Path, Path, dict[str, object]]: + repo, base, first_revision = _prepare(tmp_path) + _run_results(repo, first_revision, monkeypatch, _review_divergence_output()) + profile_record_report(str(repo), "c1", first_revision) + second_revision = _prepare_next_revision(repo, base) + _run_results(repo, second_revision, monkeypatch) + written = profile_record_report(str(repo), "c1", second_revision) + records = records_root(repo, "records/profile-dev") + return repo, records, json.loads((records / written["report"]).read_text()) + + +def _assert_refusal(repo: Path, reason: str, records: Path | None = None) -> None: + before = _records_snapshot(records) if records is not None else None + with pytest.raises(ProfileError) as error: + profile_read_reverification(str(repo), "c1") + assert error.value.code == PROFILE_PUBLICATION_REFUSED + assert error.value.details == {"reason": reason} + assert error.value.message == f"publication refused: {reason}" + if records is not None: + assert _records_snapshot(records) == before + + +def test_returns_the_stored_reverification_without_writing( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + repo, records, stored = _recorded_publication_fixture(tmp_path, monkeypatch) + before = _records_snapshot(records) + + result = profile_read_reverification(str(repo), "c1") + report = profile_read_report(str(repo), "c1") + + assert canonical_json(result["reverification"]) == canonical_json( + report["report"]["reverification"] + ) + assert canonical_json(result["reverification"]) == canonical_json( + stored["reverification"] + ) + assert result["report"] == "changes/c1/report/2.json" + assert ( + result["report_sha256"] + == hashlib.sha256((records / result["report"]).read_bytes()).hexdigest() + ) + assert _records_snapshot(records) == before + + +def test_refuses_invalid_id_before_repository_access(tmp_path: Path) -> None: + with pytest.raises(ProfileError) as error: + profile_read_reverification(str(tmp_path / "absent"), "not valid") + assert error.value.code == PROFILE_PUBLICATION_REFUSED + assert error.value.details == {"reason": "id.invalid"} + + +def test_refuses_when_change_is_unrecorded_and_creates_no_directory( + tmp_path: Path, +) -> None: + repo, _, _ = change_fixture(tmp_path, lambda path: None) + records = records_root(repo, "records/profile-dev") + assert not records.exists() + _assert_refusal(repo, "change.unrecorded", records) + assert not records.exists() + + +def test_refuses_when_report_is_absent(tmp_path: Path) -> None: + repo, _, _ = _prepare(tmp_path) + _assert_refusal(repo, "report.absent", records_root(repo, "records/profile-dev")) + + +def test_refuses_not_applicable_reverification(tmp_path: Path) -> None: + repo, _, revision = _prepare(tmp_path) + profile_record_report(str(repo), "c1", revision) + _assert_refusal( + repo, "reverification.not_recorded", records_root(repo, "records/profile-dev") + ) + + +def test_refuses_older_report_schema(tmp_path: Path) -> None: + repo, _, _ = _prepare(tmp_path) + report_dir = records_root(repo, "records/profile-dev") / "changes/c1/report" + report_dir.mkdir() + (report_dir / "1.json").write_text( + json.dumps( + { + "schema": "profile-dev/verification-report/1", + "record": 1, + "change_id": "c1", + "revision": "a" * 40, + } + ) + ) + _assert_refusal( + repo, "reverification.not_recorded", report_dir.parent.parent.parent + ) + + +def test_refuses_malformed_latest_report_without_writing( + tmp_path: Path, +) -> None: + repo, _, _ = _prepare(tmp_path) + report_dir = records_root(repo, "records/profile-dev") / "changes/c1/report" + report_dir.mkdir() + (report_dir / "1.json").write_text("not json") + _assert_refusal(repo, "report.malformed", report_dir.parent.parent.parent) + + +def test_refuses_invalid_latest_reads( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + repo, records, _ = _recorded_publication_fixture(tmp_path, monkeypatch) + report_path = records / "changes/c1/report/2.json" + report = json.loads(report_path.read_text()) + report["reads"] = [{"ref": "not-a-record", "sha256": "bad"}] + report_path.write_text(json.dumps(report)) + _assert_refusal(repo, "report.malformed", records) + + +def test_refuses_when_a_report_read_digest_changes( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + repo, records, stored = _recorded_publication_fixture(tmp_path, monkeypatch) + ref = next( + item["ref"] + for item in stored["reads"] + if "/report/" not in item["ref"] and not item["ref"].endswith("change.json") + ) + (records / ref).write_bytes(b"changed\n") + _assert_refusal(repo, "digest.mismatch", records) + + +def test_source_is_read_only_and_has_no_protocol_client_logic() -> None: + source = ( + Path(__file__).parents[1] / "python/beddel_profile_dev/tools/publication.py" + ) + text = source.read_text() + assert "mcp" not in text.lower() + assert "call_tool" not in text + assert "write_create_only" not in text + assert "open(" not in text + module_root = source.parents[1] + assert all( + "mcp" not in path.read_text().lower() for path in module_root.rglob("*.py") + ) diff --git a/kits/profile-dev-kit/tests/test_publish_cli.py b/kits/profile-dev-kit/tests/test_publish_cli.py new file mode 100644 index 0000000..4cc69e3 --- /dev/null +++ b/kits/profile-dev-kit/tests/test_publish_cli.py @@ -0,0 +1,469 @@ +"""CLI publication workflow tests with an in-process fake protocol kit.""" + +from __future__ import annotations + +import json +import os +import shutil +import subprocess +from collections.abc import Callable +from pathlib import Path +from typing import Any + +import pytest +import yaml +from beddel_profile_dev._io import records_root +from beddel_profile_dev.tools.report import profile_record_report + +from ._helpers import change_fixture +from .test_publication import _recorded_publication_fixture, _records_snapshot +from .test_report import _prepare +from .test_run_cli import _beddel_argv + +KIT_ROOT = Path(__file__).resolve().parent.parent +PUBLISH = KIT_ROOT / "workflows" / "publish-reverification.yaml" + +_FAKE_KIT_YAML = """\ +name: protocol-mcp-kit +version: 0.0.0 +description: Fake protocol kit for publication tests +author: tests +adapters: +- port: IMCPClient + implementation: StreamableHTTPMCPClient + description: fake +targets: + python: + status: implemented + module: beddel_protocol_mcp + dependencies: [] + adapters: + - port: IMCPClient + name: mcp-streamable-http + target: beddel_protocol_mcp.http_client:StreamableHTTPMCPClient +""" + +_FAKE_INIT = """\ +from beddel_protocol_mcp.http_client import StreamableHTTPMCPClient +from beddel_protocol_mcp.schema_validator import validate_tool_arguments + +__all__ = ["StreamableHTTPMCPClient", "validate_tool_arguments"] +""" + +_FAKE_CLIENT = """\ +import asyncio +import json +import os +from pathlib import Path + +from beddel.domain.errors import MCPError +from beddel.error_codes import ( + MCP_AUTHORIZATION_REQUIRED, + MCP_CONNECTION_FAILED, + MCP_SCHEMA_VALIDATION_FAILED, + MCP_TOOL_INVOCATION_FAILED, +) + + +class StreamableHTTPMCPClient: + def __init__(self, *, transport, endpoint, authorization): + self.endpoint = endpoint + + async def list_tools(self): + mode = os.environ.get("FAKE_MCP_MODE", "ok") + if mode == "unreachable": + raise MCPError(MCP_CONNECTION_FAILED, "FAKE-MCP-MARKER") + if mode == "token": + raise MCPError(MCP_AUTHORIZATION_REQUIRED, "FAKE-MCP-MARKER") + return [{ + "name": "write_copy", + "inputSchema": { + "type": "object", + "properties": {"entry": {"type": "object"}}, + "required": ["entry"], + }, + }] + + async def call_tool(self, name, arguments): + log = os.environ.get("FAKE_MCP_LOG") + if log: + with Path(log).open("a", encoding="utf-8") as handle: + handle.write(json.dumps({"tool": name, "arguments": arguments}) + "\\n") + mode = os.environ.get("FAKE_MCP_MODE", "ok") + if mode == "unreachable": + raise MCPError(MCP_CONNECTION_FAILED, "FAKE-MCP-MARKER") + if mode == "token": + raise MCPError(MCP_AUTHORIZATION_REQUIRED, "FAKE-MCP-MARKER") + if mode == "rejected": + raise MCPError(MCP_SCHEMA_VALIDATION_FAILED, "FAKE-MCP-MARKER") + if mode == "timeout": + await asyncio.sleep(30) + if mode == "server_error": + raise MCPError(MCP_TOOL_INVOCATION_FAILED, "FAKE-MCP-MARKER") + return {"ok": True} + + async def disconnect(self): + return None +""" + +_FAKE_VALIDATOR = """\ +import os + +from beddel.domain.errors import MCPError +from beddel.error_codes import MCP_SCHEMA_VALIDATION_FAILED + + +def validate_tool_arguments(arguments, schema): + if os.environ.get("FAKE_MCP_MODE") == "rejected": + raise MCPError(MCP_SCHEMA_VALIDATION_FAILED, "FAKE-MCP-MARKER") + if "entry" not in arguments: + raise MCPError(MCP_SCHEMA_VALIDATION_FAILED, "FAKE-MCP-MARKER") +""" + + +def _kit_copy(tmp_path: Path) -> Path: + kits_dir = tmp_path / "kits" + shutil.copytree(KIT_ROOT, kits_dir / "profile-dev-kit") + return kits_dir + + +def _write_fake_protocol_kit(kits_dir: Path) -> None: + package = kits_dir / "protocol-mcp-kit" / "python" / "beddel_protocol_mcp" + package.mkdir(parents=True) + (package / "__init__.py").write_text(_FAKE_INIT) + (package / "http_client.py").write_text( + "from ._client import StreamableHTTPMCPClient\n" + ) + (package / "_client.py").write_text(_FAKE_CLIENT) + (package / "schema_validator.py").write_text(_FAKE_VALIDATOR) + (kits_dir / "protocol-mcp-kit" / "kit.yaml").write_text(_FAKE_KIT_YAML) + + +def _configure_home(tmp_path: Path, kits_dir: Path) -> Path: + home = tmp_path / "home" + config = home / ".config" / "beddel" + config.mkdir(parents=True, exist_ok=True) + (config / "config.json").write_text(json.dumps({"kits_paths": [str(kits_dir)]})) + return home + + +def _run_json( + workflow: Path, + inputs: dict[str, str], + tmp_path: Path, + kits_dir: Path, + **extra_env: str, +) -> subprocess.CompletedProcess[str]: + home = _configure_home(tmp_path, kits_dir) + env = { + key: value + for key, value in os.environ.items() + if not key.startswith(("GIT_", "BEDDEL_", "PYTHON")) and key != "KIRO_API_KEY" + } + env.update({"HOME": str(home), "XDG_CONFIG_HOME": str(home / ".config")}) + env.update(extra_env) + args = [arg for key, value in inputs.items() for arg in ("-i", f"{key}={value}")] + return subprocess.run( + [*_beddel_argv(), "run", "--json-output", *args, str(workflow)], + capture_output=True, + text=True, + cwd=tmp_path, + env=env, + timeout=60, + check=False, + ) + + +def _inputs(repo: Path) -> dict[str, str]: + return {"repo_path": str(repo), "change_id": "c1"} + + +def _shared_recorded( + tmp_path: Path, + monkeypatch: pytest.MonkeyPatch, +) -> tuple[Path, Path, dict[str, Any]]: + home = tmp_path / "home" + home.mkdir(parents=True, exist_ok=True) + monkeypatch.setenv("HOME", str(home)) + return _recorded_publication_fixture(tmp_path, monkeypatch) + + +def _read_log(path: Path) -> list[dict[str, Any]]: + if not path.exists(): + return [] + return [json.loads(line) for line in path.read_text().splitlines()] + + +def test_success_passes_the_stored_entry_unchanged( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + repo, records, stored = _shared_recorded(tmp_path, monkeypatch) + before = _records_snapshot(records) + kits_dir = _kit_copy(tmp_path) + _write_fake_protocol_kit(kits_dir) + log = tmp_path / "mcp.jsonl" + result = _run_json( + kits_dir / "profile-dev-kit/workflows/publish-reverification.yaml", + _inputs(repo), + tmp_path, + kits_dir, + FAKE_MCP_LOG=str(log), + FAKE_MCP_MODE="ok", + ) + assert result.returncode == 0, result.stderr + output = json.loads(result.stdout) + assert output["publish"]["tool"] == "write_copy" + assert _read_log(log) == [ + {"tool": "write_copy", "arguments": {"entry": stored["reverification"]}} + ] + assert _records_snapshot(records) == before + + +@pytest.mark.parametrize( + ("mode", "code"), + [ + ("unreachable", "BEDDEL-MCP-600"), + ("token", "BEDDEL-MCP-605"), + ("timeout", "BEDDEL-PRIM-303"), + ("server_error", "BEDDEL-MCP-602"), + ("rejected", "BEDDEL-MCP-603"), + ], +) +def test_transport_failure_is_absorbed( + tmp_path: Path, + monkeypatch: pytest.MonkeyPatch, + mode: str, + code: str, +) -> None: + repo, records, _ = _shared_recorded(tmp_path, monkeypatch) + before = _records_snapshot(records) + kits_dir = _kit_copy(tmp_path) + _write_fake_protocol_kit(kits_dir) + log = tmp_path / "mcp.jsonl" + workflow = kits_dir / "profile-dev-kit/workflows/publish-reverification.yaml" + if mode == "timeout": + document = yaml.safe_load(workflow.read_text()) + document["steps"][1]["config"]["timeout"] = 1 + workflow.write_text(yaml.safe_dump(document, sort_keys=False)) + result = _run_json( + workflow, + _inputs(repo), + tmp_path, + kits_dir, + FAKE_MCP_LOG=str(log), + FAKE_MCP_MODE=mode, + ) + assert result.returncode == 0, result.stderr + output = json.loads(result.stdout) + assert output["publish"] is None + assert output.get("_absorbed_errors") == [ + {"step_id": "publish", "error_code": code} + ] + assert "copy-destination.invalid" not in result.stdout + assert "copy-destination.invalid" not in result.stderr + assert "FAKE-MCP-MARKER" not in result.stdout + assert "FAKE-MCP-MARKER" not in result.stderr + assert _records_snapshot(records) == before + + +def test_composition_error_is_local_to_publication( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + repo, records, _ = _shared_recorded(tmp_path, monkeypatch) + before = _records_snapshot(records) + kits_dir = _kit_copy(tmp_path) + result = _run_json( + kits_dir / "profile-dev-kit/workflows/publish-reverification.yaml", + _inputs(repo), + tmp_path, + kits_dir, + ) + assert result.returncode == 1 + assert "Error [BEDDEL-EXEC-050]" in result.stderr + assert not result.stdout.strip() + assert "read" not in result.stdout + assert "publish" not in result.stdout + assert _records_snapshot(records) == before + + +def test_removed_publication_composition_leaves_read_report_usable( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + repo, _, _ = _shared_recorded(tmp_path, monkeypatch) + kits_dir = _kit_copy(tmp_path) + publish_workflow = ( + kits_dir / "profile-dev-kit/workflows/publish-reverification.yaml" + ) + document = yaml.safe_load(publish_workflow.read_text()) + document["steps"] = document["steps"][:1] + del document["mcp_servers"] + document["allowed_tools"].remove("copy_destination:write_copy") + publish_workflow.write_text(yaml.safe_dump(document, sort_keys=False)) + result = _run_json(publish_workflow, _inputs(repo), tmp_path, kits_dir) + assert result.returncode == 0, result.stderr + output = json.loads(result.stdout) + assert output.get("read", {}).get("result", {}).get("reverification") is not None + assert "publish" not in output + + read_workflow = kits_dir / "profile-dev-kit/workflows/read-report.yaml" + result = _run_json(read_workflow, _inputs(repo), tmp_path, kits_dir) + assert result.returncode == 0, result.stderr + output = json.loads(result.stdout) + assert output.get("read", {}).get("result", {}).get("report") is not None, output + + +def test_repeated_successful_publication_makes_two_identical_calls( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + repo, records, stored = _shared_recorded(tmp_path, monkeypatch) + before = _records_snapshot(records) + kits_dir = _kit_copy(tmp_path) + _write_fake_protocol_kit(kits_dir) + log = tmp_path / "mcp.jsonl" + workflow = kits_dir / "profile-dev-kit/workflows/publish-reverification.yaml" + for _ in range(2): + result = _run_json( + workflow, + _inputs(repo), + tmp_path, + kits_dir, + FAKE_MCP_LOG=str(log), + FAKE_MCP_MODE="ok", + ) + assert result.returncode == 0, result.stderr + assert _read_log(log) == [ + {"tool": "write_copy", "arguments": {"entry": stored["reverification"]}}, + {"tool": "write_copy", "arguments": {"entry": stored["reverification"]}}, + ] + assert _records_snapshot(records) == before + + +def _run_publish( + tmp_path: Path, + repo: Path, + change_id: str = "c1", +) -> tuple[subprocess.CompletedProcess[str], Path]: + kits_dir = _kit_copy(tmp_path) + _write_fake_protocol_kit(kits_dir) + log = tmp_path / "mcp.jsonl" + result = _run_json( + kits_dir / "profile-dev-kit/workflows/publish-reverification.yaml", + {"repo_path": str(repo), "change_id": change_id}, + tmp_path, + kits_dir, + FAKE_MCP_LOG=str(log), + FAKE_MCP_MODE="ok", + ) + return result, log + + +def _assert_cli_refusal( + run: Callable[[], tuple[subprocess.CompletedProcess[str], Path]], + records: Path, + reason: str, +) -> None: + before = _records_snapshot(records) + result, log = run() + assert result.returncode == 1 + assert "PROFILE_PUBLICATION_REFUSED" in result.stderr + assert reason in result.stderr + assert not log.exists() + assert _records_snapshot(records) == before + + +def _shared_prepared( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> tuple[Path, Path, str]: + home = tmp_path / "home" + home.mkdir(parents=True, exist_ok=True) + monkeypatch.setenv("HOME", str(home)) + repo, _, revision = _prepare(tmp_path) + return repo, records_root(repo, "records/profile-dev"), revision + + +def test_cli_refuses_invalid_id_without_protocol_call( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + repo, records, _ = _shared_recorded(tmp_path, monkeypatch) + _assert_cli_refusal( + lambda: _run_publish(tmp_path, repo, "not valid"), records, "id.invalid" + ) + + +def test_cli_refuses_unrecorded_change_without_directory( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + home = tmp_path / "home" + home.mkdir(parents=True, exist_ok=True) + monkeypatch.setenv("HOME", str(home)) + repo, _, _ = change_fixture(tmp_path, lambda path: None) + records = records_root(repo, "records/profile-dev") + _assert_cli_refusal( + lambda: _run_publish(tmp_path, repo), records, "change.unrecorded" + ) + assert not records.exists() + + +def test_cli_refuses_absent_report_without_protocol_call( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + repo, records, _ = _shared_prepared(tmp_path, monkeypatch) + _assert_cli_refusal(lambda: _run_publish(tmp_path, repo), records, "report.absent") + + +def test_cli_refuses_not_applicable_entry_without_protocol_call( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + repo, records, revision = _shared_prepared(tmp_path, monkeypatch) + profile_record_report(str(repo), "c1", revision) + _assert_cli_refusal( + lambda: _run_publish(tmp_path, repo), records, "reverification.not_recorded" + ) + + +def test_cli_refuses_older_schema_without_protocol_call( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + repo, records, _ = _shared_prepared(tmp_path, monkeypatch) + report_dir = records / "changes/c1/report" + report_dir.mkdir() + (report_dir / "1.json").write_text( + json.dumps( + { + "schema": "profile-dev/verification-report/1", + "record": 1, + "change_id": "c1", + "revision": "a" * 40, + } + ) + ) + _assert_cli_refusal( + lambda: _run_publish(tmp_path, repo), records, "reverification.not_recorded" + ) + + +def test_cli_refuses_malformed_report_without_protocol_call( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + repo, records, _ = _shared_prepared(tmp_path, monkeypatch) + report_dir = records / "changes/c1/report" + report_dir.mkdir() + (report_dir / "1.json").write_text("not json") + _assert_cli_refusal( + lambda: _run_publish(tmp_path, repo), records, "report.malformed" + ) + + +def test_cli_refuses_digest_mismatch_without_protocol_call( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + repo, records, stored = _shared_recorded(tmp_path, monkeypatch) + ref = next( + item["ref"] + for item in stored["reads"] + if "/report/" not in item["ref"] and not item["ref"].endswith("change.json") + ) + (records / ref).write_bytes(b"changed\n") + _assert_cli_refusal( + lambda: _run_publish(tmp_path, repo), records, "digest.mismatch" + ) diff --git a/kits/profile-dev-kit/tests/test_workflow.py b/kits/profile-dev-kit/tests/test_workflow.py index 215df21..e164fec 100644 --- a/kits/profile-dev-kit/tests/test_workflow.py +++ b/kits/profile-dev-kit/tests/test_workflow.py @@ -16,6 +16,7 @@ APPROVE_PATH = WORKFLOWS_DIR / "approve-change.yaml" RECORD_DECISION_PATH = WORKFLOWS_DIR / "record-approval-decision.yaml" READ_APPROVAL_PATH = WORKFLOWS_DIR / "read-approval.yaml" +PUBLISH_PATH = WORKFLOWS_DIR / "publish-reverification.yaml" def _load() -> Workflow: @@ -42,7 +43,11 @@ def _walk(steps: list[Step] | None) -> Iterator[Step]: def _tool_names(steps: list[Step]) -> set[str]: return { - str(s.config["tool"]) + ( + f"{s.config['mcp_server']}:{s.config['tool']}" + if "mcp_server" in s.config + else str(s.config["tool"]) + ) for s in _walk(steps) if s.primitive == "tool" and "tool" in s.config } @@ -108,7 +113,7 @@ def test_allowlist_equals_the_tools_the_steps_name() -> None: "profile_capture_evidence", "profile_record_report", ] - assert len(list(WORKFLOWS_DIR.glob("*.yaml"))) == 7 + assert len(list(WORKFLOWS_DIR.glob("*.yaml"))) == 8 def test_review_step_outlives_the_largest_accepted_review_budget() -> None: @@ -186,7 +191,7 @@ def test_read_report_workflow_is_one_allowlisted_step() -> None: assert wf.steps[0].execution_strategy.type is StrategyType.FAIL -def test_no_workflow_declares_mcp_servers_or_mcp_server_steps() -> None: +def test_only_the_publish_step_names_an_mcp_server() -> None: def walk(value: object) -> Iterator[dict[object, object]]: if isinstance(value, dict): yield value @@ -199,12 +204,29 @@ def walk(value: object) -> Iterator[dict[object, object]]: for path in sorted(WORKFLOWS_DIR.glob("*.yaml")): document = yaml.safe_load(path.read_text()) assert isinstance(document, dict) - assert "mcp_servers" not in document - assert all( - item.get("primitive") != "mcp_server" - for item in walk(document) - if isinstance(item, dict) - ), path.name + mappings = list(walk(document)) + assert all(item.get("primitive") != "mcp_server" for item in mappings), ( + path.name + ) + if path != PUBLISH_PATH: + assert "mcp_servers" not in document + assert all("mcp_server" not in item for item in mappings), path.name + + published = yaml.safe_load(PUBLISH_PATH.read_text()) + assert set(published["mcp_servers"]) == {"copy_destination"} + assert ( + published["mcp_servers"]["copy_destination"]["endpoint"] + .split("/")[2] + .endswith(".invalid") + ) + publish = WorkflowParser.parse(PUBLISH_PATH.read_text()) + assert publish.allowed_tools == [ + "profile_read_reverification", + "copy_destination:write_copy", + ] + assert _tool_names(publish.steps) == set(publish.allowed_tools) + assert [step.id for step in publish.steps] == ["read", "publish"] + assert publish.steps[1].execution_strategy.type is StrategyType.SKIP def test_approve_workflow_is_one_allowlisted_step() -> None: diff --git a/kits/profile-dev-kit/workflows/publish-reverification.yaml b/kits/profile-dev-kit/workflows/publish-reverification.yaml new file mode 100644 index 0000000..f87bb9f --- /dev/null +++ b/kits/profile-dev-kit/workflows/publish-reverification.yaml @@ -0,0 +1,35 @@ +id: publish-reverification +name: Publish Re-verification copy +description: Publishes a one-way copy of the recorded Re-verification entry of the latest stored Report through the configured MCP destination. The copy never blocks a verification and is never read back. +version: "1.0" +requires_kits: + - profile-dev-kit +mcp_servers: + copy_destination: + transport: mcp-streamable-http + endpoint: https://copy-destination.invalid/mcp + authorization: oauth +allowed_tools: + - profile_read_reverification + - copy_destination:write_copy +inputs: + - repo_path + - change_id +steps: + - id: read + primitive: tool + config: + tool: profile_read_reverification + arguments: + repo_path: "$input.repo_path" + change_id: "$input.change_id" + - id: publish + primitive: tool + execution_strategy: + type: skip + config: + mcp_server: copy_destination + tool: write_copy + timeout: 60 + arguments: + entry: "$stepResult.read.result.reverification"