From 4b7376d2293745be33a5b41229f5c53fa0952847 Mon Sep 17 00:00:00 2001 From: xucongwei Date: Fri, 10 Jul 2026 16:14:50 +0800 Subject: [PATCH 1/2] Hide console window for Windows agent foreground-process scan MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Agent foreground-process inspection re-forks powershell.exe (or the wmic fallback) to detect which agent runs in each terminal. Both spawns omitted windowsHide, so on Windows each fork popped a fresh conhost console window that flashed and stole keyboard focus from the foreground app — including Orca's own terminal — recurring roughly once every few tens of seconds while an agent session was open (and more often under continuous agent output). Add windowsHide: true to both probes (matching the codebase-wide convention) plus a regression test asserting the spawn options. Co-Authored-By: Claude Opus 4.8 (1M context) --- .../windows-foreground-process-rows.test.ts | 87 +++++++++++++++++++ .../windows-foreground-process-rows.ts | 14 ++- 2 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 src/main/providers/windows-foreground-process-rows.test.ts diff --git a/src/main/providers/windows-foreground-process-rows.test.ts b/src/main/providers/windows-foreground-process-rows.test.ts new file mode 100644 index 000000000000..3adf5ac05ee9 --- /dev/null +++ b/src/main/providers/windows-foreground-process-rows.test.ts @@ -0,0 +1,87 @@ +// Regression guard: the Windows agent foreground-process scan re-forks +// powershell.exe (or the wmic fallback) on a ~1s/pane cadence. Electron's main +// process has no console, so a spawn without windowsHide pops a fresh conhost +// window per scan that flashes and steals keyboard focus from the foreground app +// (including Orca's own terminal). Both probes MUST pass windowsHide: true. +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' + +const { execFileMock } = vi.hoisted(() => ({ execFileMock: vi.fn() })) + +vi.mock('child_process', () => ({ execFile: execFileMock })) + +import { + queryWindowsProcessDescendants, + resetWindowsProcessRowsSnapshotForTests +} from './windows-foreground-process-rows' + +type ExecFileCallback = (err: unknown, result: { stdout: string; stderr: string }) => void +type ExecFileCall = [string, string[], Record, ExecFileCallback] + +const POWERSHELL_ROWS_JSON = JSON.stringify([ + { + ProcessId: 200, + ParentProcessId: 100, + Name: 'node.exe', + CommandLine: 'node C:/Users/dev/AppData/codex/bin/codex.js', + ExecutablePath: 'C:/Program Files/nodejs/node.exe' + } +]) + +const WMIC_ROWS_VALUE = + 'CommandLine=node C:/Users/dev/AppData/codex/bin/codex.js\n' + + 'ExecutablePath=C:/Program Files/nodejs/node.exe\n' + + 'Name=node.exe\n' + + 'ParentProcessId=100\n' + + 'ProcessId=200\n' + +/** Returns the options object passed to the mocked execFile for a given command. */ +function optionsForCommand(command: string): Record | undefined { + const call = execFileMock.mock.calls.find( + (args) => (args as ExecFileCall)[0] === command + ) as ExecFileCall | undefined + return call?.[2] +} + +describe('windows foreground process rows spawn options', () => { + let platform: PropertyDescriptor | undefined + + beforeEach(() => { + execFileMock.mockReset() + resetWindowsProcessRowsSnapshotForTests() + platform = Object.getOwnPropertyDescriptor(process, 'platform') + Object.defineProperty(process, 'platform', { configurable: true, value: 'win32' }) + }) + + afterEach(() => { + if (platform) { + Object.defineProperty(process, 'platform', platform) + } + }) + + it('hides the console window for the powershell process-table scan', async () => { + execFileMock.mockImplementation((_cmd: string, _args, _opts, cb: ExecFileCallback) => { + cb(null, { stdout: POWERSHELL_ROWS_JSON, stderr: '' }) + }) + + const candidates = await queryWindowsProcessDescendants(100) + + expect(candidates?.[0]?.pid).toBe(200) + expect(optionsForCommand('powershell.exe')).toMatchObject({ windowsHide: true }) + }) + + it('hides the console window for the wmic fallback scan', async () => { + execFileMock.mockImplementation((cmd: string, _args, _opts, cb: ExecFileCallback) => { + // Force the powershell probe to miss so the wmic fallback runs. + if (cmd === 'powershell.exe') { + cb(new Error('powershell unavailable'), { stdout: '', stderr: '' }) + return + } + cb(null, { stdout: WMIC_ROWS_VALUE, stderr: '' }) + }) + + const candidates = await queryWindowsProcessDescendants(100) + + expect(candidates?.[0]?.pid).toBe(200) + expect(optionsForCommand('wmic')).toMatchObject({ windowsHide: true }) + }) +}) diff --git a/src/main/providers/windows-foreground-process-rows.ts b/src/main/providers/windows-foreground-process-rows.ts index d26067122a58..90f61ce08041 100644 --- a/src/main/providers/windows-foreground-process-rows.ts +++ b/src/main/providers/windows-foreground-process-rows.ts @@ -200,6 +200,7 @@ function collectDescendants( return descendants } +/** Runs the PowerShell/CIM whole-process-table scan; returns null when unavailable. */ async function queryWindowsProcessesWithPowerShell(): Promise { try { const { stdout } = await execFileAsync( @@ -208,7 +209,12 @@ async function queryWindowsProcessesWithPowerShell(): Promise { try { const { stdout } = await execFileAsync( @@ -231,7 +238,10 @@ async function queryWindowsProcessesWithWmic(): Promise Date: Mon, 13 Jul 2026 01:38:19 -0400 Subject: [PATCH 2/2] test(windows): include scan root in foreground fixtures --- .../windows-foreground-process-rows.test.ts | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/main/providers/windows-foreground-process-rows.test.ts b/src/main/providers/windows-foreground-process-rows.test.ts index 3adf5ac05ee9..61482224e503 100644 --- a/src/main/providers/windows-foreground-process-rows.test.ts +++ b/src/main/providers/windows-foreground-process-rows.test.ts @@ -18,6 +18,13 @@ type ExecFileCallback = (err: unknown, result: { stdout: string; stderr: string type ExecFileCall = [string, string[], Record, ExecFileCallback] const POWERSHELL_ROWS_JSON = JSON.stringify([ + { + ProcessId: 100, + ParentProcessId: 50, + Name: 'powershell.exe', + CommandLine: 'powershell.exe', + ExecutablePath: 'C:/Windows/System32/WindowsPowerShell/v1.0/powershell.exe' + }, { ProcessId: 200, ParentProcessId: 100, @@ -28,6 +35,11 @@ const POWERSHELL_ROWS_JSON = JSON.stringify([ ]) const WMIC_ROWS_VALUE = + 'CommandLine=powershell.exe\n' + + 'ExecutablePath=C:/Windows/System32/WindowsPowerShell/v1.0/powershell.exe\n' + + 'Name=powershell.exe\n' + + 'ParentProcessId=50\n' + + 'ProcessId=100\n\n' + 'CommandLine=node C:/Users/dev/AppData/codex/bin/codex.js\n' + 'ExecutablePath=C:/Program Files/nodejs/node.exe\n' + 'Name=node.exe\n' + @@ -36,9 +48,9 @@ const WMIC_ROWS_VALUE = /** Returns the options object passed to the mocked execFile for a given command. */ function optionsForCommand(command: string): Record | undefined { - const call = execFileMock.mock.calls.find( - (args) => (args as ExecFileCall)[0] === command - ) as ExecFileCall | undefined + const call = execFileMock.mock.calls.find((args) => (args as ExecFileCall)[0] === command) as + | ExecFileCall + | undefined return call?.[2] }