From 8df95dc311890e6e80e7de5774b3f94a0d5ca760 Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Sat, 30 May 2026 13:21:34 -0700 Subject: [PATCH] perf: skip stale replay concat for large pty chunks (#3915) --- src/main/runtime/orca-runtime.test.ts | 17 ++++++++++++++++- src/main/runtime/orca-runtime.ts | 9 ++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/main/runtime/orca-runtime.test.ts b/src/main/runtime/orca-runtime.test.ts index 3bf31ce9d..3071f1605 100644 --- a/src/main/runtime/orca-runtime.test.ts +++ b/src/main/runtime/orca-runtime.test.ts @@ -26,7 +26,12 @@ import { import { getBranchConflictKind, getDefaultBaseRef } from '../git/repo' import type { OrchestrationDb } from './orchestration/db' import type { MessagePriority, MessageRow, MessageType } from './orchestration/types' -import { appendNormalizedToTailBuffer, buildPreview, OrcaRuntimeService } from './orca-runtime' +import { + appendNormalizedToTailBuffer, + appendRecentPtyOutput, + buildPreview, + OrcaRuntimeService +} from './orca-runtime' import { registerSshFilesystemProvider, unregisterSshFilesystemProvider @@ -3964,6 +3969,16 @@ describe('OrcaRuntimeService', () => { expect(mapCallCount).toBe(0) }) + it('keeps recent PTY replay output capped without needing previous data for large chunks', () => { + const previous = 'old-output'.repeat(1000) + const data = 'new-output'.repeat(1000) + const expected = `${previous}${data}`.slice(-4096) + + expect(appendRecentPtyOutput(previous, 'tail')).toBe(`${previous}tail`.slice(-4096)) + expect(appendRecentPtyOutput(previous, data)).toBe(expected) + expect(appendRecentPtyOutput(undefined, data)).toBe(data.slice(-4096)) + }) + it('bounds retained partial terminal output before preview reads', async () => { const runtime = new OrcaRuntimeService(store) diff --git a/src/main/runtime/orca-runtime.ts b/src/main/runtime/orca-runtime.ts index d8ab826a2..b98470ae0 100644 --- a/src/main/runtime/orca-runtime.ts +++ b/src/main/runtime/orca-runtime.ts @@ -2257,7 +2257,7 @@ export class OrcaRuntimeService { this.ptyOutputSequenceById.set(ptyId, outputSequence) this.recentPtyOutputById.set( ptyId, - `${this.recentPtyOutputById.get(ptyId) ?? ''}${data}`.slice(-RECENT_PTY_OUTPUT_LIMIT) + appendRecentPtyOutput(this.recentPtyOutputById.get(ptyId), data) ) // Agent detection runs on raw data before leaf processing, since the // tail buffer logic normalizes away the OSC sequences we need. @@ -12665,6 +12665,13 @@ function withTimeoutResult( }) } +export function appendRecentPtyOutput(previous: string | undefined, data: string): string { + if (data.length >= RECENT_PTY_OUTPUT_LIMIT) { + return data.slice(-RECENT_PTY_OUTPUT_LIMIT) + } + return `${previous ?? ''}${data}`.slice(-RECENT_PTY_OUTPUT_LIMIT) +} + export function buildPreview(lines: string[], partialLine: string): string { const previewLines: string[] = [] const collectVisibleLine = (line: string): void => {