Skip to content

The agent that grows with you

refactor(desktop): share one resume retry backoff between the two self-heals

Teknium54f6097cd218
diff --git a/apps/desktop/src/app/session/hooks/use-resume-reattach.ts b/apps/desktop/src/app/session/hooks/use-resume-reattach.ts
index 52a947c8831..6ec297ac582 100644
--- a/apps/desktop/src/app/session/hooks/use-resume-reattach.ts
+++ b/apps/desktop/src/app/session/hooks/use-resume-reattach.ts
@@ -4,7 +4,12 @@ import { type MutableRefObject, useEffect, useRef } from 'react'
import { dismissNotification } from '@/store/notifications'
import { $resumeReattachSessionId } from '@/store/session'
-import { type ResumeAttemptOptions, resumeFailedToastId } from './use-session-actions/resume-latches'
+import {
+ MAX_RESUME_RETRIES,
+ type ResumeAttemptOptions,
+ resumeFailedToastId,
+ resumeRetryDelayMs
+} from './use-session-actions/resume-latches'
interface ResumeReattachOptions {
activeSessionId: string | null
@@ -22,17 +27,13 @@ interface ResumeReattachOptions {
selectedStoredSessionIdRef: MutableRefObject<string | null>
}
-const MAX_REATTACH_ATTEMPTS = 4
-const REATTACH_BASE_MS = 1_000
-const REATTACH_MAX_MS = 8_000
-
/**
* Re-attach a routed chat whose resume RPC failed while the REST fallback
* still painted its history ($resumeReattachSessionId). The window reads fine
* but is bound to no runtime, and nothing else resumes it: the route, the
* selection and the gateway are all unchanged. A turn still running there then
* never streams its reply in, and the view keeps "Result unavailable" for good.
- * Retries back off and stop after MAX_REATTACH_ATTEMPTS; the history stays on
+ * Retries back off and stop after MAX_RESUME_RETRIES; the history stays on
* screen either way, and a reconnect or reselect resumes it as before. They
* are quiet: no app-wide toast wipe, no repeat "Resume failed" (resume-latches).
*/
@@ -69,29 +70,26 @@ export function useResumeReattach({
gatewayState !== 'open' ||
!routedSessionId ||
reattachSessionId !== routedSessionId ||
- attempts.count >= MAX_REATTACH_ATTEMPTS
+ attempts.count >= MAX_RESUME_RETRIES
) {
return
}
const sessionId = routedSessionId
- const timer = setTimeout(
- () => {
- // The resume may have landed, or the user moved on, while we waited.
- if (
- creatingSessionRef.current ||
- selectedStoredSessionIdRef.current !== sessionId ||
- activeSessionIdRef.current !== null
- ) {
- return
- }
+ const timer = setTimeout(() => {
+ // The resume may have landed, or the user moved on, while we waited.
+ if (
+ creatingSessionRef.current ||
+ selectedStoredSessionIdRef.current !== sessionId ||
+ activeSessionIdRef.current !== null
+ ) {
+ return
+ }
- attempts.count += 1
- void resumeSession(sessionId, true, undefined, { reattach: true })
- },
- Math.min(REATTACH_MAX_MS, REATTACH_BASE_MS * 2 ** attempts.count)
- )
+ attempts.count += 1
+ void resumeSession(sessionId, true, undefined, { reattach: true })
+ }, resumeRetryDelayMs(attempts.count))
return () => clearTimeout(timer)
}, [
diff --git a/apps/desktop/src/app/session/hooks/use-route-resume.ts b/apps/desktop/src/app/session/hooks/use-route-resume.ts
index 4bc2bb26286..68e501d3d48 100644
--- a/apps/desktop/src/app/session/hooks/use-route-resume.ts
+++ b/apps/desktop/src/app/session/hooks/use-route-resume.ts
@@ -6,7 +6,7 @@ import type { SessionProfileRoute } from '@/store/session-request-router'
import { markSelectionRestore } from '@/store/session-states'
import { useResumeReattach } from './use-resume-reattach'
-import type { ResumeAttemptOptions } from './use-session-actions/resume-latches'
+import { MAX_RESUME_RETRIES, type ResumeAttemptOptions, resumeRetryDelayMs } from './use-session-actions/resume-latches'
interface RouteResumeOptions {
activeSessionId: string | null
@@ -45,14 +45,8 @@ interface RouteResumeOptions {
// (gateway RPC reject + REST fallback failure) on a transiently wedged backend —
// dead provider key, a runaway turn hogging the dispatcher, flaky DNS. Without a
// retry the loader latches forever. We retry with backoff, capped, so a
-// genuinely dead backend doesn't hot-loop the resume.
-const MAX_RESUME_RETRIES = 4
-const RESUME_RETRY_BASE_MS = 1_000
-const RESUME_RETRY_MAX_MS = 8_000
-
-function resumeRetryDelayMs(attempt: number): number {
- return Math.min(RESUME_RETRY_MAX_MS, RESUME_RETRY_BASE_MS * 2 ** attempt)
-}
+// genuinely dead backend doesn't hot-loop the resume (MAX_RESUME_RETRIES /
+// resumeRetryDelayMs, shared with useResumeReattach).
// Resume the routed session with the owner route / snapshot mode carried by an
// explicit resume request for that same session.
diff --git a/apps/desktop/src/app/session/hooks/use-session-actions/resume-latches.ts b/apps/desktop/src/app/session/hooks/use-session-actions/resume-latches.ts
index 5817103ff9b..cb4c79065f7 100644
--- a/apps/desktop/src/app/session/hooks/use-session-actions/resume-latches.ts
+++ b/apps/desktop/src/app/session/hooks/use-session-actions/resume-latches.ts
@@ -22,6 +22,16 @@ export function clearResumeLatches(storedSessionId: string): void {
setResumeReattachSessionId(retire)
}
+// One backoff for both resume self-heals (use-route-resume's stranded retry and
+// use-resume-reattach's quiet re-attach): capped so a dead backend never hot-loops.
+export const MAX_RESUME_RETRIES = 4
+const RESUME_RETRY_BASE_MS = 1_000
+const RESUME_RETRY_MAX_MS = 8_000
+
+export function resumeRetryDelayMs(attempt: number): number {
+ return Math.min(RESUME_RETRY_MAX_MS, RESUME_RETRY_BASE_MS * 2 ** attempt)
+}
+
export const resumeFailedToastId = (storedSessionId: string) => `resume-failed:${storedSessionId}`
/**