Skip to content
TypeScript

feat(server): sends can start a fresh provider session (#30323)

Julius Marmingee139768a7e7f
diff --git a/apps/server/src/environment/ServerEnvironment.ts b/apps/server/src/environment/ServerEnvironment.ts
index 0787a3702c0..82010efdee8 100644
--- a/apps/server/src/environment/ServerEnvironment.ts
+++ b/apps/server/src/environment/ServerEnvironment.ts
@@ -268,6 +268,7 @@ export const make = Effect.gen(function* () {
ignoredFileListing: true,
triage: true,
oneTimeSchedules: true,
+ freshContext: true,
npmReleaseLookup: true,
...(serverSelfUpdate === null ? {} : { serverSelfUpdate }),
...(serverInstallation === null ? {} : { serverInstallation }),
diff --git a/apps/server/src/orchestration-v2/Orchestrator.ts b/apps/server/src/orchestration-v2/Orchestrator.ts
index daf64a2d438..d40ca624a11 100644
--- a/apps/server/src/orchestration-v2/Orchestrator.ts
+++ b/apps/server/src/orchestration-v2/Orchestrator.ts
@@ -45,6 +45,7 @@ import {
type OrchestrationV2DelegatedCompletionDelivery,
type OrchestrationV2DomainEvent,
type OrchestrationV2ExecutionNode,
+ type OrchestrationV2ProviderSession,
type OrchestrationV2ProviderThread,
type OrchestrationV2ProviderTurn,
type OrchestrationV2RemoteTaskChild,
@@ -998,16 +999,75 @@ function appendContextHandoffId(
return handoffId === null ? handoffIds : Array.from(new Set([...handoffIds, handoffId]));
}
+/** Ordinal of the latest started fresh-context run. Runs before it never reach a provider again. */
+function contextBoundaryOrdinal(
+ runs: ReadonlyArray<
+ Pick<
+ OrchestrationV2Run,
+ "ordinal" | "freshContext" | "status" | "startedAt" | "restartContinuationOfRunId"
+ >
+ >,
+): number {
+ let boundary = 0;
+ for (const run of runs) {
+ if (
+ run.freshContext === true &&
+ run.status !== "queued" &&
+ !cancelledBeforeStart(run) &&
+ run.ordinal > boundary
+ ) {
+ boundary = run.ordinal;
+ }
+ }
+ return boundary;
+}
+
+/** Threads with no started run (imported, or reserved by another queued run) never cross it. */
+function providerThreadPredatesBoundary(
+ providerThread: Pick<OrchestrationV2ProviderThread, "firstRunOrdinal">,
+ boundary: number,
+): boolean {
+ return (
+ boundary > 0 &&
+ (providerThread.firstRunOrdinal === null || providerThread.firstRunOrdinal < boundary)
+ );
+}
+
+/** A provider that hosts one native thread per session keeps the old conversation alive otherwise. */
+function sessionsReleasedByFreshContext(
+ projection: Pick<OrchestrationV2ThreadProjection, "providerSessions">,
+ activeProviderThread: OrchestrationV2ProviderThread | undefined,
+): ReadonlyArray<OrchestrationV2ProviderSession> {
+ const sessionId = activeProviderThread?.providerSessionId ?? null;
+ return projection.providerSessions.filter(
+ (session) =>
+ session.id === sessionId &&
+ session.status !== "stopped" &&
+ session.status !== "error" &&
+ !session.capabilities.sessions.supportsMultipleProviderThreadsPerSession,
+ );
+}
+
+const FRESH_CONTEXT_DETACH_REASON = "Started a fresh session.";
+
+function uniqueSessions(
+ sessions: ReadonlyArray<OrchestrationV2ProviderSession>,
+): ReadonlyArray<OrchestrationV2ProviderSession> {
+ return Array.from(new Map(sessions.map((session) => [session.id, session])).values());
+}
+
function rootProviderThreadsForProvider(
- projection: Pick<OrchestrationV2ThreadProjection, "providerThreads" | "thread">,
+ projection: Pick<OrchestrationV2ThreadProjection, "providerThreads" | "thread" | "runs">,
providerInstanceId: ModelSelection["instanceId"],
): ReadonlyArray<OrchestrationV2ProviderThread> {
+ const boundary = contextBoundaryOrdinal(projection.runs);
return projection.providerThreads
.filter(
(providerThread) =>
providerThread.providerInstanceId === providerInstanceId &&
providerThread.appThreadId === projection.thread.id &&
- providerThread.ownerNodeId === null,
+ providerThread.ownerNodeId === null &&
+ !providerThreadPredatesBoundary(providerThread, boundary),
)
.toSorted(
(left, right) =>
@@ -1807,15 +1867,29 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
(candidate) => candidate.id === projection.thread.activeProviderThreadId,
);
const followsCurrent = followsCurrentConversation(queuedMessage);
- const targetProviderThread = followsCurrent
- ? activeProviderThread?.providerInstanceId === queuedRun.providerInstanceId
+ const freshContext = queuedRun.freshContext === true;
+ const contextBoundary = freshContext ? ordinal : contextBoundaryOrdinal(projection.runs);
+ // A run queued before a fresh run started is bound to the conversation that run left behind.
+ const followsLatestContext =
+ !freshContext &&
+ (followsCurrent || providerThreadPredatesBoundary(storedProviderThread, contextBoundary));
+ const targetProviderThread = followsLatestContext
+ ? activeProviderThread?.providerInstanceId === queuedRun.providerInstanceId &&
+ !providerThreadPredatesBoundary(activeProviderThread, contextBoundary)
? activeProviderThread
: rootProviderThreadsForProvider(projection, queuedRun.providerInstanceId)[0]
- : storedProviderThread;
+ : freshContext &&
+ (storedProviderThread.firstRunOrdinal !== null ||
+ storedProviderThread.nativeThreadRef !== null)
+ ? undefined
+ : storedProviderThread;
const queuedProviderThread: OrchestrationV2ProviderThread = targetProviderThread ?? {
id: idAllocator.derive.providerThread({
driver: targetAdapter.driver,
- nativeThreadId: `pending:${queuedRun.id}`,
+ // The queue reserved `pending:<run>` for this run; another run may have taken it.
+ nativeThreadId: freshContext
+ ? `pending:${queuedRun.id}:fresh`
+ : `pending:${queuedRun.id}`,
}),
driver: targetAdapter.driver,
providerInstanceId: queuedRun.providerInstanceId,
@@ -1870,12 +1944,15 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
),
)
: null;
- const releasableSessions = projection.providerSessions.filter(
- (session) =>
- switchPlan?.releaseProviderSessionIds.includes(session.id) &&
- session.status !== "stopped" &&
- session.status !== "error",
- );
+ const releasableSessions = uniqueSessions([
+ ...projection.providerSessions.filter(
+ (session) =>
+ switchPlan?.releaseProviderSessionIds.includes(session.id) &&
+ session.status !== "stopped" &&
+ session.status !== "error",
+ ),
+ ...(freshContext ? sessionsReleasedByFreshContext(projection, activeProviderThread) : []),
+ ]);
const busySessionIds = new Set<ProviderSessionId>();
for (const session of releasableSessions) {
if (yield* sessionHasBackgroundWork(session, projection.providerThreads)) {
@@ -1884,7 +1961,12 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
}
// Nobody waits on a queued start to refuse it. A handoff runs in its own session, so a
// busy one stays up; a restart resumes the busy session's native thread, so wait instead.
- if (busySessionIds.size > 0 && switchPlan?.transition.type === "restart_and_resume") {
+ // A fresh run resumes nothing, so it never waits.
+ if (
+ busySessionIds.size > 0 &&
+ !freshContext &&
+ switchPlan?.transition.type === "restart_and_resume"
+ ) {
yield* Effect.logInfo(
"Holding the queue: the replaced provider session still runs background work",
{ threadId, runId: queuedRun.id, providerSessionIds: Array.from(busySessionIds) },
@@ -1907,6 +1989,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
(session) => !busySessionIds.has(session.id),
);
const canResumeAcrossInstances =
+ !freshContext &&
switchPlan?.instanceChanged === true &&
switchPlan.transition.type === "restart_and_resume" &&
activeProviderThread !== undefined &&
@@ -1936,6 +2019,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
queuedProviderThread.id,
);
const coveredRuns =
+ freshContext ||
canResumeAcrossInstances ||
latestHandoffRun === undefined ||
latestHandoffRun.providerInstanceId === queuedRun.providerInstanceId
@@ -1943,12 +2027,13 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
: projection.runs.filter(
(run) =>
isHandoffSourceRun(run) &&
+ run.ordinal >= contextBoundary &&
run.ordinal > (targetLastCompletedRun?.ordinal ?? 0) &&
run.ordinal <= latestHandoffRun.ordinal,
);
const needsFullContext = deliveryProviderThread.nativeThreadRef === null;
const legacyImportItems =
- projection.thread.historyOrigin === "v1_import"
+ projection.thread.historyOrigin === "v1_import" && contextBoundary === 0
? yield* readHandoffItems(threadId, [null])
: [];
const handoffStrategy = needsFullContext
@@ -2153,6 +2238,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
? {}
: { idleCompaction: queuedMessage.idleCompaction }),
}),
+ ...(freshContext ? { freshContext: true } : {}),
providerThreadId: queuedProviderThread.id,
inputIntent: "queued_turn",
status: "completed",
@@ -2244,6 +2330,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
},
]
: [];
+ const detachReason = freshContext
+ ? FRESH_CONTEXT_DETACH_REASON
+ : "Provider or model selection changed.";
yield* writeSystemEvents(
[
...(selectionChanged
@@ -2344,7 +2433,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
payload: {
providerSessionId: session.id,
detachedAt: now,
- reason: "Provider or model selection changed.",
+ reason: detachReason,
},
})),
...executionIdentityEvents,
@@ -2386,7 +2475,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
request: {
type: "provider-session.detach" as const,
providerSessionId: session.id,
- detail: "Provider or model selection changed.",
+ detail: detachReason,
},
})),
{
@@ -5219,6 +5308,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
type: "provider_handoff",
}),
);
+ const contextBoundary = contextBoundaryOrdinal(input.projection.runs);
restartHandoff = yield* contextHandoffService
.prepareProviderHandoff({
threadId: input.command.threadId,
@@ -5228,9 +5318,16 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
toProviderThreadId: targetProviderThreadBase.id,
fromProviderInstanceId: targetRun.providerInstanceId,
toProviderInstanceId: input.modelSelection.instanceId,
- coveredRunOrdinals: { from: 1, to: targetRun.ordinal },
+ coveredRunOrdinals: { from: Math.max(1, contextBoundary), to: targetRun.ordinal },
strategy: "full_thread_summary",
- items: yield* readHandoffItems(input.command.threadId),
+ items: yield* readHandoffItems(
+ input.command.threadId,
+ contextBoundary === 0
+ ? undefined
+ : input.projection.runs
+ .filter((run) => run.ordinal >= contextBoundary)
+ .map((run) => run.id),
+ ),
runs: input.projection.runs,
createdAt: now,
})
@@ -5750,6 +5847,13 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
) {
dispatchMode = { type: "queue_after_active" };
}
+ const freshContext = command.freshContext === true;
+ if (
+ freshContext &&
+ (dispatchMode.type === "steer_active" || dispatchMode.type === "restart_active")
+ ) {
+ dispatchMode = { type: "queue_after_active" };
+ }
// Codex and Claude own /goal; elsewhere it is ordinary text that cannot steer.
const parsedGoalCommand = parseGoalCommand(command);
const goalCommand =
@@ -5849,12 +5953,13 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
// behind a long parent turn is never seen. Never interrupt/restart a turn
// for a notification.
if (
- delegatedCompletion !== undefined
+ !freshContext &&
+ (delegatedCompletion !== undefined
? delegatedCompletion.taskIds.every(
(id) =>
projection.subagents.find((task) => task.id === id)?.completionWake === "always",
)
- : command.notification?.source.kind === "delegated_task"
+ : command.notification?.source.kind === "delegated_task")
) {
const active = projection.runs.find((run) => run.status === "running");
const providerThread = projection.providerThreads.find(
@@ -6040,8 +6145,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
const now = yield* DateTime.now;
const ordinal = nextRunOrdinal(projection);
const runId = idAllocator.derive.run({ threadId: command.threadId, ordinal });
- const targetProviderThread =
- modelSelection.instanceId === queueProviderThread.providerInstanceId
+ const targetProviderThread = freshContext
+ ? undefined
+ : modelSelection.instanceId === queueProviderThread.providerInstanceId
? queueProviderThread
: rootProviderThreadsForProvider(projection, modelSelection.instanceId)[0];
const queuedAdapter = yield* providerAdapters
@@ -6160,6 +6266,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
? {}
: { restartContinuationOfRunId: command.restartContinuationOfRunId }),
...(continuedTaskId === undefined ? {} : { delegatedTaskId: continuedTaskId }),
+ ...(freshContext ? { freshContext: true } : {}),
};
const attempt: OrchestrationV2RunAttempt = {
id: attemptId,
@@ -6349,12 +6456,23 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
},
});
}
+ if (
+ freshContext &&
+ (pendingForkTransfer !== undefined || pendingMergeBackTransfer !== undefined)
+ ) {
+ return yield* new OrchestratorDispatchError({
+ commandId: command.commandId,
+ commandType: command.type,
+ cause: "A fresh session can't start while a fork or merge is still being applied.",
+ });
+ }
const ordinal = nextRunOrdinal(projection);
const runId = idAllocator.derive.run({ threadId: command.threadId, ordinal });
+ const contextBoundary = freshContext ? ordinal : contextBoundaryOrdinal(projection.runs);
const latestCompletedRun = projection.runs.findLast((run) => run.status === "completed");
const latestHandoffRun = projection.runs.findLast(isHandoffSourceRun);
const legacyImportItems =
- projection.thread.historyOrigin === "v1_import"
+ projection.thread.historyOrigin === "v1_import" && contextBoundary === 0
? yield* readHandoffItems(command.threadId, [null])
: [];
const isProviderSwitch =
@@ -6380,22 +6498,27 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
.pipe(mapDispatchError(command))
: null;
const canResumeAcrossInstances =
+ !freshContext &&
isProviderSwitch &&
activeProviderThread.nativeThreadRef !== null &&
providerSwitchPlan?.transition.type === "restart_and_resume";
- const sessionsToDetach = projection.providerSessions.filter(
- (session) =>
- providerSwitchPlan?.releaseProviderSessionIds.includes(session.id) &&
- session.status !== "stopped" &&
- session.status !== "error",
- );
+ const sessionsToDetach = uniqueSessions([
+ ...projection.providerSessions.filter(
+ (session) =>
+ providerSwitchPlan?.releaseProviderSessionIds.includes(session.id) &&
+ session.status !== "stopped" &&
+ session.status !== "error",
+ ),
+ ...(freshContext ? sessionsReleasedByFreshContext(projection, activeProviderThread) : []),
+ ]);
for (const detaching of sessionsToDetach) {
if (yield* sessionHasBackgroundWork(detaching, projection.providerThreads)) {
return yield* new OrchestratorDispatchError({
commandId: command.commandId,
commandType: command.type,
- cause:
- "The previous provider is still running background agents or commands, and switching would end them. Wait for them to finish, or press Stop, then send the message again.",
+ cause: freshContext
+ ? "The current session is still running background agents or commands, and a fresh session would end them. Wait for them to finish, or press Stop, then send the message again."
+ : "The previous provider is still running background agents or commands, and switching would end them. Wait for them to finish, or press Stop, then send the message again.",
});
}
}
@@ -6403,8 +6526,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
if (
pendingForkTransfer === undefined &&
pendingMergeBackTransfer === undefined &&
- !isProviderSwitch
+ (!isProviderSwitch || freshContext)
) {
+ const reusableProviderThread = freshContext ? undefined : activeProviderThread;
const adapter = yield* providerAdapters.get(modelSelection.instanceId).pipe(
Effect.mapError(
(cause) =>
@@ -6416,7 +6540,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
),
);
const providerSessionId =
- activeProviderThread?.providerSessionId ??
+ reusableProviderThread?.providerSessionId ??
(yield* mapDispatchError(command)(
providerSessionIdFor({
adapter,
@@ -6425,7 +6549,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
}),
));
const providerThreadId =
- activeProviderThread?.id ??
+ reusableProviderThread?.id ??
idAllocator.derive.providerThread({
driver: adapter.driver,
nativeThreadId: `pending:${runId}`,
@@ -6447,7 +6571,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
.pipe(mapDispatchError(command))
: null;
const providerThread: OrchestrationV2ProviderThread =
- activeProviderThread === undefined
+ reusableProviderThread === undefined
? {
id: providerThreadId,
driver: adapter.driver,
@@ -6466,11 +6590,11 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
updatedAt: now,
}
: {
- ...activeProviderThread,
+ ...reusableProviderThread,
providerSessionId,
lastRunOrdinal: ordinal,
handoffIds: appendContextHandoffId(
- activeProviderThread.handoffIds,
+ reusableProviderThread.handoffIds,
legacyImportHandoff?.id ?? null,
),
updatedAt: now,
@@ -6526,6 +6650,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
: {}),
...(continuedTaskId === undefined ? {} : { delegatedTaskId: continuedTaskId }),
...wakeWorkStartedAt(projection.runs, command),
+ ...(freshContext ? { freshContext: true } : {}),
};
const attempt: OrchestrationV2RunAttempt = {
id: attemptId,
@@ -6607,6 +6732,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
text: dispatchText,
...(command.context ? { context: command.context } : {}),
attachments: command.attachments,
+ ...(freshContext ? { freshContext: true } : {}),
...(run.contextHandoffId === null ? modelSwitchSince(projection, modelSelection) : {}),
};
const preparationTurnItem: OrchestrationV2TurnItem | null =
@@ -6724,6 +6850,34 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
payload: preparationTurnItem,
});
}
+ for (const session of sessionsToDetach) {
+ if (session.id === providerSessionId) continue;
+ yield* emitEvent({
+ type: "provider-session.detached",
+ threadId: command.threadId,
+ driver: session.driver,
+ providerInstanceId: session.providerInstanceId,
+ occurredAt: now,
+ payload: {
+ providerSessionId: session.id,
+ detachedAt: now,
+ reason: FRESH_CONTEXT_DETACH_REASON,
+ },
+ });
+ yield* Ref.update(effects, (existing) => [
+ ...existing,
+ {
+ id: `effect:${command.commandId}:provider-session.detach:${session.id}`,
+ commandId: command.commandId,
+ threadId: command.threadId,
+ request: {
+ type: "provider-session.detach",
+ providerSessionId: session.id,
+ detail: FRESH_CONTEXT_DETACH_REASON,
+ },
+ } satisfies PendingOrchestrationEffectV2,
+ ]);
+ }
const pendingEffect = {
id: `effect:${command.commandId}:provider-turn.start:${runId}`,
commandId: command.commandId,
@@ -7010,6 +7164,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
: projection.runs.filter(
(run) =>
isHandoffSourceRun(run) &&
+ run.ordinal >= contextBoundary &&
run.ordinal >
(requiresFullProviderSwitchContext ? 0 : (targetLastCompletedRun?.ordinal ?? 0)) &&
run.ordinal <= latestHandoffRun.ordinal,
@@ -9681,6 +9836,14 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
now,
});
}
+ // Skipping compaction starts the message as its own turn; steering would join the current one.
+ if (queuedRun.freshContext === true) {
+ return yield* new OrchestratorDispatchError({
+ commandId: command.commandId,
+ commandType: command.type,
+ cause: "A message that starts a fresh session can't steer the current turn.",
+ });
+ }
if (goalCommand !== null) {
yield* ensureGoalCommandSupported(command, queuedMessage, queuedRun.providerInstanceId);
const goal = projection.providerThreads.find(
diff --git a/apps/server/src/orchestration-v2/SteeringCompletion.integration.test.ts b/apps/server/src/orchestration-v2/SteeringCompletion.integration.test.ts
index bd3f4e5b2e7..2b08c0b8345 100644
--- a/apps/server/src/orchestration-v2/SteeringCompletion.integration.test.ts
+++ b/apps/server/src/orchestration-v2/SteeringCompletion.integration.test.ts
@@ -1511,7 +1511,7 @@ it.effect("queues a plain send behind the stopped queue and releases its hold",
const goalHarness = Effect.fn("goalHarness")(function* (name: string) {
const harness = yield* nextTurnSelectionHarness(name);
- const send = (id: string, text: string, delivery: "queue" | "auto") =>
+ const send = (id: string, text: string, delivery: "queue" | "auto", freshContext = false) =>
Effect.gen(function* () {
const orchestrator = yield* Orchestrator.OrchestratorV2;
yield* orchestrator.dispatch({
@@ -1522,6 +1522,7 @@ const goalHarness = Effect.fn("goalHarness")(function* (name: string) {
text,
attachments: [],
modelSelection: runSelection,
+ ...(freshContext ? { freshContext } : {}),
...(delivery === "queue"
? { dispatchMode: { type: "queue_after_active" } }
: { dispatchMode: { type: "start_immediately" }, deliveryIntent: "auto" }),
@@ -1627,7 +1628,8 @@ it.effect("promotes a queued goal by stopping the running turn", () =>
),
);
-it.effect.each(["next", "/goal ship it"])(
+// A fresh message skips compaction as its own turn, which steering a running turn would not.
+it.effect.each(["next", "/goal ship it", "fresh next"])(
"promoting %s into a compaction stops it and sends it next",
(promoted) =>
Effect.scoped(
@@ -1641,7 +1643,7 @@ it.effect.each(["next", "/goal ship it"])(
const threadId = yield* startFirstTurn;
yield* send("compact", "/compact", "queue");
yield* send("queued", "queued", "queue");
- yield* send("next", promoted, "queue");
+ yield* send("next", promoted, "queue", promoted.startsWith("fresh"));
const settled = yield* orchestrator.streamDomainEvents.pipe(
Stream.filter(
(event) =>
diff --git a/apps/server/src/orchestration-v2/testkit/ProviderSwitch.integration.test.ts b/apps/server/src/orchestration-v2/testkit/ProviderSwitch.integration.test.ts
index 629b3902e76..e9b26369983 100644
--- a/apps/server/src/orchestration-v2/testkit/ProviderSwitch.integration.test.ts
+++ b/apps/server/src/orchestration-v2/testkit/ProviderSwitch.integration.test.ts
@@ -2767,6 +2767,296 @@ describe("orchestration v2 provider switching", () => {
),
);
+ it.live.each(["immediately", "behind a running turn"] as const)(
+ "starts a fresh provider session %s and never hands earlier runs to a later provider",
+ (variant) =>
+ Effect.scoped(
+ Effect.gen(function* () {
+ const queued = variant !== "immediately";
+ const key = queued ? "queued" : "immediate";
+ const cwd = yield* checkpointWorkspace(`fresh-context-${key}`);
+ const capturedTurns = yield* Ref.make<ReadonlyArray<CapturedTurn>>([]);
+ const closedSessions = yield* Ref.make<ReadonlyArray<string>>([]);
+ // A queued start has nobody to refuse, so a busy session must stay up.
+ const backgroundWork = yield* Ref.make(queued);
+ const started = yield* Deferred.make<void>();
+ const release = yield* Deferred.make<void>();
+ const layerRegistry = ProviderAdapterRegistry.layerFromAdapters([
+ makeTestAdapter({
+ instanceId: CLAUDE_MODEL_SELECTION.instanceId,
+ driver: CLAUDE_DRIVER,
+ capabilities: ClaudeProviderCapabilitiesV2,
+ modelSelection: CLAUDE_MODEL_SELECTION,
+ responseByRunOrdinal: {},
+ capturedTurns,
+ closedSessions,
+ backgroundWork,
+ nativeThreadGeneration: yield* Ref.make(0),
+ ...(queued
+ ? { holdFirstTurn: started, releaseFirstTurn: release, holdRunOrdinal: 2 }
+ : {}),
+ }),
+ makeTestAdapter({
+ instanceId: CODEX_MODEL_SELECTION.instanceId,
+ driver: CODEX_DRIVER,
+ capabilities: CodexProviderCapabilitiesV2,
+ modelSelection: CODEX_MODEL_SELECTION,
+ responseByRunOrdinal: {},
+ capturedTurns,
+ }),
+ ]);
+ const freshThreadId = ThreadId.make(`thread:fresh-context:${key}`);
+ yield* Effect.gen(function* () {
+ const orchestrator = yield* Orchestrator.OrchestratorV2;
+ const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
+ const send = (
+ name: string,
+ modelSelection: ModelSelection,
+ options: { readonly freshContext?: boolean } = {},
+ ) =>
+ orchestrator.dispatch({
+ type: "message.dispatch",
+ createdBy: "user",
+ creationSource: "web",
+ commandId: CommandId.make(`command:fresh-context:${key}:${name}`),
+ threadId: freshThreadId,
+ messageId: MessageId.make(`message:fresh-context:${key}:${name}`),
+ text: `Prompt ${name}`,
+ attachments: [],
+ modelSelection,
+ ...options,
+ // A fresh send must never steer the running turn.
+ deliveryIntent: "auto",
+ dispatchMode: { type: "start_immediately" },
+ });
+ yield* orchestrator.dispatch({
+ type: "thread.create",
+ createdBy: "user",
+ creationSource: "web",
+ commandId: CommandId.make(`command:fresh-context:${key}:create`),
+ threadId: freshThreadId,
+ projectId: ProjectId.make(`project:fresh-context:${key}`),
+ title: "Fresh context",
+ modelSelection: CLAUDE_MODEL_SELECTION,
+ runtimeMode: "full-access",
+ interactionMode: "default",
+ branch: null,
+ worktreePath: cwd,
+ });
+ // Codex keeps a native thread from before the fresh run that must not resume.
+ yield* send("zero", CODEX_MODEL_SELECTION);
+ yield* worker.drain();
+ yield* waitForIdle(freshThreadId);
+ yield* send("alpha", CLAUDE_MODEL_SELECTION);
+ if (queued) {
+ yield* Deferred.await(started);
+ yield* worker.drain();
+ } else {
+ yield* worker.drain();
+ yield* waitForIdle(freshThreadId);
+ }
+ const firstSession = (yield* orchestrator.getThreadProjection(
+ freshThreadId,
+ )).providerSessions.find(
+ (session) => session.providerInstanceId === CLAUDE_MODEL_SELECTION.instanceId,
+ )!;
+ yield* send("beta", CLAUDE_MODEL_SELECTION, { freshContext: true });
+ if (queued) {
+ // Queued before the fresh run started, so it is bound to the old conversation.
+ yield* send("gamma", CLAUDE_MODEL_SELECTION);
+ const waiting = yield* orchestrator.getThreadProjection(freshThreadId);
+ assert.deepEqual(
+ waiting.runs.map((run) => run.status),
+ ["completed", "running", "queued", "queued"],
+ );
+ // Steering would send it into the conversation it is meant to leave.
+ const promoted = yield* orchestrator
+ .dispatch({
+ type: "queued-message.promote-to-steer",
+ commandId: CommandId.make(`command:fresh-context:${key}:promote`),
+ threadId: freshThreadId,
+ queuedRunId: waiting.runs.find((run) => run.freshContext === true)!.id,
+ targetRunId: waiting.runs.find((run) => run.status === "running")!.id,
+ })
+ .pipe(Effect.flip);
+ assert.instanceOf(promoted, Orchestrator.OrchestratorDispatchError);
+ assert.include(String(promoted.cause), "fresh session");
+ yield* Deferred.succeed(release, undefined);
+ }
+ yield* worker.drain();
+ yield* waitForIdle(freshThreadId);
+ // Claude hosts one native thread per session, so an idle old one is closed.
+ assert.deepEqual(yield* Ref.get(closedSessions), queued ? [] : [firstSession.id]);
+ yield* Ref.set(backgroundWork, false);
+ yield* send("delta", CODEX_MODEL_SELECTION);
+ yield* worker.drain();
+ yield* waitForIdle(freshThreadId);
+ yield* worker.drain();
+ const projection = yield* orchestrator.getThreadProjection(freshThreadId);
+ const turns = yield* Ref.get(capturedTurns);
+ const turnFor = (name: string) =>
+ turns.find((turn) => turn.text.endsWith(`Prompt ${name}`))!;
+
+ const freshRun = projection.runs.find(
+ (run) => run.userMessageId === `message:fresh-context:${key}:beta`,
+ )!;
+ assert.isTrue(freshRun.freshContext);
+ assert.equal(freshRun.contextHandoffId, null);
+ assert.isTrue(
+ projection.turnItems.some(
+ (item) =>
+ item.type === "user_message" &&
+ item.messageId === freshRun.userMessageId &&
+ item.freshContext === true,
+ ),
+ );
+ assert.notEqual(turnFor("beta").providerThreadId, turnFor("alpha").providerThreadId);
+ assert.notEqual(turnFor("beta").nativeThreadId, turnFor("alpha").nativeThreadId);
+ assert.equal(turnFor("beta").text, "Prompt beta");
+ if (queued) {
+ assert.equal(turnFor("gamma").providerThreadId, turnFor("beta").providerThreadId);
+ }
+ const codexTurn = turnFor("delta");
+ assert.notEqual(codexTurn.providerThreadId, turnFor("zero").providerThreadId);
+ assert.include(codexTurn.text, "Prompt beta");
+ assert.notInclude(codexTurn.text, "Prompt alpha");
+ assert.notInclude(codexTurn.text, "Prompt zero");
+ assert.equal(projection.contextHandoffs.at(-1)!.coveredRunOrdinals.from, 3);
+ }).pipe(
+ Effect.provide(
+ ProviderReplayHarness.layerWithRegistry(
+ {
+ name: `fresh-context-${key}`,
+ runtimePolicyOverride: {
+ cwd,
+ approvalPolicy: "never",
+ sandboxPolicy: {
+ type: "readOnly",
+ access: { type: "fullAccess" },
+ networkAccess: false,
+ },
+ },
+ },
+ layerRegistry,
+ ),
+ ),
+ );
+ }),
+ ),
+ );
+
+ it.live("keeps the thread a queued fresh run reserved for that run", () =>
+ Effect.scoped(
+ Effect.gen(function* () {
+ const cwd = yield* checkpointWorkspace("fresh-context-reserved");
+ const capturedTurns = yield* Ref.make<ReadonlyArray<CapturedTurn>>([]);
+ const started = yield* Deferred.make<void>();
+ const release = yield* Deferred.make<void>();
+ const layerRegistry = ProviderAdapterRegistry.layerFromAdapters([
+ makeTestAdapter({
+ instanceId: CLAUDE_MODEL_SELECTION.instanceId,
+ driver: CLAUDE_DRIVER,
+ capabilities: ClaudeProviderCapabilitiesV2,
+ modelSelection: CLAUDE_MODEL_SELECTION,
+ responseByRunOrdinal: {},
+ capturedTurns,
+ nativeThreadGeneration: yield* Ref.make(0),
+ holdFirstTurn: started,
+ releaseFirstTurn: release,
+ holdRunOrdinal: 2,
+ }),
+ makeTestAdapter({
+ instanceId: CODEX_MODEL_SELECTION.instanceId,
+ driver: CODEX_DRIVER,
+ capabilities: CodexProviderCapabilitiesV2,
+ modelSelection: CODEX_MODEL_SELECTION,
+ responseByRunOrdinal: {},
+ capturedTurns,
+ nativeThreadGeneration: yield* Ref.make(0),
+ }),
+ ]);
+ const reservedThreadId = ThreadId.make("thread:fresh-context-reserved");
+ yield* Effect.gen(function* () {
+ const orchestrator = yield* Orchestrator.OrchestratorV2;
+ const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
+ const send = (
+ name: string,
+ modelSelection: ModelSelection,
+ options: { readonly freshContext?: boolean } = {},
+ ) =>
+ orchestrator.dispatch({
+ type: "message.dispatch",
+ createdBy: "user",
+ creationSource: "web",
+ commandId: CommandId.make(`command:fresh-context-reserved:${name}`),
+ threadId: reservedThreadId,
+ messageId: MessageId.make(`message:fresh-context-reserved:${name}`),
+ text: `Prompt ${name}`,
+ attachments: [],
+ modelSelection,
+ ...options,
+ dispatchMode: { type: "queue_after_active" },
+ });
+ yield* orchestrator.dispatch({
+ type: "thread.create",
+ createdBy: "user",
+ creationSource: "web",
+ commandId: CommandId.make("command:fresh-context-reserved:create"),
+ threadId: reservedThreadId,
+ projectId: ProjectId.make("project:fresh-context-reserved"),
+ title: "Fresh context reserved",
+ modelSelection: CODEX_MODEL_SELECTION,
+ runtimeMode: "full-access",
+ interactionMode: "default",
+ branch: null,
+ worktreePath: cwd,
+ });
+ yield* send("zero", CODEX_MODEL_SELECTION);
+ yield* worker.drain();
+ yield* waitForIdle(reservedThreadId);
+ yield* send("alpha", CLAUDE_MODEL_SELECTION);
+ yield* Deferred.await(started);
+ yield* worker.drain();
+ // Queued while no fresh run has started: gamma binds to zero's Codex thread, and
+ // delta reserves its own. Once beta starts, gamma must not take delta's thread.
+ yield* send("beta", CLAUDE_MODEL_SELECTION, { freshContext: true });
+ yield* send("gamma", CODEX_MODEL_SELECTION);
+ yield* send("delta", CODEX_MODEL_SELECTION, { freshContext: true });
+ yield* Deferred.succeed(release, undefined);
+ yield* worker.drain();
+ yield* waitForIdle(reservedThreadId);
+ yield* worker.drain();
+ const turns = yield* Ref.get(capturedTurns);
+ const turnFor = (name: string) =>
+ turns.find((turn) => turn.text.endsWith(`Prompt ${name}`))!;
+
+ assert.notEqual(turnFor("gamma").providerThreadId, turnFor("zero").providerThreadId);
+ assert.notEqual(turnFor("delta").providerThreadId, turnFor("gamma").providerThreadId);
+ assert.notEqual(turnFor("delta").nativeThreadId, turnFor("gamma").nativeThreadId);
+ assert.equal(turnFor("delta").text, "Prompt delta");
+ }).pipe(
+ Effect.provide(
+ ProviderReplayHarness.layerWithRegistry(
+ {
+ name: "fresh-context-reserved",
+ runtimePolicyOverride: {
+ cwd,
+ approvalPolicy: "never",
+ sandboxPolicy: {
+ type: "readOnly",
+ access: { type: "fullAccess" },
+ networkAccess: false,
+ },
+ },
+ },
+ layerRegistry,
+ ),
+ ),
+ );
+ }),
+ ),
+ );
+
// regression: #21314 a queued PR update must follow the provider that took over the thread
it.live.each([
["automatic", "Claude"],
diff --git a/packages/contracts/src/environment.ts b/packages/contracts/src/environment.ts
index 9ac89ef499b..e7a34dfe675 100644
--- a/packages/contracts/src/environment.ts
+++ b/packages/contracts/src/environment.ts
@@ -168,6 +168,8 @@ export const ExecutionEnvironmentCapabilities = Schema.Struct({
triage: Schema.optionalKey(Schema.Boolean),
/** Server accepts `{ type: "once" }` scheduled tasks; older servers reject them on save. */
oneTimeSchedules: Schema.optionalKey(Schema.Boolean),
+ /** Server honors `freshContext` on sends; older servers ignore it and keep the old context. */
+ freshContext: Schema.optionalKey(Schema.Boolean),
});
export type ExecutionEnvironmentCapabilities = typeof ExecutionEnvironmentCapabilities.Type;
diff --git a/packages/contracts/src/orchestrationV2.ts b/packages/contracts/src/orchestrationV2.ts
index 5eb2b6ed4bf..6e70cc66a72 100644
--- a/packages/contracts/src/orchestrationV2.ts
+++ b/packages/contracts/src/orchestrationV2.ts
@@ -705,6 +705,8 @@ export const OrchestrationV2Run = Schema.Struct({
),
delegatedCompletion: Schema.optional(OrchestrationV2DelegatedCompletionCohort),
workspacePreparation: Schema.optional(OrchestrationV2ThreadLaunchWorkspaceStrategy),
+ /** Starts a new provider conversation; earlier runs stay visible but never reach the provider. */
+ freshContext: Schema.optional(Schema.Boolean),
});
export type OrchestrationV2Run = typeof OrchestrationV2Run.Type;
@@ -1607,6 +1609,7 @@ export const OrchestrationV2TurnItem = Schema.Union([
senderThreadId: Schema.optional(ThreadId),
idleCompaction: Schema.optional(OrchestrationV2IdleCompaction),
workflowScript: OrchestrationV2ConversationMessage.fields.workflowScript,
+ freshContext: OrchestrationV2Run.fields.freshContext,
inputIntent: OrchestrationV2UserMessageInputIntent,
text: Schema.String,
/** Same as the text of message `messageId`. */
@@ -2443,6 +2446,7 @@ export const OrchestrationV2TurnItemJson = Schema.Union([
senderThreadId: Schema.optional(ThreadId),
idleCompaction: Schema.optional(OrchestrationV2IdleCompaction),
workflowScript: OrchestrationV2ConversationMessage.fields.workflowScript,
+ freshContext: OrchestrationV2Run.fields.freshContext,
inputIntent: OrchestrationV2UserMessageInputIntent,
text: Schema.String,
context: Schema.optional(OrchestrationMessageContext),
@@ -3152,6 +3156,8 @@ export const OrchestrationV2Command = Schema.Union([
scheduledTaskId: Schema.optional(ScheduledTaskId),
senderThreadId: Schema.optional(ThreadId),
idleCompaction: Schema.optional(OrchestrationV2IdleCompaction),
+ /** Start a new provider session without the thread's earlier context. Never steers. */
+ freshContext: Schema.optional(Schema.Boolean),
commandId: CommandId,
threadId: ThreadId,
messageId: MessageId,