Skip to content

Commit fb2954b

Browse files
fix(terminal): a restored log tail drops the control sequence it starts inside
Restoring history after a restart reads only the last bytes of the log, and that cut could land inside a control sequence whose remainder was then replayed as text and written back. The read now follows the parser state across the bytes it skips and drops the rest of a cut sequence. Co-Authored-By: Claude Fable 5.2 <[email protected]>
1 parent 2390434 commit fb2954b

2 files changed

Lines changed: 39 additions & 1 deletion

File tree

‎apps/server/src/terminal/Manager.test.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,6 +1197,24 @@ it.layer(
11971197
}),
11981198
);
11991199

1200+
it.effect("restores a log tail that starts inside a control sequence without its remainder", () =>
1201+
Effect.gen(function* () {
1202+
const fs = yield* FileSystem.FileSystem;
1203+
for (const [persisted, restored] of [
1204+
["\u001b]0;x\u0007ABCD", "ABCD"],
1205+
["名\u001b]0;名\u0007ABCD", "ABCD"],
1206+
["\u001b]0;x\u0007\u001b[31mABCD", "ABCD"],
1207+
] as const) {
1208+
const { manager, logsDir } = yield* createManager(5, { historyByteLimit: 8 });
1209+
const path = yield* historyLogPath(logsDir);
1210+
yield* fs.makeDirectory(logsDir, { recursive: true });
1211+
yield* fs.writeFileString(path, persisted);
1212+
expect((yield* manager.open(openInput())).history).toBe(restored);
1213+
expect(yield* fs.readFileString(path)).toBe(restored);
1214+
}
1215+
}),
1216+
);
1217+
12001218
it.effect("persists small terminal updates without rewriting the retained history", () =>
12011219
Effect.gen(function* () {
12021220
const fs = yield* FileSystem.FileSystem;

‎apps/server/src/terminal/Manager.ts‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2106,6 +2106,19 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func
21062106
const info = yield* file.stat;
21072107
const limit = BigInt(historyByteLimit);
21082108
const offset = info.size > limit ? info.size - limit : 0n;
2109+
// The tail can start inside a control sequence; only the bytes before it say which.
2110+
const decoder = new TextDecoder("utf-8", { ignoreBOM: true });
2111+
let sequenceState = TERMINAL_SEQUENCE_GROUND;
2112+
const skipped = new Uint8Array(Math.min(Number(offset), 64 * 1024));
2113+
for (let remaining = Number(offset); remaining > 0;) {
2114+
const read = Number(yield* file.read(skipped.subarray(0, remaining)));
2115+
if (read === 0) break;
2116+
remaining -= read;
2117+
sequenceState = terminalSequenceStateAfter(
2118+
sequenceState,
2119+
decoder.decode(skipped.subarray(0, read), { stream: true }),
2120+
);
2121+
}
21092122
yield* file.seek(offset, "start");
21102123
const bytes = new Uint8Array(Number(info.size - offset));
21112124
let length = 0;
@@ -2119,8 +2132,15 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func
21192132
// A tail read can start inside a UTF-8 code point. Skip its remaining bytes.
21202133
while (start < length && ((bytes[start] ?? 0) & 0xc0) === 0x80) start += 1;
21212134
}
2135+
sequenceState = terminalSequenceStateAfter(
2136+
sequenceState,
2137+
decoder.decode(bytes.subarray(0, start)),
2138+
);
2139+
const history = new TextDecoder("utf-8", { ignoreBOM: true }).decode(
2140+
bytes.subarray(start, length),
2141+
);
21222142
return {
2123-
history: new TextDecoder("utf-8", { ignoreBOM: true }).decode(bytes.subarray(start, length)),
2143+
history: history.slice(terminalSequenceEnd(sequenceState, history)),
21242144
truncated: offset > 0n,
21252145
};
21262146
});

0 commit comments

Comments
 (0)