Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(desktop-app): recover from a 401 once the recovery cookie went stale (R1-M3)

Diego Zacarias727bfbc558c7
diff --git a/desktop-app/src/__tests__/sandboxUiSessionRefresh.test.ts b/desktop-app/src/__tests__/sandboxUiSessionRefresh.test.ts
index 30ef86e3b..7ce0c78a3 100644
--- a/desktop-app/src/__tests__/sandboxUiSessionRefresh.test.ts
+++ b/desktop-app/src/__tests__/sandboxUiSessionRefresh.test.ts
@@ -525,6 +525,50 @@ describe('recovery after retries ran out or went stale', () => {
expect(reload).toHaveBeenCalledTimes(1)
})
+ it('a 401 once the recovery cookie expired is recovered, even while the timer mint is retrying', async () => {
+ const refresh = vi.fn<SandboxUiRefreshFn>(mintOk)
+ const reload = vi.fn()
+ const args = makeArgs({ refresh, reload })
+ startSandboxUiRefresh(args)
+ handleSandboxUiUnauthorizedNavigation(args.webContentsId)
+ await flush()
+ expect(reload).toHaveBeenCalledTimes(1)
+
+ // The timer mint fails and keeps retrying; the recovery cookie dies.
+ refresh.mockImplementation(mintDown)
+ await advance(SANDBOX_UI_COOKIE_TTL_MS + 5_000)
+ // The user navigates: the dead cookie 401s. Its own mint fails too, but
+ // the retry that lands must still reload the dead page.
+ handleSandboxUiUnauthorizedNavigation(args.webContentsId)
+ refresh.mockImplementation(mintOk)
+ await advance(30_000)
+
+ expect(reload).toHaveBeenCalledTimes(2)
+ })
+
+ it('a 401 once the recovery cookie expired reopens retries that ran out', async () => {
+ const refresh = vi.fn<SandboxUiRefreshFn>(mintOk)
+ const reload = vi.fn()
+ const onError = vi.fn()
+ const args = makeArgs({ refresh, reload, onError })
+ startSandboxUiRefresh(args)
+ handleSandboxUiUnauthorizedNavigation(args.webContentsId)
+ await flush()
+ expect(reload).toHaveBeenCalledTimes(1)
+
+ refresh.mockImplementation(mintDown)
+ await advance(SANDBOX_UI_REFRESH_INTERVAL_MS + 100_000)
+ expect(onError).toHaveBeenCalledTimes(1)
+ const callsWhenExhausted = refresh.mock.calls.length
+
+ refresh.mockImplementation(mintOk)
+ handleSandboxUiUnauthorizedNavigation(args.webContentsId)
+ await flush()
+
+ expect(refresh).toHaveBeenCalledTimes(callsWhenExhausted + 1)
+ expect(reload).toHaveBeenCalledTimes(2)
+ })
+
it('a restore after sleeping through a failure gets a fresh retry window instead of reporting', async () => {
const window = new FakeBrowserWindow()
const refresh = vi.fn<SandboxUiRefreshFn>(mintDown)
@@ -634,6 +678,53 @@ describe('concurrent triggers share one mint', () => {
expect(reload).toHaveBeenCalledTimes(1)
})
+ it('a 401 during the mint of a restore after the recovery cookie expired reloads once it lands', async () => {
+ const window = new FakeBrowserWindow()
+ const { refresh, land } = heldMint()
+ const reload = vi.fn()
+ const args = makeArgs({ parentWindow: window, refresh, reload })
+ startSandboxUiRefresh(args)
+ handleSandboxUiUnauthorizedNavigation(args.webContentsId)
+ land()
+ await flush()
+ expect(reload).toHaveBeenCalledTimes(1)
+
+ window.minimized = true
+ window.emit('minimize')
+ await advance(SANDBOX_UI_COOKIE_TTL_MS + 60_000)
+ window.minimized = false
+ window.emit('restore')
+ await flush()
+ expect(refresh).toHaveBeenCalledTimes(2)
+
+ handleSandboxUiUnauthorizedNavigation(args.webContentsId)
+ land()
+ await flush()
+ expect(refresh).toHaveBeenCalledTimes(2)
+ expect(reload).toHaveBeenCalledTimes(2)
+ })
+
+ it('a 401 under the recovery cookie while a newer mint is in flight reloads once it lands', async () => {
+ const { refresh, land } = heldMint()
+ const reload = vi.fn()
+ const args = makeArgs({ refresh, reload })
+ startSandboxUiRefresh(args)
+ handleSandboxUiUnauthorizedNavigation(args.webContentsId)
+ land()
+ await flush()
+ expect(reload).toHaveBeenCalledTimes(1)
+
+ // The timer mint starts while the recovery cookie is still valid.
+ await advance(SANDBOX_UI_REFRESH_INTERVAL_MS + 1)
+ expect(refresh).toHaveBeenCalledTimes(2)
+
+ handleSandboxUiUnauthorizedNavigation(args.webContentsId)
+ land()
+ await flush()
+ expect(refresh).toHaveBeenCalledTimes(2)
+ expect(reload).toHaveBeenCalledTimes(2)
+ })
+
it('a focus burst neither duplicates nor advances the mint', async () => {
const window = new FakeBrowserWindow()
const { refresh, land } = heldMint()
diff --git a/desktop-app/src/sandboxUiSessionRefresh.ts b/desktop-app/src/sandboxUiSessionRefresh.ts
index 7e49c9394..bceb6d4e0 100644
--- a/desktop-app/src/sandboxUiSessionRefresh.ts
+++ b/desktop-app/src/sandboxUiSessionRefresh.ts
@@ -347,13 +347,22 @@ export async function handleEmbedRefreshRequest(senderId: number): Promise<void>
/**
* The embed's top-level document came back 401 (an expired cookie leaves a
* dead error page with no script to ask for a refresh). Re-mints and reloads,
- * at most once per mint: if the cookie minted for that recovery still yields
- * a 401, the next one is left alone instead of looping.
+ * at most once per mint.
+ *
+ * A 401 under the cookie minted for the previous recovery is ignored only
+ * while that cookie should still be valid and no newer mint is in flight:
+ * only then does it mean the server rejected a fresh cookie, and reloading
+ * again would loop. Once that cookie expired (a failing timer mint, a long
+ * minimize) the 401 is the expected outcome and must still be recovered, or
+ * the embed stays on a dead page; a newer mint in flight brings a different
+ * cookie, so reloading under it is a new attempt rather than a loop.
*/
export function handleSandboxUiUnauthorizedNavigation(webContentsId: number): void {
const ref = senderToRefreshContext.get(webContentsId)
if (!ref || ref.stopped) return
- if (ref.unauthorizedRecoveryMintSeq === ref.mintSeq) return
+ const recoveryCookieRejected =
+ ref.unauthorizedRecoveryMintSeq === ref.mintSeq && !ref.inflight && ref.now() < ref.expiresAt
+ if (recoveryCookieRejected) return
const exhausted = ref.nextAttemptAt === null
// A pending recovery is already being retried — unless its retries ran out,
// in which case this 401 is the only signal left that can reopen them.