Skip to content

Commit 0cbbc3a

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

2 files changed

Lines changed: 18 additions & 5 deletions

File tree

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,14 @@ describe("pending context handoff", () => {
101101
).toBeNull();
102102
}
103103
});
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+
});
104112
it("hands off from the latest started run, not the latest finished one", () => {
105113
const withLaterRun = (status: string, laterStartedAt: string | null) =>
106114
({

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,17 @@ export function resolvePendingHandoff(
2828
const startedRuns = projection?.runs.filter(
2929
(run) => run.startedAt !== null && run.status !== "rolled_back",
3030
);
31+
const latest = startedRuns?.at(-1);
3132
// Mirrors the server's handoff sources: a thread whose only run is still
32-
// running has no conversation to hand off yet.
33-
const hasHistory = startedRuns?.some(
34-
(run) => run.status === "completed" || run.status === "failed" || run.status === "interrupted",
35-
);
36-
const from = startedRuns?.at(-1)?.modelSelection;
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.
35+
const hasHistory =
36+
(latest?.ordinal ?? 0) > 1 ||
37+
startedRuns?.some(
38+
(run) =>
39+
run.status === "completed" || run.status === "failed" || run.status === "interrupted",
40+
);
41+
const from = latest?.modelSelection;
3742
if (!hasHistory || from === undefined || selection == null) return null;
3843
const source = providers.find((provider) => provider.instanceId === from.instanceId);
3944
const target = providers.find((provider) => provider.instanceId === selection.instanceId);

0 commit comments

Comments
 (0)