arborist/tests/test_journal.py
russell@unturf.com d3ad520529
journal: emit unfirehose/1.0 JSONL for queries + bench cycles
aborist now writes one JSONL session per `make query` invocation
and per `bench-emergent` cycle to:

    ~/.aborist/unfirehose/{project-slug}/{session-uuid}.jsonl

Unfirehose's native-harness auto-discovery picks up any
~/.{name}/unfirehose/ directory (see ingest.ts:discoverNativeHarnesses)
without registration — once a session lands, the unfirehose watcher
debounces, ingests, and exposes it in the dashboard alongside
Claude Code / Fetch / uncloseai sessions.

Schema: unfirehose/1.0 (per ~/git/unfirehose-nextjs-logger/docs/
unfirehose-schema.md). Each session file:

    line 1   type=session  (header — id, projectId, firstPrompt,
                            harness="aborist", harnessVersion)
    line 2   type=message role=user
    line 3   type=message role=assistant
                          content=[text]
                          model=hermes-3-llama-3.1-8b-fp8-dynamic
                          provider=hermes
                          durationMs=<wall>
                          aborist_meta={audit_mode, n_verified/n_quotes,
                            cache_key, cache_status, lookup_path,
                            violations, sources, timings_ms, answer_mode}
    line 4   type=message role=system subtype=session_end durationMs

aborist-specific extras (verifier verdict, sources, timings) ride
under namespaced ``aborist_meta`` so the canonical fields stay clean
for off-the-shelf consumers; per the spec, unknown fields are
ignored downstream.

Bench-emergent cycles emit an additional system init message at
the start of each session noting the 3 random words, marking the
session as a generator-driven cycle vs a normal user query.

Failure-isolation: journal write is wrapped in a broad try/except
at every call site. A journaling bug must NEVER break the query
or bench loop.

Tests: 10 new in tests/test_journal.py (slug encoding, session
header, parent-id chain, session_end on close, aborist_meta
passthrough, usage block, idempotent close). Full suite: 663 passed.

Live verified: `make query Q="what is photosynthesis?"` produced
a 4-line JSONL with STRICT 3/3, all sources + timings populated,
ready for unfirehose ingestion.
2026-05-02 15:19:39 -04:00

120 lines
4.2 KiB
Python

"""Tests for the unfirehose-compatible session journal writer.
The contract being tested is the unfirehose/1.0 schema:
- session header line first
- one message per subsequent line
- session_end system message on close
- ``$schema: "unfirehose/1.0"`` on every record
- aborist-specific extras under namespaced ``aborist_meta``
"""
from __future__ import annotations
import json
from pathlib import Path
from aborist.journal import (
HARNESS_NAME,
UNFIREHOSE_SCHEMA,
SessionWriter,
new_session_id,
slugify_cwd,
)
def _read_lines(path: Path) -> list[dict]:
return [json.loads(line) for line in path.read_text().splitlines() if line.strip()]
def test_slugify_cwd_basic():
assert slugify_cwd("/home/fox/git/aborist") == "-home-fox-git-aborist"
def test_slugify_cwd_dots_become_hyphens():
assert slugify_cwd("/home/fox/git/my.app") == "-home-fox-git-my-app"
def test_slugify_cwd_uses_cwd_when_none(tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
expected = str(tmp_path).replace("/", "-").replace(".", "-")
assert slugify_cwd() == expected
def test_session_header_first_line(tmp_path):
with SessionWriter(first_prompt="hello?", root=tmp_path, cwd="/home/fox/git/myproj"):
pass
files = list(tmp_path.glob("**/*.jsonl"))
assert len(files) == 1
rows = _read_lines(files[0])
header = rows[0]
assert header["$schema"] == UNFIREHOSE_SCHEMA
assert header["type"] == "session"
assert header["projectId"] == "-home-fox-git-myproj"
assert header["firstPrompt"] == "hello?"
assert header["harness"] == HARNESS_NAME
assert header["status"] == "active"
def test_session_messages_chained_via_parent_id(tmp_path):
with SessionWriter(first_prompt="q", root=tmp_path) as s:
s.user_message("first user")
s.assistant_message("first assistant", model="hermes-3")
s.user_message("second user")
rows = _read_lines(list(tmp_path.glob("**/*.jsonl"))[0])
# Look at the user/assistant chain — session_end is a separate
# system message we exclude here.
msgs = [r for r in rows if r["type"] == "message" and r["role"] != "system"]
assert len(msgs) == 3
# First message has no parent.
assert msgs[0]["parentId"] is None
# Subsequent messages chain to the previous.
assert msgs[1]["parentId"] == msgs[0]["id"]
assert msgs[2]["parentId"] == msgs[1]["id"]
def test_close_writes_session_end(tmp_path):
with SessionWriter(first_prompt="q", root=tmp_path) as s:
s.user_message("hi")
rows = _read_lines(list(tmp_path.glob("**/*.jsonl"))[0])
last = rows[-1]
assert last["type"] == "message"
assert last["role"] == "system"
assert last["subtype"] == "session_end"
assert isinstance(last["durationMs"], int)
assert last["durationMs"] >= 0
def test_aborist_meta_passes_through(tmp_path):
meta = {"audit_mode": "STRICT", "n_verified": 2, "cache_key": "abc123"}
with SessionWriter(first_prompt="q", root=tmp_path) as s:
s.assistant_message("answer", aborist_meta=meta)
rows = _read_lines(list(tmp_path.glob("**/*.jsonl"))[0])
asst = next(r for r in rows if r["type"] == "message" and r["role"] == "assistant")
assert asst["aborist_meta"]["audit_mode"] == "STRICT"
assert asst["aborist_meta"]["n_verified"] == 2
assert asst["aborist_meta"]["cache_key"] == "abc123"
def test_session_id_used_as_filename(tmp_path):
sid = new_session_id()
with SessionWriter(first_prompt="q", root=tmp_path, session_id=sid):
pass
files = list(tmp_path.glob("**/*.jsonl"))
assert files[0].stem == sid
def test_assistant_usage_block(tmp_path):
usage = {"inputTokens": 100, "outputTokens": 50, "totalTokens": 150}
with SessionWriter(first_prompt="q", root=tmp_path) as s:
s.assistant_message("a", model="hermes-3", usage=usage, duration_ms=1234)
rows = _read_lines(list(tmp_path.glob("**/*.jsonl"))[0])
asst = next(r for r in rows if r["type"] == "message" and r["role"] == "assistant")
assert asst["usage"] == usage
assert asst["durationMs"] == 1234
assert asst["model"] == "hermes-3"
def test_writer_is_idempotent_on_double_close(tmp_path):
s = SessionWriter(first_prompt="q", root=tmp_path)
s.close()
s.close() # should be a no-op, not raise