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

CheckpointDiffQuery.test.ts

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