# Pipeline Performance

The pipeline logs per-event metrics to `~/.claude/logs/multi-agent/metrics.jsonl`
as it runs. The `aggregate-metrics.mjs` script turns that file into a summary
you can read, embed in a report, or feed into a dashboard.

## Quick Start

```bash
# Plain-text summary (the default - fits in a terminal)
node pipeline/scripts/aggregate-metrics.mjs

# Markdown table (for PR descriptions, wikis, dashboards)
node pipeline/scripts/aggregate-metrics.mjs --markdown

# JSON (for machine consumption - Phase 5 report uses this)
node pipeline/scripts/aggregate-metrics.mjs --json

# Filtered - only recent runs
node pipeline/scripts/aggregate-metrics.mjs --since=2026-04-01

# Per-task or per-phase drill-down
node pipeline/scripts/aggregate-metrics.mjs --task-id=PROJ-123
node pipeline/scripts/aggregate-metrics.mjs --phase=4
```

## What Gets Measured

Each event in `metrics.jsonl` is a single-line JSON object written by
`log-metric.sh`. Events cover:

- **Review cycles** - how many iterations of Phase 3 → Phase 2 → Phase 3 a task
  needed before triage approved.
- **Triage classification** - accepted / deferred / rejected counts per review.
- **Triage edge cases** - over-rejection guard trips, contradictions, invalid-
  JSON retries, timeouts.
- **Phase 2 retries** - build / test / lint retry distribution per task.
- **Cost per model** - calls, duration, tokens in/out, broken down by model
  (Fable, Opus, Sonnet, GPT-5.4).
- **Language preference** - distribution of EN vs TR prompts.

## Typical Output (Markdown)

```markdown
# Pipeline Metrics Summary

_Source: ~/.claude/logs/multi-agent/metrics.jsonl · Events: 421 (0 parse errors) · Unique tasks: 34_

## Reviews

| Metric                | Value |
| --------------------- | ----- |
| Completed             | 28    |
| Cycles per task (avg) | 1.3   |
| Cycles per task (p95) | 2     |

## Triage Classification

| Category     | Count | Rate |
| ------------ | ----- | ---- |
| Raw findings | 167   | -    |
| Accepted     | 41    | 0.25 |
| Deferred     | 19    | -    |
| Rejected     | 107   | 0.64 |

## Cost Per Model

| Model               | Calls | Duration (ms) | Tokens In | Tokens Out |
| ------------------- | ----- | ------------- | --------- | ---------- |
| `claude-fable-5-2`  | 124   | 612430        | 380221    | 92114      |
| `claude-sonnet-5-5` | 89    | 412318        | 201445    | 58903      |
| `gpt-5.4`           | 88    | 398214        | 194302    | 55128      |
```

## Interpreting Review-Cycle Metrics

- **`cycles per task avg` > 2.0** - triage is rejecting too many real findings
  or Phase 3 isn't converging. Inspect the edge-cases table.
- **Most-common edge case = `over_rejection_guard_tripped`** - the triage
  prompt lost scope context. The guard is in `validate-triage.mjs`; the triage
  step it checks is in `phase-3-review.md`.
- **`p95` much higher than `avg`** - a few tasks are looping 3+ times. Usually
  means one of: bad acceptance criteria in Phase 1, tests that flake, or
  environment-dependent build failures.

## Phase-by-Phase Token Budget

Phase docs have hard-capped token limits in
`pipeline/schemas/token-budget.json`. `smoke-token-budget.sh` enforces them.
One `max_tokens` ceiling per phase doc, one row per phase of the six-phase
contract; the file is the number, so read it there rather than from a copy:

```bash
jq '.phases' pipeline/schemas/token-budget.json
```

The warn line is not stored: the smoke computes it per phase from that phase
doc's own history of per-commit deltas.

Lazy loading keeps these off the model's context until each phase actually
runs, so the phase docs are never loaded all at once.

## Embedding Metrics in Phase 5 Reports

Phase 5 automatically calls `aggregate-metrics.mjs --json` with the current
task id and embeds the last 30 days' summary into the report body. See the
template in `phase-5-report.md`.

## Disabling Metrics

Remove or rename `~/.claude/logs/multi-agent/metrics.jsonl`. The pipeline
will not error - `log-metric.sh` fails silently if the file isn't writable.
This file is never sent off-device. Operational reporting is a separate, coarser record described in `pipeline/multi-agent-refs/features/usage-reporting.md`.
