Skip to content

Commit 1300b22

Browse files
committed
fix(qa-lab): classify runtime token efficiency
1 parent 29653e4 commit 1300b22

8 files changed

Lines changed: 669 additions & 9 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ Docs: https://docs.openclaw.ai
5353
- Agents/subagents: require the initial subagent registry save before reporting spawn accepted, returning a spawn error instead of losing an untracked run when the registry write fails. (#83146) Thanks @yetval.
5454
- QA-Lab/qa-channel: attach redacted agent tool-start traces to outbound `QaBusMessage` records so scenarios can assert actual tool use instead of relying only on reply text. Fixes #67637. Thanks @100yenadmin.
5555
- QA-Lab: fail live runtime parity reports when assistant-message usage is missing, preventing `0 vs 0` live token rows from being reported as passing proof. Fixes #80411. Thanks @100yenadmin.
56+
- QA-Lab: add a runtime token-efficiency sidecar report that classifies Codex savings separately from regressions and fails only positive Codex-over-Pi live token deltas above threshold. Fixes #81093. Thanks @100yenadmin.
5657
- QA-Lab: fail Codex-backed OpenAI live runtime-pair runs before launching isolated workers when no portable Codex auth is available, while staging API-key fallbacks and configured Codex keys for isolated QA agents. Fixes #80412. Thanks @100yenadmin.
5758
- QA-Lab: refresh parity gates, mock frontier fixtures, model scenarios, and workflow artifact lanes to compare GPT-5.5 against Claude Opus 4.7. Fixes #74262. Thanks @100yenadmin.
5859
- QA-Lab: make mock parity dispatch provider-aware for source discovery and subagent scenarios so OpenAI and Anthropic lanes no longer share identical canned plans. Fixes #64879. Thanks @100yenadmin.

‎docs/concepts/qa-e2e-automation.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ script aliases; both forms are supported.
3434
| `qa run` | Bundled QA self-check; writes a Markdown report. |
3535
| `qa suite` | Run repo-backed scenarios against the QA gateway lane. Aliases: `pnpm openclaw qa suite --runner multipass` for a disposable Linux VM. |
3636
| `qa coverage` | Print the markdown scenario-coverage inventory (`--json` for machine output). |
37-
| `qa parity-report` | Compare two `qa-suite-summary.json` files and write the agentic parity report. |
37+
| `qa parity-report` | Compare two `qa-suite-summary.json` files and write the agentic parity report, or use `--runtime-axis --token-efficiency` to write Codex-vs-Pi runtime parity and token-efficiency reports from one runtime-pair summary. |
3838
| `qa character-eval` | Run the character QA scenario across multiple live models with a judged report. See [Reporting](#reporting). |
3939
| `qa manual` | Run a one-off prompt against the selected provider/model lane. |
4040
| `qa ui` | Start the QA debugger UI and local QA bus (alias: `pnpm qa:lab:ui`). |

‎extensions/qa-lab/src/cli.runtime.test.ts‎

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -965,6 +965,108 @@ describe("qa cli runtime", () => {
965965
}
966966
});
967967

968+
it("writes a runtime-axis token-efficiency report when requested", async () => {
969+
const repoRoot = await fs.mkdtemp(path.join(os.tmpdir(), "qa-runtime-token-efficiency-"));
970+
const priorExitCode = process.exitCode;
971+
process.exitCode = undefined;
972+
973+
try {
974+
await fs.writeFile(
975+
path.join(repoRoot, "runtime-summary.json"),
976+
JSON.stringify({
977+
scenarios: [
978+
{
979+
name: "runtime-tool-fs-read",
980+
status: "pass",
981+
steps: [],
982+
runtimeParity: {
983+
scenarioId: "runtime-tool-fs-read",
984+
drift: "none",
985+
cells: {
986+
pi: {
987+
runtime: "pi",
988+
transcriptBytes: '{"role":"assistant"}\n',
989+
toolCalls: [{ tool: "fs.read", argsHash: "a", resultHash: "r" }],
990+
finalText: "done",
991+
usage: { inputTokens: 72_000, outputTokens: 381, totalTokens: 72_381 },
992+
wallClockMs: 10,
993+
bootStateLines: [],
994+
},
995+
codex: {
996+
runtime: "codex",
997+
transcriptBytes: '{"role":"assistant"}\n',
998+
toolCalls: Array.from({ length: 40 }, (_, index) => ({
999+
tool: "fs.read",
1000+
argsHash: `a-${index}`,
1001+
resultHash: `r-${index}`,
1002+
})),
1003+
finalText: "done",
1004+
usage: { inputTokens: 118_000, outputTokens: 1_489, totalTokens: 119_489 },
1005+
wallClockMs: 10,
1006+
bootStateLines: [],
1007+
},
1008+
},
1009+
},
1010+
},
1011+
],
1012+
counts: { total: 1, passed: 1, failed: 0 },
1013+
run: {
1014+
providerMode: "live-frontier",
1015+
primaryModel: "openai/gpt-5.5",
1016+
runtimePair: ["pi", "codex"],
1017+
},
1018+
}),
1019+
"utf8",
1020+
);
1021+
1022+
await runQaParityReportCommand({
1023+
repoRoot,
1024+
runtimeAxis: true,
1025+
summary: "runtime-summary.json",
1026+
tokenEfficiency: true,
1027+
});
1028+
1029+
expect(process.exitCode).toBe(1);
1030+
expect(stdoutWrite).toHaveBeenCalledWith(
1031+
expect.stringContaining("QA runtime parity verdict: pass"),
1032+
);
1033+
expect(stdoutWrite).toHaveBeenCalledWith(
1034+
expect.stringContaining("QA runtime token efficiency report:"),
1035+
);
1036+
expect(stdoutWrite).toHaveBeenCalledWith(
1037+
expect.stringContaining("QA runtime token efficiency verdict: fail"),
1038+
);
1039+
const [artifactDir] = await fs.readdir(path.join(repoRoot, ".artifacts", "qa-e2e"));
1040+
const tokenSummary = JSON.parse(
1041+
await fs.readFile(
1042+
path.join(
1043+
repoRoot,
1044+
".artifacts",
1045+
"qa-e2e",
1046+
artifactDir ?? "",
1047+
"qa-runtime-token-efficiency-summary.json",
1048+
),
1049+
"utf8",
1050+
),
1051+
) as { aggregate?: { flaggedScenarios?: string[] } };
1052+
expect(tokenSummary.aggregate?.flaggedScenarios).toEqual(["runtime-tool-fs-read"]);
1053+
} finally {
1054+
process.exitCode = priorExitCode;
1055+
await fs.rm(repoRoot, { recursive: true, force: true });
1056+
}
1057+
});
1058+
1059+
it("rejects token-efficiency without runtime-axis mode", async () => {
1060+
await expect(
1061+
runQaParityReportCommand({
1062+
repoRoot: process.cwd(),
1063+
candidateSummary: "candidate.json",
1064+
baselineSummary: "baseline.json",
1065+
tokenEfficiency: true,
1066+
}),
1067+
).rejects.toThrow("--token-efficiency requires --runtime-axis.");
1068+
});
1069+
9681070
it("prints a markdown coverage report from scenario metadata", async () => {
9691071
await runQaCoverageReportCommand({ repoRoot: process.cwd() });
9701072

‎extensions/qa-lab/src/cli.runtime.ts‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ import {
5050
import { resolveQaScenarioPackScenarioIds } from "./scenario-packs.js";
5151
import { runQaSuiteFromRuntime } from "./suite-launch.runtime.js";
5252
import { readQaSuiteFailedScenarioCountFromSummary } from "./suite-summary.js";
53+
import {
54+
buildTokenEfficiencyReport,
55+
renderTokenEfficiencyMarkdownReport,
56+
type TokenEfficiencySuiteSummary,
57+
} from "./token-efficiency-report.js";
5358
import {
5459
buildQaToolCoverageReport,
5560
renderQaToolCoverageMarkdownReport,
@@ -681,8 +686,12 @@ export async function runQaParityReportCommand(opts: {
681686
outputDir?: string;
682687
runtimeAxis?: boolean;
683688
summary?: string;
689+
tokenEfficiency?: boolean;
684690
}) {
685691
const repoRoot = path.resolve(opts.repoRoot ?? process.cwd());
692+
if (opts.tokenEfficiency === true && opts.runtimeAxis !== true) {
693+
throw new Error("--token-efficiency requires --runtime-axis.");
694+
}
686695
const outputDir =
687696
resolveRepoRelativeOutputDir(repoRoot, opts.outputDir) ??
688697
path.join(repoRoot, ".artifacts", "qa-e2e", `parity-${Date.now().toString(36)}`);
@@ -706,7 +715,26 @@ export async function runQaParityReportCommand(opts: {
706715
process.stdout.write(`QA runtime parity report: ${reportPath}\n`);
707716
process.stdout.write(`QA runtime parity summary: ${runtimeSummaryPath}\n`);
708717
process.stdout.write(`QA runtime parity verdict: ${reportPayload.pass ? "pass" : "fail"}\n`);
709-
if (!reportPayload.pass) {
718+
719+
let tokenEfficiencyPass = true;
720+
if (opts.tokenEfficiency === true) {
721+
const tokenPayload = buildTokenEfficiencyReport({
722+
summary: summary as TokenEfficiencySuiteSummary,
723+
});
724+
tokenEfficiencyPass = tokenPayload.pass;
725+
const tokenReport = renderTokenEfficiencyMarkdownReport(tokenPayload);
726+
const tokenReportPath = path.join(outputDir, "qa-runtime-token-efficiency-report.md");
727+
const tokenSummaryPath = path.join(outputDir, "qa-runtime-token-efficiency-summary.json");
728+
await fs.writeFile(tokenReportPath, tokenReport, "utf8");
729+
await fs.writeFile(tokenSummaryPath, `${JSON.stringify(tokenPayload, null, 2)}\n`, "utf8");
730+
process.stdout.write(`QA runtime token efficiency report: ${tokenReportPath}\n`);
731+
process.stdout.write(`QA runtime token efficiency summary: ${tokenSummaryPath}\n`);
732+
process.stdout.write(
733+
`QA runtime token efficiency verdict: ${tokenPayload.status === "skipped" ? "skipped" : tokenPayload.pass ? "pass" : "fail"}\n`,
734+
);
735+
}
736+
737+
if (!reportPayload.pass || !tokenEfficiencyPass) {
710738
process.exitCode = 1;
711739
}
712740
return;

‎extensions/qa-lab/src/cli.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ async function runQaParityReport(opts: {
6666
outputDir?: string;
6767
runtimeAxis?: boolean;
6868
summary?: string;
69+
tokenEfficiency?: boolean;
6970
}) {
7071
const runtime = await loadQaLabCliRuntime();
7172
await runtime.runQaParityReportCommand(opts);
@@ -353,6 +354,11 @@ export function registerQaLabCli(program: Command) {
353354
.option("--baseline-summary <path>", "Baseline qa-suite-summary.json path")
354355
.option("--runtime-axis", "Interpret --summary as a runtime-pair qa-suite-summary.json", false)
355356
.option("--summary <path>", "Runtime-axis qa-suite-summary.json path")
357+
.option(
358+
"--token-efficiency",
359+
"Also write the runtime token-efficiency report for --runtime-axis summaries",
360+
false,
361+
)
356362
.option("--repo-root <path>", "Repository root to target when running from a neutral cwd")
357363
.option(
358364
"--candidate-label <label>",
@@ -371,6 +377,7 @@ export function registerQaLabCli(program: Command) {
371377
outputDir?: string;
372378
runtimeAxis?: boolean;
373379
summary?: string;
380+
tokenEfficiency?: boolean;
374381
}) => {
375382
await runQaParityReport(opts);
376383
},
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
import { describe, expect, it } from "vitest";
2+
import type {
3+
RuntimeId,
4+
RuntimeParityCell,
5+
RuntimeParityResult,
6+
RuntimeParityToolCall,
7+
} from "./runtime-parity.js";
8+
import {
9+
buildTokenEfficiencyReport,
10+
renderTokenEfficiencyMarkdownReport,
11+
type TokenEfficiencySuiteSummary,
12+
} from "./token-efficiency-report.js";
13+
14+
function makeToolCall(tool: string): RuntimeParityToolCall {
15+
return {
16+
tool,
17+
argsHash: `${tool}-args`,
18+
resultHash: `${tool}-result`,
19+
};
20+
}
21+
22+
function makeCell(
23+
runtime: RuntimeId,
24+
usage: RuntimeParityCell["usage"],
25+
toolCalls: RuntimeParityToolCall[] = [],
26+
): RuntimeParityCell {
27+
return {
28+
runtime,
29+
transcriptBytes: '{"role":"assistant"}\n',
30+
toolCalls,
31+
finalText: "done",
32+
usage,
33+
wallClockMs: 10,
34+
bootStateLines: [],
35+
};
36+
}
37+
38+
function makeRuntimeParity(
39+
scenarioId: string,
40+
pi: RuntimeParityCell,
41+
codex: RuntimeParityCell,
42+
): RuntimeParityResult {
43+
return {
44+
scenarioId,
45+
drift: "none",
46+
cells: { pi, codex },
47+
};
48+
}
49+
50+
function makeLiveSummary(runtimeParity: RuntimeParityResult[]): TokenEfficiencySuiteSummary {
51+
return {
52+
scenarios: runtimeParity.map((result) => ({
53+
name: result.scenarioId,
54+
status: "pass" as const,
55+
runtimeParity: result,
56+
})),
57+
run: {
58+
providerMode: "live-frontier",
59+
runtimePair: ["pi", "codex"],
60+
},
61+
};
62+
}
63+
64+
describe("token efficiency report", () => {
65+
it("does not fail live reports solely because Codex uses fewer tokens", () => {
66+
const report = buildTokenEfficiencyReport({
67+
generatedAt: "2026-05-10T00:00:00.000Z",
68+
summary: makeLiveSummary([
69+
makeRuntimeParity(
70+
"codex-savings",
71+
makeCell("pi", { inputTokens: 120, outputTokens: 80, totalTokens: 200 }),
72+
makeCell("codex", { inputTokens: 60, outputTokens: 40, totalTokens: 100 }),
73+
),
74+
]),
75+
});
76+
77+
expect(report.pass).toBe(true);
78+
expect(report.aggregate.flaggedScenarios).toEqual([]);
79+
expect(report.aggregate.savingsScenarios).toEqual(["codex-savings"]);
80+
expect(report.rows[0]).toMatchObject({
81+
deltaPercent: -50,
82+
classification: "savings",
83+
flagged: false,
84+
});
85+
});
86+
87+
it("fails live reports on positive Codex token increases over the threshold", () => {
88+
const report = buildTokenEfficiencyReport({
89+
generatedAt: "2026-05-10T00:00:00.000Z",
90+
summary: makeLiveSummary([
91+
makeRuntimeParity(
92+
"runtime-tool-fs-read",
93+
makeCell("pi", { inputTokens: 72_000, outputTokens: 381, totalTokens: 72_381 }, [
94+
makeToolCall("fs.read"),
95+
makeToolCall("fs.read"),
96+
]),
97+
makeCell(
98+
"codex",
99+
{ inputTokens: 118_000, outputTokens: 1_489, totalTokens: 119_489 },
100+
Array.from({ length: 40 }, () => makeToolCall("fs.read")),
101+
),
102+
),
103+
]),
104+
});
105+
106+
expect(report.pass).toBe(false);
107+
expect(report.aggregate.flaggedScenarios).toEqual(["runtime-tool-fs-read"]);
108+
expect(report.rows[0]).toMatchObject({
109+
classification: "regression",
110+
flagged: true,
111+
toolsUsed: ["fs.read"],
112+
});
113+
expect(report.failures).toEqual([
114+
"runtime-tool-fs-read token delta=+65.1% exceeds 15.0% Codex increase threshold",
115+
]);
116+
});
117+
118+
it("keeps live zero-usage rows failing instead of passing as neutral", () => {
119+
const report = buildTokenEfficiencyReport({
120+
summary: makeLiveSummary([
121+
makeRuntimeParity(
122+
"missing-live-usage",
123+
makeCell("pi", { inputTokens: 0, outputTokens: 0, totalTokens: 0 }),
124+
makeCell("codex", { inputTokens: 0, outputTokens: 0, totalTokens: 0 }),
125+
),
126+
]),
127+
});
128+
129+
expect(report.pass).toBe(false);
130+
expect(report.failures).toEqual([
131+
"missing-live-usage pi live usage totalTokens=0",
132+
"missing-live-usage codex live usage totalTokens=0",
133+
]);
134+
});
135+
136+
it("labels mock-estimated Codex increases as regressions without failing the live gate", () => {
137+
const report = buildTokenEfficiencyReport({
138+
summary: {
139+
scenarios: [
140+
{
141+
name: "mock-regression",
142+
status: "pass",
143+
runtimeParity: makeRuntimeParity(
144+
"mock-regression",
145+
makeCell("pi", { inputTokens: 100, outputTokens: 0, totalTokens: 100 }),
146+
makeCell("codex", { inputTokens: 130, outputTokens: 0, totalTokens: 130 }),
147+
),
148+
},
149+
],
150+
run: {
151+
providerMode: "mock-openai",
152+
runtimePair: ["pi", "codex"],
153+
},
154+
},
155+
});
156+
157+
expect(report.status).toBe("estimated");
158+
expect(report.pass).toBe(true);
159+
expect(report.aggregate.flaggedScenarios).toEqual([]);
160+
expect(report.rows[0]).toMatchObject({
161+
usageSource: "mock-estimate",
162+
classification: "regression",
163+
flagged: false,
164+
});
165+
});
166+
167+
it("renders savings and regression classifications in the markdown report", () => {
168+
const report = buildTokenEfficiencyReport({
169+
generatedAt: "2026-05-10T00:00:00.000Z",
170+
summary: makeLiveSummary([
171+
makeRuntimeParity(
172+
"codex-savings",
173+
makeCell("pi", { inputTokens: 100, outputTokens: 100, totalTokens: 200 }),
174+
makeCell("codex", { inputTokens: 50, outputTokens: 50, totalTokens: 100 }),
175+
),
176+
makeRuntimeParity(
177+
"codex-regression",
178+
makeCell("pi", { inputTokens: 100, outputTokens: 0, totalTokens: 100 }),
179+
makeCell("codex", { inputTokens: 130, outputTokens: 0, totalTokens: 130 }),
180+
),
181+
]),
182+
});
183+
184+
const markdown = renderTokenEfficiencyMarkdownReport(report);
185+
expect(markdown).toContain("p50 per scenario");
186+
expect(markdown).toContain("| codex-savings | live-usage |");
187+
expect(markdown).toContain("| -50.0% | savings | no |");
188+
expect(markdown).toContain("| codex-regression | live-usage |");
189+
expect(markdown).toContain("| +30.0% | regression | yes |");
190+
});
191+
});

0 commit comments

Comments
 (0)