Skip to content

The agent that grows with you

fix(desktop): ignore stale remote connection attempts

604maestro0a6ead0e9bde
diff --git a/apps/desktop/electron/backend-connection-state.test.ts b/apps/desktop/electron/backend-connection-state.test.ts
index 5520435d321..ce9dd2fded1 100644
--- a/apps/desktop/electron/backend-connection-state.test.ts
+++ b/apps/desktop/electron/backend-connection-state.test.ts
@@ -6,6 +6,43 @@ import { createBackendConnectionState } from './backend-connection-state'
type FakeProcess = { id: string }
+function deferred<T>() {
+ let resolve!: (value: T) => void
+
+ const promise = new Promise<T>(next => {
+ resolve = next
+ })
+
+ return { promise, resolve }
+}
+
+test('an invalidated remote attempt cannot publish a late descriptor', async () => {
+ const state = createBackendConnectionState<FakeProcess, string>()
+ const oldProbe = deferred<string>()
+ const oldAttempt = state.startAttempt()
+
+ const oldResult = oldProbe.promise.then(descriptor => {
+ if (!state.isCurrentAttempt(oldAttempt)) {
+ throw new Error('Hermes backend start was superseded by a newer connection attempt.')
+ }
+
+ return descriptor
+ })
+
+ state.setPromise(oldAttempt, oldResult)
+ state.invalidate()
+
+ const newAttempt = state.startAttempt()
+ const newResult = Promise.resolve('https://new.example')
+
+ state.setPromise(newAttempt, newResult)
+ assert.equal(await newResult, 'https://new.example')
+
+ oldProbe.resolve('https://old.example')
+ await assert.rejects(oldResult, /superseded by a newer connection attempt/)
+ assert.equal(state.getPromise(), newResult)
+})
+
test('a stale backend exit cannot clear a newer connection attempt', () => {
const state = createBackendConnectionState<FakeProcess, string>()
const oldAttempt = state.startAttempt()
diff --git a/apps/desktop/electron/backend-connection-state.ts b/apps/desktop/electron/backend-connection-state.ts
index 07b289c7db6..d20165db851 100644
--- a/apps/desktop/electron/backend-connection-state.ts
+++ b/apps/desktop/electron/backend-connection-state.ts
@@ -29,6 +29,10 @@ export function createBackendConnectionState<TProcess, TConnection>() {
return true
},
+ isCurrentAttempt(attempt: BackendConnectionAttempt<TConnection>): boolean {
+ return attempt.generation === generation
+ },
+
attachProcess(
attempt: BackendConnectionAttempt<TConnection>,
nextProcess: TProcess
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts
index 47f1508b041..faf8f3e5366 100644
--- a/apps/desktop/electron/main.ts
+++ b/apps/desktop/electron/main.ts
@@ -9870,8 +9870,22 @@ async function startHermes() {
const connectionPromise = (async () => {
const connectRemote = async remote => {
+ // resolveRemote() may take arbitrarily long (settings resolve / ws-ticket
+ // mint). If a newer attempt started meanwhile (e.g. the user switched
+ // remotes and Apply invalidated this attempt), bail before probing.
+ if (!backendConnectionState.isCurrentAttempt(connectionAttempt)) {
+ throw new Error('Hermes backend start was superseded by a newer connection attempt.')
+ }
+
await advanceBootProgress('backend.remote', `Connecting to remote Hermes backend at ${remote.baseUrl}`, 24)
await waitForHermes(remote.baseUrl, remote.token, undefined, remote.authMode)
+
+ // Second async boundary: the health probe itself can outlive the
+ // attempt. A late success here must not publish a stale descriptor.
+ if (!backendConnectionState.isCurrentAttempt(connectionAttempt)) {
+ throw new Error('Hermes backend start was superseded by a newer connection attempt.')
+ }
+
updateBootProgress({
phase: 'backend.ready',
message: 'Remote Hermes backend is ready',