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

CheckpointDiffQuery.test.ts

195 lines6.6 KB

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 * as CheckpointStore from "./CheckpointStore.ts";
12import {
13 CheckpointRefUnavailableError,
14 CheckpointThreadNotFoundError,
15 CheckpointTurnRangeUnavailableError,
16} from "./Errors.ts";
17​
18const threadId = ThreadId.make("thread:checkpoint-diff-v2");
19const firstRunId = RunId.make("run:checkpoint-diff-v2:1");
20const secondRunId = RunId.make("run:checkpoint-diff-v2:2");
21const firstScopeId = CheckpointScopeId.make("scope:checkpoint-diff-v2:1");
22const secondScopeId = CheckpointScopeId.make("scope:checkpoint-diff-v2:2");
23const secondRef = CheckpointRef.make("refs/t3/test/second");
24​
25function makeProjection(): ProjectionCheckpointContext {
26 return {
27 runs: [
28 { id: firstRunId, ordinal: 1, status: "completed" },
29 { id: secondRunId, ordinal: 2, status: "completed" },
30 ],
31 checkpointScopes: [
32 { id: firstScopeId, runId: firstRunId, kind: "root_run", cwd: "/repo" },
33 { id: secondScopeId, runId: secondRunId, kind: "root_run", cwd: "/repo" },
34 ],
35 checkpoints: [
36 {
37 scopeId: secondScopeId,
38 runId: secondRunId,
39 appRunOrdinal: 2,
40 status: "ready",
41 ref: secondRef,
42 },
43 ],
44 };
46​
47function makeLayer(input: {
48 readonly projection: Effect.Effect<ProjectionCheckpointContext, OrchestratorProjectionError>;
49 readonly diffCheckpoints?: CheckpointStore.CheckpointStore["Service"]["diffCheckpoints"];
50}) {
51 return CheckpointDiffQuery.layer.pipe(
52 Layer.provide(
53 Layer.mergeAll(
54 Layer.mock(ThreadManagement.ThreadManagementService)({
55 getCheckpointContext: () => input.projection,
56 }),
57 Layer.mock(CheckpointStore.CheckpointStore)({
58 diffCheckpoints: input.diffCheckpoints ?? (() => Effect.succeed("diff")),
59 }),
60 ),
61 ),
62 );
64​
65it.effect("computes V2 run diffs from projected checkpoint scopes", () => {
66 const diffCheckpoints = vi.fn((_input: CheckpointStore.DiffCheckpointsInput) =>
67 Effect.succeed("diff --git a/file b/file"),
68 );
69 const layer = makeLayer({ projection: Effect.succeed(makeProjection()), diffCheckpoints });
70​
71 return Effect.gen(function* () {
72 const query = yield* CheckpointDiffQuery.CheckpointDiffQuery;
73 const result = yield* query.getFullThreadDiff({ threadId, toTurnCount: 2 });
74​
75 assert.deepEqual(result, {
76 threadId,
77 fromTurnCount: 0,
78 toTurnCount: 2,
79 diff: "diff --git a/file b/file",
80 });
81 assert.deepEqual(diffCheckpoints.mock.calls[0]?.[0], {
82 cwd: "/repo",
83 fromCheckpointRef: checkpointRefForScopeOrdinal({
84 scopeId: firstScopeId,
85 ordinalWithinScope: 0,
86 }),
87 toCheckpointRef: secondRef,
88 fallbackFromToHead: false,
89 ignoreWhitespace: true,
90 });
91 }).pipe(Effect.provide(layer));
92});
93​
94it.effect("preserves the typed missing-thread error contract", () => {
95 const layer = makeLayer({
96 projection: Effect.fail(new OrchestratorProjectionError({ threadId })),
97 });
98​
99 return Effect.gen(function* () {
100 const query = yield* CheckpointDiffQuery.CheckpointDiffQuery;
101 const error = yield* query
102 .getTurnDiff({ threadId, fromTurnCount: 0, toTurnCount: 1 })
103 .pipe(Effect.flip);
104​
105 assert.instanceOf(error, CheckpointThreadNotFoundError);
106 assert.deepEqual(
107 { operation: error.operation, threadId: error.threadId },
108 { operation: "CheckpointDiffQuery.getTurnDiff", threadId },
109 );
110 }).pipe(Effect.provide(layer));
111});
112​
113it.effect("preserves the typed unavailable-range error contract", () => {
114 const layer = makeLayer({ projection: Effect.succeed(makeProjection()) });
115​
116 return Effect.gen(function* () {
117 const query = yield* CheckpointDiffQuery.CheckpointDiffQuery;
118 const error = yield* query
119 .getTurnDiff({ threadId, fromTurnCount: 0, toTurnCount: 3 })
120 .pipe(Effect.flip);
121​
122 assert.instanceOf(error, CheckpointTurnRangeUnavailableError);
123 assert.deepEqual(
124 {
125 requestedTurnCount: error.requestedTurnCount,
126 availableTurnCount: error.availableTurnCount,
127 },
128 { requestedTurnCount: 3, availableTurnCount: 2 },
129 );
130 }).pipe(Effect.provide(layer));
131});
132​
133it.effect("excludes ready checkpoints from rolled-back runs", () => {
134 const projection = makeProjection();
135 const layer = makeLayer({
136 projection: Effect.succeed({
137 ...projection,
138 runs: projection.runs.map((run) =>
139 run.id === secondRunId ? { ...run, status: "rolled_back" as const } : run,
140 ),
141 checkpoints: [
142 {
143 ...projection.checkpoints[0]!,
144 scopeId: firstScopeId,
145 runId: firstRunId,
146 appRunOrdinal: 1,
147 ref: CheckpointRef.make("refs/t3/test/first"),
148 },
149 ...projection.checkpoints,
150 ],
151 }),
152 });
153​
154 return Effect.gen(function* () {
155 const query = yield* CheckpointDiffQuery.CheckpointDiffQuery;
156 const error = yield* query
157 .getTurnDiff({ threadId, fromTurnCount: 0, toTurnCount: 2 })
158 .pipe(Effect.flip);
159​
160 assert.instanceOf(error, CheckpointTurnRangeUnavailableError);
161 assert.deepEqual(
162 {
163 requestedTurnCount: error.requestedTurnCount,
164 availableTurnCount: error.availableTurnCount,
165 },
166 { requestedTurnCount: 2, availableTurnCount: 1 },
167 );
168 }).pipe(Effect.provide(layer));
169});
170​
171it.effect("preserves the typed missing-baseline-ref error contract", () => {
172 const projection = makeProjection();
173 const layer = makeLayer({
174 projection: Effect.succeed({
175 ...projection,
176 checkpointScopes: projection.checkpointScopes.map((scope) => ({
177 ...scope,
178 kind: "tool" as const,
179 })),
180 }),
181 });
182​
183 return Effect.gen(function* () {
184 const query = yield* CheckpointDiffQuery.CheckpointDiffQuery;
185 const error = yield* query
186 .getTurnDiff({ threadId, fromTurnCount: 0, toTurnCount: 2 })
187 .pipe(Effect.flip);
188​
189 assert.instanceOf(error, CheckpointRefUnavailableError);
190 assert.deepEqual(
191 { checkpoint: error.checkpoint, turnCount: error.turnCount },
192 { checkpoint: "from", turnCount: 0 },
193 );
194 }).pipe(Effect.provide(layer));
195});