diff --git a/apps/desktop/electron/connection-registry.test.ts b/apps/desktop/electron/connection-registry.test.ts index 2c4ed212bc..f32ff926b5 100644 --- a/apps/desktop/electron/connection-registry.test.ts +++ b/apps/desktop/electron/connection-registry.test.ts @@ -284,6 +284,137 @@ test('roster: duplicate profiles from one connection remain one routable agent', ) }) +// --- buildAgentRoster: same-backend (install_id) collapse --- + +test('roster: two connections with the same install_id collapse to one row per profile', () => { + const hostname = { id: 'spark', kind: 'remote' as const, label: 'Spark', url: 'http://spark:8642' } + const tailscale = { id: 'spark-ts', kind: 'remote' as const, label: 'Spark TS', url: 'http://100.1.2.3:8642' } + + const roster = buildAgentRoster([ + { connection: hostname, profiles: ['default', 'research'], installId: 'aaa' }, + { connection: tailscale, profiles: ['default', 'research'], installId: 'aaa' } + ]) + + // One row per (install, profile) — no duplicate bots for the same box. + assert.deepEqual( + roster.map(agent => `${agent.connectionId}/${agent.profile}`).sort(), + ['spark/default', 'spark/research'] + ) + // Handle disambiguation runs AFTER the collapse: no more suffixed names. + assert.deepEqual( + roster.map(agent => agent.handle).sort(), + ['default', 'research'] + ) +}) + +test('roster: collapse prefers the active (primary) connection', () => { + const hostname = { id: 'spark', kind: 'remote' as const, label: 'Spark', url: 'http://spark:8642' } + const tailscale = { id: 'spark-ts', kind: 'remote' as const, label: 'Spark TS', url: 'http://100.1.2.3:8642' } + + const roster = buildAgentRoster( + [ + { connection: hostname, profiles: ['default'], installId: 'aaa' }, + { connection: tailscale, profiles: ['default'], installId: 'aaa' } + ], + { primaryConnectionId: 'spark-ts' } + ) + + assert.equal(roster.length, 1) + assert.equal(roster[0].connectionId, 'spark-ts') +}) + +test('roster: collapse pick order is local > ssh > remote > cloud, then registration order', () => { + const local = { id: 'local', kind: 'local' as const, label: 'This device' } + const remote = { id: 'loop', kind: 'remote' as const, label: 'Loopback', url: 'http://127.0.0.1:8642' } + const cloud = { id: 'cl', kind: 'cloud' as const, label: 'Cloud twin', url: 'http://cl:1' } + const ssh = { id: 'tun', kind: 'ssh' as const, label: 'Tunnel', host: 'box' } + + // Same box registered four ways; primary is NOT one of them (unset). + const roster = buildAgentRoster([ + { connection: cloud, profiles: ['default'], installId: 'aaa' }, + { connection: remote, profiles: ['default'], installId: 'aaa' }, + { connection: ssh, profiles: ['default'], installId: 'aaa' }, + { connection: local, profiles: ['default'], installId: 'aaa' } + ]) + + assert.equal(roster.length, 1) + assert.equal(roster[0].connectionId, 'local') + + // Without the local candidate, ssh wins over remote/cloud. + const noLocal = buildAgentRoster([ + { connection: cloud, profiles: ['default'], installId: 'aaa' }, + { connection: remote, profiles: ['default'], installId: 'aaa' }, + { connection: ssh, profiles: ['default'], installId: 'aaa' } + ]) + + assert.equal(noLocal[0].connectionId, 'tun') + + // Same kind → earliest-registered (enumeration order) wins. + const twin = { id: 'loop2', kind: 'remote' as const, label: 'Loopback 2', url: 'http://[::1]:8642' } + const sameKind = buildAgentRoster([ + { connection: remote, profiles: ['default'], installId: 'aaa' }, + { connection: twin, profiles: ['default'], installId: 'aaa' } + ]) + + assert.equal(sameKind[0].connectionId, 'loop') +}) + +test('roster: missing install_id bypasses the collapse (older backends keep current behavior)', () => { + const hostname = { id: 'spark', kind: 'remote' as const, label: 'Spark', url: 'http://spark:8642' } + const tailscale = { id: 'spark-ts', kind: 'remote' as const, label: 'Spark TS', url: 'http://100.1.2.3:8642' } + + // Neither reports an id → both rows survive, handles disambiguate. + const roster = buildAgentRoster([ + { connection: hostname, profiles: ['default'] }, + { connection: tailscale, profiles: ['default'] } + ]) + + assert.equal(roster.length, 2) + assert.deepEqual(roster.map(a => a.handle).sort(), ['default-spark', 'default-spark-ts']) + + // One id + one missing must NOT collapse either (undefined never matches). + const mixed = buildAgentRoster([ + { connection: hostname, profiles: ['default'], installId: 'aaa' }, + { connection: tailscale, profiles: ['default'] } + ]) + + assert.equal(mixed.length, 2) +}) + +test('roster: different install_ids stay separate rows with disambiguated handles', () => { + const spark = { id: 'spark', kind: 'remote' as const, label: 'Spark', url: 'http://spark:8642' } + const mini = { id: 'mini', kind: 'remote' as const, label: 'Mini', url: 'http://mini:8642' } + + const roster = buildAgentRoster([ + { connection: spark, profiles: ['default'], installId: 'aaa' }, + { connection: mini, profiles: ['default'], installId: 'bbb' } + ]) + + assert.equal(roster.length, 2) + assert.deepEqual(roster.map(a => a.handle).sort(), ['default-mini', 'default-spark']) +}) + +test('roster: collapse also folds a third same-box connection from a per-profile v1 override import', () => { + // The reporter's "profile with cron appears as another duplicate": the v1 + // migration imports per-profile override blocks as EXTRA connections, so a + // cron profile pinned to the same box via a third URL becomes a third + // registry entry. Same install_id → still one row per profile. + const hostname = { id: 'spark', kind: 'remote' as const, label: 'Spark', url: 'http://spark:8642' } + const tailscale = { id: 'spark-ts', kind: 'remote' as const, label: 'Spark TS', url: 'http://100.1.2.3:8642' } + const override = { id: 'spark-lan', kind: 'remote' as const, label: 'Spark LAN', url: 'http://192.168.1.5:8642' } + + const roster = buildAgentRoster([ + { connection: hostname, profiles: ['default', 'cron-bot'], installId: 'aaa' }, + { connection: tailscale, profiles: ['default', 'cron-bot'], installId: 'aaa' }, + { connection: override, profiles: ['default', 'cron-bot'], installId: 'aaa' } + ]) + + assert.deepEqual( + roster.map(agent => `${agent.profile}:${agent.handle}`).sort(), + ['cron-bot:cron-bot', 'default:default'] + ) +}) + // --- updateEligibility --- test('update fan-out: cloud is platform-managed, everything else eligible', () => { diff --git a/apps/desktop/electron/connection-registry.ts b/apps/desktop/electron/connection-registry.ts index eae226578b..a8fae85b82 100644 --- a/apps/desktop/electron/connection-registry.ts +++ b/apps/desktop/electron/connection-registry.ts @@ -242,6 +242,12 @@ export interface ConnectionAgents { profiles: null | string[] /** Present when profiles is null: why enumeration was skipped. */ error?: string + /** Stable backend identity from the connection's /api/status (`install_id`). + * Two connections reporting the same id are the SAME physical install + * registered under two addresses (hostname + Tailscale IP), so the roster + * collapses their rows. Absent on older backends → no collapse (fully + * backward compatible). */ + installId?: string } export interface RosterAgent { @@ -333,33 +339,64 @@ export function parseRemoteProfileListing(text: string): string[] { * the duplicate-handle rule ONCE across all sources. Pure so the disambiguation * policy is testable without IPC; main.ts feeds it live enumerations. */ -export function buildAgentRoster(enumerations: ConnectionAgents[]): RosterAgent[] { +export function buildAgentRoster( + enumerations: ConnectionAgents[], + opts: { primaryConnectionId?: string } = {} +): RosterAgent[] { // A connection can transiently report the same profile more than once (or // arrive twice while registry state is reconciling). A roster row represents // one routable identity, so collapse strictly by connection + profile before // counting names for @name-device disambiguation. - const identities = new Map() + const identities = new Map< + string, + { connection: RegistryConnection; installId?: string; order: number; profile: string } + >() + let order = 0 - for (const { connection, profiles } of enumerations) { + for (const { connection, installId, profiles } of enumerations) { for (const profile of profiles || []) { const name = String(profile || '').trim() || 'default' const key = `${connection.id}\0${name}` if (!identities.has(key)) { - identities.set(key, { connection, profile: name }) + identities.set(key, { connection, installId, order, profile: name }) } } + + order += 1 + } + + // Backend-identity collapse: two connections reporting the same install_id + // are the SAME physical install registered under two addresses, so their + // (install, profile) rows are one bot, not two. Connections without an id + // (older backends, undialed ssh) keep a per-connection key — no collapse. + const backends = new Map() + + for (const { connection, installId, order: rank, profile } of identities.values()) { + const key = installId ? `id:${installId}\0${profile}` : `conn:${connection.id}\0${profile}` + const group = backends.get(key) + + if (group) { + group.push({ connection, order: rank, profile }) + } else { + backends.set(key, [{ connection, order: rank, profile }]) + } } + const rows = [...backends.values()].map(group => pickCanonicalConnection(group, opts.primaryConnectionId)) + + // The @name-device duplicate-handle rule runs AFTER the collapse, so a + // profile that only *looked* duplicated (one box, two addresses) keeps its + // bare name once the rows are recognized as one backend. const counts = new Map() - for (const { profile } of identities.values()) { + for (const { profile } of rows) { counts.set(profile, (counts.get(profile) || 0) + 1) } const roster: RosterAgent[] = [] - for (const { connection, profile } of identities.values()) { + for (const { connection, profile } of rows) { roster.push({ connectionId: connection.id, connectionKind: connection.kind, @@ -372,6 +409,32 @@ export function buildAgentRoster(enumerations: ConnectionAgents[]): RosterAgent[ return roster } +/** Deterministic route priority for same-backend rows: local is definitionally + * this box; ssh beats HTTP remotes; cloud last. */ +const CANONICAL_KIND_PRIORITY: Record = { cloud: 3, local: 0, remote: 2, ssh: 1 } + +/** + * Which connection represents a collapsed same-backend roster row: the ACTIVE + * (primary) connection when it is one of the candidates — the row should route + * where the window already routes — else the highest kind priority, else the + * earliest-registered (enumeration order follows registry order). + */ +function pickCanonicalConnection( + candidates: T[], + primaryConnectionId?: string +): T { + const active = primaryConnectionId ? candidates.find(c => c.connection.id === primaryConnectionId) : undefined + + if (active) { + return active + } + + return [...candidates].sort( + (a, b) => + CANONICAL_KIND_PRIORITY[a.connection.kind] - CANONICAL_KIND_PRIORITY[b.connection.kind] || a.order - b.order + )[0] +} + // ── Fan-out update eligibility ────────────────────────────────────────────── export interface UpdateEligibility { diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts index 520c193825..06a1eb9181 100644 --- a/apps/desktop/electron/main.ts +++ b/apps/desktop/electron/main.ts @@ -8200,11 +8200,16 @@ function writeDesktopConnectionsRegistry(registry) { function sanitizeRegistryConnection(entry) { const { token, headers, ...rest } = entry const decrypted = decryptDesktopSecret(token) + // Last-known stable backend identity (from roster enumeration / Test) so + // Settings can hint "Same backend as