How-to guide

Stop an AI coding agent from crossing an architectural boundary

Load the Mneme plugin in Claude Code. Its hook, a command Claude Code runs before the agent edits a file, checks each change against your recorded decisions and blocks a violation, so the file doesn’t change and the agent sees why. Shell writes it can’t check in advance are caught when the agent finishes its turn.

You’ll end up with: Claude Code refusing to write a direct database import under app/handlers/, with the broken decision named in the feedback the agent receives.

Goal

A pull request check catches a forbidden import after the agent wrote it, and someone still has to send the work back. A hook catches it while the agent is still working on the change. A hook is a command Claude Code runs at fixed points; the Mneme plugin registers one that runs before every file edit and shell command, at session start, and each time the agent finishes a turn.

The hook runs in strict mode by default: when a change breaks a decision, it exits with code 2, which Claude Code treats as “block this change”. The example decision is an architecture decision record (ADR), ADR-012, that bans importing the database client in request handlers.

Prerequisites

  • A git repository with ADR-012 imported into the memory file, .mneme/project_memory.json, where Mneme stores your decisions. This is the ADR:
File · docs/adr/ADR-012-handlers-use-repositories.md
---
id: ADR-012
title: Request handlers do not import the database client
status: accepted
priority: foundational
date: "2026-10-01"
scope: handlers
---
Handlers call repositories. A direct client import skips the transaction
and tenancy handling that lives in app/repositories.

## Constraints
- FORBID_LITERAL:
    value: from app.db.client import
    include_paths:
      - "app/handlers/**"
Command
pipx install "mneme-hq==0.11.0"
  • Claude Code. You install the Mneme plugin from Claude’s plugin directory in the first step below. The Claude Code integration page lists what the plugin contains.

Configure

1. Install the plugin from the directory

Command
claude plugin install mneme@anthropic-plugin-directory

The plugin adds the hook and four slash commands. It does not install the mneme command, which the pipx step above already did.

2. Start Claude Code from your repository root

Command
claude

Keep the default strict mode. Warn mode (MNEME_HOOK_MODE=warn) stops the hook from blocking edits, and Claude Code then shows neither you nor the agent why an edit broke a decision. See Troubleshooting.

To fix the plugin files at the 0.11.0 release instead, for example to review them first, skip step 1 and load them from a checkout:

Command
git clone --branch v0.11.0 --depth 1 https://github.com/MnemeHQ/mneme mneme-src
claude --plugin-dir ./mneme-src/integrations/claude-code-plugin

Run

Ask Claude Code for a change that breaks ADR-012, for example: “Add a list_invoices handler in app/handlers/invoices.py that queries the invoices table with get_connection from app.db.client.”

When the agent goes to write the file, Claude Code sends the hook an event: a JSON description of the tool call (Write here; the hook also checks Edit and MultiEdit). This one is shortened to relative paths; Claude Code sends absolute ones.

File · events/write-direct-import.json
{
  "hook_event_name": "PreToolUse",
  "session_id": "guide-2",
  "cwd": ".",
  "tool_name": "Write",
  "tool_input": {
    "file_path": "app/handlers/invoices.py",
    "content": "from app.db.client import get_connection\n\n\ndef list_invoices(customer_id: str):\n    conn = get_connection()\n    return conn.execute(\"SELECT * FROM invoices WHERE customer_id = ?\", (customer_id,))\n"
  }
}

To see the hook's answer without running an agent, save the event as events/write-direct-import.json and pipe it to the hook from the repository root:

Command
mneme-hook < events/write-direct-import.json

Expected result

The hook exits with code 2 and prints:

Recorded output
mneme: FAIL - architectural decision violated
  [ADR-012] FAIL "from app.db.client import" - trigger: from app.db.client import
      Request handlers do not import the database client
      path: app/handlers/invoices.py via app/handlers/**

Claude Code treats exit code 2 from this hook as a block. The file isn't written, and the agent receives that text as feedback. A write that calls the repository instead exits 0 with no output, so the agent can carry on.

Shell writes

The hook also reads shell commands. A single command of the form cat > app/handlers/invoices.py <<'PY', with a quoted delimiter and nothing else in the command, is blocked the same way. Its event, events/bash-heredoc.json:

File · events/bash-heredoc.json
{
  "hook_event_name": "PreToolUse",
  "session_id": "guide-2",
  "cwd": ".",
  "tool_name": "Bash",
  "tool_input": {
    "command": "cat > app/handlers/invoices.py <<'PY'\nfrom app.db.client import get_connection\n\n\ndef list_invoices(customer_id: str):\n    conn = get_connection()\n    return conn.execute(\"SELECT * FROM invoices WHERE customer_id = ?\", (customer_id,))\nPY\n"
  }
}
Command
mneme-hook < events/bash-heredoc.json
Recorded output
mneme: FAIL - architectural decision violated
  [ADR-012] FAIL "from app.db.client import" - trigger: from app.db.client import
      Request handlers do not import the database client
      path: app/handlers/invoices.py via app/handlers/**

Other shell writes can't be checked before they run: echo >, tee, sed -i, an unquoted heredoc, a heredoc chained after another command, or a script such as python scripts/gen_invoices.py. The hook can't work out in advance what they will write, so it lets them through. Its note says the command might change files (POTENTIALLY_MUTATING) and that the end-of-turn check still applies:

Recorded output
mneme-hook: Bash classified POTENTIALLY_MUTATING: no deterministic pre-execution check (session-delta backstop still applies).

The end-of-turn check catches them instead. Here is the script the agent might run, scripts/gen_invoices.py:

File · scripts/gen_invoices.py
from pathlib import Path

Path("app/handlers").mkdir(parents=True, exist_ok=True)
Path("app/handlers/invoices.py").write_text("from app.db.client import get_connection\n")

At session start the hook records the state of the repository, its baseline. When the agent finishes its turn, Claude Code sends a Stop event, and the hook checks the content each file gained since the baseline. Replayed in order:

Command
mneme-hook < events/session-start.json
python scripts/gen_invoices.py
mneme-hook < events/stop.json

The Stop check blocks the end of the turn and sends the agent back with the violation:

Recorded output
{"decision": "block", "reason": "mneme: repository mutations made during this session violate 1 governed artifact(s):\n[app/handlers/invoices.py]\nmneme: FAIL - architectural decision violated\n  [ADR-012] FAIL \"from app.db.client import\" - trigger: from app.db.client import\n      Request handlers do not import the database client\n      path: app/handlers/invoices.py via app/handlers/**"}

Evidence

Every FAIL block names the decision (ADR-012), the matched text, and the path selector that made the rule apply. Behind the hook is the same mneme check command you can run yourself. To keep a machine-readable record of a file's state, run it with --json:

Command
mneme check --memory .mneme/project_memory.json \
  --input app/handlers/invoices.py \
  --target-path app/handlers/invoices.py \
  --query "edit to app/handlers/invoices.py" \
  --json
Recorded output
{"schema": "mneme.check/v1", "verdict": "FAIL", "mode": "strict", "evaluation_complete": true, "applicability": [{"decision_id": "ADR-012", "rule_type": "FORBID_LITERAL", "rule_value": "from app.db.client import", "rule_index": 0, "path_scoped": true, "input_path": "app/handlers/invoices.py", "outcome": "APPLIED", "selector": "app/handlers/**", "reason": "an include selector matched", "rule_id": "ADR-012:FORBID_LITERAL:e7f58cdee4107aea2503f458432cbf7f", "decision_version_id": "dver-07b2d935ba6c357186624774df0c2845"}], "violations": [{"decision_id": "ADR-012", "decision_text": "Request handlers do not import the database client", "severity": "FAIL", "rule": "from app.db.client import", "trigger": "from app.db.client import", "kind": "typed_rule", "rule_type": "FORBID_LITERAL", "input_path": "app/handlers/invoices.py", "selector": "app/handlers/**", "rule_id": "ADR-012:FORBID_LITERAL:e7f58cdee4107aea2503f458432cbf7f", "decision_version_id": "dver-07b2d935ba6c357186624774df0c2845"}], "freshness": []}

Troubleshooting

Nothing is ever blocked, and the hook prints nothing

The hook didn't find a memory file. It looks for .mneme/project_memory.json in the session's working directory and each parent directory, or at the path in MNEME_MEMORY. With no memory file it allows every change silently:

Command
MNEME_MEMORY=missing.json mneme-hook < events/write-direct-import.json
Recorded output

Start Claude Code from the repository root, or set MNEME_MEMORY.

not inside a git work tree

Recorded output
{"hookSpecificOutput": {"hookEventName": "Stop", "additionalContext": "mneme: not inside a git work tree; the session-delta gate is inactive for this turn."}}

The end-of-turn check compares against git. Run Claude Code inside a git repository. Checks before each write still work without git.

Edits that break ADR-012 go through, with no message

MNEME_HOOK_MODE=warn is set. Check echo $MNEME_HOOK_MODE in the shell that started Claude Code, and the plugin's mode option, which sets the same thing. In warn mode the hook doesn't block; it answers defer, which hands the edit to Claude Code's normal permission flow. Claude Code doesn't show the violation the hook attaches as the reason:

Recorded output
{"hookSpecificOutput": {"hookEventName": "PreToolUse", "permissionDecision": "defer", "permissionDecisionReason": "mneme: FAIL - architectural decision violated\n  [ADR-012] FAIL \"from app.db.client import\" - trigger: from app.db.client import\n      Request handlers do not import the database client\n      path: app/handlers/invoices.py via app/handlers/**"}}

Claude Code ignores that reason, so neither the agent nor you sees it. Only the end-of-turn check reaches the agent in warn mode. Unset the variable to go back to strict mode, and use mneme check or the CI check to collect findings instead.

Limits

  • The hook fails open: if a mneme check call crashes, takes longer than 10 seconds, or can't complete its evaluation (INCOMPLETE), the hook allows the change without telling the agent. Its notes go to Claude Code's debug log only.
  • Shell writes other than that single cat heredoc form are checked only when the agent finishes its turn, after the file exists. The hook then keeps the agent working and feeds back the violation. It doesn't undo the write.
  • The rule matches exact text, so import app.db.client as db isn't caught by this ADR. See Turn an ADR into an enforceable decision.
  • Other agents get different coverage. Codex CLI checks apply_patch add and update operations before they run, and audits each changed file as a whole when the agent stops. Kiro checks its native write tools only, with no shell or end-of-turn checks.

Next