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

CheckpointDiffQuery.test.ts

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