The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): recover checkpoint restore and tile actions after a stale session drop
bbc1305b645d08
diff --git a/apps/desktop/src/app/contrib/hooks/use-session-tile-delegate.ts b/apps/desktop/src/app/contrib/hooks/use-session-tile-delegate.tsindex 57bfa92bfa2..5770dfb85b8 100644--- a/apps/desktop/src/app/contrib/hooks/use-session-tile-delegate.ts+++ b/apps/desktop/src/app/contrib/hooks/use-session-tile-delegate.ts@@ -6,6 +6,7 @@ import { publishSessionState, setSessionTileDelegate } from '@/store/session-staimport type { SessionResumeResponse } from '@/types/hermes'import type { usePromptActions } from '../../session/hooks/use-prompt-actions'+import { withSessionNotFoundResume } from '../../session/hooks/use-prompt-actions/utils'import { resolveSessionProfile } from '../../session/hooks/use-session-actions/utils'import type { useSessionStateCache } from '../../session/hooks/use-session-state-cache'import type { GatewayRequester } from '../types'@@ -41,6 +42,35 @@ export function useSessionTileDelegate({updateSessionState}: SessionTileDelegateParams): void {useEffect(() => {+ // A tile's runtime binding can die the same way the foreground's does+ // (sleep/wake, backend restart). The cache maps stored -> runtime, so walk+ // it backwards to find the durable id this runtime belongs to.+ const storedSessionIdForRuntime = (runtimeId: string): null | string => {+ const cached = sessionStateByRuntimeIdRef.current.get(runtimeId)?.storedSessionId++ if (cached) {+ return cached+ }++ for (const [storedId, mapped] of runtimeIdByStoredSessionIdRef.current) {+ if (mapped === runtimeId) {+ return storedId+ }+ }++ return null+ }++ // Repoint the stored -> runtime mapping at the recovered id so subsequent+ // tile actions use the live binding instead of re-recovering every call.+ const rebindTileRuntime = (deadRuntimeId: string) => (recoveredId: string) => {+ const storedId = storedSessionIdForRuntime(deadRuntimeId)++ if (storedId) {+ runtimeIdByStoredSessionIdRef.current.set(storedId, recoveredId)+ }+ }+setSessionTileDelegate({archiveSession: async storedSessionId => {await archiveSession(storedSessionId)@@ -55,7 +85,12 @@ export function useSessionTileDelegate({await executeSlashCommand(rawCommand, { sessionId })},interruptSession: async runtimeId => {- await requestGateway('session.interrupt', { session_id: runtimeId })+ await withSessionNotFoundResume(+ runtimeId,+ storedSessionIdForRuntime(runtimeId),+ liveId => requestGateway('session.interrupt', { session_id: liveId }),+ { requestGateway, onRecovered: rebindTileRuntime(runtimeId) }+ )},resumeTile: async storedSessionId => {const existing = runtimeIdByStoredSessionIdRef.current.get(storedSessionId)@@ -104,7 +139,12 @@ export function useSessionTileDelegate({return runtimeId},submitToSession: async (runtimeId, text) => {- await requestGateway('prompt.submit', { session_id: runtimeId, text }, PROMPT_SUBMIT_REQUEST_TIMEOUT_MS)+ await withSessionNotFoundResume(+ runtimeId,+ storedSessionIdForRuntime(runtimeId),+ liveId => requestGateway('prompt.submit', { session_id: liveId, text }, PROMPT_SUBMIT_REQUEST_TIMEOUT_MS),+ { requestGateway, onRecovered: rebindTileRuntime(runtimeId) }+ )},updateSession: (runtimeId, updater) => updateSessionState(runtimeId, updater)})diff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/rewind.ts b/apps/desktop/src/app/session/hooks/use-prompt-actions/rewind.tsindex 6fd1d9c6cd7..c2c2560387d 100644--- a/apps/desktop/src/app/session/hooks/use-prompt-actions/rewind.ts+++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/rewind.ts@@ -20,7 +20,8 @@ import {isSessionBusyError,visibleUserIndexAtOrdinal,visibleUserOrdinal,- withSessionBusyRetry+ withSessionBusyRetry,+ withSessionNotFoundResume} from './utils'type RequestGateway = <T = unknown>(method: string, params?: Record<string, unknown>, timeoutMs?: number) => Promise<T>@@ -51,33 +52,61 @@ export function truncateSubmitParams(truncateOrdinal: number | undefined): Recor* (interrupting an idle agent can leave a stale interrupt flag that cancels the* fresh turn); live/stuck turns interrupt first, and a raced "session busy"* response interrupts + retries through the shared busy gate.+ *+ * A rewind runs right after `cancelRun`, and interrupting can drop the+ * gateway's in-memory session — so the follow-up submit hits a dead runtime id+ * and used to surface a bare "session not found" ("Restore checkpoint" failing+ * after a stop). Pass `recovery` so a stale id re-registers and retries like+ * every other session-scoped RPC.*/export async function runRewindSubmit(requestGateway: RequestGateway,sessionId: string,text: string,truncateOrdinal: number | undefined,- interruptFirst: boolean+ interruptFirst: boolean,+ recovery?: { storedSessionId?: null | string; onSessionRecovered?: (sessionId: string) => void }): Promise<void> {+ // Recovery may rebind the live id mid-flight; interrupt/submit must both+ // follow it rather than pinning the dead one.+ let liveSessionId = sessionId+const interrupt = async () => {try {- await requestGateway('session.interrupt', { session_id: sessionId })+ await requestGateway('session.interrupt', { session_id: liveSessionId })} catch {// Best-effort. The submit path still gates on the gateway state.}}- const submit = () =>+ const submitFor = (targetId: string) =>requestGateway('prompt.submit',{- session_id: sessionId,+ session_id: targetId,text,...truncateSubmitParams(truncateOrdinal)},PROMPT_SUBMIT_REQUEST_TIMEOUT_MS)+ const submit = async () => {+ const { sessionId: usedId } = await withSessionNotFoundResume(+ liveSessionId,+ recovery?.storedSessionId,+ submitFor,+ {+ requestGateway,+ onRecovered: recoveredId => {+ liveSessionId = recoveredId+ recovery?.onSessionRecovered?.(recoveredId)+ }+ }+ )++ liveSessionId = usedId+ }+if (interruptFirst) {await interrupt()}