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

ThreadFork.execution.test.ts

249 lines8.8 KB

1import { assert, it } from "@effect/vitest";
2import {
3 CommandId,
4 EventId,
5 MessageId,
6 NodeId,
7 ProjectId,
8 ProviderDriverKind,
9 ProviderInstanceId,
10 ProviderThreadId,
11 ProviderTurnId,
12 RunAttemptId,
13 RunId,
14 ThreadId,
15 TurnItemId,
16} from "@t3tools/contracts";
17import * as DateTime from "effect/DateTime";
18import * as Effect from "effect/Effect";
19​
20import { CodexProviderCapabilitiesV2 } from "./Adapters/CodexAdapterV2.ts";
21import { ClaudeProviderCapabilitiesV2 } from "./Adapters/ClaudeAdapterV2.ts";
22import * as EventSink from "./EventSink.ts";
23import * as Orchestrator from "./Orchestrator.ts";
24import * as ProviderAdapterRegistry from "./ProviderAdapterRegistry.ts";
25import * as ProviderReplayHarness from "./testkit/ProviderReplayHarness.ts";
26import type * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
27​
28const forkCases = (["codex", "claudeAgent"] as const).flatMap((driverName) => {
29 const driver = ProviderDriverKind.make(driverName);
30 const instanceId = ProviderInstanceId.make(driver);
31 const modelSelection = { instanceId, model: "test-model" };
32 const adapter: ProviderAdapter.ProviderAdapterV2["Service"] = {
33 instanceId,
34 driver,
35 getCapabilities: () =>
36 Effect.succeed(
37 driver === "codex" ? CodexProviderCapabilitiesV2 : ClaudeProviderCapabilitiesV2,
38 ),
39 planSelectionTransition: () => Effect.succeed({ type: "apply_on_next_turn" }),
40 openSession: () => Effect.die("Execution is paused after dispatch for handoff inspection"),
41 };
42 const layer = ProviderReplayHarness.layerWithRegistry(
43 { name: `fork-boundary-${driver}` },
44 ProviderAdapterRegistry.layerFromAdapters([adapter]),
45 { runEffectWorker: false },
46 );
47​
48 return (["failed", "interrupted", "cancelled"] as const).map((status) => ({
49 driver,
50 status,
51 instanceId,
52 modelSelection,
53 layer,
54 }));
55});
56​
57it.effect.each(forkCases)(
58 "bounds $driver context when continuing a fork of a $status run",
59 ({ driver, status, instanceId, modelSelection, layer }) =>
60 Effect.gen(function* () {
61 const orchestrator = yield* Orchestrator.OrchestratorV2;
62 const eventSink = yield* EventSink.EventSinkV2;
63 const now = yield* DateTime.now;
64 const sourceThreadId = ThreadId.make("fork-boundary-source");
65 const targetThreadId = ThreadId.make("fork-boundary-target");
66 const providerThreadId = ProviderThreadId.make("fork-boundary-native-thread");
67 const sourceRunId = RunId.make("fork-boundary-source-run");
68 const attemptId = RunAttemptId.make("interrupted-source-attempt");
69 const providerTurnId = ProviderTurnId.make("interrupted-source-turn");
70 const rootNodeId = NodeId.make("interrupted-source-root");
71​
72 yield* orchestrator.dispatch({
73 type: "thread.create",
74 commandId: CommandId.make("create-source"),
75 threadId: sourceThreadId,
76 projectId: ProjectId.make("fork-boundary-project"),
77 title: "Fork boundary source",
78 modelSelection,
79 runtimeMode: "full-access",
80 interactionMode: "default",
81 branch: null,
82 worktreePath: null,
83 createdBy: "user",
84 creationSource: "web",
85 });
86 yield* eventSink.write({
87 events: [
88 {
89 id: EventId.make("source-provider-thread"),
90 type: "provider-thread.updated",
91 threadId: sourceThreadId,
92 occurredAt: now,
93 payload: {
94 id: providerThreadId,
95 driver,
96 providerInstanceId: instanceId,
97 providerSessionId: null,
98 appThreadId: sourceThreadId,
99 ownerNodeId: null,
100 nativeThreadRef: { driver, nativeId: "native-source", strength: "strong" },
101 nativeConversationHeadRef: null,
102 status: "idle",
103 firstRunOrdinal: 1,
104 lastRunOrdinal: 2,
105 handoffIds: [],
106 forkedFrom: null,
107 createdAt: now,
108 updatedAt: now,
109 },
110 },
111 ],
112 });
113 if (status === "interrupted") {
114 yield* eventSink.write({
115 events: [
116 {
117 id: EventId.make("source-attempt"),
118 type: "run-attempt.created",
119 threadId: sourceThreadId,
120 runId: sourceRunId,
121 occurredAt: now,
122 payload: {
123 id: attemptId,
124 runId: sourceRunId,
125 attemptOrdinal: 1,
126 rootNodeId,
127 providerInstanceId: instanceId,
128 providerThreadId,
129 providerTurnId,
130 reason: "initial",
131 status,
132 startedAt: now,
133 completedAt: now,
134 },
135 },
136 {
137 id: EventId.make("source-provider-turn"),
138 type: "provider-turn.updated",
139 threadId: sourceThreadId,
140 occurredAt: now,
141 payload: {
142 id: providerTurnId,
143 providerThreadId,
144 nodeId: rootNodeId,
145 runAttemptId: attemptId,
146 nativeTurnRef: { driver, nativeId: "turn:synthetic", strength: "weak" },
147 ordinal: 1,
148 status,
149 startedAt: now,
150 completedAt: now,
151 },
152 },
153 ],
154 });
155 }
156 for (const ordinal of [1, 2]) {
157 const runId = ordinal === 1 ? sourceRunId : RunId.make("later-run");
158 const messageId = MessageId.make(`source-message-${ordinal}`);
159 yield* eventSink.write({
160 events: [
161 {
162 id: EventId.make(`run-${ordinal}`),
163 type: "run.created",
164 threadId: sourceThreadId,
165 runId,
166 occurredAt: now,
167 payload: {
168 id: runId,
169 threadId: sourceThreadId,
170 ordinal,
171 providerInstanceId: instanceId,
172 modelSelection,
173 providerThreadId,
174 userMessageId: messageId,
175 rootNodeId: null,
176 activeAttemptId: ordinal === 1 && status === "interrupted" ? attemptId : null,
177 status: ordinal === 1 ? status : "completed",
178 queuePosition: null,
179 requestedAt: now,
180 startedAt: now,
181 completedAt: now,
182 checkpointId: null,
183 contextHandoffId: null,
184 },
185 },
186 {
187 id: EventId.make(`item-${ordinal}`),
188 type: "turn-item.updated",
189 threadId: sourceThreadId,
190 runId,
191 occurredAt: now,
192 payload: {
193 id: TurnItemId.make(`item-${ordinal}`),
194 threadId: sourceThreadId,
195 runId,
196 nodeId: null,
197 providerThreadId,
198 providerTurnId: null,
199 nativeItemRef: null,
200 parentItemId: null,
201 ordinal,
202 status: "completed",
203 title: null,
204 startedAt: now,
205 completedAt: now,
206 updatedAt: now,
207 type: "user_message",
208 createdBy: "user",
209 creationSource: "web",
210 inputIntent: "turn_start",
211 messageId,
212 text: ordinal === 1 ? "INCLUDED_SOURCE_MARKER" : "EXCLUDED_LATER_MARKER",
213 attachments: [],
214 },
215 },
216 ],
217 });
218 }
219 yield* orchestrator.dispatch({
220 type: "thread.fork",
221 commandId: CommandId.make("fork-source"),
222 sourceThreadId,
223 targetThreadId,
224 sourcePoint: { type: "run", runId: sourceRunId },
225 createdBy: "user",
226 creationSource: "web",
227 });
228 yield* orchestrator.dispatch({
229 type: "message.dispatch",
230 commandId: CommandId.make("continue-fork"),
231 threadId: targetThreadId,
232 messageId: MessageId.make("continue-fork"),
233 text: "Continue from the selected source run",
234 attachments: [],
235 modelSelection,
236 dispatchMode: { type: "start_immediately" },
237 createdBy: "user",
238 creationSource: "web",
239 });
240 const target = yield* orchestrator.getThreadProjection(targetThreadId);
241 assert.equal(target.contextTransfers[0]?.resolution?.strategy, "portable_context");
242 assert.lengthOf(target.contextHandoffs, 1);
243 const handoff = target.contextHandoffs[0]!;
244 const history = handoff.history?.messages.map((message) => message.text).join("\n") ?? "";
245 assert.include(`${handoff.summaryText}\n${history}`, "INCLUDED_SOURCE_MARKER");
246 assert.notInclude(`${handoff.summaryText}\n${history}`, "EXCLUDED_LATER_MARKER");
247 assert.isNull(target.providerThreads[0]?.forkedFrom);
248 }).pipe(Effect.provide(layer)),
249);