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

CheckpointDiffQuery.test.ts

532 lines22.2 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 attributeCheckpointDiff: () => Effect.succeed(null),
73 };
74​
75 const layer = CheckpointDiffQuery.layer.pipe(
76 Layer.provideMerge(Layer.succeed(CheckpointStore.CheckpointStore, checkpointStore)),
77 Layer.provideMerge(
78 Layer.succeed(ProjectionSnapshotQuery.ProjectionSnapshotQuery, {
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 getActiveProjectByWorkspaceRoot: () => Effect.succeed(Option.none()),
90 getProjectShellById: () => Effect.succeed(Option.none()),
91 getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),
92 getThreadCheckpointContext: () =>
93 Effect.sync(() => {
94 getThreadCheckpointContextCalls += 1;
95 return Option.none();
96 }),
97 getFullThreadDiffContext: () =>
98 Effect.sync(() => {
99 getFullThreadDiffContextCalls += 1;
100 return Option.some({
101 threadId,
102 projectId,
103 workspaceRoot: "/tmp/workspace",
104 worktreePath: "/tmp/worktree",
105 latestCheckpointTurnCount: 4,
106 toCheckpointRef,
107 });
108 }),
109 getThreadShellById: () => Effect.succeed(Option.none()),
110 getThreadDetailById: () => Effect.succeed(Option.none()),
111 getThreadDetailSnapshot: () => Effect.succeed(Option.none()),
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 attributeCheckpointDiff: () => Effect.succeed(null),
182 };
183​
184 const layer = CheckpointDiffQuery.layer.pipe(
185 Layer.provideMerge(Layer.succeed(CheckpointStore.CheckpointStore, checkpointStore)),
186 Layer.provideMerge(
187 Layer.succeed(ProjectionSnapshotQuery.ProjectionSnapshotQuery, {
188 getCommandReadModel: () =>
189 Effect.die("CheckpointDiffQuery should not request the command read model"),
190 getSnapshot: () =>
191 Effect.die("CheckpointDiffQuery should not request the full orchestration snapshot"),
192 getShellSnapshot: () =>
193 Effect.die("CheckpointDiffQuery should not request the orchestration shell snapshot"),
194 getArchivedShellSnapshot: () =>
195 Effect.die("CheckpointDiffQuery should not request archived shell snapshots"),
196 getSnapshotSequence: () => Effect.succeed({ snapshotSequence: 0 }),
197 getCounts: () => Effect.succeed({ projectCount: 0, threadCount: 0 }),
198 getActiveProjectByWorkspaceRoot: () => Effect.succeed(Option.none()),
199 getProjectShellById: () => Effect.succeed(Option.none()),
200 getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),
201 getThreadCheckpointContext: () => Effect.succeed(Option.some(threadCheckpointContext)),
202 getFullThreadDiffContext: () => Effect.die("unused"),
203 getThreadShellById: () => Effect.succeed(Option.none()),
204 getThreadDetailById: () => Effect.succeed(Option.none()),
205 getThreadDetailSnapshot: () => Effect.succeed(Option.none()),
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 attributeCheckpointDiff: () => Effect.succeed(null),
266 };
267​
268 const layer = CheckpointDiffQuery.layer.pipe(
269 Layer.provideMerge(Layer.succeed(CheckpointStore.CheckpointStore, checkpointStore)),
270 Layer.provideMerge(
271 Layer.succeed(ProjectionSnapshotQuery.ProjectionSnapshotQuery, {
272 getCommandReadModel: () =>
273 Effect.die("CheckpointDiffQuery should not request the command read model"),
274 getSnapshot: () =>
275 Effect.die("CheckpointDiffQuery should not request the full orchestration snapshot"),
276 getShellSnapshot: () =>
277 Effect.die("CheckpointDiffQuery should not request the orchestration shell snapshot"),
278 getArchivedShellSnapshot: () =>
279 Effect.die("CheckpointDiffQuery should not request archived shell snapshots"),
280 getSnapshotSequence: () => Effect.succeed({ snapshotSequence: 0 }),
281 getCounts: () => Effect.succeed({ projectCount: 0, threadCount: 0 }),
282 getActiveProjectByWorkspaceRoot: () => Effect.succeed(Option.none()),
283 getProjectShellById: () => Effect.succeed(Option.none()),
284 getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),
285 getThreadCheckpointContext: () => Effect.succeed(Option.some(threadCheckpointContext)),
286 getFullThreadDiffContext: () => Effect.die("unused"),
287 getThreadShellById: () => Effect.succeed(Option.none()),
288 getThreadDetailById: () => Effect.succeed(Option.none()),
289 getThreadDetailSnapshot: () => Effect.succeed(Option.none()),
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("filters git-attributed sections and reports gitFileCount from patch sections", () =>
308 Effect.gen(function* () {
309 const projectId = ProjectId.make("project-attribution");
310 const threadId = ThreadId.make("thread-attribution");
311 const toCheckpointRef = checkpointRefForThreadTurn(threadId, 1);
312 const patch = [
313 "diff --git a/agent.ts b/agent.ts",
314 "index 1111111..2222222 100644",
315 "--- a/agent.ts",
316 "+++ b/agent.ts",
317 "@@ -1,1 +1,1 @@",
318 "-old",
319 "+new",
320 "diff --git a/pulled.ts b/pulled.ts",
321 "index 3333333..4444444 100644",
322 "--- a/pulled.ts",
323 "+++ b/pulled.ts",
324 "@@ -1,1 +1,1 @@",
325 "-before",
326 "+after",
327 "",
328 ].join("\n");
329 // Attribution also tags a path that has no section in this patch
330 // (e.g. whitespace-only change elided by ignoreWhitespace) — it must
331 // not count toward gitFileCount.
332 const attribution = new Map<string, "agent" | "git">([
333 ["agent.ts", "agent"],
334 ["pulled.ts", "git"],
335 ["whitespace-only.ts", "git"],
336 ]);
337​
338 const threadCheckpointContext = makeThreadCheckpointContext({
339 projectId,
340 threadId,
341 workspaceRoot: "/tmp/workspace",
342 worktreePath: null,
343 checkpointTurnCount: 1,
344 checkpointRef: toCheckpointRef,
345 });
346​
347 const checkpointStore: CheckpointStore.CheckpointStore["Service"] = {
348 isGitRepository: () => Effect.succeed(true),
349 captureCheckpoint: () => Effect.void,
350 hasCheckpointRef: () => Effect.succeed(true),
351 restoreCheckpoint: () => Effect.succeed(true),
352 diffCheckpoints: () => Effect.succeed(patch),
353 deleteCheckpointRefs: () => Effect.void,
354 attributeCheckpointDiff: () => Effect.succeed(attribution),
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 getCommandReadModel: () => Effect.die("unused"),
362 getSnapshot: () => Effect.die("unused"),
363 getShellSnapshot: () => Effect.die("unused"),
364 getArchivedShellSnapshot: () => Effect.die("unused"),
365 getSnapshotSequence: () => Effect.succeed({ snapshotSequence: 0 }),
366 getCounts: () => Effect.succeed({ projectCount: 0, threadCount: 0 }),
367 getActiveProjectByWorkspaceRoot: () => Effect.succeed(Option.none()),
368 getProjectShellById: () => Effect.succeed(Option.none()),
369 getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),
370 getThreadCheckpointContext: () => Effect.succeed(Option.some(threadCheckpointContext)),
371 getFullThreadDiffContext: () => Effect.die("unused"),
372 getThreadShellById: () => Effect.succeed(Option.none()),
373 getThreadDetailById: () => Effect.succeed(Option.none()),
374 getThreadDetailSnapshot: () => Effect.succeed(Option.none()),
375 }),
376 ),
377 );
378​
379 const { filtered, revealed } = yield* Effect.gen(function* () {
380 const query = yield* CheckpointDiffQuery.CheckpointDiffQuery;
381 const filtered = yield* query.getTurnDiff({
382 threadId,
383 fromTurnCount: 0,
384 toTurnCount: 1,
385 });
386 const revealed = yield* query.getTurnDiff({
387 threadId,
388 fromTurnCount: 0,
389 toTurnCount: 1,
390 includeGitChanges: true,
391 });
392 return { filtered, revealed };
393 }).pipe(Effect.provide(layer));
394​
395 expect(filtered.diff).toContain("agent.ts");
396 expect(filtered.diff).not.toContain("pulled.ts");
397 expect(filtered.gitFileCount).toBe(1);
398 expect(revealed.diff).toBe(patch);
399 expect(revealed.gitFileCount).toBe(1);
400 }),
401 );
402​
403 it.effect("does not preflight checkpoint refs before diffing", () =>
404 Effect.gen(function* () {
405 const projectId = ProjectId.make("project-no-preflight");
406 const threadId = ThreadId.make("thread-no-preflight");
407 const toCheckpointRef = checkpointRefForThreadTurn(threadId, 1);
408 let hasCheckpointRefCallCount = 0;
409​
410 const threadCheckpointContext = makeThreadCheckpointContext({
411 projectId,
412 threadId,
413 workspaceRoot: "/tmp/workspace",
414 worktreePath: null,
415 checkpointTurnCount: 1,
416 checkpointRef: toCheckpointRef,
417 });
418​
419 const checkpointStore: CheckpointStore.CheckpointStore["Service"] = {
420 isGitRepository: () => Effect.succeed(true),
421 captureCheckpoint: () => Effect.void,
422 hasCheckpointRef: () =>
423 Effect.sync(() => {
424 hasCheckpointRefCallCount += 1;
425 return true;
426 }),
427 restoreCheckpoint: () => Effect.succeed(true),
428 diffCheckpoints: () => Effect.succeed("diff patch"),
429 deleteCheckpointRefs: () => Effect.void,
430 attributeCheckpointDiff: () => Effect.succeed(null),
431 };
432​
433 const layer = CheckpointDiffQuery.layer.pipe(
434 Layer.provideMerge(Layer.succeed(CheckpointStore.CheckpointStore, checkpointStore)),
435 Layer.provideMerge(
436 Layer.succeed(ProjectionSnapshotQuery.ProjectionSnapshotQuery, {
437 getCommandReadModel: () =>
438 Effect.die("CheckpointDiffQuery should not request the command read model"),
439 getSnapshot: () =>
440 Effect.die("CheckpointDiffQuery should not request the full orchestration snapshot"),
441 getShellSnapshot: () =>
442 Effect.die("CheckpointDiffQuery should not request the orchestration shell snapshot"),
443 getArchivedShellSnapshot: () =>
444 Effect.die("CheckpointDiffQuery should not request archived shell snapshots"),
445 getSnapshotSequence: () => Effect.succeed({ snapshotSequence: 0 }),
446 getCounts: () => Effect.succeed({ projectCount: 0, threadCount: 0 }),
447 getActiveProjectByWorkspaceRoot: () => Effect.succeed(Option.none()),
448 getProjectShellById: () => Effect.succeed(Option.none()),
449 getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),
450 getThreadCheckpointContext: () => Effect.succeed(Option.some(threadCheckpointContext)),
451 getFullThreadDiffContext: () => Effect.die("unused"),
452 getThreadShellById: () => Effect.succeed(Option.none()),
453 getThreadDetailById: () => Effect.succeed(Option.none()),
454 getThreadDetailSnapshot: () => Effect.succeed(Option.none()),
455 }),
456 ),
457 );
458​
459 yield* Effect.gen(function* () {
460 const query = yield* CheckpointDiffQuery.CheckpointDiffQuery;
461 return yield* query.getTurnDiff({
462 threadId,
463 fromTurnCount: 0,
464 toTurnCount: 1,
465 ignoreWhitespace: true,
466 });
467 }).pipe(Effect.provide(layer));
468​
469 expect(hasCheckpointRefCallCount).toBe(0);
470 }),
471 );
472​
473 it.effect("fails when the thread is missing from the snapshot", () =>
474 Effect.gen(function* () {
475 const threadId = ThreadId.make("thread-missing");
476​
477 const checkpointStore: CheckpointStore.CheckpointStore["Service"] = {
478 isGitRepository: () => Effect.succeed(true),
479 captureCheckpoint: () => Effect.void,
480 hasCheckpointRef: () => Effect.succeed(true),
481 restoreCheckpoint: () => Effect.succeed(true),
482 diffCheckpoints: () => Effect.succeed(""),
483 deleteCheckpointRefs: () => Effect.void,
484 attributeCheckpointDiff: () => Effect.succeed(null),
485 };
486​
487 const layer = CheckpointDiffQuery.layer.pipe(
488 Layer.provideMerge(Layer.succeed(CheckpointStore.CheckpointStore, checkpointStore)),
489 Layer.provideMerge(
490 Layer.succeed(ProjectionSnapshotQuery.ProjectionSnapshotQuery, {
491 getCommandReadModel: () =>
492 Effect.die("CheckpointDiffQuery should not request the command read model"),
493 getSnapshot: () =>
494 Effect.die("CheckpointDiffQuery should not request the full orchestration snapshot"),
495 getShellSnapshot: () =>
496 Effect.die("CheckpointDiffQuery should not request the orchestration shell snapshot"),
497 getArchivedShellSnapshot: () =>
498 Effect.die("CheckpointDiffQuery should not request archived shell snapshots"),
499 getSnapshotSequence: () => Effect.succeed({ snapshotSequence: 0 }),
500 getCounts: () => Effect.succeed({ projectCount: 0, threadCount: 0 }),
501 getActiveProjectByWorkspaceRoot: () => Effect.succeed(Option.none()),
502 getProjectShellById: () => Effect.succeed(Option.none()),
503 getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),
504 getThreadCheckpointContext: () => Effect.succeed(Option.none()),
505 getFullThreadDiffContext: () => Effect.succeed(Option.none()),
506 getThreadShellById: () => Effect.succeed(Option.none()),
507 getThreadDetailById: () => Effect.succeed(Option.none()),
508 getThreadDetailSnapshot: () => Effect.succeed(Option.none()),
509 }),
510 ),
511 );
512​
513 const error = yield* Effect.gen(function* () {
514 const query = yield* CheckpointDiffQuery.CheckpointDiffQuery;
515 return yield* query.getTurnDiff({
516 threadId,
517 fromTurnCount: 0,
518 toTurnCount: 1,
519 });
520 }).pipe(Effect.provide(layer), Effect.flip);
521​
522 expect(error).toBeInstanceOf(CheckpointThreadNotFoundError);
523 expect(error).toMatchObject({
524 operation: "CheckpointDiffQuery.getTurnDiff",
525 threadId,
526 });
527 expect(error.message).toBe(
528 "Checkpoint invariant violation in CheckpointDiffQuery.getTurnDiff: Thread 'thread-missing' not found.",
529 );
530 }),
531 );
532});