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

CheckpointScopeOwnership.test.ts

244 lines8.3 KB

1import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import { assert, it } from "@effect/vitest";
4import * as DateTime from "effect/DateTime";
5import * as Effect from "effect/Effect";
6import * as Layer from "effect/Layer";
7import {
8 CheckpointId,
9 EventId,
10 MessageId,
11 NodeId,
12 ProjectId,
13 ProviderInstanceId,
14 ProviderThreadId,
15 RunId,
16 ThreadId,
17} from "@t3tools/contracts";
18import * as CheckpointDiffQuery from "../checkpointing/CheckpointDiffQuery.ts";
19import * as CheckpointStore from "../checkpointing/CheckpointStore.ts";
20import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
21import { checkpointRefForScopeOrdinal } from "./CheckpointService.ts";
22import * as ProjectionStore from "./ProjectionStore.ts";
23import * as ThreadManagement from "./ThreadManagementService.ts";
24import * as SqlitePersistence from "../persistence/Sqlite.ts";
25const layerProjection = Layer.mergeAll(
26 ProjectionStore.layer.pipe(
27 Layer.provide(NodeCrypto.layer),
28 Layer.provideMerge(SqlitePersistence.layerMemory),
29 ),
30 SqlitePersistence.layerMemory,
31 IdAllocator.layer,
32 NodeCrypto.layer,
33 NodeServices.layer,
34);
35it.effect("resolves the thread baseline after a second root run replaces scope ownership", () =>
36 Effect.gen(function* () {
37 const ids = yield* IdAllocator.IdAllocatorV2;
38 const projections = yield* ProjectionStore.ProjectionStoreV2;
39 const now = DateTime.makeUnsafe("2026-09-08T12:00:00.000Z");
40 const threadId = ThreadId.make("thread:audit-root-scope");
41 const firstRunId = RunId.make("run:audit-root-scope:1");
42 const secondRunId = RunId.make("run:audit-root-scope:2");
43 const firstNodeId = NodeId.make("node:audit-root-scope:1");
44 const secondNodeId = NodeId.make("node:audit-root-scope:2");
45 const providerThreadId = ProviderThreadId.make("provider-thread:audit-root-scope");
46 const providerInstanceId = ProviderInstanceId.make("codex");
47 const scopeId = yield* ids.allocate.checkpointScope({ threadId, name: "root" });
48 const checkpointRefs = [
49 yield* checkpointRefForScopeOrdinal({ scopeId, ordinalWithinScope: 0 }),
50 yield* checkpointRefForScopeOrdinal({ scopeId, ordinalWithinScope: 1 }),
51 yield* checkpointRefForScopeOrdinal({ scopeId, ordinalWithinScope: 2 }),
52 ];
53 const baselineId = CheckpointId.make("checkpoint:audit-root-scope:0");
54 const firstCheckpointId = CheckpointId.make("checkpoint:audit-root-scope:1");
55 const secondCheckpointId = CheckpointId.make("checkpoint:audit-root-scope:2");
56 const modelSelection = { instanceId: providerInstanceId, model: "gpt-5.4" } as const;
57​
58 yield* projections.apply({
59 id: EventId.make("event:audit-root-scope:thread"),
60 type: "thread.created",
61 threadId,
62 providerInstanceId,
63 occurredAt: now,
64 payload: {
65 createdBy: "user",
66 creationSource: "web",
67 id: threadId,
68 projectId: ProjectId.make("project:audit-root-scope"),
69 title: "Checkpoint scope probe",
70 providerInstanceId,
71 modelSelection,
72 runtimeMode: "full-access",
73 interactionMode: "default",
74 branch: null,
75 worktreePath: null,
76 activeProviderThreadId: providerThreadId,
77 lineage: { parentThreadId: null, relationshipToParent: null, rootThreadId: threadId },
78 forkedFrom: null,
79 createdAt: now,
80 updatedAt: now,
81 archivedAt: null,
82 settledOverride: null,
83 settledAt: null,
84 lastVisitedAt: null,
85 deletedAt: null,
86 },
87 });
88​
89 const applyRun = (input: {
90 readonly runId: RunId;
91 readonly nodeId: NodeId;
92 readonly ordinal: number;
93 readonly checkpointId: CheckpointId;
94 }) =>
95 projections.apply({
96 id: EventId.make(`event:audit-root-scope:run:${input.ordinal}`),
97 type: "run.updated",
98 threadId,
99 runId: input.runId,
100 nodeId: input.nodeId,
101 providerInstanceId,
102 occurredAt: now,
103 payload: {
104 id: input.runId,
105 threadId,
106 ordinal: input.ordinal,
107 providerInstanceId,
108 modelSelection,
109 providerThreadId,
110 userMessageId: MessageId.make(`message:audit-root-scope:${input.ordinal}`),
111 rootNodeId: input.nodeId,
112 activeAttemptId: null,
113 status: "completed" as const,
114 requestedAt: now,
115 startedAt: now,
116 completedAt: now,
117 checkpointId: input.checkpointId,
118 contextHandoffId: null,
119 },
120 });
121 const applyScope = (runId: RunId, nodeId: NodeId, ordinal: number) =>
122 projections.apply({
123 id: EventId.make(`event:audit-root-scope:scope:${ordinal}`),
124 type: "checkpoint-scope.created",
125 threadId,
126 runId,
127 nodeId,
128 providerInstanceId,
129 occurredAt: now,
130 payload: {
131 id: scopeId,
132 threadId,
133 runId,
134 nodeId,
135 parentScopeId: null,
136 providerThreadId,
137 kind: "root_run" as const,
138 ordinalWithinParent: 0,
139 advancesAppRunCount: true,
140 cwd: ordinal === 1 ? "/repo" : "/prepared-repo",
141 createdAt: now,
142 },
143 });
144 const applyCheckpoint = (input: {
145 readonly id: CheckpointId;
146 readonly runId: RunId | null;
147 readonly nodeId: NodeId;
148 readonly ordinal: number;
149 readonly appRunOrdinal: number | null;
150 }) =>
151 projections.apply({
152 id: EventId.make(`event:audit-root-scope:checkpoint:${input.ordinal}`),
153 type: "checkpoint.captured",
154 threadId,
155 ...(input.runId === null ? {} : { runId: input.runId }),
156 nodeId: input.nodeId,
157 providerInstanceId,
158 occurredAt: now,
159 payload: {
160 id: input.id,
161 scopeId,
162 threadId,
163 runId: input.runId,
164 nodeId: input.nodeId,
165 parentCheckpointId:
166 input.ordinal === 0 ? null : input.ordinal === 1 ? baselineId : firstCheckpointId,
167 ordinalWithinScope: input.ordinal,
168 appRunOrdinal: input.appRunOrdinal,
169 ref: checkpointRefs[input.ordinal]!,
170 status: "ready" as const,
171 files: [],
172 capturedAt: now,
173 },
174 });
175​
176 yield* applyRun({
177 runId: firstRunId,
178 nodeId: firstNodeId,
179 ordinal: 1,
180 checkpointId: firstCheckpointId,
181 });
182 yield* applyScope(firstRunId, firstNodeId, 1);
183 yield* applyCheckpoint({
184 id: baselineId,
185 runId: null,
186 nodeId: firstNodeId,
187 ordinal: 0,
188 appRunOrdinal: null,
189 });
190 yield* applyCheckpoint({
191 id: firstCheckpointId,
192 runId: firstRunId,
193 nodeId: firstNodeId,
194 ordinal: 1,
195 appRunOrdinal: 1,
196 });
197 yield* applyRun({
198 runId: secondRunId,
199 nodeId: secondNodeId,
200 ordinal: 2,
201 checkpointId: secondCheckpointId,
202 });
203 yield* applyScope(secondRunId, secondNodeId, 2);
204 yield* applyCheckpoint({
205 id: secondCheckpointId,
206 runId: secondRunId,
207 nodeId: secondNodeId,
208 ordinal: 2,
209 appRunOrdinal: 2,
210 });
211​
212 const context = yield* projections.getCheckpointContext(threadId);
213 assert.deepEqual(
214 context.checkpoints.map((checkpoint) => checkpoint.appRunOrdinal),
215 [null, 1, 2],
216 );
217 assert.equal(context.checkpointScopes.length, 1);
218 assert.equal(context.checkpointScopes[0]?.runId, secondRunId);
219​
220 const layerQuery = CheckpointDiffQuery.layer.pipe(
221 Layer.provide(
222 Layer.mergeAll(
223 Layer.mock(ThreadManagement.ThreadManagementService)({
224 getCheckpointContext: () => Effect.succeed(context),
225 }),
226 Layer.mock(CheckpointStore.CheckpointStore)({
227 listAuthoredPaths: () => Effect.succeed(null),
228 diffCheckpoints: (input) => {
229 assert.equal(input.cwd, "/prepared-repo");
230 assert.equal(input.fromCheckpointRef, checkpointRefs[0]);
231 assert.equal(input.toCheckpointRef, checkpointRefs[2]);
232 return Effect.succeed("two-run diff");
233 },
234 }),
235 ),
236 ),
237 );
238 const result = yield* Effect.gen(function* () {
239 const query = yield* CheckpointDiffQuery.CheckpointDiffQuery;
240 return yield* query.getFullThreadDiff({ threadId, toTurnCount: 2 });
241 }).pipe(Effect.provide(layerQuery));
242 assert.equal(result.diff, "two-run diff");
243 }).pipe(Effect.provide(layerProjection)),
244);