|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Cross-repo consistency check for the SIN-Code Bundle (WS4 of operational-hardening). |
| 3 | +
|
| 4 | +The Bundle orchestrates 8 sibling subsystems that are installed via local |
| 5 | +``pip install -e`` of adjacent repos. This script asserts that the Bundle's |
| 6 | +own expectations stay internally consistent and reports drift against any |
| 7 | +subsystems that happen to be installed. |
| 8 | +
|
| 9 | +Design goals: |
| 10 | +- Exit 0 on a clean *bundle-only* checkout (subsystems absent -> warnings, not |
| 11 | + failures), so it is safe to wire into CI as a non-blocking job first. |
| 12 | +- Promote ``--strict`` to make any missing subsystem or mismatch fail (exit 1), |
| 13 | + for use once the full multi-repo environment is provisioned. |
| 14 | +
|
| 15 | +Checks performed: |
| 16 | +1. Bundle metadata: ``pyproject`` version == ``__init__.__version__``. |
| 17 | +2. Subsystem import specs: each subsystem the ``status`` command probes either |
| 18 | + imports cleanly or is reported as not-installed. |
| 19 | +3. MCP advertising: every client config emitted by ``sin mcp-config`` points at |
| 20 | + the same ``sin serve`` entry point that the package actually registers. |
| 21 | +""" |
| 22 | + |
| 23 | +from __future__ import annotations |
| 24 | + |
| 25 | +import argparse |
| 26 | +import importlib.metadata as md |
| 27 | +import importlib.util |
| 28 | +import sys |
| 29 | +import tomllib |
| 30 | +from pathlib import Path |
| 31 | + |
| 32 | +REPO_ROOT = Path(__file__).resolve().parent.parent |
| 33 | + |
| 34 | +# Canonical subsystem map -- kept in sync with cli.status(). |
| 35 | +SUBSYSTEMS = { |
| 36 | + "sin_code_sckg": "SCKG (knowledge graph)", |
| 37 | + "sin_code_ibd": "IBD (intent diff)", |
| 38 | + "sin_code_poc": "POC (proof of correctness)", |
| 39 | + "sin_code_efsm": "EFSM (mock orchestration)", |
| 40 | + "sin_code_adw": "ADW (debt watchdog)", |
| 41 | + "sin_code_oracle": "Oracle (verification)", |
| 42 | + "sin_code_orchestration": "Orchestration (multi-agent workflow)", |
| 43 | + "sin_code_review_interface": "Review-Interface (semantic review UI)", |
| 44 | +} |
| 45 | + |
| 46 | +GREEN, YELLOW, RED, RESET = "\033[32m", "\033[33m", "\033[31m", "\033[0m" |
| 47 | + |
| 48 | + |
| 49 | +def _ok(msg: str) -> None: |
| 50 | + print(f"{GREEN}OK{RESET} {msg}") |
| 51 | + |
| 52 | + |
| 53 | +def _warn(msg: str) -> None: |
| 54 | + print(f"{YELLOW}WARN{RESET} {msg}") |
| 55 | + |
| 56 | + |
| 57 | +def _fail(msg: str) -> None: |
| 58 | + print(f"{RED}FAIL{RESET} {msg}") |
| 59 | + |
| 60 | + |
| 61 | +def check_version() -> list[str]: |
| 62 | + errors: list[str] = [] |
| 63 | + pyproject = tomllib.loads((REPO_ROOT / "pyproject.toml").read_text()) |
| 64 | + declared = pyproject["project"]["version"] |
| 65 | + init_text = (REPO_ROOT / "src" / "sin_code_bundle" / "__init__.py").read_text() |
| 66 | + runtime = next( |
| 67 | + ( |
| 68 | + line.split("=", 1)[1].strip().strip('"').strip("'") |
| 69 | + for line in init_text.splitlines() |
| 70 | + if line.startswith("__version__") |
| 71 | + ), |
| 72 | + None, |
| 73 | + ) |
| 74 | + if runtime == declared: |
| 75 | + _ok(f"version aligned: pyproject == __init__ == {declared}") |
| 76 | + else: |
| 77 | + _fail(f"version drift: pyproject={declared!r} but __init__={runtime!r}") |
| 78 | + errors.append("version drift") |
| 79 | + return errors |
| 80 | + |
| 81 | + |
| 82 | +def check_subsystems(strict: bool) -> list[str]: |
| 83 | + errors: list[str] = [] |
| 84 | + for module, desc in SUBSYSTEMS.items(): |
| 85 | + installed = importlib.util.find_spec(module) is not None |
| 86 | + if installed: |
| 87 | + try: |
| 88 | + version = md.version(module.replace("_", "-")) |
| 89 | + except md.PackageNotFoundError: |
| 90 | + version = "unknown" |
| 91 | + _ok(f"{desc}: importable (v{version})") |
| 92 | + elif strict: |
| 93 | + _fail(f"{desc}: module '{module}' not installed (strict)") |
| 94 | + errors.append(f"{module} missing") |
| 95 | + else: |
| 96 | + _warn(f"{desc}: module '{module}' not installed (expected in bundle-only checkout)") |
| 97 | + return errors |
| 98 | + |
| 99 | + |
| 100 | +def check_mcp_advertising() -> list[str]: |
| 101 | + errors: list[str] = [] |
| 102 | + from sin_code_bundle import mcp_config |
| 103 | + |
| 104 | + expected_cmd, expected_args = mcp_config.COMMAND, mcp_config.ARGS |
| 105 | + if (expected_cmd, expected_args) != ("sin", ["serve"]): |
| 106 | + _fail(f"mcp entry point unexpected: {expected_cmd} {expected_args}") |
| 107 | + errors.append("mcp entry point") |
| 108 | + return errors |
| 109 | + |
| 110 | + # The package must actually expose the `sin` console script the configs point at. |
| 111 | + scripts = {ep.name: ep.value for ep in md.entry_points(group="console_scripts")} |
| 112 | + if scripts.get("sin", "").startswith("sin_code_bundle.cli"): |
| 113 | + _ok("'sin' console script resolves to sin_code_bundle.cli") |
| 114 | + else: |
| 115 | + _fail(f"'sin' console script missing or wrong: {scripts.get('sin')!r}") |
| 116 | + errors.append("console script") |
| 117 | + |
| 118 | + for client in mcp_config.SUPPORTED_CLIENTS: |
| 119 | + rendered = mcp_config.generate(client) |
| 120 | + if expected_cmd in rendered and "serve" in rendered: |
| 121 | + _ok(f"mcp-config[{client}] advertises '{expected_cmd} serve'") |
| 122 | + else: |
| 123 | + _fail(f"mcp-config[{client}] does not advertise the serve entry point") |
| 124 | + errors.append(f"mcp-config {client}") |
| 125 | + return errors |
| 126 | + |
| 127 | + |
| 128 | +def main() -> int: |
| 129 | + parser = argparse.ArgumentParser(description=__doc__) |
| 130 | + parser.add_argument( |
| 131 | + "--strict", |
| 132 | + action="store_true", |
| 133 | + help="Treat missing subsystems as failures (full multi-repo env).", |
| 134 | + ) |
| 135 | + args = parser.parse_args() |
| 136 | + |
| 137 | + print("== SIN-Code Bundle consistency check ==") |
| 138 | + errors: list[str] = [] |
| 139 | + errors += check_version() |
| 140 | + errors += check_subsystems(args.strict) |
| 141 | + errors += check_mcp_advertising() |
| 142 | + |
| 143 | + print() |
| 144 | + if errors: |
| 145 | + _fail(f"{len(errors)} consistency problem(s): {', '.join(errors)}") |
| 146 | + return 1 |
| 147 | + _ok("all consistency checks passed") |
| 148 | + return 0 |
| 149 | + |
| 150 | + |
| 151 | +if __name__ == "__main__": |
| 152 | + sys.exit(main()) |
0 commit comments