Why coding agents fake benchmarks, and the Rust guardrail I built to stop them
Whenever someone ships an optimization harness for coding agents, the first question from experienced engineers is blunt: why would I install a tool instead of just telling Claude Code or Codex to profile the repo and run hyperfine?
I ran that exact prompt loop across C, C++20, Rust, Python, and TypeScript repositories before building oa. Without external mechanical enforcement, a coding agent fails in four repeatable ways:
- It loosens a test assertion or shrinks the benchmark workload so the timing drops while the real code barely changed.
- It runs baseline once, edits code, runs candidate once, and keeps a 4% difference that sits well inside CPU frequency scaling and thermal noise.
- It introduces a fast path that silently alters edge-case output, ANSI formatting, or boundary checks.
- It buys 10% faster wall time by caching unbounded structures and inflating Peak RSS by 40% or more.
Asking an LLM to "be careful and do statistical benchmarking" fails because the same model generating the edit is grading its own work. oa moves every grading decision outside the prompt into a single 1.6 MB Rust binary (#![forbid(unsafe_code)], two direct dependencies: serde and serde_json) that speaks both CLI and stdio MCP.
1. Five mechanical gates, in order
Every candidate edit passes through the same sequence before oa allows refs/oa/best to advance:
oa profileruns the matching native or runtime profiler first (python:cProfile+tracemallocfor scripts,-m, and inline-c; V8--cpu-profand--heap-proffor Node;go test -cpuprofile/-memprofilefor Go; rootless Linuxperfwith ELFfile:linesnippets plus optional liveLD_PRELOADpeak-live heap tracking via--heap-track;/usr/bin/sampleon macOS).- Cheat Guard hashes every tracked test and benchmark file into
.oa/locks.jsononoa init. If any locked file changes by a single byte,oa evaluateaborts withCheatBlocked.oa checkpoint --keepalso checks a full working-tree SHA-256 fingerprint so nothing can be sneaked in between evaluation and keeping. - Equivalence Gate compares exit code, stdout SHA-256, stderr, and any declared
--output-filehashes against the baseline before timing starts (or runs a custom--oraclevalidator when outputs include timestamps or tolerances). - Interleaved A/B timing checks out
refs/oa/bestinto a detached git shadow worktree under.oa/worktrees/(ignored via.git/info/exclude, never touching git history) and runs balancedA-B/B-Apairs back to back. Sequential runs like plainhyperfinelet laptop boost clocks or background daemons skew one side; alternating pairs cancel monotonic drift. - Statistical and Pareto gating requires two-sided Mann-Whitney U
p <= 0.01and a 2,000-resample bootstrap 95% confidence interval lower bound above--min-speedup(default1.03x). Under--metric pareto, a wall-time speedup is automatically rejected if Peak RSS exceeds--max-rss-regression(default1.05x), and a memory cut is rejected if wall time regresses past--max-wall-regression.
2. Why live peak-heap attribution matters
Linux perf page-faults is useful with zero setup, but page faults only fire the first time a virtual page is touched. Once an allocator arena grows and reuses freed pages, page faults go quiet even while a hot path allocates gigabytes of temporary buffers.
Passing --heap-track to oa profile or oa compare compiles a small C LD_PRELOAD interposer on demand, tracks live bytes across malloc, calloc, realloc, and free, records the exact allocation call stacks sitting live at the peak high-water mark, and resolves addresses to project file:line via addr2line.
Running oa compare --git main --check-output --heap-track on sharkdp/bat #4165 immediately exposed why bat --style=plain allocated over 1 MiB of live heap just to stream a file: eager compilation of 129 builtin globset regex DFAs on every startup and eager theme deserialization even when syntax highlighting was disabled. Replacing the 129 regex DFAs with compile-time FastMatcher and ASCII prefilters, lazily initializing themes with OnceCell, recycling line buffers, and buffering owned stdout writes (flushing per input file so I/O cycle detection stays intact) dropped plain/piped runtime from 11.49 ms to 3.90 ms (2.97x) and peak-live heap from 1,042.0 KiB to 117.7 KiB (-88.7%, with -94.3% total heap churn).
3. Real upstream PRs and what the guardrails rejected
Synthetic demos inside a tool's own repository prove very little. Below are real upstream pull requests measured with oa across Rust, C, C++20, and Python:
| Repository & PR | Status | Wall (median) | Memory / Heap | Correctness check |
|---|---|---|---|---|
| sharkdp/bat #4154 | Merged (v0.26.0) |
49.42 -> 26.88 ms (1.80x) | Unchanged | Full suite + SHA-256 |
| Delgan/loguru #1557 | Merged (4 commits) | 1.04x to 1.16x per commit | -2.9% RSS | 22/22 CI checks |
| AnyPS5 #28125 | Approved (2 reviews) | 617.3 -> 122.6 ms (3.26x to 5.03x) | 164.9 -> 54.4 MiB (-67.0%) | 36/36 CTests + exact ELF bytes |
| AnyPS5 #28223 | Approved | 109.66 -> 63.87 ms (1.70x) | -0.3% to +1.3% RSS | 472,032-vector differential oracle |
| sharkdp/bat #4165 | Open (CI green) | 11.49 -> 3.90 ms (2.97x plain), 1.70x full | -88.7% / -37.0% peak-live heap | 503/503 cargo tests + 5-mode SHA-256 |
| bootandy/dust #664 | Open (CI green) | 1.56x default, 2.10x -D, 2.33x -t |
-56.6% to -72.6% Peak RSS | 90/90 cargo tests + 16-flag SHA-256 |
| jqlang/jq #3768 | Open (CI green) | 535.6 -> 4.3 ms (124.36x) | -0.8% RSS | 9/9 test suites + ASan/LSan/UBSan |
Equally telling are the patches that failed the gate or review. On brave/adblock-rust (already tuned to 3.05 IPC), oa rejected two real regressions and two noise edits, keeping only a modest 1.05x win (142.7 to 136.1 ms, p = 0.0002). On Delgan/loguru #1557, splitting the branch into per-commit benchmarks let four safe allocation cuts merge while dropping a fifth commit that replaced _protected_lock() with a custom context manager vulnerable to CPython asynchronous KeyboardInterrupt lock leaks. On nodeca/js-yaml, oa blocked a "plausible" edit that measured p = 0.0404 against the 0.01 alpha gate.
4. What broke when I tested oa cold on unfamiliar repos
Before releasing v0.10.0, I ran install.sh inside an empty HOME directory against unfamiliar repositories (pallets/itsdangerous, hukkin/tomli, dtolnay/semver, BurntSushi/same-file, and antirez/sds) and logged every failure. That audit surfaced real defects that unit tests had missed:
oa benchgenondtolnay/semverrancargo bench --bench parsethree times from cold cache (97.24 seconds) and flagged Criterion's timing stdout asnon-deterministic. Pre-building with--no-run, passing fast Criterion bounds, and switching benchmark runners to--oracle "cargo test"cut coldbenchgenfrom 97.24s to 10.10s.- On
pallets/itsdangerous(which usespytestrather thanunittest),oa benchgenhad silently fallen back to hashingdir(module)in a loop whenpytestwas missing. Inv0.10.0, that fallback is gone: if neitherpytest(in.venvorPATH) norunittestis runnable,oafails cleanly and asks for a real workload. - Inline
oa profile "python3 -c '...'"had skipped the Python profiler and fallen back to Linuxperfshowingld-linux-x86-64.so.2. A quote-aware shell tokenizer now routes-cinvocations straight intocProfile + tracemalloc. - Standard C Makefile repos like
antirez/sdsnow detect C language files and built test binaries (./sds-test) automatically, and every leaked RustOption::Some(...)formatting bug in stdout is fixed.
5. Reproduce any comparison in one command
You do not need an agent or a session to use oa. Comparing your working tree against main (or ranking 3+ commits with --git-refs, gating declarative oa.toml budgets with oa suite and OA005, diffing hotspots with oa profile-diff, and isolating regressions with statistical oa bisect) with balanced interleaved pairs, stdout SHA-256 verification, live peak-heap diffing, and Markdown/HTML/CSV/SARIF 2.1.0 output is one command (verified by a 13.3k-case fuzz suite, with full reference pages via oa man):
# Install via Homebrew or the release script:
brew install alexander-src/tap/oa
# or: curl -fsSL https://raw.githubusercontent.com/alexander-src/oa/main/install.sh | sh
oa demo
oa compare --git main \
--build "cargo build --release" \
--bench "./target/release/my_cli input.dat" \
--pairs 15 --check-output --heap-track --markdown
Limitations
oacannot invent algorithmic breakthroughs on already-tuned SIMD or assembly kernels; when hardware is saturated, expect small gains or noise rejections.- Equivalence checking is only as strong as your unit tests, output hashes, or
--oraclescript. Uncovered edge cases still need human code review, as thelogurulock hazard andAnyPS5ModRM boundary check showed. --heap-trackis Linux-only, needsccandaddr2line, and intercepts dynamically linked libc allocators rather than static musl builds or custom embedded allocators.- Sub-15 ms commands are dominated by process-spawn jitter; wrap short functions in an inner loop of at least 30 to 50 ms before timing.