* 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
245 lines
12 KiB
Python
245 lines
12 KiB
Python
import sqlite3
|
|
from datetime import datetime
|
|
from unittest.mock import AsyncMock, MagicMock
|
|
|
|
import pytest
|
|
|
|
from nanobot.agent.context import TranscriptInput
|
|
from nanobot.agent.loop import AgentLoop
|
|
from nanobot.agent.tools.context import RequestContext
|
|
from nanobot.bus.events import INBOUND_META_TURN_ID, InboundMessage
|
|
from nanobot.bus.queue import MessageBus
|
|
from nanobot.providers.base import GenerationSettings, LLMResponse
|
|
from nanobot.runtime_context import public_history_message
|
|
from nanobot.session.manager import Session, SessionManager
|
|
from nanobot.session.recovery import (
|
|
PENDING_FOLLOWUP_ID_KEY,
|
|
RUNTIME_CHECKPOINT_KEY,
|
|
pending_followups,
|
|
restore_runtime_checkpoint,
|
|
)
|
|
from nanobot.session.session_messages import SESSION_MESSAGE_METADATA_KEY
|
|
from nanobot.webui.recovery import RecoveryCoordinator
|
|
|
|
|
|
def _loop(sessions):
|
|
loop = AgentLoop.__new__(AgentLoop)
|
|
loop.sessions = sessions
|
|
return loop
|
|
|
|
|
|
def _input(text, turn_id):
|
|
return InboundMessage(
|
|
channel="websocket", sender_id="user", chat_id="chat", content=text,
|
|
timestamp=datetime(2026, 10, 18, 19, 0, 0),
|
|
metadata={INBOUND_META_TURN_ID: turn_id},
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize("count", [1, 2])
|
|
@pytest.mark.parametrize("leave_unconsumed", [False, True])
|
|
async def test_late_consumption_commits_history_and_only_its_ids(tmp_path, monkeypatch, count, leave_unconsumed):
|
|
monkeypatch.setattr("nanobot.webui.history.get_webui_dir", lambda: tmp_path / "webui")
|
|
sessions = SessionManager(tmp_path / "workspace", sessions_root=tmp_path / "sessions")
|
|
state = sessions.state
|
|
try:
|
|
draft = await state.get("websocket:chat")
|
|
draft.add_message("user", "initial input")
|
|
await state.prepare_input(draft)
|
|
consumed = []
|
|
for index in range(count):
|
|
message = _input(f"follow-up {index}", f"input-{index}")
|
|
consumed.append(await state.queue_followup(draft.key, message))
|
|
await state.checkpoint_view(draft, {"phase": "final_response"})
|
|
_loop(sessions)._save_turn(draft, [
|
|
*[
|
|
{"role": "user", "content": f"follow-up {index}", PENDING_FOLLOWUP_ID_KEY: identifier}
|
|
for index, identifier in enumerate(consumed)
|
|
],
|
|
{"role": "assistant", "content": "completed answer"},
|
|
], skip=0)
|
|
if leave_unconsumed:
|
|
await state.queue_followup(draft.key, _input("arrived after model boundary", "late-input"))
|
|
await state.finish_turn(draft)
|
|
committed = await state.get(draft.key)
|
|
expected = ["arrived after model boundary"] if leave_unconsumed else []
|
|
assert [item.content for item in pending_followups(committed)] == expected
|
|
assert [item["content"] for item in committed.messages] == [
|
|
"initial input", *[f"follow-up {index}" for index in range(count)], "completed answer",
|
|
]
|
|
finally:
|
|
await state.aclose()
|
|
|
|
reopened = SessionManager(tmp_path / "workspace", sessions_root=tmp_path / "sessions")
|
|
try:
|
|
bus = MessageBus()
|
|
await RecoveryCoordinator(reopened, bus).scan()
|
|
queued = []
|
|
while not bus.inbound.empty():
|
|
queued.append(bus.inbound.get_nowait().content)
|
|
assert queued == expected
|
|
assert (await reopened.state.get(draft.key)).messages == committed.messages
|
|
finally:
|
|
await reopened.state.aclose()
|
|
|
|
|
|
@pytest.mark.parametrize("recover_checkpoint", [False, True])
|
|
async def test_failed_followup_commit_rolls_back_history_and_ack_then_can_retry(tmp_path, recover_checkpoint):
|
|
sessions = SessionManager(tmp_path / "workspace", sessions_root=tmp_path / "sessions")
|
|
state = sessions.state
|
|
try:
|
|
draft = await state.get("websocket:chat")
|
|
draft.add_message("user", "initial input")
|
|
await state.prepare_input(draft)
|
|
identifier = await state.queue_followup(draft.key, _input("consumed input", "follow-up"))
|
|
projection = Session(key=draft.key) if recover_checkpoint else draft
|
|
_loop(sessions)._save_turn(projection, [
|
|
{"role": "user", "content": "consumed input", PENDING_FOLLOWUP_ID_KEY: identifier},
|
|
{"role": "assistant", "content": "completed answer"},
|
|
], skip=0)
|
|
if recover_checkpoint:
|
|
await state.checkpoint_view(draft, {
|
|
"phase": "final_response",
|
|
"preceding_messages": projection.messages[:-1],
|
|
"assistant_message": projection.messages[-1],
|
|
"completed_tool_results": [],
|
|
"pending_tool_calls": [],
|
|
"consumed_followup_ids": list(projection.consumed_followup_ids),
|
|
})
|
|
draft = await state.get(draft.key)
|
|
assert restore_runtime_checkpoint(draft)
|
|
with sqlite3.connect(sessions._store.path) as db:
|
|
db.execute("""CREATE TRIGGER reject_ack BEFORE DELETE ON pending_inputs
|
|
BEGIN SELECT RAISE(ABORT, 'failed follow-up commit'); END""")
|
|
with pytest.raises(sqlite3.IntegrityError, match="failed follow-up commit"):
|
|
await state.finish_turn(draft)
|
|
for stored in (sessions.read_session_snapshot(draft.key), state.peek(draft.key)):
|
|
assert [row["content"] for row in stored.messages] == ["initial input"]
|
|
assert [item.content for item in pending_followups(stored)] == ["consumed input"]
|
|
assert (RUNTIME_CHECKPOINT_KEY in stored.metadata) is recover_checkpoint
|
|
with sqlite3.connect(sessions._store.path) as db:
|
|
db.execute("DROP TRIGGER reject_ack")
|
|
await state.finish_turn(draft)
|
|
committed = sessions.read_session_snapshot(draft.key)
|
|
assert [row["content"] for row in committed.messages] == [
|
|
"initial input", "consumed input", "completed answer",
|
|
]
|
|
assert pending_followups(committed) == []
|
|
finally:
|
|
await state.aclose()
|
|
|
|
|
|
async def test_recovered_input_keeps_admission_identity_and_timestamp(tmp_path):
|
|
sessions = SessionManager(tmp_path / "workspace", sessions_root=tmp_path / "sessions")
|
|
state = sessions.state
|
|
message = _input("queued input", "original-input")
|
|
try:
|
|
draft = await state.get(message.session_key)
|
|
await state.queue_followup(draft.key, message)
|
|
recovered = pending_followups(await state.get(draft.key))[0]
|
|
assert recovered.timestamp == message.timestamp
|
|
assert recovered.metadata[INBOUND_META_TURN_ID] == "original-input"
|
|
assert await _loop(sessions)._persist_user_message_early(recovered, draft)
|
|
row = (await state.get(draft.key)).messages[-1]
|
|
assert row["timestamp"] == message.timestamp.isoformat()
|
|
assert row[INBOUND_META_TURN_ID] == "original-input"
|
|
assert pending_followups(await state.get(draft.key)) == []
|
|
finally:
|
|
await state.aclose()
|
|
|
|
|
|
@pytest.mark.parametrize("interrupt", [False, True], ids=["completed", "checkpoint-recovery"])
|
|
@pytest.mark.parametrize("cross_session", [False, True])
|
|
async def test_injected_inputs_keep_their_own_identity_through_runner_and_commit(tmp_path, monkeypatch, interrupt, cross_session):
|
|
import asyncio
|
|
|
|
provider = MagicMock(aclose=AsyncMock())
|
|
provider.get_default_model.return_value = "test-model"
|
|
provider.generation = GenerationSettings()
|
|
provider.chat_stream_with_retry = AsyncMock(return_value=LLMResponse(content="completed answer"))
|
|
loop = AgentLoop(bus=MessageBus(), provider=provider, workspace=tmp_path / "workspace", model="test-model")
|
|
loop.sessions = SessionManager(tmp_path / "workspace", sessions_root=tmp_path / "sessions")
|
|
# Plain-text inputs are valid without extension-provided runtime context.
|
|
monkeypatch.setattr(loop, "_resolve_runtime_context_for_request", AsyncMock(return_value=[]))
|
|
checkpoint_saved = asyncio.Event()
|
|
checkpoint = loop._set_runtime_checkpoint
|
|
|
|
async def save_checkpoint(session, payload):
|
|
await checkpoint(session, payload)
|
|
checkpoint_saved.set()
|
|
if interrupt:
|
|
await asyncio.Event().wait()
|
|
|
|
monkeypatch.setattr(loop, "_set_runtime_checkpoint", save_checkpoint)
|
|
try:
|
|
draft = await loop.sessions.state.get("websocket:chat")
|
|
draft.add_message(
|
|
"user", "intentionally identical text", **{INBOUND_META_TURN_ID: "initial-input"},
|
|
timestamp="2026-10-18T19:00:00", input_id="user:initial-input",
|
|
)
|
|
await loop.sessions.state.prepare_input(draft)
|
|
queue = asyncio.Queue()
|
|
for turn_id in ("first-input", "second-input"):
|
|
message = _input("intentionally identical text", turn_id)
|
|
if cross_session:
|
|
message.channel, message.input_role = "system", "user"
|
|
message.chat_id = message.session_key_override = draft.key
|
|
message.metadata = {SESSION_MESSAGE_METADATA_KEY: {
|
|
"message_id": turn_id, "created_at_ms": 1, "expect_reply": False,
|
|
"source_handle": "luma", "source_session_key": "websocket:source", "target_session_key": draft.key,
|
|
}}
|
|
identifier = await loop.sessions.state.queue_followup(draft.key, message)
|
|
message.metadata[PENDING_FOLLOWUP_ID_KEY] = identifier
|
|
queue.put_nowait(message)
|
|
runtime = loop.llm_runtime()
|
|
task = asyncio.create_task(loop._run_agent_loop(
|
|
TranscriptInput(history=draft.get_history(), current_message=None),
|
|
runtime=runtime, session=draft, pending_queue=queue,
|
|
request_context=RequestContext(
|
|
channel="websocket", chat_id="chat", session_key=draft.key,
|
|
turn_id="active-run", runtime=runtime,
|
|
),
|
|
))
|
|
try:
|
|
await asyncio.wait_for(checkpoint_saved.wait(), 5)
|
|
if interrupt:
|
|
task.cancel()
|
|
with pytest.raises(asyncio.CancelledError):
|
|
await task
|
|
assert len(pending_followups(await loop.sessions.state.get(draft.key))) == 2
|
|
else:
|
|
result = await task
|
|
loop._save_turn(draft, result.messages, skip=2)
|
|
await loop.sessions.state.finish_turn(draft)
|
|
finally:
|
|
if not task.done():
|
|
task.cancel()
|
|
await asyncio.gather(task, return_exceptions=True)
|
|
provider.chat_stream_with_retry.assert_awaited_once()
|
|
finally:
|
|
await loop.aclose()
|
|
|
|
reopened = SessionManager(tmp_path / "workspace", sessions_root=tmp_path / "sessions")
|
|
try:
|
|
bus = MessageBus()
|
|
recovery = RecoveryCoordinator(reopened, bus)
|
|
await recovery.scan()
|
|
stored = await reopened.state.get(draft.key)
|
|
inputs = [row for row in stored.messages if row.get("role") == "user"]
|
|
assert [public_history_message(row)["content"] for row in inputs] == ["intentionally identical text"] * 3
|
|
if cross_session:
|
|
assert [row["session_message"]["message_id"] for row in inputs[1:]] == ["first-input", "second-input"]
|
|
else:
|
|
assert [row[INBOUND_META_TURN_ID] for row in inputs] == ["initial-input", "first-input", "second-input"]
|
|
prefix = "session-message" if cross_session else "user"
|
|
input_ids = ["user:initial-input", f"{prefix}:first-input", f"{prefix}:second-input"]
|
|
assert [row["input_id"] for row in inputs] == input_ids
|
|
assert stored.messages[-1]["response_input_ids"] == input_ids
|
|
assert [row["timestamp"] for row in inputs] == ["2026-10-18T19:00:00"] * 3
|
|
assert pending_followups(stored) == []
|
|
assert bus.inbound.empty()
|
|
await recovery.scan()
|
|
assert (await reopened.state.get(draft.key)).messages == stored.messages
|
|
finally:
|
|
await reopened.state.aclose()
|