Open History to see the last commit
1import { assert, it, vi } from "@effect/vitest";
2import {
3 CheckpointRef,
4 CheckpointScopeId,
5 RunId,
6 ThreadId,
7 type OrchestrationV2ThreadProjection,
8} from "@t3tools/contracts";
9import * as Effect from "effect/Effect";
10import * as Layer from "effect/Layer";
11
12import { checkpointRefForScopeOrdinal } from "../orchestration-v2/CheckpointService.ts";
13import { OrchestratorProjectionError } from "../orchestration-v2/Orchestrator.ts";
14import * as ThreadManagement from "../orchestration-v2/ThreadManagementService.ts";
15import * as CheckpointDiffQuery from "./CheckpointDiffQuery.ts";
16import * as CheckpointStore from "./CheckpointStore.ts";
17import {
18 CheckpointRefUnavailableError,
19 CheckpointThreadNotFoundError,
20 CheckpointTurnRangeUnavailableError,
21} from "./Errors.ts";
22
23const threadId = ThreadId.make("thread:checkpoint-diff-v2");
24const firstRunId = RunId.make("run:checkpoint-diff-v2:1");
25const secondRunId = RunId.make("run:checkpoint-diff-v2:2");
26const firstScopeId = CheckpointScopeId.make("scope:checkpoint-diff-v2:1");
27const secondScopeId = CheckpointScopeId.make("scope:checkpoint-diff-v2:2");
28const secondRef = CheckpointRef.make("refs/t3/test/second");
29
30function makeProjection(): OrchestrationV2ThreadProjection {
31 return {
32 runs: [
33 { id: firstRunId, ordinal: 1, status: "completed" },
34 { id: secondRunId, ordinal: 2, status: "completed" },
35 ],
36 checkpointScopes: [
37 { id: firstScopeId, runId: firstRunId, kind: "root_run", cwd: "/repo" },
38 { id: secondScopeId, runId: secondRunId, kind: "root_run", cwd: "/repo" },
39 ],
40 checkpoints: [
41 {
42 scopeId: secondScopeId,
43 runId: secondRunId,
44 appRunOrdinal: 2,
45 status: "ready",
46 ref: secondRef,
47 },
48 ],
49 } as unknown as OrchestrationV2ThreadProjection;
50}
51
52function makeLayer(input: {
53 readonly projection: Effect.Effect<OrchestrationV2ThreadProjection, OrchestratorProjectionError>;
54 readonly diffCheckpoints?: CheckpointStore.CheckpointStore["Service"]["diffCheckpoints"];
55}) {
56 return CheckpointDiffQuery.layer.pipe(
57 Layer.provide(
58 Layer.mergeAll(
59 Layer.mock(ThreadManagement.ThreadManagementService)({
60 getThreadProjection: () => input.projection,
61 }),
62 Layer.mock(CheckpointStore.CheckpointStore)({
63 diffCheckpoints: input.diffCheckpoints ?? (() => Effect.succeed("diff")),
64 }),
65 ),
66 ),
67 );
68}
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 = makeLayer({ 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: 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("preserves the typed missing-thread error contract", () => {
100 const layer = makeLayer({
101 projection: Effect.fail(new OrchestratorProjectionError({ threadId })),
102 });
103
104 return Effect.gen(function* () {
105 const query = yield* CheckpointDiffQuery.CheckpointDiffQuery;
106 const error = yield* query
107 .getTurnDiff({ threadId, fromTurnCount: 0, toTurnCount: 1 })
108 .pipe(Effect.flip);
109
110 assert.instanceOf(error, CheckpointThreadNotFoundError);
111 assert.deepEqual(
112 { operation: error.operation, threadId: error.threadId },
113 { operation: "CheckpointDiffQuery.getTurnDiff", threadId },
114 );
115 }).pipe(Effect.provide(layer));
116});
117
118it.effect("preserves the typed unavailable-range error contract", () => {
119 const layer = makeLayer({ projection: Effect.succeed(makeProjection()) });
120
121 return Effect.gen(function* () {
122 const query = yield* CheckpointDiffQuery.CheckpointDiffQuery;
123 const error = yield* query
124 .getTurnDiff({ threadId, fromTurnCount: 0, toTurnCount: 3 })
125 .pipe(Effect.flip);
126
127 assert.instanceOf(error, CheckpointTurnRangeUnavailableError);
128 assert.deepEqual(
129 {
130 requestedTurnCount: error.requestedTurnCount,
131 availableTurnCount: error.availableTurnCount,
132 },
133 { requestedTurnCount: 3, availableTurnCount: 2 },
134 );
135 }).pipe(Effect.provide(layer));
136});
137
138it.effect("excludes ready checkpoints from rolled-back runs", () => {
139 const projection = makeProjection();
140 const layer = makeLayer({
141 projection: Effect.succeed({
142 ...projection,
143 runs: projection.runs.map((run) =>
144 run.id === secondRunId ? { ...run, status: "rolled_back" as const } : run,
145 ),
146 checkpoints: [
147 {
148 ...projection.checkpoints[0]!,
149 scopeId: firstScopeId,
150 runId: firstRunId,
151 appRunOrdinal: 1,
152 ref: CheckpointRef.make("refs/t3/test/first"),
153 },
154 ...projection.checkpoints,
155 ],
156 }),
157 });
158
159 return Effect.gen(function* () {
160 const query = yield* CheckpointDiffQuery.CheckpointDiffQuery;
161 const error = yield* query
162 .getTurnDiff({ threadId, fromTurnCount: 0, toTurnCount: 2 })
163 .pipe(Effect.flip);
164
165 assert.instanceOf(error, CheckpointTurnRangeUnavailableError);
166 assert.deepEqual(
167 {
168 requestedTurnCount: error.requestedTurnCount,
169 availableTurnCount: error.availableTurnCount,
170 },
171 { requestedTurnCount: 2, availableTurnCount: 1 },
172 );
173 }).pipe(Effect.provide(layer));
174});
175
176it.effect("preserves the typed missing-baseline-ref error contract", () => {
177 const projection = makeProjection();
178 const layer = makeLayer({
179 projection: Effect.succeed({
180 ...projection,
181 checkpointScopes: projection.checkpointScopes.filter((scope) => scope.id !== firstScopeId),
182 }),
183 });
184
185 return Effect.gen(function* () {
186 const query = yield* CheckpointDiffQuery.CheckpointDiffQuery;
187 const error = yield* query
188 .getTurnDiff({ threadId, fromTurnCount: 0, toTurnCount: 2 })
189 .pipe(Effect.flip);
190
191 assert.instanceOf(error, CheckpointRefUnavailableError);
192 assert.deepEqual(
193 { checkpoint: error.checkpoint, turnCount: error.turnCount },
194 { checkpoint: "from", turnCount: 0 },
195 );
196 }).pipe(Effect.provide(layer));
197});