repo-contract — AI Agent Governance Layer
repo-contract is a lightweight, portable Python package and command-line tool that acts as an enforceable governance layer for AI coding agents (such as Claude Code, Cursor, Codex, Windsurf, Aider, etc.).
It allows repositories to declare machine-checkable architectural, safety, and workflow rules. The engine deterministically validates proposed agent plans (pre-edit) and git diff patches (post-edit) to ensure compliance before code is modified or merged.
Why Governance?
AI agents are excellent at generating edits, but often fail to respect repository-specific constraints:
- Scope Creep: Editing unrelated components or refactoring large sub-systems.
- Architectural Regressions: Importing internal database handlers directly into view controllers.
- Workflow Breaches: Editing crucial functionality without adding corresponding tests.
- Safety Violations: Reading/modifying forbidden files (
.env,secrets/) or sensitive directories.
Rather than trying to help agents "understand more code" or storing synthetic meaning, repo-contract focuses on enforcing negative constraints, which empirical studies show is the most effective way to shape safe agent workflows.
Key Features
- YAML/TOML Contracts: Declarative policy schema containing architecture layers, safety paths, agent limits, and test mappings.
- Python Module Inspector: AST-based import resolution (both absolute and relative package paths).
- Plan Validation: Pre-checks an agent's text/markdown plan block, extracting planned files and verifying safety boundaries before edits begin.
- Diff Validation: Analyzes git patches/diffs to catch forbidden imports, path violations, test gaps, and file counts.
- Dependency Guard: Warns when a diff adds imports of external packages that are not declared in
pyproject.toml/requirements.txt/setup.py— a common AI-agent failure mode. - Unrelated Refactor Detection: Identifies whether modifications span disjoint, unconnected modules in the import graph (connected components analysis).
- Deterministic Engine: 100% deterministic rules with clear, human-readable explanations and JSON-RPC outputs.
- MCP Server Integration: Stdio-based Model Context Protocol (MCP) server wrapper that exposes validators directly as tools for AI assistants.
Installation
pip install repo-contract
Requires Python 3.11 or newer. This installs the repo-contract command along with the MCP server.
Installing from source (for development)
git clone https://github.com/dager23/RepoContract.git
cd RepoContract
pip install -e ".[dev]"
pytest
Quick Start
1. Initialize the Contract
Create a default configuration file in your repository:
repo-contract init
# Generates repo-contract.yaml
2. Configure Rules
Modify repo-contract.yaml (or TOML equivalent) to define your rules:
version: 1
project:
name: my-app
src_root: "my_app" # Directory containing python package modules
architecture:
layers:
- name: ui
paths: ["my_app/ui/**"]
# Entries may be layer names ("db"), dotted module prefixes ("my_app.db"),
# or path prefixes ("my_app/db") — all three are equivalent.
cannot_import: ["my_app/db"] # UI layer cannot import DB layer directly
- name: db
paths: ["my_app/db/**"]
cannot_import: ["my_app/ui"]
workflow:
required_tests:
# Require corresponding test file to be modified when source is modified
- when_paths_match: ["my_app/**/*.py"]
require: ["tests/**/test_*.py"]
safety:
forbidden_paths:
- ".env"
- "secrets/**"
approval_required_for:
- "my_app/auth/**"
agent:
max_files_per_change: 6
forbid_unrelated_refactors: true
dependencies:
# Warn when a diff adds imports of packages not declared in the
# project's dependency files (pyproject.toml / requirements.txt / setup.py)
check_undeclared: true
3. Run Validation
Scan Codebase
Scan the entire repository for static layer boundary violations or missing tests:
repo-contract scan
Validate Agent Plan (Pre-edit)
Pass the agent's proposed plan (as raw text or from a file) to ensure they aren't touching forbidden files or planning changes that are too broad:
repo-contract check-plan "I will edit my_app/auth/login.py and .env to configure authentication."
# Fails because .env is forbidden and auth/ requires approval.
# Or validate a plan saved to a file:
repo-contract check-plan plan.md
If the argument is a path to an existing file it is read as a plan file; otherwise it is treated as raw plan text (use --text to force raw-text interpretation).
Validate Git Diff (Post-edit / CI Gate)
Check uncommitted working tree edits, a specific git commit range, or a patch file:
# Validate local uncommitted edits
repo-contract check-diff
# Validate staging changes
repo-contract check-diff --staged
# Validate range (e.g. CI run)
repo-contract check-diff HEAD~1..HEAD
# Validate a patch file
repo-contract check-diff my-change.patch
Model Context Protocol (MCP) Integration
You can run repo-contract as a stdio-based MCP Server in your favorite editor (like Cursor, Claude Desktop, or Windsurf) by adding this command to your configuration:
{
"mcpServers": {
"repo-contract": {
"command": "python",
"args": ["-m", "repo_contract.mcp.server"],
"cwd": "/path/to/your/project"
}
}
}
Exposed Tools
scan_repository: Runs structural checks on the current workspace.check_plan: Analyzes the agent's textual/markdown plan, extracting planned files and validating bounds before edits begin.check_diff: Checks current uncommitted working directory edits.
License
MIT © Yash Shah
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file repo_contract-0.2.1.tar.gz.
File metadata
- Download URL: repo_contract-0.2.1.tar.gz
- Upload date:
- Size: 34.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
009e2dce735244b0d7738e9e85165a17da34edae65b45ef87c949bdd1582a05c
|
|
| MD5 |
a5b5f0357b900dcd89bb2c7174a6e08e
|
|
| BLAKE2b-256 |
65e87feb256c5425242e66318feacc8c0af872cde00d71a8791ad70d2cbd6e6f
|
Provenance
The following attestation bundles were made for repo_contract-0.2.1.tar.gz:
Publisher:
publish.yml on dager23/RepoContract
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
repo_contract-0.2.1.tar.gz -
Subject digest:
009e2dce735244b0d7738e9e85165a17da34edae65b45ef87c949bdd1582a05c - Sigstore transparency entry: 2348964559
- Sigstore integration time:
-
Permalink:
dager23/RepoContract@48f86dd7e6c08bbf598d101affe573042c9ac5de -
Branch / Tag:
refs/tags/v0.2.1 - Owner: https://github.com/dager23
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@48f86dd7e6c08bbf598d101affe573042c9ac5de -
Trigger Event:
release
-
Statement type:
File details
Details for the file repo_contract-0.2.1-py3-none-any.whl.
File metadata
- Download URL: repo_contract-0.2.1-py3-none-any.whl
- Upload date:
- Size: 30.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1c79af93f52f3ed091765e292db25aa71cb0ad729216b3b49a05de4478c44217
|
|
| MD5 |
52f189d18563eb236b8ff6dce63aa63f
|
|
| BLAKE2b-256 |
a572fcb538441d0ba43e1da751b099fdcafa0e31e7f968e79afad168822ce42e
|
Provenance
The following attestation bundles were made for repo_contract-0.2.1-py3-none-any.whl:
Publisher:
publish.yml on dager23/RepoContract
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
repo_contract-0.2.1-py3-none-any.whl -
Subject digest:
1c79af93f52f3ed091765e292db25aa71cb0ad729216b3b49a05de4478c44217 - Sigstore transparency entry: 2348965027
- Sigstore integration time:
-
Permalink:
dager23/RepoContract@48f86dd7e6c08bbf598d101affe573042c9ac5de -
Branch / Tag:
refs/tags/v0.2.1 - Owner: https://github.com/dager23
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@48f86dd7e6c08bbf598d101affe573042c9ac5de -
Trigger Event:
release
-
Statement type: