Open History to see the last commit
1import { EventId, ProviderDriverKind } from "@t3tools/contracts";
2
3import type {
4 FixtureProviderRuntimeEvent,
5 TestTurnResponse,
6} from "../TestProviderAdapter.integration.ts";
7
8const FIXTURE_THREAD_ID = "transfer-budget-thread";
9const FIXTURE_TURN_ID = "transfer-budget-turn";
10
11export const TRANSFER_HISTORY_TURN_COUNT = 10;
12export const TRANSFER_HISTORY_TOOLS_PER_TURN = 5;
13export const TRANSFER_MEASURED_TOOLS = 20;
14export const TRANSFER_HISTORY_MCP_RESULT_BYTES = 900_000;
15export const TRANSFER_MEASURED_MCP_RESULT_BYTES = 1_100_000;
16
17const sourceModules = [
18 "connection/session.ts",
19 "connection/supervisor.ts",
20 "rpc/client.ts",
21 "rpc/protocol.ts",
22 "state/threads.ts",
23 "state/threadReducer.ts",
24 "state/threadSnapshotHttp.ts",
25 "orchestration/http.ts",
26 "orchestration/Normalizer.ts",
27 "orchestration/ActivityPayloadProjection.ts",
28 "provider/ProviderService.ts",
29 "provider/ProviderRuntimeIngestion.ts",
30 "persistence/ProjectionSnapshotQuery.ts",
31 "persistence/OrchestrationEventStore.ts",
32 "checkpointing/CheckpointStore.ts",
33 "checkpointing/CheckpointDiffQuery.ts",
34 "server.ts",
35] as const;
36
37function fixtureTimestamp(turnIndex: number, eventIndex: number): string {
38 const minute = String(turnIndex).padStart(2, "0");
39 const second = String(Math.floor(eventIndex / 1_000)).padStart(2, "0");
40 const millisecond = String(eventIndex % 1_000).padStart(3, "0");
41 return `2026-06-01T00:${minute}:${second}.${millisecond}Z`;
42}
43
44function mix(value: number): number {
45 let mixed = value | 0;
46 mixed ^= mixed >>> 16;
47 mixed = Math.imul(mixed, 0x7feb352d);
48 mixed ^= mixed >>> 15;
49 mixed = Math.imul(mixed, 0x846ca68b);
50 mixed ^= mixed >>> 16;
51 return mixed >>> 0;
52}
53
54function digest(seed: number): string {
55 return [0, 1, 2, 3]
56 .map((offset) =>
57 mix(seed + offset * 0x9e3779b9)
58 .toString(16)
59 .padStart(8, "0"),
60 )
61 .join("");
62}
63
64/** Produces safe, deterministic output with enough entropy to exercise gzip. */
65function diagnosticOutput(input: {
66 readonly provider: ProviderDriverKind;
67 readonly turnIndex: number;
68 readonly toolIndex: number;
69 readonly targetBytes: number;
70}): string {
71 const chunks: string[] = [];
72 const providerSeed = input.provider === "codex" ? 0x43_4f_44_45 : 0x43_4c_41_55;
73 let length = 0;
74 let lineIndex = 0;
75
76 while (length < input.targetBytes) {
77 const modulePath = sourceModules[(input.toolIndex + lineIndex) % sourceModules.length];
78 const seed =
79 providerSeed + input.turnIndex * 100_003 + input.toolIndex * 10_007 + lineIndex * 101;
80 const line =
81 `${String(lineIndex + 1).padStart(6, "0")} ${modulePath} ` +
82 `operation=project-transfer-${input.turnIndex + 1}-${input.toolIndex + 1} ` +
83 `cursor=${mix(seed)} digest=${digest(seed)} status=completed\n`;
84 chunks.push(line);
85 length += line.length;
86 lineIndex += 1;
87 }
88
89 return chunks.join("").slice(0, input.targetBytes);
90}
91
92function assistantChunks(provider: ProviderDriverKind, turnIndex: number): ReadonlyArray<string> {
93 const providerName = provider === "codex" ? "Codex" : "Claude";
94 const paragraphs: string[] = [
95 `I traced the ${providerName} request through the environment connection and orchestration layers. `,
96 ];
97 let paragraphIndex = 0;
98 while (paragraphs.join("").length < 4_096) {
99 const modulePath = sourceModules[paragraphIndex % sourceModules.length];
100 paragraphs.push(
101 `Pass ${paragraphIndex + 1} reviewed ${modulePath} for turn ${turnIndex + 1}. ` +
102 "The shell cursor stayed monotonic, the thread snapshot remained resumable, and the client received only incremental events. ",
103 );
104 paragraphIndex += 1;
105 }
106 const text = paragraphs.join("").slice(0, 4_096);
107 return Array.from({ length: Math.ceil(text.length / 256) }, (_, index) =>
108 text.slice(index * 256, (index + 1) * 256),
109 );
110}
111
112export function expectedRecordedAssistantText(
113 provider: ProviderDriverKind,
114 turnIndex: number,
115): string {
116 return assistantChunks(provider, turnIndex).join("");
117}
118
119function unifiedDiff(provider: ProviderDriverKind, turnIndex: number): string {
120 const lines = sourceModules
121 .slice(0, 8)
122 .flatMap((modulePath, index) => [
123 `diff --git a/${modulePath} b/${modulePath}`,
124 `--- a/${modulePath}`,
125 `+++ b/${modulePath}`,
126 `@@ -${index + 1},2 +${index + 1},3 @@`,
127 ` const provider = "${provider}";`,
128 `+const transferTurn = ${turnIndex + 1};`,
129 `+const transferSample = ${1_500 + index * 97};`,
130 ]);
131 return lines.join("\n");
132}
133
134function baseEvent(
135 provider: ProviderDriverKind,
136 turnIndex: number,
137 eventIndex: number,
138): Pick<FixtureProviderRuntimeEvent, "eventId" | "provider" | "createdAt" | "threadId"> {
139 return {
140 eventId: EventId.make(`recorded:${provider}:${turnIndex}:${eventIndex}`),
141 provider,
142 createdAt: fixtureTimestamp(turnIndex, eventIndex),
143 threadId: FIXTURE_THREAD_ID,
144 };
145}
146
147/**
148 * Synthetic canonical events calibrated from heavy local Codex and Claude
149 * threads. Ten historical turns produce 9 MB of retained MCP results without
150 * committing user content. Command output is intentionally modest because the
151 * client projection strips it.
152 */
153export function makeRecordedTransferTurn(
154 provider: ProviderDriverKind,
155 turnIndex: number,
156): TestTurnResponse {
157 const measuredTurn = turnIndex >= TRANSFER_HISTORY_TURN_COUNT;
158 const toolCount = measuredTurn ? TRANSFER_MEASURED_TOOLS : TRANSFER_HISTORY_TOOLS_PER_TURN;
159 const turnId = `${FIXTURE_TURN_ID}-${turnIndex + 1}`;
160 const events: FixtureProviderRuntimeEvent[] = [];
161 let eventIndex = 0;
162
163 events.push({
164 type: "turn.started",
165 ...baseEvent(provider, turnIndex, eventIndex++),
166 turnId,
167 payload: {
168 model: provider === "codex" ? "gpt-5.4" : "claude-opus-4-1",
169 effort: provider === "codex" ? "high" : "default",
170 },
171 });
172
173 for (let toolIndex = 0; toolIndex < toolCount; toolIndex += 1) {
174 const itemId = `tool-${turnIndex + 1}-${toolIndex + 1}`;
175 const command =
176 provider === "codex"
177 ? `vp test transfer-budget-${toolIndex + 1}`
178 : `review transfer budget ${toolIndex + 1}`;
179 events.push(
180 {
181 type: "item.started",
182 ...baseEvent(provider, turnIndex, eventIndex++),
183 turnId,
184 itemId,
185 payload: {
186 itemType: "command_execution",
187 status: "inProgress",
188 title: `Inspect transfer path ${toolIndex + 1}`,
189 detail: "Inspecting the HTTP snapshot and WebSocket projection boundaries.",
190 data: {
191 threadId: FIXTURE_THREAD_ID,
192 turnId,
193 startedAtMs: turnIndex * 60_000 + eventIndex,
194 item: {
195 id: itemId,
196 type: "commandExecution",
197 command,
198 cwd: "/workspace/transfer-budget",
199 processId: String(toolIndex + 1),
200 status: "inProgress",
201 commandActions: [],
202 aggregatedOutput: "",
203 },
204 },
205 },
206 },
207 {
208 type: "item.completed",
209 ...baseEvent(provider, turnIndex, eventIndex++),
210 turnId,
211 itemId,
212 payload: {
213 itemType: "command_execution",
214 status: "completed",
215 title: `Inspected transfer path ${toolIndex + 1}`,
216 detail: "Collected a deterministic multi-module transfer diagnostic.",
217 data: {
218 threadId: FIXTURE_THREAD_ID,
219 turnId,
220 completedAtMs: turnIndex * 60_000 + eventIndex,
221 item: {
222 id: itemId,
223 type: "commandExecution",
224 command,
225 cwd: "/workspace/transfer-budget",
226 processId: String(toolIndex + 1),
227 status: "completed",
228 commandActions: [],
229 aggregatedOutput: diagnosticOutput({
230 provider,
231 turnIndex,
232 toolIndex,
233 targetBytes: 1_000,
234 }),
235 exitCode: 0,
236 durationMs: 500 + toolIndex,
237 },
238 },
239 },
240 },
241 );
242 }
243
244 const mcpItemId = `mcp-${turnIndex + 1}`;
245 const mcpResultBytes = measuredTurn
246 ? TRANSFER_MEASURED_MCP_RESULT_BYTES
247 : TRANSFER_HISTORY_MCP_RESULT_BYTES;
248 events.push(
249 {
250 type: "item.started",
251 ...baseEvent(provider, turnIndex, eventIndex++),
252 turnId,
253 itemId: mcpItemId,
254 payload: {
255 itemType: "mcp_tool_call",
256 status: "inProgress",
257 title: "fixture-history · inspect_transfer_log",
258 detail: "Reading a retained diagnostic result from the provider history.",
259 data: {
260 startedAtMs: turnIndex * 60_000 + eventIndex,
261 threadId: FIXTURE_THREAD_ID,
262 turnId,
263 item: {
264 type: "mcpToolCall",
265 id: mcpItemId,
266 server: "fixture-history",
267 tool: "inspect_transfer_log",
268 arguments: { turn: turnIndex + 1 },
269 status: "inProgress",
270 },
271 },
272 },
273 },
274 {
275 type: "item.completed",
276 ...baseEvent(provider, turnIndex, eventIndex++),
277 turnId,
278 itemId: mcpItemId,
279 payload: {
280 itemType: "mcp_tool_call",
281 status: "completed",
282 title: "fixture-history · inspect_transfer_log",
283 detail: "Retained a deterministic diagnostic result in the thread history.",
284 data: {
285 completedAtMs: turnIndex * 60_000 + eventIndex,
286 threadId: FIXTURE_THREAD_ID,
287 turnId,
288 item: {
289 type: "mcpToolCall",
290 id: mcpItemId,
291 server: "fixture-history",
292 tool: "inspect_transfer_log",
293 arguments: { turn: turnIndex + 1 },
294 durationMs: 1_000 + turnIndex,
295 error: null,
296 result: {
297 content: [
298 {
299 type: "text",
300 text: diagnosticOutput({
301 provider,
302 turnIndex,
303 toolIndex: toolCount,
304 targetBytes: mcpResultBytes,
305 }),
306 },
307 ],
308 },
309 status: "completed",
310 },
311 },
312 },
313 },
314 );
315
316 const chunks = assistantChunks(provider, turnIndex);
317 for (const [contentIndex, delta] of chunks.entries()) {
318 events.push({
319 type: "content.delta",
320 ...baseEvent(provider, turnIndex, eventIndex++),
321 turnId,
322 itemId: `assistant-${turnIndex + 1}`,
323 payload: {
324 streamKind: "assistant_text",
325 delta,
326 contentIndex,
327 },
328 });
329 }
330
331 events.push(
332 {
333 type: "thread.token-usage.updated",
334 ...baseEvent(provider, turnIndex, eventIndex++),
335 turnId,
336 payload: {
337 usage: {
338 usedTokens: 18_000 + turnIndex * 1_900,
339 maxTokens: 200_000,
340 inputTokens: 15_000 + turnIndex * 1_700,
341 cachedInputTokens: 9_000 + turnIndex * 1_100,
342 outputTokens: 3_000 + turnIndex * 200,
343 toolUses: toolCount,
344 durationMs: 4_000 + turnIndex * 250,
345 },
346 },
347 },
348 {
349 type: "turn.diff.updated",
350 ...baseEvent(provider, turnIndex, eventIndex++),
351 turnId,
352 payload: {
353 unifiedDiff: unifiedDiff(provider, turnIndex),
354 },
355 },
356 {
357 type: "turn.completed",
358 ...baseEvent(provider, turnIndex, eventIndex),
359 turnId,
360 payload: {
361 state: "completed",
362 stopReason: "end_turn",
363 usage: {
364 inputTokens: 15_000 + turnIndex * 1_700,
365 outputTokens: 3_000 + turnIndex * 200,
366 },
367 },
368 },
369 );
370
371 return { events };
372}