Skip to content

Commit 10f7db7

Browse files
lbgosjuliusmarmingeclaude
authored
fix(server): a message promoted into a stopping run stays queued (#16926)
Co-authored-by: Julius Marminge <[email protected]> Co-authored-by: Claude Fable 5.1 <[email protected]>
1 parent 771fd93 commit 10f7db7

2 files changed

Lines changed: 131 additions & 0 deletions

File tree

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3958,6 +3958,15 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
39583958
cause: `Target run ${targetRun.id} is ${targetRun.status} and cannot be steered.`,
39593959
});
39603960
}
3961+
// The run reads as running until its provider stops, but a steer accepted
3962+
// after Stop never reaches the agent.
3963+
if (yield* stopReachedRun(input.command, input.command.threadId, targetRun.id)) {
3964+
return yield* new OrchestratorDispatchError({
3965+
commandId: input.command.commandId,
3966+
commandType: input.command.type,
3967+
cause: `Target run ${targetRun.id} is stopping and cannot be steered.`,
3968+
});
3969+
}
39613970
const providerThread = input.projection.providerThreads.find(
39623971
(candidate) => candidate.id === targetRun.providerThreadId,
39633972
);
@@ -4804,6 +4813,17 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
48044813
dispatchMode = { type: "start_immediately" };
48054814
}
48064815
}
4816+
// Stop leaves the run reading as running until its provider stops. A plain
4817+
// send in that window queues behind the stopped queue instead of steering
4818+
// text the agent never receives, and like any user send it releases the
4819+
// hold; an explicit steer or restart still fails below.
4820+
if (
4821+
command.deliveryIntent === "auto" &&
4822+
(dispatchMode.type === "steer_active" || dispatchMode.type === "restart_active") &&
4823+
(yield* stopReachedRun(command, command.threadId, dispatchMode.targetRunId))
4824+
) {
4825+
dispatchMode = { type: "queue_after_active" };
4826+
}
48074827
if (
48084828
command.notification !== undefined &&
48094829
(command.createdBy !== "agent" ||

‎apps/server/src/orchestration-v2/SteeringCompletion.integration.test.ts‎

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,3 +818,114 @@ it.effect("keeps a held commentary draft before the steer when turn end flushes
818818
}),
819819
),
820820
);
821+
822+
it.effect("keeps a queued message queued when promoted into a run that Stop reached", () =>
823+
Effect.scoped(
824+
Effect.gen(function* () {
825+
const { started, steered, layer, startFirstTurn } =
826+
yield* nextTurnSelectionHarness("steering-after-stop");
827+
yield* Effect.gen(function* () {
828+
const orchestrator = yield* Orchestrator.OrchestratorV2;
829+
const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
830+
const threadId = yield* startFirstTurn;
831+
const first = started[0]!;
832+
yield* orchestrator.dispatch({
833+
type: "message.dispatch",
834+
commandId: CommandId.make("queued"),
835+
threadId,
836+
messageId: MessageId.make("message:queued"),
837+
text: "queued",
838+
attachments: [],
839+
modelSelection: runSelection,
840+
dispatchMode: { type: "queue_after_active" },
841+
createdBy: "user",
842+
creationSource: "web",
843+
});
844+
const queuedRun = (yield* orchestrator.getThreadProjection(threadId)).runs.find(
845+
(run) => run.status === "queued",
846+
)!;
847+
// The worker has not delivered the interrupt yet, so the run still reads as running.
848+
yield* orchestrator.dispatch({
849+
type: "run.interrupt",
850+
commandId: CommandId.make("stop"),
851+
threadId,
852+
runId: first.runId,
853+
holdQueue: true,
854+
});
855+
856+
const promoteError = yield* orchestrator
857+
.dispatch({
858+
type: "queued-message.promote-to-steer",
859+
commandId: CommandId.make("promote"),
860+
threadId,
861+
queuedRunId: queuedRun.id,
862+
targetRunId: first.runId,
863+
})
864+
.pipe(Effect.flip);
865+
yield* worker.drain();
866+
867+
const projection = yield* orchestrator.getThreadProjection(threadId);
868+
assert.equal(promoteError._tag, "OrchestratorDispatchError");
869+
assert.deepEqual(steered, []);
870+
const queued = projection.runs.find((run) => run.id === queuedRun.id);
871+
assert.equal(queued?.status, "queued");
872+
assert.isTrue(queued?.queueHeld);
873+
}).pipe(Effect.provide(layer));
874+
}),
875+
),
876+
);
877+
878+
it.effect("queues a plain send behind the stopped queue and releases its hold", () =>
879+
Effect.scoped(
880+
Effect.gen(function* () {
881+
const { started, steered, layer, startFirstTurn } =
882+
yield* nextTurnSelectionHarness("plain-send-after-stop");
883+
yield* Effect.gen(function* () {
884+
const orchestrator = yield* Orchestrator.OrchestratorV2;
885+
const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
886+
const threadId = yield* startFirstTurn;
887+
const send = (id: string, delivery: "queue" | "auto") =>
888+
orchestrator.dispatch({
889+
type: "message.dispatch",
890+
commandId: CommandId.make(id),
891+
threadId,
892+
messageId: MessageId.make(`message:${id}`),
893+
text: id,
894+
attachments: [],
895+
modelSelection: runSelection,
896+
...(delivery === "queue"
897+
? { dispatchMode: { type: "queue_after_active" } }
898+
: { dispatchMode: { type: "start_immediately" }, deliveryIntent: "auto" }),
899+
createdBy: "user",
900+
creationSource: "web",
901+
});
902+
yield* send("queued", "queue");
903+
yield* orchestrator.dispatch({
904+
type: "run.interrupt",
905+
commandId: CommandId.make("stop"),
906+
threadId,
907+
runId: started[0]!.runId,
908+
holdQueue: true,
909+
});
910+
// The provider has not stopped yet, so a plain send still resolves to a steer.
911+
yield* send("sent", "auto");
912+
yield* worker.drain();
913+
914+
const projection = yield* orchestrator.getThreadProjection(threadId);
915+
assert.deepEqual(steered, []);
916+
assert.equal(started.length, 1);
917+
const queued = projection.runs
918+
.filter((run) => run.status === "queued")
919+
.sort((left, right) => (left.queuePosition ?? 0) - (right.queuePosition ?? 0));
920+
assert.deepEqual(
921+
queued.map(
922+
(run) => projection.messages.find((message) => message.id === run.userMessageId)?.text,
923+
),
924+
["queued", "sent"],
925+
);
926+
// The user sent it, so the queue they stopped runs again once the provider stops.
927+
assert.isFalse(queued.some((run) => run.queueHeld === true));
928+
}).pipe(Effect.provide(layer));
929+
}),
930+
),
931+
);

0 commit comments

Comments
 (0)