From f730fd05a171cb147b48ec4bbb721364c88b027c Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Mon, 16 Nov 2026 18:51:45 -0800 Subject: [PATCH] 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 --- .../protocol/daemon-protocol-version.ts | 5 +- .../daemon/sessions/session-output-plane.ts | 7 +- ...session-output-sequence-pre-attach.test.ts | 64 +++++++++++++++++++ .../local-build-compatibility-contract.json | 4 +- .../local-build-compatibility-contract.ts | 4 +- 5 files changed, 77 insertions(+), 7 deletions(-) create mode 100644 src/main/daemon/sessions/testing/session-output-sequence-pre-attach.test.ts diff --git a/src/main/daemon/protocol/daemon-protocol-version.ts b/src/main/daemon/protocol/daemon-protocol-version.ts index ddb82c4e367..0a492795235 100644 --- a/src/main/daemon/protocol/daemon-protocol-version.ts +++ b/src/main/daemon/protocol/daemon-protocol-version.ts @@ -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 { diff --git a/src/main/daemon/sessions/session-output-plane.ts b/src/main/daemon/sessions/session-output-plane.ts index bbe8682c763..e077f008bd2 100644 --- a/src/main/daemon/sessions/session-output-plane.ts +++ b/src/main/daemon/sessions/session-output-plane.ts @@ -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): 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 diff --git a/src/main/daemon/sessions/testing/session-output-sequence-pre-attach.test.ts b/src/main/daemon/sessions/testing/session-output-sequence-pre-attach.test.ts new file mode 100644 index 00000000000..20d75e6e189 --- /dev/null +++ b/src/main/daemon/sessions/testing/session-output-sequence-pre-attach.test.ts @@ -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() + }) +}) diff --git a/src/shared/remote-runtime/orcad-distribution/local-build-compatibility-contract.json b/src/shared/remote-runtime/orcad-distribution/local-build-compatibility-contract.json index 0cc7c9f4b0f..0fccd4f93ef 100644 --- a/src/shared/remote-runtime/orcad-distribution/local-build-compatibility-contract.json +++ b/src/shared/remote-runtime/orcad-distribution/local-build-compatibility-contract.json @@ -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 ] } diff --git a/src/shared/remote-runtime/orcad-distribution/local-build-compatibility-contract.ts b/src/shared/remote-runtime/orcad-distribution/local-build-compatibility-contract.ts index a208d577989..10c7d4110c9 100644 --- a/src/shared/remote-runtime/orcad-distribution/local-build-compatibility-contract.ts +++ b/src/shared/remote-runtime/orcad-distribution/local-build-compatibility-contract.ts @@ -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