Skip to content

Commit d76e066

Browse files
fix(server): snooze no longer waits two minutes after a steered queued message (pingdotgg#35661)
1 parent 56b6c2a commit d76e066

4 files changed

Lines changed: 88 additions & 8 deletions

File tree

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

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5049,8 +5049,7 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
50495049
);
50505050
}
50515051

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.
50545053
const steeredItemId = "turn-item:bounded-sql-history:steered-output";
50555054
const steeredRunId = RunId.make("run:bounded-sql-history:1000");
50565055
yield* sql`
@@ -5088,7 +5087,14 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
50885087
assert.isFalse(
50895088
steeredSnapshot.projection.turnItems.some((item) => item.runId === cancelledRunId),
50905089
);
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));
50925098
yield* sql`DELETE FROM orchestration_v2_projection_turn_items WHERE turn_item_id = ${steeredItemId}`;
50935099

50945100
const interruptRunId = RunId.make("run:bounded-sql-history:interrupt");
@@ -7419,6 +7425,28 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
74197425
silent ? hidden : visible,
74207426
]);
74217427

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+
74227450
// A run that fails outright, e.g. on a provider error, needs the user too.
74237451
const running = (yield* projectionStore.getThreadProjection(threadId)).runs[0]!;
74247452
const setRunStatus = (status: "failed" | "running") =>
@@ -7657,9 +7685,27 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
76577685
value == null ? null : DateTime.toEpochMillis(value);
76587686
assert.equal(timestamp(sqlShell.activityRunStartedAt), expected);
76597687
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+
}
76617692
}
76627693
}
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+
}
76637709

76647710
yield* projectionStore.apply({
76657711
id: EventId.make("event:clock:newer:held"),

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3813,7 +3813,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
38133813
OR run_id IN (SELECT value FROM json_each(${cohortRunIds}))
38143814
OR run_id = ${window.requiredRunId ?? null}
38153815
${
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.
38173817
window.anchorItemId === undefined && window.requiredRunId === undefined
38183818
? sql`OR run_id = ${presentedShellRunId(sql`${threadId}`)}`
38193819
: sql``
@@ -7464,7 +7464,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
74647464
)
74657465
)`;
74667466
};
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.
74687468
const presentedShellRunId = (threadId: Statement.Fragment) => sql`(
74697469
SELECT candidate.run_id
74707470
FROM orchestration_v2_projection_runs candidate
@@ -7473,6 +7473,22 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
74737473
candidate.status = 'queued'
74747474
AND json_extract(candidate.payload_json, '$.queueHeld') IS 1
74757475
)
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+
)
74767492
AND NOT ${isSilentShellRun("candidate")}
74777493
ORDER BY candidate.ordinal DESC, candidate.run_id DESC
74787494
LIMIT 1

‎apps/server/src/orchestration-v2/testkit/fixtures/queued_cancelled_while_active/codex_output.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ export function assertQueuedCancelledWhileActiveOutput(
3434
.get(ACTIVE_RUN_SHELL_SNAPSHOT_KEY)
3535
?.threads.find((thread) => thread.id === projection.thread.id);
3636
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");
3940
assert.equal(capturedShell.activeRunId, activeRun.id);
4041
assert.equal(capturedShell.activityRunStatus, "running");
4142
assert.deepEqual(

‎packages/shared/src/orchestrationV2ThreadError.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,24 @@ export function latestUnheldRun(
122122
let latest: OrchestrationV2Run | null = null;
123123
for (const run of runs) {
124124
if (run.status === "queued" && run.queueHeld === true) continue;
125+
if (outlivedBeforeStart(run, runs)) continue;
125126
if (latest === null || run.ordinal > latest.ordinal) latest = run;
126127
}
127128
return latest;
128129
}
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+
}

0 commit comments

Comments
 (0)