Download architecture/selftest.py from EnergyVenom/patchwise-agent-studio: direct link, hf CLI and curl.
- Browser
- Download file 7.53 kB
-
https://huggingface.co/spaces/EnergyVenom/patchwise-agent-studio/resolve/main/architecture/selftest.py
- Command line
-
hf download hf://spaces/EnergyVenom/patchwise-agent-studio/architecture/selftest.py
-
curl -L -o selftest.py https://huggingface.co/spaces/EnergyVenom/patchwise-agent-studio/resolve/main/architecture/selftest.py
7.53 kB
| """Stub-only self-test: python selftest.py (exit 0 = all checks pass). | |
| Uses the REAL triage/classifier.py and reports/generate_report.py on synthetic stub reports.""" | |
| import json | |
| import os | |
| import tempfile | |
| from pathlib import Path | |
| os.environ["P2P_RUNS_DIR"] = tempfile.mkdtemp(prefix="p2p-selftest-") | |
| os.environ["PLAYWRIGHT_MODE"] = "stub" | |
| os.environ["P2P_CASES_DIR"] = tempfile.mkdtemp(prefix="p2p-nocases-") # pin to SAMPLE_CASES for determinism | |
| from langgraph.types import Command # noqa: E402 | |
| import p2p_graph as g # noqa: E402 | |
| CHECKS = [] | |
| def check(name, cond, detail=""): | |
| CHECKS.append(name) | |
| assert cond, f"FAILED: {name} {detail}" | |
| print(f" ok {name}") | |
| def run(env=None, approve=True, review=None, include_p2=False, saver=None, thread="t"): | |
| for k in ("STUB_FAIL", "STUB_DEFECT", "STUB_UI", "STUB_FLAKY", "FRONTIER_SUMMARY", "LOCAL_TRIAGE_HINTS"): | |
| os.environ.pop(k, None) | |
| os.environ.update(env or {}) | |
| graph = g.build_graph(saver) | |
| cfg = {"configurable": {"thread_id": thread}} | |
| graph.invoke({"patch_id": thread, "include_p2": include_p2}, cfg) | |
| gates, items = [], [] | |
| while graph.get_state(cfg).next: | |
| v = graph.get_state(cfg).tasks[0].interrupts[0].value | |
| gates.append(v["gate"]) | |
| if v["gate"] == "approve_cases": | |
| graph.invoke(Command(resume={"approved": v["pending_case_ids"] if approve else []}), cfg) | |
| else: | |
| items.extend(v["items"]) | |
| graph.invoke(Command(resume={"review": review or {}}), cfg) | |
| run.items = items | |
| return graph.get_state(cfg).values, gates | |
| def attempt_ids(path): | |
| return [s["tags"][0].lstrip("@") for s in g._walk_specs(json.loads(Path(path).read_text())["suites"])] | |
| print("selftest (stub Playwright, real classifier + report generator)") | |
| # 1. all pass | |
| s, gates = run(thread="allpass") | |
| check("all-pass: one approval gate, one attempt, no human review", gates == ["approve_cases"] and len(s["attempt_files"]) == 1) | |
| check("all-pass: QA rule says GO (C1-C6 all ok)", s["decision"] == "GO", json.dumps(s["counts"])) | |
| # 2. decision is delegated, not recomputed | |
| gr = g.report_generator() | |
| tri = json.loads(Path(s["triage_path"]).read_text()) | |
| again = gr.summarize(tri, Path(s["report_root"]), {}) | |
| check("decision == generate_report.summarize() on the same inputs", s["decision"] == again["decision"]) | |
| check("graph source has no go/no-go rule of its own", | |
| 'decision = "GO"' not in Path(g.__file__).read_text() and '"NO-GO"' not in Path(g.__file__).read_text()) | |
| # 3. flaky P1 -> exactly one retry, failed case only; QA: flaky is not a pass -> NO-GO; owner asked | |
| s, gates = run({"STUB_FLAKY": "P2P-REQ-001"}, review={"P2P-REQ-001": {"owner": "automation"}}, thread="flaky") | |
| check("flaky: exactly 2 attempts", len(s["attempt_files"]) == 2 and s["retry_count"] == 1) | |
| check("flaky: retry re-ran only the failed case", attempt_ids(s["attempt_files"][1]) == ["P2P-REQ-001"]) | |
| check("flaky: counted flaky=1 failed=0", s["counts"]["flaky"] == 1 and s["counts"]["failed"] == 0) | |
| check("flaky: human asked for an owner", run.items == [{"case_id": "P2P-REQ-001", "ask": "owner"}]) | |
| check("flaky P1: NO-GO under QA rule (flaky is not a pass)", s["decision"] == "NO-GO") | |
| # 4. hard unclear failure on P1 -> one retry only, human review, NO-GO | |
| s, gates = run({"STUB_FAIL": "P2P-PAY-001"}, review={"P2P-PAY-001": {"confirmed": "not_defect"}}, thread="hard") | |
| check("hard fail: never more than one retry (2 attempts)", len(s["attempt_files"]) == 2) | |
| check("hard fail: low confidence goes to human review", gates == ["approve_cases", "human_review"]) | |
| check("hard fail P1: NO-GO", s["decision"] == "NO-GO") | |
| # 5. P2 failures: delegation follows QA C2 (verdict present; confirmed defect needs a waiver) | |
| s, gates = run({"STUB_UI": "P2P-RCV-001"}, include_p2=True, thread="p2-ui") | |
| check("P2 ui_change (confident) -> no human gate, GO with gap", gates == ["approve_cases"] and s["decision"] == "GO") | |
| s, _ = run({"STUB_DEFECT": "P2P-RCV-001"}, include_p2=True, | |
| review={"P2P-RCV-001": {"confirmed": "real_defect"}}, thread="p2-defect") | |
| check("P2 real defect (match amounts kept) confirmed, no waiver -> NO-GO", s["decision"] == "NO-GO") | |
| s, _ = run({"STUB_DEFECT": "P2P-RCV-001"}, include_p2=True, | |
| review={"P2P-RCV-001": {"confirmed": "real_defect", "waiver": "CAB-123 accepted"}}, thread="p2-waived") | |
| check("P2 confirmed defect with waiver -> GO", s["decision"] == "GO") | |
| # 6. priority scope | |
| s, _ = run(thread="scope-p1") | |
| ins = {c["id"] for c in s["test_cases"]} | |
| dfr = {d["id"]: d["reason"] for d in s["deferred"]} | |
| check("scope default: P1 only (RCV P2 + SUP P3 deferred)", "P2P-RCV-001" not in ins and set(dfr) == {"P2P-RCV-001", "P2P-SUP-001"}) | |
| s, _ = run(include_p2=True, thread="scope-p2") | |
| check("scope include_p2: P2 runs, P3 still deferred", | |
| "P2P-RCV-001" in {c["id"] for c in s["test_cases"]} and [d["id"] for d in s["deferred"]] == ["P2P-SUP-001"]) | |
| check("priority uses generate_report.tier vocabulary", [g.normalize_priority(d) for d in | |
| ({"priority": "P2"}, {"tier": 3}, {"risk": "critical"}, {"risk": "high"}, {"risk": {"level": "low"}}, {})] | |
| == ["P2", "P3", "P1", "P2", "P4", "P1"]) | |
| # 7. rejecting new cases defers them | |
| s, _ = run(approve=False, thread="reject") | |
| check("rejected cases are removed and listed as deferred", | |
| all(not c["new_or_changed"] for c in s["test_cases"]) and any(d["reason"] == "rejected at approval gate" for d in s["deferred"])) | |
| # 8. routing | |
| check("route: triage is local", g.route_model("triage", {"passed": 3})[0] == "local") | |
| check("route: match payload with amounts is local", g.route_model("summary", {"invoice_amount": 1250})[0] == "local") | |
| check("route: float values refused", g.route_model("summary", {"passed": 3.0})[0] == "local") | |
| check("route: text refused", g.route_model("summary", "3 passed")[0] == "local") | |
| check("route: integer counts -> frontier", g.route_model("summary", {"passed": 3, "failed": 1})[0] == "frontier") | |
| s, _ = run({"FRONTIER_SUMMARY": "1", "LOCAL_TRIAGE_HINTS": "1", "STUB_DEFECT": "P2P-INV-001"}, | |
| review={"P2P-INV-001": {"confirmed": "real_defect"}}, thread="routes") | |
| check("route in graph: triage hint local, summary frontier", | |
| [(c["node"], c["route"]) for c in s["llm_calls"]] == [("triage", "local"), ("report", "frontier")]) | |
| # 9. Playwright command | |
| cmd = g.playwright_command(["P2P-INV-001"]) | |
| check("live Playwright command uses --retries=0", "--retries=0" in cmd and "--reporter=json" in cmd) | |
| # 10. SQLite checkpoint survives a restart | |
| with tempfile.TemporaryDirectory() as d: | |
| for k in ("STUB_FAIL", "STUB_DEFECT", "STUB_UI", "STUB_FLAKY"): | |
| os.environ.pop(k, None) | |
| cfg = {"configurable": {"thread_id": "crash"}} | |
| cp1 = g.sqlite_checkpointer(Path(d) / "cp.sqlite") | |
| g1 = g.build_graph(cp1) | |
| g1.invoke({"patch_id": "crash"}, cfg) | |
| paused = g1.get_state(cfg).next == ("approval_gate",) | |
| del g1 | |
| cp2 = g.sqlite_checkpointer(Path(d) / "cp.sqlite") | |
| g2 = g.build_graph(cp2) | |
| pending = g2.get_state(cfg).tasks[0].interrupts[0].value["pending_case_ids"] | |
| g2.invoke(Command(resume={"approved": pending}), cfg) | |
| check("SQLite: paused run resumes in a new graph instance", paused and g2.get_state(cfg).values["decision"] == "GO") | |
| cp1.conn.close(), cp2.conn.close() # Windows cannot delete the temp dir while the DB is open | |
| print(f"selftest: {len(CHECKS)}/{len(CHECKS)} checks passed") | |