Skip to content

The agent that grows with you

fix(desktop): distinguish transport failure classes in WS-ticket mint copy

Brooklyn Nicholson02e41181107b
diff --git a/apps/desktop/electron/connection-config.ts b/apps/desktop/electron/connection-config.ts
index d37bb362485..e12dce43974 100644
--- a/apps/desktop/electron/connection-config.ts
+++ b/apps/desktop/electron/connection-config.ts
@@ -112,6 +112,39 @@ function isStaleAppTokenRejection(error: unknown) {
return Boolean(error && typeof error === 'object' && (error as any).appTokenRejected === true)
}
+/**
+ * User-facing copy for a ticket mint that failed as a TRANSPORT fault (the
+ * gateway never answered with a usable HTTP status — timeout, refused
+ * connection, DNS). Distinguishing these lets the user act on the real
+ * problem instead of a blanket "could not reach" (#98647's diagnostic-cost
+ * complaint): a timeout suggests a network/VPN path issue, a refused
+ * connection a stopped gateway, DNS a wrong hostname.
+ */
+export function gatewayTicketTransportMessage(error: unknown): string {
+ const code =
+ error && typeof error === 'object' && typeof (error as { code?: unknown }).code === 'string'
+ ? (error as { code: string }).code
+ : ''
+
+ if (['ETIMEDOUT', 'ETIMEOUT', 'ENETUNREACH', 'EHOSTUNREACH', 'EAI_AGAIN', 'ENOTFOUND'].includes(code)) {
+ return (
+ 'Could not reach the remote Hermes gateway while refreshing its WebSocket ticket: ' +
+ 'the connection timed out or the host could not be resolved. ' +
+ 'Check your network/VPN path and the gateway URL, then reconnect.'
+ )
+ }
+
+ if (code === 'ECONNREFUSED') {
+ return (
+ 'Could not reach the remote Hermes gateway while refreshing its WebSocket ticket: ' +
+ 'the connection was refused. The gateway process is likely down or listening on another port. ' +
+ 'Start the gateway (or fix its URL), then reconnect.'
+ )
+ }
+
+ return 'Could not reach the remote Hermes gateway while refreshing its WebSocket ticket. Try reconnecting.'
+}
+
function gatewayTicketFailure(error, authMessage, transportMessage) {
const needsOauthLogin = isGatewayAuthRejection(error)
diff --git a/apps/desktop/electron/remote-oauth-ticket.test.ts b/apps/desktop/electron/remote-oauth-ticket.test.ts
index b67c9abd70c..acd5a9be951 100644
--- a/apps/desktop/electron/remote-oauth-ticket.test.ts
+++ b/apps/desktop/electron/remote-oauth-ticket.test.ts
@@ -77,4 +77,51 @@ describe('resolveRemoteOauthTicket', () => {
}
}
})
+
+ it('distinguishes transport failure classes in user-facing copy (timeout vs refused vs other)', async () => {
+ const mintThrowing = (cause: unknown) =>
+ resolveRemoteOauthTicket('https://gateway.example.com', {}, {
+ hasNativeSession: () => false,
+ mintGatewayWsTicket: async () => {
+ throw cause
+ }
+ }).catch((failure: Error) => failure)
+
+ const timeout = await mintThrowing(Object.assign(new Error('timeout of 8000ms exceeded'), { code: 'ETIMEDOUT' }))
+ const refused = await mintThrowing(Object.assign(new Error('connect ECONNREFUSED 10.0.0.5:8446'), { code: 'ECONNREFUSED' }))
+ const dns = await mintThrowing(Object.assign(new Error('getaddrinfo ENOTFOUND gw.example.com'), { code: 'ENOTFOUND' }))
+ const ambiguous = await mintThrowing(new Error('socket hang up'))
+ const http500 = await mintThrowing(Object.assign(new Error('500: upstream'), { statusCode: 500 }))
+
+ expect(timeout).toBeInstanceOf(Error)
+ expect(refused).toBeInstanceOf(Error)
+ expect(dns).toBeInstanceOf(Error)
+ expect(ambiguous).toBeInstanceOf(Error)
+ expect(http500).toBeInstanceOf(Error)
+
+ if (!(timeout instanceof Error && refused instanceof Error && dns instanceof Error && ambiguous instanceof Error && http500 instanceof Error)) {
+ throw new Error('Expected transport rejections')
+ }
+
+ // Timeout / DNS / unreachable all name the network path.
+ expect(timeout.message).toContain('timed out or the host could not be resolved')
+ expect(dns.message).toContain('timed out or the host could not be resolved')
+ // Refused names the stopped-gateway case.
+ expect(refused.message).toContain('connection was refused')
+ expect(refused.message).not.toContain('timed out')
+ // Ambiguous / unknown transport keeps the legacy one-liner.
+ expect(ambiguous.message).toBe(
+ 'Could not reach the remote Hermes gateway while refreshing its WebSocket ticket. Try reconnecting.'
+ )
+ expect(http500.message).toBe(
+ 'Could not reach the remote Hermes gateway while refreshing its WebSocket ticket. Try reconnecting.'
+ )
+
+ // None of the transport classes may ever read as an auth failure.
+ for (const err of [timeout, refused, dns, ambiguous, http500]) {
+ expect(isReauthRequiredError(err)).toBe(false)
+ expect((err as Error & { needsOauthLogin?: boolean }).needsOauthLogin).toBeUndefined()
+ }
+ })
+
})
diff --git a/apps/desktop/electron/remote-oauth-ticket.ts b/apps/desktop/electron/remote-oauth-ticket.ts
index 2564677ec95..393830a3946 100644
--- a/apps/desktop/electron/remote-oauth-ticket.ts
+++ b/apps/desktop/electron/remote-oauth-ticket.ts
@@ -1,5 +1,5 @@
import { makeNousCloudBackendDownError } from './backend-health'
-import { gatewayTicketFailure } from './connection-config'
+import { gatewayTicketFailure, gatewayTicketTransportMessage } from './connection-config'
import { oauthTicketFailureAuthMessage } from './native-auth-decisions'
interface RemoteOauthTicketDeps {
@@ -27,12 +27,16 @@ export async function resolveRemoteOauthTicket(
try {
return await deps.mintGatewayWsTicket(baseUrl, headers)
} catch (error) {
+ // Transport faults keep one headline ("could not reach") but the copy now
+ // names the actual failure class — timeout/DNS vs connection refused vs
+ // an HTTP fault — so fleet logs stop hiding three different upstream
+ // causes behind one sentence (#98647).
throw (
makeNousCloudBackendDownError(baseUrl, error) ??
gatewayTicketFailure(
error,
oauthTicketFailureAuthMessage(hadNativeSession),
- 'Could not reach the remote Hermes gateway while refreshing its WebSocket ticket. Try reconnecting.'
+ gatewayTicketTransportMessage(error)
)
)
}