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

ProviderTurnControlService.test.ts

324 lines12.3 KB

1import { assert, it } from "@effect/vitest";
2import {
3 type ModelSelection,
4 NodeId,
5 type OrchestrationV2ProviderThread,
6 type OrchestrationV2ThreadProjection,
7 ProjectId,
8 ProviderDriverKind,
9 ProviderInstanceId,
10 ProviderSessionId,
11 ProviderThreadId,
12 ProviderTurnId,
13 RunAttemptId,
14 RunId,
15 ThreadId,
16} from "@t3tools/contracts";
17import * as DateTime from "effect/DateTime";
18import * as Effect from "effect/Effect";
19import * as Exit from "effect/Exit";
20import * as Layer from "effect/Layer";
21import * as Option from "effect/Option";
22import * as Ref from "effect/Ref";
23import * as Stream from "effect/Stream";
24​
25import { CodexProviderCapabilitiesV2 } from "./Adapters/CodexAdapterV2.ts";
26import * as ProjectionStore from "./ProjectionStore.ts";
27import type { ProviderAdapterV2SessionRuntime } from "./ProviderAdapter.ts";
28import * as ProviderSessionManager from "./ProviderSessionManager.ts";
29import * as ProviderTurnControlService from "./ProviderTurnControlService.ts";
30​
31const driver = ProviderDriverKind.make("codex");
32const providerInstanceId = ProviderInstanceId.make("codex");
33const modelSelection = {
34 instanceId: providerInstanceId,
35 model: "gpt-5.4",
36} satisfies ModelSelection;
37​
38function makeProjection(input: {
39 readonly now: DateTime.Utc;
40 readonly threadId: ThreadId;
41 readonly providerThread: OrchestrationV2ProviderThread;
42 readonly providerTurnId: ProviderTurnId;
43 readonly attemptId: RunAttemptId;
44}): OrchestrationV2ThreadProjection {
45 const runId = RunId.make("run:restart-session");
46 const nodeId = NodeId.make("node:restart-session");
47 return {
48 thread: {
49 createdBy: "user",
50 creationSource: "web",
51 id: input.threadId,
52 projectId: ProjectId.make("project:restart-session"),
53 title: "Restart session",
54 providerInstanceId,
55 modelSelection,
56 runtimeMode: "full-access",
57 interactionMode: "default",
58 branch: null,
59 worktreePath: "/workspace",
60 activeProviderThreadId: input.providerThread.id,
61 lineage: {
62 parentThreadId: null,
63 relationshipToParent: null,
64 rootThreadId: input.threadId,
65 },
66 forkedFrom: null,
67 createdAt: input.now,
68 updatedAt: input.now,
69 archivedAt: null,
70 settledOverride: null,
71 settledAt: null,
72 lastVisitedAt: null,
73 deletedAt: null,
74 },
75 runs: [],
76 attempts: [
77 {
78 id: input.attemptId,
79 runId,
80 attemptOrdinal: 1,
81 rootNodeId: nodeId,
82 providerInstanceId,
83 providerThreadId: input.providerThread.id,
84 providerTurnId: input.providerTurnId,
85 reason: "initial",
86 status: "superseded",
87 startedAt: input.now,
88 completedAt: input.now,
89 },
90 ],
91 nodes: [],
92 subagents: [],
93 providerSessions: [],
94 providerThreads: [input.providerThread],
95 providerTurns: [
96 {
97 id: input.providerTurnId,
98 providerThreadId: input.providerThread.id,
99 nodeId,
100 runAttemptId: input.attemptId,
101 nativeTurnRef: {
102 driver,
103 nativeId: "native-turn:restart-session",
104 strength: "strong",
105 },
106 ordinal: 1,
107 status: "running",
108 startedAt: input.now,
109 completedAt: null,
110 },
111 ],
112 runtimeRequests: [],
113 messages: [],
114 plans: [],
115 turnItems: [],
116 checkpointScopes: [],
117 checkpoints: [],
118 contextHandoffs: [],
119 contextTransfers: [],
120 visibleTurnItems: [],
121 updatedAt: input.now,
122 };
124​
125it.effect(
126 "interrupts the historical session only for the exact committed restart replacement",
127 () =>
128 Effect.gen(function* () {
129 const now = yield* DateTime.now;
130 const threadId = ThreadId.make("thread:restart-session");
131 const oldSessionId = ProviderSessionId.make("provider-session:restart-session:old");
132 const replacementSessionId = ProviderSessionId.make(
133 "provider-session:restart-session:replacement",
134 );
135 const unrelatedSessionId = ProviderSessionId.make(
136 "provider-session:restart-session:unrelated",
137 );
138 const providerThreadId = ProviderThreadId.make("provider-thread:restart-session");
139 const providerTurnId = ProviderTurnId.make("provider-turn:restart-session");
140 const attemptId = RunAttemptId.make("run-attempt:restart-session");
141 const providerThread: OrchestrationV2ProviderThread = {
142 id: providerThreadId,
143 driver,
144 providerInstanceId,
145 // The restart command has already projected this replacement binding
146 // before the process-bound restart effect executes.
147 providerSessionId: replacementSessionId,
148 appThreadId: threadId,
149 ownerNodeId: null,
150 nativeThreadRef: {
151 driver,
152 nativeId: "native-thread:restart-session",
153 strength: "strong",
154 },
155 nativeConversationHeadRef: null,
156 status: "not_loaded",
157 firstRunOrdinal: 1,
158 lastRunOrdinal: 1,
159 handoffIds: [],
160 forkedFrom: null,
161 createdAt: now,
162 updatedAt: now,
163 };
164 const projection = yield* Ref.make(
165 makeProjection({ now, threadId, providerThread, providerTurnId, attemptId }),
166 );
167 const interruptedThread = yield* Ref.make<OrchestrationV2ProviderThread | null>(null);
168 const providerSession = {
169 id: oldSessionId,
170 driver,
171 providerInstanceId,
172 status: "running" as const,
173 cwd: "/workspace",
174 model: modelSelection.model,
175 capabilities: CodexProviderCapabilitiesV2,
176 createdAt: now,
177 updatedAt: now,
178 lastError: null,
179 };
180 const runtime: ProviderAdapterV2SessionRuntime = {
181 instanceId: providerInstanceId,
182 driver,
183 providerSessionId: oldSessionId,
184 providerSession,
185 events: Stream.empty,
186 ensureThread: () => Effect.die("unused ensureThread"),
187 resumeThread: () => Effect.die("unused resumeThread"),
188 startTurn: () => Effect.die("unused startTurn"),
189 steerTurn: () => Effect.die("unused steerTurn"),
190 interruptTurn: ({ providerThread: target }) =>
191 Effect.all(
192 [
193 Ref.set(interruptedThread, target),
194 Ref.update(projection, (current) => ({
195 ...current,
196 providerTurns: current.providerTurns.map((turn) =>
197 turn.id === providerTurnId
198 ? { ...turn, status: "interrupted" as const, completedAt: now }
199 : turn,
200 ),
201 })),
202 ],
203 { discard: true },
204 ),
205 respondToRuntimeRequest: () => Effect.die("unused respondToRuntimeRequest"),
206 readThreadSnapshot: () => Effect.die("unused readThreadSnapshot"),
207 rollbackThread: () => Effect.die("unused rollbackThread"),
208 forkThread: () => Effect.die("unused forkThread"),
209 };
210 const projectionLayer = Layer.succeed(
211 ProjectionStore.ProjectionStoreV2,
212 ProjectionStore.ProjectionStoreV2.of({
213 apply: () => Effect.void,
214 getLimitRecoveryCandidates: () => Effect.die("unused getLimitRecoveryCandidates"),
215 getShellSnapshot: () => Effect.die("unused getShellSnapshot"),
216 getThreadShell: () => Effect.die("unused getThreadShell"),
217 getThread: () => Ref.get(projection).pipe(Effect.map((state) => state.thread)),
218 getSettlementCandidates: () => Effect.die("unused getSettlementCandidates"),
219 getThreadsWithPullRequests: () => Effect.die("unused getThreadsWithPullRequests"),
220 getThreadProjection: () => Effect.die("control effects must not load transcript"),
221 getTurnStartContext: () => Effect.die("unused"),
222 getTurnStartHistory: () => Effect.die("unused"),
223 getRuntimeRecoveryProjection: () => Effect.die("unused getRuntimeRecoveryProjection"),
224 getPlan: () => Effect.die("unused"),
225 hasUnpairedRunInterruptRequest: () => Effect.die("unused interrupt read"),
226 getThreadAttachmentIds: () => Effect.die("Unused attachment lookup"),
227 getTimelinePage: () => Effect.die("Unused timeline read"),
228 getMessageCount: () => Effect.die("unused message count"),
229 getNextTurnItemOrdinal: () => Effect.die("unused ordinal read"),
230 getThreadRecords: () => Effect.die("unused record read"),
231 getRuntimeRequest: () => Effect.die("unused getRuntimeRequest"),
232 getRunningTurnContext: () => Effect.die("unused getRunningTurnContext"),
233 getThreadProviderContext: () => Effect.die("unused getThreadProviderContext"),
234 getRuntimeResponseContext: () => Effect.die("unused getRuntimeResponseContext"),
235 getPendingNativeUserInputs: () => Effect.die("unused getPendingNativeUserInputs"),
236 getProviderControlContext: (_threadId, target) =>
237 Ref.get(projection).pipe(
238 Effect.map((current) => ({
239 providerThread: current.providerThreads.find(
240 (thread) => thread.id === target.providerThreadId,
241 ),
242 providerTurn: current.providerTurns.find(
243 (turn) => turn.id === target.providerTurnId,
244 ),
245 attempt: current.attempts.find((attempt) => attempt.id === target.attemptId),
246 message: undefined,
247 run: undefined,
248 })),
249 ),
250 getCheckpointContext: () => Effect.die("not used"),
251 getCheckpointCaptureContext: () => Effect.die("not used"),
252 getRunMessage: () => Effect.die("not used"),
253 canStartQueuedRun: () => Effect.die("not used"),
254 getRecoveryThreadIds: () => Effect.die("unused getRecoveryThreadIds"),
255 getUnreadableThreadIds: () => Effect.die("unused getUnreadableThreadIds"),
256 getThreadSnapshot: () => Effect.die("unused getThreadSnapshot"),
257 getThreadSnapshotWindow: () => Effect.die("unused getThreadSnapshotWindow"),
258 }),
259 );
260 const sessionManagerLayer = Layer.succeed(
261 ProviderSessionManager.ProviderSessionManagerV2,
262 ProviderSessionManager.ProviderSessionManagerV2.of({
263 shutdown: Effect.void,
264 open: () => Effect.die("unused open"),
265 get: (providerSessionId) =>
266 Effect.succeed(
267 providerSessionId === oldSessionId ? Option.some(runtime) : Option.none(),
268 ),
269 close: () => Effect.void,
270 closeInstance: () => Effect.void,
271 release: () => Effect.void,
272 detach: () => Effect.void,
273 }),
274 );
275 const controlLayer = ProviderTurnControlService.layer.pipe(
276 Layer.provide(Layer.merge(projectionLayer, sessionManagerLayer)),
277 );
278​
279 const [ordinaryInterrupt, unrelatedRestart] = yield* Effect.gen(function* () {
280 const control = yield* ProviderTurnControlService.ProviderTurnControlServiceV2;
281 const ordinary = yield* Effect.exit(
282 control.interrupt({
283 threadId,
284 providerSessionId: oldSessionId,
285 providerThreadId,
286 providerTurnId,
287 }),
288 );
289 const unrelated = yield* Effect.exit(
290 control.interruptAndAwaitTerminal({
291 threadId,
292 providerSessionId: oldSessionId,
293 replacementProviderSessionId: unrelatedSessionId,
294 providerThreadId,
295 providerTurnId,
296 interruptedAttemptId: attemptId,
297 }),
298 );
299 return [ordinary, unrelated] as const;
300 }).pipe(Effect.provide(controlLayer));
301​
302 assert.isTrue(Exit.isFailure(ordinaryInterrupt));
303 assert.isTrue(Exit.isFailure(unrelatedRestart));
304 assert.isNull(yield* Ref.get(interruptedThread));
305​
306 yield* Effect.gen(function* () {
307 const control = yield* ProviderTurnControlService.ProviderTurnControlServiceV2;
308 yield* control.interruptAndAwaitTerminal({
309 threadId,
310 providerSessionId: oldSessionId,
311 replacementProviderSessionId: replacementSessionId,
312 providerThreadId,
313 providerTurnId,
314 interruptedAttemptId: attemptId,
315 });
316 }).pipe(Effect.provide(controlLayer));
317​
318 const interrupted = yield* Ref.get(interruptedThread);
319 assert.isNotNull(interrupted);
320 assert.equal(interrupted?.providerSessionId, oldSessionId);
321 assert.equal(interrupted?.id, providerThreadId);
322 assert.equal(interrupted?.nativeThreadRef?.nativeId, "native-thread:restart-session");
323 }),
324);