|
| 1 | +"""Deterministic CLI for compact MCP context-layer outputs.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import argparse |
| 6 | +import json |
| 7 | +import sys |
| 8 | +from pathlib import Path |
| 9 | +from typing import Any |
| 10 | + |
| 11 | +REPO_ROOT = Path(__file__).resolve().parents[1] |
| 12 | +if str(REPO_ROOT) not in sys.path: |
| 13 | + sys.path.insert(0, str(REPO_ROOT)) |
| 14 | + |
| 15 | +from src.comptext_v7.mcp import build_replay_payload, render_prompt_context, validate_replay_payload |
| 16 | + |
| 17 | +DEFAULT_FIXTURE = Path("fixtures/mcp_trace_replay_v1/original") |
| 18 | + |
| 19 | + |
| 20 | +def _repo_relative(path: Path) -> str: |
| 21 | + resolved = path.resolve() |
| 22 | + try: |
| 23 | + return resolved.relative_to(REPO_ROOT).as_posix() |
| 24 | + except ValueError: |
| 25 | + return path.as_posix() |
| 26 | + |
| 27 | + |
| 28 | +def _load_json(path: Path) -> dict[str, Any]: |
| 29 | + display_path = _repo_relative(path) |
| 30 | + try: |
| 31 | + payload = json.loads(path.read_text(encoding="utf-8")) |
| 32 | + except FileNotFoundError as exc: |
| 33 | + raise RuntimeError(f"missing required fixture file: {display_path}") from exc |
| 34 | + except json.JSONDecodeError as exc: |
| 35 | + raise RuntimeError(f"invalid JSON in fixture file: {display_path}: {exc}") from exc |
| 36 | + if not isinstance(payload, dict): |
| 37 | + raise RuntimeError(f"fixture file must contain a JSON object: {display_path}") |
| 38 | + return payload |
| 39 | + |
| 40 | + |
| 41 | +def load_fixture_context(fixture: Path) -> dict[str, Any]: |
| 42 | + fixture_path = fixture if fixture.is_absolute() else REPO_ROOT / fixture |
| 43 | + return { |
| 44 | + "task": fixture_path.parent.name, |
| 45 | + "trace": _load_json(fixture_path / "trace.json"), |
| 46 | + "state": _load_json(fixture_path / "state.json"), |
| 47 | + "dependency_graph": _load_json(fixture_path / "dependency_graph.json"), |
| 48 | + } |
| 49 | + |
| 50 | + |
| 51 | +def build_cli_output(fixture: Path, *, include_prompt: bool, include_validation: bool) -> dict[str, Any]: |
| 52 | + fixture_path = fixture if fixture.is_absolute() else REPO_ROOT / fixture |
| 53 | + replay_payload = build_replay_payload(load_fixture_context(fixture_path)) |
| 54 | + output: dict[str, Any] = { |
| 55 | + "replay_payload": replay_payload, |
| 56 | + "source_fixture_path": _repo_relative(fixture_path), |
| 57 | + } |
| 58 | + |
| 59 | + validation = validate_replay_payload(replay_payload) if include_validation else None |
| 60 | + if validation is not None: |
| 61 | + output["validation"] = validation |
| 62 | + if include_prompt: |
| 63 | + prompt_payload = {**replay_payload} |
| 64 | + if validation is not None: |
| 65 | + prompt_payload["validation"] = validation |
| 66 | + output["prompt_context"] = render_prompt_context(prompt_payload) |
| 67 | + |
| 68 | + return output |
| 69 | + |
| 70 | + |
| 71 | +def _parse_args(argv: list[str]) -> argparse.Namespace: |
| 72 | + parser = argparse.ArgumentParser(description="Generate deterministic MCP context-layer output from a fixture.") |
| 73 | + parser.add_argument("--fixture", type=Path, default=DEFAULT_FIXTURE, help="Fixture directory containing trace/state/dependency_graph JSON files.") |
| 74 | + parser.add_argument("--json", action="store_true", help="Emit deterministic JSON output.") |
| 75 | + parser.add_argument("--render-prompt", action="store_true", help="Include or emit compact prompt context.") |
| 76 | + parser.add_argument("--validate", action="store_true", help="Include validation/admissibility result.") |
| 77 | + return parser.parse_args(argv) |
| 78 | + |
| 79 | + |
| 80 | +def main(argv: list[str] | None = None) -> int: |
| 81 | + args = _parse_args(sys.argv[1:] if argv is None else argv) |
| 82 | + output = build_cli_output(args.fixture, include_prompt=args.render_prompt, include_validation=args.validate) |
| 83 | + |
| 84 | + if args.json or not args.render_prompt: |
| 85 | + sys.stdout.write(json.dumps(output, indent=2, sort_keys=True) + "\n") |
| 86 | + return 0 |
| 87 | + |
| 88 | + prompt_context = output.get("prompt_context") |
| 89 | + if not isinstance(prompt_context, str): |
| 90 | + prompt_context = render_prompt_context(output["replay_payload"]) |
| 91 | + sys.stdout.write(prompt_context + "\n") |
| 92 | + return 0 |
| 93 | + |
| 94 | + |
| 95 | +if __name__ == "__main__": |
| 96 | + raise SystemExit(main()) |
0 commit comments