Open History to see the last commit
1import type { OrchestrationThreadShell } from "@t3tools/contracts";
2
3export interface SettlementPullRequest {
4 readonly state: "open" | "closed" | "merged";
5 readonly closedAt?: string | null;
6 readonly mergedAt?: string | null;
7 readonly updatedAt?: string | null;
8}
9
10const DAY_MS = 24 * 60 * 60 * 1_000;
11const QUEUED_TURN_START_GRACE_MS = 2 * 60 * 1_000;
12
13function latestTimestamp(values: ReadonlyArray<string | null | undefined>): string | null {
14 let latest: string | null = null;
15 let latestMs = Number.NEGATIVE_INFINITY;
16 for (const value of values) {
17 if (value == null) continue;
18 const valueMs = Date.parse(value);
19 if (valueMs > latestMs) {
20 latest = value;
21 latestMs = valueMs;
22 }
23 }
24 return latest;
25}
26
27/** A recent user message stays queued until a turn adopts its timestamp.
28 * Absolute age bounds client clock skew in both directions and stops stale
29 * pre-adoption data from blocking the thread forever. */
30export function threadHasQueuedTurnStart(
31 thread: Pick<OrchestrationThreadShell, "latestUserMessageAt" | "latestTurn" | "session">,
32 now: string,
33): boolean {
34 if (thread.latestUserMessageAt === null || thread.session?.status === "error") return false;
35 const messageAt = Date.parse(thread.latestUserMessageAt);
36 const age = Date.parse(now) - messageAt;
37 if (Number.isNaN(age) || Math.abs(age) > QUEUED_TURN_START_GRACE_MS) return false;
38 if (thread.latestTurn === null) return true;
39 return [
40 thread.latestTurn.requestedAt,
41 thread.latestTurn.startedAt,
42 thread.latestTurn.completedAt,
43 ].every((value) => value == null || Date.parse(value) < messageAt);
44}
45
46function pullRequestSettles(
47 thread: Pick<OrchestrationThreadShell, "createdAt" | "latestUserMessageAt" | "latestTurn">,
48 pullRequest: SettlementPullRequest,
49 autoSettleOnMerge: boolean,
50): boolean {
51 if (pullRequest.state !== "closed" && (pullRequest.state !== "merged" || !autoSettleOnMerge)) {
52 return false;
53 }
54 const terminalAt = pullRequest.state === "merged" ? pullRequest.mergedAt : pullRequest.closedAt;
55 if (terminalAt == null) return false;
56 const userAnchor = latestTimestamp([
57 thread.createdAt,
58 thread.latestUserMessageAt,
59 thread.latestTurn?.requestedAt,
60 ]);
61 if (userAnchor === null) return false;
62 const pullRequestAt = Date.parse(terminalAt);
63 const userAnchorAt = Date.parse(userAnchor);
64 if (Number.isNaN(pullRequestAt) || Number.isNaN(userAnchorAt)) return false;
65 return pullRequestAt >= userAnchorAt;
66}
67
68export function resolveAutoSettlementAt(input: {
69 readonly thread: OrchestrationThreadShell;
70 readonly pullRequest: SettlementPullRequest | null;
71 readonly now: string;
72 readonly autoSettleAfterDays: number | null;
73 readonly autoSettleOnMerge: boolean;
74}): string | null {
75 const { thread, pullRequest } = input;
76 if (!isAutoSettlementCandidate(thread, input.now)) return null;
77 const activityAt = latestTimestamp([
78 thread.latestUserMessageAt,
79 thread.latestTurn?.requestedAt,
80 thread.latestTurn?.startedAt,
81 thread.latestTurn?.completedAt,
82 ]);
83 if (pullRequest !== null) {
84 if (pullRequestSettles(thread, pullRequest, input.autoSettleOnMerge)) {
85 return activityAt ?? thread.createdAt;
86 }
87 }
88 if (input.autoSettleAfterDays === null || activityAt === null) return null;
89 return Date.parse(activityAt) < Date.parse(input.now) - input.autoSettleAfterDays * DAY_MS
90 ? activityAt
91 : null;
92}
93
94/** Cheap checks that run before any source control lookup. */
95export function isAutoSettlementCandidate(thread: OrchestrationThreadShell, now: string): boolean {
96 if (thread.archivedAt !== null || thread.settledOverride !== null) return false;
97 if (thread.hasPendingApprovals || thread.hasPendingUserInput) return false;
98 if (thread.session?.status === "starting" || thread.session?.status === "running") return false;
99 if (thread.backgroundLiveness != null) return false;
100 if (threadHasQueuedTurnStart(thread, now)) return false;
101 if (thread.snoozedUntil == null || Date.parse(thread.snoozedUntil) <= Date.parse(now))
102 return true;
103 const wokeOnError =
104 thread.session?.status === "error" &&
105 (thread.snoozedAt == null ||
106 Date.parse(thread.session.updatedAt) > Date.parse(thread.snoozedAt));
107 const wokeOnCompletion =
108 thread.snoozedAt != null &&
109 thread.latestTurn?.state === "completed" &&
110 thread.latestTurn.completedAt != null &&
111 Date.parse(thread.latestTurn.completedAt) > Date.parse(thread.snoozedAt);
112 return wokeOnError || wokeOnCompletion;
113}