Open History to see the last commit
1import { assert, it, vi } from "@effect/vitest";
2import { CheckpointRef, CheckpointScopeId, RunId, ThreadId } from "@t3tools/contracts";
3import * as Effect from "effect/Effect";
4import * as Layer from "effect/Layer";
5
6import { checkpointRefForScopeOrdinal } from "../orchestration-v2/CheckpointService.ts";
7import { OrchestratorProjectionError } from "../orchestration-v2/Orchestrator.ts";
8import type { ProjectionCheckpointContext } from "../orchestration-v2/ProjectionStore.ts";
9import * as ThreadManagement from "../orchestration-v2/ThreadManagementService.ts";
10import * as CheckpointDiffQuery from "./CheckpointDiffQuery.ts";
11import { checkpointStartRef } from "./Utils.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 appRunOrdinal: 2,
41 status: "ready",
42 ref: secondRef,
43 },
44 ],
45 };
46}
47
48function makeLayer(input: {
49 readonly projection: Effect.Effect<ProjectionCheckpointContext, OrchestratorProjectionError>;
50 readonly hasStartSnapshot?: boolean;
51 readonly diffCheckpoints?: CheckpointStore.CheckpointStore["Service"]["diffCheckpoints"];
52}) {
53 return CheckpointDiffQuery.layer.pipe(
54 Layer.provide(
55 Layer.mergeAll(
56 Layer.mock(ThreadManagement.ThreadManagementService)({
57 getCheckpointContext: () => input.projection,
58 }),
59 Layer.mock(CheckpointStore.CheckpointStore)({
60 hasCheckpointRef: () => Effect.succeed(input.hasStartSnapshot ?? false),
61 diffCheckpoints: input.diffCheckpoints ?? (() => Effect.succeed("diff")),
62 }),
63 ),
64 ),
65 );
66}
67
68it.effect("computes V2 run diffs from projected checkpoint scopes", () => {
69 const diffCheckpoints = vi.fn((_input: CheckpointStore.DiffCheckpointsInput) =>
70 Effect.succeed("diff --git a/file b/file"),
71 );
72 const layer = makeLayer({
73 projection: Effect.succeed(makeProjection()),
74 diffCheckpoints,
75 hasStartSnapshot: true,
76 });
77
78 return Effect.gen(function* () {
79 const query = yield* CheckpointDiffQuery.CheckpointDiffQuery;
80 const result = yield* query.getFullThreadDiff({ threadId, toTurnCount: 2 });
81
82 assert.deepEqual(result, {
83 threadId,
84 fromTurnCount: 0,
85 toTurnCount: 2,
86 diff: "diff --git a/file b/file",
87 });
88 assert.deepEqual(diffCheckpoints.mock.calls[0]?.[0], {
89 cwd: "/repo",
90 fromCheckpointRef: checkpointRefForScopeOrdinal({
91 scopeId: firstScopeId,
92 ordinalWithinScope: 0,
93 }),
94 toCheckpointRef: secondRef,
95 fallbackFromToHead: false,
96 ignoreWhitespace: true,
97 });
98 }).pipe(Effect.provide(layer));
99});
100
101it.effect("preserves the typed missing-thread error contract", () => {
102 const layer = makeLayer({
103 projection: Effect.fail(new OrchestratorProjectionError({ threadId })),
104 });
105
106 return Effect.gen(function* () {
107 const query = yield* CheckpointDiffQuery.CheckpointDiffQuery;
108 const error = yield* query
109 .getTurnDiff({ threadId, fromTurnCount: 0, toTurnCount: 1 })
110 .pipe(Effect.flip);
111
112 assert.instanceOf(error, CheckpointThreadNotFoundError);
113 assert.deepEqual(
114 { operation: error.operation, threadId: error.threadId },
115 { operation: "CheckpointDiffQuery.getTurnDiff", threadId },
116 );
117 }).pipe(Effect.provide(layer));
118});
119
120it.effect("preserves the typed unavailable-range error contract", () => {
121 const layer = makeLayer({ projection: Effect.succeed(makeProjection()) });
122
123 return Effect.gen(function* () {
124 const query = yield* CheckpointDiffQuery.CheckpointDiffQuery;
125 const error = yield* query
126 .getTurnDiff({ threadId, fromTurnCount: 0, toTurnCount: 3 })
127 .pipe(Effect.flip);
128
129 assert.instanceOf(error, CheckpointTurnRangeUnavailableError);
130 assert.deepEqual(
131 {
132 requestedTurnCount: error.requestedTurnCount,
133 availableTurnCount: error.availableTurnCount,
134 },
135 { requestedTurnCount: 3, availableTurnCount: 2 },
136 );
137 }).pipe(Effect.provide(layer));
138});
139
140it.effect("excludes ready checkpoints from rolled-back runs", () => {
141 const projection = makeProjection();
142 const layer = makeLayer({
143 projection: Effect.succeed({
144 ...projection,
145 runs: projection.runs.map((run) =>
146 run.id === secondRunId ? { ...run, status: "rolled_back" as const } : run,
147 ),
148 checkpoints: [
149 {
150 ...projection.checkpoints[0]!,
151 scopeId: firstScopeId,
152 runId: firstRunId,
153 appRunOrdinal: 1,
154 ref: CheckpointRef.make("refs/t3/test/first"),
155 },
156 ...projection.checkpoints,
157 ],
158 }),
159 });
160
161 return Effect.gen(function* () {
162 const query = yield* CheckpointDiffQuery.CheckpointDiffQuery;
163 const error = yield* query
164 .getTurnDiff({ threadId, fromTurnCount: 0, toTurnCount: 2 })
165 .pipe(Effect.flip);
166
167 assert.instanceOf(error, CheckpointTurnRangeUnavailableError);
168 assert.deepEqual(
169 {
170 requestedTurnCount: error.requestedTurnCount,
171 availableTurnCount: error.availableTurnCount,
172 },
173 { requestedTurnCount: 2, availableTurnCount: 1 },
174 );
175 }).pipe(Effect.provide(layer));
176});
177
178it.effect("preserves the typed missing-baseline-ref error contract", () => {
179 const projection = makeProjection();
180 const layer = makeLayer({
181 projection: Effect.succeed({
182 ...projection,
183 checkpointScopes: projection.checkpointScopes.map((scope) => ({
184 ...scope,
185 kind: "tool" as const,
186 })),
187 }),
188 });
189
190 return Effect.gen(function* () {
191 const query = yield* CheckpointDiffQuery.CheckpointDiffQuery;
192 const error = yield* query
193 .getTurnDiff({ threadId, fromTurnCount: 0, toTurnCount: 2 })
194 .pipe(Effect.flip);
195
196 assert.instanceOf(error, CheckpointRefUnavailableError);
197 assert.deepEqual(
198 { checkpoint: error.checkpoint, turnCount: error.turnCount },
199 { checkpoint: "from", turnCount: 0 },
200 );
201 }).pipe(Effect.provide(layer));
202});
203
204for (const hasStartSnapshot of [false, true]) {
205 it.effect(
206 `uses ${hasStartSnapshot ? "fresh" : "legacy"} baseline for an individual V2 turn`,
207 () => {
208 const projection = makeProjection();
209 const firstRef = checkpointRefForScopeOrdinal({
210 scopeId: firstScopeId,
211 ordinalWithinScope: 1,
212 });
213 const diffCheckpoints = vi.fn((_input: CheckpointStore.DiffCheckpointsInput) =>
214 Effect.succeed("patch"),
215 );
216 const layer = makeLayer({
217 hasStartSnapshot,
218 diffCheckpoints,
219 projection: Effect.succeed({
220 ...projection,
221 checkpoints: [
222 ...projection.checkpoints,
223 {
224 scopeId: firstScopeId,
225 runId: firstRunId,
226 appRunOrdinal: 1,
227 status: "ready",
228 ref: firstRef,
229 },
230 ],
231 }),
232 });
233 return Effect.gen(function* () {
234 const query = yield* CheckpointDiffQuery.CheckpointDiffQuery;
235 yield* query.getTurnDiff({ threadId, fromTurnCount: 1, toTurnCount: 2 });
236 assert.equal(
237 diffCheckpoints.mock.calls[0]?.[0].fromCheckpointRef,
238 hasStartSnapshot ? checkpointStartRef(secondRef) : firstRef,
239 );
240 }).pipe(Effect.provide(layer));
241 },
242 );
243}