From 18372e16301f69768348a5713a13df120c1ac7ff Mon Sep 17 00:00:00 2001 From: FabioLukas123 <146683844+FabioLukas123@users.noreply.github.com> Date: Wed, 14 Oct 2026 20:17:30 -0400 Subject: [PATCH 1/2] fix(clients): allow portable provider handoffs after detach --- .../state/thread-provider-switching.test.ts | 26 ++++++++++ .../ProviderSwitchService.test.ts | 48 ++++++++++------- .../src/state/threadWorkflows.test.ts | 52 +++++++++++++++++++ .../src/state/threadWorkflows.ts | 13 ++--- 4 files changed, 110 insertions(+), 29 deletions(-) diff --git a/apps/mobile/src/state/thread-provider-switching.test.ts b/apps/mobile/src/state/thread-provider-switching.test.ts index 950a4dd33685d..2f866fd739373 100644 --- a/apps/mobile/src/state/thread-provider-switching.test.ts +++ b/apps/mobile/src/state/thread-provider-switching.test.ts @@ -86,4 +86,30 @@ describe("threadAllowsProviderSwitch", () => { it("keeps a started thread bound until its projection resolves a session", () => { expect(threadAllowsProviderSwitch({ thread: startedThread, projection: null })).toBe(false); }); + + it("offers providers after disconnecting a thread without a native reference", () => { + expect( + threadAllowsProviderSwitch({ + thread: startedThread, + projection: { + thread: { + id: "thread", + activeProviderThreadId: "provider-thread", + modelSelection: { instanceId: "claudeAgent", model: "claude-opus-4-6" }, + }, + runs: [{ id: "run-1", status: "failed" }], + providerThreads: [ + { + id: "provider-thread", + appThreadId: "thread", + providerInstanceId: "claudeAgent", + providerSessionId: null, + nativeThreadRef: null, + }, + ], + providerSessions: [], + } as unknown as OrchestrationV2ThreadProjection, + }), + ).toBe(true); + }); }); diff --git a/apps/server/src/orchestration-v2/ProviderSwitchService.test.ts b/apps/server/src/orchestration-v2/ProviderSwitchService.test.ts index d36721bbd6a1c..769b6cb93ce79 100644 --- a/apps/server/src/orchestration-v2/ProviderSwitchService.test.ts +++ b/apps/server/src/orchestration-v2/ProviderSwitchService.test.ts @@ -253,26 +253,36 @@ it.effect("falls back to the native provider thread when every recorded session ), ); -it.effect("hands off from a native provider thread after its session detaches", () => - Effect.gen(function* () { - const service = yield* ProviderSwitch.ProviderSwitchServiceV2; - const result = yield* service.plan({ - projection: { ...deadNativeThreadProjection("stopped"), providerSessions: [] }, - targetModelSelection: { - instanceId: ProviderInstanceId.make("codex_other"), - model: "gpt-5.2-codex", - }, - }); - assert.equal(result.transition.type, "create_with_handoff"); - assert.deepEqual(result.releaseProviderSessionIds, []); - }).pipe( - Effect.provide( - layerTest({ - [currentInstanceId]: { continuationKey: "codex:account:primary" }, - codex_other: { continuationKey: "codex:account:other" }, - }), +it.effect.each([true, false])( + "hands off after its session detaches (native reference: %s)", + (hasNativeRef) => + Effect.gen(function* () { + const service = yield* ProviderSwitch.ProviderSwitchServiceV2; + const detached = deadNativeThreadProjection("stopped"); + const result = yield* service.plan({ + projection: { + ...detached, + providerSessions: [], + providerThreads: detached.providerThreads.map((thread) => ({ + ...thread, + nativeThreadRef: hasNativeRef ? thread.nativeThreadRef : null, + })), + }, + targetModelSelection: { + instanceId: ProviderInstanceId.make("codex_other"), + model: "gpt-5.2-codex", + }, + }); + assert.equal(result.transition.type, "create_with_handoff"); + assert.deepEqual(result.releaseProviderSessionIds, []); + }).pipe( + Effect.provide( + layerTest({ + [currentInstanceId]: { continuationKey: "codex:account:primary" }, + codex_other: { continuationKey: "codex:account:other" }, + }), + ), ), - ), ); it.effect.each(["stopped", "error"] as const)( diff --git a/packages/client-runtime/src/state/threadWorkflows.test.ts b/packages/client-runtime/src/state/threadWorkflows.test.ts index 23722260b8839..2350b1d590d6c 100644 --- a/packages/client-runtime/src/state/threadWorkflows.test.ts +++ b/packages/client-runtime/src/state/threadWorkflows.test.ts @@ -63,6 +63,58 @@ describe("thread workflows", () => { ...projection, providerThreads: [{ ...projection.providerThreads[0]!, nativeThreadRef: null }], }), + ).toBe(true); + }); + + it.each(["completed", "failed", "interrupted"] as const)( + "allows portable handoff after a %s run without a native session", + (status) => { + const projection = { + thread: { + id: "thread", + activeProviderThreadId: null, + historyOrigin: "native", + modelSelection: { instanceId: "claudeAgent", model: "claude-opus-4-6" }, + }, + runs: [{ id: "run", status }], + providerSessions: [], + providerThreads: [], + } as unknown as OrchestrationV2ThreadProjection; + + expect(threadSupportsProviderHandoff(projection)).toBe(true); + }, + ); + + it.each(["preparing", "starting", "running", "waiting"] as const)( + "keeps a %s run bound until its session capabilities resolve", + (status) => { + expect( + threadSupportsProviderHandoff({ + thread: { id: "thread", activeProviderThreadId: null }, + runs: [{ id: "run", status }], + providerSessions: [], + providerThreads: [], + } as unknown as OrchestrationV2ThreadProjection), + ).toBe(false); + }, + ); + + it("honors a live session that cannot hand off", () => { + expect( + threadSupportsProviderHandoff({ + thread: { id: "thread", activeProviderThreadId: "provider-thread" }, + runs: [], + providerThreads: [ + { id: "provider-thread", appThreadId: "thread", providerSessionId: "session" }, + ], + providerSessions: [ + { + id: "session", + status: "ready", + capabilities: { sessions: { supportsProviderSwitchingViaHandoff: false } }, + }, + ], + } as unknown as OrchestrationV2ThreadProjection), ).toBe(false); }); diff --git a/packages/client-runtime/src/state/threadWorkflows.ts b/packages/client-runtime/src/state/threadWorkflows.ts index cb2b4f1fdb4ab..98eb650a3ca9b 100644 --- a/packages/client-runtime/src/state/threadWorkflows.ts +++ b/packages/client-runtime/src/state/threadWorkflows.ts @@ -94,17 +94,10 @@ export function threadSupportsProviderHandoff(projection: Projection | null | un return session.capabilities.sessions.supportsProviderSwitchingViaHandoff; } if (resolveActiveThreadRun(projection) !== null) return false; - if (projection.thread.historyOrigin === "v1_import" || projection.runs.length === 0) return true; - // Detaching a stopped session removes it from the projection, but its native - // provider thread remains available for the next turn's portable handoff. - return projection.providerThreads.some( - (thread) => - thread.id === projection.thread.activeProviderThreadId && - thread.appThreadId === projection.thread.id && - thread.providerInstanceId === projection.thread.modelSelection.instanceId && - thread.nativeThreadRef !== null, - ); + // Without an attached session, the server can hand off saved app history even + // when the outgoing provider never returned a native thread reference. + return true; } /** Automatic completion/notification runs are not messages in the user's queue. */ From 798ab3170850b0224e03fb695ab0ed86953b798b Mon Sep 17 00:00:00 2001 From: Julius Marminge <51714798+juliusmarminge@users.noreply.github.com> Date: Sun, 18 Oct 2026 17:16:31 -0700 Subject: [PATCH 2/2] fix(clients): simplify detached provider handoff coverage --- .../state/thread-provider-switching.test.ts | 26 ---------- .../ProviderSwitchService.test.ts | 48 +++++++---------- .../src/state/threadWorkflows.test.ts | 52 ------------------- .../src/state/threadWorkflows.ts | 4 +- 4 files changed, 21 insertions(+), 109 deletions(-) diff --git a/apps/mobile/src/state/thread-provider-switching.test.ts b/apps/mobile/src/state/thread-provider-switching.test.ts index 2f866fd739373..950a4dd33685d 100644 --- a/apps/mobile/src/state/thread-provider-switching.test.ts +++ b/apps/mobile/src/state/thread-provider-switching.test.ts @@ -86,30 +86,4 @@ describe("threadAllowsProviderSwitch", () => { it("keeps a started thread bound until its projection resolves a session", () => { expect(threadAllowsProviderSwitch({ thread: startedThread, projection: null })).toBe(false); }); - - it("offers providers after disconnecting a thread without a native reference", () => { - expect( - threadAllowsProviderSwitch({ - thread: startedThread, - projection: { - thread: { - id: "thread", - activeProviderThreadId: "provider-thread", - modelSelection: { instanceId: "claudeAgent", model: "claude-opus-4-6" }, - }, - runs: [{ id: "run-1", status: "failed" }], - providerThreads: [ - { - id: "provider-thread", - appThreadId: "thread", - providerInstanceId: "claudeAgent", - providerSessionId: null, - nativeThreadRef: null, - }, - ], - providerSessions: [], - } as unknown as OrchestrationV2ThreadProjection, - }), - ).toBe(true); - }); }); diff --git a/apps/server/src/orchestration-v2/ProviderSwitchService.test.ts b/apps/server/src/orchestration-v2/ProviderSwitchService.test.ts index 72a93217a9cdb..d9b1de61a5715 100644 --- a/apps/server/src/orchestration-v2/ProviderSwitchService.test.ts +++ b/apps/server/src/orchestration-v2/ProviderSwitchService.test.ts @@ -253,36 +253,26 @@ it.effect("falls back to the native provider thread when every recorded session ), ); -it.effect.each([true, false])( - "hands off after its session detaches (native reference: %s)", - (hasNativeRef) => - Effect.gen(function* () { - const service = yield* ProviderSwitch.ProviderSwitchServiceV2; - const detached = deadNativeThreadProjection("stopped"); - const result = yield* service.plan({ - projection: { - ...detached, - providerSessions: [], - providerThreads: detached.providerThreads.map((thread) => ({ - ...thread, - nativeThreadRef: hasNativeRef ? thread.nativeThreadRef : null, - })), - }, - targetModelSelection: { - instanceId: ProviderInstanceId.make("codex_other"), - model: "gpt-5.2-codex", - }, - }); - assert.equal(result.transition.type, "create_with_handoff"); - assert.deepEqual(result.releaseProviderSessionIds, []); - }).pipe( - Effect.provide( - layerTest({ - [currentInstanceId]: { continuationKey: "codex:account:primary" }, - codex_other: { continuationKey: "codex:account:other" }, - }), - ), +it.effect("hands off from a native provider thread after its session detaches", () => + Effect.gen(function* () { + const service = yield* ProviderSwitch.ProviderSwitchServiceV2; + const result = yield* service.plan({ + projection: { ...deadNativeThreadProjection("stopped"), providerSessions: [] }, + targetModelSelection: { + instanceId: ProviderInstanceId.make("codex_other"), + model: "gpt-5.2-codex", + }, + }); + assert.equal(result.transition.type, "create_with_handoff"); + assert.deepEqual(result.releaseProviderSessionIds, []); + }).pipe( + Effect.provide( + layerTest({ + [currentInstanceId]: { continuationKey: "codex:account:primary" }, + codex_other: { continuationKey: "codex:account:other" }, + }), ), + ), ); it.effect.each(["stopped", "error"] as const)( diff --git a/packages/client-runtime/src/state/threadWorkflows.test.ts b/packages/client-runtime/src/state/threadWorkflows.test.ts index 2350b1d590d6c..85e5459cdb8d7 100644 --- a/packages/client-runtime/src/state/threadWorkflows.test.ts +++ b/packages/client-runtime/src/state/threadWorkflows.test.ts @@ -66,58 +66,6 @@ describe("thread workflows", () => { ).toBe(true); }); - it.each(["completed", "failed", "interrupted"] as const)( - "allows portable handoff after a %s run without a native session", - (status) => { - const projection = { - thread: { - id: "thread", - activeProviderThreadId: null, - historyOrigin: "native", - modelSelection: { instanceId: "claudeAgent", model: "claude-opus-4-6" }, - }, - runs: [{ id: "run", status }], - providerSessions: [], - providerThreads: [], - } as unknown as OrchestrationV2ThreadProjection; - - expect(threadSupportsProviderHandoff(projection)).toBe(true); - }, - ); - - it.each(["preparing", "starting", "running", "waiting"] as const)( - "keeps a %s run bound until its session capabilities resolve", - (status) => { - expect( - threadSupportsProviderHandoff({ - thread: { id: "thread", activeProviderThreadId: null }, - runs: [{ id: "run", status }], - providerSessions: [], - providerThreads: [], - } as unknown as OrchestrationV2ThreadProjection), - ).toBe(false); - }, - ); - - it("honors a live session that cannot hand off", () => { - expect( - threadSupportsProviderHandoff({ - thread: { id: "thread", activeProviderThreadId: "provider-thread" }, - runs: [], - providerThreads: [ - { id: "provider-thread", appThreadId: "thread", providerSessionId: "session" }, - ], - providerSessions: [ - { - id: "session", - status: "ready", - capabilities: { sessions: { supportsProviderSwitchingViaHandoff: false } }, - }, - ], - } as unknown as OrchestrationV2ThreadProjection), - ).toBe(false); - }); - it("sorts queued messages and gates reorder and promotion from capabilities", () => { const state = deriveThreadQueueWorkflowState({ thread: { id: "thread", activeProviderThreadId: "provider-thread" }, diff --git a/packages/client-runtime/src/state/threadWorkflows.ts b/packages/client-runtime/src/state/threadWorkflows.ts index 6ca577ee6f503..7ff1043eb2212 100644 --- a/packages/client-runtime/src/state/threadWorkflows.ts +++ b/packages/client-runtime/src/state/threadWorkflows.ts @@ -94,8 +94,8 @@ export function threadSupportsProviderHandoff(projection: Projection | null | un } if (resolveActiveThreadRun(projection) !== null) return false; - // Without an attached session, the server can hand off saved app history even - // when the outgoing provider never returned a native thread reference. + // With no live session the server plans a portable handoff from saved app + // history, so the outgoing provider's native thread ref is not required. return true; }