Skip to content

Commit c53a2f2

Browse files
committed
fix(client-runtime): only count unloaded earlier runs as handoff history
1 parent 0cbbc3a commit c53a2f2

2 files changed

Lines changed: 18 additions & 4 deletions

File tree

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,16 @@ describe("pending context handoff", () => {
108108
expect(resolvePendingHandoff(windowed, { instanceId: to, model: "next" }, [])?.from).toEqual([
109109
{ instanceId: from, model: "source" },
110110
]);
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();
111121
});
112122
it("hands off from the latest started run, not the latest finished one", () => {
113123
const withLaterRun = (status: string, laterStartedAt: string | null) =>

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,15 @@ export function resolvePendingHandoff(
2929
(run) => run.startedAt !== null && run.status !== "rolled_back",
3030
);
3131
const latest = startedRuns?.at(-1);
32-
// Mirrors the server's handoff sources: a thread whose only run is still
33-
// running has no conversation to hand off yet. Bounded snapshots can omit
34-
// earlier finished runs, so a later ordinal also counts as history.
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);
3539
const hasHistory =
36-
(latest?.ordinal ?? 0) > 1 ||
40+
(latest !== undefined && earlierLoadedRuns < latest.ordinal - 1) ||
3741
startedRuns?.some(
3842
(run) =>
3943
run.status === "completed" || run.status === "failed" || run.status === "interrupted",

0 commit comments

Comments
 (0)