GeezerVison is showing github.com — fix(server): snooze no longer waits two minutes after a steered queue… · Pipyakas/t3code@d76e066
File treeExpand file treeCollapse file tree Open diff view settings apps/server/src/orchestration-v2 testkit/fixtures/queued_cancelled_while_active
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/ProjectionStore.test.ts +50-4Lines changed: 50 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -5049,8 +5049,7 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => { | 5049 | 5049 | ); | 5050 | 5050 | } | 5051 | 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. | 5054 | 5053 | const steeredItemId = "turn-item:bounded-sql-history:steered-output"; | 5055 | 5054 | const steeredRunId = RunId.make("run:bounded-sql-history:1000"); | 5056 | 5055 | yield* sql` | @@ -5088,7 +5087,14 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => { | 5088 | 5087 | assert.isFalse( | 5089 | 5088 | steeredSnapshot.projection.turnItems.some((item) => item.runId === cancelledRunId), | 5090 | 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)); | 5092 | 5098 | yield* sql`DELETE FROM orchestration_v2_projection_turn_items WHERE turn_item_id = ${steeredItemId}`; | 5093 | 5099 |
| 5094 | 5100 | const interruptRunId = RunId.make("run:bounded-sql-history:interrupt"); | @@ -7419,6 +7425,28 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => { | 7419 | 7425 | silent ? hidden : visible, | 7420 | 7426 | ]); | 7421 | 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 | + | 7422 | 7450 | // A run that fails outright, e.g. on a provider error, needs the user too. | 7423 | 7451 | const running = (yield* projectionStore.getThreadProjection(threadId)).runs[0]!; | 7424 | 7452 | const setRunStatus = (status: "failed" | "running") => | @@ -7657,9 +7685,27 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => { | 7657 | 7685 | value == null ? null : DateTime.toEpochMillis(value); | 7658 | 7686 | assert.equal(timestamp(sqlShell.activityRunStartedAt), expected); | 7659 | 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 | + } | 7661 | 7692 | } | 7662 | 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 | + } | 7663 | 7709 |
| 7664 | 7710 | yield* projectionStore.apply({ | 7665 | 7711 | id: EventId.make("event:clock:newer:held"), | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/ProjectionStore.ts +18-2Lines changed: 18 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -3813,7 +3813,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> = | 3813 | 3813 | OR run_id IN (SELECT value FROM json_each(${cohortRunIds})) | 3814 | 3814 | OR run_id = ${window.requiredRunId ?? null} | 3815 | 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 | 3817 | window.anchorItemId === undefined && window.requiredRunId === undefined | 3818 | 3818 | ? sql`OR run_id = ${presentedShellRunId(sql`${threadId}`)}` | 3819 | 3819 | : sql`` | @@ -7464,7 +7464,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> = | 7464 | 7464 | ) | 7465 | 7465 | )`; | 7466 | 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 | 7468 | const presentedShellRunId = (threadId: Statement.Fragment) => sql`( | 7469 | 7469 | SELECT candidate.run_id | 7470 | 7470 | FROM orchestration_v2_projection_runs candidate | @@ -7473,6 +7473,22 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> = | 7473 | 7473 | candidate.status = 'queued' | 7474 | 7474 | AND json_extract(candidate.payload_json, '$.queueHeld') IS 1 | 7475 | 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 | + ) | 7476 | 7492 | AND NOT ${isSilentShellRun("candidate")} | 7477 | 7493 | ORDER BY candidate.ordinal DESC, candidate.run_id DESC | 7478 | 7494 | LIMIT 1 | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/testkit/fixtures/queued_cancelled_while_active/codex_output.ts +3-2Lines changed: 3 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -34,8 +34,9 @@ export function assertQueuedCancelledWhileActiveOutput( | 34 | 34 | .get(ACTIVE_RUN_SHELL_SNAPSHOT_KEY) | 35 | 35 | ?.threads.find((thread) => thread.id === projection.thread.id); | 36 | 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"); | 39 | 40 | assert.equal(capturedShell.activeRunId, activeRun.id); | 40 | 41 | assert.equal(capturedShell.activityRunStatus, "running"); | 41 | 42 | assert.deepEqual( | |
Collapse file Copy file name to clipboardExpand all lines: packages/shared/src/orchestrationV2ThreadError.ts +17Lines changed: 17 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -122,7 +122,24 @@ export function latestUnheldRun( | 122 | 122 | let latest: OrchestrationV2Run | null = null; | 123 | 123 | for (const run of runs) { | 124 | 124 | if (run.status === "queued" && run.queueHeld === true) continue; | | 125 | + if (outlivedBeforeStart(run, runs)) continue; | 125 | 126 | if (latest === null || run.ordinal > latest.ordinal) latest = run; | 126 | 127 | } | 127 | 128 | return latest; | 128 | 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 | +} |
|