From adbccb69a8a9fd0f2bdf16f3201c592d70e4cb8c Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Tue, 10 Nov 2026 03:30:15 -0800 Subject: [PATCH] fix(terminal): keep remote-viewer clipboard env on command-less creates (#40550) * fix(terminal): keep remote-viewer clipboard env on command-less creates (#39852) A paired viewer's plain terminal reaches the host renderer's tab-create bridge with spawn env (ORCA_REMOTE_VIEWER_CLIPBOARD, PI_CLIPBOARD_OSC52) but no startup command. The bridge queued pendingStartup only when a command existed, dropping the env, so the spawned shell never saw the remote-viewer clipboard marker and copied text stayed on the host clipboard (#39852). - Queue the startup whenever the request carries launch material (env/envToDelete/launchConfig/resume), not only a command. - PtyPaneStartup.command becomes optional for env-only startups; spawn options already tolerate a missing command. - Keep the plain-shell pre-bind input hold for startups that plan no post-bind work, so an env-only startup types as a plain shell does. Co-Authored-By: Claude Code * test(e2e): layout oracle marks only panes the user can see A hidden tab with a queued startup is laid out at opacity 0 so it spawns at real geometry. The marker check counted it as visible because it has a size, then waited for output that the hidden-delivery gate correctly withholds. Paired-client creates now queue spawn env, so their background tabs hit this. --------- Co-authored-by: Chen Co-authored-by: Claude Code --- .../layout/split/pane-split-startup.ts | 5 +- .../pty-connection/pane-transport-options.ts | 15 ++- .../startup/pty-pane-startup.ts | 3 +- ...nal-request-ipc-bridge-startup-env.test.ts | 94 +++++++++++++++++++ .../ipc-events/terminal-request-ipc-bridge.ts | 40 ++++---- .../new-tab/launch-agent-in-new-tab.test.ts | 2 +- .../src/store/terminals/terminal-actions.ts | 4 +- .../src/store/terminals/terminal-state.ts | 3 +- .../workspace-layout-oracle-markers.ts | 7 +- 9 files changed, 148 insertions(+), 25 deletions(-) create mode 100644 src/renderer/src/hooks/ipc-events/terminal-request-ipc-bridge-startup-env.test.ts diff --git a/src/renderer/src/components/terminal-pane/layout/split/pane-split-startup.ts b/src/renderer/src/components/terminal-pane/layout/split/pane-split-startup.ts index 58be4adc495..5eb7242b7c5 100644 --- a/src/renderer/src/components/terminal-pane/layout/split/pane-split-startup.ts +++ b/src/renderer/src/components/terminal-pane/layout/split/pane-split-startup.ts @@ -1,7 +1,10 @@ +import type { PtyPaneStartup } from '../../pty-connection/startup/pty-pane-startup' + export type SplitStartupPayload = { command: string; env?: Record } export function splitPaneWithOneShotStartup( - deps: { startup?: SplitStartupPayload | null }, + // Why the union: pane deps hold a full PtyPaneStartup, whose command is optional (#39852). + deps: { startup?: SplitStartupPayload | PtyPaneStartup | null }, startup: SplitStartupPayload, splitPane: () => TPane ): TPane { diff --git a/src/renderer/src/components/terminal-pane/pty-connection/pane-transport-options.ts b/src/renderer/src/components/terminal-pane/pty-connection/pane-transport-options.ts index d92fb7a9ffb..ecdc6a39827 100644 --- a/src/renderer/src/components/terminal-pane/pty-connection/pane-transport-options.ts +++ b/src/renderer/src/components/terminal-pane/pty-connection/pane-transport-options.ts @@ -19,10 +19,23 @@ export function paneHasStartupWork(session: ConnectPanePtySession): boolean { return paneHasKnownStartupWork(session) || mainStartsPane(session) } +/** Spawn env alone is not post-bind work; a plain paired terminal types as a plain shell does (#39852). */ +function startupPlansPostBindWork(startup: ConnectPanePtySession['paneStartup']): boolean { + return Boolean( + startup && + (startup.command !== undefined || + startup.agentLaunch !== undefined || + startup.freshAgentLaunch !== undefined || + startup.resumeProviderSession !== undefined || + startup.launchConfig !== undefined || + startup.draftPrompt !== undefined) + ) +} + function paneHasKnownStartupWork(session: ConnectPanePtySession): boolean { const state = useAppStore.getState() return Boolean( - session.paneStartup || + startupPlansPostBindWork(session.paneStartup) || session.getSleepingRecordForPane(state) || findColdRestoreResumeSource(session, state) ) diff --git a/src/renderer/src/components/terminal-pane/pty-connection/startup/pty-pane-startup.ts b/src/renderer/src/components/terminal-pane/pty-connection/startup/pty-pane-startup.ts index 89a89d8e853..cda3849bd5e 100644 --- a/src/renderer/src/components/terminal-pane/pty-connection/startup/pty-pane-startup.ts +++ b/src/renderer/src/components/terminal-pane/pty-connection/startup/pty-pane-startup.ts @@ -11,7 +11,8 @@ import type { SessionOptionValue } from '../../../../../../shared/native-chat/in import type { AgentPaneLaunchRequest } from '../../../../../../shared/agent-launch/contracts/agent-pane-launch-request' export type PtyPaneStartup = { - command: string + /** Absent when the startup carries only spawn env, e.g. a paired viewer's plain terminal (#39852). */ + command?: string /** Renderer-delivered startup input for callers that need xterm paste * semantics before the submit Enter. */ delivery?: 'terminal-paste' diff --git a/src/renderer/src/hooks/ipc-events/terminal-request-ipc-bridge-startup-env.test.ts b/src/renderer/src/hooks/ipc-events/terminal-request-ipc-bridge-startup-env.test.ts new file mode 100644 index 00000000000..e8f4f9e9ff8 --- /dev/null +++ b/src/renderer/src/hooks/ipc-events/terminal-request-ipc-bridge-startup-env.test.ts @@ -0,0 +1,94 @@ +import { beforeEach, describe, expect, it, vi } from 'vitest' +import { createTabsSliceMockApi } from '../../store/slices/tabs/tabs-slice-test-harness' +import { createTestStore } from '../../store/testing/app-store-test-fixture' + +vi.mock('sonner', () => ({ toast: { info: vi.fn(), success: vi.fn(), error: vi.fn() } })) +const testStore = vi.hoisted(() => { + const ref: { current: ReturnType | null } = { current: null } + return ref +}) +vi.mock('../../store', () => ({ + useAppStore: { + getState: () => { + if (!testStore.current) { + throw new Error('no test store') + } + return testStore.current.getState() + } + } +})) +vi.mock('@/lib/terminal-worktree-route', () => ({ + resolveTerminalWorktreeRoute: () => ({ runtimeEnvironmentId: null }) +})) +vi.mock('./terminal-command-state', async (importOriginal) => ({ + ...(await importOriginal()), + focusTerminalInitiatedTab: vi.fn() +})) + +createTabsSliceMockApi() + +const { registerTerminalRequestIpcBridge } = await import('./terminal-request-ipc-bridge') + +const WT = 'repo1::/tmp/feature' + +type CreateRequest = { + requestId: string + worktreeId: string + presentation?: 'focused' | 'background' + source?: 'runtime-session' + env?: Record +} + +let request!: (data: CreateRequest) => void +const replyTerminalCreate = vi.fn() +let store: ReturnType + +beforeEach(() => { + store = createTestStore() + testStore.current = store + replyTerminalCreate.mockClear() + vi.stubGlobal('window', { + ...globalThis.window, + api: { + ...globalThis.window?.api, + ui: { + ...globalThis.window?.api?.ui, + onRequestTerminalCreate: (callback: (data: CreateRequest) => void) => { + request = callback + return () => {} + }, + replyTerminalCreate + } + } + }) + registerTerminalRequestIpcBridge([]) + store.getState().setActiveWorktree(WT) +}) + +describe("a paired viewer's command-less terminal create", () => { + // Main sends only spawn env for a plain remote terminal; dropping it left the pane without the + // remote-viewer clipboard marker, so copied text never reached the viewer (#39852). + it('queues the env as startup so the spawned pane carries it', () => { + request({ + requestId: 'request-1', + worktreeId: WT, + source: 'runtime-session', + presentation: 'focused', + env: { ORCA_REMOTE_VIEWER_CLIPBOARD: '1', PI_CLIPBOARD_OSC52: '1' } + }) + + const tabId = replyTerminalCreate.mock.calls[0]?.[0]?.tabId + expect(tabId).toBeDefined() + expect(store.getState().pendingStartupByTabId[tabId!]).toEqual({ + env: { ORCA_REMOTE_VIEWER_CLIPBOARD: '1', PI_CLIPBOARD_OSC52: '1' } + }) + }) + + it('still queues nothing for a bare create without command or env', () => { + request({ requestId: 'request-2', worktreeId: WT, presentation: 'focused' }) + + const tabId = replyTerminalCreate.mock.calls[0]?.[0]?.tabId + expect(tabId).toBeDefined() + expect(store.getState().pendingStartupByTabId[tabId!]).toBeUndefined() + }) +}) diff --git a/src/renderer/src/hooks/ipc-events/terminal-request-ipc-bridge.ts b/src/renderer/src/hooks/ipc-events/terminal-request-ipc-bridge.ts index 48972a7de07..f6c72d1e923 100644 --- a/src/renderer/src/hooks/ipc-events/terminal-request-ipc-bridge.ts +++ b/src/renderer/src/hooks/ipc-events/terminal-request-ipc-bridge.ts @@ -62,23 +62,29 @@ export function registerTerminalRequestIpcBridge(unsubs: (() => void)[]): void { recordInteraction: false, ...(data.cwd ? { startupCwd: data.cwd } : {}) } - const pendingStartup = data.command - ? { - command: data.command, - ...(data.env ? { env: data.env } : {}), - ...(data.envToDelete ? { envToDelete: data.envToDelete } : {}), - ...(data.launchConfig ? { launchConfig: data.launchConfig } : {}), - ...(data.resumeProviderSession - ? { resumeProviderSession: data.resumeProviderSession } - : {}), - ...(data.launchToken ? { launchToken: data.launchToken } : {}), - ...(data.launchAgent ? { launchAgent: data.launchAgent } : {}), - ...(data.trustWorktreeId ? { trustWorktreeId: data.trustWorktreeId } : {}), - ...(data.startupCommandDelivery - ? { startupCommandDelivery: data.startupCommandDelivery } - : {}) - } - : undefined + // Why beyond command: a paired viewer's plain terminal carries only spawn env (#39852). + const pendingStartup = + data.command || + data.env || + data.envToDelete || + data.launchConfig || + data.resumeProviderSession + ? { + ...(data.command ? { command: data.command } : {}), + ...(data.env ? { env: data.env } : {}), + ...(data.envToDelete ? { envToDelete: data.envToDelete } : {}), + ...(data.launchConfig ? { launchConfig: data.launchConfig } : {}), + ...(data.resumeProviderSession + ? { resumeProviderSession: data.resumeProviderSession } + : {}), + ...(data.launchToken ? { launchToken: data.launchToken } : {}), + ...(data.launchAgent ? { launchAgent: data.launchAgent } : {}), + ...(data.trustWorktreeId ? { trustWorktreeId: data.trustWorktreeId } : {}), + ...(data.startupCommandDelivery + ? { startupCommandDelivery: data.startupCommandDelivery } + : {}) + } + : undefined const tab = store.createTab( worktreeId, data.targetGroupId, diff --git a/src/renderer/src/lib/agent-session/launch/new-tab/launch-agent-in-new-tab.test.ts b/src/renderer/src/lib/agent-session/launch/new-tab/launch-agent-in-new-tab.test.ts index a0160a1175a..1af066f2fb0 100644 --- a/src/renderer/src/lib/agent-session/launch/new-tab/launch-agent-in-new-tab.test.ts +++ b/src/renderer/src/lib/agent-session/launch/new-tab/launch-agent-in-new-tab.test.ts @@ -208,7 +208,7 @@ describe('launchAgentInNewTab', () => { } const startup = queuedStartups()[0] expect(startup).toBeDefined() - expect(tokenizeStartupCommand(startup.command, 'posix')).toMatchObject({ + expect(tokenizeStartupCommand(startup.command ?? '', 'posix')).toMatchObject({ ok: true, tokens: ['cx', '-c', 'model="true"', '-c', 'model_reasoning_effort=high'] }) diff --git a/src/renderer/src/store/terminals/terminal-actions.ts b/src/renderer/src/store/terminals/terminal-actions.ts index 111bec36f7e..44e45b91489 100644 --- a/src/renderer/src/store/terminals/terminal-actions.ts +++ b/src/renderer/src/store/terminals/terminal-actions.ts @@ -208,7 +208,7 @@ export type TerminalActions = { queueTabStartupCommand: ( tabId: string, startup: { - command: string + command?: string delivery?: 'terminal-paste' startupCommandDelivery?: StartupCommandDelivery env?: Record @@ -236,7 +236,7 @@ export type TerminalActions = { tabId: string, expected?: TerminalState['pendingStartupByTabId'][string] ) => { - command: string + command?: string delivery?: 'terminal-paste' startupCommandDelivery?: StartupCommandDelivery env?: Record diff --git a/src/renderer/src/store/terminals/terminal-state.ts b/src/renderer/src/store/terminals/terminal-state.ts index b453f3948b2..a8002026ded 100644 --- a/src/renderer/src/store/terminals/terminal-state.ts +++ b/src/renderer/src/store/terminals/terminal-state.ts @@ -64,7 +64,8 @@ export type TerminalState = { pendingStartupByTabId: Record< string, { - command: string + /** Absent when the startup carries only spawn env, e.g. a paired viewer's plain terminal (#39852). */ + command?: string delivery?: 'terminal-paste' startupCommandDelivery?: StartupCommandDelivery env?: Record diff --git a/tests/e2e/helpers/workspace-layout-oracle-markers.ts b/tests/e2e/helpers/workspace-layout-oracle-markers.ts index 390b08a3f00..b477220824f 100644 --- a/tests/e2e/helpers/workspace-layout-oracle-markers.ts +++ b/tests/e2e/helpers/workspace-layout-oracle-markers.ts @@ -19,7 +19,12 @@ export async function checkPaneMarkers( const mounted = new Set( await page.evaluate(() => Array.from(document.querySelectorAll('.pane[data-leaf-id][data-pty-id]')) - .filter((pane) => pane.getBoundingClientRect().width > 0) + // Why opacity: a hidden tab with a queued startup is laid out at opacity 0 so it spawns at real geometry; it has a size but is not shown. + .filter( + (pane) => + pane.getBoundingClientRect().width > 0 && + pane.checkVisibility({ opacityProperty: true, visibilityProperty: true }) + ) .map((pane) => pane.dataset.leafId ?? '') ) )