Skip to content

The agent that grows with you

fix(desktop): the REST stale-ticket redial keeps the failed request's route

Teknium618af5b505de
diff --git a/apps/desktop/electron/desktop-redial-route-request.test.ts b/apps/desktop/electron/desktop-redial-route-request.test.ts
new file mode 100644
index 00000000000..e615d58ed66
--- /dev/null
+++ b/apps/desktop/electron/desktop-redial-route-request.test.ts
@@ -0,0 +1,23 @@
+import { expect, test } from 'vitest'
+
+import { resolveProfileBackendRoute } from './connection-config'
+
+// B2-15: an unscopable mutating REST call keeps its pooled backend only while the routing decision
+// sees the request; the stale-ticket redial (ensure + forget) must resolve that same route, or the
+// retry lands on the shared primary home (wrong-profile write) and forget clears the primary.
+test('the REST redial re-ensures and forgets the route the failed request used', async () => {
+ const opts = { primaryProfile: 'default', globalRemote: false, profileRemoteOverride: false, isolatedBackend: false }
+ const write = { ...opts, requestMethod: 'POST', requestPath: '/api/not-a-real-route/destroy' }
+ expect(resolveProfileBackendRoute('work', write).backend).toBe('pool')
+ expect(resolveProfileBackendRoute('work', opts).backend).toBe('primary')
+
+ // main.ts is not importable under vitest; pin the one REST redial seam's wiring instead.
+ const fs = await import('node:fs/promises')
+ const source = await fs.readFile(new URL('./main.ts', import.meta.url), 'utf8')
+ const first = source.indexOf('connection = await ensureBackend(routeProfile, ensureOpts)')
+ const seam = source.slice(first, source.indexOf('use: send', first))
+ expect(first).toBeGreaterThan(0)
+ expect(seam).toContain('ensure: () => ensureBackend(routeProfile, ensureOpts)')
+ expect(seam).toContain('forget: () => forgetLocalGatewayDescriptor(routeProfile, ensureOpts.request)')
+ expect(source).toContain('resolveProfileBackendRoute(key, profileRouteOptions(key, request))')
+})
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts
index 55d87a631db..3a9efc83d7a 100644
--- a/apps/desktop/electron/main.ts
+++ b/apps/desktop/electron/main.ts
@@ -10785,9 +10785,10 @@ function profileRouteOptions(
// so the next ensureBackend() re-runs `gateway ensure` (attach or start) instead
// of minting tickets against a dead control socket. Nothing is killed: the
// descriptor never owned the runtime.
-async function forgetLocalGatewayDescriptor(profile) {
+async function forgetLocalGatewayDescriptor(profile, request?: { method?: string; path?: string }) {
const key = profile && String(profile).trim() ? String(profile).trim() : primaryProfileKey()
- const route = resolveProfileBackendRoute(key, profileRouteOptions(key))
+ // Resolve the route the failed call used (B2-15): an unscopable mutation's stale entry is pooled.
+ const route = resolveProfileBackendRoute(key, profileRouteOptions(key, request))
if (route.backend === 'primary') {
const attempt = backendConnectionState.startAttempt()
@@ -16442,10 +16443,10 @@ async function handleHermesApiRequest(request) {
let connection
try {
- connection = await ensureBackend(routeProfile, {
- passive: request?.passive,
- request: { method: request?.method, path: request?.path }
- })
+ // The redial reuses these options: without the request an unscopable mutation's retry
+ // resolves the shared primary and writes in the wrong profile (B2-15).
+ const ensureOpts = { passive: request?.passive, request: { method: request?.method, path: request?.path } }
+ connection = await ensureBackend(routeProfile, ensureOpts)
const timeoutMs = resolveTimeoutMs(request?.timeoutMs, DEFAULT_FETCH_TIMEOUT_MS)
@@ -16461,8 +16462,8 @@ async function handleHermesApiRequest(request) {
response = connection.gatewayEndpoint
? await redialLocalGateway({
- ensure: () => ensureBackend(routeProfile),
- forget: () => forgetLocalGatewayDescriptor(routeProfile),
+ ensure: () => ensureBackend(routeProfile, ensureOpts),
+ forget: () => forgetLocalGatewayDescriptor(routeProfile, ensureOpts.request),
use: send
})
: await send(connection)