fix(session): search older pages of persisted conversation history
This commit is contained in:
1 parent
4535496064
commit
151173c8ba
2 files changed
+96
-7
No files matched your search
@@ -130,13 +130,36 @@ class WebuiSessionAccess:
|
|||||||
if isinstance(message, dict)
|
if isinstance(message, dict)
|
||||||
]
|
]
|
||||||
|
|
||||||
thread = build_webui_thread_response(
|
# The WebUI replay API returns one page, even when no limit is given.
|
||||||
session_key,
|
# Session tools need the older pages too, including turns no longer in
|
||||||
session_messages_loader=load_session_messages,
|
# the compacted model history. Keep the UI's per-page replay budgets.
|
||||||
)
|
pages: list[list[object]] = []
|
||||||
if thread is not None:
|
before: str | None = None
|
||||||
return _visible_messages(thread.get("messages"))
|
while True:
|
||||||
return _visible_messages(load_session_messages())
|
thread = build_webui_thread_response(
|
||||||
|
session_key,
|
||||||
|
session_messages_loader=load_session_messages,
|
||||||
|
before=before,
|
||||||
|
)
|
||||||
|
if thread is None:
|
||||||
|
if before is None:
|
||||||
|
return _visible_messages(load_session_messages())
|
||||||
|
break
|
||||||
|
raw_messages = thread.get("messages")
|
||||||
|
if isinstance(raw_messages, list):
|
||||||
|
pages.append(cast(list[object], raw_messages))
|
||||||
|
raw_page = thread.get("page")
|
||||||
|
if not isinstance(raw_page, dict):
|
||||||
|
break
|
||||||
|
page = cast(dict[str, Any], raw_page)
|
||||||
|
cursor = page.get("before_cursor")
|
||||||
|
if not page.get("has_more_before") or not isinstance(cursor, str) or cursor == before:
|
||||||
|
break
|
||||||
|
before = cursor
|
||||||
|
|
||||||
|
# Page traversal is newest-first; filter after restoring chronological
|
||||||
|
# order so message indexes and the latest-match limits stay consistent.
|
||||||
|
return _visible_messages([message for page in reversed(pages) for message in page])
|
||||||
|
|
||||||
def search(
|
def search(
|
||||||
self,
|
self,
|
||||||
|
|||||||
@@ -0,0 +1,66 @@
|
|||||||
|
"""Session tools must not mistake a transcript page for the whole conversation."""
|
||||||
|
|
||||||
|
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
|
||||||
|
from nanobot.webui.transcript import append_transcript_object, webui_transcript_segments_dir
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
@pytest.mark.parametrize("compacted", [False, True])
|
||||||
|
@pytest.mark.parametrize("tool_name", ["search", "read"])
|
||||||
|
async def test_session_tools_find_old_matches_beyond_the_latest_page(
|
||||||
|
tmp_path, monkeypatch, compacted, tool_name,
|
||||||
|
):
|
||||||
|
webui_dir = tmp_path / "webui"
|
||||||
|
monkeypatch.setattr("nanobot.webui.transcript.get_webui_dir", lambda: webui_dir)
|
||||||
|
monkeypatch.setattr("nanobot.webui.session_list_index.get_webui_dir", lambda: webui_dir)
|
||||||
|
if compacted:
|
||||||
|
monkeypatch.setattr("nanobot.webui.transcript._ACTIVE_TRANSCRIPT_ROTATE_BYTES", 8192)
|
||||||
|
monkeypatch.setattr("nanobot.webui.transcript._TARGET_ACTIVE_TRANSCRIPT_BYTES", 4096)
|
||||||
|
manager = SessionManager(tmp_path)
|
||||||
|
key = "websocket: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}")
|
||||||
|
for record in (
|
||||||
|
{"event": "user", "text": text},
|
||||||
|
{"event": "message", "text": f"acknowledged {index}"},
|
||||||
|
{"event": "turn_end"},
|
||||||
|
):
|
||||||
|
append_transcript_object(key, record)
|
||||||
|
if compacted:
|
||||||
|
session.messages = session.messages[-2:]
|
||||||
|
assert any(webui_transcript_segments_dir(key).glob("*.jsonl"))
|
||||||
|
manager.save(session)
|
||||||
|
|
||||||
|
with request_context(RequestContext(
|
||||||
|
channel="websocket", chat_id="current", session_key="websocket: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"
|
||||||
|
|
||||||
|
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]
|
||||||
Reference in new issue
Block a user