The reusable artifact here was never the vendored targets — it is the discipline: prove equivalence, prove significance, or claim nothing. This packages it as a Claude Code skill usable from any repo. Two modes. `verify` works anywhere, on any two shell commands in any language: equivalence before timing -> A/A calibration -> interleaved A/B with a bootstrap CI -> a verdict decided by the CI lower bound, never the median. `attempt` drives the full in-repo loop (gates, pristine-baseline verdict, ledger row, ROI report). It drives the real bench-runner binary and refuses rather than degrade: on an unsupported host, or with no trust layer reachable, preflight stops and says so instead of falling back to a hand-rolled timing loop. A second copy of the statistics is a second thing that can lie. A refusal ends the claim, not the work. An adversarial review of the skill caught six high-severity defects, all fixed here — several of them the very errors the skill exists to prevent: - verify mode had no risk classification at all. bench-runner emits only accepted/rejected-bench; needs-human-review is minted by the in-repo verdict path, which verify does not run. A concurrency change with byte-identical output would have sailed through as "accepted". The skill now makes the agent the classifier and says so explicitly. - the accept template exposed `pin_prefix`, which is inert for compare/aa/calibrate (only `verdict` reads it) — an agent would have set it and reported pinning that never happened. Now documents wrapping both sides in taskset instead. - the equivalence gate compared stdout only, so two commands that write to stderr and a dist/ tree would "GOLDEN PASS" on two empty files. Now hashes stdout + stderr + exit code + artifact tree, and an empty diff over empty output is a failed gate, not a passed one. - nothing told the agent to build two coexisting immutable artifacts, so a `cargo run` baseline could rebuild from the patched tree mid-bench — the exact trap phase1-verdict-smoke-001 recorded. - service mode was documented as generic HTTP. It is not: the server must take <port> <doc> <iters>, print READY, and close the connection. A stock Express app hangs the harness forever. Contract now stated. - the CI refusal test could not pass: preflight also walks up from its own location, so running it in-place found this very repo. The test now copies the skill out of the tree first. CI job `skill-preflight` asserts the contract that matters — resolve when a trust layer is reachable, REFUSE when it is not, and work when consumed from another repo via PELTIER_HOME. It deliberately does not assert a statistical outcome: a shared runner is too noisy for that, which is precisely why the skill calibrates before it claims. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
14 KiB
hotpath — profile-guided optimization agent
Working name. Rename freely; nothing depends on it.
Mission
An agent that profiles real binaries and services, isolates hot paths, proposes optimizations, and accepts a change only when (a) behavioral-equivalence gates pass and (b) the benchmark improvement is statistically significant on trusted infrastructure. Output: verified performance deltas translated into ROI reports nobody can dispute.
Full engineering spec: SPEC.md. Read it before starting any phase.
Non-negotiables (re-read every session)
- No unverified performance claims. A change without passing equivalence gates AND a significant bench delta does not exist. Discard it. Log it.
- Measure before optimizing. No patch is proposed until profile data identifies the hot path. Never trust a single benchmark run.
- Cheap wins first, always. Build flags → LTO → PGO → allocator swap → then code changes. Check the ledger before re-attempting any class of change on a target.
- Equivalence gates are hard gates. Changes touching floating-point ordering, concurrency primitives, or anything UB-adjacent are NEVER auto-accepted — verdict is
needs-human-review. - Every attempt goes in the ledger, including failures and rejections. Failures are future training data.
- The agent never modifies the trust layer.
crates/,config/,corpora/, and upstream test suites are read-only to the agent. Patches may touch only allowlisted paths undertargets/<name>/. See SPEC.md §10. - Sandbox everything. Target code must never run unisolated on the host. As built: the harnessd-launched verdict pipeline execs through
scripts/no-net.sh(Linux network namespace; fails closed — exit 97 — when namespaces are unavailable, unlessHOTPATH_ALLOW_UNISOLATED=1, which runs it with network and stamps that fact into the ledger's isolation note). CI runs the bench workload underdocker run --network=none. Two honest limits: a human invokingjust verdictdirectly is not wrapped, and full-container (seccomp-restricted) isolation of the whole pipeline is still an open gap. See SPEC §10. - Statistical bar: accept only if the bootstrap 95% CI lower bound of speedup ≥ threshold in
config/accept.toml(default 2%), from interleaved A/B runs on the same pinned hardware.
Architecture map
crates/bench-runner— trust layer: CPU-pinned, interleaved A/B benchmarking with bootstrap confidence intervals and A/A self-tests (it does not containerize anything itself — isolation is the wrapper's job, non-negotiable 7)crates/diff-test— behavioral equivalence: corpus/test-suite pin checks + upstream test suite + golden I/O replay + differential fuzz (a baseline-vs-candidate gate; sanitizers run on the accept path incrates/verdict, anddiff-testreports them as skipped-with-reason)crates/ledger— append-only SQLite record of every attempt: hypothesis, patch, gate results, bench deltas, verdict, costcrates/report— ROI generator: cores saved × $/core-hr, latency percentile deltas, CIs, workload caveats printed on every numberagent/— Claude Agent SDK (Python) loop, prompts, tool definitionstargets/— vendored OSS targets for case studies (permissive licenses only)playbook/— optimization classes, ordered, with preconditions and known risksresults/— generated reports and flamegraphs.claude/skills/peltier/— the trust layer packaged as a consumable Claude Code skill:verifymode (equivalence → A/A calibration → interleaved A/B + bootstrap CI) works in any repo against any two shell commands;attemptmode drives the full in-repo loop. Drives the realbench-runnerbinary and refuses on unsupported hosts rather than falling back to a second, forkable copy of the statistics. Preflight refusal is asserted in CI (skill-preflight).
Phase 4 COMPLETE — all SPEC §5 exit criteria met. Phases 0–4 are all done. Service-mode latency benchmarking shipped: a coordinated-omission-correct, open-loop, fixed-rate load generator (crates/bench-runner/src/service.rs) measuring latency from each request's intended send time (queueing counted, not hidden), interleaved A/B, exact percentiles, bootstrap p50/p99 CIs. A real service target — targets/cjson/service.c, a minimal HTTP server wrapping the patched cJSON.c (trust-layer, outside the workspace allowlist). Service calibration PASS (0/10 A/A false-positive, 10/10 injected-5%-latency-regression detection; results/calibration/cjson-service-aa.json). The verified batch win phase3-cjson-002 measured under a 150 rps-target replay (20 interleaved rounds, 0 drops — the phase4-cjson-service ledger row records sessions=20, rate_rps=150, drop_rate=0.0; total request count is not machine-recorded): p50 latency +6.2%, 95% CI [+5.8%, +7.2%] — accepted; p99 NOT claimed (CI [0.07, 4.97], single-worker loopback tail jitter) — harness correctly rejected it. Mechanical ROI report generator (crates/report bin, just report, SPEC §9): reads a ledger row + service-latency JSON → throughput→cores→dollars (27.5 cores / $9,621-per-year CI lower bound on a 500-core fleet) and latency percentiles, every figure with CI + workload + methodology inline; flags any non-accepted row. Case study: results/phase4/. The roadmap is complete — the profile→hypothesize→patch→gated-verdict→ROI loop runs end to end across Rust and C, batch and service, with zero shipped false accepts across all phases.
Hardening pass (2026-07-13, audit/resolve-findings) — what is enforced vs. what is only mechanized. Five gates were addressed in code. Two are live on the verdict path (a, b); three are wired but currently inert, because no target has opted in (c, d, e). The distinction is stated precisely because collapsing it is exactly the overclaim this project exists to prevent.
- (a) Differential fuzz — live, and now blocks accepts.
diff-testruns the target's declared[gates].fuzzcommand and grades it strictly from aFUZZ-RESULT iters=<n> divergences=<m>line (no line = Failed, not a pass by silence); the ledger records the iteration count actually executed. It is a baseline-vs-candidate gate, so it runs only on the accept path (crates/verdict, the one flow that rebuilds a pristine baseline). The standalonejust gatesflow has no baseline, so the gate reports Skipped with that reason —just gatesis a fast equivalence check, not a fuzz run. Hard rule: a machineacceptedverdict is impossible unless the DifferentialFuzz gate actually Passed; skipped or failed caps the verdict atneeds-human-review.cjson/comrak/tokeideclare[gates].fuzz;matmuldoes not. Ledger history: fuzz ran out-of-band viascripts/diff-fuzz-*.pythrough Phase 5, so 8 of the 10 accepted rows recordfuzz_iters=0— the two exceptions,phase0-tokei-002andphase0-tokei-003, recordfuzz_iters=4332. - (b) Lexical risk classifier — live.
crates/verdict/src/risk.rsscans the patch's changed lines for concurrency /unsafe/ floating-point tokens; any signal on a would-be accept forcesneeds-human-review, and using fp-tolerance equivalence mode is itself always a signal. It is lexical and deliberately over-triggering, not semantic — it detects the presence of risk markers; it does not understand the code and cannot prove their absence.harnessdalways passes--patch-file, so every agent-proposed patch is classified. - (c) Test-suite pinning — mechanized, INERT.
diff-testverifiescorpora/<t>/TESTSUITE.sha256when present (mismatch = hard refusal, same posture as the corpus pin) and prints an explicit "suite unpinned" warning when absent. No target ships a pin today — there are zeroTESTSUITE.sha256files in the repo — so the gate constrains nothing yet. Pins must be generated per target, post-fetch, by a deliberatescripts/pin-testsuite.shrun. - (d) TSan lane — mechanized, INERT.
crates/verdictbuilds and runs a TSan lane on the accept path only when a target declares[build].tsan. No target declares it today, so no TSan coverage exists. (ASan/UBSan is live on the accept path: a flagged run — or a target with no sanitizer build at all — caps the accept atneeds-human-review.) - (e) FP-tolerance equivalence — mechanized in the pipeline, target NOT wired. The gate pipeline honors an fp-tolerance policy (
targets/<t>/equivalence.toml; requires[corpus].golden_reference), and any fp-tolerance run auto-routes toneeds-human-review. But the only FP target,matmul, has anequivalence.tomland notarget.toml— so it is not a pipeline target at all, and the kernel lane stays script-driven (scripts/kernel-lane-demo.sh).
Phase 5 COMPLETE — the two SPEC §13 research forks, both built and both routed to human review. (a) Learned class-selection policy (crates/policy, cargo run -p policy): reads the append-only ledger and ranks optimization classes by the Wilson lower bound of their shippable-win rate — a learned prior over the fixed cheapest-first ordering, advisory only (gates still decide). A "win" counts only machine-sanitizer-verified accepts; the overturned comrak-010 and tier-gated mimalloc are excluded/held. On the current 34-row ledger it recommends algorithmic first (Wilson lb 0.066). (b) Kernel lane (targets/matmul/kernel.c, crates/diff-test FP-tolerance policy + fp-compare bin, scripts/kernel-lane-demo.sh): a matmul optimization (transpose-B cache locality + eight-accumulator ILP) that reorders FP accumulation, so byte-identical golden replay is the wrong gate — 244,901/262,144 values differ. The FP-tolerance policy (abs 1e-4 + rel 1e-3) accepts it, still REJECTS a +0.5 perturbation, and the bench measures 3.23× [3.16, 3.26] with the same interleaved A/B + bootstrap CI. Ledger row phase5-matmul-opt = needs-human-review (using the tolerance tier is a §8 signal, same posture as mimalloc). Honest scope: the FP-tolerance mechanism lives in the gate pipeline, but matmul is not wired into it — it has an equivalence.toml and no target.toml, so diff-test/verdict cannot load it. The lane is demonstrated script-driven (fp-compare + bench-runner compare), and that ledger row was recorded outside the automated verdict path. Wiring matmul as a real pipeline target is open work. No GPU in this environment, so the GPU fork is the identical shape shown on CPU — only the timer and hardware change. Case study: results/phase5/; docs chapter docs/src/research-forks.md. Open follow-up: the standing accept-scoped ruling on phase0-comrak-002 (mimalloc) is resolved (results/rulings/).**
Commands
The real recipe list (justfile); keep it current:
just build / test / lint # trust-layer workspace: cargo build/test/clippy+fmt
just aa [cmd] # A/A self-test of bench-runner on a shell command
just compare <a> <b> # interleaved A/B of two shell commands, bootstrap CI
just gates <target> # corpus pin (+ suite pin if present) + upstream tests + golden replay.
# diff-fuzz SKIPS here (needs a baseline) — it runs in `just verdict`
just pin-check <target> # verify corpus MANIFEST.sha256 (verify-only, never re-pins)
just pin-corpus <target> # deliberate corpus re-pin (writes MANIFEST.sha256)
just calibrate <cmd> <out> # automated A/A + regression-injection calibration
just verdict <t> <bin> ... # gates + bench vs pristine-rebuilt baseline + ledger row
just report <run-id> # ROI report from a ledger row (CIs + methodology inline)
just agent-attempt <t> <id> # one unattended agent attempt behind the OS boundary
just isolation-check # verify the SPEC §10 OS boundary (19 checks, both modes)
just service <b> <c> <doc> # service-mode latency A/B, CO-correct, p50/p99 CIs
just service-calibrate ... # latency A/A + injected-regression calibration
just coz <target> # causal profile of a C/C++ target
cargo run -p harnessd # agent IPC daemon: seven-op JSON surface (SPEC §3.5)
cargo run -p policy # learned class-ranking prior from the ledger (advisory)
Platform note: the harness is Linux/POSIX-only at runtime — bench-runner
service mode, harnessd (Unix sockets, setsid), and every sh-based gate and
isolation script assume a Unix host. Windows compiles the workspace and runs
the portable unit tests, but cannot run the pipeline.
Definition of done — one optimization attempt
- Hypothesis logged in ledger before patching
- Patch touches only allowlisted paths under
targets/<name>/ - Upstream test suite green
- Golden replay byte-identical (or within the target's explicit FP tolerance policy)
- Differential fuzz on changed functions: no divergence (60s or 10k iterations minimum). Requires
[gates].fuzzin the target'starget.tomland the accept path (just verdict) — an accept is impossible without a Passed fuzz gate - ASan + UBSan clean (machine-enforced on accept). TSan if the patch touches anything threaded — note the lane only runs when the target declares
[build].tsan, and no target declares one today, so a threaded patch needs that opt-in added first - Interleaved A/B bench passes the CI-lower-bound threshold
- Ledger row written with verdict and full evidence
- If FP-ordering / concurrency / UB-adjacent: verdict
needs-human-review, never auto-accept
An attempt that fails any gate is a valid, complete outcome — write the ledger row and move on. Do not iterate on a rejected patch more than twice without a new hypothesis.
Conventions
- Rust workspace (edition 2021+) for all harness crates;
justfor task running - Python 3.11+ for
agent/; Claude Agent SDK — verify current package name and API at https://docs.claude.com/en/docs/claude-code/overview before scaffolding - Every number shown to a human carries its confidence interval and workload description. No naked percentages, ever.
- Commit style:
phase0:,bench:,gates:,agent:,playbook:prefixes - When in doubt between shipping a feature and hardening measurement, harden measurement. The product is trust.