From e365f6d1e8ce362e73b5823788b4f49fbcef80ee Mon Sep 17 00:00:00 2001 From: Onibot Date: Tue, 27 Oct 2026 07:47:21 +0100 Subject: [PATCH] =?UTF-8?q?feat:=20floop-inspect=20=E2=80=94=20the=20post-?= =?UTF-8?q?mortem=20HTML=20debugger=20(PR=20B)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit One run dir in -> ONE self-contained inspect.html out. The events are inlined as JSON; the page opens offline from file:// with ZERO network references. Four views (owner 26/10: IDE-like debugging a posteriori): - timeline: every component as a bar in time order — LLM calls (with real durations) vs code nodes colored differently; click a bar -> its in/out in the header detail line. The 'which expert are we inside' view. - kg: the snapshot graph on canvas (spring layout, vanilla JS): nodes sized by degree, edges with temporal windows, THIS RUN's touched edges highlighted in accent blue, click a node/edge -> detail (subject, rel, object, window, last-touch event). - facts: the two-pass debug table — claims per chunk, each claim -> typed fact (or declined), each merge verdict with reason. - probes: per question what the hybrid probe returned (seeds, rows, semantic adds, tau) and the prover's DSL + verdict + bindings. Fixed on real render (browser-verified): the KG canvas measured 0x0 while its tab was hidden (display:none) — layout now runs lazily on first visible draw and every tab activation dispatches a resize, so the canvas paints correctly (verified: painted pixels present, all four tabs render content). TDD: inspect_html 3/3 (self-contained offline, honest error without events, empty run renders). Suite: 32 test files green. Usage: python -m floop.inspect_html runs// --- code/floop/inspect_html.py | 317 +++++++++++++++++++++++++++++++++++++ code/tests/inspect_html.py | 108 +++++++++++++ 2 files changed, 425 insertions(+) create mode 100644 code/floop/inspect_html.py create mode 100644 code/tests/inspect_html.py diff --git a/code/floop/inspect_html.py b/code/floop/inspect_html.py new file mode 100644 index 0000000..839e42e --- /dev/null +++ b/code/floop/inspect_html.py @@ -0,0 +1,317 @@ +"""floop-inspect — the post-mortem HTML debugger (PR B). + +One run dir in -> ONE self-contained inspect.html out. The events are +inlined as JSON; the page opens offline from file:// with zero network +references. Four views (owner 26/10): + - timeline: every component as a bar in time order (LLM vs code + colored), the 'where inside which expert are we' view + - kg: the snapshot graph (canvas, force layout, temporal windows + + last-touch highlighted) + - facts: the two-pass debug table (claim -> typed fact -> merge + verdict, rejections with reason) + - probes: per question, what the semantic search returned +""" +from __future__ import annotations + +import json +import sys +from pathlib import Path + + +def generate(run_dir: str | Path) -> Path: + run_dir = Path(run_dir) + ev_path = run_dir / "events.jsonl" + if not ev_path.exists(): + print(f"no events.jsonl in {run_dir} — nothing to inspect", + file=sys.stderr) + raise SystemExit(1) + events = [] + for line in ev_path.read_text(encoding="utf-8").splitlines(): + line = line.strip() + if line: + try: + events.append(json.loads(line)) + except json.JSONDecodeError: + pass # torn line mid-crash: skip, honest + html = _render(events) + out = run_dir / "inspect.html" + out.write_text(html, encoding="utf-8") + return out + + +def main(argv: list[str]) -> int: + if len(argv) != 2: + print("usage: python -m floop.inspect_html ", file=sys.stderr) + return 2 + out = generate(argv[1]) + print(f"inspect: {out}") + return 0 + + +def _render(events: list[dict]) -> str: + payload = json.dumps(events, default=str) + return _TEMPLATE.replace("__EVENTS__", payload) + + +# ---- the page (vanilla JS, no deps, no network) --------------------------- +_TEMPLATE = r""" + + + +floop-inspect + + + +
+ floop-inspect — post-mortem debugger   + + +
hover/click a bar or node
+
+
+
+
+
+ + + +""" + +if __name__ == "__main__": + raise SystemExit(main(sys.argv)) \ No newline at end of file diff --git a/code/tests/inspect_html.py b/code/tests/inspect_html.py new file mode 100644 index 0000000..cef2c0f --- /dev/null +++ b/code/tests/inspect_html.py @@ -0,0 +1,108 @@ +"""floop-inspect tests (PR B: the post-mortem HTML debugger). + +Law: one run dir in, ONE self-contained inspect.html out — the events +JSON inlined, zero network references (opens offline from file://), +every tab present (timeline, KG, facts, probes), and the run's final +answer visible. A run dir without events.jsonl errors honestly.""" +import json +import sys +from pathlib import Path + +sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) + +from floop.inspect_html import generate + +EVENTS = [ + {"seq": 1, "ts": "10:00:00.000", "run_ts": "10:00:00.000", + "event": "comp.start", "comp": "INIT", "desc": "decompose: q0"}, + {"seq": 2, "ts": "10:00:01.000", "run_ts": "10:00:00.000", + "event": "llm.done", "stage": "INIT", "call_seq": 1, "dur_s": 0.9, + "file": "prompts/01-INIT.txt", "exception": None}, + {"seq": 3, "ts": "10:00:01.100", "run_ts": "10:00:00.000", + "event": "comp.out", "comp": "INIT", "out": "Q1 sub"}, + {"seq": 4, "ts": "10:00:02.000", "run_ts": "10:00:00.000", + "event": "claims.done", "page": "Pedro Sanchez", + "heading": "Career", "out": {"n_claims": 1, + "claims": ["PM of Spain since 2018"]}}, + {"seq": 5, "ts": "10:00:03.000", "run_ts": "10:00:00.000", + "event": "type.done", "page": "Pedro Sanchez", + "claim": "PM of Spain since 2018", + "out": {"fact": {"subject": "Pedro Sanchez", + "relation": "prime minister of", "object": "Spain"}}}, + {"seq": 6, "ts": "10:00:03.500", "run_ts": "10:00:00.000", + "event": "fact.merged", "eid": 7, + "fact": {"subject": "Pedro Sanchez", "relation": "prime minister of", + "object": "Spain"}, "page": "Pedro Sanchez"}, + {"seq": 7, "ts": "10:00:04.000", "run_ts": "10:00:00.000", + "event": "probe.done", "q": "Q1 sub", "seeds": ["pedro"], "rows": 3, + "semantic": 1, "tau": 0.45}, + {"seq": 8, "ts": "10:00:05.000", "run_ts": "10:00:00.000", + "event": "prover.done", "q": "Q1 sub", "verdict": "ambiguous", + "dsl": {"lookup": {"subject": "pedro sanchez", + "rel_any": ["prime minister of"], "bind": "x"}}, + "n_bindings": 2, "bindings": ["Spain", "PSOE"]}, + {"seq": 9, "ts": "10:00:06.000", "run_ts": "10:00:00.000", + "event": "kg.snapshot", + "kg": {"n_edges": 1, "n_live": 1, "n_conflicts": 0, + "edges": [{"eid": 7, "subject": "pedro sanchez", + "rel": "prime minister of", "object": "Spain", + "t_start": "2018", "t_end": None, "retracted": False, + "last_touch": {"seq": 6, "ts": "10:00:03.500", + "page": "Pedro Sanchez"}}]}}, + {"seq": 10, "ts": "10:00:07.000", "run_ts": "10:00:00.000", + "event": "run.final", "out": "Spain and PSOE"}, +] + + +def _make_run(tmp: Path, events) -> Path: + run_dir = tmp / "run" + run_dir.mkdir(parents=True) + (run_dir / "events.jsonl").write_text( + "\n".join(json.dumps(e) for e in events), encoding="utf-8") + return run_dir + + +def test_generate_writes_selfcontained_html(): + import tempfile + tmp = Path(tempfile.mkdtemp()) + run_dir = _make_run(tmp, EVENTS) + out = generate(run_dir) + assert out == run_dir / "inspect.html", out + html = out.read_text(encoding="utf-8") + # self-contained: zero network refs, JSON inlined + assert "http://" not in html and "https://" not in html, "network ref!" + assert '"PM of Spain since 2018"' in html # events inlined + for tab in ("timeline", "kg", "facts", "probes"): + assert f'id="{tab}"' in html, tab # all tabs present + assert "Spain and PSOE" in html # final answer in + assert "prime minister of" in html # KG data in + print("ok: one self-contained inspect.html (offline, all tabs, inlined)") + + +def test_missing_events_errors_honestly(): + import tempfile + tmp = Path(tempfile.mkdtemp()) + run_dir = tmp / "run" + run_dir.mkdir() + try: + generate(run_dir) + raise AssertionError("must fail") + except SystemExit as e: + assert e.code not in (0, None), e + print("ok: no events.jsonl -> honest error (exit != 0)") + + +def test_empty_events_still_renders(): + import tempfile + tmp = Path(tempfile.mkdtemp()) + run_dir = _make_run(tmp, []) + out = generate(run_dir) + assert out.exists() + print("ok: empty run renders an honest empty view") + + +if __name__ == "__main__": + test_generate_writes_selfcontained_html() + test_missing_events_errors_honestly() + test_empty_events_still_renders() + print("\ninspect_run: 3/3 ok") \ No newline at end of file -- 2.55.0