diff --git a/src/main/runtime/orca-runtime.test.ts b/src/main/runtime/orca-runtime.test.ts index 153f78407..e9c32dd16 100644 --- a/src/main/runtime/orca-runtime.test.ts +++ b/src/main/runtime/orca-runtime.test.ts @@ -1409,14 +1409,16 @@ function makeRuntimeStoreWithWorkspaceSession(initialSession: WorkspaceSessionSt persistPtyBinding: ReturnType } getSession: () => WorkspaceSessionState + setSession: (next: WorkspaceSessionState) => void } { let session = initialSession + const setSession = (next: WorkspaceSessionState): void => { + session = next + } const runtimeStore = { ...store, getWorkspaceSession: () => session, - setWorkspaceSession: vi.fn((next: WorkspaceSessionState) => { - session = next - }), + setWorkspaceSession: vi.fn(setSession), persistPtyBinding: vi.fn( (args: { worktreeId: string; tabId: string; leafId: string; ptyId: string }) => { const tabs = session.tabsByWorktree[args.worktreeId] ?? [] @@ -1446,7 +1448,7 @@ function makeRuntimeStoreWithWorkspaceSession(initialSession: WorkspaceSessionSt } ) } - return { runtimeStore, getSession: () => session } + return { runtimeStore, getSession: () => session, setSession } } function makeWorkspaceSessionWithHeadlessTerminal( @@ -24830,6 +24832,267 @@ describe('OrcaRuntimeService', () => { ]) }) + it('leases renderer publication for a paired create and preserves host-owned inventory', async () => { + const leafId = '91919191-9191-4919-8919-919191919191' + const spawn = vi.fn() + const setBackgroundThrottling = vi.fn() + const runtime = new OrcaRuntimeService(store) + runtime.setPtyController({ + spawn, + write: () => true, + kill: () => true, + getForegroundProcess: async () => null + }) + runtime.attachWindow(1) + runtime.syncWindowGraph(1, { tabs: [], leaves: [] }) + const webContents: { + isDestroyed: () => boolean + setBackgroundThrottling: typeof setBackgroundThrottling + send: ReturnType + } = { + isDestroyed: () => false, + setBackgroundThrottling, + send: vi.fn() + } + webContents.send.mockImplementation((_channel: string, payload: { requestId: string }) => { + expect(setBackgroundThrottling).toHaveBeenCalledWith(false) + runtime.registerPty('pty-paired-headed', TEST_WORKTREE_ID, null, { + tabId: 'tab-paired-headed', + leafId + }) + ipcMain.emit( + 'terminal:tabCreateReply', + { sender: webContents }, + { requestId: payload.requestId, tabId: 'tab-paired-headed', title: 'Terminal' } + ) + runtime.syncWindowGraph(1, { + tabs: [ + { + tabId: 'tab-paired-headed', + worktreeId: TEST_WORKTREE_ID, + title: 'Terminal', + activeLeafId: leafId, + layout: null + } + ], + leaves: [ + { + tabId: 'tab-paired-headed', + worktreeId: TEST_WORKTREE_ID, + leafId, + paneRuntimeId: 1, + ptyId: 'pty-paired-headed' + } + ], + mobileSessionTabs: [ + { + worktree: TEST_WORKTREE_ID, + publicationEpoch: 'renderer:paired-headed', + snapshotVersion: 1, + activeGroupId: 'group-1', + activeTabId: null, + activeTabType: null, + tabs: [ + { + type: 'terminal', + id: `tab-paired-headed::${leafId}`, + parentTabId: 'tab-paired-headed', + leafId, + ptyId: 'pty-paired-headed', + title: 'Terminal', + viewMode: 'chat', + isActive: false + } + ] + } + ] + }) + }) + electronMocks.BrowserWindow.fromId.mockReturnValue({ + isDestroyed: () => false, + webContents + }) + + const result = await runtime.createMobileSessionTerminal(`id:${TEST_WORKTREE_ID}`, { + clientNavigationId: 'device-a', + navigation: 'caller', + activate: false, + select: false, + viewMode: 'chat' + }) + + expect(webContents.send).toHaveBeenCalledWith( + 'terminal:requestTabCreate', + expect.objectContaining({ source: 'runtime-session', viewMode: 'chat' }) + ) + expect(spawn).not.toHaveBeenCalled() + expect(setBackgroundThrottling.mock.calls).toEqual([[false], [true]]) + expect(result.tab).toMatchObject({ + type: 'terminal', + status: 'ready', + ptyId: 'pty-paired-headed', + terminal: expect.stringMatching(/^term_/), + viewMode: 'chat' + }) + const host = await runtime.listMobileSessionTabs(`id:${TEST_WORKTREE_ID}`) + const clientA = await runtime.listMobileSessionTabs(`id:${TEST_WORKTREE_ID}`, 'device-a') + const clientB = await runtime.listMobileSessionTabs(`id:${TEST_WORKTREE_ID}`, 'device-b') + const inventory = (snapshot: RuntimeMobileSessionTabsResult) => + snapshot.tabs.map((tab) => + tab.type === 'terminal' + ? { + id: tab.id, + type: tab.type, + leafId: tab.leafId, + parentTabId: tab.parentTabId, + ptyId: tab.ptyId + } + : { id: tab.id, type: tab.type } + ) + expect(inventory(clientA)).toEqual(inventory(host)) + expect(inventory(clientB)).toEqual(inventory(host)) + + runtime['mobileSessionTabsByWorktree'].delete(TEST_WORKTREE_ID) + expect(runtime['syncMobileSessionTabs']([])).toEqual(new Set([TEST_WORKTREE_ID])) + expect(runtime['mobileSessionTabsByWorktree'].get(TEST_WORKTREE_ID)?.tabs).toEqual([ + expect.objectContaining({ + parentTabId: 'tab-paired-headed', + leafId, + ptyId: 'pty-paired-headed' + }) + ]) + const restoredHost = await runtime.listMobileSessionTabs(`id:${TEST_WORKTREE_ID}`) + const restoredClientA = await runtime.listMobileSessionTabs( + `id:${TEST_WORKTREE_ID}`, + 'device-a' + ) + const restoredClientB = await runtime.listMobileSessionTabs( + `id:${TEST_WORKTREE_ID}`, + 'device-b' + ) + expect(inventory(restoredHost)).toEqual(inventory(host)) + expect(inventory(restoredClientA)).toEqual(inventory(host)) + expect(inventory(restoredClientB)).toEqual(inventory(host)) + expect(spawn).not.toHaveBeenCalled() + }) + + it.each([ + ['paired', 'reloading'], + ['paired', 'unavailable'], + ['unpaired', 'reloading'], + ['unpaired', 'unavailable'] + ] as const)( + 'does not spawn a %s terminal while the headed renderer graph is %s', + async (caller, graphStatus) => { + const spawn = vi.fn() + const send = vi.fn() + const runtime = new OrcaRuntimeService(store) + runtime.setPtyController({ + spawn, + write: () => true, + kill: () => true, + getForegroundProcess: async () => null + }) + runtime.attachWindow(1) + runtime.syncWindowGraph(1, { tabs: [], leaves: [] }) + electronMocks.BrowserWindow.fromId.mockReturnValue({ + isDestroyed: () => false, + webContents: { isDestroyed: () => false, send, setBackgroundThrottling: vi.fn() } + }) + if (graphStatus === 'reloading') { + runtime.markRendererReloading(1) + } else { + runtime.markGraphUnavailable(1) + } + + await expect( + runtime.createMobileSessionTerminal( + `id:${TEST_WORKTREE_ID}`, + caller === 'paired' + ? { clientNavigationId: 'device-a', navigation: 'caller' as const } + : {} + ) + ).rejects.toThrow('runtime_unavailable') + expect(spawn).not.toHaveBeenCalled() + expect(send).not.toHaveBeenCalled() + } + ) + + it('rejects a paired create if renderer authority changes across async resolution', async () => { + const spawn = vi.fn() + const send = vi.fn() + const setBackgroundThrottling = vi.fn() + const runtime = new OrcaRuntimeService(store) + runtime.setPtyController({ + spawn, + write: () => true, + kill: () => true, + getForegroundProcess: async () => null + }) + runtime.attachWindow(1) + electronMocks.BrowserWindow.fromId.mockReturnValue({ + isDestroyed: () => false, + webContents: { isDestroyed: () => false, send, setBackgroundThrottling } + }) + runtime.syncWindowGraph(1, { tabs: [], leaves: [] }) + let resolutionStarted = (): void => {} + const started = new Promise((resolve) => { + resolutionStarted = resolve + }) + let releaseResolution = (): void => {} + vi.mocked(listWorktrees).mockImplementationOnce( + () => + new Promise((resolve) => { + releaseResolution = () => resolve(MOCK_GIT_WORKTREES) + resolutionStarted() + }) + ) + + const create = runtime.createMobileSessionTerminal(`id:${TEST_WORKTREE_ID}`, { + clientNavigationId: 'device-a', + navigation: 'caller' + }) + await started + expect(spawn).not.toHaveBeenCalled() + runtime.markRendererReloading(1) + releaseResolution() + await expect(create).rejects.toThrow('runtime_unavailable') + expect(send).not.toHaveBeenCalled() + expect(spawn).not.toHaveBeenCalled() + expect(setBackgroundThrottling).not.toHaveBeenCalled() + }) + + it('cancels a paired renderer-owned create before publication when its client disconnects', async () => { + const spawn = vi.fn() + const send = vi.fn() + const setBackgroundThrottling = vi.fn() + const abort = new AbortController() + const runtime = new OrcaRuntimeService(store) + runtime.setPtyController({ + spawn, + write: () => true, + kill: () => true, + getForegroundProcess: async () => null + }) + runtime.attachWindow(1) + runtime.syncWindowGraph(1, { tabs: [], leaves: [] }) + electronMocks.BrowserWindow.fromId.mockReturnValue({ + isDestroyed: () => false, + webContents: { isDestroyed: () => false, send, setBackgroundThrottling } + }) + abort.abort() + + await expect( + runtime.createMobileSessionTerminal(`id:${TEST_WORKTREE_ID}`, { + clientNavigationId: 'device-a', + signal: abort.signal + }) + ).rejects.toThrow('client_disconnected') + expect(spawn).not.toHaveBeenCalled() + expect(send).not.toHaveBeenCalled() + expect(setBackgroundThrottling).not.toHaveBeenCalled() + }) + it('selects a created terminal only for the paired caller', async () => { let spawnIndex = 0 const runtime = new OrcaRuntimeService(store) @@ -25790,6 +26053,172 @@ describe('OrcaRuntimeService', () => { }) }) + it('leases renderer publication until a paired whole-tab close is acknowledged', async () => { + const { runtimeStore } = makeRuntimeStoreWithWorkspaceSession( + makeWorkspaceSessionWithHeadlessTerminal() + ) + const acknowledged = makeDeferred() + const closeTerminalTab = vi.fn(() => acknowledged.promise) + const setBackgroundThrottling = vi.fn() + const runtime = new OrcaRuntimeService(runtimeStore as never) + runtime.setNotifier({ closeTerminal: vi.fn(), closeTerminalTab } as never) + runtime.setPtyController({ + write: () => true, + kill: () => true, + getForegroundProcess: async () => null, + listProcesses: async () => [] + }) + runtime.syncWindowGraph(1, { + tabs: [ + { + tabId: 'host-tab', + worktreeId: TEST_WORKTREE_ID, + title: 'Durable', + activeLeafId: HEADLESS_LEAF_ID, + layout: null + } + ], + leaves: [ + { + tabId: 'host-tab', + worktreeId: TEST_WORKTREE_ID, + leafId: HEADLESS_LEAF_ID, + paneRuntimeId: 1, + ptyId: 'persisted-pty' + } + ], + mobileSessionTabs: [ + { + worktree: TEST_WORKTREE_ID, + publicationEpoch: 'renderer:paired-close', + snapshotVersion: 1, + activeGroupId: 'group-1', + activeTabId: `host-tab::${HEADLESS_LEAF_ID}`, + activeTabType: 'terminal', + tabs: [ + { + type: 'terminal', + id: `host-tab::${HEADLESS_LEAF_ID}`, + parentTabId: 'host-tab', + leafId: HEADLESS_LEAF_ID, + ptyId: 'persisted-pty', + title: 'Durable', + isActive: true + } + ] + } + ] + }) + runtime.registerPty('persisted-pty', TEST_WORKTREE_ID, null, { + tabId: 'host-tab', + leafId: HEADLESS_LEAF_ID + }) + electronMocks.BrowserWindow.fromId.mockReturnValue({ + isDestroyed: () => false, + webContents: { + isDestroyed: () => false, + setBackgroundThrottling + } + }) + + const pending = runtime.closeMobileSessionTab(`id:${TEST_WORKTREE_ID}`, 'host-tab', { + reason: 'user', + clientNavigationId: 'device-a' + }) + await vi.waitFor(() => expect(closeTerminalTab).toHaveBeenCalledWith('host-tab')) + expect(setBackgroundThrottling.mock.calls).toEqual([[false]]) + + acknowledged.resolve() + await expect(pending).resolves.toEqual({ closed: true }) + expect(setBackgroundThrottling.mock.calls).toEqual([[false], [true]]) + }) + + it('accepts a paired close after the renderer already persisted its removal', async () => { + const { runtimeStore, getSession, setSession } = makeRuntimeStoreWithWorkspaceSession( + makeWorkspaceSessionWithHeadlessTerminal() + ) + const acknowledged = makeDeferred() + const closeTerminalTab = vi.fn(() => acknowledged.promise) + const kill = vi.fn(() => true) + const runtime = new OrcaRuntimeService(runtimeStore as never) + runtime.setNotifier({ closeTerminal: vi.fn(), closeTerminalTab } as never) + runtime.setPtyController({ + write: () => true, + kill, + getForegroundProcess: async () => null, + listProcesses: async () => [] + }) + runtime.syncWindowGraph(1, { + tabs: [ + { + tabId: 'host-tab', + worktreeId: TEST_WORKTREE_ID, + title: 'Durable', + activeLeafId: HEADLESS_LEAF_ID, + layout: null + } + ], + leaves: [ + { + tabId: 'host-tab', + worktreeId: TEST_WORKTREE_ID, + leafId: HEADLESS_LEAF_ID, + paneRuntimeId: 1, + ptyId: 'persisted-pty' + } + ], + mobileSessionTabs: [ + { + worktree: TEST_WORKTREE_ID, + publicationEpoch: 'renderer:paired-close', + snapshotVersion: 1, + activeGroupId: 'group-1', + activeTabId: `host-tab::${HEADLESS_LEAF_ID}`, + activeTabType: 'terminal', + tabs: [ + { + type: 'terminal', + id: `host-tab::${HEADLESS_LEAF_ID}`, + parentTabId: 'host-tab', + leafId: HEADLESS_LEAF_ID, + ptyId: 'persisted-pty', + title: 'Durable', + isActive: true + } + ] + } + ] + }) + runtime.registerPty('persisted-pty', TEST_WORKTREE_ID, null, { + tabId: 'host-tab', + leafId: HEADLESS_LEAF_ID + }) + electronMocks.BrowserWindow.fromId.mockReturnValue({ + isDestroyed: () => false, + webContents: { + isDestroyed: () => false, + setBackgroundThrottling: vi.fn() + } + }) + + const pending = runtime.closeMobileSessionTab(`id:${TEST_WORKTREE_ID}`, 'host-tab', { + reason: 'user', + clientNavigationId: 'device-a' + }) + await vi.waitFor(() => expect(closeTerminalTab).toHaveBeenCalledWith('host-tab')) + const session = getSession() + setSession({ + ...session, + tabsByWorktree: { ...session.tabsByWorktree, [TEST_WORKTREE_ID]: [] }, + terminalLayoutsByTabId: {} + }) + runtime.syncWindowGraph(1, { tabs: [], leaves: [] }) + + acknowledged.resolve() + await expect(pending).resolves.toEqual({ closed: true }) + expect(kill).toHaveBeenCalledWith('persisted-pty') + }) + it('reuses pane close for live PTYs that do not own a renderer tab', async () => { const kill = vi.fn(() => true) const closeTerminalTab = vi.fn(async () => {}) diff --git a/src/main/runtime/orca-runtime.ts b/src/main/runtime/orca-runtime.ts index abc824376..7f4c42c9a 100644 --- a/src/main/runtime/orca-runtime.ts +++ b/src/main/runtime/orca-runtime.ts @@ -541,6 +541,7 @@ import { import { joinWorktreeRelativePath } from './runtime-relative-paths' import { collectMemorySnapshot } from '../memory/collector' import { app, BrowserWindow, ipcMain, Notification } from 'electron' +import { RendererPublicationThrottle } from '../window/renderer-publication-throttle' import type { AgentBrowserBridge } from '../browser/agent-browser-bridge' import type { BrowserBackend } from '../browser/browser-backend' import { BrowserError } from '../browser/cdp-bridge' @@ -2640,6 +2641,8 @@ export class OrcaRuntimeService { private rendererGraphEpoch = 0 private graphStatus: RuntimeGraphStatus = 'unavailable' private authoritativeWindowId: number | null = null + // Why: paired graph transactions need foreground timer cadence only until their publication settles. + private readonly rendererPublicationThrottle = new RendererPublicationThrottle() private tabs = new Map() private mobileSessionTabsByWorktree = new Map() // Why: renderer publication ordering must be judged against the renderer's @@ -2689,6 +2692,7 @@ export class OrcaRuntimeService { string, { activate: boolean + paired: boolean selectIfNoActiveTab: boolean viewMode?: 'terminal' | 'chat' } @@ -2804,6 +2808,7 @@ export class OrcaRuntimeService { // iterates them all. Listeners are cleaned up via subscriptionCleanups. private notificationListeners = new Set<(event: MobileNotificationEvent) => void>() private ptysById = new Map() + private readonly pairedRendererSessionOwnedPtyIds = new Set() private wslDistroByPtyId = new Map() private titleObservationSequence = 0 private headlessTerminals = new Map() @@ -5431,6 +5436,7 @@ export class OrcaRuntimeService { }) this.hydrateHeadlessMobileSessionTabsFromWorkspaceSession(explicitWorktreeId) await this.refreshMobileSessionPtyRecords(explicitWorktreeId) + this.restoreLivePairedRendererSessionOwnedMobileTerminals(explicitWorktreeId) return this.getMobileSessionTabsForWorktree(explicitWorktreeId, clientNavigationId) } const worktree = await this.resolveWorktreeSelector(worktreeSelector) @@ -5440,6 +5446,7 @@ export class OrcaRuntimeService { }) this.hydrateHeadlessMobileSessionTabsFromWorkspaceSession(worktree.id) await this.refreshMobileSessionPtyRecords() + this.restoreLivePairedRendererSessionOwnedMobileTerminals(worktree.id) return this.getMobileSessionTabsForWorktree(worktree.id, clientNavigationId) } @@ -6008,6 +6015,7 @@ export class OrcaRuntimeService { const pty = this.ptysById.get(ptyId) if (pty?.worktreeId === worktreeId && pty.tabId === parentTabId) { pty.runtimeSessionOwned = false + this.setPairedRendererSessionOwnership(pty.ptyId, false) } } } @@ -6154,6 +6162,7 @@ export class OrcaRuntimeService { startupCwd?: string viewMode?: 'terminal' | 'chat' split?: { splitFromLeafId: string; direction: 'horizontal' | 'vertical' } + notify?: boolean } ): void { if ( @@ -6262,7 +6271,9 @@ export class OrcaRuntimeService { tabs } this.mobileSessionTabsByWorktree.set(worktreeId, next) - this.notifyMobileSessionTabsChanged(worktreeId) + if (args.notify !== false) { + this.notifyMobileSessionTabsChanged(worktreeId) + } } private touchMobileSessionSnapshotsForPty( @@ -6879,7 +6890,11 @@ export class OrcaRuntimeService { } } - private removePersistedHeadlessTerminalTab(worktreeId: string, parentTabId: string): string[] { + private removePersistedHeadlessTerminalTab( + worktreeId: string, + parentTabId: string, + options: { allowMissing?: boolean } = {} + ): string[] { const session = this.getWorkspaceSessionForWorktree(worktreeId) if (!session || !this.store?.setWorkspaceSession) { throw new Error('workspace_session_unavailable') @@ -6889,6 +6904,9 @@ export class OrcaRuntimeService { throw new Error('terminal_tab_pinned') } if (!result.closed) { + if (options.allowMissing) { + return [] + } throw new Error('tab_not_found') } this.setWorkspaceSessionForWorktree( @@ -7049,7 +7067,7 @@ export class OrcaRuntimeService { } } try { - await this.createHeadlessMobileSessionTerminal(worktreeId, targetsHost, undefined, { + await this.createRuntimeOwnedMobileSessionTerminal(worktreeId, targetsHost, undefined, { identity: { tabId: tab.parentTabId, leafId: tab.leafId, @@ -7317,13 +7335,19 @@ export class OrcaRuntimeService { reason?: RuntimeSessionTabCloseReason expectedPublicationEpoch?: string expectedTerminalHandle?: string + clientNavigationId?: string } = {} ): Promise { + const graphEpoch = options.clientNavigationId ? this.captureReadyGraphEpoch() : null const explicitWorktreeId = this.getValidatedExplicitWorktreeIdSelector(worktreeSelector) const worktreeId = explicitWorktreeId ?? (await this.resolveWorktreeSelector(worktreeSelector)).id this.hydrateHeadlessMobileSessionTabsFromWorkspaceSession(worktreeId) const observedPtyIds = await this.refreshMobileSessionPtyRecords() + if (graphEpoch !== null) { + this.assertStableReadyGraph(graphEpoch) + } + this.restoreLivePairedRendererSessionOwnedMobileTerminals(worktreeId) const snapshot = this.mobileSessionTabsByWorktree.get(worktreeId) if (options.reason !== undefined && options.reason !== 'user' && observedPtyIds === null) { // Why: keep-on-unknown must also restore the mirror the caller already pruned. @@ -7455,7 +7479,19 @@ export class OrcaRuntimeService { if (closingWholeParent && this.notifier?.closeTerminalTab) { // Why: whole-tab close is a lifecycle transaction. The renderer reply // arrives only after canonical retirement and a forced session flush. - await this.notifier.closeTerminalTab(tab.parentTabId) + const win = this.getAvailableAuthoritativeWindow() + if (win?.webContents.isDestroyed?.()) { + throw new Error('runtime_unavailable') + } + const releasePublicationThrottle = + options.clientNavigationId && win + ? this.rendererPublicationThrottle.acquire(win.webContents) + : () => {} + try { + await this.notifier.closeTerminalTab(tab.parentTabId) + } finally { + releasePublicationThrottle() + } const remainingSnapshot = this.mobileSessionTabsByWorktree.get(worktreeId) const remainingTab = remainingSnapshot?.tabs.find( (candidate): candidate is RuntimeMobileSessionTerminalTab => @@ -7467,7 +7503,10 @@ export class OrcaRuntimeService { this.isRuntimeOwnedHeadlessMobileTab(worktreeId, remainingTab) ) { // Why: after relay recovery the renderer can acknowledge a tab it no longer mirrors; the HUB must still retire its SSH-owned surface. - this.closeHeadlessMobileTerminalTab(worktreeId, remainingSnapshot, remainingTab) + this.closeHeadlessMobileTerminalTab(worktreeId, remainingSnapshot, remainingTab, { + // Why: the renderer may already have durably removed the tab before acknowledging. + allowMissingPersistedTab: true + }) this.notifyRendererOfHeadlessTerminalClose(tab.parentTabId) this.store?.flushOrThrow?.() } @@ -7641,11 +7680,13 @@ export class OrcaRuntimeService { worktreeId: string, snapshot: RuntimeMobileSessionTabsSnapshot, tab: RuntimeMobileSessionTerminalTab, - options: { killPtys?: boolean } = {} + options: { allowMissingPersistedTab?: boolean; killPtys?: boolean } = {} ): void { const closedParentTabId = tab.parentTabId this.clearRuntimeSessionOwnershipForMobileTab(worktreeId, snapshot, closedParentTabId) - const projectedPtyIds = this.removePersistedHeadlessTerminalTab(worktreeId, closedParentTabId) + const projectedPtyIds = this.removePersistedHeadlessTerminalTab(worktreeId, closedParentTabId, { + allowMissing: options.allowMissingPersistedTab + }) // Why: local provider ids can be reused after restart, so a dormant // persisted id is not kill authority. SSH relay ids remain durable exact // identities even before pane metadata reconnects. @@ -12847,6 +12888,7 @@ export class OrcaRuntimeService { if (pty) { pty.connected = false pty.runtimeSessionOwned = false + this.setPairedRendererSessionOwnership(pty.ptyId, false) pty.disconnectedAt = Date.now() pty.lastExitCode = exitCode this.resolvePtyExitWaiters(pty, ptyId) @@ -24651,11 +24693,13 @@ export class OrcaRuntimeService { launchAgent?: TuiAgent viewMode?: 'terminal' | 'chat' activate?: boolean + clientNavigationId?: string clientMutationId?: string signal?: AbortSignal } = {} ): Promise { - this.assertGraphReady() + const pairedCreate = Boolean(opts.clientNavigationId) + const graphEpoch = this.captureReadyGraphEpoch() const workspace = await this.resolveTerminalWorkspaceLaunchScope(worktreeSelector) const worktreeId = workspace.id const cwd = this.resolveWorkspaceTerminalStartupCwd(workspace, opts.cwd) @@ -24670,10 +24714,13 @@ export class OrcaRuntimeService { afterDesktopTabId = anchor.type === 'terminal' ? anchor.parentTabId : anchor.id } const startupCommand = await this.resolveMobileSessionTerminalCommand(workspace, opts) - + this.assertStableReadyGraph(graphEpoch) + if (opts.signal?.aborted) { + throw new Error('client_disconnected') + } const win = this.getAvailableAuthoritativeWindow() if (!win) { - return await this.createHeadlessMobileSessionTerminal( + return await this.createRuntimeOwnedMobileSessionTerminal( worktreeId, opts.activate !== false, opts.afterTabId, @@ -24686,142 +24733,155 @@ export class OrcaRuntimeService { launchAgent: startupCommand.launchAgent, viewMode: opts.viewMode, targetGroupId: opts.targetGroupId, - launchConfig: startupCommand.launchConfig + launchConfig: startupCommand.launchConfig, + signal: opts.signal } ) } - const requestId = randomUUID() - const reply = await new Promise<{ tabId: string; title: string }>((resolve, reject) => { - const timer = setTimeout(() => { - ipcMain.removeListener('terminal:tabCreateReply', handler) - opts.signal?.removeEventListener('abort', onAbort) - reject(new Error('Terminal creation timed out')) - }, 10_000) - // Why: a dead client connection cancels the wait; the renderer tab (and - // its shell) stays alive for the host and mirrors on reconnect (#7718). - const onAbort = (): void => { - clearTimeout(timer) - ipcMain.removeListener('terminal:tabCreateReply', handler) - reject(new Error('client_disconnected')) - } - - const handler = ( - event: Electron.IpcMainEvent, - r: { requestId: string; tabId?: string; title?: string; error?: string } - ): void => { - if (event.sender !== win.webContents || r.requestId !== requestId) { - return - } - clearTimeout(timer) - ipcMain.removeListener('terminal:tabCreateReply', handler) - opts.signal?.removeEventListener('abort', onAbort) - if (r.error) { - reject(new Error(r.error)) - } else { - resolve({ tabId: r.tabId!, title: r.title ?? '' }) - } - } - opts.signal?.addEventListener('abort', onAbort, { once: true }) - ipcMain.on('terminal:tabCreateReply', handler) - win.webContents.send('terminal:requestTabCreate', { - requestId, - worktreeId, - afterTabId: afterDesktopTabId, - targetGroupId: opts.targetGroupId, - command: startupCommand.command, - cwd, - ...(startupCommand.env ? { env: startupCommand.env } : {}), - ...(startupCommand.envToDelete ? { envToDelete: startupCommand.envToDelete } : {}), - ...(startupCommand.launchConfig ? { launchConfig: startupCommand.launchConfig } : {}), - ...(startupCommand.launchAgent ? { launchAgent: startupCommand.launchAgent } : {}), - ...(opts.viewMode ? { viewMode: opts.viewMode } : {}), - startupCommandDelivery: startupCommand.startupCommandDelivery, - source: 'runtime-session', - activate: opts.activate - }) - }) - - if (opts.activate !== false) { - this.notifier?.focusTerminal(reply.tabId, worktreeId, null) + if (win.webContents.isDestroyed?.()) { + throw new Error('runtime_unavailable') } - // Why: register the wait before the renderer's PTY spawn arrives so that - // spawn (registerPty) can publish the pty-backed surface main-side even if - // graph-sync is stalled (#7587). Removed in the finally below. - const pendingCreateKey = `${worktreeId}::${reply.tabId}` - // Why: a rescue publishes into the active group (opts.targetGroupId is not - // threaded); the renderer's reconciling publication then moves the tab to the - // requested group, so any wrong-group placement is cosmetic and stall-window-only. - this.pendingMobileTerminalCreatesByKey.set(pendingCreateKey, { - activate: opts.activate !== false, - selectIfNoActiveTab: true, - ...(opts.viewMode ? { viewMode: opts.viewMode } : {}) - }) + const releasePublicationThrottle = pairedCreate + ? this.rendererPublicationThrottle.acquire(win.webContents) + : () => {} try { - // Why: the PTY spawn and the tabCreate reply race on independent IPC - // channels; if the spawn already registered, publish immediately so the - // wait resolves without depending on a graph sync. - this.ensurePtyBackedMobileSurfaceForRendererTab(worktreeId, reply.tabId) - const surface = await this.waitForMobileTerminalSurface(worktreeId, reply.tabId, { - timeoutMs: MOBILE_TERMINAL_SURFACE_TIMEOUT_MS, - signal: opts.signal - }) - if (this.isReadyMobileTerminalSurface(surface)) { - return surface - } - const readySurface = await this.waitForMobileTerminalSurface(worktreeId, reply.tabId, { - timeoutMs: MOBILE_TERMINAL_READY_FALLBACK_MS, - requireReady: true, - signal: opts.signal - }).catch(() => null) - if (readySurface) { - return readySurface - } - if (opts.signal?.aborted) { - // Why: nobody awaits this create anymore; don't materialize or roll back — the renderer's own publication settles the tab. - throw new Error('client_disconnected') - } - const pendingSurface = this.findMobileTerminalSurface(worktreeId, reply.tabId) - if (!pendingSurface) { - throw new Error('Timed out waiting for terminal surface after creation') - } - // Why: a hidden renderer can publish the tab shell before the PTY spawns; reuse the same identity so later focus adopts instead of creating another tab. - return await this.createHeadlessMobileSessionTerminal( - worktreeId, - opts.activate !== false, - opts.afterTabId, - { + const requestId = randomUUID() + const reply = await new Promise<{ tabId: string; title: string }>((resolve, reject) => { + const timer = setTimeout(() => { + ipcMain.removeListener('terminal:tabCreateReply', handler) + opts.signal?.removeEventListener('abort', onAbort) + reject(new Error('Terminal creation timed out')) + }, 10_000) + // Why: a dead client connection cancels the wait; the renderer tab (and + // its shell) stays alive for the host and mirrors on reconnect (#7718). + const onAbort = (): void => { + clearTimeout(timer) + ipcMain.removeListener('terminal:tabCreateReply', handler) + reject(new Error('client_disconnected')) + } + + const handler = ( + event: Electron.IpcMainEvent, + r: { requestId: string; tabId?: string; title?: string; error?: string } + ): void => { + if (event.sender !== win.webContents || r.requestId !== requestId) { + return + } + clearTimeout(timer) + ipcMain.removeListener('terminal:tabCreateReply', handler) + opts.signal?.removeEventListener('abort', onAbort) + if (r.error) { + reject(new Error(r.error)) + } else { + resolve({ tabId: r.tabId!, title: r.title ?? '' }) + } + } + opts.signal?.addEventListener('abort', onAbort, { once: true }) + ipcMain.on('terminal:tabCreateReply', handler) + win.webContents.send('terminal:requestTabCreate', { + requestId, + worktreeId, + afterTabId: afterDesktopTabId, + targetGroupId: opts.targetGroupId, command: startupCommand.command, cwd, - env: startupCommand.env, - envToDelete: startupCommand.envToDelete, + ...(startupCommand.env ? { env: startupCommand.env } : {}), + ...(startupCommand.envToDelete ? { envToDelete: startupCommand.envToDelete } : {}), + ...(startupCommand.launchConfig ? { launchConfig: startupCommand.launchConfig } : {}), + ...(startupCommand.launchAgent ? { launchAgent: startupCommand.launchAgent } : {}), + ...(opts.viewMode ? { viewMode: opts.viewMode } : {}), startupCommandDelivery: startupCommand.startupCommandDelivery, - identity: { tabId: pendingSurface.tab.parentTabId, leafId: pendingSurface.tab.leafId }, - launchAgent: startupCommand.launchAgent, - viewMode: opts.viewMode, - targetGroupId: opts.targetGroupId, - launchConfig: startupCommand.launchConfig - } - ) - } catch (error) { - // Why: publication latency (hidden renderer) can trip the surface timeout; rescue only when a live PTY backs the tab, else a ghost tab skips rollback (#7587). - if (this.findLiveRegisteredPtyForRendererTab(worktreeId, reply.tabId)) { - const rescued = this.ensurePtyBackedMobileSurfaceForRendererTab(worktreeId, reply.tabId) - if (rescued) { - return rescued - } + source: 'runtime-session', + activate: opts.activate + }) + }) + + if (opts.activate !== false) { + this.notifier?.focusTerminal(reply.tabId, worktreeId, null) } - // Why: don't roll back on a client disconnect or a live shell already backing the tab — that would kill a visible terminal ("tab dies after ~10s", #7718). - if ( - isClientDisconnectedError(error) || - this.hasLiveShellForRendererTab(worktreeId, reply.tabId) - ) { + // Why: register the wait before the renderer's PTY spawn arrives so that + // spawn (registerPty) can publish the pty-backed surface main-side even if + // graph-sync is stalled (#7587). Removed in the finally below. + const pendingCreateKey = `${worktreeId}::${reply.tabId}` + // Why: a rescue publishes into the active group (opts.targetGroupId is not + // threaded); the renderer's reconciling publication then moves the tab to the + // requested group, so any wrong-group placement is cosmetic and stall-window-only. + this.pendingMobileTerminalCreatesByKey.set(pendingCreateKey, { + activate: opts.activate !== false, + paired: pairedCreate, + selectIfNoActiveTab: true, + ...(opts.viewMode ? { viewMode: opts.viewMode } : {}) + }) + try { + // Why: the PTY spawn and the tabCreate reply race on independent IPC + // channels; if the spawn already registered, publish immediately so the + // wait resolves without depending on a graph sync. + this.ensurePtyBackedMobileSurfaceForRendererTab(worktreeId, reply.tabId) + const surface = await this.waitForMobileTerminalSurface(worktreeId, reply.tabId, { + timeoutMs: MOBILE_TERMINAL_SURFACE_TIMEOUT_MS, + signal: opts.signal + }) + if (this.isReadyMobileTerminalSurface(surface)) { + return surface + } + const readySurface = await this.waitForMobileTerminalSurface(worktreeId, reply.tabId, { + timeoutMs: MOBILE_TERMINAL_READY_FALLBACK_MS, + requireReady: true, + signal: opts.signal + }).catch(() => null) + if (readySurface) { + return readySurface + } + if (opts.signal?.aborted) { + // Why: nobody awaits this create anymore; don't materialize or roll back — the renderer's own publication settles the tab. + throw new Error('client_disconnected') + } + const pendingSurface = this.findMobileTerminalSurface(worktreeId, reply.tabId) + if (!pendingSurface) { + throw new Error('Timed out waiting for terminal surface after creation') + } + // Why: a hidden renderer can publish the tab shell before the PTY spawns; reuse the same identity so later focus adopts instead of creating another tab. + return await this.createRuntimeOwnedMobileSessionTerminal( + worktreeId, + opts.activate !== false, + opts.afterTabId, + { + command: startupCommand.command, + cwd, + env: startupCommand.env, + envToDelete: startupCommand.envToDelete, + startupCommandDelivery: startupCommand.startupCommandDelivery, + identity: { tabId: pendingSurface.tab.parentTabId, leafId: pendingSurface.tab.leafId }, + launchAgent: startupCommand.launchAgent, + viewMode: opts.viewMode, + targetGroupId: opts.targetGroupId, + launchConfig: startupCommand.launchConfig, + signal: opts.signal + } + ) + } catch (error) { + // Why: publication latency (hidden renderer) can trip the surface timeout; rescue only when a live PTY backs the tab, else a ghost tab skips rollback (#7587). + if (this.findLiveRegisteredPtyForRendererTab(worktreeId, reply.tabId)) { + const rescued = this.ensurePtyBackedMobileSurfaceForRendererTab(worktreeId, reply.tabId) + if (rescued) { + return rescued + } + } + // Why: don't roll back on a client disconnect or a live shell already backing the tab — that would kill a visible terminal ("tab dies after ~10s", #7718). + if ( + isClientDisconnectedError(error) || + this.hasLiveShellForRendererTab(worktreeId, reply.tabId) + ) { + throw error + } + // Why: renderer made the tab but no live PTY backs it (real spawn/handle failure); roll it back so it can't linger as a ghost in mobile snapshots. + this.notifier?.closeTerminal(reply.tabId) throw error + } finally { + this.pendingMobileTerminalCreatesByKey.delete(pendingCreateKey) } - // Why: renderer made the tab but no live PTY backs it (real spawn/handle failure); roll it back so it can't linger as a ghost in mobile snapshots. - this.notifier?.closeTerminal(reply.tabId) - throw error } finally { - this.pendingMobileTerminalCreatesByKey.delete(pendingCreateKey) + releasePublicationThrottle() } } @@ -24909,7 +24969,7 @@ export class OrcaRuntimeService { } } - private async createHeadlessMobileSessionTerminal( + private async createRuntimeOwnedMobileSessionTerminal( worktreeId: string, activate: boolean, afterTabId?: string, @@ -24924,6 +24984,7 @@ export class OrcaRuntimeService { viewMode?: 'terminal' | 'chat' targetGroupId?: string launchConfig?: SleepingAgentLaunchConfig + signal?: AbortSignal } = {} ): Promise { const workspace = await this.resolveTerminalWorkspaceLaunchScope(`id:${worktreeId}`) @@ -24952,7 +25013,8 @@ export class OrcaRuntimeService { : {}), persistHostSessionBinding: true, // Why: this method publishes the authoritative snapshot below; skip the intermediate publish to avoid a wrong-group flash. - deferMobileSessionPublish: true + deferMobileSessionPublish: true, + signal: opts.signal }) const livePty = this.getLivePtyForHandle(terminal.handle) if (!livePty) { @@ -25148,6 +25210,9 @@ export class OrcaRuntimeService { const pty = this.findLiveRegisteredPtyForRendererTab(worktreeId, tabId) if (pty) { pty.runtimeSessionOwned = true + if (pending.paired) { + this.setPairedRendererSessionOwnership(pty.ptyId, true) + } } if ( existing && @@ -25176,6 +25241,69 @@ export class OrcaRuntimeService { return this.findMobileTerminalSurface(worktreeId, tabId) } + private restoreLivePairedRendererSessionOwnedMobileTerminals( + worktreeId: string | null, + options: { missingSnapshotOnly?: boolean; notify?: boolean } = {} + ): void { + for (const ptyId of this.pairedRendererSessionOwnedPtyIds) { + const pty = this.ptysById.get(ptyId) + if ( + !pty?.connected || + !pty.tabId || + (worktreeId !== null && !runtimeWorktreeIdsEqual(pty.worktreeId, worktreeId)) + ) { + continue + } + const targetWorktreeId = worktreeId ?? pty.worktreeId + const pane = parsePaneKey(pty.paneKey ?? '') + if (!pane || pane.tabId !== pty.tabId) { + continue + } + const existing = this.mobileSessionTabsByWorktree.get(targetWorktreeId) + if (existing && options.missingSnapshotOnly) { + continue + } + if ( + existing?.tabs.some( + (tab) => + tab.type === 'terminal' && + (tab.ptyId === pty.ptyId || + (tab.parentTabId === pty.tabId && tab.leafId === pane.leafId)) + ) + ) { + continue + } + if (!existing) { + this.mobileSessionTabsByWorktree.set(targetWorktreeId, { + worktree: targetWorktreeId, + publicationEpoch: `renderer-rescue:${Date.now().toString(36)}`, + snapshotVersion: 0, + activeGroupId: null, + activeTabId: null, + activeTabType: null, + tabGroups: [], + tabs: [] + }) + } + this.publishPtyBackedMobileSessionTerminal(targetWorktreeId, pty, { + tabId: pty.tabId, + leafId: pane.leafId, + title: null, + activate: false, + selectIfNoActiveTab: false, + notify: options.notify + }) + } + } + + private setPairedRendererSessionOwnership(ptyId: string, owned: boolean): void { + if (owned) { + this.pairedRendererSessionOwnedPtyIds.add(ptyId) + } else { + this.pairedRendererSessionOwnedPtyIds.delete(ptyId) + } + } + private findLiveRegisteredPtyForRendererTab( worktreeId: string, tabId: string @@ -25602,6 +25730,10 @@ export class OrcaRuntimeService { createdPty.tabId = parentTabId createdPty.paneKey = paneKey createdPty.runtimeSessionOwned = pty.runtimeSessionOwned + this.setPairedRendererSessionOwnership( + createdPty.ptyId, + this.pairedRendererSessionOwnedPtyIds.has(pty.ptyId) + ) } try { @@ -25617,6 +25749,7 @@ export class OrcaRuntimeService { splitTelemetrySource: opts.telemetrySource }) } catch (error) { + this.setPairedRendererSessionOwnership(result.id, false) this.ptyController.kill?.(result.id) throw error } @@ -27978,6 +28111,7 @@ export class OrcaRuntimeService { private dropDisconnectedPtyRecord(ptyId: string): void { // Why: pruning can remove a PTY without the normal exit callback. this.advancePtyLifecycleGeneration(ptyId) + this.pairedRendererSessionOwnedPtyIds.delete(ptyId) this.ptysById.delete(ptyId) this.recentPtyOutputById.delete(ptyId) this.setupCompletionTokenByPtyId.delete(ptyId) @@ -28110,6 +28244,10 @@ export class OrcaRuntimeService { // renderer resends before they replace an entry — so reference identity // before/after detects exactly the entries that actually changed. const before = new Map(this.mobileSessionTabsByWorktree) + this.restoreLivePairedRendererSessionOwnedMobileTerminals(null, { + missingSnapshotOnly: true, + notify: false + }) const worktreeIdsToHydrate = this.getKnownWorkspaceSessionWorktreeIds() for (const snapshot of snapshots) { worktreeIdsToHydrate.add(snapshot.worktree) diff --git a/src/main/runtime/rpc/methods/session-tab-close-methods.ts b/src/main/runtime/rpc/methods/session-tab-close-methods.ts index e11e2b78a..eabffa24a 100644 --- a/src/main/runtime/rpc/methods/session-tab-close-methods.ts +++ b/src/main/runtime/rpc/methods/session-tab-close-methods.ts @@ -28,7 +28,10 @@ export const SESSION_TAB_CLOSE_METHODS: RpcAnyMethod[] = [ const result = await context.runtime.closeMobileSessionTab( params.worktree, params.tabId, - { reason: 'user' } + { + reason: 'user', + ...(context.pairedDeviceId ? { clientNavigationId: context.pairedDeviceId } : {}) + } ) span.setAttribute( 'decision', @@ -68,7 +71,8 @@ export const SESSION_TAB_CLOSE_METHODS: RpcAnyMethod[] = [ { reason: params.reason, expectedPublicationEpoch: params.publicationEpoch, - expectedTerminalHandle: params.terminal + expectedTerminalHandle: params.terminal, + ...(context.pairedDeviceId ? { clientNavigationId: context.pairedDeviceId } : {}) } ) span.setAttribute( diff --git a/src/main/runtime/rpc/methods/session-tabs.test.ts b/src/main/runtime/rpc/methods/session-tabs.test.ts index b9496b3da..428fde012 100644 --- a/src/main/runtime/rpc/methods/session-tabs.test.ts +++ b/src/main/runtime/rpc/methods/session-tabs.test.ts @@ -136,7 +136,8 @@ describe('session tab RPC methods', () => { expect(replies).toHaveLength(1) expect(runtime.closeMobileSessionTab).toHaveBeenCalledWith('id:wt-1', 'tab-1', { - reason: 'user' + reason: 'user', + clientNavigationId: 'current-runtime' }) expect(runtime.refuseUnattributedMobileSessionTabClose).not.toHaveBeenCalled() }) @@ -180,7 +181,8 @@ describe('session tab RPC methods', () => { expect(replies).toHaveLength(1) expect(runtime.closeMobileSessionTab).toHaveBeenCalledWith('id:wt-1', 'tab-1', { - reason: 'user' + reason: 'user', + clientNavigationId: 'legacy-runtime' }) expect(runtime.refuseUnattributedMobileSessionTabClose).not.toHaveBeenCalled() }) diff --git a/src/main/runtime/runtime-rpc.test.ts b/src/main/runtime/runtime-rpc.test.ts index 433967935..fe44dc64b 100644 --- a/src/main/runtime/runtime-rpc.test.ts +++ b/src/main/runtime/runtime-rpc.test.ts @@ -3419,6 +3419,47 @@ describe('OrcaRuntimeRpcServer', () => { ) }) + it('rejects unpaired terminal creates before runtime dispatch', async () => { + const userDataPath = mkdtempSync(join(tmpdir(), 'orca-runtime-rpc-')) + const createMobileSessionTerminal = vi.fn() + const runtime = { + getRuntimeId: () => 'test-runtime', + createMobileSessionTerminal + } as unknown as OrcaRuntimeService + const server = new OrcaRuntimeRpcServer({ runtime, userDataPath, enableWebSocket: false }) + server['deviceRegistry'] = new DeviceRegistry(userDataPath) + const replies: Record[] = [] + const send = async (id: string, deviceToken?: string): Promise => { + await server['handleWebSocketMessage']( + JSON.stringify({ + id, + method: 'session.tabs.createTerminal', + ...(deviceToken ? { deviceToken } : {}), + params: { worktree: 'id:wt-1' } + }), + (response) => replies.push(JSON.parse(response) as Record), + () => {} + ) + } + + await send('req_missing') + await send('req_invalid', 'invalid-token') + + expect(replies).toEqual([ + expect.objectContaining({ + id: 'req_missing', + error: expect.objectContaining({ code: 'unauthorized' }), + ok: false + }), + expect.objectContaining({ + id: 'req_invalid', + error: expect.objectContaining({ code: 'unauthorized' }), + ok: false + }) + ]) + expect(createMobileSessionTerminal).not.toHaveBeenCalled() + }) + it('allows runtime-scoped WebSocket tokens to use the full RPC surface', async () => { const userDataPath = mkdtempSync(join(tmpdir(), 'orca-runtime-rpc-')) const pushRuntimeGit = vi.fn().mockResolvedValue({ ok: true }) diff --git a/src/main/window/renderer-publication-throttle.test.ts b/src/main/window/renderer-publication-throttle.test.ts new file mode 100644 index 000000000..5715ef25a --- /dev/null +++ b/src/main/window/renderer-publication-throttle.test.ts @@ -0,0 +1,58 @@ +import { describe, expect, it } from 'vitest' +import { + RendererPublicationThrottle, + type RendererPublicationThrottleTarget +} from './renderer-publication-throttle' + +function createTarget(): RendererPublicationThrottleTarget & { + calls: boolean[] + destroyed: boolean +} { + const calls: boolean[] = [] + return { + calls, + destroyed: false, + isDestroyed() { + return this.destroyed + }, + setBackgroundThrottling(allowed) { + calls.push(allowed) + } + } +} + +describe('RendererPublicationThrottle', () => { + it('unthrottles only while a publication lease is active', () => { + const target = createTarget() + const throttle = new RendererPublicationThrottle() + + const release = throttle.acquire(target) + release() + + expect(target.calls).toEqual([false, true]) + }) + + it('keeps overlapping publications unthrottled until the last release', () => { + const target = createTarget() + const throttle = new RendererPublicationThrottle() + + const releaseFirst = throttle.acquire(target) + const releaseSecond = throttle.acquire(target) + releaseFirst() + releaseSecond() + releaseSecond() + + expect(target.calls).toEqual([false, true]) + }) + + it('does not restore throttling on a destroyed renderer', () => { + const target = createTarget() + const throttle = new RendererPublicationThrottle() + + const release = throttle.acquire(target) + target.destroyed = true + release() + + expect(target.calls).toEqual([false]) + }) +}) diff --git a/src/main/window/renderer-publication-throttle.ts b/src/main/window/renderer-publication-throttle.ts new file mode 100644 index 000000000..c7a63d745 --- /dev/null +++ b/src/main/window/renderer-publication-throttle.ts @@ -0,0 +1,32 @@ +export type RendererPublicationThrottleTarget = { + isDestroyed?: () => boolean + setBackgroundThrottling: (allowed: boolean) => void +} + +export class RendererPublicationThrottle { + private readonly leasesByTarget = new Map() + + acquire(target: RendererPublicationThrottleTarget): () => void { + const leaseCount = this.leasesByTarget.get(target) ?? 0 + if (leaseCount === 0) { + target.setBackgroundThrottling(false) + } + this.leasesByTarget.set(target, leaseCount + 1) + let released = false + return () => { + if (released) { + return + } + released = true + const remaining = (this.leasesByTarget.get(target) ?? 1) - 1 + if (remaining > 0) { + this.leasesByTarget.set(target, remaining) + return + } + this.leasesByTarget.delete(target) + if (target.isDestroyed?.() !== true) { + target.setBackgroundThrottling(true) + } + } + } +} diff --git a/src/renderer/src/store/slices/cmd-j-create-actions.test.ts b/src/renderer/src/store/slices/cmd-j-create-actions.test.ts index 179321855..abf1904cc 100644 --- a/src/renderer/src/store/slices/cmd-j-create-actions.test.ts +++ b/src/renderer/src/store/slices/cmd-j-create-actions.test.ts @@ -1,5 +1,6 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { FLOATING_TERMINAL_WORKTREE_ID } from '../../../../shared/constants' +import { folderWorkspaceKey } from '../../../../shared/workspace-scope' import type { AppState } from '../types' import { createTestStore, makeWorktree, seedStore, TEST_REPO } from './store-test-helpers' @@ -15,6 +16,11 @@ vi.mock('@/lib/focus-terminal-tab-surface', () => ({ focusTerminalTabSurface: vi.fn() })) +vi.mock('@/lib/web-client-location', () => ({ + isWebClientLocation: () => + Boolean((globalThis as { __ORCA_WEB_CLIENT__?: boolean }).__ORCA_WEB_CLIENT__) +})) + const pairedWebFlag = globalThis as { __ORCA_WEB_CLIENT__?: boolean } function seedActiveWorkspace(store: ReturnType): void { @@ -165,6 +171,61 @@ describe('Cmd+J lifted creation actions', () => { expect(store.getState().tabsByWorktree['wt-1'] ?? []).toEqual([]) }) + it('does not create a local folder terminal while paired ownership is unresolved', async () => { + const folderId = 'folder-1' + const workspaceKey = folderWorkspaceKey(folderId) + const store = createTestStore() + seedStore(store, { + activeWorktreeId: workspaceKey, + settings: { activeRuntimeEnvironmentId: null } as AppState['settings'], + folderWorkspaces: [ + { + id: folderId, + projectGroupId: 'group-1', + name: 'Folder workspace', + folderPath: '/tmp/folder', + connectionId: null, + linkedTask: null, + comment: '', + isArchived: false, + isUnread: false, + isPinned: false, + sortOrder: 1, + lastActivityAt: 0, + createdAt: 1, + updatedAt: 1 + } + ], + projectGroups: [ + { + id: 'group-1', + name: 'Folder group', + parentPath: '/tmp', + connectionId: null, + executionHostId: null, + parentGroupId: null, + createdFrom: 'manual', + tabOrder: 0, + isCollapsed: false, + color: null, + createdAt: 1, + updatedAt: 1 + } + ], + groupsByWorktree: { + [workspaceKey]: [ + { id: 'tab-group-1', worktreeId: workspaceKey, activeTabId: null, tabOrder: [] } + ] + }, + activeGroupIdByWorktree: { [workspaceKey]: 'tab-group-1' } + }) + + await store.getState().openNewTerminalTabInActiveWorkspace('tab-group-1') + + expect(createWebRuntimeSessionTerminalMock).not.toHaveBeenCalled() + expect(store.getState().tabsByWorktree[workspaceKey] ?? []).toEqual([]) + }) + it('creates desktop remote-server terminal tabs through the owning runtime', async () => { delete pairedWebFlag.__ORCA_WEB_CLIENT__ createWebRuntimeSessionTerminalMock.mockResolvedValue(false) diff --git a/src/renderer/src/store/slices/terminals.ts b/src/renderer/src/store/slices/terminals.ts index 210c2f355..4d1492db8 100644 --- a/src/renderer/src/store/slices/terminals.ts +++ b/src/renderer/src/store/slices/terminals.ts @@ -124,6 +124,7 @@ import { focusTerminalTabSurface } from '@/lib/focus-terminal-tab-surface' import { getRuntimeEnvironmentIdForWorktree } from '@/lib/worktree-runtime-owner' import { resolveTerminalWorktreeRoute } from '@/lib/terminal-worktree-route' import { resolveWorktreeOperationRouteResult } from '@/lib/worktree-operation-route' +import { isWebClientLocation } from '@/lib/web-client-location' import { getLocalProjectExecutionRuntimeContext } from '@/lib/local-preflight-context' import type { NativeChatLaunchDraft, NativeChatLaunchPrompt } from '@/lib/native-chat-launch-prompt' import { resolveAgentPaneAuthorityKey } from './agent-pane-authority' @@ -1527,6 +1528,9 @@ export const createTerminalSlice: StateCreator }) return } + if (isWebClientLocation() && worktreeId !== FLOATING_TERMINAL_WORKTREE_ID) { + return + } const terminal = get().createTab(worktreeId, groupId) get().setActiveTab(terminal.id) get().setActiveTabType('terminal') diff --git a/tests/e2e/paired-remote-terminal-stall-recovery.spec.ts b/tests/e2e/paired-remote-terminal-stall-recovery.spec.ts index 2f6026f0b..4406ebd2f 100644 --- a/tests/e2e/paired-remote-terminal-stall-recovery.spec.ts +++ b/tests/e2e/paired-remote-terminal-stall-recovery.spec.ts @@ -1,7 +1,8 @@ import { mkdtempSync, rmSync, writeFileSync } from 'node:fs' import os from 'node:os' import path from 'node:path' -import type { Page } from '@stablyai/playwright-test' +import type { ElectronApplication, Page } from '@stablyai/playwright-test' +import { PNG } from 'pngjs' import type { RuntimeTerminalRead } from '../../src/shared/runtime-types' import { toWebTerminalSurfaceTabId } from '../../src/shared/terminal-surface-id' import { expect, test } from './helpers/orca-app' @@ -12,6 +13,7 @@ import { import { getTerminalContent, waitForActivePanePtyId } from './helpers/terminal' const MIN_EXHAUSTED_ACK_BYTES = 400 * 1024 +const PUBLICATION_DEADLINE_MS = 10_000 const scratch = mkdtempSync(path.join(os.tmpdir(), 'orca-paired-stalled-stream-')) const fixturePath = path.join(scratch, 'stalled-stream-terminal.mjs') writeFileSync( @@ -30,7 +32,7 @@ writeFileSync( " process.stdout.write('HOST_FLOOD_COMPLETE\\r\\n')", ' continue', ' }', - ' process.stdout.write(`LIVE:${input}\\r\\n`)', + " process.stdout.write(`\\x1b[48;2;24;96;144mLIVE:${input}${' '.repeat(48)}\\x1b[0m\\r\\n`)", ' }', '})', 'process.stdin.resume()' @@ -65,11 +67,169 @@ async function callRuntime(page: Page, method: string, params: unknown) ) as Promise } +type AppResourceProxies = { + gpuCpuPercent: number + gpuIdleWakeupsPerSecond: number + processCount: number + totalCpuPercent: number + totalIdleWakeupsPerSecond: number +} + +async function getAppResourceProxies( + electronApp: ElectronApplication +): Promise { + return electronApp.evaluate(({ app }) => { + const metrics = app.getAppMetrics() + const gpu = metrics.find((metric) => metric.type === 'GPU') + return { + gpuCpuPercent: gpu?.cpu.percentCPUUsage ?? 0, + gpuIdleWakeupsPerSecond: gpu?.cpu.idleWakeupsPerSecond ?? 0, + processCount: metrics.length, + totalCpuPercent: metrics.reduce((total, metric) => total + metric.cpu.percentCPUUsage, 0), + totalIdleWakeupsPerSecond: metrics.reduce( + (total, metric) => total + metric.cpu.idleWakeupsPerSecond, + 0 + ) + } + }) +} + +async function minimizeHeadedHost(electronApp: ElectronApplication, page: Page): Promise { + const host = await electronApp.browserWindow(page) + await host.evaluate((window) => window.minimize()) + await expect + .poll(() => + host.evaluate((window) => ({ + backgroundThrottling: window.webContents.getBackgroundThrottling(), + minimized: window.isMinimized(), + visible: window.isVisible() + })) + ) + .toEqual({ backgroundThrottling: true, minimized: true, visible: false }) +} + +async function restoreHeadedHost(electronApp: ElectronApplication, page: Page): Promise { + const host = await electronApp.browserWindow(page) + await host.evaluate((window) => { + window.restore() + window.show() + window.focus() + }) + await expect + .poll(() => + host.evaluate((window) => ({ + backgroundThrottling: window.webContents.getBackgroundThrottling(), + minimized: window.isMinimized(), + visible: window.isVisible() + })) + ) + .toEqual({ backgroundThrottling: true, minimized: false, visible: true }) + await expect.poll(() => page.evaluate(() => document.visibilityState)).toBe('visible') + await page.evaluate( + () => new Promise((resolve) => requestAnimationFrame(() => requestAnimationFrame(resolve))) + ) +} + +async function showHeadedClient(electronApp: ElectronApplication, page: Page): Promise { + const clientWindow = await electronApp.browserWindow(page) + await clientWindow.evaluate((window) => { + window.show() + window.focus() + }) + await expect.poll(() => clientWindow.evaluate((window) => window.isVisible())).toBe(true) +} + +function countForegroundPixels(buffer: Buffer): number { + const image = PNG.sync.read(buffer) + const buckets = new Map() + for (let offset = 0; offset < image.data.length; offset += 4) { + if ((image.data[offset + 3] ?? 0) < 128) { + continue + } + const red = image.data[offset] ?? 0 + const green = image.data[offset + 1] ?? 0 + const blue = image.data[offset + 2] ?? 0 + const key = `${red >> 3},${green >> 3},${blue >> 3}` + const bucket = buckets.get(key) ?? { count: 0, red, green, blue } + bucket.count += 1 + buckets.set(key, bucket) + } + const background = [...buckets.values()].sort((a, b) => b.count - a.count)[0] + if (!background) { + return 0 + } + let foregroundPixels = 0 + for (let offset = 0; offset < image.data.length; offset += 4) { + if ((image.data[offset + 3] ?? 0) < 128) { + continue + } + const distance = + Math.abs((image.data[offset] ?? 0) - background.red) + + Math.abs((image.data[offset + 1] ?? 0) - background.green) + + Math.abs((image.data[offset + 2] ?? 0) - background.blue) + if (distance > 48) { + foregroundPixels += 1 + } + } + return foregroundPixels +} + +function countVisualMarkerPixels(buffer: Buffer): number { + const image = PNG.sync.read(buffer) + let count = 0 + for (let offset = 0; offset < image.data.length; offset += 4) { + const red = image.data[offset] ?? 0 + const green = image.data[offset + 1] ?? 0 + const blue = image.data[offset + 2] ?? 0 + if ( + (image.data[offset + 3] ?? 0) >= 245 && + blue >= 100 && + blue - red >= 50 && + blue - green >= 20 && + green - red >= 10 + ) { + count += 1 + } + } + return count +} + +async function findHostPaneWithMarker( + page: Page, + marker: string +): Promise<{ paneId: number; ptyId: string; tabId: string }> { + let target: { paneId: number; ptyId: string; tabId: string } | null = null + await expect + .poll( + async () => { + target = await page.evaluate((expectedMarker) => { + for (const [tabId, manager] of window.__paneManagers?.entries() ?? []) { + for (const pane of manager.getPanes?.() ?? []) { + const content = pane.serializeAddon?.serialize?.() ?? '' + const ptyId = pane.container?.dataset?.ptyId + if (content.includes(expectedMarker) && ptyId) { + return { paneId: pane.id, ptyId, tabId } + } + } + } + return null + }, marker) + return target !== null + }, + { timeout: 30_000, message: 'host renderer never mirrored the paired terminal marker' } + ) + .toBe(true) + if (!target) { + throw new Error('Host terminal marker target disappeared') + } + return target +} + test('restarts one ACK-starved paired terminal stream without replacing its PTY @headful', async ({ electronApp, orcaPage -}) => { - test.setTimeout(120_000) +}, testInfo) => { + test.setTimeout(150_000) const liveMarker = `PAIRED_STALL_RECOVERED_${Date.now()}` const worktree = await orcaPage.evaluate(() => { const state = window.__store?.getState() @@ -80,11 +240,13 @@ test('restarts one ACK-starved paired terminal stream without replacing its PTY } return { id: active.id } }) + const noClientResources = await getAppResourceProxies(electronApp) const offer = await createRuntimeDesktopPairingOffer(orcaPage) const client = await launchPairedWebClient(electronApp, offer, { disableRemoteTerminalStallRecovery: process.env.ORCA_E2E_DISABLE_REMOTE_TERMINAL_STALL_RECOVERY === '1' }) + let observer: Awaited> | null = null let terminal: string | null = null try { await expect @@ -101,8 +263,29 @@ test('restarts one ACK-starved paired terminal stream without replacing its PTY { timeout: 30_000 } ) .toBe(true) + const observerOffer = await createRuntimeDesktopPairingOffer(orcaPage) + observer = await launchPairedWebClient(electronApp, observerOffer) + await showHeadedClient(electronApp, client.page) + await showHeadedClient(electronApp, observer.page) + await expect + .poll( + () => + observer?.page.evaluate( + (worktreeId) => + window.__store + ?.getState() + .allWorktrees() + .some((candidate) => candidate.id === worktreeId), + worktree.id + ), + { timeout: 30_000 } + ) + .toBe(true) + const connectedIdleResources = await getAppResourceProxies(electronApp) + await minimizeHeadedHost(electronApp, orcaPage) + const createStartedAt = performance.now() const created = await callRuntime<{ - tab: { parentTabId: string; terminal: string | null } + tab: { id: string; parentTabId: string; terminal: string | null } }>(client.page, 'session.tabs.createTerminal', { worktree: `id:${worktree.id}`, command: fixtureCommand(), @@ -110,6 +293,8 @@ test('restarts one ACK-starved paired terminal stream without replacing its PTY select: false, navigation: 'caller' }) + const createLatencyMs = performance.now() - createStartedAt + expect(createLatencyMs).toBeLessThan(PUBLICATION_DEADLINE_MS) terminal = created.tab.terminal if (!terminal) { throw new Error('Paired runtime did not publish the stalled-stream fixture') @@ -128,6 +313,42 @@ test('restarts one ACK-starved paired terminal stream without replacing its PTY { timeout: 30_000 } ) .toBe(true) + await expect + .poll( + () => + observer?.page.evaluate( + ({ tabId, worktreeId }) => + (window.__store?.getState().tabsByWorktree[worktreeId] ?? []).some( + (tab) => tab.id === tabId + ), + { tabId: webTabId, worktreeId: worktree.id } + ), + { timeout: 30_000 } + ) + .toBe(true) + await expect + .poll( + () => + orcaPage.evaluate( + ({ tabId, worktreeId }) => + (window.__store?.getState().tabsByWorktree[worktreeId] ?? []).some( + (tab) => tab.id === tabId + ), + { tabId: created.tab.parentTabId, worktreeId: worktree.id } + ), + { timeout: 30_000 } + ) + .toBe(true) + expect( + ( + await callRuntime<{ tabs: { terminal?: string | null }[] }>( + client.page, + 'session.tabs.list', + { worktree: `id:${worktree.id}` } + ) + ).tabs.some((candidate) => candidate.terminal === terminal), + 'authoritative inventory dropped the terminal before ACK starvation' + ).toBe(true) await client.page.evaluate( (worktreeId) => window.__store?.getState().setActiveWorktree(worktreeId), worktree.id @@ -139,6 +360,20 @@ test('restarts one ACK-starved paired terminal stream without replacing its PTY await expect .poll(() => getTerminalContent(client.page), { timeout: 30_000 }) .toContain('PAIRED_STALL_READY') + await observer.page.evaluate( + (worktreeId) => window.__store?.getState().setActiveWorktree(worktreeId), + worktree.id + ) + const observerTab = observer.page.locator( + `[data-testid="sortable-tab"][data-tab-id="${webTabId}"]` + ) + await expect(observerTab).toBeVisible({ timeout: 30_000 }) + await observerTab.click() + const observerOriginalPtyId = await waitForActivePanePtyId(observer.page, 30_000) + expect(observerOriginalPtyId.split('@@').at(-1)).toBe(originalPtyId.split('@@').at(-1)) + await expect + .poll(() => getTerminalContent(observer.page), { timeout: 30_000 }) + .toContain('PAIRED_STALL_READY') await client.page.evaluate((target) => { const gate = ( @@ -233,6 +468,157 @@ test('restarts one ACK-starved paired terminal stream without replacing its PTY .toContain(`LIVE:${liveMarker}`) expect(await waitForActivePanePtyId(client.page, 30_000)).toBe(originalPtyId) await expect(tab).toHaveAttribute('data-active', 'true') + await expect + .poll(() => getTerminalContent(observer.page), { timeout: 30_000 }) + .toContain(`LIVE:${liveMarker}`) + expect(await waitForActivePanePtyId(observer.page, 30_000)).toBe(observerOriginalPtyId) + expect( + ( + await callRuntime<{ tabs: { terminal?: string | null }[] }>( + client.page, + 'session.tabs.list', + { worktree: `id:${worktree.id}` } + ) + ).tabs.some((candidate) => candidate.terminal === terminal), + 'authoritative inventory dropped the terminal during ACK recovery' + ).toBe(true) + + await restoreHeadedHost(electronApp, orcaPage) + await orcaPage.evaluate( + (worktreeId) => window.__store?.getState().setActiveWorktree(worktreeId), + worktree.id + ) + const hostTab = orcaPage.locator( + `[data-testid="sortable-tab"][data-tab-id="${created.tab.parentTabId}"]` + ) + await expect(hostTab).toBeVisible({ timeout: 30_000 }) + await hostTab.click() + const hostPane = await findHostPaneWithMarker(orcaPage, `LIVE:${liveMarker}`) + expect(hostPane.tabId).toBe(created.tab.parentTabId) + await orcaPage.evaluate(({ paneId, tabId }) => { + const manager = window.__paneManagers?.get(tabId) + manager?.setActivePane?.(paneId, { focus: true }) + }, hostPane) + await expect + .poll(() => getTerminalContent(orcaPage), { timeout: 30_000 }) + .toContain(`LIVE:${liveMarker}`) + const restoredTerminalScreenshot = await orcaPage + .locator( + `[data-terminal-tab-id="${hostPane.tabId}"] .pane[data-pane-id="${hostPane.paneId}"] .xterm-screen` + ) + .screenshot({ animations: 'disabled' }) + await testInfo.attach('host-terminal-after-create-restore', { + body: restoredTerminalScreenshot, + contentType: 'image/png' + }) + expect( + countVisualMarkerPixels(restoredTerminalScreenshot), + 'host terminal marker did not repaint after the background publication toggle' + ).toBeGreaterThan(500) + expect( + ( + await callRuntime<{ tabs: { terminal?: string | null }[] }>( + client.page, + 'session.tabs.list', + { worktree: `id:${worktree.id}` } + ) + ).tabs.some((candidate) => candidate.terminal === terminal), + 'authoritative inventory dropped the terminal while restoring the host' + ).toBe(true) + + await minimizeHeadedHost(electronApp, orcaPage) + await showHeadedClient(electronApp, observer.page) + + const authoritativeInventory = await callRuntime<{ + tabs: { id: string; parentTabId?: string; terminal?: string | null }[] + }>(client.page, 'session.tabs.list', { worktree: `id:${worktree.id}` }) + const authoritativeTab = authoritativeInventory.tabs.find( + (candidate) => candidate.terminal === terminal + ) + if (!authoritativeTab) { + throw new Error( + `Paired terminal was absent from authoritative inventory before close: ${JSON.stringify( + authoritativeInventory.tabs.map((candidate) => ({ + id: candidate.id, + parentTabId: candidate.parentTabId, + terminal: candidate.terminal + })) + )}` + ) + } + const closeStartedAt = performance.now() + await callRuntime(client.page, 'session.tabs.close', { + worktree: `id:${worktree.id}`, + tabId: authoritativeTab.id, + reason: 'user', + navigation: 'caller' + }) + const closeLatencyMs = performance.now() - closeStartedAt + expect(closeLatencyMs).toBeLessThan(PUBLICATION_DEADLINE_MS) + await expect + .poll( + () => + Promise.all([ + orcaPage.evaluate( + ({ tabId, worktreeId }) => + (window.__store?.getState().tabsByWorktree[worktreeId] ?? []).some( + (candidate) => candidate.id === tabId + ), + { tabId: created.tab.parentTabId, worktreeId: worktree.id } + ), + client.page.evaluate( + ({ tabId, worktreeId }) => + (window.__store?.getState().tabsByWorktree[worktreeId] ?? []).some( + (candidate) => candidate.id === tabId + ), + { tabId: webTabId, worktreeId: worktree.id } + ), + observer.page.evaluate( + ({ tabId, worktreeId }) => + (window.__store?.getState().tabsByWorktree[worktreeId] ?? []).some( + (candidate) => candidate.id === tabId + ), + { tabId: webTabId, worktreeId: worktree.id } + ) + ]), + { timeout: 30_000 } + ) + .toEqual([false, false, false]) + terminal = null + + await restoreHeadedHost(electronApp, orcaPage) + const restoredHostScreenshot = await orcaPage.screenshot({ fullPage: true }) + expect( + countForegroundPixels(restoredHostScreenshot), + 'host compositor remained blank after the background close toggle' + ).toBeGreaterThan(1_000) + await testInfo.attach('host-window-after-close-restore', { + body: restoredHostScreenshot, + contentType: 'image/png' + }) + + const afterPublicationResources = await getAppResourceProxies(electronApp) + const evidencePath = test.info().outputPath('publication-evidence.json') + writeFileSync( + evidencePath, + `${JSON.stringify( + { + afterPublicationResources, + closeLatencyMs, + connectedIdleResources, + createLatencyMs, + hostMinimizedDuringCreateAndClose: true, + hostRestoredAfterEachPublication: true, + noClientResources + }, + null, + 2 + )}\n` + ) + await test.info().attach('publication-evidence', { + contentType: 'application/json', + path: evidencePath + }) } finally { await client.page .evaluate(() => { @@ -243,6 +629,7 @@ test('restarts one ACK-starved paired terminal stream without replacing its PTY ).__remoteTerminalMultiplexAckGate?.release() }) .catch(() => undefined) + await observer?.dispose() if (terminal) { await callRuntime(client.page, 'terminal.closeTab', { terminal }).catch(() => undefined) }