# Distill: compact command output

Run noisy commands through repowise distill to get errors-first, fully reversible output, saving 61 to 89% of tokens in measured runs on tests and git log.

Most of what an AI coding agent reads from a shell command is noise:
300 lines of passing tests around 4 failures, full commit bodies for
"what changed recently". **Distill** compresses that output before the
agent reads it: errors-first, structure preserved, and every omission
reversible. It's a capability built *on* the intelligence layers (symbol
bounds, graph centrality, hotspots decide what to keep), not a layer of
its own.

**Guarantees**, enforced by the engine:

- Every error/failure-classified line in the raw output survives.
- Raw output is stored *before* any marker renders, so `repowise expand`
  always round-trips it.
- Any filter problem falls back to the raw output, unchanged.
- Output is only distilled when it actually gets smaller (net-positive
  guard); small outputs pass through untouched.
- Exit codes are preserved, so it's a drop-in wrapper.

Measured on a public OSS repo (paired runs, one per command):

| Command | Raw tokens | Distilled tokens | Saved |
|---|---|---|---|
| `pytest -q` (11 failures) | 3,374 | 1,317 | **61%**, all 11 failure lines preserved |
| `git log -50` | 3,064 | 331 | **89%** |

## `repowise distill <command>`

Run a command and print the compact rendering. Ten content filters
cover test runs (pytest, jest, vitest, cargo, go), builds (npm, tsc,
cargo, go), linters (eslint/biome, ruff/flake8/mypy, clippy,
golangci-lint), `git status`, `git log`, `git diff`, grep/rg floods, file
listings, file reads, and generic logs.

```bash
repowise distill pytest -x
repowise distill git status
repowise distill npm run lint
```

The lint filter keeps every error verbatim and groups warnings by rule
id with counts and `file:line` anchors, the shape that compresses best
on the output agents run most.

Dropped content is stored in `.repowise/omissions/` and referenced by an
inline marker:

```text
[repowise#a1b2c3d4e5f6: 230 lines omitted (~6.1k tokens); restore: repowise expand a1b2c3d4e5f6]
```

## `repowise expand REF`

Restore the original output behind a marker. Accepts a bare 12-hex ref
or a pasted whole marker.

```bash
repowise expand a1b2c3d4e5f6
repowise expand a1b2c3d4e5f6 -q "FAILED"     # only the matching lines
```

| Name | Type | Default | Description |
|---|---|---|---|
| `--query / -q` | string |  | Return only the lines matching this regex (or substring). |

The omission store is durable across sessions (an agent resuming work
tomorrow can still expand yesterday's markers), pruned by TTL + size cap
(7 days / 50 MB by default, configurable).

## The command-rewrite hook (Claude Code + Codex)

On by default after `repowise init` (`--no-distill-hook` skips it). A
PreToolUse hook rewrites noisy agent commands (`pytest -x` becomes
`repowise distill pytest -x`) before they run. When `repowise`
is not on `PATH` in the hook's environment, the command runs unchanged.

```bash
repowise hook rewrite install      # after an init that skipped it
repowise hook rewrite status
repowise hook rewrite uninstall    # removes only the repowise entries
```

> **Warning**
>
> **The default posture is `allow`, not `ask`.** A rewrite auto-runs: the agent
> never sees a prompt, and the command that actually executes is
> `repowise distill <original command>`, not the one the agent typed. This is
> deliberate (a rewrite is always a bailout-filtered wrap around the same
> command, so auto-allow is considered safe), but it's surprising the first
> time you notice `distill` in a shell history you didn't write. Set
> `permission: ask` under `distill.commands` in `.repowise/config.yaml` to
> require approval before each rewrite instead.

The hook covers both Claude Code shell tools (Bash and, on Windows,
PowerShell) and never rewrites pipes, redirections, compound commands,
watch modes, PowerShell-native constructs (`Verb-Noun` cmdlets, `&`
invocations, aliases like `ls` that don't mean their unix namesakes), or
anything in a repo that hasn't opted into repowise. Declining the `init`
prompt gates the repo off in config, so a hook installed globally from
another repo stays inert.

`repowise init` also adds an "Output Distillation" section to the
managed `CLAUDE.md`, so any agent that runs shell commands prefers
`repowise distill <cmd>` voluntarily, hook or no hook.

> **Warning**
>
> **The allowlist trap.** A rewrite changes the command string, so a
> Claude Code permission rule you already had, say `Bash(git diff:*)`,
> does not match the rewritten `repowise distill git diff ...`, and the
> prompt comes back for commands you'd already allowed. `repowise hook
> rewrite install` offers to seed `Bash(repowise distill:*)` /
> `PowerShell(repowise distill:*)` allow rules (or pass `--allow-rule` /
> `--no-allow-rule` non-interactively). The rule only stops
> *double*-asking. `repowise distill` runs the wrapped command unchanged
> and never widens what it can do.

### Codex

When `~/.codex` exists, `install` also covers the Codex CLI, with two
caveats its hook protocol imposes:

- Codex applies a PreToolUse command rewrite only from **version
  0.137**; on older builds the hook entry is skipped (a rewrite response
  would error on every shell call). `repowise hook rewrite status`
  reports what your build can do.
- Codex has **no ask-with-mutation**: a rewrite can only be
  auto-allowed, never shown for approval. So under Codex, rewrites fire
  **only for command families you set to `permission: allow`**; `ask`
  families always pass through unchanged.

The hook entry lands in `~/.codex/hooks.json` (run `/hooks` inside Codex
to review and trust it). Independently of any hook, `install` maintains
a marker-managed "Output Distillation" section in the repo's `AGENTS.md`
that works on every Codex version; `uninstall` removes it and restores
your file byte-for-byte.

## `repowise saved [PATH]`

Report the input tokens agents did not have to read, and what they were
worth. Covers the distill path, the replacement hooks and MCP calls.

```bash
repowise saved                  # per-operation rollup + totals + est. dollars
repowise saved --by surface     # distill vs hooks vs MCP
repowise saved --by day
repowise saved --since 2026-06-01
repowise saved --missed         # savings raw commands left on the table
```

| Name | Type | Default | Description |
|---|---|---|---|
| `--by` | string | `operation` | Grouping: operation, surface, agent, model, or day. |
| `--since` | string |  | Only count savings since this ISO date. |
| `--model` | string |  | Pricing model for the --missed estimates only. Recorded savings are priced at the rate captured on each event. Default: the model of this repo's most recent agent session, else claude-sonnet-4-6. |
| `--missed` | boolean | `false` | Report savings foregone by raw (non-distilled) agent commands. |
| `--missed-days` | number | `7` | Transcript window for the missed-savings scan. |
| `--format` | string | `table` | table or json |

A directory with no `.repowise/` has no ledger, which reads as "nothing
recorded", not as zero. The figure is a floor: where the evidence is
ambiguous it claims no saving.

## `repowise savings sync [PATH]`

Record savings your agents were shown but the ledger never banked. It
reads only what was appended to agent transcripts since the last run, so
repeating it is cheap.

```bash
repowise savings sync
repowise savings sync --dry-run
```

| Name | Type | Default | Description |
|---|---|---|---|
| `--dry-run` | boolean | `false` | Report what would be recorded without writing. |
| `--budget` | number |  | Seconds of transcript reading. A non-zero deferred count means the budget ran out; run it again. |
| `--format` | string | `table` | table or json |

### Missed savings

`--missed` is the adoption feedback loop: it scans your local Claude
Code transcripts for shell commands in this repo that were **not**
routed through `repowise distill`, classifies each with the same router
the engine uses, and estimates the foregone savings using each filter's
*conservative* fixture floor; the estimate undersells on purpose. Plain
`repowise saved` appends a one-line summary when there's anything to
report.

The scan is read-only, best-effort, and entirely local: commands are
read from your own transcript directory (`~/.claude/projects/`);
nothing leaves the machine.

The dashboard mirrors all of this: the Costs page's *Cache & savings*
tab shows a Distill savings card with the same rollup plus the
missed-savings secondary stat.

> **Note**
>
> Truncated MCP content is recoverable: see
> [`get_symbol`](https://docs.repowise.dev/mcp/get-symbol) omission refs.

## `repowise corrections [PATH]`

The same transcript reader, pointed at a different waste: commands the
agent got *wrong* and then fixed. The scan finds consecutive runs of the
same base command where the first failed and a later variant succeeded,
then classifies the fumble: wrong tool (bare `python` vs the venv
interpreter), wrong path, unknown flag, missing argument. It reports
the recurring rules.

```bash
repowise corrections                # report-only (default window: 30 days)
repowise corrections --days 60
repowise corrections --write        # maintain the managed guidance block
```

| Name | Type | Default | Description |
|---|---|---|---|
| `--days` | number | `30` | Transcript window for the scan. |
| `--write` | boolean | `false` | Maintain the 'Known command corrections' managed block in CLAUDE.md/AGENTS.md (strictly opt-in). |
| `--min-count` | number | `2` | Occurrences a rule needs before --write includes it. |
| `--format` | string | `table` | table or json |

Classification is precision-first: apart from the structural wrong-tool
case, a rule only forms when the error text actually names the dropped
flag or path; a red-green dev loop re-running tests with different
selections never becomes a "correction". Wrong-path rules consult the
symbol index when available and note where the corrected target actually
lives.

`--write` maintains a short **"Known command corrections"** block
(most-frequent rules first, capped at 10 lines) between managed markers
in the repo's `CLAUDE.md` (and `AGENTS.md` when one exists), so the next
agent session is told up front. Re-running refreshes the block in place;
content outside the markers is never touched. The same privacy contract
as the missed scan applies: read-only, entirely local.

## Configuration

The `distill:` block in `.repowise/config.yaml`:

```yaml
distill:
  enabled: true                  # master switch for this repo
  commands:
    enabled: true                # the command path (CLI + hook rewrites)
    permission: allow            # ask | allow | off (rewrite-hook posture; allow is the default)
    families:                    # per-filter overrides
      test_output: allow         #   auto-allow rewrites for test runs
      git_diff: deny             #   never rewrite git diff here
    disabled_filters: []         # filters to skip entirely, e.g. [logs]
  omission_store:
    ttl_days: 7
    max_mb: 50
```

`repowise doctor` validates the block, reports the omission store's size
against its cap, and shows whether the rewrite hook is installed.

## Skeletons: the read-side counterpart

For structure-level questions about a large indexed file, ask MCP for a
**skeleton** instead of reading the whole file: every signature, the
imports, and the bodies of only the most central symbols, typically
~15% of the full file's tokens, sliced from persisted symbol bounds with
zero query-time parsing.

```python
get_context(["src/big_module.py"], include=["skeleton"])
```

After a large `Read`, the PostToolUse hook nudges the agent with the
skeleton's cost (once per file per session) and warns when a re-read
follows an edit. With `hooks.search_digest` on, a grep flood comes back as a
per-file digest ranked by graph centrality. See
[`get_context`](https://docs.repowise.dev/mcp/get-context).

## Next
- [Hooks](https://docs.repowise.dev/intelligence/hooks) for every hook that feeds the agent.
- [repowise hook](https://docs.repowise.dev/cli/hook) to install or remove the rewrite hook.
- [Connect your agent](https://docs.repowise.dev/agents) to wire the agent that runs these commands.
