fix(session): prevent consumed follow-ups from replaying after restart (#6299)

* fix(session): atomically acknowledge consumed follow-ups

* refactor(webui): drop legacy follow-up replay compatibility
This commit is contained in:
chengyongru authored and GitHub committed 2026-10-19 11:31:19 +08:00
1 parent 743d40bbba
commit 53e0edf709
17 files changed
+645 -215

No files matched your search

+81
View File
@@ -9,6 +9,7 @@ from websockets.http11 import Request
from nanobot.bus.queue import MessageBus
from nanobot.channels.websocket.runtime import WebSocketConfig
from nanobot.session.manager import SessionManager
from nanobot.webui import transcript
from nanobot.webui.client_contract import webui_contract
from nanobot.webui.gateway_services import build_gateway_services
@@ -122,3 +123,83 @@ def test_summary_deltas_rebuild_after_append_rewrite_eviction_and_delete(tmp_pat
assert transcript.prompt_index_response(key, replaced["revision"])["replaceFrom"] == 0
transcript.delete_webui_transcript(key)
assert transcript.prompt_index_response(key, replaced["revision"])["prompts"] == ()
@pytest.mark.parametrize("input_id", [None, "injected-guidance"])
def test_recovered_prompt_identity_time_and_cursor_survive_pages(tmp_path, monkeypatch, input_id):
monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)
key = "websocket:recovered-navigation"
messages = [{"role": "system", "content": "context"}]
for i in range(2):
user = {"role": "user", "content": "same guidance", "timestamp": f"2026-06-02T10:0{i}:00+00:00"}
if input_id:
user["webui_turn_id"] = f"{input_id}-{i}"
messages.extend([user, {"role": "assistant", "content": f"answer {i}"}])
transcript.append_transcript_object(key, {"event": "message", "text": f"answer {i}", "turn_id": f"execution-{i}"})
transcript.append_transcript_object(key, {"event": "turn_end", "turn_id": f"execution-{i}"})
def loader():
return messages
cold_page = transcript.build_webui_thread_response(key, limit=1, session_messages_loader=loader)
index = transcript.build_prompt_index(key, session_messages_loader=loader)
assert transcript.build_webui_thread_response(key, limit=1, session_messages_loader=loader) == cold_page
assert len(index) == 2
assert index[0]["id"] != index[1]["id"]
assert [prompt["createdAt"] for prompt in index] == [1780394400000, 1780394460000]
for position, prompt in enumerate(index):
page = transcript.build_webui_thread_response(key, after=prompt["cursor"], limit=1, session_messages_loader=loader)
user = next(event for event in page["events"] if event["event"] == "user_message")
assert user["projection_id"] == prompt["id"]
assert user["created_at_ms"] == prompt["createdAt"]
assert user.get("turn_id") == (f"{input_id}-{position}" if input_id else None)
assert transcript.build_prompt_index(key, session_messages_loader=loader) == index
@pytest.mark.asyncio
async def test_recovered_public_index_and_page_validators_include_canonical_timestamp(tmp_path, monkeypatch):
monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)
key = "websocket:canonical-revision"
manager = SessionManager(tmp_path)
session = manager.get_or_create(key)
session.messages = [
{"role": "user", "content": "guidance", "webui_turn_id": "input", "timestamp": "2026-06-02T10:00:00+00:00"},
{"role": "assistant", "content": "answer"},
]
manager.save(session)
for event in ({"event": "message", "text": "answer"}, {"event": "turn_end"}):
transcript.append_transcript_object(key, event)
gateway = build_gateway_services(
config=WebSocketConfig(), bus=MessageBus(), session_manager=manager,
static_dist_path=None, workspace_path=tmp_path, config_path=tmp_path / "config.json",
default_restrict_to_workspace=False, runtime_model_name=None,
runtime_surface="browser", runtime_capabilities_overrides=None,
)
gateway.tokens.api_tokens["test-token"] = time.monotonic() + 60
auth = [("Authorization", "Bearer test-token")]
base = "/api/sessions/websocket%3Acanonical-revision/webui-thread"
initial = {}
for path in (base, base + "/prompts"):
response = await gateway.http._dispatch_session_routes(Request(path, Headers(auth)), path)
assert response.status_code == 200
initial[path] = response
unchanged = await gateway.http._dispatch_session_routes(
Request(path, Headers([*auth, ("If-None-Match", response.headers["ETag"])])), path,
)
assert unchanged.status_code == 304
# A canonical-only update must invalidate the recovered display even when
# transcript artifacts and the session's updated_at are unchanged.
session.messages[0]["timestamp"] = "2026-06-02T10:01:00+00:00"
manager.save(session)
changed = {}
for path, previous in initial.items():
response = await gateway.http._dispatch_session_routes(
Request(path, Headers([*auth, ("If-None-Match", previous.headers["ETag"])])), path,
)
assert response.status_code == 200
assert response.headers["ETag"] != previous.headers["ETag"]
changed[path] = json.loads(response.body)
user = next(event for event in changed[base]["events"] if event["event"] == "user_message")
prompt = changed[base + "/prompts"]["prompts"][0]
assert user["created_at_ms"] == prompt["createdAt"] == 1780394460000
assert user["projection_id"] == prompt["id"]
assert prompt["id"] == json.loads(initial[base + "/prompts"].body)["prompts"][0]["id"]