feat: flight recorder — events.jsonl for post-mortem debugging
Owner ruling (26/10): 'vull poder fer com un debug amb l'IDE de programació, però a posteriori de l'execució' — see WHERE inside which expert we are at every moment, and the input/output of every component, LLM or not. This is the recorder half (PR A of the observability plan). - floop/recorder.py: append-only JSONL, one event per line. LAWS: never breaks a run (emitting is swallowed; no run dir = TOTAL no-op, no buffer, no counters); every line self-describing (seq, ts, run_ts, event, payload); in/out carry structured data, the exact prompt text stays in prompts/NN-stage.txt (events stay small). - Wired via RunStreams (every component already had streams): comp.start/comp.out (machine stages INIT/ROUTE/EXPERT/ANSWER/FINAL), state, llm.done (stage, dur_s, call_seq, prompt file link — from the dspy callback), run.final. - Knowledge pipeline components (floop-internal nodes, not LLMs): probe.done (seeds, rows, tau, semantic adds), claims.done (the claim list), type.done (claim -> typed fact, one per claim as ruled), fact.merged / fact.rejected (one per fact — _ingest_candidate is called per fact; verdict + reason + page), prover.done (dsl, verdict: invalid_dsl/no_closed_path/closed/ambiguous, bindings), kg.snapshot (once at run end: every edge with temporal window + last_touch from this run's merge events). - kg_snapshot(): rows annotated with their last-touch event (owner: 'cada fila del kg podria tenir anotat quan s'afegeix o edita'). Live-checked: baseline run writes real events (comp.start, llm.done dur_s=17.35, comp.out '4'). TDD: recorder 3/3; suite 31 files green. Next (PR B): floop-inspect — the HTML debugger reading events.jsonl (timeline Gantt, KG view, facts table, probe view).
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
"""The flight recorder tests (events.jsonl).
|
||||
|
||||
Law: every component of a run — LLM or not — leaves one JSON line in
|
||||
runs/<ts>/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")
|
||||
Reference in New Issue
Block a user