Skip to content
TypeScript

fix(server): say why a thread can't be settled (#17258)

Dylan Corsoa5b482452dc8
diff --git a/apps/server/src/orchestration-v2/Orchestrator.ts b/apps/server/src/orchestration-v2/Orchestrator.ts
index 3f17a6faa55..94560f62f6f 100644
--- a/apps/server/src/orchestration-v2/Orchestrator.ts
+++ b/apps/server/src/orchestration-v2/Orchestrator.ts
@@ -2605,10 +2605,12 @@ 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",
@@ -2616,11 +2618,19 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
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}`,
});
}
diff --git a/apps/server/src/orchestration-v2/runtimeLayer.test.ts b/apps/server/src/orchestration-v2/runtimeLayer.test.ts
index 3d4bf4c2a64..42c47bba51c 100644
--- a/apps/server/src/orchestration-v2/runtimeLayer.test.ts
+++ b/apps/server/src/orchestration-v2/runtimeLayer.test.ts
@@ -3389,6 +3389,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);
@@ -3587,6 +3588,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.`,
+ );
}),
);