Skip to content
Closed
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
12 changes: 11 additions & 1 deletion apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,9 @@ const CODEX_CLIENT_CAPABILITIES = {

export const CodexProviderCapabilitiesV2 = {
sessions: {
supportsMultipleProviderThreadsPerSession: true,
// A native writer lives as long as its app-server process. Isolate app threads
// so switching accounts can close that writer without stopping other conversations.
supportsMultipleProviderThreadsPerSession: false,
supportsModelSwitchInSession: true,
supportsProviderSwitchingViaHandoff: true,
supportsRuntimeModeSwitchInSession: true,
Expand Down Expand Up @@ -6547,6 +6549,14 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
),
Effect.flatMap(decodeCodexResumeMetadata),
);
if (response.thread.id !== nativeThreadId) {
return yield* new ProviderAdapter.ProviderAdapterResumeThreadError({
driver: CODEX_PROVIDER,
providerThreadId: threadInput.providerThread.id,
providerSessionId: input.providerSessionId,
cause: "Codex resumed a different native thread than requested.",
});
}
// Codex follows a resume with a goal snapshot notification; the
// run's first turn writes it if it differs from the stored goal.
return {
Expand Down
170 changes: 97 additions & 73 deletions apps/server/src/orchestration-v2/Orchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -872,20 +872,27 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
readonly adapter: ProviderAdapter.ProviderAdapterV2["Service"];
readonly providerInstanceId: ProviderInstanceId;
readonly threadId: ThreadId;
readonly existingProviderSessionId?: ProviderSessionId | null | undefined;
}) =>
input.adapter.getCapabilities().pipe(
Effect.flatMap((capabilities) =>
capabilities.sessions.supportsMultipleProviderThreadsPerSession
? Effect.succeed(
idAllocator.derive.providerSession({
providerInstanceId: input.providerInstanceId,
}),
)
: idAllocator.allocate.providerSession({
providerInstanceId: input.providerInstanceId,
threadId: input.threadId,
}),
),
Effect.flatMap((capabilities) => {
const pooledId = idAllocator.derive.providerSession({
providerInstanceId: input.providerInstanceId,
});
if (capabilities.sessions.supportsMultipleProviderThreadsPerSession) {
return Effect.succeed(input.existingProviderSessionId ?? pooledId);
}
if (
input.existingProviderSessionId != null &&
input.existingProviderSessionId !== pooledId
) {
return Effect.succeed(input.existingProviderSessionId);
}
return idAllocator.allocate.providerSession({
providerInstanceId: input.providerInstanceId,
threadId: input.threadId,
});
}),
);

const enforceCommandPolicy =
Expand Down Expand Up @@ -1412,13 +1419,27 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio

const commandId = CommandId.make(`command:system:start-queued:${queuedRun.id}`);
const now = yield* DateTime.now;
const activeProviderThread = projection.providerThreads.find(
(candidate) => candidate.id === projection.thread.activeProviderThreadId,
);
const currentModelSelection = {
...projection.thread.modelSelection,
instanceId:
activeProviderThread?.providerInstanceId ?? projection.thread.modelSelection.instanceId,
};
const selectionChanged = !modelSelectionsEqual(
projection.thread.modelSelection,
currentModelSelection,
queuedRun.modelSelection,
);
const switchPlan = selectionChanged
? yield* providerSwitchService
.plan({ projection, targetModelSelection: queuedRun.modelSelection })
.plan({
projection: {
...projection,
thread: { ...projection.thread, modelSelection: currentModelSelection },
},
targetModelSelection: queuedRun.modelSelection,
})
.pipe(
Effect.mapError(
(cause) =>
Expand All @@ -1430,9 +1451,6 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
),
)
: null;
const activeProviderThread = projection.providerThreads.find(
(candidate) => candidate.id === projection.thread.activeProviderThreadId,
);
const canResumeAcrossInstances =
switchPlan?.instanceChanged === true &&
switchPlan.transition.type === "restart_and_resume" &&
Expand Down Expand Up @@ -1612,29 +1630,31 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
}),
),
));
const providerSessionId =
(!canResumeAcrossInstances &&
queuedProviderThread.providerSessionId !== null &&
!switchPlan?.releaseProviderSessionIds.includes(queuedProviderThread.providerSessionId)
? queuedProviderThread.providerSessionId
: null) ??
(yield* providerAdapters.get(queuedRun.providerInstanceId).pipe(
Effect.flatMap((adapter) =>
providerSessionIdFor({
adapter,
providerInstanceId: queuedRun.providerInstanceId,
threadId,
const providerSessionId = yield* providerAdapters.get(queuedRun.providerInstanceId).pipe(
Effect.flatMap((adapter) =>
providerSessionIdFor({
adapter,
providerInstanceId: queuedRun.providerInstanceId,
threadId,
existingProviderSessionId:
!canResumeAcrossInstances &&
queuedProviderThread.providerSessionId !== null &&
!switchPlan?.releaseProviderSessionIds.includes(
queuedProviderThread.providerSessionId,
)
? queuedProviderThread.providerSessionId
: null,
}),
),
Effect.mapError(
(cause) =>
new OrchestratorDispatchError({
commandId,
commandType: "message.dispatch",
cause,
}),
),
Effect.mapError(
(cause) =>
new OrchestratorDispatchError({
commandId,
commandType: "message.dispatch",
cause,
}),
),
));
),
);
const providerThread: OrchestrationV2ProviderThread = {
...deliveryProviderThread,
providerSessionId,
Expand Down Expand Up @@ -2744,6 +2764,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
})
: null;

const selectionHasActiveRun =
(command.type === "thread.model-selection.set" || command.type === "provider.switch") &&
(yield* loadProjectionForCommand(command, ["runs"])).runs.some(isBlockingRun);
const now = yield* DateTime.now;
if (command.type === "thread.metadata.update" && command.limitRecovery != null) {
const projection = yield* loadProjectionForCommand(
Expand Down Expand Up @@ -3395,13 +3418,15 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
command.worktreePath !== undefined &&
command.worktreePath !== thread.worktreePath
? (providerContext?.providerSessions ?? []).map((session) => session.id)
: command.type === "thread.runtime-mode.set"
? (providerContext?.providerSessions ?? [])
.filter(
(session) => !session.capabilities.sessions.supportsRuntimeModeSwitchInSession,
)
.map((session) => session.id)
: (providerSwitchPlan?.releaseProviderSessionIds ?? []),
: selectionHasActiveRun
? []
: command.type === "thread.runtime-mode.set"
? (providerContext?.providerSessions ?? [])
.filter(
(session) => !session.capabilities.sessions.supportsRuntimeModeSwitchInSession,
)
.map((session) => session.id)
: (providerSwitchPlan?.releaseProviderSessionIds ?? []),
);
if (detachSessionIds.size > 0) {
const liveSessions = (providerContext?.providerSessions ?? []).filter(
Expand Down Expand Up @@ -4198,15 +4223,14 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
input.projection,
input.modelSelection.instanceId,
).find((candidate) => candidate.id !== providerThread.id);
const targetProviderSessionId =
existingTargetProviderThread?.providerSessionId ??
(yield* mapDispatchError(input.command)(
providerSessionIdFor({
adapter: targetAdapter,
providerInstanceId: input.modelSelection.instanceId,
threadId: input.command.threadId,
}),
));
const targetProviderSessionId = yield* mapDispatchError(input.command)(
providerSessionIdFor({
adapter: targetAdapter,
providerInstanceId: input.modelSelection.instanceId,
threadId: input.command.threadId,
existingProviderSessionId: existingTargetProviderThread?.providerSessionId,
}),
);
const targetProviderThreadBase: OrchestrationV2ProviderThread =
existingTargetProviderThread === undefined
? {
Expand Down Expand Up @@ -5306,15 +5330,14 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
}),
),
);
const providerSessionId =
activeProviderThread?.providerSessionId ??
(yield* mapDispatchError(command)(
providerSessionIdFor({
adapter,
providerInstanceId: modelSelection.instanceId,
threadId: command.threadId,
}),
));
const providerSessionId = yield* mapDispatchError(command)(
providerSessionIdFor({
adapter,
providerInstanceId: modelSelection.instanceId,
threadId: command.threadId,
existingProviderSessionId: activeProviderThread?.providerSessionId,
}),
);
const providerThreadId =
activeProviderThread?.id ??
idAllocator.derive.providerThread({
Expand Down Expand Up @@ -5694,15 +5717,16 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
isProviderSwitch && !canResumeAcrossInstances
? rootProviderThreadsForProvider(projection, modelSelection.instanceId)[0]
: activeProviderThread;
const providerSessionId =
(canResumeAcrossInstances ? undefined : targetProviderThread?.providerSessionId) ??
(yield* mapDispatchError(command)(
providerSessionIdFor({
adapter,
providerInstanceId: modelSelection.instanceId,
threadId: command.threadId,
}),
));
const providerSessionId = yield* mapDispatchError(command)(
providerSessionIdFor({
adapter,
providerInstanceId: modelSelection.instanceId,
threadId: command.threadId,
existingProviderSessionId: canResumeAcrossInstances
? null
: targetProviderThread?.providerSessionId,
}),
);
const existingProviderSession = projection.providerSessions.find(
(candidate) => candidate.id === providerSessionId,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,13 @@ const layerPausingAttachEventSink = (pause: {
}),
).pipe(Layer.provide(layerTestEventSink));

const CodexCapabilities: OrchestrationV2ProviderCapabilities = CodexProviderCapabilitiesV2;
const CodexCapabilities: OrchestrationV2ProviderCapabilities = {
...CodexProviderCapabilitiesV2,
sessions: {
...CodexProviderCapabilitiesV2.sessions,
supportsMultipleProviderThreadsPerSession: true,
},
};
const ExclusiveCapabilities: OrchestrationV2ProviderCapabilities = {
...CodexCapabilities,
sessions: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,10 @@ function makeLocalCommandHarness(input: {
const providerSessionId = ProviderSessionId.make("new-provider-session");
const oldProviderThreadId = ProviderThreadId.make("existing-native-provider-thread");
const oldInstanceId = ProviderInstanceId.make("antigravity-personal");
const newInstanceId = ProviderInstanceId.make("codex-personal");
const driver = ProviderDriverKind.make(
"historyReadFailureAfterFallback" in input ? "claudeAgent" : "codex",
);
const newInstanceId = ProviderInstanceId.make(`${driver}-personal`);
const checkpointScopeId = CheckpointScopeId.make("scope-native-account-command");
const messageId = MessageId.make("message-native-account-command");
const run: OrchestrationV2ThreadProjection["runs"][number] = {
Expand All @@ -203,7 +206,7 @@ function makeLocalCommandHarness(input: {
};
const providerThread: OrchestrationV2ThreadProjection["providerThreads"][number] = {
id: providerThreadId,
driver: ProviderDriverKind.make("codex"),
driver,
providerInstanceId: newInstanceId,
providerSessionId,
appThreadId: threadId,
Expand Down
4 changes: 4 additions & 0 deletions apps/server/src/orchestration-v2/ProviderTurnStartService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,10 @@ export const layer: Layer.Layer<
return resumed.success;
}

if (session.driver === "codex" && !uncertainDelivery) {
return yield* loadFromProvider(Effect.fail(resumed.failure));
}

yield* Effect.logWarning("Provider resume failed; attempting a fresh native session", {
driver: session.driver,
providerThreadId: providerThread.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,13 @@ const handoffSelection = {
instanceId: handoffProviderInstanceId,
model: "handoff-model",
} satisfies ModelSelection;
const pooledCapabilities: OrchestrationV2ProviderCapabilities = CodexProviderCapabilitiesV2;
const pooledCapabilities: OrchestrationV2ProviderCapabilities = {
...CodexProviderCapabilitiesV2,
sessions: {
...CodexProviderCapabilitiesV2.sessions,
supportsMultipleProviderThreadsPerSession: true,
},
};
const exclusiveCapabilities: OrchestrationV2ProviderCapabilities = {
...CodexProviderCapabilitiesV2,
sessions: {
Expand Down
Loading
Loading