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