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

CheckpointScopeOwnership.test.ts

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