Skip to content
TypeScript

fix(server): idle compaction still runs after a queued message is removed or steered (#29904)

Alex Southwell7f17f5153895
diff --git a/apps/server/src/orchestration-v2/IdleCompaction.test.ts b/apps/server/src/orchestration-v2/IdleCompaction.test.ts
index 3c13452e98e..a11a534b326 100644
--- a/apps/server/src/orchestration-v2/IdleCompaction.test.ts
+++ b/apps/server/src/orchestration-v2/IdleCompaction.test.ts
@@ -163,6 +163,22 @@ describe("idleCompactionWindow", () => {
const compacted = records(cache);
compacted.messages = [{ ...compacted.messages[0]!, text: "/compact" }];
expect(idleCompactionWindow(compacted, NOW_MS)).toBeNull();
+ const steered = records(cache);
+ steered.runs = [
+ ...steered.runs,
+ {
+ ...steered.runs[0]!,
+ id: RunId.make("run-2"),
+ ordinal: 2,
+ userMessageId: MessageId.make("message-2"),
+ status: "cancelled",
+ startedAt: null,
+ },
+ ];
+ expect(idleCompactionWindow(steered, NOW_MS)?.runId).toBe(runId);
+ // A turn Claude itself reported as cancelled did run, so it stays the latest.
+ steered.runs = steered.runs.map((run) => ({ ...run, startedAt: at(-MINUTE_MS) }));
+ expect(idleCompactionWindow(steered, NOW_MS)).toBeNull();
const archived = records(cache);
archived.thread = { ...archived.thread, archivedAt: at() };
expect(idleCompactionWindow(archived, NOW_MS)).toBeNull();
@@ -323,9 +339,17 @@ describe("IdleCompactionService", () => {
ordinal: 2,
userMessageId: laterMessageId,
};
+ const cancelledRun = {
+ ...earlierRun,
+ id: RunId.make("run-3"),
+ ordinal: 3,
+ userMessageId: MessageId.make("message-3"),
+ status: "cancelled" as const,
+ startedAt: null,
+ };
const data: OrchestrationV2ThreadProjection = {
...base,
- runs: [earlierRun, laterRun],
+ runs: [earlierRun, laterRun, cancelledRun],
messages: [
...base.messages,
{ ...base.messages[0]!, id: laterMessageId, runId: laterRunId, text: "Keep going" },
diff --git a/apps/server/src/orchestration-v2/IdleCompaction.ts b/apps/server/src/orchestration-v2/IdleCompaction.ts
index a89894045c2..c4d58681755 100644
--- a/apps/server/src/orchestration-v2/IdleCompaction.ts
+++ b/apps/server/src/orchestration-v2/IdleCompaction.ts
@@ -51,10 +51,13 @@ export function idleCompactionWindow(
if (thread.snoozedUntil != null && DateTime.toEpochMillis(thread.snoozedUntil) > nowMs)
return null;
if (!isListedThreadLineage(thread.lineage)) return null;
- const latestRun = runs.reduce<(typeof runs)[number] | null>(
- (latest, run) => (latest === null || run.ordinal > latest.ordinal ? run : latest),
- null,
- );
+ // A removed or steered queued message leaves a cancelled run that never started.
+ const latestRun = runs
+ .filter((run) => run.status !== "cancelled" || run.startedAt !== null)
+ .reduce<(typeof runs)[number] | null>(
+ (latest, run) => (latest === null || run.ordinal > latest.ordinal ? run : latest),
+ null,
+ );
if (latestRun?.status !== "completed" || latestRun.completedAt === null) return null;
const message = messages.find((candidate) => candidate.id === latestRun.userMessageId);
// Never compact a compaction, so one idle spell compacts at most once.
diff --git a/apps/server/src/orchestration-v2/IdleCompactionService.ts b/apps/server/src/orchestration-v2/IdleCompactionService.ts
index 3b3b788b183..c7035de4ec5 100644
--- a/apps/server/src/orchestration-v2/IdleCompactionService.ts
+++ b/apps/server/src/orchestration-v2/IdleCompactionService.ts
@@ -88,10 +88,12 @@ export const make = Effect.gen(function* () {
)
)
return;
- const latestRun = records.runs.reduce<(typeof records.runs)[number] | undefined>(
- (latest, run) => (latest === undefined || run.ordinal > latest.ordinal ? run : latest),
- undefined,
- );
+ const latestRun = records.runs
+ .filter((run) => run.status !== "cancelled" || run.startedAt !== null)
+ .reduce<(typeof records.runs)[number] | undefined>(
+ (latest, run) => (latest === undefined || run.ordinal > latest.ordinal ? run : latest),
+ undefined,
+ );
// A late update for an earlier run replaced the latest run's timer, so take it over.
if (latestRun?.status === "completed" && latestRun.id !== runId) {
runId = latestRun.id;
diff --git a/apps/server/src/orchestration-v2/ProjectionStore.test.ts b/apps/server/src/orchestration-v2/ProjectionStore.test.ts
index 5b24a65a995..a79b2f37a1d 100644
--- a/apps/server/src/orchestration-v2/ProjectionStore.test.ts
+++ b/apps/server/src/orchestration-v2/ProjectionStore.test.ts
@@ -322,6 +322,8 @@ it.effect("idle compaction filters keep the latest run, live work and the newest
[2, "completed"],
[3, "completed"],
[4, "failed"],
+ // A removed or steered queued message.
+ [5, "cancelled"],
] as const) {
const runId = RunId.make(`run:idle-compaction:${ordinal}`);
yield* store.apply({
@@ -345,7 +347,7 @@ it.effect("idle compaction filters keep the latest run, live work and the newest
activeAttemptId: null,
status,
requestedAt: now,
- startedAt: now,
+ startedAt: status === "cancelled" ? null : now,
completedAt: now,
checkpointId: null,
contextHandoffId: null,
@@ -446,7 +448,8 @@ it.effect("idle compaction filters keep the latest run, live work and the newest
contextTransferStatuses: ["pending"],
},
);
- // The failed latest run stays, so a completed earlier run is not mistaken for the latest.
+ // The failed latest run stays, so a completed earlier run is not mistaken for the latest,
+ // and a cancelled queued run never counts as the latest.
assert.deepEqual(
records.runs.map((run) => run.id),
["run:idle-compaction:rolled-back", "run:idle-compaction:4"],
diff --git a/apps/server/src/orchestration-v2/ProjectionStore.ts b/apps/server/src/orchestration-v2/ProjectionStore.ts
index 3cffce15b70..d67da31d073 100644
--- a/apps/server/src/orchestration-v2/ProjectionStore.ts
+++ b/apps/server/src/orchestration-v2/ProjectionStore.ts
@@ -3278,6 +3278,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
OR (${filter.includeLatestRun === true ? 1 : 0} = 1 AND run_id = (
SELECT run_id FROM orchestration_v2_projection_runs
WHERE thread_id = ${threadId}
+ AND (status <> 'cancelled' OR json_extract(payload_json, '$.startedAt') IS NOT NULL)
ORDER BY ordinal DESC
LIMIT 1
)))`