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