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

CheckpointDiffQuery.test.ts

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