Skip to content

The agent that grows with you

fix(desktop): keep Cloud portal sign-in alive through redirect aborts (#123091)

Gillea7059225d570
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts
index 0291d35349c..e7983b7205e 100644
--- a/apps/desktop/electron/main.ts
+++ b/apps/desktop/electron/main.ts
@@ -353,6 +353,7 @@ import { runNativeLogin } from './native-oauth-login'
import { loadNativeTokenSet, type NativeTokenStoreIo, persistNativeTokenSet } from './native-token-store'
import { planNoConsoleGitSpawn, setNoConsoleGitRoots, windowsGitHost } from './no-console-git'
import { registerNativeNotifications } from './notification-ipc'
+import { isExpectedOauthNavigationAbort } from './oauth-navigation'
import { serializeJsonBody, setJsonRequestHeaders } from './oauth-net-request'
import { LEGACY_OAUTH_PARTITION, resolveOauthPartition } from './oauth-partition'
import { mintGatewayWsTicket as mintOauthGatewayWsTicket, requestWithOauthFallback } from './oauth-rest-request'
@@ -7595,7 +7596,7 @@ function openOauthLoginWindow(baseUrl, { silent = false, background = false } =
win.loadURL(loginUrl, oauthLoginLoadUrlOptions(loginHeaders)).catch(error => {
// Callback navigation can abort the original load after setting cookies.
// Keep the bounded hidden recovery alive long enough to observe them.
- if (background && (Number(error?.code) === -3 || /\bERR_ABORTED\b/.test(String(error?.message)))) {
+ if (background && isExpectedOauthNavigationAbort(error)) {
void checkCookie()
return
diff --git a/apps/desktop/electron/oauth-navigation.test.ts b/apps/desktop/electron/oauth-navigation.test.ts
new file mode 100644
index 00000000000..ac8ddc6fc8a
--- /dev/null
+++ b/apps/desktop/electron/oauth-navigation.test.ts
@@ -0,0 +1,18 @@
+import { describe, expect, it } from 'vitest'
+
+import { isExpectedOauthNavigationAbort } from './oauth-navigation'
+
+describe('isExpectedOauthNavigationAbort', () => {
+ it('treats Electron ERR_ABORTED callback navigation as non-fatal', () => {
+ const error = Object.assign(new Error("ERR_ABORTED (-3) loading 'https://agent.example.com/auth/callback?code=redacted'"), {
+ code: -3
+ })
+
+ expect(isExpectedOauthNavigationAbort(error)).toBe(true)
+ })
+
+ it('does not suppress genuine gateway load failures', () => {
+ expect(isExpectedOauthNavigationAbort(new Error('ERR_NAME_NOT_RESOLVED (-105)'))).toBe(false)
+ expect(isExpectedOauthNavigationAbort(new Error('HTTP 502 from gateway'))).toBe(false)
+ })
+})
diff --git a/apps/desktop/electron/oauth-navigation.ts b/apps/desktop/electron/oauth-navigation.ts
new file mode 100644
index 00000000000..b0f2b7719bf
--- /dev/null
+++ b/apps/desktop/electron/oauth-navigation.ts
@@ -0,0 +1,10 @@
+export function isExpectedOauthNavigationAbort(error: unknown): boolean {
+ if (!error || typeof error !== 'object') {
+ return false
+ }
+
+ const code = 'code' in error ? Number((error as { code?: unknown }).code) : Number.NaN
+ const message = error instanceof Error ? error.message : String(error)
+
+ return code === -3 || /\bERR_ABORTED\b|\(-3\)/.test(message)
+}
diff --git a/apps/desktop/electron/portal-session.test.ts b/apps/desktop/electron/portal-session.test.ts
new file mode 100644
index 00000000000..eb621957717
--- /dev/null
+++ b/apps/desktop/electron/portal-session.test.ts
@@ -0,0 +1,75 @@
+import { EventEmitter } from 'node:events'
+
+import type { BrowserWindow, Session } from 'electron'
+import { expect, test, vi } from 'vitest'
+
+import { createPortalSession } from './portal-session'
+
+vi.mock('./window-renderer-lifecycle', () => ({
+ installWindowRendererLifecycle: () => {}
+}))
+
+class PortalWindow extends EventEmitter {
+ readonly webContents = new EventEmitter()
+ readonly loadURL: ReturnType<typeof vi.fn>
+ private destroyed = false
+
+ constructor(error: Error) {
+ super()
+ this.loadURL = vi.fn().mockRejectedValue(error)
+ }
+
+ isDestroyed() {
+ return this.destroyed
+ }
+
+ destroy() {
+ this.destroyed = true
+ this.emit('closed')
+ }
+}
+
+function portalWithRejectedInitialLoad(error: Error) {
+ const win = new PortalWindow(error)
+ const cookies: { name: string; value: string }[] = []
+ const session = { cookies: { get: async () => cookies } } as unknown as Session
+
+ const portal = createPortalSession({
+ isReady: () => true,
+ getOauthSession: () => session,
+ resolvePortalBaseUrl: () => 'https://portal.example.test',
+ warmOauthCookieStore: async () => {},
+ createWindow: () => win as unknown as BrowserWindow,
+ rememberLog: () => {}
+ })
+
+ return { portal, win, cookies }
+}
+
+test('a superseded portal navigation stays open until the new access cookie lands', async () => {
+ const error = Object.assign(new Error('ERR_ABORTED (-3) loading the portal'), { code: -3 })
+ const { portal, win, cookies } = portalWithRejectedInitialLoad(error)
+
+ const result = portal.openPortalLoginWindow().then(
+ () => 'landed',
+ (failure: Error) => failure.message
+ )
+
+ await vi.waitFor(() => expect(win.loadURL).toHaveBeenCalledOnce())
+ await new Promise<void>(resolve => setImmediate(resolve))
+ expect(win.isDestroyed()).toBe(false)
+
+ cookies.push({ name: 'nas-session', value: 'new-access' })
+ win.webContents.emit('did-redirect-navigation')
+
+ expect(await result).toBe('landed')
+ expect(win.isDestroyed()).toBe(true)
+})
+
+test('a real portal load failure still ends sign-in', async () => {
+ const error = Object.assign(new Error('ERR_FAILED (-2) loading the portal'), { code: -2 })
+ const { portal, win } = portalWithRejectedInitialLoad(error)
+
+ await expect(portal.openPortalLoginWindow()).rejects.toThrow(error.message)
+ expect(win.isDestroyed()).toBe(true)
+})
diff --git a/apps/desktop/electron/portal-session.ts b/apps/desktop/electron/portal-session.ts
index c8b9ce78a98..2da2d444dac 100644
--- a/apps/desktop/electron/portal-session.ts
+++ b/apps/desktop/electron/portal-session.ts
@@ -1,5 +1,6 @@
import type { BrowserWindow, BrowserWindowConstructorOptions, Session } from 'electron'
+import { isExpectedOauthNavigationAbort } from './oauth-navigation'
import { cookiesHavePortalSession, portalAccessCookies, type PortalCookie } from './portal-cookies'
import { installWindowRendererLifecycle } from './window-renderer-lifecycle'
@@ -175,7 +176,17 @@ export function createPortalSession({
}
win.on('closed', () => finish('closed'))
- win.loadURL(portalBaseUrl).catch(error => finish(error instanceof Error ? error : new Error(String(error))))
+ win.loadURL(portalBaseUrl).catch(error => {
+ // A portal redirect can supersede the initial load. Keep watching the
+ // cookie jar instead of destroying the window before sign-in completes.
+ if (isExpectedOauthNavigationAbort(error)) {
+ void checkCookie()
+
+ return
+ }
+
+ finish(error instanceof Error ? error : new Error(String(error)))
+ })
})
}