From a081b67cd205d3f2580fbf9c97ceccf63c56a0dc Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Sat, 28 Nov 2026 15:43:00 -0800 Subject: [PATCH 1/2] fix(desktop-terminal): keep a fast command's output when its start and end markers share a read The session applied every shell marker in a PTY chunk before looking at the chunk's text. When a quick command's start marker, output and end marker arrived in one read, the command finished before its text was examined, so terminal_run returned completed with empty output. Marker offsets now place each piece of text before or after the markers around it. --- .../desktop/src/main/terminal/session.test.ts | 29 +++++++++++++++ apps/desktop/src/main/terminal/session.ts | 35 ++++++++++++------- .../src/main/terminal/shell-integration.ts | 14 ++++++-- 3 files changed, 64 insertions(+), 14 deletions(-) diff --git a/apps/desktop/src/main/terminal/session.test.ts b/apps/desktop/src/main/terminal/session.test.ts index ce2725af3ae..dad7bd96585 100644 --- a/apps/desktop/src/main/terminal/session.test.ts +++ b/apps/desktop/src/main/terminal/session.test.ts @@ -75,6 +75,35 @@ describe('stripTerminalQueries', () => { }) describe('TerminalSession command lifecycle', () => { + it('returns the output of a command whose start, output and end arrive in one read', async () => { + const originalShell = process.env.SHELL + process.env.SHELL = '/bin/zsh' + const session = TerminalSession.create({ + terminalId: 'terminal-1', + cwd: '/tmp', + cols: 80, + rows: 24, + callbacks: { onData: () => {}, onState: () => {}, onCommand: () => {}, onExit: () => {} }, + }) + + try { + const resultPromise = session.runCommand('printf done', 'tool-call-fast', 5_000) + // A fast command's echo, start marker, output, end marker and next prompt can share one read. + ptyStub.dataHandler?.( + 'printf done\r\n\u001b]633;C;test-nonce\u0007done\u001b]633;P;Cwd=/tmp;test-nonce\u0007\u001b]633;D;0;test-nonce\u0007\u001b]633;A;test-nonce\u0007% ' + ) + expect(await resultPromise).toMatchObject({ + status: 'completed', + exitCode: 0, + output: 'done', + }) + } finally { + session.dispose() + if (originalShell === undefined) process.env.SHELL = undefined + else process.env.SHELL = originalShell + } + }) + it('keeps normal long-command activity until the foreground process exits', async () => { vi.useFakeTimers() const originalShell = process.env.SHELL diff --git a/apps/desktop/src/main/terminal/session.ts b/apps/desktop/src/main/terminal/session.ts index 60afc3e54e3..b042506cacd 100644 --- a/apps/desktop/src/main/terminal/session.ts +++ b/apps/desktop/src/main/terminal/session.ts @@ -824,11 +824,7 @@ export class TerminalSession { } private handleData(chunk: string): void { - const { text, markers } = this.parser.parse(chunk) - - for (const marker of markers) { - this.applyMarker(marker) - } + const { text, markers, offsets } = this.parser.parse(chunk) if (text) { this.lastOutputAt = Date.now() @@ -837,18 +833,33 @@ export class TerminalSession { if (this.scrollback.length > MAX_SCROLLBACK_CHARS + TRIM_SLACK_CHARS) { this.scrollback = this.scrollback.slice(-MAX_SCROLLBACK_CHARS) } - if (this.pendingCommand?.capturing) { - this.pendingCommand.lastActivityAt = Date.now() - this.captureOutput(this.pendingCommand, text) - if (text.includes(ALT_SCREEN_ENTER)) { - this.resolveInteractiveCommand() - } - } + } + + // Text is attributed in stream order, so a command keeps only the output between its own + // start and end markers even when they share this chunk with the output. + let from = 0 + markers.forEach((marker, index) => { + this.captureCommandOutput(text.slice(from, offsets[index])) + from = offsets[index] + this.applyMarker(marker) + }) + this.captureCommandOutput(text.slice(from)) + + if (text) { this.pendingOutput += text this.scheduleFlush() } } + private captureCommandOutput(text: string): void { + if (!text || !this.pendingCommand?.capturing) return + this.pendingCommand.lastActivityAt = Date.now() + this.captureOutput(this.pendingCommand, text) + if (text.includes(ALT_SCREEN_ENTER)) { + this.resolveInteractiveCommand() + } + } + private applyMarker( marker: ReturnType['markers'][number] ): void { diff --git a/apps/desktop/src/main/terminal/shell-integration.ts b/apps/desktop/src/main/terminal/shell-integration.ts index da7349bbb27..47706b20ea4 100644 --- a/apps/desktop/src/main/terminal/shell-integration.ts +++ b/apps/desktop/src/main/terminal/shell-integration.ts @@ -53,6 +53,12 @@ export interface ParseResult { /** Stream with the OSC 633 sequences removed, safe to hand to xterm.js. */ text: string markers: ShellMarker[] + /** + * Where each marker fell in `text`: `markers[i]` came after `text.slice(0, offsets[i])`. A fast + * command's start, output and end can share one chunk, so output belongs to a command only + * between its own markers. + */ + offsets: number[] } /** Undoes the escaping applied by the shell hooks. */ @@ -84,6 +90,7 @@ export class ShellIntegrationParser { this.pending = '' const markers: ShellMarker[] = [] + const offsets: number[] = [] let text = '' let index = 0 @@ -110,11 +117,14 @@ export class ShellIntegrationParser { const body = buffer.slice(start + '\u001b]633;'.length, terminator.index) const marker = this.toMarker(body) - if (marker) markers.push(marker) + if (marker) { + markers.push(marker) + offsets.push(text.length) + } index = terminator.index + terminator.length } - return { text, markers } + return { text, markers, offsets } } private toMarker(body: string): ShellMarker | null { From 7d4012d2f999876f369d50cb28a8bf0b87d8cde7 Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Sun, 29 Nov 2026 16:32:16 -0800 Subject: [PATCH 2/2] fix(desktop-terminal): judge a full-screen command only after the whole read is applied A program that opened and closed the alternate screen and finished within one read was resolved as interactive before its finish marker ran. Interactive resolution now waits until every marker in the read is applied and requires the alternate screen to still be held. --- .../desktop/src/main/terminal/session.test.ts | 24 +++++++++++++++++++ apps/desktop/src/main/terminal/session.ts | 20 ++++++++++------ 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/apps/desktop/src/main/terminal/session.test.ts b/apps/desktop/src/main/terminal/session.test.ts index dad7bd96585..2e468d9ceeb 100644 --- a/apps/desktop/src/main/terminal/session.test.ts +++ b/apps/desktop/src/main/terminal/session.test.ts @@ -104,6 +104,30 @@ describe('TerminalSession command lifecycle', () => { } }) + it('reports a full-screen program that exits within one read as completed, with its exit code', async () => { + const originalShell = process.env.SHELL + process.env.SHELL = '/bin/zsh' + const session = TerminalSession.create({ + terminalId: 'terminal-1', + cwd: '/tmp', + cols: 80, + rows: 24, + callbacks: { onData: () => {}, onState: () => {}, onCommand: () => {}, onExit: () => {} }, + }) + + try { + const resultPromise = session.runCommand("vim -c 'cq'", 'tool-call-vim', 5_000) + ptyStub.dataHandler?.( + '\u001b]633;C;test-nonce\u0007\u001b[?1049h\u001b[?1049l\u001b]633;D;1;test-nonce\u0007\u001b]633;A;test-nonce\u0007' + ) + expect(await resultPromise).toMatchObject({ status: 'completed', exitCode: 1 }) + } finally { + session.dispose() + if (originalShell === undefined) process.env.SHELL = undefined + else process.env.SHELL = originalShell + } + }) + it('keeps normal long-command activity until the foreground process exits', async () => { vi.useFakeTimers() const originalShell = process.env.SHELL diff --git a/apps/desktop/src/main/terminal/session.ts b/apps/desktop/src/main/terminal/session.ts index b042506cacd..e0d91fc0085 100644 --- a/apps/desktop/src/main/terminal/session.ts +++ b/apps/desktop/src/main/terminal/session.ts @@ -838,12 +838,19 @@ export class TerminalSession { // Text is attributed in stream order, so a command keeps only the output between its own // start and end markers even when they share this chunk with the output. let from = 0 + let enteredFullScreen = false markers.forEach((marker, index) => { - this.captureCommandOutput(text.slice(from, offsets[index])) + enteredFullScreen = + this.captureCommandOutput(text.slice(from, offsets[index])) || enteredFullScreen from = offsets[index] this.applyMarker(marker) }) - this.captureCommandOutput(text.slice(from)) + enteredFullScreen = this.captureCommandOutput(text.slice(from)) || enteredFullScreen + // Judged once the whole read is applied: a program that opened and closed the alternate + // screen and finished within it already has its result, and one that left it is not holding it. + if (enteredFullScreen && this.pendingCommand?.capturing && this.altScreen) { + this.resolveInteractiveCommand() + } if (text) { this.pendingOutput += text @@ -851,13 +858,12 @@ export class TerminalSession { } } - private captureCommandOutput(text: string): void { - if (!text || !this.pendingCommand?.capturing) return + /** Adds text to the running command's output; true when it entered the alternate screen. */ + private captureCommandOutput(text: string): boolean { + if (!text || !this.pendingCommand?.capturing) return false this.pendingCommand.lastActivityAt = Date.now() this.captureOutput(this.pendingCommand, text) - if (text.includes(ALT_SCREEN_ENTER)) { - this.resolveInteractiveCommand() - } + return text.includes(ALT_SCREEN_ENTER) } private applyMarker(