Skip to content

The agent that grows with you

fix(bot-mode): live active-connection id for roster classification + source-qualified row keys

Tekniumece487ceee0e
diff --git a/apps/desktop/src/plugins/hermes-bots/plugin.js b/apps/desktop/src/plugins/hermes-bots/plugin.js
index 1d190feb6fa..111252e9195 100644
--- a/apps/desktop/src/plugins/hermes-bots/plugin.js
+++ b/apps/desktop/src/plugins/hermes-bots/plugin.js
@@ -2055,7 +2055,7 @@ function useRoster() {
if (typeof host.agents === 'function') {
try {
const union = await host.agents()
- return mergeMultiSourceRoster(local, union)
+ return mergeMultiSourceRoster(local, union, liveActiveConnectionId())
} catch {
/* older build or roster failure — single-source list stands */
}
@@ -2072,14 +2072,34 @@ function useRoster() {
})
}
+/** Registry connection id of the LIVE active gateway (feature-detected;
+ * null on older desktops or the local/legacy primary path). Live beats the
+ * roster's primaryConnectionId when the user has activated a non-primary
+ * source's agent — profiles.list answers from THAT source, so the merge
+ * must classify against it, not the registry's primary. */
+function liveActiveConnectionId() {
+ if (typeof host.activeConnectionId !== 'function') {
+ return null
+ }
+
+ try {
+ return host.activeConnectionId()
+ } catch {
+ return null
+ }
+}
+
/** Merge the union agent roster (host.agents) over the active gateway's
- * profiles.list. Rows from the ACTIVE gateway (union.primaryConnectionId)
- * are matched by profile name and only ANNOTATED (handle, connectionId) —
- * their rich fields stay authoritative and they are NOT duplicated. Rows
- * from other sources become new roster entries tagged with their source
- * label so BotRow can badge them and route open/warm through
+ * profiles.list. Rows from the ACTIVE gateway are matched by connection id
+ * and only ANNOTATED (handle, connectionId) — their rich fields stay
+ * authoritative and they are NOT duplicated. The active id prefers the LIVE
+ * host.activeConnectionId() (the gateway may be a non-primary source after
+ * the user opens another connection's agent), falls back to the roster's
+ * primaryConnectionId, then to the legacy kind==='local' rule on older
+ * desktops. Rows from other sources become new roster entries tagged with
+ * their source label so BotRow can badge them and route open/warm through
* ensureAgent/warmAgent. Pure — exercised directly by the tests. */
-function mergeMultiSourceRoster(local, union) {
+function mergeMultiSourceRoster(local, union, liveActiveId = null) {
// Keep the first rich local row for every profile. profiles.list is normally
// unique, but a duplicated backend response must not turn one agent into an
// unbounded list of visually identical Bots rows.
@@ -2098,7 +2118,7 @@ function mergeMultiSourceRoster(local, union) {
}
const agents = Array.isArray(union?.agents) ? union.agents : []
- const primaryId = String(union?.primaryConnectionId || '').trim()
+ const primaryId = String(liveActiveId || union?.primaryConnectionId || '').trim()
if (!agents.length) {
return { ...local, profiles }
@@ -2156,6 +2176,15 @@ function mergeMultiSourceRoster(local, union) {
return { ...local, profiles }
}
+/** React list key for a roster row. Names alone are NOT unique in a
+ * multi-source roster (two connections can both expose 'default'); duplicate
+ * keys make React reconciliation repeat whole blocks of the list on every
+ * poll repaint. Annotated ACTIVE-source rows keep the plain-name key so
+ * existing rows don't remount when a desktop gains the union roster. */
+function botRowKey(bot) {
+ return `${bot.remoteSource ? bot.connectionId ?? '' : ''}:${bot.name}`
+}
+
/** The @handle users tag a bot with. Multi-source rosters precompute the
* handle (bare name, or name-device when the profile exists on several
* registered sources) — prefer it when present. The primary profile's
@@ -5980,7 +6009,7 @@ function ActiveNowStrip({ roster, activeProfile, gatewayState, metaByName, onOpe
children: label
})
]
- }, bot.name)
+ }, botRowKey(bot))
})
]
})
@@ -6152,7 +6181,7 @@ function CreateGroupChatDialog({ open, roster, onClose, onCreated }) {
title: 'Remove from selection',
onClick: () => setChecked(prev => ({ ...prev, [bot.name]: false })),
children: [displayName(bot, allMeta[bot.name]), jsx(Codicon, { name: 'close', className: 'text-[0.6rem]' })]
- }, bot.name)
+ }, botRowKey(bot))
)
})
: null,
@@ -6197,7 +6226,7 @@ function CreateGroupChatDialog({ open, roster, onClose, onCreated }) {
onCheckedChange: value => setChecked(prev => ({ ...prev, [bot.name]: Boolean(value) }))
})
]
- }, bot.name)
+ }, botRowKey(bot))
})
: jsx('div', {
className: 'px-1.5 py-3 text-center text-xs text-(--ui-text-tertiary)',
@@ -6624,7 +6653,7 @@ function BotsPane() {
}, `group:${section.group}`)
: null,
...section.bots.map(bot =>
- jsx(BotRow, { bot, onDelete: setDeleting, onEdit: setEditing, onGroup: setGrouping }, bot.name)
+ jsx(BotRow, { bot, onDelete: setDeleting, onEdit: setEditing, onGroup: setGrouping }, botRowKey(bot))
)
])
})
diff --git a/apps/desktop/src/plugins/hermes-bots/tests/active-now-strip.test.mjs b/apps/desktop/src/plugins/hermes-bots/tests/active-now-strip.test.mjs
index 984db4ef856..243554af6a4 100644
--- a/apps/desktop/src/plugins/hermes-bots/tests/active-now-strip.test.mjs
+++ b/apps/desktop/src/plugins/hermes-bots/tests/active-now-strip.test.mjs
@@ -90,7 +90,7 @@ test('ActiveNowStrip renders above the roster, is a live region, and is click-ac
assert.match(source, /jsx\('button', \{\s*type: 'button',\s*title: `Open \$\{label\}'s chat`/)
// The key rides as jsx()'s third argument — the ONLY form React treats as
// a list key; a `key:` prop leaves chips unkeyed (index identity).
- assert.match(source, /\}, bot\.name\)\s*\}\)\s*\]\s*\}\)\s*\}\s*\/\*\* Assign a bot to a group/s)
+ assert.match(source, /\}, botRowKey\(bot\)\)\s*\}\)\s*\]\s*\}\)\s*\}\s*\/\*\* Assign a bot to a group/s)
assert.match(source, /jsx\(BotFace,\s*\{[\s\S]*?mood: 'work'/)
assert.match(source, /openBotCanonicalChat\(bot\.name, allMeta\[bot\.name\]\?\.chat\)/)
})
diff --git a/apps/desktop/src/plugins/hermes-bots/tests/multi-source-roster.test.mjs b/apps/desktop/src/plugins/hermes-bots/tests/multi-source-roster.test.mjs
index 778297c4119..6e80aee52de 100644
--- a/apps/desktop/src/plugins/hermes-bots/tests/multi-source-roster.test.mjs
+++ b/apps/desktop/src/plugins/hermes-bots/tests/multi-source-roster.test.mjs
@@ -26,7 +26,7 @@ function runtime() {
.replace(/^import .* from 'react\/jsx-runtime'\r?\n/m, '')
.replace('export default {', 'globalThis.plugin = {')
.concat(
- '\nglobalThis.__mergeMultiSourceRoster = mergeMultiSourceRoster;\nglobalThis.__botHandle = botHandle;\nglobalThis.__filterBots = filterBots;'
+ '\nglobalThis.__mergeMultiSourceRoster = mergeMultiSourceRoster;\nglobalThis.__botHandle = botHandle;\nglobalThis.__filterBots = filterBots;\nglobalThis.__botRowKey = botRowKey;'
)
vm.runInNewContext(code, context)
return context
@@ -234,3 +234,42 @@ test('merge: primary-local desktop keeps single-source behavior (no phantom rows
assert.equal(out.profiles.filter(p => p.remoteSource).length, 0)
assert.equal(out.profiles[0].handle, 'default-this-device')
})
+
+// The LIVE-active-id override: after the user activates a non-primary
+// source's agent, profiles.list answers from THAT source — the merge must
+// classify against the live id, not the registry primary, or the active
+// source's agents duplicate all over again.
+test('merge: live active id beats primaryConnectionId for active-source matching', () => {
+ const { __mergeMultiSourceRoster: merge } = runtime()
+ const local = { profiles: [{ name: 'default', last_session: { id: 's1' } }] }
+ const union = {
+ primaryConnectionId: 'local',
+ agents: [
+ { connectionId: 'local', connectionKind: 'local', connectionLabel: 'This device', profile: 'default', handle: 'default-this-device' },
+ { connectionId: 'vps', connectionKind: 'remote', connectionLabel: 'VPS', profile: 'default', handle: 'default-vps' }
+ ]
+ }
+
+ // Live gateway = vps: its union row annotates the rich row; the primary
+ // (local) row appends as the genuinely-other source.
+ const out = merge(local, union, 'vps')
+ assert.equal(out.profiles.length, 2)
+
+ const rich = out.profiles.find(p => p.last_session)
+ assert.equal(rich.handle, 'default-vps')
+ assert.equal(out.profiles.filter(p => p.remoteSource).length, 1)
+ assert.equal(out.profiles.find(p => p.remoteSource).connectionId, 'local')
+})
+
+test('botRowKey: same name on two sources yields distinct keys; local rows stay stable', () => {
+ const { __botRowKey: botRowKey } = runtime()
+
+ const localRow = botRowKey({ name: 'default' })
+ const remoteRow = botRowKey({ name: 'default', remoteSource: true, connectionId: 'homelab' })
+
+ assert.notEqual(localRow, remoteRow)
+ // Annotated ACTIVE-source rows (connectionId set, no remoteSource) keep the
+ // plain key — annotation must not remount every row on desktops that gain
+ // the union roster mid-session.
+ assert.equal(botRowKey({ name: 'default', connectionId: 'vps' }), localRow)
+})
diff --git a/apps/desktop/src/sdk/index.ts b/apps/desktop/src/sdk/index.ts
index 3c56a1ad9db..6e0d0c42457 100644
--- a/apps/desktop/src/sdk/index.ts
+++ b/apps/desktop/src/sdk/index.ts
@@ -28,6 +28,7 @@ import { onGatewayEvent } from '@/contrib/events'
import { deleteProfile, getLogs, getStatus, type HermesGateway } from '@/hermes'
import {
$gateway,
+ activeGatewayConnectionId,
openGatewayForAgent,
openGatewayForProfile,
requestGatewayForAgent,
@@ -279,6 +280,15 @@ export const host = {
// ── Multi-source agents (the Bot Mode door) ───────────────────────────────
+ /** Registry connection id serving the gateway `host.request` currently hits
+ * — null for the local/legacy primary path. Roster UIs need this to tell
+ * "a row from the backend I'm already showing" apart from "a row from
+ * another source": two connections can both expose a 'default' profile,
+ * and matching by profile name alone duplicates every agent when the
+ * active gateway is a registered remote. Re-read per use — it changes on
+ * profile/agent swaps. */
+ activeConnectionId: (): null | string => activeGatewayConnectionId(),
+
/** The registered connection list (labels, kinds, primary) — token bytes
* never included. Rejects on Desktop builds without the registry. */
connections: async () => {