@devries-cameron20 / t3code Mirror · read-only / commits / 8cb9182021
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 22, 12:00 UTC

perf(client): find unread terminal chunks from the tail (#23779)

Utkarsh Patil committed Oct 30, 2026 at 18:12 UTC 8cb9182021bc4317f71b0f5e246d8ffb5b23d456
2 files changed +48 -3
packages/client-runtime/src/state/terminalOutput.ts
+8 -3
@@ -286,9 +286,14 @@ export function readTerminalOutputUpdate(
286 return { type: "reset", data: terminalOutputText(output), cursor: nextCursor };
287 }
288
289 - const appended = output.chunks.filter(
290 - (chunk) => chunk.startOffset + chunk.data.length > cursor.offset,
291 - );
289 + // Appends and compaction preserve offset order, so unread chunks form a suffix.
290 + let firstUnread = output.chunks.length;
291 + while (firstUnread > 0) {
292 + const chunk = output.chunks[firstUnread - 1]!;
293 + if (!(chunk.startOffset + chunk.data.length > cursor.offset)) break;
294 + firstUnread -= 1;
295 + }
296 + const appended = output.chunks.slice(firstUnread);
297 if (appended.length === 0) {
298 return { type: "none", cursor: nextCursor };
299 }
packages/client-runtime/src/state/terminalSession.test.ts
+40
@@ -166,6 +166,46 @@ describe("terminal session reducers", () => {
166 expect(readTerminalOutputUpdate(state.output, update.cursor).type).toBe("none");
167 });
168
169 + it("bounds historical chunk inspection when reading new tail output", () => {
170 + let state = applyTerminalAttachStreamEvent(EMPTY_TERMINAL_BUFFER_STATE, {
171 + type: "snapshot",
172 + snapshot: { ...BASE_SNAPSHOT, history: "" },
173 + });
174 + for (let index = 0; index < 512; index += 1) {
175 + state = applyTerminalAttachStreamEvent(state, {
176 + type: "output",
177 + threadId: TARGET.threadId,
178 + terminalId: TARGET.terminalId,
179 + data: "history\r\n",
180 + });
181 + }
182 + const cursor = readTerminalOutputUpdate(state.output, INITIAL_TERMINAL_OUTPUT_CURSOR).cursor;
183 + const next = applyTerminalAttachStreamEvent(state, {
184 + type: "output",
185 + threadId: TARGET.threadId,
186 + terminalId: TARGET.terminalId,
187 + data: "latest\r\n",
188 + });
189 + let historicalChunkReads = 0;
190 + const chunks = next.output.chunks.map((chunk, index) =>
191 + index < state.output.chunks.length
192 + ? {
193 + ...chunk,
194 + get startOffset() {
195 + historicalChunkReads += 1;
196 + return chunk.startOffset;
197 + },
198 + }
199 + : chunk,
200 + );
201 +
202 + expect(readTerminalOutputUpdate({ ...next.output, chunks }, cursor)).toMatchObject({
203 + type: "append",
204 + data: "latest\r\n",
205 + });
206 + expect(historicalChunkReads).toBeLessThanOrEqual(2);
207 + });
208 +
209 it("preserves the byte-limited tail and resets a cursor before a partially trimmed chunk", () => {
210 const initial = applyTerminalAttachStreamEvent(EMPTY_TERMINAL_BUFFER_STATE, {
211 type: "snapshot",