Skip to content

Commit 1736b60

Browse files
committed
fix(client-runtime): avoid duplicate handoff previews during startup
- Treat a committed handoff run as active once it starts, even before `startedAt` is set
1 parent b24e8ce commit 1736b60

2 files changed

Lines changed: 68 additions & 2 deletions

File tree

‎packages/client-runtime/src/handoff.test.ts‎

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
ContextHandoffId,
23
ProviderInstanceId,
34
RunId,
45
type OrchestrationV2ThreadProjection,
@@ -74,11 +75,72 @@ describe("handoff endpoints shared by web and mobile", () => {
7475
});
7576

7677
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+
],
7886
visibleTurnItems: [{ item: { type: "user_message" } }],
7987
} as unknown as OrchestrationV2ThreadProjection;
8088

8189
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+
82144
it("appears on selection, updates the destination, and disappears on returning", () => {
83145
expect(resolvePendingHandoff(projection, { instanceId: to, model: "first" }, [])).toEqual({
84146
from: [{ instanceId: from, model: "source" }],

‎packages/client-runtime/src/handoff.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,12 @@ export function resolvePendingHandoff(
2323
// A thread with an empty timeline has nothing to hand off. Any loaded row
2424
// counts: paging can leave a window of only tool rows.
2525
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.
2628
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)),
2832
)?.modelSelection;
2933
if (from === undefined || selection == null) return null;
3034
const source = providers.find((provider) => provider.instanceId === from.instanceId);

0 commit comments

Comments
 (0)