@devries-cameron20 / t3code Mirror · read-only / commits / 2c592a1086
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 22, 18:15 UTC

fix(client-runtime): avoid duplicate handoff previews during startup (#24349)

Yash Singh committed Oct 24, 2026 at 01:22 UTC 2c592a1086ff593293358529a1975dde308edeaf
2 files changed +68 -2
packages/client-runtime/src/handoff.test.ts
+63 -1
@@ -1,4 +1,5 @@
1 import {
2 + ContextHandoffId,
3 ProviderInstanceId,
4 RunId,
5 type OrchestrationV2ThreadProjection,
@@ -74,11 +75,72 @@ describe("handoff endpoints shared by web and mobile", () => {
75 });
76
77 const projection = {
77 - runs: [{ ...run("source", 1, from, "source"), startedAt: "2026-10-11T00:00:00Z" }],
78 + runs: [
79 + {
80 + ...run("source", 1, from, "source"),
81 + status: "completed",
82 + contextHandoffId: null,
83 + startedAt: "2026-10-11T00:00:00Z",
84 + },
85 + ],
86 visibleTurnItems: [{ item: { type: "user_message" } }],
87 } as unknown as OrchestrationV2ThreadProjection;
88
89 describe("pending context handoff", () => {
90 + it.each([from, to])("removes the preview while the handoff to %s starts", (instanceId) => {
91 + const selection = { instanceId, model: "destination" };
92 + const providers = [
93 + { instanceId: from, requiresNewThreadForModelChange: true },
94 + ] as unknown as ServerProvider[];
95 + const starting = {
96 + ...projection,
97 + runs: [
98 + ...projection.runs,
99 + {
100 + ...projection.runs[0]!,
101 + ...run("target", 2, instanceId, "destination"),
102 + status: "starting" as const,
103 + startedAt: null,
104 + contextHandoffId: ContextHandoffId.make("handoff"),
105 + },
106 + ],
107 + };
108 +
109 + expect(resolvePendingHandoff(projection, selection, providers)).not.toBeNull();
110 + expect(resolvePendingHandoff(starting, selection, providers)).toBeNull();
111 + expect(
112 + resolvePendingHandoff(starting, { instanceId: from, model: "source" }, providers),
113 + ).toEqual({
114 + from: [selection],
115 + to: { instanceId: from, model: "source" },
116 + });
117 + });
118 +
119 + it.each(["preparing", "queued", "failed", "cancelled", "rolled_back"] as const)(
120 + "keeps the preview when an unstarted handoff run is %s",
121 + (status) => {
122 + const selection = { instanceId: to, model: "destination" };
123 + const unstarted = {
124 + ...projection,
125 + runs: [
126 + ...projection.runs,
127 + {
128 + ...projection.runs[0]!,
129 + ...run("target", 2, to, "destination"),
130 + status,
131 + startedAt: null,
132 + contextHandoffId: ContextHandoffId.make("handoff"),
133 + },
134 + ],
135 + };
136 +
137 + expect(resolvePendingHandoff(unstarted, selection, [])).toEqual({
138 + from: [{ instanceId: from, model: "source" }],
139 + to: selection,
140 + });
141 + },
142 + );
143 +
144 it("appears on selection, updates the destination, and disappears on returning", () => {
145 expect(resolvePendingHandoff(projection, { instanceId: to, model: "first" }, [])).toEqual({
146 from: [{ instanceId: from, model: "source" }],
packages/client-runtime/src/handoff.ts
+5 -1
@@ -23,8 +23,12 @@ export function resolvePendingHandoff(
23 // A thread with an empty timeline has nothing to hand off. Any loaded row
24 // counts: paging can leave a window of only tool rows.
25 if (!projection?.visibleTurnItems.length) return null;
26 + // The handoff row is committed before the provider starts. Use its destination
27 + // during startup so the footer does not duplicate the committed handoff.
28 const from = projection.runs.findLast(
27 - (run) => run.startedAt !== null && run.status !== "rolled_back",
29 + (run) =>
30 + run.status !== "rolled_back" &&
31 + (run.startedAt !== null || (run.status === "starting" && run.contextHandoffId !== null)),
32 )?.modelSelection;
33 if (from === undefined || selection == null) return null;
34 const source = providers.find((provider) => provider.instanceId === from.instanceId);