Open History to see the last commit
1import {
2 CheckpointRef,
3 DEFAULT_PROVIDER_INTERACTION_MODE,
4 ProjectId,
5 ThreadId,
6 TurnId,
7 type OrchestrationReadModel,
8} from "@t3tools/contracts";
9import { Effect, Layer } from "effect";
10import { describe, expect, it } from "vitest";
11
12import { ProjectionSnapshotQuery } from "../../orchestration/Services/ProjectionSnapshotQuery.ts";
13import { checkpointRefForThreadTurn } from "../Utils.ts";
14import { CheckpointDiffQueryLive } from "./CheckpointDiffQuery.ts";
15import { CheckpointStore, type CheckpointStoreShape } from "../Services/CheckpointStore.ts";
16import { CheckpointDiffQuery } from "../Services/CheckpointDiffQuery.ts";
17
18function makeSnapshot(input: {
19 readonly projectId: ProjectId;
20 readonly threadId: ThreadId;
21 readonly workspaceRoot: string;
22 readonly worktreePath: string | null;
23 readonly checkpointTurnCount: number;
24 readonly checkpointRef: CheckpointRef;
25}): OrchestrationReadModel {
26 return {
27 snapshotSequence: 0,
28 updatedAt: "2026-01-01T00:00:00.000Z",
29 projects: [
30 {
31 id: input.projectId,
32 title: "Project",
33 workspaceRoot: input.workspaceRoot,
34 defaultModel: null,
35 scripts: [],
36 createdAt: "2026-01-01T00:00:00.000Z",
37 updatedAt: "2026-01-01T00:00:00.000Z",
38 deletedAt: null,
39 },
40 ],
41 threads: [
42 {
43 id: input.threadId,
44 projectId: input.projectId,
45 title: "Thread",
46 model: "gpt-5-codex",
47 interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
48 runtimeMode: "full-access",
49 vcsBackend: "git",
50 refName: null,
51 refKind: null,
52 workspacePath: input.worktreePath,
53 branch: null,
54 worktreePath: input.worktreePath,
55 latestTurn: {
56 turnId: TurnId.makeUnsafe("turn-1"),
57 state: "completed",
58 requestedAt: "2026-01-01T00:00:00.000Z",
59 startedAt: "2026-01-01T00:00:00.000Z",
60 completedAt: "2026-01-01T00:00:00.000Z",
61 assistantMessageId: null,
62 },
63 createdAt: "2026-01-01T00:00:00.000Z",
64 updatedAt: "2026-01-01T00:00:00.000Z",
65 deletedAt: null,
66 messages: [],
67 activities: [],
68 proposedPlans: [],
69 checkpoints: [
70 {
71 turnId: TurnId.makeUnsafe("turn-1"),
72 checkpointTurnCount: input.checkpointTurnCount,
73 checkpointRef: input.checkpointRef,
74 status: "ready",
75 files: [],
76 assistantMessageId: null,
77 completedAt: "2026-01-01T00:00:00.000Z",
78 },
79 ],
80 session: null,
81 },
82 ],
83 };
84}
85
86describe("CheckpointDiffQueryLive", () => {
87 it("computes diffs using canonical turn-0 checkpoint refs", async () => {
88 const projectId = ProjectId.makeUnsafe("project-1");
89 const threadId = ThreadId.makeUnsafe("thread-1");
90 const toCheckpointRef = checkpointRefForThreadTurn(threadId, 1);
91 const hasCheckpointRefCalls: Array<CheckpointRef> = [];
92 const diffCheckpointsCalls: Array<{
93 readonly fromCheckpointRef: CheckpointRef;
94 readonly toCheckpointRef: CheckpointRef;
95 readonly cwd: string;
96 }> = [];
97
98 const snapshot = makeSnapshot({
99 projectId,
100 threadId,
101 workspaceRoot: "/tmp/workspace",
102 worktreePath: null,
103 checkpointTurnCount: 1,
104 checkpointRef: toCheckpointRef,
105 });
106
107 const checkpointStore: CheckpointStoreShape = {
108 isGitRepository: () => Effect.succeed(true),
109 captureCheckpoint: () => Effect.void,
110 hasCheckpointRef: ({ checkpointRef }) =>
111 Effect.sync(() => {
112 hasCheckpointRefCalls.push(checkpointRef);
113 return true;
114 }),
115 restoreCheckpoint: () => Effect.succeed(true),
116 diffCheckpoints: ({ fromCheckpointRef, toCheckpointRef, cwd }) =>
117 Effect.sync(() => {
118 diffCheckpointsCalls.push({ fromCheckpointRef, toCheckpointRef, cwd });
119 return "diff patch";
120 }),
121 deleteCheckpointRefs: () => Effect.void,
122 };
123
124 const layer = CheckpointDiffQueryLive.pipe(
125 Layer.provideMerge(Layer.succeed(CheckpointStore, checkpointStore)),
126 Layer.provideMerge(
127 Layer.succeed(ProjectionSnapshotQuery, {
128 getSnapshot: () => Effect.succeed(snapshot),
129 }),
130 ),
131 );
132
133 const result = await Effect.runPromise(
134 Effect.gen(function* () {
135 const query = yield* CheckpointDiffQuery;
136 return yield* query.getTurnDiff({
137 threadId,
138 fromTurnCount: 0,
139 toTurnCount: 1,
140 });
141 }).pipe(Effect.provide(layer)),
142 );
143
144 const expectedFromRef = checkpointRefForThreadTurn(threadId, 0);
145 expect(hasCheckpointRefCalls).toEqual([expectedFromRef, toCheckpointRef]);
146 expect(diffCheckpointsCalls).toEqual([
147 {
148 cwd: "/tmp/workspace",
149 fromCheckpointRef: expectedFromRef,
150 toCheckpointRef,
151 },
152 ]);
153 expect(result).toEqual({
154 threadId,
155 fromTurnCount: 0,
156 toTurnCount: 1,
157 diff: "diff patch",
158 });
159 });
160
161 it("fails when the thread is missing from the snapshot", async () => {
162 const threadId = ThreadId.makeUnsafe("thread-missing");
163
164 const checkpointStore: CheckpointStoreShape = {
165 isGitRepository: () => Effect.succeed(true),
166 captureCheckpoint: () => Effect.void,
167 hasCheckpointRef: () => Effect.succeed(true),
168 restoreCheckpoint: () => Effect.succeed(true),
169 diffCheckpoints: () => Effect.succeed(""),
170 deleteCheckpointRefs: () => Effect.void,
171 };
172
173 const layer = CheckpointDiffQueryLive.pipe(
174 Layer.provideMerge(Layer.succeed(CheckpointStore, checkpointStore)),
175 Layer.provideMerge(
176 Layer.succeed(ProjectionSnapshotQuery, {
177 getSnapshot: () =>
178 Effect.succeed({
179 snapshotSequence: 0,
180 projects: [],
181 threads: [],
182 updatedAt: "2026-01-01T00:00:00.000Z",
183 } satisfies OrchestrationReadModel),
184 }),
185 ),
186 );
187
188 await expect(
189 Effect.runPromise(
190 Effect.gen(function* () {
191 const query = yield* CheckpointDiffQuery;
192 return yield* query.getTurnDiff({
193 threadId,
194 fromTurnCount: 0,
195 toTurnCount: 1,
196 });
197 }).pipe(Effect.provide(layer)),
198 ),
199 ).rejects.toThrow("Thread 'thread-missing' not found.");
200 });
201});