Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

CheckpointDiffQuery.test.ts

451 lines19.3 KB

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