From 221a1557adcb1141e22448f5d2a86e9a3158f860 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Wed, 18 Nov 2026 00:11:16 +0000 Subject: [PATCH 1/4] test(server): a delta handoff into a resumed native session must say it supersedes that history Reproduces #38376: switching back to an earlier Claude instance resumes its old native session, and the delta handoff only labels the other instance's newer runs as historical context, so the model answers from the stale session. Co-Authored-By: Claude Opus 5.5 --- .../ContextHandoffBudget.test.ts | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/apps/server/src/orchestration-v2/ContextHandoffBudget.test.ts b/apps/server/src/orchestration-v2/ContextHandoffBudget.test.ts index 6a7b75528ef9d..39063cd56cc66 100644 --- a/apps/server/src/orchestration-v2/ContextHandoffBudget.test.ts +++ b/apps/server/src/orchestration-v2/ContextHandoffBudget.test.ts @@ -491,6 +491,33 @@ describe("handoff delivery", () => { }), ); + it.effect("tells a resumed native thread that a delta handoff supersedes its own history", () => + Effect.gen(function* () { + const result = yield* deliverContextHandoffs({ + handoffs: [ + { + ...handoff, + strategy: "delta_since_target_last_seen", + coveredRunOrdinals: { from: 158, to: 264 }, + history: { + ...handoff.history!, + coverage: handoffCoverage({ + threadId, + coveredRunOrdinals: { from: 158, to: 264 }, + items: [], + }), + }, + }, + ], + providerThread, + budget: 16_000, + alreadyDeliveredItemIds: new Set(), + persist: () => Effect.void, + }); + assert.match(result.context, /supersede/i); + }), + ); + it.effect("persists successful injection before turn start and skips it on retry", () => Effect.gen(function* () { let durable = handoff; From c86654e3a965afefa1e2b764a9979f07204eed76 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Wed, 18 Nov 2026 00:18:57 +0000 Subject: [PATCH 2/4] fix(server): a delta handoff into a resumed native thread says it supersedes that thread's history Switching back to an earlier provider instance resumes its old native session and prepends a delta handoff that only called the newer runs historical context, so the model answered from the stale session. The handoff now states that its runs came after the resumed conversation's own turns and supersede them where they conflict. Fixes pingdotgg/t3code#38376 Co-Authored-By: Claude Opus 5.5 --- apps/server/src/orchestration-v2/ContextHandoffDelivery.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/apps/server/src/orchestration-v2/ContextHandoffDelivery.ts b/apps/server/src/orchestration-v2/ContextHandoffDelivery.ts index 5211da2faf169..675da93c4c3b5 100644 --- a/apps/server/src/orchestration-v2/ContextHandoffDelivery.ts +++ b/apps/server/src/orchestration-v2/ContextHandoffDelivery.ts @@ -52,6 +52,12 @@ export const deliverContextHandoffs = Effect.fn("orchestrationV2.deliverContextH const strategies = Array.from(new Set(pending.map((handoff) => handoff.strategy))); coverage = `Context handoff (${strategies.join(", ")}). ${pending.length} handoff records; detailed coverage references omitted. Recover an omitted item only when the request needs it, with t3_thread_read({threadId:"${input.providerThread.appThreadId ?? pending[0]!.threadId}",view:"activity",itemId:"",maxCharsPerItem:4000}); continue long text with textOffset=nextTextOffset until null. Follow fork/handoff source references in activity. Items included below need no read.`; } + // A resumed native thread otherwise weighs its own older turns above this delta. + if ( + nativeThreadId !== undefined && + pending.some((handoff) => handoff.strategy === "delta_since_target_last_seen") + ) + coverage += `\nThe delta runs above happened after this conversation's own earlier turns and supersede them where they conflict.`; const seen = new Set(input.alreadyDeliveredItemIds); const messages = pending .flatMap((handoff) => handoff.history?.messages ?? []) From 225c6aecec1e545fc824aca8e70e0ea8060f29a2 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Wed, 18 Nov 2026 00:39:19 +0000 Subject: [PATCH 3/4] fix(server): switching back to a provider after a long delta starts a fresh native session The previous commit only added a sentence to the delta handoff. In the reported case the stale answer came from turns the trimmed delta left out, which no sentence can supply. When a cross-instance delta into a resumed native session would omit items, the resume fit check now treats that session as stale, so the existing fallback starts a fresh native session with the full thread history, as the staleness policy in docs/orchestration-v2/provider-switching-and-context.md describes. Same-thread retry deltas keep their native session. Fixes pingdotgg/t3code#38376 Co-Authored-By: Claude Opus 5.5 --- .../ContextHandoffBudget.test.ts | 49 ++++++++++--------- .../ContextHandoffDelivery.ts | 20 +++++--- .../ProviderTurnStartService.ts | 1 + .../ProviderSwitch.integration.test.ts | 20 ++++++-- 4 files changed, 58 insertions(+), 32 deletions(-) diff --git a/apps/server/src/orchestration-v2/ContextHandoffBudget.test.ts b/apps/server/src/orchestration-v2/ContextHandoffBudget.test.ts index 39063cd56cc66..b4c78cfe776e6 100644 --- a/apps/server/src/orchestration-v2/ContextHandoffBudget.test.ts +++ b/apps/server/src/orchestration-v2/ContextHandoffBudget.test.ts @@ -491,30 +491,35 @@ describe("handoff delivery", () => { }), ); - it.effect("tells a resumed native thread that a delta handoff supersedes its own history", () => + 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 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* deliverContextHandoffs({ - handoffs: [ - { - ...handoff, - strategy: "delta_since_target_last_seen", - coveredRunOrdinals: { from: 158, to: 264 }, - history: { - ...handoff.history!, - coverage: handoffCoverage({ - threadId, - coveredRunOrdinals: { from: 158, to: 264 }, - items: [], - }), + const result = yield* Effect.result( + deliverContextHandoffs({ + handoffs: [ + { + ...handoff, + strategy: "delta_since_target_last_seen", + fromProviderThreadIds: [ProviderThreadId.make(from)], + history: { ...handoff.history!, omittedItems: omitted }, }, - }, - ], - providerThread, - budget: 16_000, - alreadyDeliveredItemIds: new Set(), - persist: () => Effect.void, - }); - assert.match(result.context, /supersede/i); + ], + providerThread, + budget: 16_000, + requireWholeSwitchDelta: true, + alreadyDeliveredItemIds: new Set(), + persist: () => Effect.void, + }), + ); + assert.equal(result._tag === "Success", fits); }), ); diff --git a/apps/server/src/orchestration-v2/ContextHandoffDelivery.ts b/apps/server/src/orchestration-v2/ContextHandoffDelivery.ts index 675da93c4c3b5..df453f9269893 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, @@ -52,12 +54,6 @@ export const deliverContextHandoffs = Effect.fn("orchestrationV2.deliverContextH const strategies = Array.from(new Set(pending.map((handoff) => handoff.strategy))); coverage = `Context handoff (${strategies.join(", ")}). ${pending.length} handoff records; detailed coverage references omitted. Recover an omitted item only when the request needs it, with t3_thread_read({threadId:"${input.providerThread.appThreadId ?? pending[0]!.threadId}",view:"activity",itemId:"",maxCharsPerItem:4000}); continue long text with textOffset=nextTextOffset until null. Follow fork/handoff source references in activity. Items included below need no read.`; } - // A resumed native thread otherwise weighs its own older turns above this delta. - if ( - nativeThreadId !== undefined && - pending.some((handoff) => handoff.strategy === "delta_since_target_last_seen") - ) - coverage += `\nThe delta runs above happened after this conversation's own earlier turns and supersede them where they conflict.`; const seen = new Set(input.alreadyDeliveredItemIds); const messages = pending .flatMap((handoff) => handoff.history?.messages ?? []) @@ -86,6 +82,18 @@ 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.includes(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( From dfcb40840716af3761875e5849c344a35c392ded Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Wed, 18 Nov 2026 01:02:00 +0000 Subject: [PATCH 4/4] fix(server): a switch delta that also covers the target thread is still checked for trimming Exempt a trimmed delta from the stale-session check only when its sole source is the target thread itself. Co-Authored-By: Claude Opus 5.5 --- .../orchestration-v2/ContextHandoffBudget.test.ts | 14 ++++++++++---- .../src/orchestration-v2/ContextHandoffDelivery.ts | 3 ++- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/apps/server/src/orchestration-v2/ContextHandoffBudget.test.ts b/apps/server/src/orchestration-v2/ContextHandoffBudget.test.ts index b4c78cfe776e6..f5f72b352d20b 100644 --- a/apps/server/src/orchestration-v2/ContextHandoffBudget.test.ts +++ b/apps/server/src/orchestration-v2/ContextHandoffBudget.test.ts @@ -492,11 +492,17 @@ 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 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", + from: ["provider-thread:target"], omitted: 1, fits: true, }, @@ -508,7 +514,7 @@ describe("handoff delivery", () => { { ...handoff, strategy: "delta_since_target_last_seen", - fromProviderThreadIds: [ProviderThreadId.make(from)], + fromProviderThreadIds: from.map((id) => ProviderThreadId.make(id)), history: { ...handoff.history!, omittedItems: omitted }, }, ], diff --git a/apps/server/src/orchestration-v2/ContextHandoffDelivery.ts b/apps/server/src/orchestration-v2/ContextHandoffDelivery.ts index df453f9269893..0a1346763ed19 100644 --- a/apps/server/src/orchestration-v2/ContextHandoffDelivery.ts +++ b/apps/server/src/orchestration-v2/ContextHandoffDelivery.ts @@ -88,7 +88,8 @@ export const deliverContextHandoffs = Effect.fn("orchestrationV2.deliverContextH pending.some( (handoff) => handoff.strategy === "delta_since_target_last_seen" && - !handoff.fromProviderThreadIds.includes(handoff.toProviderThreadId) && + (handoff.fromProviderThreadIds.length !== 1 || + handoff.fromProviderThreadIds[0] !== handoff.toProviderThreadId) && ((handoff.history?.omittedItems ?? 0) > 0 || handoff.history?.messages.some((message) => omittedItemIds.has(message.itemId))), )