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
8/** Only a failed root turn of the current run owns the thread's failure state. */
9export function latestRootProviderFailure(
10 run: OrchestrationV2Run | null,
11 turnItems: ReadonlyArray<OrchestrationV2TurnItem>,
12): OrchestrationV2ProviderFailure | null {
13 if (run?.status !== "failed") return null;
14 let latest: Extract<OrchestrationV2TurnItem, { type: "error" }> | null = null;
15 for (const item of turnItems) {
16 if (
17 item.type !== "error" ||
18 item.status !== "failed" ||
19 item.runId !== run.id ||
20 item.nodeId !== run.rootNodeId
21 )
22 continue;
23 if (
24 latest === null ||
25 DateTime.toEpochMillis(item.updatedAt) > DateTime.toEpochMillis(latest.updatedAt) ||
26 (DateTime.toEpochMillis(item.updatedAt) === DateTime.toEpochMillis(latest.updatedAt) &&
27 (item.ordinal > latest.ordinal || (item.ordinal === latest.ordinal && item.id > latest.id)))
28 ) {
29 latest = item;
30 }
31 }
32 return latest?.failure ?? null;
33}
34
35/** A distinct session failure supersedes the turn's classification. */
36export function threadErrorSummary(
37 failure: OrchestrationV2ProviderFailure | null,
38 sessionError: string | null,
39) {
40 const currentFailure =
41 sessionError !== null && sessionError !== failure?.message ? null : failure;
42 return {
43 usageLimitResetAt:
44 currentFailure?.class === "usage_limit" ? (currentFailure.resetAt ?? null) : null,
45 lastError: sessionError ?? failure?.message ?? null,
46 lastErrorClass:
47 sessionError !== null && sessionError !== failure?.message ? null : (failure?.class ?? null),
48 };
49}
50
51export function latestExecutedRun(
52 runs: ReadonlyArray<OrchestrationV2Run>,
53): OrchestrationV2Run | null {
54 let latest: OrchestrationV2Run | null = null;
55 for (const run of runs) {
56 if (run.status === "queued") continue;
57 if (run.status === "cancelled" && run.startedAt === null) continue;
58 if (latest === null || run.ordinal > latest.ordinal) latest = run;
59 }
60 return latest;
61}
62
63/**
64 * The latest run that actually started, when it stopped because the
65 * subscription limit was reached. Queued messages after that run must stay
66 * queued instead of being sent into the same limit.
67 */
68export function usageLimitBlockedRun(
69 runs: ReadonlyArray<OrchestrationV2Run>,
70 turnItems: ReadonlyArray<OrchestrationV2TurnItem>,
71 sessionError: string | null,
72): OrchestrationV2Run | null {
73 const executed = latestExecutedRun(runs);
74 if (executed?.status !== "failed") return null;
75 const summary = threadErrorSummary(latestRootProviderFailure(executed, turnItems), sessionError);
76 return summary.lastErrorClass === "usage_limit" ? executed : null;
77}
78
79/**
80 * That limited run, when newer runs were queued or cancelled before starting.
81 * Callers that treat the highest-ordinal run as the thread outcome would
82 * otherwise hide the limit.
83 */
84export function usageLimitRunPresentedAsLatest(
85 runs: ReadonlyArray<OrchestrationV2Run>,
86 turnItems: ReadonlyArray<OrchestrationV2TurnItem>,
87 sessionError: string | null,
88): OrchestrationV2Run | null {
89 const blocked = usageLimitBlockedRun(runs, turnItems, sessionError);
90 return blocked !== null && runs.some((run) => run.ordinal > blocked.ordinal) ? blocked : null;
91}
92
93/**
94 * The newest run that is not waiting in a held queue, or null when only held
95 * runs exist. A held queue waits for the user, so its runs never stand for the
96 * thread's outcome. The SQL thread shell selects the same run.
97 */
98export function latestUnheldRun(
99 runs: ReadonlyArray<OrchestrationV2Run>,
100): OrchestrationV2Run | null {
101 let latest: OrchestrationV2Run | null = null;
102 for (const run of runs) {
103 if (run.status === "queued" && run.queueHeld === true) continue;
104 if (latest === null || run.ordinal > latest.ordinal) latest = run;
105 }
106 return latest;
107}