Git-based governance middleware for AI coding agents
AI handles artifacts and execution. Humans handle business logic and ground truth.
English · Tiếng Việt
Note
defense-in-depth ships an opinionated scaffold, not a turnkey black box.
The deterministic guard pipeline (9 built-in guards + pure Guard interface) is the core. The optional .agents/ ecosystem (20 rules, cognitive tree, skill templates) is a starting point: fork it, delete what doesn't fit, and adapt it to your team's conventions.
Important
Client-side hooks are bypassable with --no-verify. For real enterprise governance, pair local hooks with the official GitHub Action and branch protection rules on your default branch.
# 1. Initialize inside any existing Git repository
npx defense-in-depth init
# What this does:
# ✅ Creates defense.config.yml with sensible defaults
# ✅ Installs pre-commit and pre-push Git hooks
# ✅ Enables hollow-artifact and ssot-pollution guards
# 2. Verify repository health
npx defense-in-depth doctor
# 3. Manually scan your staged files
npx defense-in-depth verify# Set up the full .agents/ governance framework for AI-driven workflows
npx defense-in-depth init --scaffoldAdd the official GitHub Action to validate pull requests beyond any agent's reach:
# .github/workflows/defense.yml
name: defense-in-depth
on: [pull_request]
jobs:
verify:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: tamld/defense-in-depth/.github/actions/verify@v1.0.0-rc.1| Guard | Default | Severity | Hook Trigger | What It Catches |
|---|---|---|---|---|
| Hollow Artifact | ✅ ON | BLOCK |
pre-commit |
Unfilled stub markers and empty scaffolding |
| SSoT Pollution | ✅ ON | BLOCK |
pre-commit |
Unauthorized edits to governance files (.agents/**, backlog.yml) |
| Root Pollution | ✅ ON | BLOCK |
pre-commit |
Unapproved scratch files or folders placed in the repository root |
| Commit Format | ✅ ON | WARN |
commit-msg |
Non-conventional commit messages (type(scope): description) |
| Branch Naming | ❌ OFF | WARN |
pre-push |
Branch names not matching feat/*, fix/*, chore/*, docs/* |
| Ticket Identity | ❌ OFF | WARN |
pre-commit |
Conflicting or missing ticket IDs in commit metadata (TKID) |
| Phase Gate | ❌ OFF | BLOCK |
pre-commit |
Committing feature code without an implementation_plan.md |
| HITL Review | ❌ OFF | BLOCK |
pre-commit |
Committing protected files without human review sign-off markers |
| Federation | ❌ OFF | BLOCK |
pre-commit |
Ticket state validation across federated/parent-child repositories |
📖 For complete configuration options and customization, see Configuration Guide.
defense-in-depth is engineered with a strict 4-tier layering model:
Tier 0 — Deterministic Core (Zero dependencies, stdlib + yaml only)
Regex/AST heuristics, Git hooks, sequential engine, <100ms execution
→ Guarantees: BLOCK/WARN on known anti-patterns anywhere
Tier 1 — Lesson Injection (Offline, evidence-grounded)
Matches findings to lessons.jsonl (wrongApproachPattern → correctApproach)
→ Guarantees: Actionable hints from past failures; RUNTIME/CODE evidence
Tier 2 — Optional Intelligence (Opt-in plugins)
Local embeddings (MiniLM), LLM-as-judge (Ollama/vLLM), DSPy semantic evaluation
→ Guarantees: Recall/precision boost when available; Tier 0+1 hold when offline
Tier 3 — Multi-Agent Governance (.agents/ directory)
Lazy-loaded rules, cognitive framework, and multi-agent contracts
→ Guarantees: Consistent behavioral standards across all AI contributors
flowchart LR
A["🤖 AI Agent<br/>generates code"] --> B["📦 git commit"]
B --> C{"🛡️ defense-in-depth<br/>pre-commit hook"}
C -->|"❌ BLOCK (Tier 0)"| D["Agent fixes<br/>before commit"]
C -->|"⚠️ WARN + Hint (Tier 1)"| E["Lesson suggestion<br/>from past failures"]
C -->|"✅ PASS"| F["Clean commit"]
E --> F
F --> G["👨💼 Human Review<br/>(Business Logic)"]
G -->|"Approved"| H["✅ Merged to main"]
| Command | Purpose |
|---|---|
npx defense-in-depth init [--scaffold] |
Install Git hooks and initialize defense.config.yml |
npx defense-in-depth verify [--staged] [--all] |
Execute the guard pipeline on staged or workspace files |
npx defense-in-depth doctor |
Run comprehensive environment and hook health checks |
npx defense-in-depth feedback --file <path> |
Ingest human feedback into the Án Lệ memory loop |
npx defense-in-depth lesson --tag <tag> |
Query recorded lessons and past failure patterns |
npx defense-in-depth eval |
Run semantic evaluation on artifacts via DSPy (Tier 3, opt-in) |
npx defense-in-depth hints-emit |
Emit context-aware progressive hints for agents |
📖 Full CLI reference: CLI Reference Manual.
Explore our comprehensive 5-pillar documentation:
- 🚀 Getting Started — Quickstart guide, SemVer Contract, and Migration Guide.
- ⚙️ User Guide — Configuration, CLI Reference, Providers, and Hints.
- 🛠️ Developer Guide — Architecture, Writing Custom Guards, DSPy Layer, and Fail-Fast Policy.
- 🤖 Ecosystem & Governance — Agent Guidelines and Multi-Agent Coordination.
- 🔭 Vision & Roadmap — Meta-Architecture, System Blueprint, and STRATEGY.md.
npm run dev # Watch mode compilation
npm test # Run test suite
npm run test:watch # Run tests in watch mode
npm run coverage # Run tests with strict coverage gate
npm run lint # TypeScript strict type check
npm run sync:wiki # Sync wiki/ directory to GitHub Wiki repoWe welcome contributions from both humans and AI agents operating under human direction!
- Read the Immutable Consistency Rules.
- Check open issues on GitHub Issues.
- Fork, create a branch (
feat/*,fix/*,docs/*), and open a Pull Request. - Ensure all tests pass:
npm testandnpx defense-in-depth verify.
Distributed under the MIT License. See LICENSE for more information.