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 getCommandReadModel: () =>
80 Effect.die("CheckpointDiffQuery should not request the command read model"),
81 getSnapshot: () =>
82 Effect.die("CheckpointDiffQuery should not request the full orchestration snapshot"),
83 getShellSnapshot: () =>
84 Effect.die("CheckpointDiffQuery should not request the orchestration shell snapshot"),
85 getArchivedShellSnapshot: () =>
86 Effect.die("CheckpointDiffQuery should not request archived shell snapshots"),
87 getSnapshotSequence: () => Effect.succeed({ snapshotSequence: 0 }),
88 getCounts: () => Effect.succeed({ projectCount: 0, threadCount: 0 }),
89 getEventReplayStats: () => Effect.die("unused"),
90 getActiveProjectByWorkspaceRoot: () => Effect.succeed(Option.none()),
91 getProjectShells: () => Effect.die("unused"),
92 getProjectShellById: () => Effect.succeed(Option.none()),
93 getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),
94 getImportedAgentSessionSources: () => Effect.die("unused"),
95 getThreadCheckpointContext: () =>
96 Effect.sync(() => {
97 getThreadCheckpointContextCalls += 1;
98 return Option.none();
99 }),
100 getFullThreadDiffContext: () =>
101 Effect.sync(() => {
102 getFullThreadDiffContextCalls += 1;
103 return Option.some({
104 threadId,
105 projectId,
106 workspaceRoot: "/tmp/workspace",
107 worktreePath: "/tmp/worktree",
108 latestCheckpointTurnCount: 4,
109 toCheckpointRef,
110 });
111 }),
112 getThreadRuntimeContext: () => Effect.die("unused"),
113 getTurnStartMessage: () => Effect.die("unused"),
114 getThreadShellById: () => Effect.succeed(Option.none()),
115 getThreadDetailById: () => Effect.succeed(Option.none()),
116 getThreadDetailSnapshot: () => Effect.succeed(Option.none()),
117 searchThreads: () => Effect.succeed({ matches: [] }),
118 }),
119 ),
120 );
121
122 const result = yield* Effect.gen(function* () {
123 const query = yield* CheckpointDiffQuery.CheckpointDiffQuery;
124 return yield* query.getFullThreadDiff({
125 threadId,
126 toTurnCount: 4,
127 ignoreWhitespace: true,
128 });
129 }).pipe(Effect.provide(layer));
130
131 expect(getThreadCheckpointContextCalls).toBe(0);
132 expect(getFullThreadDiffContextCalls).toBe(1);
133 expect(diffCheckpointsCalls).toEqual([
134 {
135 cwd: "/tmp/worktree",
136 fromCheckpointRef: checkpointRefForThreadTurn(threadId, 0),
137 toCheckpointRef,
138 ignoreWhitespace: true,
139 },
140 ]);
141 expect(result).toEqual({
142 threadId,
143 fromTurnCount: 0,
144 toTurnCount: 4,
145 diff: "full thread diff patch",
146 });
147 }),
148 );
149
150 it.effect("computes diffs using canonical turn-0 checkpoint refs", () =>
151 Effect.gen(function* () {
152 const projectId = ProjectId.make("project-1");
153 const threadId = ThreadId.make("thread-1");
154 const toCheckpointRef = checkpointRefForThreadTurn(threadId, 1);
155 const diffCheckpointsCalls: Array<{
156 readonly fromCheckpointRef: CheckpointRef;
157 readonly toCheckpointRef: CheckpointRef;
158 readonly cwd: string;
159 readonly ignoreWhitespace: boolean;
160 }> = [];
161
162 const threadCheckpointContext = makeThreadCheckpointContext({
163 projectId,
164 threadId,
165 workspaceRoot: "/tmp/workspace",
166 worktreePath: null,
167 checkpointTurnCount: 1,
168 checkpointRef: toCheckpointRef,
169 });
170
171 const checkpointStore: CheckpointStore.CheckpointStore["Service"] = {
172 isGitRepository: () => Effect.succeed(true),
173 captureCheckpoint: () => Effect.void,
174 hasCheckpointRef: () => Effect.succeed(true),
175 restoreCheckpoint: () => Effect.succeed(true),
176 diffCheckpoints: ({ fromCheckpointRef, toCheckpointRef, cwd, ignoreWhitespace }) =>
177 Effect.sync(() => {
178 diffCheckpointsCalls.push({
179 fromCheckpointRef,
180 toCheckpointRef,
181 cwd,
182 ignoreWhitespace,
183 });
184 return "diff patch";
185 }),
186 deleteCheckpointRefs: () => Effect.void,
187 };
188
189 const layer = CheckpointDiffQuery.layer.pipe(
190 Layer.provideMerge(Layer.succeed(CheckpointStore.CheckpointStore, checkpointStore)),
191 Layer.provideMerge(
192 Layer.succeed(ProjectionSnapshotQuery.ProjectionSnapshotQuery, {
193 getUserInputActivity: () => Effect.die("unused"),
194 getCommandReadModel: () =>
195 Effect.die("CheckpointDiffQuery should not request the command read model"),
196 getSnapshot: () =>
197 Effect.die("CheckpointDiffQuery should not request the full orchestration snapshot"),
198 getShellSnapshot: () =>
199 Effect.die("CheckpointDiffQuery should not request the orchestration shell snapshot"),
200 getArchivedShellSnapshot: () =>
201 Effect.die("CheckpointDiffQuery should not request archived shell snapshots"),
202 getSnapshotSequence: () => Effect.succeed({ snapshotSequence: 0 }),
203 getCounts: () => Effect.succeed({ projectCount: 0, threadCount: 0 }),
204 getEventReplayStats: () => Effect.die("unused"),
205 getActiveProjectByWorkspaceRoot: () => Effect.succeed(Option.none()),
206 getProjectShells: () => Effect.die("unused"),
207 getProjectShellById: () => Effect.succeed(Option.none()),
208 getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),
209 getImportedAgentSessionSources: () => Effect.die("unused"),
210 getThreadCheckpointContext: () => Effect.succeed(Option.some(threadCheckpointContext)),
211 getFullThreadDiffContext: () => Effect.die("unused"),
212 getThreadRuntimeContext: () => Effect.die("unused"),
213 getTurnStartMessage: () => Effect.die("unused"),
214 getThreadShellById: () => Effect.succeed(Option.none()),
215 getThreadDetailById: () => Effect.succeed(Option.none()),
216 getThreadDetailSnapshot: () => Effect.succeed(Option.none()),
217 searchThreads: () => Effect.succeed({ matches: [] }),
218 }),
219 ),
220 );
221
222 const result = yield* Effect.gen(function* () {
223 const query = yield* CheckpointDiffQuery.CheckpointDiffQuery;
224 return yield* query.getTurnDiff({
225 threadId,
226 fromTurnCount: 0,
227 toTurnCount: 1,
228 ignoreWhitespace: true,
229 });
230 }).pipe(Effect.provide(layer));
231
232 const expectedFromRef = checkpointRefForThreadTurn(threadId, 0);
233 expect(diffCheckpointsCalls).toEqual([
234 {
235 cwd: "/tmp/workspace",
236 fromCheckpointRef: expectedFromRef,
237 toCheckpointRef,
238 ignoreWhitespace: true,
239 },
240 ]);
241 expect(result).toEqual({
242 threadId,
243 fromTurnCount: 0,
244 toTurnCount: 1,
245 diff: "diff patch",
246 });
247 }),
248 );
249
250 it.effect("defaults to hide whitespace changes", () =>
251 Effect.gen(function* () {
252 const projectId = ProjectId.make("project-default-whitespace");
253 const threadId = ThreadId.make("thread-default-whitespace");
254 const toCheckpointRef = checkpointRefForThreadTurn(threadId, 1);
255 const diffCheckpointsCalls: Array<{ readonly ignoreWhitespace: boolean }> = [];
256
257 const threadCheckpointContext = makeThreadCheckpointContext({
258 projectId,
259 threadId,
260 workspaceRoot: "/tmp/workspace",
261 worktreePath: null,
262 checkpointTurnCount: 1,
263 checkpointRef: toCheckpointRef,
264 });
265
266 const checkpointStore: CheckpointStore.CheckpointStore["Service"] = {
267 isGitRepository: () => Effect.succeed(true),
268 captureCheckpoint: () => Effect.void,
269 hasCheckpointRef: () => Effect.succeed(true),
270 restoreCheckpoint: () => Effect.succeed(true),
271 diffCheckpoints: ({ ignoreWhitespace }) =>
272 Effect.sync(() => {
273 diffCheckpointsCalls.push({ ignoreWhitespace });
274 return "diff patch";
275 }),
276 deleteCheckpointRefs: () => Effect.void,
277 };
278
279 const layer = CheckpointDiffQuery.layer.pipe(
280 Layer.provideMerge(Layer.succeed(CheckpointStore.CheckpointStore, checkpointStore)),
281 Layer.provideMerge(
282 Layer.succeed(ProjectionSnapshotQuery.ProjectionSnapshotQuery, {
283 getUserInputActivity: () => Effect.die("unused"),
284 getCommandReadModel: () =>
285 Effect.die("CheckpointDiffQuery should not request the command read model"),
286 getSnapshot: () =>
287 Effect.die("CheckpointDiffQuery should not request the full orchestration snapshot"),
288 getShellSnapshot: () =>
289 Effect.die("CheckpointDiffQuery should not request the orchestration shell snapshot"),
290 getArchivedShellSnapshot: () =>
291 Effect.die("CheckpointDiffQuery should not request archived shell snapshots"),
292 getSnapshotSequence: () => Effect.succeed({ snapshotSequence: 0 }),
293 getCounts: () => Effect.succeed({ projectCount: 0, threadCount: 0 }),
294 getEventReplayStats: () => Effect.die("unused"),
295 getActiveProjectByWorkspaceRoot: () => Effect.succeed(Option.none()),
296 getProjectShells: () => Effect.die("unused"),
297 getProjectShellById: () => Effect.succeed(Option.none()),
298 getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),
299 getImportedAgentSessionSources: () => Effect.die("unused"),
300 getThreadCheckpointContext: () => Effect.succeed(Option.some(threadCheckpointContext)),
301 getFullThreadDiffContext: () => Effect.die("unused"),
302 getThreadRuntimeContext: () => Effect.die("unused"),
303 getTurnStartMessage: () => Effect.die("unused"),
304 getThreadShellById: () => Effect.succeed(Option.none()),
305 getThreadDetailById: () => Effect.succeed(Option.none()),
306 getThreadDetailSnapshot: () => Effect.succeed(Option.none()),
307 searchThreads: () => Effect.succeed({ matches: [] }),
308 }),
309 ),
310 );
311
312 yield* Effect.gen(function* () {
313 const query = yield* CheckpointDiffQuery.CheckpointDiffQuery;
314 return yield* query.getTurnDiff({
315 threadId,
316 fromTurnCount: 0,
317 toTurnCount: 1,
318 });
319 }).pipe(Effect.provide(layer));
320
321 expect(diffCheckpointsCalls).toEqual([{ ignoreWhitespace: true }]);
322 }),
323 );
324
325 it.effect("does not preflight checkpoint refs before diffing", () =>
326 Effect.gen(function* () {
327 const projectId = ProjectId.make("project-no-preflight");
328 const threadId = ThreadId.make("thread-no-preflight");
329 const toCheckpointRef = checkpointRefForThreadTurn(threadId, 1);
330 let hasCheckpointRefCallCount = 0;
331
332 const threadCheckpointContext = makeThreadCheckpointContext({
333 projectId,
334 threadId,
335 workspaceRoot: "/tmp/workspace",
336 worktreePath: null,
337 checkpointTurnCount: 1,
338 checkpointRef: toCheckpointRef,
339 });
340
341 const checkpointStore: CheckpointStore.CheckpointStore["Service"] = {
342 isGitRepository: () => Effect.succeed(true),
343 captureCheckpoint: () => Effect.void,
344 hasCheckpointRef: () =>
345 Effect.sync(() => {
346 hasCheckpointRefCallCount += 1;
347 return true;
348 }),
349 restoreCheckpoint: () => Effect.succeed(true),
350 diffCheckpoints: () => Effect.succeed("diff patch"),
351 deleteCheckpointRefs: () => Effect.void,
352 };
353
354 const layer = CheckpointDiffQuery.layer.pipe(
355 Layer.provideMerge(Layer.succeed(CheckpointStore.CheckpointStore, checkpointStore)),
356 Layer.provideMerge(
357 Layer.succeed(ProjectionSnapshotQuery.ProjectionSnapshotQuery, {
358 getUserInputActivity: () => Effect.die("unused"),
359 getCommandReadModel: () =>
360 Effect.die("CheckpointDiffQuery should not request the command read model"),
361 getSnapshot: () =>
362 Effect.die("CheckpointDiffQuery should not request the full orchestration snapshot"),
363 getShellSnapshot: () =>
364 Effect.die("CheckpointDiffQuery should not request the orchestration shell snapshot"),
365 getArchivedShellSnapshot: () =>
366 Effect.die("CheckpointDiffQuery should not request archived shell snapshots"),
367 getSnapshotSequence: () => Effect.succeed({ snapshotSequence: 0 }),
368 getCounts: () => Effect.succeed({ projectCount: 0, threadCount: 0 }),
369 getEventReplayStats: () => Effect.die("unused"),
370 getActiveProjectByWorkspaceRoot: () => Effect.succeed(Option.none()),
371 getProjectShells: () => Effect.die("unused"),
372 getProjectShellById: () => Effect.succeed(Option.none()),
373 getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),
374 getImportedAgentSessionSources: () => Effect.die("unused"),
375 getThreadCheckpointContext: () => Effect.succeed(Option.some(threadCheckpointContext)),
376 getFullThreadDiffContext: () => Effect.die("unused"),
377 getThreadRuntimeContext: () => Effect.die("unused"),
378 getTurnStartMessage: () => Effect.die("unused"),
379 getThreadShellById: () => Effect.succeed(Option.none()),
380 getThreadDetailById: () => Effect.succeed(Option.none()),
381 getThreadDetailSnapshot: () => Effect.succeed(Option.none()),
382 searchThreads: () => Effect.succeed({ matches: [] }),
383 }),
384 ),
385 );
386
387 yield* Effect.gen(function* () {
388 const query = yield* CheckpointDiffQuery.CheckpointDiffQuery;
389 return yield* query.getTurnDiff({
390 threadId,
391 fromTurnCount: 0,
392 toTurnCount: 1,
393 ignoreWhitespace: true,
394 });
395 }).pipe(Effect.provide(layer));
396
397 expect(hasCheckpointRefCallCount).toBe(0);
398 }),
399 );
400
401 it.effect("fails when the thread is missing from the snapshot", () =>
402 Effect.gen(function* () {
403 const threadId = ThreadId.make("thread-missing");
404
405 const checkpointStore: CheckpointStore.CheckpointStore["Service"] = {
406 isGitRepository: () => Effect.succeed(true),
407 captureCheckpoint: () => Effect.void,
408 hasCheckpointRef: () => Effect.succeed(true),
409 restoreCheckpoint: () => Effect.succeed(true),
410 diffCheckpoints: () => Effect.succeed(""),
411 deleteCheckpointRefs: () => Effect.void,
412 };
413
414 const layer = CheckpointDiffQuery.layer.pipe(
415 Layer.provideMerge(Layer.succeed(CheckpointStore.CheckpointStore, checkpointStore)),
416 Layer.provideMerge(
417 Layer.succeed(ProjectionSnapshotQuery.ProjectionSnapshotQuery, {
418 getUserInputActivity: () => Effect.die("unused"),
419 getCommandReadModel: () =>
420 Effect.die("CheckpointDiffQuery should not request the command read model"),
421 getSnapshot: () =>
422 Effect.die("CheckpointDiffQuery should not request the full orchestration snapshot"),
423 getShellSnapshot: () =>
424 Effect.die("CheckpointDiffQuery should not request the orchestration shell snapshot"),
425 getArchivedShellSnapshot: () =>
426 Effect.die("CheckpointDiffQuery should not request archived shell snapshots"),
427 getSnapshotSequence: () => Effect.succeed({ snapshotSequence: 0 }),
428 getCounts: () => Effect.succeed({ projectCount: 0, threadCount: 0 }),
429 getEventReplayStats: () => Effect.die("unused"),
430 getActiveProjectByWorkspaceRoot: () => Effect.succeed(Option.none()),
431 getProjectShells: () => Effect.die("unused"),
432 getProjectShellById: () => Effect.succeed(Option.none()),
433 getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),
434 getImportedAgentSessionSources: () => Effect.die("unused"),
435 getThreadCheckpointContext: () => Effect.succeed(Option.none()),
436 getFullThreadDiffContext: () => Effect.succeed(Option.none()),
437 getThreadRuntimeContext: () => Effect.die("unused"),
438 getTurnStartMessage: () => Effect.die("unused"),
439 getThreadShellById: () => Effect.succeed(Option.none()),
440 getThreadDetailById: () => Effect.succeed(Option.none()),
441 getThreadDetailSnapshot: () => Effect.succeed(Option.none()),
442 searchThreads: () => Effect.succeed({ matches: [] }),
443 }),
444 ),
445 );
446
447 const error = yield* Effect.gen(function* () {
448 const query = yield* CheckpointDiffQuery.CheckpointDiffQuery;
449 return yield* query.getTurnDiff({
450 threadId,
451 fromTurnCount: 0,
452 toTurnCount: 1,
453 });
454 }).pipe(Effect.provide(layer), Effect.flip);
455
456 expect(error).toBeInstanceOf(CheckpointThreadNotFoundError);
457 expect(error).toMatchObject({
458 operation: "CheckpointDiffQuery.getTurnDiff",
459 threadId,
460 });
461 expect(error.message).toBe(
462 "Checkpoint invariant violation in CheckpointDiffQuery.getTurnDiff: Thread 'thread-missing' not found.",
463 );
464 }),
465 );
466});