Setup guide

Set up Mneme with Codex CLI and CI

Install mneme-hq 0.11.0 into a Python environment you control, activate one decision, and register its Codex hook in your repository’s .codex/hooks.json. Trust the hooks in Codex with /hooks. Codex then denies apply_patch changes that break the decision and audits the turn when it ends. Add the CI check for everything else.

Enforce: CLI + Codex hookVerify: CI checkDiscover: MCP, optional

You’ll end up with: Codex CLI denying an apply_patch change that breaks a decision with an activated rule before it runs, an end-of-turn audit of every file the session changed, and a required CI check behind both.

Goal

Codex CLI writes files mainly through apply_patch, its built-in tool for adding and changing files. Mneme’s Codex hook checks those patches before they run. At the end of each turn, when Codex finishes responding and fires its Stop event, the hook checks every file the session changed, which catches shell and script writes. The example decision is ADR-001, an architecture decision record (ADR) that bans the Celery library.

Mneme 0.11.0 ships the Codex hook as a Python module, with no installer and no published configuration. You register it yourself, as this guide shows.

Prerequisites

  • Python 3.11 or newer, and Codex CLI.
  • A git repository. The next section adds a decision with an activated rule.
  • Permission to add a CI workflow and make it a required check.

Tested versions: Mneme’s own validation ran the hook against Codex CLI 0.149.1 on Windows through codex exec, 10 runs out of 10. Interactive sessions, other platforms and other Codex versions are not separately proven. For this guide we ran the hook commands below against mneme-hq 0.11.0 by piping Codex events to it; we did not run Codex itself.

Configure

1. Install Mneme where the hook can find it

The hook runs as a Python file, so Codex needs the path of a Python interpreter that has mneme-hq installed. A dedicated virtual environment makes that path stable:

Command
python -m venv ~/.mneme-venv
~/.mneme-venv/bin/python -m pip install "mneme-hq==0.11.0"

On Windows the interpreter is .mneme-venv\Scripts\python.exe. This environment also gives you the mneme command for the setup steps. Then print the two paths the hook configuration needs:

Command
~/.mneme-venv/bin/python -c "import sys, mneme.integrations.codex_cli.hook as h; print(sys.executable); print(h.__file__)"

The first line is the interpreter, the second the hook file. On our Windows test machine they ended in venv\Scripts\python.exe and site-packages\mneme\integrations\codex_cli\hook.py. Step 3 writes both into the configuration for you.

2. Add one decision with an activated rule

Save this architecture decision record as docs/adr/ADR-001-no-celery.md:

File · docs/adr/ADR-001-no-celery.md
---
id: ADR-001
title: No Celery
status: accepted
priority: foundational
date: "2026-10-01"
scope: workers
---
Background jobs run on the managed Pub/Sub queue. Celery was retired after
the 2026 queue incident, and reintroducing it brings back a broker the team
no longer operates.

Then, from the repository root, create the memory file (.mneme/project_memory.json, where Mneme compiles your decisions), import the ADR, and validate and activate its rule. This decision blocks nothing until its rule is activated:

Command
~/.mneme-venv/bin/mneme setup
~/.mneme-venv/bin/mneme adr import docs/adr --memory .mneme/project_memory.json --apply
~/.mneme-venv/bin/mneme protect validate ADR-001 --memory .mneme/project_memory.json
~/.mneme-venv/bin/mneme protect activate ADR-001 --memory .mneme/project_memory.json

The last command prints Protection activated and verified: [ADR-001] (rule installed). The Claude Code setup shows each step’s output. Commit the ADR and the memory file.

3. Register the hook

Save this script as write_codex_hooks.py and run it from the repository root with the same interpreter. It writes .codex/hooks.json with your interpreter and hook paths filled in, so you edit nothing by hand:

File · write_codex_hooks.py
"""Write .codex/hooks.json for Mneme's Codex CLI hook.

Run it from your repository root with the Python interpreter that has
mneme-hq installed. It fills in that interpreter's path and the hook file's path.
"""
import json
import pathlib
import sys

import mneme.integrations.codex_cli.hook as hook

command = f'"{sys.executable}" "{hook.__file__}"'


def entry(timeout, matcher=None):
    item = {"hooks": [{"type": "command", "command": command, "timeout": timeout}]}
    return [{"matcher": matcher, **item}] if matcher else [item]


config = {"hooks": {
    "SessionStart": entry(60, "startup|resume"),
    "PreToolUse": entry(30, "^apply_patch$"),
    "Stop": entry(60),
}}
out = pathlib.Path(".codex") / "hooks.json"
out.parent.mkdir(exist_ok=True)
out.write_text(json.dumps(config, indent=2) + "\n", encoding="utf-8")
print(f"Wrote {out}")
print(f"Hook command: {command}")
Command
~/.mneme-venv/bin/python write_codex_hooks.py

The file it writes registers one command for three Codex events:

  • SessionStart records the state of the repository when the session begins.
  • PreToolUse on apply_patch checks the lines a patch adds and denies the call if they break an active rule.
  • Stop compares the repository with that starting state and checks each changed file whole.

This follows the hook template in Mneme’s Codex validation. We ran the script and the command it wrote against a test event; on Windows the paths come out with backslashes escaped as JSON requires.

4. Trust the hooks

Codex runs a hook only after you approve it. Start codex in the repository and type /hooks, the Codex command that lists configured hooks; review the three Mneme entries and trust them. Codex remembers one approval per event entry, so if the command changes later, for example after you rerun the script with another interpreter, trust it again.

5. Add the required CI check

Copy the GitHub Actions workflow or GitLab CI job and make it required. The CI check covers files changed outside a Codex session, which the hook never sees. Neither the hook nor CI stops a file from being deleted.

Run

Ask Codex for a worker in workers/jobs.py that uses Celery. Before the patch runs, Codex sends the hook an event like this one, saved as events/codex-celery.json:

File · events/codex-celery.json
{
  "session_id": "setup-guide",
  "turn_id": "t1",
  "cwd": ".",
  "hook_event_name": "PreToolUse",
  "tool_name": "apply_patch",
  "tool_input": {
    "command": "*** Begin Patch\n*** Add File: workers/jobs.py\n+from celery import Celery\n+\n+app = Celery(\"jobs\")\n*** End Patch"
  },
  "tool_use_id": "exec-1"
}

To see the hook’s answer without Codex, pipe the event to it with the interpreter from step 1:

Command
~/.mneme-venv/bin/python -m mneme.integrations.codex_cli.hook < events/codex-celery.json

Then the allowed version, events/codex-pubsub.json, which writes the same worker on Pub/Sub:

File · events/codex-pubsub.json
{
  "session_id": "setup-guide",
  "turn_id": "t1",
  "cwd": ".",
  "hook_event_name": "PreToolUse",
  "tool_name": "apply_patch",
  "tool_input": {
    "command": "*** Begin Patch\n*** Add File: workers/jobs.py\n+from google.cloud import pubsub_v1\n+\n+publisher = pubsub_v1.PublisherClient()\n*** End Patch"
  },
  "tool_use_id": "exec-1"
}

Expected result

For the Celery patch the hook prints a deny decision, which Codex applies before the patch touches disk:

Recorded output
{"hookSpecificOutput": {"hookEventName": "PreToolUse", "permissionDecision": "deny", "permissionDecisionReason": "mneme: FAIL - architectural decision violated\n  [ADR-001] FAIL \"celery\" - trigger: celery\n      No Celery\n      path: workers/jobs.py"}}

The hook always exits 0; Codex reads the decision from this JSON. For the Pub/Sub patch it prints nothing and exits 0, which leaves the patch to Codex’s normal approval flow.

In CI, a pull request with both workers fails on workers/jobs.py and passes workers/publish.py, as the CI-only setup shows with recorded output.

What is and isn’t enforced

  • Denied before they run: apply_patch Add File and Update File operations. In a patch with several operations, one violation denies the whole call.
  • Checked only at the end of the turn: shell and script writes. The file exists by then; the Stop check sends Codex back with the violation.
  • Not protected: deleting a file. A deletion adds no content, so the hook skips it; the Stop audit records it.
  • Files already modified before the session are ignored unless Codex touches them. Once it does, the whole file must pass at Stop, including violations that were there before.
  • Fails open, visibly: when the check can’t run, the patch goes through rather than being blocked. If the check can’t run, times out or can’t read a file, the hook adds an [mneme] UNEVALUATED note for Codex and does not block.
  • Not yet tested: changes made through Model Context Protocol (MCP) tools inside Codex.

The Codex CLI integration page links the validation evidence for each of these.

Troubleshooting

The hooks never run

They aren’t trusted, or a changed command lost its trust. Run /hooks again in Codex and trust all three.

Codex reports a hook error

The interpreter in hooks.json can’t import Mneme, or a path is wrong. Run the path command from step 1 with exactly that interpreter, and copy its output again.

A Celery patch goes through with no note

Two causes produce no output at all. The hook found no .mneme/project_memory.json in the session’s working directory, and with none it allows every patch silently; start Codex from the repository root. Or the rule isn’t active: mneme audit --memory .mneme/project_memory.json --repo-root . should list ADR-001 as protected, and if it says Mneme-ready, repeat step 2.

Codex shows a WARN note and applies the patch

MNEME_HOOK_MODE=warn is set in the environment Codex runs in. The hook then reports the violation without denying it:

Recorded output
{"hookSpecificOutput": {"hookEventName": "PreToolUse", "additionalContext": "[mneme] WARN - architectural decision flagged (warn mode; not blocked):\nmneme: FAIL - architectural decision violated\n  [ADR-001] FAIL \"celery\" - trigger: celery\n      No Celery\n      path: workers/jobs.py"}}

Unset the variable to deny again.

Next layer

Rolling this out across a team?

We work with design partners to set up enforcement on a real repository.