Skip to content

The agent that grows with you

fix(desktop): keep a newer new-chat draft from a late switch re-home

unsupportedpastels042e925ea2a5
diff --git a/apps/desktop/src/store/connections-new-chat-route.test.ts b/apps/desktop/src/store/connections-new-chat-route.test.ts
index abfa74e3bca..9920f354caa 100644
--- a/apps/desktop/src/store/connections-new-chat-route.test.ts
+++ b/apps/desktop/src/store/connections-new-chat-route.test.ts
@@ -159,6 +159,42 @@ it('does not overwrite a newer draft when remembering a committed switch settles
expect(resolveNewChatOwnerRoute()).toEqual(route)
})
+it('keeps a newer agent draft started while an ordinary switch is still bookkeeping', async () => {
+ const withOther: DesktopConnectionsRegistry = {
+ ...registry,
+ connections: [
+ ...registry.connections,
+ { id: 'other', kind: 'remote', label: 'Other', tokenSet: true, tokenPreview: null }
+ ]
+ }
+
+ setConnectionsRegistry(withOther)
+ const remembered = deferred<{ ok: boolean; registry: DesktopConnectionsRegistry }>()
+ Object.assign(window.hermesDesktop!, {
+ connections: {
+ setLastUsed: vi.fn().mockReturnValueOnce(remembered.promise).mockResolvedValue({ ok: true, registry: withOther })
+ }
+ })
+ const first = selectConnection('local')
+ await vi.waitFor(() => expect(window.hermesDesktop!.connections!.setLastUsed).toHaveBeenCalled())
+ // The newer draft's source is still dialing, so local stays in the foreground.
+ const dial = deferred<HermesConnection>()
+ getConnectionFor.mockImplementation(async ({ connectionId, profile }) =>
+ connectionId === 'other' ? dial.promise : descriptor(connectionId, profile)
+ )
+ newSessionInAgent({ connectionId: 'other', profile: 'default' })
+ remembered.resolve({ ok: true, registry: withOther })
+ await first
+ const foreground = activeGatewayConnectionId()
+ const owner = resolveNewChatOwnerRoute()
+ // Settle the held dial before asserting so a failure can't wedge the mutex.
+ dial.resolve(descriptor('other'))
+ await ensureGatewayAgent('other', 'default')
+ expect(foreground).toBe('local')
+ expect(owner).toEqual({ connectionId: 'other', profile: 'default' })
+ expect(resolveNewChatOwnerRoute()).toEqual({ connectionId: 'other', profile: 'default' })
+})
+
it('does not let a superseded dial clear a newer explicit draft', async () => {
const dial = deferred<HermesConnection>()
getConnectionFor.mockImplementationOnce(() => dial.promise)
diff --git a/apps/desktop/src/store/connections.test.ts b/apps/desktop/src/store/connections.test.ts
index bac0ab96fd4..cb9ec749f5d 100644
--- a/apps/desktop/src/store/connections.test.ts
+++ b/apps/desktop/src/store/connections.test.ts
@@ -76,6 +76,7 @@ vi.mock('@/store/profile', () => ({
$newChatRoute: atom(null),
$showAllProfiles,
captureNewChatSource: vi.fn(),
+ currentNewChatIntent: () => 0,
ensureGatewayAgent,
normalizeProfileKey: (name: null | string | undefined) => (name ?? '').trim() || 'default',
openGatewayAgent,
diff --git a/apps/desktop/src/store/connections.ts b/apps/desktop/src/store/connections.ts
index 41a8c3708ab..b771e5a8a8e 100644
--- a/apps/desktop/src/store/connections.ts
+++ b/apps/desktop/src/store/connections.ts
@@ -26,6 +26,7 @@ import {
$newChatRoute,
$showAllProfiles,
captureNewChatSource,
+ currentNewChatIntent,
ensureGatewayAgent,
normalizeProfileKey,
openGatewayAgent,
@@ -425,6 +426,10 @@ export async function selectConnection(connectionId: string, options: SelectConn
}
const revision = ++switchRevision
+ // A draft the user starts while this switch is in flight (a gateway group's
+ // "+", a profile pick) is newer than the switch; the late re-home must not
+ // take it over. switchRevision only sees other switches.
+ const draftIntent = currentNewChatIntent()
pendingTarget = targetKey
$pendingConnectionId.set(connectionId)
// Set by the commit hook once THIS switch has wiped — i.e. it owns the
@@ -554,7 +559,10 @@ export async function selectConnection(connectionId: string, options: SelectConn
$showAllProfiles.set(false)
}
- rehomeNewChatDraft(targetProfile, restoreOnBoot)
+ if (currentNewChatIntent() === draftIntent) {
+ rehomeNewChatDraft(targetProfile, restoreOnBoot)
+ }
+
await refreshActiveProfile()
}
} catch (error) {
diff --git a/apps/desktop/src/store/profile.ts b/apps/desktop/src/store/profile.ts
index de81d5e3aac..c1556450f30 100644
--- a/apps/desktop/src/store/profile.ts
+++ b/apps/desktop/src/store/profile.ts
@@ -326,10 +326,20 @@ export const $newChatConnectionId = atom<null | string>(null)
// source policy; only this pinned intent suppresses the ambient fallback.
let legacyNewChatProfile: null | string = null
+// Bumped by every new-chat owner intent (each one captures its source). Async
+// work that re-homes the draft late compares it to tell a newer intent from
+// its own, even when the newer intent repeats the same route values.
+let newChatIntentRevision = 0
+
+export function currentNewChatIntent(): number {
+ return newChatIntentRevision
+}
+
/** Capture the registry source a new-chat profile intent lands on — by
* default the active one; callers that dial a different door (a profile
* pick, see profilePickConnectionId) pass the source that door uses. */
export function captureNewChatSource(connectionId: null | string = activeGatewayConnectionId()): void {
+ newChatIntentRevision += 1
legacyNewChatProfile = null
$newChatConnectionId.set(connectionId)
}