Skip to content

The agent that grows with you

refactor(desktop): one readStatusCode beside httpStatusError; log the reauth latch once

kshitijk4poor4f137c73fcd6
diff --git a/apps/desktop/electron/api-transport.ts b/apps/desktop/electron/api-transport.ts
index 8c2a5dfe09b..310c24608d1 100644
--- a/apps/desktop/electron/api-transport.ts
+++ b/apps/desktop/electron/api-transport.ts
@@ -189,6 +189,11 @@ function httpStatusError(statusCode, text, statusMessage?) {
return error
}
+/** Read side of httpStatusError: the HTTP status an error carries, NaN when it carries none. */
+function readStatusCode(error: unknown): number {
+ return Number(error && typeof error === 'object' ? (error as { statusCode?: unknown }).statusCode : NaN)
+}
+
export {
destroyKeepaliveAgents,
downloadAgentFor,
@@ -196,6 +201,7 @@ export {
isIdempotentMethod,
isTransientTransportError,
jsonAgentFor,
+ readStatusCode,
shouldRetryRequest,
withRetry
}
diff --git a/apps/desktop/electron/connection-config.ts b/apps/desktop/electron/connection-config.ts
index 133f646cb6f..226c4d3e6e9 100644
--- a/apps/desktop/electron/connection-config.ts
+++ b/apps/desktop/electron/connection-config.ts
@@ -34,6 +34,8 @@
// (POST /api/auth/ws-ticket), so the session is still LIVE even with no
// AT cookie. A liveness check that looked only at the AT cookie would
// force a needless full re-login every ~15 min — hence cookiesHaveLiveSession.
+import { readStatusCode } from './api-transport'
+
const AT_COOKIE_VARIANTS = ['__Host-hermes_session_at', '__Secure-hermes_session_at', 'hermes_session_at']
const RT_COOKIE_VARIANTS = ['__Host-hermes_session_rt', '__Secure-hermes_session_rt', 'hermes_session_rt']
@@ -121,7 +123,7 @@ function isGatewayAuthRejection(error) {
return true
}
- const statusCode = Number(error && typeof error === 'object' ? (error as any).statusCode : NaN)
+ const statusCode = readStatusCode(error)
return statusCode === 401 || statusCode === 403
}
@@ -149,7 +151,7 @@ function gatewayTicketFailure(error, authMessage, transportMessage) {
// the renderer overlay depend on it surviving the ticket-error wrapper. Auth
// semantics are unchanged: 401/403 route to reauth, 5xx stays a transport
// failure, everything else keeps current behavior.
- const sourceStatus = Number(error && typeof error === 'object' ? (error as any).statusCode : NaN)
+ const sourceStatus = readStatusCode(error)
if (Number.isInteger(sourceStatus)) {
;(err as any).statusCode = sourceStatus
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts
index b6adaf3a2e1..a9593d757e9 100644
--- a/apps/desktop/electron/main.ts
+++ b/apps/desktop/electron/main.ts
@@ -30,7 +30,7 @@ import {
} from 'electron'
import { classifyActiveRuntime } from './active-runtime-state'
-import { destroyKeepaliveAgents, downloadAgentFor, httpStatusError, jsonAgentFor, withRetry } from './api-transport'
+import { destroyKeepaliveAgents, downloadAgentFor, httpStatusError, jsonAgentFor, readStatusCode, withRetry } from './api-transport'
import { appIconCandidates, resolveAppIcon } from './app-icon'
import { stopBackendChild as stopBackendChildImpl, stopBackendTreesForUpdate } from './backend-child'
import {
@@ -2239,28 +2239,13 @@ function abandonFirstRunSetupChoiceForRemoteApply() {
return resumedGatedConnection
}
-// The latched reauth failure whose hold has already been logged, so a burst of
-// dropped updates from one in-flight sibling attempt logs once, not per event.
-let bootProgressHeldFor = null
-
function updateBootProgress(update, options: { allowDecrease?: boolean } = {}) {
- // A latched CONFIRMED reauth rejection owns the boot surface until a
- // recovery path clears it. Updates that are not a re-emit of that failure —
- // a running:true phase or cleared error from an attempt already in flight
- // when the latch closed, or an unrelated sibling failure that would flip
- // retryable back on — must not reach the renderer, or the overlay's Sign in
- // button flickers away again (#95701).
+ // A latched reauth rejection owns the boot surface until a recovery path
+ // clears it; see shouldHoldBootProgressForReauth (#95701).
if (shouldHoldBootProgressForReauth(remoteReauthFailure ? remoteReauthFailure.message : null, update)) {
- if (bootProgressHeldFor !== remoteReauthFailure) {
- bootProgressHeldFor = remoteReauthFailure
- rememberLog('[boot] remote reauth latched: holding the recovery overlay against a stale boot-progress update')
- }
-
return
}
- bootProgressHeldFor = null
-
const nextProgressRaw =
typeof update.progress === 'number' ? clampBootProgress(update.progress) : bootProgressState.progress
@@ -13366,11 +13351,7 @@ async function startHermes() {
// only consumes the structured result (#85335).
const isCloudBackendDown = Boolean(error && typeof error === 'object' && (error as any).isCloudBackendDown === true)
- const statusCode = Number(
- error && typeof error === 'object' && Number.isInteger((error as any).statusCode)
- ? (error as any).statusCode
- : NaN
- )
+ const statusCode = readStatusCode(error)
// Only latch LOCAL boot failures. A remote failure (lapsed session / mint
// timeout / host briefly unreachable across sleep) is transient and has no
@@ -13395,6 +13376,7 @@ async function startHermes() {
// leaving it unlatched hides the overlay's "Sign in" button on every retry.
if (shouldLatchRemoteReauthFailure({ attemptedRemote, isReauth })) {
remoteReauthFailure = error instanceof Error ? error : new Error(message)
+ rememberLog('[boot] remote reauth latched: holding boot-progress until a recovery path clears it')
}
// Every latch above is set BEFORE this first yield back to the event loop.
diff --git a/apps/desktop/electron/native-auth-decisions.ts b/apps/desktop/electron/native-auth-decisions.ts
index 852521bdb8a..0fd5aa1a282 100644
--- a/apps/desktop/electron/native-auth-decisions.ts
+++ b/apps/desktop/electron/native-auth-decisions.ts
@@ -46,6 +46,8 @@
* would be double-encoded downstream; this function exists to document and
* pin that contract at the one seam that got it wrong.
*/
+import { readStatusCode } from './api-transport'
+
export function resolveJsonBody<T>(body: T): T {
return body
}
@@ -250,7 +252,5 @@ export function oauthGuardMayHardFail(providers: unknown): boolean {
* the credential and must keep its transient classification.
*/
export function shouldRotateNativeTokenAfterRejection(error: unknown): boolean {
- const statusCode = Number(error && typeof error === 'object' ? (error as { statusCode?: unknown }).statusCode : NaN)
-
- return statusCode === 401
+ return readStatusCode(error) === 401
}