#!/usr/bin/env python3 """SDK integration check against a frozen Marqdo CLI binary. This is the cross-repository acceptance for the independent SDK (issue #13 ยง6.3 / S04-S07): 1. build the plugin fixture with only this repository's public SDK; 2. create a host-specific ``plugin.yaml``; 3. load and invoke the plugin through the *frozen* CLI binary named by ``--marqdo-bin`` (no CLI checkout, no nested CLI build); 4. reject a tampered artifact digest. Usage: python3 tools/check_with_frozen_cli.py --marqdo-bin /path/to/marqdo """ from __future__ import annotations import argparse import hashlib import platform import shutil import subprocess import sys import tempfile from pathlib import Path REPO_ROOT = Path(__file__).resolve().parents[1] FIXTURE = REPO_ROOT / "tests" / "fixture" CALL_DOC = FIXTURE / "call.mq.md" class GateError(Exception): pass def run(command: list[str], *, check: bool = True) -> subprocess.CompletedProcess: result = subprocess.run( command, cwd=REPO_ROOT, capture_output=True, text=True, check=False ) if check and result.returncode != 0: raise GateError( f"`{' '.join(command)}` failed with {result.returncode}:\n" f"{result.stdout}\n{result.stderr}" ) return result def host_triple() -> str: result = run(["rustc", "-vV"]) for line in result.stdout.splitlines(): if line.startswith("host: "): return line[len("host: ") :].strip() raise GateError("rustc -vV did not report a host triple") def artifact_name() -> str: system = platform.system() if system == "Darwin": return "libmarqdo_sdk_plugin_fixture.dylib" if system == "Windows": return "marqdo_sdk_plugin_fixture.dll" return "libmarqdo_sdk_plugin_fixture.so" def write_manifest( directory: Path, digest: str, triple: str, artifact: str, *, tamper: bool = False, ) -> None: effective = "0" * 64 if tamper else digest manifest = f"""plugin: name: sdk.demo version: 0.1.0 abi: "^1.0" kind: native exports: - name: sdk.add signature: "($a: Int, $b: Int) -> Int" - name: sdk.echo signature: "($text: Text) -> Text" artifacts: - target: {triple} file: {artifact} sha256: "{effective}" """ (directory / "plugin.yaml").write_text(manifest, encoding="utf-8") def main() -> int: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument( "--marqdo-bin", required=True, help="frozen `marqdo` executable from a released CLI archive", ) parser.add_argument( "--offline", action="store_true", help="pass --offline to cargo (warm registry cache only)", ) arguments = parser.parse_args() offline = ["--offline"] if arguments.offline else [] binary = Path(arguments.marqdo_bin).resolve() try: if not binary.is_file(): raise GateError(f"frozen CLI binary not found: {binary}") run( [ "cargo", "build", "--locked", *offline, "--manifest-path", str(FIXTURE / "Cargo.toml"), ] ) name = artifact_name() built = FIXTURE / "target" / "debug" / name if not built.is_file(): raise GateError(f"fixture artifact not found: {built}") with tempfile.TemporaryDirectory(prefix="marqdo-sdk-integration-") as raw: directory = Path(raw) artifact = "plugin" + Path(name).suffix shutil.copyfile(built, directory / artifact) digest = hashlib.sha256((directory / artifact).read_bytes()).hexdigest() command = [ str(binary), "call", str(CALL_DOC), "main", "--plugin", str(directory), "--trust-native-plugin", "--allow-plugin", "sdk.demo", ] write_manifest(directory, digest, host_triple(), artifact) positive = run(command, check=False) if positive.returncode != 0 or positive.stdout.strip() != "42": raise GateError( "frozen CLI could not invoke the SDK plugin: " f"exit={positive.returncode} stdout={positive.stdout!r} " f"stderr={positive.stderr!r}" ) print("PASS sdk-frozen-cli-invocation (Int 42)") write_manifest(directory, digest, host_triple(), artifact, tamper=True) negative = run(command, check=False) if negative.returncode == 0: raise GateError("tampered artifact digest was accepted") combined = negative.stdout + negative.stderr if "sha256" not in combined.lower() and "artifact" not in combined.lower(): raise GateError( "tampered artifact failed without an artifact/sha256 diagnostic: " + combined ) print("PASS sdk-tampered-digest-rejected") except GateError as error: print(f"FAIL sdk-integration: {error}", file=sys.stderr) return 1 print("PASS sdk-integration") return 0 if __name__ == "__main__": sys.exit(main())