fix(webui): settle interrupted turns before replaying follow-ups
This commit is contained in:
1 parent
05b66de6e6
commit
27f1a836b5
2 files changed
+75
-5
No files matched your search
@@ -17,6 +17,7 @@ from nanobot.bus.outbound_events import (
|
||||
)
|
||||
from nanobot.bus.queue import MessageBus
|
||||
from nanobot.session import turn_continuation
|
||||
from nanobot.session.display_history import HistoryFact, HistoryKind, HistoryReader
|
||||
from nanobot.session.keys import UNIFIED_SESSION_KEY, last_channel_from_metadata
|
||||
from nanobot.session.manager import Session, SessionManager
|
||||
from nanobot.session.recovery import (
|
||||
@@ -99,6 +100,7 @@ class RecoveryCoordinator:
|
||||
session = await self.sessions.state.get(key)
|
||||
try:
|
||||
await self._recover_session(session, route[1])
|
||||
await self._close_interrupted_turns(session)
|
||||
await self._requeue_pending_followups(session)
|
||||
except Exception:
|
||||
logger.exception("failed to recover interrupted WebUI session {}", session.key)
|
||||
@@ -427,6 +429,31 @@ class RecoveryCoordinator:
|
||||
)
|
||||
)
|
||||
|
||||
async def _close_interrupted_turns(self, session: Session) -> None:
|
||||
# Startup has no live execution owners. Persist their terminal identities
|
||||
# before admitting new work so reconnecting clients can settle old runs.
|
||||
queued_turns = {message.metadata.get(WEBUI_TURN_METADATA_KEY) for message in pending_followups(session)}
|
||||
|
||||
def completions(history: HistoryReader) -> list[HistoryFact]:
|
||||
if not history.turn_count:
|
||||
return []
|
||||
tail = history.read_entries(history.turn_count - 1)
|
||||
if not tail or tail[-1][1].kind is HistoryKind.COMPLETION:
|
||||
# In particular, don't bypass a transcript_incomplete marker.
|
||||
return []
|
||||
sequences: dict[str, int] = {}
|
||||
for _, fact in tail:
|
||||
if fact.turn_id and fact.turn_id not in queued_turns:
|
||||
sequences[fact.turn_id] = max(sequences.get(fact.turn_id, 0), fact.sequence or 0)
|
||||
return [HistoryFact(
|
||||
HistoryKind.COMPLETION, {"outcome": "interrupted"}, turn_id=turn_id,
|
||||
phase="complete", sequence=sequence + 1, created_at_ms=int(datetime.now().timestamp() * 1000),
|
||||
) for turn_id, sequence in sequences.items()]
|
||||
|
||||
facts = await self.sessions.state.project_history(session.key, completions)
|
||||
if facts:
|
||||
await self.sessions.state.append_history(session.key, facts, expected_generation=session.generation)
|
||||
|
||||
async def _requeue_pending_followups(self, session: Session) -> None:
|
||||
for message in pending_followups(session):
|
||||
await self.bus.publish_inbound(message)
|
||||
|
||||
Reference in new issue
Block a user