Skip to content

Your lightweight open-source personal agent.

21.0 KB
Raw Blame
from __future__ import annotations

import json
import sqlite3
from contextlib import closing
from pathlib import Path

import pytest

from nanobot.session.display_history import HistoryFact, HistoryKind
from nanobot.session.location import SessionLocation
from nanobot.session.manager import SessionManager
from nanobot.session.sqlite_store import SessionConflictError
from nanobot.webui.forking import create_webui_chat_fork
from nanobot.webui.history import HISTORY_IMPORT_MARKER, collect_legacy_history
from nanobot.webui.transcript import WebUITranscriptRecorder, build_webui_thread_response


def _manager(tmp_path: Path) -> SessionManager:
    return SessionManager(tmp_path / "workspace", sessions_root=tmp_path / "sessions")


def _legacy(directory: Path, key: str, records: list[dict]) -> Path:
    directory.mkdir(parents=True, exist_ok=True)
    path = directory / f"{SessionManager.safe_key(key)}.jsonl"
    path.write_text("".join(json.dumps(record, ensure_ascii=False) + "\n" for record in records), encoding="utf-8")
    return path


async def test_display_facts_survive_reopen_without_entering_model_history(tmp_path):
    sessions = _manager(tmp_path)
    session = await sessions.state.get("websocket:durable")
    session.add_message("user", "question")
    session.add_message("assistant", "answer")
    await sessions.state.finish_turn(session)
    facts = [
        HistoryFact(HistoryKind.INPUT, {"text": "question"}, "turn-1", "user", 1, 1000),
        HistoryFact(HistoryKind.REASONING_SEGMENT, {"text": "reasoning"}, "turn-1", "reasoning", 2, 1001),
        HistoryFact(HistoryKind.FILE_ACTIVITY, {"edits": [{"path": "a.py", "diff": "-old\n+new"}]}, "turn-1", "activity", 3, 1002),
        HistoryFact(HistoryKind.ANSWER_SEGMENT, {"text": "answer", "response_sources": [{
            "provider": "test", "model": "actual-fallback", "preset": "default", "fallback": True,
        }]}, "turn-1", "answer", 4, 1003),
        HistoryFact(HistoryKind.COMPLETION, {"usage": {"total_tokens": 30}, "round_usages": [{"total_tokens": 30}],
                    "latency_ms": 40, "outcome": "failed", "failure_message": "model unavailable"},
                    "turn-1", "complete", 5, 1004),
    ]
    await sessions.state.append_history(session.key, facts, expected_generation=session.generation)
    with closing(sqlite3.connect(sessions.sessions_dir / "sessions.sqlite3")) as db:
        stored = db.execute("SELECT record FROM history_entries ORDER BY position").fetchall()
        assert [json.loads(row[0])["kind"] for row in stored] == [fact.kind for fact in facts]
        assert all("chat_id" not in json.loads(row[0]) and "event" not in json.loads(row[0]) for row in stored)
    await sessions.state.aclose()

    reopened = _manager(tmp_path)
    assert reopened.project_history(session.key, lambda history: history.all_facts()) == facts
    model = reopened.read_session_file(session.key)
    assert [message["content"] for message in model["messages"]] == ["question", "answer"]
    payload = reopened.project_history(session.key, lambda history: build_webui_thread_response(session.key, history=history))
    assert payload["completed_turn_ids"] == ["turn-1"]
    terminal = next(event for event in payload["events"] if event["event"] == "turn_end")
    assert terminal["failure_message"] == "model unavailable" and terminal["outcome"] == "failed"
    assert terminal["round_usages"] == [{"total_tokens": 30}]
    assert any(event.get("edits") == facts[2].content["edits"] for event in payload["events"])
    assert next(event for event in payload["events"] if event["event"] == "stream_end")["response_sources"] == facts[3].content["response_sources"]


async def test_unbound_display_append_targets_current_session(tmp_path):
    sessions = _manager(tmp_path)
    key = "websocket:current"
    await sessions.state.get(key)
    await sessions.state.reset(key)
    fact = HistoryFact(HistoryKind.INPUT, {"text": "new message"})

    await sessions.state.append_history(key, [fact], expected_generation=None)

    assert await sessions.state.project_history(key, lambda history: list(history.all_facts())) == [fact]
    await sessions.state.aclose()


async def test_delete_reset_reject_old_display_generation(tmp_path):
    sessions = _manager(tmp_path)
    key = "websocket:generation"
    first = await sessions.state.get(key)
    first.add_message("user", "old")
    await sessions.state.finish_turn(first)
    fact = HistoryFact(HistoryKind.INPUT, {"text": "old"})
    await sessions.state.append_history(key, [fact], expected_generation=first.generation)
    assert await sessions.state.delete(key)
    with pytest.raises(SessionConflictError):
        await sessions.state.append_history(key, [fact], expected_generation=first.generation)
    assert await sessions.state.project_history(key, lambda history: history.revision) is None
    second = await sessions.state.get(key)
    second.add_message("user", "new")
    await sessions.state.finish_turn(second)
    with pytest.raises(SessionConflictError):
        await sessions.state.append_history(key, [fact], expected_generation=first.generation)
    assert await sessions.state.project_history(key, lambda history: history.all_facts()) == []
    await sessions.state.aclose()


async def test_fork_rolls_back_both_histories_if_display_copy_fails(tmp_path, monkeypatch):
    sessions = _manager(tmp_path)
    source = await sessions.state.get("websocket:source")
    source.add_message("user", "question")
    source.add_message("assistant", "answer")
    await sessions.state.finish_turn(source)
    monkeypatch.setattr("nanobot.webui.forking.uuid.uuid4", lambda: "failed-fork")
    original = sessions._store.append_history

    def fail(key, facts, *, expected_generation):
        original(key, facts, expected_generation=expected_generation)
        raise OSError("disk full")

    monkeypatch.setattr(sessions._store, "append_history", fail)
    with pytest.raises(OSError, match="disk full"):
        await create_webui_chat_fork(sessions, source_chat_id="source", before_user_index=1)
    assert await sessions.state.read_metadata("websocket:failed-fork") is None
    assert await sessions.state.project_history("websocket:failed-fork", lambda history: history.all_facts()) == []
    assert (await sessions.state.get(source.key)).messages == source.messages
    await sessions.state.aclose()


async def test_legacy_import_is_atomic_repeatable_and_deletion_cannot_resurrect_backup(tmp_path):
    sessions = _manager(tmp_path)
    directory = tmp_path / "legacy"
    path = _legacy(directory, "websocket:imported", [
        {"event": "user", "chat_id": "imported", "text": "hello", "created_at_ms": 1000},
        {"event": "delta", "chat_id": "imported", "text": "an"},
        {"event": "delta", "chat_id": "imported", "text": "swer"},
        {"event": "stream_end", "chat_id": "imported"},
        {"event": "turn_end", "chat_id": "imported"},
    ])
    before = path.read_bytes()
    def collect(rows):
        return collect_legacy_history(rows, directory=directory)
    await sessions.state.migrate_history(HISTORY_IMPORT_MARKER, collect)
    facts = await sessions.state.project_history("websocket:imported", lambda history: history.all_facts())
    assert [fact.kind for fact in facts] == [HistoryKind.INPUT, HistoryKind.ANSWER_SEGMENT, HistoryKind.COMPLETION]
    assert facts[1].content["text"] == "answer"
    assert path.read_bytes() == before
    await sessions.state.migrate_history(HISTORY_IMPORT_MARKER, collect)
    assert await sessions.state.project_history("websocket:imported", lambda history: history.all_facts()) == facts
    assert await sessions.state.delete("websocket:imported")
    await sessions.state.aclose()
    reopened = _manager(tmp_path)
    await reopened.state.migrate_history(HISTORY_IMPORT_MARKER, collect)
    assert await reopened.state.read_metadata("websocket:imported") is None
    await reopened.state.aclose()


async def test_corrupt_legacy_input_rolls_back_import_marker_and_all_records(tmp_path):
    sessions = _manager(tmp_path)
    directory = tmp_path / "legacy"
    _legacy(directory, "websocket:first", [{"event": "user", "chat_id": "first", "text": "safe"}])
    corrupt = _legacy(directory, "websocket:second", [{"event": "user", "chat_id": "second", "text": "also safe"}])
    with corrupt.open("a", encoding="utf-8") as handle:
        handle.write('{"event":\n')
    with pytest.raises(ValueError, match="cannot import history"):
        await sessions.state.migrate_history(HISTORY_IMPORT_MARKER, lambda rows: collect_legacy_history(rows, directory=directory))
    assert await sessions.state.list_sessions() == []
    with closing(sqlite3.connect(sessions.sessions_dir / "sessions.sqlite3")) as db:
        assert db.execute("SELECT value FROM storage_meta WHERE key=?", (HISTORY_IMPORT_MARKER,)).fetchone() is None
    _legacy(directory, "websocket:second", [{"event": "user", "chat_id": "second", "text": "fixed"}])
    await sessions.state.migrate_history(HISTORY_IMPORT_MARKER, lambda rows: collect_legacy_history(rows, directory=directory))
    assert {row["key"] for row in await sessions.state.list_sessions()} == {"websocket:first", "websocket:second"}
    await sessions.state.aclose()


async def test_segments_import_in_order_without_trusting_damaged_manifest(tmp_path):
    sessions = _manager(tmp_path)
    directory = tmp_path / "legacy"
    segments = directory / "websocket_order.segments"
    segments.mkdir(parents=True)
    for number in (2, 1):
        (segments / f"{number:06}.jsonl").write_text(json.dumps({"event": "message", "chat_id": "order", "text": str(number)}) + "\n", encoding="utf-8")
    (segments / "manifest.json").write_text("broken cached manifest", encoding="utf-8")
    _legacy(directory, "websocket:order", [{"event": "message", "chat_id": "order", "text": "3"}])
    await sessions.state.migrate_history(HISTORY_IMPORT_MARKER, lambda rows: collect_legacy_history(rows, directory=directory))
    assert await sessions.state.project_history("websocket:order", lambda history: [fact.content["text"] for fact in history.all_facts()]) == ["1", "2", "3"]
    await sessions.state.aclose()


def test_orphan_lossy_filename_requires_recorded_identity(tmp_path):
    directory = tmp_path / "legacy"
    _legacy(directory, "websocket:a_b", [{"event": "message", "text": "unknown whether a_b or a:b"}])
    with pytest.raises(ValueError, match="ambiguous legacy history identity"):
        collect_legacy_history([], directory=directory)


async def test_recorder_keeps_completed_segments_not_every_transport_delta(tmp_path):
    sessions = _manager(tmp_path)
    key = "websocket:stream"
    generation, _ = await sessions.state.open_with_metadata(key, {})
    recorder = WebUITranscriptRecorder(sessions)
    metadata = {"_history_generation": generation, "_webui_turn_id": "turn"}
    for _ in range(8):
        assert await recorder.prepare_and_append_stream_event("stream", {"event": "delta", "text": "x"}, completed_text=None, metadata=metadata, phase="answer")
    assert await recorder.prepare_and_append_stream_event("stream", {"event": "stream_end"}, completed_text="x" * 8, metadata=metadata, phase="answer")
    facts = await sessions.state.project_history(key, lambda history: history.all_facts())
    assert [fact.kind for fact in facts] == [HistoryKind.ANSWER_SEGMENT]
    assert facts[0].content["text"] == "x" * 8
    await sessions.state.aclose()


@pytest.mark.parametrize("committed", [False, True])
async def test_real_process_exit_cannot_split_fork_model_and_display_histories(tmp_path, committed):
    import subprocess
    import sys

    sessions = _manager(tmp_path)
    source = await sessions.state.get("websocket:source")
    source.add_message("user", "question")
    source.add_message("assistant", "answer")
    await sessions.state.finish_turn(source)
    await sessions.state.append_history(source.key, [
        HistoryFact(HistoryKind.INPUT, {"text": "question"}),
        HistoryFact(HistoryKind.ANSWER, {"text": "answer"}),
        HistoryFact(HistoryKind.COMPLETION),
    ], expected_generation=source.generation)
    await sessions.state.aclose()
    child = tmp_path / "crash.py"
    child.write_text('''import asyncio, os, sys
from pathlib import Path
from nanobot.session.manager import SessionManager
root = Path(sys.argv[1])
sessions = SessionManager(root / "workspace", sessions_root=root / "sessions")
original = sessions._store.append_history
def append(key, facts, *, expected_generation):
    original(key, facts, expected_generation=expected_generation)
    if sys.argv[2] == "False":
        os._exit(91)
sessions._store.append_history = append
async def run():
    await sessions.state.fork("websocket:source", "websocket:fork", 1)
    os._exit(92)
asyncio.run(run())
''', encoding="utf-8")
    result = subprocess.run([sys.executable, str(child), str(tmp_path), str(committed)], capture_output=True, timeout=15)
    assert result.returncode == (92 if committed else 91), result.stderr.decode("utf-8", errors="replace")
    reopened = _manager(tmp_path)
    fork = reopened.read_session_file("websocket:fork")
    facts = reopened.project_history("websocket:fork", lambda history: history.all_facts())
    if committed:
        assert [message["content"] for message in fork["messages"]] == ["question", "answer"]
        assert [fact.kind for fact in facts] == [HistoryKind.INPUT, HistoryKind.ANSWER, HistoryKind.COMPLETION, HistoryKind.FORK_BOUNDARY]
    else:
        assert fork is None and facts == []
    assert reopened.read_session_file(source.key)["messages"] == source.messages


async def test_legacy_import_waits_for_unmigrated_workspace_owner(tmp_path, monkeypatch):
    root = tmp_path / "sessions"
    directory = tmp_path / "webui"
    monkeypatch.setattr("nanobot.webui.history.get_webui_dir", lambda: directory)
    locations = {}
    sources = {}
    for name in ("first", "second"):
        location = SessionLocation(tmp_path / name, sessions_root=root)
        locations[name] = location
        key = f"websocket:{name}"
        source = location.sessions_dir / f"{SessionLocation.storage_key(key)}.jsonl"
        source.write_text("\n".join(json.dumps(record) for record in [
            {"_type": "metadata", "key": key, "metadata": {}},
            {"role": "user", "content": f"question {name}"},
        ]) + "\n", encoding="utf-8")
        display = _legacy(directory, key, [
            {"event": "user", "chat_id": name, "text": f"question {name}"},
            {"event": "message", "chat_id": name, "text": f"answer {name}"},
            {"event": "turn_end", "chat_id": name},
        ])
        sources.update({path: path.read_bytes() for path in (source, display)})

    async def initialize(name: str):
        sessions = SessionManager(tmp_path / name, sessions_root=root)
        key = f"websocket:{name}"
        try:
            await WebUITranscriptRecorder(sessions).initialize()
            assert [row["key"] for row in await sessions.state.list_sessions()] == [key]
            session = await sessions.state.read(key)
            assert session is not None
            assert session.messages == [{"role": "user", "content": f"question {name}"}]
            facts = await sessions.state.project_history(key, lambda history: history.all_facts())
            assert [(fact.kind, fact.content.get("text")) for fact in facts] == [
                (HistoryKind.INPUT, f"question {name}"),
                (HistoryKind.ANSWER, f"answer {name}"),
                (HistoryKind.COMPLETION, None),
            ]
        finally:
            await sessions.state.aclose()

    await initialize("second")
    assert not (locations["first"].sessions_dir / "sessions.sqlite3").exists()
    await initialize("first")
    await initialize("second")
    await initialize("first")
    assert {path: path.read_bytes() for path in sources} == sources


@pytest.mark.asyncio
async def test_legacy_import_owner_survives_delete_and_new_workspace(tmp_path):
    from nanobot.webui.history import collect_legacy_history

    root = tmp_path / "sessions"
    legacy = tmp_path / "webui"
    legacy.mkdir()
    key = "websocket:owned"
    (legacy / f"{SessionManager.safe_key(key)}.jsonl").write_text(
        '{"event":"user","chat_id":"owned","text":"keep ownership"}\n', encoding="utf-8",
    )
    first = SessionManager(tmp_path / "first", sessions_root=root)
    await first.state.migrate_history(HISTORY_IMPORT_MARKER, lambda rows: collect_legacy_history(
        rows, directory=legacy, namespace=first.sessions_dir,
    ))
    await first.state.delete(key)
    await first.state.aclose()
    second = SessionManager(tmp_path / "second", sessions_root=root)
    await second.state.migrate_history(HISTORY_IMPORT_MARKER, lambda rows: collect_legacy_history(
        rows, directory=legacy, namespace=second.sessions_dir,
    ))
    assert await second.state.read(key) is None
    await second.state.aclose()


async def test_large_display_fact_serialization_and_copy_stay_off_runtime_loop(tmp_path, monkeypatch):
    import threading

    import nanobot.session.state as state_module

    sessions = _manager(tmp_path)
    session = await sessions.state.get_header("websocket:large-activity")
    runtime_thread = threading.get_ident()
    encode, copy = HistoryFact.encode, state_module.deepcopy

    def worker_encode(fact):
        assert threading.get_ident() != runtime_thread
        return encode(fact)

    def worker_copy(value, *args, **kwargs):
        if isinstance(value, list) and value and isinstance(value[0], HistoryFact):
            assert threading.get_ident() != runtime_thread
        return copy(value, *args, **kwargs)

    monkeypatch.setattr(HistoryFact, "encode", worker_encode)
    monkeypatch.setattr(state_module, "deepcopy", worker_copy)
    recorder = WebUITranscriptRecorder(sessions)
    assert await recorder.prepare_and_append("large-activity", {
        "event": "message", "kind": "tool_hint", "text": "exec",
        "tool_events": [{"tool": "exec", "result": "x" * 1_000_000}],
    }, metadata={"_history_generation": session.generation})
    facts = await sessions.state.project_history(session.key, lambda history: history.all_facts())
    assert len(facts[0].content["tool_events"][0]["result"]) == 1_000_000
    await sessions.state.aclose()


def test_message_projection_adapter_preserves_reply_and_agent_activity():
    from nanobot.webui.history import fact_from_record, record_from_fact

    record = {
        "event": "message", "chat_id": "activity", "kind": "progress",
        "text": "Inspecting workspace", "reply_to": "message-1", "created_at_ms": 1000,
        "agent_ui": {"version": 1, "status": "working", "label": "Inspecting workspace"},
    }
    fact = HistoryFact.decode(fact_from_record(record).encode())
    assert record_from_fact(fact, "websocket:activity") == record


async def test_legacy_activity_without_record_timestamps_preserves_file_time(tmp_path):
    import os

    from nanobot.webui.session_list_index import list_webui_sessions

    sessions = _manager(tmp_path)
    session = await sessions.state.get_header("websocket:old-activity")
    directory = tmp_path / "legacy"
    path = _legacy(directory, session.key, [{
        "event": "message", "chat_id": "old-activity", "text": "historical display activity",
    }])
    os.utime(path, (1760000000, 1760000000))
    await sessions.state.migrate_history(HISTORY_IMPORT_MARKER, lambda rows: collect_legacy_history(
        rows, directory=directory,
    ))
    rows = list_webui_sessions(sessions, await sessions.state.list_metadata())
    from datetime import datetime

    assert datetime.fromisoformat(rows[0]["updated_at"]).timestamp() == 1760000000
    assert rows[0]["preview"] == "historical display activity"
    await sessions.state.aclose()


@pytest.mark.parametrize("stored_display", [False, True])
async def test_new_fork_activity_uses_creation_time_not_inherited_history(tmp_path, stored_display):
    from datetime import datetime

    from nanobot.webui.session_list_index import list_webui_sessions

    sessions = _manager(tmp_path)
    source = await sessions.state.get("websocket:old-source")
    old_time = datetime(2020, 1, 1)
    source.add_message("user", "old question", timestamp=old_time.isoformat())
    source.add_message("assistant", "old answer", timestamp=old_time.isoformat())
    await sessions.state.finish_turn(source)
    if stored_display:
        old_ms = int(old_time.timestamp() * 1000)
        await sessions.state.append_history(source.key, [
            HistoryFact(HistoryKind.INPUT, {"text": "old question"}, created_at_ms=old_ms),
            HistoryFact(HistoryKind.ANSWER, {"text": "old answer"}, created_at_ms=old_ms),
            HistoryFact(HistoryKind.COMPLETION, created_at_ms=old_ms),
        ], expected_generation=source.generation)
    forked = await create_webui_chat_fork(sessions, source_chat_id="old-source", before_user_index=1)
    assert forked is not None
    _, fork_key = forked
    fork = await sessions.state.get(fork_key)
    facts = await sessions.state.project_history(fork_key, lambda history: history.all_facts())
    assert facts[-1].kind is HistoryKind.FORK_BOUNDARY
    assert facts[-1].created_at_ms == int(fork.created_at.timestamp() * 1000)
    rows = list_webui_sessions(sessions, await sessions.state.list_metadata())
    assert rows[0]["key"] == fork_key
    await sessions.state.aclose()