Skip to content

Commit 79f3ee1

Browse files
fix(server): idle compaction survives a late update for an earlier run (pingdotgg#28102)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent da2d6ec commit 79f3ee1

2 files changed

Lines changed: 97 additions & 2 deletions

File tree

‎apps/server/src/orchestration-v2/IdleCompaction.test.ts‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,4 +308,89 @@ describe("IdleCompactionService", () => {
308308
);
309309
},
310310
);
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+
);
311396
});

‎apps/server/src/orchestration-v2/IdleCompactionService.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ export const make = Effect.gen(function* () {
3131
const settings = yield* ServerSettings.ServerSettingsService;
3232

3333
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.
3637
while (true) {
3738
const current = yield* settings.getSettings;
3839
const thread = yield* projections.getThread(threadId);
@@ -72,6 +73,15 @@ export const make = Effect.gen(function* () {
7273
)
7374
)
7475
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+
}
7585
const nowMs = DateTime.toEpochMillis(yield* DateTime.now);
7686
const window = idleCompactionWindow(records, nowMs);
7787
if (window?.runId !== runId) return;

0 commit comments

Comments
 (0)