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