@@ -4,6 +4,7 @@ const path = require("node:path"); |
4 | 4 |
|
5 | 5 | const ARTIFACT_NAME = "thread-transfer-results"; |
6 | 6 | const RESULT_FILE = "thread-transfer-result.json"; |
| 7 | +const SCENARIOS_FILE = "thread-transfer-scenarios.json"; |
7 | 8 | const COMMENT_MARKER = "<!-- t3-thread-transfer-report -->"; |
8 | 9 | const PROVIDERS = ["codex", "claudeAgent"]; |
9 | 10 | const OBSERVED_KEYS = [ |
@@ -30,6 +31,12 @@ const SCENARIO_KEYS = [ |
30 | 31 | "measuredMcpResultBytes", |
31 | 32 | ]; |
32 | 33 |
|
| 34 | +const SCENARIO_ROW_KEYS = ["phase", "metric", "unit", "observed", "ceiling"]; |
| 35 | +// These labels come from the PR and reach a comment the bot posts: no markup, links or mentions. |
| 36 | +// GitHub still links plain text such as `GH-26`, so they are rendered as code spans. |
| 37 | +const SCENARIO_LABEL = /^[A-Za-z0-9][A-Za-z0-9 ,-]{0,79}$/; |
| 38 | +const MAX_SCENARIO_ROWS = 100; |
| 39 | + |
33 | 40 | function resultShaMarker(sha) { |
34 | 41 | return `<!-- t3-thread-transfer-result-sha:${sha} -->`; |
35 | 42 | } |
@@ -86,15 +93,63 @@ function validateResult(value) { |
86 | 93 | return value; |
87 | 94 | } |
88 | 95 |
|
89 | | -function readResult(directory) { |
| 96 | +function validateScenarios(value) { |
| 97 | + assertExactKeys(value, ["schemaVersion", "providers"], "scenarios"); |
| 98 | + if (value.schemaVersion !== 1) { |
| 99 | + throw new Error("scenarios.schemaVersion must be 1"); |
| 100 | + } |
| 101 | + assertExactKeys(value.providers, PROVIDERS, "scenarios.providers"); |
| 102 | + for (const provider of PROVIDERS) { |
| 103 | + const rows = value.providers[provider]; |
| 104 | + if (!Array.isArray(rows) || rows.length > MAX_SCENARIO_ROWS) { |
| 105 | + throw new Error( |
| 106 | + `scenarios.providers.${provider} must list at most ${MAX_SCENARIO_ROWS} rows`, |
| 107 | + ); |
| 108 | + } |
| 109 | + const seen = new Set(); |
| 110 | + for (const [index, row] of rows.entries()) { |
| 111 | + const label = `scenarios.providers.${provider}[${index}]`; |
| 112 | + assertExactKeys(row, SCENARIO_ROW_KEYS, label); |
| 113 | + for (const key of ["phase", "metric"]) { |
| 114 | + if (typeof row[key] !== "string" || !SCENARIO_LABEL.test(row[key])) { |
| 115 | + throw new Error(`${label}.${key} must be a short plain label`); |
| 116 | + } |
| 117 | + } |
| 118 | + if (row.unit !== "bytes" && row.unit !== "count") { |
| 119 | + throw new Error(`${label}.unit must be bytes or count`); |
| 120 | + } |
| 121 | + assertMetric(row.observed, `${label}.observed`); |
| 122 | + assertMetric(row.ceiling, `${label}.ceiling`); |
| 123 | + const key = scenarioKey(row); |
| 124 | + if (seen.has(key)) throw new Error(`${label} repeats an earlier row`); |
| 125 | + seen.add(key); |
| 126 | + } |
| 127 | + } |
| 128 | + return value; |
| 129 | +} |
| 130 | + |
| 131 | +function scenarioKey(row) { |
| 132 | + return `${row.phase}|${row.metric}`; |
| 133 | +} |
| 134 | + |
| 135 | +function readArtifactFile(directory, name, validate) { |
90 | 136 | if (!directory) return undefined; |
91 | | - const file = path.join(directory, RESULT_FILE); |
| 137 | + const file = path.join(directory, name); |
92 | 138 | if (!fs.existsSync(file)) return undefined; |
93 | 139 | const stat = fs.lstatSync(file); |
94 | 140 | if (!stat.isFile() || stat.size > 64 * 1_024) { |
95 | | - throw new Error("thread transfer result must be a regular file smaller than 64 KiB"); |
| 141 | + throw new Error(`${name} must be a regular file smaller than 64 KiB`); |
96 | 142 | } |
97 | | - return validateResult(JSON.parse(fs.readFileSync(file, "utf8"))); |
| 143 | + return validate(JSON.parse(fs.readFileSync(file, "utf8"))); |
| 144 | +} |
| 145 | + |
| 146 | +function readResult(directory) { |
| 147 | + return readArtifactFile(directory, RESULT_FILE, validateResult); |
| 148 | +} |
| 149 | + |
| 150 | +// Older runs upload only the result file, so a run without scenarios is still a valid run. |
| 151 | +function readScenarios(directory) { |
| 152 | + return readArtifactFile(directory, SCENARIOS_FILE, validateScenarios); |
98 | 153 | } |
99 | 154 |
|
100 | 155 | function formatBytes(bytes) { |
@@ -104,7 +159,7 @@ function formatBytes(bytes) { |
104 | 159 | } |
105 | 160 |
|
106 | 161 | function formatValue(value, kind) { |
107 | | - return kind === "messages" ? value.toLocaleString("en-US") : formatBytes(value); |
| 162 | + return kind === "bytes" ? formatBytes(value) : value.toLocaleString("en-US"); |
108 | 163 | } |
109 | 164 |
|
110 | 165 | function formatImpact(current, baseline, kind) { |
@@ -137,6 +192,95 @@ const METRICS = [ |
137 | 192 | { key: "measuredTurnWebSocketMessages", label: "Live turn messages", kind: "messages" }, |
138 | 193 | ]; |
139 | 194 |
|
| 195 | +function providerLabel(provider) { |
| 196 | + return provider === "codex" ? "Codex" : "Claude"; |
| 197 | +} |
| 198 | + |
| 199 | +/** |
| 200 | + * The scenario rows of a run against the same rows on `main`. Rows that fail, differ from |
| 201 | + * `main` or are missing from the run are listed in the open; the full table is collapsed below. |
| 202 | + */ |
| 203 | +function renderScenarios(runScenarios, baseline, ceilingChanges) { |
| 204 | + // A run without the file still misses every row `main` measured. |
| 205 | + const current = |
| 206 | + runScenarios ?? |
| 207 | + (PROVIDERS.some((provider) => (baseline?.providers[provider].length ?? 0) > 0) |
| 208 | + ? { providers: Object.fromEntries(PROVIDERS.map((provider) => [provider, []])) } |
| 209 | + : undefined); |
| 210 | + if (!current) return { lines: [], failed: false, removed: 0 }; |
| 211 | + const header = [ |
| 212 | + "| Provider | Scenario | Metric | Main baseline | This PR | Impact | PR ceiling | |", |
| 213 | + "| --- | --- | --- | ---: | ---: | ---: | ---: | --- |", |
| 214 | + ]; |
| 215 | + const all = []; |
| 216 | + const notable = []; |
| 217 | + const removed = []; |
| 218 | + let failed = false; |
| 219 | + let withoutBaseline = 0; |
| 220 | + |
| 221 | + for (const provider of PROVIDERS) { |
| 222 | + const baselineRows = new Map( |
| 223 | + (baseline?.providers[provider] ?? []).map((row) => [scenarioKey(row), row]), |
| 224 | + ); |
| 225 | + for (const row of current.providers[provider]) { |
| 226 | + const before = baselineRows.get(scenarioKey(row)); |
| 227 | + baselineRows.delete(scenarioKey(row)); |
| 228 | + const beforeObserved = before?.unit === row.unit ? before.observed : undefined; |
| 229 | + const pass = row.observed <= row.ceiling; |
| 230 | + failed ||= !pass; |
| 231 | + if (beforeObserved === undefined) withoutBaseline += 1; |
| 232 | + 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 ? "✅" : "❌"} |`; |
| 233 | + all.push(line); |
| 234 | + if (!pass || (beforeObserved !== undefined && beforeObserved !== row.observed)) { |
| 235 | + notable.push(line); |
| 236 | + } |
| 237 | + if (beforeObserved !== undefined && before.ceiling !== row.ceiling) { |
| 238 | + ceilingChanges.push( |
| 239 | + `- ${providerLabel(provider)} \`${row.phase}\` \`${row.metric}\`: ${formatValue(before.ceiling, row.unit)} → ${formatValue(row.ceiling, row.unit)}`, |
| 240 | + ); |
| 241 | + } |
| 242 | + } |
| 243 | + for (const before of baselineRows.values()) { |
| 244 | + removed.push( |
| 245 | + `| ${providerLabel(provider)} | \`${before.phase}\` | \`${before.metric}\` | ${formatValue(before.observed, before.unit)} | — | removed | — | ⚠️ |`, |
| 246 | + ); |
| 247 | + } |
| 248 | + } |
| 249 | + |
| 250 | + const compared = all.length - withoutBaseline; |
| 251 | + const summary = !baseline |
| 252 | + ? `No \`main\` baseline exists yet for these ${all.length} scenario measurements.` |
| 253 | + : [ |
| 254 | + notable.length > 0 |
| 255 | + ? `${notable.length} of ${all.length} scenario measurements changed or failed.` |
| 256 | + : `${compared === all.length && removed.length === 0 ? "All " : ""}${compared} scenario measurements match \`main\`.`, |
| 257 | + ...(withoutBaseline > 0 ? [`${withoutBaseline} have no \`main\` baseline.`] : []), |
| 258 | + ...(removed.length > 0 |
| 259 | + ? [`${removed.length} measured on \`main\` are missing from this PR.`] |
| 260 | + : []), |
| 261 | + ].join(" "); |
| 262 | + const listed = [...notable, ...removed]; |
| 263 | + return { |
| 264 | + failed, |
| 265 | + removed: removed.length, |
| 266 | + lines: [ |
| 267 | + "### Other scenarios", |
| 268 | + "", |
| 269 | + summary, |
| 270 | + ...(listed.length > 0 ? ["", ...header, ...listed] : []), |
| 271 | + "", |
| 272 | + "<details>", |
| 273 | + `<summary>All ${all.length} scenario measurements</summary>`, |
| 274 | + "", |
| 275 | + ...header, |
| 276 | + ...all, |
| 277 | + "", |
| 278 | + "</details>", |
| 279 | + "", |
| 280 | + ], |
| 281 | + }; |
| 282 | +} |
| 283 | + |
140 | 284 | function renderComment(input) { |
141 | 285 | const current = input.current; |
142 | 286 | const baseline = input.baseline; |
@@ -155,16 +299,22 @@ function renderComment(input) { |
155 | 299 | const pass = observed <= ceiling; |
156 | 300 | failed ||= !pass; |
157 | 301 | rows.push( |
158 | | - `| ${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 ? "✅" : "❌"} |`, |
| 302 | + `| ${providerLabel(provider)} | ${metric.label} | ${baselineObserved === undefined ? "—" : formatValue(baselineObserved, metric.kind)} | ${formatValue(observed, metric.kind)} | ${formatImpact(observed, baselineObserved, metric.kind)} | ${formatValue(ceiling, metric.kind)} | ${pass ? "✅" : "❌"} |`, |
159 | 303 | ); |
160 | 304 |
|
161 | 305 | if (comparable && baseline.providers[provider].ceiling[metric.key] !== ceiling) { |
162 | 306 | ceilingChanges.push( |
163 | | - `- ${provider === "codex" ? "Codex" : "Claude"} ${metric.label}: ${formatValue(baseline.providers[provider].ceiling[metric.key], metric.kind)} → ${formatValue(ceiling, metric.kind)}`, |
| 307 | + `- ${providerLabel(provider)} ${metric.label}: ${formatValue(baseline.providers[provider].ceiling[metric.key], metric.kind)} → ${formatValue(ceiling, metric.kind)}`, |
164 | 308 | ); |
165 | 309 | } |
166 | 310 | } |
167 | 311 | } |
| 312 | + const scenarios = renderScenarios( |
| 313 | + input.currentScenarios, |
| 314 | + input.baselineScenarios, |
| 315 | + ceilingChanges, |
| 316 | + ); |
| 317 | + failed ||= scenarios.failed; |
168 | 318 |
|
169 | 319 | const baselineLink = input.baselineRun |
170 | 320 | ? `[\`${input.baselineRun.sha.slice(0, 7)}\`](${input.baselineRun.url})` |
@@ -192,7 +342,9 @@ function renderComment(input) { |
192 | 342 |
|
193 | 343 | return [ |
194 | 344 | COMMENT_MARKER, |
195 | | - failed || ceilingChanges.length > 0 || (baseline && !comparable) ? ATTENTION : OK, |
| 345 | + failed || ceilingChanges.length > 0 || scenarios.removed > 0 || (baseline && !comparable) |
| 346 | + ? ATTENTION |
| 347 | + : OK, |
196 | 348 | resultShaMarker(input.currentRun.sha), |
197 | 349 | "## Thread transfer impact", |
198 | 350 | "", |
@@ -205,6 +357,7 @@ function renderComment(input) { |
205 | 357 | "| --- | --- | ---: | ---: | ---: | ---: | --- |", |
206 | 358 | ...rows, |
207 | 359 | "", |
| 360 | + ...scenarios.lines, |
208 | 361 | `Baseline: ${baselineLink} · PR result: ${currentLink} · Source CI: ${input.currentRun.conclusion}`, |
209 | 362 | "", |
210 | 363 | "<details>", |
@@ -214,7 +367,7 @@ function renderComment(input) { |
214 | 367 | "", |
215 | 368 | ...PROVIDERS.map( |
216 | 369 | (provider) => |
217 | | - `- ${provider === "codex" ? "Codex" : "Claude"} decoded thread snapshot: ${formatBytes(current.providers[provider].observed.threadSnapshotDecodedBytes)}`, |
| 370 | + `- ${providerLabel(provider)} decoded thread snapshot: ${formatBytes(current.providers[provider].observed.threadSnapshotDecodedBytes)}`, |
218 | 371 | ), |
219 | 372 | "", |
220 | 373 | "</details>", |
@@ -441,7 +594,14 @@ async function publish({ github, context, core }) { |
441 | 594 | url: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${process.env.BASELINE_RUN_ID}`, |
442 | 595 | } |
443 | 596 | : undefined; |
444 | | - const body = renderComment({ current, baseline, currentRun, baselineRun }); |
| 597 | + const body = renderComment({ |
| 598 | + current, |
| 599 | + baseline, |
| 600 | + currentScenarios: readScenarios(process.env.PR_RESULT_DIR), |
| 601 | + baselineScenarios: readScenarios(process.env.BASELINE_RESULT_DIR), |
| 602 | + currentRun, |
| 603 | + baselineRun, |
| 604 | + }); |
445 | 605 | await core.summary.addRaw(body).write(); |
446 | 606 | const published = await upsertCommentForCurrentHead( |
447 | 607 | github, |
@@ -463,4 +623,5 @@ module.exports = { |
463 | 623 | resolve, |
464 | 624 | upsertCommentForCurrentHead, |
465 | 625 | validateResult, |
| 626 | + validateScenarios, |
466 | 627 | }; |