"""Node: adversary. Independent, and its job is to make the claim fail. It looks for the ways a green suite can still be a lie: an assertion that was removed, a lock test that asserts nothing about the change, an edit outside the window the locator found, a shape that is still there under another name. """ 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 shapes # noqa: E402 def _assertion_count(source: str) -> int: try: tree = ast.parse(source) except SyntaxError: return -1 return sum(1 for n in ast.walk(tree) if isinstance(n, ast.Assert)) def main() -> int: task = json.load(sys.stdin) workspace, plan = task["workspace"], task["plan"] attacks = [] 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": "adversary", "status": "fail", "reasons": ["SEARCH_NOT_FOUND"]})) return 1 if _assertion_count(source) < 0: attacks.append("SUITE_RED") lock_path = os.path.join(workspace, "tests", "test_lock_%s.py" % plan["symbol"]) if os.path.isfile(lock_path): with open(lock_path, "r", encoding="utf-8") as handle: lock = handle.read() body = "\n".join(line for line in lock.splitlines() if line.startswith(" ") and line.strip()) if len(body.strip()) < 20: attacks.append("PAIR_DISHONEST") else: attacks.append("PAIR_DISHONEST") hits, _ = shapes.match(plan["frame"], source) if hits and not any(h["symbol"] != plan["symbol"] for h in hits) and \ any(h["line"] == plan["line"] for h in hits): attacks.append("SHAPE_STILL_PRESENT") for edit in plan["edits"]: search, replace = edit.get("search", ""), edit.get("replace", "") if search and search in source and search not in replace: attacks.append("PAIR_DISHONEST") status = "survived" if not attacks else "refuted" print(json.dumps({"node": "adversary", "status": status, "reasons": sorted(set(attacks))})) return 0 if __name__ == "__main__": raise SystemExit(main())