fix(terminal): stop stray escape tails on Windows after returning to a busy pane (#43348)

* fix(terminal): stop stray escape tails on Windows by starting the daemon output sequence at first attach

On Windows, returning to a busy terminal could print the tail of an escape
sequence on the prompt row ("?2026h", "149C", "5;46m").

ConPTY's startup preamble is replayed into a new session before its creating
client attaches. The daemon counted those bytes in the session's output
sequence, but main counts a new session's stream from attach, so the daemon's
sequence ran 20 chars ahead of main's. A restore that used a daemon snapshot
(after a hidden-pane data gap or a renderer reload) was labelled 20 chars past
the stream position it covered; the renderer then dropped the next 20 chars of
live output, often from the middle of an escape sequence.

The output plane now counts only bytes emitted once a client has attached, so
both sides count the same stream. Protocol v57 rolls the fix into a fresh
daemon.

* test(terminal): pin the bytes the attached client receives in the pre-attach sequence test
This commit is contained in:
Neil
2026-11-16 18:51:45 -08:00
committed by GitHub
parent 1b10368eb2
commit f730fd05a1
5 changed files with 77 additions and 7 deletions
@@ -1,4 +1,5 @@
// Why: daemons survive app updates, so wire behavior must be version-gated.
// v57 starts a session's output sequence at its first attach, so a Windows restore snapshot no longer lands 20 chars past the stream.
// v56 accepts `reportFocusLost`; v55 daemons from 1.4.251 predate it and reject the request.
// v55 observes the foreground group at the physical PTY owner.
// v54 reads the macOS process table in process instead of forking setuid ps on every poll (#31783).
@@ -14,7 +15,7 @@
// v43 dropped it with the revert); v41 stages long startup commands as sourced scripts; v40 rolled
// the #25130/#24636 shell-wrapper changes and the wider agent list into a fresh daemon; older owners
// stay attachable.
export const PROTOCOL_VERSION = 56
export const PROTOCOL_VERSION = 57
export const FOCUS_LOSS_REPORT_PROTOCOL_VERSION = 56
export const PTY_FOREGROUND_OBSERVATION_PROTOCOL_VERSION = 55
export const FENCED_TERMINATION_PROTOCOL_VERSION = 51
@@ -52,7 +53,7 @@ export const MODE_2031_UNSUBSCRIBE_FACT_PROTOCOL_VERSION = 29
export const PREVIOUS_DAEMON_PROTOCOL_VERSIONS = [
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27,
28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
52, 53, 54, 55
52, 53, 54, 55, 56
] as const
export function supportsColorQueryReplyColors(protocolVersion: number): boolean {
@@ -43,6 +43,8 @@ export class SessionOutputPlane {
private pendingOutputOverflowed = false
private pendingOutputSeq = 0
private _outputSequence = 0
// Main counts a new session's stream from its first attach, so bytes emitted before it (ConPTY's startup preamble) stay out of the sequence.
private streamOpened = false
private deviceAttributesQueryFilter: StartupDeviceAttributesQueryFilter | null = null
private disposed = false
@@ -74,6 +76,7 @@ export class SessionOutputPlane {
attachClient(client: Omit<AttachedClient, 'token'>): symbol {
const token = Symbol('attach')
this.streamOpened = true
this.attachedClients.push({ token, ...client })
return token
}
@@ -200,7 +203,9 @@ export class SessionOutputPlane {
let { data } = emission
const rawLength = emission.rawEndSeq - emission.rawStartSeq
// Count raw input even when thinning drops bytes so snapshots and renderer tail deduplication align.
this._outputSequence += rawLength
if (this.streamOpened) {
this._outputSequence += rawLength
}
if (data.length > 0) {
this.emulator.write(data)
data = this.deviceAttributesQueryFilter?.accept(data) ?? data
@@ -0,0 +1,64 @@
import { describe, expect, it } from 'vitest'
import { Session } from '../session'
import type { SubprocessHandle } from '../session-subprocess-handle'
function createUnattachedSession() {
let onData: ((data: string) => void) | null = null
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: Session reads only these members.
const handle = {
pid: 999,
getForegroundProcess: () => null,
confirmShellForeground: async () => false,
write: () => {},
resize: () => {},
pause: () => {},
resume: () => {},
kill: () => {},
forceKill: () => {},
signal: () => {},
terminateOwnedTree: () => 'unavailable' as const,
onData(cb: (data: string) => void) {
onData = cb
},
onExit() {},
dispose: () => {}
} as unknown as SubprocessHandle
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the omitted options are optional.
const session = new Session({
sessionId: 'pre-attach',
cols: 80,
rows: 24,
subprocess: handle,
shellReadySupported: false
} as never)
return { session, emit: (data: string) => onData?.(data) }
}
// ConPTY's startup preamble: emitted, and replayed into the session, before the creating client attaches.
const CONPTY_PREAMBLE = '\x1b[?9001h\x1b[?1004h\x1b[6n'
describe('Session output sequence before the first attach', () => {
it('counts only bytes a stream client can receive, so it matches the count main keeps from attach', () => {
const { session, emit } = createUnattachedSession()
emit(CONPTY_PREAMBLE)
expect(session.getSnapshot()!.outputSequence).toBe(0)
const received: string[] = []
const token = session.attachClient({
onData: (data: string) => received.push(data),
onExit: () => {}
})
emit('prompt> ')
emit('\x1b[38;5;46mok\x1b[0m')
// Pin what the client received, so a silently dropped delivery cannot make both sides 0.
expect(received.join('')).toBe('prompt> \x1b[38;5;46mok\x1b[0m')
expect(session.getSnapshot()!.outputSequence).toBe(received.join('').length)
// A detached session keeps counting: the next attach reconciles against the snapshot's sequence.
session.detachClient(token)
emit('later')
expect(session.getSnapshot()!.outputSequence).toBe(received.join('').length + 'later'.length)
session.dispose()
})
})
@@ -3,10 +3,10 @@
"appId": "com.stablyai.orca",
"stateSchemaVersion": 1,
"readableStateSchemaVersions": [1],
"daemonProtocolVersion": 56,
"daemonProtocolVersion": 57,
"attachableDaemonProtocolVersions": [
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
51, 52, 53, 54, 55, 56
51, 52, 53, 54, 55, 56, 57
]
}
@@ -3,10 +3,10 @@ export const LOCAL_BUILD_COMPATIBILITY_CONTRACT = {
appId: 'com.stablyai.orca',
stateSchemaVersion: 1,
readableStateSchemaVersions: [1],
daemonProtocolVersion: 56,
daemonProtocolVersion: 57,
attachableDaemonProtocolVersions: [
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
51, 52, 53, 54, 55, 56
51, 52, 53, 54, 55, 56, 57
]
} as const