fix(terminal): replay snapshot history at the PTY size it was drawn for

Opening the terminal panel sometimes showed stray fragments on the prompt
row: zsh's end-of-line mark and pieces of the prompt path. The shell had
laid its output out for one PTY width, but the client replayed that
history into an emulator of another width (an early size estimate, a
remount, or a renderer rebuild after fonts loaded). ghostty-web's reflow
then left fragments the shell's SIGWINCH redraw never clears.

The server now reports the PTY cols/rows in every snapshot, the transport
carries them through projections and accepted resizes, and the viewport
replays a sized snapshot chunk at that size before returning to the
fitted size. The container-based size estimate only seeds newly spawned
shells and is no longer sent to a running PTY.

Tests cover the sized replay, the store chunk size, the transport
projection, and the server snapshot; verified in a production build by
reloading with the panel open and switching tabs at a changed width.
This commit is contained in:
Bohdan Triapitsyn
2026-09-06 22:57:43 +03:00
parent d8215ef5b3
commit c2f36fb5e7
14 changed files with 227 additions and 70 deletions
@@ -17,6 +17,7 @@ import { useDeviceInfo } from '@/lib/device';
import { isDesktopShell } from '@/lib/desktop';
import { useUIStore } from '@/stores/useUIStore';
import { useTerminalStore } from '@/stores/useTerminalStore';
import { terminalSnapshotSize } from '@/lib/terminalApi';
import { extractAnnouncedUrls, extractProjectActionUrl } from '@/lib/terminalPreview';
import { setAnnouncedDevServers } from '@/lib/browser/announcedServers';
import { useThemeSystem } from '@/contexts/useThemeSystem';
@@ -641,7 +642,7 @@ export const ProjectActionsButton = ({
onEvent: (event) => {
if (!matchesActionExecution(tabDirectory, tab.id, currentExecutionId)) return;
if (event.type === 'snapshot') {
useTerminalStore.getState().replaceBuffer(tabDirectory, tab.id, event.data ?? '', event.sequence ?? 0);
useTerminalStore.getState().replaceBuffer(tabDirectory, tab.id, event.data ?? '', event.sequence ?? 0, terminalSnapshotSize(event));
if (event.status === 'running') {
useTerminalStore.getState().setTabLifecycle(tabDirectory, tab.id, 'running', { expectedExecutionId: currentExecutionId });
}
@@ -851,7 +852,7 @@ export const ProjectActionsButton = ({
if (!matchesActionExecution(executionDirectory, tabId, adoptedExecutionId)) return;
if (event.purpose?.type === 'project-action' && event.purpose.executionId !== adoptedExecutionId) return;
if (event.type === 'snapshot') {
useTerminalStore.getState().replaceBuffer(executionDirectory, tabId, event.data ?? '', event.sequence ?? 0);
useTerminalStore.getState().replaceBuffer(executionDirectory, tabId, event.data ?? '', event.sequence ?? 0, terminalSnapshotSize(event));
useTerminalStore.getState().setConnecting(executionDirectory, tabId, false, { expectedExecutionId: adoptedExecutionId });
if (event.purpose?.type === 'project-action') {
useTerminalStore.getState().setTabPurpose(executionDirectory, tabId, { type: 'project-action', actionId: event.purpose.actionId, executionId: event.purpose.executionId });
@@ -5,15 +5,21 @@ import { Window } from 'happy-dom';
import { useTerminalStore, type TerminalChunk } from '@/stores/useTerminalStore';
const terminalEvents: Array<{ type: 'write'; data: string } | { type: 'reset' }> = [];
type TerminalEvent =
| { type: 'write'; data: string }
| { type: 'reset' }
| { type: 'resize'; cols: number; rows: number };
const terminalEvents: TerminalEvent[] = [];
class GhosttyTerminalDouble {
public options: { cursorBlink: boolean };
public cols = 80;
public rows = 24;
constructor(options: { cursorBlink?: boolean }) {
constructor(options: { cursorBlink?: boolean; cols?: number; rows?: number }) {
this.options = { cursorBlink: options.cursorBlink ?? false };
this.cols = options.cols ?? 80;
this.rows = options.rows ?? 24;
}
loadAddon() {}
@@ -25,6 +31,11 @@ class GhosttyTerminalDouble {
terminalEvents.push({ type: 'write', data });
callback?.();
}
resize(cols: number, rows: number) {
this.cols = cols;
this.rows = rows;
terminalEvents.push({ type: 'resize', cols, rows });
}
reset() {
terminalEvents.push({ type: 'reset' });
}
@@ -252,4 +263,46 @@ describe('TerminalViewport chunk replay integration', () => {
expect(terminalEvents.filter((event) => event.type === 'write' && event.data === replacementReplayPayload)).toHaveLength(1);
expect(terminalEvents.filter((event) => event.type === 'write' && event.data === 'tail-live\n')).toHaveLength(1);
});
test('would fail if snapshot history drawn for another PTY size were replayed at the fitted size', async () => {
// A zsh prompt drawn for a 94-column PTY: the `%` end-of-line mark plus
// padding fills exactly one 94-column row. Written into an 80-column
// emulator it wraps and the mark survives as a stray fragment.
const history = `%${' '.repeat(93)}\r \r~ `;
const chunks: TerminalChunk[] = [
{ id: 1, data: history, byteLength: history.length, size: { cols: 94, rows: 56 } },
{ id: 2, data: 'live\n', byteLength: 5 },
];
await renderViewport(root, chunks);
await flushGhosttyLoad();
// Default-background resets inside the history are rewritten before the
// write, so identify the history write by the prompt it carries.
const relevant = terminalEvents
.filter((event) => event.type === 'resize' || (event.type === 'write' && (event.data.includes('~ ') || event.data === 'live\n')))
.map((event) => (event.type === 'write' && event.data.includes('~ ') ? { type: 'write', data: 'history' } : event));
expect(relevant).toEqual([
{ type: 'resize', cols: 94, rows: 56 },
{ type: 'write', data: 'history' },
{ type: 'resize', cols: 80, rows: 24 },
{ type: 'write', data: 'live\n' },
]);
terminalEvents.length = 0;
await renderViewport(root, [...chunks, { id: 3, data: 'more\n', byteLength: 5 }]);
expect(terminalEvents).toEqual([{ type: 'write', data: 'more\n' }]);
});
test('would fail if a snapshot drawn at the fitted size still bounced the emulator through a resize', async () => {
const chunks: TerminalChunk[] = [
{ id: 1, data: 'prompt ', byteLength: 11, size: { cols: 80, rows: 24 } },
];
await renderViewport(root, chunks);
await flushGhosttyLoad();
expect(terminalEvents.filter((event) => event.type === 'resize')).toHaveLength(0);
expect(replayWriteEvents(['prompt '])).toEqual([{ type: 'write', data: 'prompt ' }]);
});
});
@@ -97,7 +97,14 @@ type Props = {
sessionKey: string;
chunks: TerminalChunk[];
onInput: (data: string) => void;
/** Fitted size: the emulator has this size, so the PTY should follow. */
onResize: (cols: number, rows: number) => void;
/**
* Size estimated from the container before Ghostty has measured anything.
* Good enough to spawn a shell early, not authoritative: an existing PTY
* must not be resized to it. Falls back to `onResize` when omitted.
*/
onProvisionalSize?: (cols: number, rows: number) => void;
theme: TerminalTheme;
monoFont: MonoFontOption;
fontFamily: string;
@@ -109,7 +116,7 @@ type Props = {
};
const TerminalViewport = React.forwardRef<TerminalController, Props>(({
sessionKey, chunks, onInput, onResize, theme, monoFont, fontFamily, fontSize, className,
sessionKey, chunks, onInput, onResize, onProvisionalSize, theme, monoFont, fontFamily, fontSize, className,
enableTouchScroll = false, autoFocus = true, isVisible = true,
}, ref) => {
const containerRef = React.useRef<HTMLDivElement>(null);
@@ -117,6 +124,7 @@ const TerminalViewport = React.forwardRef<TerminalController, Props>(({
const fitRef = React.useRef<FitAddon | null>(null);
const inputRef = React.useRef(onInput);
const resizeRef = React.useRef(onResize);
const provisionalSizeCallbackRef = React.useRef(onProvisionalSize);
const lastSizeRef = React.useRef<TerminalSize | null>(null);
const provisionalSizeRef = React.useRef<TerminalSize | null>(null);
const lastChunkRef = React.useRef<number | null>(null);
@@ -133,6 +141,7 @@ const TerminalViewport = React.forwardRef<TerminalController, Props>(({
const [rendererGeneration, setRendererGeneration] = React.useState(0);
inputRef.current = onInput;
resizeRef.current = onResize;
provisionalSizeCallbackRef.current = onProvisionalSize;
visibleRef.current = isVisible;
safeResetRef.current = getGhosttySafeResetSequence(theme.background);
@@ -141,7 +150,7 @@ const TerminalViewport = React.forwardRef<TerminalController, Props>(({
if (!container) return;
const size = getProvisionalTerminalSize(container, fontFamily, fontSize);
provisionalSizeRef.current = size;
if (size) resizeRef.current(size.cols, size.rows);
if (size) (provisionalSizeCallbackRef.current ?? resizeRef.current)(size.cols, size.rows);
}, [fontFamily, fontSize]);
const fit = React.useCallback(() => {
@@ -327,18 +336,51 @@ const TerminalViewport = React.forwardRef<TerminalController, Props>(({
terminal.options.cursorBlink = isVisible && document.hasFocus() && container.contains(document.activeElement);
}, [isVisible, ready]);
/**
* Snapshot history was laid out by the shell for the PTY size recorded on the
* chunk. Writing it into an emulator of another width wraps or joins lines the
* shell never wrapped, and the shell's later SIGWINCH redraw only repaints
* from its own cursor row down, so the stray fragments stay on screen. Replay
* such a chunk at its own size and let the emulator reflow back to the fitted
* size; a subsequent PTY resize (when the sizes differ) makes the shell redraw
* on top of a consistent screen.
*
* Only valid while nothing is queued: the write must not overtake bytes that
* are still waiting for the emulator.
*/
const writeReplayAtDrawnSize = React.useCallback((terminal: GhosttyTerminal, chunk: TerminalChunk): boolean => {
if (!chunk.size || writingRef.current || writeQueueRef.current) return false;
const rewritten = rewriteGhosttyDefaultBackgroundResets(
chunk.replayData ?? chunk.data,
outputRewriteCarryRef.current,
safeResetRef.current,
);
outputRewriteCarryRef.current = rewritten.carry;
if (!rewritten.data) return true;
const fitted = { cols: terminal.cols, rows: terminal.rows };
const resizeForReplay = chunk.size.cols !== fitted.cols || chunk.size.rows !== fitted.rows;
if (resizeForReplay) terminal.resize(chunk.size.cols, chunk.size.rows);
try {
terminal.write(rewritten.data);
} finally {
if (resizeForReplay) terminal.resize(fitted.cols, fitted.rows);
}
return true;
}, []);
React.useEffect(() => {
const terminal = terminalRef.current;
if (!terminal) return;
const { reset, replay, pending } = selectTerminalChunkReplay(chunks, lastChunkRef.current);
if (reset) recreateRenderer();
if (pending.length === 0) return;
writeQueueRef.current += pending
const queued = replay && writeReplayAtDrawnSize(terminal, pending[0]) ? pending.slice(1) : pending;
writeQueueRef.current += queued
.map((chunk) => replay ? (chunk.replayData ?? chunk.data) : chunk.data)
.join('');
lastChunkRef.current = chunks.at(-1)?.id ?? null;
flush();
}, [chunks, flush, ready, recreateRenderer]);
}, [chunks, flush, ready, recreateRenderer, writeReplayAtDrawnSize]);
React.useEffect(() => {
if (!autoFocus || !isVisible) return;
@@ -376,7 +376,7 @@ describe('TerminalView project action tab indicator', () => {
});
connectBehavior = (_sessionId, handlers) => {
void Promise.resolve().then(() => {
handlers.onEvent({ type: 'snapshot', data: snapshotData, sequence: 7, status: 'running' });
handlers.onEvent({ type: 'snapshot', data: snapshotData, sequence: 7, status: 'running', cols: 94, rows: 56 });
});
return { close: () => undefined };
};
@@ -391,6 +391,7 @@ describe('TerminalView project action tab indicator', () => {
expect(createSessionCalls.length).toBe(0);
expect(readBufferContent('/repo', actionTab.id)).toBe(snapshotData);
expect(useTerminalStore.getState().getBuffer('/repo', actionTab.id).lastSequence).toBe(7);
expect(useTerminalStore.getState().getBuffer('/repo', actionTab.id).chunks[0]?.size).toEqual({ cols: 94, rows: 56 });
expect(replaceCount).toBe(1);
});
@@ -17,7 +17,7 @@ import { Icon } from "@/components/icon/Icon";
import type { IconName } from '@/components/icon/icons';
import { useDeviceInfo } from '@/lib/device';
import { useRuntimeAPIs } from '@/hooks/useRuntimeAPIs';
import { isTerminalCwdMissingError } from '@/lib/terminalApi';
import { isTerminalCwdMissingError, terminalSnapshotSize } from '@/lib/terminalApi';
import { extractTerminalPreviewUrl, isTerminalPreviewUrlAvailable } from '@/lib/terminalPreview';
import { useI18n } from '@/lib/i18n';
import { PROJECT_ACTION_ICONS } from '@/lib/projectActions';
@@ -321,7 +321,7 @@ export const TerminalView: React.FC<TerminalViewProps> = ({ visible, directory }
setIsReconnectPending(false);
focusTerminalWhenWindowActive();
replaceBuffer(directory, tabId, event.data ?? '', event.sequence ?? 0);
replaceBuffer(directory, tabId, event.data ?? '', event.sequence ?? 0, terminalSnapshotSize(event));
scanTerminalPreviewOutput(directory, tabId, event.data ?? '');
if (event.status === 'exited') setTabLifecycle(directory, tabId, 'exited');
break;
@@ -763,6 +763,14 @@ export const TerminalView: React.FC<TerminalViewProps> = ({ visible, directory }
[activeModifier, focusTerminalController, isReconnectPending, setActiveModifier, t, terminal]
);
// The estimate only seeds the size a brand-new shell is spawned with. A
// running PTY keeps its size until Ghostty has fitted the viewport for
// real; resizing it to an estimate makes the shell redraw for a width the
// emulator never shows.
const handleProvisionalSize = React.useCallback((cols: number, rows: number) => {
lastViewportSizeRef.current = { cols, rows };
}, []);
const handleViewportResize = React.useCallback(
(cols: number, rows: number) => {
const previous = lastViewportSizeRef.current;
@@ -1145,6 +1153,7 @@ export const TerminalView: React.FC<TerminalViewProps> = ({ visible, directory }
chunks={bufferChunks}
onInput={handleViewportInput}
onResize={handleViewportResize}
onProvisionalSize={handleProvisionalSize}
theme={xtermTheme}
monoFont={monoFont}
fontFamily={resolvedFontStack}
+3
View File
@@ -45,6 +45,9 @@ export interface TerminalStreamEvent {
sequence?: number;
data?: string;
replayData?: string;
/** PTY size the snapshot history was drawn for; only `snapshot` events carry it. */
cols?: number;
rows?: number;
status?: 'running' | 'exited' | 'error';
exitCode?: number;
signal?: number | null;
+32
View File
@@ -22,6 +22,8 @@ type WireMessage = {
v?: number;
d?: string;
r?: string;
cols?: number;
rows?: number;
history?: string;
status?: TerminalStreamEvent['status'];
exitCode?: number;
@@ -120,6 +122,36 @@ describe('terminal transport', () => {
}
});
test('carries the PTY size through snapshots, projection replays, and accepted resizes', async () => {
const socket = new FakeSocket();
const transport = new TerminalTransport({ refreshAuth: async () => '', openSocket: () => socket });
const sizes: Array<[number | undefined, number | undefined]> = [];
transport.subscribe('term-1', { onEvent: (event) => { if (event.type === 'snapshot') sizes.push([event.cols, event.rows]); } });
await tick();
socket.open();
await tick();
socket.emit({ t: 'snapshot', v: 3, s: 'term-1', q: 1, history: 'prompt', status: 'running', cols: 94, rows: 56 });
await tick();
expect(sizes).toEqual([[94, 56]]);
const lateSizes: Array<[number | undefined, number | undefined]> = [];
transport.subscribe('term-1', { onEvent: (event) => { if (event.type === 'snapshot') lateSizes.push([event.cols, event.rows]); } });
expect(lateSizes).toEqual([[94, 56]]);
transport.noteResize('term-1', 80, 24);
const afterResize: Array<[number | undefined, number | undefined]> = [];
transport.subscribe('term-1', { onEvent: (event) => { if (event.type === 'snapshot') afterResize.push([event.cols, event.rows]); } });
expect(afterResize).toEqual([[80, 24]]);
socket.emit({ t: 'snapshot', v: 3, s: 'term-2', q: 0, history: '', status: 'running' });
const legacy: Array<[number | undefined, number | undefined]> = [];
transport.subscribe('term-2', { onEvent: (event) => { if (event.type === 'snapshot') legacy.push([event.cols, event.rows]); } });
await tick();
expect(legacy).toEqual([]);
transport.dispose();
});
test('hydrates simultaneous subscribers and rejects duplicate sequences', async () => {
const socket = new FakeSocket();
const transport = new TerminalTransport({ refreshAuth: async () => '', openSocket: () => socket });
+28 -3
View File
@@ -1,4 +1,5 @@
import type { CreateTerminalOptions, TerminalError, TerminalHandlers, TerminalServerSession, TerminalSession, TerminalSessionPurpose, TerminalShellOption, TerminalStreamEvent } from './api/types';
import type { TerminalChunkSize } from '@/stores/useTerminalStore';
import { openRuntimeWebSocket } from './relay/runtime-socket';
import type { RelayTunnelSocketMessageEvent, RelayTunnelWebSocket } from './relay/tunnel-client';
import { runtimeFetch } from './runtime-fetch';
@@ -15,6 +16,9 @@ type Subscriber = { handlers: TerminalHandlers; lastSequence: number };
type TerminalProjection = {
sequence: number;
history: string;
/** Current PTY size: what the server reported at attach, updated by every accepted resize. */
cols?: number;
rows?: number;
status: TerminalStreamEvent['status'];
mode?: TerminalSession['mode'];
purpose?: TerminalSessionPurpose;
@@ -84,6 +88,7 @@ const terminalMessageSchema = z.discriminatedUnion('t', [
z.object({
t: z.literal('snapshot'), s: z.string(), q: z.number().int().nonnegative().default(0),
history: z.string().default(''), status: terminalStatusSchema,
cols: z.number().int().positive().optional(), rows: z.number().int().positive().optional(),
exitCode: z.number().nullish().transform(value => value ?? undefined), signal: z.number().nullable().optional(),
runtime: terminalRuntimeSchema.optional(), ptyBackend: z.string().optional(), ...terminalMessageMetadata,
}),
@@ -124,6 +129,10 @@ export class TerminalRequestError extends Error {
}
}
/** The PTY size a snapshot's history was drawn for, when the server reported one. */
export const terminalSnapshotSize = (event: Pick<TerminalStreamEvent, 'cols' | 'rows'>): TerminalChunkSize | undefined =>
event.cols !== undefined && event.rows !== undefined ? { cols: event.cols, rows: event.rows } : undefined;
export const isTerminalCwdMissingError = (error: unknown): boolean =>
error instanceof TerminalRequestError && error.code === TERMINAL_CWD_MISSING_CODE;
@@ -187,7 +196,7 @@ export class TerminalTransport {
const projection = this.projections.get(sessionId);
if (projection) {
subscriber.lastSequence = projection.sequence;
handlers.onEvent({ type: 'snapshot', sequence: projection.sequence, data: projection.history, status: projection.status, mode: projection.mode, purpose: projection.purpose, exitCode: projection.exitCode, signal: projection.signal, runtime: projection.runtime, ptyBackend: projection.ptyBackend });
handlers.onEvent({ type: 'snapshot', sequence: projection.sequence, data: projection.history, cols: projection.cols, rows: projection.rows, status: projection.status, mode: projection.mode, purpose: projection.purpose, exitCode: projection.exitCode, signal: projection.signal, runtime: projection.runtime, ptyBackend: projection.ptyBackend });
}
const socketWasOpen = this.socket?.readyState === SOCKET_OPEN;
this.ensureConnected().then(() => {
@@ -251,6 +260,17 @@ export class TerminalTransport {
this.projections.delete(sessionId);
}
/**
* Records a resize the server accepted, so a projection snapshot replayed to
* a later subscriber (tab switch, remount) still names the size the
* terminal's current screen is drawn for.
*/
noteResize(sessionId: string, cols: number, rows: number): void {
const projection = this.projections.get(sessionId);
if (!projection) return;
this.projections.set(sessionId, { ...projection, cols, rows });
}
private async ensureConnected(): Promise<void> {
if (this.disposed) throw new Error('Terminal runtime changed');
if (this.socket?.readyState === SOCKET_OPEN) return;
@@ -358,6 +378,8 @@ export class TerminalTransport {
const projection: TerminalProjection = {
sequence: message.q ?? 0,
history: message.history ?? '',
cols: message.cols,
rows: message.rows,
status: message.status,
mode: message.mode,
purpose: message.purpose,
@@ -369,7 +391,7 @@ export class TerminalTransport {
this.projections.set(message.s, projection);
for (const sub of subscribers) {
sub.lastSequence = projection.sequence;
sub.handlers.onEvent({ type: 'snapshot', sequence: projection.sequence, data: projection.history, status: projection.status, mode: projection.mode, purpose: projection.purpose, exitCode: projection.exitCode, signal: projection.signal, runtime: projection.runtime, ptyBackend: projection.ptyBackend });
sub.handlers.onEvent({ type: 'snapshot', sequence: projection.sequence, data: projection.history, cols: projection.cols, rows: projection.rows, status: projection.status, mode: projection.mode, purpose: projection.purpose, exitCode: projection.exitCode, signal: projection.signal, runtime: projection.runtime, ptyBackend: projection.ptyBackend });
}
return;
}
@@ -492,7 +514,10 @@ async function command(path: string, method: string, body?: unknown): Promise<Re
if (!response.ok) throw await responseError(response, 'Terminal command failed');
return response;
}
export async function resizeTerminal(sessionId: string, cols: number, rows: number): Promise<void> { await command(`/api/terminal/${sessionId}/resize`, 'POST', { cols, rows }); }
export async function resizeTerminal(sessionId: string, cols: number, rows: number): Promise<void> {
await command(`/api/terminal/${sessionId}/resize`, 'POST', { cols, rows });
transport.noteResize(sessionId, cols, rows);
}
export async function updateTerminalAppearance(sessionId: string, appearance: Pick<CreateTerminalOptions, 'themeMode' | 'terminalBackground' | 'terminalForeground'>): Promise<void> { await command(`/api/terminal/${sessionId}/appearance`, 'POST', appearance); }
export async function closeTerminal(sessionId: string): Promise<void> { await command(`/api/terminal/${sessionId}`, 'DELETE'); transport.forget(sessionId); }
export async function restartTerminalSession(currentSessionId: string, options: CreateTerminalOptions): Promise<TerminalSession> { return (await command(`/api/terminal/${currentSessionId}/restart`, 'POST', options)).json() as Promise<TerminalSession>; }
@@ -423,6 +423,20 @@ describe('terminal state reconciliation', () => {
expect(buffer(tabId).chunks).toBe(previous);
});
test('records the PTY size a snapshot was drawn for and treats a size change as a new snapshot', () => {
const tabId = setup();
useTerminalStore.getState().replaceBuffer('/repo', tabId, 'prompt', 8, { cols: 94, rows: 56 });
expect(buffer(tabId).chunks[0].size).toEqual({ cols: 94, rows: 56 });
const previous = buffer(tabId).chunks;
useTerminalStore.getState().replaceBuffer('/repo', tabId, 'prompt', 8, { cols: 94, rows: 56 });
expect(buffer(tabId).chunks).toBe(previous);
useTerminalStore.getState().replaceBuffer('/repo', tabId, 'prompt', 8, { cols: 80, rows: 24 });
expect(buffer(tabId).chunks).not.toBe(previous);
expect(buffer(tabId).chunks[0].size).toEqual({ cols: 80, rows: 24 });
useTerminalStore.getState().appendToBuffer('/repo', tabId, ' live', 9);
expect(buffer(tabId).chunks[1].size).toBe(undefined);
});
test('caps multibyte scrollback by UTF-8 bytes', () => {
const tabId = setup();
useTerminalStore.getState().appendToBuffer('/repo', tabId, '界'.repeat(200_000), 1);
+18 -4
View File
@@ -7,11 +7,20 @@ import { getSafeSessionStorage } from '@/stores/utils/safeStorage';
import type { TerminalServerSession } from '@/lib/api/types';
import { normalizeTerminalDirectory } from '@/lib/pathNormalization';
export type TerminalChunkSize = { cols: number; rows: number };
export interface TerminalChunk {
id: number;
data: string;
replayData?: string;
byteLength: number;
/**
* PTY size this chunk was drawn for. Only snapshot history carries it: the
* viewport replays such a chunk at this size and then re-fits, because
* shell output laid out for one width turns into stray fragments when it is
* written into an emulator of another width.
*/
size?: TerminalChunkSize;
}
/**
@@ -99,7 +108,7 @@ interface TerminalStore {
setTabSessionId: (directory: string, tabId: string, sessionId: string | null, options?: { expectedExecutionId?: string | null }) => void;
setTabLifecycle: (directory: string, tabId: string, lifecycle: TerminalTabLifecycle, options?: { expectedExecutionId?: string | null }) => void;
setConnecting: (directory: string, tabId: string, isConnecting: boolean, options?: { expectedExecutionId?: string | null }) => void;
replaceBuffer: (directory: string, tabId: string, content: string, sequence: number) => void;
replaceBuffer: (directory: string, tabId: string, content: string, sequence: number, size?: TerminalChunkSize) => void;
appendToBuffer: (directory: string, tabId: string, chunk: string, sequence?: number, replayData?: string) => void;
setTabPreviewUrl: (directory: string, tabId: string, url: string | null, options?: { locked?: boolean; autoOpened?: boolean; expectedExecutionId?: string | null }) => void;
markPreviewAutoOpened: (directory: string, tabId: string) => void;
@@ -976,7 +985,7 @@ export const useTerminalStore = create<TerminalStore>()(
});
},
replaceBuffer: (directory: string, tabId: string, content: string, sequence: number) => {
replaceBuffer: (directory: string, tabId: string, content: string, sequence: number, size?: TerminalChunkSize) => {
const key = normalizeDirectory(directory);
set((state) => {
const existing = state.sessions.get(key);
@@ -985,17 +994,22 @@ export const useTerminalStore = create<TerminalStore>()(
const buffer = state.buffers.get(entryKey) ?? EMPTY_TERMINAL_BUFFER;
if (buffer.lastSequence > sequence) return state;
const retained = trimToBufferLimit(content);
const previousSize = buffer.chunks[0]?.size;
if (
buffer.lastSequence === sequence &&
buffer.byteLength === retained.byteLength &&
buffer.chunks.map((chunk) => chunk.data).join('') === retained.text
buffer.chunks.map((chunk) => chunk.data).join('') === retained.text &&
previousSize?.cols === size?.cols &&
previousSize?.rows === size?.rows
) {
return state;
}
const chunkId = state.nextChunkId;
const buffers = new Map(state.buffers);
buffers.set(entryKey, {
chunks: retained.text ? [{ id: chunkId, data: retained.text, byteLength: retained.byteLength }] : [],
chunks: retained.text
? [{ id: chunkId, data: retained.text, byteLength: retained.byteLength, ...(size ? { size } : {}) }]
: [],
byteLength: retained.byteLength,
lastSequence: sequence,
});