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

CheckpointScopeOwnership.test.ts

237 lines8.0 KB

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