The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): removing or re-pointing a connection forgets what was cached about it
John Paul Soliva07888c880f76
diff --git a/apps/desktop/electron/connection-caches.test.ts b/apps/desktop/electron/connection-caches.test.tsnew file mode 100644index 00000000000..4c665463f76--- /dev/null+++ b/apps/desktop/electron/connection-caches.test.ts@@ -0,0 +1,69 @@+import assert from 'node:assert/strict'++import { beforeEach, test } from 'vitest'++import {+ connectionInstallIds,+ evictConnectionCaches,+ sshInventoryAttemptedAt,+ sshRosterCache+} from './connection-caches'+import { shouldRetrySshInventory } from './connection-registry'++beforeEach(() => {+ sshRosterCache.clear()+ sshInventoryAttemptedAt.clear()+ connectionInstallIds.clear()+})++function seed(id: string) {+ sshRosterCache.set(id, ['default', 'dixie'])+ sshInventoryAttemptedAt.set(id, Date.now())+ connectionInstallIds.set(id, { id: 'aaa', ts: Date.now() })+}++test('evicting a connection id forgets every cache keyed by it', () => {+ seed('mac-mini')+ seed('spark')++ evictConnectionCaches('mac-mini')++ // Nothing about the evicted id survives in ANY connection-scoped cache. A cache added later+ // and not registered in the module would fail this by omission.+ assert.equal(sshRosterCache.has('mac-mini'), false)+ assert.equal(sshInventoryAttemptedAt.has('mac-mini'), false)+ assert.equal(connectionInstallIds.has('mac-mini'), false)++ // Its neighbours are untouched.+ assert.deepEqual(sshRosterCache.get('spark'), ['default', 'dixie'])+ assert.equal(connectionInstallIds.get('spark')?.id, 'aaa')+})++test('an evicted id enumerates from the live target again instead of serving the old one', () => {+ // The reason eviction matters: a cached success is never retried, so without it a recycled or+ // re-pointed id keeps answering with the previous machine's inventory for the whole session.+ seed('mac-mini')+ assert.equal(+ shouldRetrySshInventory(sshRosterCache.has('mac-mini'), sshInventoryAttemptedAt.get('mac-mini'), Date.now()),+ false+ )++ evictConnectionCaches('mac-mini')++ assert.equal(+ shouldRetrySshInventory(sshRosterCache.has('mac-mini'), sshInventoryAttemptedAt.get('mac-mini'), Date.now()),+ true+ )+})++test('evicting an unknown or empty id is a no-op', () => {+ seed('mac-mini')++ for (const id of ['', 'never-registered', undefined as unknown as string]) {+ evictConnectionCaches(id)+ }++ assert.equal(sshRosterCache.size, 1)+ assert.equal(sshInventoryAttemptedAt.size, 1)+ assert.equal(connectionInstallIds.size, 1)+})diff --git a/apps/desktop/electron/connection-caches.ts b/apps/desktop/electron/connection-caches.tsnew file mode 100644index 00000000000..4f0a1b295b4--- /dev/null+++ b/apps/desktop/electron/connection-caches.ts@@ -0,0 +1,48 @@+/**+ * Main-process caches keyed by connection id.+ *+ * Every entry here answers "what is at connection <id>", so it is only valid while that id keeps+ * pointing at the same machine. Two lifecycle events break that: removing a connection (ids are+ * recycled label slugs — `connectionIdForLabel` suffixes only against CURRENTLY taken ids, so+ * re-adding "Mac mini" gets `mac-mini` back) and editing its dial material (same id, new host).+ * Neither used to evict, and `shouldRetrySshInventory` never retries a cached success, so the old+ * machine's profile list stayed authoritative for the rest of the app session.+ *+ * They live in one module so that invariant has somewhere to be stated — and tested — instead of+ * being three loose `Map`s in main.ts.+ */++/** Last known profile list per ssh connection (reused so switching back to local doesn't empty Bot Mode). */+export const sshRosterCache = new Map<string, string[]>()++/** When the ssh inventory probe last ran, for the retry backoff. */+export const sshInventoryAttemptedAt = new Map<string, number>()++/**+ * Stable backend identity per connection (the `install_id` its /api/status reports; absent on+ * older backends). TTL-cached because enumeration runs on the ~5s Bot Mode roster poll.+ */+export const connectionInstallIds = new Map<string, { id?: string; ts: number }>()++const CONNECTION_SCOPED_CACHES: Map<string, unknown>[] = [+ sshRosterCache,+ sshInventoryAttemptedAt,+ connectionInstallIds+]++/**+ * Forget everything cached about a connection id. Call whenever that id stops naming the machine+ * it named — it is removed, or its dial material changes — so the next probe re-learns from the+ * live target instead of serving the previous one.+ */+export function evictConnectionCaches(connectionId: string): void {+ const id = String(connectionId || '')++ if (!id) {+ return+ }++ for (const cache of CONNECTION_SCOPED_CACHES) {+ cache.delete(id)+ }+}diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.tsindex 073f95c516b..8e7162fb687 100644--- a/apps/desktop/electron/main.ts+++ b/apps/desktop/electron/main.ts@@ -103,6 +103,12 @@ import { discoverWithTeamFallback } from './cloud-discovery'import { installCommandScreenshot } from './command-screenshot'import { writeComposerPaste } from './composer-paste'import { applyConnectionChange, teardownSshState } from './connection-apply'+import {+ connectionInstallIds,+ evictConnectionCaches,+ sshInventoryAttemptedAt,+ sshRosterCache+} from './connection-caches'import {apiRequestRegistryConnectionId,authModeFromStatus,@@ -9586,6 +9592,9 @@ async function saveRegistryConnection(input: any = {}) {// their secondaries for this connection id.if (existing && connectionDialFieldsChanged(existing, entry)) {await stopRegistryConnectionBackends(entry.id)+ // The id now names a different machine: its cached roster/identity describe the old one,+ // and a cached ssh inventory is never retried (`shouldRetrySshInventory`).+ evictConnectionCaches(entry.id)broadcastConnectionsChanged({ connectionId: entry.id, reason: 'updated' })} else {// Every OTHER successful save (a brand-new connection, a label rename)@@ -15574,6 +15583,9 @@ ipcMain.handle('hermes:connections:remove', async (_event, id) => {// Tear down anything the removed connection still had running: pooled// backends under its composite keys and any ssh tunnel scopes it owned.await stopRegistryConnectionBackends(key)+ // …and everything cached ABOUT it. Ids are recycled label slugs, so re-adding "Mac mini"+ // gets `mac-mini` back — with the removed machine's profile list still cached under it.+ evictConnectionCaches(key)// And the renderer side: without this push, secondaries scoped to the// removed connection keep their WebSocket open (remote/cloud have no local// process to kill) and stream ghost events until page reload.@@ -15698,8 +15710,9 @@ ipcMain.handle('hermes:connections:test', async (_event, id) => {// would spawn tunnels the user never asked for); once dialed, their pooled// descriptor serves the enumeration like any remote. Last-known SSH profile// lists are reused so switching the window back to local does not empty Bot Mode.-const sshRosterCache = new Map<string, string[]>()-const sshInventoryAttemptedAt = new Map<string, number>()+// These three live in ./connection-caches, which states (and tests) the invariant they share:+// each is keyed by connection id and is only valid while that id names the same machine, so+// removing a connection or re-pointing it must evict them (`evictConnectionCaches`).const SSH_INVENTORY_RETRY_MS = 60_000// Stable backend identity per registered connection: the `install_id` its@@ -15708,7 +15721,6 @@ const SSH_INVENTORY_RETRY_MS = 60_000// status probe is cached per connection with a TTL to avoid doubling roster// traffic; the Test button refreshes it eagerly. A missing id simply bypasses// the same-backend roster collapse — fully backward compatible.-const connectionInstallIds = new Map<string, { id?: string; ts: number }>()const INSTALL_ID_TTL_MS = 5 * 60_000const INSTALL_ID_NEGATIVE_TTL_MS = 60_000