# Proactive agent hooks

Proactive agent hooks push graph, git, health and decision context into Claude Code and Codex the moment the agent needs it. No LLM, no network.

The [MCP tools](https://docs.repowise.dev/mcp/overview) wait to be called. Hooks don't. They fire
from editor lifecycle and tool-use events and push context to your agent with
zero effort on its part, and keep your index fresh on every commit.

> **Note**
>
> This page is about the proactive context hooks below (SessionStart,
> PostToolUse, the command-rewrite hook). For the git post-commit hook that
> auto-syncs your index on every commit, see [`repowise hook`](https://docs.repowise.dev/cli/hook).
> `repowise init` installs that one for you by default; `--no-hook` skips it.

Every agent hook shares the same guarantees: **no LLM calls, no network**, only
local SQLite (`wiki.db`) and `git` reads. They are import-isolated (cold start
under ~500ms) and any failure exits `0` silently, so a broken environment never
crashes or blocks the agent.

## The hooks at a glance

| Hook | Family | Installed by | Fires on | What it does |
|------|--------|--------------|----------|--------------|
| Post-commit auto-sync | git | `repowise init` by default (`--no-hook` skips it), or `repowise hook install` | every `git commit` | Runs `repowise update` in the background |
| Pre-commit security check | git | `repowise hook install --security` (opt-in) | every `git commit` | Runs `repowise security check --staged` and blocks a commit that adds a high-severity finding |
| SessionStart context | Claude Code | `repowise init` | `startup` / `resume` / `clear` | Only what is new: an index behind `HEAD` or catching up, plus the decisions relevant to this session when `decisions.session_start` is on |
| PostToolUse enrichment | Claude Code | `repowise init` | `Grep` / `Glob` / `Read` / `Edit` / `Write` / repowise MCP calls | Graph context on searches, read-intelligence notices, edit-time "governed by" and bug-history notices |
| Wrong-path rescue | Claude Code | `repowise init` | a path tool call that failed | Names the file when exactly one indexed file carries that basename; silent otherwise |
| Decision capture prompt | Claude Code, Codex | `repowise init` and the Claude Code plugin; `repowise decision config capture-prompt --off` silences it per repo | a `git commit` whose message states a choice | Once a session, asks the agent to record the decision with `repowise decision add --agent`; it comes into force when the commit lands |
| Decision upkeep prompt | Claude Code, Codex | `decisions.upkeep_prompt: true` (opt-in, `repowise decision config upkeep-prompt --on`) | a `git commit` that changes governed files | Names up to three decisions the commit's files are under, with one command each for still holds, replaced and knowingly broken |
| Coverage re-ingest | Claude Code | `hooks.coverage_reingest: true` (opt-in) | `Bash` / `PowerShell` | Re-ingests a fresh full-suite coverage report in the background |
| Health gate | Claude Code | `repowise hook health-gate install` (opt-in) | `git commit` / `git push` | Runs `repowise health check` on what the command records and blocks it when the gate fails |
| Edit capture | Claude Code, Codex (`PostToolUse`), Cursor (`afterFileEdit`) | `attribution.capture: local` or `notes` in both repo and user config (opt-in) | file edits | Records line hashes of what the agent wrote, for [AI attribution](https://docs.repowise.dev/intelligence/ai-attribution) |
| Command-rewrite (distill) | Claude Code, Codex, Cursor | `repowise init` by default (`--no-distill-hook` skips it), the Claude Code plugin, or `repowise hook rewrite install` | shell commands (Cursor: `preToolUse` on `Shell`) | Rewrites noisy commands to `repowise distill <cmd>` and runs them automatically, no approval prompt by default |
| Codex context + staleness | Codex | `repowise init --codex` | lifecycle / edit / shell | Reminds Codex to use the MCP tools and flags stale context after edits |

The git hook is documented under the [`hook` CLI command](https://docs.repowise.dev/cli/hook); the
rest are covered below.

## SessionStart: only what is new

The block is paid for the whole session, so this hook says only what is true
and not already in context, and stays silent otherwise. A current index says
nothing: the generated `CLAUDE.md` already carries the indexed commit.

- **No generated `CLAUDE.md` block** (a plugin-only install) → the one line
  telling the agent to call a Repowise tool first, the same line the block
  leads with.
- **Update running** → a "catching up" notice, never a stale scare.
- **Index behind** → indexed vs `HEAD` with a changed-file count, and the
  target-scoped trust rule (a stale warning fires only when a file a response
  actually served has changed).

When the rewrite hook is installed but `repowise` is not on `PATH`, the block
adds one sentence: `Command distilling is off: repowise is not on PATH.`

With `decisions.session_start: true` (off by default,
`repowise decision config session-start --on`) it also carries the decisions
most relevant to this session. repowise scores the decisions in force against
the session's likely working set (dirty and staged files, files changed on the
branch vs `main`, the previous session's edited files, and branch-name tokens)
expanded one hop through import edges and co-change partners. The top few land
under a hard 400-token cap. Relevance or silence: if nothing clears the floor,
nothing is injected. Off, a session pays nothing for decisions until it edits
code one governs.

## PostToolUse: enrichment on every tool call

One hook covers several jobs, matched on the agent's `Grep` / `Glob` / `Read` /
`Edit` / `Write` and repowise MCP calls:

**Grep/Glob enrichment.** When the agent runs a broad or zero-result search,
repowise appends focused context pulled straight from `wiki.db`: the symbols
defined in each related file, what imports it, what it depends on, and its git
signals (hotspot, bus factor, owner). An agent that greps for `PageGenerator`
immediately knows what depends on it and that it's a hotspot, without a separate
MCP call:

```text
[repowise] 2 related file(s) found:

  packages/core/.../page_generator.py
    Symbols: function:_now_iso, class:PageGenerator, method:__init__
    Imported by: init_cmd.py, update_cmd.py, generation/__init__.py
    Depends on: context_assembler.py, base.py, models.py
    Git: HOTSPOT, bus-factor=1
```

**Read-intelligence.** On a read of an indexed file, repowise can nudge the agent
toward the cheaper `get_context(..., include=["skeleton"])` for structure-level
questions, and emit a per-file stale-read notice when the file changed after
indexing.

**Edit-time "governed by" decisions.** When the agent edits a file governed by
[decisions in force](https://docs.repowise.dev/intelligence/decisions), it gets up to three of them,
one line each with the rationale, once per session per decision and at most six
per session, so a decision reaches the agent right when it's about to honor or
violate it. A decision governs the files its scope names and every file under a
directory its scope names, including a new one. Background records mined from
history are never injected.

**Commit-time upkeep.** With `decisions.upkeep_prompt: true` (off by default),
a commit that changes governed files gets one line naming up to three of their
decisions, each once per session, and the one command for each outcome: still
holds (`decision reaffirm`), replaced, or knowingly broken
(`decision deprecate`). `repowise hook stats` shows whether agents act on it.

**The decisions mode.** Every decision line follows `decisions.mode`:
`read_only` keeps the edit-time notice and the upkeep line, and drops the
capture prompt; `off` silences them all, including session start.

**Stale-wiki check after history moves.** After a `git commit`, `pull`,
`merge`, `rebase` or `cherry-pick` in the shell, the hook compares `HEAD` with
the last indexed commit and flags that indexed context may be stale. The shell
entry tests the payload for those commands first, so any other shell call
costs only the shell start.

**Inside a git worktree.** A linked worktree with no `.repowise/` of its own
reads its main checkout's index, read-only, for every hook.
`REPOWISE_WORKTREE_FALLBACK=0` turns that off.

**Bug-history notice.** When the agent is about to edit a file with a recent run
of [bug fixes](https://docs.repowise.dev/intelligence/bug-history) (at least 3 counted fixes, the
last within 180 days), it gets one line naming the count, the age of the last
fix and the function that absorbed most of them, once per file per session.

## Edit capture for AI attribution, opt-in

With `attribution.capture` set to `local` or `notes` in both the repository
and the user config, the agent's edit hook records the lines it wrote as
content hashes in `.repowise/attribution/`, with the path, an opaque session
id, the tool and the model. It never records prompts, code or identities.
`repowise update` binds those hashes to commits by content. `init` and
`--yes` never turn capture on, and `DO_NOT_TRACK=1` keeps it off. See
[AI attribution](https://docs.repowise.dev/intelligence/ai-attribution).

## Command-rewrite (distill)

The rewrite hook intercepts noisy shell commands and rewrites them to
[`repowise distill <cmd>`](https://docs.repowise.dev/cli/distill), which compresses the output
errors-first before the agent reads it, exit code preserved and every omission
reversible.

`repowise init` installs it by default, together with the read-side surfaces
(`hooks.read_skeleton`, `hooks.search_digest`, `hooks.read_reread`): one
consent covers all four. Interactive runs ask (default yes); `--yes` and runs
without a terminal install it and print how to turn it off. A repo that opted
out stays off and is not asked again, and `--no-editor-setup` installs
nothing. When the enabled Claude Code plugin carries the rewrite hook, `init`
leaves the `settings.json` copy out, so each shell call is rewritten once.

Every hook command is pinned to the install that wrote it, with a `PATH`
lookup as the fallback, so a virtual environment that is not on `PATH` still
runs its hooks. When `repowise` itself is not on `PATH`, the rewrite hook
passes commands through unchanged rather than rewriting them.
[`repowise doctor`](https://docs.repowise.dev/cli/other#repowise-doctor) reports a hook that
cannot find its entry point and hooks that are installed but not firing.

> **Warning**
>
> **The default permission is `allow`, not `ask`.** Once you install the hook,
> a recognized command (test/lint/build/git/search/listing/log) is rewritten
> and run automatically, with no approval prompt, for the main agent and every
> subagent. This is deliberate: the hook only ever rewrites a closed set of
> recognized command families, with no redirects, compound commands,
> substitution, or interactive commands, and at most a single trailing
> `head`/`tail`/`grep`/`rg` filter, so the rewritten command is always
> `repowise distill <one recognized command>`, never something else smuggled
> through. If you want to review every rewrite before it runs, set
> `permission: ask` in `.repowise/config.yaml`; setting a family to `off`
> exempts it from rewriting entirely.

The hook never touches pipes it doesn't recognize, compound commands, or watch
modes. Install and manage it via [`repowise hook rewrite`](https://docs.repowise.dev/cli/hook).

## Codex hooks

`repowise init --codex` writes project-local Codex hooks (it does not touch
your global `~/.codex/config.toml`):

- **SessionStart** reminds Codex to use the repowise MCP tools it is listed,
  and carries the same relevance-ranked decisions Claude Code receives when
  `decisions.session_start` is on.
- **PostToolUse**, on the shell tool and on `apply_patch` / `Edit` / `Write`,
  compares `HEAD` against the last indexed commit after a successful `git
  commit`, `merge`, `rebase`, `cherry-pick` or `pull` and flags that indexed
  context may be stale.

Nothing is registered on `UserPromptSubmit`. Firing on every prompt only
repeats the MCP-usage note SessionStart already delivered, at the cost of a
hook run per turn. `repowise agents refresh` rewrites this repo's `.codex/hooks.json` and
clears any repowise entry on that event. A hook *you* wrote there is left
alone.

Codex names its shell tool `shell_command` on current releases and `Bash` on
older ones, so the matcher covers both. The Claude Code PostToolUse hook
deliberately does not watch shell commands: it has `Read` / `Grep` / `Glob`
and a SessionStart freshness line, so the shell adds cost without adding
reach. Codex has neither. See
[Connecting Codex](https://docs.repowise.dev/getting-started/quickstart) for setup.

## Learns from how you actually use it

The hooks aren't just delivery; they close a loop. Every decision line a hook
delivers is recorded locally in `.repowise/sessions/sessions.db`, and
`repowise decision list` and `show` report how often each decision reached an
agent and when last. With `hooks.measure` on, `update` reads the session
transcripts locally to settle whether the agent acted on each firing, for
`repowise hook stats`.

That feedback is one half of a flywheel that makes repowise adapt to your repo,
all local and deterministic:

- **[Decisions your agents record](https://docs.repowise.dev/intelligence/decisions)**: a choice an
  agent makes comes into force when its commit lands, and is delivered back at
  edit time.
- **[Docs that follow your questions](https://docs.repowise.dev/intelligence/auto-wiki)**: the wiki
  generation budget tilts toward the modules you and your agent ask about most,
  so depth lands where you actually work.

> **Note**
>
> Hooks and [MCP tools](https://docs.repowise.dev/mcp/overview) are complementary. Hooks are passive,
> automatic, and free: they fire whether or not the agent is thinking about
> graph context. MCP tools are active and richer, for when the agent needs full
> documentation, a risk assessment, or dependency tracing.
