@devries-cameron20 / t3code-1 / commits / d76e06679b
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(server): snooze no longer waits two minutes after a steered queued message (#35661)

Julius Marminge committed Nov 12, 2026 at 15:51 UTC d76e06679bdc7d0384a611a3922c87e2722078c2
4 files changed +88 -8
apps/server/src/orchestration-v2/ProjectionStore.test.ts
+50 -4
@@ -5049,8 +5049,7 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
5049 );
5050 }
5051
5052 - // A steer cancels the queued run before it starts while the steered run keeps writing, so
5053 - // the queued message leaves the window but its run is still the shell's latest run.
5052 + // A steer moves the cancelled queued message outside the window.
5053 const steeredItemId = "turn-item:bounded-sql-history:steered-output";
5054 const steeredRunId = RunId.make("run:bounded-sql-history:1000");
5055 yield* sql`
@@ -5088,7 +5087,14 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
5087 assert.isFalse(
5088 steeredSnapshot.projection.turnItems.some((item) => item.runId === cancelledRunId),
5089 );
5091 - assert.isTrue(steeredSnapshot.projection.runs.some((run) => run.id === cancelledRunId));
5090 + const latestRunId = (yield* projectionStore.getShellSnapshot()).threads.find(
5091 + (thread) => thread.id === threadId,
5092 + )?.latestRunId;
5093 + assert.equal(latestRunId, hiddenRunId);
5094 + assert.isFalse(
5095 + steeredSnapshot.projection.turnItems.some((item) => item.runId === latestRunId),
5096 + );
5097 + assert.isTrue(steeredSnapshot.projection.runs.some((run) => run.id === latestRunId));
5098 yield* sql`DELETE FROM orchestration_v2_projection_turn_items WHERE turn_item_id = ${steeredItemId}`;
5099
5100 const interruptRunId = RunId.make("run:bounded-sql-history:interrupt");
@@ -7419,6 +7425,28 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
7425 silent ? hidden : visible,
7426 ]);
7427
7428 + // A follow-up removed before it started stays latest unless a visible run outlives it.
7429 + const removedRunId = RunId.make(`run:projection-shell-silent:${key}:removed`);
7430 + yield* projectionStore.apply({
7431 + id: EventId.make(`event:projection-shell-silent:${key}:removed`),
7432 + type: "run.updated",
7433 + threadId,
7434 + occurredAt: now,
7435 + payload: {
7436 + ...(yield* projectionStore.getThreadProjection(threadId)).runs[0]!,
7437 + id: removedRunId,
7438 + ordinal: 2,
7439 + userMessageId: MessageId.make(`message:projection-shell-silent:${key}:removed`),
7440 + status: "cancelled",
7441 + startedAt: null,
7442 + completedAt: now,
7443 + },
7444 + });
7445 + assert.deepStrictEqual(
7446 + (yield* presented).map((shell) => shell.latestRunId),
7447 + silent ? [removedRunId, removedRunId] : [runId, runId],
7448 + );
7449 +
7450 // A run that fails outright, e.g. on a provider error, needs the user too.
7451 const running = (yield* projectionStore.getThreadProjection(threadId)).runs[0]!;
7452 const setRunStatus = (status: "failed" | "running") =>
@@ -7657,9 +7685,27 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
7685 value == null ? null : DateTime.toEpochMillis(value);
7686 assert.equal(timestamp(sqlShell.activityRunStartedAt), expected);
7687 assert.equal(timestamp(memoryShell.activityRunStartedAt), expected);
7660 - assert.equal(sqlShell.latestRunId, "run:clock:newer");
7688 + // A queued message steered or removed before it started never ran, so the run that did presents.
7689 + for (const shell of [sqlShell, memoryShell]) {
7690 + assert.equal(shell.latestRunId, status === "cancelled" ? runId : "run:clock:newer");
7691 + }
7692 }
7693 }
7694 + // Removing a message after the earlier run ended leaves the cancelled run as the latest.
7695 + yield* projectionStore.apply({
7696 + id: EventId.make("event:clock:ended-before-cancel"),
7697 + type: "run.updated",
7698 + threadId,
7699 + occurredAt: later,
7700 + payload: { ...run, status: "completed", startedAt: now, completedAt: now },
7701 + });
7702 + const endedBeforeCancelProjection = yield* projectionStore.getThreadProjection(threadId);
7703 + for (const shell of [
7704 + (yield* projectionStore.getShellSnapshot()).threads.find((row) => row.id === threadId)!,
7705 + ProjectionStore.threadShellFromProjection(endedBeforeCancelProjection),
7706 + ]) {
7707 + assert.equal(shell.latestRunId, "run:clock:newer");
7708 + }
7709
7710 yield* projectionStore.apply({
7711 id: EventId.make("event:clock:newer:held"),
apps/server/src/orchestration-v2/ProjectionStore.ts
+18 -2
@@ -3813,7 +3813,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
3813 OR run_id IN (SELECT value FROM json_each(${cohortRunIds}))
3814 OR run_id = ${window.requiredRunId ?? null}
3815 ${
3816 - // The shell's latest run, even a steer-cancelled one outside the newest window.
3816 + // Keep the shell's latest run even when its items are outside the newest window.
3817 window.anchorItemId === undefined && window.requiredRunId === undefined
3818 ? sql`OR run_id = ${presentedShellRunId(sql`${threadId}`)}`
3819 : sql``
@@ -7464,7 +7464,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
7464 )
7465 )`;
7466 };
7467 - // The newest run not waiting in a held queue, matching latestUnheldRun.
7467 + // The newest run not held or outlived before it started, matching latestUnheldRun.
7468 const presentedShellRunId = (threadId: Statement.Fragment) => sql`(
7469 SELECT candidate.run_id
7470 FROM orchestration_v2_projection_runs candidate
@@ -7473,6 +7473,22 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
7473 candidate.status = 'queued'
7474 AND json_extract(candidate.payload_json, '$.queueHeld') IS 1
7475 )
7476 + AND NOT (
7477 + candidate.status = 'cancelled'
7478 + AND json_extract(candidate.payload_json, '$.startedAt') IS NULL
7479 + AND candidate.completed_at IS NOT NULL
7480 + AND EXISTS (
7481 + SELECT 1 FROM orchestration_v2_projection_runs earlier
7482 + WHERE earlier.thread_id = candidate.thread_id
7483 + AND earlier.ordinal < candidate.ordinal
7484 + AND earlier.status <> 'queued'
7485 + AND NOT ${isSilentShellRun("earlier")}
7486 + AND (
7487 + earlier.completed_at IS NULL
7488 + OR julianday(earlier.completed_at) >= julianday(candidate.completed_at)
7489 + )
7490 + )
7491 + )
7492 AND NOT ${isSilentShellRun("candidate")}
7493 ORDER BY candidate.ordinal DESC, candidate.run_id DESC
7494 LIMIT 1
apps/server/src/orchestration-v2/testkit/fixtures/queued_cancelled_while_active/codex_output.ts
+3 -2
@@ -34,8 +34,9 @@ export function assertQueuedCancelledWhileActiveOutput(
34 .get(ACTIVE_RUN_SHELL_SNAPSHOT_KEY)
35 ?.threads.find((thread) => thread.id === projection.thread.id);
36 assert.isDefined(capturedShell);
37 - assert.equal(capturedShell.latestRunId, cancelledRun.id);
38 - assert.equal(capturedShell.status, "cancelled");
37 + // The cancelled queued run never started, so the active run presents as the latest.
38 + assert.equal(capturedShell.latestRunId, activeRun.id);
39 + assert.equal(capturedShell.status, "running");
40 assert.equal(capturedShell.activeRunId, activeRun.id);
41 assert.equal(capturedShell.activityRunStatus, "running");
42 assert.deepEqual(
packages/shared/src/orchestrationV2ThreadError.ts
+17
@@ -122,7 +122,24 @@ export function latestUnheldRun(
122 let latest: OrchestrationV2Run | null = null;
123 for (const run of runs) {
124 if (run.status === "queued" && run.queueHeld === true) continue;
125 + if (outlivedBeforeStart(run, runs)) continue;
126 if (latest === null || run.ordinal > latest.ordinal) latest = run;
127 }
128 return latest;
129 }
130 +
131 +// Steering a queued message cancels its run before it starts and hands the message to the
132 +// earlier run, which outlives the cancellation; that earlier run then presents as latest.
133 +function outlivedBeforeStart(
134 + run: OrchestrationV2Run,
135 + runs: ReadonlyArray<OrchestrationV2Run>,
136 +): boolean {
137 + if (run.status !== "cancelled" || run.startedAt !== null || !run.completedAt) return false;
138 + const cancelledAt = DateTime.toEpochMillis(run.completedAt);
139 + return runs.some(
140 + (earlier) =>
141 + earlier.ordinal < run.ordinal &&
142 + earlier.status !== "queued" &&
143 + (!earlier.completedAt || DateTime.toEpochMillis(earlier.completedAt) >= cancelledAt),
144 + );
145 +}