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, 05:45 UTC
fix(server): idle compaction still runs after a queued message is removed or steered (#29904)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Alex Southwell committed
Nov 3, 2026 at 19:20 UTC
7f17f515389505a26fe2963e61905cc5d7417963
5 files changed
+44
-11
apps/server/src/orchestration-v2/IdleCompaction.test.ts
+25
-1
@@ -163,6 +163,22 @@ describe("idleCompactionWindow", () => {
163
const compacted = records(cache);
164
compacted.messages = [{ ...compacted.messages[0]!, text: "/compact" }];
165
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();
182
const archived = records(cache);
183
archived.thread = { ...archived.thread, archivedAt: at() };
184
expect(idleCompactionWindow(archived, NOW_MS)).toBeNull();
@@ -323,9 +339,17 @@ describe("IdleCompactionService", () => {
339
ordinal: 2,
340
userMessageId: laterMessageId,
341
};
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
+ };
350
const data: OrchestrationV2ThreadProjection = {
351
...base,
328
- runs: [earlierRun, laterRun],
352
+ runs: [earlierRun, laterRun, cancelledRun],
353
messages: [
354
...base.messages,
355
{ ...base.messages[0]!, id: laterMessageId, runId: laterRunId, text: "Keep going" },
apps/server/src/orchestration-v2/IdleCompaction.ts
+7
-4
@@ -51,10 +51,13 @@ export function idleCompactionWindow(
51
if (thread.snoozedUntil != null && DateTime.toEpochMillis(thread.snoozedUntil) > nowMs)
52
return null;
53
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
+ );
61
if (latestRun?.status !== "completed" || latestRun.completedAt === null) return null;
62
const message = messages.find((candidate) => candidate.id === latestRun.userMessageId);
63
// Never compact a compaction, so one idle spell compacts at most once.
apps/server/src/orchestration-v2/IdleCompactionService.ts
+6
-4
@@ -88,10 +88,12 @@ export const make = Effect.gen(function* () {
88
)
89
)
90
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
+ );
97
// A late update for an earlier run replaced the latest run's timer, so take it over.
98
if (latestRun?.status === "completed" && latestRun.id !== runId) {
99
runId = latestRun.id;
apps/server/src/orchestration-v2/ProjectionStore.test.ts
+5
-2
@@ -322,6 +322,8 @@ it.effect("idle compaction filters keep the latest run, live work and the newest
322
[2, "completed"],
323
[3, "completed"],
324
[4, "failed"],
325
+ // A removed or steered queued message.
326
+ [5, "cancelled"],
327
] as const) {
328
const runId = RunId.make(`run:idle-compaction:${ordinal}`);
329
yield* store.apply({
@@ -345,7 +347,7 @@ it.effect("idle compaction filters keep the latest run, live work and the newest
347
activeAttemptId: null,
348
status,
349
requestedAt: now,
348
- startedAt: now,
350
+ startedAt: status === "cancelled" ? null : now,
351
completedAt: now,
352
checkpointId: null,
353
contextHandoffId: null,
@@ -446,7 +448,8 @@ it.effect("idle compaction filters keep the latest run, live work and the newest
448
contextTransferStatuses: ["pending"],
449
},
450
);
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.
453
assert.deepEqual(
454
records.runs.map((run) => run.id),
455
["run:idle-compaction:rolled-back", "run:idle-compaction:4"],
apps/server/src/orchestration-v2/ProjectionStore.ts
+1
@@ -3278,6 +3278,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
3278
OR (${filter.includeLatestRun === true ? 1 : 0} = 1 AND run_id = (
3279
SELECT run_id FROM orchestration_v2_projection_runs
3280
WHERE thread_id = ${threadId}
3281
+ AND (status <> 'cancelled' OR json_extract(payload_json, '$.startedAt') IS NOT NULL)
3282
ORDER BY ordinal DESC
3283
LIMIT 1
3284
)))`