refactor(webui): clear turns only through the owner registry
This commit is contained in:
1 parent
06dd977838
commit
80c246744f
3 files changed
+41
-40
No files matched your search
@@ -21,6 +21,7 @@ from nanobot.bus.events import (
|
|||||||
INBOUND_META_USER_SHELL,
|
INBOUND_META_USER_SHELL,
|
||||||
OUTBOUND_META_AGENT_UI,
|
OUTBOUND_META_AGENT_UI,
|
||||||
RUNTIME_CONTROL_SESSION_DISCARD,
|
RUNTIME_CONTROL_SESSION_DISCARD,
|
||||||
|
InboundMessage,
|
||||||
OutboundMessage,
|
OutboundMessage,
|
||||||
)
|
)
|
||||||
from nanobot.bus.outbound_events import (
|
from nanobot.bus.outbound_events import (
|
||||||
@@ -93,6 +94,20 @@ from .ws_test_client import http_get as _http_get
|
|||||||
_PORT = 29876
|
_PORT = 29876
|
||||||
|
|
||||||
|
|
||||||
|
async def _register_running_turn(
|
||||||
|
chat_id: str, started_at: float, *, owner: str | None = None,
|
||||||
|
) -> None:
|
||||||
|
metadata = {}
|
||||||
|
if owner:
|
||||||
|
metadata[WEBSOCKET_TURN_OWNER_METADATA_KEY] = owner
|
||||||
|
await wth.publish_turn_run_status(
|
||||||
|
MessageBus(),
|
||||||
|
InboundMessage(channel="websocket", sender_id="user", chat_id=chat_id,
|
||||||
|
content="hello", metadata=metadata),
|
||||||
|
"running", started_at=started_at,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def _thread_conversation_events(body: dict[str, Any]) -> list[dict[str, Any]]:
|
def _thread_conversation_events(body: dict[str, Any]) -> list[dict[str, Any]]:
|
||||||
messages: list[dict[str, Any]] = []
|
messages: list[dict[str, Any]] = []
|
||||||
for event in body["events"]:
|
for event in body["events"]:
|
||||||
@@ -1879,7 +1894,7 @@ async def test_webui_scope_rejects_running_scope_change(bus: MagicMock, tmp_path
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
wth._WEBSOCKET_TURN_WALL_STARTED_AT["chat-running"] = 123.0
|
await _register_running_turn("chat-running", 123.0)
|
||||||
try:
|
try:
|
||||||
await channel._dispatch_envelope(
|
await channel._dispatch_envelope(
|
||||||
conn,
|
conn,
|
||||||
@@ -1897,7 +1912,7 @@ async def test_webui_scope_rejects_running_scope_change(bus: MagicMock, tmp_path
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
finally:
|
finally:
|
||||||
wth._WEBSOCKET_TURN_WALL_STARTED_AT.clear()
|
wth.clear_websocket_turns("chat-running")
|
||||||
|
|
||||||
payload = json.loads(conn.send.await_args.args[0])
|
payload = json.loads(conn.send.await_args.args[0])
|
||||||
assert payload["event"] == "error"
|
assert payload["event"] == "error"
|
||||||
@@ -1943,7 +1958,7 @@ async def test_webui_set_workspace_scope_rejects_running_chat(bus: MagicMock, tm
|
|||||||
)
|
)
|
||||||
conn.send.reset_mock()
|
conn.send.reset_mock()
|
||||||
|
|
||||||
wth._WEBSOCKET_TURN_WALL_STARTED_AT["chat-running"] = 123.0
|
await _register_running_turn("chat-running", 123.0)
|
||||||
try:
|
try:
|
||||||
await channel._dispatch_envelope(
|
await channel._dispatch_envelope(
|
||||||
conn,
|
conn,
|
||||||
@@ -1958,7 +1973,7 @@ async def test_webui_set_workspace_scope_rejects_running_chat(bus: MagicMock, tm
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
finally:
|
finally:
|
||||||
wth._WEBSOCKET_TURN_WALL_STARTED_AT.clear()
|
wth.clear_websocket_turns("chat-running")
|
||||||
|
|
||||||
payload = json.loads(conn.send.await_args.args[0])
|
payload = json.loads(conn.send.await_args.args[0])
|
||||||
assert payload["event"] == "error"
|
assert payload["event"] == "error"
|
||||||
@@ -3231,8 +3246,7 @@ async def test_turn_end_persists_and_conditionally_clears_when_delivery_fails(
|
|||||||
mock_ws.send.side_effect = RuntimeError("fanout failed")
|
mock_ws.send.side_effect = RuntimeError("fanout failed")
|
||||||
chat_id = f"turn-end-failure-{expected_cleared}"
|
chat_id = f"turn-end-failure-{expected_cleared}"
|
||||||
channel._attach(mock_ws, chat_id)
|
channel._attach(mock_ws, chat_id)
|
||||||
wth._WEBSOCKET_TURN_WALL_STARTED_AT[chat_id] = 1234.5
|
await _register_running_turn(chat_id, 1234.5, owner=active_owner)
|
||||||
wth._WEBSOCKET_TURN_OWNERS[chat_id] = active_owner
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
await channel.send(OutboundMessage(
|
await channel.send(OutboundMessage(
|
||||||
@@ -3249,9 +3263,7 @@ async def test_turn_end_persists_and_conditionally_clears_when_delivery_fails(
|
|||||||
if not expected_cleared:
|
if not expected_cleared:
|
||||||
assert wth._WEBSOCKET_TURN_OWNERS[chat_id] == active_owner
|
assert wth._WEBSOCKET_TURN_OWNERS[chat_id] == active_owner
|
||||||
finally:
|
finally:
|
||||||
wth._WEBSOCKET_TURN_WALL_STARTED_AT.pop(chat_id, None)
|
wth.clear_websocket_turns(chat_id)
|
||||||
wth._WEBSOCKET_TURN_IDS.pop(chat_id, None)
|
|
||||||
wth._WEBSOCKET_TURN_OWNERS.pop(chat_id, None)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
@@ -3626,8 +3638,7 @@ async def test_idle_clears_matching_owner_when_delivery_fails() -> None:
|
|||||||
chat_id = "idle-failure"
|
chat_id = "idle-failure"
|
||||||
owner = "owner-idle"
|
owner = "owner-idle"
|
||||||
channel._attach(mock_ws, chat_id)
|
channel._attach(mock_ws, chat_id)
|
||||||
wth._WEBSOCKET_TURN_WALL_STARTED_AT[chat_id] = 1234.5
|
await _register_running_turn(chat_id, 1234.5, owner=owner)
|
||||||
wth._WEBSOCKET_TURN_OWNERS[chat_id] = owner
|
|
||||||
|
|
||||||
await channel.send(OutboundMessage(
|
await channel.send(OutboundMessage(
|
||||||
channel="websocket",
|
channel="websocket",
|
||||||
@@ -3916,10 +3927,10 @@ async def test_hydrate_replays_running_turn() -> None:
|
|||||||
|
|
||||||
wth._WEBSOCKET_TURN_WALL_STARTED_AT.clear()
|
wth._WEBSOCKET_TURN_WALL_STARTED_AT.clear()
|
||||||
try:
|
try:
|
||||||
wth._WEBSOCKET_TURN_WALL_STARTED_AT["chat-1"] = 1_700_000_000.0
|
await _register_running_turn("chat-1", 1_700_000_000.0)
|
||||||
await channel._outbound.hydrate("chat-1")
|
await channel._outbound.hydrate("chat-1")
|
||||||
finally:
|
finally:
|
||||||
wth._WEBSOCKET_TURN_WALL_STARTED_AT.pop("chat-1", None)
|
wth.clear_websocket_turns("chat-1")
|
||||||
|
|
||||||
mock_ws.send.assert_awaited_once()
|
mock_ws.send.assert_awaited_once()
|
||||||
body = json.loads(mock_ws.send.await_args.args[0])
|
body = json.loads(mock_ws.send.await_args.args[0])
|
||||||
@@ -5586,13 +5597,13 @@ def test_sessions_list_includes_active_run_started_at(monkeypatch) -> None:
|
|||||||
)
|
)
|
||||||
channel.gateway.tokens.api_tokens["tok"] = time.monotonic() + 300.0
|
channel.gateway.tokens.api_tokens["tok"] = time.monotonic() + 300.0
|
||||||
|
|
||||||
wth._WEBSOCKET_TURN_WALL_STARTED_AT.clear()
|
wth.clear_websocket_turns("chat-1")
|
||||||
try:
|
try:
|
||||||
wth._WEBSOCKET_TURN_WALL_STARTED_AT["chat-1"] = 1_700_000_000.0
|
asyncio.run(_register_running_turn("chat-1", 1_700_000_000.0))
|
||||||
req = Request("/api/sessions", Headers([("Authorization", "Bearer tok")]))
|
req = Request("/api/sessions", Headers([("Authorization", "Bearer tok")]))
|
||||||
resp = asyncio.run(channel.gateway.http._handle_sessions_list(req))
|
resp = asyncio.run(channel.gateway.http._handle_sessions_list(req))
|
||||||
finally:
|
finally:
|
||||||
wth._WEBSOCKET_TURN_WALL_STARTED_AT.clear()
|
wth.clear_websocket_turns("chat-1")
|
||||||
|
|
||||||
assert resp.status_code == 200
|
assert resp.status_code == 200
|
||||||
body = json.loads(resp.body.decode())
|
body = json.loads(resp.body.decode())
|
||||||
|
|||||||
@@ -390,26 +390,13 @@ def clear_websocket_turn_if_current(
|
|||||||
if not owner:
|
if not owner:
|
||||||
return False
|
return False
|
||||||
turns = _WEBSOCKET_ACTIVE_TURNS.get(chat_id)
|
turns = _WEBSOCKET_ACTIVE_TURNS.get(chat_id)
|
||||||
if turns is not None:
|
if turns is None or owner not in turns:
|
||||||
if owner not in turns:
|
return False
|
||||||
return False
|
if preserve_persistence_failure and turns[owner].transcript_persistence_failed:
|
||||||
if preserve_persistence_failure and turns[owner].transcript_persistence_failed:
|
return False
|
||||||
return False
|
turns.pop(owner)
|
||||||
turns.pop(owner)
|
_sync_websocket_turn_projection(chat_id)
|
||||||
_sync_websocket_turn_projection(chat_id)
|
return True
|
||||||
return True
|
|
||||||
|
|
||||||
# Compatibility for callers/tests that populated the legacy projection
|
|
||||||
# directly before the multi-owner registry existed.
|
|
||||||
if (
|
|
||||||
chat_id in _WEBSOCKET_TURN_WALL_STARTED_AT
|
|
||||||
and _WEBSOCKET_TURN_OWNERS.get(chat_id) == owner
|
|
||||||
):
|
|
||||||
_WEBSOCKET_TURN_WALL_STARTED_AT.pop(chat_id, None)
|
|
||||||
_WEBSOCKET_TURN_IDS.pop(chat_id, None)
|
|
||||||
_WEBSOCKET_TURN_OWNERS.pop(chat_id, None)
|
|
||||||
return True
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
def clear_websocket_turns(chat_id: str) -> None:
|
def clear_websocket_turns(chat_id: str) -> None:
|
||||||
|
|||||||
@@ -102,10 +102,13 @@ async def test_publish_turn_run_status_idle_retains_registry_until_delivery() ->
|
|||||||
assert wth.websocket_turn_id("chat-b") == "turn-b"
|
assert wth.websocket_turn_id("chat-b") == "turn-b"
|
||||||
|
|
||||||
|
|
||||||
def test_clear_websocket_turn_only_clears_matching_owner() -> None:
|
async def test_clear_websocket_turn_only_clears_matching_owner() -> None:
|
||||||
wth._WEBSOCKET_TURN_WALL_STARTED_AT["chat-b"] = 1234.5
|
bus = MessageBus()
|
||||||
wth._WEBSOCKET_TURN_IDS["chat-b"] = "turn-new"
|
msg = InboundMessage(
|
||||||
wth._WEBSOCKET_TURN_OWNERS["chat-b"] = "owner-new"
|
channel="websocket", sender_id="u", chat_id="chat-b", content="hi",
|
||||||
|
metadata={"webui_turn_id": "turn-new", WEBSOCKET_TURN_OWNER_METADATA_KEY: "owner-new"},
|
||||||
|
)
|
||||||
|
await wth.publish_turn_run_status(bus, msg, "running", started_at=1234.5)
|
||||||
|
|
||||||
assert wth.clear_websocket_turn_if_current("chat-b", "owner-old") is False
|
assert wth.clear_websocket_turn_if_current("chat-b", "owner-old") is False
|
||||||
assert wth.websocket_turn_wall_started_at("chat-b") == 1234.5
|
assert wth.websocket_turn_wall_started_at("chat-b") == 1234.5
|
||||||
|
|||||||
Reference in new issue
Block a user