"""The flight recorder tests (events.jsonl). Law: every component of a run — LLM or not — leaves one JSON line in runs//events.jsonl with its input, output, and parent, so the run can be debugged a posteriori like an IDE session (owner 26/10: 'vull fer com un debug amb l'IDE, però a posteriori'). The recorder must never break a run: emitting is side-effect-free and cheap, and with no run dir it is a silent no-op. """ import json import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) from floop.recorder import Recorder def test_emit_writes_valid_jsonl_lines(tmp=None): import tempfile tmp = Path(tempfile.mkdtemp()) r = Recorder(run_dir=tmp) r.emit("comp.done", comp="gate", parent="page:pedro", in_={"heading": "x"}, out={"kept": True}, dur_ms=3) r.emit("fact.merged", fact={"s": "a"}, verdict="merged", eid=42) lines = (tmp / "events.jsonl").read_text().strip().splitlines() assert len(lines) == 2, lines e1, e2 = (json.loads(l) for l in lines) assert e1["event"] == "comp.done" and e1["comp"] == "gate", e1 assert e1["in"] == {"heading": "x"} and e1["out"] == {"kept": True}, e1 assert e1["parent"] == "page:pedro" and e1["seq"] == 1 and e1["dur_ms"] == 3 assert "ts" in e1 and e1["run_ts"], e1 assert e2["seq"] == 2 and e2["verdict"] == "merged" and e2["eid"] == 42 print("ok: emit -> one valid JSON line each, seq/ts/in/out/parent kept") def test_disabled_recorder_is_a_silent_noop(): r = Recorder(run_dir=None) r.emit("comp.done", comp="gate", out={"kept": True}) # must not raise assert r.events == 0 print("ok: no run dir -> no-op, never breaks a run") def test_in_memory_events_readable_for_snapshot(): import tempfile tmp = Path(tempfile.mkdtemp()) r = Recorder(run_dir=tmp) r.emit("merge.done", eid=7, verdict="merged") r.emit("merge.done", eid=7, verdict="dup") # corroboration bump evs = r.events_of("merge.done") assert [e["eid"] for e in evs] == [7, 7] assert [e["verdict"] for e in evs] == ["merged", "dup"] # last-touch annotation: per-row latest event last = {} for e in evs: last[e["eid"]] = e["verdict"] assert last == {7: "dup"} print("ok: events_of() lets the snapshot annotate each row's last touch") if __name__ == "__main__": test_emit_writes_valid_jsonl_lines() test_disabled_recorder_is_a_silent_noop() test_in_memory_events_readable_for_snapshot() print("\nrecorder: 3/3 ok")