fix(client): preserve live thread relationships

Co-authored-by: codex <[email protected]>
This commit is contained in:
Julius Marmingeandcodex committed 2026-08-15 16:50:18 +02:00
1 parent a40a1d8630
commit fdf06c403a
4 files changed
+88 -5

No files matched your search

@@ -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");
@@ -48,10 +48,18 @@ export function deriveThreadRelationshipGraph(input: {
readonly threads: ReadonlyArray<OrchestrationV2ThreadShell>;
readonly projection: OrchestrationV2ThreadProjection | null;
}): ThreadRelationshipGraph {
const threadsById = new Map<ThreadId, OrchestrationV2ThreadShell>();
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<ThreadId, ThreadRelationshipNode>(
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<string, ThreadRelationshipEdge>();
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
@@ -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();
},
);
});
@@ -11,6 +11,11 @@ type ProviderSession = Projection["providerSessions"][number];
const ACTIVE_RUN_STATUSES = new Set<Run["status"]>(["preparing", "starting", "running", "waiting"]);
const MERGE_BACK_RUN_STATUSES = new Set<Run["status"]>(["waiting", "completed"]);
const MERGE_BACK_BLOCKING_RUN_STATUSES = new Set<Run["status"]>([
"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<Run | null>(
const latestProviderFinishedRun = projection.runs.reduce<Run | null>(
(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 {