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

CheckpointDiffQuery.test.ts

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