# Runtime evidence and perf: evidence, perf measure, perf check

Flags for ingesting profiles, benchmark results and test timings with repowise evidence, and for comparing a change's wall time or allocations with repowise perf.

Runtime evidence is what a real run of your code showed: a CPU profile,
benchmark results, test timings or query counts. Repowise never runs your
code to get it. `repowise evidence setup` prints the commands, you or your
agent run them, and `repowise evidence add` joins what they wrote to the
indexed functions. How the evidence feeds the performance queue is on
[Runtime evidence](https://docs.repowise.dev/intelligence/runtime-evidence).

## `repowise evidence setup`

Print how to collect evidence here. Runs nothing and writes nothing without
`--write`, and works before the repository is indexed.

```bash
repowise evidence setup [OPTIONS]
```

| Name | Type | Default | Description |
|---|---|---|---|
| `--write` | boolean | `false` | Also write one CI file that runs each unit's tests with coverage and JUnit XML and keeps both as artifacts. Never overwrites a file that differs. |
| `--ci` | string |  | The CI file --write writes: github (.github/workflows/repowise-evidence.yml) or gitlab (.gitlab/repowise-evidence.yml). Default: gitlab when the repo has a .gitlab-ci.yml and no GitHub workflows, else github. |
| `--path` | string |  | Repo path (defaults to cwd / workspace primary). |
| `--format` | string | `table` | table or json. |

```bash
repowise evidence setup                      # what to run here, per build unit
repowise evidence setup --format json        # the same, for an agent
repowise evidence setup --write              # plus a CI file that collects coverage and test results
repowise evidence setup --write --ci gitlab
```

For each build unit it prints the stack, the test command with coverage (run
from the unit's root), what it needs first, where the report lands and the
exact ingest line (run from the repository root). Then the unit's runtime
targets, entry points first, then benchmarks, then the unit-test suite, each
with a profiler command that needs no elevated privileges (pyinstrument in
process, `go test -cpuprofile`, `node --cpu-prof`) and the exact
`evidence add` line with `--workload`, `--class` and `--commit` filled in.

`next_call` lists the steps to run next, each with its directory: a coverage
report already on disk first, then the best profile, then the tests with
coverage. What is already stored for the checkout's commit is not asked for
again, so an agent that reruns `setup` after each step stops when
`next_call` is `null`. An entry point's command ends in `<args>`: replace it
with the arguments of a representative run. A target that cannot be profiled
as is says why in `reason`.

`repowise coverage setup` prints the coverage part alone.

## `repowise evidence add`

Ingest the evidence of one run of a named workload.

```bash
repowise evidence add FILE... --workload NAME --class CLASS [OPTIONS]
```

| Name | Type | Default | Description |
|---|---|---|---|
| `FILE` | string |  | Required. A repowise-evidence-v1 document, or a profile, benchmark results or test report as the tool wrote it, detected from its content. Several profiles of one run (a parent and its workers, or a directory with one file per process) merge into one run. |
| `--workload` | string |  | Required. What ran, e.g. 'cli build'. |
| `--class` | string |  | Required. unit_test, benchmark, entry_point or load_test. production is refused: only a verified source sets it. |
| `--commit` | string |  | The full SHA measured. Defaults to the file's commit_sha; required for a converted file. |
| `--checkout-root` | string |  | The checkout the run used. Defaults to the repository root. |
| `--dirty / --clean` | boolean |  | Whether the tree had uncommitted changes when measured. Defaults to the file's dirty field, else the working tree when the commit is HEAD. |
| `--environment` | string |  | Where it ran, one word. Defaults to the OS and whether this is a CI job: linux-ci, macos-local, windows-local. |
| `--input-format` | string |  | Read FILE as repowise-evidence-v1, speedscope, pprof, cpuprofile, pytest-benchmark, go-bench, hyperfine, junit, collapsed, or a format a plugin registers. |
| `--value-kind` | string |  | cpu or wall: the clock a converted profile sampled. Needed when the profiler's output does not say (collapsed stacks, py-spy). |
| `--allow-partial` | boolean | `false` | Store the run even when more than half its entries did not map (flagged partial). |
| `--path` | string |  | Repo path (defaults to cwd / workspace primary). |
| `--format` | string | `table` | table or json. |

```bash
repowise evidence add prof.speedscope.json --workload "cli build" --class entry_point --commit $(git rev-parse HEAD)
repowise evidence add stacks.txt --workload "api" --class load_test --value-kind cpu --commit $(git rev-parse HEAD)
repowise evidence add cpu.pprof --workload "go test ./pkg" --class unit_test --commit $(git rev-parse HEAD)
repowise evidence add bench.json --workload "pytest bench" --class benchmark --commit $(git rev-parse HEAD)
repowise evidence add target/surefire-reports --workload "maven tests" --class unit_test --commit $(git rev-parse HEAD)
```

| Kind | Formats read |
|---|---|
| Profile | Speedscope JSON (pyinstrument, py-spy), Go pprof, V8 `.cpuprofile`, collapsed stacks |
| Benchmark | pytest-benchmark JSON, `go test -bench -benchmem` output, hyperfine JSON |
| Test timings | JUnit XML (pytest, Maven Surefire, Gradle, Vitest, Jest with `jest-junit`) |
| Query counts | A `repowise-evidence-v1` document |

- Entries join to indexed functions by exact path and qualified name. A
  profile's frames join only under the checkout root, each checked by a
  second signal. Anything else is skipped and counted by reason, never
  guessed.
- Functions are ranked by self time, waits excluded. The output says where
  the run's time went: the repository's own code, waits (event-loop selects,
  joins, sleeps) and libraries.
- A single profile that spent time starting or waiting on other processes is
  stored with a warning that their work is not in it.
- Evidence from a commit other than HEAD keeps its commit and reads as stale.
  It is never re-mapped.
- Caps come from the [`evidence:` block](https://docs.repowise.dev/installation/configuration#the-evidence-block).
- Exits `1` when nothing was stored, `2` on a usage error. With
  `--format json`, stdout is one document either way.

## `repowise evidence status`

The newest run of each workload: its freshness against the indexed commit
(`current`, `stale` or `unknown`), where its time went and what was skipped.
`--path`, `--format table|json`.

`repowise status`, `repowise health` and `repowise doctor` show the same
freshness for coverage and runtime evidence, and `doctor` names the setup
command when nothing is stored.

## `repowise evidence tests`

The slowest tests, and the test time spent on each production file, from
the JUnit XML timings `evidence add` stored.

```bash
repowise evidence tests
repowise evidence tests --limit 50 --format json
```

| Name | Type | Default | Description |
|---|---|---|---|
| `--limit` | number | `20` | Tests and files to list, 1 to 500. |
| `--path` | string |  | Repo path (defaults to cwd / workspace primary). |
| `--format` | string | `table` | table or json. |

Test time per file is `measured` from the per-test coverage map when one is
stored, else `inferred` from each function's nearest tests on the call
graph. Timing orders the list; it never gates.

---

## `repowise perf measure`

Time a command and say whether a change made it faster or slower. Runs only
the command typed after `--`.

```bash
repowise perf measure --save base -- pytest tests/test_parse.py      # before the edit
repowise perf measure --against base -- pytest tests/test_parse.py   # after it
repowise perf measure --base-dir ../main-checkout -- python -m mypkg build
```

| Name | Type | Default | Description |
|---|---|---|---|
| `--save NAME` | string |  | Run the command and store the runs as baseline NAME in .repowise/perf/NAME.json, with the commit and whether the tree was dirty. |
| `--against NAME` | string |  | Run the same command again and compare with baseline NAME. |
| `--base-dir DIR` | string |  | Alternate runs between DIR (base) and the current directory (head) in one session, which cancels most machine drift. |
| `--runs` | number | `10` | Timed runs per side, at least 4. |
| `--warmup` | number | `1` | Untimed runs per side before the timed ones. |
| `--timeout` | number | `600` | Seconds one run may take before its process tree is killed. |
| `--format` | string | `table` | table or json. |

Pass exactly one of `--save`, `--against` and `--base-dir`. The verdict
compares median wall times, head over base, with a 95% bootstrap interval.
It says `improved` or `regressed` only when the whole interval clears 1 by
more than the threshold: twice the base-versus-base noise, with a floor of
5% before and after and 2% interleaved. Anything else is `inconclusive`.

Every run must exit 0: a failing run is an error, never a timing. Exits `0`
when saved, improved or inconclusive, `1` when regressed, `2` when nothing
could be measured. Wall time is noisy, so the exit code is for an agent to
branch on; do not gate CI on it.

## `repowise perf check`

Gate a change on exact runtime evidence: allocations per operation, from
`go test -bench -benchmem` results ingested with `evidence add`. Runs
nothing, and timing never gates.

```bash
repowise perf check --save base
# edit the code, run the benchmarks again, repowise evidence add the results
repowise perf check --against base
```

| Name | Type | Default | Description |
|---|---|---|---|
| `--save NAME` | string |  | Store the newest benchmark run of each workload as exact baseline NAME in .repowise/perf/exact/NAME.json, beside any perf measure baseline of the same name. |
| `--against NAME` | string |  | Compare the newest benchmark runs with baseline NAME. |
| `--workload` | string |  | Only this workload's runs. With --save, the baseline's other workloads are kept. |
| `--path` | string |  | Repo path (defaults to cwd / workspace primary). |
| `--format` | string | `table` | table or json. |

A benchmark regresses when it allocates more per operation than in the
baseline. A workload whose newest run is still the saved one was not run
again and is not judged (`not_rerun`); a benchmark the run now lacks is
listed under `dropped`. Exits `0` when saved or when no exact value grew,
`1` when a benchmark allocates more per operation, `2` when nothing could be
judged or on a usage error.
