Open History to see the last commit
1import type {
2 OrchestrationV2ProviderFailure,
3 OrchestrationV2Run,
4 OrchestrationV2TurnItem,
5} from "@t3tools/contracts";
6import * as DateTime from "effect/DateTime";
7
8type ErrorTurnItem = Extract<OrchestrationV2TurnItem, { type: "error" }>;
9/** Only error items are read, so callers may pass items stripped of other fields. */
10type FailureScanTurnItem =
11 | ErrorTurnItem
12 | { readonly type: Exclude<OrchestrationV2TurnItem["type"], "error"> };
13
14export function latestRootProviderFailure(
15 run: OrchestrationV2Run | null,
16 turnItems: ReadonlyArray<FailureScanTurnItem>,
17): OrchestrationV2ProviderFailure | null {
18 if (run?.status !== "failed") return null;
19 let latest: ErrorTurnItem | null = null;
20 for (const item of turnItems) {
21 if (
22 item.type !== "error" ||
23 item.status !== "failed" ||
24 item.runId !== run.id ||
25 item.nodeId !== run.rootNodeId
26 )
27 continue;
28 if (
29 latest === null ||
30 DateTime.toEpochMillis(item.updatedAt) > DateTime.toEpochMillis(latest.updatedAt) ||
31 (DateTime.toEpochMillis(item.updatedAt) === DateTime.toEpochMillis(latest.updatedAt) &&
32 (item.ordinal > latest.ordinal || (item.ordinal === latest.ordinal && item.id > latest.id)))
33 ) {
34 latest = item;
35 }
36 }
37 return latest?.failure ?? null;
38}
39
40export function threadErrorSummary(
41 failure: OrchestrationV2ProviderFailure | null,
42 sessionError: string | null,
43) {
44 const currentFailure =
45 sessionError !== null && sessionError !== failure?.message ? null : failure;
46 return {
47 usageLimitResetAt:
48 currentFailure?.class === "usage_limit" ? (currentFailure.resetAt ?? null) : null,
49 lastError: sessionError ?? failure?.message ?? null,
50 lastErrorClass: currentFailure?.class ?? null,
51 lastErrorCode: currentFailure?.code ?? null,
52 };
53}
54
55export function latestExecutedRun(
56 runs: ReadonlyArray<OrchestrationV2Run>,
57): OrchestrationV2Run | null {
58 let latest: OrchestrationV2Run | null = null;
59 for (const run of runs) {
60 if (run.status === "queued") continue;
61 if (run.status === "cancelled" && run.startedAt === null) continue;
62 if (latest === null || runRanAfter(run, latest)) latest = run;
63 }
64 return latest;
65}
66
67/** Held queues can execute out of ordinal order; unfinished runs count as latest. */
68export function runRanAfter(
69 run: Pick<OrchestrationV2Run, "ordinal" | "completedAt">,
70 other: Pick<OrchestrationV2Run, "ordinal" | "completedAt">,
71): boolean {
72 const end = (candidate: typeof run) =>
73 !candidate.completedAt
74 ? Number.POSITIVE_INFINITY
75 : DateTime.toEpochMillis(candidate.completedAt);
76 return end(run) === end(other) ? run.ordinal > other.ordinal : end(run) > end(other);
77}
78
79export function usageLimitBlockedRun(
80 runs: ReadonlyArray<OrchestrationV2Run>,
81 turnItems: ReadonlyArray<FailureScanTurnItem>,
82 sessionError: string | null,
83): OrchestrationV2Run | null {
84 const executed = latestExecutedRun(runs);
85 if (executed?.status !== "failed") return null;
86 const summary = threadErrorSummary(latestRootProviderFailure(executed, turnItems), sessionError);
87 return summary.lastErrorClass === "usage_limit" ? executed : null;
88}
89
90/** Retains a limited run's outcome when higher-ordinal runs never started. */
91export function usageLimitRunPresentedAsLatest(
92 runs: ReadonlyArray<OrchestrationV2Run>,
93 turnItems: ReadonlyArray<FailureScanTurnItem>,
94 sessionError: string | null,
95): OrchestrationV2Run | null {
96 const blocked = usageLimitBlockedRun(runs, turnItems, sessionError);
97 return blocked !== null && runs.some((run) => run.ordinal > blocked.ordinal) ? blocked : null;
98}
99
100export function latestUnheldRun(
101 runs: ReadonlyArray<OrchestrationV2Run>,
102): OrchestrationV2Run | null {
103 let latest: OrchestrationV2Run | null = null;
104 for (const run of runs) {
105 if (run.status === "queued" && run.queueHeld === true) continue;
106 if (latest === null || run.ordinal > latest.ordinal) latest = run;
107 }
108 return latest;
109}