The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 07:00 UTC
fix(server): idle compaction survives a late update for an earlier run (#28102)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Nov 1, 2026 at 21:32 UTC
79f3ee139b99a932178c4bd7c191c4a7f5cf0481
2 files changed
+97
-2
apps/server/src/orchestration-v2/IdleCompaction.test.ts
+85
@@ -308,4 +308,89 @@ describe("IdleCompactionService", () => {
308
);
309
},
310
);
311
+
312
+ it.effect("keeps the latest run's compaction when an earlier run reports completed again", () =>
313
+ Effect.scoped(
314
+ Effect.gen(function* () {
315
+ yield* TestClock.setTime(NOW_MS);
316
+ const base = records({ cacheExpiresInMs: 10 * MINUTE_MS });
317
+ const laterRunId = RunId.make("run-2");
318
+ const laterMessageId = MessageId.make("message-2");
319
+ const earlierRun = base.runs[0]!;
320
+ const laterRun = {
321
+ ...earlierRun,
322
+ id: laterRunId,
323
+ ordinal: 2,
324
+ userMessageId: laterMessageId,
325
+ };
326
+ const data: OrchestrationV2ThreadProjection = {
327
+ ...base,
328
+ runs: [earlierRun, laterRun],
329
+ messages: [
330
+ ...base.messages,
331
+ { ...base.messages[0]!, id: laterMessageId, runId: laterRunId, text: "Keep going" },
332
+ ],
333
+ providerThreads: [{ ...base.providerThreads[0]!, lastRunOrdinal: 2 }],
334
+ attempts: [],
335
+ nodes: [],
336
+ subagents: [],
337
+ providerSessions: [],
338
+ plans: [],
339
+ checkpointScopes: [],
340
+ checkpoints: [],
341
+ contextHandoffs: [],
342
+ visibleTurnItems: [],
343
+ updatedAt: at(),
344
+ };
345
+ const completed = (run: typeof earlierRun) => ({
346
+ id: EventId.make(`completed-${run.id}`),
347
+ type: "run.updated" as const,
348
+ threadId,
349
+ runId: run.id,
350
+ occurredAt: at(),
351
+ payload: run,
352
+ });
353
+ const dispatched: Array<string> = [];
354
+ const streamDone = yield* Deferred.make<void>();
355
+ const dependencies = Layer.mergeAll(
356
+ Layer.mock(Orchestrator.OrchestratorV2)({
357
+ // A delegated completion on the earlier run lands after the later run completed.
358
+ streamDomainEvents: Stream.make(completed(laterRun), completed(earlierRun)).pipe(
359
+ Stream.ensuring(Deferred.succeed(streamDone, undefined)),
360
+ ),
361
+ dispatch: (command) =>
362
+ Effect.sync(() => {
363
+ dispatched.push(command.commandId);
364
+ return { sequence: 1, storedEvents: [] };
365
+ }),
366
+ }),
367
+ Layer.mock(ProjectionStore.ProjectionStoreV2)({
368
+ getThread: () => Effect.succeed(data.thread),
369
+ getThreadRecords: (_threadId, _keys, options) =>
370
+ Effect.succeed({
371
+ ...data,
372
+ messages: data.messages.filter(
373
+ (message) =>
374
+ options?.messageRunIds === undefined ||
375
+ (message.runId !== null && options.messageRunIds.includes(message.runId)),
376
+ ),
377
+ }),
378
+ }),
379
+ Layer.mock(ServerSettings.ServerSettingsService)({
380
+ getSettings: Effect.succeed({
381
+ ...DEFAULT_SERVER_SETTINGS,
382
+ idleCompaction: "beforeCacheExpires",
383
+ }),
384
+ }),
385
+ );
386
+ yield* Effect.gen(function* () {
387
+ const service = yield* IdleCompactionService.make;
388
+ yield* service.start();
389
+ yield* Deferred.await(streamDone);
390
+ yield* TestClock.adjust(9 * MINUTE_MS);
391
+ }).pipe(Effect.provide(dependencies));
392
+ expect(dispatched).toEqual(["server:idle-compact:idle-thread:run-2"]);
393
+ }),
394
+ ),
395
+ );
396
});
apps/server/src/orchestration-v2/IdleCompactionService.ts
+12
-2
@@ -31,8 +31,9 @@ export const make = Effect.gen(function* () {
31
const settings = yield* ServerSettings.ServerSettingsService;
32
33
const compactWhenDue = Effect.fn("IdleCompactionService.compactWhenDue")(
34
- function* (threadId: ThreadId, runId: RunId) {
35
- // Loops once after the wait: the thread may have moved on.
34
+ function* (threadId: ThreadId, completedRunId: RunId) {
35
+ let runId = completedRunId;
36
+ // Loops after the wait or a retarget: the thread may have moved on.
37
while (true) {
38
const current = yield* settings.getSettings;
39
const thread = yield* projections.getThread(threadId);
@@ -72,6 +73,15 @@ export const make = Effect.gen(function* () {
73
)
74
)
75
return;
76
+ const latestRun = records.runs.reduce<(typeof records.runs)[number] | undefined>(
77
+ (latest, run) => (latest === undefined || run.ordinal > latest.ordinal ? run : latest),
78
+ undefined,
79
+ );
80
+ // A late update for an earlier run replaced the latest run's timer, so take it over.
81
+ if (latestRun?.status === "completed" && latestRun.id !== runId) {
82
+ runId = latestRun.id;
83
+ continue;
84
+ }
85
const nowMs = DateTime.toEpochMillis(yield* DateTime.now);
86
const window = idleCompactionWindow(records, nowMs);
87
if (window?.runId !== runId) return;