#!/usr/bin/env python3 """Cold-install smoke test for a built marqdo-knowledge release archive. Extracts the archive into a fresh directory, verifies its checksum, source SHA, file manifest and version consistency, installs the bundle through the real core CLI into an isolated MARQDO_TOOL_HOME, then exercises the shipped tool: help, compile/query/check on the packaged examples, and removal. It never uses the development source tree. Usage: MARQDO_BIN=/path/to/marqdo python3 tools/release/smoke.py \ --archive dist/marqdo-knowledge-0.1.0-beta.2-x86_64-unknown-linux-gnu.tar.gz """ from __future__ import annotations import argparse import hashlib import json import os import re import shutil import subprocess import sys import tarfile import tempfile from pathlib import Path def fail(message: str) -> "NoReturn": print(f"smoke: FAIL: {message}", file=sys.stderr) raise SystemExit(1) def sha256_file(path: Path) -> str: digest = hashlib.sha256() with path.open("rb") as handle: for chunk in iter(lambda: handle.read(1024 * 1024), b""): digest.update(chunk) return digest.hexdigest() def run(command: list[str], *, env: dict[str, str] | None = None) -> str: completed = subprocess.run( command, check=True, text=True, capture_output=True, env=env, ) return completed.stdout.strip() def main() -> int: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--archive", required=True) parser.add_argument("--marqdo-bin", default=os.environ.get("MARQDO_BIN", "")) parser.add_argument("--evidence-out", default="") args = parser.parse_args() marqdo = Path(args.marqdo_bin) if not marqdo.is_file(): fail("set --marqdo-bin or MARQDO_BIN to a real core CLI binary") archive = Path(args.archive).resolve() if not archive.is_file(): fail(f"archive {archive} does not exist") checksum_path = archive.with_name(archive.name + ".sha256") if not checksum_path.is_file(): fail(f"missing checksum {checksum_path.name}") expected = checksum_path.read_text(encoding="utf-8").split()[0] actual = sha256_file(archive) if expected != actual: fail(f"archive SHA-256 {actual} does not match checksum {expected}") with tempfile.TemporaryDirectory(prefix="marqdo-knowledge-smoke-") as work: work_path = Path(work) with tarfile.open(archive, "r:gz") as tar: members = tar.getnames() if any(name.startswith("/") or ".." in Path(name).parts for name in members): fail("archive contains an unsafe path") tar.extractall(work_path) roots = [path for path in work_path.iterdir() if path.is_dir()] if len(roots) != 1: fail("archive must contain exactly one top directory") package = roots[0] info = json.loads((package / "RELEASE_INFO.json").read_text(encoding="utf-8")) tool = json.loads((package / "tool.json").read_text(encoding="utf-8")) source_sha = (package / "SOURCE_SHA").read_text(encoding="utf-8").strip() if not re.fullmatch(r"[a-f0-9]{40}", source_sha): fail("SOURCE_SHA is not a full commit sha") if info.get("source_sha") != source_sha: fail("RELEASE_INFO source_sha does not match SOURCE_SHA") if info.get("product_version") != tool.get("version"): fail("RELEASE_INFO product_version does not match tool.json") if tool.get("entrypoint") != "bin/marqdo-knowledge": fail("tool.json entrypoint is not bin/marqdo-knowledge") binary = package / "bin/marqdo-knowledge" if not binary.is_file() or not os.access(binary, os.X_OK): fail("packaged entrypoint is missing or not executable") version_output = run([str(binary), "--version"]) if version_output != f"marqdo-knowledge {tool.get('version')}": fail(f"packaged binary reports `{version_output}`") manifest = package / "FILES.sha256" listed: dict[str, str] = {} for line in manifest.read_text(encoding="utf-8").splitlines(): parts = line.split(" ", 1) if len(parts) != 2: fail("FILES.sha256 has a malformed line") digest, relative = parts listed[relative] = digest manifest_digest = sha256_file(manifest) if info.get("files_manifest_sha256") != manifest_digest: fail("RELEASE_INFO files_manifest_sha256 does not match FILES.sha256") for relative, digest in listed.items(): path = package / relative if not path.is_file(): fail(f"manifest lists missing file {relative}") if sha256_file(path) != digest: fail(f"file digest mismatch for {relative}") tool_home = work_path / "tool-home" tool_home.mkdir() env = os.environ.copy() env["MARQDO_TOOL_HOME"] = str(tool_home) env.pop("MARQDO_API_REVISION", None) run([str(marqdo), "tool", "install", str(package)], env=env) try: listing = run([str(marqdo), "tool", "list"], env=env) if "knowledge" not in listing: fail("tool was not listed after install") run([str(marqdo), "knowledge", "--help"], env=env) compile_report = json.loads( run( [ str(marqdo), "knowledge", "compile", str(package / "examples/mixed"), "--json", ], env=env, ) ) if not compile_report.get("success"): fail("compile did not succeed") if compile_report["payload"]["documents"] <= 0: fail("compile reported no documents") query_report = json.loads( run( [ str(marqdo), "knowledge", "query", "归一化", "--root", str(package / "examples/mixed"), "--format", "json", ], env=env, ) ) if not query_report.get("success") or not query_report.get("hits"): fail("query returned no hits") check_report = json.loads( run( [ str(marqdo), "knowledge", "check", str(package / "examples/mixed"), "--json", ], env=env, ) ) if not check_report.get("success") or check_report["payload"]["errors"] != 0: fail("check failed") if check_report["payload"]["database"] != "current": fail("check did not report a current database") evidence = { "schema": "marqdo.knowledge.smoke", "schema_version": 1, "archive": archive.name, "archive_sha256": actual, "source_sha": source_sha, "version": tool["version"], "core": run([str(marqdo), "--version"]), "documents": compile_report["payload"]["documents"], "query_hits": len(query_report["hits"]), "database": check_report["payload"]["database"], } print(json.dumps(evidence, indent=2, sort_keys=True, ensure_ascii=False)) if args.evidence_out: Path(args.evidence_out).write_text( json.dumps(evidence, indent=2, sort_keys=True, ensure_ascii=False) + "\n", encoding="utf-8", ) finally: run([str(marqdo), "tool", "remove", "knowledge"], env=env) remaining = run([str(marqdo), "tool", "list"], env=env) if "knowledge" in remaining: fail("tool was not removed") print("smoke: OK") return 0 if __name__ == "__main__": sys.exit(main())