diff --git a/src/main/startup/synthetic-title-runtime.test.ts b/src/main/startup/synthetic-title-runtime.test.ts new file mode 100644 index 00000000000..42e6c0bf972 --- /dev/null +++ b/src/main/startup/synthetic-title-runtime.test.ts @@ -0,0 +1,88 @@ +import { beforeEach, describe, expect, it, vi } from 'vitest' + +const { ingestSyntheticTitleFrame } = vi.hoisted(() => ({ + ingestSyntheticTitleFrame: vi.fn((_ptyId: string, _data: string) => {}) +})) + +vi.mock('../ipc/pty/pane/key-state', () => ({ + registerPaneKeyTeardownListener: vi.fn(), + getPtyIdForPaneKey: (paneKey: string) => `pty:${paneKey}` +})) +vi.mock('../agent-hooks/server/agent-hook-server', () => ({ agentHookServer: {} })) +vi.mock('./main-process-state', () => ({ + mainProcessState: { + mainWindow: null, + store: null, + runtime: { ingestSyntheticTitleFrame, getTrackedPtyTitle: () => null } + } +})) + +import { + driveSyntheticTitleFromHookStatus, + stopSyntheticTitleSpinner +} from './synthetic-title-runtime' + +const PANE = 'tab-1:leaf-1' +const QUESTION = JSON.stringify({ questions: [{ title: 'Which database?' }] }) + +function codexStatus( + state: 'working' | 'waiting' | 'done', + interactivePrompt?: string, + toolName = 'request_user_input_async' +) { + driveSyntheticTitleFromHookStatus({ + paneKey: PANE, + payload: { + state, + agentType: 'codex', + ...(interactivePrompt ? { toolName, interactivePrompt } : {}) + } + }) +} + +// Codex's PermissionRequest hook carries no request id; two approvals can look identical. +const APPROVAL = JSON.stringify({ approval: { command: 'pnpm tc' } }) + +function bellCount(): number { + return ingestSyntheticTitleFrame.mock.calls.filter(([, data]) => data.endsWith('\x07\x07')).length +} + +describe('synthetic title BEL', () => { + beforeEach(() => { + ingestSyntheticTitleFrame.mockClear() + stopSyntheticTitleSpinner(PANE) + }) + + it('rings once while hooks keep restating the same open question', () => { + // Codex restates waiting on every tool call while its async question stays open. + for (let i = 0; i < 20; i += 1) { + codexStatus('waiting', QUESTION) + } + expect(bellCount()).toBe(1) + // The permission title is still reasserted, only without a bell. + expect(ingestSyntheticTitleFrame).toHaveBeenCalledTimes(20) + }) + + it('rings again for a new prompt and after the wait ends', () => { + codexStatus('waiting', QUESTION) + codexStatus('waiting', JSON.stringify({ questions: [{ title: 'Which table name?' }] })) + codexStatus('waiting', QUESTION) + expect(bellCount()).toBe(2) + + codexStatus('done') + codexStatus('waiting', QUESTION) + expect(bellCount()).toBe(3) + + codexStatus('working') + codexStatus('waiting', QUESTION) + expect(bellCount()).toBe(4) + }) + it('rings for each approval during an open question, but not for the question again', () => { + codexStatus('waiting', QUESTION) + codexStatus('waiting', APPROVAL, 'Bash') + // Approved: Codex keeps working and restates the still-open question. + codexStatus('waiting', QUESTION) + codexStatus('waiting', APPROVAL, 'Bash') + expect(bellCount()).toBe(3) + }) +}) diff --git a/src/main/startup/synthetic-title-runtime.ts b/src/main/startup/synthetic-title-runtime.ts index 7c5d24652bb..717b9bea6cf 100644 --- a/src/main/startup/synthetic-title-runtime.ts +++ b/src/main/startup/synthetic-title-runtime.ts @@ -15,6 +15,7 @@ import { import { shouldSendSyntheticTitleFrame } from '../runtime/terminal/title/synthetic-title-visibility' import { shouldCopySyntheticTitleFrameToPtyData } from '../runtime/terminal/title/synthetic-title-frame-routing' import { mainProcessState as state } from './main-process-state' +import { CODEX_ASYNC_QUESTION_TOOL } from '../../shared/codex-async-question-input' // Cursor redraws overwrite one-shot OSC titles; reassert at Pi's 80ms cadence and share the animation with OpenCode. const SPINNER_FRAMES = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'] @@ -24,6 +25,16 @@ const syntheticTitleSpinnerByPaneKey = new Map< SyntheticTitleSpinnerEntry >() let syntheticTitleSpinnerTimer: ReturnType | null = null +/** What one pane's current needs-input stretch has already rung for. */ +type NeedsInputBellState = { + /** The prompt the last needs-input frame showed; restating it is not a new request. */ + lastPromptKey: string + /** Async questions stay open across the turn, so one that resurfaces after an approval stays rung. */ + rungAsyncQuestionKeys: Set +} +// Why: hooks restate a held wait on every event (Codex re-sends one per tool call while an async +// question stays open), so ringing on each restatement becomes an OS notification every few seconds. +const needsInputBellByPaneKey = new Map() function isSyntheticTitleWindowVisible(): boolean { const window = state.mainWindow @@ -64,6 +75,7 @@ function canSendDecorativeSyntheticTitle(): boolean { } export function stopSyntheticTitleSpinner(paneKey: string): void { + needsInputBellByPaneKey.delete(paneKey) if (syntheticTitleSpinnerByPaneKey.delete(paneKey)) { stopSyntheticTitleSpinnerTimerIfIdle() } @@ -117,15 +129,28 @@ export function resumeSyntheticTitleSpinnerTimer(): void { ensureSyntheticTitleSpinnerTimer() } +function isNeedsInputState(agentState: AgentStatusState): boolean { + return agentState === 'blocked' || agentState === 'waiting' +} + export function driveSyntheticTitleFromHook( paneKey: string, agentState: AgentStatusState, - profile: SyntheticAgentTitleProfile + profile: SyntheticAgentTitleProfile, + /** Identifies what the agent is waiting on; a new prompt in the same wait rings again. */ + needsInput: { promptKey: string; asyncQuestion: boolean } = { + promptKey: '', + asyncQuestion: false + } ): void { const ptyId = getPtyIdForPaneKey(paneKey) if (!ptyId) { return } + const needsUserInput = isNeedsInputState(agentState) + if (!needsUserInput) { + needsInputBellByPaneKey.delete(paneKey) + } if (agentState === 'working') { // Emit immediately so visible startup does not wait for the next 80ms tick. const existing = syntheticTitleSpinnerByPaneKey.get(paneKey) @@ -143,10 +168,22 @@ export function driveSyntheticTitleFromHook( return } // Stop before the next tick can restore working; only permission frames ring BEL, completion owns its own notifications. + const bell = needsInputBellByPaneKey.get(paneKey) stopSyntheticTitleSpinner(paneKey) - const needsUserInput = agentState === 'blocked' || agentState === 'waiting' const label = needsUserInput ? profile.permissionLabel : profile.idleLabel - sendSyntheticTitle(ptyId, `\x1b]0;${label}\x07${needsUserInput ? '\x07' : ''}`, { force: true }) + let ringsBell = false + if (needsUserInput) { + const next = bell ?? { lastPromptKey: '', rungAsyncQuestionKeys: new Set() } + // Approvals carry no request id (Codex PermissionRequest), so a repeat after another prompt is new. + ringsBell = bell === undefined || next.lastPromptKey !== needsInput.promptKey + if (needsInput.asyncQuestion) { + ringsBell &&= !next.rungAsyncQuestionKeys.has(needsInput.promptKey) + next.rungAsyncQuestionKeys.add(needsInput.promptKey) + } + next.lastPromptKey = needsInput.promptKey + needsInputBellByPaneKey.set(paneKey, next) + } + sendSyntheticTitle(ptyId, `\x1b]0;${label}\x07${ringsBell ? '\x07' : ''}`, { force: true }) } // Why: a profile that leaves working titles to the agent never overwrites the rest label Orca @@ -187,7 +224,13 @@ export function initializeSyntheticTitleRuntime(): void { export function driveSyntheticTitleFromHookStatus(event: { paneKey: string - payload: { state: AgentStatusState; agentType?: string } + payload: { + state: AgentStatusState + agentType?: string + toolName?: string + toolInput?: string + interactivePrompt?: string + } providerSessionOnly?: boolean structuredHost?: unknown }): void { @@ -200,8 +243,16 @@ export function driveSyntheticTitleFromHookStatus(event: { if (!profile) { return } + // Profiles that leave working titles to the agent never reach the drive below on working. + if (!isNeedsInputState(event.payload.state)) { + needsInputBellByPaneKey.delete(event.paneKey) + } if (shouldDriveSyntheticAgentTitleFromHook(event.payload.agentType, event.payload.state)) { - driveSyntheticTitleFromHook(event.paneKey, event.payload.state, profile) + const { toolName, toolInput, interactivePrompt } = event.payload + driveSyntheticTitleFromHook(event.paneKey, event.payload.state, profile, { + promptKey: JSON.stringify([toolName ?? null, interactivePrompt ?? toolInput ?? null]), + asyncQuestion: toolName === CODEX_ASYNC_QUESTION_TOOL + }) } else if (event.payload.state === 'working' && profile.synthesizeTerminalTitle !== false) { retractSyntheticRestTitle(event.paneKey, profile) }