Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions apps/desktop/electron/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,9 @@ if (IS_WSL && !REMOTE_DISPLAY_REASON && fs.existsSync('/dev/dxg')) {
ipcMain.handle('hermes:get-remote-display-reason', () => REMOTE_DISPLAY_REASON)

// Keep the renderer running at full speed while the window is in the background
// or occluded. The chat transcript streams to screen through a
// requestAnimationFrame-gated flush; Chromium pauses rAF (and clamps timers)
// for backgrounded/occluded renderers, so without these the live answer stalls
// or occluded. The chat transcript streams to screen through a bounded timer
// flush; Chromium clamps timers for backgrounded/occluded renderers, so without
// these the live answer stalls
// whenever the window loses focus (switching to your editor mid-turn, detached
// devtools, another window covering it) and only paints on refocus or refresh.
// `backgroundThrottling: false` on the BrowserWindow covers the blurred case;
Expand Down Expand Up @@ -4463,6 +4463,8 @@ function getNativeOverlayWidth() {
function getWindowState() {
return {
isFullscreen: Boolean(mainWindow?.isFullScreen?.()),
isMinimized: Boolean(mainWindow?.isMinimized?.()),
isVisible: Boolean(mainWindow?.isVisible?.()),
nativeOverlayWidth: getNativeOverlayWidth(),
windowButtonPosition: getWindowButtonPosition()
}
Expand Down Expand Up @@ -7286,9 +7288,9 @@ function createWindow() {
show: false,
backgroundColor: getWindowBackgroundColor(),
// Shared with the secondary session windows (chatWindowWebPreferences) so
// both keep `backgroundThrottling: false` — the chat transcript streams via
// a requestAnimationFrame-gated flush that Chromium pauses for blurred
// windows, stalling the live answer until refocus. See session-windows.ts.
// both keep `backgroundThrottling: false` — the chat transcript uses a
// bounded timer flush that Chromium clamps for blurred windows, stalling
// the live answer until refocus. See session-windows.ts.
webPreferences: chatWindowWebPreferences(PRELOAD_PATH)
})

Expand Down Expand Up @@ -7323,6 +7325,10 @@ function createWindow() {
mainWindow.on('enter-full-screen', () => sendWindowStateChanged(true))
mainWindow.on('will-leave-full-screen', () => sendWindowStateChanged(false))
mainWindow.on('leave-full-screen', () => sendWindowStateChanged(false))
mainWindow.on('minimize', () => sendWindowStateChanged())
mainWindow.on('restore', () => sendWindowStateChanged())
mainWindow.on('hide', () => sendWindowStateChanged())
mainWindow.on('show', () => sendWindowStateChanged())

// Reopen where the user left off. resized/moved settle once per drag; close is
// the cross-platform backstop, flushed synchronously before the window is gone.
Expand Down
4 changes: 2 additions & 2 deletions apps/desktop/electron/session-windows.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ test('registry trims the session id before keying', () => {

test('chatWindowWebPreferences disables background throttling so streaming paints while blurred', () => {
// Regression: secondary session windows used to omit this flag, so a streamed
// answer stalled until the window regained focus (Chromium pauses the
// requestAnimationFrame-gated transcript flush for backgrounded windows).
// answer stalled until the window regained focus (Chromium clamps the
// transcript flush timer for backgrounded windows).
const prefs = chatWindowWebPreferences('/tmp/preload.cjs')

assert.equal(prefs.backgroundThrottling, false)
Expand Down
4 changes: 2 additions & 2 deletions apps/desktop/electron/session-windows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ const SESSION_WINDOW_MIN_HEIGHT = 620
// false`, so a streamed answer stalled until the window regained focus.
//
// `backgroundThrottling: false` is load-bearing: the transcript streams to the
// screen through a requestAnimationFrame-gated flush, which Chromium pauses for
// blurred/occluded windows. A streaming chat app must keep painting in the
// screen through a bounded timer flush, which Chromium clamps for blurred/
// occluded windows. A streaming chat app must keep painting in the
// background, so every chat window opts out. The preload path is injected
// because it depends on the Electron entry's __dirname.
function chatWindowWebPreferences(preloadPath: string) {
Expand Down
2 changes: 1 addition & 1 deletion apps/desktop/src/app/pet-overlay/pet-overlay-app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ export function PetOverlayApp() {
<PetBubble />
</div>
<div style={{ lineHeight: 0, position: 'relative' }}>
<PetSprite info={info} />
<PetSprite info={info} pauseWhenUnfocused={false} />

{/* Hearts on the popped-out pet — identical to in-window. */}
<PetHeartField
Expand Down
310 changes: 310 additions & 0 deletions apps/desktop/src/app/right-sidebar/terminal/persistent.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,310 @@
import { act, type ReactNode } from 'react'
import { createRoot, type Root } from 'react-dom/client'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'

import { PersistentTerminal, TerminalSlot } from './persistent'

vi.mock('./terminals', () => ({
ensureTerminal: vi.fn()
}))

vi.mock('./workspace', () => ({
TerminalWorkspace: () => <div data-testid="terminal-workspace" />
}))

let resizeObserverCallback: ResizeObserverCallback | null = null
let mutationObserverCallback: MutationCallback | null = null
let mutationObserveCalls: Array<{ options?: MutationObserverInit; target: Node }> = []
let root: Root | null = null
let container: HTMLDivElement | null = null
let windowStateCallback: ((payload: { isMinimized?: boolean; isVisible?: boolean }) => void) | null = null

function render(ui: ReactNode) {
container = document.createElement('div')

Check warning on line 23 in apps/desktop/src/app/right-sidebar/terminal/persistent.test.tsx

View workflow job for this annotation

GitHub Actions / JS & TS checks / Typecheck & Test (apps/desktop)

Unexpected use of 'document'
document.body.append(container)

Check warning on line 24 in apps/desktop/src/app/right-sidebar/terminal/persistent.test.tsx

View workflow job for this annotation

GitHub Actions / JS & TS checks / Typecheck & Test (apps/desktop)

Unexpected use of 'document'
root = createRoot(container)

act(() => {
root!.render(ui)
})
}

function cleanup() {
if (root) {
act(() => {
root!.unmount()
})
}

container?.remove()
root = null
container = null
}

function setVisibility(hidden: boolean) {
Object.defineProperty(document, 'hidden', { configurable: true, value: hidden })

Check warning on line 45 in apps/desktop/src/app/right-sidebar/terminal/persistent.test.tsx

View workflow job for this annotation

GitHub Actions / JS & TS checks / Typecheck & Test (apps/desktop)

Unexpected use of 'document'
Object.defineProperty(document, 'visibilityState', { configurable: true, value: hidden ? 'hidden' : 'visible' })

Check warning on line 46 in apps/desktop/src/app/right-sidebar/terminal/persistent.test.tsx

View workflow job for this annotation

GitHub Actions / JS & TS checks / Typecheck & Test (apps/desktop)

Unexpected use of 'document'
}

function installWindowStateBridge() {
windowStateCallback = null
Object.defineProperty(window, 'hermesDesktop', {
configurable: true,
value: {
onWindowStateChanged: vi.fn((callback: typeof windowStateCallback) => {
windowStateCallback = callback

return () => {
if (windowStateCallback === callback) {
windowStateCallback = null
}
}
})
}
})
}

function rect(top: number, left: number, width: number, height: number): DOMRect {
return {
bottom: top + height,
height,
left,
right: left + width,
top,
width,
x: left,
y: top,
toJSON: () => ({})
} as DOMRect
}

function installRaf() {
let nextId = 1
const frames = new Map<number, FrameRequestCallback>()
const request = vi.fn((callback: FrameRequestCallback) => {
const id = nextId++
frames.set(id, callback)

return id
})
const cancel = vi.fn((id: number) => {
frames.delete(id)
})

Object.defineProperty(window, 'requestAnimationFrame', { configurable: true, value: request })
Object.defineProperty(window, 'cancelAnimationFrame', { configurable: true, value: cancel })

return {
cancel,
pending: () => frames.size,
request,
runNext: () => {
const next = frames.entries().next().value

if (!next) {
throw new Error('No pending RAF')
}

const [id, callback] = next
frames.delete(id)
callback(0)
}
}
}

function Harness() {
return (
<>
<TerminalSlot className="slot" />
<PersistentTerminal onAddSelectionToChat={() => undefined} />
</>
)
}

describe('PersistentTerminal rect tracking', () => {
beforeEach(() => {
;(globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true
setVisibility(false)
vi.spyOn(document, 'hasFocus').mockReturnValue(true)
installWindowStateBridge()
resizeObserverCallback = null

Check warning on line 130 in apps/desktop/src/app/right-sidebar/terminal/persistent.test.tsx

View workflow job for this annotation

GitHub Actions / JS & TS checks / Typecheck & Test (apps/desktop)

Unexpected use of 'document'
mutationObserverCallback = null
mutationObserveCalls = []
vi.stubGlobal(
'ResizeObserver',
class {
constructor(callback: ResizeObserverCallback) {
resizeObserverCallback = callback
}

disconnect = vi.fn()
observe = vi.fn()
unobserve = vi.fn()
} as unknown as typeof ResizeObserver
)
vi.stubGlobal(
'MutationObserver',
class {
constructor(callback: MutationCallback) {
mutationObserverCallback = callback
}

disconnect = vi.fn()
observe = vi.fn((target: Node, options?: MutationObserverInit) => {
mutationObserveCalls.push({ options, target })
})
takeRecords = vi.fn(() => [])
} as unknown as typeof MutationObserver
)
})

afterEach(() => {
cleanup()
vi.unstubAllGlobals()
vi.restoreAllMocks()
setVisibility(false)
delete (window as unknown as { hermesDesktop?: unknown }).hermesDesktop
})

it('settles after rect changes instead of polling forever', () => {
const raf = installRaf()
let currentRect = rect(10, 20, 200, 100)
vi.spyOn(HTMLElement.prototype, 'getBoundingClientRect').mockImplementation(() => currentRect)

render(<Harness />)

expect(raf.request).toHaveBeenCalledTimes(1)

act(() => {
raf.runNext()
})

expect(raf.request).toHaveBeenCalledTimes(1)
expect(raf.pending()).toBe(0)

currentRect = rect(12, 24, 220, 120)
act(() => {
resizeObserverCallback?.([], {} as ResizeObserver)
})

expect(raf.request).toHaveBeenCalledTimes(2)

act(() => {
raf.runNext()
})

expect(raf.request).toHaveBeenCalledTimes(3)

act(() => {
raf.runNext()
})

expect(raf.request).toHaveBeenCalledTimes(3)
expect(raf.pending()).toBe(0)
})

it('remeasures when layout moves the slot without resizing it', () => {
const raf = installRaf()
let currentRect = rect(10, 20, 200, 100)
vi.spyOn(HTMLElement.prototype, 'getBoundingClientRect').mockImplementation(() => currentRect)

render(<Harness />)

expect(mutationObserveCalls.some(call => call.options?.subtree === true)).toBe(true)

act(() => {
raf.runNext()
})

const overlay = container!.lastElementChild as HTMLElement
expect(overlay.style.top).toBe('10px')
expect(overlay.style.left).toBe('20px')
expect(raf.pending()).toBe(0)

currentRect = rect(32, 48, 200, 100)
act(() => {
mutationObserverCallback?.([], {} as MutationObserver)
})

expect(raf.request).toHaveBeenCalledTimes(2)

act(() => {
raf.runNext()
})

expect(overlay.style.top).toBe('32px')
expect(overlay.style.left).toBe('48px')

act(() => {
raf.runNext()
})

expect(raf.pending()).toBe(0)
})

it('does not schedule rect RAFs while the Electron window is paused, then resumes when visible', () => {
const raf = installRaf()
vi.spyOn(HTMLElement.prototype, 'getBoundingClientRect').mockReturnValue(rect(10, 20, 200, 100))

render(<Harness />)

expect(raf.request).toHaveBeenCalledTimes(1)

act(() => {
windowStateCallback?.({ isMinimized: true, isVisible: false })
})

expect(raf.cancel).toHaveBeenCalledTimes(1)
expect(raf.pending()).toBe(0)

act(() => {
resizeObserverCallback?.([], {} as ResizeObserver)
})

expect(raf.request).toHaveBeenCalledTimes(1)

act(() => {
windowStateCallback?.({ isMinimized: false, isVisible: true })
})

expect(raf.request).toHaveBeenCalledTimes(2)
})

it('suspends while unfocused and cancels every callback on unmount', () => {
const raf = installRaf()
vi.spyOn(HTMLElement.prototype, 'getBoundingClientRect').mockReturnValue(rect(10, 20, 200, 100))

render(<Harness />)
expect(raf.pending()).toBe(1)

act(() => window.dispatchEvent(new Event('blur')))
expect(raf.pending()).toBe(0)

act(() => window.dispatchEvent(new Event('focus')))
expect(raf.pending()).toBe(1)

cleanup()
expect(raf.pending()).toBe(0)

act(() => {
window.dispatchEvent(new Event('focus'))
resizeObserverCallback?.([], {} as ResizeObserver)
mutationObserverCallback?.([], {} as MutationObserver)
})
expect(raf.pending()).toBe(0)
})

it('does not schedule an initial frame when mounted while unfocused', () => {
const raf = installRaf()
vi.mocked(document.hasFocus).mockReturnValue(false)
vi.spyOn(HTMLElement.prototype, 'getBoundingClientRect').mockReturnValue(rect(10, 20, 200, 100))

Check warning on line 301 in apps/desktop/src/app/right-sidebar/terminal/persistent.test.tsx

View workflow job for this annotation

GitHub Actions / JS & TS checks / Typecheck & Test (apps/desktop)

Unexpected use of 'document'
render(<Harness />)

expect(raf.request).not.toHaveBeenCalled()

act(() => window.dispatchEvent(new Event('focus')))

expect(raf.pending()).toBe(1)
})
})
Loading
Loading