Skip to content

Commit 110bb0d

Browse files
committed
fix(terminal): 历史压缩失败时仍允许打开终端
- 通过临时文件和原子重命名压缩历史,避免部分写入破坏原文件。 - 普通压缩失败只记录警告,保留严格的旧历史迁移错误处理。 - 扩展现有回归覆盖写入和重命名失败,变更范围检查及 80 项测试通过。
1 parent 6d2f8eb commit 110bb0d

2 files changed

Lines changed: 55 additions & 15 deletions

File tree

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

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,10 +1110,41 @@ it.layer(
11101110
const expected = "a".repeat(maxBytes - 16 * 1024) + "b".repeat(16 * 1024);
11111111
const persisted = yield* fs.readFileString(yield* historyLogPath(logsDir));
11121112
expect(Buffer.byteLength(persisted)).toBe(maxBytes + 16 * 1024);
1113-
const recovered = yield* createManager(5_000, { historyByteLimit: maxBytes });
1114-
yield* fs.makeDirectory(recovered.logsDir, { recursive: true });
1115-
yield* fs.writeFileString(yield* historyLogPath(recovered.logsDir), persisted);
1116-
expect((yield* recovered.manager.open(openInput())).history).toBe(expected);
1113+
for (const failure of [undefined, "write", "rename"] as const) {
1114+
let failed = false;
1115+
const fail = () => {
1116+
failed = true;
1117+
return Effect.fail(
1118+
PlatformError.systemError({
1119+
_tag: "Unknown",
1120+
module: "FileSystem",
1121+
method: failure === "write" ? "writeFileString" : "rename",
1122+
cause: { code: "ENOSPC" },
1123+
}),
1124+
);
1125+
};
1126+
const recovered = yield* createManager(5_000, { historyByteLimit: maxBytes }).pipe(
1127+
Effect.provideService(FileSystem.FileSystem, {
1128+
...fs,
1129+
writeFileString: (file, data, options) =>
1130+
failure === "write" && !failed && data === expected
1131+
? fs.writeFileString(file, "partial", options).pipe(Effect.andThen(fail))
1132+
: fs.writeFileString(file, data, options),
1133+
rename: (from, to) => (failure === "rename" && !failed ? fail() : fs.rename(from, to)),
1134+
}),
1135+
);
1136+
const recoveredPath = yield* historyLogPath(recovered.logsDir);
1137+
yield* fs.makeDirectory(recovered.logsDir, { recursive: true });
1138+
yield* fs.writeFileString(recoveredPath, persisted);
1139+
const snapshot = yield* recovered.manager.open(openInput());
1140+
expect(snapshot.status).toBe("running");
1141+
expect(snapshot.history).toBe(expected);
1142+
expect(failed).toBe(failure !== undefined);
1143+
expect(yield* fs.readFileString(recoveredPath)).toBe(
1144+
failure === undefined ? expected : persisted,
1145+
);
1146+
expect(yield* fs.readDirectory(recovered.logsDir)).toHaveLength(1);
1147+
}
11171148
yield* manager.close({ threadId: "thread-1" });
11181149
expect((yield* manager.open(openInput())).history).toBe(expected);
11191150
expect(yield* fs.readFileString(yield* historyLogPath(logsDir))).toBe(expected);

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

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2087,17 +2087,15 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func
20872087
const history = new BoundedTerminalHistory(historyLineLimit, raw, historyByteLimit);
20882088
const capped = history.value();
20892089
if (migrating || truncated || capped !== raw) {
2090-
const write = migrating
2091-
? Effect.gen(function* () {
2092-
const temporaryPath = yield* fileSystem.makeTempFileScoped({
2093-
directory: logsDir,
2094-
prefix: ".history-",
2095-
});
2096-
yield* fileSystem.writeFileString(temporaryPath, capped);
2097-
yield* fileSystem.rename(temporaryPath, nextPath);
2098-
}).pipe(Effect.scoped)
2099-
: fileSystem.writeFileString(nextPath, capped);
2100-
yield* write.pipe(
2090+
const write = Effect.gen(function* () {
2091+
const temporaryPath = yield* fileSystem.makeTempFileScoped({
2092+
directory: logsDir,
2093+
prefix: ".history-",
2094+
});
2095+
yield* fileSystem.writeFileString(temporaryPath, capped);
2096+
yield* fileSystem.rename(temporaryPath, nextPath);
2097+
}).pipe(
2098+
Effect.scoped,
21012099
Effect.mapError(
21022100
(cause) =>
21032101
new TerminalHistoryError({
@@ -2108,6 +2106,17 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func
21082106
}),
21092107
),
21102108
);
2109+
yield* migrating
2110+
? write
2111+
: write.pipe(
2112+
Effect.catch((error) =>
2113+
Effect.logWarning("failed to compact terminal history", {
2114+
threadId,
2115+
terminalId,
2116+
error,
2117+
}),
2118+
),
2119+
);
21112120
}
21122121
if (migrating) {
21132122
yield* fileSystem.remove(sourcePath, { force: true }).pipe(

0 commit comments

Comments
 (0)