Skip to content

Commit 1f86811

Browse files
committed
fix(terminal): preserve append cursors across chunk compaction
1 parent 88fc713 commit 1f86811

3 files changed

Lines changed: 114 additions & 84 deletions

File tree

‎apps/web/src/terminal/ghostty/core.test.ts‎

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ describe("ghosttyCellText", () => {
6060
describe("GhosttyTerminalCore snapshots", () => {
6161
const cores = new Set<GhosttyTerminalCore>();
6262

63-
async function createCore() {
63+
async function createCore(onData: (data: string) => void = () => {}) {
6464
const core = await GhosttyTerminalCore.create(
6565
12,
6666
3,
@@ -71,7 +71,7 @@ describe("GhosttyTerminalCore snapshots", () => {
7171
background: { r: 0, g: 0, b: 0 },
7272
cursor: { r: 255, g: 255, b: 255 },
7373
},
74-
() => {},
74+
onData,
7575
);
7676
cores.add(core);
7777
return core;
@@ -260,6 +260,31 @@ describe("GhosttyTerminalCore snapshots", () => {
260260
expect(core.snapshot()).toEqual(reference.snapshot());
261261
});
262262

263+
it("answers a live VT query when batched reads cross a chunk compaction", async () => {
264+
const replies: string[] = [];
265+
const core = await createCore((data) => replies.push(data));
266+
core.write("\x1b[5n");
267+
expect(replies).toEqual(["\x1b[0n"]);
268+
replies.length = 0;
269+
270+
let state = createSession("");
271+
const initial = readTerminalOutputUpdate(state.output, INITIAL_TERMINAL_OUTPUT_CURSOR);
272+
writeTerminalOutputUpdate(core, initial);
273+
let cursor = initial.cursor;
274+
for (let index = 0; index < 1000; index += 1) {
275+
state = append(state, "x");
276+
const update = readTerminalOutputUpdate(state.output, cursor);
277+
writeTerminalOutputUpdate(core, update);
278+
cursor = update.cursor;
279+
}
280+
for (let index = 0; index < 24; index += 1) state = append(state, "x");
281+
state = append(state, "\x1b[5n");
282+
const update = readTerminalOutputUpdate(state.output, cursor);
283+
writeTerminalOutputUpdate(core, update);
284+
285+
expect({ type: update.type, replies }).toEqual({ type: "append", replies: ["\x1b[0n"] });
286+
});
287+
263288
it("recovers a lagging renderer once from bounded output and resumes appending", async () => {
264289
const [core, reference] = await Promise.all([createCore(), createCore()]);
265290
let state = createSession("\x1b[31mold");

‎packages/client-runtime/src/state/terminalOutput.ts‎

Lines changed: 46 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export interface TerminalOutputChunk {
2-
readonly id: number;
2+
/** UTF-16 string offset within this generation and reset. */
3+
readonly startOffset: number;
34
readonly data: string;
45
readonly byteLength: number;
56
}
@@ -9,24 +10,20 @@ export interface TerminalOutputState {
910
readonly chunks: ReadonlyArray<TerminalOutputChunk>;
1011
readonly retainedBytes: number;
1112
readonly resetVersion: number;
12-
readonly latestChunkId: number;
13-
/**
14-
* A cursor below this id cannot safely append after chunks are merged or trimmed.
15-
*/
16-
readonly compactedThroughId: number;
13+
readonly nextOffset: number;
1714
}
1815

1916
export interface TerminalOutputCursor {
2017
readonly generation: number;
2118
readonly resetVersion: number;
22-
readonly lastChunkId: number;
19+
readonly offset: number;
2320
}
2421

2522
/** Forces the first `readTerminalOutputUpdate` to resynchronize from a reset. */
2623
export const INITIAL_TERMINAL_OUTPUT_CURSOR = Object.freeze<TerminalOutputCursor>({
2724
generation: -1,
2825
resetVersion: -1,
29-
lastChunkId: 0,
26+
offset: 0,
3027
});
3128

3229
export type TerminalOutputUpdate =
@@ -57,8 +54,7 @@ export const EMPTY_TERMINAL_OUTPUT_STATE = Object.freeze<TerminalOutputState>({
5754
chunks: Object.freeze([]),
5855
retainedBytes: 0,
5956
resetVersion: 0,
60-
latestChunkId: 0,
61-
compactedThroughId: 0,
57+
nextOffset: 0,
6258
});
6359

6460
interface Utf8Chunk {
@@ -129,59 +125,57 @@ function trimBufferToBytes(buffer: string, maxBufferBytes: number): string {
129125

130126
function splitOutputChunks(
131127
data: string,
132-
firstChunkId: number,
128+
firstOffset: number,
133129
maxChunkBytes = DEFAULT_TERMINAL_CHUNK_BYTES,
134130
): {
135131
readonly chunks: ReadonlyArray<TerminalOutputChunk>;
136-
readonly latestChunkId: number;
132+
readonly nextOffset: number;
137133
readonly byteLength: number;
138134
} {
139135
const split = splitStringByUtf8Bytes(data, maxChunkBytes);
140136
let byteLength = 0;
141-
const chunks = split.map((chunk, index) => {
137+
let nextOffset = firstOffset;
138+
const chunks = split.map((chunk) => {
142139
byteLength += chunk.byteLength;
140+
const startOffset = nextOffset;
141+
nextOffset += chunk.data.length;
143142
return {
144-
id: firstChunkId + index,
143+
startOffset,
145144
data: chunk.data,
146145
byteLength: chunk.byteLength,
147146
};
148147
});
149148

150149
return {
151150
chunks,
152-
latestChunkId: firstChunkId + chunks.length - 1,
151+
nextOffset,
153152
byteLength,
154153
};
155154
}
156155

157156
/**
158-
* Merge adjacent chunks up to the standard chunk size so a long
159-
* run of tiny interactive writes cannot exhaust the chunk budget and force a
160-
* full renderer reset. Merged chunks take the id of their last constituent.
157+
* Merge adjacent chunks without changing their string positions. A reader can
158+
* still append the unread suffix when its cursor falls inside a merged chunk.
161159
*/
162-
function compactRetainedChunks(chunks: ReadonlyArray<TerminalOutputChunk>): {
163-
readonly chunks: ReadonlyArray<TerminalOutputChunk>;
164-
readonly compactedThroughId: number | null;
165-
} {
160+
function compactRetainedChunks(chunks: ReadonlyArray<TerminalOutputChunk>) {
166161
const compacted: TerminalOutputChunk[] = [];
167-
let compactedThroughId: number | null = null;
168162
for (const chunk of chunks) {
169163
const previous = compacted.at(-1);
170164
if (
171165
previous !== undefined &&
166+
previous.startOffset + previous.data.length === chunk.startOffset &&
172167
previous.byteLength + chunk.byteLength <= DEFAULT_TERMINAL_CHUNK_BYTES
173168
) {
174169
compacted[compacted.length - 1] = {
175-
id: chunk.id,
170+
startOffset: previous.startOffset,
176171
data: `${previous.data}${chunk.data}`,
177172
byteLength: previous.byteLength + chunk.byteLength,
178173
};
179-
compactedThroughId = chunk.id;
180174
} else {
181175
compacted.push(chunk);
182176
}
183177
}
184-
return { chunks: compacted, compactedThroughId };
178+
return compacted;
185179
}
186180

187181
// Scan only the removed prefix instead of encoding retained output again.
@@ -198,6 +192,7 @@ function trimOutputChunkStart(
198192
}
199193
return {
200194
...chunk,
195+
startOffset: chunk.startOffset + offset,
201196
data: chunk.data.slice(offset),
202197
byteLength: chunk.byteLength - droppedBytes,
203198
};
@@ -208,34 +203,31 @@ function appendOutput(
208203
data: string,
209204
maxBufferBytes: number,
210205
): TerminalOutputState {
211-
const appended = splitOutputChunks(
212-
data,
213-
current.latestChunkId + 1,
214-
Math.min(DEFAULT_TERMINAL_CHUNK_BYTES, Math.max(1, maxBufferBytes)),
215-
);
216-
if (appended.chunks.length === 0) return current;
206+
if (data.length === 0) return current;
217207
if (maxBufferBytes <= 0) {
218208
return {
219209
generation: current.generation,
220210
chunks: [],
221211
retainedBytes: 0,
222212
resetVersion: current.resetVersion + 1,
223-
latestChunkId: appended.latestChunkId,
224-
compactedThroughId: appended.latestChunkId,
213+
nextOffset: current.nextOffset + data.length,
225214
};
226215
}
216+
const appended = splitOutputChunks(
217+
data,
218+
current.nextOffset,
219+
Math.min(DEFAULT_TERMINAL_CHUNK_BYTES, Math.max(1, maxBufferBytes)),
220+
);
227221

228222
const chunks = [...current.chunks, ...appended.chunks];
229223
let retainedBytes = current.retainedBytes + appended.byteLength;
230224
let firstRetainedIndex = 0;
231-
let compactedThroughId = current.compactedThroughId;
232225
while (retainedBytes > maxBufferBytes && firstRetainedIndex < chunks.length) {
233226
const first = chunks[firstRetainedIndex]!;
234227
const bytesToDrop = retainedBytes - maxBufferBytes;
235228
if (bytesToDrop < first.byteLength) {
236229
const trimmed = trimOutputChunkStart(first, bytesToDrop);
237230
retainedBytes -= first.byteLength - trimmed.byteLength;
238-
compactedThroughId = Math.max(compactedThroughId, first.id);
239231
if (trimmed.byteLength > 0) {
240232
chunks[firstRetainedIndex] = trimmed;
241233
} else {
@@ -248,36 +240,14 @@ function appendOutput(
248240
}
249241

250242
let retainedChunks = firstRetainedIndex === 0 ? chunks : chunks.slice(firstRetainedIndex);
251-
if (retainedChunks.length === 0) {
252-
return {
253-
generation: current.generation,
254-
chunks: [],
255-
retainedBytes: 0,
256-
resetVersion: current.resetVersion + 1,
257-
latestChunkId: appended.latestChunkId,
258-
compactedThroughId: appended.latestChunkId,
259-
};
260-
}
261243
if (retainedChunks.length > MAX_TERMINAL_OUTPUT_CHUNKS) {
262-
// Compact only chunks that predate this append: an up-to-date cursor sits
263-
// on the previous latest chunk id, and merged spans keep the id of their
264-
// last constituent, so that boundary survives compaction.
265-
const retainedOldCount = Math.max(0, retainedChunks.length - appended.chunks.length);
266-
const compacted = compactRetainedChunks(retainedChunks.slice(0, retainedOldCount));
267-
const compactedChunks = [...compacted.chunks, ...retainedChunks.slice(retainedOldCount)];
268-
if (compactedChunks.length > MAX_TERMINAL_OUTPUT_CHUNKS) {
269-
return resetOutput(
270-
{
271-
...current,
272-
latestChunkId: appended.latestChunkId,
273-
},
274-
retainedChunks.map((chunk) => chunk.data).join(""),
275-
maxBufferBytes,
276-
);
277-
}
278-
retainedChunks = compactedChunks;
279-
if (compacted.compactedThroughId !== null) {
280-
compactedThroughId = Math.max(compactedThroughId, compacted.compactedThroughId);
244+
retainedChunks = compactRetainedChunks(retainedChunks);
245+
const excessChunks = retainedChunks.length - MAX_TERMINAL_OUTPUT_CHUNKS;
246+
if (excessChunks > 0) {
247+
for (const chunk of retainedChunks.slice(0, excessChunks)) {
248+
retainedBytes -= chunk.byteLength;
249+
}
250+
retainedChunks = retainedChunks.slice(excessChunks);
281251
}
282252
}
283253

@@ -286,8 +256,7 @@ function appendOutput(
286256
chunks: retainedChunks,
287257
retainedBytes,
288258
resetVersion: current.resetVersion,
289-
latestChunkId: appended.latestChunkId,
290-
compactedThroughId,
259+
nextOffset: appended.nextOffset,
291260
};
292261
}
293262

@@ -299,16 +268,15 @@ function resetOutput(
299268
const retained = trimBufferToBytes(data, maxBufferBytes);
300269
const reset = splitOutputChunks(
301270
retained,
302-
current.latestChunkId + 1,
271+
0,
303272
Math.min(DEFAULT_TERMINAL_CHUNK_BYTES, Math.max(1, maxBufferBytes)),
304273
);
305274
return {
306275
generation: current.generation,
307276
chunks: reset.chunks,
308277
retainedBytes: reset.byteLength,
309278
resetVersion: current.resetVersion + 1,
310-
latestChunkId: reset.latestChunkId,
311-
compactedThroughId: current.latestChunkId,
279+
nextOffset: reset.nextOffset,
312280
};
313281
}
314282

@@ -323,26 +291,28 @@ export function readTerminalOutputUpdate(
323291
const nextCursor = {
324292
generation: output.generation,
325293
resetVersion: output.resetVersion,
326-
lastChunkId: output.latestChunkId,
294+
offset: output.nextOffset,
327295
};
328296
const firstChunk = output.chunks[0];
329297
if (
330298
cursor.generation !== output.generation ||
331299
cursor.resetVersion !== output.resetVersion ||
332-
// The cursor may precede a trimmed chunk or sit inside merged data.
333-
cursor.lastChunkId < output.compactedThroughId ||
334-
(firstChunk !== undefined && firstChunk.id > cursor.lastChunkId + 1)
300+
cursor.offset < (firstChunk?.startOffset ?? output.nextOffset)
335301
) {
336302
return { type: "reset", data: terminalOutputText(output), cursor: nextCursor };
337303
}
338304

339-
const appended = output.chunks.filter((chunk) => chunk.id > cursor.lastChunkId);
305+
const appended = output.chunks.filter(
306+
(chunk) => chunk.startOffset + chunk.data.length > cursor.offset,
307+
);
340308
if (appended.length === 0) {
341309
return { type: "none", cursor: nextCursor };
342310
}
343311
return {
344312
type: "append",
345-
data: appended.map((chunk) => chunk.data).join(""),
313+
data: appended
314+
.map((chunk) => chunk.data.slice(Math.max(0, cursor.offset - chunk.startOffset)))
315+
.join(""),
346316
cursor: nextCursor,
347317
};
348318
}

0 commit comments

Comments
 (0)