"""The traces exporter tests (events.jsonl -> OTLP spans). Law (ARCHITECTURE.md §9-bis): one run = one trace; each LLM call, engine step and evidence append = one span. The exporter is POST-MORTEM only — it reads the recorded events and never touches the run itself. These tests check the structural invariants against synthetic + recorded event files: unique span ids, no orphan children, the root envelope covering every span, deterministic trace ids, and the child-run blocks nesting honestly. No network: payloads are built, never pushed. """ import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) from floop.traces import build_payload, load_events, _cap, _Clock def _check(spans: list[dict], label: str) -> None: ids = {s["spanId"] for s in spans} assert len(ids) == len(spans), f"{label}: duplicate span ids" root = spans[0] assert not root.get("parentSpanId"), f"{label}: root has a parent" r0, r1 = int(root["startTimeUnixNano"]), int(root["endTimeUnixNano"]) orphans = [s["name"] for s in spans[1:] if s["parentSpanId"] not in ids] assert not orphans, f"{label}: orphans {orphans[:3]}" outside = [s["name"] for s in spans[1:] if not (r0 <= int(s["startTimeUnixNano"]) and int(s["endTimeUnixNano"]) <= r1)] assert not outside, f"{label}: spans outside root window {outside[:3]}" def test_builds_one_trace_per_run(): import tempfile tmp = Path(tempfile.mkdtemp(prefix="floop-traces-")) ev = tmp / "events.jsonl" ev.write_text( '{"seq": 1, "ts": "09:15:21.295", "run_ts": "09:15:21.136",' ' "event": "comp.start", "comp": "INIT",' ' "desc": "decompose: What is 2+2?"}\n' '{"seq": 2, "ts": "09:15:22.000", "run_ts": "09:15:21.136",' ' "event": "llm.done", "stage": "INIT", "call_seq": 1,' ' "dur_s": 0.7, "file": "prompts/01-INIT.txt", "exception": null}\n' '{"seq": 3, "ts": "09:15:23.000", "run_ts": "09:15:21.136",' ' "event": "comp.out", "comp": "INIT", "out": "What is 4?"}\n' '{"seq": 4, "ts": "09:15:24.000", "run_ts": "09:15:21.136",' ' "event": "run.final", "out": "4"}\n') payload, tid = build_payload(tmp, load_events(tmp)) spans = payload["resourceSpans"][0]["scopeSpans"][0]["spans"] _check(spans, "single run") assert len(spans) == 4, spans # root + INIT + llm.INIT + run.final assert spans[0]["name"] == "What is 2+2?" # root named by the question tags = {a["key"]: a["value"]["stringValue"] for a in spans[0]["attributes"]} assert tags["run.answer"] == "4" llm = [s for s in spans if s["name"] == "llm.INIT"][0] lt = {a["key"]: a["value"]["stringValue"] for a in llm["attributes"]} assert lt["floop.file"] == "prompts/01-INIT.txt" and lt["floop.dur_s"] == "0.7" # llm duration is real: end-start = 0.7s dur = (int(llm["endTimeUnixNano"]) - int(llm["startTimeUnixNano"])) / 1e9 assert abs(dur - 0.7) < 0.01, dur print("ok: one run -> one trace, question-named root, real llm duration") def test_trace_id_is_deterministic(): import tempfile tmp = Path(tempfile.mkdtemp(prefix="floop-traces-")) (tmp / "events.jsonl").write_text( '{"seq": 1, "ts": "09:15:21.295", "run_ts": "09:15:21.136",' ' "event": "run.final", "out": "x"}\n') _, t1 = build_payload(tmp, load_events(tmp)) _, t2 = build_payload(tmp, load_events(tmp)) assert t1 == t2 and len(t1) == 32 print("ok: same run -> same trace id (stable UI link)") def test_child_runs_nest_as_blocks(): import json import tempfile tmp = Path(tempfile.mkdtemp(prefix="floop-traces-")) lines = [] for rts in ("09:15:21.100", "09:15:21.200"): # two child runs, one file lines += [ json.dumps({"seq": 1, "ts": rts, "run_ts": rts, "event": "comp.start", "comp": "INIT", "desc": "decompose: child question?"}), json.dumps({"seq": 2, "ts": rts, "run_ts": rts, "event": "comp.out", "comp": "INIT", "out": "done"}), ] (tmp / "events.jsonl").write_text("\n".join(lines) + "\n") payload, _ = build_payload(tmp, load_events(tmp)) spans = payload["resourceSpans"][0]["scopeSpans"][0]["spans"] _check(spans, "child runs") runs = [s for s in spans[1:] if s["name"] == "child question?"] assert len(runs) == 2, spans # one run-span per block, both shipped print("ok: child runs in one file nest as per-run spans, no dups") def test_clock_only_in_event_order(): import tempfile tmp = Path(tempfile.mkdtemp(prefix="20261027-000000-test-")) lines = ( '{"seq": 1, "ts": "23:59:59.500", "run_ts": "23:59:59.1",' ' "event": "run.final", "out": "a"}\n' '{"seq": 2, "ts": "00:00:01.500", "run_ts": "23:59:59.1",' ' "event": "run.final", "out": "b"}\n') (tmp / "events.jsonl").write_text(lines) payload, _ = build_payload(tmp, load_events(tmp)) spans = payload["resourceSpans"][0]["scopeSpans"][0]["spans"] r0 = int(spans[0]["startTimeUnixNano"]) r1 = int(spans[0]["endTimeUnixNano"]) assert 0 < (r1 - r0) < 10 * 1_000_000_000, (r1 - r0) # 2s, not +24h print("ok: midnight crossing handled (clock read in event order only)") def test_cap_keeps_spans_small(): big = {"k" + str(i): "x" * 100 for i in range(50)} out = _cap(big) assert len(out) <= 20 # width cap deep = _cap({"a": {"b": {"c": {"d": {"e": "x"}}}}}) assert deep != {"a": {"b": {"c": {"d": {"e": "x"}}}}} # depth cap assert _cap("y" * 1000) == ("y" * 497 + "…") print("ok: _cap truncates depth/width/strings (kg snapshot stays small)") if __name__ == "__main__": test_builds_one_trace_per_run() test_trace_id_is_deterministic() test_child_runs_nest_as_blocks() test_clock_only_in_event_order() test_cap_keeps_spans_small() print("\ntraces: 5/5 ok")