fix(models): synchronize canonical runtime selection

This commit is contained in:
Xubin Ren
2026-08-16 11:50:56 +08:00
parent 0a6ee1c539
commit 731b8fc2ed
15 changed files with 265 additions and 16 deletions
+52
View File
@@ -7,7 +7,11 @@ import pytest
from nanobot.agent.tools.context import RequestContext, request_context
from nanobot.bus.events import InboundMessage
from nanobot.bus.outbound_events import GoalStatusEvent, TurnModelUpdatedEvent
from nanobot.bus.runtime_events import RuntimeEventBus, RuntimeEventContext, TurnRuntimeAdmitted
from nanobot.providers.base import GenerationSettings
from nanobot.session import webui_turns as wth
from nanobot.session.manager import SessionManager
from nanobot.utils.llm_runtime import LLMRuntime
from nanobot.webui.metadata import WEBSOCKET_TURN_OWNER_METADATA_KEY
@@ -144,10 +148,18 @@ async def test_fallback_model_is_scoped_to_its_websocket_chat() -> None:
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},
)
):
@@ -159,6 +171,46 @@ async def test_fallback_model_is_scoped_to_its_websocket_chat() -> None:
assert outbound.metadata == {"webui": True}
assert isinstance(outbound.event, TurnModelUpdatedEvent)
assert outbound.event.model == "deepseek/deepseek-chat"
assert outbound.event.model_preset == "Deep Research"
@pytest.mark.asyncio
async def test_admitted_runtime_publishes_chat_scoped_model_and_preset(tmp_path) -> None:
bus = MagicMock()
bus.publish_outbound = AsyncMock()
runtime_events = RuntimeEventBus()
coordinator = wth.WebuiTurnCoordinator(
bus=bus,
sessions=SessionManager(tmp_path),
schedule_background=lambda coro: coro.close(),
)
coordinator.subscribe(runtime_events)
runtime = LLMRuntime(
provider=MagicMock(),
model="openai-codex/gpt-5.6",
generation=GenerationSettings(),
context_window_tokens=262_144,
model_preset="Codex",
)
await runtime_events.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