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

+2
View File
@@ -149,6 +149,8 @@ class Session:
# Owner-private committed snapshot. Never mutate or expose its records as draft data.
_baseline: Session | None = field(default=None, repr=False, compare=False)
# Explicit draft command, committed with history against the latest owner journal.
consumed_followup_ids: frozenset[str] = field(default_factory=frozenset, repr=False, compare=False)
persisted: bool = field(default=False, repr=False, compare=False)
revision: int = field(default=0, repr=False, compare=False)
generation: str = field(default_factory=lambda: uuid4().hex, repr=False, compare=False)
+5
View File
@@ -88,6 +88,7 @@ def record_pending_followup(session: Session, message: InboundMessage) -> str |
"sender_id": message.sender_id,
"chat_id": message.chat_id,
"content": message.content,
"timestamp": message.timestamp.isoformat(),
"media": list(message.media or []),
"metadata": metadata,
}
@@ -129,12 +130,16 @@ def pending_followups(session: Session) -> list[InboundMessage]:
if isinstance(media_value, list)
else []
)
timestamp = record.get("timestamp")
# Older journals did not retain admission time; retain their existing recovery policy.
received_at = datetime.fromisoformat(timestamp) if isinstance(timestamp, str) else datetime.now()
messages.append(
InboundMessage(
channel="websocket",
sender_id=sender_id,
chat_id=chat_id,
content=content,
timestamp=received_at,
media=media,
metadata={**cast(dict[str, Any], metadata), PENDING_FOLLOWUP_ID_KEY: followup_id},
session_key_override=session.key,
+6 -8
View File
@@ -504,6 +504,8 @@ class SessionState:
setattr(draft, item.name, getattr(fresh, item.name))
def _apply_turn(self, draft: Session) -> tuple[Session, int]:
from nanobot.session.recovery import PENDING_FOLLOWUPS_KEY, acknowledge_pending_followups
baseline = draft._baseline # pyright: ignore[reportPrivateUsage]
if baseline is None:
raise ValueError("session changes require an owner-issued draft")
@@ -517,20 +519,15 @@ class SessionState:
):
raise SessionConflictError("session was reset")
for key in baseline.metadata.keys() | draft.metadata.keys():
if key == PENDING_FOLLOWUPS_KEY:
# queue_followup owns this journal; a draft may predate its inputs.
continue
missing = object()
before = baseline.metadata.get(key, missing)
after = draft.metadata.get(key, missing)
if before == after:
continue
existing = current.metadata.get(key, missing)
if key == "pending_user_followups":
old_records = cast(list[dict[str, Any]], before) if isinstance(before, list) else []
new_records = cast(list[dict[str, Any]], after) if isinstance(after, list) else []
removed_ids = {item["id"] for item in old_records} - {item["id"] for item in new_records}
from nanobot.session.recovery import acknowledge_pending_followups
acknowledge_pending_followups(current, removed_ids)
continue
if existing != before and existing != after:
raise SessionConflictError(f"turn metadata changed: {key}")
if after is missing:
@@ -554,6 +551,7 @@ class SessionState:
if existing != before and existing != after:
raise SessionConflictError(f"session {name} changed")
setattr(current, name, deepcopy(after))
acknowledge_pending_followups(current, draft.consumed_followup_ids)
current.policy = draft.policy
current.updated_at = max(current.updated_at, draft.updated_at)
return current, self._changed_start(before_messages, current.messages)