The best way to control Claude Code, Codex and more from any computer or phone
fix(server): provider logs of finished threads obey retention limits (#23274)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: maria-rcks <254055478+maria-rcks@users.noreply.github.com>
Victor Vogel committed
Oct 30, 2026 at 16:26 UTC
e2d9d9a7bb968ee8dfc33787dcfe4d5f54116da0
2 files changed
+51
apps/server/src/provider/EventNdjsonLogger.test.ts
+39
@@ -420,6 +420,45 @@ it.layer(NodeServices.layer)("EventNdjsonLogger", (it) => {
420
}),
421
);
422
423
+ it.effect("retires a thread log once a retention interval passes without writes", () =>
424
+ Effect.gen(function* () {
425
+ const tempDir = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-provider-log-"));
426
+ const basePath = NodePath.join(tempDir, "events.log");
427
+ const finishedPath = ownedLogPath(basePath, "finished");
428
+ const activePath = ownedLogPath(basePath, "active");
429
+
430
+ try {
431
+ const store = yield* makeEventNdjsonLogStore(basePath, {
432
+ batchWindowMs: 0,
433
+ });
434
+ const logger = store.logger("native");
435
+
436
+ yield* logger.write({ id: "finished-first" }, ThreadId.make("finished"));
437
+ yield* TestClock.adjust("5 minutes");
438
+ yield* logger.write({ id: "active-first" }, ThreadId.make("active"));
439
+ assert.equal(NodeFS.existsSync(finishedPath), true);
440
+ NodeFS.truncateSync(finishedPath, 512 * 1024 * 1024 + 1);
441
+
442
+ yield* TestClock.adjust("5 minutes");
443
+ yield* logger.write({ id: "active-second" }, ThreadId.make("active"));
444
+ assert.equal(NodeFS.existsSync(finishedPath), false);
445
+ assert.equal(NodeFS.existsSync(activePath), true);
446
+
447
+ yield* logger.write({ id: "finished-again" }, ThreadId.make("finished"));
448
+ yield* store.close();
449
+ assert.deepEqual(
450
+ NodeFS.readFileSync(finishedPath, "utf8")
451
+ .trim()
452
+ .split("\n")
453
+ .map((line) => parseLogLine(line).payload),
454
+ ['{"id":"finished-again"}'],
455
+ );
456
+ } finally {
457
+ NodeFS.rmSync(tempDir, { recursive: true, force: true });
458
+ }
459
+ }),
460
+ );
461
+
462
it.effect("attributes batches that were written before a later chunk fails", () =>
463
Effect.gen(function* () {
464
const records: ReadonlyArray<PendingRecord> = [
apps/server/src/provider/EventNdjsonLogger.ts
+12
@@ -114,6 +114,7 @@ interface StoreState {
114
readonly pending: Array<PendingRecord>;
115
readonly pendingBytes: number;
116
readonly sinks: ReadonlyMap<string, AsyncFileSink>;
117
+ readonly writtenSinceRetention: ReadonlySet<string>;
118
readonly flushScheduled: boolean;
119
readonly closed: boolean;
120
readonly lastRetentionAt: number;
@@ -483,6 +484,7 @@ const drainPending = Effect.fnUntraced(function* (input: {
484
}
485
486
const sinks = new Map(input.state.sinks);
487
+ const writtenSegments = new Set(input.state.writtenSinceRetention);
488
const failures: Array<FileOperationFailure> = [];
489
const attributionByStream = new Map<
490
EventNdjsonStream,
@@ -508,6 +510,7 @@ const drainPending = Effect.fnUntraced(function* (input: {
510
sink = created.success;
511
sinks.set(threadSegment, sink);
512
}
513
+ writtenSegments.add(threadSegment);
514
515
const written = yield* writeBatchedMessages(
516
sink,
@@ -534,6 +537,13 @@ const drainPending = Effect.fnUntraced(function* (input: {
537
538
const retentionDue =
539
input.now - input.state.lastRetentionAt >= input.options.retentionCheckIntervalMs;
540
+ if (retentionDue) {
541
+ // A sink idle for a whole retention interval reopens from disk on its next
542
+ // write, so retention may remove its files without leaving a stale size.
543
+ for (const threadSegment of sinks.keys()) {
544
+ if (!writtenSegments.has(threadSegment)) sinks.delete(threadSegment);
545
+ }
546
+ }
547
const retention = retentionDue
548
? yield* Effect.promise(() =>
549
enforceRetention({
@@ -563,6 +573,7 @@ const drainPending = Effect.fnUntraced(function* (input: {
573
pending: [],
574
pendingBytes: 0,
575
sinks,
576
+ writtenSinceRetention: retentionDue ? new Set() : writtenSegments,
577
flushScheduled: input.timerFired ? false : input.state.flushScheduled,
578
closed: input.close,
579
lastRetentionAt: retentionDue ? input.now : input.state.lastRetentionAt,
@@ -629,6 +640,7 @@ export const makeEventNdjsonLogStore = Effect.fnUntraced(function* (
640
pending: [],
641
pendingBytes: 0,
642
sinks: new Map(),
643
+ writtenSinceRetention: new Set(),
644
flushScheduled: false,
645
closed: false,
646
lastRetentionAt: initializedAt,