fix(mobile): preserve active thread state

Keep executing runs visible when newer work is queued, expose provider errors in compact shells, and preserve mobile creation metadata for fork and merge operations.

Co-authored-by: codex <[email protected]>
This commit is contained in:
Julius Marmingeandcodex committed 2026-07-28 12:15:53 +02:00
1 parent ae3a25649a
commit e56b1c3dde
16 files changed
+230 -44

No files matched your search

@@ -68,6 +68,21 @@ describe("V2 client presentation", () => {
expect(shell.source).toBe(v2ThreadShell);
});
it("presents provider errors carried by failed thread shells", () => {
const runId = RunId.make("run-failed");
const shell = presentThreadShell(environmentId, {
...v2ThreadShell,
latestRunId: runId,
status: "failed",
lastError: "provider process exited",
});
expect(shell.runtime).toMatchObject({
status: "failed",
lastError: "provider process exited",
});
});
it("derives execution summaries without wrapping or copying the projection", () => {
const runId = RunId.make("run-1");
const now = DateTime.makeUnsafe("2026-06-20T01:00:00.000Z");
+1 -1
View File
@@ -124,7 +124,7 @@ function shellRuntime(thread: OrchestrationV2ThreadShell): ThreadRuntimeSummary
activeRunId: thread.activeRunId,
providerInstanceId: thread.providerInstanceId,
providerName: null,
lastError: null,
lastError: thread.lastError ?? null,
updatedAt: iso(thread.updatedAt),
};
}
@@ -0,0 +1,71 @@
import { MessageId, RunId, type OrchestrationV2RunStatus } from "@t3tools/contracts";
import * as DateTime from "effect/DateTime";
import { describe, expect, it } from "vite-plus/test";
import { v2Projection } from "./orchestrationV2TestFixtures.ts";
import {
deriveLatestThreadRun,
deriveThreadActivityRun,
deriveThreadRuntime,
threadRuntimeHasInterruptibleRun,
} from "./threadExecution.ts";
const now = DateTime.makeUnsafe("2026-07-28T10:00:00.000Z");
function run(id: string, ordinal: number, status: OrchestrationV2RunStatus) {
return {
id: RunId.make(id),
threadId: v2Projection.thread.id,
ordinal,
providerInstanceId: v2Projection.thread.providerInstanceId,
modelSelection: v2Projection.thread.modelSelection,
providerThreadId: null,
userMessageId: MessageId.make(`message-${id}`),
rootNodeId: null,
activeAttemptId: null,
status,
requestedAt: now,
startedAt: status === "queued" ? null : now,
completedAt: null,
checkpointId: null,
contextHandoffId: null,
};
}
describe("thread execution presentation", () => {
it("keeps live activity attached to an executing run when a newer run is queued", () => {
const runningRun = run("run-running", 1, "running");
const queuedRun = run("run-queued", 2, "queued");
const projection = { ...v2Projection, runs: [queuedRun, runningRun], updatedAt: now };
expect(deriveLatestThreadRun(projection)?.runId).toBe(queuedRun.id);
expect(deriveThreadActivityRun(projection)).toMatchObject({
runId: runningRun.id,
status: "running",
});
const runtime = deriveThreadRuntime(projection);
expect(runtime).toMatchObject({
status: "queued",
activeRunId: runningRun.id,
});
expect(threadRuntimeHasInterruptibleRun(runtime)).toBe(true);
});
it("does not expose a queued-only run as interruptible", () => {
const queuedRun = run("run-queued", 1, "queued");
const projection = { ...v2Projection, runs: [queuedRun], updatedAt: now };
expect(deriveThreadActivityRun(projection)).toMatchObject({
runId: queuedRun.id,
status: "queued",
});
const runtime = deriveThreadRuntime(projection);
expect(runtime).toMatchObject({
status: "queued",
activeRunId: null,
});
expect(threadRuntimeHasInterruptibleRun(runtime)).toBe(false);
});
});
@@ -3,17 +3,25 @@ import * as DateTime from "effect/DateTime";
import type { ThreadRunSummary, ThreadRuntimeSummary } from "./models.ts";
const ACTIVE_RUN_STATUSES = new Set(["preparing", "queued", "starting", "running", "waiting"]);
const INTERRUPTIBLE_RUN_STATUSES = new Set(["preparing", "starting", "running", "waiting"]);
export function deriveLatestThreadRun(
function latestMatchingRun(
projection: OrchestrationV2ThreadProjection,
): ThreadRunSummary | null {
const run = projection.runs.reduce<(typeof projection.runs)[number] | null>(
predicate: (run: OrchestrationV2ThreadProjection["runs"][number]) => boolean,
): OrchestrationV2ThreadProjection["runs"][number] | null {
return projection.runs.reduce<OrchestrationV2ThreadProjection["runs"][number] | null>(
(latest, candidate) =>
latest === null || candidate.ordinal > latest.ordinal ? candidate : latest,
predicate(candidate) && (latest === null || candidate.ordinal > latest.ordinal)
? candidate
: latest,
null,
);
if (run === null) return null;
}
function summarizeThreadRun(
projection: OrchestrationV2ThreadProjection,
run: OrchestrationV2ThreadProjection["runs"][number],
): ThreadRunSummary {
return {
runId: run.id,
status: run.status,
@@ -28,6 +36,28 @@ export function deriveLatestThreadRun(
};
}
export function deriveLatestThreadRun(
projection: OrchestrationV2ThreadProjection,
): ThreadRunSummary | null {
const run = latestMatchingRun(projection, () => true);
return run === null ? null : summarizeThreadRun(projection, run);
}
/**
* Returns the run that owns live provider work, falling back to the newest run
* once the thread is idle. A newer queued run must not make an older executing
* run look settled in clients that render per-run activity.
*/
export function deriveThreadActivityRun(
projection: OrchestrationV2ThreadProjection,
): ThreadRunSummary | null {
const run =
latestMatchingRun(projection, (candidate) =>
INTERRUPTIBLE_RUN_STATUSES.has(candidate.status),
) ?? latestMatchingRun(projection, () => true);
return run === null ? null : summarizeThreadRun(projection, run);
}
export function deriveThreadRuntime(
projection: OrchestrationV2ThreadProjection,
): ThreadRuntimeSummary | null {
@@ -37,7 +67,7 @@ export function deriveThreadRuntime(
);
if (latestRun === null && projection.thread.activeProviderThreadId === null) return null;
const activeRunId =
projection.runs.findLast((run) => ACTIVE_RUN_STATUSES.has(run.status))?.id ?? null;
latestMatchingRun(projection, (run) => INTERRUPTIBLE_RUN_STATUSES.has(run.status))?.id ?? null;
return {
status: latestRun?.status ?? "idle",
activeRunId,
@@ -47,3 +77,9 @@ export function deriveThreadRuntime(
updatedAt: DateTime.formatIso(projection.updatedAt),
};
}
export function threadRuntimeHasInterruptibleRun(
runtime: ThreadRuntimeSummary | null | undefined,
): boolean {
return runtime?.activeRunId !== null && runtime?.activeRunId !== undefined;
}