Migrate legacy terminal history to terminal-scoped log path

- On open, read legacy transcript files and write them to the new terminal-scoped history path
- Remove legacy files after migration and warn if cleanup fails
- Update tests to assert migration, content preservation, and legacy file removal
This commit is contained in:
Julius Marminge authored and Julius Marminge committed 2026-02-12 22:20:01 -08:00
1 parent 485747b892
commit d27b08bae6
2 files changed
+21 -8

No files matched your search

+5 -2
View File
@@ -334,15 +334,18 @@ describe("TerminalManager", () => {
manager.dispose();
});
it("loads existing legacy transcript filenames and keeps new naming for writes", async () => {
it("migrates legacy transcript filenames to terminal-scoped history path on open", async () => {
const { manager, logsDir } = makeManager();
const legacyPath = path.join(logsDir, "thread-1.log");
const nextPath = historyLogPath(logsDir);
fs.writeFileSync(legacyPath, "legacy-line\n", "utf8");
const snapshot = await manager.open(openInput());
expect(snapshot.history).toBe("legacy-line\n");
expect(fs.existsSync(legacyPath)).toBe(true);
expect(fs.existsSync(nextPath)).toBe(true);
expect(fs.readFileSync(nextPath, "utf8")).toBe("legacy-line\n");
expect(fs.existsSync(legacyPath)).toBe(false);
manager.dispose();
});
+16 -6
View File
@@ -582,12 +582,12 @@ export class TerminalManager extends EventEmitter<TerminalManagerEvents> {
}
private async readHistory(threadId: string, terminalId: string): Promise<string> {
const historyPath = this.historyPath(threadId, terminalId);
const nextPath = this.historyPath(threadId, terminalId);
try {
const raw = await fs.promises.readFile(historyPath, "utf8");
const raw = await fs.promises.readFile(nextPath, "utf8");
const capped = capHistory(raw, this.historyLineLimit);
if (capped !== raw) {
await fs.promises.writeFile(historyPath, capped, "utf8");
await fs.promises.writeFile(nextPath, capped, "utf8");
}
return capped;
} catch (error) {
@@ -600,12 +600,22 @@ export class TerminalManager extends EventEmitter<TerminalManagerEvents> {
return "";
}
const legacyPath = this.legacyHistoryPath(threadId);
try {
const raw = await fs.promises.readFile(this.legacyHistoryPath(threadId), "utf8");
const raw = await fs.promises.readFile(legacyPath, "utf8");
const capped = capHistory(raw, this.historyLineLimit);
if (capped !== raw) {
await fs.promises.writeFile(this.legacyHistoryPath(threadId), capped, "utf8");
// Migrate legacy transcript filename to the terminal-scoped path.
await fs.promises.writeFile(nextPath, capped, "utf8");
try {
await fs.promises.rm(legacyPath, { force: true });
} catch (cleanupError) {
this.logger.warn("failed to remove legacy terminal history", {
threadId,
error: cleanupError instanceof Error ? cleanupError.message : String(cleanupError),
});
}
return capped;
} catch (error) {
if ((error as NodeJS.ErrnoException).code === "ENOENT") {