From 733f77d52298ccbc528ad79865d6f4ad7f5b698e Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Sun, 7 Jun 2026 04:47:10 -0700 Subject: [PATCH] Track terminal titles in headless snapshots (#4780) --- src/main/daemon/headless-emulator.test.ts | 18 ++++++++++++++++++ src/main/daemon/headless-emulator.ts | 9 ++++++++- src/main/daemon/types.ts | 1 + src/main/ipc/pty.ts | 1 + src/main/runtime/orca-runtime.test.ts | 13 +++++++++++++ src/main/runtime/orca-runtime.ts | 5 +++++ 6 files changed, 46 insertions(+), 1 deletion(-) diff --git a/src/main/daemon/headless-emulator.test.ts b/src/main/daemon/headless-emulator.test.ts index 5e73efd57..260d849e7 100644 --- a/src/main/daemon/headless-emulator.test.ts +++ b/src/main/daemon/headless-emulator.test.ts @@ -111,6 +111,24 @@ describe('HeadlessEmulator', () => { }) }) + describe('OSC title tracking', () => { + it('captures the latest OSC window title in snapshots', async () => { + emulator = new HeadlessEmulator({ cols: 80, rows: 24 }) + + await emulator.write('\x1b]0;Codex working\x07hello') + + expect(emulator.getSnapshot().lastTitle).toBe('Codex working') + }) + + it('uses the last OSC title when a chunk contains multiple title updates', async () => { + emulator = new HeadlessEmulator({ cols: 80, rows: 24 }) + + await emulator.write('\x1b]0;Codex working\x07output\x1b]2;Codex idle\x1b\\') + + expect(emulator.getSnapshot().lastTitle).toBe('Codex idle') + }) + }) + describe('resize', () => { it('updates dimensions', () => { emulator = new HeadlessEmulator({ cols: 80, rows: 24 }) diff --git a/src/main/daemon/headless-emulator.ts b/src/main/daemon/headless-emulator.ts index 2611ea025..f432aced0 100644 --- a/src/main/daemon/headless-emulator.ts +++ b/src/main/daemon/headless-emulator.ts @@ -1,6 +1,7 @@ import './xterm-env-polyfill' import { Terminal } from '@xterm/headless' import { SerializeAddon } from '@xterm/addon-serialize' +import { extractLastOscTitle } from '../../shared/agent-detection' import type { TerminalSnapshot, TerminalModes } from './types' export type HeadlessEmulatorOptions = { @@ -51,6 +52,7 @@ export class HeadlessEmulator { private terminal: Terminal private serializer: SerializeAddon private cwd: string | null = null + private lastTitle: string | null = null private privateModeScanTail = '' private mouseTrackingMode: MouseTrackingMode = 'none' private sgrMouseMode = false @@ -87,6 +89,10 @@ export class HeadlessEmulator { } this.scanOsc7(data) + const lastTitle = extractLastOscTitle(data) + if (lastTitle !== null) { + this.lastTitle = lastTitle + } return new Promise((resolve) => { this.terminal.write(data, () => { // Why: snapshots combine serialized xterm state with mirrored mouse @@ -118,7 +124,8 @@ export class HeadlessEmulator { modes, cols: this.terminal.cols, rows: this.terminal.rows, - scrollbackLines: this.terminal.buffer.normal.length - this.terminal.rows + scrollbackLines: this.terminal.buffer.normal.length - this.terminal.rows, + lastTitle: this.lastTitle ?? undefined } } diff --git a/src/main/daemon/types.ts b/src/main/daemon/types.ts index e9f3c966d..f0eeb9d0a 100644 --- a/src/main/daemon/types.ts +++ b/src/main/daemon/types.ts @@ -23,6 +23,7 @@ export type TerminalSnapshot = { cols: number rows: number scrollbackLines: number + lastTitle?: string } export type TerminalModes = { diff --git a/src/main/ipc/pty.ts b/src/main/ipc/pty.ts index 6987c3759..b297c9cb8 100644 --- a/src/main/ipc/pty.ts +++ b/src/main/ipc/pty.ts @@ -1766,6 +1766,7 @@ export function registerPtyHandlers( data: string cols: number rows: number + lastTitle?: string seq?: number source?: 'headless' | 'renderer' } | null> => { diff --git a/src/main/runtime/orca-runtime.test.ts b/src/main/runtime/orca-runtime.test.ts index 2c6bbe647..f476f6e36 100644 --- a/src/main/runtime/orca-runtime.test.ts +++ b/src/main/runtime/orca-runtime.test.ts @@ -3434,6 +3434,19 @@ describe('OrcaRuntimeService', () => { }) }) + it('returns OSC titles from headless main terminal snapshots', async () => { + const runtime = createRuntime() + syncSinglePty(runtime, 'pty-1') + + runtime.onPtyData('pty-1', '\x1b]0;Codex working\x07hello\n', 100) + + const snapshot = await runtime.serializeMainTerminalBuffer('pty-1', { scrollbackRows: 1000 }) + expect(snapshot).toMatchObject({ + source: 'headless', + lastTitle: 'Codex working' + }) + }) + it('emits explicit OSC 9999 agent status from runtime PTY data', () => { const statuses: RuntimeTerminalAgentStatusEvent[] = [] const runtime = new OrcaRuntimeService(store, undefined, { diff --git a/src/main/runtime/orca-runtime.ts b/src/main/runtime/orca-runtime.ts index 20da22ad0..9fc4cd3bd 100644 --- a/src/main/runtime/orca-runtime.ts +++ b/src/main/runtime/orca-runtime.ts @@ -3291,6 +3291,7 @@ export class OrcaRuntimeService { data: string cols: number rows: number + lastTitle?: string seq?: number source?: 'headless' | 'renderer' } | null> { @@ -3304,6 +3305,7 @@ export class OrcaRuntimeService { data: string cols: number rows: number + lastTitle?: string seq?: number source?: 'headless' | 'renderer' } | null> { @@ -3505,6 +3507,7 @@ export class OrcaRuntimeService { data: string cols: number rows: number + lastTitle?: string seq?: number source?: 'headless' | 'renderer' } | null> { @@ -3546,6 +3549,7 @@ export class OrcaRuntimeService { data: string cols: number rows: number + lastTitle?: string seq?: number source?: 'headless' } | null> { @@ -3570,6 +3574,7 @@ export class OrcaRuntimeService { data, cols: snapshot.cols, rows: snapshot.rows, + lastTitle: snapshot.lastTitle, seq: state.outputSequence, source: 'headless' }