Skip to content
TypeScript

fix(server): a message promoted into a stopping run stays queued (#16926)

Lbgos10f7db7e38ea
diff --git a/apps/server/src/orchestration-v2/Orchestrator.ts b/apps/server/src/orchestration-v2/Orchestrator.ts
index 4464d09916e..48df36a3a7a 100644
--- a/apps/server/src/orchestration-v2/Orchestrator.ts
+++ b/apps/server/src/orchestration-v2/Orchestrator.ts
@@ -3958,6 +3958,15 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
cause: `Target run ${targetRun.id} is ${targetRun.status} and cannot be steered.`,
});
}
+ // The run reads as running until its provider stops, but a steer accepted
+ // after Stop never reaches the agent.
+ if (yield* stopReachedRun(input.command, input.command.threadId, targetRun.id)) {
+ return yield* new OrchestratorDispatchError({
+ commandId: input.command.commandId,
+ commandType: input.command.type,
+ cause: `Target run ${targetRun.id} is stopping and cannot be steered.`,
+ });
+ }
const providerThread = input.projection.providerThreads.find(
(candidate) => candidate.id === targetRun.providerThreadId,
);
@@ -4804,6 +4813,17 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
dispatchMode = { type: "start_immediately" };
}
}
+ // Stop leaves the run reading as running until its provider stops. A plain
+ // send in that window queues behind the stopped queue instead of steering
+ // text the agent never receives, and like any user send it releases the
+ // hold; an explicit steer or restart still fails below.
+ if (
+ command.deliveryIntent === "auto" &&
+ (dispatchMode.type === "steer_active" || dispatchMode.type === "restart_active") &&
+ (yield* stopReachedRun(command, command.threadId, dispatchMode.targetRunId))
+ ) {
+ dispatchMode = { type: "queue_after_active" };
+ }
if (
command.notification !== undefined &&
(command.createdBy !== "agent" ||
diff --git a/apps/server/src/orchestration-v2/SteeringCompletion.integration.test.ts b/apps/server/src/orchestration-v2/SteeringCompletion.integration.test.ts
index fca2d937212..884683979fb 100644
--- a/apps/server/src/orchestration-v2/SteeringCompletion.integration.test.ts
+++ b/apps/server/src/orchestration-v2/SteeringCompletion.integration.test.ts
@@ -818,3 +818,114 @@ it.effect("keeps a held commentary draft before the steer when turn end flushes
}),
),
);
+
+it.effect("keeps a queued message queued when promoted into a run that Stop reached", () =>
+ Effect.scoped(
+ Effect.gen(function* () {
+ const { started, steered, layer, startFirstTurn } =
+ yield* nextTurnSelectionHarness("steering-after-stop");
+ yield* Effect.gen(function* () {
+ const orchestrator = yield* Orchestrator.OrchestratorV2;
+ const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
+ const threadId = yield* startFirstTurn;
+ const first = started[0]!;
+ yield* orchestrator.dispatch({
+ type: "message.dispatch",
+ commandId: CommandId.make("queued"),
+ threadId,
+ messageId: MessageId.make("message:queued"),
+ text: "queued",
+ attachments: [],
+ modelSelection: runSelection,
+ dispatchMode: { type: "queue_after_active" },
+ createdBy: "user",
+ creationSource: "web",
+ });
+ const queuedRun = (yield* orchestrator.getThreadProjection(threadId)).runs.find(
+ (run) => run.status === "queued",
+ )!;
+ // The worker has not delivered the interrupt yet, so the run still reads as running.
+ yield* orchestrator.dispatch({
+ type: "run.interrupt",
+ commandId: CommandId.make("stop"),
+ threadId,
+ runId: first.runId,
+ holdQueue: true,
+ });
+
+ const promoteError = yield* orchestrator
+ .dispatch({
+ type: "queued-message.promote-to-steer",
+ commandId: CommandId.make("promote"),
+ threadId,
+ queuedRunId: queuedRun.id,
+ targetRunId: first.runId,
+ })
+ .pipe(Effect.flip);
+ yield* worker.drain();
+
+ const projection = yield* orchestrator.getThreadProjection(threadId);
+ assert.equal(promoteError._tag, "OrchestratorDispatchError");
+ assert.deepEqual(steered, []);
+ const queued = projection.runs.find((run) => run.id === queuedRun.id);
+ assert.equal(queued?.status, "queued");
+ assert.isTrue(queued?.queueHeld);
+ }).pipe(Effect.provide(layer));
+ }),
+ ),
+);
+
+it.effect("queues a plain send behind the stopped queue and releases its hold", () =>
+ Effect.scoped(
+ Effect.gen(function* () {
+ const { started, steered, layer, startFirstTurn } =
+ yield* nextTurnSelectionHarness("plain-send-after-stop");
+ yield* Effect.gen(function* () {
+ const orchestrator = yield* Orchestrator.OrchestratorV2;
+ const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
+ const threadId = yield* startFirstTurn;
+ const send = (id: string, delivery: "queue" | "auto") =>
+ orchestrator.dispatch({
+ type: "message.dispatch",
+ commandId: CommandId.make(id),
+ threadId,
+ messageId: MessageId.make(`message:${id}`),
+ text: id,
+ attachments: [],
+ modelSelection: runSelection,
+ ...(delivery === "queue"
+ ? { dispatchMode: { type: "queue_after_active" } }
+ : { dispatchMode: { type: "start_immediately" }, deliveryIntent: "auto" }),
+ createdBy: "user",
+ creationSource: "web",
+ });
+ yield* send("queued", "queue");
+ yield* orchestrator.dispatch({
+ type: "run.interrupt",
+ commandId: CommandId.make("stop"),
+ threadId,
+ runId: started[0]!.runId,
+ holdQueue: true,
+ });
+ // The provider has not stopped yet, so a plain send still resolves to a steer.
+ yield* send("sent", "auto");
+ yield* worker.drain();
+
+ const projection = yield* orchestrator.getThreadProjection(threadId);
+ assert.deepEqual(steered, []);
+ assert.equal(started.length, 1);
+ const queued = projection.runs
+ .filter((run) => run.status === "queued")
+ .sort((left, right) => (left.queuePosition ?? 0) - (right.queuePosition ?? 0));
+ assert.deepEqual(
+ queued.map(
+ (run) => projection.messages.find((message) => message.id === run.userMessageId)?.text,
+ ),
+ ["queued", "sent"],
+ );
+ // The user sent it, so the queue they stopped runs again once the provider stops.
+ assert.isFalse(queued.some((run) => run.queueHeld === true));
+ }).pipe(Effect.provide(layer));
+ }),
+ ),
+);