Open History to see the last commit
1import {
2 ORCHESTRATION_V2_WORKSPACE_PREPARATION_FAILURE_CODE,
3 type OrchestrationV2Run,
4 type OrchestrationV2TurnItem,
5} from "@t3tools/contracts";
6
7import type { ThreadStructureTurnItem } from "./models.ts";
8
9const WORKSPACE_PREPARATION_INPUT = "Preparing workspace";
10
11export function turnItemIsWorkspacePreparation(item: OrchestrationV2TurnItem): boolean {
12 return (
13 (item.type === "command_execution" && item.input === WORKSPACE_PREPARATION_INPUT) ||
14 (item.status === "cancelled" &&
15 (item.type === "run_interrupt_request" ||
16 item.type === "run_interrupt_result" ||
17 (item.type === "error" &&
18 item.failure.code === ORCHESTRATION_V2_WORKSPACE_PREPARATION_FAILURE_CODE)))
19 );
20}
21
22/** A reply the provider withdrew after a retry replaced it; it has nothing to show. */
23export function turnItemIsWithdrawnReply(item: OrchestrationV2TurnItem): boolean {
24 return (
25 item.type === "assistant_message" &&
26 item.status === "cancelled" &&
27 item.text.length === 0 &&
28 !item.attachments?.length
29 );
30}
31
32export function workspacePreparationRetryRunIds(
33 runs: ReadonlyArray<
34 Pick<OrchestrationV2Run, "id" | "status" | "startedAt" | "workspacePreparation">
35 >,
36 items: ReadonlyArray<ThreadStructureTurnItem>,
37): ReadonlySet<OrchestrationV2Run["id"]> {
38 const stoppedRuns = new Map<OrchestrationV2Run["id"], "failed" | "interrupted">();
39 for (const run of runs) {
40 if (run.workspacePreparation === undefined) continue;
41 if (
42 run.status === "failed" ||
43 (run.status === "interrupted" &&
44 run.startedAt === null &&
45 run.workspacePreparation.type === "worktree")
46 ) {
47 stoppedRuns.set(run.id, run.status);
48 }
49 }
50 const retryable = new Set<OrchestrationV2Run["id"]>();
51 if (stoppedRuns.size === 0) return retryable;
52 for (const item of items) {
53 if (item.runId === null) continue;
54 const runStatus = stoppedRuns.get(item.runId);
55 if (
56 (runStatus === "failed" &&
57 item.type === "error" &&
58 item.status === "failed" &&
59 item.failure.code === ORCHESTRATION_V2_WORKSPACE_PREPARATION_FAILURE_CODE) ||
60 (runStatus === "interrupted" &&
61 item.type === "command_execution" &&
62 item.input === WORKSPACE_PREPARATION_INPUT &&
63 item.status === "interrupted")
64 ) {
65 retryable.add(item.runId);
66 }
67 }
68 return retryable;
69}