Skip to content

Commit df9bf1b

Browse files
nullStack65claude
authored andcommitted
feat(server): typed provider errors and guarded cross-provider failover
Failed runs now keep the provider's HTTP status, and readback classes the failure as payment, rate_limit, auth, quota or other. A message.dispatch can continue a failed run on the first eligible candidate of an ordered list, admitted atomically in dispatchMessage only for those classes, with no open request, no item that may have acted, an included and enabled target not yet tried in the lineage, and a bounded lineage. Repeats are accepted once. The Closura providerFailover setting ships off. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 63c2b61 commit df9bf1b

10 files changed

Lines changed: 906 additions & 24 deletions

File tree

‎apps/server/src/mcp/OrchestratorMcpService.ts‎

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,15 @@ import {
6969
agentLaunchInboxError,
7070
agentLaunchInboxRouting,
7171
withLegacyPeerInboxReason,
72+
orchestrationV2RunFailure,
7273
} from "@t3tools/contracts";
7374
import { getProviderOptionCurrentValue, withLegacyUltracodeEffort } from "@t3tools/shared/model";
7475
import { backgroundWorkHoldsCompletion } from "@t3tools/shared/orchestrationV2PendingBackgroundWork";
75-
import { latestExecutedRun, runRanAfter } from "@t3tools/shared/orchestrationV2ThreadError";
76+
import {
77+
latestExecutedRun,
78+
latestRootProviderFailure,
79+
runRanAfter,
80+
} from "@t3tools/shared/orchestrationV2ThreadError";
7681
import { resolveProjectSettings } from "@t3tools/shared/projectSettings";
7782
import { formatThreadLink } from "@t3tools/shared/threadLinks";
7883
import {
@@ -1058,11 +1063,25 @@ function runExecutedModel(
10581063
: (latestProviderTurnForAttempt(projection.providerTurns, run.activeAttemptId)?.model ?? null);
10591064
}
10601065

1066+
/** A failed run's typed provider error, read from its own error items only. */
1067+
function runFailure(
1068+
run: OrchestrationV2Run | undefined,
1069+
turnItems: Parameters<typeof latestRootProviderFailure>[1],
1070+
) {
1071+
const failure = run === undefined ? null : latestRootProviderFailure(run, turnItems);
1072+
return run === undefined || failure === null
1073+
? undefined
1074+
: orchestrationV2RunFailure(run, failure);
1075+
}
1076+
10611077
function threadRun(
10621078
projection: Pick<OrchestrationV2ThreadProjection, "providerTurns">,
10631079
run: OrchestrationV2Run,
10641080
): OrchestratorMcpThreadRun {
10651081
return {
1082+
...(run.providerFailoverOfRunId === undefined
1083+
? {}
1084+
: { providerFailoverOfRunId: run.providerFailoverOfRunId }),
10661085
runId: run.id,
10671086
ordinal: run.ordinal,
10681087
status: run.status,
@@ -1841,6 +1860,7 @@ const make = Effect.gen(function* () {
18411860
instanceId: task.providerInstanceId,
18421861
model: task.model,
18431862
};
1863+
const latestTerminalFailure = runFailure(terminalRun, childProjection.turnItems);
18441864
const result = {
18451865
taskId: task.id,
18461866
childThreadId: task.childThreadId,
@@ -1864,6 +1884,7 @@ const make = Effect.gen(function* () {
18641884
latestTerminalStatus:
18651885
terminalStatus !== null && isTerminalTaskStatus(terminalStatus) ? terminalStatus : null,
18661886
latestTerminalSummary: latestTerminalSummary === derivedResult ? null : latestTerminalSummary,
1887+
...(latestTerminalFailure === undefined ? {} : { latestTerminalFailure }),
18671888
latestTerminalResultContextTransferId:
18681889
latestTerminalResultContextTransferId === resultContextTransferId
18691890
? null
@@ -3482,30 +3503,34 @@ const make = Effect.gen(function* () {
34823503
const runs = target.runs
34833504
.toSorted((left, right) => right.ordinal - left.ordinal)
34843505
.slice(0, input.runLimit ?? DEFAULT_THREAD_RUN_LIMIT);
3485-
// Only an agent-created thread's latest run is judged, so a read loads one run's replies and notices.
3506+
// Only the latest run is judged, and only when it is an agent's or failed, so a read loads
3507+
// one run's replies and notices.
34863508
const latestRun = runs[0];
3487-
const latestAlert =
3509+
const latestRecords =
34883510
latestRun === undefined ||
3489-
target.thread.createdBy !== "agent" ||
3511+
(target.thread.createdBy !== "agent" && latestRun.status !== "failed") ||
34903512
!ThreadManagementService.isTerminalRunStatus(latestRun.status)
34913513
? undefined
3492-
: subagentResultForRun(
3493-
{
3494-
...target,
3495-
...(yield* threadManagement
3496-
.getThreadRecords(input.threadId, ["messages", "turnItems"], {
3497-
messageRoles: ["assistant"],
3498-
messageRunIds: [latestRun.id],
3499-
turnItemRunId: latestRun.id,
3500-
turnItemTypes: ["assistant_message", "error", "system_notice"],
3501-
})
3502-
.pipe(Effect.mapError(threadManagementFailure))),
3503-
},
3504-
latestRun,
3505-
).workerAlert;
3514+
: yield* threadManagement
3515+
.getThreadRecords(input.threadId, ["messages", "turnItems"], {
3516+
messageRoles: ["assistant"],
3517+
messageRunIds: [latestRun.id],
3518+
turnItemRunId: latestRun.id,
3519+
turnItemTypes: ["assistant_message", "error", "system_notice"],
3520+
})
3521+
.pipe(Effect.mapError(threadManagementFailure));
3522+
const latestAlert =
3523+
latestRun === undefined ||
3524+
latestRecords === undefined ||
3525+
target.thread.createdBy !== "agent"
3526+
? undefined
3527+
: subagentResultForRun({ ...target, ...latestRecords }, latestRun).workerAlert;
3528+
const latestFailure =
3529+
latestRecords === undefined ? undefined : runFailure(latestRun, latestRecords.turnItems);
35063530
const recentRuns = runs.map((run) => ({
35073531
...threadRun(target, run),
35083532
...(run === latestRun && latestAlert !== undefined ? { workerAlert: latestAlert } : {}),
3533+
...(run === latestRun && latestFailure !== undefined ? { failure: latestFailure } : {}),
35093534
}));
35103535
const { items, start } = timelineItemsWithinBudget(
35113536
{ thread, recentRuns, nextPosition: null, hasMore: false, earlierPosition: null },

‎apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5248,6 +5248,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
52485248
payload.error.codexErrorInfo,
52495249
notificationCode,
52505250
),
5251+
httpStatus: codexErrorHttpStatus(payload.error.codexErrorInfo),
52515252
}),
52525253
payload.error,
52535254
),
@@ -5278,6 +5279,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
52785279
? "transport_error"
52795280
: "provider_error",
52805281
retryable: true,
5282+
httpStatus: codexErrorHttpStatus(payload.error.codexErrorInfo),
52815283
}),
52825284
payload.error,
52835285
);
@@ -6442,6 +6444,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
64426444
message: input.failureMessage,
64436445
code: failureCode,
64446446
class: failureClass,
6447+
httpStatus: codexErrorHttpStatus(input.failureErrorInfo),
64456448
}),
64466449
input.failureMessage,
64476450
);

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
usageLimitBlockedRun,
2424
} from "@t3tools/shared/orchestrationV2ThreadError";
2525
import { canonicalizeGitCafePullRequestUrl } from "@t3tools/source-control-definitions/registry";
26+
import { admitProviderFailover } from "./providerFailover.ts";
2627
import { threadPullRequestsOf } from "../sourceControl/threadPullRequests.ts";
2728
import {
2829
normalizeThreadPullRequestKey,
@@ -1097,6 +1098,7 @@ function carriesDigest(
10971098
| "restartContinuationOfRunId"
10981099
| "manualContinuationOfRunId"
10991100
| "usageLimitContinuationOfRunId"
1101+
| "providerFailoverOfRunId"
11001102
>,
11011103
message: {
11021104
readonly text: string;
@@ -1109,6 +1111,7 @@ function carriesDigest(
11091111
run.restartContinuationOfRunId === undefined &&
11101112
run.manualContinuationOfRunId === undefined &&
11111113
run.usageLimitContinuationOfRunId === undefined &&
1114+
run.providerFailoverOfRunId === undefined &&
11121115
message.idleCompaction === undefined &&
11131116
!isNativeMaintenanceCommand(message) &&
11141117
parseGoalCommand(message) === null
@@ -7061,6 +7064,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
70617064
command.manualContinuationOfRunId,
70627065
command.restartContinuationOfRunId,
70637066
command.usageLimitContinuationOfRunId,
7067+
command.providerFailover?.ofRunId,
70647068
command.steerFollowUpOfRunId,
70657069
...(command.dispatchMode.type === "steer_active" ||
70667070
command.dispatchMode.type === "restart_active"
@@ -7122,6 +7126,44 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
71227126
});
71237127
return;
71247128
}
7129+
if (command.providerFailover !== undefined) {
7130+
const admission =
7131+
command.dispatchMode.type === "start_immediately" && command.modelSelection === undefined
7132+
? admitProviderFailover(
7133+
command.providerFailover,
7134+
projection,
7135+
// The command projection keeps only user messages and errors; effects need every item.
7136+
(yield* projectionStore
7137+
.getThreadRecords(command.threadId, ["turnItems"], {
7138+
turnItemRunId: command.providerFailover.ofRunId,
7139+
})
7140+
.pipe(mapDispatchError(command))).turnItems,
7141+
yield* serverSettings.getSettings.pipe(Effect.orDie),
7142+
)
7143+
: null;
7144+
if (admission?.type === "duplicate") {
7145+
yield* emit(
7146+
events,
7147+
command,
7148+
)({
7149+
type: "thread.metadata-updated",
7150+
threadId: command.threadId,
7151+
occurredAt: yield* DateTime.now,
7152+
payload: projection.thread,
7153+
});
7154+
return;
7155+
}
7156+
if (admission?.type !== "admitted") {
7157+
return yield* new OrchestratorDispatchError({
7158+
commandId: command.commandId,
7159+
commandType: command.type,
7160+
cause:
7161+
admission?.refusal ??
7162+
"A provider failover starts immediately and takes its model from its candidates.",
7163+
});
7164+
}
7165+
command = { ...command, modelSelection: admission.target };
7166+
}
71257167
const activeProviderInstanceId = projection.providerThreads.find(
71267168
(candidate) => candidate.id === projection.thread.activeProviderThreadId,
71277169
)?.providerInstanceId;
@@ -8116,6 +8158,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
81168158
...(command.usageLimitContinuationOfRunId === undefined
81178159
? {}
81188160
: { usageLimitContinuationOfRunId: command.usageLimitContinuationOfRunId }),
8161+
...(command.providerFailover === undefined
8162+
? {}
8163+
: { providerFailoverOfRunId: command.providerFailover.ofRunId }),
81198164
...(continuedTaskId === undefined ? {} : { delegatedTaskId: continuedTaskId }),
81208165
...(freshContext ? { freshContext: true } : {}),
81218166
...(command.delivery === undefined ? {} : { delivery: command.delivery }),
@@ -8534,6 +8579,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
85348579
...(command.usageLimitContinuationOfRunId === undefined
85358580
? {}
85368581
: { usageLimitContinuationOfRunId: command.usageLimitContinuationOfRunId }),
8582+
...(command.providerFailover === undefined
8583+
? {}
8584+
: { providerFailoverOfRunId: command.providerFailover.ofRunId }),
85378585
...(dispatchMode.type === "defer_start" && dispatchMode.workspaceStrategy !== undefined
85388586
? { workspacePreparation: dispatchMode.workspaceStrategy }
85398587
: {}),
@@ -9346,6 +9394,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
93469394
...(command.usageLimitContinuationOfRunId === undefined
93479395
? {}
93489396
: { usageLimitContinuationOfRunId: command.usageLimitContinuationOfRunId }),
9397+
...(command.providerFailover === undefined
9398+
? {}
9399+
: { providerFailoverOfRunId: command.providerFailover.ofRunId }),
93499400
...(continuedTaskId === undefined ? {} : { delegatedTaskId: continuedTaskId }),
93509401
...wakeWorkStartedAt(projection, command),
93519402
...(command.delivery === undefined ? {} : { delivery: command.delivery }),

0 commit comments

Comments
 (0)