@@ -3,6 +3,7 @@ const path = require("node:path"); |
3 | 3 |
|
4 | 4 | const ARTIFACT_NAME = "thread-transfer-results"; |
5 | 5 | const RESULT_FILE = "thread-transfer-result.json"; |
| 6 | +const SCENARIOS_FILE = "thread-transfer-scenarios.json"; |
6 | 7 | const COMMENT_MARKER = "<!-- t3-thread-transfer-report -->"; |
7 | 8 | const PROVIDERS = ["codex", "claudeAgent"]; |
8 | 9 | const OBSERVED_KEYS = [ |
@@ -29,6 +30,12 @@ const SCENARIO_KEYS = [ |
29 | 30 | "measuredMcpResultBytes", |
30 | 31 | ]; |
31 | 32 |
|
| 33 | +const SCENARIO_ROW_KEYS = ["phase", "metric", "unit", "observed", "ceiling"]; |
| 34 | +// These labels come from the PR and reach a comment the bot posts: no markup, links or mentions. |
| 35 | +// GitHub still links plain text such as `GH-26`, so they are rendered as code spans. |
| 36 | +const SCENARIO_LABEL = /^[A-Za-z0-9][A-Za-z0-9 ,-]{0,79}$/; |
| 37 | +const MAX_SCENARIO_ROWS = 100; |
| 38 | + |
32 | 39 | function resultShaMarker(sha) { |
33 | 40 | return `<!-- t3-thread-transfer-result-sha:${sha} -->`; |
34 | 41 | } |
@@ -85,15 +92,63 @@ function validateResult(value) { |
85 | 92 | return value; |
86 | 93 | } |
87 | 94 |
|
88 | | -function readResult(directory) { |
| 95 | +function validateScenarios(value) { |
| 96 | + assertExactKeys(value, ["schemaVersion", "providers"], "scenarios"); |
| 97 | + if (value.schemaVersion !== 1) { |
| 98 | + throw new Error("scenarios.schemaVersion must be 1"); |
| 99 | + } |
| 100 | + assertExactKeys(value.providers, PROVIDERS, "scenarios.providers"); |
| 101 | + for (const provider of PROVIDERS) { |
| 102 | + const rows = value.providers[provider]; |
| 103 | + if (!Array.isArray(rows) || rows.length > MAX_SCENARIO_ROWS) { |
| 104 | + throw new Error( |
| 105 | + `scenarios.providers.${provider} must list at most ${MAX_SCENARIO_ROWS} rows`, |
| 106 | + ); |
| 107 | + } |
| 108 | + const seen = new Set(); |
| 109 | + for (const [index, row] of rows.entries()) { |
| 110 | + const label = `scenarios.providers.${provider}[${index}]`; |
| 111 | + assertExactKeys(row, SCENARIO_ROW_KEYS, label); |
| 112 | + for (const key of ["phase", "metric"]) { |
| 113 | + if (typeof row[key] !== "string" || !SCENARIO_LABEL.test(row[key])) { |
| 114 | + throw new Error(`${label}.${key} must be a short plain label`); |
| 115 | + } |
| 116 | + } |
| 117 | + if (row.unit !== "bytes" && row.unit !== "count") { |
| 118 | + throw new Error(`${label}.unit must be bytes or count`); |
| 119 | + } |
| 120 | + assertMetric(row.observed, `${label}.observed`); |
| 121 | + assertMetric(row.ceiling, `${label}.ceiling`); |
| 122 | + const key = scenarioKey(row); |
| 123 | + if (seen.has(key)) throw new Error(`${label} repeats an earlier row`); |
| 124 | + seen.add(key); |
| 125 | + } |
| 126 | + } |
| 127 | + return value; |
| 128 | +} |
| 129 | + |
| 130 | +function scenarioKey(row) { |
| 131 | + return `${row.phase}|${row.metric}`; |
| 132 | +} |
| 133 | + |
| 134 | +function readArtifactFile(directory, name, validate) { |
89 | 135 | if (!directory) return undefined; |
90 | | - const file = path.join(directory, RESULT_FILE); |
| 136 | + const file = path.join(directory, name); |
91 | 137 | if (!fs.existsSync(file)) return undefined; |
92 | 138 | const stat = fs.lstatSync(file); |
93 | 139 | if (!stat.isFile() || stat.size > 64 * 1_024) { |
94 | | - throw new Error("thread transfer result must be a regular file smaller than 64 KiB"); |
| 140 | + throw new Error(`${name} must be a regular file smaller than 64 KiB`); |
95 | 141 | } |
96 | | - return validateResult(JSON.parse(fs.readFileSync(file, "utf8"))); |
| 142 | + return validate(JSON.parse(fs.readFileSync(file, "utf8"))); |
| 143 | +} |
| 144 | + |
| 145 | +function readResult(directory) { |
| 146 | + return readArtifactFile(directory, RESULT_FILE, validateResult); |
| 147 | +} |
| 148 | + |
| 149 | +// Older runs upload only the result file, so a run without scenarios is still a valid run. |
| 150 | +function readScenarios(directory) { |
| 151 | + return readArtifactFile(directory, SCENARIOS_FILE, validateScenarios); |
97 | 152 | } |
98 | 153 |
|
99 | 154 | function formatBytes(bytes) { |
@@ -103,7 +158,7 @@ function formatBytes(bytes) { |
103 | 158 | } |
104 | 159 |
|
105 | 160 | function formatValue(value, kind) { |
106 | | - return kind === "messages" ? value.toLocaleString("en-US") : formatBytes(value); |
| 161 | + return kind === "bytes" ? formatBytes(value) : value.toLocaleString("en-US"); |
107 | 162 | } |
108 | 163 |
|
109 | 164 | function formatImpact(current, baseline, kind) { |
@@ -136,6 +191,77 @@ const METRICS = [ |
136 | 191 | { key: "measuredTurnWebSocketMessages", label: "Live turn messages", kind: "messages" }, |
137 | 192 | ]; |
138 | 193 |
|
| 194 | +function providerLabel(provider) { |
| 195 | + return provider === "codex" ? "Codex" : "Claude"; |
| 196 | +} |
| 197 | + |
| 198 | +/** |
| 199 | + * The scenario rows of a run against the same rows on `main`. Rows that fail or differ from |
| 200 | + * `main` are listed in the open; the full table is collapsed below them. |
| 201 | + */ |
| 202 | +function renderScenarios(current, baseline, ceilingChanges) { |
| 203 | + if (!current) return { lines: [], failed: false }; |
| 204 | + const header = [ |
| 205 | + "| Provider | Scenario | Metric | Main baseline | This PR | Impact | PR ceiling | |", |
| 206 | + "| --- | --- | --- | ---: | ---: | ---: | ---: | --- |", |
| 207 | + ]; |
| 208 | + const all = []; |
| 209 | + const notable = []; |
| 210 | + let failed = false; |
| 211 | + let withoutBaseline = 0; |
| 212 | + |
| 213 | + for (const provider of PROVIDERS) { |
| 214 | + const baselineRows = new Map( |
| 215 | + (baseline?.providers[provider] ?? []).map((row) => [scenarioKey(row), row]), |
| 216 | + ); |
| 217 | + for (const row of current.providers[provider]) { |
| 218 | + const before = baselineRows.get(scenarioKey(row)); |
| 219 | + const beforeObserved = before?.unit === row.unit ? before.observed : undefined; |
| 220 | + const pass = row.observed <= row.ceiling; |
| 221 | + failed ||= !pass; |
| 222 | + if (beforeObserved === undefined) withoutBaseline += 1; |
| 223 | + const line = `| ${providerLabel(provider)} | \`${row.phase}\` | \`${row.metric}\` | ${beforeObserved === undefined ? "—" : formatValue(beforeObserved, row.unit)} | ${formatValue(row.observed, row.unit)} | ${formatImpact(row.observed, beforeObserved, row.unit)} | ${formatValue(row.ceiling, row.unit)} | ${pass ? "✅" : "❌"} |`; |
| 224 | + all.push(line); |
| 225 | + if (!pass || (beforeObserved !== undefined && beforeObserved !== row.observed)) { |
| 226 | + notable.push(line); |
| 227 | + } |
| 228 | + if (beforeObserved !== undefined && before.ceiling !== row.ceiling) { |
| 229 | + ceilingChanges.push( |
| 230 | + `- ${providerLabel(provider)} \`${row.phase}\` \`${row.metric}\`: ${formatValue(before.ceiling, row.unit)} → ${formatValue(row.ceiling, row.unit)}`, |
| 231 | + ); |
| 232 | + } |
| 233 | + } |
| 234 | + } |
| 235 | + |
| 236 | + const compared = all.length - withoutBaseline; |
| 237 | + const summary = !baseline |
| 238 | + ? `No \`main\` baseline exists yet for these ${all.length} scenario measurements.` |
| 239 | + : [ |
| 240 | + notable.length > 0 |
| 241 | + ? `${notable.length} of ${all.length} scenario measurements changed or failed.` |
| 242 | + : `${compared === all.length ? "All " : ""}${compared} scenario measurements match \`main\`.`, |
| 243 | + ...(withoutBaseline > 0 ? [`${withoutBaseline} have no \`main\` baseline.`] : []), |
| 244 | + ].join(" "); |
| 245 | + return { |
| 246 | + failed, |
| 247 | + lines: [ |
| 248 | + "### Other scenarios", |
| 249 | + "", |
| 250 | + summary, |
| 251 | + ...(notable.length > 0 ? ["", ...header, ...notable] : []), |
| 252 | + "", |
| 253 | + "<details>", |
| 254 | + `<summary>All ${all.length} scenario measurements</summary>`, |
| 255 | + "", |
| 256 | + ...header, |
| 257 | + ...all, |
| 258 | + "", |
| 259 | + "</details>", |
| 260 | + "", |
| 261 | + ], |
| 262 | + }; |
| 263 | +} |
| 264 | + |
139 | 265 | function renderComment(input) { |
140 | 266 | const current = input.current; |
141 | 267 | const baseline = input.baseline; |
@@ -154,16 +280,22 @@ function renderComment(input) { |
154 | 280 | const pass = observed <= ceiling; |
155 | 281 | failed ||= !pass; |
156 | 282 | rows.push( |
157 | | - `| ${provider === "codex" ? "Codex" : "Claude"} | ${metric.label} | ${baselineObserved === undefined ? "—" : formatValue(baselineObserved, metric.kind)} | ${formatValue(observed, metric.kind)} | ${formatImpact(observed, baselineObserved, metric.kind)} | ${formatValue(ceiling, metric.kind)} | ${pass ? "✅" : "❌"} |`, |
| 283 | + `| ${providerLabel(provider)} | ${metric.label} | ${baselineObserved === undefined ? "—" : formatValue(baselineObserved, metric.kind)} | ${formatValue(observed, metric.kind)} | ${formatImpact(observed, baselineObserved, metric.kind)} | ${formatValue(ceiling, metric.kind)} | ${pass ? "✅" : "❌"} |`, |
158 | 284 | ); |
159 | 285 |
|
160 | 286 | if (comparable && baseline.providers[provider].ceiling[metric.key] !== ceiling) { |
161 | 287 | ceilingChanges.push( |
162 | | - `- ${provider === "codex" ? "Codex" : "Claude"} ${metric.label}: ${formatValue(baseline.providers[provider].ceiling[metric.key], metric.kind)} → ${formatValue(ceiling, metric.kind)}`, |
| 288 | + `- ${providerLabel(provider)} ${metric.label}: ${formatValue(baseline.providers[provider].ceiling[metric.key], metric.kind)} → ${formatValue(ceiling, metric.kind)}`, |
163 | 289 | ); |
164 | 290 | } |
165 | 291 | } |
166 | 292 | } |
| 293 | + const scenarios = renderScenarios( |
| 294 | + input.currentScenarios, |
| 295 | + input.baselineScenarios, |
| 296 | + ceilingChanges, |
| 297 | + ); |
| 298 | + failed ||= scenarios.failed; |
167 | 299 |
|
168 | 300 | const baselineLink = input.baselineRun |
169 | 301 | ? `[\`${input.baselineRun.sha.slice(0, 7)}\`](${input.baselineRun.url})` |
@@ -203,6 +335,7 @@ function renderComment(input) { |
203 | 335 | "| --- | --- | ---: | ---: | ---: | ---: | --- |", |
204 | 336 | ...rows, |
205 | 337 | "", |
| 338 | + ...scenarios.lines, |
206 | 339 | `Baseline: ${baselineLink} · PR result: ${currentLink} · Source CI: ${input.currentRun.conclusion}`, |
207 | 340 | "", |
208 | 341 | "<details>", |
@@ -212,7 +345,7 @@ function renderComment(input) { |
212 | 345 | "", |
213 | 346 | ...PROVIDERS.map( |
214 | 347 | (provider) => |
215 | | - `- ${provider === "codex" ? "Codex" : "Claude"} decoded thread snapshot: ${formatBytes(current.providers[provider].observed.threadSnapshotDecodedBytes)}`, |
| 348 | + `- ${providerLabel(provider)} decoded thread snapshot: ${formatBytes(current.providers[provider].observed.threadSnapshotDecodedBytes)}`, |
216 | 349 | ), |
217 | 350 | "", |
218 | 351 | "</details>", |
@@ -437,7 +570,14 @@ async function publish({ github, context, core }) { |
437 | 570 | url: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${process.env.BASELINE_RUN_ID}`, |
438 | 571 | } |
439 | 572 | : undefined; |
440 | | - const body = renderComment({ current, baseline, currentRun, baselineRun }); |
| 573 | + const body = renderComment({ |
| 574 | + current, |
| 575 | + baseline, |
| 576 | + currentScenarios: readScenarios(process.env.PR_RESULT_DIR), |
| 577 | + baselineScenarios: readScenarios(process.env.BASELINE_RESULT_DIR), |
| 578 | + currentRun, |
| 579 | + baselineRun, |
| 580 | + }); |
441 | 581 | const published = await upsertCommentForCurrentHead( |
442 | 582 | github, |
443 | 583 | context, |
@@ -458,4 +598,5 @@ module.exports = { |
458 | 598 | resolve, |
459 | 599 | upsertCommentForCurrentHead, |
460 | 600 | validateResult, |
| 601 | + validateScenarios, |
461 | 602 | }; |