Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions apps/server/src/orchestration-v2/Orchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2602,22 +2602,32 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
const automaticQueuedRuns = projection.runs.filter(
(run) => run.status === "queued" && automaticMessageIds.has(run.userMessageId),
);
const activeRunExists = projection.runs.some(
(run) =>
["preparing", "queued", "starting", "running", "waiting"].includes(run.status) &&
!automaticQueuedRuns.includes(run),
const runningRunExists = projection.runs.some((run) =>
["preparing", "starting", "running", "waiting"].includes(run.status),
);
// Includes a queue held after a Stop, which otherwise shows no activity.
const queuedMessageExists = projection.runs.some(
(run) => run.status === "queued" && !automaticQueuedRuns.includes(run),
);
const pendingRequests = projection.runtimeRequests.filter(
(request) => request.status === "pending",
);
const blockingRequestExists = pendingRequests.some(
(request) => request.kind !== "user_input" || request.responseCapability.type !== "message",
);
if (activeRunExists || blockingRequestExists) {
// The reason is shown to the user, so it names what to clear first.
const blocker = blockingRequestExists
? "is waiting on an approval or question. Answer it before settling."
: runningRunExists
? "is still running. Stop it before settling."
: queuedMessageExists
? "has a queued message. Send it or remove it from the queue before settling."
: null;
if (blocker !== null) {
return yield* new OrchestratorDispatchError({
commandId: command.commandId,
commandType: command.type,
cause: `Thread ${command.threadId} has active or blocked work and cannot be settled.`,
cause: `Thread ${command.threadId} ${blocker}`,
});
}

Expand Down
5 changes: 5 additions & 0 deletions apps/server/src/orchestration-v2/runtimeLayer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3365,6 +3365,7 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
.pipe(Effect.flip);

assert.equal(error._tag, "OrchestratorDispatchError");
assert.equal(error.cause, `Thread ${threadId} is still running. Stop it before settling.`);
const projection = yield* orchestrator.getThreadProjection(threadId);
assert.equal(projection.runs[0]?.status, "starting");
assert.isNull(projection.thread.settledOverride);
Expand Down Expand Up @@ -3563,6 +3564,10 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
})
.pipe(Effect.flip);
assert.equal(error._tag, "OrchestratorDispatchError");
assert.equal(
error.cause,
`Thread ${threadId} has a queued message. Send it or remove it from the queue before settling.`,
);
}),
);

Expand Down
Loading