Skip to content

The agent that grows with you

fix(bot-screen): a bot whose connection was removed no longer throws from screen listeners

teknium1817d9d3831e4
diff --git a/apps/desktop/src/plugins/hermes-bots/screen-connection.test.ts b/apps/desktop/src/plugins/hermes-bots/screen-connection.test.ts
index 3894ada4112..8a6875d35bd 100644
--- a/apps/desktop/src/plugins/hermes-bots/screen-connection.test.ts
+++ b/apps/desktop/src/plugins/hermes-bots/screen-connection.test.ts
@@ -6,6 +6,7 @@
import { describe, expect, it, vi } from 'vitest'
+import type * as routing from './routing'
import type { RosterRow } from './types'
const routeMock = vi.fn<() => { connectionId: string; profile: string } | null>(() => null)
@@ -15,13 +16,38 @@ vi.mock('@hermes/plugin-sdk', () => ({
resolveSiblingWsUrl: vi.fn()
}))
-vi.mock('./routing', () => ({
- botConnectionRoute: () => routeMock()
-}))
+vi.mock('./routing', async importOriginal => {
+ const actual = await importOriginal<typeof routing>()
+
+ // A mocked route stands in for a resolved registry row; without one the real resolver runs, so
+ // an orphaned (`owner_removed`) row behaves exactly as it does in the app.
+ const resolveBotConnectionRoute = (bot: RosterRow): ReturnType<typeof actual.resolveBotConnectionRoute> => {
+ const route = routeMock()
+
+ return route ? { status: 'resolved', route: { ...route, mode: 'remote', targetProfile: route.profile } } : actual.resolveBotConnectionRoute(bot)
+ }
+
+ return {
+ ...actual,
+ resolveBotConnectionRoute,
+ botConnectionRoute: (bot: RosterRow) => {
+ const resolved = resolveBotConnectionRoute(bot)
+
+ if (resolved.status === 'owner_removed') {
+ throw new Error(`Bot ${resolved.profile} has no connection owner`)
+ }
+
+ return resolved.route
+ }
+ }
+})
+
+import { host } from '@hermes/plugin-sdk'
-import { isEventForBotScreen } from './screen-connection'
+import { displayRequest, isEventForBotScreen } from './screen-connection'
const bot = { name: 'ops' } as RosterRow
+const orphan = { name: 'ops', remoteSource: true } as RosterRow
const key = '/home/hermes/.hermes'
describe('isEventForBotScreen', () => {
@@ -41,4 +67,20 @@ describe('isEventForBotScreen', () => {
expect(isEventForBotScreen(bot, { payload: { profile_key: key }, type: 'display.lease' }, key)).toBe(true)
expect(isEventForBotScreen(bot, { payload: { profile_key: '/other' }, type: 'display.lease' }, key)).toBe(false)
})
+
+ it('treats a row whose connection was removed as having no screen instead of throwing', () => {
+ routeMock.mockReturnValue(null)
+
+ // Runs for EVERY display.* event, so a throw here would kill the listener for a stale sidebar row.
+ expect(isEventForBotScreen(orphan, { payload: { profile_key: '/x' } } as never, '/x')).toBe(false)
+ })
+})
+
+describe('displayRequest', () => {
+ it('rejects instead of throwing synchronously for a row whose connection was removed', async () => {
+ routeMock.mockReturnValue(null)
+
+ await expect(displayRequest(orphan, 'display.status')).rejects.toThrow(/no connection owner/)
+ expect(host.requestProfile).not.toHaveBeenCalled()
+ })
})
diff --git a/apps/desktop/src/plugins/hermes-bots/screen-connection.ts b/apps/desktop/src/plugins/hermes-bots/screen-connection.ts
index e72d0b97e37..6256c2f904d 100644
--- a/apps/desktop/src/plugins/hermes-bots/screen-connection.ts
+++ b/apps/desktop/src/plugins/hermes-bots/screen-connection.ts
@@ -13,7 +13,7 @@
import { host, resolveSiblingWsUrl } from '@hermes/plugin-sdk'
import type { DisplayLease, PluginProfileRoute, RpcEvent } from '@hermes/plugin-sdk'
-import { botConnectionRoute } from './routing'
+import { resolveBotConnectionRoute } from './routing'
import type { RosterRow } from './types'
// Wire shapes come from the generated contract (Python is the source: `tui_gateway/contracts/display.py`).
@@ -59,9 +59,20 @@ export function isDisplayUnavailable(error: unknown): boolean {
return message.includes('method not found') || message.includes('method-not-found')
}
-/** Bare-profile fallback so a v1 local bot (no registry route) still resolves. */
-export function botScreenRoute(bot: RosterRow): PluginProfileRoute | string {
- return botConnectionRoute(bot) ?? bot.name
+/**
+ * Where `display.*` for `bot` goes. Passive resolution (the typed resolver, never
+ * the throwing dispatch wrapper): a source-scoped row whose connection was deleted
+ * is a settled "no screen" (null), and a v1 local bot with no registry route falls
+ * back to its bare profile name.
+ */
+export function botScreenRoute(bot: RosterRow): PluginProfileRoute | string | null {
+ const resolved = resolveBotConnectionRoute(bot)
+
+ if (resolved.status === 'resolved') {
+ return resolved.route
+ }
+
+ return resolved.status === 'not_scoped' ? bot.name : null
}
/**
@@ -76,14 +87,28 @@ export function isEventForBotScreen(bot: RosterRow, event: RpcEvent, profileKey:
return false
}
- const expected = botConnectionRoute(bot)?.connectionId ?? null
+ // Only now resolve the route: this runs for every display.* event, and an orphaned row (its
+ // connection deleted) owns no screen rather than being an error to throw from a listener.
+ const resolved = resolveBotConnectionRoute(bot)
+
+ if (resolved.status === 'owner_removed') {
+ return false
+ }
+
+ const expected = resolved.route?.connectionId ?? null
const actual = event.connectionId ?? null
return expected === actual || (expected === 'local' && actual === null)
}
export function displayRequest<T>(bot: RosterRow, method: string, params: Record<string, unknown> = {}): Promise<T> {
- return host.requestProfile<T>(botScreenRoute(bot), method, params)
+ const route = botScreenRoute(bot)
+
+ if (route === null) {
+ return Promise.reject(new Error(`Bot ${bot.name} has no connection owner`))
+ }
+
+ return host.requestProfile<T>(route, method, params)
}
/**
@@ -96,12 +121,14 @@ export function displayRequest<T>(bot: RosterRow, method: string, params: Record
export async function retainBotScreen(bot: RosterRow): Promise<() => void> {
const noop = () => undefined
- if (typeof host.retainProfile !== 'function') {
+ const route = botScreenRoute(bot)
+
+ if (typeof host.retainProfile !== 'function' || route === null) {
return noop
}
try {
- const release = await host.retainProfile(botScreenRoute(bot))
+ const release = await host.retainProfile(route)
return typeof release === 'function' ? release : noop
} catch {
@@ -115,7 +142,13 @@ export async function retainBotScreen(bot: RosterRow): Promise<() => void> {
* bridge and the single-use display ticket attached.
*/
export async function resolveScreenWsUrl(bot: RosterRow, ticket: string): Promise<string> {
- const route = botConnectionRoute(bot)
+ const resolved = resolveBotConnectionRoute(bot)
+
+ if (resolved.status === 'owner_removed') {
+ throw new Error(`Bot ${bot.name} has no connection owner`)
+ }
+
+ const route = resolved.route
// The /api/ws credential authenticated the RPC that minted the display ticket;
// the bridge authenticates on the ticket alone, so the gateway credential is
diff --git a/apps/desktop/src/plugins/hermes-bots/screen-install-retention.test.tsx b/apps/desktop/src/plugins/hermes-bots/screen-install-retention.test.tsx
index aabb93e6afb..250b3fb77e6 100644
--- a/apps/desktop/src/plugins/hermes-bots/screen-install-retention.test.tsx
+++ b/apps/desktop/src/plugins/hermes-bots/screen-install-retention.test.tsx
@@ -38,9 +38,11 @@ vi.mock('@hermes/plugin-sdk', async () => {
}
}
})
-vi.mock('./routing', () => ({
- botConnectionRoute: () => ({ connectionId: 'host-a', profile: 'ops', targetProfile: 'ops' })
-}))
+vi.mock('./routing', () => {
+ const route = { connectionId: 'host-a', mode: 'remote', profile: 'ops', targetProfile: 'ops' }
+
+ return { botConnectionRoute: () => route, resolveBotConnectionRoute: () => ({ status: 'resolved', route }) }
+})
vi.mock('./i18n', () => ({
useBots: () => ({
screen: {
diff --git a/apps/desktop/src/plugins/hermes-bots/screen-pane-lifecycle.test.tsx b/apps/desktop/src/plugins/hermes-bots/screen-pane-lifecycle.test.tsx
index fc71f21f25f..194c4447a6f 100644
--- a/apps/desktop/src/plugins/hermes-bots/screen-pane-lifecycle.test.tsx
+++ b/apps/desktop/src/plugins/hermes-bots/screen-pane-lifecycle.test.tsx
@@ -38,9 +38,11 @@ vi.mock('@hermes/plugin-sdk', async () => {
}
}
})
-vi.mock('./routing', () => ({
- botConnectionRoute: () => ({ connectionId: 'host-a', profile: 'default', targetProfile: 'default' })
-}))
+vi.mock('./routing', () => {
+ const route = { connectionId: 'host-a', mode: 'remote', profile: 'default', targetProfile: 'default' }
+
+ return { botConnectionRoute: () => route, resolveBotConnectionRoute: () => ({ status: 'resolved', route }) }
+})
vi.mock('./data', () => ({ botSelectionKey: (bot: RosterRow) => bot.name }))
vi.mock('./i18n', () => ({
useBots: () => ({
diff --git a/apps/desktop/src/plugins/hermes-bots/screen-pane-status-events.test.tsx b/apps/desktop/src/plugins/hermes-bots/screen-pane-status-events.test.tsx
index cefacbf7848..1f318f3a182 100644
--- a/apps/desktop/src/plugins/hermes-bots/screen-pane-status-events.test.tsx
+++ b/apps/desktop/src/plugins/hermes-bots/screen-pane-status-events.test.tsx
@@ -27,9 +27,11 @@ vi.mock('@hermes/plugin-sdk', async () => {
host: { onEvent: onGatewayEvent }
}
})
-vi.mock('./routing', () => ({
- botConnectionRoute: () => ({ connectionId: 'host-a', profile: 'ops', targetProfile: 'ops' })
-}))
+vi.mock('./routing', () => {
+ const route = { connectionId: 'host-a', mode: 'remote', profile: 'ops', targetProfile: 'ops' }
+
+ return { botConnectionRoute: () => route, resolveBotConnectionRoute: () => ({ status: 'resolved', route }) }
+})
vi.mock('./data', () => ({ botSelectionKey: (bot: RosterRow) => bot.name }))
vi.mock('./i18n', () => ({
useBots: () => ({