Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ out/
*.log
.DS_Store
dist-build/
.kspec/
45 changes: 39 additions & 6 deletions src/main/app/ipc-handlers/pty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,33 @@ export function registerPtyHandlers(
})

ipcMain.handle('pty:set-backend', async (_, { id: oldId, backend: newBackend }: { id: string; backend: 'claude' | 'gemini' | 'codex' | 'opencode' | 'aider' }) => {
const process = ptyManager.getProcess(oldId)
if (!process) return
const proc = ptyManager.getProcess(oldId)
if (!proc) return

const { cwd, sessionId, backend: oldBackend } = process
const { cwd, sessionId, backend: oldBackend } = proc
const effectiveSessionId = oldBackend !== newBackend ? undefined : sessionId

// Look up project settings so the new PTY inherits permissions and model
const workspace = sessionStore.getWorkspace()
const project = workspace.projects.find(p => p.path === cwd)
const globalSettings = sessionStore.getSettings()
const autoAcceptTools = project?.autoAcceptTools ?? globalSettings.autoAcceptTools
const permissionMode = project?.permissionMode ?? globalSettings.permissionMode

// Kill old PTY first, then spawn new one with error recovery
ptyManager.kill(oldId)
const newId = ptyManager.spawn(cwd, effectiveSessionId, undefined, undefined, undefined, newBackend)

let newId: string
try {
newId = ptyManager.spawn(cwd, effectiveSessionId, autoAcceptTools, permissionMode, undefined, newBackend)
} catch (error: any) {
console.error('Failed to spawn new PTY during backend switch:', error)
// Clean up stale map entries from the killed PTY
ptyToProject.delete(oldId)
ptyToBackend.delete(oldId)
autoCloseSessions.delete(oldId)
throw new Error(`Failed to switch backend to ${newBackend}: ${error.message}`)
}

const projectPath = ptyToProject.get(oldId)
if (projectPath) {
Expand All @@ -118,17 +137,31 @@ export function registerPtyHandlers(
ptyToBackend.set(newId, newBackend)
ptyToBackend.delete(oldId)

// Transfer autoClose flag if the old session had it
if (autoCloseSessions.delete(oldId)) {
autoCloseSessions.add(newId)
}

const mainWindow = getMainWindow()

ptyManager.onData(newId, (data) => {
maybeRespondToCursorPositionRequest(ptyManager, ptyToBackend, newId, data)
mainWindow?.webContents.send(`pty:data:${newId}`, data)
try {
mainWindow?.webContents.send(`pty:data:${newId}`, data)
} catch (e) {
console.error('IPC send failed', e)
}
})

ptyManager.onExit(newId, (code) => {
mainWindow?.webContents.send(`pty:exit:${newId}`, code)
try {
mainWindow?.webContents.send(`pty:exit:${newId}`, code)
} catch (e) {
console.error('IPC send failed', e)
}
ptyToProject.delete(newId)
ptyToBackend.delete(newId)
autoCloseSessions.delete(newId)
})

mainWindow?.webContents.send('pty:recreated', { oldId, newId, backend: newBackend })
Expand Down
8 changes: 7 additions & 1 deletion src/main/pty-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ class OutputBuffer {
this.partial = parts.pop() || ''
for (const line of parts) {
// Strip ANSI escape sequences for readability
const clean = line.replace(/\x1b\[[0-9;]*[a-zA-Z]/g, '').replace(/\x1b\][^\x07]*\x07/g, '').trim()
// Strip all CSI sequences (including private modes like ?25h, ?1049h),
// OSC sequences, and single-character escape sequences (e.g. \x1b(B)
const clean = line
.replace(/\x1b\[[\x20-\x3f]*[\x40-\x7e]/g, '') // CSI: covers standard + private mode sequences
.replace(/\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)/g, '') // OSC: terminated by BEL or ST
.replace(/\x1b[()][A-Z0-9]/g, '') // Character set selection (e.g. \x1b(B)
.trim()
if (clean) {
this.lines.push(clean)
if (this.lines.length > OUTPUT_BUFFER_MAX_LINES) {
Expand Down