Skip to content

The agent that grows with you

fix(bot-screen): hand back on intentional pane closure

Yags26c4be0a5395
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
new file mode 100644
index 00000000000..55ce8b6e40c
--- /dev/null
+++ b/apps/desktop/src/plugins/hermes-bots/screen-pane-lifecycle.test.tsx
@@ -0,0 +1,129 @@
+import { act, fireEvent, render, waitFor } from '@testing-library/react'
+import { afterEach, beforeEach, expect, it, vi } from 'vitest'
+
+import type { DisplayStatus } from './screen-connection'
+import type { RosterRow } from './types'
+
+const sockets = vi.hoisted(() => [] as Array<{ closeCodes: number[]; closed: boolean; close: (code?: number) => void }>)
+
+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('./data', () => ({ botSelectionKey: (bot: RosterRow) => bot.name }))
+vi.mock('./i18n', () => ({
+ useBots: () => ({
+ screen: {
+ title: 'Screen',
+ youControl: 'You control',
+ handBack: 'Hand back',
+ takeOver: 'Take over',
+ reconnect: 'Reconnect',
+ streamLost: 'Stream lost'
+ }
+ })
+}))
+vi.mock('./screen-connection', () => ({
+ VIEWER_ID: 'this-viewer',
+ displayRequest: vi.fn(),
+ resolveScreenWsUrl: vi.fn(async () => 'ws://localhost/api/display/ws'),
+ isEventForBotScreen: () => false
+}))
+vi.mock('@novnc/novnc', () => ({
+ default: class {
+ constructor(
+ _target: HTMLElement,
+ private socket: { close: () => void }
+ ) {}
+ addEventListener(type: string, callback: () => void) {
+ if (type === 'connect') {
+ queueMicrotask(callback)
+ }
+ }
+ // noVNC 1.7 disconnects its WebSocket without a close code.
+ disconnect() {
+ this.socket.close()
+ }
+ focus() {}
+ }
+}))
+
+import { displayRequest } from './screen-connection'
+import { BotScreenPane } from './screen-pane'
+import { $screenState } from './screen-state'
+
+const bot: RosterRow = { name: 'default' }
+
+const status: DisplayStatus = {
+ profile: 'default',
+ profile_key: '/home/hermes/.hermes',
+ supported: true,
+ installed: true,
+ missing: [],
+ running: true,
+ pid: 42,
+ display: ':20',
+ socket: '/tmp/rfb.sock',
+ geometry: '1440x900',
+ install_command: null,
+ lease: { holder: 'human', viewer_id: 'this-viewer', pending_handoff: null, since: 1, reason: '' }
+}
+
+beforeEach(() => {
+ $screenState.set({})
+ sockets.length = 0
+ vi.mocked(displayRequest)
+ .mockReset()
+ .mockResolvedValue({ ...status, ticket: 'test-ticket', viewer_id: 'this-viewer' })
+ vi.stubGlobal(
+ 'WebSocket',
+ class {
+ closeCodes: number[] = []
+ closed = false
+ constructor() {
+ sockets.push(this)
+ }
+ close(code?: number) {
+ // Subsequent close calls cannot replace the frame already sent to the server.
+ if (this.closed) {
+ return
+ }
+
+ this.closed = true
+ this.closeCodes.push(code ?? 1005)
+ }
+ }
+ )
+})
+
+afterEach(() => vi.unstubAllGlobals())
+
+it('sends an intentional close before noVNC can send its statusless close on pane unmount', async () => {
+ const view = render(<BotScreenPane bot={bot} />)
+ await waitFor(() => expect(sockets).toHaveLength(1))
+ await act(async () => {})
+ view.unmount()
+ expect(sockets[0].closeCodes).toEqual([1000])
+})
+
+it('does not hand back while replacing a stream to reconnect the same viewer', async () => {
+ const view = render(<BotScreenPane bot={bot} />)
+ await waitFor(() => expect(sockets).toHaveLength(1))
+ await act(async () => {})
+ fireEvent.click(view.getByTitle('Reconnect'))
+ await waitFor(() => expect(sockets).toHaveLength(2))
+ expect(sockets[0].closeCodes).toEqual([1005])
+ expect(sockets[1].closed).toBe(false)
+ 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 bfe65b6d69a..e81fc263331 100644
--- a/apps/desktop/src/plugins/hermes-bots/screen-pane.tsx
+++ b/apps/desktop/src/plugins/hermes-bots/screen-pane.tsx
@@ -78,8 +78,15 @@ export function BotScreenPane({ bot }: { bot: RosterRow }) {
})
}, [bot, refresh, status?.profile_key])
- const detach = useCallback(() => {
+ const detach = useCallback((handBack = false) => {
attachGeneration.current += 1
+
+ // noVNC closes without a status. Intentional pane closure must send 1000
+ // first; reconnect teardown must keep the human lease instead.
+ if (handBack) {
+ socket.current?.close(1000)
+ }
+
rfb.current?.disconnect()
rfb.current = null
socket.current?.close()
@@ -158,7 +165,7 @@ export function BotScreenPane({ bot }: { bot: RosterRow }) {
// Visibility is not lifecycle: the stream stays attached while the pane is
// hidden; only unmount tears it down (and hands control back server-side).
- useEffect(() => () => detach(), [detach])
+ useEffect(() => () => detach(true), [detach])
useEffect(() => {
if (status?.running && conn === 'idle') {