Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2import {
3 ProviderInstanceId,
4 ThreadId,
5 ProjectId,
6 TurnId,
7 type OrchestrationThreadShell,
8} from "@t3tools/contracts";
9import { type SettlementPullRequest, resolveAutoSettlementAt } from "./ThreadSettlementPolicy.ts";
10
11const NOW = "2026-08-28T12:00:00.000Z";
12const makeThread = (
13 overrides: Partial<OrchestrationThreadShell> = {},
14): OrchestrationThreadShell => ({
15 id: ThreadId.make("thread-1"),
16 projectId: ProjectId.make("project-1"),
17 title: "Thread",
18 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5" },
19 runtimeMode: "full-access",
20 interactionMode: "default",
21 branch: "feature",
22 worktreePath: "/repo",
23 latestTurn: null,
24 createdAt: "2026-08-01T00:00:00.000Z",
25 updatedAt: "2026-08-20T00:00:00.000Z",
26 archivedAt: null,
27 settledOverride: null,
28 settledAt: null,
29 session: null,
30 latestUserMessageAt: "2026-08-20T00:00:00.000Z",
31 hasPendingApprovals: false,
32 hasPendingUserInput: false,
33 hasActionableProposedPlan: false,
34 ...overrides,
35});
36
37const decide = (
38 thread: OrchestrationThreadShell,
39 pullRequest: SettlementPullRequest | null = null,
40 settings: { days?: number | null; merge?: boolean } = {},
41) =>
42 resolveAutoSettlementAt({
43 thread,
44 pullRequest,
45 now: NOW,
46 autoSettleAfterDays: settings.days === undefined ? 3 : settings.days,
47 autoSettleOnMerge: settings.merge ?? true,
48 }) !== null;
49
50describe("resolveAutoSettlementAt", () => {
51 it("returns the last activity time for persisted settlement", () => {
52 expect(
53 resolveAutoSettlementAt({
54 thread: makeThread({
55 latestTurn: {
56 turnId: TurnId.make("turn-terminal"),
57 state: "completed",
58 requestedAt: "2026-08-19T00:00:00.000Z",
59 startedAt: "2026-08-19T00:01:00.000Z",
60 completedAt: "2026-08-21T00:00:00.000Z",
61 assistantMessageId: null,
62 },
63 }),
64 pullRequest: null,
65 now: NOW,
66 autoSettleAfterDays: 3,
67 autoSettleOnMerge: true,
68 }),
69 ).toBe("2026-08-21T00:00:00.000Z");
70 });
71
72 it("uses creation time for PR settlement when the thread has no activity", () => {
73 expect(
74 resolveAutoSettlementAt({
75 thread: makeThread({
76 latestUserMessageAt: null,
77 latestTurn: null,
78 updatedAt: "2026-08-27T00:00:00.000Z",
79 }),
80 pullRequest: { state: "closed", closedAt: NOW },
81 now: NOW,
82 autoSettleAfterDays: null,
83 autoSettleOnMerge: true,
84 }),
85 ).toBe("2026-08-01T00:00:00.000Z");
86 });
87
88 it("settles inactive threads and leaves never-used threads active", () => {
89 expect(decide(makeThread())).toBe(true);
90 expect(decide(makeThread({ latestUserMessageAt: null }))).toBe(false);
91 expect(decide(makeThread(), null, { days: null })).toBe(false);
92 });
93
94 it("keeps a thread active at the exact inactivity boundary", () => {
95 expect(decide(makeThread({ latestUserMessageAt: "2026-08-25T12:00:00.000Z" }))).toBe(false);
96 });
97
98 it("settles inactive threads with open pull requests", () => {
99 expect(decide(makeThread(), { state: "open", updatedAt: NOW })).toBe(true);
100 });
101
102 it("settles closed requests and honors the merge setting", () => {
103 expect(decide(makeThread(), { state: "closed", closedAt: NOW }, { merge: false })).toBe(true);
104 expect(decide(makeThread(), { state: "merged", mergedAt: NOW }, { merge: false })).toBe(true);
105 expect(
106 decide(makeThread(), { state: "merged", mergedAt: NOW }, { merge: false, days: null }),
107 ).toBe(false);
108 });
109
110 it("does not settle again after user activity newer than the PR", () => {
111 expect(
112 decide(
113 makeThread({ latestUserMessageAt: "2026-08-27T00:00:00.000Z" }),
114 { state: "merged", mergedAt: "2026-08-26T00:00:00.000Z" },
115 { days: null },
116 ),
117 ).toBe(false);
118 });
119
120 it.each(["closed", "merged"] as const)(
121 "ignores metadata edits after resumed work for %s requests",
122 (state) => {
123 expect(
124 decide(
125 makeThread({ latestUserMessageAt: "2026-08-27T00:00:00.000Z" }),
126 {
127 state,
128 closedAt: "2026-08-26T00:00:00.000Z",
129 mergedAt: "2026-08-26T00:00:00.000Z",
130 updatedAt: NOW,
131 },
132 { days: null },
133 ),
134 ).toBe(false);
135 expect(decide(makeThread(), { state, updatedAt: NOW }, { days: null })).toBe(false);
136 },
137 );
138
139 it("does not inherit a terminal pull request older than the thread", () => {
140 expect(
141 decide(
142 makeThread({ createdAt: "2026-08-20T00:00:00.000Z", latestUserMessageAt: null }),
143 { state: "closed", closedAt: "2026-08-19T00:00:00.000Z" },
144 { days: null },
145 ),
146 ).toBe(false);
147 });
148
149 it("requires a comparable PR timestamp for immediate settlement", () => {
150 const recentThread = makeThread({ latestUserMessageAt: "2026-08-27T00:00:00.000Z" });
151 expect(decide(recentThread, { state: "closed", closedAt: null })).toBe(false);
152 expect(decide(recentThread, { state: "merged", mergedAt: "unknown" })).toBe(false);
153 expect(decide(makeThread(), { state: "closed", closedAt: null })).toBe(true);
154 });
155
156 it("uses user request time instead of completion time as the PR anchor", () => {
157 const thread = makeThread({
158 latestTurn: {
159 turnId: TurnId.make("turn-1"),
160 state: "completed",
161 requestedAt: "2026-08-25T00:00:00.000Z",
162 startedAt: "2026-08-25T00:01:00.000Z",
163 completedAt: "2026-08-27T00:00:00.000Z",
164 assistantMessageId: null,
165 },
166 });
167 expect(decide(thread, { state: "merged", mergedAt: "2026-08-26T00:00:00.000Z" })).toBe(true);
168 });
169
170 it("blocks pins, snooze, pending work, live sessions, and queued starts", () => {
171 expect(decide(makeThread({ settledOverride: "active" }))).toBe(false);
172 expect(decide(makeThread({ snoozedUntil: "2026-08-29T00:00:00.000Z" }))).toBe(false);
173 expect(decide(makeThread({ hasPendingApprovals: true }))).toBe(false);
174 expect(decide(makeThread({ hasPendingUserInput: true }))).toBe(false);
175 expect(decide(makeThread({ backgroundLiveness: "working" }))).toBe(false);
176 expect(decide(makeThread({ backgroundLiveness: "monitoring" }))).toBe(false);
177 expect(
178 decide(
179 makeThread({
180 session: {
181 threadId: ThreadId.make("thread-1"),
182 status: "running",
183 providerName: "codex",
184 runtimeMode: "full-access",
185 activeTurnId: TurnId.make("turn-1"),
186 lastError: null,
187 updatedAt: NOW,
188 },
189 }),
190 ),
191 ).toBe(false);
192 expect(
193 decide(makeThread({ latestUserMessageAt: "2026-08-28T11:59:00.000Z", latestTurn: null })),
194 ).toBe(false);
195 });
196
197 it("allows a fresh completion to wake snooze before settlement", () => {
198 expect(
199 decide(
200 makeThread({
201 snoozedAt: "2026-08-19T00:00:00.000Z",
202 snoozedUntil: "2026-08-29T00:00:00.000Z",
203 latestTurn: {
204 turnId: TurnId.make("turn-woke"),
205 state: "completed",
206 requestedAt: "2026-08-18T00:00:00.000Z",
207 startedAt: "2026-08-18T00:01:00.000Z",
208 completedAt: "2026-08-20T00:00:00.000Z",
209 assistantMessageId: null,
210 },
211 }),
212 ),
213 ).toBe(true);
214 });
215});