From 67cc8ffb95e960a2509dd0fd70fe5f53b9e62e2e Mon Sep 17 00:00:00 2001 From: DonutsDelivery Date: Sun, 8 Mar 2026 08:54:57 +0100 Subject: [PATCH 1/2] chore: add .kspec/ to .gitignore for shadow branch --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index ca5ce606..a5a54590 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ out/ *.log .DS_Store dist-build/ +.kspec/ From 55116697a23a98b957d41955fb6397b6cb228df3 Mon Sep 17 00:00:00 2001 From: DonutsDelivery Date: Tue, 10 Mar 2026 05:08:50 +0100 Subject: [PATCH 2/2] fix: PTY backend switch error handling, settings inheritance, ANSI stripping MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - pty:set-backend now looks up project/global settings (autoAcceptTools, permissionMode) so new PTY inherits permissions after switch - Add try-catch around spawn in backend switch — propagates error to renderer instead of silently leaving user with no PTY - Transfer autoCloseSessions flag from old to new PTY ID on switch - Add try-catch on IPC sends in backend switch data/exit callbacks (consistent with pty:spawn handler) - Fix OutputBuffer ANSI regex to strip private mode sequences (e.g. ?25h, ?1049h), character set escapes, and OSC with ST terminator Task: @bug-pty Spec: @pty-lifecycle Co-Authored-By: Claude Opus 4.6 --- src/main/app/ipc-handlers/pty.ts | 45 +++++++++++++++++++++++++++----- src/main/pty-manager.ts | 8 +++++- 2 files changed, 46 insertions(+), 7 deletions(-) diff --git a/src/main/app/ipc-handlers/pty.ts b/src/main/app/ipc-handlers/pty.ts index 39201764..ea53e558 100644 --- a/src/main/app/ipc-handlers/pty.ts +++ b/src/main/app/ipc-handlers/pty.ts @@ -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) { @@ -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 }) diff --git a/src/main/pty-manager.ts b/src/main/pty-manager.ts index fcfb7ef1..3cdb709a 100644 --- a/src/main/pty-manager.ts +++ b/src/main/pty-manager.ts @@ -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) {