Open History to see the last commit
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";
14
15export interface TransferBudgetRun {
16 readonly provider: ProviderDriverKind;
17 readonly threadSnapshot: HttpTransferMeasurement;
18 readonly measuredTurnWebSocket: WebSocketTransferTotals;
19}
20
21interface ProviderTransferBudget {
22 readonly totalWireBytes: number;
23 readonly threadSnapshotWireBytes: number;
24 readonly measuredTurnWebSocketWireBytes: number;
25 readonly measuredTurnWebSocketDecodedBytes: number;
26 readonly measuredTurnWebSocketMessages: number;
27}
28
29// These caps leave roughly 30% headroom above the client projection of the
30// deterministic 9 MB retained-result fixture. Full MCP results stay in
31// persistence, so accidentally shipping them again exceeds these caps by
32// orders of magnitude. The CI report preserves exact values for review.
33const TRANSFER_BUDGET = {
34 totalWireBytes: 15_500,
35 threadSnapshotWireBytes: 7_500,
36 measuredTurnWebSocketWireBytes: 8_000,
37 measuredTurnWebSocketDecodedBytes: 68_000,
38 measuredTurnWebSocketMessages: 21,
39} satisfies ProviderTransferBudget;
40
41export const TRANSFER_BUDGETS: Readonly<Record<string, ProviderTransferBudget>> = {
42 codex: TRANSFER_BUDGET,
43 claudeAgent: TRANSFER_BUDGET,
44};
45
46function totalWireBytes(run: TransferBudgetRun): number {
47 return run.threadSnapshot.wireBytes + run.measuredTurnWebSocket.wireBytes;
48}
49
50function observedTransfer(run: TransferBudgetRun) {
51 return {
52 totalWireBytes: totalWireBytes(run),
53 threadSnapshotWireBytes: run.threadSnapshot.wireBytes,
54 threadSnapshotDecodedBytes: run.threadSnapshot.decodedBodyBytes,
55 measuredTurnWebSocketWireBytes: run.measuredTurnWebSocket.wireBytes,
56 measuredTurnWebSocketDecodedBytes: run.measuredTurnWebSocket.decodedBytes,
57 measuredTurnWebSocketMessages: run.measuredTurnWebSocket.messages,
58 };
59}
60
61/** Machine-readable input for the trusted PR comment publisher. */
62export function formatTransferBudgetResult(runs: ReadonlyArray<TransferBudgetRun>): string {
63 const providers = Object.fromEntries(
64 runs.flatMap((run) => {
65 const ceiling = TRANSFER_BUDGETS[run.provider];
66 return ceiling ? [[run.provider, { observed: observedTransfer(run), ceiling }]] : [];
67 }),
68 );
69
70 return `${JSON.stringify(
71 {
72 schemaVersion: 1,
73 scenario: {
74 id: "thread-transfer-v1",
75 historyTurns: TRANSFER_HISTORY_TURN_COUNT,
76 historyCommandToolsPerTurn: TRANSFER_HISTORY_TOOLS_PER_TURN,
77 historyMcpResultBytes: TRANSFER_HISTORY_MCP_RESULT_BYTES,
78 measuredCommandTools: TRANSFER_MEASURED_TOOLS,
79 measuredMcpResultBytes: TRANSFER_MEASURED_MCP_RESULT_BYTES,
80 },
81 providers,
82 },
83 null,
84 2,
85 )}\n`;
86}
87
88function formatBytes(bytes: number): string {
89 if (bytes < 1_024) return `${bytes} B`;
90 if (bytes >= 1_024 * 1_024) {
91 return `${(bytes / 1_024 / 1_024).toFixed(2)} MiB (${bytes.toLocaleString("en-US")} B)`;
92 }
93 return `${(bytes / 1_024).toFixed(1)} KiB (${bytes.toLocaleString("en-US")} B)`;
94}
95
96function row(
97 provider: ProviderDriverKind,
98 phase: string,
99 metric: string,
100 observed: number,
101 maximum: number,
102 format: (value: number) => string = formatBytes,
103): string {
104 const status = observed <= maximum ? "PASS" : "FAIL";
105 return `| ${provider} | ${phase} | ${metric} | ${format(observed)} | ${format(maximum)} | ${status} |`;
106}
107
108export function transferBudgetViolations(runs: ReadonlyArray<TransferBudgetRun>): string[] {
109 const violations: string[] = [];
110 for (const run of runs) {
111 const budget = TRANSFER_BUDGETS[run.provider];
112 if (!budget) {
113 violations.push(`${run.provider}: no transfer budget is configured`);
114 continue;
115 }
116 const checks = [
117 ["total thread wire bytes", totalWireBytes(run), budget.totalWireBytes],
118 ["thread snapshot wire bytes", run.threadSnapshot.wireBytes, budget.threadSnapshotWireBytes],
119 [
120 "measured-turn WebSocket wire bytes",
121 run.measuredTurnWebSocket.wireBytes,
122 budget.measuredTurnWebSocketWireBytes,
123 ],
124 [
125 "measured-turn WebSocket decoded bytes",
126 run.measuredTurnWebSocket.decodedBytes,
127 budget.measuredTurnWebSocketDecodedBytes,
128 ],
129 [
130 "measured-turn WebSocket messages",
131 run.measuredTurnWebSocket.messages,
132 budget.measuredTurnWebSocketMessages,
133 ],
134 ] as const;
135 for (const [metric, observed, maximum] of checks) {
136 if (observed > maximum) {
137 violations.push(`${run.provider}: ${metric} was ${observed}, maximum ${maximum}`);
138 }
139 }
140 }
141 return violations;
142}
143
144export function formatTransferBudgetReport(runs: ReadonlyArray<TransferBudgetRun>): string {
145 const lines = [
146 "# T3 Code thread transfer budget",
147 "",
148 "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.",
149 `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. Payload sizes are calibrated from heavy local Codex and Claude histories and contain no user data.`,
150 "",
151 "| Provider | Total thread wire | Budget | Result |",
152 "| --- | ---: | ---: | --- |",
153 ...runs.flatMap((run) => {
154 const budget = TRANSFER_BUDGETS[run.provider];
155 if (!budget) return [];
156 const observed = observedTransfer(run).totalWireBytes;
157 return [
158 `| ${run.provider} | ${formatBytes(observed)} | ${formatBytes(budget.totalWireBytes)} | ${observed <= budget.totalWireBytes ? "PASS" : "FAIL"} |`,
159 ];
160 }),
161 "",
162 "## Detailed measurements",
163 "",
164 "| Provider | Phase | Metric | Observed | Budget | Result |",
165 "| --- | --- | --- | ---: | ---: | --- |",
166 ];
167
168 for (const run of runs) {
169 const budget = TRANSFER_BUDGETS[run.provider];
170 if (!budget) continue;
171 lines.push(
172 row(
173 run.provider,
174 "thread snapshot",
175 "HTTP wire",
176 run.threadSnapshot.wireBytes,
177 budget.threadSnapshotWireBytes,
178 ),
179 row(
180 run.provider,
181 "measured turn",
182 "WebSocket wire",
183 run.measuredTurnWebSocket.wireBytes,
184 budget.measuredTurnWebSocketWireBytes,
185 ),
186 row(
187 run.provider,
188 "measured turn",
189 "WebSocket decoded",
190 run.measuredTurnWebSocket.decodedBytes,
191 budget.measuredTurnWebSocketDecodedBytes,
192 ),
193 row(
194 run.provider,
195 "measured turn",
196 "WebSocket messages",
197 run.measuredTurnWebSocket.messages,
198 budget.measuredTurnWebSocketMessages,
199 String,
200 ),
201 );
202 }
203
204 lines.push("", "## Compression diagnostics", "");
205 for (const run of runs) {
206 lines.push(
207 `- ${run.provider}: thread snapshot ${formatBytes(run.threadSnapshot.decodedBodyBytes)} decoded to ${formatBytes(run.threadSnapshot.encodedBodyBytes)} gzip.`,
208 );
209 }
210
211 return `${lines.join("\n")}\n`;
212}