TypeScript
fix(server): show the database error instead of "Provider turn failed." when event ingestion can't write (#26782)
George Nijo1a3f1459b26d
diff --git a/apps/server/src/orchestration-v2/RunExecutionService.test.ts b/apps/server/src/orchestration-v2/RunExecutionService.test.tsindex 5ccfe080252..dee246855b8 100644--- a/apps/server/src/orchestration-v2/RunExecutionService.test.ts+++ b/apps/server/src/orchestration-v2/RunExecutionService.test.ts@@ -36,7 +36,9 @@ import * as Layer from "effect/Layer";import * as Option from "effect/Option";import * as Ref from "effect/Ref";import * as Stream from "effect/Stream";+import { SqlError, classifySqliteError } from "effect/sql/SqlError";+import { toPersistenceSqlError } from "../persistence/Errors.ts";import * as ServerSettings from "../serverSettings.ts";import * as CheckpointService from "./CheckpointService.ts";import type { PendingOrchestrationEffectV2 } from "./EffectOutbox.ts";@@ -2653,7 +2655,7 @@ it.effect.each(["failure", "defect", "interrupt-failure", "ownership-lost"] as cReadonlyArray<ProviderAdapter.ProviderAdapterV2InterruptInput>>([]);const ids = backgroundScenarioIds(`ingestion:${failureMode}`);- const { observed, finalEvents } = yield* captureRootRunTermination({+ const { written, observed, finalEvents } = yield* captureRootRunTermination({key: `ingestion:${failureMode}`,initialProviderTurnId: null,rejectTerminalWrite: failureMode === "ownership-lost",@@ -2686,7 +2688,13 @@ it.effect.each(["failure", "defect", "interrupt-failure", "ownership-lost"] as cconst error = new ProviderEventIngestor.ProviderEventPublishError({providerSessionId: ProviderSessionId.make(`session:${failureMode}`),eventCount: 1,- cause: new Error("SQLITE_FULL: database or disk is full"),+ cause: toPersistenceSqlError("OrchestrationEventStore.appendAgentEvents:insert")(+ new SqlError({+ reason: classifySqliteError(+ Object.assign(new Error("unable to open database file"), { errcode: 14 }),+ ),+ }),+ ),});return failureMode === "defect" ? Effect.die(error) : Effect.fail(error);},@@ -2730,6 +2738,8 @@ it.effect.each(["failure", "defect", "interrupt-failure", "ownership-lost"] as cfailureMode === "interrupt-failure",);assert.deepEqual(observed, ["run:failed", "pull-requests-refreshed"]);+ const error = written.find((item) => item.type === "error");+ assert.include(error?.failure.message ?? "", "(SQLITE_CANTOPEN)");const repaired = finalEvents.find((event) => event.type === "provider-turn.updated");if (failureMode === "interrupt-failure") {assert.isUndefined(repaired);diff --git a/apps/server/src/orchestration-v2/RunExecutionService.ts b/apps/server/src/orchestration-v2/RunExecutionService.tsindex e9713cfd2a6..22d5b8b879a 100644--- a/apps/server/src/orchestration-v2/RunExecutionService.ts+++ b/apps/server/src/orchestration-v2/RunExecutionService.ts@@ -48,11 +48,13 @@ import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter"import * as ProviderEventIngestor from "./ProviderEventIngestor.ts";import type { ProjectionStoreV2Error } from "./ProjectionStore.ts";import {+ knownProviderFailureMessage,makeProviderFailure,makeProviderFailureTurnItem,} from "@t3tools/provider-core/server/failure";import * as RunFinalizationService from "./RunFinalizationService.ts";import * as TurnItemPositionStore from "./TurnItemPositionStore.ts";+import { userFacingDatabaseErrorMessage } from "./UserFacingErrors.ts";export interface ProviderEventRoutingState {readonly ownedThreadIds: ReadonlySet<ThreadId>;@@ -1554,6 +1556,7 @@ export const layer: Layer.Layer<: null;const latestItemOrdinal = yield* Ref.get(latestTurnItemOrdinal);const openSubagents = yield* Ref.get(openRunOwnedSubagents);+ const squashed = Cause.squash(cause);let nextRetryWarningAt = completedAt;yield* writeFinalRunEvents({run: input.run,@@ -1569,7 +1572,13 @@ export const layer: Layer.Layer<...(abandonedProviderTurn === null ? {} : { abandonedProviderTurn }),completedAt,terminal: makeFailedTerminalEvent(- makeProviderFailure({ cause: Cause.squash(cause), class: "unknown" }),+ makeProviderFailure({+ cause: squashed,+ message:+ knownProviderFailureMessage(squashed) ??+ userFacingDatabaseErrorMessage(squashed),+ class: "unknown",+ }),latestItemOrdinal + 1,),failureItemPersisted: false,diff --git a/apps/server/src/orchestration-v2/UserFacingErrors.ts b/apps/server/src/orchestration-v2/UserFacingErrors.tsindex c47f1c2227d..7d47dffd1a7 100644--- a/apps/server/src/orchestration-v2/UserFacingErrors.ts+++ b/apps/server/src/orchestration-v2/UserFacingErrors.ts@@ -37,6 +37,18 @@ function databaseErrorMessage(value: unknown): string | undefined {return "The T3 Code server could not complete this database request. Retry once; if it still fails, check the server logs for the database error.";}+export function userFacingDatabaseErrorMessage(cause: unknown): string | undefined {+ const seen = new Set<unknown>();+ let value = cause;+ while (Predicate.isObject(value) && !seen.has(value)) {+ seen.add(value);+ const message = databaseErrorMessage(value);+ if (message !== undefined) return message;+ value = value.cause;+ }+ return undefined;+}+const CAPABILITY_REJECTION_MESSAGES: Record<string, (provider: string) => string> = {queued_messages: (p) => `${p} cannot queue messages behind an active run.`,active_steering: (p) =>