This reverts commit cb3ba45317.
Keep JSONL history and legacy proxy migration available throughout 0.4.x. Mark their removal for 0.5.0 in code and release guidance, preserving later context graph, archive and recovery changes.
101 lines
4.4 KiB
Python
101 lines
4.4 KiB
Python
from __future__ import annotations
|
|
|
|
import json
|
|
|
|
import pytest
|
|
|
|
from nanobot.agent.tools.context import RequestContext, request_context
|
|
from nanobot.agent.tools.sessions import ReadSessionTool, SearchSessionsTool
|
|
from nanobot.session.manager import SessionManager
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize("compacted", [False, True])
|
|
@pytest.mark.parametrize("tool_name", ["search", "read"])
|
|
async def test_session_tools_find_old_matches_in_full_session(
|
|
tmp_path, compacted, tool_name,
|
|
):
|
|
manager = SessionManager(tmp_path)
|
|
key = "sdk:history"
|
|
session = manager.get_or_create(key)
|
|
session.metadata.update({"title": "Project notes", "title_user_edited": True})
|
|
for index in range(180):
|
|
text = f"launch decision {index}" if index < 10 else f"unrelated update {index}"
|
|
session.add_message("user", text)
|
|
session.add_message("assistant", f"acknowledged {index}")
|
|
if compacted:
|
|
session.commit_summary_checkpoint("Project updates are complete.")
|
|
assert session.get_history() == []
|
|
manager.save(session)
|
|
manager = SessionManager(tmp_path)
|
|
|
|
try:
|
|
with request_context(RequestContext(
|
|
channel="sdk", chat_id="current", session_key="sdk:current",
|
|
)):
|
|
if tool_name == "search":
|
|
output = json.loads(await SearchSessionsTool(manager).execute(query="launch decision"))
|
|
assert [item["session_key"] for item in output["results"]] == [key]
|
|
matches = output["results"][0]["excerpts"]
|
|
expected = range(8, 10)
|
|
else:
|
|
output = json.loads(await ReadSessionTool(manager).execute(
|
|
session_key=key, query="launch decision",
|
|
))
|
|
matches = output["messages"]
|
|
expected = range(2, 10)
|
|
latest = json.loads(await ReadSessionTool(manager).execute(session_key=key))
|
|
assert len(latest["messages"]) == 8
|
|
assert [item["message_index"] for item in latest["messages"]] == list(range(352, 360))
|
|
assert latest["messages"][-1]["content"] == "acknowledged 179"
|
|
finally:
|
|
await manager.state.aclose()
|
|
|
|
assert [item["content"] for item in matches] == [f"launch decision {i}" for i in expected]
|
|
assert [item["message_index"] for item in matches] == [i * 2 for i in expected]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize("state", ["canonical", "transcript", "mixed"])
|
|
async def test_session_tools_use_canonical_records_after_restart(history, tmp_path, monkeypatch, state):
|
|
webui_dir = tmp_path / "webui"
|
|
monkeypatch.setattr("nanobot.webui.history.get_webui_dir", lambda: webui_dir)
|
|
manager = history.sessions
|
|
key = "websocket:history"
|
|
if state != "transcript":
|
|
session = manager.get_or_create(key)
|
|
session.metadata.update({"title": "Project notes", "title_user_edited": True})
|
|
session.add_message("user", "canonical needle")
|
|
manager.save(session)
|
|
if state != "canonical":
|
|
history.append(key, {
|
|
"event": "user", "chat_id": "history", "text": "display-only needle",
|
|
})
|
|
history.append(key, {"event": "turn_end", "chat_id": "history"})
|
|
|
|
def durable_files():
|
|
return {
|
|
path: path.read_bytes()
|
|
for root in (manager.sessions_dir, webui_dir)
|
|
for path in root.rglob("*.jsonl")
|
|
}
|
|
|
|
original = durable_files()
|
|
for _ in range(2):
|
|
manager = history.sessions
|
|
with request_context(RequestContext(
|
|
channel="websocket", chat_id="current", session_key="websocket:current",
|
|
)):
|
|
search = json.loads(await SearchSessionsTool(manager).execute(query="needle"))
|
|
read = await ReadSessionTool(manager).execute(session_key=key, query="needle")
|
|
display_search = json.loads(await SearchSessionsTool(manager).execute(query="display-only"))
|
|
if state == "transcript":
|
|
assert search["results"] == []
|
|
assert json.loads(read)["messages"] == []
|
|
else:
|
|
assert [row["session_key"] for row in search["results"]] == [key]
|
|
assert search["results"][0]["excerpts"][0]["content"] == "canonical needle"
|
|
assert [item["content"] for item in json.loads(read)["messages"]] == ["canonical needle"]
|
|
assert display_search["results"] == []
|
|
assert durable_files() == original
|