fix(orchestration): handle checkpoint-wait runs

Allow merge-back from provider-finished runs awaiting checkpoint capture while keeping waiting runs visible but non-interruptible in mobile.

Co-authored-by: codex <[email protected]>
This commit is contained in:
Julius Marmingeandcodex authored and maria-rcks committed 2026-08-03 11:45:11 -04:00
1 parent 091a76003e
commit 6ab4ce9fe3
8 files changed
+238 -14

No files matched your search

@@ -68,4 +68,21 @@ describe("thread execution presentation", () => {
});
expect(threadRuntimeHasInterruptibleRun(runtime)).toBe(false);
});
it("keeps checkpoint-wait activity visible without exposing a non-functional interrupt", () => {
const waitingRun = run("run-waiting", 1, "waiting");
const projection = { ...v2Projection, runs: [waitingRun], updatedAt: now };
expect(deriveThreadActivityRun(projection)).toMatchObject({
runId: waitingRun.id,
status: "waiting",
});
const runtime = deriveThreadRuntime(projection);
expect(runtime).toMatchObject({
status: "waiting",
activeRunId: null,
});
expect(threadRuntimeHasInterruptibleRun(runtime)).toBe(false);
});
});
@@ -3,7 +3,8 @@ import * as DateTime from "effect/DateTime";
import type { ThreadRunSummary, ThreadRuntimeSummary } from "./models.ts";
const INTERRUPTIBLE_RUN_STATUSES = new Set(["preparing", "starting", "running", "waiting"]);
const ACTIVITY_RUN_STATUSES = new Set(["preparing", "starting", "running", "waiting"]);
const INTERRUPTIBLE_RUN_STATUSES = new Set(["preparing", "starting", "running"]);
function latestMatchingRun(
projection: OrchestrationV2ThreadProjection,
@@ -52,9 +53,8 @@ export function deriveThreadActivityRun(
projection: OrchestrationV2ThreadProjection,
): ThreadRunSummary | null {
const run =
latestMatchingRun(projection, (candidate) =>
INTERRUPTIBLE_RUN_STATUSES.has(candidate.status),
) ?? latestMatchingRun(projection, () => true);
latestMatchingRun(projection, (candidate) => ACTIVITY_RUN_STATUSES.has(candidate.status)) ??
latestMatchingRun(projection, () => true);
return run === null ? null : summarizeThreadRun(projection, run);
}
@@ -4,6 +4,7 @@ import {
canDetachThreadProviderSession,
canForkProjectedAssistantItem,
deriveThreadQueueWorkflowState,
resolveLatestMergeBackRun,
} from "./threadWorkflows.ts";
const capabilities = (input?: {
@@ -130,4 +131,16 @@ describe("thread workflows", () => {
}),
).toBe(false);
});
it("merges the newest provider-finished run while checkpoint capture is pending", () => {
const projection = {
runs: [
{ id: "newest-queued", status: "queued", ordinal: 3 },
{ id: "older-completed", status: "completed", ordinal: 1 },
{ id: "newest-finished", status: "waiting", ordinal: 2 },
],
} as never;
expect(resolveLatestMergeBackRun(projection)?.id).toBe("newest-finished");
});
});
@@ -10,6 +10,7 @@ type Run = Projection["runs"][number];
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"]);
export interface QueuedThreadRun {
readonly run: Run;
@@ -27,6 +28,21 @@ export function resolveActiveThreadRun(projection: Projection): Run | null {
return projection.runs.findLast((run) => ACTIVE_RUN_STATUSES.has(run.status)) ?? null;
}
/**
* A successfully finished provider turn remains in `waiting` while its
* checkpoint is captured. Keep that newest turn available for merge-back
* instead of falling through to an older fully checkpointed run.
*/
export function resolveLatestMergeBackRun(projection: Projection): Run | null {
return projection.runs.reduce<Run | null>(
(latest, run) =>
MERGE_BACK_RUN_STATUSES.has(run.status) && (latest === null || run.ordinal > latest.ordinal)
? run
: latest,
null,
);
}
export function resolveThreadProviderSession(projection: Projection): ProviderSession | null {
const activeRun = resolveActiveThreadRun(projection);
const providerThreadId = activeRun?.providerThreadId ?? projection.thread.activeProviderThreadId;