Skip to content

The agent that grows with you

fix(desktop): re-arm the terminal mount when the xterm host connects late

Brooklyn Nicholson60e2e33e9b9c
diff --git a/apps/desktop/src/app/right-sidebar/terminal/use-agent-terminal.test.tsx b/apps/desktop/src/app/right-sidebar/terminal/use-agent-terminal.test.tsx
index 46de8eaab75..b21e28a50dd 100644
--- a/apps/desktop/src/app/right-sidebar/terminal/use-agent-terminal.test.tsx
+++ b/apps/desktop/src/app/right-sidebar/terminal/use-agent-terminal.test.tsx
@@ -179,4 +179,79 @@ describe('useAgentTerminal', () => {
expect(() => handler()).not.toThrow()
expect(xterm.refresh).toHaveBeenCalledWith(0, 23)
})
+
+ it('retries the mount once a host rendered disconnected joins the document', async () => {
+ // The #118004 strand: the pane shell can render the host before it is
+ // connected to the document, so the font wait's isCurrent() goes false at
+ // an await boundary and resolves null — the old code returned silently
+ // and the pane stayed blank forever (no open, no stream attach). The
+ // mount must re-arm and run as soon as the host connects.
+ const frames = new Map<number, FrameRequestCallback>()
+ let nextFrameId = 1
+
+ const request = vi.fn((callback: FrameRequestCallback) => {
+ const id = nextFrameId++
+
+ frames.set(id, callback)
+
+ return id
+ })
+
+ vi.stubGlobal('requestAnimationFrame', request)
+ vi.stubGlobal(
+ 'cancelAnimationFrame',
+ vi.fn((id: number) => frames.delete(id))
+ )
+
+ // The effect runs while the container is still attached (the font wait
+ // starts against a connected host), then the container is detached so
+ // isCurrent() goes false at the warm() await boundary — the same race as
+ // a pane shell that renders its host before the document connects it.
+ const mount = render(<Harness />, {
+ container: globalThis.document.body.appendChild(globalThis.document.createElement('div'))
+ })
+
+ const host = mount.container.querySelector('div')!
+
+ // Disconnect the container so host.isConnected is false when the font
+ // promise settles.
+ mount.container.remove()
+
+ await act(async () => {
+ resolveFontLoad([])
+ await Promise.resolve()
+ })
+
+ expect(host.isConnected).toBe(false)
+ expect(xterm.open).not.toHaveBeenCalled()
+ expect(terminalRegistrations.registerWriter).not.toHaveBeenCalled()
+
+ // The watch is armed: reconnecting the host and draining the frame loop
+ // retries the font wait (fonts resolve immediately now) and mounts.
+ globalThis.document.body.appendChild(mount.container)
+
+ await act(async () => {
+ while (frames.size > 0) {
+ const next = frames.entries().next().value as [number, FrameRequestCallback]
+ frames.delete(next[0])
+ next[1](0)
+ }
+
+ await Promise.resolve()
+ })
+
+ expect(xterm.open).toHaveBeenCalledWith(host)
+ expect(terminalRegistrations.registerWriter).toHaveBeenCalled()
+
+ // Tearing down while the watch is still armed must cancel it cleanly.
+ mount.unmount()
+ expect(() => {
+ while (frames.size > 0) {
+ const next = frames.entries().next().value as [number, FrameRequestCallback]
+ frames.delete(next[0])
+ next[1](0)
+ }
+ }).not.toThrow()
+ expect(xterm.dispose).toHaveBeenCalled()
+ })
})
diff --git a/apps/desktop/src/app/right-sidebar/terminal/use-agent-terminal.ts b/apps/desktop/src/app/right-sidebar/terminal/use-agent-terminal.ts
index 7b6f68509da..88c1f667540 100644
--- a/apps/desktop/src/app/right-sidebar/terminal/use-agent-terminal.ts
+++ b/apps/desktop/src/app/right-sidebar/terminal/use-agent-terminal.ts
@@ -50,6 +50,15 @@ export function useAgentTerminal({ active, id, procId }: { active: boolean; id:
let disposed = false
let observer: ResizeObserver | null = null
+ let mounted = false
+ let mountWatchFrame = 0
+
+ const cancelMountWatch = () => {
+ if (mountWatchFrame) {
+ window.cancelAnimationFrame(mountWatchFrame)
+ mountWatchFrame = 0
+ }
+ }
let unregister = () => {}
@@ -133,6 +142,7 @@ export function useAgentTerminal({ active, id, procId }: { active: boolean; id:
term.open(host)
termRef.current = term
mountedRef.current = true
+ mounted = true
try {
const webgl = new WebglAddon()
@@ -174,6 +184,34 @@ export function useAgentTerminal({ active, id, procId }: { active: boolean; id:
() => !disposed && host.isConnected
).then(fontFamily => {
if (!fontFamily) {
+ // Same host-connection race as the user terminal (#118004): the font
+ // wait resolves null when host.isConnected went false at an await
+ // boundary, and returning here used to strand the pane blank. Poll
+ // frames until the host connects, then retry the wait+mount.
+ const watchForHost = () => {
+ if (disposed || mounted) {
+ return
+ }
+
+ if (host.isConnected) {
+ void prepareTerminalFontFamily(
+ () => latestFontFamilyRef.current,
+ () => !disposed && host.isConnected
+ ).then(next => {
+ if (next && !disposed && !mounted && host.isConnected) {
+ term.options.fontFamily = next
+ mount()
+ }
+ })
+
+ return
+ }
+
+ mountWatchFrame = window.requestAnimationFrame(watchForHost)
+ }
+
+ mountWatchFrame = window.requestAnimationFrame(watchForHost)
+
return
}
@@ -184,6 +222,7 @@ export function useAgentTerminal({ active, id, procId }: { active: boolean; id:
return () => {
disposed = true
mountedRef.current = false
+ cancelMountWatch()
unregister()
unregisterReader()
unregisterWebglRefresh()
diff --git a/apps/desktop/src/app/right-sidebar/terminal/use-terminal-session.ts b/apps/desktop/src/app/right-sidebar/terminal/use-terminal-session.ts
index 701c7e947a9..9ea13700673 100644
--- a/apps/desktop/src/app/right-sidebar/terminal/use-terminal-session.ts
+++ b/apps/desktop/src/app/right-sidebar/terminal/use-terminal-session.ts
@@ -26,7 +26,13 @@ import {
} from './selection'
import { registerTerminalContextMenu } from './terminal-context-menu'
import { prepareTerminalFontFamily } from './terminal-font'
-import { closeTerminal, redrawAllTerminals, registerWebglRefresh, updateTerminalRestoreCwd, updateTerminalReviveBuffer } from './terminals'
+import {
+ closeTerminal,
+ redrawAllTerminals,
+ registerWebglRefresh,
+ updateTerminalRestoreCwd,
+ updateTerminalReviveBuffer
+} from './terminals'
import { useTerminalFontController } from './use-terminal-font'
// How many scrollback lines to serialize for relaunch restore. Mirrors VS Code's
@@ -947,6 +953,16 @@ export function useTerminalSession({
// picks the wrong row count, the shell boots at that size, then the real font
// loads -> refit -> SIGWINCH -> the shell reprints its prompt lower, leaving
// stale blank rows (and a stray selection) above it.
+ let mounted = false
+ let mountWatchFrame = 0
+
+ const cancelMountWatch = () => {
+ if (mountWatchFrame) {
+ window.cancelAnimationFrame(mountWatchFrame)
+ mountWatchFrame = 0
+ }
+ }
+
const mount = () => {
if (disposed || !host.isConnected) {
return
@@ -954,6 +970,7 @@ export function useTerminalSession({
term.open(host)
mountedRef.current = true
+ mounted = true
term.focus()
// WebGL renderer matches the dashboard ChatPage path; xterm's default DOM
@@ -994,6 +1011,37 @@ export function useTerminalSession({
() => !disposed && host.isConnected
).then(fontFamily => {
if (!fontFamily) {
+ // The pane shell can render this host before it's connected to the
+ // document (inactive keep-alive tab, a remount race, a reload
+ // mid-render) — isCurrent() above goes false at an await boundary and
+ // this used to return silently: the pane stayed blank forever, with
+ // no spawn attempt and no log line (#118004). Poll frames until the
+ // host connects, then retry the wait+mount exactly once; a host that
+ // never connects (or a dispose before then) stops the watch.
+ const watchForHost = () => {
+ if (disposed || mounted) {
+ return
+ }
+
+ if (host.isConnected) {
+ void prepareTerminalFontFamily(
+ () => latestFontFamilyRef.current,
+ () => !disposed && host.isConnected
+ ).then(next => {
+ if (next && !disposed && !mounted && host.isConnected) {
+ term.options.fontFamily = next
+ mount()
+ }
+ })
+
+ return
+ }
+
+ mountWatchFrame = window.requestAnimationFrame(watchForHost)
+ }
+
+ mountWatchFrame = window.requestAnimationFrame(watchForHost)
+
return
}
@@ -1004,6 +1052,7 @@ export function useTerminalSession({
return () => {
disposed = true
mountedRef.current = false
+ cancelMountWatch()
cleanup.forEach(run => run())
fitRef.current = null