diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts index 5a7388ade1..4c1ea389a5 100644 --- a/apps/desktop/electron/main.ts +++ b/apps/desktop/electron/main.ts @@ -12504,10 +12504,17 @@ async function enumerateRegistryAgentSources(registry = readDesktopConnectionsRe } ipcMain.handle('hermes:agents:roster', async () => { - const enumerations = await enumerateRegistryAgentSources() + const registry = readDesktopConnectionsRegistry() + const enumerations = await enumerateRegistryAgentSources(registry) return { agents: buildAgentRoster(enumerations), + // The active gateway owns the renderer's profiles.list — union agents + // that report THIS connection are the same identities, not extra rows. + // Expose the primary id so the plugin merger can annotate them in place + // instead of appending duplicates (remote-only desktops doubled every + // bot otherwise; see #88344). + primaryConnectionId: registry.primary, sources: enumerations.map(({ connection, profiles, error }) => ({ connectionId: connection.id, label: connection.label, diff --git a/apps/desktop/src/plugins/hermes-bots/plugin.js b/apps/desktop/src/plugins/hermes-bots/plugin.js index 5ffc635fd7..232b01bb99 100644 --- a/apps/desktop/src/plugins/hermes-bots/plugin.js +++ b/apps/desktop/src/plugins/hermes-bots/plugin.js @@ -2073,14 +2073,16 @@ function useRoster() { } /** Merge the union agent roster (host.agents) over the active gateway's - * profiles.list. Local-source rows are matched by profile name and only - * ANNOTATED (handle, connectionId) — their rich fields stay authoritative. - * 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 (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 * ensureAgent/warmAgent. Pure — exercised directly by the tests. */ function mergeMultiSourceRoster(local, union) { const profiles = Array.isArray(local?.profiles) ? local.profiles.slice() : [] const agents = Array.isArray(union?.agents) ? union.agents : [] + const primaryId = String(union?.primaryConnectionId || '').trim() if (!agents.length) { return { ...local, profiles } @@ -2089,8 +2091,16 @@ function mergeMultiSourceRoster(local, union) { const localByName = new Map(profiles.map(p => [p.name, p])) for (const agent of agents) { - const isLocalSource = agent.connectionKind === 'local' - const row = isLocalSource ? localByName.get(agent.profile) : null + // The union enumerates EVERY registered connection, including the active + // gateway that already answered profiles.list. Without this the active + // gateway's own agents (connectionKind 'remote' on a remote-primary + // desktop) would be appended as phantom duplicates — every bot listed + // twice. Older Electron builds predate primaryConnectionId; fall back to + // the legacy local-source rule so single-source behavior stays intact. + const isPrimarySource = primaryId + ? String(agent.connectionId || '').trim() === primaryId + : agent.connectionKind === 'local' + const row = isPrimarySource ? localByName.get(agent.profile) : null if (row) { // Annotate in place: the @name-device handle only differs from the @@ -2100,9 +2110,9 @@ function mergeMultiSourceRoster(local, union) { continue } - if (isLocalSource) { - // Union saw a local profile profiles.list didn't return (older - // backend mid-refresh) — skip rather than invent a thin row. + if (isPrimarySource) { + // Union saw a primary-source profile profiles.list didn't return + // (older backend mid-refresh) — skip rather than invent a thin row. continue } 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 1ec2b20b19..5d95a91dcc 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 @@ -126,3 +126,62 @@ test('filterBots: matches the source device name for remote rows', () => { // Bare profile search keeps matching both rows. assert.equal(filterBots(roster, {}, 'research').length, 2) }) + +test('merge: active-gateway union agents annotate local rows, not duplicates (remote-primary desktop)', () => { + const { __mergeMultiSourceRoster: merge } = runtime() + const local = { + profiles: [ + { name: 'default', last_session: { id: 's-default' } }, + { name: 'dev', last_session: { id: 's-dev' } } + ] + } + const union = { + primaryConnectionId: '10-244-108-128-9119', + agents: [ + // The ACTIVE remote gateway itself — same identities as local, must + // annotate in place rather than append phantom duplicates (#88344). + { connectionId: '10-244-108-128-9119', connectionKind: 'remote', connectionLabel: '10.244.108.128:9119', profile: 'default', handle: 'default-10-244-108-128-9119' }, + { connectionId: '10-244-108-128-9119', connectionKind: 'remote', connectionLabel: '10.244.108.128:9119', profile: 'dev', handle: 'dev' }, + // A genuinely separate source with a same-named profile keeps its own row. + { connectionId: 'local', connectionKind: 'local', connectionLabel: 'This device', profile: 'default', handle: 'default-this-device' }, + { connectionId: 'local', connectionKind: 'local', connectionLabel: 'This device', profile: 'agent-mentor', handle: 'agent-mentor' } + ] + } + + const out = merge(local, union) + // 2 annotated local rows + 2 other-source rows = 4, NOT 6 (no phantom copies). + assert.equal(out.profiles.length, 4) + + const defaultRow = out.profiles.find(p => p.name === 'default' && !p.remoteSource) + // Annotated with the ACTIVE gateway's handle; rich fields survive. + assert.equal(defaultRow.last_session.id, 's-default') + assert.equal(defaultRow.handle, 'default-10-244-108-128-9119') + + // The same-named profile on the local device stays a separate tagged row. + const localDefault = out.profiles.find(p => p.name === 'default' && p.remoteSource) + assert.equal(localDefault.handle, 'default-this-device') + assert.equal(localDefault.connectionId, 'local') + + const mentor = out.profiles.find(p => p.name === 'agent-mentor') + assert.equal(mentor.remoteSource, true) + + // Exactly the two other-source rows are tagged remote; none from the active gateway. + assert.equal(out.profiles.filter(p => p.remoteSource).length, 2) +}) + +test('merge: primary-local desktop keeps single-source behavior (no phantom rows)', () => { + const { __mergeMultiSourceRoster: merge } = runtime() + const local = { profiles: [{ name: 'default' }, { name: 'dev' }] } + const union = { + primaryConnectionId: 'local', + agents: [ + { connectionId: 'local', connectionKind: 'local', connectionLabel: 'This device', profile: 'default', handle: 'default-this-device' }, + { connectionId: 'local', connectionKind: 'local', connectionLabel: 'This device', profile: 'dev', handle: 'dev' } + ] + } + + const out = merge(local, union) + assert.equal(out.profiles.length, 2) + assert.equal(out.profiles.filter(p => p.remoteSource).length, 0) + assert.equal(out.profiles[0].handle, 'default-this-device') +})