Skip to content
TypeScript

fix(server): provider failures say why a turn could not start (#16303)

Alex Southwell300a35670abc
diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts
index 6b936c01fe8..5b22e2f121a 100644
--- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts
+++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts
@@ -8509,12 +8509,10 @@ describe("ClaudeAdapterV2 background wake turns", () => {
);
yield* userTurn("attempt-failed-start-1", 1);
- yield* Queue.offer(harness.sdkMessages, wakeTaskStarted);
- yield* Queue.offer(harness.sdkMessages, turnOneResult);
- yield* awaitUntil(() => harness.terminalEvents().length === 1, "first turn terminal");
- yield* Queue.offer(harness.sdkMessages, wakeNotification);
- let notificationYields = 0;
- yield* awaitUntil(() => notificationYields++ >= 50, "notification to be recorded");
+ yield* harness.offerAndWait(wakeTaskStarted);
+ yield* harness.offerAndWait(turnOneResult);
+ yield* Queue.take(harness.terminalReceipts);
+ yield* harness.offerAndWait(wakeNotification);
assert.isFalse(yield* harness.hasPendingBackgroundWork);
const missing = ChatImageAttachment.make({
@@ -8524,20 +8522,22 @@ describe("ClaudeAdapterV2 background wake turns", () => {
mimeType: "image/png",
sizeBytes: 4,
});
- const failed = yield* Effect.exit(userTurn("attempt-failed-start-2", 2, [missing]));
- assert.isTrue(Exit.isFailure(failed));
+ const failed = yield* userTurn("attempt-failed-start-2", 2, [missing]).pipe(Effect.flip);
+ assert.equal(
+ makeProviderFailure({ cause: failed }).message,
+ "The provider could not start this turn: an attachment could not be read. Retry the turn; if it keeps failing, check the provider setup and server logs.",
+ );
yield* userTurn("attempt-failed-start-3", 2);
- yield* Queue.offer(
- harness.sdkMessages,
+ yield* harness.offerAndWait(
makeResultFrame({ uuid: "00000000-0000-4000-8000-000000000911", result: "Answered." }),
);
- yield* awaitUntil(() => harness.terminalEvents().length === 2, "second turn terminal");
+ yield* Queue.take(harness.terminalReceipts);
- yield* Queue.offer(harness.sdkMessages, wakeAssistant);
- yield* awaitUntil(() => harness.continuationRequests.length === 1, "command wake");
+ yield* harness.offerAndWait(wakeAssistant);
+ const continuation = yield* Queue.take(harness.continuationReceipts);
assert.equal(
- harness.continuationRequests[0]?.notification?.summary,
+ continuation.notification?.summary,
`Command "${WAKE_TASK_DESCRIPTION}" finished`,
);
}).pipe(
diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
index 5a211e37054..30115329be5 100644
--- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
+++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
@@ -1685,6 +1685,7 @@ const makeClaudeUserMessageWithAttachments = Effect.fnUntraced(function* (input:
new ProviderAdapter.ProviderAdapterProtocolError({
driver: CLAUDE_PROVIDER,
detail: `Failed to read attachment '${attachment.id}'`,
+ reason: "attachment_unreadable",
payload: cause,
}),
),
@@ -8760,6 +8761,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
return yield* new ProviderAdapter.ProviderAdapterProtocolError({
driver: CLAUDE_PROVIDER,
detail: `Claude provider turn ${currentTurn.providerTurnId} is still active.`,
+ reason: "previous_turn_active",
});
}
yield* Ref.update(lastTurnRouteByNativeThread, (current) => {
diff --git a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts
index 928ae4a7e13..a4f46514204 100644
--- a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts
+++ b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts
@@ -3126,13 +3126,17 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
if (attachmentPath === null) {
return yield* toProtocolError(`Invalid attachment id '${attachment.id}'`);
}
- const bytes = yield* fileSystem
- .readFile(attachmentPath)
- .pipe(
- Effect.mapError((cause) =>
- toProtocolError(`Failed to read attachment '${attachment.id}'.`, cause),
- ),
- );
+ const bytes = yield* fileSystem.readFile(attachmentPath).pipe(
+ Effect.mapError(
+ (cause) =>
+ new ProviderAdapter.ProviderAdapterProtocolError({
+ driver: CODEX_PROVIDER,
+ detail: `Failed to read attachment '${attachment.id}'.`,
+ reason: "attachment_unreadable",
+ payload: cause,
+ }),
+ ),
+ );
return {
type: "image" as const,
url: `data:${attachment.mimeType};base64,${Buffer.from(bytes).toString("base64")}`,
diff --git a/packages/provider-acp/src/server/adapter.ts b/packages/provider-acp/src/server/adapter.ts
index e65c2fea611..c23f2b99a91 100644
--- a/packages/provider-acp/src/server/adapter.ts
+++ b/packages/provider-acp/src/server/adapter.ts
@@ -6354,6 +6354,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
new ProviderAdapter.ProviderAdapterProtocolError({
driver,
detail: `Failed to read attachment '${attachment.id}'`,
+ reason: "attachment_unreadable",
payload: cause,
}),
),
@@ -6411,6 +6412,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
return yield* new ProviderAdapter.ProviderAdapterProtocolError({
driver,
detail: `ACP provider turn ${existing.providerTurnId} is still active`,
+ reason: "previous_turn_active",
});
}
useProviderThreadIdentity(turnInput.providerThread);
diff --git a/packages/provider-core/src/server/ProviderAdapter.ts b/packages/provider-core/src/server/ProviderAdapter.ts
index d9a0f2fc95e..d58a6c5904a 100644
--- a/packages/provider-core/src/server/ProviderAdapter.ts
+++ b/packages/provider-core/src/server/ProviderAdapter.ts
@@ -360,11 +360,19 @@ export class ProviderAdapterEventStreamError extends Schema.TaggedError<Provider
}
}
+// Only fixed reasons reach failure messages; detail can contain private configuration.
+const ProviderAdapterFailureReason = Schema.Literals([
+ "previous_turn_active",
+ "attachment_unreadable",
+]);
+export type ProviderAdapterFailureReason = typeof ProviderAdapterFailureReason.Type;
+
export class ProviderAdapterProtocolError extends Schema.TaggedError<ProviderAdapterProtocolError>()(
"ProviderAdapterProtocolError",
{
driver: ProviderDriverKind,
detail: Schema.String,
+ reason: Schema.optional(ProviderAdapterFailureReason),
cause: Schema.optional(Schema.Defect()),
payload: Schema.optional(Schema.Unknown),
},
diff --git a/packages/provider-core/src/server/failure.test.ts b/packages/provider-core/src/server/failure.test.ts
index a4429213db2..175c3a2526c 100644
--- a/packages/provider-core/src/server/failure.test.ts
+++ b/packages/provider-core/src/server/failure.test.ts
@@ -101,6 +101,65 @@ it("preserves actionable handoff errors wrapped by turn startup", () => {
);
});
+it.each([
+ ["previous_turn_active", "the previous turn is still running"],
+ ["attachment_unreadable", "an attachment could not be read"],
+] as const)("names %s without exposing provider detail or losing guidance", (reason, wording) => {
+ const failure = makeProviderFailure({
+ cause: Cause.fail({
+ _tag: "ProviderAdapterTurnStartError",
+ cause: Cause.fail(
+ new ProviderAdapter.ProviderAdapterProtocolError({
+ driver: ProviderDriverKind.make("claudeAgent"),
+ detail: `Private configuration: ${"x".repeat(5000)}`,
+ reason,
+ }),
+ ),
+ }),
+ });
+ assert.equal(
+ failure.message,
+ `The provider could not start this turn: ${wording}. Retry the turn; if it keeps failing, check the provider setup and server logs.`,
+ );
+});
+
+it("keeps unrecognized reasons and provider text out of category messages", () => {
+ for (const cause of [
+ "Private launch command",
+ { _tag: "ProviderAdapterProtocolError", detail: "Pi model 'private-config' is invalid" },
+ { _tag: "ProviderAdapterProtocolError", reason: "private-config" },
+ { _tag: "ProviderAdapterProtocolError", reason: "constructor" },
+ { _tag: "constructor", cause: "private-config" },
+ ]) {
+ assert.equal(
+ makeProviderFailure({ cause: { _tag: "ProviderAdapterTurnStartError", cause } }).message,
+ "The provider could not start this turn. Retry the turn; if it keeps failing, check the provider setup and server logs.",
+ );
+ }
+ assert.equal(
+ makeProviderFailure({
+ cause: { _tag: "ProviderAdapterProtocolError", reason: "previous_turn_active" },
+ }).message,
+ "Provider turn failed.",
+ );
+});
+
+it("uses the innermost category without carrying an outer reason into it", () => {
+ assert.equal(
+ makeProviderFailure({
+ cause: {
+ _tag: "ProviderAdapterTurnStartError",
+ cause: {
+ _tag: "ProviderAdapterProtocolError",
+ reason: "previous_turn_active",
+ cause: Cause.fail({ _tag: "ProviderAdapterOpenSessionError" }),
+ },
+ },
+ }).message,
+ "The provider session could not be opened. Check that the provider is installed and signed in, then retry the turn.",
+ );
+});
+
it("does not expose defect text nested inside a known error category", () => {
const failure = makeProviderFailure({
cause: {
diff --git a/packages/provider-core/src/server/failure.ts b/packages/provider-core/src/server/failure.ts
index d5dfad31a1c..94441085a2a 100644
--- a/packages/provider-core/src/server/failure.ts
+++ b/packages/provider-core/src/server/failure.ts
@@ -14,6 +14,7 @@ import * as DateTime from "effect/DateTime";
import * as Cause from "effect/Cause";
import * as IdAllocator from "./IdAllocator.ts";
+import type { ProviderAdapterFailureReason } from "./ProviderAdapter.ts";
import * as Schema from "effect/Schema";
export class ContextHandoffBudgetError extends Schema.TaggedError<ContextHandoffBudgetError>()(
@@ -30,9 +31,36 @@ export const MAX_PROVIDER_FAILURE_CODE_LENGTH = 128;
const DEFAULT_PROVIDER_FAILURE_MESSAGE = "Provider turn failed.";
+const PROVIDER_FAILURE_CATEGORIES: Partial<
+ Record<string, { readonly summary: string; readonly guidance: string }>
+> = {
+ ProviderAdapterTurnStartError: {
+ summary: "The provider could not start this turn",
+ guidance: "Retry the turn; if it keeps failing, check the provider setup and server logs.",
+ },
+ ProviderAdapterEventStreamError: {
+ summary: "The provider event stream closed unexpectedly",
+ guidance: "Retry the turn; if it keeps failing, check the provider and server logs.",
+ },
+ ProviderAdapterOpenSessionError: {
+ summary: "The provider session could not be opened",
+ guidance: "Check that the provider is installed and signed in, then retry the turn.",
+ },
+ ProviderAdapterResumeThreadError: {
+ summary: "The provider conversation could not be resumed",
+ guidance: "Retry the turn; if it keeps failing, check the provider and server logs.",
+ },
+};
+
+const PROVIDER_FAILURE_REASONS: Record<ProviderAdapterFailureReason, string> = {
+ previous_turn_active: "the previous turn is still running",
+ attachment_unreadable: "an attachment could not be read",
+};
+
function causeMessage(cause: unknown): string | undefined {
const seen = new Set<unknown>();
- let message: string | undefined;
+ let category: (typeof PROVIDER_FAILURE_CATEGORIES)[string];
+ let reason: string | undefined;
for (let depth = 0; depth < 16 && cause != null && !seen.has(cause); depth++) {
seen.add(cause);
try {
@@ -41,7 +69,8 @@ function causeMessage(cause: unknown): string | undefined {
continue;
}
if (typeof cause !== "object") break;
- switch ((cause as Record<string, unknown>)._tag) {
+ const tag = (cause as Record<string, unknown>)._tag;
+ switch (tag) {
case "ContextHandoffBudgetError":
return new ContextHandoffBudgetError().message;
case "ClaudeBackgroundWorkBlocksQueryReplacementError":
@@ -49,29 +78,31 @@ function causeMessage(cause: unknown): string | undefined {
return stringField(cause, "message");
case "ContextHandoffDeliveryUncertainError":
return "T3 could not confirm whether conversation history reached the provider. Retry the turn to recover the session.";
- case "ProviderAdapterTurnStartError":
- message =
- "The provider could not start this turn. Retry the turn; if it keeps failing, check the provider setup and server logs.";
- break;
- case "ProviderAdapterEventStreamError":
- message =
- "The provider event stream closed unexpectedly. Retry the turn; if it keeps failing, check the provider and server logs.";
- break;
- case "ProviderAdapterOpenSessionError":
- message =
- "The provider session could not be opened. Check that the provider is installed and signed in, then retry the turn.";
- break;
- case "ProviderAdapterResumeThreadError":
- message =
- "The provider conversation could not be resumed. Retry the turn; if it keeps failing, check the provider and server logs.";
+ case "ProviderAdapterProtocolError": {
+ const typedReason = (cause as Record<string, unknown>).reason;
+ if (
+ category !== undefined &&
+ typeof typedReason === "string" &&
+ Object.hasOwn(PROVIDER_FAILURE_REASONS, typedReason)
+ ) {
+ reason ??= PROVIDER_FAILURE_REASONS[typedReason as ProviderAdapterFailureReason];
+ }
break;
+ }
+ }
+ if (typeof tag === "string" && Object.hasOwn(PROVIDER_FAILURE_CATEGORIES, tag)) {
+ category = PROVIDER_FAILURE_CATEGORIES[tag];
+ reason = undefined;
}
cause = (cause as Record<string, unknown>).cause;
} catch {
break;
}
}
- return message;
+ if (category === undefined) return undefined;
+ return reason
+ ? `${category.summary}: ${reason}. ${category.guidance}`
+ : `${category.summary}. ${category.guidance}`;
}
function stringField(value: unknown, key: "message" | "code"): string | undefined {
diff --git a/packages/provider-cursor/src/server/adapter.ts b/packages/provider-cursor/src/server/adapter.ts
index ae495016e9f..c9898a507fb 100644
--- a/packages/provider-cursor/src/server/adapter.ts
+++ b/packages/provider-cursor/src/server/adapter.ts
@@ -2199,6 +2199,7 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* (
new ProviderAdapter.ProviderAdapterProtocolError({
driver: CursorAgentSdk.CURSOR_PROVIDER,
detail: `Failed to read attachment '${attachment.id}'.`,
+ reason: "attachment_unreadable",
payload: cause,
}),
),
@@ -2232,6 +2233,7 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* (
return yield* new ProviderAdapter.ProviderAdapterProtocolError({
driver: CursorAgentSdk.CURSOR_PROVIDER,
detail: `Cursor provider turn ${current.providerTurnId} is still active.`,
+ reason: "previous_turn_active",
});
}
const agentId = nativeThreadId(turnInput.providerThread);
diff --git a/packages/provider-muse/src/server/adapter.ts b/packages/provider-muse/src/server/adapter.ts
index d61462576b6..d762c85d976 100644
--- a/packages/provider-muse/src/server/adapter.ts
+++ b/packages/provider-muse/src/server/adapter.ts
@@ -1904,9 +1904,11 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
if (!validateThread(turnInput.providerThread) || broken || closed)
return yield* protocolError("Muse thread is not attached to this host");
if (active)
- return yield* protocolError(
- "Muse already has an active turn; queue through the orchestrator",
- );
+ return yield* new ProviderAdapter.ProviderAdapterProtocolError({
+ driver: MUSE_PROVIDER,
+ detail: "Muse already has an active turn; queue through the orchestrator",
+ reason: "previous_turn_active",
+ });
if (turnInput.runtimePolicy.interactionMode === "plan")
return yield* protocolError("Muse does not support dedicated Plan mode");
if (turnInput.runtimePolicy.runtimeMode !== input.runtimePolicy.runtimeMode)
diff --git a/packages/provider-opencode/src/server/adapter.ts b/packages/provider-opencode/src/server/adapter.ts
index 71144f24dd7..4a73195d2dc 100644
--- a/packages/provider-opencode/src/server/adapter.ts
+++ b/packages/provider-opencode/src/server/adapter.ts
@@ -3094,9 +3094,11 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
return yield* protocolError(`OpenCode session ${sessionId} is not registered`);
}
if (state.activeTurn !== null) {
- return yield* protocolError(
- `OpenCode provider thread ${turnInput.providerThread.id} already has an active turn`,
- );
+ return yield* new ProviderAdapter.ProviderAdapterProtocolError({
+ driver: OPENCODE_PROVIDER,
+ detail: `OpenCode provider thread ${turnInput.providerThread.id} already has an active turn`,
+ reason: "previous_turn_active",
+ });
}
const parsedModel = OpenCodeRuntime.parseOpenCodeModelSlug(
turnInput.modelSelection.model,
@@ -3138,9 +3140,11 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
);
}
if (state.activeTurn !== null) {
- return yield* protocolError(
- `OpenCode provider thread ${turnInput.providerThread.id} already has an active turn`,
- );
+ return yield* new ProviderAdapter.ProviderAdapterProtocolError({
+ driver: OPENCODE_PROVIDER,
+ detail: `OpenCode provider thread ${turnInput.providerThread.id} already has an active turn`,
+ reason: "previous_turn_active",
+ });
}
const turn: ActiveOpenCodeTurn = {
isRoot: true,
diff --git a/packages/provider-opencode/src/server/v2/adapter.ts b/packages/provider-opencode/src/server/v2/adapter.ts
index 17214ae2116..736a01d715c 100644
--- a/packages/provider-opencode/src/server/v2/adapter.ts
+++ b/packages/provider-opencode/src/server/v2/adapter.ts
@@ -3441,6 +3441,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
return yield* new ProviderAdapter.ProviderAdapterProtocolError({
driver: OPENCODE_PROVIDER,
detail: `OpenCode session ${sessionId} already has an active turn`,
+ reason: "previous_turn_active",
});
}
if (isContinuation(turnInput)) return yield* runWake(state, turnInput);
diff --git a/packages/provider-pi/src/server/adapter.ts b/packages/provider-pi/src/server/adapter.ts
index 25c3984246e..1d33720f3cb 100644
--- a/packages/provider-pi/src/server/adapter.ts
+++ b/packages/provider-pi/src/server/adapter.ts
@@ -2418,9 +2418,11 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
return yield* protocolError("Pi session has no registered thread");
}
if (state.activeTurn !== null) {
- return yield* protocolError(
- `Pi provider thread ${turnInput.providerThread.id} already has an active turn`,
- );
+ return yield* new ProviderAdapter.ProviderAdapterProtocolError({
+ driver: PI_PROVIDER,
+ detail: `Pi provider thread ${turnInput.providerThread.id} already has an active turn`,
+ reason: "previous_turn_active",
+ });
}
if (
state.providerThread.nativeThreadRef?.nativeId !==