Raven-1/tests/test_rpc_session.py
江国庆/Forrest c18278b8af
feat(*): import exported and reported conversations to read on the web page (#960)
## Summary

The web page can now import a Trajectory Export, a Bug Report Package or
a trajectory report and read it as a read-only conversation, transcript
and trajectory both. It is meant for developers reading a bug report
they received, and for looking at a conversation from another machine.
It builds on #948 (export package v1, the trajectory view on by default,
and the conversation menu's availability predicate).

- **Import store** (`raven/trajectory/imported_package.py`,
`imported.py`). Three kinds are accepted: a `raven_trajectory_export` v1
export, a `bug_report_package` v1, and a `raven trajectory report`
tarball. A file that is not gzip is "not a package Raven can import", an
unknown version asks for an update, anything else is "damaged or
unsafe". Packages are unpacked member by member on `package_check`'s
bounded layer, with one budget shared by both layers: 100 MiB per
package, 1 GiB unpacked for an export and 256 MiB for the report kinds,
100k members; traversal, absolute names, backslashes, symlinks, hard
links, devices, FIFOs, sparse members, repeats, directories with data
and oversized extended headers are refused, and every file is created
with `open(..., "xb")`. The package's session record is held to 32 MiB
and 50,000 records before anything reads it (the import and every open
load it whole), or the package is refused as `transcript_too_large`. The
result is normalized into one trace store: export v2 shells stay as they
are, report v1 model inputs become v2 shells that resolve to exactly the
original payload (so long conversations read whole), and a pointer that
maps to nothing in the package becomes null and reads as not recorded.
Each import lives in `<raven home>/imports/<uuid>/` with a `meta.json`
(source file name, import time, title, kind, sha256); it is unpacked
under `.staging/` and renamed into place, removed whole on failure or
cancel, and stale staging is swept at startup. Default titles: an export
takes the original conversation's title or its first user input, a bug
report the first line of its description or its report id, a trajectory
report the original title or its id. One import runs at a time on a
dedicated worker thread that owns the slot; there is no queue, a closed
page does not free the slot early, and gateway shutdown stops and waits
for it.
- **RPC and server-side guard.** New `imported.upload` (base64, under
the same 100 MiB limit as `fs.upload`) answers `imported` or `duplicate`
(by sha256), and imports another copy with `force`. New error codes:
-32029 `imported_read_only`, -32030 `imported_package_invalid`
(`data.reason` carries a stable refusal code), -32031 `imported_busy`.
An Imported Conversation's key is `imported:<uuid>`. `session.list`
appends imported rows when asked for the `imported` channel (never in
the archived list); `session.resume`, `session.title` and
`session.delete` read and write `imports/` directly and never touch the
SessionManager. A dispatcher guard allows only a vetted list of read
methods on an imported key (any of the four session parameter names, or
`channel: imported`) and refuses every other method with -32029,
including methods added later. The SessionManager refuses to save or
fork an imported key and `/file` answers 404 for one, so an imported
conversation never lands in `<workspace>/sessions/`. The trajectory
methods read an import from its own trace store; the indexer registry
gains an LRU cap (four foreign stores), idle eviction covers every
indexer, and deleting an import drops its indexer.
- **Web page.** "Import conversation" in the left menu picks a file and
uploads it; while it runs the button shows the file name and is
disabled, every refusal has its own message, and a repeated package
offers "Open existing" or "Import anyway" (the confirm dialog gains an
optional second action). An "Imported" group below Recent (hidden when
empty) lists the imports with only Rename (inline) and Delete, and the
source file name in the tooltip; search mode matches, and "delete all"
in settings leaves imports alone. An open import is read-only: no input
box (and no transcript mask left behind for it), no title editing in the
header, no Branch, the conversation menu hidden with its slot kept, no
working-directory tag. Sub-agent panes, DAG runs, attachments, delivered
files and generated images show as unavailable without sending requests
the server would refuse; a package without a transcript says so and the
trajectory view still opens; `[REDACTED:...]` placeholders are shown as
they are.
- **Docs and tests.** The webui page (English and Chinese) gains "Import
a conversation"; `CONTEXT.md` (Imported Conversation) and
`ui-web/CONTEXT.md` define the term. Unit tests cover every refusal of
the unpacker, normalization, meta and titles, duplicates and `force`,
runner admission and cleanup, the guard (checked against every
registered method), and the imported branches of `session.*` and
`trajectory.*`; vitest covers the import entry, the rail and the
read-only mode. A new integration test imports packages made by the real
writers into a second raven home and reads them back, and covers
duplicates, rename, delete, refused writes, and damaged or hostile
packages (an oversized upload and a decompression bomb built at the
shipped limits).

## Type

- [ ] Fix
- [x] Feature
- [ ] Docs
- [ ] CI / tooling
- [ ] Refactor
- [ ] Other

## Verification

- The branch merges the current `main` (merge commits, no conflicts; the
generated RPC bindings and i18n catalogue still match their sources);
everything below ran on the merged tree unless noted.
- `uv run pytest tests/ -q` (full suite): 10 failed / 31918 passed / 188
skipped. All 10 (proxy environment, IPv6 prefix resolution, node
runtime) also fail on `main` on this machine; none is introduced by this
branch.
- `uv run pytest -m integration
tests/integration/test_imported_conversation_e2e.py
tests/integration/test_web_traj_export_e2e.py
tests/integration/test_web_bugreport_e2e.py -q`: 16 passed.
- Changed-line coverage over the feature's tests (before the merge),
`scripts/coverage_gate.py diff --base-ref origin/main --threshold 90`:
97.75% (955/977).
- `uv run ruff check`, `uv run ruff format --check`, `make
lint-imports`, `make lint-types`: pass.
- ui-web (the merge touches no frontend, i18n or RPC contract file):
`npm run gen:check` (240 methods), `npm run lint`, `npm run type-check`,
`npx vitest run` (248 files / 3923 passed), `npm run build` and `python3
ui-web/build.py` (both boot snapshots match), `check-page.mjs`,
`check-css.mjs`, `check-class-namespace.mjs`: pass.
- ui-tui: `npm run lint`, `npm run lint:rpc`, `npm run lint:i18n`, `npm
run type-check`, `npm test` (144 files / 2111 passed), `npm run build`:
pass.
- `uv run --group docs mkdocs build -f docs-site/mkdocs.yml --strict`:
pass.
- `make check-source-language check-large-files check-commits`: pass.
- Headless Chromium against a real gateway (`raven web --foreground` on
a fresh raven home, at 1280 and 360 px): imported a real export and a
real bug report through the page, opened both read-only, read a
122-message model input to its end in the trajectory view, used both
duplicate choices, renamed and reloaded, deleted, and uploaded a text
file; no page errors. The same flow on the built page with offline
fixtures.

- [x] Relevant tests pass locally
- [x] Relevant lint / type checks pass locally
- [x] User-facing docs or screenshots are updated when needed

## Risk

- `imported.upload` takes untrusted archives. The unpacker is bounded
and refuses unsafe members, the guard is an allow-list, and the
SessionManager refuses imported keys as a backstop; the import directory
is under the raven home, which `fs.*` and `/file` already refuse to
serve.
- An export is not redacted, so importing someone else's export gives
you its raw contents; the docs say so.
- Two processes sharing one raven home (for example `raven serve` and
`raven web`) do not see each other's import slot, so importing the same
package in both at once can leave two copies; the extra one can be
deleted.
- Rollback: revert the squash commit. Imported data stays in `<raven
home>/imports/`, which older versions ignore and which can be removed by
hand.

- [x] Security impact considered
- [x] Backward compatibility considered
- [x] Rollback path is clear for risky changes

## Related Issues

N/A

---------

Co-authored-by: Claude (claude-opus-5-5) <noreply@anthropic.com>
2026-10-16 06:54:34 +08:00

4008 lines
162 KiB
Python

"""Tests for ``session.create`` / ``session.close`` / ``session.resume`` handlers.
These handlers return realistic return shapes (NOT -32012 errors) because
ui-tui's ``useSessionLifecycle.ts`` boot path requires a usable
``SessionInfo`` payload before the UI renders.
A previous shape (session-manager-record: ``session_id`` / ``channel`` /
``chat_id`` / ``created_at`` / …) was wrong — it didn't match the TS ``SessionInfo`` at
``ui-tui/src/types.ts:148`` that ``SessionPanel`` consumes via
``Object.entries(info.skills)``. The current tests assert the
SessionPanel-required keys (``model`` / ``skills`` / ``tools``) plus the
optional banner fields actually populated by the stub.
"""
from __future__ import annotations
import json
import os
import re
from datetime import date, datetime, timedelta, timezone
from pathlib import Path
from types import SimpleNamespace
import pytest
from raven.agent.workdir import WorkdirPolicy, WorkdirResolver, default_channel_root
from raven.config.loader import load_config
from raven.rpc.dispatcher import Dispatcher
from raven.rpc.errors import TurnInProgressError
from raven.rpc.methods import session as session_module
from raven.rpc.methods import turn as turn_module
from raven.rpc.methods.session import (
_map_to_wire,
register_session_methods,
session_archive,
session_branch,
session_close,
session_create,
session_delete,
session_export,
session_list,
session_most_recent,
session_pin,
session_resume,
session_set_harness,
session_set_mode,
session_title,
session_usage,
)
from raven.session.manager import SessionManager
_SESSION_ID_RE = re.compile(r"^tui:\d{8}_\d{6}_[0-9a-f]{6}$")
# Required by SessionPanel (``ui-tui/src/components/branding.tsx``):
# * ``info.model`` — line 231 ``info.model.split('/').pop()``
# * ``info.skills`` — line 138 ``Object.entries(info.skills)``
# * ``info.tools`` — line 166 ``Object.entries(info.tools)``
_SESSION_PANEL_REQUIRED_KEYS = {"model", "skills", "tools"}
def _assert_session_info(info: dict) -> None:
"""Assert that ``info`` matches the TS ``SessionInfo`` wire shape that
``SessionPanel`` consumes (``ui-tui/src/types.ts:148``)."""
# Must contain everything SessionPanel reads without optional-chaining.
assert _SESSION_PANEL_REQUIRED_KEYS.issubset(set(info)), (
f"missing SessionPanel-required keys; got {set(info)}, missing {_SESSION_PANEL_REQUIRED_KEYS - set(info)}"
)
# Types: skills / tools must be dicts so ``Object.entries`` works in JS.
assert isinstance(info["skills"], dict), "info.skills must be a dict (Object.entries target)"
assert isinstance(info["tools"], dict), "info.tools must be a dict (Object.entries target)"
assert isinstance(info["model"], str) and info["model"], "info.model must be a non-empty str"
async def test_session_create_returns_panel_compatible_info() -> None:
result = await session_create({})
assert set(result) == {"session_id", "info"}
assert _SESSION_ID_RE.match(result["session_id"]), result["session_id"]
_assert_session_info(result["info"])
async def test_session_close_returns_ok() -> None:
result = await session_close({})
assert result == {"ok": True}
async def test_session_resume_unknown_id_returns_fresh_mint_with_empty_messages(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""An unknown session_id returns a fresh-minted key + empty messages (no error, no file)."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
result = await session_resume({"session_id": "tui:nonexistent_id"})
assert set(result) == {"session_id", "info", "messages"}
assert _SESSION_ID_RE.match(result["session_id"]), result["session_id"]
_assert_session_info(result["info"])
assert result["messages"] == []
async def test_session_info_skills_and_tools_are_empty_dicts_in_v01() -> None:
"""Regression for P0 fix #2: ``Object.entries(info.skills)`` in
``SessionPanel`` (``ui-tui/src/components/branding.tsx:138``) must not
throw ``"Cannot convert undefined or null to object"``. Empty-dict v0.1
placeholders satisfy that contract."""
result = await session_create({})
assert result["info"]["skills"] == {}
assert result["info"]["tools"] == {}
@pytest.mark.parametrize(
"method,expected_keys",
[
("session.create", {"session_id", "info"}),
("session.close", {"ok"}),
("session.resume", {"session_id", "info", "messages"}),
],
)
async def test_session_handlers_dispatch_via_dispatcher(method: str, expected_keys: set[str]) -> None:
"""End-to-end: each handler is reachable through the Dispatcher and
returns a JSON-RPC success frame (no ``error`` key)."""
d = Dispatcher()
register_session_methods(d)
resp = await d.dispatch({"jsonrpc": "2.0", "id": 1, "method": method, "params": {}})
assert "error" not in resp, f"{method} unexpectedly raised: {resp}"
assert set(resp["result"]) == expected_keys
async def test_session_create_ignores_extra_params() -> None:
"""Extra params are accepted without error; channel override is not honoured."""
result = await session_create({"unused": "ignored", "channel": "telegram", "cols": 120})
assert _SESSION_ID_RE.match(result["session_id"]), result["session_id"]
_assert_session_info(result["info"])
async def test_session_create_returns_unique_ids_per_call() -> None:
r1 = await session_create({})
r2 = await session_create({})
assert r1["session_id"] != r2["session_id"], "two consecutive session.create calls returned the same session_id"
async def test_session_create_writes_no_file(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""Lazy mint: session.create must not create any file under the workspace.
The handler does not touch the filesystem today; this test pins the seam
a future in-module persistence would go through (module-level
``load_config`` -> ``config.workspace_path`` -> ``SessionManager``).
``SessionManager.__init__`` calls ``ensure_dir`` on the sessions dir, so
even constructing a manager — let alone saving — would trip the snapshot.
"""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
before = {p for p in tmp_path.rglob("*")}
await session_create({})
after = {p for p in tmp_path.rglob("*")}
assert after == before, f"lazy mint must not touch the workspace; new paths: {after - before}"
async def test_session_create_accepts_title_param_without_error() -> None:
"""Accepting an optional title must not crash (title storage is a later task)."""
result = await session_create({"title": "My new chat"})
assert _SESSION_ID_RE.match(result["session_id"]), result["session_id"]
# ---------------------------------------------------------------------------
# P2-B new tests: session.resume with real transcript + session.close flush
# ---------------------------------------------------------------------------
def _write_session(tmp_path: Path, session_key: str, messages: list[dict]) -> None:
"""Write a minimal JSONL session file with the given messages."""
mgr = SessionManager(tmp_path)
session = mgr.get_or_create(session_key)
for msg in messages:
session.add_message(msg["role"], msg["content"])
mgr.save(session)
async def test_session_resume_loads_n_stored_messages(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""Resume of a session with 3 stored messages returns exactly 3 wire messages."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
session_key = "tui:20260610_143052_aabbcc"
stored = [
{"role": "user", "content": "hello"},
{"role": "assistant", "content": "hi there"},
{"role": "user", "content": "how are you"},
]
_write_session(tmp_path, session_key, stored)
result = await session_resume({"session_id": session_key})
assert set(result) == {"session_id", "info", "messages"}
assert result["session_id"] == session_key
_assert_session_info(result["info"])
msgs = result["messages"]
assert len(msgs) == 3, f"expected 3 wire messages, got {len(msgs)}: {msgs}"
assert msgs[0]["role"] == "user"
assert msgs[0]["text"] == "hello"
assert msgs[1]["role"] == "assistant"
assert msgs[1]["text"] == "hi there"
assert msgs[2]["role"] == "user"
assert msgs[2]["text"] == "how are you"
def _loop_with_resolver(tmp_path: Path, *, explicit: Path | None = None) -> SimpleNamespace:
"""A stand-in loop that answers ``peek_session_workdir`` the way the real one
does: through a ``WorkdirResolver`` built the way ``raven serve`` builds it."""
resolver = WorkdirResolver(
WorkdirPolicy.PER_CHANNEL,
agent_home=tmp_path / "home",
sessions=SessionManager(tmp_path),
**({"explicit_workdir": explicit} if explicit else {}),
)
return SimpleNamespace(
peek_session_workdir=lambda key: resolver.resolve(key, create=False),
# The rest of what the init bundle reads off a loop. Present so the
# bundle builds; this stand-in is about one question only.
tools=SimpleNamespace(tool_names=[]),
context=SimpleNamespace(skills=SimpleNamespace(list_skills=lambda **_: [])),
model=None,
strategies=SimpleNamespace(get=lambda _name: None),
)
async def test_session_resume_reports_where_the_session_actually_runs(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""A session pinned to its own directory resumes reporting THAT as ``info.cwd``.
The served page shortens every path it shows against this value, so the
launch directory is the wrong answer for a session started elsewhere.
"""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
pinned = tmp_path / "elsewhere"
pinned.mkdir()
session_key = "tui:20260610_143052_workdir"
mgr = SessionManager(tmp_path)
session = mgr.get_or_create(session_key)
session.add_message("user", "hello")
session.metadata["workdir"] = str(pinned)
mgr.save(session)
result = await session_resume({"session_id": session_key}, agent_loop_factory=lambda: _loop_with_resolver(tmp_path))
assert result["info"]["cwd"] == str(pinned)
async def test_session_resume_sizes_the_banner_by_the_model_the_session_switched_to(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""The banner reports the session's model, so it must report that model's
window: the pair is what the context bar divides. The same baseline serves
``session.usage``, and both used to read the window off the default
binding.
"""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
cfg.agents.defaults.context_window_tokens = None
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
windows = {"boot/model": 100_000, "switched/model": 200_000}
monkeypatch.setattr(session_module, "resolve_context_window", lambda model: windows.get(model, 0))
session_key = "tui:20260610_143052_switched"
_write_session(tmp_path, session_key, [{"role": "user", "content": "hello"}])
loop = _loop_with_resolver(tmp_path)
loop.model = "boot/model"
loop.session_model = lambda _key: "switched/model"
result = await session_resume({"session_id": session_key}, agent_loop_factory=lambda: loop)
assert result["info"]["model"] == "switched/model"
assert result["info"]["context_window"] == 200_000
assert result["info"]["usage"]["context_max"] == 200_000
async def test_session_resume_reports_the_policy_default_not_the_launch_dir(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""The gateway's own case: nothing pinned, and its turns still do not run here.
``raven serve`` resolves per channel, so a ``tui:`` session lands under the
channel root -- a sibling of agent home. Reading the session's stored
``workdir`` alone answered ``os.getcwd()`` for every such session, which is
the case the page hits most.
"""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
session_key = "tui:20260610_143052_policy"
_write_session(tmp_path, session_key, [{"role": "user", "content": "hello"}])
result = await session_resume({"session_id": session_key}, agent_loop_factory=lambda: _loop_with_resolver(tmp_path))
expected = default_channel_root(tmp_path / "home") / "tui"
assert result["info"]["cwd"] == str(expected)
assert result["info"]["cwd"] != os.getcwd()
async def test_session_resume_lets_an_explicit_workdir_outrank_the_stored_one(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""``-w`` beats the persisted override, because that is what the resolver does.
Reading the metadata directly reported a directory the session's turns were
not using -- a new wrong answer rather than a missing one.
"""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
stored = tmp_path / "stored"
stored.mkdir()
explicit = tmp_path / "explicit"
explicit.mkdir()
session_key = "tui:20260610_143052_explicit"
mgr = SessionManager(tmp_path)
session = mgr.get_or_create(session_key)
session.add_message("user", "hello")
session.metadata["workdir"] = str(stored)
mgr.save(session)
result = await session_resume(
{"session_id": session_key},
agent_loop_factory=lambda: _loop_with_resolver(tmp_path, explicit=explicit),
)
assert result["info"]["cwd"] == str(explicit)
async def test_session_create_reports_where_the_new_session_will_run(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""A new session's ``cwd`` is its own directory, not the gateway's."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
result = await session_create({}, agent_loop_factory=lambda: _loop_with_resolver(tmp_path))
expected = default_channel_root(tmp_path / "home") / "tui"
assert result["info"]["cwd"] == str(expected)
def _warming_loop(tmp_path: Path, warmed: list[Path], *, fail: bool = False, exec_tool: bool = True) -> SimpleNamespace:
"""The resolver stand-in with a command tool whose warm-up a session open calls.
Each warm-up records the directory it was handed, so a test can tell it
covered the directory the session will actually work in."""
loop = _loop_with_resolver(tmp_path)
loop.sessions = SessionManager(tmp_path)
# Resolved through the manager the open writes the override into, as the real loop is.
resolver = WorkdirResolver(WorkdirPolicy.PER_CHANNEL, agent_home=tmp_path / "home", sessions=loop.sessions)
loop.peek_session_workdir = lambda key: resolver.resolve(key, create=False)
async def warm(target: Path) -> None:
warmed.append(target)
if fail:
raise RuntimeError("git is gone")
command = SimpleNamespace(warm=warm)
loop.tools = SimpleNamespace(tool_names=[], get=lambda name: command if exec_tool and name == "exec" else None)
return loop
async def test_opening_a_session_warms_the_directory_it_will_work_in(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""The first command of a session would otherwise hash a large tree inside
its own call. A new session warms its directory as it opens, after a
requested workdir has become the session's, so the warm-up covers it."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
pinned = tmp_path / "project"
pinned.mkdir()
warmed: list[Path] = []
loop = _warming_loop(tmp_path, warmed)
monkeypatch.setattr(session_module, "manager_for", lambda *_: loop.sessions)
await session_create({"workdir": str(pinned)}, agent_loop_factory=lambda: loop)
assert warmed == [pinned.resolve()]
async def test_resuming_a_session_warms_its_directory(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
warmed: list[Path] = []
loop = _warming_loop(tmp_path, warmed)
session_key = "tui:20260929_120000_warm"
stored = loop.sessions.get_or_create(session_key)
stored.add_message("user", "hello")
loop.sessions.save(stored)
expected = loop.peek_session_workdir(session_key)
expected.mkdir(parents=True, exist_ok=True)
monkeypatch.setattr(session_module, "manager_for", lambda *_: loop.sessions)
await session_resume({"session_id": session_key}, agent_loop_factory=lambda: loop)
assert warmed == [expected]
@pytest.mark.parametrize("fail", [True, False], ids=["warm-up raises", "no command tool"])
async def test_a_warm_up_that_cannot_run_does_not_fail_the_open(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, fail: bool
) -> None:
"""A warm-up is best-effort: without it the first command stages the tree
itself, so a broken one -- or a loop with no command to warm for -- must
not cost the user the session."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
pinned = tmp_path / "project"
pinned.mkdir()
warmed: list[Path] = []
loop = _warming_loop(tmp_path, warmed, fail=fail, exec_tool=fail)
monkeypatch.setattr(session_module, "manager_for", lambda *_: loop.sessions)
result = await session_create({"workdir": str(pinned)}, agent_loop_factory=lambda: loop)
assert result["session_id"].startswith("tui:")
assert warmed == ([pinned.resolve()] if fail else [])
async def test_session_resume_keeps_the_launch_dir_with_no_loop_to_ask(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""No loop, no resolver: the answer stays this process's own directory."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
session_key = "tui:20260610_143052_noloop"
_write_session(tmp_path, session_key, [{"role": "user", "content": "hello"}])
result = await session_resume({"session_id": session_key})
assert result["info"]["cwd"] == os.getcwd()
async def test_session_resume_joins_text_blocks_of_list_content(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Multimodal user messages store LIST content (agent loop history sync).
The wire ``text`` must join the ``text`` fields of ``type == "text"`` dict
blocks (non-text blocks skipped) — not render the Python list repr.
"""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
session_key = "tui:20260610_143052_ffeedd"
mgr = SessionManager(tmp_path)
session = mgr.get_or_create(session_key)
session.add_message(
"user",
[
{"type": "text", "text": "look at this"},
{"type": "image_url", "image_url": {"url": "data:image/png;base64,xxxx"}},
{"type": "text", "text": "what is it?"},
],
)
mgr.save(session)
result = await session_resume({"session_id": session_key})
msgs = result["messages"]
assert len(msgs) == 1
assert msgs[0]["role"] == "user"
assert msgs[0]["text"] == "look at this what is it?"
assert "[{" not in msgs[0]["text"], "list repr must never leak into the wire text"
async def test_session_resume_maps_tool_role_with_name_and_context(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""A stored role="tool" message passes through with name/context preserved.
Known degradation: the TS renderer (``domain/messages.ts:54``) collapses
tool rows into a generic tool trail line attached to the next assistant
message — full tool output is not re-rendered on resume.
"""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
session_key = "tui:20260610_143052_112233"
mgr = SessionManager(tmp_path)
session = mgr.get_or_create(session_key)
session.add_message("user", "run the tool")
session.add_message("tool", "tool output here", name="exec", context="ls -la")
session.add_message("assistant", "done")
mgr.save(session)
result = await session_resume({"session_id": session_key})
msgs = result["messages"]
assert len(msgs) == 3
tool_msg = msgs[1]
assert tool_msg["role"] == "tool"
assert tool_msg["text"] == "tool output here"
assert tool_msg["name"] == "exec"
assert tool_msg["context"] == "ls -la"
async def test_session_resume_skips_malformed_stored_lines(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""A roleless message line mid-transcript is skipped, not a -32603 crash.
One corrupt line must never permanently brick resume for that id
("session bootstrap failed" panel) — the good messages still come back.
"""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
session_path = tmp_path / "sessions" / "tui" / "20260610_143052_445566.jsonl"
session_path.parent.mkdir(parents=True)
session_path.write_text(
json.dumps({"role": "user", "content": "hello"})
+ "\n"
+ json.dumps({"foo": "bar"})
+ "\n"
+ json.dumps({"role": "assistant", "content": "hi"})
+ "\n"
)
result = await session_resume({"session_id": "tui:20260610_143052_445566"})
assert result["session_id"] == "tui:20260610_143052_445566"
msgs = result["messages"]
assert len(msgs) == 2, f"malformed line must be skipped, got: {msgs}"
assert msgs[0]["text"] == "hello"
assert msgs[1]["text"] == "hi"
async def test_session_resume_corrupt_load_falls_back_to_fresh_mint(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""A load path that raises must degrade to the fresh-mint response, not crash."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
class _BoomManager:
_cache: dict = {}
def _load(self, key: str):
raise RuntimeError("corrupt session store")
monkeypatch.setattr("raven.session.resolve.build_manager", lambda cfg: _BoomManager())
result = await session_resume({"session_id": "tui:20260610_143052_998877"})
assert set(result) == {"session_id", "info", "messages"}
assert _SESSION_ID_RE.match(result["session_id"]), result["session_id"]
_assert_session_info(result["info"])
assert result["messages"] == []
async def test_session_resume_prefers_live_cache_with_unflushed_tail(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Resume reads the live manager cache first — a mid-turn unflushed tail
message must appear in the transcript even though disk lags behind."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
session_key = "tui:20260610_143052_778899"
mgr = SessionManager(tmp_path)
session = mgr.get_or_create(session_key)
session.add_message("user", "persisted message")
mgr.save(session)
session.add_message("assistant", "unflushed tail")
monkeypatch.setattr("raven.session.resolve.build_manager", lambda cfg: mgr)
result = await session_resume({"session_id": session_key})
msgs = result["messages"]
assert len(msgs) == 2, f"cached unflushed tail must be visible, got: {msgs}"
assert msgs[1]["text"] == "unflushed tail"
async def test_session_resume_unknown_id_does_not_create_file(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""Resuming an unknown session_id does not write any file to the workspace."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
sessions_dir = tmp_path / "sessions"
before = set(sessions_dir.rglob("*")) if sessions_dir.exists() else set()
await session_resume({"session_id": "tui:totally_unknown_id"})
after = set(sessions_dir.rglob("*")) if sessions_dir.exists() else set()
# Manager construction ensure_dirs the sessions dir, so directories are
# expected; only files indicate actual persistence.
new_files = {p for p in after - before if p.is_file()}
assert not new_files, f"resume of unknown id must not write files; got: {new_files}"
async def test_session_close_flushes_dirty_cached_session(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""close flushes a dirty cached session — message added after last save lands on disk."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
session_key = "tui:20260610_143052_ccddee"
mgr = SessionManager(tmp_path)
session = mgr.get_or_create(session_key)
session.add_message("user", "first message")
mgr.save(session)
session.add_message("assistant", "second message")
assert session._persisted_count == 1
assert len(session.messages) == 2
monkeypatch.setattr("raven.session.resolve.build_manager", lambda cfg: mgr)
result = await session_close({"session_id": session_key})
assert result == {"ok": True}
session_path = tmp_path / "sessions" / "tui" / "20260610_143052_ccddee.jsonl"
assert session_path.exists()
lines = [json.loads(ln) for ln in session_path.read_text().splitlines() if ln.strip()]
msg_lines = [ln for ln in lines if ln.get("_type") != "metadata"]
assert len(msg_lines) == 2, f"expected 2 messages on disk after flush, got {msg_lines}"
async def test_session_close_save_failure_still_returns_ok(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""A save that raises during close degrades to a warning, not a -32603."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
session_key = "tui:20260610_143052_boom01"
mgr = SessionManager(tmp_path)
session = mgr.get_or_create(session_key)
session.add_message("user", "dirty message")
def _boom_save(s) -> None:
raise OSError("disk full")
monkeypatch.setattr(mgr, "save", _boom_save)
monkeypatch.setattr("raven.session.resolve.build_manager", lambda cfg: mgr)
result = await session_close({"session_id": session_key})
assert result == {"ok": True}
async def test_session_close_returns_ok_for_unknown_key(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""close with an unknown/absent session_key must not raise and returns ok."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
result = await session_close({"session_id": "tui:does_not_exist"})
assert result == {"ok": True}
async def test_session_close_returns_ok_without_session_key() -> None:
"""close with no params must not raise (legacy call path)."""
result = await session_close({})
assert result == {"ok": True}
# ---------------------------------------------------------------------------
# P2-C new tests: session.list / session.delete / session.most_recent / session.title
# ---------------------------------------------------------------------------
async def test_session_list_returns_sessions_for_tui_channel(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""session.list returns a sessions list from the tui channel, sorted by updated_at desc."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
from raven.session.manager import SessionManager
mgr = SessionManager(tmp_path)
for key in ("tui:20260610_100000_aaa111", "tui:20260610_110000_bbb222"):
s = mgr.get_or_create(key)
s.add_message("user", "hello")
mgr.save(s)
monkeypatch.setattr("raven.session.resolve.build_manager", lambda cfg: mgr)
result = await session_list({})
assert "sessions" in result
items = result["sessions"]
assert len(items) == 2
ids = [item["id"] for item in items]
assert "tui:20260610_100000_aaa111" in ids
assert "tui:20260610_110000_bbb222" in ids
async def test_session_list_says_which_sessions_have_a_turn_in_flight(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""A page that has just loaded has no other way to know: the running turn's
frames went to a socket it did not have. Without the flag the rail draws a
conversation that is answering as idle, and a send into it is refused."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
mgr = SessionManager(tmp_path)
busy_key, quiet_key = "tui:20260610_100000_busy11", "tui:20260610_110000_quiet1"
for key in (busy_key, quiet_key):
s = mgr.get_or_create(key)
s.add_message("user", "hello")
mgr.save(s)
monkeypatch.setattr("raven.session.resolve.build_manager", lambda cfg: mgr)
turn_module._active_turns.clear()
try:
turn_module._active_turns[busy_key] = object()
result = await session_list({})
finally:
turn_module._active_turns.clear()
running = {item["id"]: item["running"] for item in result["sessions"]}
assert running == {busy_key: True, quiet_key: False}
async def test_session_resume_says_whether_the_session_is_answering_right_now(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""The same fact on the bundle a reload reads, which is where the page
learns to put the stop button back."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
session_key = "tui:20260610_143052_running"
_write_session(tmp_path, session_key, [{"role": "user", "content": "read the repo"}])
turn_module._active_turns.clear()
try:
quiet = await session_resume({"session_id": session_key})
turn_module._active_turns[session_key] = object()
busy = await session_resume({"session_id": session_key})
finally:
turn_module._active_turns.clear()
assert quiet["info"]["running"] is False
assert busy["info"]["running"] is True
async def test_a_direct_chat_alone_does_not_report_the_conversation_as_running(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""A sub-agent's direct chat runs on ``session#agent/handle``, a lane of its
own whose events are routed to that chat. Reported as the conversation's
running turn, a reload during it armed the main composer with a stop button
that cancelled the wrong lane and queued every message until the chat ended
and the reader reloaded again. The main lane is the fact the page reads."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
mgr = SessionManager(tmp_path)
session_key = "tui:20260610_143052_direct"
s = mgr.get_or_create(session_key)
s.add_message("user", "ask the coder")
mgr.save(s)
monkeypatch.setattr("raven.session.resolve.build_manager", lambda cfg: mgr)
turn_module._active_turns.clear()
try:
turn_module._active_turns[f"{session_key}#raven-code/h1"] = object()
assert turn_module.is_session_busy(session_key) is True
chatting = await session_resume({"session_id": session_key})
listed = await session_list({})
turn_module._active_turns[session_key] = object()
answering = await session_resume({"session_id": session_key})
finally:
turn_module._active_turns.clear()
assert chatting["info"]["running"] is False
assert {item["id"]: item["running"] for item in listed["sessions"]} == {session_key: False}
assert answering["info"]["running"] is True
class _LaneScheduler:
"""The spine's scheduler as these readers ask it: one lane in flight."""
def __init__(self, lane: str) -> None:
self._lane = lane
def has_inflight(self, conversation_id: str) -> bool:
return conversation_id == self._lane
async def test_session_list_says_running_for_a_turn_turn_send_never_saw(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""A cron run is in flight on the lane and in no map this surface writes.
``_active_turns`` is ``turn.send``'s bookkeeping, so a scheduled turn that
had been answering for a minute and a half listed as idle: no badge on the
rail, and opening it gave an idle composer over a question with no answer.
The lane is the fact, whoever filed the work onto it.
"""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
mgr = SessionManager(tmp_path)
cron_key, quiet_key = "cron:20260610_100000_job111", "tui:20260610_110000_quiet1"
for key in (cron_key, quiet_key):
s = mgr.get_or_create(key)
s.add_message("user", "hello")
mgr.save(s)
monkeypatch.setattr("raven.session.resolve.build_manager", lambda cfg: mgr)
turn_module._active_turns.clear()
monkeypatch.setattr(turn_module, "_scheduler", _LaneScheduler(cron_key))
result = await session_list({"channels": ["tui", "cron"]})
running = {item["id"]: item["running"] for item in result["sessions"]}
assert running == {cron_key: True, quiet_key: False}
async def test_session_resume_says_running_for_a_turn_turn_send_never_saw(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""The same fact on the bundle a reload reads, and the elapsed with it:
the question the scheduler is answering is on disk, so the age measures
from it exactly as it does for a turn this surface started."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
monkeypatch.setattr(session_module, "datetime", _FixedGatewayClock)
session_key = "cron:20260610_143052_job222"
_running_session(tmp_path, session_key, "2026-09-22T10:00:00")
turn_module._active_turns.clear()
monkeypatch.setattr(turn_module, "_scheduler", _LaneScheduler(session_key))
busy = await session_resume({"session_id": session_key})
monkeypatch.setattr(turn_module, "_scheduler", _LaneScheduler("cron:someone_else"))
quiet = await session_resume({"session_id": session_key})
assert busy["info"]["running"] is True
assert busy["info"]["running_ms"] == 5 * 60 * 1000
assert quiet["info"]["running"] is False
async def test_session_create_reports_a_session_nothing_is_running_in() -> None:
"""A key minted this instant cannot have a turn on it, and the field is
required on the bundle either way."""
result = await session_create({})
assert result["info"]["running"] is False
_GATEWAY_NOW = datetime(2026, 9, 22, 10, 5, tzinfo=timezone(timedelta(hours=8)))
class _FixedGatewayClock(datetime):
"""A gateway whose wall clock stands at 10:05 in a +08:00 zone.
Answers the two ways the stdlib does, because the measurement depends on
both: naive is that zone's wall clock, aware is the same instant rendered
in the zone asked for.
"""
@classmethod
def now(cls, tz: timezone | None = None) -> datetime:
return _GATEWAY_NOW.astimezone(tz) if tz is not None else _GATEWAY_NOW.replace(tzinfo=None)
def _running_session(tmp_path: Path, session_key: str, stamp: str) -> None:
mgr = SessionManager(tmp_path)
session = mgr.get_or_create(session_key)
session.add_message("user", "read the repo", timestamp=stamp)
mgr.save(session)
@pytest.mark.parametrize("stamp", ["2026-09-22T10:00:00", "2026-09-22T02:00:00+00:00"])
async def test_session_resume_measures_the_running_turn_against_the_gateway_clock(
stamp: str, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""The age of the turn, not the stamp it is measured from.
The loop writes that stamp off this machine's wall clock with no offset on
it, so a reader in another zone parsing it against its own clock got the
difference between the two zones back as the turn's age -- a turn five
minutes old reading as eight hours, or as not yet started. Measured here
the zone cancels, which is why both spellings of the same instant answer
the same number.
"""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
monkeypatch.setattr(session_module, "datetime", _FixedGatewayClock)
session_key = "tui:20260610_143052_running"
_running_session(tmp_path, session_key, stamp)
turn_module._active_turns.clear()
try:
turn_module._active_turns[session_key] = object()
busy = await session_resume({"session_id": session_key})
finally:
turn_module._active_turns.clear()
assert busy["info"]["running_ms"] == 5 * 60 * 1000
async def test_session_resume_reports_no_elapsed_it_cannot_measure(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Nothing in flight has no age, and an unreadable stamp is not a zero.
Zero would draw a turn that has just this second started, on a turn that
has been running for however long: the client leaves its clock alone on a
null and has nothing to leave it alone on if the field is a guess.
"""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
session_key = "tui:20260610_143052_nostamp"
_running_session(tmp_path, session_key, "the day before yesterday")
turn_module._active_turns.clear()
try:
quiet = await session_resume({"session_id": session_key})
turn_module._active_turns[session_key] = object()
busy = await session_resume({"session_id": session_key})
finally:
turn_module._active_turns.clear()
assert quiet["info"].get("running_ms") is None
assert busy["info"]["running_ms"] is None
async def test_session_list_sorted_by_updated_at_desc(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""session.list returns sessions ordered by updated_at descending."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
from raven.session.manager import SessionManager
mgr = SessionManager(tmp_path)
older = mgr.get_or_create("tui:20260610_090000_old111")
older.add_message("user", "old")
mgr.save(older)
newer = mgr.get_or_create("tui:20260610_120000_new222")
newer.add_message("user", "new")
mgr.save(newer)
monkeypatch.setattr("raven.session.resolve.build_manager", lambda cfg: mgr)
result = await session_list({})
items = result["sessions"]
assert items[0]["id"] == "tui:20260610_120000_new222"
assert items[1]["id"] == "tui:20260610_090000_old111"
async def test_session_list_item_shape(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""Each session.list item carries id, message_count, preview, started_at, title."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
from raven.session.manager import SessionManager
mgr = SessionManager(tmp_path)
s = mgr.get_or_create("tui:20260610_100000_shape1")
s.add_message("user", "first user message")
s.add_message("assistant", "reply")
mgr.save(s)
monkeypatch.setattr("raven.session.resolve.build_manager", lambda cfg: mgr)
result = await session_list({})
item = result["sessions"][0]
assert "id" in item
assert "message_count" in item
assert "preview" in item
assert "started_at" in item
assert "title" in item
assert item["message_count"] == 2
assert isinstance(item["started_at"], (int, float))
async def test_session_list_item_carries_the_workdir_it_was_pinned_to(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""The rail groups conversations by whether they were pinned to a folder,
so the list says which: the stored override, or null for the policy default."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
from raven.session.manager import SessionManager
mgr = SessionManager(tmp_path)
pinned = mgr.get_or_create("tui:20260610_100000_pinned")
pinned.metadata["workdir"] = "/work/project"
pinned.add_message("user", "hello")
mgr.save(pinned)
plain = mgr.get_or_create("tui:20260610_110000_plainx")
plain.add_message("user", "hello")
mgr.save(plain)
monkeypatch.setattr("raven.session.resolve.build_manager", lambda cfg: mgr)
items = {item["id"]: item for item in (await session_list({}))["sessions"]}
assert items["tui:20260610_100000_pinned"]["workdir"] == "/work/project"
assert items["tui:20260610_110000_plainx"]["workdir"] is None
async def test_session_list_only_tui_channel(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""session.list does not include sessions from non-tui channels."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
from raven.session.manager import SessionManager
mgr = SessionManager(tmp_path)
tui_session = mgr.get_or_create("tui:20260610_100000_tui01")
tui_session.add_message("user", "tui msg")
mgr.save(tui_session)
cli_session = mgr.get_or_create("cli:20260610_100000_cli01")
cli_session.add_message("user", "cli msg")
mgr.save(cli_session)
monkeypatch.setattr("raven.session.resolve.build_manager", lambda cfg: mgr)
result = await session_list({})
ids = [item["id"] for item in result["sessions"]]
assert all(i.startswith("tui:") for i in ids)
assert "cli:20260610_100000_cli01" not in ids
async def test_session_list_honors_limit_after_sort(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""limit slices AFTER the updated_at-desc sort — newest sessions win."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
from raven.session.manager import SessionManager
mgr = SessionManager(tmp_path)
for chat_id in ("20260610_090000_lim001", "20260610_100000_lim002", "20260610_110000_lim003"):
s = mgr.get_or_create(f"tui:{chat_id}")
s.add_message("user", "x")
mgr.save(s)
monkeypatch.setattr("raven.session.resolve.build_manager", lambda cfg: mgr)
result = await session_list({"limit": 2})
items = result["sessions"]
assert len(items) == 2
assert items[0]["id"] == "tui:20260610_110000_lim003"
assert items[1]["id"] == "tui:20260610_100000_lim002"
async def test_session_list_ignores_non_positive_limit(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""A missing, zero, or invalid limit returns all sessions."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
from raven.session.manager import SessionManager
mgr = SessionManager(tmp_path)
for chat_id in ("20260610_090000_nl0001", "20260610_100000_nl0002"):
s = mgr.get_or_create(f"tui:{chat_id}")
s.add_message("user", "x")
mgr.save(s)
monkeypatch.setattr("raven.session.resolve.build_manager", lambda cfg: mgr)
for params in ({}, {"limit": 0}, {"limit": -1}, {"limit": "bogus"}):
result = await session_list(params)
assert len(result["sessions"]) == 2, f"params={params}"
async def test_session_list_empty_workspace(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""session.list returns an empty list when no tui sessions exist."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
from raven.session.manager import SessionManager
mgr = SessionManager(tmp_path)
monkeypatch.setattr("raven.session.resolve.build_manager", lambda cfg: mgr)
result = await session_list({})
assert result == {"sessions": []}
async def test_session_delete_removes_session(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""session.delete returns {deleted: session_id} and removes the file."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
from raven.session.manager import SessionManager
mgr = SessionManager(tmp_path)
s = mgr.get_or_create("tui:20260610_100000_rm01")
s.add_message("user", "bye")
mgr.save(s)
monkeypatch.setattr("raven.session.resolve.build_manager", lambda cfg: mgr)
result = await session_delete({"session_id": "tui:20260610_100000_rm01"})
assert result == {"deleted": "tui:20260610_100000_rm01", "still_on_disk": False}
path = tmp_path / "sessions" / "tui" / "20260610_100000_rm01.jsonl"
assert not path.exists()
async def test_session_delete_reaches_the_shared_stores_observers(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""[code seam-2] Every delete face resolves to the shared manager, so an
observer attached there hears an rpc session.delete too -- with the
removal outcome (paper: contracts/session_events.py)."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
from raven.session.manager import SessionManager
heard: list[tuple[str, bool]] = []
class _Probe:
def on_session_deleted(self, session_key: str, removed: bool) -> None:
heard.append((session_key, removed))
mgr = SessionManager(tmp_path)
s = mgr.get_or_create("tui:20260610_100000_ob01")
s.add_message("user", "bye")
mgr.save(s)
mgr.set_delete_observers((_Probe(),))
monkeypatch.setattr("raven.session.resolve.build_manager", lambda cfg: mgr)
result = await session_delete({"session_id": "tui:20260610_100000_ob01"})
assert result == {"deleted": "tui:20260610_100000_ob01", "still_on_disk": False}
assert heard == [("tui:20260610_100000_ob01", True)]
async def test_session_delete_unknown_key_returns_null(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""session.delete on a missing session returns {deleted: null} so the UI
can distinguish a typo from a real removal."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
from raven.session.manager import SessionManager
mgr = SessionManager(tmp_path)
monkeypatch.setattr("raven.session.resolve.build_manager", lambda cfg: mgr)
result = await session_delete({"session_id": "tui:ghost_session"})
assert result == {"deleted": None, "still_on_disk": False}
async def test_session_delete_missing_param_returns_null(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""session.delete with no session_id returns {deleted: null}."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
from raven.session.manager import SessionManager
mgr = SessionManager(tmp_path)
monkeypatch.setattr("raven.session.resolve.build_manager", lambda cfg: mgr)
result = await session_delete({})
assert result == {"deleted": None, "still_on_disk": False}
async def test_session_delete_says_a_refused_removal_left_the_file(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""A removal the filesystem refuses answers a null `deleted` like an absent
session does, and is told apart by `still_on_disk`: the file is still there,
so a client must keep listing the session rather than claim it is gone."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
from raven.session.manager import SessionManager
mgr = SessionManager(tmp_path)
s = mgr.get_or_create("tui:20260610_100000_ro01")
s.add_message("user", "stay")
mgr.save(s)
monkeypatch.setattr("raven.session.resolve.build_manager", lambda cfg: mgr)
path = mgr.session_path("tui:20260610_100000_ro01")
real_unlink = Path.unlink
def _refuse(self: Path, *args: object, **kwargs: object) -> None:
if self == path:
raise OSError(13, "Permission denied")
real_unlink(self, *args, **kwargs)
monkeypatch.setattr(Path, "unlink", _refuse)
result = await session_delete({"session_id": "tui:20260610_100000_ro01"})
assert result == {"deleted": None, "still_on_disk": True}
assert path.exists()
async def test_session_delete_says_an_absent_session_left_no_file(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""A session with no file answers `still_on_disk: False`, which is what lets
a client drop a row for a conversation whose first turn never saved one --
the case that otherwise cannot be cleared from the list at all."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
from raven.session.manager import SessionManager
mgr = SessionManager(tmp_path)
mgr.get_or_create("tui:20260610_100000_lz01")
monkeypatch.setattr("raven.session.resolve.build_manager", lambda cfg: mgr)
assert not mgr.exists("tui:20260610_100000_lz01")
result = await session_delete({"session_id": "tui:20260610_100000_lz01"})
assert result == {"deleted": None, "still_on_disk": False}
async def test_session_most_recent_returns_session_key(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""session.most_recent returns the full session_key for the newest tui session."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
from raven.session.manager import SessionManager
mgr = SessionManager(tmp_path)
s = mgr.get_or_create("tui:20260610_100000_recent1")
s.add_message("user", "hi")
mgr.save(s)
monkeypatch.setattr("raven.session.resolve.build_manager", lambda cfg: mgr)
result = await session_most_recent({})
assert result["session_id"] == "tui:20260610_100000_recent1"
async def test_session_most_recent_returns_null_when_no_sessions(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""session.most_recent returns session_id=null when no tui sessions exist."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
from raven.session.manager import SessionManager
mgr = SessionManager(tmp_path)
monkeypatch.setattr("raven.session.resolve.build_manager", lambda cfg: mgr)
result = await session_most_recent({})
assert result["session_id"] is None
async def test_session_title_on_existing_file_persists_immediately(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Setting a title on a session with an on-disk file saves it right away.
Durability contract: the title must survive a fresh-manager reload,
and the response carries pending=False.
"""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
from raven.session.manager import SessionManager
mgr = SessionManager(tmp_path)
s = mgr.get_or_create("tui:20260610_100000_title1")
s.add_message("user", "hello")
mgr.save(s)
monkeypatch.setattr("raven.session.resolve.build_manager", lambda cfg: mgr)
result = await session_title({"session_id": "tui:20260610_100000_title1", "title": "My Chat"})
assert result["title"] == "My Chat"
assert result["pending"] is False
reloaded = SessionManager(tmp_path).peek("tui:20260610_100000_title1")
assert reloaded is not None
assert reloaded.metadata.get("title") == "My Chat"
async def test_session_title_on_fresh_session_is_pending_and_writes_no_file(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Setting a title on a never-saved session keeps it in memory only.
pending=True signals deferred persistence; lazy mint is preserved
(no file appears until the session's first save).
"""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
from raven.session.manager import SessionManager
mgr = SessionManager(tmp_path)
monkeypatch.setattr("raven.session.resolve.build_manager", lambda cfg: mgr)
result = await session_title({"session_id": "tui:20260610_100000_lazy01", "title": "Early"})
assert result["title"] == "Early"
assert result["pending"] is True
sessions_dir = tmp_path / "sessions"
files = [p for p in sessions_dir.rglob("*") if p.is_file()] if sessions_dir.exists() else []
assert not files, f"lazy title must not write files; got: {files}"
assert mgr.get_or_create("tui:20260610_100000_lazy01").metadata.get("title") == "Early"
async def test_session_title_missing_session_id_returns_early(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""A falsy session_id short-circuits — no manager construction, no cache pollution."""
def _boom(cfg):
raise AssertionError("manager must not be built for a falsy session_id")
monkeypatch.setattr("raven.session.resolve.build_manager", _boom)
result = await session_title({})
assert result == {"title": None, "session_key": "", "pending": False}
async def test_session_title_get_returns_current_title(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""session.title with no title param returns the current title from metadata."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
from raven.session.manager import SessionManager
mgr = SessionManager(tmp_path)
s = mgr.get_or_create("tui:20260610_100000_title2")
s.metadata["title"] = "Existing Title"
s.add_message("user", "hello")
mgr.save(s)
monkeypatch.setattr("raven.session.resolve.build_manager", lambda cfg: mgr)
result = await session_title({"session_id": "tui:20260610_100000_title2"})
assert result["title"] == "Existing Title"
async def test_session_list_via_dispatcher(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""session.list is reachable through the Dispatcher."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
from raven.session.manager import SessionManager
mgr = SessionManager(tmp_path)
monkeypatch.setattr("raven.session.resolve.build_manager", lambda cfg: mgr)
d = Dispatcher()
register_session_methods(d)
resp = await d.dispatch({"jsonrpc": "2.0", "id": 1, "method": "session.list", "params": {}})
assert "error" not in resp, f"session.list dispatch failed: {resp}"
assert "sessions" in resp["result"]
async def test_session_delete_via_dispatcher(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""session.delete is reachable through the Dispatcher."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
from raven.session.manager import SessionManager
mgr = SessionManager(tmp_path)
s = mgr.get_or_create("tui:20260610_100000_disp1")
s.add_message("user", "x")
mgr.save(s)
monkeypatch.setattr("raven.session.resolve.build_manager", lambda cfg: mgr)
d = Dispatcher()
register_session_methods(d)
resp = await d.dispatch(
{"jsonrpc": "2.0", "id": 1, "method": "session.delete", "params": {"session_id": "tui:20260610_100000_disp1"}}
)
assert "error" not in resp, f"session.delete dispatch failed: {resp}"
assert resp["result"]["deleted"] == "tui:20260610_100000_disp1"
async def test_session_most_recent_via_dispatcher(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""session.most_recent is reachable through the Dispatcher."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
from raven.session.manager import SessionManager
mgr = SessionManager(tmp_path)
monkeypatch.setattr("raven.session.resolve.build_manager", lambda cfg: mgr)
d = Dispatcher()
register_session_methods(d)
resp = await d.dispatch({"jsonrpc": "2.0", "id": 1, "method": "session.most_recent", "params": {}})
assert "error" not in resp, f"session.most_recent dispatch failed: {resp}"
assert "session_id" in resp["result"]
# ---------------------------------------------------------------------------
# manager_for: shared-loop preference vs fresh-manager fall-through
# ---------------------------------------------------------------------------
def test_manager_for_reuses_shared_loop_manager(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""When agent_loop.sessions IS a SessionManager, manager_for returns that
exact instance — the shared loop manager is reused, not rebuilt."""
from types import SimpleNamespace
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
shared = SessionManager(tmp_path)
loop = SimpleNamespace(sessions=shared)
def _boom(_cfg):
raise AssertionError("must not build a fresh manager when the loop has one")
monkeypatch.setattr("raven.session.resolve.build_manager", _boom)
assert session_module.manager_for(loop, cfg) is shared
def test_manager_for_falls_through_when_no_loop(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""agent_loop=None falls through to a freshly built manager."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
sentinel = SessionManager(tmp_path)
monkeypatch.setattr("raven.session.resolve.build_manager", lambda _cfg: sentinel)
assert session_module.manager_for(None, cfg) is sentinel
def test_manager_for_falls_through_when_loop_sessions_not_a_manager(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""A loop whose .sessions is not a SessionManager falls through to a fresh one."""
from types import SimpleNamespace
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
sentinel = SessionManager(tmp_path)
monkeypatch.setattr("raven.session.resolve.build_manager", lambda _cfg: sentinel)
loop = SimpleNamespace(sessions=None)
assert session_module.manager_for(loop, cfg) is sentinel
def test_is_turn_active_reflects_active_turns(monkeypatch):
import asyncio
from raven.rpc.methods import turn as turn_module
assert turn_module.is_turn_active("tui:none") is False
async def _noop():
await asyncio.sleep(3600)
loop = asyncio.new_event_loop()
task = loop.create_task(_noop())
monkeypatch.setitem(turn_module._active_turns, "tui:busy", task)
try:
assert turn_module.is_turn_active("tui:busy") is True
finally:
task.cancel()
loop.run_until_complete(asyncio.gather(task, return_exceptions=True))
loop.close()
def _seed_manager(tmp_path, key="tui:s1"):
mgr = SessionManager(tmp_path)
s = mgr.get_or_create(key)
s.record({"role": "user", "content": "q1"})
s.record({"role": "assistant", "content": "a1"})
s.record({"role": "user", "content": "q2"})
s.record({"role": "assistant", "content": "a2"})
mgr.save(s)
return mgr, key
class _LoopWithManager:
def __init__(self, mgr):
self.sessions = mgr
async def test_session_clear_keeps_id_and_wipes(tmp_path):
from raven.rpc.methods.session import session_clear
mgr, key = _seed_manager(tmp_path)
result = await session_clear({"session_id": key}, agent_loop_factory=lambda: _LoopWithManager(mgr))
assert result == {"session_id": key, "cleared": True}
assert mgr.get_or_create(key).messages == []
assert mgr.peek(key).messages == []
async def test_session_clear_rejects_when_turn_active(tmp_path, monkeypatch):
from raven.rpc.methods.session import session_clear
mgr, key = _seed_manager(tmp_path)
monkeypatch.setattr(turn_module, "is_session_busy", lambda k: k == key)
with pytest.raises(TurnInProgressError):
await session_clear({"session_id": key}, agent_loop_factory=lambda: _LoopWithManager(mgr))
async def test_session_undo_drops_last_turn(tmp_path):
from raven.rpc.methods.session import session_undo
mgr, key = _seed_manager(tmp_path)
result = await session_undo({"session_id": key}, agent_loop_factory=lambda: _LoopWithManager(mgr))
assert result == {"removed": 2}
assert [m["content"] for m in mgr.get_or_create(key).messages] == ["q1", "a1"]
async def test_session_undo_nothing_to_undo_returns_zero(tmp_path):
from raven.rpc.methods.session import session_undo
mgr = SessionManager(tmp_path)
mgr.get_or_create("tui:empty")
result = await session_undo({"session_id": "tui:empty"}, agent_loop_factory=lambda: _LoopWithManager(mgr))
assert result == {"removed": 0}
async def test_session_undo_rejects_when_turn_active(tmp_path, monkeypatch):
from raven.rpc.methods.session import session_undo
mgr, key = _seed_manager(tmp_path)
monkeypatch.setattr(turn_module, "is_session_busy", lambda k: True)
with pytest.raises(TurnInProgressError):
await session_undo({"session_id": key}, agent_loop_factory=lambda: _LoopWithManager(mgr))
async def test_a_retry_after_an_undo_whose_save_failed_reaches_the_disk(tmp_path, monkeypatch):
from raven.rpc.methods.session import session_undo
def _boom(*_args, **_kwargs):
raise OSError("disk full")
mgr, key = _seed_manager(tmp_path)
with monkeypatch.context() as patched:
patched.setattr("raven.session.manager.atomic_replace", _boom)
result = await session_undo({"session_id": key}, agent_loop_factory=lambda: _LoopWithManager(mgr))
assert result == {"removed": 2}
session = mgr.get_or_create(key)
session.record({"role": "user", "content": "q2"})
session.record({"role": "assistant", "content": "a2, retried"})
mgr.save(session)
reloaded = SessionManager(tmp_path).get_or_create(key)
assert [m["content"] for m in reloaded.messages] == ["q1", "a1", "q2", "a2, retried"]
# ---------------------------------------------------------------------------
# session.branch — fork the session
# ---------------------------------------------------------------------------
async def test_session_branch_forks_and_returns_child_key(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
src_key = "tui:20260610_143052_aabbcc"
_write_session(tmp_path, src_key, [{"role": "user", "content": "hi"}])
result = await session_branch({"session_id": src_key})
child_key = result["session_id"]
assert child_key and child_key.startswith("tui:") and child_key != src_key
child = SessionManager(tmp_path).get_or_create(child_key)
assert child.metadata["parent_session_id"] == src_key
assert [m["content"] for m in child.messages] == ["hi"]
async def test_session_branch_returns_carried_message_count(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
src_key = "tui:20260610_143052_ddeeff"
_write_session(
tmp_path,
src_key,
[
{"role": "user", "content": "q1"},
{"role": "assistant", "content": "a1"},
{"role": "user", "content": "q2"},
],
)
result = await session_branch({"session_id": src_key})
assert result["message_count"] == 3
async def test_session_branch_uses_name_as_title(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
src_key = "tui:20260610_143052_bbccdd"
_write_session(tmp_path, src_key, [{"role": "user", "content": "hi"}])
result = await session_branch({"session_id": src_key, "name": "Experiment"})
assert result["title"] == "Experiment"
child = SessionManager(tmp_path).get_or_create(result["session_id"])
assert child.metadata["title"] == "Experiment"
async def test_session_branch_empty_name_defaults_fork_suffix(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
src_key = "tui:20260610_143052_ccddee"
mgr = SessionManager(tmp_path)
s = mgr.get_or_create(src_key)
s.add_message("user", "hi")
s.metadata["title"] = "Chat"
mgr.save(s)
result = await session_branch({"session_id": src_key, "name": ""})
assert result["title"] == "Chat (fork)"
async def test_session_branch_unknown_session_returns_no_id(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
result = await session_branch({"session_id": "tui:nonexistent_id"})
assert not result.get("session_id")
def test_session_branch_no_duplicate_registration() -> None:
"""session.branch is a real handler, removed from the stub table, so
registering both groups on one dispatcher does not raise."""
from raven.rpc.methods._stubs import (
HERMES_ONLY_STUB_METHODS,
register_stub_methods,
)
d = Dispatcher()
register_session_methods(d)
register_stub_methods(d)
assert "session.branch" in d.methods()
assert "session.branch" not in HERMES_ONLY_STUB_METHODS
# ── session.export ─────────────────────────────────────────────────────
async def test_session_export_writes_markdown_for_existing_session(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Exporting a known session writes a Markdown file and returns its path."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
session_key = "tui:20260622_120000_abcdef"
_write_session(
tmp_path,
session_key,
[
{"role": "user", "content": "hello"},
{"role": "assistant", "content": "hi there"},
],
)
result = await session_export({"session_id": session_key})
assert result["exported"] is True
assert result["path"]
written = Path(result["path"])
assert written.exists()
body = written.read_text(encoding="utf-8")
assert "hello" in body and "hi there" in body
assert written.parent == (tmp_path / "exports")
async def test_session_export_unknown_id_returns_not_found(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""An unresolvable id yields not_found and writes nothing."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
result = await session_export({"session_id": "nope000"})
assert result == {"exported": False, "path": None, "reason": "not_found"}
assert not (tmp_path / "exports").exists()
async def test_session_export_ambiguous_returns_candidates(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""A bare id on two channels is ambiguous; both keys surface, nothing written."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
cid = "20260622_120000_dddddd"
_write_session(tmp_path, f"cli:{cid}", [{"role": "user", "content": "a"}])
_write_session(tmp_path, f"tui:{cid}", [{"role": "user", "content": "b"}])
result = await session_export({"session_id": cid})
assert result["exported"] is False
assert result["reason"] == "ambiguous"
assert set(result["candidates"]) == {f"cli:{cid}", f"tui:{cid}"}
assert result["path"] is None
async def test_session_export_empty_session_id_returns_not_found(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""An absent/empty session_id has nothing to export."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
result = await session_export({})
assert result == {"exported": False, "path": None, "reason": "not_found"}
async def test_session_export_is_read_only_during_active_turn(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""Export does not reject on an active turn (read-only, unlike clear/undo)."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
monkeypatch.setattr(turn_module, "is_session_busy", lambda key: True)
session_key = "tui:20260622_120000_eeeeee"
_write_session(tmp_path, session_key, [{"role": "user", "content": "hi"}])
result = await session_export({"session_id": session_key})
assert result["exported"] is True
async def test_session_info_reports_this_sessions_model_not_the_default(monkeypatch) -> None:
"""The picker and the status bar sit next to each other; reporting the
global default here would show two models for one conversation.
"""
from unittest.mock import MagicMock
import raven.rpc.methods.session as session_mod
# MagicMock so the unrelated skills/tools enumeration in the bundle works;
# only ``session_model`` is under test.
loop = MagicMock()
loop.model = "vendor-a/model" # the usage baseline resolves a window from it
loop.session_model = lambda key: "vendor-a/model" if key == "tui:a" else "boot/model"
info = await session_mod._default_session_info(loop, session_mod.load_config(), "tui:a")
assert info["model"] == "vendor-a/model"
assert info["model_id"] == "vendor-a/model"
async def test_session_info_without_a_session_reports_the_default() -> None:
"""A session being created has no model of its own yet; the default is the
right answer, because that is what it will start on.
"""
from unittest.mock import MagicMock
import raven.rpc.methods.session as session_mod
config = session_mod.load_config()
loop = MagicMock()
loop.model = "vendor-a/model" # the usage baseline resolves a window from it
loop.session_model = lambda key: "vendor-a/model"
info = await session_mod._default_session_info(loop, config, None)
assert info["model"] == config.agents.defaults.model
async def test_session_resume_reports_the_model_the_loop_restored(tmp_path) -> None:
"""The handler no longer restores anything -- the loop reads the stored model
on first ask, which is what makes the choice survive on every surface and not
only on the one that calls this handler. What the handler still owes is
passing the session key down, so the bundle reports *this* session's model
instead of the configured default.
"""
from raven.rpc.methods.session import session_resume
from raven.session.manager import SessionManager
sessions = SessionManager(tmp_path)
record = sessions.get_or_create("tui:a")
record.metadata["model"] = "vendor-a/model"
sessions.save(record)
from unittest.mock import MagicMock
asked: list[str] = []
loop = MagicMock()
loop.sessions = sessions
# A real id, not the MagicMock default: the init bundle resolves a window
# from whatever the loop reports as its model.
loop.model = "vendor-a/model"
loop.session_model = lambda key: (asked.append(key), "vendor-a/model")[1]
result = await session_resume({"session_id": "tui:a"}, agent_loop_factory=lambda: loop)
assert asked == ["tui:a"], "the handler stopped passing the session key down"
assert result["info"]["model"] == "vendor-a/model"
async def test_session_branch_carries_the_parents_model_to_the_child(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""A fork continues its parent's conversation on its parent's model, and
keeps it across a restart.
Both halves matter and fail independently: dropping the binding hand-off
leaves the child running on the default in this process, and dropping the
record leaves it running on the default in the next one.
"""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
src_key = "tui:20260610_143052_bb0001"
_write_session(tmp_path, src_key, [{"role": "user", "content": "hi"}])
sessions = SessionManager(tmp_path)
parent = sessions.get_or_create(src_key)
parent.metadata["model"] = "vendor-a/model"
parent.metadata["provider"] = "anthropic"
sessions.save(parent)
parent_binding = SimpleNamespace(provider="prov-a", model="vendor-a/model")
class _Loop:
def __init__(self) -> None:
self.sessions = sessions
self.bindings: dict[str, object] = {src_key: parent_binding}
def has_session_binding(self, key: str) -> bool:
return key in self.bindings
def binding_for_session(self, key: str) -> object:
return self.bindings.get(key, SimpleNamespace(provider="boot", model="boot/model"))
def set_session_binding(self, key: str, binding: object) -> None:
self.bindings[key] = binding
loop = _Loop()
result = await session_branch({"session_id": src_key}, agent_loop_factory=lambda: loop)
child_key = result["session_id"]
assert loop.bindings[child_key] is parent_binding, "the fork must run on its parent's model now"
reloaded = SessionManager(tmp_path).get_or_create(child_key)
assert reloaded.metadata["model"] == "vendor-a/model", "and after a restart"
assert reloaded.metadata["provider"] == "anthropic"
async def test_session_delete_releases_the_sessions_binding(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""A deleted session must not leave its override -- and the live provider
behind it -- held for the life of the process.
"""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
key = "tui:20260610_100000_bb0002"
_write_session(tmp_path, key, [{"role": "user", "content": "hi"}])
cleared: list[str] = []
class _Loop:
sessions = None
def clear_session_binding(self, session_key: str) -> None:
cleared.append(session_key)
await session_delete({"session_id": key}, agent_loop_factory=lambda: _Loop())
assert cleared == [key]
async def test_session_resume_estimates_only_what_the_next_call_sends(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""The context meter counts the unconsolidated tail, not the whole file.
The runtime consolidates on every user-inbound turn and never removes the
archived messages from the transcript, so estimating over everything stored
reported the size of a context that had already been archived -- a session a
quarter full resumed showing a meter pegged at 100%.
"""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
session_key = "tui:20260610_143052_consol"
mgr = SessionManager(tmp_path)
session = mgr.get_or_create(session_key)
for i in range(4):
session.add_message("user", f"message number {i} " * 40)
session.last_consolidated = 3
mgr.save(session)
result = await session_resume({"session_id": session_key})
used = result["info"]["usage"]["context_used"]
# The floor is what the tail alone costs; the ceiling keeps this from
# passing on a version that counts everything -- four near-identical
# messages make the whole file roughly four times the tail.
tail_only = estimate_prompt_tokens(session.get_history())
assert used == tail_only, "the estimate must be over what get_history() returns"
whole_file = estimate_prompt_tokens(
[{"role": m["role"], "content": m.get("content", "")} for m in session.messages]
)
assert used < whole_file / 2, f"{used} looks like the whole transcript ({whole_file}), not the tail"
# The transcript itself is unchanged: resume still hands back everything on
# disk, because that is what the caller draws.
assert len(result["messages"]) == 4
async def test_session_resume_estimates_past_the_five_hundred_message_default(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""The tail is bounded in tokens, not in messages, so it can run past 500.
`get_history()` defaults to the last 500; the two places that build or
measure the real prompt both pass `max_messages=0`. Taking the default here
under-reported a long unconsolidated session -- and it does not converge,
because the reported number pins at whatever the newest 500 cost while the
real prompt keeps growing toward the window.
"""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
session_key = "tui:20260610_143052_long01"
mgr = SessionManager(tmp_path)
session = mgr.get_or_create(session_key)
for i in range(600):
session.add_message("user", f"message number {i} " * 10)
mgr.save(session)
result = await session_resume({"session_id": session_key})
used = result["info"]["usage"]["context_used"]
whole_tail = estimate_prompt_tokens(session.get_history(max_messages=0))
newest_500 = estimate_prompt_tokens(session.get_history())
assert newest_500 < whole_tail, "fixture must exceed the default cap for this to mean anything"
assert used == whole_tail, f"{used} is the newest 500 ({newest_500}), not the whole tail ({whole_tail})"
async def test_session_resume_carries_a_stored_tool_diff(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""The stored diff is the one record with real line numbers -- the wire
must carry it or a reloaded page can never renumber a change."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
session_key = "tui:20260610_143052_445566"
mgr = SessionManager(tmp_path)
session = mgr.get_or_create(session_key)
session.add_message("user", "edit it")
session.add_message("tool", "Successfully edited", tool_call_id="c1", name="edit_file", diff="@@ -1 +1 @@\n-a\n+b")
mgr.save(session)
msgs = (await session_resume({"session_id": session_key}))["messages"]
assert msgs[1]["diff"].startswith("@@ -1 +1 @@")
async def test_session_resume_carries_the_files_a_stored_call_removed(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Nothing else in a stored transcript records a deletion: the command's
arguments are a string, and the file it names is gone by the time a reloaded
page looks. The line count travels, not the body -- what the page needs is
that the file went and how big the hole is."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
session_key = "tui:20260610_143052_667788"
mgr = SessionManager(tmp_path)
session = mgr.get_or_create(session_key)
session.add_message("user", "clean it up")
session.add_message(
"tool",
"swept",
tool_call_id="c1",
name="exec",
file_removed=[{"path": "/w/gone.txt", "del": 2}],
)
mgr.save(session)
msgs = (await session_resume({"session_id": session_key}))["messages"]
assert msgs[1]["file_removed"] == [{"path": "/w/gone.txt", "del": 2}]
async def test_session_resume_carries_the_files_a_stored_command_wrote(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""A command's own result says only that it ran, so a reloaded page has no
other record that it wrote anything. The stored shape is the live one here:
a size and a line count are already what the page draws."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
session_key = "tui:20260610_143052_778899"
mgr = SessionManager(tmp_path)
session = mgr.get_or_create(session_key)
session.add_message("user", "build it")
session.add_message(
"tool",
"ran",
tool_call_id="c1",
name="exec",
file_written=[{"path": "/w/made.txt", "created": True, "size": 8, "lines": 2}],
)
mgr.save(session)
msgs = (await session_resume({"session_id": session_key}))["messages"]
assert msgs[1]["file_written"] == [{"path": "/w/made.txt", "created": True, "size": 8, "lines": 2}]
async def test_session_resume_carries_the_broken_turn_marker(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""``turn_ended`` says why a transcript stops where it does; without it a
reloaded page renders the marker's model-facing text as an answer."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
session_key = "tui:20260610_143052_778899"
mgr = SessionManager(tmp_path)
session = mgr.get_or_create(session_key)
session.add_message("user", "do it")
session.add_message("assistant", "(turn cancelled by the user)", turn_ended={"status": "cancelled"})
mgr.save(session)
msgs = (await session_resume({"session_id": session_key}))["messages"]
assert msgs[1]["turn_ended"] == {"status": "cancelled"}
async def test_session_resume_carries_the_runtime_notice_marker(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Runtime prose is stored as an assistant message because the MODEL has to
read it on the next turn. A reader must not: without ``notice`` on the wire
the reload attributes it to the model, contradicting the live view, which
drew it as its own row."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
session_key = "tui:20260610_143052_991122"
mgr = SessionManager(tmp_path)
session = mgr.get_or_create(session_key)
session.add_message("user", "clean up")
session.add_message(
"assistant",
"The operation was not completed",
notice={"kind": "action_blocked", "detail": "Error: Command blocked by safety guard"},
)
mgr.save(session)
msgs = (await session_resume({"session_id": session_key}))["messages"]
assert msgs[1]["notice"]["kind"] == "action_blocked"
assert "safety guard" in msgs[1]["notice"]["detail"]
def test_resume_marks_missing_deliveries_without_dropping_metadata(tmp_path: Path) -> None:
present = tmp_path / "report.pdf"
present.write_bytes(b"pdf")
messages = [
{
"role": "tool",
"content": "Delivered files",
"metadata": {
"raven_delivery": {
"message": "Final files",
"files": [
{"name": "report.pdf", "path": str(present), "size": 3},
{"name": "gone.csv", "path": str(tmp_path / "gone.csv"), "size": 8},
],
},
"other": "kept",
},
}
]
wire = session_module._map_to_wire(messages, "tui:s1")
metadata = wire[0]["metadata"]
assert metadata["other"] == "kept"
assert [item["missing"] for item in metadata["raven_delivery"]["files"]] == [False, True]
async def test_session_list_sorted_by_latest_conversation_message(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""session.list returns sessions ordered by latest readable message."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
from raven.session.manager import SessionManager
mgr = SessionManager(tmp_path)
older = mgr.get_or_create("tui:20260610_090000_old111")
older.add_message("user", "old")
mgr.save(older)
newer = mgr.get_or_create("tui:20260610_120000_new222")
newer.add_message("user", "new")
mgr.save(newer)
monkeypatch.setattr("raven.session.resolve.build_manager", lambda cfg: mgr)
result = await session_list({})
items = result["sessions"]
assert items[0]["id"] == "tui:20260610_120000_new222"
assert items[1]["id"] == "tui:20260610_090000_old111"
async def test_session_list_contract_accepts_real_multichannel_rows(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""The declared contract includes the fields the browser actually sends
and consumes, including a populated nested row."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
mgr = SessionManager(tmp_path)
session = mgr.get_or_create("cron:contract")
session.add_message("user", "run the digest")
session.add_message("assistant", "digest complete")
mgr.save(session)
monkeypatch.setattr("raven.session.resolve.build_manager", lambda cfg: mgr)
params_model, result_model = METHOD_MODELS["session.list"]
params_model.model_validate({"channels": ["tui", "cron"], "limit": 10})
result_model.model_validate(await session_list({"channels": ["tui", "cron"]}))
async def test_session_list_scans_once_for_multiple_channels(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
mgr = SessionManager(tmp_path)
for key in ("tui:one", "cron:two", "cli:three"):
session = mgr.get_or_create(key)
session.add_message("user", key)
mgr.save(session)
monkeypatch.setattr("raven.session.resolve.build_manager", lambda cfg: mgr)
scans = 0
scan_file = mgr._scan_file
def counted(path: Path):
nonlocal scans
scans += 1
return scan_file(path)
monkeypatch.setattr(mgr, "_scan_file", counted)
result = await session_list({"channels": ["tui", "cron"]})
assert {row["source"] for row in result["sessions"]} == {"tui", "cron"}
assert scans == 3, "every stored file is scanned once, not once per requested channel"
async def test_session_delete_rejects_a_running_session(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
key = "tui:running"
mgr = SessionManager(tmp_path)
session = mgr.get_or_create(key)
session.add_message("user", "keep working")
mgr.save(session)
monkeypatch.setattr("raven.session.resolve.build_manager", lambda cfg: mgr)
monkeypatch.setattr(turn_module, "is_session_busy", lambda candidate: candidate == key)
with pytest.raises(TurnInProgressError):
await session_delete({"session_id": key})
assert mgr.exists(key), "a running writer must keep its transcript"
async def test_session_most_recent_skips_archived_sessions(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""session.most_recent does not auto-resume a session hidden by archiving."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
mgr = SessionManager(tmp_path)
archived = mgr.get_or_create("tui:20260610_110000_archived")
archived.add_message("user", "hide this")
archived.metadata["archived"] = True
mgr.save(archived)
monkeypatch.setattr("raven.session.resolve.build_manager", lambda cfg: mgr)
result = await session_most_recent({})
assert result["session_id"] is None
async def test_session_pin_persists_and_shows_up_in_the_list(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""Pinning survives a reload and rides the session.list rows.
The regression this pins: the web UI used to keep the flag in page memory
only, so every refresh silently dropped the pinned group.
"""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
from raven.session.manager import SessionManager
mgr = SessionManager(tmp_path)
s = mgr.get_or_create("tui:20260610_100000_pin001")
s.add_message("user", "hello")
mgr.save(s)
monkeypatch.setattr("raven.session.resolve.build_manager", lambda cfg: mgr)
result = await session_pin({"session_id": "tui:20260610_100000_pin001", "pinned": True})
assert result == {"pinned": True, "session_key": "tui:20260610_100000_pin001", "pending": False}
reloaded = SessionManager(tmp_path).peek("tui:20260610_100000_pin001")
assert reloaded is not None and reloaded.metadata.get("pinned") is True
listed = await session_list({})
row = next(r for r in listed["sessions"] if r["id"] == "tui:20260610_100000_pin001")
assert row["pinned"] is True
result = await session_pin({"session_id": "tui:20260610_100000_pin001", "pinned": False})
assert result["pinned"] is False
# Unpinned is written, not left unsaid: one appended key cannot remove a
# key, and every reader of this flag asks whether it is true.
reloaded = SessionManager(tmp_path).peek("tui:20260610_100000_pin001")
assert reloaded is not None and reloaded.metadata.get("pinned") is False
listed = await session_list({})
row = next(r for r in listed["sessions"] if r["id"] == "tui:20260610_100000_pin001")
assert row["pinned"] is False
async def test_a_refused_pin_or_rename_is_reported_rather_than_swallowed(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""A filesystem that refuses the append answers pending, not success.
Same contract the archive verb answers under: the flag holds in memory for
as long as this process lives, and the reply says it did not reach the
disk, so a client does not draw a state the next load will contradict.
"""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
mgr = SessionManager(tmp_path)
monkeypatch.setattr("raven.session.resolve.build_manager", lambda cfg: mgr)
for key in ("tui:20260610_100000_pinrefused", "tui:20260610_100000_titlerefused"):
session = mgr.get_or_create(key)
session.add_message("user", "hello")
mgr.save(session)
def refuse(*_args: object, **_kwargs: object) -> bool:
raise OSError("read-only file system")
monkeypatch.setattr(mgr, "append_metadata_patch", refuse)
pinned = await session_pin({"session_id": "tui:20260610_100000_pinrefused", "pinned": True})
assert pinned == {"pinned": True, "session_key": "tui:20260610_100000_pinrefused", "pending": True}
assert mgr.get_or_create("tui:20260610_100000_pinrefused").metadata["pinned"] is True
named = await session_title({"session_id": "tui:20260610_100000_titlerefused", "title": "by hand"})
assert named == {"title": "by hand", "session_key": "tui:20260610_100000_titlerefused", "pending": True}
assert mgr.get_or_create("tui:20260610_100000_titlerefused").metadata["title"] == "by hand"
async def test_pinning_and_renaming_keep_a_key_another_writer_added(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Each verb speaks for its own key and for nothing else.
Both used to persist by saving the whole session, which rewrites the
metadata record from this manager's copy of it -- so a flag written to the
file after that copy was loaded was dropped by an unrelated pin or rename.
A page and a terminal over one home are two managers over one file, which
is how archiving a conversation from one of them and renaming it from the
other put it back in everybody's list.
"""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
mgr = SessionManager(tmp_path)
monkeypatch.setattr("raven.session.resolve.build_manager", lambda cfg: mgr)
for key, verb in (
("tui:20260610_100000_pinkeep", "pin"),
("tui:20260610_100000_titlekeep", "title"),
):
session = mgr.get_or_create(key)
session.add_message("user", "two writers hold this")
mgr.save(session)
# Somebody else archives it straight on the file; this manager's copy
# of the metadata knows nothing about it.
SessionManager(tmp_path).append_metadata_patch(key, {"archived": True})
assert mgr.get_or_create(key).metadata.get("archived") is None
if verb == "pin":
assert (await session_pin({"session_id": key, "pinned": True}))["pending"] is False
else:
assert (await session_title({"session_id": key, "title": "renamed"}))["pending"] is False
reloaded = SessionManager(tmp_path).peek(key)
assert reloaded is not None
assert reloaded.metadata.get("archived") is True, verb
async def test_session_archive_persists_and_filters_the_list(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
mgr = SessionManager(tmp_path)
session_key = "tui:20260610_100000_archive1"
session = mgr.get_or_create(session_key)
session.add_message("user", "hide this session")
mgr.save(session)
monkeypatch.setattr("raven.session.resolve.build_manager", lambda cfg: mgr)
result = await session_archive({"session_id": session_key, "archived": True})
assert result == {"archived": True, "session_key": session_key, "pending": False}
assert await session_list({}) == {"sessions": []}
reloaded = SessionManager(tmp_path).peek(session_key)
assert reloaded is not None and reloaded.metadata.get("archived") is True
result = await session_archive({"session_id": session_key, "archived": False})
assert result == {"archived": False, "session_key": session_key, "pending": False}
assert [row["id"] for row in (await session_list({}))["sessions"]] == [session_key]
async def test_archiving_keeps_a_key_another_writer_added(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""Archiving speaks for the archived flag and for nothing else.
It used to persist by saving the whole session, which rewrites the metadata
record from this manager's copy of it -- so every key written to the file
after that copy was loaded was dropped by an unrelated archive. A page and
a terminal over one home are two managers over one file, and that is how a
conversation came back after being archived: not because archiving failed,
but because somebody else's save spoke for a flag it had never seen.
"""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
session_key = "tui:20260610_100000_foreignkey"
mgr = SessionManager(tmp_path)
session = mgr.get_or_create(session_key)
session.add_message("user", "two writers hold this")
mgr.save(session)
monkeypatch.setattr("raven.session.resolve.build_manager", lambda cfg: mgr)
# Somebody else writes a flag straight to the file; this manager's copy of
# the metadata knows nothing about it.
SessionManager(tmp_path).append_metadata_patch(session_key, {"pinned": True})
assert mgr.get_or_create(session_key).metadata.get("pinned") is None
result = await session_archive({"session_id": session_key, "archived": True})
assert result == {"archived": True, "session_key": session_key, "pending": False}
reloaded = SessionManager(tmp_path).peek(session_key)
assert reloaded is not None
assert reloaded.metadata.get("archived") is True
assert reloaded.metadata.get("pinned") is True
async def test_archiving_a_session_with_no_transcript_says_so(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""Nothing reached the disk, and the reply says it rather than implying it.
A conversation minted but never saved has no file to fold the flag into.
The call answers ``pending``, and a client that reads that as a success
takes the row off its list and finds it back on the next load.
"""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
mgr = SessionManager(tmp_path)
monkeypatch.setattr("raven.session.resolve.build_manager", lambda cfg: mgr)
session_key = "tui:20260610_100000_nofile"
result = await session_archive({"session_id": session_key, "archived": True})
assert result == {"archived": True, "session_key": session_key, "pending": True}
assert not mgr.exists(session_key)
assert mgr.get_or_create(session_key).metadata.get("archived") is True
async def test_a_refused_write_is_reported_rather_than_swallowed(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""A filesystem that refuses the append answers pending, not success.
The flag is still applied in memory so the session behaves as asked for as
long as this process lives, but the reply says it did not reach the disk --
a client that takes the row off its list on a plain success would find it
back on the next load.
"""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
session_key = "tui:20260610_100000_refused"
mgr = SessionManager(tmp_path)
session = mgr.get_or_create(session_key)
session.add_message("user", "hello")
mgr.save(session)
monkeypatch.setattr("raven.session.resolve.build_manager", lambda cfg: mgr)
def refuse(*_args: object, **_kwargs: object) -> bool:
raise OSError("read-only file system")
monkeypatch.setattr(mgr, "append_metadata_patch", refuse)
result = await session_archive({"session_id": session_key, "archived": True})
assert result == {"archived": True, "session_key": session_key, "pending": True}
assert mgr.get_or_create(session_key).metadata["archived"] is True
assert SessionManager(tmp_path).peek(session_key).metadata.get("archived") is None
async def test_session_archive_via_dispatcher(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
mgr = SessionManager(tmp_path)
session_key = "tui:20260610_100000_archive2"
session = mgr.get_or_create(session_key)
session.add_message("user", "archive through dispatcher")
mgr.save(session)
monkeypatch.setattr("raven.session.resolve.build_manager", lambda cfg: mgr)
dispatcher = Dispatcher()
register_session_methods(dispatcher)
response = await dispatcher.dispatch(
{
"jsonrpc": "2.0",
"id": 1,
"method": "session.archive",
"params": {"session_id": session_key, "archived": True},
}
)
assert "error" not in response
assert response["result"] == {"archived": True, "session_key": session_key, "pending": False}
async def test_a_direct_turn_still_counts_as_the_session_being_busy(monkeypatch):
"""A direct chat runs on a lane of its own, which is what makes it
concurrent -- but clear / undo / compress / model-switch mean "is anything
running in this session", and a sub-agent answering is."""
from raven.rpc.methods import turn as turn_module
from raven.spine import direct_lane
turn_module._active_turns.clear()
try:
lane = direct_lane("tui:busy", "Coder", "h1")
turn_module._active_turns[lane] = object()
# Not the lane the guard is asked about ...
assert turn_module.is_turn_active("tui:busy") is False
# ... but the session it belongs to is busy.
assert turn_module.is_session_busy("tui:busy") is True
assert turn_module.is_session_busy("tui:other") is False
finally:
turn_module._active_turns.clear()
async def test_session_compress_requires_a_session_id() -> None:
"""No session_id is a caller error, not a silent no-op."""
from raven.rpc.errors import ConfigValidationError
with pytest.raises(ConfigValidationError) as exc:
await session_module.session_compress({})
assert exc.value.data == {"field": "session_id"}
async def test_session_compress_refuses_while_a_turn_is_running(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Compacting under a live turn would archive messages the turn is still
reading, so the handler refuses rather than racing it."""
session_key = "tui:compress_busy"
monkeypatch.setattr(turn_module, "is_session_busy", lambda key: key == session_key)
with pytest.raises(TurnInProgressError):
await session_module.session_compress({"session_id": session_key})
async def test_session_compress_is_a_noop_without_a_consolidator(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""A runtime whose context engine owns compaction reports noop with a note,
and leaves the message count untouched."""
from types import SimpleNamespace
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
session_key = "tui:compress_noop"
_write_session(tmp_path, session_key, [{"role": "user", "content": "hi"}])
loop = SimpleNamespace(memory_consolidator=None, sessions=SessionManager(tmp_path))
result = await session_module.session_compress({"session_id": session_key}, agent_loop_factory=lambda: loop)
assert result["removed"] == 0
assert result["before_messages"] == result["after_messages"] == 1
assert result["summary"]["noop"] is True
assert result["summary"]["note"] == "no memory consolidator in this runtime"
async def test_session_compress_reports_what_the_consolidator_archived(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""With a consolidator the handler forces a token pass and reports the
before/after counts the clients print verbatim."""
from types import SimpleNamespace
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
session_key = "tui:compress_real"
_write_session(
tmp_path,
session_key,
[{"role": "user", "content": f"m{i}"} for i in range(5)],
)
forced: list[bool] = []
class _Consolidator:
async def maybe_consolidate_by_tokens(self, session, force=False):
forced.append(force)
# Reporting an archive and leaving the session untouched is a state
# the real consolidator cannot produce: `archived` is its report,
# `last_consolidated` is the fact, and the handler reads the fact.
session.last_consolidated = 3
return {"before_tokens": 900, "after_tokens": 300, "compacted": 3}
# A fake is only evidence if it answers to the same contract as the real
# thing. This one was written against a signature the shipped consolidator
# did not have (`force` was added later, on a branch), so the test passed
# while the handler raised TypeError against the module it actually calls.
# Binding the real signature is what makes the fake accountable.
_real = inspect.signature(MemoryConsolidator.maybe_consolidate_by_tokens)
_real.bind(object(), object(), force=True)
assert inspect.signature(_Consolidator.maybe_consolidate_by_tokens).parameters.keys() == _real.parameters.keys(), (
"the fake consolidator has drifted from MemoryConsolidator"
)
loop = SimpleNamespace(memory_consolidator=_Consolidator(), sessions=SessionManager(tmp_path))
result = await session_module.session_compress({"session_id": session_key}, agent_loop_factory=lambda: loop)
assert forced == [True], "the point of session.compress is forcing the pass early"
assert result["removed"] == 3
assert result["before_messages"] == 5
assert result["after_messages"] == 2
assert result["summary"]["headline"] == "compacted 3 messages"
assert result["summary"]["noop"] is False
assert result["summary"]["token_line"] == "900 -> 300 tokens"
async def test_session_list_reaches_the_channels_it_was_asked_for(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""The handler used to hardcode channel="tui" and ignore ``channels``, so
scheduled runs -- which live under ``cron:`` -- were unreachable even though
they were on disk. Nothing had ever passed the parameter."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
mgr = SessionManager(tmp_path)
for key in ("tui:20260610_100000_aaa111", "cron:20260610_110000_bbb222"):
s = mgr.get_or_create(key)
s.add_message("user", "hello")
mgr.save(s)
monkeypatch.setattr("raven.session.resolve.build_manager", lambda cfg: mgr)
both = await session_list({"channels": ["tui", "cron"]})
sources = {item["source"] for item in both["sessions"]}
assert sources == {"tui", "cron"}, "session.list must return every channel it was given"
# Asking for one non-default channel alone: the bug only shows when the
# caller wants something other than the default, and "both" would still
# pass against a handler that ignored the parameter and returned everything.
cron_only = await session_list({"channels": ["cron"]})
assert [item["source"] for item in cron_only["sessions"]] == ["cron"]
default = await session_list({})
assert {item["source"] for item in default["sessions"]} == {"tui"}, (
"omitting channels must keep the old tui-only behaviour for existing callers"
)
async def test_session_list_orders_by_latest_conversational_activity(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""An assistant reply is new Session activity and moves its row."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
mgr = SessionManager(tmp_path)
older_user = mgr.get_or_create("tui:20260610_100000_older1")
older_user.add_message("user", "spoke first", timestamp="2026-06-10T10:00:00")
mgr.save(older_user)
recent_user = mgr.get_or_create("tui:20260610_110000_recent")
recent_user.add_message("user", "spoke second", timestamp="2026-06-10T11:00:00")
mgr.save(recent_user)
older_user.add_message("assistant", "finished later", timestamp="2026-06-10T12:00:00")
mgr.save(older_user)
monkeypatch.setattr("raven.session.resolve.build_manager", lambda cfg: mgr)
result = await session_list({})
assert [item["id"] for item in result["sessions"]][0] == "tui:20260610_100000_older1"
async def test_session_list_counts_runtime_origin_user_rows_as_activity(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Delegated and runtime-origin content visibly changes its Session."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
mgr = SessionManager(tmp_path)
injected = mgr.get_or_create("tui:injected")
injected.add_message("user", "human at ten", timestamp="2026-06-10T10:00:00")
mgr.save(injected)
human = mgr.get_or_create("tui:human")
human.add_message("user", "human at eleven", timestamp="2026-06-10T11:00:00")
mgr.save(human)
injected.add_message(
"user",
"delegated result at noon",
timestamp="2026-06-10T12:00:00",
origin="runtime",
delegated={"kind": "subagent"},
)
mgr.save(injected)
monkeypatch.setattr("raven.session.resolve.build_manager", lambda cfg: mgr)
rows = (await session_list({}))["sessions"]
assert rows[0]["id"] == "tui:injected"
assert rows[0]["preview"] == "human at ten"
assert rows[0]["last_message_preview"] == "delegated result at noon"
async def test_session_compress_persists_what_it_archived(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""Compaction that is not saved is a report, not a compaction: the next read
of the session would find every message still there."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
session_key = "tui:compress_persists"
_write_session(tmp_path, session_key, [{"role": "user", "content": f"m{i}"} for i in range(5)])
class _Consolidator:
async def maybe_consolidate_by_tokens(self, session, *, force=False):
session.last_consolidated = 3
return {"before_tokens": 900, "after_tokens": 300, "compacted": 3}
mgr = SessionManager(tmp_path)
loop = SimpleNamespace(memory_consolidator=_Consolidator(), sessions=mgr)
await session_module.session_compress({"session_id": session_key}, agent_loop_factory=lambda: loop)
reread = SessionManager(tmp_path).get_or_create(session_key)
assert reread.last_consolidated == 3, "the archived boundary must survive a reload"
async def test_session_list_previews_the_first_thing_the_user_said(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""A scheduled run has no title of its own, so the preview is the only thing
distinguishing one row from another."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
mgr = SessionManager(tmp_path)
s = mgr.get_or_create("tui:20260610_100000_ccc333")
# A non-user message first, deliberately: a scan that takes "the first
# message" rather than "the first *user* message" previews the machine
# talking to itself, and a session opened by a scheduled task or a system
# preamble is exactly where that shows.
s.add_message("assistant", "[Scheduled Task] Timer fired")
s.add_message("user", "summarise the quarterly report")
s.add_message("assistant", "sure")
mgr.save(s)
monkeypatch.setattr("raven.session.resolve.build_manager", lambda cfg: mgr)
result = await session_list({})
assert result["sessions"][0]["preview"] == "summarise the quarterly report"
async def test_session_list_keeps_identity_separate_from_latest_preview(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
mgr = SessionManager(tmp_path)
session = mgr.get_or_create("tui:preview_fields")
session.add_message("user", "original question")
session.add_message("assistant", "first answer")
session.add_message("tool", "internal tool output")
session.add_message("assistant", "latest answer")
mgr.save(session)
monkeypatch.setattr("raven.session.resolve.build_manager", lambda cfg: mgr)
[row] = (await session_list({}))["sessions"]
assert row["preview"] == "original question"
assert row["last_message_preview"] == "latest answer"
async def test_session_compress_hands_back_what_the_caller_must_redraw(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""After a compaction the caller is displaying messages that no longer
exist, so the reply carries the survivors, refreshed info, and usage."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
session_key = "tui:compress_redraw"
_write_session(tmp_path, session_key, [{"role": "user", "content": f"m{i}"} for i in range(5)])
class _Consolidator:
# Advances the boundary and removes nothing, which is what
# MemoryConsolidator does: it annotates through the store and moves
# `last_consolidated`. A fake that deleted from the list agreed with a
# handler counting `before - archived` and hid that the reply was
# handing back every message it had just called archived.
async def maybe_consolidate_by_tokens(self, session, force=False):
session.last_consolidated = 3
return {"before_tokens": 900, "after_tokens": 300, "compacted": 3}
loop = SimpleNamespace(
memory_consolidator=_Consolidator(),
sessions=SessionManager(tmp_path),
context=SimpleNamespace(skills=SimpleNamespace(list_skills=lambda **_kw: [])),
tools=SimpleNamespace(tool_names=[], get=lambda _n: None),
model="test/model",
)
result = await session_module.session_compress({"session_id": session_key}, agent_loop_factory=lambda: loop)
assert len(result["messages"]) == 2, "the survivors, so the caller can redraw"
# The count and the payload have to come from one slice, or a caller is told
# a number that contradicts the list printed beside it.
assert result["after_messages"] == len(result["messages"])
assert [m["text"] for m in result["messages"]] == ["m3", "m4"]
assert result["info"]["model"]
assert isinstance(result["usage"], dict)
async def test_session_compress_reports_the_same_session_as_resume(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""The redraw bundle answers for THIS session, not for no session at all.
The TUI adopts it wholesale, and ``info.cwd`` drives its status-bar
directory and the git branch beside it -- so a bundle built without the key
read as ``/compress`` moving the session to the launch directory, with the
configured default model in place of the one it runs on, until the next
resume put both back.
"""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
session_key = "tui:compress_same_session"
_write_session(tmp_path, session_key, [{"role": "user", "content": f"m{i}"} for i in range(5)])
class _Consolidator:
async def maybe_consolidate_by_tokens(self, session, force=False):
session.last_consolidated = 3
return {"before_tokens": 900, "after_tokens": 300, "compacted": 3}
resolver = WorkdirResolver(
WorkdirPolicy.PER_CHANNEL,
agent_home=tmp_path / "home",
sessions=SessionManager(tmp_path),
)
loop = SimpleNamespace(
memory_consolidator=_Consolidator(),
sessions=SessionManager(tmp_path),
context=SimpleNamespace(skills=SimpleNamespace(list_skills=lambda **_kw: [])),
tools=SimpleNamespace(tool_names=[], get=lambda _n: None),
model="configured/default",
session_model=lambda key: f"chosen/for-{key}",
peek_session_workdir=lambda key: resolver.resolve(key, create=False),
)
result = await session_module.session_compress({"session_id": session_key}, agent_loop_factory=lambda: loop)
resumed = await session_resume({"session_id": session_key}, agent_loop_factory=lambda: loop)
assert result["info"]["cwd"] == resumed["info"]["cwd"]
assert result["info"]["cwd"] == str(default_channel_root(tmp_path / "home") / "tui")
assert result["info"]["cwd"] != os.getcwd()
assert result["info"]["model"] == f"chosen/for-{session_key}"
async def test_session_create_persists_the_workdir_override(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""A ``workdir`` param must land in the minted session's metadata — the
override ``WorkdirResolver`` reads — and be reported back as ``info.cwd``,
so a client attached to a shared gateway works in its own launch
directory rather than the gateway's."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path / "home")
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
mgr = SessionManager(tmp_path / "home")
monkeypatch.setattr("raven.session.resolve.build_manager", lambda _cfg: mgr)
project = tmp_path / "proj"
project.mkdir()
result = await session_create({"workdir": str(project)})
resolved = str(project.resolve())
assert result["info"]["cwd"] == resolved
assert mgr.get_or_create(result["session_id"]).metadata["workdir"] == resolved
async def test_session_create_rejects_a_workdir_inside_the_agent_home(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
from raven.rpc.errors import ConfigValidationError
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path / "home")
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
with pytest.raises(ConfigValidationError):
await session_create({"workdir": str(tmp_path / "home" / "skills")})
async def test_session_create_without_workdir_stays_lazy_and_unpinned(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""The embedded path sends no workdir; nothing may change for it — no
manager built, no metadata written (see the writes-no-file test above)."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path / "home")
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
def _must_not_build(_cfg):
raise AssertionError("no workdir given, so no manager may be built")
monkeypatch.setattr("raven.session.resolve.build_manager", _must_not_build)
result = await session_create({"cols": 80})
assert _SESSION_ID_RE.match(result["session_id"])
def test_a_dag_tool_row_names_the_run_it_started() -> None:
"""The run id is in the result text the tool authored; the client should not
have to parse prose to find it."""
rows = _map_to_wire(
[
{
"role": "tool",
"name": "run_subagent_dag",
"tool_call_id": "call-1",
"content": (
"DAG run 20260823T063516789587Z-1e97d546 finished: 2 completed, 0 failed "
"(of 2).\nRun dir: /root/.raven/x/subagents/mas_dag/20260823T063516789587Z-1e97d546"
),
}
],
"sess",
)
assert rows[0]["dag_run_id"] == "20260823T063516789587Z-1e97d546"
def test_a_non_dag_tool_row_names_no_run() -> None:
rows = _map_to_wire(
[{"role": "tool", "name": "read_file", "tool_call_id": "c", "content": "contents"}],
"sess",
)
assert "dag_run_id" not in rows[0]
def test_a_dag_row_whose_text_names_no_run_is_left_alone() -> None:
"""A graph rejected by validation returns an error, not a run."""
rows = _map_to_wire(
[{"role": "tool", "name": "run_subagent_dag", "tool_call_id": "c", "content": "rejected: bad spec"}],
"sess",
)
assert "dag_run_id" not in rows[0]
def test_a_non_dag_tool_row_with_dag_shaped_content_names_no_run() -> None:
"""A read_file or grep row can legitimately quote a DAG's own "finished"
line back (a saved log, a grep hit) -- the name guard, not the regex, is
what must keep such a row from being mistaken for the run it quotes."""
rows = _map_to_wire(
[
{
"role": "tool",
"name": "read_file",
"tool_call_id": "c",
"content": (
"DAG run 20260823T063516789587Z-1e97d546 finished: 2 completed, 0 failed "
"(of 2).\nRun dir: /root/.raven/x/subagents/mas_dag/20260823T063516789587Z-1e97d546"
),
}
],
"sess",
)
assert "dag_run_id" not in rows[0]
def test_a_dag_tool_row_with_notice_prefix_still_names_the_run() -> None:
"""``_with_notices`` prepends "Note: ...\\n\\n" ahead of the tool's own
first line whenever the graph carried a capability-downgrade notice; the
id must still be found mid-string, not just at the start of it."""
rows = _map_to_wire(
[
{
"role": "tool",
"name": "run_subagent_dag",
"tool_call_id": "call-1",
"content": (
"Note: node 'b': agent 'x' cannot take injected skills.\n\n"
"DAG run 20260823T063516789587Z-1e97d546 finished: 2 completed, 0 failed "
"(of 2).\nRun dir: /root/.raven/x/subagents/mas_dag/20260823T063516789587Z-1e97d546"
),
}
],
"sess",
)
assert rows[0]["dag_run_id"] == "20260823T063516789587Z-1e97d546"
def test_a_spawn_tool_row_names_the_task_it_started() -> None:
"""The task id is in the result text the manager authored; the client
should not have to parse prose to find it."""
rows = _map_to_wire(
[
{
"role": "tool",
"name": "spawn",
"tool_call_id": "call-1",
"content": ("Subagent [research CO] started (id: 1a021575). I'll notify you when it completes."),
}
],
"sess",
)
assert rows[0]["spawn_task_id"] == "1a021575"
def test_a_spawn_label_quoting_a_started_line_does_not_hijack_the_task_id() -> None:
"""The label between the brackets is model-authored and can itself contain
a "started (id: ...)" shape; the manager's own suffix is the last one in
the sentence it wrote, so the last match names the task."""
rows = _map_to_wire(
[
{
"role": "tool",
"name": "spawn",
"tool_call_id": "call-1",
"content": (
"Subagent [inspect log: started (id: deadbeef)] started (id: 1a021575). "
"I'll notify you when it completes."
),
}
],
"sess",
)
assert rows[0]["spawn_task_id"] == "1a021575"
def test_a_non_spawn_tool_row_with_spawn_shaped_content_names_no_task() -> None:
"""A read_file or grep row can legitimately quote a spawn's own "started"
line back -- the name guard, not the regex, keeps such a row from being
mistaken for the run it quotes."""
rows = _map_to_wire(
[
{
"role": "tool",
"name": "read_file",
"tool_call_id": "c",
"content": "Subagent [x] started (id: 1a021575).",
}
],
"sess",
)
assert "spawn_task_id" not in rows[0]
def test_a_spawn_row_that_was_refused_names_no_task() -> None:
"""A refused spawn returns its refusal, not a run."""
rows = _map_to_wire(
[
{
"role": "tool",
"name": "spawn",
"tool_call_id": "c",
"content": "Spawn refused: delegation is paused.",
}
],
"sess",
)
assert "spawn_task_id" not in rows[0]
async def test_session_resume_carries_the_stored_title(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""The banner names what is being resumed.
Carried on the init bundle rather than fetched separately: a client resuming
a session is already being told what it is resuming, and a second round trip
for the name would draw the panel without it first.
"""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
session_key = "tui:20260610_143052_titled"
mgr = SessionManager(tmp_path)
session = mgr.get_or_create(session_key)
session.add_message("user", "cut a desktop release")
session.set_title("Cut a desktop release")
mgr.save(session)
result = await session_resume({"session_id": session_key})
assert result["info"]["title"] == "Cut a desktop release"
async def test_session_resume_of_an_unnamed_session_carries_no_title(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""A fresh mint has nothing to name yet, so the field is absent rather than
an empty string the panel would have to test for."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
result = await session_resume({"session_id": "tui:nonexistent_for_title"})
assert result["info"].get("title") is None
async def test_session_title_refuses_a_name_past_the_ceiling_with_a_legible_error(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Not -32603. A name too long is ordinary user input, and an internal_error
tells the person nothing about what they typed."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
with pytest.raises(SessionTitleTooLongError) as caught:
await session_title({"session_id": "tui:20260610_143052_toolong", "title": "x" * 201})
assert caught.value.code == -32018
assert caught.value.data == {"limit": 200}
async def test_session_title_answers_with_the_stored_form_not_the_argument(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""The caller draws what it is told. Echoing the raw argument would have it
paint a name that is not the one on disk."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
result = await session_title({"session_id": "tui:20260610_143052_ws", "title": " Ship\n the fix "})
assert result["title"] == "Ship the fix"
import inspect
from raven.memory_engine.consolidate.consolidator import MemoryConsolidator
from raven.rpc.dispatcher import Dispatcher
from raven.rpc.errors import SessionTitleTooLongError, TurnInProgressError
from raven.rpc.methods import session as session_module
from raven.rpc.models import METHOD_MODELS
from raven.utils.tokens import estimate_prompt_tokens
# ---------------------------------------------------------------------------
# session.set_mode: this session's own sub-agent tier
# ---------------------------------------------------------------------------
class _Loop:
def __init__(self) -> None:
self.policies: dict[str, str] = {}
self.iterations: dict[str, int | None] = {}
def set_session_policy(self, key, *, max_iterations=None, mode="", mode_overlay=None, reasoning_effort=None):
self.policies[key] = mode
self.iterations[key] = max_iterations
def session_policy(self, key):
from raven.agent.loop._shared import SessionPolicy
return SessionPolicy(mode=self.policies.get(key, ""))
async def test_a_read_reports_the_default_and_the_whole_menu():
loop = _Loop()
result = await session_set_mode({"session_key": "tui:1"}, agent_loop_factory=lambda: loop)
assert result["mode"] == "high"
assert [m["id"] for m in result["availableModes"]] == ["medium", "high", "max"]
async def test_a_set_lands_on_the_loop_policy():
loop = _Loop()
result = await session_set_mode({"session_key": "tui:1", "mode": "max"}, agent_loop_factory=lambda: loop)
assert result["mode"] == "max"
assert loop.policies["tui:1"] == "max"
async def test_a_clear_returns_to_the_catalogue_default():
loop = _Loop()
await session_set_mode({"session_key": "tui:1", "mode": "medium"}, agent_loop_factory=lambda: loop)
result = await session_set_mode({"session_key": "tui:1", "clear": True}, agent_loop_factory=lambda: loop)
assert result["mode"] == "high"
assert loop.policies["tui:1"] == "high"
async def test_an_unknown_tier_is_refused_naming_what_is_on_offer():
from raven.rpc.errors import ConfigValidationError
loop = _Loop()
with pytest.raises(ConfigValidationError) as exc:
await session_set_mode({"session_key": "tui:1", "mode": "turbo"}, agent_loop_factory=lambda: loop)
assert "medium" in str(exc.value) and "high" in str(exc.value) and "max" in str(exc.value)
assert loop.policies == {}, "a refused set must not half-land"
async def test_a_set_forwards_the_tiers_iteration_cap(monkeypatch: pytest.MonkeyPatch) -> None:
from raven.config.schema import AcpConfig, AcpModeConfig
cfg = load_config()
cfg.acp = AcpConfig(modes={"careful": AcpModeConfig(name="Careful", max_tool_iterations=7)}, default_mode="careful")
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
loop = _Loop()
result = await session_set_mode({"session_key": "tui:1", "mode": "careful"}, agent_loop_factory=lambda: loop)
assert result["mode"] == "careful"
assert loop.iterations["tui:1"] == 7
async def test_a_clear_wins_over_a_mode_given_alongside_it():
loop = _Loop()
result = await session_set_mode(
{"session_key": "tui:1", "mode": "max", "clear": True}, agent_loop_factory=lambda: loop
)
assert result["mode"] == "high"
assert loop.policies["tui:1"] == "high"
# ---------------------------------------------------------------------------
# session.list {archived} and the lazy auto-archive pass
# ---------------------------------------------------------------------------
def _seed_session_file(mgr: SessionManager, key: str, *, days_ago: int, pinned: bool = False, archived=None) -> Path:
"""A session file written the way ``save`` writes it, with old timestamps."""
from datetime import datetime, timedelta
when = (datetime.now() - timedelta(days=days_ago)).isoformat()
meta: dict = {"title": key, "pinned": pinned}
if archived is not None:
meta["archived"] = archived
path = mgr.session_path(key)
path.parent.mkdir(parents=True, exist_ok=True)
lines = [
json.dumps(
{
"_type": "metadata",
"key": key,
"created_at": when,
"updated_at": when,
"metadata": meta,
"last_consolidated": None,
"pending_clarification": None,
}
),
json.dumps({"role": "user", "content": "hello", "timestamp": when}),
]
path.write_text("".join(line + "\n" for line in lines), encoding="utf-8")
return path
def _last_meta(path: Path) -> dict:
records = [json.loads(line) for line in path.read_text(encoding="utf-8").splitlines() if line.strip()]
return [r for r in records if r.get("_type") == "metadata"][-1]["metadata"]
def _archive_setup(tmp_path: Path, monkeypatch: pytest.MonkeyPatch, *, after_days) -> SessionManager:
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
mgr = SessionManager(tmp_path)
monkeypatch.setattr("raven.session.resolve.build_manager", lambda cfg: mgr)
monkeypatch.setattr(
session_module,
"load_raven_config",
lambda: SimpleNamespace(sessions=SimpleNamespace(auto_archive_after_days=after_days)),
)
return mgr
async def test_session_list_archived_true_returns_only_archived(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
mgr = _archive_setup(tmp_path, monkeypatch, after_days=None)
_seed_session_file(mgr, "tui:a", days_ago=1, archived=True)
_seed_session_file(mgr, "tui:b", days_ago=1)
archived = await session_list({"archived": True})
live = await session_list({})
assert [s["id"] for s in archived["sessions"]] == ["tui:a"]
assert [s["id"] for s in live["sessions"]] == ["tui:b"]
async def test_auto_archive_marks_stale_unpinned_sessions_once(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
mgr = _archive_setup(tmp_path, monkeypatch, after_days=30)
old = _seed_session_file(mgr, "tui:old", days_ago=40)
pinned = _seed_session_file(mgr, "tui:pinned", days_ago=40, pinned=True)
fresh = _seed_session_file(mgr, "tui:fresh", days_ago=1)
before = {p: len(p.read_text().splitlines()) for p in (old, pinned, fresh)}
live = await session_list({})
assert [s["id"] for s in live["sessions"]] == ["tui:fresh", "tui:pinned"]
assert _last_meta(old) == {"title": "tui:old", "pinned": False, "archived": True, "archivedBy": "auto"}
assert "archived" not in _last_meta(pinned)
assert "archived" not in _last_meta(fresh)
assert len(old.read_text().splitlines()) == before[old] + 1
await session_list({})
assert len(old.read_text().splitlines()) == before[old] + 1
assert [s["id"] for s in (await session_list({"archived": True}))["sessions"]] == ["tui:old"]
async def test_restored_session_is_never_auto_archived_again(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
mgr = _archive_setup(tmp_path, monkeypatch, after_days=30)
old = _seed_session_file(mgr, "tui:old", days_ago=40, archived=True)
await session_archive({"session_id": "tui:old", "archived": False})
assert _last_meta(old)["archived"] is False
live = await session_list({})
assert [s["id"] for s in live["sessions"]] == ["tui:old"]
assert _last_meta(old)["archived"] is False
async def test_auto_archive_off_appends_nothing(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
mgr = _archive_setup(tmp_path, monkeypatch, after_days=None)
old = _seed_session_file(mgr, "tui:old", days_ago=400)
lines = len(old.read_text().splitlines())
await session_list({})
assert len(old.read_text().splitlines()) == lines
async def test_auto_archive_never_loads_transcripts(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
mgr = _archive_setup(tmp_path, monkeypatch, after_days=30)
for i in range(50):
_seed_session_file(mgr, f"tui:s{i}", days_ago=40)
loads: list[str] = []
real_load = SessionManager._load
def spy(self, key):
loads.append(key)
return real_load(self, key)
monkeypatch.setattr(SessionManager, "_load", spy)
await session_list({})
assert loads == []
assert len((await session_list({"archived": True}))["sessions"]) == 50
def test_as_local_datetime_answers_none_for_anything_but_an_iso_string() -> None:
assert session_module._as_local_datetime(None) is None
assert session_module._as_local_datetime("") is None
assert session_module._as_local_datetime("yesterday") is None
assert session_module._as_local_datetime(1_700_000_000) is None
parsed = session_module._as_local_datetime("2026-09-01T00:00:00Z")
assert parsed is not None and parsed.tzinfo is None
def test_auto_archive_pass_leaves_everything_alone_when_the_config_cannot_be_read(
monkeypatch: pytest.MonkeyPatch,
) -> None:
def broken():
raise RuntimeError("config unreadable")
monkeypatch.setattr(session_module, "load_raven_config", broken)
touched: list[str] = []
mgr = SimpleNamespace(append_metadata_patch=lambda key, patch: touched.append(key))
entries = [{"key": "tui:old", "metadata": {}, "last_message_at": "2000-01-01T00:00:00+00:00"}]
session_module._auto_archive_stale(mgr, entries)
assert touched == [] and "archived" not in entries[0]["metadata"]
def test_auto_archive_pass_skips_a_keyless_entry_and_survives_a_write_failure(
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.setattr(
session_module,
"load_raven_config",
lambda: SimpleNamespace(sessions=SimpleNamespace(auto_archive_after_days=30)),
)
def failing(key: str, patch: dict) -> None:
raise OSError("disk full")
mgr = SimpleNamespace(append_metadata_patch=failing)
stale = "2000-01-01T00:00:00+00:00"
entries = [
{"key": None, "metadata": {}, "last_message_at": stale},
{"key": "tui:old", "metadata": {}, "last_message_at": stale},
]
session_module._auto_archive_stale(mgr, entries)
assert all("archived" not in e["metadata"] for e in entries)
def test_append_metadata_patch_is_a_no_op_for_a_missing_or_headless_file_and_updates_the_cache(
tmp_path: Path,
) -> None:
mgr = SessionManager(tmp_path)
mgr.append_metadata_patch("tui:absent", {"archived": True})
assert not mgr.session_path("tui:absent").exists()
headless = mgr.session_path("tui:headless")
headless.parent.mkdir(parents=True, exist_ok=True)
headless.write_text(json.dumps({"role": "user", "content": "no metadata record"}) + "\n", encoding="utf-8")
mgr.append_metadata_patch("tui:headless", {"archived": True})
assert headless.read_text(encoding="utf-8").count("\n") == 1
live = mgr.get_or_create("tui:cached")
mgr.save(live)
mgr.append_metadata_patch("tui:cached", {"archived": True, "archivedBy": "auto"})
assert live.metadata["archived"] is True and live.metadata["archivedBy"] == "auto"
# ---------------------------------------------------------------------------
# session.usage — what one conversation has spent, its delegations included
# ---------------------------------------------------------------------------
def _usage_setup(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:
"""Point the handler at a throwaway workspace and a throwaway raven home."""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
# Pinned, so context_max is the number this test chose rather than whatever
# the provider table says about the default model today.
cfg.agents.defaults.context_window_tokens = 200_000
monkeypatch.setattr(session_module, "load_config", lambda: cfg)
home = tmp_path / "home"
monkeypatch.setattr(session_module, "raven_home", lambda: home)
return home
def _usage_row(
session_key: str,
*,
root: str | None = None,
input_tokens: int = 0,
output_tokens: int = 0,
cache_read_tokens: int = 0,
cache_write_tokens: int = 0,
cost_usd: float | None = None,
schema_version: int = 2,
) -> dict:
"""One telemetry row, carrying the fields the scanner reads."""
row: dict = {
"ts": "2026-09-22T10:00:00+08:00",
"model": "claude-sonnet-5",
"schema_version": schema_version,
"session_key": session_key,
"root_session_key": root if root is not None else session_key,
"input_tokens": input_tokens,
"output_tokens": output_tokens,
"cache_read_tokens": cache_read_tokens,
"cache_write_tokens": cache_write_tokens,
"finish_reason": "stop",
}
if cost_usd is not None:
row["cost_usd"] = cost_usd
return row
def _write_telemetry(home: Path, day: date, rows: list[dict]) -> None:
telemetry = home / "telemetry"
telemetry.mkdir(parents=True, exist_ok=True)
with (telemetry / f"usage-{day.isoformat()}.jsonl").open("a", encoding="utf-8") as fh:
for row in rows:
fh.write(json.dumps(row) + "\n")
async def test_session_usage_sums_the_sessions_calls_its_delegations_included(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
home = _usage_setup(tmp_path, monkeypatch)
root = "tui:20260922_101500_aabbcc"
_write_session(tmp_path, root, [{"role": "user", "content": "hello"}])
_write_telemetry(
home,
date.today(),
[
_usage_row(
root,
input_tokens=1000,
output_tokens=200,
cache_read_tokens=300,
cache_write_tokens=50,
cost_usd=0.01,
),
_usage_row("agent:sub_1", root=root, input_tokens=500, output_tokens=100, cost_usd=0.005),
_usage_row("tui:20260922_090000_deadbe", input_tokens=9999, cost_usd=1.0),
{**_usage_row(root, input_tokens=4000), "_type": "tool_call", "tool_name": "read"},
],
)
result = await session_usage({"session_id": root})
assert result["calls"] == 2
assert result["input"] == 1500
assert result["output"] == 300
assert result["cache_read"] == 300
assert result["cache_write"] == 50
assert result["total"] == 2150
assert result["cost_usd"] == pytest.approx(0.015)
assert result["cost_status"] == "exact" and result["cost_missing_calls"] == 0
assert result["model"]
assert result["context_max"] == 200_000
assert 0 < result["context_used"] < result["context_max"]
assert result["context_estimated"] is True
async def test_session_usage_for_a_worker_that_only_delegated_reports_zeros(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""A sub-agent's own calls are recorded under the session that dispatched it."""
home = _usage_setup(tmp_path, monkeypatch)
parent = "tui:20260922_101500_aabbcc"
_write_session(tmp_path, parent, [{"role": "user", "content": "hello"}])
_write_telemetry(home, date.today(), [_usage_row("agent:sub_1", root=parent, input_tokens=500, cost_usd=0.005)])
result = await session_usage({"session_id": "agent:sub_1"})
assert result["calls"] == 0 and result["total"] == 0
assert result["cost_usd"] is None
async def test_session_usage_ignores_the_days_before_the_session_existed(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
home = _usage_setup(tmp_path, monkeypatch)
root = "tui:20260922_101500_aabbcc"
_seed_session_file(SessionManager(tmp_path), root, days_ago=2)
_write_telemetry(home, date.today() - timedelta(days=5), [_usage_row(root, input_tokens=77, cost_usd=0.5)])
_write_telemetry(home, date.today() - timedelta(days=2), [_usage_row(root, input_tokens=30, cost_usd=0.002)])
_write_telemetry(home, date.today(), [_usage_row(root, input_tokens=100, cost_usd=0.01)])
result = await session_usage({"session_id": root})
assert result["calls"] == 2 and result["input"] == 130
async def test_session_usage_counts_only_provider_reported_costs(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""A legacy row's local estimate is not a bill; it is counted missing."""
home = _usage_setup(tmp_path, monkeypatch)
priced = "tui:20260922_101500_aabbcc"
unpriced = "tui:20260922_101501_bbccdd"
_write_session(tmp_path, priced, [{"role": "user", "content": "hello"}])
_write_session(tmp_path, unpriced, [{"role": "user", "content": "hello"}])
_write_telemetry(
home,
date.today(),
[
_usage_row(priced, input_tokens=10, cost_usd=0.02),
{**_usage_row(priced, input_tokens=10, schema_version=1), "estimated_cost_usd": 0.5},
_usage_row(unpriced, input_tokens=10, schema_version=1),
],
)
priced_result = await session_usage({"session_id": priced})
assert priced_result["cost_usd"] == pytest.approx(0.02)
assert priced_result["cost_status"] == "estimated" and priced_result["cost_missing_calls"] == 1
unpriced_result = await session_usage({"session_id": unpriced})
assert unpriced_result["cost_usd"] is None and unpriced_result["cost_missing_calls"] == 1
assert unpriced_result["total"] == 10
async def test_session_usage_measures_the_window_of_the_model_the_session_runs_on(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""The gauge divides by the window of the model this session switched to.
Read off the default binding instead, a session moved from a 100k model to
a 200k one is reported on the model it switched to and measured against the
window it left, so the percentage is wrong by the ratio between them.
"""
home = _usage_setup(tmp_path, monkeypatch)
# No pin, so the model is what answers: the pin outranks it either way and
# would hide which model was asked.
session_module.load_config().agents.defaults.context_window_tokens = None
windows = {"boot/model": 100_000, "switched/model": 200_000}
monkeypatch.setattr(session_module, "resolve_context_window", lambda model: windows.get(model, 0))
root = "tui:20260922_101500_aabbcc"
_write_session(tmp_path, root, [{"role": "user", "content": "hello"}])
_write_telemetry(home, date.today(), [_usage_row(root, input_tokens=42, cost_usd=0.001)])
loop = SimpleNamespace(model="boot/model", session_model=lambda _key: "switched/model")
result = await session_usage({"session_id": root}, agent_loop_factory=lambda: loop)
assert result["model"] == "switched/model"
assert result["context_max"] == 200_000
assert result["context_percent"] == round(100 * result["context_used"] / 200_000)
async def test_session_usage_answers_a_null_cost_its_published_schema_accepts(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""An unpriced session answers ``cost_usd: null``, and the wire contract has
to accept the value the handler returns -- both generated clients type
themselves off this schema, and a consumer that validates rejects the whole
result, not the one field."""
import jsonschema
home = _usage_setup(tmp_path, monkeypatch)
root = "tui:20260922_101500_aabbcc"
_write_session(tmp_path, root, [{"role": "user", "content": "hello"}])
_write_telemetry(home, date.today(), [_usage_row(root, input_tokens=42)])
result = await session_usage({"session_id": root})
assert result["cost_usd"] is None
schema = json.loads((Path(__file__).resolve().parent.parent / "rpc-schema" / "openrpc.json").read_text())
method = next(entry for entry in schema["methods"] if entry["name"] == "session.usage")
jsonschema.validate(result, {**method["result"]["schema"], "components": schema["components"]})
async def test_session_usage_requires_a_session_id() -> None:
from raven.rpc.errors import ConfigValidationError
with pytest.raises(ConfigValidationError) as exc:
await session_usage({})
assert exc.value.data == {"field": "session_id"}
async def test_session_usage_is_no_longer_a_stub(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""The promoted handler answers ``session.usage`` end to end."""
from raven.rpc.methods import register_aligned_methods
from raven.rpc.methods._stubs import HERMES_ONLY_STUB_METHODS
from raven.rpc.models import METHOD_MODELS
assert "session.usage" not in HERMES_ONLY_STUB_METHODS
home = _usage_setup(tmp_path, monkeypatch)
root = "tui:20260922_101500_aabbcc"
_write_session(tmp_path, root, [{"role": "user", "content": "hello"}])
_write_telemetry(home, date.today(), [_usage_row(root, input_tokens=42, cost_usd=0.001)])
dispatcher = Dispatcher()
register_aligned_methods(dispatcher)
assert "session.usage" in dispatcher.methods()
response = await dispatcher.dispatch(
{"jsonrpc": "2.0", "id": 1, "method": "session.usage", "params": {"session_id": root}}
)
assert "error" not in response, response
METHOD_MODELS["session.usage"][1].model_validate(response["result"])
assert response["result"]["input"] == 42
# ---------------------------------------------------------------------------
# session.set_knowledge -- which bases a conversation's turns may search
# ---------------------------------------------------------------------------
async def test_resuming_an_id_that_does_not_exist_caches_no_session(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""A read must not mint what it was asked to look at.
`_default_session_info` reads the knowledge selection before `resume`
peeks, so a read that used `get_or_create` cached a fresh empty session
under the unknown id -- and the peek below it then found that ghost and
answered it as a session, with nothing on disk behind it.
"""
sessions = _shared_sessions(tmp_path, monkeypatch)
unknown = "tui:doesnotexist123"
await session_resume({"session_id": unknown})
assert unknown not in sessions._cache, "a read minted nothing"
def _shared_sessions(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> SessionManager:
"""One session manager behind every call in a case.
`manager_for` prefers the loop's own and builds a fresh one otherwise, so
two calls with no loop write to two different managers -- and a test would
read back nothing whatever the handler did. Pinning it is what makes the
record the picker writes the record the turn reads.
"""
cfg = load_config()
cfg.agents.defaults.workspace = str(tmp_path)
monkeypatch.setattr("raven.rpc.methods.session.load_config", lambda: cfg)
sessions = SessionManager(tmp_path)
monkeypatch.setattr("raven.rpc.methods.session.manager_for", lambda loop, config: sessions)
return sessions
async def test_a_session_is_created_with_the_bases_it_was_pointed_at(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
from raven.agent import knowledge_scope
sessions = _shared_sessions(tmp_path, monkeypatch)
made = await session_create({"knowledge_bases": ["kb-a", "kb-b"]})
assert knowledge_scope.read(sessions, made["session_id"]) == ("kb-a", "kb-b")
async def test_a_session_created_without_them_searches_nothing(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""The ordinary case: a conversation nobody pointed at a base."""
from raven.agent import knowledge_scope
sessions = _shared_sessions(tmp_path, monkeypatch)
made = await session_create({})
assert knowledge_scope.read(sessions, made["session_id"]) == ()
async def test_what_was_set_is_what_the_turn_will_read(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""The whole point of the selection: the record the picker writes is the
record the turn binds from."""
from raven.agent import knowledge_scope
from raven.rpc.methods.session import session_set_knowledge
sessions = _shared_sessions(tmp_path, monkeypatch)
made = await session_create({})
await session_set_knowledge({"session_id": made["session_id"], "knowledge_bases": ["kb-b", "kb-a"]})
assert knowledge_scope.read(sessions, made["session_id"]) == ("kb-b", "kb-a")
async def test_setting_the_bases_replaces_what_was_there(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""The picker sends what is ticked, and a call that added would have no way
to say that something was unticked."""
from raven.rpc.methods.session import session_set_knowledge
_shared_sessions(tmp_path, monkeypatch)
made = await session_create({"knowledge_bases": ["kb-a", "kb-b"]})
out = await session_set_knowledge({"session_id": made["session_id"], "knowledge_bases": ["kb-c"]})
assert out == {"knowledge_bases": ["kb-c"]}
async def test_the_bases_can_be_cleared(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""Detaching every base has to be sayable, or the tool could never be taken
away once it was offered."""
from raven.rpc.methods.session import session_set_knowledge
_shared_sessions(tmp_path, monkeypatch)
made = await session_create({"knowledge_bases": ["kb-a"]})
assert await session_set_knowledge({"session_id": made["session_id"], "knowledge_bases": []}) == {
"knowledge_bases": []
}
async def test_setting_the_bases_without_a_session_is_refused() -> None:
from raven.rpc.errors import ConfigValidationError
from raven.rpc.methods.session import session_set_knowledge
with pytest.raises(ConfigValidationError):
await session_set_knowledge({"knowledge_bases": ["kb-a"]})
async def test_a_selection_that_is_not_a_list_is_refused(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
from raven.rpc.errors import ConfigValidationError
from raven.rpc.methods.session import session_set_knowledge
_shared_sessions(tmp_path, monkeypatch)
made = await session_create({})
with pytest.raises(ConfigValidationError):
await session_set_knowledge({"session_id": made["session_id"], "knowledge_bases": "kb-a"})
class _HarnessLoop:
"""The two verbs `session.set_harness` reaches for, and a library of one."""
def __init__(self, known=("travel-concierge",)) -> None:
self.known = set(known)
self.bound: dict[str, str | None] = {}
def bind_session_harness(self, session_key: str, name: str | None) -> str | None:
if name is None:
self.bound.pop(session_key, None)
return None
if name not in self.known:
raise ValueError(f"no stored Harness named {name!r}")
self.bound[session_key] = name
return name
def session_harness_name(self, session_key: str) -> str | None:
return self.bound.get(session_key)
@pytest.mark.asyncio
async def test_set_harness_reports_binds_and_unbinds_by_which_fields_are_present() -> None:
"""Three calls told apart by the `harness` key, the way session.set_mode does it."""
loop = _HarnessLoop()
reported = await session_set_harness({"session_key": "s1"}, agent_loop_factory=lambda: loop)
assert reported == {"session_key": "s1", "harness": None}
bound = await session_set_harness(
{"session_key": "s1", "harness": "travel-concierge"}, agent_loop_factory=lambda: loop
)
assert bound == {"session_key": "s1", "harness": "travel-concierge"}
assert loop.bound == {"s1": "travel-concierge"}
again = await session_set_harness({"session_key": "s1"}, agent_loop_factory=lambda: loop)
assert again["harness"] == "travel-concierge"
cleared = await session_set_harness({"session_key": "s1", "harness": None}, agent_loop_factory=lambda: loop)
assert cleared == {"session_key": "s1", "harness": None}
assert loop.bound == {}
@pytest.mark.asyncio
async def test_set_harness_refuses_a_missing_key_and_an_unknown_harness() -> None:
from raven.rpc.errors import ConfigValidationError
loop = _HarnessLoop()
with pytest.raises(ConfigValidationError) as missing:
await session_set_harness({}, agent_loop_factory=lambda: loop)
assert missing.value.data["field"] == "session_key"
with pytest.raises(ConfigValidationError) as unknown:
await session_set_harness({"session_key": "s1", "harness": "nope"}, agent_loop_factory=lambda: loop)
assert unknown.value.data["field"] == "harness"
assert "nope" in str(unknown.value)
@pytest.mark.asyncio
async def test_set_harness_says_so_when_this_build_cannot_bind_one() -> None:
"""A loop without the verb refuses the write rather than reporting success."""
from raven.rpc.errors import ConfigValidationError
with pytest.raises(ConfigValidationError):
await session_set_harness(
{"session_key": "s1", "harness": "travel-concierge"}, agent_loop_factory=lambda: object()
)
reported = await session_set_harness({"session_key": "s1"}, agent_loop_factory=lambda: object())
assert reported == {"session_key": "s1", "harness": None}
@pytest.mark.asyncio
async def test_session_create_refuses_a_harness_a_build_cannot_bind() -> None:
"""The zero-factory path has no binder, so asking to open on one is an error
rather than a session that silently comes up as an ordinary one."""
from raven.rpc.errors import ConfigValidationError
with pytest.raises(ConfigValidationError) as exc:
await session_create({"harness": "travel-concierge"})
assert exc.value.data["field"] == "harness"
# ── Imported Conversations through session.* ───────────────────────────
def _import_export(tmp_path: Path, **source: object) -> str:
from raven.trajectory import imported
from tests import imported_package_builders as builders
package, _ = builders.build_export(tmp_path / f"src-{len(list(tmp_path.glob('src-*')))}")
return imported.import_bytes(package.read_bytes(), package.name).conversation.key
def _imported_with_record(rows: list[dict]) -> str:
import gzip
import tarfile
from raven.trajectory import imported
from tests.test_trajectory_imported_package import EXP, _export_entries, _raw
record = "".join(json.dumps(r) + "\n" for r in rows).encode()
entries = _export_entries(extra=[(f"{EXP}/session.jsonl", tarfile.REGTYPE, record)])
return imported.import_bytes(gzip.compress(_raw(entries), mtime=0), "record.tar.gz", force=True).conversation.key
async def test_session_list_adds_imported_rows_when_asked(tmp_path: Path) -> None:
from raven.trajectory import imported
key = _import_export(tmp_path)
rows = (await session_list({"channels": ["tui", "imported"]}))["sessions"]
row = next(r for r in rows if r["id"] == key)
conversation = imported.get(key)
assert row == {
"id": key,
"message_count": 4,
"preview": conversation.title,
"last_message_preview": "",
"source": "imported",
"started_at": datetime.fromisoformat(conversation.imported_at).timestamp(),
"updated_at": datetime.fromisoformat(conversation.imported_at).timestamp(),
"title": conversation.title,
"pinned": False,
"running": False,
"workdir": None,
"imported": {"kind": "export", "source_name": "export.tar.gz"},
}
assert key not in [r["id"] for r in (await session_list({"channels": ["tui"]}))["sessions"]]
assert key not in [r["id"] for r in (await session_list({}))["sessions"]]
archived = await session_list({"channels": ["tui", "imported"], "archived": True})
assert key not in [r["id"] for r in archived["sessions"]]
async def test_session_list_keeps_imported_rows_out_of_the_limit_and_newest_first(tmp_path: Path) -> None:
import time
mgr = SessionManager(load_config().workspace_path)
for name in ("tui:one", "tui:two"):
session = mgr.get_or_create(name)
session.add_message("user", name)
mgr.save(session)
first = _import_export(tmp_path)
time.sleep(0.01)
second = _import_export(tmp_path)
rows = (await session_list({"channels": ["tui", "imported"], "limit": 1}))["sessions"]
assert [r["source"] for r in rows] == ["tui", "imported", "imported"]
assert [r["id"] for r in rows if r["source"] == "imported"] == [second, first]
async def test_auto_archive_never_touches_an_imported_conversation(tmp_path: Path, monkeypatch) -> None:
from raven.trajectory import imported
key = _import_export(tmp_path)
conversation = imported.get(key)
meta = dict(conversation.meta, imported_at="2020-01-01T00:00:00+00:00")
(conversation.path / "meta.json").write_text(json.dumps(meta), encoding="utf-8")
before = (conversation.path / "meta.json").read_bytes()
rows = (await session_list({"channels": ["tui", "imported"]}))["sessions"]
assert key in [r["id"] for r in rows]
assert (conversation.path / "meta.json").read_bytes() == before
async def test_resume_serves_an_imported_conversation_read_only(tmp_path: Path) -> None:
from tests import imported_package_builders as builders
key = _import_export(tmp_path)
result = await session_resume({"session_id": key})
assert result["session_id"] == key
assert result["info"]["read_only"] is True
assert result["info"]["title"] == builders.TITLE
assert result["info"]["running"] is False
_assert_session_info(result["info"])
assert [m["text"] for m in result["messages"]] == [
builders.FIRST_INPUT,
"found three hotels",
builders.SECOND_INPUT,
"booked",
]
@pytest.mark.parametrize("key", ["imported:" + "0" * 32, "imported:../../etc", "imported:"])
async def test_resume_of_an_unknown_imported_key_creates_nothing(key: str) -> None:
from raven.rpc.errors import SessionNotFoundError
sessions = load_config().workspace_path / "sessions"
before = sorted(sessions.rglob("*")) if sessions.exists() else []
with pytest.raises(SessionNotFoundError):
await session_resume({"session_id": key})
after = sorted(sessions.rglob("*")) if sessions.exists() else []
assert after == before
async def test_an_imported_delivery_is_missing_whatever_this_machine_holds(tmp_path: Path) -> None:
from raven.agent.tools.deliverables import DeliverableStore
present = tmp_path / "report.pdf"
present.write_bytes(b"pdf")
store = DeliverableStore(tmp_path / "deliverables.json")
record = store.register(
path=str(present), name="report.pdf", media_type="application/pdf", size=3, conversation="tui:s1"
)
download = f"/files/download?token={record.token}"
assert store.get(record.token) is not None
delivery = {
"role": "tool",
"content": "Delivered files",
"metadata": {
"raven_delivery": {
"message": "Final files",
"files": [
{
"name": "report.pdf",
"title": "Report",
"path": str(present),
"size": 3,
"download_path": download,
}
],
}
},
}
ordinary = session_module._map_to_wire([delivery], "tui:s1")[0]["metadata"]["raven_delivery"]["files"][0]
assert (ordinary["missing"], ordinary["download_path"]) == (False, download)
key = _imported_with_record([{"_type": "metadata", "metadata": {}}, delivery])
files = (await session_resume({"session_id": key}))["messages"][0]["metadata"]["raven_delivery"]["files"]
assert files == [
{"name": "report.pdf", "title": "Report", "path": str(present), "size": 3, "download_path": "", "missing": True}
]
async def test_title_of_an_imported_conversation(tmp_path: Path) -> None:
from raven.rpc.errors import InvalidParamsError, SessionNotFoundError, SessionTitleTooLongError
from raven.session.title import TITLE_STORAGE_MAX
from tests import imported_package_builders as builders
key = _import_export(tmp_path)
assert await session_title({"session_id": key}) == {"title": builders.TITLE, "session_key": key, "pending": False}
renamed = await session_title({"session_id": key, "title": " Hotel analysis "})
assert renamed == {"title": "Hotel analysis", "session_key": key, "pending": False}
rows = (await session_list({"channels": ["imported"]}))["sessions"]
assert [r["title"] for r in rows if r["id"] == key] == ["Hotel analysis"]
with pytest.raises(InvalidParamsError):
await session_title({"session_id": key, "title": " "})
with pytest.raises(SessionTitleTooLongError):
await session_title({"session_id": key, "title": "x" * (TITLE_STORAGE_MAX + 1)})
with pytest.raises(SessionNotFoundError):
await session_title({"session_id": "imported:" + "0" * 32, "title": "x"})
with pytest.raises(SessionNotFoundError):
await session_title({"session_id": "imported:" + "0" * 32})
async def test_delete_of_an_imported_conversation(tmp_path: Path) -> None:
from raven.trajectory import imported
from raven.trajectory import index as tidx
key = _import_export(tmp_path)
state, _ = imported.trajectory_target(key)
tidx.indexer_for(state)
assert state.resolve() in tidx._INDEXERS
assert await session_delete({"session_id": key}) == {"deleted": key, "still_on_disk": False}
assert imported.get(key) is None
assert not state.exists()
assert state.resolve() not in tidx._INDEXERS
assert await session_delete({"session_id": key}) == {"deleted": None, "still_on_disk": False}