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
This commit is contained in:
chengyongru authored and GitHub committed 2026-11-11 10:46:44 +08:00
1 parent 0ce335bf12
commit 394a78e82d
51 files changed
+1412 -334

No files matched your search

+21 -3
View File
@@ -24,6 +24,7 @@ from nanobot.events import (
AgentEvent,
ContextCompactionEvent,
EventSink,
ResponseInputsEvent,
ResponseSource,
ResponseSourceEvent,
)
@@ -34,6 +35,7 @@ from nanobot.session.keys import (
is_internal_session,
last_channel_from_metadata,
)
from nanobot.session.message_metadata import input_history_metadata
from nanobot.utils.cancellation import shield_and_drain
from nanobot.utils.helpers import strip_think
@@ -110,7 +112,7 @@ class TurnDeliveryFactory:
*,
enable_stream: bool = False,
) -> TurnDelivery:
route = self._default_route(msg, session_key)
route = self.input_route(msg, session_key)
if self.route_policy is not None:
route = self.route_policy(msg, session_key, route)
if not isinstance(cast(object, route), TurnRoute):
@@ -178,7 +180,8 @@ class TurnDeliveryFactory:
return _bind_events(self.bus, TurnRoute(channel, chat_id, metadata), session_key)
@staticmethod
def _default_route(msg: InboundMessage, session_key: str) -> TurnRoute:
def input_route(msg: InboundMessage, session_key: str) -> TurnRoute:
"""Resolve the input's destination without acquiring a turn lifecycle owner."""
if msg.channel != "system":
return TurnRoute(
channel=msg.channel,
@@ -219,6 +222,7 @@ class TurnDelivery:
_stop_reason: str | None = field(init=False, default=None)
_failure_error_kind: str | None = field(init=False, default=None)
_retry_status: RetryStatusEvent | None = field(init=False, default=None)
_response_input_ids: tuple[str, ...] | None = field(init=False, default=None)
_response_source: ResponseSource | None = field(init=False, default=None)
_response_source_seen: bool = field(init=False, default=False)
_response_content: str | None = field(init=False, default=None)
@@ -227,8 +231,13 @@ class TurnDelivery:
def __post_init__(self) -> None:
self.run_id = uuid4().hex
input_id = input_history_metadata(self.input_message)["input_id"]
self._response_input_ids = (input_id,) if input_id else None
self._routed_events = _bind_events(self.bus, self.route, self.session_key)
self.events = EventSink(self._publish_event, self._routed_events.accepts)
self.events = EventSink(
self._publish_event,
lambda event_type: event_type is ResponseInputsEvent or self._routed_events.accepts(event_type),
)
self.delivery_message = dataclasses.replace(
self.input_message,
channel=self.route.channel,
@@ -376,6 +385,10 @@ class TurnDelivery:
**response.metadata,
"response_sources": [dataclasses.asdict(self._response_source)],
})
if self._response_input_ids is not None and (response.channel, response.chat_id) == (self.route.channel, self.route.chat_id):
response = dataclasses.replace(response, metadata={
**response.metadata, "response_input_ids": list(self._response_input_ids),
})
if response.channel != "websocket" or stop_reason != "error":
delivery = DeliveryRequest.from_context(
self.input_message.metadata, self.session_key,
@@ -460,6 +473,9 @@ class TurnDelivery:
return f"{self._stream_base_id}:{self._stream_segment}"
async def _publish_event(self, event: AgentEvent) -> None:
if isinstance(event, ResponseInputsEvent):
self._response_input_ids = event.input_ids
return
if isinstance(event, ResponseSourceEvent):
self._response_source_seen = True
self._response_source = event.source
@@ -484,6 +500,8 @@ class TurnDelivery:
self._stream_sources.append(source)
if self._routed_events.accepts(type(event)):
metadata = deepcopy(self.route.metadata)
if self._response_input_ids is not None:
metadata["response_input_ids"] = list(self._response_input_ids)
if self._response_source_seen and self._routed_events.accepts(ResponseSourceEvent):
metadata["response_sources"] = (
[] if self._stream_source_unknown else