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