Skip to content

Commit 33e2766

Browse files
Yuruzuuclaude
andcommitted
perf(server): terminal output bursts no longer rewrite unchanged history
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent c65f8d5 commit 33e2766

2 files changed

Lines changed: 204 additions & 11 deletions

File tree

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

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import * as Option from "effect/Option";
2424
import * as PlatformError from "effect/PlatformError";
2525
import * as Path from "effect/Path";
2626
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
27+
import * as Queue from "effect/Queue";
2728
import * as Ref from "effect/Ref";
2829
import * as Schema from "effect/Schema";
2930
import * as Scope from "effect/Scope";
@@ -317,6 +318,73 @@ const createManager = (
317318
}),
318319
);
319320

321+
// Holds each 250 ms persist debounce and records every history write attempt.
322+
const createPersistFixture = Effect.gen(function* () {
323+
const fs = yield* FileSystem.FileSystem;
324+
const clock = yield* Clock.Clock;
325+
const debounces = yield* Queue.unbounded<Deferred.Deferred<void>>();
326+
const attempts: Array<string> = [];
327+
let beforeNextWrite: Effect.Effect<void, PlatformError.PlatformError> = Effect.void;
328+
329+
const gatedClock: Clock.Clock = {
330+
currentTimeMillisUnsafe: () => clock.currentTimeMillisUnsafe(),
331+
currentTimeMillis: clock.currentTimeMillis,
332+
currentTimeNanosUnsafe: () => clock.currentTimeNanosUnsafe(),
333+
currentTimeNanos: clock.currentTimeNanos,
334+
monotonicTimeNanosUnsafe: () => clock.monotonicTimeNanosUnsafe(),
335+
monotonicTimeNanos: clock.monotonicTimeNanos,
336+
sleep: (duration) =>
337+
Duration.toMillis(duration) === 250
338+
? Effect.gen(function* () {
339+
const release = yield* Deferred.make<void>();
340+
yield* Queue.offer(debounces, release);
341+
yield* Deferred.await(release);
342+
})
343+
: clock.sleep(duration),
344+
};
345+
const recording = FileSystem.FileSystem.of({
346+
...fs,
347+
writeFileString: (filePath, data, options) => {
348+
attempts.push(data);
349+
const before = beforeNextWrite;
350+
beforeNextWrite = Effect.void;
351+
return Effect.andThen(before, fs.writeFileString(filePath, data, options));
352+
},
353+
});
354+
const { manager, ptyAdapter, logsDir } = yield* createManager().pipe(
355+
Effect.provideService(FileSystem.FileSystem, recording),
356+
Effect.provideService(Clock.Clock, gatedClock),
357+
);
358+
359+
const emitOutput = (processIndex: number, data: string) =>
360+
Effect.gen(function* () {
361+
const persistQueued = yield* Deferred.make<void>();
362+
const unsubscribe = yield* manager.subscribe((event) =>
363+
event.type === "output" && event.data === data
364+
? Deferred.succeed(persistQueued, undefined).pipe(Effect.asVoid)
365+
: Effect.void,
366+
);
367+
ptyAdapter.processes[processIndex]!.emitData(data);
368+
yield* Deferred.await(persistQueued);
369+
unsubscribe();
370+
});
371+
const releaseNextDebounce = Effect.flatMap(Queue.take(debounces), (release) =>
372+
Deferred.succeed(release, undefined),
373+
);
374+
375+
return {
376+
manager,
377+
attempts,
378+
emitOutput,
379+
releaseNextDebounce,
380+
nextDebounce: Queue.take(debounces),
381+
readHistory: Effect.flatMap(historyLogPath(logsDir), (filePath) => fs.readFileString(filePath)),
382+
setBeforeNextWrite: (effect: Effect.Effect<void, PlatformError.PlatformError>) => {
383+
beforeNextWrite = effect;
384+
},
385+
};
386+
});
387+
320388
const layerWithHostPlatform = (platform: NodeJS.Platform) =>
321389
Layer.succeed(HostProcess.Platform, platform);
322390

@@ -1501,6 +1569,117 @@ it.layer(
15011569
}),
15021570
);
15031571

1572+
it.effect("skips a background history write the previous write already covered", () =>
1573+
Effect.gen(function* () {
1574+
const { manager, attempts, emitOutput, releaseNextDebounce, nextDebounce, readHistory } =
1575+
yield* createPersistFixture;
1576+
yield* manager.open(openInput());
1577+
yield* emitOutput(0, "first\n");
1578+
const firstDebounce = yield* nextDebounce;
1579+
yield* emitOutput(0, "second\n");
1580+
yield* Deferred.succeed(firstDebounce, undefined);
1581+
yield* releaseNextDebounce;
1582+
yield* manager.close({ threadId: "thread-1" });
1583+
1584+
// The final attempt is close's forced write of an unchanged revision.
1585+
expect(attempts).toEqual(["first\nsecond\n", "first\nsecond\n"]);
1586+
expect(yield* readHistory).toBe("first\nsecond\n");
1587+
}),
1588+
);
1589+
1590+
it.effect("persists output that arrives while a history write is in flight", () =>
1591+
Effect.gen(function* () {
1592+
const fixture = yield* createPersistFixture;
1593+
const { manager, attempts, emitOutput, releaseNextDebounce, readHistory } = fixture;
1594+
const writeStarted = yield* Deferred.make<void>();
1595+
const releaseWrite = yield* Deferred.make<void>();
1596+
fixture.setBeforeNextWrite(
1597+
Deferred.succeed(writeStarted, undefined).pipe(
1598+
Effect.andThen(Deferred.await(releaseWrite)),
1599+
),
1600+
);
1601+
yield* manager.open(openInput());
1602+
yield* emitOutput(0, "first\n");
1603+
yield* releaseNextDebounce;
1604+
yield* Deferred.await(writeStarted);
1605+
yield* emitOutput(0, "second\n");
1606+
yield* Deferred.succeed(releaseWrite, undefined);
1607+
yield* releaseNextDebounce;
1608+
yield* manager.close({ threadId: "thread-1" });
1609+
1610+
expect(attempts).toEqual(["first\n", "first\nsecond\n", "first\nsecond\n"]);
1611+
expect(yield* readHistory).toBe("first\nsecond\n");
1612+
}),
1613+
);
1614+
1615+
it.effect("retries a coalesced background write after the first attempt fails", () =>
1616+
Effect.gen(function* () {
1617+
const fixture = yield* createPersistFixture;
1618+
const { manager, attempts, emitOutput, releaseNextDebounce, nextDebounce, readHistory } =
1619+
fixture;
1620+
fixture.setBeforeNextWrite(
1621+
Effect.fail(
1622+
PlatformError.systemError({
1623+
_tag: "Unknown",
1624+
module: "FileSystem",
1625+
method: "writeFileString",
1626+
cause: { code: "ENOSPC" },
1627+
}),
1628+
),
1629+
);
1630+
yield* manager.open(openInput());
1631+
yield* emitOutput(0, "first\n");
1632+
const firstDebounce = yield* nextDebounce;
1633+
yield* emitOutput(0, "second\n");
1634+
yield* Deferred.succeed(firstDebounce, undefined);
1635+
yield* releaseNextDebounce;
1636+
// Close blocks on the worker, so its forced write is always the last attempt.
1637+
yield* manager.close({ threadId: "thread-1" });
1638+
1639+
expect(attempts).toEqual(["first\nsecond\n", "first\nsecond\n", "first\nsecond\n"]);
1640+
expect(yield* readHistory).toBe("first\nsecond\n");
1641+
}),
1642+
);
1643+
1644+
it.effect("writes a clear that lands while a redundant background pass is pending", () =>
1645+
Effect.gen(function* () {
1646+
const { manager, attempts, emitOutput, nextDebounce, readHistory } =
1647+
yield* createPersistFixture;
1648+
yield* manager.open(openInput());
1649+
yield* emitOutput(0, "first\n");
1650+
const firstDebounce = yield* nextDebounce;
1651+
yield* emitOutput(0, "second\n");
1652+
yield* Deferred.succeed(firstDebounce, undefined);
1653+
const pendingDebounce = yield* nextDebounce;
1654+
const clearing = yield* Effect.forkChild(
1655+
manager.clear({ threadId: "thread-1", terminalId: DEFAULT_TERMINAL_ID }),
1656+
);
1657+
yield* Deferred.succeed(pendingDebounce, undefined);
1658+
yield* Fiber.join(clearing);
1659+
1660+
expect(attempts.at(-1)).toBe("");
1661+
expect(yield* readHistory).toBe("");
1662+
}),
1663+
);
1664+
1665+
it.effect("persists a reopened terminal's history under the same id", () =>
1666+
Effect.gen(function* () {
1667+
const { manager, attempts, emitOutput, releaseNextDebounce, readHistory } =
1668+
yield* createPersistFixture;
1669+
yield* manager.open(openInput());
1670+
yield* emitOutput(0, "first\n");
1671+
yield* releaseNextDebounce;
1672+
yield* manager.close({ threadId: "thread-1" });
1673+
yield* manager.open(openInput());
1674+
yield* emitOutput(1, "second\n");
1675+
yield* releaseNextDebounce;
1676+
yield* manager.close({ threadId: "thread-1" });
1677+
1678+
expect(attempts).toEqual(["first\n", "first\n", "first\nsecond\n", "first\nsecond\n"]);
1679+
expect(yield* readHistory).toBe("first\nsecond\n");
1680+
}),
1681+
);
1682+
15041683
it.effect("migrates legacy transcript filenames to terminal-scoped history path on open", () =>
15051684
Effect.gen(function* () {
15061685
const { manager, logsDir } = yield* createManager();

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

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,8 @@ export class BoundedTerminalHistory {
879879
// Reading the old string's tail on each append can force chunk concatenation.
880880
private lastCodeUnit: number | undefined;
881881
private cachedValue: string | null = "";
882+
revision = 0;
883+
persistedRevision: number | null = null;
882884

883885
constructor(maxLines: number, initial: string, maxBytes = DEFAULT_HISTORY_BYTE_LIMIT) {
884886
this.maxLines = maxLines;
@@ -888,6 +890,7 @@ export class BoundedTerminalHistory {
888890

889891
append(text: string): void {
890892
if (text.length === 0) return;
893+
this.revision += 1;
891894
this.cachedValue = null;
892895
if (this.maxBytes <= 0 || this.maxLines <= 0) {
893896
this.clear();
@@ -1026,6 +1029,7 @@ export class BoundedTerminalHistory {
10261029
}
10271030

10281031
clear(): void {
1032+
this.revision += 1;
10291033
this.chunks = [];
10301034
this.start = 0;
10311035
this.byteLength = 0;
@@ -1969,17 +1973,27 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func
19691973
return;
19701974
}
19711975

1972-
yield* fileSystem
1973-
.writeFileString(historyPath(threadId, terminalId), request.history.value())
1974-
.pipe(
1975-
Effect.catch((error) =>
1976-
Effect.logWarning("failed to persist terminal history", {
1977-
threadId,
1978-
terminalId,
1979-
error,
1980-
}),
1981-
),
1982-
);
1976+
const { history } = request;
1977+
if (!request.immediate && history.persistedRevision === history.revision) {
1978+
return;
1979+
}
1980+
1981+
// Captured with the serialized value: output during the write must stay unpersisted.
1982+
const revision = history.revision;
1983+
yield* fileSystem.writeFileString(historyPath(threadId, terminalId), history.value()).pipe(
1984+
Effect.tap(() =>
1985+
Effect.sync(() => {
1986+
history.persistedRevision = revision;
1987+
}),
1988+
),
1989+
Effect.catch((error) =>
1990+
Effect.logWarning("failed to persist terminal history", {
1991+
threadId,
1992+
terminalId,
1993+
error,
1994+
}),
1995+
),
1996+
);
19831997
}),
19841998
});
19851999

0 commit comments

Comments
 (0)