refactor(events): unify scoped runtime notifications across clients (#5670)

* refactor(events): unify scoped runtime notifications across clients

* style(tests): sort auto-compaction event imports

* refactor(events): scope connections and remove retry callback plumbing

* docs: remove runtime notification design write-up

* refactor(bus): route operation events through the message bus

* fix(channels): isolate outbound sends by destination

* style(events): group shared notification imports

* refactor(events): converge runtime and hook delivery on MessageBus

* test(events): migrate fixtures to unified message bus
This commit is contained in:
chengyongru authored and GitHub committed 2026-09-06 02:11:19 +08:00
1 parent 3495281282
commit 192e2e90dc
78 files changed
+1620 -1194

No files matched your search

+19 -19
View File
@@ -4,7 +4,8 @@ from __future__ import annotations
import re
import time
from collections.abc import Awaitable, Callable
from collections.abc import Awaitable, Callable, Generator
from contextlib import contextmanager
from dataclasses import dataclass, replace
from typing import Any, cast
from uuid import uuid4
@@ -13,7 +14,6 @@ from loguru import logger
from nanobot.agent.tools.context import current_request_context
from nanobot.agent.turn_delivery import TurnRoute
from nanobot.bus import progress as bus_progress
from nanobot.bus.events import InboundMessage
from nanobot.bus.outbound_events import (
GoalStateSyncEvent,
@@ -28,7 +28,6 @@ from nanobot.bus.outbound_events import (
from nanobot.bus.queue import MessageBus
from nanobot.bus.runtime_events import (
GoalStateChanged,
RuntimeEventBus,
RuntimeEventContext,
RuntimeModelChanged,
SessionTurnStarted,
@@ -417,14 +416,6 @@ def clear_websocket_turns(chat_id: str) -> None:
_sync_websocket_turn_projection(chat_id)
def build_bus_progress_callback(
bus: MessageBus,
msg: InboundMessage,
) -> Callable[..., Awaitable[None]]:
"""Compatibility wrapper for the generic bus progress callback."""
return bus_progress.build_bus_progress_callback(bus, msg)
async def publish_turn_run_status(
bus: MessageBus,
msg: InboundMessage,
@@ -568,34 +559,34 @@ class WebuiTurnCoordinator:
schedule_background: Callable[[Awaitable[None]], None]
recovery: RecoveryCoordinator | None = None
def subscribe(self, runtime_events: RuntimeEventBus) -> Callable[[], None]:
def subscribe(self) -> Callable[[], None]:
"""Subscribe this coordinator to runtime events."""
unsubscribe = [
runtime_events.subscribe(
self.bus.subscribe(
self._handle_user_input_accepted,
UserInputAccepted,
),
runtime_events.subscribe(
self.bus.subscribe(
self._handle_session_turn_started,
SessionTurnStarted,
),
runtime_events.subscribe(
self.bus.subscribe(
self._handle_run_status_changed,
TurnRunStatusChanged,
),
runtime_events.subscribe(
self.bus.subscribe(
self._handle_turn_runtime_admitted,
TurnRuntimeAdmitted,
),
runtime_events.subscribe(
self.bus.subscribe(
self._handle_turn_completed_event,
TurnCompleted,
),
runtime_events.subscribe(
self.bus.subscribe(
self._handle_goal_state_changed,
GoalStateChanged,
),
runtime_events.subscribe(
self.bus.subscribe(
self._handle_runtime_model_changed,
RuntimeModelChanged,
),
@@ -607,6 +598,15 @@ class WebuiTurnCoordinator:
return _unsubscribe
@contextmanager
def connected(self) -> Generator[None, None, None]:
"""Keep connections alive through shutdown, then release the coordinator."""
disconnect = self.subscribe()
try:
yield
finally:
disconnect()
@staticmethod
def _ctx_msg(ctx: RuntimeEventContext) -> InboundMessage:
return InboundMessage(