From fdf06c403a9e49b9f4efe0a8b4e6cb360b037f9d Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Tue, 28 Jul 2026 14:24:54 +0200 Subject: [PATCH] fix(client): preserve live thread relationships Co-authored-by: codex --- .../src/state/threadRelationships.test.ts | 48 +++++++++++++++++++ .../src/state/threadRelationships.ts | 16 +++++-- .../src/state/threadWorkflows.test.ts | 14 ++++++ .../src/state/threadWorkflows.ts | 15 +++++- 4 files changed, 88 insertions(+), 5 deletions(-) diff --git a/packages/client-runtime/src/state/threadRelationships.test.ts b/packages/client-runtime/src/state/threadRelationships.test.ts index b601222654..fc384d5edd 100644 --- a/packages/client-runtime/src/state/threadRelationships.test.ts +++ b/packages/client-runtime/src/state/threadRelationships.test.ts @@ -106,6 +106,54 @@ describe("thread relationships", () => { ); }); + it("keeps the live shell when an archived snapshot contains the same thread id", () => { + const parent = ThreadId.make("thread-parent"); + const staleParent = ThreadId.make("thread-stale-parent"); + const child = ThreadId.make("thread-child"); + const liveChild = { + id: child, + title: "Live child", + status: "running", + archivedAt: null, + forkedFrom: { type: "run", threadId: parent, runId: "run-live" }, + lineage: { + rootThreadId: parent, + parentThreadId: parent, + relationshipToParent: "fork", + }, + } as const; + const staleArchivedChild = { + ...liveChild, + title: "Stale archived child", + status: "completed", + archivedAt: "2026-06-24T00:00:00.000Z", + forkedFrom: { type: "run", threadId: staleParent, runId: "run-stale" }, + lineage: { + rootThreadId: staleParent, + parentThreadId: staleParent, + relationshipToParent: "fork", + }, + } as const; + + const graph = deriveThreadRelationshipGraph({ + threads: [liveChild, staleArchivedChild] as never, + projection: null, + }); + + expect(graph.nodes.get(child)?.thread).toMatchObject({ + title: "Live child", + status: "running", + archivedAt: null, + }); + expect(graph.edges).toEqual([ + expect.objectContaining({ + sourceThreadId: parent, + targetThreadId: child, + }), + ]); + expect(graph.nodes.has(staleParent)).toBe(false); + }); + it("resolves merge-back only for forks and prefers the recorded fork source", () => { const source = ThreadId.make("thread-source"); const fallbackParent = ThreadId.make("thread-parent"); diff --git a/packages/client-runtime/src/state/threadRelationships.ts b/packages/client-runtime/src/state/threadRelationships.ts index d57c5a468b..da6f527aea 100644 --- a/packages/client-runtime/src/state/threadRelationships.ts +++ b/packages/client-runtime/src/state/threadRelationships.ts @@ -48,10 +48,18 @@ export function deriveThreadRelationshipGraph(input: { readonly threads: ReadonlyArray; readonly projection: OrchestrationV2ThreadProjection | null; }): ThreadRelationshipGraph { + const threadsById = new Map(); + for (const thread of input.threads) { + // Callers order shells from most to least authoritative. In particular, + // live shells precede archived snapshots, which may still contain a stale + // copy during archive refresh. + if (!threadsById.has(thread.id)) { + threadsById.set(thread.id, thread); + } + } + const threads = [...threadsById.values()]; const nodes = new Map( - input.threads.map( - (thread) => [thread.id, { threadId: thread.id, thread, missing: false }] as const, - ), + threads.map((thread) => [thread.id, { threadId: thread.id, thread, missing: false }]), ); const edgesByKey = new Map(); const ensureNode = (threadId: ThreadId) => { @@ -65,7 +73,7 @@ export function deriveThreadRelationshipGraph(input: { edgesByKey.set(edgeKey(edge), edge); }; - for (const thread of input.threads) { + for (const thread of threads) { const parentThreadId = thread.forkedFrom?.type === "run" ? thread.forkedFrom.threadId diff --git a/packages/client-runtime/src/state/threadWorkflows.test.ts b/packages/client-runtime/src/state/threadWorkflows.test.ts index d5bfb0995e..44699d130f 100644 --- a/packages/client-runtime/src/state/threadWorkflows.test.ts +++ b/packages/client-runtime/src/state/threadWorkflows.test.ts @@ -143,4 +143,18 @@ describe("thread workflows", () => { expect(resolveLatestMergeBackRun(projection)?.id).toBe("newest-finished"); }); + + it.each(["preparing", "starting", "running"] as const)( + "does not merge older history while a newer run is %s", + (status) => { + const projection = { + runs: [ + { id: "older-completed", status: "completed", ordinal: 1 }, + { id: "newer-active", status, ordinal: 2 }, + ], + } as never; + + expect(resolveLatestMergeBackRun(projection)).toBeNull(); + }, + ); }); diff --git a/packages/client-runtime/src/state/threadWorkflows.ts b/packages/client-runtime/src/state/threadWorkflows.ts index 81536d2516..20134b9cfc 100644 --- a/packages/client-runtime/src/state/threadWorkflows.ts +++ b/packages/client-runtime/src/state/threadWorkflows.ts @@ -11,6 +11,11 @@ type ProviderSession = Projection["providerSessions"][number]; const ACTIVE_RUN_STATUSES = new Set(["preparing", "starting", "running", "waiting"]); const MERGE_BACK_RUN_STATUSES = new Set(["waiting", "completed"]); +const MERGE_BACK_BLOCKING_RUN_STATUSES = new Set([ + "preparing", + "starting", + "running", +]); export interface QueuedThreadRun { readonly run: Run; @@ -34,13 +39,21 @@ export function resolveActiveThreadRun(projection: Projection): Run | null { * instead of falling through to an older fully checkpointed run. */ export function resolveLatestMergeBackRun(projection: Projection): Run | null { - return projection.runs.reduce( + const latestProviderFinishedRun = projection.runs.reduce( (latest, run) => MERGE_BACK_RUN_STATUSES.has(run.status) && (latest === null || run.ordinal > latest.ordinal) ? run : latest, null, ); + if (latestProviderFinishedRun === null) return null; + + const hasNewerActiveRun = projection.runs.some( + (run) => + run.ordinal > latestProviderFinishedRun.ordinal && + MERGE_BACK_BLOCKING_RUN_STATUSES.has(run.status), + ); + return hasNewerActiveRun ? null : latestProviderFinishedRun; } export function resolveThreadProviderSession(projection: Projection): ProviderSession | null {