Open History to see the last commit
1import { CheckpointRef, ProjectId, ThreadId, TurnId } from "@t3tools/contracts";
2import { it } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import * as Layer from "effect/Layer";
5import * as Option from "effect/Option";
6import { describe, expect } from "vite-plus/test";
7
8import * as ProjectionSnapshotQuery from "../orchestration/Services/ProjectionSnapshotQuery.ts";
9import { checkpointRefForThreadTurn } from "./Utils.ts";
10import * as CheckpointDiffQuery from "./CheckpointDiffQuery.ts";
11import * as CheckpointStore from "./CheckpointStore.ts";
12import { CheckpointThreadNotFoundError } from "./Errors.ts";
13
14function makeThreadCheckpointContext(input: {
15 readonly projectId: ProjectId;
16 readonly threadId: ThreadId;
17 readonly workspaceRoot: string;
18 readonly worktreePath: string | null;
19 readonly checkpointTurnCount: number;
20 readonly checkpointRef: CheckpointRef;
21}): ProjectionSnapshotQuery.ProjectionThreadCheckpointContext {
22 return {
23 threadId: input.threadId,
24 projectId: input.projectId,
25 workspaceRoot: input.workspaceRoot,
26 worktreePath: input.worktreePath,
27 checkpoints: [
28 {
29 turnId: TurnId.make("turn-1"),
30 checkpointTurnCount: input.checkpointTurnCount,
31 checkpointRef: input.checkpointRef,
32 status: "ready",
33 files: [],
34 assistantMessageId: null,
35 completedAt: "2026-01-01T00:00:00.000Z",
36 },
37 ],
38 };
39}
40
41describe("CheckpointDiffQuery.layer", () => {
42 it.effect("uses the narrow full-thread context lookup for all-turns diffs", () =>
43 Effect.gen(function* () {
44 const projectId = ProjectId.make("project-full-thread");
45 const threadId = ThreadId.make("thread-full-thread");
46 const toCheckpointRef = checkpointRefForThreadTurn(threadId, 4);
47 let getThreadCheckpointContextCalls = 0;
48 let getFullThreadDiffContextCalls = 0;
49 const diffCheckpointsCalls: Array<{
50 readonly fromCheckpointRef: CheckpointRef;
51 readonly toCheckpointRef: CheckpointRef;
52 readonly cwd: string;
53 readonly ignoreWhitespace: boolean;
54 }> = [];
55
56 const checkpointStore: CheckpointStore.CheckpointStore["Service"] = {
57 isGitRepository: () => Effect.succeed(true),
58 captureCheckpoint: () => Effect.void,
59 hasCheckpointRef: () => Effect.succeed(true),
60 restoreCheckpoint: () => Effect.succeed(true),
61 diffCheckpoints: ({ fromCheckpointRef, toCheckpointRef, cwd, ignoreWhitespace }) =>
62 Effect.sync(() => {
63 diffCheckpointsCalls.push({
64 fromCheckpointRef,
65 toCheckpointRef,
66 cwd,
67 ignoreWhitespace,
68 });
69 return "full thread diff patch";
70 }),
71 deleteCheckpointRefs: () => Effect.void,
72 };
73
74 const layer = CheckpointDiffQuery.layer.pipe(
75 Layer.provideMerge(Layer.succeed(CheckpointStore.CheckpointStore, checkpointStore)),
76 Layer.provideMerge(
77 Layer.succeed(ProjectionSnapshotQuery.ProjectionSnapshotQuery, {
78 getUserInputActivity: () => Effect.die("unused"),
79 listActivitiesByKind: () => Effect.die("unused"),
80 getCommandReadModel: () =>
81 Effect.die("CheckpointDiffQuery should not request the command read model"),
82 getSnapshot: () =>
83 Effect.die("CheckpointDiffQuery should not request the full orchestration snapshot"),
84 getShellSnapshot: () =>
85 Effect.die("CheckpointDiffQuery should not request the orchestration shell snapshot"),
86 getDeletedWorktreeThreads: () => Effect.die("unused"),
87 listThreadsWithPullRequests: () => Effect.die("unused"),
88 getArchivedShellSnapshot: () =>
89 Effect.die("CheckpointDiffQuery should not request archived shell snapshots"),
90 getSnapshotSequence: () => Effect.succeed({ snapshotSequence: 0 }),
91 getCounts: () => Effect.succeed({ projectCount: 0, threadCount: 0 }),
92 getEventReplayStats: () => Effect.die("unused"),
93 getActiveProjectByWorkspaceRoot: () => Effect.succeedNone,
94 hasActiveProjectAtWorkspaceRoot: () => Effect.succeed(false),
95 getProjectShells: () => Effect.die("unused"),
96 getProjectShellById: () => Effect.succeedNone,
97 getFirstActiveThreadIdByProjectId: () => Effect.succeedNone,
98 getImportedAgentSessionSources: () => Effect.die("unused"),
99 getThreadCheckpointContext: () =>
100 Effect.sync(() => {
101 getThreadCheckpointContextCalls += 1;
102 return Option.none();
103 }),
104 getFullThreadDiffContext: () =>
105 Effect.sync(() => {
106 getFullThreadDiffContextCalls += 1;
107 return Option.some({
108 threadId,
109 projectId,
110 workspaceRoot: "/tmp/workspace",
111 worktreePath: "/tmp/worktree",
112 latestCheckpointTurnCount: 4,
113 toCheckpointRef,
114 });
115 }),
116 getThreadRuntimeContext: () => Effect.die("unused"),
117 getTurnStartMessage: () => Effect.die("unused"),
118 getThreadShellById: () => Effect.succeedNone,
119 getThreadDetailById: () => Effect.succeedNone,
120 getThreadDetailSnapshot: () => Effect.succeedNone,
121 searchThreads: () => Effect.succeed({ matches: [] }),
122 }),
123 ),
124 );
125
126 const result = yield* Effect.gen(function* () {
127 const query = yield* CheckpointDiffQuery.CheckpointDiffQuery;
128 return yield* query.getFullThreadDiff({
129 threadId,
130 toTurnCount: 4,
131 ignoreWhitespace: true,
132 });
133 }).pipe(Effect.provide(layer));
134
135 expect(getThreadCheckpointContextCalls).toBe(0);
136 expect(getFullThreadDiffContextCalls).toBe(1);
137 expect(diffCheckpointsCalls).toEqual([
138 {
139 cwd: "/tmp/worktree",
140 fromCheckpointRef: checkpointRefForThreadTurn(threadId, 0),
141 toCheckpointRef,
142 ignoreWhitespace: true,
143 },
144 ]);
145 expect(result).toEqual({
146 threadId,
147 fromTurnCount: 0,
148 toTurnCount: 4,
149 diff: "full thread diff patch",
150 });
151 }),
152 );
153
154 it.effect("computes diffs using canonical turn-0 checkpoint refs", () =>
155 Effect.gen(function* () {
156 const projectId = ProjectId.make("project-1");
157 const threadId = ThreadId.make("thread-1");
158 const toCheckpointRef = checkpointRefForThreadTurn(threadId, 1);
159 const diffCheckpointsCalls: Array<{
160 readonly fromCheckpointRef: CheckpointRef;
161 readonly toCheckpointRef: CheckpointRef;
162 readonly cwd: string;
163 readonly ignoreWhitespace: boolean;
164 }> = [];
165
166 const threadCheckpointContext = makeThreadCheckpointContext({
167 projectId,
168 threadId,
169 workspaceRoot: "/tmp/workspace",
170 worktreePath: null,
171 checkpointTurnCount: 1,
172 checkpointRef: toCheckpointRef,
173 });
174
175 const checkpointStore: CheckpointStore.CheckpointStore["Service"] = {
176 isGitRepository: () => Effect.succeed(true),
177 captureCheckpoint: () => Effect.void,
178 hasCheckpointRef: () => Effect.succeed(true),
179 restoreCheckpoint: () => Effect.succeed(true),
180 diffCheckpoints: ({ fromCheckpointRef, toCheckpointRef, cwd, ignoreWhitespace }) =>
181 Effect.sync(() => {
182 diffCheckpointsCalls.push({
183 fromCheckpointRef,
184 toCheckpointRef,
185 cwd,
186 ignoreWhitespace,
187 });
188 return "diff patch";
189 }),
190 deleteCheckpointRefs: () => Effect.void,
191 };
192
193 const layer = CheckpointDiffQuery.layer.pipe(
194 Layer.provideMerge(Layer.succeed(CheckpointStore.CheckpointStore, checkpointStore)),
195 Layer.provideMerge(
196 Layer.succeed(ProjectionSnapshotQuery.ProjectionSnapshotQuery, {
197 getUserInputActivity: () => Effect.die("unused"),
198 listActivitiesByKind: () => Effect.die("unused"),
199 getCommandReadModel: () =>
200 Effect.die("CheckpointDiffQuery should not request the command read model"),
201 getSnapshot: () =>
202 Effect.die("CheckpointDiffQuery should not request the full orchestration snapshot"),
203 getShellSnapshot: () =>
204 Effect.die("CheckpointDiffQuery should not request the orchestration shell snapshot"),
205 getDeletedWorktreeThreads: () => Effect.die("unused"),
206 listThreadsWithPullRequests: () => Effect.die("unused"),
207 getArchivedShellSnapshot: () =>
208 Effect.die("CheckpointDiffQuery should not request archived shell snapshots"),
209 getSnapshotSequence: () => Effect.succeed({ snapshotSequence: 0 }),
210 getCounts: () => Effect.succeed({ projectCount: 0, threadCount: 0 }),
211 getEventReplayStats: () => Effect.die("unused"),
212 getActiveProjectByWorkspaceRoot: () => Effect.succeedNone,
213 hasActiveProjectAtWorkspaceRoot: () => Effect.succeed(false),
214 getProjectShells: () => Effect.die("unused"),
215 getProjectShellById: () => Effect.succeedNone,
216 getFirstActiveThreadIdByProjectId: () => Effect.succeedNone,
217 getImportedAgentSessionSources: () => Effect.die("unused"),
218 getThreadCheckpointContext: () => Effect.succeedSome(threadCheckpointContext),
219 getFullThreadDiffContext: () => Effect.die("unused"),
220 getThreadRuntimeContext: () => Effect.die("unused"),
221 getTurnStartMessage: () => Effect.die("unused"),
222 getThreadShellById: () => Effect.succeedNone,
223 getThreadDetailById: () => Effect.succeedNone,
224 getThreadDetailSnapshot: () => Effect.succeedNone,
225 searchThreads: () => Effect.succeed({ matches: [] }),
226 }),
227 ),
228 );
229
230 const result = yield* Effect.gen(function* () {
231 const query = yield* CheckpointDiffQuery.CheckpointDiffQuery;
232 return yield* query.getTurnDiff({
233 threadId,
234 fromTurnCount: 0,
235 toTurnCount: 1,
236 ignoreWhitespace: true,
237 });
238 }).pipe(Effect.provide(layer));
239
240 const expectedFromRef = checkpointRefForThreadTurn(threadId, 0);
241 expect(diffCheckpointsCalls).toEqual([
242 {
243 cwd: "/tmp/workspace",
244 fromCheckpointRef: expectedFromRef,
245 toCheckpointRef,
246 ignoreWhitespace: true,
247 },
248 ]);
249 expect(result).toEqual({
250 threadId,
251 fromTurnCount: 0,
252 toTurnCount: 1,
253 diff: "diff patch",
254 });
255 }),
256 );
257
258 it.effect("defaults to hide whitespace changes", () =>
259 Effect.gen(function* () {
260 const projectId = ProjectId.make("project-default-whitespace");
261 const threadId = ThreadId.make("thread-default-whitespace");
262 const toCheckpointRef = checkpointRefForThreadTurn(threadId, 1);
263 const diffCheckpointsCalls: Array<{ readonly ignoreWhitespace: boolean }> = [];
264
265 const threadCheckpointContext = makeThreadCheckpointContext({
266 projectId,
267 threadId,
268 workspaceRoot: "/tmp/workspace",
269 worktreePath: null,
270 checkpointTurnCount: 1,
271 checkpointRef: toCheckpointRef,
272 });
273
274 const checkpointStore: CheckpointStore.CheckpointStore["Service"] = {
275 isGitRepository: () => Effect.succeed(true),
276 captureCheckpoint: () => Effect.void,
277 hasCheckpointRef: () => Effect.succeed(true),
278 restoreCheckpoint: () => Effect.succeed(true),
279 diffCheckpoints: ({ ignoreWhitespace }) =>
280 Effect.sync(() => {
281 diffCheckpointsCalls.push({ ignoreWhitespace });
282 return "diff patch";
283 }),
284 deleteCheckpointRefs: () => Effect.void,
285 };
286
287 const layer = CheckpointDiffQuery.layer.pipe(
288 Layer.provideMerge(Layer.succeed(CheckpointStore.CheckpointStore, checkpointStore)),
289 Layer.provideMerge(
290 Layer.succeed(ProjectionSnapshotQuery.ProjectionSnapshotQuery, {
291 getUserInputActivity: () => Effect.die("unused"),
292 listActivitiesByKind: () => Effect.die("unused"),
293 getCommandReadModel: () =>
294 Effect.die("CheckpointDiffQuery should not request the command read model"),
295 getSnapshot: () =>
296 Effect.die("CheckpointDiffQuery should not request the full orchestration snapshot"),
297 getShellSnapshot: () =>
298 Effect.die("CheckpointDiffQuery should not request the orchestration shell snapshot"),
299 getDeletedWorktreeThreads: () => Effect.die("unused"),
300 listThreadsWithPullRequests: () => Effect.die("unused"),
301 getArchivedShellSnapshot: () =>
302 Effect.die("CheckpointDiffQuery should not request archived shell snapshots"),
303 getSnapshotSequence: () => Effect.succeed({ snapshotSequence: 0 }),
304 getCounts: () => Effect.succeed({ projectCount: 0, threadCount: 0 }),
305 getEventReplayStats: () => Effect.die("unused"),
306 getActiveProjectByWorkspaceRoot: () => Effect.succeedNone,
307 hasActiveProjectAtWorkspaceRoot: () => Effect.succeed(false),
308 getProjectShells: () => Effect.die("unused"),
309 getProjectShellById: () => Effect.succeedNone,
310 getFirstActiveThreadIdByProjectId: () => Effect.succeedNone,
311 getImportedAgentSessionSources: () => Effect.die("unused"),
312 getThreadCheckpointContext: () => Effect.succeedSome(threadCheckpointContext),
313 getFullThreadDiffContext: () => Effect.die("unused"),
314 getThreadRuntimeContext: () => Effect.die("unused"),
315 getTurnStartMessage: () => Effect.die("unused"),
316 getThreadShellById: () => Effect.succeedNone,
317 getThreadDetailById: () => Effect.succeedNone,
318 getThreadDetailSnapshot: () => Effect.succeedNone,
319 searchThreads: () => Effect.succeed({ matches: [] }),
320 }),
321 ),
322 );
323
324 yield* Effect.gen(function* () {
325 const query = yield* CheckpointDiffQuery.CheckpointDiffQuery;
326 return yield* query.getTurnDiff({
327 threadId,
328 fromTurnCount: 0,
329 toTurnCount: 1,
330 });
331 }).pipe(Effect.provide(layer));
332
333 expect(diffCheckpointsCalls).toEqual([{ ignoreWhitespace: true }]);
334 }),
335 );
336
337 it.effect("does not preflight checkpoint refs before diffing", () =>
338 Effect.gen(function* () {
339 const projectId = ProjectId.make("project-no-preflight");
340 const threadId = ThreadId.make("thread-no-preflight");
341 const toCheckpointRef = checkpointRefForThreadTurn(threadId, 1);
342 let hasCheckpointRefCallCount = 0;
343
344 const threadCheckpointContext = makeThreadCheckpointContext({
345 projectId,
346 threadId,
347 workspaceRoot: "/tmp/workspace",
348 worktreePath: null,
349 checkpointTurnCount: 1,
350 checkpointRef: toCheckpointRef,
351 });
352
353 const checkpointStore: CheckpointStore.CheckpointStore["Service"] = {
354 isGitRepository: () => Effect.succeed(true),
355 captureCheckpoint: () => Effect.void,
356 hasCheckpointRef: () =>
357 Effect.sync(() => {
358 hasCheckpointRefCallCount += 1;
359 return true;
360 }),
361 restoreCheckpoint: () => Effect.succeed(true),
362 diffCheckpoints: () => Effect.succeed("diff patch"),
363 deleteCheckpointRefs: () => Effect.void,
364 };
365
366 const layer = CheckpointDiffQuery.layer.pipe(
367 Layer.provideMerge(Layer.succeed(CheckpointStore.CheckpointStore, checkpointStore)),
368 Layer.provideMerge(
369 Layer.succeed(ProjectionSnapshotQuery.ProjectionSnapshotQuery, {
370 getUserInputActivity: () => Effect.die("unused"),
371 listActivitiesByKind: () => Effect.die("unused"),
372 getCommandReadModel: () =>
373 Effect.die("CheckpointDiffQuery should not request the command read model"),
374 getSnapshot: () =>
375 Effect.die("CheckpointDiffQuery should not request the full orchestration snapshot"),
376 getShellSnapshot: () =>
377 Effect.die("CheckpointDiffQuery should not request the orchestration shell snapshot"),
378 getDeletedWorktreeThreads: () => Effect.die("unused"),
379 listThreadsWithPullRequests: () => Effect.die("unused"),
380 getArchivedShellSnapshot: () =>
381 Effect.die("CheckpointDiffQuery should not request archived shell snapshots"),
382 getSnapshotSequence: () => Effect.succeed({ snapshotSequence: 0 }),
383 getCounts: () => Effect.succeed({ projectCount: 0, threadCount: 0 }),
384 getEventReplayStats: () => Effect.die("unused"),
385 getActiveProjectByWorkspaceRoot: () => Effect.succeedNone,
386 hasActiveProjectAtWorkspaceRoot: () => Effect.succeed(false),
387 getProjectShells: () => Effect.die("unused"),
388 getProjectShellById: () => Effect.succeedNone,
389 getFirstActiveThreadIdByProjectId: () => Effect.succeedNone,
390 getImportedAgentSessionSources: () => Effect.die("unused"),
391 getThreadCheckpointContext: () => Effect.succeedSome(threadCheckpointContext),
392 getFullThreadDiffContext: () => Effect.die("unused"),
393 getThreadRuntimeContext: () => Effect.die("unused"),
394 getTurnStartMessage: () => Effect.die("unused"),
395 getThreadShellById: () => Effect.succeedNone,
396 getThreadDetailById: () => Effect.succeedNone,
397 getThreadDetailSnapshot: () => Effect.succeedNone,
398 searchThreads: () => Effect.succeed({ matches: [] }),
399 }),
400 ),
401 );
402
403 yield* Effect.gen(function* () {
404 const query = yield* CheckpointDiffQuery.CheckpointDiffQuery;
405 return yield* query.getTurnDiff({
406 threadId,
407 fromTurnCount: 0,
408 toTurnCount: 1,
409 ignoreWhitespace: true,
410 });
411 }).pipe(Effect.provide(layer));
412
413 expect(hasCheckpointRefCallCount).toBe(0);
414 }),
415 );
416
417 it.effect("fails when the thread is missing from the snapshot", () =>
418 Effect.gen(function* () {
419 const threadId = ThreadId.make("thread-missing");
420
421 const checkpointStore: CheckpointStore.CheckpointStore["Service"] = {
422 isGitRepository: () => Effect.succeed(true),
423 captureCheckpoint: () => Effect.void,
424 hasCheckpointRef: () => Effect.succeed(true),
425 restoreCheckpoint: () => Effect.succeed(true),
426 diffCheckpoints: () => Effect.succeed(""),
427 deleteCheckpointRefs: () => Effect.void,
428 };
429
430 const layer = CheckpointDiffQuery.layer.pipe(
431 Layer.provideMerge(Layer.succeed(CheckpointStore.CheckpointStore, checkpointStore)),
432 Layer.provideMerge(
433 Layer.succeed(ProjectionSnapshotQuery.ProjectionSnapshotQuery, {
434 getUserInputActivity: () => Effect.die("unused"),
435 listActivitiesByKind: () => Effect.die("unused"),
436 getCommandReadModel: () =>
437 Effect.die("CheckpointDiffQuery should not request the command read model"),
438 getSnapshot: () =>
439 Effect.die("CheckpointDiffQuery should not request the full orchestration snapshot"),
440 getShellSnapshot: () =>
441 Effect.die("CheckpointDiffQuery should not request the orchestration shell snapshot"),
442 getDeletedWorktreeThreads: () => Effect.die("unused"),
443 listThreadsWithPullRequests: () => Effect.die("unused"),
444 getArchivedShellSnapshot: () =>
445 Effect.die("CheckpointDiffQuery should not request archived shell snapshots"),
446 getSnapshotSequence: () => Effect.succeed({ snapshotSequence: 0 }),
447 getCounts: () => Effect.succeed({ projectCount: 0, threadCount: 0 }),
448 getEventReplayStats: () => Effect.die("unused"),
449 getActiveProjectByWorkspaceRoot: () => Effect.succeedNone,
450 hasActiveProjectAtWorkspaceRoot: () => Effect.succeed(false),
451 getProjectShells: () => Effect.die("unused"),
452 getProjectShellById: () => Effect.succeedNone,
453 getFirstActiveThreadIdByProjectId: () => Effect.succeedNone,
454 getImportedAgentSessionSources: () => Effect.die("unused"),
455 getThreadCheckpointContext: () => Effect.succeedNone,
456 getFullThreadDiffContext: () => Effect.succeedNone,
457 getThreadRuntimeContext: () => Effect.die("unused"),
458 getTurnStartMessage: () => Effect.die("unused"),
459 getThreadShellById: () => Effect.succeedNone,
460 getThreadDetailById: () => Effect.succeedNone,
461 getThreadDetailSnapshot: () => Effect.succeedNone,
462 searchThreads: () => Effect.succeed({ matches: [] }),
463 }),
464 ),
465 );
466
467 const error = yield* Effect.gen(function* () {
468 const query = yield* CheckpointDiffQuery.CheckpointDiffQuery;
469 return yield* query.getTurnDiff({
470 threadId,
471 fromTurnCount: 0,
472 toTurnCount: 1,
473 });
474 }).pipe(Effect.provide(layer), Effect.flip);
475
476 expect(error).toBeInstanceOf(CheckpointThreadNotFoundError);
477 expect(error).toMatchObject({
478 operation: "CheckpointDiffQuery.getTurnDiff",
479 threadId,
480 });
481 expect(error.message).toBe(
482 "Checkpoint invariant violation in CheckpointDiffQuery.getTurnDiff: Thread 'thread-missing' not found.",
483 );
484 }),
485 );
486});