Open History to see the last commit
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 };
64}
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 };
85}
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 };
108}
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("treats usage-limited and other provider-finished runs as forkable", () => {
134 assert.isTrue(ThreadForkService.isForkableSourceRunStatus("completed"));
135 assert.isTrue(ThreadForkService.isForkableSourceRunStatus("waiting"));
136 assert.isTrue(ThreadForkService.isForkableSourceRunStatus("failed"));
137 assert.isTrue(ThreadForkService.isForkableSourceRunStatus("interrupted"));
138 assert.isTrue(ThreadForkService.isForkableSourceRunStatus("cancelled"));
139 assert.isFalse(ThreadForkService.isForkableSourceRunStatus("running"));
140 assert.isFalse(ThreadForkService.isForkableSourceRunStatus("starting"));
141 assert.isFalse(ThreadForkService.isForkableSourceRunStatus("queued"));
142 assert.isFalse(ThreadForkService.isForkableSourceRunStatus("preparing"));
143 assert.isFalse(ThreadForkService.isForkableSourceRunStatus("rolled_back"));
144});
145
146it.effect("keeps a fork awake when its source thread is snoozed", () =>
147 Effect.gen(function* () {
148 const sourceThread = makeSourceThread();
149 const sourceRun = makeSourceRun("completed");
150 const result = yield* planFork(sourceRun);
151
152 assert.isNull(result.targetThread.snoozedUntil);
153 assert.isNull(result.targetThread.snoozedAt);
154 assert.equal(result.targetThread.projectId, sourceThread.projectId);
155 assert.equal(result.targetThread.providerInstanceId, sourceThread.providerInstanceId);
156 assert.deepEqual(result.targetThread.modelSelection, sourceThread.modelSelection);
157 assert.equal(result.targetThread.runtimeMode, sourceThread.runtimeMode);
158 assert.equal(result.targetThread.interactionMode, sourceThread.interactionMode);
159 assert.equal(result.targetThread.branch, sourceThread.branch);
160 assert.equal(result.targetThread.worktreePath, sourceThread.worktreePath);
161 assert.isNull(result.targetThread.activeProviderThreadId);
162 assert.deepEqual(result.targetThread.lineage, {
163 parentThreadId: sourceThreadId,
164 relationshipToParent: "fork",
165 rootThreadId: sourceThreadId,
166 });
167 assert.deepEqual(result.targetThread.forkedFrom, {
168 type: "run",
169 threadId: sourceThreadId,
170 runId: sourceRunId,
171 });
172 }),
173);
174
175it.effect("forks from a usage-limited failed run", () =>
176 Effect.gen(function* () {
177 const result = yield* planFork(makeSourceRun("failed"));
178 assert.deepEqual(result.targetThread.forkedFrom, {
179 type: "run",
180 threadId: sourceThreadId,
181 runId: sourceRunId,
182 });
183 }),
184);
185
186it.effect("preserves pull request context without inheriting any watches", () =>
187 Effect.gen(function* () {
188 const sourceRun = makeSourceRun("completed");
189 const watch = {
190 startedAt: DateTime.formatIso(sourceCreatedAt),
191 headSha: "source-head",
192 failedChecks: ["lint"],
193 passed: false,
194 passedChecks: [],
195 remarksThrough: DateTime.formatIso(snoozedAt),
196 remarkIds: ["review-1"],
197 conflicting: true,
198 wakes: 2,
199 };
200 const links = (["agent", "stack", "manual", "stack-dismissed"] as const).map(
201 (source, index) =>
202 ({
203 host: "github.com",
204 repository: "example/repo",
205 number: index + 1,
206 url: `https://github.com/example/repo/pull/${index + 1}`,
207 source,
208 linkedAt: DateTime.formatIso(sourceCreatedAt),
209 snapshot: {
210 state: "open",
211 title: `Pull request ${index + 1}`,
212 headBranch: `feature-${index + 1}`,
213 baseBranch: "main",
214 isDraft: false,
215 updatedAt: null,
216 syncedAt: DateTime.formatIso(snoozedAt),
217 },
218 stack: {
219 kind: "native",
220 id: "stack-1",
221 number: 1,
222 url: "https://github.com/example/repo/stack/1",
223 base: "main",
224 layers: [{ number: 1, headBranch: "feature-1", state: "open" }],
225 },
226 }) satisfies ThreadPullRequestLink,
227 );
228 const watchedLinks: ReadonlyArray<ThreadPullRequestLink> = links.map((link, index) =>
229 index < 2 ? { ...link, watch } : link,
230 );
231 const legacyLink = {
232 projectId: makeSourceThread().projectId,
233 repository: "example/repo",
234 number: 1,
235 url: links[0]!.url,
236 };
237 const sourceProjection = {
238 ...makeSourceProjection(sourceRun),
239 thread: {
240 ...makeSourceThread(),
241 pullRequests: watchedLinks,
242 linkedPullRequest: legacyLink,
243 branchPullRequest: legacyLink,
244 },
245 };
246 const result = yield* planFork(sourceRun, sourceProjection);
247
248 assert.deepEqual(result.targetThread.pullRequests, links);
249 assert.deepEqual(result.targetThread.linkedPullRequest, legacyLink);
250 assert.deepEqual(result.targetThread.branchPullRequest, legacyLink);
251 assert.deepEqual(sourceProjection.thread.pullRequests, watchedLinks);
252 assert.deepEqual(sourceProjection.thread.pullRequests[0]?.watch, watch);
253 assert.deepEqual(sourceProjection.thread.pullRequests[1]?.watch, watch);
254 }),
255);
256
257it.effect("rejects in-progress and rolled-back fork sources", () =>
258 Effect.gen(function* () {
259 for (const status of ["running", "rolled_back"] as const) {
260 const sourceRun = makeSourceRun(status);
261 const error = yield* planFork(sourceRun).pipe(Effect.flip);
262 assert.equal(error._tag, "ThreadForkPlanError");
263 assert.equal(error.sourceThreadId, sourceThreadId);
264 assert.equal(error.targetThreadId, targetThreadId);
265 assert.equal(error.cause, ThreadForkService.forkableSourceRunStatusError(sourceRun));
266 }
267 }),
268);