Files
nanobot/tests/utils/test_webui_turn_helpers.py
T
chengyongru 394a78e82d fix(sessions): preserve cross-session input ownership and history (#6613)
* fix(webui): reconcile cross-session history by message ID

* fix(sessions): preserve target authority and input ownership

Resolve cross-session follow-ups through the target route, carry stable input IDs through admission and recovery, and associate live and replayed answers with the inputs actually consumed.

Keep ordinary input scope isolation and omit historical anomaly repair. Resolves NAN-430.

* fix(sessions): complete input identity integration across boundaries

Carry input identity explicitly in projected input events, keep runner annotations out of public history, and reuse the canonical WebUI session parser during recovery.

Type validated recovery metadata and align existing channel and subagent regressions with explicit response ownership. All nine failed compatibility cases and adjacent recovery coverage pass in a focused 58-test run.

* fix(sessions): unify input recording and block remote commands

* fix(webui): keep input ownership checks page-bounded

Look up referenced inputs through the existing identity index before invoking transcript backfill. Complete pages no longer scan the full prompt history, including answers whose inputs live on earlier pages.

Cover cold-cache pagination and cross-turn ownership in existing regressions. Focused pagination, replay and follow-up recovery checks: 88 passed.

* fix(webui): preserve response ownership during history replay

* fix: resolve strict typing in response replay

* fix: preserve continuation ownership during recovery

* test: diagnose stuck gateway asyncio shutdown

* test: retain gateway thread stack diagnostics

* test(webui): stabilize native autoscroll browser setup
2026-11-11 10:46:44 +08:00

459 lines
17 KiB
Python

import sqlite3
from unittest.mock import AsyncMock, MagicMock
import pytest
from nanobot.agent.tools.context import RequestContext, request_context
from nanobot.agent.turn_delivery import TurnDeliveryFactory
from nanobot.bus.events import InboundMessage
from nanobot.bus.outbound_events import (
GoalStatusEvent,
RetryStatusEvent,
TurnEndEvent,
TurnModelUpdatedEvent,
UserInputEvent,
)
from nanobot.bus.queue import MessageBus
from nanobot.bus.runtime_events import (
RuntimeEventContext,
TurnRuntimeAdmitted,
UserInputAccepted,
)
from nanobot.providers.base import GenerationSettings, LLMUsage
from nanobot.providers.fallback_provider import FallbackModelSelection
from nanobot.session import webui_turns as wth
from nanobot.session.manager import SessionManager
from nanobot.session.session_handles import session_handle_for_name
from nanobot.session.session_messages import SESSION_MESSAGE_METADATA_KEY
from nanobot.utils.llm_runtime import LLMRuntime
from nanobot.webui.metadata import WEBSOCKET_TURN_OWNER_METADATA_KEY
@pytest.fixture(autouse=True)
def _clear_turn_wall_clock() -> None:
wth._WEBSOCKET_ACTIVE_TURNS.clear()
wth._WEBSOCKET_TURN_WALL_STARTED_AT.clear()
wth._WEBSOCKET_TURN_IDS.clear()
wth._WEBSOCKET_TURN_OWNERS.clear()
yield
wth._WEBSOCKET_ACTIVE_TURNS.clear()
wth._WEBSOCKET_TURN_WALL_STARTED_AT.clear()
wth._WEBSOCKET_TURN_IDS.clear()
wth._WEBSOCKET_TURN_OWNERS.clear()
@pytest.mark.asyncio
async def test_publish_turn_run_status_running_records_wall_clock() -> None:
bus = MessageBus()
bus.publish_outbound = AsyncMock()
msg = InboundMessage(
channel="websocket",
sender_id="u",
chat_id="chat-a",
content="hi",
metadata={"webui_turn_id": "turn-a"},
)
await wth.publish_turn_run_status(bus, msg, "running")
assert "chat-a" in wth._WEBSOCKET_TURN_WALL_STARTED_AT
t0 = wth.websocket_turn_wall_started_at("chat-a")
assert isinstance(t0, float)
assert wth.websocket_turn_id("chat-a") == "turn-a"
call = bus.publish_outbound.await_args[0][0]
assert call.chat_id == "chat-a"
assert isinstance(call.event, GoalStatusEvent)
assert call.event.started_at == t0
@pytest.mark.asyncio
async def test_publish_turn_run_status_reuses_explicit_wall_clock() -> None:
bus = MessageBus()
bus.publish_outbound = AsyncMock()
msg = InboundMessage(channel="websocket", sender_id="u", chat_id="chat-a", content="hi")
await wth.publish_turn_run_status(bus, msg, "running", started_at=1234.5)
assert wth.websocket_turn_wall_started_at("chat-a") == 1234.5
call = bus.publish_outbound.await_args[0][0]
assert isinstance(call.event, GoalStatusEvent)
assert call.event.started_at == 1234.5
@pytest.mark.asyncio
async def test_publish_turn_run_status_idle_retains_registry_until_delivery() -> None:
bus = MessageBus()
bus.publish_outbound = AsyncMock()
msg = InboundMessage(
channel="websocket",
sender_id="u",
chat_id="chat-b",
content="hi",
metadata={"webui_turn_id": "turn-b"},
)
await wth.publish_turn_run_status(bus, msg, "running")
assert wth.websocket_turn_wall_started_at("chat-b") is not None
assert wth.websocket_turn_id("chat-b") == "turn-b"
await wth.publish_turn_run_status(bus, msg, "idle")
assert wth.websocket_turn_wall_started_at("chat-b") is not None
assert wth.websocket_turn_id("chat-b") == "turn-b"
async def test_clear_websocket_turn_only_clears_matching_owner() -> None:
bus = MessageBus()
msg = InboundMessage(
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.websocket_turn_wall_started_at("chat-b") == 1234.5
assert wth.websocket_turn_id("chat-b") == "turn-new"
assert wth.clear_websocket_turn_if_current("chat-b", "owner-new") is True
assert wth.websocket_turn_wall_started_at("chat-b") is None
assert wth.websocket_turn_id("chat-b") is None
@pytest.mark.asyncio
async def test_ownerless_turns_receive_distinct_internal_owners() -> None:
bus = MessageBus()
bus.publish_outbound = AsyncMock()
first = InboundMessage(
channel="websocket",
sender_id="u",
chat_id="chat-ownerless",
content="first",
)
second = InboundMessage(
channel="websocket",
sender_id="u",
chat_id="chat-ownerless",
content="second",
)
await wth.publish_turn_run_status(bus, first, "running")
first_owner = first.metadata[WEBSOCKET_TURN_OWNER_METADATA_KEY]
await wth.publish_turn_run_status(bus, second, "running")
second_owner = second.metadata[WEBSOCKET_TURN_OWNER_METADATA_KEY]
assert first_owner != second_owner
assert wth.clear_websocket_turn_if_current("chat-ownerless", first_owner) is True
assert wth._WEBSOCKET_TURN_OWNERS["chat-ownerless"] == second_owner
assert wth.websocket_turn_wall_started_at("chat-ownerless") is not None
assert wth.clear_websocket_turn_if_current("chat-ownerless", second_owner) is True
@pytest.mark.asyncio
async def test_publish_turn_run_status_non_websocket_noop_registry() -> None:
bus = MessageBus()
bus.publish_outbound = AsyncMock()
msg = InboundMessage(channel="telegram", sender_id="u", chat_id="1", content="hi")
await wth.publish_turn_run_status(bus, msg, "running")
assert wth._WEBSOCKET_TURN_WALL_STARTED_AT == {}
assert wth._WEBSOCKET_TURN_IDS == {}
@pytest.mark.asyncio
async def test_fallback_model_is_scoped_to_its_websocket_chat() -> None:
bus = MessageBus()
bus.publish_outbound = AsyncMock()
observer = wth.build_webui_fallback_model_observer(bus)
runtime = LLMRuntime(
provider=MagicMock(),
model="openai/gpt-4.1",
generation=GenerationSettings(),
context_window_tokens=16_000,
model_preset="Deep Research",
)
with request_context(
RequestContext(
channel="websocket",
chat_id="chat-model",
runtime=runtime,
metadata={"webui": True},
)
):
await observer(FallbackModelSelection("deepseek/deepseek-chat", "openai_codex"))
outbound = bus.publish_outbound.await_args.args[0]
assert outbound.channel == "websocket"
assert outbound.chat_id == "chat-model"
assert outbound.metadata == {"webui": True}
assert isinstance(outbound.event, TurnModelUpdatedEvent)
assert outbound.event.model == "deepseek/deepseek-chat"
assert outbound.event.model_preset == "Deep Research"
assert outbound.event.reauth_provider == "openai_codex"
@pytest.mark.asyncio
async def test_coordinator_connections_end_on_scope_exit_and_can_reconnect(tmp_path):
from nanobot.bus.runtime_events import RuntimeModelChanged
bus = MessageBus()
bus.publish_outbound = AsyncMock()
coordinator = wth.WebuiTurnCoordinator(
bus=bus, sessions=SessionManager(tmp_path),
schedule_background=lambda coro: coro.close(),
)
event = RuntimeModelChanged("model", None)
with pytest.raises(RuntimeError, match="shutdown"):
with coordinator.connected():
await bus.publish(event)
raise RuntimeError("shutdown")
bus.publish_outbound.assert_awaited_once()
await bus.publish(event)
bus.publish_outbound.assert_awaited_once()
with coordinator.connected():
bus.publish_nowait(event)
await bus.drain()
assert bus.publish_outbound.await_count == 2
async def test_admitted_runtime_publishes_chat_scoped_model_and_preset(tmp_path) -> None:
bus = MessageBus()
bus.publish_outbound = AsyncMock()
coordinator = wth.WebuiTurnCoordinator(
bus=bus,
sessions=SessionManager(tmp_path),
schedule_background=lambda coro: coro.close(),
)
coordinator.subscribe()
runtime = LLMRuntime(
provider=MagicMock(),
model="openai-codex/gpt-5.6",
generation=GenerationSettings(),
context_window_tokens=262_144,
model_preset="Codex",
)
await bus.publish(
TurnRuntimeAdmitted(
context=RuntimeEventContext(
channel="websocket",
chat_id="chat-model",
session_key="websocket:chat-model",
metadata={"webui": True},
),
runtime=runtime,
)
)
outbound = bus.publish_outbound.await_args.args[0]
assert outbound.channel == "websocket"
assert outbound.chat_id == "chat-model"
assert isinstance(outbound.event, TurnModelUpdatedEvent)
assert outbound.event.model == "openai-codex/gpt-5.6"
assert outbound.event.model_preset == "Codex"
@pytest.mark.asyncio
@pytest.mark.parametrize(("states", "terminal_kind", "expected_kind", "attempts"), [
(("exhausted",), None, "connection", 4),
(("waiting", "recovered"), None, None, None),
(("waiting",), "billing", "billing", None),
(("exhausted", "cleared"), "billing", "billing", None),
])
async def test_turn_retry_cause_survives_only_actual_exhaustion(
tmp_path, states, terminal_kind, expected_kind, attempts,
) -> None:
bus = MessageBus()
coordinator = wth.WebuiTurnCoordinator(
bus=bus, sessions=SessionManager(tmp_path),
schedule_background=lambda coro: coro.close(),
)
with coordinator.connected():
msg = InboundMessage(
channel="websocket", sender_id="user", chat_id="chat-retry", content="hello",
metadata={"webui": True, "webui_turn_id": "turn-1"},
)
delivery = TurnDeliveryFactory(bus).create(msg, msg.session_key)
for state in states:
await delivery.events.emit(RetryStatusEvent(
state=state, attempt=4, max_attempts=4, error_kind="connection",
))
delivery.record_stop_reason("error", failure_error_kind=terminal_kind)
await delivery.complete(None, publish_completion=True)
outbounds = [bus.outbound.get_nowait() for _ in range(bus.outbound_size)]
retry_events = [out.event for out in outbounds if isinstance(out.event, RetryStatusEvent)]
assert [event.state for event in retry_events] == list(states)
completed = next(out.event for out in outbounds if isinstance(out.event, TurnEndEvent))
assert completed.failure_error_kind == expected_kind
assert completed.failure_attempts == attempts
@pytest.mark.asyncio
@pytest.mark.parametrize("write_succeeds", [True, False])
async def test_session_input_is_projected_by_the_webui_coordinator(
tmp_path,
monkeypatch,
write_succeeds,
) -> None:
bus = MessageBus()
bus.publish_outbound = AsyncMock()
sessions = SessionManager(tmp_path)
target_session = sessions.get_or_create("websocket:target")
target_session.metadata["webui"] = True
sessions.save(target_session)
source = session_handle_for_name("websocket:source", "luma")
envelope = {
"message_id": "message-1",
"created_at_ms": 123,
"expect_reply": False,
"source_handle": source.name,
"source_session_key": "websocket:source",
"target_session_key": "websocket:target",
}
append_input = AsyncMock(return_value=write_succeeds)
monkeypatch.setattr(wth.WebUITranscriptRecorder, "append_user_message", append_input)
coordinator = wth.WebuiTurnCoordinator(
bus=bus,
sessions=sessions,
schedule_background=lambda coro: coro.close(),
)
coordinator.subscribe()
await bus.publish(UserInputAccepted(
context=RuntimeEventContext(
channel="system",
chat_id="websocket:target",
session_key="websocket:target",
metadata={SESSION_MESSAGE_METADATA_KEY: envelope},
),
content="Review this",
))
append_input.assert_awaited_once()
if not write_succeeds:
bus.publish_outbound.assert_not_awaited()
return
outbound = bus.publish_outbound.await_args.args[0]
assert outbound.channel == "websocket"
assert outbound.chat_id == "target"
assert isinstance(outbound.event, UserInputEvent)
assert outbound.event.content == "Review this"
assert outbound.event.provenance["session_message"]["session"] == {
"id": source.id,
"name": source.name,
}
@pytest.mark.asyncio
async def test_session_input_rejects_replaced_target_generation(tmp_path) -> None:
bus = MessageBus()
bus.publish_outbound = AsyncMock()
sessions = SessionManager(tmp_path)
target = sessions.get_or_create("websocket:target")
target.metadata["webui"] = True
sessions.save(target)
source = session_handle_for_name("websocket:source", "luma")
coordinator = wth.WebuiTurnCoordinator(
bus=bus, sessions=sessions, schedule_background=lambda coro: coro.close(),
)
coordinator.subscribe()
await bus.publish(UserInputAccepted(
context=RuntimeEventContext(
channel="system", chat_id="websocket:target", session_key="websocket:target",
session_generation="replaced-generation",
metadata={SESSION_MESSAGE_METADATA_KEY: {
"message_id": "message-1", "created_at_ms": 123, "expect_reply": False,
"source_handle": source.name, "source_session_key": "websocket:source",
"target_session_key": "websocket:target",
}},
),
content="Review this",
))
bus.publish_outbound.assert_not_awaited()
records = await sessions.state.project_history("websocket:target", lambda h: list(h.all_facts()))
assert records == []
@pytest.mark.asyncio
async def test_fallback_model_ignores_non_websocket_requests() -> None:
bus = MessageBus()
bus.publish_outbound = AsyncMock()
observer = wth.build_webui_fallback_model_observer(bus)
with request_context(RequestContext(channel="telegram", chat_id="chat-model")):
await observer(FallbackModelSelection("fallback", "openai_codex"))
bus.publish_outbound.assert_not_awaited()
@pytest.mark.parametrize(("goal_status", "outcome"), [
("active", "completed"),
("blocked", "failed"),
])
async def test_turn_end_reads_goal_without_loading_conversation_history(
tmp_path, monkeypatch, goal_status, outcome,
) -> None:
sessions = SessionManager(tmp_path)
key = "websocket:long-chat"
messages = [{"role": "user", "content": f"message {i}"} for i in range(100)]
await sessions.state.import_messages(key, messages)
await sessions.state.update_metadata(key, {
"goal_state": {"status": goal_status, "objective": "Finish the task"},
})
await sessions.state.aclose()
sessions = SessionManager(tmp_path)
def deny_history(action, table, *_args):
return (
sqlite3.SQLITE_DENY
if action == sqlite3.SQLITE_READ and table == "messages"
else sqlite3.SQLITE_OK
)
open_connection = sessions._store._open_connection
def protected_connection(*args, **kwargs):
db = open_connection(*args, **kwargs)
db.set_authorizer(deny_history)
return db
monkeypatch.setattr(sessions._store, "_open_connection", protected_connection)
bus = MessageBus()
coordinator = wth.WebuiTurnCoordinator(
bus=bus, sessions=sessions, schedule_background=lambda coro: coro.close(),
)
msg = InboundMessage(
channel="websocket", sender_id="user", chat_id="long-chat", content="hello",
metadata={"webui": True, "webui_turn_id": "turn-1"},
)
usage = LLMUsage.reported(input_tokens=100, output_tokens=20)
failure_kind = "model" if outcome == "failed" else None
try:
await coordinator.handle_turn_end(
msg, session_key=key, latency_ms=123, usage=usage,
round_usages=(usage,), context_window_tokens=8192, outcome=outcome,
failure_kind=failure_kind,
)
outbound = bus.outbound.get_nowait()
assert outbound.channel == "websocket"
assert outbound.chat_id == "long-chat"
assert outbound.metadata == msg.metadata
event = outbound.event
assert isinstance(event, TurnEndEvent)
assert event.goal_state == {
"active": goal_status == "active", "status": goal_status,
"objective": "Finish the task",
}
assert event.latency_ms == 123
assert event.usage == usage
assert event.round_usages == (usage,)
assert event.context_window_tokens == 8192
assert event.outcome == outcome
assert event.failure_kind == failure_kind
finally:
await sessions.state.aclose()
monkeypatch.setattr(sessions._store, "_open_connection", open_connection)
assert sessions.read_session_snapshot(key).messages == messages