"""Node: gate_checker. Independent re-verification of the applied pair. It does not read the executor receipt as evidence. It re-reads the tree, checks the shape is gone, checks the lock test exists and asserts something, and runs the suite itself. """ import ast import json import os import sys ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0, ROOT) from lingologos_thinkingpython import session, shapes # noqa: E402 def main() -> int: task = json.load(sys.stdin) workspace, plan = task["workspace"], task["plan"] problems = [] path = os.path.join(workspace, plan["file"]) try: with open(path, "r", encoding="utf-8") as handle: source = handle.read() except OSError: print(json.dumps({"node": "gate_checker", "status": "fail", "reasons": ["SEARCH_NOT_FOUND"]})) return 1 for edit in plan["edits"]: if edit["replace"] and edit["replace"] not in source: problems.append("PAIR_DISHONEST") break hits, _ = shapes.match(plan["frame"], source) if any(h["symbol"] == plan["symbol"] and h["line"] == plan["line"] for h in hits): problems.append("SHAPE_STILL_PRESENT") lock_name = "test_lock_%s.py" % plan["symbol"] lock_path = os.path.join(workspace, "tests", lock_name) if not os.path.isfile(lock_path): problems.append("PAIR_DISHONEST") else: with open(lock_path, "r", encoding="utf-8") as handle: tree = ast.parse(handle.read()) tests = [n for n in ast.walk(tree) if isinstance(n, ast.FunctionDef) and n.name.startswith("test_")] asserts = [n for n in ast.walk(tree) if isinstance(n, ast.Assert)] if not tests or not asserts: problems.append("PAIR_DISHONEST") cwd = os.getcwd() os.chdir(workspace) try: suite = session.run_suite(workspace) finally: os.chdir(cwd) if suite["status"] != "GREEN": problems.append(suite["status"]) status = "pass" if not problems else "fail" print(json.dumps({"node": "gate_checker", "status": status, "reasons": sorted(set(problems)), "suite": suite["status"]})) return 0 if status == "pass" else 1 if __name__ == "__main__": raise SystemExit(main())