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