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

-1
View File
@@ -216,7 +216,6 @@ class ChannelManager:
static_dist_path=static_path,
workspace_path=workspace,
default_restrict_to_workspace=self.config.tools.restrict_to_workspace,
unified_session=self.config.agents.defaults.unified_session,
config_path=self._config_path,
disabled_skills=set(self.config.agents.defaults.disabled_skills),
runtime_model_name=self._webui_runtime_model_name,
-2
View File
@@ -426,7 +426,6 @@ class WebSocketChannel(BaseChannel):
media: list[str] | None,
metadata: dict[str, Any],
is_dm: bool,
session_key: str | None,
require_existing_session: bool,
session_generation: str | None,
) -> None:
@@ -437,7 +436,6 @@ class WebSocketChannel(BaseChannel):
media=media,
metadata=metadata,
is_dm=is_dm,
session_key=session_key,
require_existing_session=require_existing_session,
session_generation=session_generation,
)
@@ -510,7 +510,6 @@ async def test_temporary_chat_is_transient_and_discarded(history, bus, tmp_path)
inbound = bus.publish_inbound.await_args_list[0].args[0]
assert inbound.session_key == f"websocket:{chat_id}"
assert inbound.session_key_override == f"websocket:{chat_id}"
assert inbound.require_existing_session is True
assert inbound.metadata["cli_apps"] == [{"name": "drawio"}]
assert inbound.metadata[WORKSPACE_SCOPE_METADATA_KEY] == {
@@ -5948,7 +5947,6 @@ def test_sessions_list_includes_active_run_started_at(monkeypatch) -> None:
("abc", True),
("a1b2_c:d-e", True),
("x" * 64, True),
("unified:default", True),
("", False),
("x" * 65, False),
("has space", False),
@@ -21,7 +21,6 @@ from nanobot.cron.service import CronService
from nanobot.cron.types import CronJob, CronPayload, CronRunResult, CronSchedule
from nanobot.optional_features import InstallResult
from nanobot.security.workspace_access import WORKSPACE_SCOPE_METADATA_KEY
from nanobot.session.keys import UNIFIED_SESSION_KEY
from nanobot.session.manager import Session, SessionManager
from nanobot.session.session_handles import SessionHandleResolver
from nanobot.triggers.local_store import LocalTriggerStore
@@ -429,7 +428,7 @@ async def test_session_automations_route_ignores_unified_owner(
name="Unified check",
schedule=hourly,
message="Check the shared session",
session_key=UNIFIED_SESSION_KEY,
session_key="unified:default",
origin_channel="websocket",
origin_chat_id="abc",
)
@@ -17,7 +17,6 @@ from nanobot.channels.websocket.runtime import WebSocketChannel
from nanobot.config.schema import Config
from nanobot.providers.base import LLMProvider, LLMResponse
from nanobot.runtime_context import public_history_message
from nanobot.session.keys import UNIFIED_SESSION_KEY, session_key_for_channel
from nanobot.session.manager import SessionManager
from nanobot.session.webui_turns import clear_websocket_turns
@@ -64,24 +63,23 @@ class _Runtime:
async def execute(self, message: InboundMessage) -> None:
await self.loop._enqueue_session_message(message)
tasks = self.loop._active_tasks[self.loop._effective_session_key(message)]
tasks = self.loop._active_tasks[message.session_key]
await asyncio.gather(*tuple(tasks))
@pytest.fixture(params=[False, True], ids=["separate", "shared"])
async def runtime(request: pytest.FixtureRequest, tmp_path: Path) -> AsyncIterator[_Runtime]:
unified = request.param
@pytest.fixture
async def runtime(tmp_path: Path) -> AsyncIterator[_Runtime]:
workspace = tmp_path / "workspace"
workspace.mkdir()
config = Config.model_validate({"agents": {"defaults": {
"workspace": str(workspace), "unifiedSession": unified,
"workspace": str(workspace), "unifiedSession": True,
}}})
bus = MessageBus()
sessions = SessionManager(workspace)
provider = _Provider()
loop = AgentLoop(
bus=bus, provider=provider, workspace=workspace,
session_manager=sessions, unified_session=unified,
session_manager=sessions,
)
manager = ChannelManager(
config, bus, session_manager=sessions, webui_static_dist=False,
@@ -107,18 +105,12 @@ async def test_webui_admission_executes_with_the_routed_session(runtime: _Runtim
message = await runtime.message(chat_id)
await runtime.execute(message)
key = session_key_for_channel(
"websocket", chat_id, unified_session=runtime.channel.gateway.unified_session,
)
key = f"websocket:{chat_id}"
session = await runtime.sessions.state.read(key)
assert session is not None
assert [public_history_message(record)["content"] for record in session.messages
if record["role"] == "user"] == (
["hello", "hello"] if key == UNIFIED_SESSION_KEY and chat_id == "chat-b"
else ["hello"]
)
display = await runtime.sessions.state.read_metadata(f"websocket:{chat_id}")
assert display is not None and display["metadata"]["webui"] is True
if record["role"] == "user"] == ["hello"]
assert session.metadata["webui"] is True
assert runtime.provider.calls == 2
@@ -127,7 +119,7 @@ async def test_replaced_execution_session_rejects_accepted_input(
runtime: _Runtime, operation: str,
) -> None:
message = await runtime.message("chat-stale")
key = runtime.loop._effective_session_key(message)
key = message.session_key
if operation == "reset":
await runtime.sessions.state.reset(key)
else:
@@ -153,6 +145,11 @@ async def test_failed_transport_admission_removes_new_sessions(
async def test_temporary_chat_retains_its_own_generation(history, runtime: _Runtime) -> None:
state = runtime.sessions.state
retired = await state.get("unified:default")
retired.add_message("user", "Old shared history")
await state.finish_turn(retired)
await runtime.channel._dispatch_envelope(
runtime.connection, "webui-client", {"type": "new_temporary_chat"},
)
@@ -161,8 +158,8 @@ async def test_temporary_chat_retains_its_own_generation(history, runtime: _Runt
first = await runtime.message(chat_id)
await runtime.execute(first)
assert runtime.provider.calls == 1
assert runtime.loop._effective_session_key(first) == f"websocket:{chat_id}"
assert await runtime.sessions.state.list_sessions() == []
assert first.session_key == f"websocket:{chat_id}"
assert {row["key"] for row in await state.list_sessions()} == {retired.key}
assert history.read(f"websocket:{chat_id}") == []
queued = await runtime.message(chat_id, "discarded input")
@@ -170,5 +167,7 @@ async def test_temporary_chat_retains_its_own_generation(history, runtime: _Runt
await runtime.execute(queued)
assert runtime.provider.calls == 1
assert await runtime.sessions.state.read(first.session_key) is None
assert await runtime.sessions.state.list_sessions() == []
assert await state.read(first.session_key) is None
assert {row["key"] for row in await state.list_sessions()} == {retired.key}
saved = runtime.sessions.read_session_snapshot(retired.key)
assert saved is not None and saved.messages == retired.messages