Skip to content

The agent that grows with you

fix(bot-relay): a failed roster clear is retried, not spent

John Paul Soliva2cf241210a62
diff --git a/apps/desktop/src/plugins/hermes-bots/relay.test.ts b/apps/desktop/src/plugins/hermes-bots/relay.test.ts
index 7674b29592f..ba8f5e8bd16 100644
--- a/apps/desktop/src/plugins/hermes-bots/relay.test.ts
+++ b/apps/desktop/src/plugins/hermes-bots/relay.test.ts
@@ -741,6 +741,52 @@ describe('the roster loop forgets a machine that left', () => {
stopBotRelay()
})
+ it('retries the clear on the next tick when the push fails, instead of spending it', async () => {
+ // The clear is one-shot per sole connection, so spending it before the push lands means a
+ // failure is never retried: the surviving gateway keeps the departed machine's agents in every
+ // bot's prompt, and as message_agent targets, for the life of the Desktop. requestProfile
+ // throws on a socket that is not up yet, not only on a backend too old to have the RPC.
+ let clearFails = true
+
+ const calls = respondWith(call => {
+ if (call.method === 'profiles.list') {
+ return { profiles: [{ name: call.connectionId === 'a' ? 'default' : 'ops' }] }
+ }
+
+ if (call.method === 'bot_relay.roster.sync' && clearFails && !(call.params.agents as unknown[]).length) {
+ throw new Error('Hermes gateway is not connected')
+ }
+
+ return {}
+ })
+
+ const { startBotRelay, stopBotRelay } = await loadRelay()
+
+ startBotRelay()
+ await vi.advanceTimersByTimeAsync(0)
+ calls.length = 0
+ hostMock.profileRoutes = vi.fn(async () => [route('a')])
+
+ // b leaves; the clear is attempted and fails.
+ await vi.advanceTimersByTimeAsync(60_000)
+ expect(calls.filter(call => call.method === 'bot_relay.roster.sync')).toHaveLength(1)
+ calls.length = 0
+
+ // Next tick must try again rather than treat the roster as already forgotten.
+ clearFails = false
+ await vi.advanceTimersByTimeAsync(60_000)
+ expect(calls.filter(call => call.method === 'bot_relay.roster.sync')).toEqual([
+ expect.objectContaining({ connectionId: 'a', params: { agents: [] } })
+ ])
+
+ // And once it lands it is spent, exactly as before.
+ calls.length = 0
+ await vi.advanceTimersByTimeAsync(60_000)
+ expect(calls.filter(call => call.method === 'bot_relay.roster.sync')).toEqual([])
+
+ stopBotRelay()
+ })
+
it('clears the roster of a sole connection that replaced the previous sole one', async () => {
const calls = respondWith(call => {
if (call.method === 'profiles.list') {
diff --git a/apps/desktop/src/plugins/hermes-bots/relay.ts b/apps/desktop/src/plugins/hermes-bots/relay.ts
index 6086e0beb8c..6bcc19373c9 100644
--- a/apps/desktop/src/plugins/hermes-bots/relay.ts
+++ b/apps/desktop/src/plugins/hermes-bots/relay.ts
@@ -268,16 +268,25 @@ async function syncRelayRosters() {
// forgets it — a replacement sole connection has never been told. An
// empty route list (registry not loaded yet) must not spend the clear.
if (connections.length === 1 && connections[0].id !== relay.rosterClearedFor) {
- relay.rosterClearedFor = connections[0].id
- await Promise.all(
+ const cleared = await Promise.all(
connections.map(async connection => {
try {
await host.requestProfile(connection.route, 'bot_relay.roster.sync', { agents: [] })
+
+ return true
} catch {
- // Older backend without the relay RPCs — skip this connection.
+ // An older backend without the relay RPCs, but also a socket that is not up yet —
+ // requestProfile throws for both. Either way this gateway still holds the departed
+ // machine's agents, so the clear is NOT spent and the next tick tries again.
+ return false
}
})
)
+
+ // Spend it only once every gateway has actually forgotten.
+ if (cleared.every(Boolean)) {
+ relay.rosterClearedFor = connections[0].id
+ }
}
return