"""build_turn is the one recipe for a finished turn's message rows.""" from __future__ import annotations import json import os from pathlib import Path import pytest from raven.agent.subagent.instance_log import ( append_turn, build_turn, instance_root, instance_title, open_instance_log, transcript_path, ) from raven.config.agent_names import GENERIC_AGENT def test_turn_is_prompt_then_work_then_answer() -> None: turn = build_turn( prompt="read the readme", messages=[{"role": "assistant", "content": "looking"}], answer="there is no readme", ) assert [row["role"] for row in turn] == ["user", "assistant", "assistant"] assert turn[0]["content"] == "read the readme" assert turn[-1]["content"] == "there is no readme" def test_rows_built_by_turn_are_timestamped() -> None: turn = build_turn(prompt="hi", answer="hello") assert len(turn) == 2 assert all(row["timestamp"] for row in turn) def test_messages_rows_pass_through_with_or_without_timestamp() -> None: # Transcript producers attach a timestamp only when the underlying event # carries a wall clock. Rows arriving without one are legitimate. row_with_timestamp = {"role": "assistant", "content": "x", "timestamp": "2026-01-01T00:00:00"} row_without_timestamp = {"role": "assistant", "content": "y"} turn = build_turn(prompt="ask", messages=[row_with_timestamp, row_without_timestamp]) assert turn[1] == row_with_timestamp assert turn[2] == row_without_timestamp def test_non_dict_messages_are_dropped() -> None: turn = build_turn(prompt="hi", messages=["not a dict", {"role": "tool", "content": "x"}]) # type: ignore[list-item] assert [row["role"] for row in turn] == ["user", "tool"] def test_error_is_recorded_as_a_failed_answer() -> None: turn = build_turn(prompt="hi", error="boom") assert turn[-1]["role"] == "assistant" assert turn[-1]["content"] == "[failed] boom" def test_omitted_parts_add_no_rows() -> None: assert build_turn() == [] assert [row["role"] for row in build_turn(messages=[{"role": "tool", "content": "x"}])] == ["tool"] def test_append_turn_writes_exactly_what_build_turn_returns(tmp_path: Path) -> None: # The whole point of the extraction: the payload handed to everos and the # rows written to the log must be the same turn, or the memory would # describe a conversation no file on disk agrees with. kwargs = { "prompt": "read the readme", "messages": [{"role": "assistant", "content": "looking"}], "answer": "there is no readme", } append_turn(tmp_path, agent="Coder", handle="h1", session_key="s1", kind="spawn", **kwargs) written = [ json.loads(line) for line in transcript_path(tmp_path, "Coder", "h1").read_text(encoding="utf-8").splitlines() if line.strip() ] rows = [row for row in written if row.get("_type") != "metadata"] expected = build_turn(**kwargs) assert [(r["role"], r["content"]) for r in rows] == [(e["role"], e["content"]) for e in expected] def test_an_instance_is_named_before_it_has_said_anything(tmp_path: Path) -> None: # The title used to arrive with the first turn, and the first turn is # written when the dispatch *finishes*. Every surface that heads a panel by # what the instance was dispatched for therefore fell back to the handle -- # an id -- for exactly as long as the run was still going. open_instance_log(tmp_path, agent="Coder", handle="h1", session_key="s1", kind="spawn", title="Draft a deck") assert instance_title(tmp_path, "Coder", "h1") == "Draft a deck" def test_opening_an_instance_twice_writes_one_header(tmp_path: Path) -> None: # An instance is reached through several lanes and a later dispatch of the # same handle joins its conversation. It must not rename it, and must not # leave a second metadata row in the middle of the file. open_instance_log(tmp_path, agent="Coder", handle="h1", session_key="s1", kind="spawn", title="first") open_instance_log(tmp_path, agent="Coder", handle="h1", session_key="s1", kind="dag", title="second") append_turn(tmp_path, agent="Coder", handle="h1", session_key="s1", kind="dag", prompt="go", answer="done") written = [ json.loads(line) for line in transcript_path(tmp_path, "Coder", "h1").read_text(encoding="utf-8").splitlines() if line.strip() ] assert [row.get("_type") for row in written].count("metadata") == 1 assert instance_title(tmp_path, "Coder", "h1") == "first" def test_an_unnamed_instance_still_opens(tmp_path: Path) -> None: # A direct chat has no line saying what the instance is for, and must not # invent one -- but the header it opens is still the file's first row. open_instance_log(tmp_path, agent="Coder", handle="h1", session_key="s1", kind="chat") assert transcript_path(tmp_path, "Coder", "h1").is_file() assert instance_title(tmp_path, "Coder", "h1") == "" def test_an_instance_logged_under_a_former_name_keeps_its_file(tmp_path: Path) -> None: """The generic agent was renamed without moving the logs written before. A continued instance appends where its earlier turns are, rather than opening a second file under the new name that the viewer would show as a new instance.""" legacy = instance_root(tmp_path, "Raven") / "h1.jsonl" legacy.parent.mkdir(parents=True) legacy.write_text(json.dumps({"_type": "metadata", "metadata": {"title": "Draft"}}) + "\n", encoding="utf-8") assert transcript_path(tmp_path, GENERIC_AGENT, "h1") == legacy append_turn(tmp_path, agent=GENERIC_AGENT, handle="h1", session_key="s1", kind="spawn", prompt="go", answer="ok") assert not instance_root(tmp_path, GENERIC_AGENT).exists() assert "ok" in legacy.read_text(encoding="utf-8") @pytest.mark.parametrize("latest", ["raven", "Raven"]) def test_two_legacy_transcripts_choose_the_latest_write_for_every_alias(tmp_path: Path, latest: str) -> None: older = instance_root(tmp_path, "raven") / "h1.jsonl" newer = instance_root(tmp_path, "Raven") / "h1.jsonl" older.parent.mkdir(parents=True) newer.parent.mkdir(parents=True, exist_ok=True) if older.parent.samefile(newer.parent): pytest.skip("Two casing generations require a case-sensitive filesystem") for path, title in ((older, "Older"), (newer, "Newer")): path.write_text(json.dumps({"_type": "metadata", "metadata": {"title": title}}) + "\n", encoding="utf-8") chosen = older if latest == "raven" else newer other = newer if latest == "raven" else older for path in (older, newer): stamp = 2_000_000_000 if path == chosen else 1_000_000_000 os.utime(path, ns=(stamp, stamp)) for name in (GENERIC_AGENT, "Raven", "raven"): assert transcript_path(tmp_path, name, "h1") == chosen assert instance_title(tmp_path, name, "h1") == ("Older" if latest == "raven" else "Newer") append_turn(tmp_path, agent=name, handle="h1", session_key="s1", answer=name) assert len(chosen.read_text(encoding="utf-8").splitlines()) == 4 assert len(other.read_text(encoding="utf-8").splitlines()) == 1 def test_a_new_instance_is_logged_under_the_current_name(tmp_path: Path) -> None: open_instance_log(tmp_path, agent="Raven", handle="old", session_key="s1", kind="spawn", title="Draft") assert transcript_path(tmp_path, GENERIC_AGENT, "fresh").parent == instance_root(tmp_path, GENERIC_AGENT)