diff --git a/apps/server/src/orchestration-v2/ContextHandoffBudget.test.ts b/apps/server/src/orchestration-v2/ContextHandoffBudget.test.ts index 6a7b75528ef9d..f5f72b352d20b 100644 --- a/apps/server/src/orchestration-v2/ContextHandoffBudget.test.ts +++ b/apps/server/src/orchestration-v2/ContextHandoffBudget.test.ts @@ -491,6 +491,44 @@ describe("handoff delivery", () => { }), ); + it.effect.each([ + { label: "a trimmed switch delta", from: ["provider-thread:other"], omitted: 366, fits: false }, + { label: "a whole switch delta", from: ["provider-thread:other"], omitted: 0, fits: true }, + { + label: "a trimmed delta that also covers the target", + from: ["provider-thread:target", "provider-thread:other"], + omitted: 1, + fits: false, + }, + { + label: "a trimmed same-thread retry delta", + from: ["provider-thread:target"], + omitted: 1, + fits: true, + }, + ])("treats a resumed native thread as stale only for $label", ({ from, omitted, fits }) => + Effect.gen(function* () { + const result = yield* Effect.result( + deliverContextHandoffs({ + handoffs: [ + { + ...handoff, + strategy: "delta_since_target_last_seen", + fromProviderThreadIds: from.map((id) => ProviderThreadId.make(id)), + history: { ...handoff.history!, omittedItems: omitted }, + }, + ], + providerThread, + budget: 16_000, + requireWholeSwitchDelta: true, + alreadyDeliveredItemIds: new Set(), + persist: () => Effect.void, + }), + ); + assert.equal(result._tag === "Success", fits); + }), + ); + it.effect("persists successful injection before turn start and skips it on retry", () => Effect.gen(function* () { let durable = handoff; diff --git a/apps/server/src/orchestration-v2/ContextHandoffDelivery.ts b/apps/server/src/orchestration-v2/ContextHandoffDelivery.ts index 5211da2faf169..0a1346763ed19 100644 --- a/apps/server/src/orchestration-v2/ContextHandoffDelivery.ts +++ b/apps/server/src/orchestration-v2/ContextHandoffDelivery.ts @@ -23,6 +23,8 @@ export const deliverContextHandoffs = Effect.fn("orchestrationV2.deliverContextH readonly providerThread: OrchestrationV2ProviderThread; readonly budget: number | Effect.Effect; readonly deferInline?: boolean; + /** Refuse a trimmed delta from another provider thread into this resumed native thread. */ + readonly requireWholeSwitchDelta?: boolean; readonly alreadyDeliveredItemIds: ReadonlySet; readonly inject?: ( history: ProviderAdapter.ProviderAdapterV2HistoricalContext, @@ -80,6 +82,19 @@ export const deliverContextHandoffs = Effect.fn("orchestrationV2.deliverContextH return yield* new ContextHandoffBudgetError(); } const omittedItemIds = new Set(selected.omittedItemIds); + // The resumed native history would outweigh what the trimmed delta dropped; the caller starts fresh. + if ( + input.requireWholeSwitchDelta && + pending.some( + (handoff) => + handoff.strategy === "delta_since_target_last_seen" && + (handoff.fromProviderThreadIds.length !== 1 || + handoff.fromProviderThreadIds[0] !== handoff.toProviderThreadId) && + ((handoff.history?.omittedItems ?? 0) > 0 || + handoff.history?.messages.some((message) => omittedItemIds.has(message.itemId))), + ) + ) + return yield* new ContextHandoffBudgetError(); const persist = (status: "pending" | "injected" | "inline") => Effect.forEach( pending, diff --git a/apps/server/src/orchestration-v2/ProviderTurnStartService.ts b/apps/server/src/orchestration-v2/ProviderTurnStartService.ts index 28f8f474cd6ab..6495ee826f363 100644 --- a/apps/server/src/orchestration-v2/ProviderTurnStartService.ts +++ b/apps/server/src/orchestration-v2/ProviderTurnStartService.ts @@ -1450,6 +1450,7 @@ export const layer: Layer.Layer< providerThread: { ...resumed.success, id: providerThread.id }, budget: context.budget, deferInline: isCompactionCommandMessage(providerMessage) || goalCommand, + requireWholeSwitchDelta: true, alreadyDeliveredItemIds: context.deliveredItemIds, persist: () => Effect.void, }).pipe( 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 a05636033eb8b..79c93d91c9cbe 100644 --- a/apps/server/src/orchestration-v2/testkit/ProviderSwitch.integration.test.ts +++ b/apps/server/src/orchestration-v2/testkit/ProviderSwitch.integration.test.ts @@ -1292,7 +1292,19 @@ describe("orchestration v2 provider switching", () => { }), ); - it.live("keeps a native session whose stored prose is far below an unreported window", () => + it.live.each([ + { + label: "keeps a native session whose stored prose is far below an unreported window", + sourceWork: "Source work", + generations: 1, + }, + // The resumed native history would outweigh what a trimmed delta drops, so the session is stale. + { + label: "replaces the native session when the switch-back delta must be trimmed", + sourceWork: "Newer decision. ".repeat(8_000), + generations: 2, + }, + ])("$label", ({ sourceWork, generations }) => Effect.gen(function* () { const cwd = yield* checkpointWorkspace("handoff-estimate-headroom"); const capturedTurns = yield* Ref.make>([]); @@ -1305,7 +1317,7 @@ describe("orchestration v2 provider switching", () => { driver: CODEX_DRIVER, capabilities: CodexProviderCapabilitiesV2, modelSelection: CODEX_MODEL_SELECTION, - responseByRunOrdinal: { 2: "Source work" }, + responseByRunOrdinal: { 2: sourceWork }, capturedTurns, }), // No window and no usage, like an ACP registry agent that sends no usage_update. @@ -1370,8 +1382,8 @@ describe("orchestration v2 provider switching", () => { (yield* orchestrator.getThreadProjection(threadId)).runs.at(-1)?.status, "completed", ); - // A second generation means T3 discarded the native session as full. - assert.equal(yield* Ref.get(generation), 1); + // A second generation means T3 discarded the native session. + assert.equal(yield* Ref.get(generation), generations); }).pipe( Effect.provide( ProviderReplayHarness.layerWithRegistry(