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

CheckpointDiffQuery.test.ts

312 lines10.9 KB

1import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
2import { assert, it, vi } from "@effect/vitest";
3import { CheckpointRef, CheckpointScopeId, RunId, ThreadId } from "@t3tools/contracts";
4import * as Effect from "effect/Effect";
5import * as Layer from "effect/Layer";
6​
7import { checkpointRefForScopeOrdinal } from "../orchestration-v2/CheckpointService.ts";
8import { OrchestratorProjectionError } from "../orchestration-v2/Orchestrator.ts";
9import type { ProjectionCheckpointContext } from "../orchestration-v2/ProjectionStore.ts";
10import * as ThreadManagement from "../orchestration-v2/ThreadManagementService.ts";
11import * as CheckpointDiffQuery from "./CheckpointDiffQuery.ts";
12import * as CheckpointStore from "./CheckpointStore.ts";
13import {
14 CheckpointRefUnavailableError,
15 CheckpointThreadNotFoundError,
16 CheckpointTurnRangeUnavailableError,
17} from "./Errors.ts";
18​
19const threadId = ThreadId.make("thread:checkpoint-diff-v2");
20const firstRunId = RunId.make("run:checkpoint-diff-v2:1");
21const secondRunId = RunId.make("run:checkpoint-diff-v2:2");
22const firstScopeId = CheckpointScopeId.make("scope:checkpoint-diff-v2:1");
23const secondScopeId = CheckpointScopeId.make("scope:checkpoint-diff-v2:2");
24const secondRef = CheckpointRef.make("refs/t3/test/second");
25​
26function makeProjection(): ProjectionCheckpointContext {
27 return {
28 runs: [
29 { id: firstRunId, ordinal: 1, status: "completed" },
30 { id: secondRunId, ordinal: 2, status: "completed" },
31 ],
32 checkpointScopes: [
33 { id: firstScopeId, runId: firstRunId, kind: "root_run", cwd: "/repo" },
34 { id: secondScopeId, runId: secondRunId, kind: "root_run", cwd: "/repo" },
35 ],
36 checkpoints: [
37 {
38 scopeId: secondScopeId,
39 runId: secondRunId,
40 ordinalWithinScope: 1,
41 appRunOrdinal: 2,
42 status: "ready",
43 ref: secondRef,
44 },
45 ],
46 };
48​
49function layerFor(input: {
50 readonly projection: Effect.Effect<ProjectionCheckpointContext, OrchestratorProjectionError>;
51 readonly diffCheckpoints?: CheckpointStore.CheckpointStore["Service"]["diffCheckpoints"];
52 readonly authoredPaths?: ReadonlySet<string>;
53}) {
54 return CheckpointDiffQuery.layer.pipe(
55 Layer.provide(
56 Layer.mergeAll(
57 Layer.mock(ThreadManagement.ThreadManagementService)({
58 getCheckpointContext: () => input.projection,
59 }),
60 Layer.mock(CheckpointStore.CheckpointStore)({
61 diffCheckpoints: input.diffCheckpoints ?? (() => Effect.succeed("diff")),
62 listAuthoredPaths: () => Effect.succeed(input.authoredPaths ?? null),
63 }),
64 ),
65 ),
66 Layer.provideMerge(NodeCrypto.layer),
67 );
69​
70it.effect("computes V2 run diffs from projected checkpoint scopes", () => {
71 const diffCheckpoints = vi.fn((_input: CheckpointStore.DiffCheckpointsInput) =>
72 Effect.succeed("diff --git a/file b/file"),
73 );
74 const layer = layerFor({ projection: Effect.succeed(makeProjection()), diffCheckpoints });
75​
76 return Effect.gen(function* () {
77 const query = yield* CheckpointDiffQuery.CheckpointDiffQuery;
78 const result = yield* query.getFullThreadDiff({ threadId, toTurnCount: 2 });
79​
80 assert.deepEqual(result, {
81 threadId,
82 fromTurnCount: 0,
83 toTurnCount: 2,
84 diff: "diff --git a/file b/file",
85 });
86 assert.deepEqual(diffCheckpoints.mock.calls[0]?.[0], {
87 cwd: "/repo",
88 fromCheckpointRef: yield* checkpointRefForScopeOrdinal({
89 scopeId: firstScopeId,
90 ordinalWithinScope: 0,
91 }),
92 toCheckpointRef: secondRef,
93 fallbackFromToHead: false,
94 ignoreWhitespace: true,
95 });
96 }).pipe(Effect.provide(layer));
97});
98​
99it.effect("leaves Git imports out of large turn diffs", () => {
100 const ownPaths = Array.from({ length: 1_000 }, (_, index) => `src/module-${index}/file.ts`);
101 const diffCheckpoints = vi.fn((input: CheckpointStore.DiffCheckpointsInput) =>
102 Effect.succeed(
103 input.format === "numstat"
104 ? [...ownPaths, "upstream.ts"].map((path) => `1\t0\t${path}\0`).join("")
105 : (input.filePaths ?? ["all"]).join("\n"),
106 ),
107 );
108 const layer = layerFor({
109 projection: Effect.succeed(makeProjection()),
110 diffCheckpoints,
111 authoredPaths: new Set(ownPaths),
112 });
113​
114 return Effect.gen(function* () {
115 const query = yield* CheckpointDiffQuery.CheckpointDiffQuery;
116 yield* query.getFullThreadDiff({ threadId, toTurnCount: 2 });
117​
118 const patchPaths = diffCheckpoints.mock.calls
119 .map(([call]) => call)
120 .filter((call) => call.format !== "numstat")
121 .map((call) => call.filePaths ?? ["all"]);
122 assert.isAbove(patchPaths.length, 1);
123 assert.sameMembers(patchPaths.flat(), ownPaths);
124 }).pipe(Effect.provide(layer));
125});
126​
127it.effect("preserves the typed missing-thread error contract", () => {
128 const layer = layerFor({
129 projection: Effect.fail(new OrchestratorProjectionError({ threadId })),
130 });
131​
132 return Effect.gen(function* () {
133 const query = yield* CheckpointDiffQuery.CheckpointDiffQuery;
134 const error = yield* query
135 .getTurnDiff({ threadId, fromTurnCount: 0, toTurnCount: 1 })
136 .pipe(Effect.flip);
137​
138 assert.instanceOf(error, CheckpointThreadNotFoundError);
139 assert.deepEqual(
140 { operation: error.operation, threadId: error.threadId },
141 { operation: "CheckpointDiffQuery.getTurnDiff", threadId },
142 );
143 }).pipe(Effect.provide(layer));
144});
145​
146it.effect("preserves the typed unavailable-range error contract", () => {
147 const layer = layerFor({ projection: Effect.succeed(makeProjection()) });
148​
149 return Effect.gen(function* () {
150 const query = yield* CheckpointDiffQuery.CheckpointDiffQuery;
151 const error = yield* query
152 .getTurnDiff({ threadId, fromTurnCount: 0, toTurnCount: 3 })
153 .pipe(Effect.flip);
154​
155 assert.instanceOf(error, CheckpointTurnRangeUnavailableError);
156 assert.deepEqual(
157 {
158 requestedTurnCount: error.requestedTurnCount,
159 availableTurnCount: error.availableTurnCount,
160 },
161 { requestedTurnCount: 3, availableTurnCount: 2 },
162 );
163 }).pipe(Effect.provide(layer));
164});
165​
166it.effect("excludes ready checkpoints from rolled-back runs", () => {
167 const projection = makeProjection();
168 const layer = layerFor({
169 projection: Effect.succeed({
170 ...projection,
171 runs: projection.runs.map((run) =>
172 run.id === secondRunId ? { ...run, status: "rolled_back" as const } : run,
173 ),
174 checkpoints: [
175 {
176 ...projection.checkpoints[0]!,
177 scopeId: firstScopeId,
178 runId: firstRunId,
179 appRunOrdinal: 1,
180 ref: CheckpointRef.make("refs/t3/test/first"),
181 },
182 ...projection.checkpoints,
183 ],
184 }),
185 });
186​
187 return Effect.gen(function* () {
188 const query = yield* CheckpointDiffQuery.CheckpointDiffQuery;
189 const error = yield* query
190 .getTurnDiff({ threadId, fromTurnCount: 0, toTurnCount: 2 })
191 .pipe(Effect.flip);
192​
193 assert.instanceOf(error, CheckpointTurnRangeUnavailableError);
194 assert.deepEqual(
195 {
196 requestedTurnCount: error.requestedTurnCount,
197 availableTurnCount: error.availableTurnCount,
198 },
199 { requestedTurnCount: 2, availableTurnCount: 1 },
200 );
201 }).pipe(Effect.provide(layer));
202});
203​
204function makeProjectionAfterStoppedRun(stoppedRunCheckpoint: boolean) {
205 const thirdRunId = RunId.make("run:checkpoint-diff-v2:3");
206 const checkpoint = (
207 ordinalWithinScope: number,
208 runId: RunId | null,
209 ): ProjectionCheckpointContext["checkpoints"][number] => ({
210 scopeId: firstScopeId,
211 runId,
212 ordinalWithinScope,
213 appRunOrdinal: runId === null ? null : ordinalWithinScope,
214 status: "ready",
215 ref: CheckpointRef.make(`refs/t3/test/ordinal/${ordinalWithinScope}`),
216 });
217 return {
218 runs: [
219 { id: firstRunId, ordinal: 1, status: "completed" },
220 { id: secondRunId, ordinal: 2, status: "interrupted" },
221 { id: thirdRunId, ordinal: 3, status: "completed" },
222 ],
223 checkpointScopes: [
224 { id: firstScopeId, runId: thirdRunId, kind: "root_run", cwd: "/repo" },
225 { id: secondScopeId, runId: secondRunId, kind: "tool", cwd: "/repo" },
226 ],
227 checkpoints: [
228 {
229 ...checkpoint(2, null),
230 scopeId: secondScopeId,
231 ref: CheckpointRef.make("refs/t3/test/tool/2"),
232 },
233 checkpoint(0, null),
234 checkpoint(1, firstRunId),
235 checkpoint(2, stoppedRunCheckpoint ? secondRunId : null),
236 checkpoint(3, thirdRunId),
237 ],
238 } satisfies ProjectionCheckpointContext;
240​
241it.effect.each([false, true])(
242 "diffs the turn after a stopped run from its parent checkpoint (stopped run captured: %s)",
243 (stoppedRunCheckpoint) => {
244 const diffCheckpoints = vi.fn((_input: CheckpointStore.DiffCheckpointsInput) =>
245 Effect.succeed("diff --git a/file b/file"),
246 );
247 const layer = layerFor({
248 projection: Effect.succeed(makeProjectionAfterStoppedRun(stoppedRunCheckpoint)),
249 diffCheckpoints,
250 });
251​
252 return Effect.gen(function* () {
253 const query = yield* CheckpointDiffQuery.CheckpointDiffQuery;
254 const result = yield* query.getTurnDiff({ threadId, fromTurnCount: 2, toTurnCount: 3 });
255​
256 assert.strictEqual(result.diff, "diff --git a/file b/file");
257 yield* query.getTurnDiff({ threadId, fromTurnCount: 1, toTurnCount: 3 });
258 yield* query.getFullThreadDiff({ threadId, toTurnCount: 3 });
259 assert.deepEqual(
260 diffCheckpoints.mock.calls.map(([input]) => ({
261 from: input.fromCheckpointRef,
262 to: input.toCheckpointRef,
263 })),
264 [
265 { from: "refs/t3/test/ordinal/2", to: "refs/t3/test/ordinal/3" },
266 { from: "refs/t3/test/ordinal/1", to: "refs/t3/test/ordinal/3" },
267 {
268 from: yield* checkpointRefForScopeOrdinal({
269 scopeId: firstScopeId,
270 ordinalWithinScope: 0,
271 }),
272 to: "refs/t3/test/ordinal/3",
273 },
274 ],
275 );
276 const stoppedTurnError = yield* query
277 .getTurnDiff({ threadId, fromTurnCount: 1, toTurnCount: 2 })
278 .pipe(Effect.flip);
279 assert.instanceOf(stoppedTurnError, CheckpointRefUnavailableError);
280 assert.deepEqual(
281 { checkpoint: stoppedTurnError.checkpoint, turnCount: stoppedTurnError.turnCount },
282 { checkpoint: "to", turnCount: 2 },
283 );
284 }).pipe(Effect.provide(layer));
285 },
286);
287​
288it.effect("preserves the typed missing-baseline-ref error contract", () => {
289 const projection = makeProjection();
290 const layer = layerFor({
291 projection: Effect.succeed({
292 ...projection,
293 checkpointScopes: projection.checkpointScopes.map((scope) => ({
294 ...scope,
295 kind: "tool" as const,
296 })),
297 }),
298 });
299​
300 return Effect.gen(function* () {
301 const query = yield* CheckpointDiffQuery.CheckpointDiffQuery;
302 const error = yield* query
303 .getTurnDiff({ threadId, fromTurnCount: 0, toTurnCount: 2 })
304 .pipe(Effect.flip);
305​
306 assert.instanceOf(error, CheckpointRefUnavailableError);
307 assert.deepEqual(
308 { checkpoint: error.checkpoint, turnCount: error.turnCount },
309 { checkpoint: "from", turnCount: 0 },
310 );
311 }).pipe(Effect.provide(layer));
312});