Skip to content

The agent that grows with you

fix(bot-mode): discard stopped relay roster refreshes

Gille8dc829288381
diff --git a/apps/desktop/src/plugins/hermes-bots/relay.test.ts b/apps/desktop/src/plugins/hermes-bots/relay.test.ts
index e8188e1d209..9b54fbbc46c 100644
--- a/apps/desktop/src/plugins/hermes-bots/relay.test.ts
+++ b/apps/desktop/src/plugins/hermes-bots/relay.test.ts
@@ -158,6 +158,7 @@ beforeEach(() => {
hostMock.profileRoutes = vi.fn(async () => [route('a'), route('b')])
hostMock.requestProfile = vi.fn(async () => ({}))
hostMock.retainProfileSocket = vi.fn(() => vi.fn())
+ delete hostMock.connections
})
afterEach(() => {
@@ -517,6 +518,79 @@ describe('relay-route socket retention (#93594)', () => {
})
describe('the roster loop pushes the OTHER connections’ agents', () => {
+ it.each([false, true])('discards a stopped roster fetch (restart=%s)', async restart => {
+ let release!: () => void
+ const held = new Promise<void>(resolve => {
+ release = resolve
+ })
+ let first = true
+ let failB = false
+ const calls = respondWith(async call => {
+ if (call.method === 'profiles.list') {
+ if (call.connectionId === 'b' && failB) {
+ throw new Error('temporary profile lookup failure')
+ }
+ if (call.connectionId === 'b' && first) {
+ first = false
+ await held
+ return { profiles: [{ name: 'stale' }] }
+ }
+ return { profiles: [{ name: 'current' }] }
+ }
+ return {}
+ })
+ const { startBotRelay, stopBotRelay } = await loadRelay()
+ startBotRelay()
+ await vi.advanceTimersByTimeAsync(0)
+ expect(calls.filter(call => call.method === 'profiles.list')).toHaveLength(2)
+ stopBotRelay()
+ if (restart) {
+ startBotRelay()
+ await vi.advanceTimersByTimeAsync(0)
+ expect(calls.filter(call => call.method === 'bot_relay.roster.sync')).toHaveLength(2)
+ }
+ release()
+ await vi.advanceTimersByTimeAsync(0)
+ const syncs = calls.filter(call => call.method === 'bot_relay.roster.sync')
+ expect(syncs).toHaveLength(restart ? 2 : 0)
+ expect(
+ syncs.every(call => (call.params.agents as Array<{ profile: string }>).every(row => row.profile === 'current'))
+ ).toBe(true)
+ if (restart) {
+ // A late old response must not poison the new run's fallback cache.
+ failB = true
+ await vi.advanceTimersByTimeAsync(60_000)
+ const later = calls.filter(call => call.method === 'bot_relay.roster.sync').slice(2)
+ expect(later).toHaveLength(2)
+ expect(
+ later.every(call => (call.params.agents as Array<{ profile: string }>).every(row => row.profile === 'current'))
+ ).toBe(true)
+ }
+ stopBotRelay()
+ })
+
+ it('does not clear a sole gateway after stopping during label lookup', async () => {
+ let release!: () => void
+ const held = new Promise<void>(resolve => {
+ release = resolve
+ })
+ hostMock.profileRoutes = vi.fn(async () => [route('a')])
+ hostMock.connections = vi.fn(async () => {
+ await held
+ return []
+ })
+ const calls = respondWith(() => ({}))
+ const { startBotRelay, stopBotRelay } = await loadRelay()
+ startBotRelay()
+ await vi.advanceTimersByTimeAsync(0)
+ expect(hostMock.connections).toHaveBeenCalled()
+ stopBotRelay()
+ release()
+ await vi.advanceTimersByTimeAsync(0)
+ expect(calls.filter(call => call.method === 'bot_relay.roster.sync')).toEqual([])
+ delete hostMock.connections
+ })
+
it('gives each gateway a union roster that excludes its own agents', 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 957bc386030..4f4c68f44f3 100644
--- a/apps/desktop/src/plugins/hermes-bots/relay.ts
+++ b/apps/desktop/src/plugins/hermes-bots/relay.ts
@@ -55,6 +55,8 @@ interface RelayLifecycle {
pushDebounceTimer: null | ReturnType<typeof setTimeout>
pushUnsub: (() => void) | null
rosterBusy: boolean
+ /** Invalidates pending roster reads when the relay stops or restarts. */
+ rosterGeneration: number
/** Id of the sole connection whose roster clear went out; null once the peer set relays again. */
rosterClearedFor: null | string
rosterTimer: null | ReturnType<typeof setInterval>
@@ -68,6 +70,7 @@ const relay: RelayLifecycle = {
pushDebounceTimer: null,
pushUnsub: null,
rosterBusy: false,
+ rosterGeneration: 0,
rosterClearedFor: null,
rosterTimer: null
}
@@ -340,10 +343,18 @@ async function syncRelayRosters() {
}
relay.rosterBusy = true
+ const generation = relay.rosterGeneration
+ const isCurrent = () => !relay.disposed && relay.rosterGeneration === generation && relayBotModeOn()
try {
const connections = await relayConnections()
+ if (!isCurrent()) {
+ return
+ }
const labels = await connectionLabels()
+ if (!isCurrent()) {
+ return
+ }
if (connections.length < 2) {
// Nothing to relay — but the gateways that remain still hold the last
@@ -355,6 +366,9 @@ async function syncRelayRosters() {
if (connections.length === 1 && connections[0].id !== relay.rosterClearedFor) {
const cleared = await Promise.all(
connections.map(async connection => {
+ if (!isCurrent()) {
+ return false
+ }
try {
await host.requestProfile(connection.route, 'bot_relay.roster.sync', { agents: [] })
@@ -369,7 +383,7 @@ async function syncRelayRosters() {
)
// Spend it only once every gateway has actually forgotten.
- if (cleared.every(Boolean)) {
+ if (isCurrent() && cleared.every(Boolean)) {
relay.rosterClearedFor = connections[0].id
}
}
@@ -383,6 +397,9 @@ async function syncRelayRosters() {
await Promise.all(
connections.map(async connection => {
const agents = await relayAgentsOn(connection, labels)
+ if (!isCurrent()) {
+ return
+ }
if (agents === null) {
// Transient fetch failure: reuse the last good rows for this
@@ -397,6 +414,10 @@ async function syncRelayRosters() {
})
)
+ if (!isCurrent()) {
+ return
+ }
+
// Connections gone from profileRoutes are genuinely disconnected — drop
// their cache so a later reconnect starts from live data.
const liveIds = new Set(connections.map(connection => connection.id))
@@ -409,6 +430,9 @@ async function syncRelayRosters() {
await Promise.all(
connections.map(async connection => {
+ if (!isCurrent()) {
+ return
+ }
const others: RelayAgentRow[] = []
for (const [id, agents] of agentsByConnection) {
@@ -427,7 +451,9 @@ async function syncRelayRosters() {
})
)
} finally {
- relay.rosterBusy = false
+ if (relay.rosterGeneration === generation) {
+ relay.rosterBusy = false
+ }
}
}
@@ -676,6 +702,8 @@ export function startBotRelay() {
export function stopBotRelay() {
relay.disposed = true
+ relay.rosterGeneration += 1
+ relay.rosterBusy = false
// A rerun remembered mid-drain must not leak into the next start —
// it would fire one stale drain after restart.
relay.drainRerun = false