Skip to content

The agent that grows with you

fix(desktop): the relay clears the remaining gateways' remote roster when the peer set drops below two

John Paul Soliva8b091e539c96
diff --git a/apps/desktop/src/plugins/hermes-bots/relay.test.ts b/apps/desktop/src/plugins/hermes-bots/relay.test.ts
index 84cd45a7a6f..8f19c429977 100644
--- a/apps/desktop/src/plugins/hermes-bots/relay.test.ts
+++ b/apps/desktop/src/plugins/hermes-bots/relay.test.ts
@@ -456,7 +456,9 @@ describe('the roster loop pushes the OTHER connections’ agents', () => {
stopBotRelay()
})
- it('stays quiet with a single connection — there is no peer to relay to', async () => {
+ it('with a single connection it only clears that gateway’s remote roster — there is no peer to relay to', async () => {
+ // The one call is the clear: the gateway may still hold a roster pushed
+ // while a second machine was registered (see 'forgets a machine that left').
hostMock.profileRoutes = vi.fn(async () => [route('a')])
const calls = respondWith(() => ({}))
@@ -465,7 +467,10 @@ describe('the roster loop pushes the OTHER connections’ agents', () => {
startBotRelay()
await vi.advanceTimersByTimeAsync(0)
- expect(calls).toHaveLength(0)
+ expect(calls).toEqual([expect.objectContaining({ connectionId: 'a', method: 'bot_relay.roster.sync', params: { agents: [] } })])
+
+ await vi.advanceTimersByTimeAsync(60_000)
+ expect(calls).toHaveLength(1)
stopBotRelay()
})
@@ -672,3 +677,42 @@ describe('stop halts both loops', () => {
stopBotRelay()
})
})
+
+describe('the roster loop forgets a machine that left', () => {
+ it('clears each remaining gateway’s remote roster once when the peer set drops below two', async () => {
+ const calls = respondWith(call => {
+ if (call.method === 'profiles.list') {
+ return { profiles: [{ name: call.connectionId === 'a' ? 'default' : 'ops' }] }
+ }
+
+ return {}
+ })
+
+ const { startBotRelay, stopBotRelay } = await loadRelay()
+
+ startBotRelay()
+ await vi.advanceTimersByTimeAsync(0)
+ expect(calls.filter(call => call.method === 'bot_relay.roster.sync')).toHaveLength(2)
+ calls.length = 0
+
+ // b leaves the registry. Without a push, a's gateway keeps ops in its
+ // roster (and as a message_agent target) until a second peer shows up.
+ hostMock.profileRoutes = vi.fn(async () => [route('a')])
+ await vi.advanceTimersByTimeAsync(60_000)
+
+ const syncs = calls.filter(call => call.method === 'bot_relay.roster.sync')
+ expect(syncs).toEqual([expect.objectContaining({ connectionId: 'a', params: { agents: [] } })])
+
+ // Once, not on every tick.
+ calls.length = 0
+ await vi.advanceTimersByTimeAsync(60_000)
+ expect(calls.filter(call => call.method === 'bot_relay.roster.sync')).toEqual([])
+
+ // A returning peer starts the union pushes again.
+ hostMock.profileRoutes = vi.fn(async () => [route('a'), route('b')])
+ await vi.advanceTimersByTimeAsync(60_000)
+ expect(calls.filter(call => call.method === 'bot_relay.roster.sync').map(call => call.connectionId)).toEqual(['a', 'b'])
+
+ stopBotRelay()
+ })
+})
diff --git a/apps/desktop/src/plugins/hermes-bots/relay.ts b/apps/desktop/src/plugins/hermes-bots/relay.ts
index 91adf8f7f79..95a68835524 100644
--- a/apps/desktop/src/plugins/hermes-bots/relay.ts
+++ b/apps/desktop/src/plugins/hermes-bots/relay.ts
@@ -80,6 +80,8 @@ interface RelayLifecycle {
pushDebounceTimer: null | ReturnType<typeof setTimeout>
pushUnsub: (() => void) | null
rosterBusy: boolean
+ /** The below-two-connections roster clear went out; stays set until the peer set relays again. */
+ rosterCleared: boolean
rosterTimer: null | ReturnType<typeof setInterval>
}
@@ -91,6 +93,7 @@ const relay: RelayLifecycle = {
pushDebounceTimer: null,
pushUnsub: null,
rosterBusy: false,
+ rosterCleared: false,
rosterTimer: null
}
@@ -258,9 +261,28 @@ async function syncRelayRosters() {
const connections = await relayConnections()
if (connections.length < 2) {
+ // Nothing to relay — but the gateways that remain still hold the last
+ // pushed roster, so a departed machine's agents would stay in every
+ // bot's prompt (and as message_agent targets) until a second connection
+ // reappears. Push the now-empty roster once so they forget it.
+ if (!relay.rosterCleared) {
+ relay.rosterCleared = true
+ await Promise.all(
+ connections.map(async connection => {
+ try {
+ await host.requestProfile(connection.route, 'bot_relay.roster.sync', { agents: [] })
+ } catch {
+ // Older backend without the relay RPCs — skip this connection.
+ }
+ })
+ )
+ }
+
return
}
+ relay.rosterCleared = false
+
const agentsByConnection = new Map<string, RelayAgentRow[]>()
await Promise.all(
connections.map(async connection => {
@@ -463,6 +485,7 @@ function scheduleRelayPushDrain() {
export function startBotRelay() {
relay.disposed = false
+ relay.rosterCleared = false
// Source-shape test harnesses evaluate plugin.js without DOM timers —
// the relay only runs where a real event loop exists.