Examples
pi autoresearch measure scripts
A measure script is the part you write. Everything else in the loop is generic: the extension runs whatever you give it, reads one number per run, and compares that number to the baseline.
The contract is small. Print one line per metric on stdout:
METRIC <name>=<number>
That is it. No JSON, no exit-code trickery. The direction — lower is better or higher is better — comes from init_experiment, not from the script.
1. Test runtime
#!/usr/bin/env bash
set -euo pipefail
start=$(date +%s%N)
pnpm test -- --run > /tmp/autoresearch-test.log 2>&1 || { cat /tmp/autoresearch-test.log; exit 1; }
end=$(date +%s%N)
echo "METRIC total_ms=$(( (end - start) / 1000000 ))"
Direction: lower. Do not let a failed run print a metric — a crash should mean no number, not a very fast zero.
2. Bundle size
#!/usr/bin/env bash
set -euo pipefail
pnpm build > /dev/null
bytes=$(du -sb dist | cut -f1)
echo "METRIC bundle_kb=$(( bytes / 1024 ))"
Direction: lower. This one is cheap and deterministic, which makes it a good first target while you learn how the loop behaves.
3. JavaScript parse time
#!/usr/bin/env bash
set -euo pipefail
pnpm build > /dev/null
node --input-type=module -e "import('./dist/index.js')" > /dev/null
echo "METRIC parse_ok=1"
Direction: higher. Use a binary metric when the thing you care about is “it still loads”.
4. Coverage percentage
#!/usr/bin/env bash
set -euo pipefail
pnpm test -- --coverage > /tmp/cov.log 2>&1 || true
pct=$(grep -E 'All files' /tmp/cov.log | grep -oE '[0-9]+(\.[0-9]+)?' | head -1)
echo "METRIC coverage_pct=${pct:-0}"
Direction: higher. Pair it with .auto/checks.sh so a green coverage number cannot come from deleting tests.
5. Lighthouse performance
#!/usr/bin/env bash
set -euo pipefail
npx lighthouse http://localhost:3000 --output=json --output-path=/tmp/lh.json --chrome-flags="--headless" > /dev/null
score=$(node -e "console.log(Math.round(require('/tmp/lh.json').categories.performance.score*100))")
echo "METRIC perf_score=$score"
Direction: higher. Lighthouse is the noisiest example here; expect a lot of yellow confidence scores and plan for a re-run when the gain is small.
Stop a win that breaks things
.auto/checks.sh runs after every benchmark that exited 0. If it fails, the experiment is logged as checks_failed and the change is reverted without a commit:
#!/usr/bin/env bash
set -euo pipefail
pnpm test -- --run
pnpm typecheck
Stop when you reach the goal
Set a target in init_experiment and the loop can end by itself instead of running forever:
{ "name": "test-speed", "metric": "total_ms", "unit": "ms", "direction": "lower", "target_value": 1000 }
With direction: "lower" the loop stops when the metric drops to 1000 or below. With direction: "higher" and target_value: 0.95 it stops at 0.95 or above.
Two habits that save a session
First, run the baseline three times by hand and look at the spread before you let an agent chase a 3% win. If the spread is 8%, that win is not real. Second, keep the measurement command identical for the whole session — swapping the command mid-session resets the meaning of every number in the log.
More on how the score is computed is on the pi-autoresearch home page, and the variant that isolates each session in a git worktree is covered on the harness page.