Skip to content

Commit c43a295

Browse files
fix(server): idle compaction survives a late run.updated for an earlier run
A late completed run.updated for an earlier run (from a delegated-task completion) replaced the latest run's idle-compaction timer with one that loaded only the earlier run's messages, found no window and exited, so the thread never compacted. compactWhenDue now retargets to the thread's latest run when that run is completed and differs from the event's run. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 13bb727 commit c43a295

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

‎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)