From d1545eea17034b4233d12130be42c00451476f8c Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 12 Nov 2026 22:03:54 +0000 Subject: [PATCH 1/4] test(server): a steered queued run that never started is not the shell's latest run Failing test for pingdotgg/t3code#23778. After a queued follow-up is steered into the running turn, its cancelled run never started but the shell still presents it as latestRunId, which keeps Snooze disabled for the grace window. AI-assisted; verified with the tests and tools listed above. --- apps/server/src/orchestration-v2/ProjectionStore.test.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/apps/server/src/orchestration-v2/ProjectionStore.test.ts b/apps/server/src/orchestration-v2/ProjectionStore.test.ts index 511c252689cc8..f9371ce63d74e 100644 --- a/apps/server/src/orchestration-v2/ProjectionStore.test.ts +++ b/apps/server/src/orchestration-v2/ProjectionStore.test.ts @@ -7633,7 +7633,10 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => { value == null ? null : DateTime.toEpochMillis(value); assert.equal(timestamp(sqlShell.activityRunStartedAt), expected); assert.equal(timestamp(memoryShell.activityRunStartedAt), expected); - assert.equal(sqlShell.latestRunId, "run:clock:newer"); + // A queued message steered or removed before it started never ran, so the run that did presents. + for (const shell of [sqlShell, memoryShell]) { + assert.equal(shell.latestRunId, status === "cancelled" ? runId : "run:clock:newer"); + } } } From 29ac4dc934d6ab6192e9e83abd821967be12b1d4 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 12 Nov 2026 22:12:31 +0000 Subject: [PATCH 2/4] fix(server): snooze no longer waits two minutes after a steered queued message Steering a queued message into the running turn cancels its queued run before it starts. The shell still presented that never-started run as the thread's latest run, so its old timestamps read as a pending turn start and Snooze stayed disabled until the queued-start grace window ran out. latestUnheldRun and its SQL mirror presentedShellRunId now skip cancelled runs that never started, so the run that actually ran presents. AI-assisted; verified with the tests and tools listed above. --- .../src/orchestration-v2/ProjectionStore.test.ts | 12 +++++++++--- apps/server/src/orchestration-v2/ProjectionStore.ts | 8 ++++++-- .../queued_cancelled_while_active/codex_output.ts | 5 +++-- packages/shared/src/orchestrationV2ThreadError.ts | 2 ++ 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/apps/server/src/orchestration-v2/ProjectionStore.test.ts b/apps/server/src/orchestration-v2/ProjectionStore.test.ts index f9371ce63d74e..5324437b6cd9d 100644 --- a/apps/server/src/orchestration-v2/ProjectionStore.test.ts +++ b/apps/server/src/orchestration-v2/ProjectionStore.test.ts @@ -5039,8 +5039,7 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => { ), ); - // A steer cancels the queued run before it starts while the steered run keeps writing, so - // the queued message leaves the window but its run is still the shell's latest run. + // A steer moves the cancelled queued message outside the window. const steeredItemId = "turn-item:bounded-sql-history:steered-output"; const steeredRunId = RunId.make("run:bounded-sql-history:1000"); yield* sql` @@ -5078,7 +5077,14 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => { assert.isFalse( steeredSnapshot.projection.turnItems.some((item) => item.runId === cancelledRunId), ); - assert.isTrue(steeredSnapshot.projection.runs.some((run) => run.id === cancelledRunId)); + const latestRunId = (yield* projectionStore.getShellSnapshot()).threads.find( + (thread) => thread.id === threadId, + )?.latestRunId; + assert.equal(latestRunId, hiddenRunId); + assert.isFalse( + steeredSnapshot.projection.turnItems.some((item) => item.runId === latestRunId), + ); + assert.isTrue(steeredSnapshot.projection.runs.some((run) => run.id === latestRunId)); yield* sql`DELETE FROM orchestration_v2_projection_turn_items WHERE turn_item_id = ${steeredItemId}`; const interruptRunId = RunId.make("run:bounded-sql-history:interrupt"); diff --git a/apps/server/src/orchestration-v2/ProjectionStore.ts b/apps/server/src/orchestration-v2/ProjectionStore.ts index d3c55b6d85715..5e0ad2aca4de5 100644 --- a/apps/server/src/orchestration-v2/ProjectionStore.ts +++ b/apps/server/src/orchestration-v2/ProjectionStore.ts @@ -3784,7 +3784,7 @@ export const layer: Layer.Layer = OR run_id IN (SELECT value FROM json_each(${cohortRunIds})) OR run_id = ${window.requiredRunId ?? null} ${ - // The shell's latest run, even a steer-cancelled one outside the newest window. + // Keep the shell's latest run even when its items are outside the newest window. window.anchorItemId === undefined && window.requiredRunId === undefined ? sql`OR run_id = ${presentedShellRunId(sql`${threadId}`)}` : sql`` @@ -7435,7 +7435,7 @@ export const layer: Layer.Layer = ) )`; }; - // The newest run not waiting in a held queue, matching latestUnheldRun. + // The newest run not held or removed before it started, matching latestUnheldRun. const presentedShellRunId = (threadId: Statement.Fragment) => sql`( SELECT candidate.run_id FROM orchestration_v2_projection_runs candidate @@ -7444,6 +7444,10 @@ export const layer: Layer.Layer = candidate.status = 'queued' AND json_extract(candidate.payload_json, '$.queueHeld') IS 1 ) + AND NOT ( + candidate.status = 'cancelled' + AND json_extract(candidate.payload_json, '$.startedAt') IS NULL + ) AND NOT ${isSilentShellRun("candidate")} ORDER BY candidate.ordinal DESC, candidate.run_id DESC LIMIT 1 diff --git a/apps/server/src/orchestration-v2/testkit/fixtures/queued_cancelled_while_active/codex_output.ts b/apps/server/src/orchestration-v2/testkit/fixtures/queued_cancelled_while_active/codex_output.ts index b0d6a44ef45ea..7ab76f9e5a7d4 100644 --- a/apps/server/src/orchestration-v2/testkit/fixtures/queued_cancelled_while_active/codex_output.ts +++ b/apps/server/src/orchestration-v2/testkit/fixtures/queued_cancelled_while_active/codex_output.ts @@ -34,8 +34,9 @@ export function assertQueuedCancelledWhileActiveOutput( .get(ACTIVE_RUN_SHELL_SNAPSHOT_KEY) ?.threads.find((thread) => thread.id === projection.thread.id); assert.isDefined(capturedShell); - assert.equal(capturedShell.latestRunId, cancelledRun.id); - assert.equal(capturedShell.status, "cancelled"); + // The cancelled queued run never started, so the active run presents as the latest. + assert.equal(capturedShell.latestRunId, activeRun.id); + assert.equal(capturedShell.status, "running"); assert.equal(capturedShell.activeRunId, activeRun.id); assert.equal(capturedShell.activityRunStatus, "running"); assert.deepEqual( diff --git a/packages/shared/src/orchestrationV2ThreadError.ts b/packages/shared/src/orchestrationV2ThreadError.ts index 3ba3ba2bf3247..f632aa12667e3 100644 --- a/packages/shared/src/orchestrationV2ThreadError.ts +++ b/packages/shared/src/orchestrationV2ThreadError.ts @@ -122,6 +122,8 @@ export function latestUnheldRun( let latest: OrchestrationV2Run | null = null; for (const run of runs) { if (run.status === "queued" && run.queueHeld === true) continue; + // A removed or steered queued message leaves a cancelled run that never started. + if (run.status === "cancelled" && run.startedAt === null) continue; if (latest === null || run.ordinal > latest.ordinal) latest = run; } return latest; From 761c8b7e1997a915e1f932c7d3406e173100d8ab Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 12 Nov 2026 22:27:37 +0000 Subject: [PATCH 3/4] fix(server): keep a removed queued run latest when the earlier run already ended Skipping every cancelled run that never started also skipped a held message removed after the earlier run had stopped. That earlier run predates the message's last edit, so the shell read a pending turn start and Snooze stayed disabled for two minutes. Skip such a run only when an earlier run that was not queued is still live or ended at or after the cancellation, which is the steer case: the steered message joins that run and its later timestamps cover it. AI-assisted; verified with the tests and tools listed above. --- .../orchestration-v2/ProjectionStore.test.ts | 15 +++++++++++++++ .../src/orchestration-v2/ProjectionStore.ts | 13 ++++++++++++- .../shared/src/orchestrationV2ThreadError.ts | 19 +++++++++++++++++-- 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/apps/server/src/orchestration-v2/ProjectionStore.test.ts b/apps/server/src/orchestration-v2/ProjectionStore.test.ts index 5324437b6cd9d..0b3a11e78263e 100644 --- a/apps/server/src/orchestration-v2/ProjectionStore.test.ts +++ b/apps/server/src/orchestration-v2/ProjectionStore.test.ts @@ -7645,6 +7645,21 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => { } } } + // Removing a message after the earlier run ended leaves the cancelled run as the latest. + yield* projectionStore.apply({ + id: EventId.make("event:clock:ended-before-cancel"), + type: "run.updated", + threadId, + occurredAt: later, + payload: { ...run, status: "completed", startedAt: now, completedAt: now }, + }); + const endedBeforeCancelProjection = yield* projectionStore.getThreadProjection(threadId); + for (const shell of [ + (yield* projectionStore.getShellSnapshot()).threads.find((row) => row.id === threadId)!, + ProjectionStore.threadShellFromProjection(endedBeforeCancelProjection), + ]) { + assert.equal(shell.latestRunId, "run:clock:newer"); + } yield* projectionStore.apply({ id: EventId.make("event:clock:newer:held"), diff --git a/apps/server/src/orchestration-v2/ProjectionStore.ts b/apps/server/src/orchestration-v2/ProjectionStore.ts index 5e0ad2aca4de5..e4d42854edffa 100644 --- a/apps/server/src/orchestration-v2/ProjectionStore.ts +++ b/apps/server/src/orchestration-v2/ProjectionStore.ts @@ -7435,7 +7435,7 @@ export const layer: Layer.Layer = ) )`; }; - // The newest run not held or removed before it started, matching latestUnheldRun. + // The newest run not held or outlived before it started, matching latestUnheldRun. const presentedShellRunId = (threadId: Statement.Fragment) => sql`( SELECT candidate.run_id FROM orchestration_v2_projection_runs candidate @@ -7447,6 +7447,17 @@ export const layer: Layer.Layer = AND NOT ( candidate.status = 'cancelled' AND json_extract(candidate.payload_json, '$.startedAt') IS NULL + AND candidate.completed_at IS NOT NULL + AND EXISTS ( + SELECT 1 FROM orchestration_v2_projection_runs earlier + WHERE earlier.thread_id = candidate.thread_id + AND earlier.ordinal < candidate.ordinal + AND earlier.status <> 'queued' + AND ( + earlier.completed_at IS NULL + OR julianday(earlier.completed_at) >= julianday(candidate.completed_at) + ) + ) ) AND NOT ${isSilentShellRun("candidate")} ORDER BY candidate.ordinal DESC, candidate.run_id DESC diff --git a/packages/shared/src/orchestrationV2ThreadError.ts b/packages/shared/src/orchestrationV2ThreadError.ts index f632aa12667e3..141d73f402540 100644 --- a/packages/shared/src/orchestrationV2ThreadError.ts +++ b/packages/shared/src/orchestrationV2ThreadError.ts @@ -122,9 +122,24 @@ export function latestUnheldRun( let latest: OrchestrationV2Run | null = null; for (const run of runs) { if (run.status === "queued" && run.queueHeld === true) continue; - // A removed or steered queued message leaves a cancelled run that never started. - if (run.status === "cancelled" && run.startedAt === null) continue; + if (outlivedBeforeStart(run, runs)) continue; if (latest === null || run.ordinal > latest.ordinal) latest = run; } return latest; } + +// Steering a queued message cancels its run before it starts and hands the message to the +// earlier run, which outlives the cancellation; that earlier run then presents as latest. +function outlivedBeforeStart( + run: OrchestrationV2Run, + runs: ReadonlyArray, +): boolean { + if (run.status !== "cancelled" || run.startedAt !== null || !run.completedAt) return false; + const cancelledAt = DateTime.toEpochMillis(run.completedAt); + return runs.some( + (earlier) => + earlier.ordinal < run.ordinal && + earlier.status !== "queued" && + (!earlier.completedAt || DateTime.toEpochMillis(earlier.completedAt) >= cancelledAt), + ); +} From c351030d22e1884be6ba5f5dfdd6a15f11543981 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 12 Nov 2026 22:42:06 +0000 Subject: [PATCH 4/4] fix(server): silent compaction runs no longer hide a removed queued run in the shell The SQL shell counted T3 Code's silent compaction runs as earlier runs that outlived a cancelled queued run, while the in-memory shell drops those runs first. Removing a follow-up during an idle compaction then presented an older turn over SQL and kept Snooze disabled. AI-assisted; verified with the tests and tools listed above. --- .../orchestration-v2/ProjectionStore.test.ts | 22 +++++++++++++++++++ .../src/orchestration-v2/ProjectionStore.ts | 1 + 2 files changed, 23 insertions(+) diff --git a/apps/server/src/orchestration-v2/ProjectionStore.test.ts b/apps/server/src/orchestration-v2/ProjectionStore.test.ts index 0b3a11e78263e..96f79266c129d 100644 --- a/apps/server/src/orchestration-v2/ProjectionStore.test.ts +++ b/apps/server/src/orchestration-v2/ProjectionStore.test.ts @@ -7401,6 +7401,28 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => { silent ? hidden : visible, ]); + // A follow-up removed before it started stays latest unless a visible run outlives it. + const removedRunId = RunId.make(`run:projection-shell-silent:${key}:removed`); + yield* projectionStore.apply({ + id: EventId.make(`event:projection-shell-silent:${key}:removed`), + type: "run.updated", + threadId, + occurredAt: now, + payload: { + ...(yield* projectionStore.getThreadProjection(threadId)).runs[0]!, + id: removedRunId, + ordinal: 2, + userMessageId: MessageId.make(`message:projection-shell-silent:${key}:removed`), + status: "cancelled", + startedAt: null, + completedAt: now, + }, + }); + assert.deepStrictEqual( + (yield* presented).map((shell) => shell.latestRunId), + silent ? [removedRunId, removedRunId] : [runId, runId], + ); + // A run that fails outright, e.g. on a provider error, needs the user too. const running = (yield* projectionStore.getThreadProjection(threadId)).runs[0]!; const setRunStatus = (status: "failed" | "running") => diff --git a/apps/server/src/orchestration-v2/ProjectionStore.ts b/apps/server/src/orchestration-v2/ProjectionStore.ts index e4d42854edffa..f659b0e439fed 100644 --- a/apps/server/src/orchestration-v2/ProjectionStore.ts +++ b/apps/server/src/orchestration-v2/ProjectionStore.ts @@ -7453,6 +7453,7 @@ export const layer: Layer.Layer = WHERE earlier.thread_id = candidate.thread_id AND earlier.ordinal < candidate.ordinal AND earlier.status <> 'queued' + AND NOT ${isSilentShellRun("earlier")} AND ( earlier.completed_at IS NULL OR julianday(earlier.completed_at) >= julianday(candidate.completed_at)