Skip to content

The agent that grows with you

test(bot-mode): pin the roster busy flag across a stop/restart overlap

ethernet91b17917e605
diff --git a/apps/desktop/src/plugins/hermes-bots/relay.test.ts b/apps/desktop/src/plugins/hermes-bots/relay.test.ts
index 9b54fbbc46c..e91894f09ab 100644
--- a/apps/desktop/src/plugins/hermes-bots/relay.test.ts
+++ b/apps/desktop/src/plugins/hermes-bots/relay.test.ts
@@ -520,35 +520,45 @@ 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')
@@ -556,6 +566,7 @@ describe('the roster loop pushes the OTHER connections’ agents', () => {
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
@@ -566,17 +577,74 @@ describe('the roster loop pushes the OTHER connections’ agents', () => {
later.every(call => (call.params.agents as Array<{ profile: string }>).every(row => row.profile === 'current'))
).toBe(true)
}
+
+ stopBotRelay()
+ })
+
+ it('a stale run finishing does not free the restarted run’s busy flag', async () => {
+ const holds: Record<string, { release: () => void; promise: Promise<void> }> = {}
+
+ for (const id of ['stale', 'current']) {
+ let release!: () => void
+
+ const promise = new Promise<void>(resolve => {
+ release = resolve
+ })
+
+ holds[id] = { promise, release }
+ }
+
+ const seen: Record<string, number> = { a: 0, b: 0 }
+
+ const calls = respondWith(async call => {
+ if (call.method === 'profiles.list') {
+ seen[call.connectionId] += 1
+
+ // Run 1 parks on b, the restarted run parks on a.
+ if (call.connectionId === 'b' && seen.b === 1) {
+ await holds.stale.promise
+ } else if (call.connectionId === 'a' && seen.a === 2) {
+ await holds.current.promise
+ }
+
+ return { profiles: [{ name: call.connectionId }] }
+ }
+
+ return {}
+ })
+
+ const { startBotRelay, stopBotRelay } = await loadRelay()
+ const listCount = () => calls.filter(call => call.method === 'profiles.list').length
+
+ startBotRelay()
+ await vi.advanceTimersByTimeAsync(0)
+ stopBotRelay()
+ startBotRelay()
+ await vi.advanceTimersByTimeAsync(0)
+ expect(listCount()).toBe(4)
+
+ // The stale run lands while the restarted run is still in flight; the
+ // next tick must still see that run's busy flag, not start a third.
+ holds.stale.release()
+ await vi.advanceTimersByTimeAsync(60_000)
+ expect(listCount()).toBe(4)
+
+ holds.current.release()
+ await vi.advanceTimersByTimeAsync(0)
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(() => ({}))
diff --git a/apps/desktop/src/plugins/hermes-bots/relay.ts b/apps/desktop/src/plugins/hermes-bots/relay.ts
index 4f4c68f44f3..93226cebadc 100644
--- a/apps/desktop/src/plugins/hermes-bots/relay.ts
+++ b/apps/desktop/src/plugins/hermes-bots/relay.ts
@@ -348,10 +348,13 @@ async function syncRelayRosters() {
try {
const connections = await relayConnections()
+
if (!isCurrent()) {
return
}
+
const labels = await connectionLabels()
+
if (!isCurrent()) {
return
}
@@ -369,6 +372,7 @@ async function syncRelayRosters() {
if (!isCurrent()) {
return false
}
+
try {
await host.requestProfile(connection.route, 'bot_relay.roster.sync', { agents: [] })
@@ -397,6 +401,7 @@ async function syncRelayRosters() {
await Promise.all(
connections.map(async connection => {
const agents = await relayAgentsOn(connection, labels)
+
if (!isCurrent()) {
return
}
@@ -433,6 +438,7 @@ async function syncRelayRosters() {
if (!isCurrent()) {
return
}
+
const others: RelayAgentRow[] = []
for (const [id, agents] of agentsByConnection) {