# Change Guard

`guard` answers one question after an agent edits code: **what does this change reach that is
not in the change set?** It returns untouched consumers, candidate tests, affected routes and
public symbols that no documentation file mentions — or nothing at all.

It is the in-loop companion to [impact](impact.md) and [brief](brief.md). Impact is a report you
ask for; Change Guard is a check that can run on every edit and stay silent until it has something
useful to say.

## Install

If your agent has a plugin marketplace, install Change Guard from this repository. There is no
JSON to merge and no file to copy.

**Claude Code**

~~~text
/plugin marketplace add ankurhalder/codebase-snapshot
/plugin install codebase-snapshot-change-guard@codebase-snapshot
~~~

**Codex**

~~~bash
codex plugin marketplace add ankurhalder/codebase-snapshot
codex plugin add codebase-snapshot-change-guard@codebase-snapshot
~~~

**Copilot CLI, Devin and Qoder**

~~~bash
copilot plugin install ankurhalder/codebase-snapshot:agent-plugin
devin plugins install ankurhalder/codebase-snapshot#agent-plugin
qoder plugins marketplace add ankurhalder/codebase-snapshot
qoder plugins install codebase-snapshot-change-guard
~~~

Claude Code and Qoder install the `PostToolUse` hook together with the plugin; Copilot uses
`com.github.copilot/hooks.json`; Codex and Devin install the skill, which calls the guard
when the policy says to. A host without a marketplace can copy `~/.agents/skills/change-guard`
and use the hook block below.

The hook runs a pinned CLI, so the machine needs Node.js 18+ and `npx`:

~~~bash
npx -y codebase-snapshot@0.22.2 --version
~~~

## Run it

~~~bash
codebase-snapshot guard --changed
codebase-snapshot guard --paths src/auth.ts --format json
codebase-snapshot guard --diff origin/main --format json
~~~

With no options it checks the working tree against `HEAD`. The output is small on purpose:

~~~text
Change Guard: 3 gap(s) in this change
  consumer  src/api.ts (1 hop, relative-import, via src/auth.ts)
  test      tests/auth.test.ts (test: src/auth.ts)
  docs      src/public-api.ts (newApi)
  coverage: complete · confidence: file-import
~~~

Consumer, test and route gaps name a path outside the change; a file the change already touched
is never reported again. A documentation gap names the changed public file and the exported symbols
that no Markdown/reStructuredText/AsciiDoc file mentions. Docs are checked only when the repository
has a documentation surface, and only exported symbols of at least three characters are considered,
so the signal stays small. `confidence` is `symbol-pruned` only when a diff seeded symbol-level
matches; a hook payload or `--paths` run is file-level (`file-import`).

## Hook mode

`--hook <client>` reads one JSON payload from stdin and writes the host-shaped response to stdout.
It never fails the agent's turn: a malformed payload or a missing manifest produces an empty object
and a note on stderr.

| Client | Flag | Response |
| :-- | :-- | :-- |
| Claude Code | `--hook claude` | `hookSpecificOutput.additionalContext` with `hookEventName` |
| Copilot | `--hook copilot` | top-level `additionalContext` |
| Codex | `--hook codex` | top-level `additionalContext` |
| Other | `--hook raw` | top-level `additionalContext` |

When there is nothing to report, the response is `{}`. That is the point: a guard that speaks when
there is nothing wrong is a guard users disable.

### Claude Code

The plugin install above already wires this hook. Use this block only for a manual setup.

~~~json
{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Edit|Write",
        "hooks": [
          {
            "type": "command",
            "command": "npx -y codebase-snapshot@0.22.2 guard --hook claude",
            "timeout": 10
          }
        ]
      }
    ]
  }
}
~~~

### Copilot in VS Code

~~~json
{
  "version": 1,
  "hooks": {
    "postToolUse": [
      {
        "type": "command",
        "command": "npx -y codebase-snapshot@0.22.2 guard --hook copilot",
        "timeoutSec": 10
      }
    ]
  }
}
~~~

### Codex CLI

~~~json
{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Edit|Write",
        "hooks": [
          {
            "type": "command",
            "command": "npx -y codebase-snapshot@0.22.2 guard --hook codex",
            "timeout": 10
          }
        ]
      }
    ]
  }
}
~~~

The exact file location is the host's: `.claude/settings.json`, `.github/hooks/*.json`,
`.codex/hooks.json`, or the plugin's own `hooks/hooks.json`. The command is the same.

## What it reads

The guard reuses the live index: the working tree is scanned in memory and uncommitted edits are
overlaid, so no `generate` run is required. `--no-live` requires a persisted manifest instead.

It reads the same manifest and import resolver as [impact](impact.md). It never writes into the
repository and never reads a sensitive file: `.env`, `*.pem` and the rest stay out.

## What it does not claim

- **Candidate tests, not coverage.** A test that imports a changed file is a candidate; it may not
  exercise the changed behavior.
- **Static reach, not runtime reach.** A dynamic import the parser cannot evaluate is absent rather
  than guessed; the trust envelope says when coverage is partial.
- **No proof.** A clean report means no gap was found within the bounds, not that the change is
  correct.
- **No model call.** The guard is deterministic. It does not ask another model to review the diff.

## Bounds

| Limit | Default | Why |
| :-- | --: | :-- |
| Consumers in one report | 10 | Keep the model-facing note under a token budget |
| Candidate tests | 5 | The agent can run the highest-value ones first |
| Routes | 5 | Same |
| Hook text | 1200 chars | `--max-chars` changes it (200-10000) |

When the list is cut, `truncated` is true and the omitted counts are recorded. Nothing disappears
without a number.

## Exit codes

| Code | Meaning |
| :-- | :-- |
| 0 | The check ran. This includes a clean report and a hook that returned `{}`. |
| 1 | The live index could not be built, or `--no-live` was given without a manifest. |
| 2 | A usage error: an unknown client, `--max-chars` out of range, or `--hook` combined with `--paths`. |

In hook mode a failure is fail-open: stdout is `{}` and the reason is on stderr, so a broken guard
cannot break the agent's turn.

## Related

- [Impact reports](impact.md) — the analysis the guard summarizes.
- [Briefs](brief.md) — the reviewer-facing pack.
- [MCP tools](tools.md) — the same analysis over MCP.
- [Stale context is your bug](stale-context-is-your-bug.md) — why freshness is a separate check.
