diff --git a/config/scripts/check-focus-read-ratchet.mjs b/config/scripts/check-focus-read-ratchet.mjs index 3af9936ffb53..6ba68dc41279 100644 --- a/config/scripts/check-focus-read-ratchet.mjs +++ b/config/scripts/check-focus-read-ratchet.mjs @@ -355,10 +355,7 @@ const ADVICE = "Route by the resource's owner. Only a creation flow with no source row may use defaultCreationHost." export function isScannedPath(rel) { - return ( - /\.(ts|tsx)$/.test(rel) && - !/\.(test|spec)(?:-fixture)?\.tsx?$/.test(rel) - ) + return /\.(ts|tsx)$/.test(rel) && !/\.(test|spec)(?:-fixture)?\.tsx?$/.test(rel) } // One read and one discovery per root per run; `--prune` then `main` reuse them. diff --git a/src/main/ipc/pty-launch-parity.test-fixture.ts b/src/main/ipc/pty-launch-parity.test-fixture.ts index bce4dcc2ebc9..bfea858cebc7 100644 --- a/src/main/ipc/pty-launch-parity.test-fixture.ts +++ b/src/main/ipc/pty-launch-parity.test-fixture.ts @@ -150,7 +150,9 @@ export function startParityLanes( // A folder's contained repo is saved too, so the host can borrow its runtime if it would. const repo = scopeRepo ?? - (args.workspace.repoPath ? launchScope({ kind: 'repo', path: args.workspace.repoPath }).repo : null) + (args.workspace.repoPath + ? launchScope({ kind: 'repo', path: args.workspace.repoPath }).repo + : null) const store = { getSettings: () => settings, getProjects: () => [ diff --git a/src/main/ipc/worktrees-canonical-ssh-listing.test.ts b/src/main/ipc/worktrees-canonical-ssh-listing.test.ts index c80ee295c3db..7f174639a1ae 100644 --- a/src/main/ipc/worktrees-canonical-ssh-listing.test.ts +++ b/src/main/ipc/worktrees-canonical-ssh-listing.test.ts @@ -147,71 +147,84 @@ describe.each([ }) }) -it.each(['git', 'folder'] as const)('projects exact known SSH %s endpoints without building foreign repos', async (kind) => { - const { repo, store } = fixture({ connectionId: 'host-a' }) - const parentInstance = '11111111-2222-3333-4444-555555555555' - const parentPath = `/remote/parent::workspace:${parentInstance}` - const parentRepo = hydrateRepo( - { ...repo, id: 'parent-repo', kind, path: kind === 'folder' ? '/remote/parent' : parentPath, displayName: 'parent' }, - new Map() - ) - const childId = 'repo::/remote/repository' - const parentId = `parent-repo::${parentPath}` - const metaById: Record = { - [childId]: mergeWorktreeMetaForWrite(undefined, { - hostId: 'ssh:host-a', - instanceId: 'child-instance', - projectId: 'child-project' - }), - [parentId]: mergeWorktreeMetaForWrite(undefined, { - hostId: 'ssh:host-a', - instanceId: parentInstance, - projectId: 'parent-project' +it.each(['git', 'folder'] as const)( + 'projects exact known SSH %s endpoints without building foreign repos', + async (kind) => { + const { repo, store } = fixture({ connectionId: 'host-a' }) + const parentInstance = '11111111-2222-3333-4444-555555555555' + const parentPath = `/remote/parent::workspace:${parentInstance}` + const parentRepo = hydrateRepo( + { + ...repo, + id: 'parent-repo', + kind, + path: kind === 'folder' ? '/remote/parent' : parentPath, + displayName: 'parent' + }, + new Map() + ) + const childId = 'repo::/remote/repository' + const parentId = `parent-repo::${parentPath}` + const metaById: Record = { + [childId]: mergeWorktreeMetaForWrite(undefined, { + hostId: 'ssh:host-a', + instanceId: 'child-instance', + projectId: 'child-project' + }), + [parentId]: mergeWorktreeMetaForWrite(undefined, { + hostId: 'ssh:host-a', + instanceId: parentInstance, + projectId: 'parent-project' + }) + } + if (kind === 'folder') { + metaById[parentId].instanceId = undefined + } + store.setWorktreeMeta.mockImplementation((id: string, updates: Partial) => { + const meta = mergeWorktreeMetaForWrite(metaById[id], updates) + metaById[id] = meta + return meta }) - } - if (kind === 'folder') { - metaById[parentId].instanceId = undefined - } - store.setWorktreeMeta.mockImplementation((id: string, updates: Partial) => { - const meta = mergeWorktreeMetaForWrite(metaById[id], updates) - metaById[id] = meta - return meta - }) - Object.assign(store, { - getRepos: () => [repo, parentRepo], - getAllWorktreeMeta: () => metaById, - getWorktreeMeta: (id: string) => metaById[id], - getAllWorktreeLineage: () => ({ - [childId]: { - worktreeId: childId, - worktreeInstanceId: 'child-instance', - parentWorktreeId: parentId, - parentWorktreeInstanceId: parentInstance, - origin: 'cli', - capture: { source: 'explicit-cli-flag', confidence: 'explicit' }, - createdAt: 1 - } + Object.assign(store, { + getRepos: () => [repo, parentRepo], + getAllWorktreeMeta: () => metaById, + getWorktreeMeta: (id: string) => metaById[id], + getAllWorktreeLineage: () => ({ + [childId]: { + worktreeId: childId, + worktreeInstanceId: 'child-instance', + parentWorktreeId: parentId, + parentWorktreeInstanceId: parentInstance, + origin: 'cli', + capture: { source: 'explicit-cli-flag', confidence: 'explicit' }, + createdAt: 1 + } + }) }) - }) - const child = await invoke('known') - expect(child).toMatchObject({ - status: 'complete', - result: { worktrees: [expect.objectContaining({ id: childId, parentWorktreeId: parentId })] } - }) - expect(store.setWorktreeMeta).not.toHaveBeenCalledWith(parentId, expect.anything()) - const parent = mocks.handlers.get('worktrees:listKnownForExecutionHost')!(null, { - repoId: parentRepo.id, - executionHostId: 'ssh:host-a' - }) - expect(parent).toMatchObject({ - status: 'complete', - result: { worktrees: [ - ...(kind === 'folder' ? [expect.objectContaining({ id: 'parent-repo::/remote/parent' })] : []), - expect.objectContaining({ id: parentId, childWorktreeIds: [childId] }) - ] } - }) -}) + const child = await invoke('known') + expect(child).toMatchObject({ + status: 'complete', + result: { worktrees: [expect.objectContaining({ id: childId, parentWorktreeId: parentId })] } + }) + expect(store.setWorktreeMeta).not.toHaveBeenCalledWith(parentId, expect.anything()) + const parent = mocks.handlers.get('worktrees:listKnownForExecutionHost')!(null, { + repoId: parentRepo.id, + executionHostId: 'ssh:host-a' + }) + expect(parent).toMatchObject({ + status: 'complete', + result: { + worktrees: [ + ...(kind === 'folder' + ? [expect.objectContaining({ id: 'parent-repo::/remote/parent' })] + : []), + expect.objectContaining({ id: parentId, childWorktreeIds: [childId] }) + ] + } + }) + } +) it.each([undefined, 'nested'])( 'keeps runtime rows out of local cache and root registration: %s', diff --git a/src/main/runtime/rpc/methods/terminal/stream-schemas.ts b/src/main/runtime/rpc/methods/terminal/stream-schemas.ts index d2d562b67e79..afca8964a31d 100644 --- a/src/main/runtime/rpc/methods/terminal/stream-schemas.ts +++ b/src/main/runtime/rpc/methods/terminal/stream-schemas.ts @@ -18,6 +18,7 @@ export const TerminalMultiplexSubscribeFrame = TerminalHandle.extend({ ackOutputSourceRanges: z.literal(1).optional(), desktopViewportClaims: z.literal(1).optional(), outputPause: z.literal(1).optional(), + outputPauseDropNotice: z.literal(1).optional(), writeUnavailable: z.literal(1).optional(), inputAck: z.literal(1).optional() }) diff --git a/src/main/runtime/rpc/methods/terminal/terminal-multiplex-frame-delivery.ts b/src/main/runtime/rpc/methods/terminal/terminal-multiplex-frame-delivery.ts index 7e931445cf08..e673379edde5 100644 --- a/src/main/runtime/rpc/methods/terminal/terminal-multiplex-frame-delivery.ts +++ b/src/main/runtime/rpc/methods/terminal/terminal-multiplex-frame-delivery.ts @@ -6,6 +6,7 @@ import { encodeTerminalStreamText } from '../../../../../shared/terminal-stream-protocol' import { appendAckPendingOutput } from './terminal-stream-replay' +import { notePausedOutputDropped } from './terminal-multiplex-paused-output-drop' import { isMultiplexStreamAttached, type TerminalMultiplexConnection @@ -121,7 +122,11 @@ export function installMultiplexFrameDelivery(state: TerminalMultiplexConnection stream: TerminalMultiplexStream, chunk: TerminalOutputFrameChunk ): void => { - if (!isMultiplexStreamAttached(state, stream) || stream.outputPaused) { + if (!isMultiplexStreamAttached(state, stream)) { + return + } + if (stream.outputPaused) { + notePausedOutputDropped(state, stream) return } if ( diff --git a/src/main/runtime/rpc/methods/terminal/terminal-multiplex-initial-snapshot.ts b/src/main/runtime/rpc/methods/terminal/terminal-multiplex-initial-snapshot.ts index 7b56a66ce89a..f027d5498dd1 100644 --- a/src/main/runtime/rpc/methods/terminal/terminal-multiplex-initial-snapshot.ts +++ b/src/main/runtime/rpc/methods/terminal/terminal-multiplex-initial-snapshot.ts @@ -65,6 +65,7 @@ export async function publishMultiplexInitialSnapshot( ...((stream.supportsOutputPause || stream.inputSessionId !== null) && { capabilities: { ...(stream.supportsOutputPause ? { outputPause: 1 as const } : {}), + ...(stream.supportsOutputPauseDropNotice ? { outputPauseDropNotice: 1 as const } : {}), ...(stream.inputSessionId !== null ? { inputAck: 1 as const } : {}) } }), diff --git a/src/main/runtime/rpc/methods/terminal/terminal-multiplex-paused-output-drop.ts b/src/main/runtime/rpc/methods/terminal/terminal-multiplex-paused-output-drop.ts new file mode 100644 index 000000000000..c485e48d2440 --- /dev/null +++ b/src/main/runtime/rpc/methods/terminal/terminal-multiplex-paused-output-drop.ts @@ -0,0 +1,16 @@ +import { TerminalStreamOpcode } from '../../../../../shared/terminal-stream-protocol' +import type { TerminalMultiplexConnection } from './terminal-multiplex-connection' +import type { TerminalMultiplexStream } from './terminal-stream-types' + +/** Tells a negotiated client that this pause discarded output, so its reveal must restore. */ +export function notePausedOutputDropped( + state: Pick, + stream: TerminalMultiplexStream +): void { + if (!stream.supportsOutputPauseDropNotice || stream.pausedOutputDropNoticeSent) { + return + } + // Why mark before send: a failed send means the link is going down, and resubscribe repaints anyway. + stream.pausedOutputDropNoticeSent = true + state.sendFrame(stream.streamId, TerminalStreamOpcode.PausedOutputDropped) +} diff --git a/src/main/runtime/rpc/methods/terminal/terminal-multiplex-slot-frames.ts b/src/main/runtime/rpc/methods/terminal/terminal-multiplex-slot-frames.ts index d01e8dab3171..ae7b93278636 100644 --- a/src/main/runtime/rpc/methods/terminal/terminal-multiplex-slot-frames.ts +++ b/src/main/runtime/rpc/methods/terminal/terminal-multiplex-slot-frames.ts @@ -18,6 +18,7 @@ import { serializeBudgetedRequestedSnapshot } from './terminal-snapshot-publication' import { updateViewportForClient } from './terminal-viewport-update' +import { notePausedOutputDropped } from './terminal-multiplex-paused-output-drop' import { isMultiplexStreamAttached, type MultiplexSnapshotRequest, @@ -80,10 +81,17 @@ export function installMultiplexSlotFrames(state: TerminalMultiplexConnection): } stream.outputPaused = payload.paused if (stream.outputPaused) { + stream.pausedOutputDropNoticeSent = false + const discardsQueuedOutput = + stream.ackPendingOutput.length > 0 || stream.ackPendingOutputOverflowed + // Why: batched bytes flush into the paused gate, which reports them as dropped. stream.outputBatcher.flush() stream.ackPendingOutput = [] stream.ackPendingOutputBytes = 0 stream.ackPendingOutputOverflowed = false + if (discardsQueuedOutput) { + notePausedOutputDropped(state, stream) + } } return } diff --git a/src/main/runtime/rpc/methods/terminal/terminal-multiplex-stream-initialization.ts b/src/main/runtime/rpc/methods/terminal/terminal-multiplex-stream-initialization.ts index 6c0da16cf875..48df4c9e7216 100644 --- a/src/main/runtime/rpc/methods/terminal/terminal-multiplex-stream-initialization.ts +++ b/src/main/runtime/rpc/methods/terminal/terminal-multiplex-stream-initialization.ts @@ -7,6 +7,7 @@ import { TERMINAL_MULTIPLEX_ACK_STREAM_INITIAL_WINDOW_BYTES } from '../../../../ import { createTerminalOutputBatcher } from './terminal-output-batcher' import { appendPendingMultiplexOutput } from './terminal-stream-replay' import { updateViewportForClient } from './terminal-viewport-update' +import { notePausedOutputDropped } from './terminal-multiplex-paused-output-drop' import { isMultiplexStreamAttached, type MultiplexSubscribeRequest, @@ -33,6 +34,9 @@ export async function initializeMultiplexStream( ackInFlightBytes: 0, ackWindowBytes: TERMINAL_MULTIPLEX_ACK_STREAM_INITIAL_WINDOW_BYTES, supportsOutputPause: request.capabilities?.outputPause === 1, + supportsOutputPauseDropNotice: + request.capabilities?.outputPause === 1 && request.capabilities.outputPauseDropNotice === 1, + pausedOutputDropNoticeSent: false, supportsWriteUnavailable: request.capabilities?.writeUnavailable === 1, inputSessionId: request.capabilities?.inputAck === 1 ? (request.inputSessionId ?? null) : null, outputPaused: false, @@ -84,6 +88,7 @@ export async function initializeMultiplexStream( return } if (stream.outputPaused) { + notePausedOutputDropped(state, stream) return } if (stream.buffering) { diff --git a/src/main/runtime/rpc/methods/terminal/terminal-stream-types.ts b/src/main/runtime/rpc/methods/terminal/terminal-stream-types.ts index fb102dd76666..8fdaccc6a938 100644 --- a/src/main/runtime/rpc/methods/terminal/terminal-stream-types.ts +++ b/src/main/runtime/rpc/methods/terminal/terminal-stream-types.ts @@ -63,6 +63,9 @@ export type TerminalMultiplexStream = { ackInFlightBytes: number ackWindowBytes: number supportsOutputPause: boolean + // Why: lets the client skip the reveal snapshot when a pause discarded nothing. + supportsOutputPauseDropNotice: boolean + pausedOutputDropNoticeSent: boolean supportsWriteUnavailable: boolean // Set only when the client negotiated `inputAck`; sequenced Input frames are then deduped and acked. inputSessionId: string | null diff --git a/src/main/runtime/rpc/terminal-multiplex-paused-output-drop.test.ts b/src/main/runtime/rpc/terminal-multiplex-paused-output-drop.test.ts new file mode 100644 index 000000000000..361404e4d425 --- /dev/null +++ b/src/main/runtime/rpc/terminal-multiplex-paused-output-drop.test.ts @@ -0,0 +1,112 @@ +import './unused-default-rpc-methods.test-fixture' +import { describe, expect, it, vi } from 'vitest' +import type { RuntimeTerminalDataMeta } from '../orca-runtime' +import { + TerminalStreamOpcode, + decodeTerminalStreamFrame, + encodeTerminalStreamFrame, + encodeTerminalStreamJson +} from '../../../shared/terminal-stream-protocol' +import { startDesktopMultiplexSubscribe } from './terminal-multiplex-test-harness' + +async function subscribePausable(capabilities: Record) { + const listeners: ((data: string, meta?: RuntimeTerminalDataMeta) => void)[] = [] + const harness = startDesktopMultiplexSubscribe({ + subscribeToTerminalData: vi.fn((_ptyId, listener) => { + listeners.push(listener) + return vi.fn() + }) + }) + await vi.waitFor(() => expect(harness.handlers.has(0)).toBe(true)) + harness.handlers.get(0)?.( + decodeTerminalStreamFrame( + encodeTerminalStreamFrame({ + opcode: TerminalStreamOpcode.Subscribe, + streamId: 0, + seq: 1, + payload: encodeTerminalStreamJson({ + streamId: 1, + terminal: 'terminal-1', + client: { id: 'desktop-1', type: 'desktop' }, + capabilities + }) + }) + )! + ) + const subscribedEvent = () => + harness.messages + .map((message) => JSON.parse(message).result) + .find((event) => event?.type === 'subscribed' && event.streamId === 1) + await vi.waitFor(() => expect(subscribedEvent()).toBeDefined()) + let seq = 0 + let frameSeq = 10 + const setPaused = (paused: boolean): void => + harness.handlers.get(1)?.( + decodeTerminalStreamFrame( + encodeTerminalStreamFrame({ + opcode: TerminalStreamOpcode.SetOutputPaused, + streamId: 1, + seq: frameSeq++, + payload: encodeTerminalStreamJson({ paused }) + }) + )! + ) + const emitOutput = (text: string): void => { + seq += text.length + listeners[0]?.(text, { seq, rawLength: text.length }) + } + const dropNotices = (): number => + harness.binaryFrames.filter( + (bytes) => + decodeTerminalStreamFrame(bytes)?.opcode === TerminalStreamOpcode.PausedOutputDropped + ).length + const close = async (): Promise => { + harness.registry.cleanupSubscription('terminal-multiplex:conn-desktop-first-paint') + await harness.dispatchPromise + } + return { subscribed: subscribedEvent(), setPaused, emitOutput, dropNotices, close } +} + +describe('terminal multiplex paused-output drop notice', () => { + it('announces a pause only when it actually discarded output', async () => { + const host = await subscribePausable({ ackOutput: 1, outputPause: 1, outputPauseDropNotice: 1 }) + expect(host.subscribed.capabilities).toMatchObject({ outputPause: 1, outputPauseDropNotice: 1 }) + + host.setPaused(true) + host.setPaused(false) + expect(host.dropNotices()).toBe(0) + + host.setPaused(true) + host.emitOutput('hidden-1') + host.emitOutput('hidden-2') + expect(host.dropNotices()).toBe(1) + host.setPaused(false) + host.emitOutput('visible') + expect(host.dropNotices()).toBe(1) + + // Each pause reports independently. + host.setPaused(true) + host.emitOutput('hidden-3') + expect(host.dropNotices()).toBe(2) + await host.close() + }) + + it('announces output still batched when the pause lands', async () => { + const host = await subscribePausable({ ackOutput: 1, outputPause: 1, outputPauseDropNotice: 1 }) + host.emitOutput('batched-before-pause') + host.setPaused(true) + expect(host.dropNotices()).toBe(1) + await host.close() + }) + + it('never sends the frame to a client that did not negotiate it', async () => { + const host = await subscribePausable({ ackOutput: 1, outputPause: 1 }) + expect(host.subscribed.capabilities?.outputPauseDropNotice).toBeUndefined() + + host.setPaused(true) + host.emitOutput('hidden') + host.setPaused(false) + expect(host.dropNotices()).toBe(0) + await host.close() + }) +}) diff --git a/src/main/worktree-lineage-host-peers.ts b/src/main/worktree-lineage-host-peers.ts index 22658c5a34cc..b001b9d23d80 100644 --- a/src/main/worktree-lineage-host-peers.ts +++ b/src/main/worktree-lineage-host-peers.ts @@ -66,7 +66,8 @@ export function projectWorktreeLineageWithHostPeers( if ( evidence.some((owner) => owner.status !== 'owned') || hostOwners.length !== 1 || - !meta || (!meta.instanceId && !isFolderRepo(hostOwners[0])) || + !meta || + (!meta.instanceId && !isFolderRepo(hostOwners[0])) || (!meta.hostId && owners.length > 1) ) { continue diff --git a/src/renderer/src/components/new-workspace/ComposerParentWorktreePicker.tsx b/src/renderer/src/components/new-workspace/ComposerParentWorktreePicker.tsx index 907a18eb0484..74c6216ba61b 100644 --- a/src/renderer/src/components/new-workspace/ComposerParentWorktreePicker.tsx +++ b/src/renderer/src/components/new-workspace/ComposerParentWorktreePicker.tsx @@ -392,7 +392,9 @@ function ParentWorktreeCandidateList({ {[ repoMap.get(candidate.repoId)?.displayName, branchDisplayName(candidate.branch) - ].filter(Boolean).join(' · ')} + ] + .filter(Boolean) + .join(' · ')} diff --git a/src/renderer/src/components/sidebar/DeleteWorktreeDialog.test.tsx b/src/renderer/src/components/sidebar/DeleteWorktreeDialog.test.tsx index 179c6b288cd7..3c295bc27db9 100644 --- a/src/renderer/src/components/sidebar/DeleteWorktreeDialog.test.tsx +++ b/src/renderer/src/components/sidebar/DeleteWorktreeDialog.test.tsx @@ -323,11 +323,20 @@ describe('DeleteWorktreeDialog lineage copy', () => { const parent = { ...makeWorktree('parent', '/parent'), repoId: repo.id } const child = { ...makeWorktree('child', '/child'), repoId: checkout.id } const { DeleteWorktreeLineageNotice } = await import('./DeleteWorktreeLineageNotice') - const markup = renderToStaticMarkup() + const markup = renderToStaticMarkup( + + ) expect(visibleMarkupText(markup)).toContain('(Second checkout)') expect(visibleMarkupText(markup)).toContain('child · Second checkout') }) diff --git a/src/renderer/src/components/sidebar/agent-send-picker-collapse.test.tsx b/src/renderer/src/components/sidebar/agent-send-picker-collapse.test.tsx index 53e244789a37..f703300dbc88 100644 --- a/src/renderer/src/components/sidebar/agent-send-picker-collapse.test.tsx +++ b/src/renderer/src/components/sidebar/agent-send-picker-collapse.test.tsx @@ -182,7 +182,10 @@ it.each([true, false])('reveals the target with pinned ancestors visible=%s', (v capture: { source: 'orchestration-context', confidence: 'explicit' }, createdAt: 1 }) - const worktreeLineageById = { [parent.id]: lineage(parent, root), [child.id]: lineage(child, parent) } + const worktreeLineageById = { + [parent.id]: lineage(parent, root), + [child.id]: lineage(child, parent) + } const collapsedGroups = new Set([ 'host:ssh:builder', 'lineage:ssh:builder|remote', diff --git a/src/renderer/src/components/sidebar/pinned-section-worktrees.test.ts b/src/renderer/src/components/sidebar/pinned-section-worktrees.test.ts index 97eef4f075b9..a0bbc0a4f251 100644 --- a/src/renderer/src/components/sidebar/pinned-section-worktrees.test.ts +++ b/src/renderer/src/components/sidebar/pinned-section-worktrees.test.ts @@ -110,17 +110,41 @@ describe('getPinnedSectionWorktrees', () => { const visible = { ...grandchild, repoId: 'visible-project' } const map = new Map([pinnedParent, hidden, visible].map((row) => [row.id, row])) const allowed = [pinnedParent.repoId, visible.repoId] - expect(getPinnedSectionWorktrees([pinnedParent, visible], lineageById, map, allowed)) - .toEqual([pinnedParent]) - expect(getPinnedSectionWorktrees([pinnedParent, visible], lineageById, map)) - .toEqual([pinnedParent, visible]) + expect(getPinnedSectionWorktrees([pinnedParent, visible], lineageById, map, allowed)).toEqual([ + pinnedParent + ]) + expect(getPinnedSectionWorktrees([pinnedParent, visible], lineageById, map)).toEqual([ + pinnedParent, + visible + ]) const rows = buildRows( - 'repo', [pinnedParent, visible], repoMap, null, new Set(), undefined, undefined, - undefined, lineageById, map, true, undefined, [], new Set(), new Map(), new Map(), - [], undefined, [], undefined, 'local', 'single-location', allowed + 'repo', + [pinnedParent, visible], + repoMap, + null, + new Set(), + undefined, + undefined, + undefined, + lineageById, + map, + true, + undefined, + [], + new Set(), + new Map(), + new Map(), + [], + undefined, + [], + undefined, + 'local', + 'single-location', + allowed + ) + expect(rows.find((row) => row.type === 'item' && row.worktree.id === visible.id)).toMatchObject( + { sectionKey: 'repo:visible-project', depth: 0 } ) - expect(rows.find((row) => row.type === 'item' && row.worktree.id === visible.id)) - .toMatchObject({ sectionKey: 'repo:visible-project', depth: 0 }) }) it('does not collect descendants of a pinned parent that is not itself visible', () => { diff --git a/src/renderer/src/components/sidebar/pinned-section-worktrees.ts b/src/renderer/src/components/sidebar/pinned-section-worktrees.ts index 86135c9db486..963ef49b7608 100644 --- a/src/renderer/src/components/sidebar/pinned-section-worktrees.ts +++ b/src/renderer/src/components/sidebar/pinned-section-worktrees.ts @@ -57,7 +57,10 @@ export function getPinnedSectionWorktrees( rows: worktrees, isPinned: (worktree) => worktree.isPinned === true, childrenByParentIdentity: getSidebarLineageChildren( - worktrees, lineageById, worktreeMap, filterRepoIds + worktrees, + lineageById, + worktreeMap, + filterRepoIds ), identityOf: getWorktreeHostIdentity, duplicatePinned: false diff --git a/src/renderer/src/components/sidebar/worktree-list/grouping/build-rows.ts b/src/renderer/src/components/sidebar/worktree-list/grouping/build-rows.ts index efa87d3e7467..6e96427562c6 100644 --- a/src/renderer/src/components/sidebar/worktree-list/grouping/build-rows.ts +++ b/src/renderer/src/components/sidebar/worktree-list/grouping/build-rows.ts @@ -28,7 +28,10 @@ import { import { getWorktreeHostIdentity } from '../../../../../../shared/worktree/host-qualified-identity' import type { OrderedGroupEntry, WorktreeGroupEntry } from './worktree-group-entry' import type { ProjectGroupingModel } from '../../../../../../shared/project-section-grouping' -import { appendProjectGroupSections, getProjectGroupOccurrencePaths } from './project-group-sections' +import { + appendProjectGroupSections, + getProjectGroupOccurrencePaths +} from './project-group-sections' import { getSidebarLineageChildren } from '../../pinned-section-worktrees' import { emitPinnedGroup } from './pinned-group-rows' import { @@ -103,29 +106,35 @@ export function buildSidebarRowPlan( : new Map(), isPinned: (worktree) => worktree.isPinned === true, duplicatePinned: pinnedDisplayPolicy === 'duplicate-in-groups', - owningSectionOf: (worktree) => getProjectGroupingForRepo(worktree.repoId, repoMap, projectIndex), + owningSectionOf: (worktree) => + getProjectGroupingForRepo(worktree.repoId, repoMap, projectIndex), projectIdOf: (worktree) => worktree.repoId, sectionsOf: ({ pinned, natural, displayRootByIdentity }) => { const groups: OrderedGroupEntry[] = groupBy === 'none' - ? [[ALL_GROUP_KEY, { label: ALL_GROUP_META.label, items: [...natural], repoIds: new Set() }]] + ? [ + [ + ALL_GROUP_KEY, + { label: ALL_GROUP_META.label, items: [...natural], repoIds: new Set() } + ] + ] : buildOrderedGroups({ - groupBy, - naturalWorktrees: natural, - repoMap, - prCache, - settings, - workspaceStatuses, - projectIndex, - displayRootByIdentity, - placeholderRepoIds, - importedWorktreesByRepo, - newExternalWorktreesInboxByRepo, - pendingByRepo, - repoOrder, - projectOrderBy, - folderWorkspaces: renderableFolderWorkspaces - }) + groupBy, + naturalWorktrees: natural, + repoMap, + prCache, + settings, + workspaceStatuses, + projectIndex, + displayRootByIdentity, + placeholderRepoIds, + importedWorktreesByRepo, + newExternalWorktreesInboxByRepo, + pendingByRepo, + repoOrder, + projectOrderBy, + folderWorkspaces: renderableFolderWorkspaces + }) if (pinned.length > 0) { groups.unshift([ PINNED_GROUP_KEY, diff --git a/src/renderer/src/components/sidebar/worktree-list/grouping/cross-project-lineage.test.ts b/src/renderer/src/components/sidebar/worktree-list/grouping/cross-project-lineage.test.ts index f74a0ddca305..05fdb59129b1 100644 --- a/src/renderer/src/components/sidebar/worktree-list/grouping/cross-project-lineage.test.ts +++ b/src/renderer/src/components/sidebar/worktree-list/grouping/cross-project-lineage.test.ts @@ -2,11 +2,20 @@ import { describe, expect, it } from 'vitest' import type { WorktreeLineage } from '../../../../../../shared/worktree/lineage-types' import { getWorktreeHostIdentity } from '../../../../../../shared/worktree/host-qualified-identity' import type { Worktree } from '../../../../../../shared/worktree/types' -import { repo, worktree, project, projectHostSetups } from '../../worktree-list-groups-test-fixtures' +import { + repo, + worktree, + project, + projectHostSetups +} from '../../worktree-list-groups-test-fixtures' import { makeProjectGroup } from '../../folder-workspace-composer-submit-fixtures' import { getWorktreeRevealCollapsedGroupKeys } from '../navigation/worktree-reveal-group-keys' import { buildRows, buildSidebarRowPlan } from './build-rows' -import { getWorktreeLineageGroupKey, getProjectGroupHeaderKey, PINNED_GROUP_KEY } from './group-keys' +import { + getWorktreeLineageGroupKey, + getProjectGroupHeaderKey, + PINNED_GROUP_KEY +} from './group-keys' const childRepo = { ...repo, id: 'child-repo', displayName: 'Child project', path: '/tmp/child' } const repos = new Map([ @@ -63,18 +72,40 @@ describe('cross-project sidebar lineage', () => { { ...worktree, id: 'gamma', repoId: gammaRepo.id, lastActivityAt: 50 } ], new Map([...repos, [gammaRepo.id, gammaRepo]]), - null, new Set(), undefined, undefined, 'recent', lineageById, worktreeMap, true, - undefined, [], new Set(), - new Map([[childRepo.id, { repo: childRepo, hiddenWorktrees: [] }]]), new Map(), + null, + new Set(), + undefined, + undefined, + 'recent', + lineageById, + worktreeMap, + true, + undefined, + [], + new Set(), + new Map([[childRepo.id, { repo: childRepo, hiddenWorktrees: [] }]]), + new Map(), [{ creationId: 'creating-child', repoId: childRepo.id }] ) - expect(rows.filter((row) => row.type === 'header').map((row) => [row.repo?.id, row.count])) - .toEqual([[childRepo.id, 0], [gammaRepo.id, 1], [repo.id, 2]]) - expect(rows.find((row) => row.type === 'item' && row.worktree.id === child.id)) - .toMatchObject({ repo: childRepo, sectionKey: `repo:${repo.id}`, depth: 1 }) - const nextSection = rows.findIndex((row) => row.type === 'header' && row.repo?.id === gammaRepo.id) - expect(rows.slice(1, nextSection).map((row) => row.type)) - .toEqual(['imported-worktrees-card', 'pending-creation']) + expect( + rows.filter((row) => row.type === 'header').map((row) => [row.repo?.id, row.count]) + ).toEqual([ + [childRepo.id, 0], + [gammaRepo.id, 1], + [repo.id, 2] + ]) + expect(rows.find((row) => row.type === 'item' && row.worktree.id === child.id)).toMatchObject({ + repo: childRepo, + sectionKey: `repo:${repo.id}`, + depth: 1 + }) + const nextSection = rows.findIndex( + (row) => row.type === 'header' && row.repo?.id === gammaRepo.id + ) + expect(rows.slice(1, nextSection).map((row) => row.type)).toEqual([ + 'imported-worktrees-card', + 'pending-creation' + ]) expect(rows.find((row) => row.type === 'pending-creation')).toMatchObject({ repo: childRepo }) }) @@ -89,34 +120,71 @@ describe('cross-project sidebar lineage', () => { const graph = { ...lineageById, [grandchild.id]: { - ...lineage, worktreeId: grandchild.id, worktreeInstanceId: grandchild.instanceId, - parentWorktreeId: child.id, parentWorktreeInstanceId: 'child-instance' + ...lineage, + worktreeId: grandchild.id, + worktreeInstanceId: grandchild.instanceId, + parentWorktreeId: child.id, + parentWorktreeInstanceId: 'child-instance' } } const worktrees = [grandchild, child, pinnedParent] const parentSection = `project:${project.id}::setup:${repo.id}` const plan = buildSidebarRowPlan( - 'repo', worktrees, repoMap, null, new Set(), undefined, undefined, undefined, - graph, new Map(worktrees.map((row) => [row.id, row])), true, - undefined, [group], undefined, undefined, undefined, undefined, grouping, - undefined, undefined, 'local', policy + 'repo', + worktrees, + repoMap, + null, + new Set(), + undefined, + undefined, + undefined, + graph, + new Map(worktrees.map((row) => [row.id, row])), + true, + undefined, + [group], + undefined, + undefined, + undefined, + undefined, + grouping, + undefined, + undefined, + 'local', + policy + ) + const occurrence = plan.occurrencePlan.preferredByIdentity.get( + getWorktreeHostIdentity(grandchild) ) - const occurrence = plan.occurrencePlan.preferredByIdentity.get(getWorktreeHostIdentity(grandchild)) expect(occurrence).toMatchObject({ - row: { repoId: childRepo.id }, depth: 2, showProjectName: false, + row: { repoId: childRepo.id }, + depth: 2, + showProjectName: false, sectionKey: policy === 'single-location' ? PINNED_GROUP_KEY : parentSection }) expect(occurrence?.parent?.row).toBe(child) const collapsedGroups = new Set([ - PINNED_GROUP_KEY, parentSection, getProjectGroupHeaderKey(group.id), - getWorktreeLineageGroupKey(parent), getWorktreeLineageGroupKey(child) + PINNED_GROUP_KEY, + parentSection, + getProjectGroupHeaderKey(group.id), + getWorktreeLineageGroupKey(parent), + getWorktreeLineageGroupKey(child) ]) expect(plan.render(collapsedGroups).some((row) => row.type === 'item')).toBe(false) const keys = getWorktreeRevealCollapsedGroupKeys({ occurrencePlan: plan.occurrencePlan, - worktree: grandchild, worktrees, worktreeLineageById: graph, repoMap, - defaultHostId: 'local', hostScopedGroups: false, collapsedGroups, groupBy: 'repo', - pinnedDisplayPolicy: policy, prCache: null, workspaceStatuses: [], settings: null, + worktree: grandchild, + worktrees, + worktreeLineageById: graph, + repoMap, + defaultHostId: 'local', + hostScopedGroups: false, + collapsedGroups, + groupBy: 'repo', + pinnedDisplayPolicy: policy, + prCache: null, + workspaceStatuses: [], + settings: null, projectGroups: [group] }) for (const key of keys) { @@ -131,8 +199,17 @@ describe('cross-project sidebar lineage', () => { it('keeps a hidden parent child in its own section without redundant context', () => { const rows = buildRows( - 'repo', [child], repos, null, new Set(), undefined, undefined, undefined, - lineageById, worktreeMap, true + 'repo', + [child], + repos, + null, + new Set(), + undefined, + undefined, + undefined, + lineageById, + worktreeMap, + true ) expect(rows.filter((row) => row.type === 'item')).toMatchObject([ { worktree: child, sectionKey: `repo:${childRepo.id}`, depth: 0, showProjectName: false } diff --git a/src/renderer/src/components/sidebar/worktree-list/navigation/worktree-reveal-group-keys.ts b/src/renderer/src/components/sidebar/worktree-list/navigation/worktree-reveal-group-keys.ts index f9a9ce3da5eb..ffa61c81930d 100644 --- a/src/renderer/src/components/sidebar/worktree-list/navigation/worktree-reveal-group-keys.ts +++ b/src/renderer/src/components/sidebar/worktree-list/navigation/worktree-reveal-group-keys.ts @@ -39,31 +39,33 @@ export function getWorktreeRevealCollapsedGroupKeys(args: { args.repoMap.get(args.worktree.repoId), args.defaultHostId ) - const plan = args.occurrencePlan ?? buildSidebarRowPlan( - args.groupBy, - [...(args.visibleWorktrees ?? args.worktrees)], - args.repoMap, - args.prCache, - new Set(), - undefined, - args.workspaceStatuses, - undefined, - { ...args.worktreeLineageById }, - new Map(args.worktrees.map((row) => [row.id, row])), - true, - args.settings, - args.projectGroups, - undefined, - undefined, - undefined, - undefined, - args.projectGrouping, - undefined, - undefined, - args.defaultHostId, - args.pinnedDisplayPolicy, - args.filterRepoIds - ).occurrencePlan + const plan = + args.occurrencePlan ?? + buildSidebarRowPlan( + args.groupBy, + [...(args.visibleWorktrees ?? args.worktrees)], + args.repoMap, + args.prCache, + new Set(), + undefined, + args.workspaceStatuses, + undefined, + { ...args.worktreeLineageById }, + new Map(args.worktrees.map((row) => [row.id, row])), + true, + args.settings, + args.projectGroups, + undefined, + undefined, + undefined, + undefined, + args.projectGrouping, + undefined, + undefined, + args.defaultHostId, + args.pinnedDisplayPolicy, + args.filterRepoIds + ).occurrencePlan const occurrence = plan.preferredByIdentity.get(getWorktreeHostIdentity(args.worktree)) if (!occurrence) { return [] diff --git a/src/renderer/src/components/terminal-pane/pty-connection-hidden-replay.test.ts b/src/renderer/src/components/terminal-pane/pty-connection-hidden-replay.test.ts index ad9d2cd240f6..66c177b6a907 100644 --- a/src/renderer/src/components/terminal-pane/pty-connection-hidden-replay.test.ts +++ b/src/renderer/src/components/terminal-pane/pty-connection-hidden-replay.test.ts @@ -9,6 +9,7 @@ import { registerPtyConnectionHiddenTuiSnapshotReplayTests } from './pty-connect import { registerPtyConnectionStalledHiddenRestoreTests } from './pty-connection-stalled-hidden-restore.spec' import { registerPtyConnectionDaemonSnapshotReplayTests } from './pty-connection-daemon-snapshot-replay.spec' import { registerPtyConnectionDeferredReattachLiveOutputTests } from './pty-connection-deferred-reattach-live-output.spec' +import { registerPtyConnectionRemotePauseDropNoticeTests } from './pty-connection-remote-pause-drop-notice.spec' describe('pty-connection-hidden-atlas-recovery', registerPtyConnectionHiddenAtlasRecoveryTests) describe( @@ -38,3 +39,4 @@ describe( 'pty-connection-deferred-reattach-live-output', registerPtyConnectionDeferredReattachLiveOutputTests ) +describe('pty-connection-remote-pause-drop-notice', registerPtyConnectionRemotePauseDropNoticeTests) diff --git a/src/renderer/src/components/terminal-pane/pty-connection-remote-pause-drop-notice.spec.ts b/src/renderer/src/components/terminal-pane/pty-connection-remote-pause-drop-notice.spec.ts new file mode 100644 index 000000000000..40307366d2ef --- /dev/null +++ b/src/renderer/src/components/terminal-pane/pty-connection-remote-pause-drop-notice.spec.ts @@ -0,0 +1,144 @@ +import { bindPtyConnectionTestMocks } from './pty-connection-mocks.spec' +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import { flushAsyncTicks } from './pty-connection-test-async' +import { + createMockTransport, + createPane, + createManager, + type ConnectCallbacks, + type MockTransport +} from './pty-connection-test-pane-fixtures' +import { buildPaneConnectionDeps } from './pty-connection-test-deps' +import { createInitialStoreState } from './pty-connection-test-store-fixtures' +import type { StoreState } from './pty-connection-test-store-state' +import { + installTerminalTestGlobals, + restoreTerminalTestGlobals +} from './pty-connection-test-environment' + +// Why: switching back to a remote pane cost a 5000-row snapshot even when the host dropped nothing (#24863). +export function registerPtyConnectionRemotePauseDropNoticeTests(): void { + let mockStoreState: StoreState + let transportFactoryQueue: MockTransport[] = [] + let createdTransportOptions: Record[] = [] + let storeSubscribers: ((state: StoreState) => void)[] = [] + + describe('connectPanePty remote pause drop notice', () => { + beforeEach(async () => { + vi.resetModules() + vi.clearAllMocks() + transportFactoryQueue = [] + createdTransportOptions = [] + storeSubscribers = [] + mockStoreState = createInitialStoreState(() => mockStoreState) + bindPtyConnectionTestMocks({ + getState: () => mockStoreState, + getTransportQueue: () => transportFactoryQueue, + getTransportOptions: () => createdTransportOptions, + getStoreSubscribers: () => storeSubscribers, + setStoreSubscribers: (subscribers) => { + storeSubscribers = subscribers + } + }) + await installTerminalTestGlobals() + }) + + afterEach(async () => { + await restoreTerminalTestGlobals() + }) + + async function connectRemotePane(dropsReported: boolean) { + const { connectPanePty } = await import('./pty-connection') + const remotePtyId = 'remote:env-1@@terminal-1' + const transport = createMockTransport(remotePtyId) + const callbacks: { current: ConnectCallbacks } = { current: {} } + transport.connect.mockImplementation(async (options: { callbacks?: ConnectCallbacks }) => { + callbacks.current = options.callbacks ?? {} + return remotePtyId + }) + transport.setOutputPaused = vi.fn((paused: boolean) => { + callbacks.current.onOutputPauseChanged?.(paused, true, dropsReported) + return true + }) + transport.serializeBuffer = vi.fn().mockResolvedValue({ + data: 'host snapshot\r\n', + cols: 100, + rows: 30, + seq: 10, + source: 'headless' + }) + transportFactoryQueue.push(transport) + mockStoreState.repos = [ + { id: 'repo1', connectionId: null, displayName: 'orca', executionHostId: 'runtime:env-1' } + ] + mockStoreState.worktreesByRepo.repo1[0].runtimeOwnerEnvironmentId = 'env-1' + const pane = createPane(1) + const deps = buildPaneConnectionDeps(() => mockStoreState, { + isVisibleRef: { current: true } + }) + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the fixtures model the pane, manager and deps surface connectPanePty reads. + const binding = connectPanePty(pane as never, createManager(1) as never, deps as never) + await flushAsyncTicks(6) + const setVisible = async (visible: boolean): Promise => { + deps.isVisibleRef.current = visible + binding.syncProcessTracking() + await flushAsyncTicks(20) + } + const written = (): string => + pane.terminal.write.mock.calls.map((call) => String(call[0])).join('') + return { transport, callbacks, binding, setVisible, written } + } + + it('switches back to a quiet paused pane without fetching a snapshot', async () => { + const { transport, callbacks, binding, setVisible, written } = await connectRemotePane(true) + + for (let switchIndex = 0; switchIndex < 3; switchIndex += 1) { + await setVisible(false) + expect(transport.setOutputPaused).toHaveBeenLastCalledWith(true) + await setVisible(true) + expect(transport.setOutputPaused).toHaveBeenLastCalledWith(false) + } + const live = 'live after reveal\r\n' + callbacks.current.onData?.(live, { seq: 10 + live.length, rawLength: live.length }) + await flushAsyncTicks(20) + + expect(transport.serializeBuffer).not.toHaveBeenCalled() + expect(written()).toContain(live) + binding.dispose() + }) + + it('restores exactly once when the host reports a paused drop', async () => { + const { transport, callbacks, binding, setVisible, written } = await connectRemotePane(true) + + await setVisible(false) + callbacks.current.onPausedOutputDropped?.() + await flushAsyncTicks(6) + expect(transport.serializeBuffer).not.toHaveBeenCalled() + await setVisible(true) + const live = 'live after restore\r\n' + callbacks.current.onData?.(live, { seq: 10 + live.length, rawLength: live.length }) + await flushAsyncTicks(20) + + expect(transport.serializeBuffer).toHaveBeenCalledTimes(1) + const text = written() + expect(text.split('host snapshot').length - 1).toBe(1) + expect(text.split(live).length - 1).toBe(1) + + // The next quiet switch is free again. + await setVisible(false) + await setVisible(true) + expect(transport.serializeBuffer).toHaveBeenCalledTimes(1) + binding.dispose() + }) + + it('still restores on every reveal when the host cannot report drops', async () => { + const { transport, binding, setVisible } = await connectRemotePane(false) + + await setVisible(false) + await setVisible(true) + + expect(transport.serializeBuffer).toHaveBeenCalledTimes(1) + binding.dispose() + }) + }) +} diff --git a/src/renderer/src/components/terminal-pane/pty-connection-test-pane-fixtures.ts b/src/renderer/src/components/terminal-pane/pty-connection-test-pane-fixtures.ts index 0959c996bfd9..9a6ec1be7348 100644 --- a/src/renderer/src/components/terminal-pane/pty-connection-test-pane-fixtures.ts +++ b/src/renderer/src/components/terminal-pane/pty-connection-test-pane-fixtures.ts @@ -21,7 +21,8 @@ export type ConnectCallbacks = { onError?: (msg: string) => void onErrorCleared?: (msg: string) => void onWriteUnavailable?: () => void - onOutputPauseChanged?: (paused: boolean, supported: boolean) => void + onOutputPauseChanged?: (paused: boolean, supported: boolean, dropsReported?: boolean) => void + onPausedOutputDropped?: () => void } export type MockTransport = { diff --git a/src/renderer/src/components/terminal-pane/pty-connection/connect-pane-pty.ts b/src/renderer/src/components/terminal-pane/pty-connection/connect-pane-pty.ts index e18f11863965..4df6a4e9caa3 100644 --- a/src/renderer/src/components/terminal-pane/pty-connection/connect-pane-pty.ts +++ b/src/renderer/src/components/terminal-pane/pty-connection/connect-pane-pty.ts @@ -111,6 +111,7 @@ export function connectPanePty( session.syncHiddenRendererPtyDelivery = () => {} session.releaseHiddenRendererPtyDelivery = () => {} session.handleRemoteOutputPauseChanged = () => {} + session.handleRemotePausedOutputDropped = () => {} session.handleRendererOwnedAgentStatus = () => {} session.remoteOutputGatedPtyId = null session.remoteOutputFactConsumerPtyId = null diff --git a/src/renderer/src/components/terminal-pane/pty-connection/foreground-output-refresh.ts b/src/renderer/src/components/terminal-pane/pty-connection/foreground-output-refresh.ts index fb4acf696545..662399532d55 100644 --- a/src/renderer/src/components/terminal-pane/pty-connection/foreground-output-refresh.ts +++ b/src/renderer/src/components/terminal-pane/pty-connection/foreground-output-refresh.ts @@ -102,7 +102,7 @@ export function bindForegroundOutputRefresh(session: ConnectPanePtySession): voi ) } - session.handleRemoteOutputPauseChanged = (paused, supported): void => { + session.handleRemoteOutputPauseChanged = (paused, supported, dropsReported): void => { const ptyId = session.transport.getPtyId() if (!ptyId || !isRemoteRuntimePtyId(ptyId)) { return @@ -127,7 +127,24 @@ export function bindForegroundOutputRefresh(session: ConnectPanePtySession): voi session.registerSideEffectFactConsumerForPty(ptyId, true) session.remoteOutputFactConsumerPtyId = ptyId } + // Why: a host that reports drops lets a quiet pane reveal with zero snapshot fetches; others may have dropped anything. + if (!(supported && dropsReported)) { + session.markHiddenOutputRestoreNeeded() + } + } + + session.handleRemotePausedOutputDropped = (): void => { + const ptyId = session.transport.getPtyId() + if (session.disposed || !ptyId || !isRemoteRuntimePtyId(ptyId)) { + return + } + // Why: the gap invalidates cross-chunk carry, exactly like a local drop marker. + session.transport.resetCrossChunkParserState?.() + const restoreWasInFlight = session.hiddenOutputRestoreInFlight !== null session.markHiddenOutputRestoreNeeded() + if (restoreWasInFlight) { + session.hiddenOutputRestoreFreshSnapshotNeeded = true + } } session.syncHiddenRendererPtyDelivery = (): void => { diff --git a/src/renderer/src/components/terminal-pane/pty-connection/transport-output-callbacks.ts b/src/renderer/src/components/terminal-pane/pty-connection/transport-output-callbacks.ts index 46598014fc8c..6d142305be79 100644 --- a/src/renderer/src/components/terminal-pane/pty-connection/transport-output-callbacks.ts +++ b/src/renderer/src/components/terminal-pane/pty-connection/transport-output-callbacks.ts @@ -95,9 +95,18 @@ export function bindCaptureTransportOutputCallbacks(session: ConnectPanePtySessi session.deps.onPtyRecoveryStateRef?.current?.(session.pane.id, state) } }, - onOutputPauseChanged: (paused: boolean, supported: boolean): void => { + onOutputPauseChanged: ( + paused: boolean, + supported: boolean, + dropsReported?: boolean + ): void => { if (isCurrent()) { - session.handleRemoteOutputPauseChanged(paused, supported) + session.handleRemoteOutputPauseChanged(paused, supported, dropsReported === true) + } + }, + onPausedOutputDropped: (): void => { + if (isCurrent()) { + session.handleRemotePausedOutputDropped() } }, onSpawnDeferred: (reason: SpawnDeferralReason): void => { diff --git a/src/renderer/src/components/terminal-pane/pty-transport-types.ts b/src/renderer/src/components/terminal-pane/pty-transport-types.ts index 956e9ebad304..f81f4c3e3326 100644 --- a/src/renderer/src/components/terminal-pane/pty-transport-types.ts +++ b/src/renderer/src/components/terminal-pane/pty-transport-types.ts @@ -142,7 +142,9 @@ type PtyCallbacks = { onExit?: (code: number) => void onWriteUnavailable?: () => void onRecoveryStateChange?: (state: PtyTransportRecoveryState) => void - onOutputPauseChanged?: (paused: boolean, supported: boolean) => void + /** `dropsReported`: the host sends onPausedOutputDropped, so a pause without one lost nothing. */ + onOutputPauseChanged?: (paused: boolean, supported: boolean, dropsReported?: boolean) => void + onPausedOutputDropped?: () => void /** The host kept this restore asleep; no process started and the resume handle is intact. */ onSpawnDeferred?: (reason: SpawnDeferralReason) => void } diff --git a/src/renderer/src/components/terminal-pane/remote-runtime-pty-transport-attach-subscription.test.ts b/src/renderer/src/components/terminal-pane/remote-runtime-pty-transport-attach-subscription.test.ts index 5982b63a897d..55105015ce3c 100644 --- a/src/renderer/src/components/terminal-pane/remote-runtime-pty-transport-attach-subscription.test.ts +++ b/src/renderer/src/components/terminal-pane/remote-runtime-pty-transport-attach-subscription.test.ts @@ -77,6 +77,7 @@ describe('createRemoteRuntimePtyTransport', () => { ackOutputSourceRanges: 1, desktopViewportClaims: 1, outputPause: 1, + outputPauseDropNotice: 1, writeUnavailable: 1, inputAck: 1 }) diff --git a/src/renderer/src/components/terminal-pane/remote-runtime-pty-transport-stream-reconnect.test.ts b/src/renderer/src/components/terminal-pane/remote-runtime-pty-transport-stream-reconnect.test.ts index a0dc82f3b5ae..100390c93879 100644 --- a/src/renderer/src/components/terminal-pane/remote-runtime-pty-transport-stream-reconnect.test.ts +++ b/src/renderer/src/components/terminal-pane/remote-runtime-pty-transport-stream-reconnect.test.ts @@ -247,7 +247,7 @@ describe('createRemoteRuntimePtyTransport', () => { 'RECONNECT_SNAPSHOT' ]) expect(onData.mock.calls.map((call) => call[0])).toEqual(['LIVE_AFTER_RECONNECT']) - expect(onOutputPauseChanged).toHaveBeenLastCalledWith(false, true) + expect(onOutputPauseChanged).toHaveBeenLastCalledWith(false, true, false) transport.destroy?.() }) diff --git a/src/renderer/src/components/terminal-pane/remote-runtime-pty-transport.ts b/src/renderer/src/components/terminal-pane/remote-runtime-pty-transport.ts index 4142beeb43a9..23b7adc6ec0e 100644 --- a/src/renderer/src/components/terminal-pane/remote-runtime-pty-transport.ts +++ b/src/renderer/src/components/terminal-pane/remote-runtime-pty-transport.ts @@ -2410,10 +2410,16 @@ export function createRemoteRuntimePtyTransport( if (isCurrentSubscription()) { storedCallbacks.onOutputPauseChanged?.( desiredOutputPaused, - nextStream.setOutputPaused(desiredOutputPaused) + nextStream.setOutputPaused(desiredOutputPaused), + nextStream.reportsPausedOutputDrops?.() === true ) } }, + onPausedOutputDropped: () => { + if (isCurrentSubscription()) { + storedCallbacks.onPausedOutputDropped?.() + } + }, onSubscribeAcknowledged: () => { if (isCurrentSubscription()) { inputReadySubscription = generation @@ -2426,7 +2432,8 @@ export function createRemoteRuntimePtyTransport( } storedCallbacks.onOutputPauseChanged?.( desiredOutputPaused, - nextStream.setOutputPaused(desiredOutputPaused) + nextStream.setOutputPaused(desiredOutputPaused), + nextStream.reportsPausedOutputDrops?.() === true ) if (!subscriptionAttached && sameHandleEndRecovery) { recordSameHandleEndReuse(subscribedHandle) @@ -3121,8 +3128,13 @@ export function createRemoteRuntimePtyTransport( if (!connected || !handle) { return false } - const supported = getCurrentMultiplexedStream(handle)?.setOutputPaused(paused) === true - storedCallbacks.onOutputPauseChanged?.(paused, supported) + const stream = getCurrentMultiplexedStream(handle) + const supported = stream?.setOutputPaused(paused) === true + storedCallbacks.onOutputPauseChanged?.( + paused, + supported, + stream?.reportsPausedOutputDrops?.() === true + ) return supported }, diff --git a/src/renderer/src/runtime/remote-runtime-terminal-binary-controller.ts b/src/renderer/src/runtime/remote-runtime-terminal-binary-controller.ts index 14d5b7d3d91c..b182e6329a25 100644 --- a/src/renderer/src/runtime/remote-runtime-terminal-binary-controller.ts +++ b/src/renderer/src/runtime/remote-runtime-terminal-binary-controller.ts @@ -47,6 +47,14 @@ export abstract class RemoteRuntimeTerminalBinaryController extends RemoteRuntim stream.callbacks.onWriteUnavailable?.() return } + if (frame.opcode === TerminalStreamOpcode.PausedOutputDropped) { + if (!stream.supportsPausedOutputDropNotice) { + return + } + stream.pausedOutputGapExpected = true + stream.callbacks.onPausedOutputDropped?.() + return + } if (frame.opcode === TerminalStreamOpcode.InputAck) { if (!stream.supportsInputAck) { return @@ -111,11 +119,16 @@ export abstract class RemoteRuntimeTerminalBinaryController extends RemoteRuntim // Why: keep painting while recovery is in flight — gating froze the pane for // the whole round trip (#18488); the snapshot replaces the screen when it lands. // expectedSeq stays at the gap so a recovery that never lands can still be retried. - if (!resyncInFlight && this.detectOutputGap(stream, seq, rawLength)) { + if ( + !resyncInFlight && + !stream.pausedOutputGapExpected && + this.detectOutputGap(stream, seq, rawLength) + ) { this.requestResyncSnapshot(stream) return } if (typeof seq === 'number' && !resyncInFlight) { + stream.pausedOutputGapExpected = false stream.expectedSeq = seq stream.commandProbeBaselineSeq = undefined } diff --git a/src/renderer/src/runtime/remote-runtime-terminal-multiplexer-implementation.ts b/src/renderer/src/runtime/remote-runtime-terminal-multiplexer-implementation.ts index 8ad2f2759d98..5cf67d463eee 100644 --- a/src/renderer/src/runtime/remote-runtime-terminal-multiplexer-implementation.ts +++ b/src/renderer/src/runtime/remote-runtime-terminal-multiplexer-implementation.ts @@ -34,6 +34,8 @@ export class RemoteRuntimeTerminalMultiplexer extends RemoteRuntimeTerminalBinar acknowledgeOutput: true, acknowledgeOutputSourceRanges: false, supportsOutputPause: false, + supportsPausedOutputDropNotice: false, + pausedOutputGapExpected: false, supportsInputAck: false, inputLedgerId: null, outputPaused: false, @@ -117,6 +119,8 @@ export class RemoteRuntimeTerminalMultiplexer extends RemoteRuntimeTerminalBinar return claimed && resized }, setOutputPaused: (paused) => this.setOutputPaused(state, paused), + reportsPausedOutputDrops: () => + this.isRegisteredStream(state) && state.supportsPausedOutputDropNotice, serializeBuffer: (opts) => this.requestSnapshot(state, opts), serializeBufferOutcome: (opts) => this.requestSnapshotOutcome(state, opts), close: () => { @@ -147,6 +151,7 @@ export class RemoteRuntimeTerminalMultiplexer extends RemoteRuntimeTerminalBinar ackOutput: 1, ackOutputSourceRanges: 1, outputPause: 1, + outputPauseDropNotice: 1, writeUnavailable: 1, ...(args.client.type === 'desktop' ? { desktopViewportClaims: 1 } : {}), ...(args.inputSessionId ? { inputAck: 1 } : {}) diff --git a/src/renderer/src/runtime/remote-runtime-terminal-multiplexer-types.ts b/src/renderer/src/runtime/remote-runtime-terminal-multiplexer-types.ts index 9961fafc8448..219e1f4e4906 100644 --- a/src/renderer/src/runtime/remote-runtime-terminal-multiplexer-types.ts +++ b/src/renderer/src/runtime/remote-runtime-terminal-multiplexer-types.ts @@ -14,7 +14,12 @@ export type TerminalMultiplexEvent = type: 'subscribed' streamId: number streamGeneration?: string - capabilities?: { ackOutputSourceRanges?: 1; outputPause?: 1; inputAck?: 1 } + capabilities?: { + ackOutputSourceRanges?: 1 + outputPause?: 1 + outputPauseDropNotice?: 1 + inputAck?: 1 + } inputLedgerId?: string } | { type: 'end'; streamId: number; verdict?: TerminalStreamEndVerdict } @@ -55,6 +60,8 @@ export type RemoteRuntimeMultiplexedTerminalCallbacks = { /** The host installed the stream and named its capabilities; its initial snapshot is still on the way. */ onSubscribeAcknowledged?: () => void onOutputPauseCapability?: () => void + /** The host discarded output while this stream was paused (negotiated streams only). */ + onPausedOutputDropped?: () => void onEnd?: (verdict: TerminalStreamEndVerdict) => void onError?: (message: string) => void onFitOverrideChanged?: (event: { @@ -148,6 +155,8 @@ export type RemoteRuntimeMultiplexedTerminal = { resize: (cols: number, rows: number) => boolean claimViewport: (cols: number, rows: number) => boolean setOutputPaused: (paused: boolean) => boolean + // Why: only then does a pause with no drop notice prove nothing was lost. + reportsPausedOutputDrops?: () => boolean serializeBuffer: (opts?: { scrollbackRows?: number }) => Promise @@ -166,6 +175,9 @@ export type RemoteRuntimeMultiplexedTerminalState = { acknowledgeOutput: boolean acknowledgeOutputSourceRanges: boolean supportsOutputPause: boolean + supportsPausedOutputDropNotice: boolean + // Why: the host announced the seq jump the pane's own restore covers, so it must not also trigger a resync. + pausedOutputGapExpected: boolean supportsInputAck: boolean inputLedgerId: string | null outputPaused: boolean diff --git a/src/renderer/src/runtime/remote-runtime-terminal-paused-output-drop.test.ts b/src/renderer/src/runtime/remote-runtime-terminal-paused-output-drop.test.ts new file mode 100644 index 000000000000..9a5ca73cd83a --- /dev/null +++ b/src/renderer/src/runtime/remote-runtime-terminal-paused-output-drop.test.ts @@ -0,0 +1,137 @@ +import { beforeEach, describe, expect, it, vi } from 'vitest' +import { + TerminalStreamOpcode, + decodeTerminalStreamFrame, + decodeTerminalStreamJson, + encodeTerminalStreamFrame, + encodeTerminalStreamJson, + encodeTerminalStreamText +} from '../../../shared/terminal-stream-protocol' +import { + getRemoteRuntimeTerminalMultiplexer, + resetRemoteRuntimeTerminalMultiplexersForTests +} from './remote-runtime-terminal-multiplexer' +import { replaceRuntimeEnvironmentRevisions } from './runtime-environment-revision' +import { stubRuntimeEnvironmentBridge } from './runtime-environment-bridge-fixture' + +type SubscribeCallbacks = { + onResponse: (response: unknown) => void + onBinary?: (bytes: Uint8Array) => void +} + +type HostCapabilities = { outputPause?: 1; outputPauseDropNotice?: 1 } + +describe('remote terminal paused-output drop notice', () => { + let callbacks: SubscribeCallbacks + let streamId = 0 + let cursor = 0 + let hostCapabilities: HostCapabilities + let subscribeCapabilities: Record | undefined + let snapshotRequests: { requestId?: number }[] + + const send = (opcode: TerminalStreamOpcode, payload: Uint8Array, seq = 0): void => + callbacks.onBinary?.(encodeTerminalStreamFrame({ opcode, streamId, seq, payload })) + const output = (text: string): void => { + cursor += text.length + send(TerminalStreamOpcode.Output, encodeTerminalStreamText(text), cursor) + } + + beforeEach(() => { + vi.clearAllMocks() + resetRemoteRuntimeTerminalMultiplexersForTests() + replaceRuntimeEnvironmentRevisions([]) + cursor = 0 + snapshotRequests = [] + const subscribe = vi.fn(async (_args: unknown, next: SubscribeCallbacks) => { + callbacks = next + queueMicrotask(() => next.onResponse({ ok: true, result: { type: 'ready' } })) + return { + unsubscribe: vi.fn(), + sendBinary: (bytes: Uint8Array) => { + const frame = decodeTerminalStreamFrame(bytes) + if (frame?.opcode === TerminalStreamOpcode.Subscribe) { + const payload = decodeTerminalStreamJson<{ + streamId: number + capabilities?: Record + }>(frame.payload) + streamId = payload?.streamId ?? 0 + subscribeCapabilities = payload?.capabilities + next.onResponse({ + ok: true, + result: { type: 'subscribed', streamId, capabilities: hostCapabilities } + }) + send(TerminalStreamOpcode.SnapshotStart, encodeTerminalStreamJson({ seq: cursor })) + send(TerminalStreamOpcode.SnapshotEnd, new Uint8Array()) + } else if (frame?.opcode === TerminalStreamOpcode.SnapshotRequest) { + snapshotRequests.push(decodeTerminalStreamJson(frame.payload) ?? {}) + } + } + } + }) + stubRuntimeEnvironmentBridge({ subscribe }) + }) + + async function subscribeClient() { + const data: string[] = [] + const onPausedOutputDropped = vi.fn() + const stream = await getRemoteRuntimeTerminalMultiplexer('env-1').subscribeTerminal({ + terminal: 'terminal-1', + client: { id: 'desktop-1', type: 'desktop' }, + callbacks: { + onData: (chunk) => data.push(chunk), + onSnapshot: () => {}, + onPausedOutputDropped + } + }) + await Promise.resolve() + await Promise.resolve() + return { data, onPausedOutputDropped, stream } + } + + /** Paused host discards `dropped`, optionally announcing it, then resumes with `live`. */ + function pauseDropAndResume( + stream: { setOutputPaused: (paused: boolean) => boolean }, + announce: boolean + ): void { + stream.setOutputPaused(true) + cursor += 'dropped-while-hidden'.length + if (announce) { + send(TerminalStreamOpcode.PausedOutputDropped, new Uint8Array()) + } + stream.setOutputPaused(false) + output('live') + } + + it('offers the capability and treats the announced pause gap as covered, not a resync', async () => { + hostCapabilities = { outputPause: 1, outputPauseDropNotice: 1 } + const { data, onPausedOutputDropped, stream } = await subscribeClient() + expect(subscribeCapabilities).toMatchObject({ outputPause: 1, outputPauseDropNotice: 1 }) + expect(stream.reportsPausedOutputDrops?.()).toBe(true) + + output('before') + pauseDropAndResume(stream, true) + + expect(onPausedOutputDropped).toHaveBeenCalledTimes(1) + expect(data).toEqual(['before', 'live']) + expect(snapshotRequests).toEqual([]) + + // The exemption covers only that pause: a later unannounced gap still resyncs. + cursor += 5 + output('after-real-gap') + expect(snapshotRequests).toHaveLength(1) + expect(snapshotRequests[0]?.requestId).toBeUndefined() + }) + + it('keeps the resync for an older host that does not negotiate drop notices', async () => { + hostCapabilities = { outputPause: 1 } + const { data, onPausedOutputDropped, stream } = await subscribeClient() + expect(stream.reportsPausedOutputDrops?.()).toBe(false) + + // An unnegotiated frame is ignored rather than trusted. + pauseDropAndResume(stream, true) + + expect(onPausedOutputDropped).not.toHaveBeenCalled() + expect(data).toEqual([]) + expect(snapshotRequests).toHaveLength(1) + }) +}) diff --git a/src/renderer/src/runtime/remote-runtime-terminal-response-controller.ts b/src/renderer/src/runtime/remote-runtime-terminal-response-controller.ts index 8f2618430bb5..6a32666c261a 100644 --- a/src/renderer/src/runtime/remote-runtime-terminal-response-controller.ts +++ b/src/renderer/src/runtime/remote-runtime-terminal-response-controller.ts @@ -16,19 +16,30 @@ import { unwrapRuntimeRpcResult } from './runtime-rpc-result' type SubscribedCapabilities = { ackOutputSourceRanges: boolean outputPause: boolean + outputPauseDropNotice: boolean inputAck: boolean } // Why parse: the frame comes off the wire, so a malformed or legacy capabilities value must read as "unsupported". function parseSubscribedCapabilities(value: unknown): SubscribedCapabilities { if (typeof value !== 'object' || value === null) { - return { ackOutputSourceRanges: false, outputPause: false, inputAck: false } + return { + ackOutputSourceRanges: false, + outputPause: false, + outputPauseDropNotice: false, + inputAck: false + } } - const record: { ackOutputSourceRanges?: unknown; outputPause?: unknown; inputAck?: unknown } = - value + const record: { + ackOutputSourceRanges?: unknown + outputPause?: unknown + outputPauseDropNotice?: unknown + inputAck?: unknown + } = value return { ackOutputSourceRanges: record.ackOutputSourceRanges === 1, outputPause: record.outputPause === 1, + outputPauseDropNotice: record.outputPause === 1 && record.outputPauseDropNotice === 1, inputAck: record.inputAck === 1 } } @@ -75,6 +86,7 @@ export abstract class RemoteRuntimeTerminalResponseController extends RemoteRunt stream.streamGeneration = event.streamGeneration } stream.supportsOutputPause = capabilities.outputPause + stream.supportsPausedOutputDropNotice = capabilities.outputPauseDropNotice // Why require the ledger id: without it a runtime restart could not be told apart, and replay would run input twice. stream.inputLedgerId = capabilities.inputAck && diff --git a/src/renderer/src/runtime/runtime-terminal-stream.test.ts b/src/renderer/src/runtime/runtime-terminal-stream.test.ts index b0094dea0f28..3a3eb26e684c 100644 --- a/src/renderer/src/runtime/runtime-terminal-stream.test.ts +++ b/src/renderer/src/runtime/runtime-terminal-stream.test.ts @@ -110,6 +110,7 @@ describe('remote runtime terminal data subscriptions', () => { ackOutputSourceRanges: 1, desktopViewportClaims: 1, outputPause: 1, + outputPauseDropNotice: 1, writeUnavailable: 1 }) diff --git a/src/shared/project-section-grouping.ts b/src/shared/project-section-grouping.ts index 6c833c586b35..29baa3a9f8a9 100644 --- a/src/shared/project-section-grouping.ts +++ b/src/shared/project-section-grouping.ts @@ -286,7 +286,11 @@ export function buildProjectSections< for (const item of args.items) { const repoId = args.repoIdOf(item) addItemToProjectSection( - sections, item, args.displayRepoIdOf?.(item) ?? repoId, repoMap, projectIndex + sections, + item, + args.displayRepoIdOf?.(item) ?? repoId, + repoMap, + projectIndex ) } // Why: repos can arrive before worktree scans, but stale IDs passed by diff --git a/src/shared/resolved-worktree-lineage.test.ts b/src/shared/resolved-worktree-lineage.test.ts index a40cc0856c8f..19d3561ad017 100644 --- a/src/shared/resolved-worktree-lineage.test.ts +++ b/src/shared/resolved-worktree-lineage.test.ts @@ -74,9 +74,16 @@ describe('projectResolvedWorktreeLineage', () => { const child = worktree('child', 'child-instance') it('projects exact instance-aware metadata across projects on one host', () => { - const foreignParent = { ...parent, repoId: 'other', projectId: 'parent', hostId: 'local' as const } + const foreignParent = { + ...parent, + repoId: 'other', + projectId: 'parent', + hostId: 'local' as const + } const ownedChild = { ...child, projectId: 'child', hostId: 'local' as const } - const projected = projectResolvedWorktreeLineage([ownedChild, foreignParent], { child: lineage() }) + const projected = projectResolvedWorktreeLineage([ownedChild, foreignParent], { + child: lineage() + }) expect(projected).toMatchObject([ { id: 'child', parentWorktreeId: 'parent', childWorktreeIds: [], lineage: lineage() }, diff --git a/src/shared/terminal-stream-protocol.ts b/src/shared/terminal-stream-protocol.ts index 0b74d7433bd5..c91d937e08f8 100644 --- a/src/shared/terminal-stream-protocol.ts +++ b/src/shared/terminal-stream-protocol.ts @@ -34,7 +34,9 @@ export enum TerminalStreamOpcode { // Negotiated per stream because older clients reject unknown opcodes. WriteUnavailable = 17, // Host->client; `seq` is the cumulative input sequence the host applied (see encodeTerminalInputAck). Sent only after `inputAck` negotiation. - InputAck = 18 + InputAck = 18, + // Host->client, empty payload; at most once per pause, when the host first discards output for a paused stream. Sent only after `outputPauseDropNotice` negotiation. + PausedOutputDropped = 19 } export type TerminalInputAckKind = 'applied' | 'delivery-unknown' | 'resend' @@ -165,6 +167,7 @@ function isTerminalStreamOpcode(value: number): value is TerminalStreamOpcode { value === TerminalStreamOpcode.OutputSpan || value === TerminalStreamOpcode.SetOutputPaused || value === TerminalStreamOpcode.WriteUnavailable || - value === TerminalStreamOpcode.InputAck + value === TerminalStreamOpcode.InputAck || + value === TerminalStreamOpcode.PausedOutputDropped ) } diff --git a/tests/e2e/cross-version-wire/cross-version-terminal-wire.unit.test.ts b/tests/e2e/cross-version-wire/cross-version-terminal-wire.unit.test.ts index 22efc31bd3cc..52c31b48ff42 100644 --- a/tests/e2e/cross-version-wire/cross-version-terminal-wire.unit.test.ts +++ b/tests/e2e/cross-version-wire/cross-version-terminal-wire.unit.test.ts @@ -108,8 +108,9 @@ function expectWireCompatible(record: JourneyRecord): void { // The subscribe handshake still negotiates the optional output-pause opcode, // which is what keeps opcode 16 legal to send on this pairing. + // Drop notices (opcode 19) ride on pause and appear only when both peers know them. for (const event of record.subscribedEvents) { - expect(event.capabilities).toEqual({ outputPause: 1 }) + expect(event.capabilities).toMatchObject({ outputPause: 1 }) } // Input reached the process, before and after the reconnect. @@ -153,6 +154,9 @@ describe('cross-version remote terminal wire', () => { it('current client against current server completes the journey, and is the reference for a current host', () => { expectJourneyActuallyRan(currentReference) expectWireCompatible(currentReference) + for (const event of currentReference.subscribedEvents) { + expect(event.capabilities).toEqual({ outputPause: 1, outputPauseDropNotice: 1 }) + } expect(currentReference.snapshotStarts).toEqual([ expect.objectContaining({ alternateScreen: false, terminalOwner: 'shell' }), expect.objectContaining({ alternateScreen: false, terminalOwner: 'shell' }),