TypeScript
fix(client-runtime): avoid duplicate handoff previews during startup (#24349)
Yash Singh2c592a1086ff
diff --git a/packages/client-runtime/src/handoff.test.ts b/packages/client-runtime/src/handoff.test.tsindex 5df78402054..0f92619e304 100644--- a/packages/client-runtime/src/handoff.test.ts+++ b/packages/client-runtime/src/handoff.test.ts@@ -1,4 +1,5 @@import {+ ContextHandoffId,ProviderInstanceId,RunId,type OrchestrationV2ThreadProjection,@@ -74,11 +75,72 @@ describe("handoff endpoints shared by web and mobile", () => {});const projection = {- runs: [{ ...run("source", 1, from, "source"), startedAt: "2026-10-11T00:00:00Z" }],+ runs: [+ {+ ...run("source", 1, from, "source"),+ status: "completed",+ contextHandoffId: null,+ startedAt: "2026-10-11T00:00:00Z",+ },+ ],visibleTurnItems: [{ item: { type: "user_message" } }],} as unknown as OrchestrationV2ThreadProjection;describe("pending context handoff", () => {+ it.each([from, to])("removes the preview while the handoff to %s starts", (instanceId) => {+ const selection = { instanceId, model: "destination" };+ const providers = [+ { instanceId: from, requiresNewThreadForModelChange: true },+ ] as unknown as ServerProvider[];+ const starting = {+ ...projection,+ runs: [+ ...projection.runs,+ {+ ...projection.runs[0]!,+ ...run("target", 2, instanceId, "destination"),+ status: "starting" as const,+ startedAt: null,+ contextHandoffId: ContextHandoffId.make("handoff"),+ },+ ],+ };++ expect(resolvePendingHandoff(projection, selection, providers)).not.toBeNull();+ expect(resolvePendingHandoff(starting, selection, providers)).toBeNull();+ expect(+ resolvePendingHandoff(starting, { instanceId: from, model: "source" }, providers),+ ).toEqual({+ from: [selection],+ to: { instanceId: from, model: "source" },+ });+ });++ it.each(["preparing", "queued", "failed", "cancelled", "rolled_back"] as const)(+ "keeps the preview when an unstarted handoff run is %s",+ (status) => {+ const selection = { instanceId: to, model: "destination" };+ const unstarted = {+ ...projection,+ runs: [+ ...projection.runs,+ {+ ...projection.runs[0]!,+ ...run("target", 2, to, "destination"),+ status,+ startedAt: null,+ contextHandoffId: ContextHandoffId.make("handoff"),+ },+ ],+ };++ expect(resolvePendingHandoff(unstarted, selection, [])).toEqual({+ from: [{ instanceId: from, model: "source" }],+ to: selection,+ });+ },+ );+it("appears on selection, updates the destination, and disappears on returning", () => {expect(resolvePendingHandoff(projection, { instanceId: to, model: "first" }, [])).toEqual({from: [{ instanceId: from, model: "source" }],diff --git a/packages/client-runtime/src/handoff.ts b/packages/client-runtime/src/handoff.tsindex 27c0f38c176..2eebdaf2c40 100644--- a/packages/client-runtime/src/handoff.ts+++ b/packages/client-runtime/src/handoff.ts@@ -23,8 +23,12 @@ export function resolvePendingHandoff(// A thread with an empty timeline has nothing to hand off. Any loaded row// counts: paging can leave a window of only tool rows.if (!projection?.visibleTurnItems.length) return null;+ // The handoff row is committed before the provider starts. Use its destination+ // during startup so the footer does not duplicate the committed handoff.const from = projection.runs.findLast(- (run) => run.startedAt !== null && run.status !== "rolled_back",+ (run) =>+ run.status !== "rolled_back" &&+ (run.startedAt !== null || (run.status === "starting" && run.contextHandoffId !== null)),)?.modelSelection;if (from === undefined || selection == null) return null;const source = providers.find((provider) => provider.instanceId === from.instanceId);