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

ThreadSettlementPolicy.test.ts

159 lines5.6 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 { shouldAutoSettleThread } 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 shouldAutoSettleThread({
43 thread,
44 pullRequest,
45 now: NOW,
46 autoSettleAfterDays: settings.days === undefined ? 3 : settings.days,
47 autoSettleOnMerge: settings.merge ?? true,
48 });
49​
50describe("shouldAutoSettleThread", () => {
51 it("settles inactive threads and leaves never-used threads active", () => {
52 expect(decide(makeThread())).toBe(true);
53 expect(decide(makeThread({ latestUserMessageAt: null }))).toBe(false);
54 expect(decide(makeThread(), null, { days: null })).toBe(false);
55 });
56​
57 it("keeps a thread active at the exact inactivity boundary", () => {
58 expect(decide(makeThread({ latestUserMessageAt: "2026-08-25T12:00:00.000Z" }))).toBe(false);
59 });
60​
61 it("keeps open pull requests active", () => {
62 expect(decide(makeThread(), { state: "open", updatedAt: NOW })).toBe(false);
63 });
64​
65 it("settles closed requests and honors the merge setting", () => {
66 expect(decide(makeThread(), { state: "closed", updatedAt: NOW }, { merge: false })).toBe(true);
67 expect(decide(makeThread(), { state: "merged", updatedAt: NOW }, { merge: false })).toBe(true);
68 expect(
69 decide(makeThread(), { state: "merged", updatedAt: NOW }, { merge: false, days: null }),
70 ).toBe(false);
71 });
72​
73 it("does not settle again after user activity newer than the PR", () => {
74 expect(
75 decide(
76 makeThread({ latestUserMessageAt: "2026-08-27T00:00:00.000Z" }),
77 { state: "merged", updatedAt: "2026-08-26T00:00:00.000Z" },
78 { days: null },
79 ),
80 ).toBe(false);
81 });
82​
83 it("does not inherit a terminal pull request older than the thread", () => {
84 expect(
85 decide(
86 makeThread({ createdAt: "2026-08-20T00:00:00.000Z", latestUserMessageAt: null }),
87 { state: "closed", updatedAt: "2026-08-19T00:00:00.000Z" },
88 { days: null },
89 ),
90 ).toBe(false);
91 });
92​
93 it("requires a comparable PR timestamp for immediate settlement", () => {
94 const recentThread = makeThread({ latestUserMessageAt: "2026-08-27T00:00:00.000Z" });
95 expect(decide(recentThread, { state: "closed", updatedAt: null })).toBe(false);
96 expect(decide(recentThread, { state: "merged", updatedAt: "unknown" })).toBe(false);
97 expect(decide(makeThread(), { state: "closed", updatedAt: null })).toBe(true);
98 });
99​
100 it("uses user request time instead of completion time as the PR anchor", () => {
101 const thread = makeThread({
102 latestTurn: {
103 turnId: TurnId.make("turn-1"),
104 state: "completed",
105 requestedAt: "2026-08-25T00:00:00.000Z",
106 startedAt: "2026-08-25T00:01:00.000Z",
107 completedAt: "2026-08-27T00:00:00.000Z",
108 assistantMessageId: null,
109 },
110 });
111 expect(decide(thread, { state: "merged", updatedAt: "2026-08-26T00:00:00.000Z" })).toBe(true);
112 });
113​
114 it("blocks pins, snooze, pending work, live sessions, and queued starts", () => {
115 expect(decide(makeThread({ settledOverride: "active" }))).toBe(false);
116 expect(decide(makeThread({ snoozedUntil: "2026-08-29T00:00:00.000Z" }))).toBe(false);
117 expect(decide(makeThread({ hasPendingApprovals: true }))).toBe(false);
118 expect(decide(makeThread({ hasPendingUserInput: true }))).toBe(false);
119 expect(decide(makeThread({ backgroundLiveness: "working" }))).toBe(false);
120 expect(decide(makeThread({ backgroundLiveness: "monitoring" }))).toBe(false);
121 expect(
122 decide(
123 makeThread({
124 session: {
125 threadId: ThreadId.make("thread-1"),
126 status: "running",
127 providerName: "codex",
128 runtimeMode: "full-access",
129 activeTurnId: TurnId.make("turn-1"),
130 lastError: null,
131 updatedAt: NOW,
132 },
133 }),
134 ),
135 ).toBe(false);
136 expect(
137 decide(makeThread({ latestUserMessageAt: "2026-08-28T11:59:00.000Z", latestTurn: null })),
138 ).toBe(false);
139 });
140​
141 it("allows a fresh completion to wake snooze before settlement", () => {
142 expect(
143 decide(
144 makeThread({
145 snoozedAt: "2026-08-19T00:00:00.000Z",
146 snoozedUntil: "2026-08-29T00:00:00.000Z",
147 latestTurn: {
148 turnId: TurnId.make("turn-woke"),
149 state: "completed",
150 requestedAt: "2026-08-18T00:00:00.000Z",
151 startedAt: "2026-08-18T00:01:00.000Z",
152 completedAt: "2026-08-20T00:00:00.000Z",
153 assistantMessageId: null,
154 },
155 }),
156 ),
157 ).toBe(true);
158 });
159});