perf(webui): batch startup recovery and authenticate before React (#6708)
This commit is contained in:
1 parent
9652302324
commit
273582916b
26 files changed
+537
-330
No files matched your search
+12
-43
@@ -21,7 +21,6 @@ from nanobot.session.display_history import HistoryFact, HistoryKind, HistoryRea
|
||||
from nanobot.session.manager import Session, SessionManager
|
||||
from nanobot.session.recovery import (
|
||||
KNOWN_CHECKPOINT_PHASES,
|
||||
PENDING_FOLLOWUPS_KEY,
|
||||
PENDING_USER_TURN_KEY,
|
||||
RECOVERY_INBOUND_METADATA_KEY,
|
||||
RECOVERY_METADATA_KEY,
|
||||
@@ -38,7 +37,11 @@ from nanobot.session.recovery import (
|
||||
runtime_checkpoint_is_well_formed,
|
||||
)
|
||||
from nanobot.webui.metadata import WEBUI_TURN_METADATA_KEY
|
||||
from nanobot.webui.session_identity import webui_chat_id, webui_session_key
|
||||
from nanobot.webui.session_identity import (
|
||||
WEBUI_SESSION_STORAGE_PREFIX,
|
||||
webui_chat_id,
|
||||
webui_session_key,
|
||||
)
|
||||
|
||||
|
||||
class RecoveryActionError(ValueError):
|
||||
@@ -85,20 +88,15 @@ class RecoveryCoordinator:
|
||||
from nanobot.webui.history import initialize_history
|
||||
|
||||
await initialize_history(self.sessions)
|
||||
for item in await self.sessions.state.list_sessions():
|
||||
key = item["key"]
|
||||
for key, unfinished in await self.sessions.state.recovery_candidates(WEBUI_SESSION_STORAGE_PREFIX):
|
||||
chat_id = webui_chat_id(key)
|
||||
if chat_id is None:
|
||||
continue
|
||||
metadata_payload = await self.sessions.state.read_metadata(key)
|
||||
raw_metadata = metadata_payload.get("metadata") if metadata_payload else None
|
||||
metadata = cast(dict[str, Any], raw_metadata) if isinstance(raw_metadata, dict) else {}
|
||||
unfinished = await self._has_unfinished_webui_transcript(key)
|
||||
if not self._needs_recovery(metadata) and not unfinished:
|
||||
session = await self.sessions.state.read(key)
|
||||
if session is None:
|
||||
continue
|
||||
session = await self.sessions.state.get(key)
|
||||
try:
|
||||
await self._recover_session(session, chat_id)
|
||||
await self._recover_session(session, chat_id, unfinished=unfinished)
|
||||
await self._close_interrupted_turns(session)
|
||||
await self._requeue_pending_followups(session)
|
||||
except Exception:
|
||||
@@ -115,18 +113,6 @@ class RecoveryCoordinator:
|
||||
await self.sessions.state.restore_interruption(session)
|
||||
await self._publish(chat_id, failed)
|
||||
|
||||
@staticmethod
|
||||
def _needs_recovery(metadata: Mapping[str, Any]) -> bool:
|
||||
if metadata.get(PENDING_USER_TURN_KEY) is True:
|
||||
return True
|
||||
if isinstance(metadata.get(RUNTIME_CHECKPOINT_KEY), dict):
|
||||
return True
|
||||
followups = metadata.get(PENDING_FOLLOWUPS_KEY)
|
||||
if isinstance(followups, list) and len(cast(list[object], followups)) > 0:
|
||||
return True
|
||||
state = recovery_state_from_metadata(metadata)
|
||||
return bool(state and state["status"] in {"resuming", "awaiting_user", "failed"})
|
||||
|
||||
async def admit(self, message: InboundMessage) -> bool:
|
||||
"""Reject stale queued recoveries and let new user input supersede them."""
|
||||
recovery_id = message.metadata.get(RECOVERY_INBOUND_METADATA_KEY)
|
||||
@@ -254,7 +240,7 @@ class RecoveryCoordinator:
|
||||
await self._queue_continuation(session, chat_id, next_state)
|
||||
return next_state
|
||||
|
||||
async def _recover_session(self, session: Session, chat_id: str) -> None:
|
||||
async def _recover_session(self, session: Session, chat_id: str, *, unfinished: bool) -> None:
|
||||
checkpoint_value = cast(object, session.metadata.get(RUNTIME_CHECKPOINT_KEY))
|
||||
checkpoint = (
|
||||
cast(dict[str, Any], checkpoint_value)
|
||||
@@ -284,7 +270,7 @@ class RecoveryCoordinator:
|
||||
)
|
||||
await self.sessions.state.restore_interruption(session)
|
||||
await self._publish(chat_id, next_state)
|
||||
elif await self._has_unfinished_webui_transcript(session.key):
|
||||
elif unfinished:
|
||||
# A normal last-client shutdown can materialize the checkpoint
|
||||
# before the process exits. In that path there is no pending
|
||||
# marker left to classify, but the append-only transcript still
|
||||
@@ -431,20 +417,10 @@ class RecoveryCoordinator:
|
||||
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()]
|
||||
) for turn_id, sequence in history.open_tail_sequences().items() if turn_id not in queued_turns]
|
||||
|
||||
facts = await self.sessions.state.project_history(session.key, completions)
|
||||
if facts:
|
||||
@@ -517,13 +493,6 @@ class RecoveryCoordinator:
|
||||
session.updated_at = datetime.now()
|
||||
return state
|
||||
|
||||
async def _has_unfinished_webui_transcript(self, session_key: str) -> bool:
|
||||
from nanobot.webui.transcript import has_unfinished_transcript_tail
|
||||
|
||||
return await self.sessions.state.project_history(
|
||||
session_key, lambda history: has_unfinished_transcript_tail(session_key, history=history),
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _has_saved_continuation_context(session: Session) -> bool:
|
||||
"""Whether an interrupted turn left model-visible context to continue from."""
|
||||
|
||||
Reference in new issue
Block a user