From 2087baf264ddb23aac883f86f144689ee9e6a6b3 Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Tue, 4 Aug 2026 15:42:38 -0700 Subject: [PATCH] fix terminal output ownership gaps --- .../command-code-output-ownership.test.ts | 9 +++- .../command-code-output-ownership.ts | 11 +++-- .../parked-terminal-byte-watcher.test.ts | 2 +- .../parked-terminal-command-status.test.ts | 16 +++++++ .../parked-terminal-command-status.ts | 3 +- .../terminal-pane/pty-connection.test.ts | 42 +++++++++++++++++ .../terminal-pane/pty-connection.ts | 6 ++- src/shared/command-code-output-status.test.ts | 46 +++++++++++++++++-- src/shared/command-code-output-status.ts | 23 +++++++--- 9 files changed, 139 insertions(+), 19 deletions(-) diff --git a/src/renderer/src/components/terminal-pane/command-code-output-ownership.test.ts b/src/renderer/src/components/terminal-pane/command-code-output-ownership.test.ts index e876e09387b..18a3b7df437 100644 --- a/src/renderer/src/components/terminal-pane/command-code-output-ownership.test.ts +++ b/src/renderer/src/components/terminal-pane/command-code-output-ownership.test.ts @@ -15,13 +15,20 @@ describe('canCommandCodeOutputOwnPane', () => { it('rejects another agent owner or foreground process', () => { expect(canCommandCodeOutputOwnPane({ paneOwnerAgent: 'claude' })).toBe(false) expect(canCommandCodeOutputOwnPane({ foregroundAgent: 'claude' })).toBe(false) + expect( + canCommandCodeOutputOwnPane({ + paneOwnerAgent: 'unknown', + retainedPaneOwnerAgent: 'claude' + }) + ).toBe(false) }) it('prefers the current foreground process over stale pane ownership', () => { expect( canCommandCodeOutputOwnPane({ foregroundAgent: 'command-code', - paneOwnerAgent: 'claude' + paneOwnerAgent: 'claude', + retainedPaneOwnerAgent: 'claude' }) ).toBe(true) expect( diff --git a/src/renderer/src/components/terminal-pane/command-code-output-ownership.ts b/src/renderer/src/components/terminal-pane/command-code-output-ownership.ts index 78cf1ac14f0..65f5f196ca0 100644 --- a/src/renderer/src/components/terminal-pane/command-code-output-ownership.ts +++ b/src/renderer/src/components/terminal-pane/command-code-output-ownership.ts @@ -5,6 +5,7 @@ export function canCommandCodeOutputOwnPane(args: { foregroundAgent?: TuiAgent | null shellForeground?: boolean paneOwnerAgent?: AgentType | null + retainedPaneOwnerAgent?: AgentType | null }): boolean { if (args.foregroundAgent) { return args.foregroundAgent === 'command-code' @@ -12,9 +13,9 @@ export function canCommandCodeOutputOwnPane(args: { if (args.shellForeground) { return false } - return ( - !args.paneOwnerAgent || - args.paneOwnerAgent === 'unknown' || - args.paneOwnerAgent === 'command-code' - ) + const paneOwnerAgent = + args.paneOwnerAgent && args.paneOwnerAgent !== 'unknown' + ? args.paneOwnerAgent + : (args.retainedPaneOwnerAgent ?? args.paneOwnerAgent) + return !paneOwnerAgent || paneOwnerAgent === 'unknown' || paneOwnerAgent === 'command-code' } diff --git a/src/renderer/src/components/terminal-pane/parked-terminal-byte-watcher.test.ts b/src/renderer/src/components/terminal-pane/parked-terminal-byte-watcher.test.ts index 21d912dd7c0..959a8069db3 100644 --- a/src/renderer/src/components/terminal-pane/parked-terminal-byte-watcher.test.ts +++ b/src/renderer/src/components/terminal-pane/parked-terminal-byte-watcher.test.ts @@ -406,7 +406,7 @@ describe('startParkedTerminalByteWatcher', () => { it('feeds Command Code output through the parked byte detector', async () => { const { dispose } = await startWatcher() - emit('# Command Code v0.27.2') + emit('# Command Code v0.27.2\r\n') emit('⌘ Parsing...') expect(commandStatusPolicy.onCommandCodeWorking).toHaveBeenCalledTimes(1) diff --git a/src/renderer/src/components/terminal-pane/parked-terminal-command-status.test.ts b/src/renderer/src/components/terminal-pane/parked-terminal-command-status.test.ts index eb386065306..e9005fdd11f 100644 --- a/src/renderer/src/components/terminal-pane/parked-terminal-command-status.test.ts +++ b/src/renderer/src/components/terminal-pane/parked-terminal-command-status.test.ts @@ -16,6 +16,7 @@ const ROUTING = { connectionId: null } type MockStoreState = { tabsByWorktree: Record agentStatusByPaneKey: Record + retainedAgentsByPaneKey: Record paneForegroundAgentByPaneKey: Record agentLaunchConfigByPaneKey: Record runtimePaneTitlesByTabId: Record> @@ -46,6 +47,7 @@ function makeMockStoreState(): MockStoreState { return { tabsByWorktree: { [WORKTREE_ID]: [{ id: TAB_ID }] }, agentStatusByPaneKey: {}, + retainedAgentsByPaneKey: {}, paneForegroundAgentByPaneKey: {}, agentLaunchConfigByPaneKey: {}, runtimePaneTitlesByTabId: { [TAB_ID]: { [PANE_ID]: '✳ Build feature' } }, @@ -129,6 +131,19 @@ describe('createParkedTerminalCommandStatusPolicy', () => { policy.dispose() }) + it('rejects scrape status when retained Claude identity owns the pane', async () => { + mockStoreState.retainedAgentsByPaneKey[PANE_KEY] = { agentType: 'claude' } + mockStoreState.agentStatusByPaneKey[PANE_KEY] = makeStatusEntry({ agentType: 'unknown' }) + const policy = await createPolicy(PTY_ID_LOCAL) + + policy.onCommandCodeWorking('False prompt') + policy.onCommandCodeDone('False prompt') + vi.advanceTimersByTime(DONE_SETTLE_MS) + + expect(mockStoreState.setAgentStatus).not.toHaveBeenCalled() + policy.dispose() + }) + it('rejects scrape status when Claude launch metadata owns the pane', async () => { mockStoreState.tabsByWorktree[WORKTREE_ID] = [{ id: TAB_ID, launchAgent: 'claude' }] const policy = await createPolicy(PTY_ID_LOCAL) @@ -155,6 +170,7 @@ describe('createParkedTerminalCommandStatusPolicy', () => { it('lets a current Command Code process reclaim stale Claude ownership', async () => { mockStoreState.tabsByWorktree[WORKTREE_ID] = [{ id: TAB_ID, launchAgent: 'claude' }] mockStoreState.agentStatusByPaneKey[PANE_KEY] = makeStatusEntry({ state: 'done' }) + mockStoreState.retainedAgentsByPaneKey[PANE_KEY] = { agentType: 'claude' } mockStoreState.paneForegroundAgentByPaneKey[PANE_KEY] = { agent: 'command-code', shellForeground: false diff --git a/src/renderer/src/components/terminal-pane/parked-terminal-command-status.ts b/src/renderer/src/components/terminal-pane/parked-terminal-command-status.ts index c00165a1f38..9f54e02e87f 100644 --- a/src/renderer/src/components/terminal-pane/parked-terminal-command-status.ts +++ b/src/renderer/src/components/terminal-pane/parked-terminal-command-status.ts @@ -74,7 +74,8 @@ export function createParkedTerminalCommandStatusPolicy(options: { return canCommandCodeOutputOwnPane({ foregroundAgent: foreground?.agent, shellForeground: foreground?.shellForeground, - paneOwnerAgent + paneOwnerAgent, + retainedPaneOwnerAgent: state.retainedAgentsByPaneKey[paneKey]?.agentType }) } diff --git a/src/renderer/src/components/terminal-pane/pty-connection.test.ts b/src/renderer/src/components/terminal-pane/pty-connection.test.ts index c42e1e370e0..5f5cf3a0afc 100644 --- a/src/renderer/src/components/terminal-pane/pty-connection.test.ts +++ b/src/renderer/src/components/terminal-pane/pty-connection.test.ts @@ -17,6 +17,7 @@ import { TERMINAL_PASTE_DIRECT_MAX_BYTES } from './terminal-paste-coordinator' import { resolveWindowsShiftEnterEncodingForPane } from './terminal-windows-shift-enter' import type * as UseNotificationDispatchModule from './use-notification-dispatch' import { getEagerPtyBufferHandle } from './pty-dispatcher' +import type { AgentType } from '../../../../shared/agent-status-types' import { makePaneKey } from '../../../../shared/stable-pane-id' import { toAppSshPtyId } from '../../../../shared/ssh-pty-id' import type { SshConnectionState } from '../../../../shared/ssh-types' @@ -227,6 +228,7 @@ type StoreState = { consumePendingSnapshot: ReturnType runtimePaneTitlesByTabId: Record> agentStatusByPaneKey: Record + retainedAgentsByPaneKey: Record paneForegroundAgentByPaneKey: Record sleepingAgentSessionsByPaneKey: Record suppressedPtyExitIds: Record @@ -928,6 +930,7 @@ describe('connectPanePty', () => { consumePendingSnapshot: vi.fn(() => null), runtimePaneTitlesByTabId: {}, agentStatusByPaneKey: {}, + retainedAgentsByPaneKey: {}, paneForegroundAgentByPaneKey: {}, sleepingAgentSessionsByPaneKey: {}, suppressedPtyExitIds: {}, @@ -19044,6 +19047,45 @@ describe('connectPanePty', () => { expect(mockStoreState.agentStatusByPaneKey[paneKey]).toBe(claudeStatus) }) + it('rejects Command Code facts when retained Claude identity owns the pane', async () => { + enableMainAuthority() + const { connectPanePty } = await import('./pty-connection') + const handler = await import('./terminal-side-effect-facts-handler') + const transport = createMockTransport() + transportFactoryQueue.push(transport) + vi.useFakeTimers() + const paneKey = makePaneKey('tab-1', LEAF_1) + const unknownStatus = { + paneKey, + state: 'working' as const, + prompt: '', + updatedAt: Date.now(), + stateStartedAt: Date.now(), + agentType: 'unknown' as const, + stateHistory: [] + } + mockStoreState.retainedAgentsByPaneKey[paneKey] = { agentType: 'claude' } + mockStoreState.agentStatusByPaneKey[paneKey] = unknownStatus + + connectPanePty(createPane(1) as never, createManager(1) as never, createDeps() as never) + const onPtySpawn = createdTransportOptions[0]?.onPtySpawn as (ptyId: string) => void + onPtySpawn('pty-fact-retained-false-cc') + mockStoreState.setAgentStatus.mockClear() + + handler._dispatchTerminalSideEffectBatchForTest({ + ptyId: 'pty-fact-retained-false-cc', + seq: 1, + facts: [ + { kind: 'command-code-working', prompt: 'False prompt' }, + { kind: 'command-code-done', prompt: 'False prompt' } + ] + }) + vi.advanceTimersByTime(2000) + + expect(mockStoreState.setAgentStatus).not.toHaveBeenCalled() + expect(mockStoreState.agentStatusByPaneKey[paneKey]).toBe(unknownStatus) + }) + it('keeps Command Code working when a working fact lands before the done settles', async () => { enableMainAuthority() const { connectPanePty } = await import('./pty-connection') diff --git a/src/renderer/src/components/terminal-pane/pty-connection.ts b/src/renderer/src/components/terminal-pane/pty-connection.ts index 07cc6d316ae..d0b0c2a3338 100644 --- a/src/renderer/src/components/terminal-pane/pty-connection.ts +++ b/src/renderer/src/components/terminal-pane/pty-connection.ts @@ -2820,11 +2820,13 @@ export function connectPanePty( } const canApplyCommandCodeOutputStatus = (): boolean => { - const foreground = useAppStore.getState().paneForegroundAgentByPaneKey[cacheKey] + const state = useAppStore.getState() + const foreground = state.paneForegroundAgentByPaneKey[cacheKey] return canCommandCodeOutputOwnPane({ foregroundAgent: foreground?.agent, shellForeground: foreground?.shellForeground, - paneOwnerAgent: getAuthoritativePaneAgent() + paneOwnerAgent: getAuthoritativePaneAgent(), + retainedPaneOwnerAgent: state.retainedAgentsByPaneKey[cacheKey]?.agentType }) } diff --git a/src/shared/command-code-output-status.test.ts b/src/shared/command-code-output-status.test.ts index 1e84f97adac..77a8005056e 100644 --- a/src/shared/command-code-output-status.test.ts +++ b/src/shared/command-code-output-status.test.ts @@ -29,7 +29,7 @@ describe('createCommandCodeOutputStatusDetector', () => { }) expect(detector.observe('Thinking about unrelated shell output')).toBe(false) - expect(detector.observe('# Command Code v0.27.2')).toBe(false) + expect(detector.observe('# Command Code v0.27.2\r\n')).toBe(false) expect(detector.observe('⌘ Parsing...')).toBe(true) expect(onWorking).toHaveBeenCalledWith('') @@ -43,7 +43,7 @@ describe('createCommandCodeOutputStatusDetector', () => { }) expect(detector.observe('# Command')).toBe(false) - expect(detector.observe(' Code v0.27.2')).toBe(false) + expect(detector.observe(' Code v0.27.2\r\n')).toBe(false) expect(detector.observe('⌘ Parsing...')).toBe(true) expect(onWorking).toHaveBeenCalledWith('') @@ -56,7 +56,7 @@ describe('createCommandCodeOutputStatusDetector', () => { onWorking }) - expect(detector.observe('# C\x1b[35mommand Co\x1b[0mde v0.27.2')).toBe(false) + expect(detector.observe('# C\x1b[35mommand Co\x1b[0mde v0.27.2\r\n')).toBe(false) expect(detector.observe('⌘ Parsing...')).toBe(true) expect(onWorking).toHaveBeenCalledWith('') @@ -87,6 +87,46 @@ describe('createCommandCodeOutputStatusDetector', () => { expect(onWorking).not.toHaveBeenCalled() }) + it.each([ + '# Command Code v1.2\r\n', + '# Command Code v1.2.3.4\r\n', + '# Command Code v01.2.3\r\n', + '# Command Code v1.2.3suffix\r\n', + '#\nCommand Code v1.2.3\r\n' + ])('does not arm for noncanonical banner %j', (banner) => { + const onWorking = vi.fn() + const detector = createCommandCodeOutputStatusDetector({ startupCommand: null, onWorking }) + + expect(detector.observe(banner)).toBe(false) + expect(detector.observe('✻ Thinking...')).toBe(false) + + expect(onWorking).not.toHaveBeenCalled() + }) + + it('does not arm from a four-part version split at a PTY chunk boundary', () => { + const onWorking = vi.fn() + const detector = createCommandCodeOutputStatusDetector({ startupCommand: null, onWorking }) + + expect(detector.observe('# Command Code v1.2.3')).toBe(false) + expect(detector.observe('.4\r\n')).toBe(false) + expect(detector.observe('✻ Thinking...')).toBe(false) + + expect(onWorking).not.toHaveBeenCalled() + }) + + it.each(['# Command Code v1.2.3\r\n', '# Command Code v1.2.3-beta.1+build.9\n'])( + 'arms for canonical banner %j', + (banner) => { + const onWorking = vi.fn() + const detector = createCommandCodeOutputStatusDetector({ startupCommand: null, onWorking }) + + expect(detector.observe(banner)).toBe(false) + expect(detector.observe('✻ Thinking...')).toBe(true) + + expect(onWorking).toHaveBeenCalledWith('') + } + ) + it.each([ 'Pondering', 'Contemplating', diff --git a/src/shared/command-code-output-status.ts b/src/shared/command-code-output-status.ts index 276104c1e4a..af862b5abc7 100644 --- a/src/shared/command-code-output-status.ts +++ b/src/shared/command-code-output-status.ts @@ -112,7 +112,16 @@ const ACTIVE_EXECUTION_STATUS_RE = new RegExp( `(?:^|[\\r\\n])\\s*(?:${COMMAND_CODE_STATUS_GLYPH_RE_SOURCE}\\s*)?(?:Executing:\\s+\\S|Running\\s*\\()` ) const IDLE_PROMPT_RE = /(?:^|[\r\n])\s*[❯>]\s+Ask your question\.\.\./ -const COMMAND_CODE_BANNER_RE = /(?:^|[\r\n])\s*#\s*Command Code\s+v\d+(?:\.\d+){1,3}\b/ +const SEMVER_NUMBER_RE_SOURCE = '(?:0|[1-9]\\d*)' +const SEMVER_PRERELEASE_IDENTIFIER_RE_SOURCE = '(?:0|[1-9]\\d*|\\d*[A-Za-z-][0-9A-Za-z-]*)' +const SEMVER_BUILD_IDENTIFIER_RE_SOURCE = '[0-9A-Za-z-]+' +const COMMAND_CODE_BANNER_RE = new RegExp( + `(?:^|[\\r\\n])[ \\t]*#[ \\t]+Command Code[ \\t]+v` + + `${SEMVER_NUMBER_RE_SOURCE}\\.${SEMVER_NUMBER_RE_SOURCE}\\.${SEMVER_NUMBER_RE_SOURCE}` + + `(?:-${SEMVER_PRERELEASE_IDENTIFIER_RE_SOURCE}(?:\\.${SEMVER_PRERELEASE_IDENTIFIER_RE_SOURCE})*)?` + + `(?:\\+${SEMVER_BUILD_IDENTIFIER_RE_SOURCE}(?:\\.${SEMVER_BUILD_IDENTIFIER_RE_SOURCE})*)?` + + `(?=[ \\t]*[\\r\\n])` +) function cleanPromptCandidate(value: string): string { return cleanCommandCodePromptCandidate(stripTerminalControl(value)) @@ -233,17 +242,19 @@ export function createCommandCodeOutputStatusDetector(args: { : scanRawText if (!hasSeenCommandCodeUi) { - if ( - !rawTextMayContainCommandCodeBanner(scanRawText) && - !rawTextMayContainCommandCodeBanner(scanRawTextWithChunkBoundary) - ) { + if (!rawTextMayContainCommandCodeBanner(scanRawText)) { return false } const scanText = stripTerminalControl(scanRawText) const scanTextWithChunkBoundary = stripTerminalControl(scanRawTextWithChunkBoundary) + const previousTextWithChunkBoundaryLength = previousRawText + ? stripTerminalControl(`${previousRawText}\n`).length + : 0 if ( !COMMAND_CODE_BANNER_RE.test(scanText) && - !COMMAND_CODE_BANNER_RE.test(scanTextWithChunkBoundary) + !COMMAND_CODE_BANNER_RE.test( + scanTextWithChunkBoundary.slice(previousTextWithChunkBoundaryLength) + ) ) { return false }