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 titleSummaryState: "missing",
47 model: "gpt-5-codex",
48 interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
49 runtimeMode: "full-access",
50 branch: null,
51 worktreePath: input.worktreePath,
52 latestTurn: {
53 turnId: TurnId.makeUnsafe("turn-1"),
54 state: "completed",
55 requestedAt: "2026-01-01T00:00:00.000Z",
56 startedAt: "2026-01-01T00:00:00.000Z",
57 completedAt: "2026-01-01T00:00:00.000Z",
58 assistantMessageId: null,
59 },
60 createdAt: "2026-01-01T00:00:00.000Z",
61 updatedAt: "2026-01-01T00:00:00.000Z",
62 deletedAt: null,
63 messages: [],
64 activities: [],
65 proposedPlans: [],
66 checkpoints: [
67 {
68 turnId: TurnId.makeUnsafe("turn-1"),
69 checkpointTurnCount: input.checkpointTurnCount,
70 checkpointRef: input.checkpointRef,
71 status: "ready",
72 files: [],
73 assistantMessageId: null,
74 completedAt: "2026-01-01T00:00:00.000Z",
75 },
76 ],
77 session: null,
78 },
79 ],
80 };
81}
82
83describe("CheckpointDiffQueryLive", () => {
84 it("computes diffs using canonical turn-0 checkpoint refs", async () => {
85 const projectId = ProjectId.makeUnsafe("project-1");
86 const threadId = ThreadId.makeUnsafe("thread-1");
87 const toCheckpointRef = checkpointRefForThreadTurn(threadId, 1);
88 const hasCheckpointRefCalls: Array<CheckpointRef> = [];
89 const diffCheckpointsCalls: Array<{
90 readonly fromCheckpointRef: CheckpointRef;
91 readonly toCheckpointRef: CheckpointRef;
92 readonly cwd: string;
93 }> = [];
94
95 const snapshot = makeSnapshot({
96 projectId,
97 threadId,
98 workspaceRoot: "/tmp/workspace",
99 worktreePath: null,
100 checkpointTurnCount: 1,
101 checkpointRef: toCheckpointRef,
102 });
103
104 const checkpointStore: CheckpointStoreShape = {
105 isGitRepository: () => Effect.succeed(true),
106 captureCheckpoint: () => Effect.void,
107 hasCheckpointRef: ({ checkpointRef }) =>
108 Effect.sync(() => {
109 hasCheckpointRefCalls.push(checkpointRef);
110 return true;
111 }),
112 restoreCheckpoint: () => Effect.succeed(true),
113 diffCheckpoints: ({ fromCheckpointRef, toCheckpointRef, cwd }) =>
114 Effect.sync(() => {
115 diffCheckpointsCalls.push({ fromCheckpointRef, toCheckpointRef, cwd });
116 return "diff patch";
117 }),
118 deleteCheckpointRefs: () => Effect.void,
119 };
120
121 const layer = CheckpointDiffQueryLive.pipe(
122 Layer.provideMerge(Layer.succeed(CheckpointStore, checkpointStore)),
123 Layer.provideMerge(
124 Layer.succeed(ProjectionSnapshotQuery, {
125 getSnapshot: () => Effect.succeed(snapshot),
126 }),
127 ),
128 );
129
130 const result = await Effect.runPromise(
131 Effect.gen(function* () {
132 const query = yield* CheckpointDiffQuery;
133 return yield* query.getTurnDiff({
134 threadId,
135 fromTurnCount: 0,
136 toTurnCount: 1,
137 });
138 }).pipe(Effect.provide(layer)),
139 );
140
141 const expectedFromRef = checkpointRefForThreadTurn(threadId, 0);
142 expect(hasCheckpointRefCalls).toEqual([expectedFromRef, toCheckpointRef]);
143 expect(diffCheckpointsCalls).toEqual([
144 {
145 cwd: "/tmp/workspace",
146 fromCheckpointRef: expectedFromRef,
147 toCheckpointRef,
148 },
149 ]);
150 expect(result).toEqual({
151 threadId,
152 fromTurnCount: 0,
153 toTurnCount: 1,
154 diff: "diff patch",
155 });
156 });
157
158 it("fails when the thread is missing from the snapshot", async () => {
159 const threadId = ThreadId.makeUnsafe("thread-missing");
160
161 const checkpointStore: CheckpointStoreShape = {
162 isGitRepository: () => Effect.succeed(true),
163 captureCheckpoint: () => Effect.void,
164 hasCheckpointRef: () => Effect.succeed(true),
165 restoreCheckpoint: () => Effect.succeed(true),
166 diffCheckpoints: () => Effect.succeed(""),
167 deleteCheckpointRefs: () => Effect.void,
168 };
169
170 const layer = CheckpointDiffQueryLive.pipe(
171 Layer.provideMerge(Layer.succeed(CheckpointStore, checkpointStore)),
172 Layer.provideMerge(
173 Layer.succeed(ProjectionSnapshotQuery, {
174 getSnapshot: () =>
175 Effect.succeed({
176 snapshotSequence: 0,
177 projects: [],
178 threads: [],
179 updatedAt: "2026-01-01T00:00:00.000Z",
180 } satisfies OrchestrationReadModel),
181 }),
182 ),
183 );
184
185 await expect(
186 Effect.runPromise(
187 Effect.gen(function* () {
188 const query = yield* CheckpointDiffQuery;
189 return yield* query.getTurnDiff({
190 threadId,
191 fromTurnCount: 0,
192 toTurnCount: 1,
193 });
194 }).pipe(Effect.provide(layer)),
195 ),
196 ).rejects.toThrow("Thread 'thread-missing' not found.");
197 });
198});