Skip to content

The agent that grows with you

fix(desktop): warmAgent goes through the guarded prewarm resolver too

Tekniumdbc5d7c60b9e
diff --git a/apps/desktop/src/sdk/index.test.ts b/apps/desktop/src/sdk/index.test.ts
index 1d3e7fcf2a0..32c02bcffbe 100644
--- a/apps/desktop/src/sdk/index.test.ts
+++ b/apps/desktop/src/sdk/index.test.ts
@@ -11,12 +11,14 @@ import { clearAllSessionStates, publishSessionState } from '@/store/session-stat
// tests observe the guard's decision through the ONLY side effect that matters
// — whether openGatewayForProfile was dialed.
const warmMocks = vi.hoisted(() => ({
+ openGatewayForAgent: vi.fn(async (_connectionId: null | string, _profile: string) => undefined),
openGatewayForProfile: vi.fn(async (_profile: string) => undefined),
openSecondaryCount: vi.fn(() => 0)
}))
vi.mock('@/store/gateway', async importOriginal => ({
...((await importOriginal()) as Record<string, unknown>),
+ openGatewayForAgent: warmMocks.openGatewayForAgent,
openGatewayForProfile: warmMocks.openGatewayForProfile,
openSecondaryCount: warmMocks.openSecondaryCount
}))
@@ -48,6 +50,21 @@ describe('host.warmProfile pool-saturation contract', () => {
expect(warmMocks.openGatewayForProfile).not.toHaveBeenCalled()
})
+
+ it('warmAgent (multi-source rows) honours the same saturation guard', () => {
+ warmMocks.openGatewayForAgent.mockClear()
+ warmMocks.openSecondaryCount.mockReturnValue(3)
+
+ host.warmAgent('conn-vps', 'warm-agent-saturated')
+
+ expect(warmMocks.openGatewayForAgent).not.toHaveBeenCalled()
+
+ warmMocks.openSecondaryCount.mockReturnValue(2)
+
+ host.warmAgent('conn-vps', 'warm-agent-free')
+
+ expect(warmMocks.openGatewayForAgent).toHaveBeenCalledWith('conn-vps', 'warm-agent-free')
+ })
})
describe('host.state turn flags', () => {
diff --git a/apps/desktop/src/sdk/index.ts b/apps/desktop/src/sdk/index.ts
index 1ff9e720c54..4ce472aa508 100644
--- a/apps/desktop/src/sdk/index.ts
+++ b/apps/desktop/src/sdk/index.ts
@@ -821,11 +821,13 @@ export const host = {
},
/** Pre-dial an agent's socket on ITS source — the (connection, profile)
- * analogue of warmProfile. Fire-and-forget, same semantics.
+ * analogue of warmProfile. Fire-and-forget, same semantics, same guarded
+ * resolver (prewarmProfileBackend): a pointer sweep across a
+ * multi-source roster must not spawn past the pool cap either.
* `undefined` is accepted alongside `null` because a roster row's
* `connectionId` is optional; both mean "no explicit source". */
warmAgent: (connectionId: null | string | undefined, profile: string): void => {
- void openGatewayForAgent(connectionId ?? null, (profile ?? '').trim() || 'default').catch(() => undefined)
+ prewarmProfileBackend((profile ?? '').trim() || 'default', connectionId ?? null)
},
/** Activate an agent's gateway (dialing it if needed) so subsequent
diff --git a/apps/desktop/src/store/profile.ts b/apps/desktop/src/store/profile.ts
index b2d48892531..61595ba8a92 100644
--- a/apps/desktop/src/store/profile.ts
+++ b/apps/desktop/src/store/profile.ts
@@ -1,4 +1,4 @@
-import { LOCAL_CONNECTION_ID } from '@hermes/shared'
+import { LOCAL_CONNECTION_ID, registryBackendScopeKey } from '@hermes/shared'
import { atom, batch, computed } from 'nanostores'
import type { HermesConnection } from '@/global'
@@ -409,20 +409,30 @@ export const $hydrationSyncProfile = atom<string | null>(null)
// duplicating it — and a pre-warm for an already-open profile is a no-op.
// Throttled per profile so drive-by hovers can't spam spawn attempts; failures
// stay silent here and surface on the real switch, which owns retry/error UX.
+// A `connectionId` scopes the warm to a registry source (the (connection,
+// profile) rows a multi-source roster shows): same guards, keyed by the pool
+// scope key, dialed through openGatewayForAgent. Every speculative warm in
+// the app — rail, session rows, plugin rosters — goes through here so one
+// resolver owns the policy (#91545, #103631).
const PREWARM_MIN_INTERVAL_MS = 60_000
const prewarmedAt = new Map<string, number>()
-export function prewarmProfileBackend(name: string): void {
+export function prewarmProfileBackend(name: string, connectionId: null | string = null): void {
const key = normalizeProfileKey(name)
+ const connection = (connectionId ?? '').trim() || null
+ const scope = registryBackendScopeKey(connection, key)
- if (key === normalizeProfileKey($activeGatewayProfile.get())) {
+ if (
+ key === normalizeProfileKey($activeGatewayProfile.get()) &&
+ (!connection || connection === activeGatewayConnectionId())
+ ) {
return
}
const now = Date.now()
- if (now - (prewarmedAt.get(key) ?? 0) < PREWARM_MIN_INTERVAL_MS) {
+ if (now - (prewarmedAt.get(scope) ?? 0) < PREWARM_MIN_INTERVAL_MS) {
return
}
@@ -437,8 +447,9 @@ export function prewarmProfileBackend(name: string): void {
return
}
- prewarmedAt.set(key, now)
- openGatewayForProfile(key).catch(() => undefined)
+ prewarmedAt.set(scope, now)
+ const dial = connection ? openGatewayForAgent(connection, key) : openGatewayForProfile(key)
+ dial.catch(() => undefined)
}
let gatewaySwitch: Promise<void> | null = null