Skip to content

Commit 7f17f51

Browse files
saphidclaude
andauthored
fix(server): idle compaction still runs after a queued message is removed or steered (pingdotgg#29904)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 3f48896 commit 7f17f51

5 files changed

Lines changed: 44 additions & 11 deletions

File tree

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,22 @@ describe("idleCompactionWindow", () => {
163163
const compacted = records(cache);
164164
compacted.messages = [{ ...compacted.messages[0]!, text: "/compact" }];
165165
expect(idleCompactionWindow(compacted, NOW_MS)).toBeNull();
166+
const steered = records(cache);
167+
steered.runs = [
168+
...steered.runs,
169+
{
170+
...steered.runs[0]!,
171+
id: RunId.make("run-2"),
172+
ordinal: 2,
173+
userMessageId: MessageId.make("message-2"),
174+
status: "cancelled",
175+
startedAt: null,
176+
},
177+
];
178+
expect(idleCompactionWindow(steered, NOW_MS)?.runId).toBe(runId);
179+
// A turn Claude itself reported as cancelled did run, so it stays the latest.
180+
steered.runs = steered.runs.map((run) => ({ ...run, startedAt: at(-MINUTE_MS) }));
181+
expect(idleCompactionWindow(steered, NOW_MS)).toBeNull();
166182
const archived = records(cache);
167183
archived.thread = { ...archived.thread, archivedAt: at() };
168184
expect(idleCompactionWindow(archived, NOW_MS)).toBeNull();
@@ -323,9 +339,17 @@ describe("IdleCompactionService", () => {
323339
ordinal: 2,
324340
userMessageId: laterMessageId,
325341
};
342+
const cancelledRun = {
343+
...earlierRun,
344+
id: RunId.make("run-3"),
345+
ordinal: 3,
346+
userMessageId: MessageId.make("message-3"),
347+
status: "cancelled" as const,
348+
startedAt: null,
349+
};
326350
const data: OrchestrationV2ThreadProjection = {
327351
...base,
328-
runs: [earlierRun, laterRun],
352+
runs: [earlierRun, laterRun, cancelledRun],
329353
messages: [
330354
...base.messages,
331355
{ ...base.messages[0]!, id: laterMessageId, runId: laterRunId, text: "Keep going" },

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,13 @@ export function idleCompactionWindow(
5151
if (thread.snoozedUntil != null && DateTime.toEpochMillis(thread.snoozedUntil) > nowMs)
5252
return null;
5353
if (!isListedThreadLineage(thread.lineage)) return null;
54-
const latestRun = runs.reduce<(typeof runs)[number] | null>(
55-
(latest, run) => (latest === null || run.ordinal > latest.ordinal ? run : latest),
56-
null,
57-
);
54+
// A removed or steered queued message leaves a cancelled run that never started.
55+
const latestRun = runs
56+
.filter((run) => run.status !== "cancelled" || run.startedAt !== null)
57+
.reduce<(typeof runs)[number] | null>(
58+
(latest, run) => (latest === null || run.ordinal > latest.ordinal ? run : latest),
59+
null,
60+
);
5861
if (latestRun?.status !== "completed" || latestRun.completedAt === null) return null;
5962
const message = messages.find((candidate) => candidate.id === latestRun.userMessageId);
6063
// Never compact a compaction, so one idle spell compacts at most once.

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,12 @@ export const make = Effect.gen(function* () {
8888
)
8989
)
9090
return;
91-
const latestRun = records.runs.reduce<(typeof records.runs)[number] | undefined>(
92-
(latest, run) => (latest === undefined || run.ordinal > latest.ordinal ? run : latest),
93-
undefined,
94-
);
91+
const latestRun = records.runs
92+
.filter((run) => run.status !== "cancelled" || run.startedAt !== null)
93+
.reduce<(typeof records.runs)[number] | undefined>(
94+
(latest, run) => (latest === undefined || run.ordinal > latest.ordinal ? run : latest),
95+
undefined,
96+
);
9597
// A late update for an earlier run replaced the latest run's timer, so take it over.
9698
if (latestRun?.status === "completed" && latestRun.id !== runId) {
9799
runId = latestRun.id;

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,8 @@ it.effect("idle compaction filters keep the latest run, live work and the newest
322322
[2, "completed"],
323323
[3, "completed"],
324324
[4, "failed"],
325+
// A removed or steered queued message.
326+
[5, "cancelled"],
325327
] as const) {
326328
const runId = RunId.make(`run:idle-compaction:${ordinal}`);
327329
yield* store.apply({
@@ -345,7 +347,7 @@ it.effect("idle compaction filters keep the latest run, live work and the newest
345347
activeAttemptId: null,
346348
status,
347349
requestedAt: now,
348-
startedAt: now,
350+
startedAt: status === "cancelled" ? null : now,
349351
completedAt: now,
350352
checkpointId: null,
351353
contextHandoffId: null,
@@ -446,7 +448,8 @@ it.effect("idle compaction filters keep the latest run, live work and the newest
446448
contextTransferStatuses: ["pending"],
447449
},
448450
);
449-
// The failed latest run stays, so a completed earlier run is not mistaken for the latest.
451+
// The failed latest run stays, so a completed earlier run is not mistaken for the latest,
452+
// and a cancelled queued run never counts as the latest.
450453
assert.deepEqual(
451454
records.runs.map((run) => run.id),
452455
["run:idle-compaction:rolled-back", "run:idle-compaction:4"],

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3278,6 +3278,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
32783278
OR (${filter.includeLatestRun === true ? 1 : 0} = 1 AND run_id = (
32793279
SELECT run_id FROM orchestration_v2_projection_runs
32803280
WHERE thread_id = ${threadId}
3281+
AND (status <> 'cancelled' OR json_extract(payload_json, '$.startedAt') IS NOT NULL)
32813282
ORDER BY ordinal DESC
32823283
LIMIT 1
32833284
)))`

0 commit comments

Comments
 (0)