From 2b0936b3a68af560620c10d24c6b1bbed610f683 Mon Sep 17 00:00:00 2001 From: Dvitash Date: Mon, 6 Jul 2026 21:47:21 -0400 Subject: [PATCH 1/5] Fix OMP worktree title identity flicker --- src/renderer/src/lib/use-tab-agent.test.ts | 41 ++++++++++++++++++++++ src/renderer/src/lib/use-tab-agent.ts | 37 ++++++++++++++----- 2 files changed, 70 insertions(+), 8 deletions(-) diff --git a/src/renderer/src/lib/use-tab-agent.test.ts b/src/renderer/src/lib/use-tab-agent.test.ts index 9880fb9ce852..2c4b3ebcc02d 100644 --- a/src/renderer/src/lib/use-tab-agent.test.ts +++ b/src/renderer/src/lib/use-tab-agent.test.ts @@ -333,6 +333,47 @@ describe('resolveTabAgentFromSignals', () => { ).toBe('codex') }) + it('keeps OMP launch identity over Pi-compatible wrapper titles after activity', () => { + expect( + resolveTabAgentFromSignals({ + foreground: undefined, + hasObservedAgentSignal: true, + shellForegroundAfterAgentSignal: false, + isRemote: true, + title: '⠋ Pi', + hookAgent: null, + hasCompletedHook: false, + launchAgent: 'omp' + }) + ).toBe('omp') + + expect( + resolveTabAgentFromSignals({ + foreground: undefined, + hasObservedAgentSignal: true, + shellForegroundAfterAgentSignal: false, + isRemote: true, + title: '⠋ Pi', + hookAgent: 'pi', + hasCompletedHook: false, + launchAgent: 'omp' + }) + ).toBe('omp') + + expect( + resolveTabAgentFromSignals({ + foreground: undefined, + hasObservedAgentSignal: true, + shellForegroundAfterAgentSignal: false, + isRemote: true, + title: '⠋ OMP', + hookAgent: 'omp', + hasCompletedHook: false, + launchAgent: 'pi' + }) + ).toBe('pi') + }) + it('lets shell foreground clear the icon after an agent was observed running', () => { expect( resolveTabAgentFromSignals({ diff --git a/src/renderer/src/lib/use-tab-agent.ts b/src/renderer/src/lib/use-tab-agent.ts index 0b24f252eb49..d65cfb279d0d 100644 --- a/src/renderer/src/lib/use-tab-agent.ts +++ b/src/renderer/src/lib/use-tab-agent.ts @@ -12,6 +12,7 @@ import { resolveSiblingTabAgent } from './tab-agent' import { resolveExplicitTerminalTitleAgentType } from '../../../shared/terminal-title-agent-type' +import { resolveCompatibleAgentTypeForOwner } from '../../../shared/agent-title-owner' import type { TerminalTab, TuiAgent } from '../../../shared/types' export { resolveExplicitTerminalTitleAgentType as resolveTabAgentFromTitle } from '../../../shared/terminal-title-agent-type' @@ -37,6 +38,19 @@ function getTitleForegroundKey(title: string, launchAgent?: TuiAgent): string { return `unknown:${stableTitle}` } +/** + * Resolves wrapper-compatible signal identity against the launch owner. + */ +function resolveSignalAgentForLaunchOwner( + signalAgent: TuiAgent | null | undefined, + launchAgent: TuiAgent | null +): TuiAgent | null { + if (!signalAgent) { + return null + } + return (resolveCompatibleAgentTypeForOwner(signalAgent, launchAgent) ?? signalAgent) as TuiAgent +} + export function resolveTabAgentFromSignals(args: { foreground: TuiAgent | null | undefined hasObservedAgentSignal: boolean @@ -50,11 +64,13 @@ export function resolveTabAgentFromSignals(args: { launchAgent?: TuiAgent }): TuiAgent | null { const launchAgent = args.launchAgent ?? null - const explicitTitleAgent = resolveExplicitTerminalTitleAgentType(args.title) - // Why: when a pane is reused for a different agent, its launchAgent goes stale. - // A live title that explicitly names a *different* agent, once the pane has - // shown any activity, overrides that stale launch identity so the tab icon - // tracks what is actually running (codex launch reused for claude, etc.). + const explicitTitleAgent = resolveSignalAgentForLaunchOwner( + resolveExplicitTerminalTitleAgentType(args.title), + launchAgent + ) + // Why: explicit titles can override stale launches after activity, but + // Pi-compatible wrapper signals first resolve through the launch owner so + // OMP-created sessions do not repaint as Pi. const titleOverridesLaunch = launchAgent !== null && explicitTitleAgent !== null && @@ -69,9 +85,14 @@ export function resolveTabAgentFromSignals(args: { // Why: remote panes cannot cheaply prove shell foreground after hook exit, // so keep the last completed hook identity instead of flashing unknown. const completedHookAgent = - !args.isRemote && titleLooksShell && args.hasCompletedHook ? null : args.completedHookAgent - const focusedHookAgent = args.hookAgent ?? null - const fallbackHookAgent = args.siblingHookAgent ?? completedHookAgent ?? null + !args.isRemote && titleLooksShell && args.hasCompletedHook + ? null + : resolveSignalAgentForLaunchOwner(args.completedHookAgent, launchAgent) + const focusedHookAgent = resolveSignalAgentForLaunchOwner(args.hookAgent, launchAgent) + const fallbackHookAgent = resolveSignalAgentForLaunchOwner( + args.siblingHookAgent ?? completedHookAgent, + launchAgent + ) const localShellForegroundClearedLaunch = !args.isRemote && args.foreground === null && args.shellForegroundAfterAgentSignal const remoteCompletedHookAtShellTitle = args.isRemote && titleLooksShell && args.hasCompletedHook From 4099cdf628df109b8e71ba2448c23c44d89d77c3 Mon Sep 17 00:00:00 2001 From: Dvitash Date: Mon, 6 Jul 2026 21:54:06 -0400 Subject: [PATCH 2/5] Add headless fresh pairing server script --- .../serve-headless-fresh-profile-pairing.mjs | 201 ++++++++++++++++++ 1 file changed, 201 insertions(+) create mode 100755 config/scripts/serve-headless-fresh-profile-pairing.mjs diff --git a/config/scripts/serve-headless-fresh-profile-pairing.mjs b/config/scripts/serve-headless-fresh-profile-pairing.mjs new file mode 100755 index 000000000000..fba9b35b7693 --- /dev/null +++ b/config/scripts/serve-headless-fresh-profile-pairing.mjs @@ -0,0 +1,201 @@ +#!/usr/bin/env node + +import { spawn } from 'node:child_process' +import { existsSync, mkdirSync, mkdtempSync, rmSync } from 'node:fs' +import { tmpdir } from 'node:os' +import path from 'node:path' +import { createInterface } from 'node:readline' + +const scriptDir = import.meta.dirname +const repoRoot = path.resolve(scriptDir, '..', '..') +const orcaDevScript = path.join(scriptDir, 'orca-dev.mjs') +const fixedProfileDir = process.env.ORCA_HEADLESS_PAIRING_PROFILE_DIR +const parsed = parseArgs(process.argv.slice(2)) + +if (parsed.help) { + printHelp() + process.exit(0) +} + +if (parsed.serveArgs.includes('--no-pairing')) { + console.error('serve-headless-fresh-profile-pairing: --no-pairing cannot print a pairing code.') + process.exit(2) +} +if (parsed.serveArgs.includes('--recipe-json')) { + console.error( + 'serve-headless-fresh-profile-pairing: --recipe-json detaches the server and cannot use an ephemeral profile safely.' + ) + process.exit(2) +} + +const profileDir = + fixedProfileDir ?? mkdtempSync(path.join(tmpdir(), 'orca-headless-pairing-profile-')) +const ownsProfileDir = !fixedProfileDir +mkdirSync(profileDir, { recursive: true }) + +let cleanedUp = false +let child = null +let sawPairingUrl = false + +console.error(`[headless-pairing] userData=${profileDir}`) +console.error( + `[headless-pairing] starting: orca-dev serve --json${formatForwardedArgs(parsed.serveArgs)}` +) + +child = spawn(process.execPath, [orcaDevScript, 'serve', '--json', ...parsed.serveArgs], { + cwd: repoRoot, + env: { + ...process.env, + ORCA_DEV_USER_DATA_PATH: profileDir + }, + stdio: ['inherit', 'pipe', 'inherit'] +}) + +const stdoutLines = createInterface({ input: child.stdout }) +stdoutLines.on('line', (line) => { + if (!printReadyLine(line)) { + console.log(line) + } +}) + +child.once('error', (error) => { + console.error(`[headless-pairing] failed to start server: ${error.message}`) + cleanupProfile() + process.exitCode = 1 +}) + +child.once('exit', (code, signal) => { + stdoutLines.close() + if (!sawPairingUrl && code !== 0) { + console.error('[headless-pairing] server exited before printing a pairing URL.') + } + cleanupProfile() + if (typeof code === 'number') { + process.exitCode = code + return + } + process.exitCode = signal ? 1 : 0 +}) + +process.once('SIGINT', () => stopChild('SIGINT')) +process.once('SIGTERM', () => stopChild('SIGTERM')) + +/** + * Parses wrapper flags and forwards everything else to `orca serve`. + */ +function parseArgs(args) { + const serveArgs = [] + let keepProfile = false + let help = false + for (const arg of args) { + if (arg === '--keep') { + keepProfile = true + continue + } + if (arg === '-h' || arg === '--help') { + help = true + continue + } + serveArgs.push(arg) + } + return { help, keepProfile, serveArgs } +} + +/** + * Prints script usage without touching the dev profile or starting the server. + */ +function printHelp() { + console.log(`Usage: node config/scripts/serve-headless-fresh-profile-pairing.mjs [--keep] [orca serve flags] + +Starts \`orca-dev serve --json\` with a fresh isolated userData profile and prints the pairing URL. + +Wrapper flags: + --keep Keep the fresh profile after the server exits. + -h, --help Show this help. + +Forwarded examples: + node config/scripts/serve-headless-fresh-profile-pairing.mjs --port 6768 + node config/scripts/serve-headless-fresh-profile-pairing.mjs --pairing-address 100.64.1.20 + node config/scripts/serve-headless-fresh-profile-pairing.mjs --mobile-pairing + +Environment: + ORCA_HEADLESS_PAIRING_PROFILE_DIR=/path/to/profile Use a fixed profile directory. +`) +} + +/** + * Formats forwarded arguments for the startup log. + */ +function formatForwardedArgs(args) { + if (args.length === 0) { + return '' + } + return ` ${args.map(formatShellArg).join(' ')}` +} + +/** + * Quotes a shell-ish argument for display only. + */ +function formatShellArg(value) { + if (/^[\w./:=@+-]+$/.test(value)) { + return value + } + return JSON.stringify(value) +} + +/** + * Rewrites the CLI JSON readiness line into the pairing URL testers need. + */ +function printReadyLine(line) { + let payload + try { + payload = JSON.parse(line) + } catch { + return false + } + if (!payload || payload.type !== 'orca_server_ready') { + return false + } + console.log(`Orca server ready: ${payload.endpoint ?? 'websocket unavailable'}`) + if (payload.pairing?.webClientUrl) { + console.log(`Web client URL: ${payload.pairing.webClientUrl}`) + } + if (payload.pairing?.url) { + sawPairingUrl = true + console.log(`Pairing URL: ${payload.pairing.url}`) + console.error('[headless-pairing] server is running; press Ctrl+C to stop.') + return true + } + console.log('Pairing URL: unavailable') + return true +} + +/** + * Stops the foreground server so its normal shutdown path can cleanly persist state. + */ +function stopChild(signal) { + if (!child || child.killed) { + return + } + child.kill(signal) +} + +/** + * Removes only the ephemeral profile directory this script created. + */ +function cleanupProfile() { + if (cleanedUp) { + return + } + cleanedUp = true + if (parsed.keepProfile || !ownsProfileDir) { + console.error(`[headless-pairing] kept ${profileDir}`) + return + } + if (!existsSync(profileDir) || !profileDir.includes('orca-headless-pairing-profile-')) { + console.error(`[headless-pairing] skipped cleanup for unexpected profile path: ${profileDir}`) + return + } + rmSync(profileDir, { recursive: true, force: true }) + console.error(`[headless-pairing] removed ${profileDir}`) +} From 848b1a7923410fdb7e200c09e828147202ba23a6 Mon Sep 17 00:00:00 2001 From: Dvitash Date: Mon, 6 Jul 2026 22:06:22 -0400 Subject: [PATCH 3/5] Harden fresh pairing server script startup --- .../serve-headless-fresh-profile-pairing.mjs | 53 ++++++++++++++++--- 1 file changed, 47 insertions(+), 6 deletions(-) diff --git a/config/scripts/serve-headless-fresh-profile-pairing.mjs b/config/scripts/serve-headless-fresh-profile-pairing.mjs index fba9b35b7693..7f922f47a0f2 100755 --- a/config/scripts/serve-headless-fresh-profile-pairing.mjs +++ b/config/scripts/serve-headless-fresh-profile-pairing.mjs @@ -1,6 +1,6 @@ #!/usr/bin/env node -import { spawn } from 'node:child_process' +import { spawn, spawnSync } from 'node:child_process' import { existsSync, mkdirSync, mkdtempSync, rmSync } from 'node:fs' import { tmpdir } from 'node:os' import path from 'node:path' @@ -28,6 +28,8 @@ if (parsed.serveArgs.includes('--recipe-json')) { process.exit(2) } +ensureElectronRuntime() + const profileDir = fixedProfileDir ?? mkdtempSync(path.join(tmpdir(), 'orca-headless-pairing-profile-')) const ownsProfileDir = !fixedProfileDir @@ -36,6 +38,15 @@ mkdirSync(profileDir, { recursive: true }) let cleanedUp = false let child = null let sawPairingUrl = false +// Why: temp dev worktrees do not have a root-owned chrome-sandbox; this script +// is only for local headless testing, not packaged production. +const childEnv = { + ...process.env, + ORCA_DEV_USER_DATA_PATH: profileDir, + ...(process.platform === 'linux' + ? { ELECTRON_DISABLE_SANDBOX: process.env.ELECTRON_DISABLE_SANDBOX ?? '1' } + : {}) +} console.error(`[headless-pairing] userData=${profileDir}`) console.error( @@ -44,10 +55,8 @@ console.error( child = spawn(process.execPath, [orcaDevScript, 'serve', '--json', ...parsed.serveArgs], { cwd: repoRoot, - env: { - ...process.env, - ORCA_DEV_USER_DATA_PATH: profileDir - }, + detached: process.platform !== 'win32', + env: childEnv, stdio: ['inherit', 'pipe', 'inherit'] }) @@ -107,7 +116,7 @@ function parseArgs(args) { function printHelp() { console.log(`Usage: node config/scripts/serve-headless-fresh-profile-pairing.mjs [--keep] [orca serve flags] -Starts \`orca-dev serve --json\` with a fresh isolated userData profile and prints the pairing URL. +Starts orca-dev serve --json with a fresh isolated userData profile, ensures Electron's dev runtime is usable, and prints the pairing URL. Wrapper flags: --keep Keep the fresh profile after the server exits. @@ -143,6 +152,28 @@ function formatShellArg(value) { return JSON.stringify(value) } +/** + * Runs the same Electron runtime preflight used by dev/start package scripts. + */ +function ensureElectronRuntime() { + console.error('[headless-pairing] ensuring Electron runtime...') + const result = spawnSync( + process.execPath, + ['config/scripts/ensure-native-runtime.mjs', '--runtime=electron'], + { + cwd: repoRoot, + stdio: 'inherit' + } + ) + if (result.error) { + console.error(`[headless-pairing] Electron runtime preflight failed: ${result.error.message}`) + process.exit(1) + } + if (result.status !== 0) { + process.exit(result.status ?? 1) + } +} + /** * Rewrites the CLI JSON readiness line into the pairing URL testers need. */ @@ -177,6 +208,16 @@ function stopChild(signal) { if (!child || child.killed) { return } + if (process.platform !== 'win32' && child.pid) { + // Why: orca-dev synchronously owns the CLI child, which owns Electron; kill + // the spawned process group so programmatic shutdown does not orphan serve. + try { + process.kill(-child.pid, signal) + return + } catch { + // Fall back to the direct child below if the process group already exited. + } + } child.kill(signal) } From de0b1a17fec953c1756bcc1f23cbf6288727e0de Mon Sep 17 00:00:00 2001 From: Dvitash Date: Mon, 6 Jul 2026 22:10:35 -0400 Subject: [PATCH 4/5] Default fresh pairing server to reachable address --- .../serve-headless-fresh-profile-pairing.mjs | 73 +++++++++++++++++-- 1 file changed, 67 insertions(+), 6 deletions(-) diff --git a/config/scripts/serve-headless-fresh-profile-pairing.mjs b/config/scripts/serve-headless-fresh-profile-pairing.mjs index 7f922f47a0f2..b2cb180517bc 100755 --- a/config/scripts/serve-headless-fresh-profile-pairing.mjs +++ b/config/scripts/serve-headless-fresh-profile-pairing.mjs @@ -2,7 +2,7 @@ import { spawn, spawnSync } from 'node:child_process' import { existsSync, mkdirSync, mkdtempSync, rmSync } from 'node:fs' -import { tmpdir } from 'node:os' +import { hostname, tmpdir } from 'node:os' import path from 'node:path' import { createInterface } from 'node:readline' @@ -28,6 +28,8 @@ if (parsed.serveArgs.includes('--recipe-json')) { process.exit(2) } +const serveArgs = withDefaultPairingAddress(parsed.serveArgs) + ensureElectronRuntime() const profileDir = @@ -49,11 +51,9 @@ const childEnv = { } console.error(`[headless-pairing] userData=${profileDir}`) -console.error( - `[headless-pairing] starting: orca-dev serve --json${formatForwardedArgs(parsed.serveArgs)}` -) +console.error(`[headless-pairing] starting: orca-dev serve --json${formatForwardedArgs(serveArgs)}`) -child = spawn(process.execPath, [orcaDevScript, 'serve', '--json', ...parsed.serveArgs], { +child = spawn(process.execPath, [orcaDevScript, 'serve', '--json', ...serveArgs], { cwd: repoRoot, detached: process.platform !== 'win32', env: childEnv, @@ -128,10 +128,68 @@ Forwarded examples: node config/scripts/serve-headless-fresh-profile-pairing.mjs --mobile-pairing Environment: - ORCA_HEADLESS_PAIRING_PROFILE_DIR=/path/to/profile Use a fixed profile directory. + ORCA_HEADLESS_PAIRING_ADDRESS= Override the auto pairing address. + ORCA_HEADLESS_PAIRING_PROFILE_DIR=/path/to/profile Use a fixed profile directory. `) } +/** + * Adds a reachable pairing address unless the caller provided one explicitly. + */ +function withDefaultPairingAddress(args) { + if (hasPairingAddress(args)) { + return args + } + const address = resolveDefaultPairingAddress() + if (!address) { + return args + } + console.error(`[headless-pairing] pairingAddress=${address} (auto)`) + return [...args, '--pairing-address', address] +} + +/** + * Checks both supported CLI forms for an explicit pairing address. + */ +function hasPairingAddress(args) { + return args.some((arg) => arg === '--pairing-address' || arg.startsWith('--pairing-address=')) +} + +/** + * Prefers an override, then Tailscale, then the OS hostname over loopback. + */ +function resolveDefaultPairingAddress() { + const configured = process.env.ORCA_HEADLESS_PAIRING_ADDRESS?.trim() + if (configured) { + return configured + } + const tailscaleAddress = readTailscaleAddress() + if (tailscaleAddress) { + return tailscaleAddress + } + const host = hostname().trim() + return host && host !== 'localhost' ? host : null +} + +/** + * Reads the first Tailscale IPv4 address when Tailscale is installed. + */ +function readTailscaleAddress() { + const result = spawnSync('tailscale', ['ip', '-4'], { + encoding: 'utf8', + stdio: ['ignore', 'pipe', 'ignore'] + }) + if (result.error || result.status !== 0) { + return null + } + return ( + result.stdout + .split(/\r?\n/) + .map((line) => line.trim()) + .find(Boolean) ?? null + ) +} + /** * Formats forwarded arguments for the startup log. */ @@ -188,6 +246,9 @@ function printReadyLine(line) { return false } console.log(`Orca server ready: ${payload.endpoint ?? 'websocket unavailable'}`) + if (payload.pairing?.endpoint) { + console.log(`Pairing endpoint: ${payload.pairing.endpoint}`) + } if (payload.pairing?.webClientUrl) { console.log(`Web client URL: ${payload.pairing.webClientUrl}`) } From e40573439182ce4c06ce8cc7137c34c558477615 Mon Sep 17 00:00:00 2001 From: Jinwoo-H Date: Tue, 7 Jul 2026 16:46:43 -0400 Subject: [PATCH 5/5] review: harden OMP tab identity validation Co-authored-by: Orca --- .../serve-headless-fresh-profile-pairing.mjs | 59 +++++++++++++------ src/renderer/src/lib/use-tab-agent.test.ts | 43 ++++++++++++++ 2 files changed, 84 insertions(+), 18 deletions(-) diff --git a/config/scripts/serve-headless-fresh-profile-pairing.mjs b/config/scripts/serve-headless-fresh-profile-pairing.mjs index b2cb180517bc..6d4f7ac8656e 100755 --- a/config/scripts/serve-headless-fresh-profile-pairing.mjs +++ b/config/scripts/serve-headless-fresh-profile-pairing.mjs @@ -9,6 +9,7 @@ import { createInterface } from 'node:readline' const scriptDir = import.meta.dirname const repoRoot = path.resolve(scriptDir, '..', '..') const orcaDevScript = path.join(scriptDir, 'orca-dev.mjs') +const ensureNativeRuntimeScript = path.join(scriptDir, 'ensure-native-runtime.mjs') const fixedProfileDir = process.env.ORCA_HEADLESS_PAIRING_PROFILE_DIR const parsed = parseArgs(process.argv.slice(2)) @@ -17,11 +18,11 @@ if (parsed.help) { process.exit(0) } -if (parsed.serveArgs.includes('--no-pairing')) { +if (hasForwardedServeFlag(parsed.serveArgs, 'no-pairing')) { console.error('serve-headless-fresh-profile-pairing: --no-pairing cannot print a pairing code.') process.exit(2) } -if (parsed.serveArgs.includes('--recipe-json')) { +if (hasForwardedServeFlag(parsed.serveArgs, 'recipe-json')) { console.error( 'serve-headless-fresh-profile-pairing: --recipe-json detaches the server and cannot use an ephemeral profile safely.' ) @@ -40,6 +41,7 @@ mkdirSync(profileDir, { recursive: true }) let cleanedUp = false let child = null let sawPairingUrl = false +let stopAttempts = 0 // Why: temp dev worktrees do not have a root-owned chrome-sandbox; this script // is only for local headless testing, not packaged production. const childEnv = { @@ -86,8 +88,8 @@ child.once('exit', (code, signal) => { process.exitCode = signal ? 1 : 0 }) -process.once('SIGINT', () => stopChild('SIGINT')) -process.once('SIGTERM', () => stopChild('SIGTERM')) +process.on('SIGINT', () => stopChild('SIGINT')) +process.on('SIGTERM', () => stopChild('SIGTERM')) /** * Parses wrapper flags and forwards everything else to `orca serve`. @@ -152,7 +154,15 @@ function withDefaultPairingAddress(args) { * Checks both supported CLI forms for an explicit pairing address. */ function hasPairingAddress(args) { - return args.some((arg) => arg === '--pairing-address' || arg.startsWith('--pairing-address=')) + return hasForwardedServeFlag(args, 'pairing-address') +} + +/** + * Checks both exact and `--flag=value` forms for a forwarded serve flag. + */ +function hasForwardedServeFlag(args, name) { + const flag = `--${name}` + return args.some((arg) => arg === flag || arg.startsWith(`${flag}=`)) } /** @@ -215,14 +225,10 @@ function formatShellArg(value) { */ function ensureElectronRuntime() { console.error('[headless-pairing] ensuring Electron runtime...') - const result = spawnSync( - process.execPath, - ['config/scripts/ensure-native-runtime.mjs', '--runtime=electron'], - { - cwd: repoRoot, - stdio: 'inherit' - } - ) + const result = spawnSync(process.execPath, [ensureNativeRuntimeScript, '--runtime=electron'], { + cwd: repoRoot, + stdio: 'inherit' + }) if (result.error) { console.error(`[headless-pairing] Electron runtime preflight failed: ${result.error.message}`) process.exit(1) @@ -263,23 +269,35 @@ function printReadyLine(line) { } /** - * Stops the foreground server so its normal shutdown path can cleanly persist state. + * Stops the foreground server; repeated signals force its process tree down. */ function stopChild(signal) { if (!child || child.killed) { return } + stopAttempts += 1 + const targetSignal = stopAttempts > 1 ? 'SIGKILL' : signal + if (process.platform === 'win32' && child.pid) { + // Why: child.kill() only targets orca-dev on Windows; taskkill walks the + // CLI/Electron descendants so the fresh profile is not left locked. + const killer = spawn('taskkill', ['/pid', String(child.pid), '/t', '/f'], { + stdio: 'ignore', + windowsHide: true + }) + killer.on('error', () => child?.kill(targetSignal)) + return + } if (process.platform !== 'win32' && child.pid) { // Why: orca-dev synchronously owns the CLI child, which owns Electron; kill // the spawned process group so programmatic shutdown does not orphan serve. try { - process.kill(-child.pid, signal) + process.kill(-child.pid, targetSignal) return } catch { // Fall back to the direct child below if the process group already exited. } } - child.kill(signal) + child.kill(targetSignal) } /** @@ -298,6 +316,11 @@ function cleanupProfile() { console.error(`[headless-pairing] skipped cleanup for unexpected profile path: ${profileDir}`) return } - rmSync(profileDir, { recursive: true, force: true }) - console.error(`[headless-pairing] removed ${profileDir}`) + try { + rmSync(profileDir, { recursive: true, force: true, maxRetries: 5, retryDelay: 200 }) + console.error(`[headless-pairing] removed ${profileDir}`) + } catch (error) { + const message = error instanceof Error ? error.message : String(error) + console.error(`[headless-pairing] skipped cleanup for ${profileDir}: ${message}`) + } } diff --git a/src/renderer/src/lib/use-tab-agent.test.ts b/src/renderer/src/lib/use-tab-agent.test.ts index 2d6029c20492..d4d6031b2095 100644 --- a/src/renderer/src/lib/use-tab-agent.test.ts +++ b/src/renderer/src/lib/use-tab-agent.test.ts @@ -292,6 +292,27 @@ describe('resolveTabAgentFromSignals', () => { }) ).toBe('omp') + expect( + resolveTabAgentFromSignals({ + hasObservedAgentSignal: true, + isRemote: false, + title: '⠋ Pi', + hookAgent: 'pi', + launchAgent: 'omp' + }) + ).toBe('omp') + + expect( + resolveTabAgentFromSignals({ + hasObservedAgentSignal: true, + isRemote: true, + title: 'Terminal 1', + hookAgent: null, + siblingHookAgent: 'pi', + launchAgent: 'omp' + }) + ).toBe('omp') + expect( resolveTabAgentFromSignals({ hasObservedAgentSignal: true, @@ -301,6 +322,28 @@ describe('resolveTabAgentFromSignals', () => { launchAgent: 'pi' }) ).toBe('pi') + + expect( + resolveTabAgentFromSignals({ + hasObservedAgentSignal: true, + isRemote: true, + title: 'zsh', + hookAgent: null, + focusedCompletedHookAgent: 'pi', + launchAgent: 'omp' + }) + ).toBe('omp') + + expect( + resolveTabAgentFromSignals({ + hasObservedAgentSignal: true, + isRemote: false, + title: 'zsh', + hookAgent: null, + siblingCompletedHookAgent: 'pi', + launchAgent: 'omp' + }) + ).toBe('omp') }) it('prefers explicit hook identity over a conflicting title mention', () => {