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

+90 -4
View File
@@ -13,16 +13,21 @@ from nanobot.webui.client_contract import webui_contract
from nanobot.webui.gateway_services import build_gateway_services
def test_complete_index_and_bidirectional_pages_without_loading_model_messages(history, tmp_path, monkeypatch):
@pytest.mark.parametrize("identified", [False, True])
def test_complete_index_and_bidirectional_pages_without_loading_model_messages(history, tmp_path, monkeypatch, identified):
monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)
key = "websocket:navigation"
for i in range(120):
history.append(key, {"event": "user", "chat_id": "navigation", "text": f"question {i}", "turn_id": f"t{i}"})
history.append(key, {"event": "message", "chat_id": "navigation", "text": "answer " + "x" * 200, "turn_id": f"t{i}"})
input_identity = {"input_id": f"user:t{i}"} if identified else {}
response_identity = {"response_input_ids": [f"user:t{i}"]} if identified else {}
history.append(key, {"event": "user", "chat_id": "navigation", "text": f"question {i}", "turn_id": f"t{i}", **input_identity})
history.append(key, {"event": "message", "chat_id": "navigation", "text": "answer " + "x" * 200, "turn_id": f"t{i}", **response_identity})
history.append(key, {"event": "turn_end", "chat_id": "navigation", "turn_id": f"t{i}"})
with monkeypatch.context() as patch:
patch.setattr("nanobot.session.display_history.HistoryReader.navigation", lambda _: (_ for _ in ()).throw(AssertionError("page scanned full history")))
latest = history.build(key, limit=3)
index = history.prompts(key)
assert len(index) == 120
latest = history.build(key, limit=3)
assert len(latest["events"]) < len(index)
target = index[42]
stats = transcript.TranscriptReplayStats()
@@ -148,6 +153,64 @@ def test_recovered_prompt_identity_time_and_cursor_survive_pages(history, tmp_pa
assert history.prompts(key, session_messages_loader=loader) == index
@pytest.mark.parametrize("second_recorded", [True, False])
@pytest.mark.parametrize("grouped", [True, False])
def test_cross_session_inputs_are_reconciled_by_identity_across_turns(
history, tmp_path, monkeypatch, second_recorded, grouped,
):
monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)
key = "websocket:cross-session-replay"
provenance = [
{"message_id": f"message-{i}", "session": {"id": "handle_" + "1" * 32, "name": "luma"}}
for i in range(2)
]
input_ids = [f"session-message:{source['message_id']}" for source in provenance]
messages = [
{"role": "user", "content": "original question", "input_id": "user:original"},
{"role": "assistant", "content": "original answer"},
]
history.append(key, {"event": "user", "text": "original question", "input_id": "user:original"})
for i, source in enumerate(provenance):
messages.append({"role": "user", "content": "same message", "session_message": source, "input_id": input_ids[i]})
if not grouped or i == 1:
messages.append({"role": "assistant", "content": f"answer {i}"})
if i == 0 or second_recorded:
history.append(key, {"event": "user", "text": "same message", "session_message": source, "input_id": input_ids[i]})
history.append(key, {"event": "message", "text": "original answer", "response_input_ids": ["user:original"]})
assert [prompt["answerPreview"] for prompt in history.prompts(key)] == ["original answer"] + [""] * (2 if second_recorded else 1)
history.append(key, {"event": "turn_end"})
for i in ([1] if grouped else range(2)):
history.append(key, {"event": "message", "text": f"answer {i}", "response_input_ids": input_ids if grouped else [input_ids[i]]})
history.append(key, {"event": "turn_end"})
def loader():
assert not second_recorded, "complete display history must not load model history"
return messages
with monkeypatch.context() as patch:
if second_recorded:
patch.setattr("nanobot.session.display_history.HistoryReader.navigation", lambda _: (_ for _ in ()).throw(AssertionError("page scanned full history")))
latest = history.build(key, limit=1, session_messages_loader=loader)
assert [event["text"] for event in latest["events"] if event["event"] == "user_message"] == (
[] if second_recorded else ["same message"]
)
page = history.build(key, limit=10, session_messages_loader=loader)
users = [event for event in page["events"] if event["event"] == "user_message"]
assert [user["text"] for user in users] == ["original question", "same message", "same message"]
assert [user["provenance"]["session_message"]["message_id"] for user in users[1:]] == [
"message-0", "message-1",
]
prompts = history.prompts(key, session_messages_loader=loader)
assert [prompt["id"] for prompt in prompts] == [user["projection_id"] for user in users]
assert [prompt["answerPreview"] for prompt in prompts] == ["original answer", "answer 1" if grouped else "answer 0", "answer 1"]
assert [prompt["inputId"] for prompt in prompts] == ["user:original", *input_ids]
revision = history.revision(key)
history.append(key, {"event": "user", "text": "same message", "session_message": provenance[0], "input_id": input_ids[0]})
assert history.revision(key) == revision
assert history.build(key, limit=10, session_messages_loader=loader) == page
assert history.build(key, limit=1, session_messages_loader=loader) == latest
@pytest.mark.asyncio
async def test_recovered_public_index_and_page_validators_include_canonical_timestamp(history, tmp_path, monkeypatch):
monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)
@@ -331,3 +394,26 @@ def test_indexed_backfill_preserves_unique_in_suffix_matching(history, tmp_path,
assert [user["text"] for user in users] == (["question 2"] if with_anchor else [])
if with_anchor:
assert users[0]["projection_id"] == prompts[-1]["id"]
def test_recover_multiple_missing_inputs_for_one_answer_across_pages(history, tmp_path, monkeypatch):
"""A grouped answer can stand for multiple inputs absent from display history."""
monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)
key = "websocket:missing-grouped-inputs"
ids = ["session-message:first", "session-message:second"]
messages = [
{"role": "user", "content": "first", "input_id": ids[0]},
{"role": "user", "content": "second", "input_id": ids[1]},
{"role": "assistant", "content": "shared answer"},
]
history.append(key, {"event": "message", "text": "shared answer", "response_input_ids": ids})
history.append(key, {"event": "turn_end"})
def loader():
return messages
page = history.build(key, limit=1, session_messages_loader=loader)
users = [event for event in page["events"] if event["event"] == "user_message"]
assert [event["text"] for event in users] == ["first", "second"]
prompts = history.prompts(key, session_messages_loader=loader)
assert [prompt["inputId"] for prompt in prompts] == ids
assert [prompt["id"] for prompt in prompts] == [event["projection_id"] for event in users]