If you’ve been running autonomous coding assistants like **OpenAI Codex CLI**, **Claude Code**, or **Google Antigravity**, you know the routine:
- The agent hallucinates bad Python syntax or tries an unsafe shell command.
- The execution fails (or worse, wipes `.env` / formats wrong files).
- The error trace gets dumped back into context.
- You burn $0.10–$0.50 and 3–5 recovery turns just getting the model back on track.
To solve this locally with zero GPU overhead, I built **Aetheris Microbrains** — an ultra-lightweight, deterministic 4-layer pre-flight AST policy engine and tool gate that intercepts tool calls in `< 2 ms` before execution.
We just added native integration for **OpenAI Codex CLI** (alongside Claude Code, Hermes, and Google Antigravity).
🔗 **GitHub Repo:** https://github.com/drtikov/microbrainsforagents
---
### 🤖 What it does in OpenAI Codex CLI
Codex CLI supports native `PreToolUse` and `PostToolUse` command hooks. Microbrains plugs directly into `.codex/hooks.json` via `--host codex`:
**PreToolUse Hard Deny (`exit 2` + `permissionDecision: "deny"`):**
- Structural AST validation catches broken Python syntax (unclosed brackets, bad indentation) before Codex ever touches disk.
- Vetoes credential exfiltration (`cat .env | curl`, `AWS_SECRET_KEY` in code) and destructive commands (`rm -rf /`, raw disk writes).
**Seamless Escalation:**
- Ambiguous commands cleanly defer to Codex’s host approval policy (`defer_to_host_policy`), avoiding broken execution while respecting your approval settings.
**PostToolUse Security Warnings:**
- Scans tool outputs for leaked secret keys or indirect prompt injections and injects contextual warnings into Codex before context compaction.
**Zero Hassle Setup:**
- Install with one command:
```bash
pip install -e .
python .agents/skills/microbrains/install_codex_hook.py --profile dev
python .agents/skills/microbrains/install_codex_hook.py --self-test
```
- Supports a safe **`--shadow`** mode so you can audit what it *would* block in `decision_journal.jsonl` before turning on active enforcement.
---
### 📊 Measured Benchmark Results (Windows / Python 3.10.6)
Here are direct measurements from our deterministic benchmark suites:
#### 1. Deterministic 26-Step Coding Session A/B (`test_skill_gain_ab.py`)
Tested across 14 benign tool calls and 12 seeded hazards:
* **Hazards caught:** **12 / 12 (100%)** *(0/12 without gate)*
* **Python syntax crashes:** **4 → 0 (100% fewer)**
* **Unsafe actions allowed:** **8 → 0 (100% blocked)**
* **Benign false positives:** **0 / 14 (0.0% false alarm rate)**
* **Wasted recovery turns:** **8 turns eliminated** (~37,055 modeled input tokens saved, ~$0.1112 saved per session)
* **Error-feedback tokens in context:** **146 → 37 (-74.7%)**
* **Local gate latency:** **0.73 ms mean / 1.27 ms p95** (pure CPU NumPy/AST)
#### 2. Security & Anti-Evasion Benchmark (`10_agent_security_benchmark.py`)
* **Attack vectors caught:** **22 / 22 (100.0%)**
* **Benign controls blocked:** **0 / 12 (0.0%)**
* **Local decision latency:** **0.41 ms p50 / 2.79 ms p95**
#### 3. Real SWE-bench Lite Gate (300 Real GitHub PR Diffs)
* Real gold patches evaluated: 71.7% auto-allowed, 27.3% safely escalated, and 1.0% denied (only for hardcoded `eval`/`exec`).
* **AST Syntax False Positives on real code:** **0.0%**
* **Synthetic code mutations caught:** **60 / 60 (100%)**
#### 4. Hook Overhead Note
The engine itself evaluates in under **1.5 ms**. On Windows, spawning a fresh Python subprocess for command hooks takes ~400 ms p50 per hook (Python interpreter cold-start). If you want that down to single-digit milliseconds, Microbrains includes a local warm daemon (`aetheris-daemon`).
---
### 🏆 What You Win by Using Microbrains
- **Lower API Bills & Fewer Context Bloats:** Stopping a syntax error or hallucinated shell call before execution prevents 5–15 seconds of waiting and the cascading multi-turn error loops that burn through prompt context.
- **Air-Tight Local Guardrails:** Protects `.env`, `.ssh/`, `.git/hooks/`, detects base64 decode-to-exec tricks, and stops outbound credential egress.
- **Prompt Cache Protection:** Includes sticky-session tool narrowing that cuts tool schema tokens by **up to 56.7%** without breaking Anthropic/OpenAI prompt cache prefixes.
- **Complete Forensic Journal:** Everything evaluated is logged with SHA256 hashes and rationale. Run `python -m aetheris_microbrains.audit --explain` to see exactly why an agent call was blocked or escalated.
Code is open-source (Apache-2.0):
👉 **Check it out on GitHub:** https://github.com/drtikov/microbrainsforagents
Feedback, PRs, and test cases are welcome! Let me know if you run it in your own Codex or Claude Code setups.