* 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
96 lines
3.1 KiB
Python
96 lines
3.1 KiB
Python
from __future__ import annotations
|
|
|
|
import re
|
|
from collections.abc import Mapping
|
|
from typing import TYPE_CHECKING, Any, TypedDict, cast
|
|
|
|
from nanobot.session.session_handles import normalize_session_handle, session_handle_for_name
|
|
|
|
if TYPE_CHECKING:
|
|
from nanobot.bus.events import InboundMessage
|
|
|
|
SESSION_MESSAGE_METADATA_KEY = "_session_message"
|
|
|
|
_MAX_SESSION_KEY_CHARS = 512
|
|
_MESSAGE_ID_RE = re.compile(r"^[A-Za-z0-9._:-]{1,128}$")
|
|
|
|
|
|
class SessionMessageEnvelope(TypedDict):
|
|
message_id: str
|
|
created_at_ms: int
|
|
expect_reply: bool
|
|
source_handle: str
|
|
source_session_key: str
|
|
target_session_key: str
|
|
|
|
|
|
def session_message_envelope(
|
|
metadata: Mapping[str, Any] | None,
|
|
) -> SessionMessageEnvelope | None:
|
|
"""Read a validated envelope from request or persisted-message metadata."""
|
|
if not isinstance(metadata, Mapping):
|
|
return None
|
|
raw = metadata.get(SESSION_MESSAGE_METADATA_KEY)
|
|
if not isinstance(raw, Mapping):
|
|
return None
|
|
data = cast(Mapping[str, object], raw)
|
|
message_id = data.get("message_id")
|
|
created_at_ms = data.get("created_at_ms")
|
|
expect_reply = data.get("expect_reply")
|
|
source_handle_value = data.get("source_handle")
|
|
source_session_key = _session_key(data.get("source_session_key"))
|
|
target_session_key = _session_key(data.get("target_session_key"))
|
|
try:
|
|
source_handle = (
|
|
normalize_session_handle(source_handle_value)
|
|
if isinstance(source_handle_value, str)
|
|
else None
|
|
)
|
|
except ValueError:
|
|
source_handle = None
|
|
if (
|
|
not isinstance(message_id, str)
|
|
or _MESSAGE_ID_RE.fullmatch(message_id) is None
|
|
or not isinstance(created_at_ms, int)
|
|
or isinstance(created_at_ms, bool)
|
|
or created_at_ms < 0
|
|
or not isinstance(expect_reply, bool)
|
|
or source_handle is None
|
|
or source_session_key is None
|
|
or target_session_key is None
|
|
):
|
|
return None
|
|
return {
|
|
"message_id": message_id,
|
|
"created_at_ms": created_at_ms,
|
|
"expect_reply": expect_reply,
|
|
"source_handle": source_handle,
|
|
"source_session_key": source_session_key,
|
|
"target_session_key": target_session_key,
|
|
}
|
|
|
|
|
|
def session_message_input(message: InboundMessage) -> SessionMessageEnvelope | None:
|
|
envelope = session_message_envelope(message.metadata)
|
|
if (
|
|
message.channel == "system" and message.is_user_input and envelope is not None
|
|
and envelope["target_session_key"] == message.session_key
|
|
):
|
|
return envelope
|
|
return None
|
|
|
|
|
|
def session_message_public_metadata(envelope: SessionMessageEnvelope) -> dict[str, Any]:
|
|
"""Return stable provenance without private routing identifiers."""
|
|
source = session_handle_for_name(envelope["source_session_key"], envelope["source_handle"])
|
|
return {"message_id": envelope["message_id"], "session": source.public_payload()}
|
|
|
|
|
|
def _session_key(value: object) -> str | None:
|
|
if not isinstance(value, str):
|
|
return None
|
|
normalized_key = value.strip()
|
|
if not normalized_key or len(normalized_key) > _MAX_SESSION_KEY_CHARS:
|
|
return None
|
|
return normalized_key
|