Skip to content

The agent that grows with you

fix(desktop): claim-guard every remaining ensureRegistryBackend()/ensureBackend() call in Electron main

nftpoetrist1e73281614a0
diff --git a/apps/desktop/electron/backend-dial-claim.test.ts b/apps/desktop/electron/backend-dial-claim.test.ts
index 6d13d7b261f..4788da16d9d 100644
--- a/apps/desktop/electron/backend-dial-claim.test.ts
+++ b/apps/desktop/electron/backend-dial-claim.test.ts
@@ -140,4 +140,61 @@ describe('main.ts wiring for #90812', () => {
expect(body).toContain('backendDialClaims.run(backendScopeKey(id, profile)')
expect(body).toContain('ensureRegistryBackend(id, profile)')
})
+
+ // The four IPC/probe surfaces below call ensureRegistryBackend()/ensureBackend()
+ // directly, bypassing backendDialClaims entirely — so a renderer's guarded
+ // reconnect dial and one of these can independently race the SAME
+ // ensureRegistryBackend() await-before-pool-check window (main.ts) and each
+ // bootstrap its own SSH tunnel / remote dashboard for the same
+ // (connectionId, profile) scope.
+
+ it('routes a media-stream connection resolve through the single-owner claim', () => {
+ const handlerStart = mainSource.indexOf('resolveRemoteConnection: ({ connectionId, profile }) =>')
+ expect(handlerStart).toBeGreaterThan(-1)
+ const body = mainSource.slice(handlerStart, handlerStart + 300)
+
+ expect(body).toContain('backendDialClaims.run(backendScopeKey(connectionId, profile)')
+ expect(body).toContain('ensureRegistryBackend(connectionId, profile)')
+ expect(body).toContain('ensureBackend(profile)')
+ })
+
+ it('routes a terminal-pane backend resolve through the single-owner claim on both the registry and local branches', () => {
+ const handlerStart = mainSource.indexOf('async function ensureTerminalBackend(webContentsId: number) {')
+ expect(handlerStart).toBeGreaterThan(-1)
+ const body = mainSource.slice(handlerStart, handlerStart + 900)
+
+ expect(body).toContain('backendDialClaims.run(backendScopeKey(windowRoute.connectionId, windowRoute.profile)')
+ expect(body).toContain('ensureRegistryBackend(windowRoute.connectionId, windowRoute.profile)')
+ expect(body).toContain('backendDialClaims.run(backendScopeKey(null, profile)')
+ expect(body).toContain('ensureBackend(profile)')
+ })
+
+ it('routes the roster-enumeration probe through the single-owner claim', () => {
+ const handlerStart = mainSource.indexOf('async function enumerateRegistryAgentSources')
+ expect(handlerStart).toBeGreaterThan(-1)
+ const body = mainSource.slice(handlerStart, handlerStart + 3_700)
+
+ expect(body).toContain('backendDialClaims.run(backendScopeKey(connection.id, null)')
+ expect(body).toContain('ensureRegistryBackend(connection.id, null)')
+ expect(body).toContain("getJsonForBackend(descriptor, '/api/profiles'")
+ })
+
+ it('routes the connections update-all dispatch through the single-owner claim', () => {
+ const handlerStart = mainSource.indexOf("ipcMain.handle('hermes:connections:update-all',")
+ expect(handlerStart).toBeGreaterThan(-1)
+ const body = mainSource.slice(handlerStart, handlerStart + 2_000)
+
+ expect(body).toContain('backendDialClaims.run(backendScopeKey(connection.id, null)')
+ expect(body).toContain('ensureRegistryBackend(connection.id, null)')
+ expect(body).toContain("postJsonForBackend(descriptor, '/api/hermes/update'")
+ })
+
+ it('routes every registry-scoped REST dispatch (hermes:api) through the single-owner claim', () => {
+ const handlerStart = mainSource.indexOf('async function dispatchRegistryApiRequest(')
+ expect(handlerStart).toBeGreaterThan(-1)
+ const body = mainSource.slice(handlerStart, handlerStart + 900)
+
+ expect(body).toContain('backendDialClaims.run(backendScopeKey(registryConnectionId, routeProfile)')
+ expect(body).toContain('ensureRegistryBackend(registryConnectionId, routeProfile)')
+ })
})
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts
index 1f1c86b22ed..4e81259247b 100644
--- a/apps/desktop/electron/main.ts
+++ b/apps/desktop/electron/main.ts
@@ -1362,8 +1362,13 @@ function registerMediaProtocol() {
return resolvedPath
},
+ // Claim-guarded (#90812): a media stream load can race a renderer's own
+ // reconnect dial for the same (connectionId, profile) scope; coalescing
+ // here avoids bootstrapping a second SSH tunnel / remote dashboard.
resolveRemoteConnection: ({ connectionId, profile }) =>
- connectionId ? ensureRegistryBackend(connectionId, profile) : ensureBackend(profile)
+ backendDialClaims.run(backendScopeKey(connectionId, profile), () =>
+ connectionId ? ensureRegistryBackend(connectionId, profile) : ensureBackend(profile)
+ )
})
protocol.handle(MEDIA_PROTOCOL, handler)
@@ -9932,11 +9937,18 @@ function activeSshTerminalTarget(webContentsId?: number) {
async function ensureTerminalBackend(webContentsId: number) {
const windowRoute = windowConnectionRoutes.get(webContentsId)
+ // Claim-guarded (#90812): opening a terminal pane can race a renderer's own
+ // reconnect dial for the same (connectionId, profile) scope; coalescing
+ // here avoids bootstrapping a second SSH tunnel / remote dashboard.
if (windowRoute?.registryScoped && windowRoute.connectionId) {
- return ensureRegistryBackend(windowRoute.connectionId, windowRoute.profile)
+ return backendDialClaims.run(backendScopeKey(windowRoute.connectionId, windowRoute.profile), () =>
+ ensureRegistryBackend(windowRoute.connectionId, windowRoute.profile)
+ )
}
- return ensureBackend(windowRoute?.profile ?? primaryProfileKey())
+ const profile = windowRoute?.profile ?? primaryProfileKey()
+
+ return backendDialClaims.run(backendScopeKey(null, profile), () => ensureBackend(profile))
}
// Loopback reach for the browser pane. Scoped to the SSH connection that
@@ -14953,8 +14965,15 @@ async function enumerateRegistryAgentSources(registry = readDesktopConnectionsRe
}
}
+ // Claim-guarded (#90812): this ~5s roster poll can race a renderer's
+ // own reconnect dial for the same connection; coalescing avoids
+ // bootstrapping a second SSH tunnel / remote dashboard.
const descriptor: any = await withEnumerationDeadline(
- Promise.resolve(ensureRegistryBackend(connection.id, null))
+ Promise.resolve(
+ backendDialClaims.run(backendScopeKey(connection.id, null), () =>
+ ensureRegistryBackend(connection.id, null)
+ )
+ )
)
const body: any = await getJsonForBackend(descriptor, '/api/profiles', { timeoutMs: 8_000 })
@@ -15169,7 +15188,11 @@ ipcMain.handle('hermes:connections:update-all', async (_event, payload) => {
}
}
- const descriptor: any = await ensureRegistryBackend(connection.id, null)
+ // Claim-guarded (#90812): coalesce with a concurrent renderer dial
+ // for the same connection instead of bootstrapping a second backend.
+ const descriptor: any = await backendDialClaims.run(backendScopeKey(connection.id, null), () =>
+ ensureRegistryBackend(connection.id, null)
+ )
const body: any = await postJsonForBackend(descriptor, '/api/hermes/update', {}, { timeoutMs: 15_000 })
@@ -15809,7 +15832,13 @@ async function dispatchRegistryApiRequest(
routeProfile = request?.profile,
requestProfile = request?.profile
) {
- const connection: any = await ensureRegistryBackend(registryConnectionId, routeProfile)
+ // Claim-guarded (#90812): every registry-scoped REST call funnels through
+ // here, so it can race a renderer's own WS reconnect dial for the same
+ // (connectionId, profile) scope; coalescing avoids bootstrapping a second
+ // SSH tunnel / remote dashboard.
+ const connection: any = await backendDialClaims.run(backendScopeKey(registryConnectionId, routeProfile), () =>
+ ensureRegistryBackend(registryConnectionId, routeProfile)
+ )
const requestPath = pathForRegistryBackendRequest(request.path, requestProfile, connection)