diff --git a/.github/scripts/msinsight_acceptance/README.md b/.github/scripts/msinsight_acceptance/README.md new file mode 100644 index 0000000..1250d87 --- /dev/null +++ b/.github/scripts/msinsight_acceptance/README.md @@ -0,0 +1,11 @@ +# Optional pinned msinsight consumer acceptance + +Draft, not yet validated as a hosted workflow. Keep separate from the core installed-HTML release gate. + +The locally verified pieces use msinsight revision 101f65b877a267ffd5f66ea3834706057ba243e5, the candidate installed wheel, and the exact reviewed 581140-byte snapshot_expandable.pkl SHA256 3afc9d1c5ef4ca4b417e58c0830e9eb8a913eb9459f4088b8c66c22325c68c40. + +Required runner: Linux, official compiler/build tools, Node, Python, Playwright Chromium and strace; browser and three loopback-only server ports must share one network runtime. No tokens or third-party authentication, public tunnel, deployment, upstream edits, tags or release actions. + +Keep all producer raw output, server traces, SQLite databases, source pickle, WebSocket messages and screenshots private. Hosted artifacts must be an explicit allowlist of sanitized numeric gate summaries. A screenshot must first be reviewed to exclude source user paths and callstacks; do not upload a whole working directory on success or failure. + +Current local evidence: isolated official dependency install passed, unchanged frozen frontend lockfile passed; original producer, 11 installed API templates and 11 console templates passed. Minimal server compilation is in progress; production frontend builds are not yet run. Cloud-browser/runtime isolation blocks local GUI, and local ptrace is denied. The hosted job requires actual GUI import, window navigation, peak-event label, 167 typed protocol assertions, mandatory process trace and preservation checks. None is claimed passed merely by adding this harness. Twelve fast fail-closed contract tests pass; exact-source hosted execution is the remaining acceptance step. diff --git a/.github/scripts/msinsight_acceptance/evidence_inventory.py b/.github/scripts/msinsight_acceptance/evidence_inventory.py new file mode 100644 index 0000000..7de0d96 --- /dev/null +++ b/.github/scripts/msinsight_acceptance/evidence_inventory.py @@ -0,0 +1,159 @@ +#!/usr/bin/env python3 +"""New readonly helper; only syntax/synthetic tests performed, not a rerun of acceptance. +Use after writers have stopped; immutable SQLite assumes no pending WAL. +Never prints source call stacks; JSON stores only table hashes/counts, not rows. +""" + +import argparse +import hashlib +import json +import sqlite3 +from pathlib import Path + + +def digest(path): + h = hashlib.sha256() + with path.open("rb") as stream: + for b in iter(lambda: stream.read(1024 * 1024), b""): + h.update(b) + return h.hexdigest() + + +def inventory(root): + result = {} + for p in sorted(root.rglob("*")): + if p.name.endswith(("-wal", "-shm", "-journal")): + raise ValueError("Stop writers and resolve WAL/SHM/journal sidecars before inspection") + if p.is_symlink(): + raise ValueError("Refusing symlink in evidence directory") + if p.is_file(): + result[str(p.relative_to(root))] = { + "sha256": digest(p), + "bytes": p.stat().st_size, + } + return result + + +def quote(name): + return '"' + name.replace('"', '""') + '"' + + +def logical(path): + for ending in ("-wal", "-shm", "-journal"): + if Path(str(path) + ending).exists() or Path(str(path) + ending).is_symlink(): + raise ValueError("Stop writers and resolve WAL/journal before immutable inspection") + with sqlite3.connect(path.resolve().as_uri() + "?mode=ro&immutable=1", uri=True) as db: + result = {} + for table, schema in db.execute( + "SELECT name,sql FROM sqlite_master WHERE type='table' ORDER BY name" + ): + cols = [r[1] for r in db.execute("PRAGMA table_info(" + quote(table) + ")")] + query = "SELECT * FROM " + quote(table) + " ORDER BY " + ",".join(map(quote, cols)) + h = hashlib.sha256() + count = 0 + for row in db.execute(query): + # Typed canonical encoding also supports BLOB without exposing its contents. + encoded = [(type(x).__name__, x.hex() if isinstance(x, bytes) else x) for x in row] + h.update( + json.dumps(encoded, ensure_ascii=False, separators=(",", ":")).encode() + b"\n" + ) + count += 1 + result[table] = { + "schema": schema, + "rows": count, + "logical_sha256": h.hexdigest(), + } + return result + + +def main(): + a = argparse.ArgumentParser(description=__doc__) + a.add_argument("--artifact", type=Path, required=True) + a.add_argument("--compare", type=Path) + a.add_argument( + "--mode", + choices=("exact", "logical"), + help="Required with --compare: exact byte-for-byte copy gate; logical post-GUI cache gate", + ) + a.add_argument("--output", type=Path, required=True) + args = a.parse_args() + if bool(args.compare) != bool(args.mode): + a.error("--compare and --mode must be supplied together") + if args.output.exists() or args.output.is_symlink(): + a.error("Select a fresh output filename") + roots = [args.artifact] + ([args.compare] if args.compare else []) + for root in roots: + if not root.is_dir() or root.is_symlink(): + a.error("Artifact must be an existing nonsymlink directory") + if args.output.resolve().is_relative_to(root.resolve()): + a.error("Write the report outside the artifact") + result = { + "helper_schema_version": 2, + "mode": args.mode or "inventory", + "inventory": inventory(args.artifact), + } + if args.compare: + before = result["inventory"] + after = inventory(args.compare) + result["comparison_inventory"] = after + result["identical_file_set"] = set(before) == set(after) + result["changed_files"] = sorted( + k for k in set(before) & set(after) if before[k] != after[k] + ) + result["missing_files"] = sorted(set(before) - set(after)) + result["added_files"] = sorted(set(after) - set(before)) + result["exact_copy"] = before == after + if args.mode == "exact": + result["passed"] = result["exact_copy"] + else: + # Only shard DB bytes may change. Manifest and all other files must stay exact. + db_members = { + str(x.relative_to(args.artifact)) for x in args.artifact.glob("device_*/*.db") + } + result["unchanged_non_database_files"] = all( + before[k] == after.get(k) for k in before if k not in db_members + ) + result["manifest_unchanged"] = "manifest.json" in before and before[ + "manifest.json" + ] == after.get("manifest.json") + result["members"] = [] + allowed_added_tables = {"memory_allocation_cache_v1_0"} + for member in sorted(db_members): + if member not in after: + continue + db, other = args.artifact / member, args.compare / member + old, new = logical(db), logical(other) + added = {k: v for k, v in new.items() if k not in old} + result["members"].append( + { + "member": member, + "base_tables_unchanged": all(new.get(k) == v for k, v in old.items()), + "added_tables": added, + "only_known_derived_tables_added": set(added) <= allowed_added_tables, + "bytes_changed": digest(db) != digest(other), + } + ) + result["all_base_tables_unchanged"] = ( + bool(db_members) + and len(result["members"]) == len(db_members) + and all(m["base_tables_unchanged"] for m in result["members"]) + ) + result["only_known_derived_tables_added"] = all( + m["only_known_derived_tables_added"] for m in result["members"] + ) + result["passed"] = ( + result["identical_file_set"] + and result["unchanged_non_database_files"] + and result["manifest_unchanged"] + and result["all_base_tables_unchanged"] + and result["only_known_derived_tables_added"] + ) + with args.output.open("x") as out: + json.dump(result, out, indent=2, ensure_ascii=False) + out.write("\n") + if args.compare and not result["passed"]: + raise SystemExit("Comparison failed in " + args.mode + " mode; report saved") + + +if __name__ == "__main__": + main() diff --git a/.github/scripts/msinsight_acceptance/gui_navigation.py b/.github/scripts/msinsight_acceptance/gui_navigation.py new file mode 100644 index 0000000..bb928ab --- /dev/null +++ b/.github/scripts/msinsight_acceptance/gui_navigation.py @@ -0,0 +1,131 @@ +"""Draft real UI navigation; numerical lifecycle assertions are a separate protocol gate. +Never uploads screenshots, logs, DOM or fixture bytes. No security flags or UI overrides. +""" + +import argparse +import json +import re +from pathlib import Path + +GUI_PHASES = ( + "browser_launch", + "page_open", + "import_open", + "fixture_input", + "import_search", + "confirm_ready", + "import_confirm", + "overview_visible", + "window_count", + "window_click", + "window_selected", + "window_ready", + "peak_visible", + "peak_event", + "browser_close", + "page_errors", + "complete", +) +GUI_ERRORS = ("none", "timeout", "assertion", "runtime", "other") + + +def error_kind(exc): + # Never serialize exception messages, custom class names, paths or DOM text. + if isinstance(exc, AssertionError): + return "assertion" + if isinstance(exc, TimeoutError): + return "timeout" + if type(exc).__module__ == "playwright._impl._errors" and type(exc).__name__ == "TimeoutError": + return "timeout" + if isinstance(exc, RuntimeError): + return "runtime" + return "other" + + +def record_navigation(output, action): + state = {"phase": "browser_launch", "error_kind": "none", "transition": 0} + checks, page_errors = [], [] + try: + action(state, checks, page_errors) + state["phase"] = "complete" + except Exception as exc: + state["error_kind"] = error_kind(exc) + raise + finally: + state["completed_windows"] = [c["window"] for c in checks if "window" in c] + state["page_error_count"] = len(page_errors) + output.write_text(json.dumps(state, indent=2)) + + +def navigate(fixture, state, checks, page_errors): + from playwright.sync_api import expect, sync_playwright + + state["phase"] = "browser_launch" + with sync_playwright() as pw: + browser = pw.chromium.launch(headless=True) + context = browser.new_context(locale="en-US", viewport={"width": 1600, "height": 1000}) + page = context.new_page() + page.on("pageerror", lambda error: page_errors.append(type(error).__name__)) + state["phase"] = "page_open" + page.goto("http://127.0.0.1:5174/", wait_until="domcontentloaded") + state["phase"] = "import_open" + page.locator(".btn-import").click() + field = page.get_by_test_id("filePathInput") + state["phase"] = "fixture_input" + field.fill(str(fixture)) + state["phase"] = "import_search" + field.press("Enter") + confirm = page.get_by_role("button", name="Confirm", exact=True) + state["phase"] = "confirm_ready" + expect(confirm).to_be_enabled(timeout=30000) + state["phase"] = "import_confirm" + confirm.click() + frame = page.frame_locator('iframe[src*="plugins/Leaks"]') + overview = frame.get_by_test_id("snapshotSliceOverview") + state["phase"] = "overview_visible" + expect(overview).to_be_visible(timeout=180000) + windows = overview.locator('[role="button"]') + state["phase"] = "window_count" + expect(windows).to_have_count(5) + for index in (0, 1, 0, 3): + state["transition"] = len(checks) + state["phase"] = "window_click" + button = windows.nth(index) + if index != 0 or len(checks): + button.click() + state["phase"] = "window_selected" + expect(button).to_have_attribute("aria-pressed", "true", timeout=30000) + state["phase"] = "window_ready" + expect(button).to_have_attribute("data-ready", "true") + checks.append({"window": index + 1, "selected": True, "ready": True}) + peak = windows.nth(3).get_by_test_id("globalPeakMarker") + state["phase"] = "peak_visible" + expect(peak.first).to_be_visible() + state["phase"] = "peak_event" + expect(peak.first).to_have_attribute("aria-label", re.compile(r"\b7430\b")) + checks.append({"window_4_global_peak_marker": True, "event_id": 7430}) + # Close the sole GUI WebSocket before the separate protocol client starts. + state["phase"] = "browser_close" + context.close() + browser.close() + state["phase"] = "page_errors" + if page_errors: + raise RuntimeError("GUI page errors observed; inspect private diagnostics") + + +def main(): + p = argparse.ArgumentParser() + p.add_argument("--fixture", type=Path, required=True) + p.add_argument("--output", type=Path, required=True) + args = p.parse_args() + if args.output.exists(): + raise ValueError("Use a fresh output path") + assert args.fixture.is_absolute() and args.fixture.is_file() + record_navigation( + args.output, + lambda state, checks, errors: navigate(args.fixture, state, checks, errors), + ) + + +if __name__ == "__main__": + main() diff --git a/.github/scripts/msinsight_acceptance/gui_supervisor.py b/.github/scripts/msinsight_acceptance/gui_supervisor.py new file mode 100644 index 0000000..d0a12dd --- /dev/null +++ b/.github/scripts/msinsight_acceptance/gui_supervisor.py @@ -0,0 +1,120 @@ +#!/usr/bin/env python3 +"""Parameterized NEW derivative of the real supervisor; syntax checked only. +Run in the same network runtime as the browser, after staging reviewed binaries. +Only its own fresh process group is stopped. Use Ctrl-C in this terminal to stop. +""" + +import argparse +import functools +import http.server +import json +import os +import shutil +import signal +import subprocess +import threading +import time +from pathlib import Path + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--evidence", type=Path, required=True) + args = parser.parse_args() + root = args.evidence.resolve() + stage = root / "server" + for name in [ + "profiler_server", + "libmsinsight.so", + "libsqlite.so", + "mem_snap_dump/tools/dump2db.py", + ]: + if not (stage / name).is_file(): + parser.error("Missing staged file: " + name) + for name in ["gui-root/index.html", "gui-root/plugins/Leaks/index.html"]: + if not (root / name).is_file(): + parser.error("Missing production bundle: " + name) + for name in [ + "server-process.trace", + "server-console.log", + "owned-process.json", + "stop-gui", + ]: + if (root / name).exists(): + parser.error("Use a fresh evidence runtime; existing " + name) + strace = shutil.which("strace") + if not strace: + parser.error("strace is required before server startup") + (root / "home").mkdir(exist_ok=True) + servers = [] + process = None + stop = threading.Event() + for sig in (signal.SIGTERM, signal.SIGINT): + signal.signal(sig, lambda *_: stop.set()) + try: + # Bind HTTP first, so a busy port cannot leave an orphan C++ server. + for port, directory in [ + (5174, root / "gui-root"), + (3007, root / "gui-root/plugins/Leaks"), + ]: + srv = http.server.ThreadingHTTPServer( + ("127.0.0.1", port), + functools.partial(http.server.SimpleHTTPRequestHandler, directory=str(directory)), + ) + servers.append(srv) + threading.Thread(target=srv.serve_forever, daemon=True).start() + with (root / "server-console.log").open("x") as log: + process = subprocess.Popen( + [ + strace, + "-f", + "-tt", + "-s", + "1000", + "-e", + "trace=process", + "-o", + str(root / "server-process.trace"), + "./profiler_server", + "--wsPort=9000", + "--wsHost=127.0.0.1", + ], + cwd=stage, + env=dict(os.environ, LD_LIBRARY_PATH=str(stage), HOME=str(root / "home")), + stdout=log, + stderr=subprocess.STDOUT, + start_new_session=True, + ) + with (root / "owned-process.json").open("x") as record: + json.dump( + { + "supervisor_pid": os.getpid(), + "strace_process_group": process.pid, + "started_epoch": time.time(), + }, + record, + ) + print( + "HTTP listening at http://127.0.0.1:5174/; verify server WS readiness before importing.", + flush=True, + ) + while not stop.wait(0.5) and not (root / "stop-gui").exists(): + if process.poll() is not None: + raise RuntimeError("Server/strace exited: " + str(process.returncode)) + finally: + if process is not None and process.poll() is None: + os.killpg(process.pid, signal.SIGTERM) + try: + process.wait(timeout=10) + except subprocess.TimeoutExpired: + print( + "Owned process has not exited; inspect its recorded group. No force kill performed.", + flush=True, + ) + for srv in servers: + srv.shutdown() + srv.server_close() + + +if __name__ == "__main__": + main() diff --git a/.github/scripts/msinsight_acceptance/protocol_acceptance.py b/.github/scripts/msinsight_acceptance/protocol_acceptance.py new file mode 100644 index 0000000..d4c4b36 --- /dev/null +++ b/.github/scripts/msinsight_acceptance/protocol_acceptance.py @@ -0,0 +1,624 @@ +#!/usr/bin/env python3 +"""Parameterized derivative of the successful pinned 101f65b acceptance client. +This derivative was syntax checked only; its business assertions were not rerun. +The client requires an already-loaded GUI project and never imports it. + +Run only after the GUI WebSocket has disconnected. This client never launches +the producer or modifies snapshot files directly. Import may cause the server +to add derived SQLite cache tables: point --fixture at a runtime copy. +No project overwrite, conflict retry, deletion, or reparse request is performed. +Process-trace/reuse-log verification is deliberately a separate acceptance step. +""" + +import argparse +import asyncio +import contextlib +import datetime as dt +import json +import math +import sys +from pathlib import Path + + +def now(): + return dt.datetime.now(dt.UTC).isoformat() + + +class AcceptanceError(RuntimeError): + pass + + +class Evidence: + def __init__(self, args): + self.output = Path(args.output) + self.output.mkdir(parents=True, exist_ok=True) + names = ("requests.jsonl", "messages.jsonl", "checks.json") + if any((self.output / name).exists() for name in names): + raise AcceptanceError( + "Evidence already exists; select a fresh --output directory. Nothing was overwritten." + ) + self.requests = (self.output / names[0]).open("x", encoding="utf-8") + self.messages = (self.output / names[1]).open("x", encoding="utf-8") + self.result = { + "startedAt": now(), + "status": "running", + "uri": args.uri, + "fixture": args.fixture, + "project": args.project, + "skipImport": args.skip_import, + "sourceRevision": "101f65b877a267ffd5f66ea3834706057ba243e5", + "checks": [], + "queryErrors": [], + "importEvents": [], + "limitations": [ + "Producer process trace and reuse logs must be checked separately.", + "Snapshot source/artifact preservation must be checked separately.", + ], + } + + @staticmethod + def append(handle, value): + handle.write(json.dumps(value, ensure_ascii=False) + "\n") + handle.flush() + + def check(self, name, actual, expected): + # bool is an int subclass; protocol booleans must not pass integer checks. + if isinstance(expected, bool): + passed = actual is expected + elif isinstance(expected, int): + passed = type(actual) is int and actual == expected + elif isinstance(expected, float): + passed = type(actual) in (int, float) and math.isclose( + actual, expected, rel_tol=0, abs_tol=1e-9 + ) + else: + passed = type(actual) is type(expected) and actual == expected + self.result["checks"].append( + {"name": name, "passed": passed, "expected": expected, "actual": actual} + ) + if not passed: + raise AcceptanceError(f"{name}: expected {expected!r}, got {actual!r}") + + def finish(self, fatal=None): + self.result["finishedAt"] = now() + if fatal: + self.result["fatalError"] = fatal + checks = self.result["checks"] + self.result["passedChecks"] = sum(item["passed"] for item in checks) + self.result["failedChecks"] = sum(not item["passed"] for item in checks) + self.result["status"] = ( + "passed" + if not fatal and not self.result["queryErrors"] and not self.result["failedChecks"] + else "failed" + ) + with (self.output / "checks.json").open("x", encoding="utf-8") as handle: + json.dump(self.result, handle, indent=2, ensure_ascii=False) + handle.write("\n") + self.requests.close() + self.messages.close() + return self.result["status"] == "passed" + + +class Client: + def __init__(self, ws, evidence, args): + self.ws, self.evidence, self.args = ws, evidence, args + self.pending = {} + self.events = [] + self.changed = asyncio.Event() + self.next_id = 1 + self.reader_error = None + self.task = asyncio.create_task(self.receive()) + + async def receive(self): + try: + async for raw in self.ws: + wire = raw.decode("utf-8") if isinstance(raw, bytes) else raw + try: + message = json.loads(wire) + except (TypeError, ValueError) as exc: + Evidence.append( + self.evidence.messages, + {"at": now(), "wire": wire, "decodeError": str(exc)}, + ) + raise AcceptanceError("Received non-JSON WebSocket message") from exc + Evidence.append( + self.evidence.messages, + {"at": now(), "wire": wire, "message": message}, + ) + if not isinstance(message, dict): + raise AcceptanceError("Received JSON is not a protocol object") + if message.get("type") == "response": + future = self.pending.get(message.get("requestId")) + if future is not None and not future.done(): + future.set_result(message) + elif message.get("type") == "event": + self.events.append(message) + self.changed.set() + self.reader_error = AcceptanceError("Server closed the WebSocket") + except asyncio.CancelledError: + raise + except Exception as exc: + self.reader_error = exc + finally: + if self.reader_error is not None: + for future in self.pending.values(): + if not future.done(): + future.set_exception(self.reader_error) + self.changed.set() + + async def request(self, label, command, params, module="leaks", include_file=True): + if self.reader_error: + raise self.reader_error + request_id = self.next_id + self.next_id += 1 + payload = { + "id": request_id, + "type": "request", + "moduleName": module, + "command": command, + "projectName": self.args.project, + "params": params, + } + if include_file: + payload["fileId"] = self.args.fixture + wire = json.dumps(payload, ensure_ascii=False, separators=(",", ":")) + future = asyncio.get_running_loop().create_future() + self.pending[request_id] = future + Evidence.append( + self.evidence.requests, + {"at": now(), "label": label, "wire": wire, "request": payload}, + ) + try: + await self.ws.send(wire) + response = await asyncio.wait_for(future, timeout=self.args.request_timeout) + finally: + self.pending.pop(request_id, None) + self.evidence.check(label + ".requestId", response.get("requestId"), request_id) + self.evidence.check(label + ".command", response.get("command"), command) + self.evidence.check(label + ".result", response.get("result"), True) + return response + + async def wait_import(self, start): + deadline = asyncio.get_running_loop().time() + self.args.import_timeout + seen = start + while True: + self.changed.clear() + for event in self.events[seen:]: + self.evidence.result["importEvents"].append(event) + body = event.get("body") or {} + if event.get("event") == "parse/fail": + raise AcceptanceError("Snapshot import reported parse/fail; see messages.jsonl") + if ( + event.get("event") == "parse/leaksMemoryCompleted" + and body.get("module") == "memsnapshot" + and body.get("dbPath") == self.args.fixture + and body.get("snapshotParsingComplete") is True + ): + slices = body.get("snapshotSlices", {}).get("0", {}) + self.evidence.check("import.eventCount", slices.get("eventCount"), 8092) + self.evidence.check("import.sliceCount", slices.get("sliceCount"), 5) + self.evidence.check( + "import.readySlices", slices.get("readySlices"), [0, 1, 2, 3, 4] + ) + return + seen = len(self.events) + if self.reader_error: + raise self.reader_error + remaining = deadline - asyncio.get_running_loop().time() + if remaining <= 0: + raise AcceptanceError("Timed out waiting for matching complete snapshot event") + try: + await asyncio.wait_for(self.changed.wait(), timeout=remaining) + except TimeoutError as exc: + raise AcceptanceError( + "Timed out waiting for matching complete snapshot event" + ) from exc + + async def stop(self): + self.task.cancel() + with contextlib.suppress(asyncio.CancelledError): + await self.task + + +def require_body(response): + body = response.get("body") + if not isinstance(body, dict): + raise AcceptanceError("Response body is missing or not an object") + return body + + +def find_one(items, key, value): + matches = [item for item in items if item.get(key) == value] + if len(matches) != 1: + raise AcceptanceError(f"Expected one {key}={value!r}; got {len(matches)}") + return matches[0] + + +async def run_checks(client, evidence): + check = evidence.check + + async def query(label, suffix, params, validator): + try: + response = await client.request(label, "Memory/snapshot/" + suffix, params) + validator(require_body(response)) + except Exception as exc: + evidence.result["queryErrors"].append( + {"query": label, "error": f"{type(exc).__name__}: {exc}"} + ) + print(f"FAIL {label}: {exc}", flush=True) + else: + print(f"PASS {label}", flush=True) + + for index in (0, 1): + low, high = ((0, 1999), (2000, 3999))[index] + base = {"deviceId": "0", "sliceIndex": index} + + def bounds(label, body, low=low, high=high): + check(label + ".minTimestamp", body.get("minTimestamp"), low) + check(label + ".maxTimestamp", body.get("maxTimestamp"), high) + + def curve(body, index=index, low=low, high=high, bounds=bounds): + label = f"curve.slice{index}" + bounds(label, body) + values = body.get("allocations") + if not isinstance(values, list) or not values: + raise AcceptanceError("Allocation curve missing or empty") + first, last, maximum = ( + (94482944, 419457080, 463470592), + (419465784, 536562752, 580911104), + )[index] + check(label + ".first", values[0], {"timestamp": low, "totalSize": first}) + check(label + ".last", values[-1], {"timestamp": high, "totalSize": last}) + check(label + ".maxSize", body.get("maxSize"), maximum) + check( + label + ".allPointsInRange", + all( + type(x.get("timestamp")) is int and low <= x["timestamp"] <= high + for x in values + ), + True, + ) + + await query(f"curve.slice{index}", "allocations", {**base, "eventType": "malloc"}, curve) + + def reserved(body, index=index, low=low, high=high, bounds=bounds): + label = f"reserved.slice{index}" + bounds(label, body) + values = body.get("reservedLine") + if not isinstance(values, list) or not values: + raise AcceptanceError("Reserved curve missing or empty") + first, last = ((155189248, 463470592), (463470592, 580911104))[index] + check(label + ".first", values[0], {"timestamp": low, "reservedSize": first}) + check(label + ".last", values[-1], {"timestamp": high, "reservedSize": last}) + + await query( + f"reserved.slice{index}", + "allocationLines", + {**base, "eventType": "malloc"}, + reserved, + ) + + boundary = 1999 + index + address = (20623368406528, 20623368415232)[index] + + def events( + body, index=index, low=low, high=high, bounds=bounds, boundary=boundary, address=address + ): + label = f"events.slice{index}" + bounds(label, body) + check(label + ".total", body.get("total"), 2000) + check( + label + ".events", + body.get("events"), + [ + { + "id": boundary, + "action": "alloc", + "address": address, + "size": 8192, + "stream": 1276474240, + } + ], + ) + + await query( + f"events.slice{index}", + "events", + { + **base, + "isTable": False, + "currentPage": 2000 if index == 0 else 1, + "pageSize": 1, + }, + events, + ) + + def event_range( + body, index=index, low=low, high=high, bounds=bounds, boundary=boundary, address=address + ): + label = f"range.slice{index}" + bounds(label, body) + check(label + ".total", body.get("total"), 1) + rows = body.get("events", []) + check(label + ".length", len(rows), 1) + row = rows[0] + check(label + ".id", row.get("id"), boundary) + check(label + ".action", row.get("action"), "alloc") + check(label + ".address", row.get("address"), address) + check(label + ".sizeKiB", row.get("size"), 8.0) + check( + label + ".headersPresent", + isinstance(body.get("headers"), list) and bool(body["headers"]), + True, + ) + + await query( + f"range.slice{index}", + "events", + { + **base, + "isTable": True, + "startTimestamp": 1999, + "endTimestamp": 2000, + "currentPage": 1, + "pageSize": 10, + }, + event_range, + ) + + def blocks(body, index=index, low=low, high=high, bounds=bounds): + label = f"blocks.slice{index}" + bounds(label, body) + total = (1297, 1489)[index] + check(label + ".total", body.get("total"), total) + check(label + ".length", len(body.get("blocks", [])), total) + block = find_one(body["blocks"], "id", 1669) + check( + label + ".crossingBlock", + block, + { + "id": 1669, + "addr": "20623386126336", + "size": 37632, + "_startTimestamp": (1669, 2000)[index], + "_endTimestamp": (1999, 2605)[index], + "allocEventId": 1669, + "freeEventId": 2605, + }, + ) + + await query( + f"blocks.slice{index}", + "blocks", + { + **base, + "eventType": "malloc", + "isTable": False, + "currentPage": 1, + "pageSize": 2000, + }, + blocks, + ) + + def block_detail(body, index=index, low=low, high=high, bounds=bounds): + label = f"blockDetail.slice{index}" + check(label + ".id", body.get("ID"), 1669) + check(label + ".address", body.get("Address"), "20623386126336") + for field in ("Requested Size(MBytes)", "Size(MBytes)"): + check(label + "." + field, body.get(field), 0.036) + for field, event_id, action in ( + ("Alloc Event", 1669, "alloc"), + ("Free Completed Event", 2605, "free_completed"), + ): + event = body.get(field, {}) + check(label + "." + field + ".id", event.get("ID"), event_id) + check(label + "." + field + ".action", event.get("Action"), action) + check( + label + "." + field + ".address", + event.get("Address"), + "0x12c1c19aa800", + ) + check(label + "." + field + ".stream", event.get("Stream"), 1276474240) + check(label + ".allocCallStack", body["Alloc Event"].get("CallStack"), "") + check( + label + ".freeCallStackNonempty", + isinstance(body["Free Completed Event"].get("CallStack"), str) + and bool(body["Free Completed Event"]["CallStack"]), + True, + ) + if index == 0: + check( + label + ".freeRequestedCurrentSliceOnly", + body.get("Free Requested Event"), + {}, + ) + else: + check( + label + ".freeRequestedID", + body.get("Free Requested Event", {}).get("ID"), + 2604, + ) + + await query( + f"blockDetail.slice{index}", + "detail", + {**base, "type": "block", "id": 1669}, + block_detail, + ) + + def cross_event(body, index=index, low=low, high=high, bounds=bounds): + label = f"crossEvent.slice{index}" + check(label + ".id", body.get("ID"), (2605, 1669)[index]) + check( + label + ".action", + body.get("Action"), + ("free_completed", "alloc")[index], + ) + check(label + ".address", body.get("Address"), "0x12c1c19aa800") + check(label + ".sizeMiB", body.get("Size(MBytes)"), 0.036) + + await query( + f"crossEvent.slice{index}", + "detail", + {**base, "type": "event", "id": (2605, 1669)[index]}, + cross_event, + ) + + def state(body, index=index, low=low, high=high, bounds=bounds): + label = f"state.slice{index}" + segments = body.get("segments", []) + check(label + ".segmentCount", len(segments), 2) + small = find_one(segments, "address", "0x12c1c0000000") + large = find_one(segments, "address", "0x12d300000000") + for segment, size in ((small, 44040192), (large, 419430400)): + check( + label + "." + segment["address"] + ".size", + segment.get("size"), + size, + ) + check( + label + "." + segment["address"] + ".stream", + segment.get("stream"), + 1276474240, + ) + check(label + ".smallBlocks", len(small.get("blocks", [])), 688 + index) + check(label + ".largeBlocks", len(large.get("blocks", [])), 113) + check( + label + ".smallAllocated", + small.get("allocated"), + (27246136, 27254840)[index], + ) + check(label + ".largeAllocated", large.get("allocated"), 392210944) + check( + label + ".crossingBlock", + find_one(small["blocks"], "id", 1669), + {"id": 1669, "size": 37632, "offset": 26912768}, + ) + if index == 0: + check( + label + ".block2000Absent", + all( + block.get("id") != 2000 + for segment in segments + for block in segment["blocks"] + ), + True, + ) + else: + check( + label + ".newBlock2000", + find_one(small["blocks"], "id", 2000), + {"id": 2000, "size": 8704, "offset": 9201664}, + ) + + await query(f"state.slice{index}", "state", {**base, "eventId": boundary}, state) + + +async def main_async(args, evidence): + try: + import websockets + except ImportError as exc: + raise AcceptanceError( + "The websockets package is required; no installation was attempted" + ) from exc + async with websockets.connect( + args.uri, + open_timeout=args.connect_timeout, + close_timeout=5, + max_size=32 * 1024 * 1024, + ping_interval=20, + ping_timeout=20, + ) as ws: + client = Client(ws, evidence, args) + try: + await client.request("readiness", "heartCheck", {}, module="global", include_file=False) + if not args.skip_import: + event_start = len(client.events) + try: + await client.request( + "import", + "import/action", + { + "projectName": args.project, + "projectAction": 1, + "path": [args.fixture], + "isConflict": False, + }, + module="timeline", + include_file=False, + ) + await client.wait_import(event_start) + except Exception as exc: + raise AcceptanceError( + f"Import stopped without overwrite or retry: {exc}. If the existing GUI project is already loaded, explicitly use --skip-import --project ." + ) from exc + await run_checks(client, evidence) + finally: + # Keep all import/parse events, including any arriving during queries. + evidence.result["allEvents"] = list(client.events) + await client.stop() + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--uri", default="ws://127.0.0.1:9000/") + parser.add_argument("--fixture", required=True) + parser.add_argument("--project", required=True) + parser.add_argument( + "--skip-import", + action="store_true", + required=True, + help="Query a known already-loaded project; do not import or transfer it", + ) + parser.add_argument( + "--output", + required=True, + help="Fresh evidence directory; existing evidence is never overwritten", + ) + parser.add_argument("--connect-timeout", type=float, default=15) + parser.add_argument("--request-timeout", type=float, default=90) + parser.add_argument("--import-timeout", type=float, default=180) + args = parser.parse_args() + if not Path(args.fixture).is_absolute(): + parser.error("--fixture must be the server's absolute runtime-copy path") + if min(args.connect_timeout, args.request_timeout, args.import_timeout) <= 0: + parser.error("Timeouts must be positive") + import hashlib + + fixture = Path(args.fixture) + if ( + fixture.stat().st_size != 581140 + or hashlib.sha256(fixture.read_bytes()).hexdigest() + != "3afc9d1c5ef4ca4b417e58c0830e9eb8a913eb9459f4088b8c66c22325c68c40" + ): + parser.error("This oracle is only for the reviewed snapshot_expandable.pkl fixture") + evidence = None + fatal = None + try: + evidence = Evidence(args) + asyncio.run(main_async(args, evidence)) + except KeyboardInterrupt: + fatal = "Interrupted by operator" + except Exception as exc: + fatal = f"{type(exc).__name__}: {exc}" + if evidence is None: + print(fatal or "Unable to initialize evidence", file=sys.stderr) + return 1 + success = evidence.finish(fatal) + print( + json.dumps( + { + "status": evidence.result["status"], + "passedChecks": evidence.result["passedChecks"], + "failedChecks": evidence.result["failedChecks"], + "queryErrors": len(evidence.result["queryErrors"]), + "fatalError": fatal, + "results": str(evidence.output / "checks.json"), + } + ) + ) + return 0 if success else 1 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/.github/scripts/msinsight_acceptance/restore_msinsight.sh b/.github/scripts/msinsight_acceptance/restore_msinsight.sh new file mode 100644 index 0000000..77440dc --- /dev/null +++ b/.github/scripts/msinsight_acceptance/restore_msinsight.sh @@ -0,0 +1,50 @@ +#!/usr/bin/env bash +# Draft reproducible restore. Every downloaded source is official and pinned. +set -euo pipefail +E=$(realpath -m "${1:?new absolute output directory}") +test ! -e "$E" +mkdir -p "$E" +python3 -m venv "$E/venv" +git init "$E/source" +git -C "$E/source" fetch --depth 1 https://gitcode.com/Ascend/msinsight.git 101f65b877a267ffd5f66ea3834706057ba243e5 +git -C "$E/source" checkout --detach FETCH_HEAD +test "$(git -C "$E/source" rev-parse HEAD)" = 101f65b877a267ffd5f66ea3834706057ba243e5 +export E + +cd "$E/source/server/third_party" +for spec in 'libuv libuv/libuv v1.44.2 0c1fa696aa502eb749c2c4735005f41ba00a27b8' 'uSockets uNetworking/uSockets v0.8.6 8cd4cb66eb061b2594ca114b9ea1ead64613ad4b' 'uWebSockets uNetworking/uWebSockets v20.48.0 bc2815d95b6ff4cad0973226d0058eadaaec99ad'; do + read -r name repo tag sha <<< "$spec" + git clone --depth 1 --branch "$tag" "https://github.com/$repo.git" "$name" > "$E/$name-fetch.log" 2>&1 + test "$(git -C "$name" rev-parse HEAD)" = "$sha" +done +git init rapidjson; git -C rapidjson fetch --depth 1 https://github.com/Tencent/rapidjson.git 24b5e7a8b27f42fa16b96fc70aade9106cf7102f > "$E/rapidjson-fetch.log" 2>&1; git -C rapidjson checkout --detach FETCH_HEAD +curl -fL --max-time 120 https://www.sqlite.org/2024/sqlite-autoconf-3460100.tar.gz -o "$E/sqlite.tar.gz" +echo '67d3fe6d268e6eaddcae3727fce58fcc8e9c53869bdd07a0c61e38ddf2965071 '"$E/sqlite.tar.gz" | sha256sum -c - +python - <<'PY' +import tarfile,pathlib,os +r=pathlib.Path(os.environ['E']) +with tarfile.open(r/'sqlite.tar.gz') as t: + for n in ['sqlite3.h','sqlite3ext.h','sqlite3.c','shell.c']: + m=t.getmember('sqlite-autoconf-3460100/'+n); assert m.isfile() + out=r/'source/server/third_party/sqlite'/('include' if n.endswith('.h') else 'src')/n + out.parent.mkdir(parents=True,exist_ok=True); out.write_bytes(t.extractfile(m).read()) +PY +"$E/venv/bin/pip" install --index-url https://pypi.org/simple cmake==3.31.6 ninja==1.11.1.4 websockets==15.0.1 > "$E/tools-install.log" 2>&1 +npm install --cache "$E/npm-cache" --prefix "$E/build-tools" --ignore-scripts --registry=https://registry.npmjs.org --strict-ssl=true pnpm@11.25.0 > "$E/pnpm-install.log" 2>&1 + +cd "$E/source/modules" +CI=true npm_config_strict_ssl=true "$E/build-tools/node_modules/.bin/pnpm" install --frozen-lockfile --ignore-scripts --registry=https://registry.npmjs.org --store-dir="$E/pnpm-store" --public-hoist-pattern=rc-select --public-hoist-pattern=rc-tree --public-hoist-pattern=rc-tabs --public-hoist-pattern=rc-table --public-hoist-pattern=@ant-design/icons --public-hoist-pattern=mobx-react-lite --public-hoist-pattern=classnames --public-hoist-pattern=csstype '--public-hoist-pattern=@types*' '--public-hoist-pattern=*eslint*' --public-hoist-pattern=react --public-hoist-pattern=react-dom --public-hoist-pattern=mobx-react --public-hoist-pattern=react-i18next + +export PATH="$E/venv/bin:$PATH" +cmake -S "$E/source/server" -B "$E/cmake-build" -G Ninja -DCMAKE_BUILD_TYPE=Release -DCMAKE_POLICY_VERSION_MINIMUM=3.5 +cmake --build "$E/cmake-build" --target profiler_server -j 3 +# Intentional incremental recheck: verify the completed target is up to date. +cmake --build "$E/cmake-build" --target profiler_server -j 3 > "$E/cmake-recheck.log" 2>&1 +grep -F "ninja: no work to do." "$E/cmake-recheck.log" + +cd "$E/source/modules/leaks" +NODE_OPTIONS='--openssl-legacy-provider --max-old-space-size=2048' NODE_ENV=production GENERATE_SOURCEMAP=false CI=false node ../node_modules/@craco/craco/dist/bin/craco.js build > "$E/leaks-build.log" 2>&1 +cd "$E/source/modules/framework" +NODE_OPTIONS='--openssl-legacy-provider --max-old-space-size=2048' NODE_ENV=production GENERATE_SOURCEMAP=false CI=false node ../node_modules/@craco/craco/dist/bin/craco.js build > "$E/framework-build.log" 2>&1 + +git -C "$E/source" diff --exit-code diff --git a/.github/scripts/msinsight_acceptance/run_consumer.py b/.github/scripts/msinsight_acceptance/run_consumer.py new file mode 100644 index 0000000..2786bc4 --- /dev/null +++ b/.github/scripts/msinsight_acceptance/run_consumer.py @@ -0,0 +1,380 @@ +"""Optional actual GUI consumer runner. Draft: requires hosted execution before any PASS claim. +Raw evidence stays inside --output/private. Publish only summary.json after review. +""" + +import argparse +import hashlib +import json +import os +import re +import shutil +import socket +import subprocess +import sys +import time +import traceback +import urllib.request +from pathlib import Path + +REV = "101f65b877a267ffd5f66ea3834706057ba243e5" +FIXTURE_SHA = "3afc9d1c5ef4ca4b417e58c0830e9eb8a913eb9459f4088b8c66c22325c68c40" + + +def digest(path): + return hashlib.sha256(path.read_bytes()).hexdigest() + + +def clean_env(home): + env = { + k: v + for k, v in os.environ.items() + if not k.startswith(("PT_SNAP_", "PYTHON", "SETUPTOOLS_SCM")) + } + env.update( + HOME=str(home), + XDG_CONFIG_HOME=str(home / ".config"), + XDG_CACHE_HOME=str(home / ".cache"), + PYTHONDONTWRITEBYTECODE="1", + ) + return env + + +GUI_PHASES = ( + "browser_launch", + "page_open", + "import_open", + "fixture_input", + "import_search", + "confirm_ready", + "import_confirm", + "overview_visible", + "window_count", + "window_click", + "window_selected", + "window_ready", + "peak_visible", + "peak_event", + "browser_close", + "page_errors", + "complete", +) +GUI_ERRORS = ("none", "timeout", "assertion", "runtime", "other") + + +def read_gui_diagnostics(path): + """Public output uses only fixed tokens and bounded numeric data, never raw input.""" + invalid = {"phase": "unavailable", "error_kind": "unavailable"} + try: + if path.stat().st_size > 4096: + return invalid + raw = json.loads(path.read_text()) + if not isinstance(raw, dict): + return invalid + phase, error = raw.get("phase"), raw.get("error_kind") + transition = raw.get("transition") + windows, count = raw.get("completed_windows"), raw.get("page_error_count") + if phase not in GUI_PHASES or error not in GUI_ERRORS: + return invalid + if type(transition) is not int or not 0 <= transition <= 3: + return invalid + if not isinstance(windows, list) or len(windows) > 4: + return invalid + if any(type(value) is not int for value in windows): + return invalid + if windows != [1, 2, 1, 4][: len(windows)]: + return invalid + if type(count) is not int or not 0 <= count <= 1000000: + return invalid + return { + "phase": phase, + "error_kind": error, + "transition": transition, + "completed_windows": windows, + "page_error_count": count, + } + except (OSError, ValueError, TypeError, RecursionError): + return invalid + + +def finalize_summary(output, private, summary, process): + try: + if process is not None and process.poll() is None: + (private / "stop-gui").touch(exist_ok=True) + process.wait(timeout=30) + except Exception as exc: + summary["status"] = "failed" + summary["cleanup_failure_type"] = type(exc).__name__ + finally: + (output / "summary.json").write_text(json.dumps(summary, indent=2)) + + +def reuse_logs_valid(logs): + return "is up-to-date" in logs and re.search(r"parse state changed:\s*4\b", logs) is not None + + +def discover_snapshot_parser(source): + """Locate the sole pinned external parser by entrypoint, without brand aliases.""" + scripts = source / "scripts" + if not scripts.is_dir() or scripts.is_symlink(): + raise ValueError("Require a regular consumer scripts directory") + candidates = list(scripts.glob("*/tools/dump2db.py")) + if len(candidates) != 1: + raise ValueError("Require exactly one snapshot parser entrypoint") + entry = candidates[0] + parser = entry.parent.parent + if not entry.is_file() or parser.parent != scripts: + raise ValueError("Invalid parser entrypoint layout") + if any(path.is_symlink() for path in (source, scripts, parser, entry.parent, entry)): + raise ValueError("Parser entrypoint must not traverse symlinks") + if any(path.is_symlink() for path in parser.rglob("*")): + raise ValueError("Parser payload must not contain symlinks") + for name in ("base", "simulate", "tools", "util"): + if not (parser / name).is_dir(): + raise ValueError("Incomplete snapshot parser module layout") + if entry.resolve().parent.parent != parser.resolve(): + raise ValueError("Parser entrypoint escapes its module root") + return parser + + +def main(): + p = argparse.ArgumentParser() + p.add_argument("--root", type=Path, required=True, help="Completed restore_msinsight.sh root") + p.add_argument("--wheel", type=Path, required=True) + p.add_argument("--wheelhouse", type=Path, required=True) + p.add_argument("--wheel-sha256", required=True) + p.add_argument("--fixture", type=Path, required=True) + p.add_argument("--output", type=Path, required=True, help="Fresh private work directory") + a = p.parse_args() + if a.output.exists(): + print(json.dumps({"status": "failed", "failure_type": "ExistingOutput"})) + return 1 + a.output.mkdir() + e = a.output / "private" + e.mkdir() + helpers = Path(__file__).resolve().parent + py = a.root / "venv/bin/python" + cli = a.root / "venv/bin/pt-snap" + (e / "home").mkdir() + env = clean_env(e / "home") + calls = [] + + def run(args, label): + with (e / (label + ".stdout")).open("x") as out, (e / (label + ".stderr")).open("x") as err: + result = subprocess.run( + list(map(str, args)), + cwd=e, + env=env, + stdout=out, + stderr=err, + timeout=600, + check=False, + ) + calls.append({"gate": label, "exit_code": result.returncode}) + if result.returncode: + raise RuntimeError(label + " failed; private diagnostics retained") + + process = None + summary = { + "status": "failed", + "consumer_revision": REV, + "wheel_sha256": ( + a.wheel_sha256 + if len(a.wheel_sha256) == 64 and all(c in "0123456789abcdef" for c in a.wheel_sha256) + else None + ), + "fixture_sha256": FIXTURE_SHA, + "gates": calls, + "GUI": "NOT_RUN", + } + try: + assert a.root.is_absolute() and a.output.is_absolute() + assert digest(a.wheel) == a.wheel_sha256 + assert a.fixture.stat().st_size == 581140 and digest(a.fixture) == FIXTURE_SHA + assert ( + subprocess.check_output( + ["git", "-C", str(a.root / "source"), "rev-parse", "HEAD"], + text=True, + stderr=subprocess.PIPE, + env=env, + ).strip() + == REV + ) + run( + [ + py, + "-m", + "pip", + "install", + "--no-index", + "--find-links", + a.wheelhouse, + a.wheel, + ], + "wheel-install", + ) + run( + [ + py, + helpers / "verify_installed_wheel.py", + "--wheel", + a.wheel, + "--expected-sha256", + a.wheel_sha256, + "--output", + e / "installed-wheel.json", + ], + "installed-wheel-byte-audit", + ) + (e / "input").mkdir() + original = e / "input/snapshot_expandable.pkl" + shutil.copy2(a.fixture, original) + run( + [ + cli, + "import", + original, + "--format", + "msinsight", + "--events-per-slice", + "2000", + "--device", + "0", + "--no-focus", + "--json", + ], + "export", + ) + exported = json.loads((e / "export.stdout").read_text()) + assert exported["ok"] and exported["slice_count"] == 5 + shutil.copytree(e / "input", e / "runtime-input") + runtime = e / "runtime-input/snapshot_expandable.pkl" + artifact = original.with_name(original.name + ".msinsight") + runtime_artifact = runtime.with_name(runtime.name + ".msinsight") + original_hashes = { + str(p.relative_to(artifact)): digest(p) for p in artifact.rglob("*") if p.is_file() + } + run( + [ + py, + helpers / "evidence_inventory.py", + "--artifact", + artifact, + "--compare", + runtime_artifact, + "--mode", + "exact", + "--output", + e / "pre-gui-exact.json", + ], + "pre-gui-exact", + ) + stage = e / "server" + stage.mkdir() + binary_root = a.root / "source/server/output/linux-x86_64/bin" + for name in ("profiler_server", "libmsinsight.so", "libsqlite.so"): + shutil.copy2(binary_root / name, stage / name) + shutil.copytree( + discover_snapshot_parser(a.root / "source"), + stage / "mem_snap_dump", + ignore=shutil.ignore_patterns("__pycache__"), + ) + shutil.copytree(a.root / "source/modules/framework/build", e / "gui-root") + shutil.copytree(a.root / "source/modules/leaks/build", e / "gui-root/plugins/Leaks") + with (e / "supervisor.stdout").open("x") as log: + process = subprocess.Popen( + [str(py), str(helpers / "gui_supervisor.py"), "--evidence", str(e)], + env=env, + stdout=log, + stderr=subprocess.STDOUT, + ) + deadline = time.monotonic() + 60 + while True: + if process.poll() is not None: + raise RuntimeError("Loopback supervisor exited") + try: + with urllib.request.urlopen("http://127.0.0.1:5174/", timeout=1) as response: + assert response.status == 200 + # HTTP binds before the C++ server; a TCP-only readiness probe + # must succeed too, without taking the sole WebSocket session. + with socket.create_connection(("127.0.0.1", 9000), timeout=1): + pass + break + except OSError: + if time.monotonic() > deadline: + raise RuntimeError("Loopback readiness timeout") from None + time.sleep(0.25) + summary["GUI"] = "FAILED" + run( + [ + py, + helpers / "gui_navigation.py", + "--fixture", + runtime, + "--output", + e / "gui-summary.json", + ], + "GUI-navigation", + ) + summary["GUI"] = "PASS import/windows 1-2-1-4 only" + run( + [ + py, + helpers / "protocol_acceptance.py", + "--skip-import", + "--project", + runtime, + "--fixture", + runtime, + "--output", + e / "protocol", + ], + "protocol", + ) + protocol_result = json.loads((e / "protocol/checks.json").read_text()) + assert protocol_result["failedChecks"] == 0 and protocol_result["passedChecks"] == 167 + summary["protocol_assertions"] = 167 + (e / "stop-gui").touch(exist_ok=False) + process.wait(timeout=30) + trace = (e / "server-process.trace").read_text() + execs = [line for line in trace.splitlines() if "execve(" in line or "execveat(" in line] + assert len(execs) == 1 and "./profiler_server" in execs[0] + logs = "\n".join(p.read_text(errors="replace") for p in stage.glob("*.log")) + assert reuse_logs_valid(logs) + run( + [ + py, + helpers / "evidence_inventory.py", + "--artifact", + artifact, + "--compare", + runtime_artifact, + "--mode", + "logical", + "--output", + e / "post-gui-logical.json", + ], + "post-gui-logical", + ) + assert digest(original) == digest(runtime) == FIXTURE_SHA + assert original_hashes == { + str(p.relative_to(artifact)): digest(p) for p in artifact.rglob("*") if p.is_file() + } + summary.update( + status="passed", + producer_reuse="UP_TO_DATE/state4 and one server exec", + preserved_original=True, + runtime_cache_mutation="allowed derived table only; runtime attestation not repaired", + ) + except Exception as exc: + summary["failure_type"] = type(exc).__name__ + (e / "failure.txt").write_text(traceback.format_exc()) + finally: + if summary["GUI"] != "NOT_RUN": + summary["gui_diagnostics"] = read_gui_diagnostics(e / "gui-summary.json") + finalize_summary(a.output, e, summary, process) + print(json.dumps({"status": summary["status"], "failure_type": summary.get("failure_type")})) + return 0 if summary["status"] == "passed" else 1 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/.github/scripts/msinsight_acceptance/verify_installed_wheel.py b/.github/scripts/msinsight_acceptance/verify_installed_wheel.py new file mode 100644 index 0000000..7449a2e --- /dev/null +++ b/.github/scripts/msinsight_acceptance/verify_installed_wheel.py @@ -0,0 +1,72 @@ +#!/usr/bin/env python3 +"""New helper, syntax checked only. Run using the intended installed venv Python. +Compares all pt_snap_cli/ wheel members with that interpreter's installed package. +No pickle execution. Does not authenticate a wheel's origin or dependency safety. +""" + +import argparse +import hashlib +import importlib.util +import json +import sys +import zipfile +from pathlib import Path + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--wheel", type=Path, required=True) + parser.add_argument("--expected-sha256", required=True) + parser.add_argument("--output", type=Path, required=True) + args = parser.parse_args() + actual = hashlib.sha256(args.wheel.read_bytes()).hexdigest() + if actual != args.expected_sha256: + parser.error("Wheel SHA256 mismatch") + if args.output.exists(): + parser.error("Select a fresh output filename") + spec = importlib.util.find_spec("pt_snap_cli") + if not spec or not spec.origin: + parser.error("No installed pt_snap_cli package in this interpreter") + root = Path(spec.origin).resolve().parent + if not root.is_relative_to(Path(sys.prefix).resolve()): + parser.error("Package is outside the current interpreter prefix") + if "site-packages" not in root.parts and "dist-packages" not in root.parts: + parser.error("Package is not in a regular installed package directory") + checked, mismatches = [], [] + with zipfile.ZipFile(args.wheel) as wheel: + for entry in wheel.infolist(): + if entry.is_dir() or not entry.filename.startswith("pt_snap_cli/"): + continue + relative = Path(entry.filename).relative_to("pt_snap_cli") + if ".." in relative.parts: + parser.error("Invalid wheel member path") + installed = root / relative + expected = hashlib.sha256(wheel.read(entry)).hexdigest() + matches = ( + installed.is_file() + and hashlib.sha256(installed.read_bytes()).hexdigest() == expected + ) + checked.append(str(relative)) + if not matches: + mismatches.append(str(relative)) + if not checked: + parser.error("No package members found") + with args.output.open("x") as out: + json.dump( + { + "wheel_sha256": actual, + "package_member_count": len(checked), + "members": checked, + "mismatches": mismatches, + }, + out, + indent=2, + ) + out.write("\n") + if mismatches: + raise SystemExit("Installed package differs from wheel") + print("Verified", len(checked), "installed package members") + + +if __name__ == "__main__": + main() diff --git a/.github/scripts/run_installed_html_acceptance.py b/.github/scripts/run_installed_html_acceptance.py new file mode 100644 index 0000000..047a09f --- /dev/null +++ b/.github/scripts/run_installed_html_acceptance.py @@ -0,0 +1,555 @@ +"""Pinned candidate wheel -> isolated console -> real loopback Chromium acceptance. + +Public artifacts are summary.json and two explicitly named invented-label privacy +proof PNGs. Real-fixture screenshots, raw paths, HTML, databases and CLI outputs +remain runner-private. +""" + +from __future__ import annotations + +import argparse +import hashlib +import importlib.util +import json +import os +import re +import sqlite3 +import subprocess +import sys +import tempfile +import threading +import traceback +import venv +from collections import Counter, defaultdict +from contextlib import closing +from functools import partial +from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer +from pathlib import Path +from zipfile import ZipFile + +CANDIDATE = "07bdf660f10f03a9c13ab313fae149adf62355f8" +FIXTURE_NAME = "snapshot_with_empty_cache.pkl" +FIXTURE_SHA = "d5cb93fa1e1689f0debb31cbc064d1cbc13f2f44204b9c76dd2c47aa8ba85b3f" +LOCAL_REFERENCE_WHEEL_SHA = "b3448724b16a638cf68d00b62cedfa892b6c396b8dc9b9550187357f95290bf3" + + +def sha(path): + return hashlib.sha256(Path(path).read_bytes()).hexdigest() + + +class AcceptanceError(RuntimeError): + """Only fixed, explicitly safe diagnostic messages may use this type.""" + + +def require(condition, message): + if not condition: + raise AcceptanceError(message) + + +def load_module(path, name): + spec = importlib.util.spec_from_file_location(name, path) + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module + + +def verify_input(source, wheel): + actual = subprocess.check_output(["git", "rev-parse", "HEAD"], cwd=source, text=True).strip() + require(actual == CANDIDATE, "Candidate source SHA mismatch") + audit = load_module(source / ".github/scripts/audit_wheel.py", "html_wheel_audit") + require(not audit.audit_wheel(wheel, source / "src"), "Wheel/source byte audit failed") + provenance = load_module(source / "tests/_fixture_provenance.py", "html_fixture_gate") + fixtures = source / "tests/fixtures/snapshots" + provenance.verify_snapshot_fixtures(fixtures, fixtures / "SHA256SUMS") + fixture = fixtures / FIXTURE_NAME + require( + sha(fixture) == FIXTURE_SHA and fixture.stat().st_size == 617060, + "Trusted executable fixture identity mismatch", + ) + return fixture + + +def tree_oracle(db, event): + """Reconstruct caller paths per live block; no product imports or template SQL.""" + expected = defaultdict( + lambda: {"size_bytes": 0, "requested_bytes": 0, "block_count": 0, "self_bytes": 0} + ) + with closing(sqlite3.connect(f"file:{db}?mode=ro", uri=True)) as con: + con.row_factory = sqlite3.Row + blocks = con.execute( + "SELECT b.*,t.callstackId FROM block_0 b LEFT JOIN trace_entry_0 t " + "ON t.id=b.allocEventId WHERE (b.allocEventId=-1 OR b.allocEventId<=?) " + "AND (b.freeEventId IS NULL OR b.freeEventId<0 OR b.freeEventId>?)", + (event, event), + ).fetchall() + for block in blocks: + path = ["root"] + if block["allocEventId"] == -1: + path.append("static" if block["freeEventId"] == -1 else "preexisting_live_at_event") + else: + frames = con.execute( + "SELECT frameId FROM callstack_frame WHERE callstackId=? ORDER BY position DESC", + (block["callstackId"],), + ).fetchall() + if frames: + identity = "frame" + for frame in frames: + identity += "/" + str(frame[0]) + path.append(identity) + else: + empty = con.execute( + "SELECT 1 FROM callstack_frame_manifest WHERE callstackId=? AND frameCount=0", + (block["callstackId"],), + ).fetchone() + path.append("empty_callstack" if empty else "missing") + for identity in path: + row = expected[identity] + row["size_bytes"] += block["size"] + row["requested_bytes"] += block["requestedSize"] or 0 + row["block_count"] += 1 + expected[path[-1]]["self_bytes"] += block["size"] + return dict(expected) + + +def compare_tree(nodes, expected): + require(set(nodes) == set(expected), "Oracle node identity set mismatch") + total = expected["root"]["size_bytes"] + for identity, row in expected.items(): + for field, value in row.items(): + require(nodes[identity][field] == value, f"Oracle {field} mismatch") + percent = round(row["size_bytes"] * 100 / total, 4) if total else None + require(nodes[identity]["percent_of_total"] == percent, "Oracle proportion mismatch") + + +def sanitized_screenshot(page, output, name, nodes): + # Capture-only CSS avoids coordinate-mask drift during full-page scrolling. + # Screenshots remain private until this replacement has independent visual proof. + selectors = [ + "#chart text", + "#detail", + "#focus-path", + "#children-list", + "#search-results", + "#search", + ] + covered = page.locator(",".join(selectors)).all_text_contents() + require( + all( + n["label"] in "\n".join(covered) + for n in nodes.values() + if page.locator('#chart .node[data-node-id="' + n["node_id"] + '"]').count() + ), + "Screenshot privacy coverage failed", + ) + target = output / f"{name}.png" + capture_style = ",".join(selectors) + "{visibility:hidden!important;}" + raw = page.screenshot(full_page=True, style=capture_style) + require(raw.startswith(b"\x89PNG\r\n\x1a\n"), "Invalid screenshot PNG") + cursor = 8 + while cursor < len(raw): + require(cursor + 12 <= len(raw), "Incomplete screenshot PNG chunk") + length = int.from_bytes(raw[cursor : cursor + 4], "big") + kind = raw[cursor + 4 : cursor + 8] + require(kind not in {b"tEXt", b"zTXt", b"iTXt"}, "Unexpected PNG text metadata") + cursor += length + 12 + require(cursor <= len(raw), "Incomplete screenshot PNG payload") + # No rejected bytes reach the public allowlist, even when CI uploads after failure. + with tempfile.NamedTemporaryFile( + dir=output.parent, prefix="html-proof-", delete=False + ) as staged: + staged.write(raw) + staging_path = Path(staged.name) + try: + os.replace(staging_path, target) + finally: + staging_path.unlink(missing_ok=True) + return { + "name": target.name, + "sha256": sha(target), + "privacy": "runner-private; capture-only CSS hides fixture text; visual proof pending", + } + + +class QuietHandler(SimpleHTTPRequestHandler): + def do_GET(self): + if self.path == "/favicon.ico": + self.send_response(204) + self.end_headers() + return + super().do_GET() + + def log_message(self, *_args): + pass + + +def browser_checks(html, nodes, output, summary): + from playwright.sync_api import expect, sync_playwright + + output.mkdir(parents=True, exist_ok=True) + errors = [] + root = nodes["root"] + server = ThreadingHTTPServer( + ("127.0.0.1", 0), partial(QuietHandler, directory=str(html.parent)) + ) + thread = threading.Thread(target=server.serve_forever, daemon=True) + thread.start() + try: + with sync_playwright() as runtime: + with runtime.chromium.launch(headless=True) as browser: + summary["browser_version"] = browser.version + context = browser.new_context( + viewport={"width": 1440, "height": 1000}, locale="en-US" + ) + try: + page = context.new_page() + page.on("pageerror", lambda _e: errors.append("pageerror")) + page.on( + "console", + lambda e: errors.append("console-error") if e.type == "error" else None, + ) + + def selected(identity): + expect(page.locator("#chart .node").first).to_have_attribute( + "data-node-id", identity + ) + expect(page.locator("#detail")).to_contain_text(identity) + require( + page.locator("#focus-path").evaluate("e=>e===document.activeElement"), + "Keyboard focus not retained", + ) + + def geometry(): + return page.locator("#chart .node").evaluate_all( + "gs=>gs.map(g=>({id:g.dataset.nodeId,width:Number(g.querySelector('rect').getAttribute('width'))}))" + ) + + def shot(name): + summary["screenshots"].append( + sanitized_screenshot(page, output, name, nodes) + ) + + page.goto( + f"http://127.0.0.1:{server.server_port}/{html.name}", wait_until="load" + ) + expect(page.locator("#chart .node")).to_have_count(len(nodes)) + expect(page.locator("#summary")).to_have_text( + f"Showing {root['size_bytes']:,} bytes" + ) + initial = geometry() + for row in initial: + wanted = max( + 0, 1200 * nodes[row["id"]]["size_bytes"] / root["size_bytes"] - 0.6 + ) + require( + abs(row["width"] - wanted) < 1e-6, "DOM proportional geometry mismatch" + ) + summary["checks"].append("initial-node-count-bytes-proportions") + shot("initial") + deepest = max(nodes.values(), key=lambda n: n["depth"]) + search = page.locator("#search") + search.fill(deepest["node_id"].upper()) + expect(page.locator("#search-results button")).to_have_count(1) + require(geometry() == initial, "Search changed proportional layout") + shot("search") + # Reach result via native Tab order, then Enter, rather than focusing it directly. + search.focus() + for _ in range(200): + page.keyboard.press("Tab") + if page.locator("#search-results button").evaluate( + "e=>e===document.activeElement" + ): + break + else: + raise AcceptanceError("Search result unreachable by keyboard") + page.keyboard.press("Enter") + selected(deepest["node_id"]) + shot("deep-focus") + summary["checks"].append("case-insensitive-search-keyboard-deep-focus") + page.locator("#parent").click() + selected(deepest["parent_id"]) + for _ in range(3): + page.locator("#reset").click() + selected("root") + require(geometry() == initial, "Repeated reset changed geometry") + search.fill("没有匹配_RELEASE_NEVER_EXISTS") + expect(page.locator("#search-results button")).to_have_count(0) + expect(page.locator("#search-status")).to_have_text("No matching frames.") + shot("empty") + search.fill("") + require(geometry() == initial, "Empty-search recovery changed geometry") + summary["checks"].append("parent-repeated-reset-unicode-no-results-recovery") + for label, count in Counter(n["label"] for n in nodes.values()).items(): + if count > 1: + search.fill(label.swapcase()) + texts = page.locator("#search-results button").all_text_contents() + require( + len(texts) >= count and len(set(texts)) == len(texts), + "Duplicate path ambiguity", + ) + for node in nodes.values(): + if "<" in node["label"]: + search.fill(node["node_id"]) + require( + node["label"] in page.locator("#search-results").inner_text(), + "HTML-like text lost", + ) + require( + page.locator( + "#chart script,#chart img,#search-results script,#search-results img" + ).count() + == 0, + "Unexpected injected elements", + ) + summary["checks"].append("duplicate-paths-html-like-labels") + search.fill("") + child = next( + n + for n in nodes.values() + if n["parent_id"] == "root" and n["size_bytes"] > 0 + ) + group = page.locator('#chart .node[data-node-id="' + child["node_id"] + '"]') + group.hover() + expect(page.locator("#detail")).to_contain_text(child["node_id"]) + expect(group.locator("title")).to_contain_text( + f"Inclusive: {child['size_bytes']:,} bytes" + ) + group.click() + selected(child["node_id"]) + page.goto("about:blank") + page.go_back(wait_until="load") + restored = page.locator("#chart .node").first.get_attribute("data-node-id") + require( + restored in {"root", child["node_id"]}, "Unexpected Back navigation focus" + ) + expect(page.locator("#summary")).to_have_text( + f"Showing {nodes[restored]['size_bytes']:,} bytes" + ) + expect(page.locator("#detail")).to_contain_text(restored) + page.locator("#reset").click() + selected("root") + expect(page.locator("#chart .node")).to_have_count(len(nodes)) + require(geometry() == initial, "Back and Reset did not restore root geometry") + summary["checks"].append("hover-tooltip-svg-zoom-browser-back") + require(not errors, "Browser console/page errors") + summary["console_page_error_count"] = len(errors) + finally: + context.close() + finally: + server.shutdown() + server.server_close() + thread.join(timeout=5) + + +def synthetic_privacy_checks(output, summary): + """Safe public proof for the capture helper; never uses real fixture labels.""" + from playwright.sync_api import expect, sync_playwright + + label = "SYNTHETIC_ONLY/release_fixture.py:1 invented_frame" + # Intentionally standalone synthetic page, separate from actual installed-CLI evidence. + html = """

Invented-label privacy proof

+

All labels on this synthetic page are invented and safe to publish.

+ + Showing 40 bytes + + + SYNTHETIC_ONLY/release_fixture.py:1 invented_frame +
SYNTHETIC_ONLY/invented_path
+
SYNTHETIC_ONLY/invented_detail
+ +

Search all frames.

+ + """ + nodes = {"root": {"node_id": "root", "label": label}} + proofs = [] + with sync_playwright() as runtime: + with runtime.chromium.launch(headless=True) as browser: + context = browser.new_context(viewport={"width": 1440, "height": 800}) + try: + page = context.new_page() + page.set_content(html, wait_until="load") + expect(page.locator("#chart text")).to_have_text(label) + proofs.append( + sanitized_screenshot(page, output, "privacy-synthetic-initial", nodes) + ) + page.locator("#focus-path").focus() + page.locator("#search").fill("SYNTHETIC_ONLY_NO_RESULTS") + expect(page.locator("#search-status")).to_have_text("No matching frames.") + page.evaluate("window.scrollTo(0,180)") + require( + page.evaluate("window.scrollY") > 0, "Synthetic privacy scroll not exercised" + ) + require( + page.locator("#search").evaluate("e=>e===document.activeElement"), + "Synthetic privacy focus not exercised", + ) + proofs.append(sanitized_screenshot(page, output, "privacy-synthetic-empty", nodes)) + finally: + context.close() + for proof in proofs: + proof["privacy"] = "invented labels only; safe by construction; CSS visual review pending" + summary["synthetic_privacy_proof"] = { + "source": "standalone invented labels; not product/real-fixture UI evidence", + "capture_function": "same screenshot-only CSS capture helper as private real screenshots", + "scrolled_focused_empty_state": True, + "screenshots": proofs, + "visual_review": "pending independent inspection", + } + + +def run(args, summary): + source, wheel, wheelhouse, output = ( + getattr(args, key).resolve() for key in ("source", "wheel", "wheelhouse", "output_dir") + ) + fixture = verify_input(source, wheel) + summary.update( + candidate_sha=CANDIDATE, + fixture_sha256=sha(fixture), + wheel_sha256=sha(wheel), + local_reference_wheel_sha256=LOCAL_REFERENCE_WHEEL_SHA, + same_binary_as_local_reference=sha(wheel) == LOCAL_REFERENCE_WHEEL_SHA, + wheel_source_byte_audit="PASS", + ) + with ZipFile(wheel) as archive: + metadata = archive.read( + next(n for n in archive.namelist() if n.endswith(".dist-info/METADATA")) + ).decode() + summary["wheel_version"] = re.search(r"^Version: (.+)$", metadata, re.M).group(1) + with tempfile.TemporaryDirectory(prefix="ptsnap-html-") as temp: + private = Path(temp) + home, cwd, environment = (private / n for n in ("home", "work", "venv")) + home.mkdir() + cwd.mkdir() + venv.EnvBuilder(with_pip=True, system_site_packages=False).create(environment) + python, cli = environment / "bin/python", environment / "bin/pt-snap" + env = { + k: v + for k, v in os.environ.items() + if not k.startswith(("PYTHON", "PT_SNAP_", "SETUPTOOLS_SCM")) + } + env.update(HOME=str(home), PYTHONNOUSERSITE="1", NO_COLOR="1", TERM="dumb") + + command_index = 0 + + def command(argv): + nonlocal command_index + command_index += 1 + result = subprocess.run( + [str(x) for x in argv], cwd=cwd, env=env, capture_output=True, timeout=120 + ) + private_log = args.private_dir / f"command-{command_index:02d}" + private_log.with_suffix(".stdout").write_bytes(result.stdout) + private_log.with_suffix(".stderr").write_bytes(result.stderr) + private_log.with_suffix(".argv.json").write_text(json.dumps([str(x) for x in argv])) + require( + result.returncode == 0, + "Isolated subprocess failed (raw output retained only privately)", + ) + return result.stdout + + command([python, "-m", "pip", "install", "--no-index", "--find-links", wheelhouse, wheel]) + origin = json.loads( + command( + [ + python, + "-c", + "import pt_snap_cli,json,sys;print(json.dumps({'origin':pt_snap_cli.__file__,'prefix':sys.prefix}))", + ] + ) + ) + require(Path(origin["origin"]).is_relative_to(environment), "Package escaped clean venv") + require(Path(origin["prefix"]) == environment, "Wrong installed interpreter") + summary["isolated_install"] = ( + "PASS: no system packages; isolated HOME/CWD; no source PYTHONPATH" + ) + command([cli, "import", "--help"]) + command([cli, "report", "memory-tree", "--help"]) + imported = json.loads( + command( + [ + cli, + "import", + fixture, + "--output-dir", + cwd / "imported", + "--format", + "single-db", + "--no-focus", + "--json", + ] + ) + ) + require(imported["ok"] is True, "CLI import did not succeed") + db = Path(imported["db_path"]) + before = sha(db) + with closing(sqlite3.connect(f"file:{db}?mode=ro", uri=True)) as con: + event = con.execute( + "SELECT id FROM trace_entry_0 WHERE id>=0 ORDER BY active DESC,id LIMIT 1" + ).fetchone()[0] + raw = command([cli, "report", "memory-tree", db, "--event-id", event, "--format", "html"]) + html = cwd / "memory-tree.html" + html.write_bytes(raw) + payload = json.loads( + re.search( + rb'', raw, re.S + ).group(1) + ) + nodes = {n["node_id"]: n for n in payload["nodes"]} + require(len(nodes) == len(payload["nodes"]), "Duplicate node IDs") + compare_tree(nodes, tree_oracle(db, event)) + summary.update( + html_sha256=sha(html), + database_sha256=before, + event_id=event, + node_count=len(nodes), + root_bytes=nodes["root"]["size_bytes"], + max_depth=max(n["depth"] for n in nodes.values()), + unicode_label_count=sum(any(ord(c) > 127 for c in n["label"]) for n in nodes.values()), + synthetic_edges="Separate source 12-case browser suite; not installed-wheel evidence", + ) + summary["checks"].append("installed-cli-import-report-independent-sql-oracle") + browser_checks(html, nodes, args.private_dir / "screenshots", summary) + require(sha(db) == before and sha(fixture) == FIXTURE_SHA, "Source/database changed") + summary["checks"].append("source-database-immutable") + synthetic_privacy_checks(output, summary) + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + for name in ("source", "wheel", "wheelhouse", "output-dir"): + parser.add_argument("--" + name, type=Path, required=True) + parser.add_argument("--private-dir", type=Path) + args = parser.parse_args() + args.private_dir = ( + args.private_dir or args.output_dir.with_name(args.output_dir.name + "-private") + ).resolve() + if args.private_dir == args.output_dir.resolve() or args.private_dir.is_relative_to( + args.output_dir.resolve() + ): + parser.error("Private diagnostics must be outside the public artifact directory") + args.private_dir.mkdir(parents=True, exist_ok=True) + args.output_dir.mkdir(parents=True, exist_ok=True) + summary = {"status": "FAIL", "checks": [], "screenshots": []} + try: + run(args, summary) + summary["status"] = "PASS" + except Exception as exc: + # Avoid publishing traceback, absolute capture paths or fixture contents. + summary["failure_type"] = type(exc).__name__ + (args.private_dir / "failure.txt").write_text(traceback.format_exc()) + if isinstance(exc, AcceptanceError): + summary["failure_reason"] = str(exc) + print("Installed HTML acceptance failed; see sanitized summary.", file=sys.stderr) + finally: + (args.output_dir / "summary.json").write_text(json.dumps(summary, indent=2) + "\n") + return 0 if summary["status"] == "PASS" else 1 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/.github/workflows/installed-html-acceptance.yml b/.github/workflows/installed-html-acceptance.yml new file mode 100644 index 0000000..d30b8ad --- /dev/null +++ b/.github/workflows/installed-html-acceptance.yml @@ -0,0 +1,66 @@ +name: Pinned installed HTML acceptance +on: + pull_request: + branches: [main] + types: [opened, synchronize, reopened] + paths: + - '.github/scripts/run_installed_html_acceptance.py' + - '.github/workflows/installed-html-acceptance.yml' + - 'tests/test_installed_html_acceptance.py' +permissions: + contents: read +jobs: + installed-html: + runs-on: ubuntu-latest + timeout-minutes: 15 + steps: + - name: Checkout reviewed harness + uses: actions/checkout@v4 + with: + ref: ${{ github.event.pull_request.head.sha }} + path: harness + persist-credentials: false + - name: Checkout exact product candidate + uses: actions/checkout@v4 + with: + ref: 07bdf660f10f03a9c13ab313fae149adf62355f8 + path: candidate + fetch-depth: 0 + persist-credentials: false + - uses: actions/setup-python@v5 + with: + python-version: '3.13' + - name: Install bounded build and browser tooling + run: | + python -m pip install build==1.6.1 setuptools==84.0.0 setuptools-scm==9.2.2 wheel==0.48.0 playwright==1.63.0 pytest==9.1.1 + python -m playwright install --with-deps chromium + - name: Build and audit exact candidate + working-directory: candidate + run: | + test "$(git rev-parse HEAD)" = 07bdf660f10f03a9c13ab313fae149adf62355f8 + export SOURCE_DATE_EPOCH="$(git show -s --format=%ct HEAD)" + python -m build --wheel --no-isolation --outdir "$RUNNER_TEMP/html-wheel" + python .github/scripts/audit_wheel.py --wheel "$RUNNER_TEMP"/html-wheel/*.whl + python -m pip download --only-binary=:all: --dest "$RUNNER_TEMP/html-wheelhouse" "$RUNNER_TEMP"/html-wheel/*.whl + - name: Verify harness unit contracts + working-directory: harness + run: python -m pytest tests/test_installed_html_acceptance.py -c /dev/null + - name: Run real installed CLI and browser acceptance + run: | + python harness/.github/scripts/run_installed_html_acceptance.py \ + --source "$GITHUB_WORKSPACE/candidate" \ + --wheel "$RUNNER_TEMP"/html-wheel/*.whl \ + --wheelhouse "$RUNNER_TEMP/html-wheelhouse" \ + --output-dir "$RUNNER_TEMP/html-safe-evidence" \ + --private-dir "$RUNNER_TEMP/html-private-evidence" + - name: Upload JSON and invented-label privacy proof only + if: always() + uses: actions/upload-artifact@v4 + with: + name: pinned-installed-html-evidence + path: | + ${{ runner.temp }}/html-safe-evidence/summary.json + ${{ runner.temp }}/html-safe-evidence/privacy-synthetic-initial.png + ${{ runner.temp }}/html-safe-evidence/privacy-synthetic-empty.png + if-no-files-found: error + retention-days: 7 diff --git a/.github/workflows/msinsight-acceptance.yml b/.github/workflows/msinsight-acceptance.yml new file mode 100644 index 0000000..9c46643 --- /dev/null +++ b/.github/workflows/msinsight-acceptance.yml @@ -0,0 +1,79 @@ +name: Optional pinned msinsight consumer acceptance +on: + pull_request: + branches: [main] + types: [opened, synchronize, reopened] + paths: + - ".github/scripts/msinsight_acceptance/**" + - ".github/workflows/msinsight-acceptance.yml" + - "tests/test_msinsight_acceptance.py" + workflow_dispatch: +permissions: + contents: read +jobs: + pinned-consumer: + runs-on: ubuntu-24.04 + timeout-minutes: 45 + steps: + - name: Checkout reviewed test harness + uses: actions/checkout@v4 + with: + ref: ${{ github.event.pull_request.head.sha || github.sha }} + path: harness + persist-credentials: false + - name: Checkout frozen product candidate + uses: actions/checkout@v4 + with: + ref: 07bdf660f10f03a9c13ab313fae149adf62355f8 + path: candidate + fetch-depth: 0 + persist-credentials: false + - uses: actions/setup-python@v5 + with: + python-version: '3.12' + - uses: actions/setup-node@v4 + with: + node-version: '24.19.0' + - name: Install official build tools + run: | + sudo apt-get update + sudo apt-get install -y build-essential strace libssl-dev zlib1g-dev + python -m pip install build==1.6.1 setuptools==84.0.0 setuptools-scm==9.2.2 wheel==0.48.0 pytest==9.1.1 PyYAML==6.0.3 + - name: Verify fast fail-closed harness contracts + working-directory: harness + run: python -m pytest tests/test_msinsight_acceptance.py -c /dev/null + - name: Build and audit separately identified hosted candidate wheel + working-directory: candidate + run: | + test "$(git rev-parse HEAD)" = 07bdf660f10f03a9c13ab313fae149adf62355f8 + export SOURCE_DATE_EPOCH="$(git show -s --format=%ct HEAD)" + python -m build --wheel --no-isolation --outdir "$RUNNER_TEMP/ms-wheel" + python .github/scripts/audit_wheel.py --wheel "$RUNNER_TEMP"/ms-wheel/*.whl + python -m pip download --only-binary=:all: --dest "$RUNNER_TEMP/ms-wheelhouse" "$RUNNER_TEMP"/ms-wheel/*.whl + - name: Restore pinned original consumer, sequential minimal builds + env: + PLAYWRIGHT_BROWSERS_PATH: ${{ runner.temp }}/ms-playwright-browsers + run: | + bash harness/.github/scripts/msinsight_acceptance/restore_msinsight.sh "$RUNNER_TEMP/ms-restore" > "$RUNNER_TEMP/ms-restore.log" 2>&1 + "$RUNNER_TEMP/ms-restore/venv/bin/python" -m pip install --index-url https://pypi.org/simple playwright==1.63.0 + "$RUNNER_TEMP/ms-restore/venv/bin/python" -m playwright install --with-deps chromium + - name: Real GUI, protocol, reuse and preservation gates + env: + PLAYWRIGHT_BROWSERS_PATH: ${{ runner.temp }}/ms-playwright-browsers + run: | + WHEEL=$(find "$RUNNER_TEMP/ms-wheel" -maxdepth 1 -name '*.whl' -print) + test "$(find "$RUNNER_TEMP/ms-wheel" -maxdepth 1 -name '*.whl' | wc -l)" = 1 + SHA=$(sha256sum "$WHEEL" | cut -d' ' -f1) + "$RUNNER_TEMP/ms-restore/venv/bin/python" harness/.github/scripts/msinsight_acceptance/run_consumer.py \ + --root "$RUNNER_TEMP/ms-restore" --wheel "$WHEEL" --wheel-sha256 "$SHA" \ + --wheelhouse "$RUNNER_TEMP/ms-wheelhouse" \ + --fixture "$GITHUB_WORKSPACE/candidate/tests/fixtures/snapshots/snapshot_expandable.pkl" \ + --output "$RUNNER_TEMP/ms-evidence" + - name: Upload sanitized numeric result only + if: always() + uses: actions/upload-artifact@v4 + with: + name: pinned-msinsight-safe-summary + path: ${{ runner.temp }}/ms-evidence/summary.json + if-no-files-found: error + retention-days: 7 diff --git a/tests/test_installed_html_acceptance.py b/tests/test_installed_html_acceptance.py new file mode 100644 index 0000000..69485f8 --- /dev/null +++ b/tests/test_installed_html_acceptance.py @@ -0,0 +1,264 @@ +"""Fail-closed contracts for the separate installed-HTML release runner.""" + +from __future__ import annotations + +import importlib.util +import io +import json +import sqlite3 +from contextlib import closing +from pathlib import Path +from types import SimpleNamespace +from zipfile import ZipFile + +import pytest + +SCRIPT = Path(__file__).resolve().parents[1] / ".github/scripts/run_installed_html_acceptance.py" + + +@pytest.fixture +def runner(): + spec = importlib.util.spec_from_file_location("installed_html_runner", SCRIPT) + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module + + +def test_independent_oracle_uses_lifetime_and_ordered_caller_paths(runner, tmp_path): + db = tmp_path / "test.db" + with closing(sqlite3.connect(db)) as con, con: + con.executescript(""" + CREATE TABLE block_0(size, requestedSize, allocEventId, freeEventId); + CREATE TABLE trace_entry_0(id, callstackId); + CREATE TABLE callstack_frame(callstackId, position, frameId); + CREATE TABLE callstack_frame_manifest(callstackId, frameCount); + INSERT INTO block_0 VALUES(10,8,1,5),(20,16,2,NULL),(4,4,-1,-1),(90,80,9,NULL); + INSERT INTO trace_entry_0 VALUES(1,7),(2,8),(9,7); + INSERT INTO callstack_frame VALUES(7,0,1),(7,1,2),(8,0,1),(8,1,3); + INSERT INTO callstack_frame_manifest VALUES(7,2),(8,2); + """) + before = runner.sha(db) + rows = runner.tree_oracle(db, 4) + assert rows["root"]["size_bytes"] == 34 + assert rows["frame/2/1"]["self_bytes"] == 10 + assert rows["frame/3/1"]["self_bytes"] == 20 + assert rows["static"]["size_bytes"] == 4 + assert runner.tree_oracle(db, 5)["root"]["size_bytes"] == 24 + assert runner.sha(db) == before + + +@pytest.mark.parametrize( + "field", ["size_bytes", "self_bytes", "block_count", "requested_bytes", "percent_of_total"] +) +def test_compare_tree_rejects_wrong_values(runner, field): + expected = {"root": {"size_bytes": 10, "self_bytes": 0, "block_count": 1, "requested_bytes": 8}} + nodes = {"root": {**expected["root"], "percent_of_total": 100.0}} + runner.compare_tree(nodes, expected) + nodes["root"][field] += 1 + with pytest.raises(runner.AcceptanceError): + runner.compare_tree(nodes, expected) + + +def test_compare_tree_rejects_missing_nodes(runner): + with pytest.raises(runner.AcceptanceError, match="identity"): + runner.compare_tree({}, {"root": {}}) + + +@pytest.mark.parametrize("exception", [RuntimeError, AssertionError]) +def test_failure_summary_never_exposes_private_exception( + runner, monkeypatch, tmp_path, exception, capsys +): + output = tmp_path / "public" + monkeypatch.setattr( + "sys.argv", + [ + "runner", + "--source", + ".", + "--wheel", + "x", + "--wheelhouse", + ".", + "--output-dir", + str(output), + ], + ) + + def fail(*_args): + raise exception("/private/person/captured_callstack.py secret-user-label") + + monkeypatch.setattr(runner, "run", fail) + assert runner.main() == 1 + raw = (output / "summary.json").read_text() + assert "private" not in raw and "secret-user-label" not in raw + assert json.loads(raw)["failure_type"] == exception.__name__ + assert "secret-user-label" in (tmp_path / "public-private/failure.txt").read_text() + captured = capsys.readouterr() + assert "secret-user-label" not in captured.out + captured.err + assert "/private/person" not in captured.out + captured.err + + +def test_workflow_is_read_only_candidate_pinned_and_artifact_allowlisted(): + workflow = (SCRIPT.parents[1] / "workflows/installed-html-acceptance.yml").read_text() + assert "contents: read" in workflow and "write" not in workflow + assert "ref: 07bdf660f10f03a9c13ab313fae149adf62355f8" in workflow + assert "pull_request_target" not in workflow and "secrets." not in workflow + assert "persist-credentials: false" in workflow + assert "html-safe-evidence/summary.json" in workflow + assert "*.png" not in workflow + assert "html-safe-evidence/privacy-synthetic-initial.png" in workflow + assert "html-safe-evidence/privacy-synthetic-empty.png" in workflow + for real_name in ("initial.png", "search.png", "deep-focus.png", "empty.png"): + assert "html-safe-evidence/" + real_name not in workflow + assert "html-wheel/*.whl" in workflow + + +@pytest.mark.parametrize( + "path,status", [("/favicon.ico", 204), ("/not-found", 404), ("/favicon.ico/extra", 404)] +) +def test_http_handler_only_suppresses_exact_favicon(runner, tmp_path, path, status): + class Request: + def __init__(self): + self.response = bytearray() + + def makefile(self, mode, *_args): + assert mode == "rb" + return io.BytesIO(f"GET {path} HTTP/1.0\r\nHost: localhost\r\n\r\n".encode()) + + def sendall(self, data): + self.response.extend(data) + + request = Request() + runner.QuietHandler(request, ("127.0.0.1", 1), None, directory=str(tmp_path)) + assert bytes(request.response).startswith(f"HTTP/1.0 {status} ".encode()) + if status == 204: + assert bytes(request.response).endswith(b"\r\n\r\n") + + +@pytest.mark.parametrize("kind", [b"tEXt", b"iTXt", b"zTXt"]) +def test_rejected_screenshot_never_reaches_public_artifacts(runner, tmp_path, kind): + output = tmp_path / "public" + output.mkdir() + + class Locator: + def all_text_contents(self): + return ["safe-label"] + + def count(self): + return 1 + + class Page: + def locator(self, _selector): + return Locator() + + def screenshot(self, **kwargs): + assert "path" not in kwargs + return b"\x89PNG\r\n\x1a\n" + (0).to_bytes(4, "big") + kind + b"\0" * 4 + + with pytest.raises(runner.AcceptanceError, match="metadata"): + runner.sanitized_screenshot( + Page(), output, "rejected", {"root": {"node_id": "root", "label": "safe-label"}} + ) + assert list(output.iterdir()) == [] + + +def test_capture_uses_css_not_coordinate_masks(runner, tmp_path): + output = tmp_path / "private" + output.mkdir() + + class Locator: + def all_text_contents(self): + return ["synthetic-label"] + + def count(self): + return 1 + + class Page: + def locator(self, _selector): + return Locator() + + def screenshot(self, **kwargs): + assert "path" not in kwargs and "mask" not in kwargs + style = kwargs["style"] + assert ( + style + == "#chart text,#detail,#focus-path,#children-list,#search-results,#search{visibility:hidden!important;}" + ) + return b"\x89PNG\r\n\x1a\n" + (0).to_bytes(4, "big") + b"IEND" + b"\0" * 4 + + result = runner.sanitized_screenshot( + Page(), output, "synthetic", {"root": {"node_id": "root", "label": "synthetic-label"}} + ) + assert (output / "synthetic.png").exists() + assert "runner-private" in result["privacy"] + + +def test_actual_browser_failure_routes_screenshots_only_to_private(runner, monkeypatch, tmp_path): + public = tmp_path / "public" + private = tmp_path / "private" + public.mkdir() + private.mkdir() + fixture = tmp_path / "fixture" + fixture.write_bytes(b"unit-only input; never deserialized") + wheel = tmp_path / "wheel.whl" + with ZipFile(wheel, "w") as archive: + archive.writestr("unit.dist-info/METADATA", "Version: 1.0\n") + db = tmp_path / "unit.db" + with closing(sqlite3.connect(db)) as con, con: + con.execute("CREATE TABLE trace_entry_0(id, active)") + con.execute("INSERT INTO trace_entry_0 VALUES(1,40)") + row = { + "node_id": "root", + "label": "invented", + "depth": 0, + "size_bytes": 40, + "requested_bytes": 40, + "block_count": 1, + "self_bytes": 40, + "percent_of_total": 100.0, + } + monkeypatch.setattr(runner, "verify_input", lambda *_: fixture) + monkeypatch.setattr( + runner, + "tree_oracle", + lambda *_: { + "root": { + k: row[k] for k in ("size_bytes", "requested_bytes", "block_count", "self_bytes") + } + }, + ) + monkeypatch.setattr(runner.venv.EnvBuilder, "create", lambda *_: None) + + def fake_run(argv, **_kwargs): + if len(argv) > 1 and argv[1] == "-c": + prefix = Path(argv[0]).parents[1] + raw = json.dumps( + {"origin": str(prefix / "lib/pt_snap_cli/__init__.py"), "prefix": str(prefix)} + ) + elif "import" in argv and "--json" in argv: + raw = json.dumps({"ok": True, "db_path": str(db)}) + elif "--format" in argv and "html" in argv: + raw = ( + '" + ) + else: + raw = "" + return SimpleNamespace(returncode=0, stdout=raw.encode(), stderr=b"") + + monkeypatch.setattr(runner.subprocess, "run", fake_run) + observed = [] + + def fail_browser(_html, _nodes, destination, _summary): + observed.append(destination) + raise RuntimeError("simulated browser failure") + + monkeypatch.setattr(runner, "browser_checks", fail_browser) + args = SimpleNamespace( + source=tmp_path, wheel=wheel, wheelhouse=tmp_path, output_dir=public, private_dir=private + ) + with pytest.raises(RuntimeError, match="simulated"): + runner.run(args, {"checks": [], "screenshots": []}) + assert observed == [private / "screenshots"] + assert list(public.iterdir()) == [] diff --git a/tests/test_msinsight_acceptance.py b/tests/test_msinsight_acceptance.py new file mode 100644 index 0000000..63a7bd2 --- /dev/null +++ b/tests/test_msinsight_acceptance.py @@ -0,0 +1,363 @@ +"""Fail-closed contracts; these tests do not run a producer, browser or server.""" + +import importlib.util +import json +import subprocess +import sys +from pathlib import Path + +import pytest + +CANDIDATE = ( + Path(__file__).resolve().parents[1] / ".github/scripts/msinsight_acceptance/run_consumer.py" +) +SCRIPT = CANDIDATE if CANDIDATE.exists() else Path(__file__).with_name("run_consumer.py") +spec = importlib.util.spec_from_file_location("ms_consumer_acceptance", SCRIPT) +module = importlib.util.module_from_spec(spec) +spec.loader.exec_module(module) + + +def arguments(tmp_path, monkeypatch): + wheel = tmp_path / "candidate.whl" + wheel.write_bytes(b"wheel") + fixture = tmp_path / "fixture.pkl" + fixture.write_bytes(b"x" * 581140) + out = tmp_path / "evidence" + argv = [ + "run_consumer", + "--root", + str(tmp_path / "restore"), + "--wheel", + str(wheel), + "--wheelhouse", + str(tmp_path / "wheelhouse"), + "--wheel-sha256", + "a" * 64, + "--fixture", + str(fixture), + "--output", + str(out), + ] + monkeypatch.setattr(sys, "argv", argv) + return wheel, fixture, out + + +def test_host_environment_is_scrubbed(monkeypatch, tmp_path): + for name in ( + "PT_SNAP_DB_PATH", + "PT_SNAP_QUERY_TIMEOUT", + "PYTHONPATH", + "PYTHONOPTIMIZE", + "SETUPTOOLS_SCM_PRETEND_VERSION", + ): + monkeypatch.setenv(name, "private-value") + env = module.clean_env(tmp_path) + assert not any(k.startswith(("PT_SNAP_", "SETUPTOOLS_SCM")) for k in env) + assert {k for k in env if k.startswith("PYTHON")} == {"PYTHONDONTWRITEBYTECODE"} + assert env["HOME"] == str(tmp_path) + assert env["XDG_CONFIG_HOME"] == str(tmp_path / ".config") + + +def test_wrong_wheel_fails_before_subprocess_and_records_safe_summary( + tmp_path, monkeypatch, capsys +): + _, _, out = arguments(tmp_path, monkeypatch) + monkeypatch.setattr( + module.subprocess, "check_output", lambda *a, **k: pytest.fail("Must not execute git") + ) + monkeypatch.setattr( + module.subprocess, "run", lambda *a, **k: pytest.fail("Must not install/load") + ) + assert module.main() == 1 + summary = json.loads((out / "summary.json").read_text()) + assert summary["status"] == "failed" + assert summary["failure_type"] == "AssertionError" + assert str(tmp_path) not in capsys.readouterr().out + assert str(tmp_path) not in json.dumps(summary) + + +def test_wrong_fixture_fails_before_any_executable_step(tmp_path, monkeypatch): + wheel, _, out = arguments(tmp_path, monkeypatch) + monkeypatch.setattr(module, "digest", lambda p: "a" * 64 if p == wheel else "b" * 64) + monkeypatch.setattr( + module.subprocess, + "check_output", + lambda *a, **k: pytest.fail("No process before exact fixture verification"), + ) + monkeypatch.setattr(module.subprocess, "run", lambda *a, **k: pytest.fail("No pickle consumer")) + assert module.main() == 1 + assert json.loads((out / "summary.json").read_text())["failure_type"] == "AssertionError" + + +def test_external_runtime_error_is_private_and_fail_closed(tmp_path, monkeypatch, capsys): + wheel, _, out = arguments(tmp_path, monkeypatch) + monkeypatch.setattr(module, "digest", lambda p: "a" * 64 if p == wheel else module.FIXTURE_SHA) + monkeypatch.setattr(module.subprocess, "check_output", lambda *a, **k: module.REV) + + class PlaywrightError(Exception): + pass + + def fail(*args, **kwargs): + raise PlaywrightError("/private/user/source.py secret-callstack") + + monkeypatch.setattr(module.subprocess, "run", fail) + assert module.main() == 1 + summary = json.loads((out / "summary.json").read_text()) + assert summary["status"] == "failed" + assert summary["failure_type"] == "PlaywrightError" + assert "secret-callstack" not in json.dumps(summary) + assert "/private/user" not in capsys.readouterr().out + assert "secret-callstack" in (out / "private/failure.txt").read_text() + + +def test_cleanup_timeout_cannot_mask_summary(tmp_path): + private = tmp_path / "private" + private.mkdir() + + class Process: + def poll(self): + return None + + def wait(self, timeout): + raise subprocess.TimeoutExpired("/private/server", timeout) + + summary = {"status": "passed"} + module.finalize_summary(tmp_path, private, summary, Process()) + result = json.loads((tmp_path / "summary.json").read_text()) + assert result == {"status": "failed", "cleanup_failure_type": "TimeoutExpired"} + assert (private / "stop-gui").exists() + + +def test_workflow_uploads_only_safe_summary(): + workflow = SCRIPT.parents[2] / "workflows/msinsight-acceptance.yml" + if not workflow.exists(): + workflow = Path(__file__).with_name("msinsight-acceptance.yml") + text = workflow.read_text() + assert "contents: read" in text and "persist-credentials: false" in text + assert "audit_wheel.py" in text and "07bdf660f10f03a9c13ab313fae149adf62355f8" in text + upload = text.split("name: Upload sanitized numeric result only")[1] + assert "ms-evidence/summary.json" in upload + assert "*.png" not in upload and "private" not in upload + + +@pytest.mark.parametrize( + "line", ["parse state changed:4", "parse state changed: 4", "parse state changed: 4"] +) +def test_reuse_state_accepts_real_log_spacing(line): + assert module.reuse_logs_valid("artifact is up-to-date.\n" + line) + + +@pytest.mark.parametrize( + "line", ["parse state changed:40", "parse state changed: 3", "no parser state"] +) +def test_reuse_state_rejects_wrong_or_missing_state(line): + assert not module.reuse_logs_valid("artifact is up-to-date.\n" + line) + + +def test_browser_cache_uses_runner_context_only_at_step_scope(): + workflow = SCRIPT.parents[2] / "workflows/msinsight-acceptance.yml" + import yaml + + job = yaml.safe_load(workflow.read_text())["jobs"]["pinned-consumer"] + assert "runner." not in json.dumps(job.get("env", {})) + needed = { + "Restore pinned original consumer, sequential minimal builds", + "Real GUI, protocol, reuse and preservation gates", + } + steps = [s for s in job["steps"] if s.get("name") in needed] + assert len(steps) == 2 + assert all( + s["env"]["PLAYWRIGHT_BROWSERS_PATH"] == "${{ runner.temp }}/ms-playwright-browsers" + for s in steps + ) + + +def parser_tree(source, name="snapshot_parser"): + root = source / "scripts" / name + for directory in ("base", "simulate", "tools", "util"): + (root / directory).mkdir(parents=True, exist_ok=True) + (root / "tools/dump2db.py").write_text("# synthetic parser entrypoint\n") + return root + + +def test_parser_discovery_uses_unique_entrypoint_and_module_layout(tmp_path): + root = parser_tree(tmp_path) + assert module.discover_snapshot_parser(tmp_path) == root + + +def test_parser_discovery_rejects_missing_entrypoint(tmp_path): + (tmp_path / "scripts").mkdir() + with pytest.raises(ValueError, match="exactly one"): + module.discover_snapshot_parser(tmp_path) + + +def test_parser_discovery_rejects_ambiguous_entrypoints(tmp_path): + parser_tree(tmp_path, "first") + parser_tree(tmp_path, "second") + with pytest.raises(ValueError, match="exactly one"): + module.discover_snapshot_parser(tmp_path) + + +def test_parser_discovery_rejects_symlinked_entrypoint(tmp_path, monkeypatch): + root = parser_tree(tmp_path) + entry = root / "tools/dump2db.py" + original = Path.is_symlink + monkeypatch.setattr(Path, "is_symlink", lambda path: path == entry or original(path)) + with pytest.raises(ValueError, match="symlinks"): + module.discover_snapshot_parser(tmp_path) + + +def test_parser_discovery_rejects_symlinked_payload(tmp_path, monkeypatch): + root = parser_tree(tmp_path) + link = root / "util/escape.py" + link.write_text("# simulated link entry for a platform-independent guard check\n") + original = Path.is_symlink + monkeypatch.setattr(Path, "is_symlink", lambda path: path == link or original(path)) + with pytest.raises(ValueError, match="symlinks"): + module.discover_snapshot_parser(tmp_path) + + +def test_parser_discovery_rejects_incomplete_module(tmp_path): + root = parser_tree(tmp_path) + (root / "simulate").rmdir() + with pytest.raises(ValueError, match="Incomplete"): + module.discover_snapshot_parser(tmp_path) + + +def gui_module(): + spec = importlib.util.spec_from_file_location("ms_gui", SCRIPT.with_name("gui_navigation.py")) + gui = importlib.util.module_from_spec(spec) + spec.loader.exec_module(gui) + return gui + + +@pytest.mark.parametrize( + "exception,kind", + [ + (TimeoutError, "timeout"), + (AssertionError, "assertion"), + (RuntimeError, "runtime"), + (ValueError, "other"), + ], +) +def test_gui_failure_breadcrumbs_never_include_exception_text(tmp_path, exception, kind): + gui = gui_module() + output = tmp_path / "gui.json" + secret = "/private/session/source.py DOM secret_value" + + def fail(state, checks, errors): + state["phase"] = "window_selected" + state["transition"] = 1 + checks.append({"window": 1}) + errors.append(secret) + raise exception(secret) + + with pytest.raises(exception): + gui.record_navigation(output, fail) + raw = output.read_text() + assert secret not in raw and "private" not in raw + safe = module.read_gui_diagnostics(output) + assert safe == { + "phase": "window_selected", + "error_kind": kind, + "transition": 1, + "completed_windows": [1], + "page_error_count": 1, + } + assert gui.GUI_PHASES == module.GUI_PHASES + assert gui.GUI_ERRORS == module.GUI_ERRORS + + +def test_gui_success_breadcrumbs(tmp_path): + output = tmp_path / "gui.json" + gui_module().record_navigation( + output, lambda state, checks, errors: checks.extend({"window": n} for n in [1, 2, 1, 4]) + ) + safe = module.read_gui_diagnostics(output) + assert safe["phase"] == "complete" + assert safe["error_kind"] == "none" + assert safe["completed_windows"] == [1, 2, 1, 4] + + +@pytest.mark.parametrize( + "field,value", + [ + ("phase", "/private/DOM"), + ("error_kind", "SecretException"), + ("transition", True), + ("transition", 4), + ("completed_windows", [1, "/private/DOM"]), + ("completed_windows", [True]), + ("completed_windows", [2]), + ("page_error_count", "/private/DOM"), + ("page_error_count", -1), + ], +) +def test_gui_diagnostic_input_is_fail_closed(tmp_path, field, value): + output = tmp_path / "gui.json" + raw = { + "phase": "complete", + "error_kind": "none", + "transition": 3, + "completed_windows": [1, 2, 1, 4], + "page_error_count": 0, + } + raw[field] = value + output.write_text(json.dumps(raw)) + assert module.read_gui_diagnostics(output) == { + "phase": "unavailable", + "error_kind": "unavailable", + } + + +@pytest.mark.parametrize("payload", ["not json /private/DOM", "[]", "x" * 4097]) +def test_gui_missing_or_malformed_diagnostics_are_safe(tmp_path, payload): + output = tmp_path / "gui.json" + assert module.read_gui_diagnostics(output)["phase"] == "unavailable" + output.write_text(payload) + assert module.read_gui_diagnostics(output)["phase"] == "unavailable" + + +def test_gui_extra_fields_and_custom_exception_name_are_not_published(tmp_path): + gui = gui_module() + secret = "/private/DOM-secret" + assert gui.error_kind(type(secret, (Exception,), {})(secret)) == "other" + output = tmp_path / "gui.json" + output.write_text( + json.dumps( + { + "phase": "confirm_ready", + "error_kind": "timeout", + "transition": 0, + "completed_windows": [], + "page_error_count": 0, + "raw_traceback": secret, + } + ) + ) + safe = module.read_gui_diagnostics(output) + assert "raw_traceback" not in safe and secret not in json.dumps(safe) + + +def test_gui_deeply_nested_json_cannot_bypass_final_summary(tmp_path): + output = tmp_path / "gui.json" + output.write_text("[" * 1100 + "0" + "]" * 1100) + assert output.stat().st_size < 4096 + assert module.read_gui_diagnostics(output) == { + "phase": "unavailable", + "error_kind": "unavailable", + } + + +def test_gui_decoder_recursion_error_is_fail_closed(tmp_path, monkeypatch): + output = tmp_path / "gui.json" + output.write_text("{}") + + def fail(_): + raise RecursionError("private decoder context") + + monkeypatch.setattr(module.json, "loads", fail) + assert module.read_gui_diagnostics(output) == { + "phase": "unavailable", + "error_kind": "unavailable", + }