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

CheckpointDiffQuery.test.ts

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