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 listActivitiesByKind: () => Effect.die("unused"),
80 getCommandReadModel: () =>
81 Effect.die("CheckpointDiffQuery should not request the command read model"),
82 getSnapshot: () =>
83 Effect.die("CheckpointDiffQuery should not request the full orchestration snapshot"),
84 getShellSnapshot: () =>
85 Effect.die("CheckpointDiffQuery should not request the orchestration shell snapshot"),
86 getArchivedShellSnapshot: () =>
87 Effect.die("CheckpointDiffQuery should not request archived shell snapshots"),
88 getSnapshotSequence: () => Effect.succeed({ snapshotSequence: 0 }),
89 getCounts: () => Effect.succeed({ projectCount: 0, threadCount: 0 }),
90 getEventReplayStats: () => Effect.die("unused"),
91 getActiveProjectByWorkspaceRoot: () => Effect.succeed(Option.none()),
92 getProjectShells: () => Effect.die("unused"),
93 getProjectShellById: () => Effect.succeed(Option.none()),
94 getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),
95 getImportedAgentSessionSources: () => Effect.die("unused"),
96 getThreadCheckpointContext: () =>
97 Effect.sync(() => {
98 getThreadCheckpointContextCalls += 1;
99 return Option.none();
100 }),
101 getFullThreadDiffContext: () =>
102 Effect.sync(() => {
103 getFullThreadDiffContextCalls += 1;
104 return Option.some({
105 threadId,
106 projectId,
107 workspaceRoot: "/tmp/workspace",
108 worktreePath: "/tmp/worktree",
109 latestCheckpointTurnCount: 4,
110 toCheckpointRef,
111 });
112 }),
113 getThreadRuntimeContext: () => Effect.die("unused"),
114 getTurnStartMessage: () => Effect.die("unused"),
115 getThreadShellById: () => Effect.succeed(Option.none()),
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 listActivitiesByKind: () => Effect.die("unused"),
196 getCommandReadModel: () =>
197 Effect.die("CheckpointDiffQuery should not request the command read model"),
198 getSnapshot: () =>
199 Effect.die("CheckpointDiffQuery should not request the full orchestration snapshot"),
200 getShellSnapshot: () =>
201 Effect.die("CheckpointDiffQuery should not request the orchestration shell snapshot"),
202 getArchivedShellSnapshot: () =>
203 Effect.die("CheckpointDiffQuery should not request archived shell snapshots"),
204 getSnapshotSequence: () => Effect.succeed({ snapshotSequence: 0 }),
205 getCounts: () => Effect.succeed({ projectCount: 0, threadCount: 0 }),
206 getEventReplayStats: () => Effect.die("unused"),
207 getActiveProjectByWorkspaceRoot: () => Effect.succeed(Option.none()),
208 getProjectShells: () => Effect.die("unused"),
209 getProjectShellById: () => Effect.succeed(Option.none()),
210 getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),
211 getImportedAgentSessionSources: () => Effect.die("unused"),
212 getThreadCheckpointContext: () => Effect.succeed(Option.some(threadCheckpointContext)),
213 getFullThreadDiffContext: () => Effect.die("unused"),
214 getThreadRuntimeContext: () => Effect.die("unused"),
215 getTurnStartMessage: () => Effect.die("unused"),
216 getThreadShellById: () => Effect.succeed(Option.none()),
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 listActivitiesByKind: () => Effect.die("unused"),
287 getCommandReadModel: () =>
288 Effect.die("CheckpointDiffQuery should not request the command read model"),
289 getSnapshot: () =>
290 Effect.die("CheckpointDiffQuery should not request the full orchestration snapshot"),
291 getShellSnapshot: () =>
292 Effect.die("CheckpointDiffQuery should not request the orchestration shell snapshot"),
293 getArchivedShellSnapshot: () =>
294 Effect.die("CheckpointDiffQuery should not request archived shell snapshots"),
295 getSnapshotSequence: () => Effect.succeed({ snapshotSequence: 0 }),
296 getCounts: () => Effect.succeed({ projectCount: 0, threadCount: 0 }),
297 getEventReplayStats: () => Effect.die("unused"),
298 getActiveProjectByWorkspaceRoot: () => Effect.succeed(Option.none()),
299 getProjectShells: () => Effect.die("unused"),
300 getProjectShellById: () => Effect.succeed(Option.none()),
301 getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),
302 getImportedAgentSessionSources: () => Effect.die("unused"),
303 getThreadCheckpointContext: () => Effect.succeed(Option.some(threadCheckpointContext)),
304 getFullThreadDiffContext: () => Effect.die("unused"),
305 getThreadRuntimeContext: () => Effect.die("unused"),
306 getTurnStartMessage: () => Effect.die("unused"),
307 getThreadShellById: () => Effect.succeed(Option.none()),
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 listActivitiesByKind: () => Effect.die("unused"),
363 getCommandReadModel: () =>
364 Effect.die("CheckpointDiffQuery should not request the command read model"),
365 getSnapshot: () =>
366 Effect.die("CheckpointDiffQuery should not request the full orchestration snapshot"),
367 getShellSnapshot: () =>
368 Effect.die("CheckpointDiffQuery should not request the orchestration shell snapshot"),
369 getArchivedShellSnapshot: () =>
370 Effect.die("CheckpointDiffQuery should not request archived shell snapshots"),
371 getSnapshotSequence: () => Effect.succeed({ snapshotSequence: 0 }),
372 getCounts: () => Effect.succeed({ projectCount: 0, threadCount: 0 }),
373 getEventReplayStats: () => Effect.die("unused"),
374 getActiveProjectByWorkspaceRoot: () => Effect.succeed(Option.none()),
375 getProjectShells: () => Effect.die("unused"),
376 getProjectShellById: () => Effect.succeed(Option.none()),
377 getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),
378 getImportedAgentSessionSources: () => Effect.die("unused"),
379 getThreadCheckpointContext: () => Effect.succeed(Option.some(threadCheckpointContext)),
380 getFullThreadDiffContext: () => Effect.die("unused"),
381 getThreadRuntimeContext: () => Effect.die("unused"),
382 getTurnStartMessage: () => Effect.die("unused"),
383 getThreadShellById: () => Effect.succeed(Option.none()),
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 listActivitiesByKind: () => Effect.die("unused"),
424 getCommandReadModel: () =>
425 Effect.die("CheckpointDiffQuery should not request the command read model"),
426 getSnapshot: () =>
427 Effect.die("CheckpointDiffQuery should not request the full orchestration snapshot"),
428 getShellSnapshot: () =>
429 Effect.die("CheckpointDiffQuery should not request the orchestration shell snapshot"),
430 getArchivedShellSnapshot: () =>
431 Effect.die("CheckpointDiffQuery should not request archived shell snapshots"),
432 getSnapshotSequence: () => Effect.succeed({ snapshotSequence: 0 }),
433 getCounts: () => Effect.succeed({ projectCount: 0, threadCount: 0 }),
434 getEventReplayStats: () => Effect.die("unused"),
435 getActiveProjectByWorkspaceRoot: () => Effect.succeed(Option.none()),
436 getProjectShells: () => Effect.die("unused"),
437 getProjectShellById: () => Effect.succeed(Option.none()),
438 getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),
439 getImportedAgentSessionSources: () => Effect.die("unused"),
440 getThreadCheckpointContext: () => Effect.succeed(Option.none()),
441 getFullThreadDiffContext: () => Effect.succeed(Option.none()),
442 getThreadRuntimeContext: () => Effect.die("unused"),
443 getTurnStartMessage: () => Effect.die("unused"),
444 getThreadShellById: () => Effect.succeed(Option.none()),
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});