Skip to content

The agent that grows with you

fix(desktop): the Screen pane listens for pushed display.status so an external start reaches it

teknium1a35487a5fcf7
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
new file mode 100644
index 00000000000..7adad053f1f
--- /dev/null
+++ b/apps/desktop/src/plugins/hermes-bots/screen-pane-status-events.test.tsx
@@ -0,0 +1,115 @@
+/**
+ * The pane paints a cache of backend truth. A start or stop made outside this
+ * window (CLI, gateway auto-start, another Desktop) reaches the renderer only
+ * as a pushed `display.status` event — a stopped pane with no sibling portal
+ * or hero mounted must pick it up itself or it stays "Stopped" forever.
+ */
+
+import { act, render, waitFor } from '@testing-library/react'
+import { afterEach, beforeEach, expect, it, vi } from 'vitest'
+
+import type { DisplayStatus } from './screen-connection'
+import type * as ScreenConnection from './screen-connection'
+import type { RosterRow } from './types'
+
+vi.mock('@hermes/plugin-sdk', async () => {
+ const { useStore } = await import('@nanostores/react')
+ const { onGatewayEvent } = await import('../../contrib/events')
+
+ return {
+ Button: ({ children, ...props }: React.ButtonHTMLAttributes<HTMLButtonElement>) => <button {...props}>{children}</button>,
+ Codicon: () => null,
+ GlyphSpinner: () => null,
+ EmptyState: () => null,
+ useValue: useStore,
+ host: { onEvent: onGatewayEvent }
+ }
+})
+vi.mock('./routing', () => ({
+ botConnectionRoute: () => ({ connectionId: 'host-a', profile: 'ops', targetProfile: 'ops' })
+}))
+vi.mock('./data', () => ({ botSelectionKey: (bot: RosterRow) => bot.name }))
+vi.mock('./i18n', () => ({
+ useBots: () => ({
+ screen: {
+ title: 'Screen',
+ stoppedTitle: 'Stopped',
+ stoppedBody: 'Body',
+ start: 'Start',
+ agentControls: 'Bot controls',
+ takeOver: 'Take over',
+ reconnect: 'Reconnect',
+ streamLost: 'Stream lost'
+ }
+ })
+}))
+vi.mock('./screen-connection', async importActual => ({
+ // Real predicate (`isEventForBotScreen`): the event must come from the bot's host.
+ ...(await importActual<typeof ScreenConnection>()),
+ displayRequest: vi.fn(),
+ resolveScreenWsUrl: vi.fn(async () => 'ws://localhost/api/display/ws')
+}))
+vi.mock('@novnc/novnc', () => ({
+ default: class {
+ addEventListener() {}
+ disconnect() {}
+ focus() {}
+ }
+}))
+
+// eslint-disable-next-line no-restricted-imports
+import { emitGatewayEvent } from '../../contrib/events'
+
+import { displayRequest } from './screen-connection'
+import { BotScreenPane } from './screen-pane'
+import { $screenState } from './screen-state'
+
+const bot: RosterRow = { name: 'ops', sourceScoped: true, connectionId: 'host-a', connectionKind: 'remote' }
+
+const stopped: DisplayStatus = {
+ profile: 'ops',
+ profile_key: '/home/hermes/.hermes',
+ supported: true,
+ installed: true,
+ missing: [],
+ running: false,
+ pid: null,
+ display: null,
+ socket: null,
+ geometry: '1440x900',
+ install_command: null,
+ lease: { holder: 'agent', viewer_id: null, viewer_hash: null, pending_handoff: null, since: 1, reason: '' }
+}
+
+const running: DisplayStatus = { ...stopped, running: true, pid: 42, display: ':20' }
+
+beforeEach(() => {
+ $screenState.set({})
+ vi.mocked(displayRequest)
+ .mockReset()
+ .mockImplementation(async (_bot, method) => (method === 'display.observe' ? { ...running, ticket: 't', viewer_id: 'v1' } : stopped))
+ vi.stubGlobal(
+ 'WebSocket',
+ class {
+ binaryType = ''
+ addEventListener() {}
+ close() {}
+ }
+ )
+})
+
+afterEach(() => vi.unstubAllGlobals())
+
+it('a stopped pane learns of an external start from the pushed display.status event and attaches', async () => {
+ const view = render(<BotScreenPane bot={bot} />)
+ await waitFor(() => expect(view.getByText('Stopped')).toBeTruthy())
+
+ // Another host's event about the same profile path is not ours.
+ act(() => emitGatewayEvent({ type: 'display.status', connectionId: 'host-b', profile: 'ops', payload: running }))
+ expect(view.getByText('Stopped')).toBeTruthy()
+
+ act(() => emitGatewayEvent({ type: 'display.status', connectionId: 'host-a', profile: 'ops', payload: running }))
+ await waitFor(() => expect(vi.mocked(displayRequest)).toHaveBeenCalledWith(bot, 'display.observe'))
+ expect(view.queryByText('Stopped')).toBeNull()
+ view.unmount()
+})
diff --git a/apps/desktop/src/plugins/hermes-bots/screen-pane.tsx b/apps/desktop/src/plugins/hermes-bots/screen-pane.tsx
index 3d1a3427fe6..6b33930aff6 100644
--- a/apps/desktop/src/plugins/hermes-bots/screen-pane.tsx
+++ b/apps/desktop/src/plugins/hermes-bots/screen-pane.tsx
@@ -86,13 +86,29 @@ export function BotScreenPane({ bot }: { bot: RosterRow }) {
useEffect(() => {
void refresh()
- return host.onEvent('display.lease', (event: RpcEvent) => {
+ const offLease = host.onEvent('display.lease', (event: RpcEvent) => {
const payload = event.payload as { lease?: DisplayLease } | undefined
if (payload?.lease && isEventForBotScreen(bot, event, status?.profile_key)) {
setScreenLease(bot, payload.lease)
}
})
+
+ // A start/stop made outside this window (CLI, gateway auto-start, another Desktop) is
+ // pushed by the serve-side runtime watcher. A stopped pane with no sibling portal or
+ // hero mounted for this bot has no other way to learn of it.
+ const offStatus = host.onEvent('display.status', (event: RpcEvent) => {
+ const payload = event.payload as DisplayStatus | undefined
+
+ if (payload?.profile_key && isEventForBotScreen(bot, event, status?.profile_key)) {
+ setScreenStatus(bot, payload)
+ }
+ })
+
+ return () => {
+ offLease()
+ offStatus()
+ }
}, [bot, refresh, status?.profile_key])
const detach = useCallback((handBack = false) => {