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

TransferBudgetReport.integration.ts

328 lines12.3 KB

1import type { ProviderDriverKind } from "@t3tools/contracts";
2​
3import type {
4 HttpTransferMeasurement,
5 WebSocketTransferTotals,
6} from "./NetworkTransferMeasurement.integration.ts";
7import {
8 TRANSFER_HISTORY_MCP_RESULT_BYTES,
9 TRANSFER_HISTORY_TOOLS_PER_TURN,
10 TRANSFER_HISTORY_TURN_COUNT,
11 TRANSFER_MEASURED_MCP_RESULT_BYTES,
12 TRANSFER_MEASURED_TOOLS,
13} from "./fixtures/transferBudget.ts";
14import { LONG_TURN_WORK_ROWS } from "./TransferBudgetV2Fixture.integration.ts";
15​
16export interface WebSocketCatchUpMeasurement extends WebSocketTransferTotals {
17 readonly mode: "replay" | "snapshot";
19​
20export interface TimelineHttpMeasurement {
21 readonly http: HttpTransferMeasurement;
22 readonly rows: number;
24​
25export interface TransferBudgetRun {
26 readonly provider: ProviderDriverKind;
27 readonly threadSnapshot: HttpTransferMeasurement;
28 readonly measuredTurnWebSocket: WebSocketTransferTotals;
29 readonly shellSnapshot: HttpTransferMeasurement;
30 readonly measuredTurnShellWebSocket: WebSocketTransferTotals;
31 readonly measuredTurnSecondClientWebSocket: WebSocketTransferTotals;
32 readonly reconnectThread: WebSocketCatchUpMeasurement;
33 readonly reconnectShell: WebSocketCatchUpMeasurement;
34 readonly measuredTurnSqlStatements: number;
35 readonly reconnectSqlStatements: number;
36 /** The compact bounded snapshot web and mobile load when the long-turn thread opens. */
37 readonly longTurnColdOpen: TimelineHttpMeasurement;
38 readonly longTurnHistoryPage: TimelineHttpMeasurement;
40​
41interface ProviderTransferBudget {
42 readonly totalWireBytes: number;
43 readonly threadSnapshotWireBytes: number;
44 readonly measuredTurnWebSocketWireBytes: number;
45 readonly measuredTurnWebSocketDecodedBytes: number;
46 readonly measuredTurnWebSocketMessages: number;
48​
49const TRANSFER_BUDGET = {
50 totalWireBytes: 7_000,
51 threadSnapshotWireBytes: 5_000,
52 measuredTurnWebSocketWireBytes: 2_000,
53 measuredTurnWebSocketDecodedBytes: 30_000,
54 measuredTurnWebSocketMessages: 8,
55} satisfies ProviderTransferBudget;
56​
57const TRANSFER_BUDGETS: Readonly<Record<string, ProviderTransferBudget>> = {
58 codex: TRANSFER_BUDGET,
59 claudeAgent: TRANSFER_BUDGET,
60};
61​
62// Decoded bytes, not wire bytes: phones parse and keep the decoded JSON. About
63// 30% above the observed 92 KB; without row budgets inside user turns the cold
64// open carried all 920 rows (1.07 MB). Kept apart from TRANSFER_BUDGET because
65// the PR comment validates that object's keys.
66const LONG_TURN_BUDGET = {
67 decodedBytes: 120_000,
68 rows: 75,
69};
70​
71function totalWireBytes(run: TransferBudgetRun): number {
72 return run.threadSnapshot.wireBytes + run.measuredTurnWebSocket.wireBytes;
74​
75function serverEgressWireBytes(run: TransferBudgetRun): number {
76 return (
77 run.measuredTurnWebSocket.wireBytes +
78 run.measuredTurnShellWebSocket.wireBytes +
79 run.measuredTurnSecondClientWebSocket.wireBytes
80 );
82​
83function observedTransfer(run: TransferBudgetRun) {
84 return {
85 totalWireBytes: totalWireBytes(run),
86 threadSnapshotWireBytes: run.threadSnapshot.wireBytes,
87 threadSnapshotDecodedBytes: run.threadSnapshot.decodedBodyBytes,
88 measuredTurnWebSocketWireBytes: run.measuredTurnWebSocket.wireBytes,
89 measuredTurnWebSocketDecodedBytes: run.measuredTurnWebSocket.decodedBytes,
90 measuredTurnWebSocketMessages: run.measuredTurnWebSocket.messages,
91 };
93​
94export function formatTransferBudgetResult(runs: ReadonlyArray<TransferBudgetRun>): string {
95 const providers = Object.fromEntries(
96 runs.flatMap((run) => {
97 const ceiling = TRANSFER_BUDGETS[run.provider];
98 return ceiling ? [[run.provider, { observed: observedTransfer(run), ceiling }]] : [];
99 }),
100 );
101​
102 return `${JSON.stringify(
103 {
104 schemaVersion: 1,
105 scenario: {
106 id: "thread-transfer-v2",
107 historyTurns: TRANSFER_HISTORY_TURN_COUNT,
108 historyCommandToolsPerTurn: TRANSFER_HISTORY_TOOLS_PER_TURN,
109 historyMcpResultBytes: TRANSFER_HISTORY_MCP_RESULT_BYTES,
110 measuredCommandTools: TRANSFER_MEASURED_TOOLS,
111 measuredMcpResultBytes: TRANSFER_MEASURED_MCP_RESULT_BYTES,
112 },
113 providers,
114 },
115 null,
116 2,
117 )}\n`;
119​
120function formatBytes(bytes: number): string {
121 if (bytes < 1_024) return `${bytes} B`;
122 if (bytes >= 1_024 * 1_024) {
123 return `${(bytes / 1_024 / 1_024).toFixed(2)} MiB (${bytes.toLocaleString("en-US")} B)`;
124 }
125 return `${(bytes / 1_024).toFixed(1)} KiB (${bytes.toLocaleString("en-US")} B)`;
127​
128function row(
129 provider: ProviderDriverKind,
130 phase: string,
131 metric: string,
132 observed: number,
133 maximum: number,
134 format: (value: number) => string = formatBytes,
135): string {
136 const status = observed <= maximum ? "PASS" : "FAIL";
137 return `| ${provider} | ${phase} | ${metric} | ${format(observed)} | ${format(maximum)} | ${status} |`;
139​
140// The 50 ms shell batching makes transfer, reconnect and SQL figures timing-dependent.
141function infoRow(
142 provider: ProviderDriverKind,
143 phase: string,
144 metric: string,
145 observed: number,
146 format: (value: number) => string = formatBytes,
147): string {
148 return `| ${provider} | ${phase} | ${metric} | ${format(observed)} | none | INFO |`;
150​
151function webSocketRows(
152 provider: ProviderDriverKind,
153 phase: string,
154 totals: WebSocketTransferTotals,
155): string[] {
156 return [
157 infoRow(provider, phase, "WebSocket wire", totals.wireBytes),
158 infoRow(provider, phase, "WebSocket decoded", totals.decodedBytes),
159 infoRow(provider, phase, "WebSocket messages", totals.messages, String),
160 ];
162​
163function longTurnRows(
164 provider: ProviderDriverKind,
165 phase: string,
166 measured: TimelineHttpMeasurement,
167): string[] {
168 return [
169 infoRow(provider, phase, "HTTP wire", measured.http.wireBytes),
170 row(
171 provider,
172 phase,
173 "HTTP decoded",
174 measured.http.decodedBodyBytes,
175 LONG_TURN_BUDGET.decodedBytes,
176 ),
177 row(provider, phase, "rows", measured.rows, LONG_TURN_BUDGET.rows, String),
178 ];
180​
181export function transferBudgetViolations(runs: ReadonlyArray<TransferBudgetRun>): string[] {
182 const violations: string[] = [];
183 for (const run of runs) {
184 const budget = TRANSFER_BUDGETS[run.provider];
185 if (!budget) {
186 violations.push(`${run.provider}: no transfer budget is configured`);
187 continue;
188 }
189 const checks = [
190 ["total thread wire bytes", totalWireBytes(run), budget.totalWireBytes],
191 ["thread snapshot wire bytes", run.threadSnapshot.wireBytes, budget.threadSnapshotWireBytes],
192 [
193 "measured-turn WebSocket wire bytes",
194 run.measuredTurnWebSocket.wireBytes,
195 budget.measuredTurnWebSocketWireBytes,
196 ],
197 [
198 "measured-turn WebSocket decoded bytes",
199 run.measuredTurnWebSocket.decodedBytes,
200 budget.measuredTurnWebSocketDecodedBytes,
201 ],
202 [
203 "measured-turn WebSocket messages",
204 run.measuredTurnWebSocket.messages,
205 budget.measuredTurnWebSocketMessages,
206 ],
207 [
208 "long-turn cold open decoded bytes",
209 run.longTurnColdOpen.http.decodedBodyBytes,
210 LONG_TURN_BUDGET.decodedBytes,
211 ],
212 ["long-turn cold open rows", run.longTurnColdOpen.rows, LONG_TURN_BUDGET.rows],
213 [
214 "long-turn history page decoded bytes",
215 run.longTurnHistoryPage.http.decodedBodyBytes,
216 LONG_TURN_BUDGET.decodedBytes,
217 ],
218 ["long-turn history page rows", run.longTurnHistoryPage.rows, LONG_TURN_BUDGET.rows],
219 ] as const;
220 for (const [metric, observed, maximum] of checks) {
221 if (observed > maximum) {
222 violations.push(`${run.provider}: ${metric} was ${observed}, maximum ${maximum}`);
223 }
224 }
225 }
226 return violations;
228​
229export function formatTransferBudgetReport(runs: ReadonlyArray<TransferBudgetRun>): string {
230 const lines = [
231 "# T3 Code thread transfer budget",
232 "",
233 "Wire values are thread data bytes read from local HTTP and WebSocket sockets. HTTP includes response headers; WebSocket measurement starts after the resumed thread subscription synchronizes. TCP/IP, TLS framing, and the WebSocket upgrade are excluded. WebSocket permessage-deflate is negotiated.",
234 "The measured turn is observed on three sockets at once: one with only the thread subscription (the capped rows), one with only the shell subscription, and a second client holding both. Server egress is the sum of the three. After the turn the second client disconnects and new sockets resubscribe from the cursor it held before the turn, which is the cursor a backgrounded phone would hold. SQL statements are the SQL client's per-statement spans, counted across V2 persistence and the HTTP/WebSocket handlers.",
235 `Scenario: ${TRANSFER_HISTORY_TURN_COUNT} historical turns with ${TRANSFER_HISTORY_TOOLS_PER_TURN} command tools and one retained ${formatBytes(TRANSFER_HISTORY_MCP_RESULT_BYTES)} MCP result each, followed by one measured turn with ${TRANSFER_MEASURED_TOOLS} command tools and a retained ${formatBytes(TRANSFER_MEASURED_MCP_RESULT_BYTES)} MCP result. Synthetic V2 domain events exercise persistence, wire projection, and production HTTP/subscription handlers; this does not measure provider adapter ingestion. Payloads contain no user data.`,
236 `The long-turn rows open a second thread through the compact bounded snapshot web and mobile use, then load one older page. Its last user turn holds ${LONG_TURN_WORK_ROWS} rows of reasoning, commands, file changes, MCP calls, progress messages, checkpoints, and subagents.`,
237 "",
238 "| Provider | Total thread wire | Budget | Result |",
239 "| --- | ---: | ---: | --- |",
240 ...runs.flatMap((run) => {
241 const budget = TRANSFER_BUDGETS[run.provider];
242 if (!budget) return [];
243 const observed = observedTransfer(run).totalWireBytes;
244 return [
245 `| ${run.provider} | ${formatBytes(observed)} | ${formatBytes(budget.totalWireBytes)} | ${observed <= budget.totalWireBytes ? "PASS" : "FAIL"} |`,
246 ];
247 }),
248 "",
249 "## Detailed measurements",
250 "",
251 "| Provider | Phase | Metric | Observed | Budget | Result |",
252 "| --- | --- | --- | ---: | ---: | --- |",
253 ];
254​
255 for (const run of runs) {
256 const budget = TRANSFER_BUDGETS[run.provider];
257 if (!budget) continue;
258 lines.push(
259 row(
260 run.provider,
261 "thread snapshot",
262 "HTTP wire",
263 run.threadSnapshot.wireBytes,
264 budget.threadSnapshotWireBytes,
265 ),
266 row(
267 run.provider,
268 "measured turn",
269 "WebSocket wire",
270 run.measuredTurnWebSocket.wireBytes,
271 budget.measuredTurnWebSocketWireBytes,
272 ),
273 row(
274 run.provider,
275 "measured turn",
276 "WebSocket decoded",
277 run.measuredTurnWebSocket.decodedBytes,
278 budget.measuredTurnWebSocketDecodedBytes,
279 ),
280 row(
281 run.provider,
282 "measured turn",
283 "WebSocket messages",
284 run.measuredTurnWebSocket.messages,
285 budget.measuredTurnWebSocketMessages,
286 String,
287 ),
288 infoRow(run.provider, "shell snapshot", "HTTP wire", run.shellSnapshot.wireBytes),
289 ...webSocketRows(run.provider, "measured turn, shell", run.measuredTurnShellWebSocket),
290 ...webSocketRows(
291 run.provider,
292 "measured turn, second client",
293 run.measuredTurnSecondClientWebSocket,
294 ),
295 infoRow(run.provider, "measured turn", "server egress wire", serverEgressWireBytes(run)),
296 infoRow(
297 run.provider,
298 "measured turn",
299 "SQL statements",
300 run.measuredTurnSqlStatements,
301 String,
302 ),
303 ...webSocketRows(
304 run.provider,
305 `reconnect, thread (${run.reconnectThread.mode})`,
306 run.reconnectThread,
307 ),
308 ...webSocketRows(
309 run.provider,
310 `reconnect, shell (${run.reconnectShell.mode})`,
311 run.reconnectShell,
312 ),
313 infoRow(run.provider, "reconnect", "SQL statements", run.reconnectSqlStatements, String),
314 ...longTurnRows(run.provider, "long-turn cold open", run.longTurnColdOpen),
315 ...longTurnRows(run.provider, "long-turn history page", run.longTurnHistoryPage),
316 );
317 }
318​
319 lines.push("", "## Compression diagnostics", "");
320 for (const run of runs) {
321 lines.push(
322 `- ${run.provider}: thread snapshot ${formatBytes(run.threadSnapshot.decodedBodyBytes)} decoded to ${formatBytes(run.threadSnapshot.encodedBodyBytes)} gzip.`,
323 `- ${run.provider}: shell snapshot ${formatBytes(run.shellSnapshot.decodedBodyBytes)} decoded to ${formatBytes(run.shellSnapshot.encodedBodyBytes)} gzip.`,
324 );
325 }
326​
327 return `${lines.join("\n")}\n`;