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

ThreadForkService.test.ts

244 lines8.0 KB

1import { assert, it } from "@effect/vitest";
2import {
3 ContextTransferId,
4 MessageId,
5 type ModelSelection,
6 type OrchestrationV2AppThread,
7 type OrchestrationV2Run,
8 type OrchestrationV2ThreadProjection,
9 ProjectId,
10 ProviderInstanceId,
11 ProviderThreadId,
12 RunId,
13 ThreadId,
14 type ThreadPullRequestLink,
15} from "@t3tools/contracts";
16import * as DateTime from "effect/DateTime";
17import * as Effect from "effect/Effect";
18​
19import * as ThreadForkService from "./ThreadForkService.ts";
20​
21const sourceThreadId = ThreadId.make("thread:fork-snoozed-source");
22const targetThreadId = ThreadId.make("thread:fork-awake-target");
23const sourceRunId = RunId.make("run:fork-snoozed-source");
24const providerInstanceId = ProviderInstanceId.make("codex");
25const modelSelection = {
26 instanceId: providerInstanceId,
27 model: "gpt-5.4",
28} satisfies ModelSelection;
29const sourceCreatedAt = DateTime.makeUnsafe("2026-07-24T09:00:00.000Z");
30const snoozedAt = DateTime.makeUnsafe("2026-07-24T09:05:00.000Z");
31const snoozedUntil = DateTime.makeUnsafe("2026-07-25T09:00:00.000Z");
32const forkCreatedAt = DateTime.makeUnsafe("2026-07-24T09:10:00.000Z");
33​
34function makeSourceThread(): OrchestrationV2AppThread {
35 return {
36 createdBy: "user",
37 creationSource: "web",
38 id: sourceThreadId,
39 projectId: ProjectId.make("project:fork-snooze"),
40 title: "Snoozed source",
41 providerInstanceId,
42 modelSelection,
43 runtimeMode: "full-access",
44 interactionMode: "plan",
45 branch: "feature/source",
46 worktreePath: "/tmp/source-worktree",
47 activeProviderThreadId: ProviderThreadId.make("provider-thread:fork-snoozed-source"),
48 lineage: {
49 parentThreadId: null,
50 relationshipToParent: null,
51 rootThreadId: sourceThreadId,
52 },
53 forkedFrom: null,
54 createdAt: sourceCreatedAt,
55 updatedAt: snoozedAt,
56 archivedAt: null,
57 settledOverride: null,
58 settledAt: null,
59 lastVisitedAt: null,
60 snoozedUntil,
61 snoozedAt,
62 deletedAt: null,
63 };
65​
66function makeSourceRun(status: OrchestrationV2Run["status"]): OrchestrationV2Run {
67 return {
68 id: sourceRunId,
69 threadId: sourceThreadId,
70 ordinal: 1,
71 providerInstanceId,
72 modelSelection,
73 providerThreadId: ProviderThreadId.make("provider-thread:fork-snoozed-source"),
74 userMessageId: MessageId.make("message:fork-snoozed-source"),
75 rootNodeId: null,
76 activeAttemptId: null,
77 status,
78 queuePosition: null,
79 requestedAt: sourceCreatedAt,
80 startedAt: sourceCreatedAt,
81 completedAt: snoozedAt,
82 checkpointId: null,
83 contextHandoffId: null,
84 };
86​
87function makeSourceProjection(sourceRun: OrchestrationV2Run): OrchestrationV2ThreadProjection {
88 return {
89 thread: makeSourceThread(),
90 runs: [sourceRun],
91 attempts: [],
92 nodes: [],
93 subagents: [],
94 providerSessions: [],
95 providerThreads: [],
96 providerTurns: [],
97 runtimeRequests: [],
98 messages: [],
99 plans: [],
100 turnItems: [],
101 checkpointScopes: [],
102 checkpoints: [],
103 contextHandoffs: [],
104 contextTransfers: [],
105 visibleTurnItems: [],
106 updatedAt: snoozedAt,
107 };
109​
110const planFork = (
111 sourceRun: OrchestrationV2Run,
112 sourceProjection = makeSourceProjection(sourceRun),
113) =>
114 Effect.gen(function* () {
115 const service = yield* ThreadForkService.ThreadForkServiceV2;
116 return yield* service.plan({
117 sourceProjection,
118 sourceRun,
119 sourceProviderThread: undefined,
120 canonicalSourcePoint: {
121 threadId: sourceThreadId,
122 runId: sourceRunId,
123 },
124 transferId: ContextTransferId.make("context-transfer:fork-snoozed-source"),
125 targetThreadId,
126 title: "Awake fork",
127 createdBy: "user",
128 creationSource: "mobile",
129 createdAt: forkCreatedAt,
130 });
131 }).pipe(Effect.provide(ThreadForkService.layer));
132​
133it.effect("keeps a fork awake when its source thread is snoozed", () =>
134 Effect.gen(function* () {
135 const sourceThread = makeSourceThread();
136 const sourceRun = makeSourceRun("completed");
137 const result = yield* planFork(sourceRun);
138​
139 assert.isNull(result.targetThread.snoozedUntil);
140 assert.isNull(result.targetThread.snoozedAt);
141 assert.equal(result.targetThread.projectId, sourceThread.projectId);
142 assert.equal(result.targetThread.providerInstanceId, sourceThread.providerInstanceId);
143 assert.deepEqual(result.targetThread.modelSelection, sourceThread.modelSelection);
144 assert.equal(result.targetThread.runtimeMode, sourceThread.runtimeMode);
145 assert.equal(result.targetThread.interactionMode, sourceThread.interactionMode);
146 assert.equal(result.targetThread.branch, sourceThread.branch);
147 assert.equal(result.targetThread.worktreePath, sourceThread.worktreePath);
148 assert.isNull(result.targetThread.activeProviderThreadId);
149 assert.deepEqual(result.targetThread.lineage, {
150 parentThreadId: sourceThreadId,
151 relationshipToParent: "fork",
152 rootThreadId: sourceThreadId,
153 });
154 assert.deepEqual(result.targetThread.forkedFrom, {
155 type: "run",
156 threadId: sourceThreadId,
157 runId: sourceRunId,
158 });
159 }),
160);
161​
162it.effect("preserves pull request context without inheriting any watches", () =>
163 Effect.gen(function* () {
164 const sourceRun = makeSourceRun("completed");
165 const watch = {
166 startedAt: DateTime.formatIso(sourceCreatedAt),
167 headSha: "source-head",
168 failedChecks: ["lint"],
169 passed: false,
170 passedChecks: [],
171 remarksThrough: DateTime.formatIso(snoozedAt),
172 remarkIds: ["review-1"],
173 conflicting: true,
174 wakes: 2,
175 };
176 const links = (["agent", "stack", "manual", "stack-dismissed"] as const).map(
177 (source, index) =>
178 ({
179 host: "github.com",
180 repository: "example/repo",
181 number: index + 1,
182 url: `https://github.com/example/repo/pull/${index + 1}`,
183 source,
184 linkedAt: DateTime.formatIso(sourceCreatedAt),
185 snapshot: {
186 state: "open",
187 title: `Pull request ${index + 1}`,
188 headBranch: `feature-${index + 1}`,
189 baseBranch: "main",
190 isDraft: false,
191 updatedAt: null,
192 syncedAt: DateTime.formatIso(snoozedAt),
193 },
194 stack: {
195 kind: "native",
196 id: "stack-1",
197 number: 1,
198 url: "https://github.com/example/repo/stack/1",
199 base: "main",
200 layers: [{ number: 1, headBranch: "feature-1", state: "open" }],
201 },
202 }) satisfies ThreadPullRequestLink,
203 );
204 const watchedLinks: ReadonlyArray<ThreadPullRequestLink> = links.map((link, index) =>
205 index < 2 ? { ...link, watch } : link,
206 );
207 const legacyLink = {
208 projectId: makeSourceThread().projectId,
209 repository: "example/repo",
210 number: 1,
211 url: links[0]!.url,
212 };
213 const sourceProjection = {
214 ...makeSourceProjection(sourceRun),
215 thread: {
216 ...makeSourceThread(),
217 pullRequests: watchedLinks,
218 linkedPullRequest: legacyLink,
219 branchPullRequest: legacyLink,
220 },
221 };
222 const result = yield* planFork(sourceRun, sourceProjection);
223​
224 assert.deepEqual(result.targetThread.pullRequests, links);
225 assert.deepEqual(result.targetThread.linkedPullRequest, legacyLink);
226 assert.deepEqual(result.targetThread.branchPullRequest, legacyLink);
227 assert.deepEqual(sourceProjection.thread.pullRequests, watchedLinks);
228 assert.deepEqual(sourceProjection.thread.pullRequests[0]?.watch, watch);
229 assert.deepEqual(sourceProjection.thread.pullRequests[1]?.watch, watch);
230 }),
231);
232​
233it.effect("rejects in-progress and rolled-back fork sources", () =>
234 Effect.gen(function* () {
235 for (const status of ["running", "rolled_back"] as const) {
236 const sourceRun = makeSourceRun(status);
237 const error = yield* planFork(sourceRun).pipe(Effect.flip);
238 assert.equal(error._tag, "ThreadForkPlanError");
239 assert.equal(error.sourceThreadId, sourceThreadId);
240 assert.equal(error.targetThreadId, targetThreadId);
241 assert.equal(error.cause, ThreadForkService.forkableSourceRunStatusError(sourceRun));
242 }
243 }),
244);