arborist/tests/test_journal.py
russell@unturf.com 8d6961fcc1
aborist/arborist
modified:   .gitlab-ci.yml
	modified:   bench/qa_questions.txt
	modified:   bench/qa_sweep.py
	modified:   bench/run.sh
	modified:   docs/TICKETS.md
	modified:   docs/_source/README.md
	modified:   docs/_source/_ext/makefile_targets.py
	modified:   docs/_source/api/cli.rst
	modified:   docs/_source/api/distill.rst
	modified:   docs/_source/api/mesh.rst
	modified:   docs/_source/api/qa.rst
	modified:   docs/_source/api/retrieval.rst
	modified:   docs/_source/api/storage.rst
	modified:   docs/_source/api/substrate.rst
	modified:   docs/_source/concepts.rst
	modified:   docs/_source/conf.py
	modified:   docs/_source/cookbook.rst
	modified:   docs/_source/index.rst
	modified:   docs/_source/license.rst
	modified:   docs/_source/quickstart.rst
	modified:   docs/bench-maxing.md
	modified:   docs/benchmarks.md
	modified:   docs/cti-architecture.md
	modified:   docs/diagrams/aborist-modules.dot
	modified:   docs/diagrams/aborist-modules.svg
	modified:   docs/diagrams/mesh-data-flow.dot
	modified:   docs/diagrams/mesh-epoch-lifecycle.dot
	modified:   docs/diagrams/mesh-epoch-lifecycle.svg
	modified:   docs/diagrams/mesh-group-decisions.dot
	modified:   docs/diagrams/mesh-group-decisions.svg
	modified:   docs/diagrams/mesh-identity-stack.dot
	modified:   docs/diagrams/mesh-secret-envelope.dot
	modified:   docs/mesh.md
	modified:   docs/qa-modes-bench.md
	modified:   docs/seven-point-program.md
	modified:   docs/tickets/ticket-000001-retrieval-keywords-audit-gap.md
	modified:   docs/tickets/ticket-000002-reference-frame-polarity-contract.md
	modified:   docs/tickets/ticket-000003-anchor-class-warrant.md
	modified:   docs/tickets/ticket-000005-label-ladder-migration.md
	modified:   docs/tickets/ticket-000006-bench-emergent-findings.md
	modified:   docs/tickets/ticket-000007-query-layer-hyphen-fold.md
	modified:   docs/tickets/ticket-000008-broad-quantifier-preflight-guard.md
	modified:   docs/tickets/ticket-000009-quantifier-preflight-dag-binding.md
	modified:   docs/tickets/ticket-000010-metacognition-preflight-guard.md
	modified:   docs/tickets/ticket-000011-soft-preflight-hint-sidecar.md
	modified:   scripts/backfill_concepts.py
	modified:   scripts/bench_emergent.py
	modified:   tests/crawler/test_async_web_fetcher.py
	modified:   tests/crawler/test_bridge.py
	modified:   tests/crawler/test_web_fetch.py
	modified:   tests/test_bench_qa_sweep.py
	modified:   tests/test_burn.py
	modified:   tests/test_burn_doc.py
	modified:   tests/test_claim_lattice.py
	modified:   tests/test_cli_render.py
	modified:   tests/test_compress.py
	modified:   tests/test_concepts.py
	modified:   tests/test_dag.py
	modified:   tests/test_directives.py
	modified:   tests/test_distill.py
	modified:   tests/test_distill_recursive.py
	modified:   tests/test_evict.py
	modified:   tests/test_frame.py
	modified:   tests/test_grok_source.py
	modified:   tests/test_html_source.py
	modified:   tests/test_ingest.py
	modified:   tests/test_inspect.py
	modified:   tests/test_journal.py
	modified:   tests/test_keys.py
	modified:   tests/test_llm_context_base.py
	modified:   tests/test_merkle.py
	modified:   tests/test_mesh.py
	modified:   tests/test_mesh_aead.py
	modified:   tests/test_mesh_chain.py
	modified:   tests/test_mesh_cli.py
	modified:   tests/test_mesh_cli_pull.py
	modified:   tests/test_mesh_wire.py
	modified:   tests/test_mesh_wire_e2e.py
	modified:   tests/test_metacognition.py
	modified:   tests/test_migration_audit_mode.py
	modified:   tests/test_providence_source.py
	modified:   tests/test_qa.py
	modified:   tests/test_qa_quality_live.py
	modified:   tests/test_quantifier_caps.py
	modified:   tests/test_quantifier_classifier.py
	modified:   tests/test_quantifier_phase4.py
	modified:   tests/test_quantifier_reminder.py
	modified:   tests/test_query.py
	modified:   tests/test_reclassify.py
	modified:   tests/test_repair.py
	modified:   tests/test_resume.py
	modified:   tests/test_snapshot.py
	modified:   tests/test_soft_preflight.py
	modified:   tests/test_tfidf.py
	modified:   tests/test_vcs_source.py
	modified:   tests/test_verify.py
	modified:   tests/test_verify_json.py
	modified:   tests/test_versioned_ingest.py
	modified:   tests/test_warrant.py
	modified:   tests/test_wikipedia_old.py
	modified:   tests/test_wikipedia_xml.py
	modified:   tests/test_wikitext.py
2026-05-07 09:31:49 -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
- arborist-specific extras under namespaced ``arborist_meta``
"""
from __future__ import annotations
import json
from pathlib import Path
from arborist.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/arborist") == "-home-fox-git-arborist"
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_arborist_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", arborist_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["arborist_meta"]["audit_mode"] == "STRICT"
assert asst["arborist_meta"]["n_verified"] == 2
assert asst["arborist_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