Files
orca/src/main/ipc/pty.ts
T
Jinjing 90d0fae8f5 feat: add explicit worktree setup flow (#286)
* feat: add explicit worktree setup flow

- Centralize worktree activation setup flow

- fix: render orca.yaml file contents in settings repo pane instead of tags when using config file

- fix: command preview title for yaml configs

- fix: update UI copy and styling to better reflect yaml setup hooks

- fix: run setup hook in background if CLI worktree is created while GUI is closed

- fix: prevent duplicate terminal tab creation when creating worktrees from the UI

- fix: ensure CLI worktree create returns correct payload type and hooks up UI setup flow

- Squashed commits

- fix minor regression

- fix: close worktree context menu when deleting

- fix: keep delete dialog open while archive hook runs and support inline yaml scripts

- chore: fix lint, typecheck and react state mutations during render for setup script feature

* fix(lint): split worktrees.test.ts to stay under max-lines limit

Extract Windows path-handling tests into worktrees-windows.test.ts
to bring the original file under the 300-line oxlint max-lines rule.

* fix(test): set up path mocks for setup launch payload test

The computeWorktreePathMock and ensurePathWithinWorkspaceMock were
cleared by afterEach but not re-established in the test, causing
areWorktreePathsEqual to receive undefined and crash.
2026-04-03 23:44:23 -07:00

172 lines
4.9 KiB
TypeScript

import { type BrowserWindow, ipcMain } from 'electron'
import * as pty from 'node-pty'
import type { OrcaRuntimeService } from '../runtime/orca-runtime'
let ptyCounter = 0
const ptyProcesses = new Map<string, pty.IPty>()
// Track which "page load generation" each PTY belongs to.
// When the renderer reloads, we only kill PTYs from previous generations,
// not ones spawned during the current page load. This prevents a race
// condition where did-finish-load fires after PTYs have already been
// created by the new page, killing them and leaving blank terminals.
let loadGeneration = 0
const ptyLoadGeneration = new Map<string, number>()
export function registerPtyHandlers(mainWindow: BrowserWindow, runtime?: OrcaRuntimeService): void {
// Remove any previously registered handlers so we can re-register them
// (e.g. when macOS re-activates the app and creates a new window).
ipcMain.removeHandler('pty:spawn')
ipcMain.removeHandler('pty:resize')
ipcMain.removeHandler('pty:kill')
ipcMain.removeAllListeners('pty:write')
// Kill orphaned PTY processes from previous page loads when the renderer reloads.
// PTYs tagged with the current loadGeneration were spawned during THIS page load
// and must be preserved — only kill PTYs from earlier generations.
mainWindow.webContents.on('did-finish-load', () => {
for (const [id, proc] of ptyProcesses) {
const gen = ptyLoadGeneration.get(id) ?? -1
if (gen < loadGeneration) {
try {
proc.kill()
} catch {
// Process may already be dead
}
ptyProcesses.delete(id)
ptyLoadGeneration.delete(id)
}
}
// Advance generation for the next page load
loadGeneration++
})
runtime?.setPtyController({
write: (ptyId, data) => {
const proc = ptyProcesses.get(ptyId)
if (!proc) {
return false
}
proc.write(data)
return true
},
kill: (ptyId) => {
const proc = ptyProcesses.get(ptyId)
if (!proc) {
return false
}
try {
proc.kill()
} catch {
return false
}
ptyProcesses.delete(ptyId)
ptyLoadGeneration.delete(ptyId)
runtime?.onPtyExit(ptyId, -1)
return true
}
})
ipcMain.handle(
'pty:spawn',
(_event, args: { cols: number; rows: number; cwd?: string; env?: Record<string, string> }) => {
const id = String(++ptyCounter)
let shellPath: string
let shellArgs: string[]
if (process.platform === 'win32') {
shellPath = process.env.COMSPEC || 'powershell.exe'
shellArgs = []
} else {
shellPath = process.env.SHELL || '/bin/zsh'
shellArgs = ['-l']
}
const defaultCwd =
process.platform === 'win32'
? process.env.USERPROFILE || process.env.HOMEPATH || 'C:\\'
: process.env.HOME || '/'
const ptyProcess = pty.spawn(shellPath, shellArgs, {
name: 'xterm-256color',
cols: args.cols,
rows: args.rows,
cwd: args.cwd || defaultCwd,
env: {
...process.env,
...args.env,
TERM: 'xterm-256color',
COLORTERM: 'truecolor',
TERM_PROGRAM: 'Orca',
FORCE_HYPERLINK: '1'
} as Record<string, string>
})
ptyProcesses.set(id, ptyProcess)
ptyLoadGeneration.set(id, loadGeneration)
runtime?.onPtySpawned(id)
ptyProcess.onData((data) => {
runtime?.onPtyData(id, data, Date.now())
if (!mainWindow.isDestroyed()) {
mainWindow.webContents.send('pty:data', { id, data })
}
})
ptyProcess.onExit(({ exitCode }) => {
ptyProcesses.delete(id)
ptyLoadGeneration.delete(id)
runtime?.onPtyExit(id, exitCode)
if (!mainWindow.isDestroyed()) {
mainWindow.webContents.send('pty:exit', { id, code: exitCode })
}
})
return { id }
}
)
ipcMain.on('pty:write', (_event, args: { id: string; data: string }) => {
const proc = ptyProcesses.get(args.id)
if (proc) {
proc.write(args.data)
}
})
ipcMain.handle('pty:resize', (_event, args: { id: string; cols: number; rows: number }) => {
const proc = ptyProcesses.get(args.id)
if (proc) {
proc.resize(args.cols, args.rows)
}
})
ipcMain.handle('pty:kill', (_event, args: { id: string }) => {
const proc = ptyProcesses.get(args.id)
if (proc) {
try {
proc.kill()
} catch {
// Process may already be dead
}
ptyProcesses.delete(args.id)
ptyLoadGeneration.delete(args.id)
runtime?.onPtyExit(args.id, -1)
}
})
}
/**
* Kill all PTY processes. Call on app quit.
*/
export function killAllPty(): void {
for (const [id, proc] of ptyProcesses) {
try {
proc.kill()
} catch {
// Process may already be dead
}
ptyProcesses.delete(id)
ptyLoadGeneration.delete(id)
}
}