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

CheckpointDiffQuery.test.ts

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