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

CheckpointDiffQuery.test.ts

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