Open History to see the last commit
1import { assert, it } from "@effect/vitest";
2import {
3 CommandId,
4 ContextTransferId,
5 EnvironmentId,
6 MessageId,
7 type ModelSelection,
8 type OrchestrationV2AppThread,
9 type OrchestrationV2Run,
10 type OrchestrationV2ThreadProjection,
11 ProjectId,
12 ProviderInstanceId,
13 ProviderThreadId,
14 RunId,
15 ThreadId,
16 type ThreadPullRequestLink,
17} from "@t3tools/contracts";
18import * as DateTime from "effect/DateTime";
19import * as Effect from "effect/Effect";
20
21import * as ThreadForkService from "./ThreadForkService.ts";
22
23const sourceThreadId = ThreadId.make("thread:fork-snoozed-source");
24const targetThreadId = ThreadId.make("thread:fork-awake-target");
25const sourceRunId = RunId.make("run:fork-snoozed-source");
26const providerInstanceId = ProviderInstanceId.make("codex");
27const modelSelection = {
28 instanceId: providerInstanceId,
29 model: "gpt-5.4",
30} satisfies ModelSelection;
31const sourceCreatedAt = DateTime.makeUnsafe("2026-07-24T09:00:00.000Z");
32const snoozedAt = DateTime.makeUnsafe("2026-07-24T09:05:00.000Z");
33const snoozedUntil = DateTime.makeUnsafe("2026-07-25T09:00:00.000Z");
34const forkCreatedAt = DateTime.makeUnsafe("2026-07-24T09:10:00.000Z");
35
36function makeSourceThread(): OrchestrationV2AppThread {
37 return {
38 createdBy: "user",
39 creationSource: "web",
40 id: sourceThreadId,
41 projectId: ProjectId.make("project:fork-snooze"),
42 title: "Snoozed source",
43 providerInstanceId,
44 modelSelection,
45 runtimeMode: "full-access",
46 interactionMode: "plan",
47 branch: "feature/source",
48 worktreePath: "/tmp/source-worktree",
49 activeProviderThreadId: ProviderThreadId.make("provider-thread:fork-snoozed-source"),
50 lineage: {
51 parentThreadId: null,
52 relationshipToParent: null,
53 rootThreadId: sourceThreadId,
54 },
55 forkedFrom: null,
56 createdAt: sourceCreatedAt,
57 updatedAt: snoozedAt,
58 archivedAt: null,
59 settledOverride: null,
60 settledAt: null,
61 lastVisitedAt: null,
62 snoozedUntil,
63 snoozedAt,
64 deletedAt: null,
65 };
66}
67
68function makeSourceRun(status: OrchestrationV2Run["status"]): OrchestrationV2Run {
69 return {
70 id: sourceRunId,
71 threadId: sourceThreadId,
72 ordinal: 1,
73 providerInstanceId,
74 modelSelection,
75 providerThreadId: ProviderThreadId.make("provider-thread:fork-snoozed-source"),
76 userMessageId: MessageId.make("message:fork-snoozed-source"),
77 rootNodeId: null,
78 activeAttemptId: null,
79 status,
80 queuePosition: null,
81 requestedAt: sourceCreatedAt,
82 startedAt: sourceCreatedAt,
83 completedAt: snoozedAt,
84 checkpointId: null,
85 contextHandoffId: null,
86 };
87}
88
89function makeSourceProjection(sourceRun: OrchestrationV2Run): OrchestrationV2ThreadProjection {
90 return {
91 thread: makeSourceThread(),
92 runs: [sourceRun],
93 attempts: [],
94 nodes: [],
95 subagents: [],
96 providerSessions: [],
97 providerThreads: [],
98 providerTurns: [],
99 runtimeRequests: [],
100 messages: [],
101 plans: [],
102 turnItems: [],
103 checkpointScopes: [],
104 checkpoints: [],
105 contextHandoffs: [],
106 contextTransfers: [],
107 visibleTurnItems: [],
108 updatedAt: snoozedAt,
109 };
110}
111
112const planFork = (
113 sourceRun: OrchestrationV2Run,
114 sourceProjection = makeSourceProjection(sourceRun),
115 options: { readonly sideChat?: boolean } = {},
116) =>
117 Effect.gen(function* () {
118 const service = yield* ThreadForkService.ThreadForkServiceV2;
119 return yield* service.plan({
120 sourceProjection,
121 sourceRun,
122 sourceProviderThread: undefined,
123 canonicalSourcePoint: {
124 threadId: sourceThreadId,
125 runId: sourceRunId,
126 },
127 transferId: ContextTransferId.make("context-transfer:fork-snoozed-source"),
128 targetThreadId,
129 title: "Awake fork",
130 ...options,
131 createdBy: "user",
132 creationSource: "mobile",
133 createdAt: forkCreatedAt,
134 });
135 }).pipe(Effect.provide(ThreadForkService.layer));
136
137it.effect("keeps a fork awake when its source thread is snoozed", () =>
138 Effect.gen(function* () {
139 const sourceThread = makeSourceThread();
140 const sourceRun = makeSourceRun("completed");
141 const result = yield* planFork(sourceRun);
142
143 assert.isNull(result.targetThread.snoozedUntil);
144 assert.isNull(result.targetThread.snoozedAt);
145 assert.equal(result.targetThread.projectId, sourceThread.projectId);
146 assert.equal(result.targetThread.providerInstanceId, sourceThread.providerInstanceId);
147 assert.deepEqual(result.targetThread.modelSelection, sourceThread.modelSelection);
148 assert.equal(result.targetThread.runtimeMode, sourceThread.runtimeMode);
149 assert.equal(result.targetThread.interactionMode, sourceThread.interactionMode);
150 assert.equal(result.targetThread.branch, sourceThread.branch);
151 assert.equal(result.targetThread.worktreePath, sourceThread.worktreePath);
152 assert.isNull(result.targetThread.activeProviderThreadId);
153 assert.deepEqual(result.targetThread.lineage, {
154 parentThreadId: sourceThreadId,
155 relationshipToParent: "fork",
156 rootThreadId: sourceThreadId,
157 });
158 assert.deepEqual(result.targetThread.forkedFrom, {
159 type: "run",
160 threadId: sourceThreadId,
161 runId: sourceRunId,
162 });
163 }),
164);
165
166it.effect("starts a fork without the source thread's pending title regeneration", () =>
167 Effect.gen(function* () {
168 const sourceRun = makeSourceRun("completed");
169 const titleRegeneration = {
170 requestId: CommandId.make("command:fork-source-title"),
171 startedAt: snoozedAt,
172 };
173 const sourceProjection = {
174 ...makeSourceProjection(sourceRun),
175 thread: { ...makeSourceThread(), titleRegeneration },
176 };
177 const result = yield* planFork(sourceRun, sourceProjection);
178
179 assert.isNull(result.targetThread.titleRegeneration);
180 assert.deepEqual(sourceProjection.thread.titleRegeneration, titleRegeneration);
181 }),
182);
183
184it.effect("preserves pull request context without inheriting any watches", () =>
185 Effect.gen(function* () {
186 const sourceRun = makeSourceRun("completed");
187 const watch = {
188 startedAt: DateTime.formatIso(sourceCreatedAt),
189 headSha: "source-head",
190 failedChecks: ["lint"],
191 passed: false,
192 passedChecks: [],
193 remarksThrough: DateTime.formatIso(snoozedAt),
194 remarkIds: ["review-1"],
195 conflicting: true,
196 wakes: 2,
197 };
198 const links = (["agent", "stack", "manual", "stack-dismissed"] as const).map(
199 (source, index) =>
200 ({
201 host: "github.com",
202 repository: "example/repo",
203 number: index + 1,
204 url: `https://github.com/example/repo/pull/${index + 1}`,
205 source,
206 linkedAt: DateTime.formatIso(sourceCreatedAt),
207 snapshot: {
208 state: "open",
209 title: `Pull request ${index + 1}`,
210 headBranch: `feature-${index + 1}`,
211 baseBranch: "main",
212 isDraft: false,
213 updatedAt: null,
214 syncedAt: DateTime.formatIso(snoozedAt),
215 },
216 stack: {
217 kind: "native",
218 id: "stack-1",
219 number: 1,
220 url: "https://github.com/example/repo/stack/1",
221 base: "main",
222 layers: [{ number: 1, headBranch: "feature-1", state: "open" }],
223 },
224 }) satisfies ThreadPullRequestLink,
225 );
226 const watchedLinks: ReadonlyArray<ThreadPullRequestLink> = links.map((link, index) =>
227 index < 2 ? { ...link, watch } : link,
228 );
229 const legacyLink = {
230 projectId: makeSourceThread().projectId,
231 repository: "example/repo",
232 number: 1,
233 url: links[0]!.url,
234 };
235 const sourceProjection = {
236 ...makeSourceProjection(sourceRun),
237 thread: {
238 ...makeSourceThread(),
239 pullRequests: watchedLinks,
240 linkedPullRequest: legacyLink,
241 branchPullRequest: legacyLink,
242 },
243 };
244 const result = yield* planFork(sourceRun, sourceProjection);
245
246 assert.deepEqual(result.targetThread.pullRequests, links);
247 assert.deepEqual(result.targetThread.linkedPullRequest, legacyLink);
248 assert.deepEqual(result.targetThread.branchPullRequest, legacyLink);
249 assert.deepEqual(sourceProjection.thread.pullRequests, watchedLinks);
250 assert.deepEqual(sourceProjection.thread.pullRequests[0]?.watch, watch);
251 assert.deepEqual(sourceProjection.thread.pullRequests[1]?.watch, watch);
252 }),
253);
254
255it.effect("rejects in-progress and rolled-back fork sources", () =>
256 Effect.gen(function* () {
257 for (const status of ["running", "rolled_back"] as const) {
258 const sourceRun = makeSourceRun(status);
259 const error = yield* planFork(sourceRun).pipe(Effect.flip);
260 assert.equal(error._tag, "ThreadForkPlanError");
261 assert.equal(error.sourceThreadId, sourceThreadId);
262 assert.equal(error.targetThreadId, targetThreadId);
263 assert.equal(error.cause, ThreadForkService.forkableSourceRunStatusError(sourceRun));
264 }
265 }),
266);
267
268it.effect("marks only requested forks as supervised side chats", () =>
269 Effect.gen(function* () {
270 const sourceRun = makeSourceRun("completed");
271 const sideChat = yield* planFork(sourceRun, makeSourceProjection(sourceRun), {
272 sideChat: true,
273 });
274 assert.isTrue(sideChat.targetThread.lineage.sideChat);
275 assert.equal(sideChat.targetThread.runtimeMode, "approval-required");
276
277 const sideChatSource = makeSourceProjection(sourceRun);
278 const sideChatProjection = {
279 ...sideChatSource,
280 thread: {
281 ...sideChatSource.thread,
282 lineage: { ...sideChatSource.thread.lineage, sideChat: true },
283 },
284 };
285 const forkOfSideChat = yield* planFork(sourceRun, sideChatProjection);
286 assert.isUndefined(forkOfSideChat.targetThread.lineage.sideChat);
287 assert.equal(forkOfSideChat.targetThread.runtimeMode, "full-access");
288
289 const nested = yield* planFork(sourceRun, sideChatProjection, { sideChat: true }).pipe(
290 Effect.flip,
291 );
292 assert.equal(nested.cause, "A side chat cannot start another side chat.");
293
294 const subagentSource = makeSourceProjection(sourceRun);
295 const fromSubagent = yield* planFork(
296 sourceRun,
297 {
298 ...subagentSource,
299 thread: {
300 ...subagentSource.thread,
301 lineage: { ...subagentSource.thread.lineage, relationshipToParent: "subagent" },
302 },
303 },
304 { sideChat: true },
305 ).pipe(Effect.flip);
306 assert.equal(fromSubagent.cause, "A subagent cannot start a side chat.");
307
308 const delegatedSource = makeSourceProjection(sourceRun);
309 const fromDelegated = yield* planFork(
310 sourceRun,
311 {
312 ...delegatedSource,
313 thread: {
314 ...delegatedSource.thread,
315 delegatedFrom: {
316 environmentId: EnvironmentId.make("linked-environment"),
317 threadId: ThreadId.make("linked-parent"),
318 title: "Linked parent",
319 },
320 },
321 },
322 { sideChat: true },
323 ).pipe(Effect.flip);
324 assert.equal(fromDelegated.cause, "A subagent cannot start a side chat.");
325 }),
326);