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

TransferBudgetV2Fixture.integration.ts

478 lines14.8 KB

1import {
2 CheckpointId,
3 CheckpointScopeId,
4 EventId,
5 MessageId,
6 NodeId,
7 ProjectId,
8 ProviderInstanceId,
9 ProviderThreadId,
10 ProviderTurnId,
11 RunId,
12 ThreadId,
13 TurnItemId,
14 type ProviderDriverKind,
15 type OrchestrationV2DomainEvent,
16 type OrchestrationV2TurnItem,
17} from "@t3tools/contracts";
18import * as DateTime from "effect/DateTime";
19import {
20 diagnosticOutput,
21 TRANSFER_HISTORY_TOOLS_PER_TURN,
22 TRANSFER_MEASURED_TOOLS,
23 TRANSFER_HISTORY_MCP_RESULT_BYTES,
24 TRANSFER_MEASURED_MCP_RESULT_BYTES,
25} from "./fixtures/transferBudget.ts";
26​
27export const THREAD_ID = ThreadId.make("transfer-budget-thread");
28type ThreadCreatedEvent = Extract<OrchestrationV2DomainEvent, { readonly type: "thread.created" }>;
29export function threadCreated(
30 provider: ProviderDriverKind,
31 threadId: ThreadId = THREAD_ID,
32 overrides: Partial<ThreadCreatedEvent["payload"]> = {},
33): ThreadCreatedEvent {
34 const now = DateTime.makeUnsafe("2026-06-01T00:00:00Z");
35 return {
36 id: EventId.make(`thread-created:${threadId}`),
37 type: "thread.created",
38 threadId,
39 occurredAt: now,
40 payload: {
41 id: threadId,
42 projectId: ProjectId.make("transfer-project"),
43 title: "Transfer budget",
44 providerInstanceId: ProviderInstanceId.make(provider),
45 modelSelection: {
46 instanceId: ProviderInstanceId.make(provider),
47 model: provider === "codex" ? "gpt-5.4" : "claude-sonnet-4-6",
48 },
49 runtimeMode: "full-access",
50 interactionMode: "default",
51 branch: null,
52 worktreePath: null,
53 activeProviderThreadId: null,
54 lineage: { parentThreadId: null, relationshipToParent: null, rootThreadId: threadId },
55 forkedFrom: null,
56 createdBy: "user",
57 creationSource: "web",
58 createdAt: now,
59 updatedAt: now,
60 archivedAt: null,
61 settledOverride: null,
62 settledAt: null,
63 lastVisitedAt: null,
64 deletedAt: null,
65 ...overrides,
66 },
67 };
69export function turnEvents(
70 provider: ProviderDriverKind,
71 index: number,
72 measured: boolean,
73): ReadonlyArray<OrchestrationV2DomainEvent> {
74 const now = DateTime.makeUnsafe(`2026-06-01T00:${String(index + 1).padStart(2, "0")}:00Z`);
75 const runId = RunId.make(`run-${index}`);
76 const nodeId = NodeId.make(`node-${index}`);
77 const userMessageId = MessageId.make(`user-${index}`);
78 const providerInstanceId = ProviderInstanceId.make(provider);
79 const run = {
80 id: runId,
81 threadId: THREAD_ID,
82 ordinal: index + 1,
83 providerInstanceId,
84 modelSelection: {
85 instanceId: ProviderInstanceId.make(provider),
86 model: provider === "codex" ? "gpt-5.4" : "claude-sonnet-4-6",
87 },
88 providerThreadId: null,
89 userMessageId,
90 rootNodeId: nodeId,
91 activeAttemptId: null,
92 status: "running" as const,
93 requestedAt: now,
94 startedAt: now,
95 completedAt: null,
96 checkpointId: null,
97 contextHandoffId: null,
98 };
99 const common = {
100 threadId: THREAD_ID,
101 runId,
102 nodeId,
103 driver: provider,
104 providerInstanceId,
105 occurredAt: now,
106 };
107 const events: OrchestrationV2DomainEvent[] = [
108 { ...common, id: EventId.make(`run-${index}`), type: "run.created", payload: run },
109 ];
110 const answer =
111 `Completed ${provider} inspection ${index}. ` +
112 "The projection preserves transcript text and bounds tool output. ".repeat(60);
113 for (const role of ["user", "assistant"] as const) {
114 events.push({
115 ...common,
116 id: EventId.make(`${role}-${index}`),
117 type: "message.updated",
118 payload: {
119 id: MessageId.make(`${role}-${index}`),
120 threadId: THREAD_ID,
121 runId,
122 nodeId,
123 role,
124 text: role === "user" ? `Inspect transfer ${index}` : answer,
125 attachments: [],
126 streaming: false,
127 createdAt: now,
128 updatedAt: now,
129 createdBy: role === "user" ? "user" : "agent",
130 creationSource: "web",
131 },
132 });
133 }
134 const toolCount = measured ? TRANSFER_MEASURED_TOOLS : TRANSFER_HISTORY_TOOLS_PER_TURN;
135 const item = (id: string, ordinal: number, title: string | null) => ({
136 id: TurnItemId.make(id),
137 threadId: THREAD_ID,
138 runId,
139 nodeId,
140 providerThreadId: null,
141 providerTurnId: null,
142 nativeItemRef: null,
143 parentItemId: null,
144 ordinal,
145 status: "completed" as const,
146 title,
147 startedAt: now,
148 completedAt: now,
149 updatedAt: now,
150 });
151 for (let tool = 0; tool <= toolCount; tool++) {
152 const mcp = tool === toolCount;
153 const output = diagnosticOutput({
154 provider,
155 turnIndex: index,
156 toolIndex: tool,
157 targetBytes: mcp
158 ? measured
159 ? TRANSFER_MEASURED_MCP_RESULT_BYTES
160 : TRANSFER_HISTORY_MCP_RESULT_BYTES
161 : 4096,
162 });
163 const base = item(
164 `item-${index}-${tool}`,
165 tool + 1,
166 mcp ? "MCP search" : `Inspect source ${tool}`,
167 );
168 events.push({
169 ...common,
170 id: EventId.make(`tool-${index}-${tool}`),
171 type: "turn-item.updated",
172 payload: mcp
173 ? {
174 ...base,
175 type: "dynamic_tool",
176 toolName: "mcp__test__search",
177 input: { query: "transfer" },
178 output: { text: output },
179 }
180 : {
181 ...base,
182 type: "command_execution",
183 input: `cat src/module-${tool}.ts`,
184 output,
185 exitCode: 0,
186 },
187 });
188 }
189 // The live turn repeats its answer in a turn item, as adapters do, so live dedupe has work.
190 // History turns skip it to keep the fallback thread snapshot under its wire ceiling.
191 if (measured)
192 events.push({
193 ...common,
194 id: EventId.make(`answer-item-${index}`),
195 type: "turn-item.updated",
196 payload: {
197 ...item(`item-${index}-answer`, toolCount + 2, null),
198 type: "assistant_message",
199 messageId: MessageId.make(`assistant-${index}`),
200 text: answer,
201 streaming: false,
202 },
203 });
204 events.push({
205 ...common,
206 id: EventId.make(`complete-${index}`),
207 type: "run.updated",
208 payload: { ...run, status: "completed", completedAt: now },
209 });
210 return events;
212​
213export const LONG_TURN_THREAD_ID = ThreadId.make("transfer-budget-long-turn");
214export const LONG_TURN_WORK_ROWS = 900;
215​
216/**
217 * Three short user turns, then one agent turn far longer than a cold-open
218 * window, with the item kinds a long agent run produces. One batch per turn.
219 * Run, node, provider and item IDs follow the composite shapes the Claude
220 * adapter stores, and every assistant message has its record, as real threads do.
221 */
222export function longTurnThreadBatches(
223 provider: ProviderDriverKind,
224): ReadonlyArray<ReadonlyArray<OrchestrationV2DomainEvent>> {
225 const threadId = LONG_TURN_THREAD_ID;
226 const providerInstanceId = ProviderInstanceId.make(provider);
227 const modelSelection = {
228 instanceId: providerInstanceId,
229 model: provider === "codex" ? "gpt-5.4" : "claude-sonnet-4-6",
230 };
231 let ordinal = 0;
232 const text = (turn: number, row: number, targetBytes: number) =>
233 diagnosticOutput({ provider, turnIndex: 100 + turn, toolIndex: row, targetBytes });
234​
235 const turn = (index: number, workRows: number): OrchestrationV2DomainEvent[] => {
236 const now = DateTime.makeUnsafe(`2026-06-02T00:${String(index).padStart(2, "0")}:00Z`);
237 const runId = RunId.make(`run:thread:${threadId}:ordinal:${index + 1}`);
238 const nodeId = NodeId.make(`node:run:${encodeURIComponent(runId)}:root`);
239 const providerThreadId = ProviderThreadId.make(
240 `provider-thread:provider:${provider}:native-thread:${encodeURIComponent(`pending:${runId}`)}`,
241 );
242 const providerTurnId = ProviderTurnId.make(
243 `provider-turn:provider:${provider}:native-turn:${encodeURIComponent(`turn:run-attempt:${encodeURIComponent(runId)}:attempt:1`)}`,
244 );
245 const common = {
246 threadId,
247 runId,
248 nodeId,
249 driver: provider,
250 providerInstanceId,
251 occurredAt: now,
252 };
253 const messageId = (role: "user" | "assistant", row: string) =>
254 MessageId.make(
255 `message:provider:${provider}:native-item:msg_01LongTurn${role}${index}x${row}`,
256 );
257 const run = {
258 id: runId,
259 threadId,
260 ordinal: index + 1,
261 providerInstanceId,
262 modelSelection,
263 providerThreadId: null,
264 userMessageId: messageId("user", "prompt"),
265 rootNodeId: nodeId,
266 activeAttemptId: null,
267 status: "running" as const,
268 requestedAt: now,
269 startedAt: now,
270 completedAt: null,
271 checkpointId: null,
272 contextHandoffId: null,
273 };
274 const base = (row: string, title: string | null) => {
275 ordinal += 1;
276 const nativeId = `toolu_01LongTurn${index}x${row}`;
277 return {
278 id: TurnItemId.make(`turn-item:provider:${provider}:native-item:${nativeId}`),
279 threadId,
280 runId,
281 nodeId,
282 providerThreadId,
283 providerTurnId,
284 nativeItemRef: { driver: provider, nativeId, strength: "strong" as const },
285 parentItemId: null,
286 ordinal,
287 status: "completed" as const,
288 title,
289 startedAt: now,
290 completedAt: now,
291 updatedAt: now,
292 };
293 };
294 const events: OrchestrationV2DomainEvent[] = [
295 { ...common, id: EventId.make(`long-turn-run-${index}`), type: "run.created", payload: run },
296 ];
297 const message = (role: "user" | "assistant", row: string, messageText: string) => {
298 const id = messageId(role, row);
299 const createdBy = role === "user" ? ("user" as const) : ("agent" as const);
300 events.push(
301 {
302 ...common,
303 id: EventId.make(`long-turn-message-${index}-${row}`),
304 type: "message.updated",
305 payload: {
306 id,
307 threadId,
308 runId,
309 nodeId,
310 role,
311 text: messageText,
312 attachments: [],
313 streaming: false,
314 createdAt: now,
315 updatedAt: now,
316 createdBy,
317 creationSource: "web",
318 },
319 },
320 {
321 ...common,
322 id: EventId.make(`long-turn-item-event-${index}-${row}`),
323 type: "turn-item.updated",
324 payload:
325 role === "user"
326 ? {
327 ...base(row, null),
328 type: "user_message",
329 createdBy,
330 creationSource: "web",
331 messageId: id,
332 inputIntent: "turn_start",
333 text: messageText,
334 attachments: [],
335 }
336 : {
337 ...base(row, null),
338 type: "assistant_message",
339 messageId: id,
340 text: messageText,
341 streaming: false,
342 },
343 },
344 );
345 };
346 const workItem = (row: number): OrchestrationV2TurnItem => {
347 if (row % 100 === 50) {
348 return {
349 ...base(String(row), "Checkpoint"),
350 type: "checkpoint",
351 checkpointId: CheckpointId.make(`long-turn-checkpoint-${index}-${row}`),
352 scopeId: CheckpointScopeId.make(`long-turn-scope-${index}`),
353 files: Array.from({ length: 40 }, (_, file) => ({
354 path: `src/feature/module-${row}-${file}.ts`,
355 kind: "modified",
356 additions: file + 1,
357 deletions: file,
358 })),
359 };
360 }
361 if (row % 100 === 99) {
362 return {
363 ...base(String(row), "Subagent"),
364 type: "subagent",
365 subagentId: NodeId.make(`long-turn-subagent-${index}-${row}`),
366 origin: "app_owned",
367 driver: provider,
368 providerInstanceId,
369 childThreadId: null,
370 prompt: text(index, row, 2_048),
371 result: text(index, row + 1, 3_072),
372 };
373 }
374 switch (row % 8) {
375 case 0:
376 case 7:
377 return {
378 ...base(String(row), "Reasoning"),
379 type: "reasoning",
380 text: text(index, row, 1_536),
381 streaming: false,
382 };
383 case 3:
384 return {
385 ...base(String(row), `Edit module ${row}`),
386 type: "file_change",
387 fileName: `src/feature/module-${row}.ts`,
388 additions: 12,
389 deletions: 4,
390 diffStr: text(index, row, 2_048),
391 };
392 case 4:
393 return {
394 ...base(String(row), "MCP search"),
395 type: "dynamic_tool",
396 toolName: "mcp__test__search",
397 input: { query: `module ${row}` },
398 output: { text: text(index, row, 6_144) },
399 };
400 default:
401 return {
402 ...base(String(row), `Run check ${row}`),
403 type: "command_execution",
404 input: `vp test run src/feature/module-${row}.test.ts`,
405 output: text(index, row, 4_096),
406 exitCode: 0,
407 };
408 }
409 };
410​
411 message("user", "prompt", `Continue the feature work, step ${index}.`);
412 for (let row = 0; row < workRows; row++) {
413 if (row % 8 === 5) {
414 message("assistant", String(row), text(index, row, 1_500));
415 continue;
416 }
417 events.push({
418 ...common,
419 id: EventId.make(`long-turn-work-${index}-${row}`),
420 type: "turn-item.updated",
421 payload: workItem(row),
422 });
423 }
424 message("assistant", "answer", text(index, workRows, 1_200));
425 events.push({
426 ...common,
427 id: EventId.make(`long-turn-complete-${index}`),
428 type: "run.updated",
429 payload: { ...run, status: "completed", completedAt: now },
430 });
431 return events;
432 };
433​
434 return [
435 [threadCreated(provider, threadId)],
436 turn(0, 4),
437 turn(1, 4),
438 turn(2, 4),
439 turn(3, LONG_TURN_WORK_ROWS),
440 ];
442​
443export const SHELL_SETTLED_THREADS = 100;
444export const SHELL_IDLE_SUBAGENTS = 40;
445​
446/**
447 * A busy sidebar: settled top-level threads, settled a minute apart, with idle subagent children
448 * under the newest ones. Opted-in cold connects skip the children and all but 25 settled threads.
449 */
450export function shellFleetEvents(
451 provider: ProviderDriverKind,
452): ReadonlyArray<OrchestrationV2DomainEvent> {
453 return Array.from({ length: SHELL_SETTLED_THREADS }, (_, index) => {
454 const threadId = ThreadId.make(`transfer-budget-settled-${index}`);
455 const settledAt = DateTime.makeUnsafe(Date.UTC(2026, 5, 3, 0, index));
456 const settled = threadCreated(provider, threadId, {
457 title: `Settled transfer thread ${index}`,
458 branch: `feature/settled-${index}`,
459 settledOverride: "settled",
460 settledAt,
461 updatedAt: settledAt,
462 });
463 if (index < SHELL_SETTLED_THREADS - SHELL_IDLE_SUBAGENTS) return [settled];
464 const childId = ThreadId.make(`transfer-budget-settled-${index}-subagent`);
465 return [
466 settled,
467 threadCreated(provider, childId, {
468 title: `Subagent of settled thread ${index}`,
469 createdBy: "agent",
470 lineage: {
471 parentThreadId: threadId,
472 relationshipToParent: "subagent",
473 rootThreadId: threadId,
474 },
475 }),
476 ];
477 }).flat();