Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ThreadSettlementPolicy.test.ts

196 lines6.7 KB

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 { 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: { state: "open" | "closed" | "merged"; updatedAt: string | null } | 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", updatedAt: 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", updatedAt: NOW }, { merge: false })).toBe(true);
104 expect(decide(makeThread(), { state: "merged", updatedAt: NOW }, { merge: false })).toBe(true);
105 expect(
106 decide(makeThread(), { state: "merged", updatedAt: 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", updatedAt: "2026-08-26T00:00:00.000Z" },
115 { days: null },
116 ),
117 ).toBe(false);
118 });
119​
120 it("does not inherit a terminal pull request older than the thread", () => {
121 expect(
122 decide(
123 makeThread({ createdAt: "2026-08-20T00:00:00.000Z", latestUserMessageAt: null }),
124 { state: "closed", updatedAt: "2026-08-19T00:00:00.000Z" },
125 { days: null },
126 ),
127 ).toBe(false);
128 });
129​
130 it("requires a comparable PR timestamp for immediate settlement", () => {
131 const recentThread = makeThread({ latestUserMessageAt: "2026-08-27T00:00:00.000Z" });
132 expect(decide(recentThread, { state: "closed", updatedAt: null })).toBe(false);
133 expect(decide(recentThread, { state: "merged", updatedAt: "unknown" })).toBe(false);
134 expect(decide(makeThread(), { state: "closed", updatedAt: null })).toBe(true);
135 });
136​
137 it("uses user request time instead of completion time as the PR anchor", () => {
138 const thread = makeThread({
139 latestTurn: {
140 turnId: TurnId.make("turn-1"),
141 state: "completed",
142 requestedAt: "2026-08-25T00:00:00.000Z",
143 startedAt: "2026-08-25T00:01:00.000Z",
144 completedAt: "2026-08-27T00:00:00.000Z",
145 assistantMessageId: null,
146 },
147 });
148 expect(decide(thread, { state: "merged", updatedAt: "2026-08-26T00:00:00.000Z" })).toBe(true);
149 });
150​
151 it("blocks pins, snooze, pending work, live sessions, and queued starts", () => {
152 expect(decide(makeThread({ settledOverride: "active" }))).toBe(false);
153 expect(decide(makeThread({ snoozedUntil: "2026-08-29T00:00:00.000Z" }))).toBe(false);
154 expect(decide(makeThread({ hasPendingApprovals: true }))).toBe(false);
155 expect(decide(makeThread({ hasPendingUserInput: true }))).toBe(false);
156 expect(decide(makeThread({ backgroundLiveness: "working" }))).toBe(false);
157 expect(decide(makeThread({ backgroundLiveness: "monitoring" }))).toBe(false);
158 expect(
159 decide(
160 makeThread({
161 session: {
162 threadId: ThreadId.make("thread-1"),
163 status: "running",
164 providerName: "codex",
165 runtimeMode: "full-access",
166 activeTurnId: TurnId.make("turn-1"),
167 lastError: null,
168 updatedAt: NOW,
169 },
170 }),
171 ),
172 ).toBe(false);
173 expect(
174 decide(makeThread({ latestUserMessageAt: "2026-08-28T11:59:00.000Z", latestTurn: null })),
175 ).toBe(false);
176 });
177​
178 it("allows a fresh completion to wake snooze before settlement", () => {
179 expect(
180 decide(
181 makeThread({
182 snoozedAt: "2026-08-19T00:00:00.000Z",
183 snoozedUntil: "2026-08-29T00:00:00.000Z",
184 latestTurn: {
185 turnId: TurnId.make("turn-woke"),
186 state: "completed",
187 requestedAt: "2026-08-18T00:00:00.000Z",
188 startedAt: "2026-08-18T00:01:00.000Z",
189 completedAt: "2026-08-20T00:00:00.000Z",
190 assistantMessageId: null,
191 },
192 }),
193 ),
194 ).toBe(true);
195 });
196});