Skip to content

Commit 2e66975

Browse files
committed
fix(client-runtime): hide pending handoff until the thread shows a sent message
1 parent c53a2f2 commit 2e66975

2 files changed

Lines changed: 10 additions & 78 deletions

File tree

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

Lines changed: 5 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ describe("handoff endpoints shared by web and mobile", () => {
7373
});
7474
});
7575

76-
const startedAt = "2026-10-11T00:00:00Z";
7776
const projection = {
78-
runs: [{ ...run("source", 1, from, "source"), startedAt, status: "completed" }],
77+
runs: [{ ...run("source", 1, from, "source"), startedAt: "2026-10-11T00:00:00Z" }],
78+
visibleTurnItems: [{ item: { type: "user_message" } }],
7979
} as unknown as OrchestrationV2ThreadProjection;
8080

8181
describe("pending context handoff", () => {
@@ -90,65 +90,10 @@ describe("pending context handoff", () => {
9090
expect(resolvePendingHandoff(projection, { instanceId: from, model: "source" }, [])).toBeNull();
9191
expect(resolvePendingHandoff(projection, { instanceId: from, model: "other" }, [])).toBeNull();
9292
});
93-
it("stays hidden until a run has history to hand off", () => {
94-
const firstRun = (status: string) =>
95-
({
96-
runs: [{ ...run("source", 1, from, "source"), startedAt, status }],
97-
}) as unknown as OrchestrationV2ThreadProjection;
98-
for (const status of ["running", "cancelled", "rolled_back"]) {
99-
expect(
100-
resolvePendingHandoff(firstRun(status), { instanceId: to, model: "first" }, []),
101-
).toBeNull();
102-
}
103-
});
104-
it("treats a later run as history when earlier runs are outside the loaded window", () => {
105-
const windowed = {
106-
runs: [{ ...run("current", 5, from, "source"), startedAt, status: "running" }],
107-
} as unknown as OrchestrationV2ThreadProjection;
108-
expect(resolvePendingHandoff(windowed, { instanceId: to, model: "next" }, [])?.from).toEqual([
109-
{ instanceId: from, model: "source" },
110-
]);
111-
// Every earlier run is loaded and none produced conversation.
112-
const afterFailedStart = {
113-
runs: [
114-
{ ...run("failed", 1, from, "source"), startedAt: null, status: "failed" },
115-
{ ...run("current", 2, from, "source"), startedAt, status: "running" },
116-
],
117-
} as unknown as OrchestrationV2ThreadProjection;
118-
expect(
119-
resolvePendingHandoff(afterFailedStart, { instanceId: to, model: "next" }, []),
120-
).toBeNull();
121-
});
122-
it("hands off from the latest started run, not the latest finished one", () => {
123-
const withLaterRun = (status: string, laterStartedAt: string | null) =>
124-
({
125-
runs: [
126-
...projection.runs,
127-
{ ...run("later", 2, to, "later"), startedAt: laterStartedAt, status },
128-
],
129-
}) as unknown as OrchestrationV2ThreadProjection;
130-
// A running handoff on the new provider is the source for the next send.
131-
expect(
132-
resolvePendingHandoff(
133-
withLaterRun("running", startedAt),
134-
{ instanceId: to, model: "later" },
135-
[],
136-
),
137-
).toBeNull();
138-
expect(
139-
resolvePendingHandoff(
140-
withLaterRun("running", startedAt),
141-
{ instanceId: from, model: "source" },
142-
[],
143-
)?.from,
144-
).toEqual([{ instanceId: to, model: "later" }]);
145-
// A handoff that failed before starting never reached the new provider.
93+
it("stays hidden until the thread shows a sent message", () => {
94+
const withoutMessages = { ...projection, visibleTurnItems: [] };
14695
expect(
147-
resolvePendingHandoff(
148-
withLaterRun("failed", null),
149-
{ instanceId: from, model: "source" },
150-
[],
151-
),
96+
resolvePendingHandoff(withoutMessages, { instanceId: to, model: "first" }, []),
15297
).toBeNull();
15398
});
15499
it("only previews a same-provider model change when it needs a fresh native thread", () => {

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

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,12 @@ export function resolvePendingHandoff(
2525
selection: ModelSelection | null | undefined,
2626
providers: ReadonlyArray<ServerProvider>,
2727
): HandoffEndpoints | null {
28-
const startedRuns = projection?.runs.filter(
28+
// Nothing to hand off until the thread shows a sent message.
29+
if (!projection?.visibleTurnItems.some(({ item }) => item.type === "user_message")) return null;
30+
const from = projection.runs.findLast(
2931
(run) => run.startedAt !== null && run.status !== "rolled_back",
30-
);
31-
const latest = startedRuns?.at(-1);
32-
// A thread whose only run is still running has no conversation to hand off
33-
// yet. Bounded snapshots can omit earlier runs, so missing ordinals before
34-
// the latest run also count as history.
35-
const earlierLoadedRuns =
36-
latest === undefined
37-
? 0
38-
: (projection?.runs.filter((run) => run.ordinal < latest.ordinal).length ?? 0);
39-
const hasHistory =
40-
(latest !== undefined && earlierLoadedRuns < latest.ordinal - 1) ||
41-
startedRuns?.some(
42-
(run) =>
43-
run.status === "completed" || run.status === "failed" || run.status === "interrupted",
44-
);
45-
const from = latest?.modelSelection;
46-
if (!hasHistory || from === undefined || selection == null) return null;
32+
)?.modelSelection;
33+
if (from === undefined || selection == null) return null;
4734
const source = providers.find((provider) => provider.instanceId === from.instanceId);
4835
const target = providers.find((provider) => provider.instanceId === selection.instanceId);
4936
const needsHandoff =

0 commit comments

Comments
 (0)