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
61 changes: 43 additions & 18 deletions apps/server/src/mcp/OrchestratorMcpService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,15 @@ import {
agentLaunchInboxError,
agentLaunchInboxRouting,
withLegacyPeerInboxReason,
orchestrationV2RunFailure,
} from "@t3tools/contracts";
import { getProviderOptionCurrentValue, withLegacyUltracodeEffort } from "@t3tools/shared/model";
import { backgroundWorkHoldsCompletion } from "@t3tools/shared/orchestrationV2PendingBackgroundWork";
import { latestExecutedRun, runRanAfter } from "@t3tools/shared/orchestrationV2ThreadError";
import {
latestExecutedRun,
latestRootProviderFailure,
runRanAfter,
} from "@t3tools/shared/orchestrationV2ThreadError";
import { resolveProjectSettings } from "@t3tools/shared/projectSettings";
import { formatThreadLink } from "@t3tools/shared/threadLinks";
import {
Expand Down Expand Up @@ -1058,11 +1063,25 @@ function runExecutedModel(
: (latestProviderTurnForAttempt(projection.providerTurns, run.activeAttemptId)?.model ?? null);
}

/** A failed run's typed provider error, read from its own error items only. */
function runFailure(
run: OrchestrationV2Run | undefined,
turnItems: Parameters<typeof latestRootProviderFailure>[1],
) {
const failure = run === undefined ? null : latestRootProviderFailure(run, turnItems);
return run === undefined || failure === null
? undefined
: orchestrationV2RunFailure(run, failure);
}

function threadRun(
projection: Pick<OrchestrationV2ThreadProjection, "providerTurns">,
run: OrchestrationV2Run,
): OrchestratorMcpThreadRun {
return {
...(run.providerFailoverOfRunId === undefined
? {}
: { providerFailoverOfRunId: run.providerFailoverOfRunId }),
runId: run.id,
ordinal: run.ordinal,
status: run.status,
Expand Down Expand Up @@ -1841,6 +1860,7 @@ const make = Effect.gen(function* () {
instanceId: task.providerInstanceId,
model: task.model,
};
const latestTerminalFailure = runFailure(terminalRun, childProjection.turnItems);
const result = {
taskId: task.id,
childThreadId: task.childThreadId,
Expand All @@ -1864,6 +1884,7 @@ const make = Effect.gen(function* () {
latestTerminalStatus:
terminalStatus !== null && isTerminalTaskStatus(terminalStatus) ? terminalStatus : null,
latestTerminalSummary: latestTerminalSummary === derivedResult ? null : latestTerminalSummary,
...(latestTerminalFailure === undefined ? {} : { latestTerminalFailure }),
latestTerminalResultContextTransferId:
latestTerminalResultContextTransferId === resultContextTransferId
? null
Expand Down Expand Up @@ -3482,30 +3503,34 @@ const make = Effect.gen(function* () {
const runs = target.runs
.toSorted((left, right) => right.ordinal - left.ordinal)
.slice(0, input.runLimit ?? DEFAULT_THREAD_RUN_LIMIT);
// Only an agent-created thread's latest run is judged, so a read loads one run's replies and notices.
// Only the latest run is judged, and only when it is an agent's or failed, so a read loads
// one run's replies and notices.
const latestRun = runs[0];
const latestAlert =
const latestRecords =
latestRun === undefined ||
target.thread.createdBy !== "agent" ||
(target.thread.createdBy !== "agent" && latestRun.status !== "failed") ||
!ThreadManagementService.isTerminalRunStatus(latestRun.status)
? undefined
: subagentResultForRun(
{
...target,
...(yield* threadManagement
.getThreadRecords(input.threadId, ["messages", "turnItems"], {
messageRoles: ["assistant"],
messageRunIds: [latestRun.id],
turnItemRunId: latestRun.id,
turnItemTypes: ["assistant_message", "error", "system_notice"],
})
.pipe(Effect.mapError(threadManagementFailure))),
},
latestRun,
).workerAlert;
: yield* threadManagement
.getThreadRecords(input.threadId, ["messages", "turnItems"], {
messageRoles: ["assistant"],
messageRunIds: [latestRun.id],
turnItemRunId: latestRun.id,
turnItemTypes: ["assistant_message", "error", "system_notice"],
})
.pipe(Effect.mapError(threadManagementFailure));
const latestAlert =
latestRun === undefined ||
latestRecords === undefined ||
target.thread.createdBy !== "agent"
? undefined
: subagentResultForRun({ ...target, ...latestRecords }, latestRun).workerAlert;
const latestFailure =
latestRecords === undefined ? undefined : runFailure(latestRun, latestRecords.turnItems);
const recentRuns = runs.map((run) => ({
...threadRun(target, run),
...(run === latestRun && latestAlert !== undefined ? { workerAlert: latestAlert } : {}),
...(run === latestRun && latestFailure !== undefined ? { failure: latestFailure } : {}),
}));
const { items, start } = timelineItemsWithinBudget(
{ thread, recentRuns, nextPosition: null, hasMore: false, earlierPosition: null },
Expand Down
3 changes: 3 additions & 0 deletions apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5248,6 +5248,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
payload.error.codexErrorInfo,
notificationCode,
),
httpStatus: codexErrorHttpStatus(payload.error.codexErrorInfo),
}),
payload.error,
),
Expand Down Expand Up @@ -5278,6 +5279,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
? "transport_error"
: "provider_error",
retryable: true,
httpStatus: codexErrorHttpStatus(payload.error.codexErrorInfo),
}),
payload.error,
);
Expand Down Expand Up @@ -6442,6 +6444,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
message: input.failureMessage,
code: failureCode,
class: failureClass,
httpStatus: codexErrorHttpStatus(input.failureErrorInfo),
}),
input.failureMessage,
);
Expand Down
51 changes: 51 additions & 0 deletions apps/server/src/orchestration-v2/Orchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
usageLimitBlockedRun,
} from "@t3tools/shared/orchestrationV2ThreadError";
import { canonicalizeGitCafePullRequestUrl } from "@t3tools/source-control-definitions/registry";
import { admitProviderFailover } from "./providerFailover.ts";
import { threadPullRequestsOf } from "../sourceControl/threadPullRequests.ts";
import {
normalizeThreadPullRequestKey,
Expand Down Expand Up @@ -1097,6 +1098,7 @@ function carriesDigest(
| "restartContinuationOfRunId"
| "manualContinuationOfRunId"
| "usageLimitContinuationOfRunId"
| "providerFailoverOfRunId"
>,
message: {
readonly text: string;
Expand All @@ -1109,6 +1111,7 @@ function carriesDigest(
run.restartContinuationOfRunId === undefined &&
run.manualContinuationOfRunId === undefined &&
run.usageLimitContinuationOfRunId === undefined &&
run.providerFailoverOfRunId === undefined &&
message.idleCompaction === undefined &&
!isNativeMaintenanceCommand(message) &&
parseGoalCommand(message) === null
Expand Down Expand Up @@ -7061,6 +7064,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
command.manualContinuationOfRunId,
command.restartContinuationOfRunId,
command.usageLimitContinuationOfRunId,
command.providerFailover?.ofRunId,
command.steerFollowUpOfRunId,
...(command.dispatchMode.type === "steer_active" ||
command.dispatchMode.type === "restart_active"
Expand Down Expand Up @@ -7122,6 +7126,44 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
});
return;
}
if (command.providerFailover !== undefined) {
const admission =
command.dispatchMode.type === "start_immediately" && command.modelSelection === undefined
? admitProviderFailover(
command.providerFailover,
projection,
// The command projection keeps only user messages and errors; effects need every item.
(yield* projectionStore
.getThreadRecords(command.threadId, ["turnItems"], {
turnItemRunId: command.providerFailover.ofRunId,
})
.pipe(mapDispatchError(command))).turnItems,
yield* serverSettings.getSettings.pipe(Effect.orDie),
)
: null;
if (admission?.type === "duplicate") {
yield* emit(
events,
command,
)({
type: "thread.metadata-updated",
threadId: command.threadId,
occurredAt: yield* DateTime.now,
payload: projection.thread,
});
return;
}
if (admission?.type !== "admitted") {
return yield* new OrchestratorDispatchError({
commandId: command.commandId,
commandType: command.type,
cause:
admission?.refusal ??
"A provider failover starts immediately and takes its model from its candidates.",
});
}
command = { ...command, modelSelection: admission.target };
}
const activeProviderInstanceId = projection.providerThreads.find(
(candidate) => candidate.id === projection.thread.activeProviderThreadId,
)?.providerInstanceId;
Expand Down Expand Up @@ -8116,6 +8158,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
...(command.usageLimitContinuationOfRunId === undefined
? {}
: { usageLimitContinuationOfRunId: command.usageLimitContinuationOfRunId }),
...(command.providerFailover === undefined
? {}
: { providerFailoverOfRunId: command.providerFailover.ofRunId }),
...(continuedTaskId === undefined ? {} : { delegatedTaskId: continuedTaskId }),
...(freshContext ? { freshContext: true } : {}),
...(command.delivery === undefined ? {} : { delivery: command.delivery }),
Expand Down Expand Up @@ -8534,6 +8579,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
...(command.usageLimitContinuationOfRunId === undefined
? {}
: { usageLimitContinuationOfRunId: command.usageLimitContinuationOfRunId }),
...(command.providerFailover === undefined
? {}
: { providerFailoverOfRunId: command.providerFailover.ofRunId }),
...(dispatchMode.type === "defer_start" && dispatchMode.workspaceStrategy !== undefined
? { workspacePreparation: dispatchMode.workspaceStrategy }
: {}),
Expand Down Expand Up @@ -9346,6 +9394,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
...(command.usageLimitContinuationOfRunId === undefined
? {}
: { usageLimitContinuationOfRunId: command.usageLimitContinuationOfRunId }),
...(command.providerFailover === undefined
? {}
: { providerFailoverOfRunId: command.providerFailover.ofRunId }),
...(continuedTaskId === undefined ? {} : { delegatedTaskId: continuedTaskId }),
...wakeWorkStartedAt(projection, command),
...(command.delivery === undefined ? {} : { delivery: command.delivery }),
Expand Down
Loading
Loading