#!/usr/bin/env python3 """Independent Marqdo CLI Tool (Tool Protocol v1). Consumes only public contracts: `$MARQDO_BIN check --emit ` (Analysis v2) and optionally `$MARQDO_BIN run --trace-file ` (Trace v2). It links no Rust crate and reads no CLI source tree. Usage: marqdo inspect-ext [--trace] Prints one deterministic JSON object: {"schema":"ext.inspect.v1","document":...,"functions":[...], "function_digest":"sha256:...", "trace": {...}|null} """ import hashlib import json import os import subprocess import sys import tempfile from pathlib import Path SCHEMA = "ext.inspect.v1" def fail(message, code=2): print(json.dumps({"schema": SCHEMA, "error": message}, ensure_ascii=False)) return code def main(argv): args = [arg for arg in argv[1:] if not arg.startswith("--")] want_trace = "--trace" in argv[1:] if len(args) != 1: return fail("usage: marqdo inspect-ext [--trace]") bin_path = os.environ.get("MARQDO_BIN") if not bin_path: return fail("MARQDO_BIN is not set; install through `marqdo tool install`") document = Path(args[0]).resolve() if not document.is_file(): return fail(f"document does not exist: {document}") with tempfile.TemporaryDirectory(prefix="ext-inspect-") as tmp: analysis_path = Path(tmp) / "analysis.json" check = subprocess.run( [bin_path, "check", str(document), "--emit", str(analysis_path), "--json"], capture_output=True, text=True, ) if check.returncode != 0 or not analysis_path.is_file(): return fail( "check failed: " + (check.stderr.strip() or check.stdout.strip()), code=1, ) analysis = json.loads(analysis_path.read_text(encoding="utf-8")) functions = [] for module in analysis.get("modules", []): for function in module.get("functions", []): functions.append( { "id": function["id"], "module": module["id"], "local_name": function.get("local_name"), "heading_span": function.get("heading_span"), "signature": function.get("signature", {}).get("kind"), } ) functions.sort(key=lambda item: item["id"]) digest = hashlib.sha256() for function in functions: digest.update(function["id"].encode()) digest.update(b"\0") heading = function.get("heading_span") or {} digest.update(f"{heading.get('start', 0)}:{heading.get('end', 0)}".encode()) digest.update(b"\0") payload = { "schema": SCHEMA, "document": document.name, "analysis_schema_version": analysis.get("schema_version"), "analysis_digest": hashlib.sha256( json.dumps(analysis, sort_keys=True, separators=(",", ":")).encode() ).hexdigest(), "functions": functions, "function_digest": "sha256:" + digest.hexdigest(), "trace": None, } if want_trace: trace_path = Path(tmp) / "trace.json" run = subprocess.run( [ bin_path, "run", str(document), "--trace-file", str(trace_path), "--json", ], capture_output=True, text=True, ) if trace_path.is_file(): trace = json.loads(trace_path.read_text(encoding="utf-8")) payload["trace"] = { "schema": trace.get("schema"), "schema_version": trace.get("schema_version"), "events": len(trace.get("events", [])), } elif run.returncode != 0: payload["trace"] = {"error": run.stderr.strip()[:500]} print(json.dumps(payload, ensure_ascii=False, sort_keys=True)) return 0 if __name__ == "__main__": raise SystemExit(main(sys.argv))