fix(webui): complete temporary chat mode
This commit is contained in:
1 parent
c9a6145878
commit
a5bc3bfbb9
51 files changed
+1285
-945
No files matched your search
@@ -8,11 +8,7 @@ from typing import Any, cast
|
||||
|
||||
from loguru import logger
|
||||
|
||||
from nanobot.bus.events import (
|
||||
INBOUND_META_TRANSIENT_SESSION,
|
||||
InboundMessage,
|
||||
OutboundMessage,
|
||||
)
|
||||
from nanobot.bus.events import InboundMessage, OutboundMessage
|
||||
from nanobot.bus.queue import MessageBus
|
||||
from nanobot.pairing import (
|
||||
PAIRING_CODE_META_KEY,
|
||||
@@ -266,6 +262,7 @@ class BaseChannel(ABC):
|
||||
session_key: str | None = None,
|
||||
is_dm: bool = False,
|
||||
authorization_id: str | None = None,
|
||||
require_existing_session: bool = False,
|
||||
) -> None:
|
||||
"""Handle a message after checking its authorization subject.
|
||||
|
||||
@@ -306,8 +303,7 @@ class BaseChannel(ABC):
|
||||
)
|
||||
return
|
||||
|
||||
meta = dict(metadata or {})
|
||||
transient_session = meta.pop(INBOUND_META_TRANSIENT_SESSION, False) is True
|
||||
meta = metadata or {}
|
||||
if self.supports_streaming:
|
||||
meta = {**meta, "_wants_stream": True}
|
||||
|
||||
@@ -319,7 +315,7 @@ class BaseChannel(ABC):
|
||||
media=media or [],
|
||||
metadata=meta,
|
||||
session_key_override=session_key,
|
||||
transient_session=transient_session,
|
||||
require_existing_session=require_existing_session,
|
||||
)
|
||||
|
||||
await self.bus.publish_inbound(msg)
|
||||
|
||||
@@ -431,6 +431,7 @@ class SignalChannel(BaseChannel):
|
||||
session_key: str | None = None,
|
||||
is_dm: bool = False,
|
||||
authorization_id: str | None = None,
|
||||
require_existing_session: bool = False,
|
||||
) -> None:
|
||||
"""Handle an inbound message whose policy has already been checked.
|
||||
|
||||
@@ -453,6 +454,7 @@ class SignalChannel(BaseChannel):
|
||||
media=media or [],
|
||||
metadata=meta,
|
||||
session_key_override=session_key,
|
||||
require_existing_session=require_existing_session,
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@@ -21,8 +21,10 @@ from websockets.exceptions import ConnectionClosed
|
||||
from websockets.http11 import Request as WsRequest
|
||||
|
||||
from nanobot.bus.events import (
|
||||
INBOUND_META_TRANSIENT_SESSION,
|
||||
INBOUND_META_RUNTIME_CONTROL,
|
||||
OUTBOUND_META_AGENT_UI,
|
||||
RUNTIME_CONTROL_SESSION_DISCARD,
|
||||
InboundMessage,
|
||||
OutboundMessage,
|
||||
)
|
||||
from nanobot.bus.outbound_events import (
|
||||
@@ -37,11 +39,6 @@ from nanobot.bus.outbound_events import (
|
||||
)
|
||||
from nanobot.bus.queue import MessageBus
|
||||
from nanobot.channels.base import BaseChannel
|
||||
from nanobot.channels.websocket.temporary_chat import (
|
||||
TEMPORARY_COMMANDS,
|
||||
TemporaryChats,
|
||||
has_temporary_chat_prefix,
|
||||
)
|
||||
from nanobot.command.builtin import builtin_command_starts_agent_turn
|
||||
from nanobot.config.schema import Base
|
||||
from nanobot.runtime_context import (
|
||||
@@ -313,6 +310,13 @@ def _parse_inbound_payload(raw: str) -> str | None:
|
||||
# Accept UUIDs and short scoped keys like "unified:default". Keeps the capability
|
||||
# namespace small enough to rule out path traversal / quote injection tricks.
|
||||
_CHAT_ID_RE = re.compile(r"^[A-Za-z0-9_:-]{1,64}$")
|
||||
_TEMPORARY_CHAT_PREFIX = "temporary-"
|
||||
_TEMPORARY_CHAT_DISABLED_TOOLS = frozenset({
|
||||
"create_goal",
|
||||
"update_goal",
|
||||
"spawn",
|
||||
"cron",
|
||||
})
|
||||
|
||||
|
||||
def _is_valid_chat_id(value: Any) -> TypeGuard[str]:
|
||||
@@ -320,7 +324,7 @@ def _is_valid_chat_id(value: Any) -> TypeGuard[str]:
|
||||
|
||||
|
||||
def _is_temporary_chat_id(value: Any) -> TypeGuard[str]:
|
||||
return _is_valid_chat_id(value) and has_temporary_chat_prefix(value)
|
||||
return _is_valid_chat_id(value) and value.startswith(_TEMPORARY_CHAT_PREFIX)
|
||||
|
||||
|
||||
def _parse_envelope(raw: str) -> dict[str, Any] | None:
|
||||
@@ -397,9 +401,9 @@ class WebSocketChannel(BaseChannel):
|
||||
if gateway.session_manager is not None
|
||||
else None
|
||||
)
|
||||
self._temporary_chats = TemporaryChats(gateway.session_manager, self._media, bus)
|
||||
|
||||
self._stream_text_buffers: dict[tuple[str, str], list[str]] = {}
|
||||
self._temporary_media_paths: dict[str, set[str]] = {}
|
||||
|
||||
# -- Subscription bookkeeping -------------------------------------------
|
||||
|
||||
@@ -428,31 +432,38 @@ class WebSocketChannel(BaseChannel):
|
||||
if key[0] == chat_id:
|
||||
self._stream_text_buffers.pop(key, None)
|
||||
|
||||
def _claim_temporary_chat(
|
||||
self,
|
||||
connection: ServerConnection,
|
||||
chat_id: str,
|
||||
) -> str | None:
|
||||
"""Create the connection's single in-memory chat on first use."""
|
||||
if connection not in self._webui_connections:
|
||||
return "temporary_chat_unavailable"
|
||||
if detail := self._temporary_chats.claim(connection, chat_id):
|
||||
return detail
|
||||
self._attach(connection, chat_id)
|
||||
return None
|
||||
def _discard_temporary_media(self, chat_id: str) -> None:
|
||||
"""Remove uploads owned by one connection-scoped temporary chat."""
|
||||
for raw_path in self._temporary_media_paths.pop(chat_id, set()):
|
||||
try:
|
||||
Path(raw_path).unlink(missing_ok=True)
|
||||
except OSError:
|
||||
self.logger.warning("failed to remove a temporary WebUI attachment")
|
||||
|
||||
async def _discard_temporary_chat(
|
||||
self,
|
||||
connection: ServerConnection,
|
||||
chat_id: str,
|
||||
) -> str | None:
|
||||
detail = await self._temporary_chats.discard(connection, chat_id)
|
||||
if detail is not None:
|
||||
return detail
|
||||
) -> None:
|
||||
session_key = f"{self.name}:{chat_id}"
|
||||
self._detach(connection, chat_id)
|
||||
clear_websocket_turns(chat_id)
|
||||
self._clear_stream_buffers(chat_id)
|
||||
return None
|
||||
self._discard_temporary_media(chat_id)
|
||||
if self.gateway.session_manager is not None:
|
||||
self.gateway.session_manager.invalidate(session_key)
|
||||
await self.bus.publish_inbound(
|
||||
InboundMessage(
|
||||
channel=self.name,
|
||||
sender_id="webui",
|
||||
chat_id=chat_id,
|
||||
content="",
|
||||
metadata={
|
||||
INBOUND_META_RUNTIME_CONTROL: RUNTIME_CONTROL_SESSION_DISCARD,
|
||||
},
|
||||
session_key_override=session_key,
|
||||
)
|
||||
)
|
||||
|
||||
async def send_webui_protocol_error(
|
||||
self,
|
||||
@@ -484,15 +495,14 @@ class WebSocketChannel(BaseChannel):
|
||||
|
||||
async def _cleanup_connection(self, connection: ServerConnection) -> None:
|
||||
"""Remove *connection* from every subscription set; safe to call multiple times."""
|
||||
try:
|
||||
temporary_chat_id = self._temporary_chats.chat_id_for(connection)
|
||||
if temporary_chat_id is not None:
|
||||
await self._discard_temporary_chat(connection, temporary_chat_id)
|
||||
finally:
|
||||
for chat_id in tuple(self._conn_chats.get(connection, ())):
|
||||
self._detach(connection, chat_id)
|
||||
self._conn_default.pop(connection, None)
|
||||
self._webui_connections.discard(connection)
|
||||
chat_ids = tuple(self._conn_chats.get(connection, ()))
|
||||
for cid in chat_ids:
|
||||
if _is_temporary_chat_id(cid):
|
||||
await self._discard_temporary_chat(connection, cid)
|
||||
else:
|
||||
self._detach(connection, cid)
|
||||
self._conn_default.pop(connection, None)
|
||||
self._webui_connections.discard(connection)
|
||||
|
||||
async def _maybe_push_active_goal_state(self, chat_id: str) -> None:
|
||||
"""Replay an active sustained goal from session metadata after *chat_id* is subscribed.
|
||||
@@ -813,22 +823,13 @@ class WebSocketChannel(BaseChannel):
|
||||
if not _is_temporary_chat_id(cid):
|
||||
await self._send_event(connection, "error", detail="invalid temporary chat_id")
|
||||
return
|
||||
if detail := await self._discard_temporary_chat(connection, cid):
|
||||
await self._send_event(connection, "error", detail=detail, chat_id=cid)
|
||||
await self._discard_temporary_chat(connection, cid)
|
||||
return
|
||||
if t == "attach":
|
||||
cid = envelope.get("chat_id")
|
||||
if not _is_valid_chat_id(cid):
|
||||
await self._send_event(connection, "error", detail="invalid chat_id")
|
||||
return
|
||||
if _is_temporary_chat_id(cid):
|
||||
await self._send_event(
|
||||
connection,
|
||||
"error",
|
||||
detail="temporary_chat_cannot_attach",
|
||||
chat_id=cid,
|
||||
)
|
||||
return
|
||||
self._attach(connection, cid)
|
||||
await self._send_event(connection, "attached", chat_id=cid)
|
||||
await self._hydrate_after_subscribe(cid)
|
||||
@@ -862,14 +863,6 @@ class WebSocketChannel(BaseChannel):
|
||||
if not _is_valid_chat_id(cid):
|
||||
await self._send_event(connection, "error", detail="invalid chat_id")
|
||||
return
|
||||
if _is_temporary_chat_id(cid):
|
||||
await self._send_event(
|
||||
connection,
|
||||
"error",
|
||||
detail="temporary_chat_scope_is_per_message",
|
||||
chat_id=cid,
|
||||
)
|
||||
return
|
||||
scope = await self._workspace_scope_or_error(
|
||||
connection,
|
||||
lambda: self._workspaces.scope_for_set_request(
|
||||
@@ -941,7 +934,7 @@ class WebSocketChannel(BaseChannel):
|
||||
|
||||
if temporary:
|
||||
command = content.strip().partition(" ")[0].lower()
|
||||
if command.startswith("/") and command not in TEMPORARY_COMMANDS:
|
||||
if command.startswith("/") and command not in {"/model", "/stop"}:
|
||||
await self._send_event(
|
||||
connection,
|
||||
"error",
|
||||
@@ -949,14 +942,18 @@ class WebSocketChannel(BaseChannel):
|
||||
**rejection_fields,
|
||||
)
|
||||
return
|
||||
if detail := self._claim_temporary_chat(connection, cid):
|
||||
if self.gateway.session_manager is None:
|
||||
await self._send_event(
|
||||
connection,
|
||||
"error",
|
||||
detail=detail,
|
||||
detail="temporary_chat_unavailable",
|
||||
**rejection_fields,
|
||||
)
|
||||
return
|
||||
self.gateway.session_manager.get_or_create_transient(
|
||||
f"{self.name}:{cid}",
|
||||
disabled_tools=_TEMPORARY_CHAT_DISABLED_TOOLS,
|
||||
)
|
||||
|
||||
raw_media = envelope.get("media")
|
||||
media_paths: list[str] = []
|
||||
@@ -970,12 +967,7 @@ class WebSocketChannel(BaseChannel):
|
||||
**rejection_fields,
|
||||
)
|
||||
return
|
||||
store_attachments = (
|
||||
self._media.store_temporary_attachments
|
||||
if temporary
|
||||
else self._media.store_inbound_attachments
|
||||
)
|
||||
media_paths, reason = store_attachments(cast(list[Any], raw_media))
|
||||
media_paths, reason = self._media.store_inbound_attachments(cast(list[Any], raw_media))
|
||||
if reason is not None:
|
||||
await self._send_event(
|
||||
connection,
|
||||
@@ -985,8 +977,9 @@ class WebSocketChannel(BaseChannel):
|
||||
**rejection_fields,
|
||||
)
|
||||
return
|
||||
if temporary:
|
||||
self._temporary_chats.remember_attachments(cid, media_paths)
|
||||
if temporary and media_paths:
|
||||
self._temporary_media_paths.setdefault(cid, set()).update(media_paths)
|
||||
|
||||
# Allow media-only turns (content may be empty when attachments are present).
|
||||
if not content.strip() and not media_paths:
|
||||
await self._send_event(
|
||||
@@ -996,19 +989,23 @@ class WebSocketChannel(BaseChannel):
|
||||
**rejection_fields,
|
||||
)
|
||||
return
|
||||
# Auto-attach on first use so clients can one-shot without a separate attach.
|
||||
self._attach(connection, cid)
|
||||
if not temporary:
|
||||
# Auto-attach on first use so clients can one-shot without a separate attach.
|
||||
self._attach(connection, cid)
|
||||
await self._hydrate_after_subscribe(cid)
|
||||
|
||||
# Resolve after hydration so a concurrent downgrade cannot be overwritten.
|
||||
scope = await self._workspace_scope_or_error(
|
||||
connection,
|
||||
lambda: self._workspaces.scope_for_message(
|
||||
envelope,
|
||||
chat_id=cid,
|
||||
chat_running=websocket_turn_wall_started_at(cid) is not None,
|
||||
controls_available=self._workspace_controls_available(connection),
|
||||
lambda: (
|
||||
self._workspaces.restricted_default_scope()
|
||||
if temporary
|
||||
else self._workspaces.scope_for_message(
|
||||
envelope,
|
||||
chat_id=cid,
|
||||
chat_running=websocket_turn_wall_started_at(cid) is not None,
|
||||
controls_available=self._workspace_controls_available(connection),
|
||||
)
|
||||
),
|
||||
chat_id=cid,
|
||||
turn_id=turn_id,
|
||||
@@ -1029,8 +1026,6 @@ class WebSocketChannel(BaseChannel):
|
||||
return
|
||||
|
||||
metadata: dict[str, Any] = {"remote": getattr(connection, "remote_address", None)}
|
||||
if temporary:
|
||||
metadata[INBOUND_META_TRANSIENT_SESSION] = True
|
||||
if envelope.get("webui") is True:
|
||||
metadata["webui"] = True
|
||||
metadata.update(self._transcripts.client_turn_metadata(envelope.get("turn_id")))
|
||||
@@ -1092,6 +1087,8 @@ class WebSocketChannel(BaseChannel):
|
||||
media=media_paths or None,
|
||||
metadata=metadata,
|
||||
is_dm=False,
|
||||
session_key=f"{self.name}:{cid}" if temporary else None,
|
||||
require_existing_session=temporary,
|
||||
)
|
||||
accepted = True
|
||||
finally:
|
||||
@@ -1147,13 +1144,13 @@ class WebSocketChannel(BaseChannel):
|
||||
except Exception as e:
|
||||
self.logger.warning("server task error during shutdown: {}", e)
|
||||
self._server_task = None
|
||||
for connection in tuple(self._conn_chats):
|
||||
await self._cleanup_connection(connection)
|
||||
self._subs.clear()
|
||||
self._conn_chats.clear()
|
||||
self._conn_default.clear()
|
||||
self._webui_connections.clear()
|
||||
self._tokens.clear()
|
||||
for chat_id in tuple(self._temporary_media_paths):
|
||||
self._discard_temporary_media(chat_id)
|
||||
|
||||
async def _safe_send_to(
|
||||
self,
|
||||
|
||||
@@ -1,84 +0,0 @@
|
||||
"""Connection-owned, in-memory WebUI chat lifecycle."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from nanobot.bus.events import (
|
||||
INBOUND_META_RUNTIME_CONTROL,
|
||||
RUNTIME_CONTROL_TRANSIENT_SESSION_DISCARD,
|
||||
InboundMessage,
|
||||
)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from nanobot.bus.queue import MessageBus
|
||||
from nanobot.session.manager import SessionManager
|
||||
from nanobot.webui.media_gateway import WebUIMediaGateway
|
||||
|
||||
|
||||
TEMPORARY_CHAT_ID_PREFIX = "temporary-"
|
||||
TEMPORARY_COMMANDS = frozenset({"/model", "/stop"})
|
||||
|
||||
|
||||
def has_temporary_chat_prefix(value: object) -> bool:
|
||||
return isinstance(value, str) and value.startswith(TEMPORARY_CHAT_ID_PREFIX)
|
||||
|
||||
|
||||
class TemporaryChats:
|
||||
"""Keep temporary session ownership and cleanup behind one boundary."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
sessions: SessionManager | None,
|
||||
media: WebUIMediaGateway,
|
||||
bus: MessageBus,
|
||||
) -> None:
|
||||
self._sessions = sessions
|
||||
self._media = media
|
||||
self._bus = bus
|
||||
self._by_owner: dict[object, str] = {}
|
||||
self._owners: dict[str, object] = {}
|
||||
self._attachments: dict[str, list[str]] = {}
|
||||
|
||||
def chat_id_for(self, owner: object) -> str | None:
|
||||
return self._by_owner.get(owner)
|
||||
|
||||
def claim(self, owner: object, chat_id: str) -> str | None:
|
||||
if self._sessions is None:
|
||||
return "temporary_chat_unavailable"
|
||||
current_owner = self._owners.get(chat_id)
|
||||
if current_owner is not None and current_owner is not owner:
|
||||
return "temporary_chat_not_owned"
|
||||
current_chat = self._by_owner.get(owner)
|
||||
if current_chat is not None and current_chat != chat_id:
|
||||
return "temporary_chat_in_use"
|
||||
self._by_owner[owner] = chat_id
|
||||
self._owners[chat_id] = owner
|
||||
self._sessions.get_or_create_transient(f"websocket:{chat_id}")
|
||||
return None
|
||||
|
||||
def remember_attachments(self, chat_id: str, paths: list[str]) -> None:
|
||||
self._attachments.setdefault(chat_id, []).extend(paths)
|
||||
|
||||
async def discard(self, owner: object, chat_id: str) -> str | None:
|
||||
current_owner = self._owners.get(chat_id)
|
||||
if current_owner is None:
|
||||
return None
|
||||
if current_owner is not owner:
|
||||
return "temporary_chat_not_owned"
|
||||
self._owners.pop(chat_id, None)
|
||||
self._by_owner.pop(owner, None)
|
||||
session_key = f"websocket:{chat_id}"
|
||||
assert self._sessions is not None
|
||||
self._sessions.discard_transient(session_key)
|
||||
self._media.discard_inbound_attachments(self._attachments.pop(chat_id, []))
|
||||
await self._bus.publish_inbound(InboundMessage(
|
||||
channel="websocket",
|
||||
sender_id="webui",
|
||||
chat_id=chat_id,
|
||||
content="",
|
||||
metadata={
|
||||
INBOUND_META_RUNTIME_CONTROL: RUNTIME_CONTROL_TRANSIENT_SESSION_DISCARD,
|
||||
},
|
||||
))
|
||||
return None
|
||||
@@ -15,7 +15,7 @@ from websockets.frames import Close
|
||||
from nanobot.bus.events import (
|
||||
INBOUND_META_RUNTIME_CONTROL,
|
||||
OUTBOUND_META_AGENT_UI,
|
||||
RUNTIME_CONTROL_TRANSIENT_SESSION_DISCARD,
|
||||
RUNTIME_CONTROL_SESSION_DISCARD,
|
||||
OutboundMessage,
|
||||
)
|
||||
from nanobot.bus.outbound_events import (
|
||||
@@ -38,6 +38,7 @@ from nanobot.channels.websocket.runtime import (
|
||||
from nanobot.config.loader import load_config, save_config
|
||||
from nanobot.config.schema import Config, ModelPresetConfig
|
||||
from nanobot.runtime_context import RUNTIME_CONTEXT_INPUT_META, WEBUI_QUOTE_SOURCE
|
||||
from nanobot.security.workspace_access import WORKSPACE_SCOPE_METADATA_KEY
|
||||
from nanobot.session import webui_turns as wth
|
||||
from nanobot.session.manager import SessionManager
|
||||
from nanobot.webui.gateway_services import GatewayServices, build_gateway_services
|
||||
@@ -195,8 +196,10 @@ def isolate_webui_workspace_state(tmp_path, monkeypatch) -> None:
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_temporary_chat_is_connection_owned_and_never_persisted(bus, tmp_path) -> None:
|
||||
async def test_temporary_chat_is_transient_and_discarded(bus, tmp_path) -> None:
|
||||
sessions = SessionManager(tmp_path)
|
||||
selected_project = tmp_path / "selected-project"
|
||||
selected_project.mkdir()
|
||||
channel = WebSocketChannel(
|
||||
{"enabled": True, "allowFrom": ["*"]},
|
||||
bus,
|
||||
@@ -210,6 +213,11 @@ async def test_temporary_chat_is_connection_owned_and_never_persisted(bus, tmp_p
|
||||
connection.remote_address = ("127.0.0.1", 5000)
|
||||
channel._webui_connections.add(connection)
|
||||
chat_id = "temporary-test"
|
||||
upload = tmp_path / "temporary-upload.txt"
|
||||
upload.write_text("private attachment", encoding="utf-8")
|
||||
channel.gateway.media.store_inbound_attachments = MagicMock(
|
||||
return_value=([str(upload)], None),
|
||||
)
|
||||
|
||||
await channel._dispatch_envelope(
|
||||
connection,
|
||||
@@ -218,8 +226,12 @@ async def test_temporary_chat_is_connection_owned_and_never_persisted(bus, tmp_p
|
||||
"type": "message",
|
||||
"chat_id": chat_id,
|
||||
"content": "read this",
|
||||
"media": [{"data_url": "data:text/plain;base64,aGVsbG8=", "name": "note.txt"}],
|
||||
"media": [{"data_url": "data:text/plain;base64,cHJpdmF0ZQ=="}],
|
||||
"cli_apps": [{"name": "drawio"}],
|
||||
"workspace_scope": {
|
||||
"project_path": str(selected_project),
|
||||
"access_mode": "full",
|
||||
},
|
||||
"turn_id": "turn-1",
|
||||
"webui": True,
|
||||
},
|
||||
@@ -227,10 +239,17 @@ async def test_temporary_chat_is_connection_owned_and_never_persisted(bus, tmp_p
|
||||
|
||||
inbound = bus.publish_inbound.await_args_list[0].args[0]
|
||||
assert inbound.session_key == f"websocket:{chat_id}"
|
||||
assert inbound.transient_session is True
|
||||
assert inbound.session_key_override == f"websocket:{chat_id}"
|
||||
assert inbound.require_existing_session is True
|
||||
assert inbound.metadata["cli_apps"] == [{"name": "drawio"}]
|
||||
assert Path(inbound.media[0]).read_text(encoding="utf-8") == "hello"
|
||||
assert sessions.get_cached(inbound.session_key).transient is True
|
||||
assert inbound.metadata[WORKSPACE_SCOPE_METADATA_KEY] == {
|
||||
"project_path": str(tmp_path.resolve()),
|
||||
"access_mode": "restricted",
|
||||
}
|
||||
session = sessions.get_cached(inbound.session_key)
|
||||
assert session is not None
|
||||
assert session.policy.persist is False
|
||||
assert upload.exists()
|
||||
assert read_transcript_lines(inbound.session_key) == []
|
||||
assert [payload["event"] for payload in _sent_ws_payloads(connection)] == [
|
||||
"message_accepted",
|
||||
@@ -243,62 +262,43 @@ async def test_temporary_chat_is_connection_owned_and_never_persisted(bus, tmp_p
|
||||
)
|
||||
|
||||
control = bus.publish_inbound.await_args_list[1].args[0]
|
||||
assert bus.publish_inbound.await_count == 2
|
||||
assert control.session_key == inbound.session_key
|
||||
assert control.metadata[INBOUND_META_RUNTIME_CONTROL] == (
|
||||
RUNTIME_CONTROL_TRANSIENT_SESSION_DISCARD
|
||||
RUNTIME_CONTROL_SESSION_DISCARD
|
||||
)
|
||||
assert sessions.is_transient_active(inbound.session_key) is False
|
||||
assert Path(inbound.media[0]).exists() is False
|
||||
assert sessions.get_cached(inbound.session_key) is None
|
||||
assert chat_id not in channel._subs
|
||||
assert chat_id not in channel._conn_chats.get(connection, set())
|
||||
assert not upload.exists()
|
||||
assert read_transcript_lines(inbound.session_key) == []
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_temporary_chat_rejects_unowned_messages(bus, tmp_path) -> None:
|
||||
@pytest.mark.parametrize("content", ["/goal private", "/trigger later", "/dream"])
|
||||
async def test_temporary_chat_rejects_persistent_commands(bus, tmp_path, content) -> None:
|
||||
sessions = SessionManager(tmp_path)
|
||||
channel = WebSocketChannel(
|
||||
{"enabled": True, "allowFrom": ["*"]},
|
||||
bus,
|
||||
gateway=_basic_handler(
|
||||
bus,
|
||||
session_manager=sessions,
|
||||
workspace_path=tmp_path,
|
||||
),
|
||||
gateway=_basic_handler(bus, session_manager=sessions, workspace_path=tmp_path),
|
||||
)
|
||||
owner = AsyncMock()
|
||||
owner.remote_address = ("127.0.0.1", 5000)
|
||||
intruder = AsyncMock()
|
||||
intruder.remote_address = ("127.0.0.1", 5001)
|
||||
channel._webui_connections.update({owner, intruder})
|
||||
connection = AsyncMock()
|
||||
connection.remote_address = ("127.0.0.1", 5000)
|
||||
|
||||
await channel._dispatch_envelope(
|
||||
owner,
|
||||
"webui-client",
|
||||
{
|
||||
"type": "message",
|
||||
"chat_id": "temporary-owned",
|
||||
"content": "hello",
|
||||
"webui": True,
|
||||
},
|
||||
await channel._dispatch_envelope(connection, "webui-client", {
|
||||
"type": "message",
|
||||
"chat_id": "temporary-command",
|
||||
"content": content,
|
||||
"webui": True,
|
||||
})
|
||||
|
||||
assert bus.publish_inbound.await_count == 0
|
||||
assert sessions.get_cached("websocket:temporary-command") is None
|
||||
assert json.loads(connection.send.await_args.args[0])["detail"] == (
|
||||
"temporary_chat_command_rejected"
|
||||
)
|
||||
|
||||
await channel._dispatch_envelope(
|
||||
intruder,
|
||||
"webui-client",
|
||||
{
|
||||
"type": "message",
|
||||
"chat_id": "temporary-owned",
|
||||
"content": "hello",
|
||||
"webui": True,
|
||||
},
|
||||
)
|
||||
|
||||
assert bus.publish_inbound.await_count == 1
|
||||
assert json.loads(intruder.send.await_args.args[0]) == {
|
||||
"event": "error",
|
||||
"detail": "temporary_chat_not_owned",
|
||||
"chat_id": "temporary-owned",
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_disconnect_discards_temporary_chat(bus, tmp_path) -> None:
|
||||
@@ -331,9 +331,9 @@ async def test_disconnect_discards_temporary_chat(bus, tmp_path) -> None:
|
||||
control = bus.publish_inbound.await_args_list[-1].args[0]
|
||||
assert control.session_key == session_key
|
||||
assert control.metadata[INBOUND_META_RUNTIME_CONTROL] == (
|
||||
RUNTIME_CONTROL_TRANSIENT_SESSION_DISCARD
|
||||
RUNTIME_CONTROL_SESSION_DISCARD
|
||||
)
|
||||
assert sessions.is_transient_active(session_key) is False
|
||||
assert sessions.get_cached(session_key) is None
|
||||
assert "temporary-disconnect" not in channel._subs
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user