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