refactor(sessions)!: retire the global shared session (#6621)

Co-authored-by: chengyongru <chengyongru.ai@gmail.com>
This commit is contained in:
Xubin Renandchengyongru authored and GitHub committed 2026-11-12 09:55:55 +08:00
1 parent 28d73b6942
commit e6b0aec2a8
77 files changed
+311 -1255

No files matched your search

+59
View File
@@ -103,6 +103,65 @@ def test_from_config_creates_instance(tmp_path):
assert bot._loop.sessions.sessions_dir.parent == config_path.parent / "sessions"
@pytest.mark.asyncio
async def test_shared_history_remains_readable_after_retirement(tmp_path):
from nanobot.session.manager import SessionManager
config_path = _write_config(tmp_path, {
"agents": {"defaults": {"model": "openai/gpt-4.1", "unifiedSession": True}},
})
workspace = tmp_path / "workspace"
before = SessionManager(workspace, sessions_root=config_path.parent / "sessions")
shared = before.get_or_create("unified:default")
shared.metadata["last_channel"] = "telegram:legacy-chat"
shared.add_message("user", "Old shared question")
shared.add_message("assistant", "Old shared answer")
before.save(shared)
await before.state.aclose()
config_bytes = config_path.read_bytes()
for _ in range(2):
async with Nanobot.from_config(config_path, workspace=workspace) as bot:
assert {row.key for row in await bot.sessions.list()} >= {"unified:default"}
snapshot = await bot.sessions.get("unified:default")
assert snapshot is not None
assert [row["content"] for row in snapshot.messages] == [
"Old shared question", "Old shared answer",
]
assert config_path.read_bytes() == config_bytes
@pytest.mark.asyncio
@pytest.mark.parametrize("legacy_key", ["unifiedSession", "unified_session"])
async def test_legacy_unified_config_keeps_runs_isolated(tmp_path, monkeypatch, legacy_key):
from nanobot.providers.base import LLMResponse
config_path = _write_config(tmp_path, {
"agents": {"defaults": {"model": "openai/gpt-4.1", legacy_key: True}},
})
original_config = config_path.read_bytes()
provider = _fake_provider("test-model")
provider.aclose = AsyncMock()
provider.chat_stream_with_retry = AsyncMock(return_value=LLMResponse(content="reply"))
monkeypatch.setattr("nanobot.providers.factory.make_provider", lambda *_args: provider)
async with Nanobot.from_config(config_path, workspace=tmp_path / "workspace") as bot:
await bot.run("alice secret", session_key="sdk:alice")
await bot.run("bob secret", session_key="sdk:bob")
await bot.run("alice followup", session_key="sdk:alice")
alice = await bot.sessions.get("sdk:alice")
bob = await bot.sessions.get("sdk:bob")
assert [m["content"] for m in alice.messages] == [
"alice secret", "reply", "alice followup", "reply",
]
assert [m["content"] for m in bob.messages] == ["bob secret", "reply"]
prompts = [str(call.kwargs["messages"]) for call in provider.chat_stream_with_retry.await_args_list]
assert "alice secret" not in prompts[1]
assert "alice secret" in prompts[2]
assert "bob secret" not in prompts[2]
assert config_path.read_bytes() == original_config
def test_from_config_composes_configured_mcp_outside_agent_loop(tmp_path):
config_path = _write_config(
tmp_path,