ContractAgent
Spec-Driven Behavioral Contract & Verification Compiler for Tool-Using AI Agents
⚡ Overview
ContractAgent brings formal Spec-as-Source engineering discipline to autonomous AI agents. Behavioral invariants, tool schemas, and multi-turn workflows are defined in declarative specifications (agent.contract.yaml) and enforced deterministically at runtime via Google Common Expression Language (CEL).
The Dual-Synthesis Compiler uses decoupled LLM personas to synthesize typed Python agent implementations and adversarial test suites, continually verifying and repairing candidates in an isolated self-healing sandbox before gated promotion.
graph LR
A[agent.contract.yaml] --> B[ContractParser]
B --> C[Deterministic CodeGen<br/>Protocols & Mocks]
B --> D[Dual-Synthesis<br/>Implementer vs Tester]
C --> E[Sandboxed Verification<br/>CEL Guards + Pytest]
D --> E
E -->|Defect Detected| F[Self-Healing Engine<br/>Trace + CEL Diagnostics]
F -->|Repair Candidate| E
E -->|100% Passed| G[Interactive Review Gate<br/>Unified Git Diff]
G -->|Promote| H[dist/ Artifacts]
🛡️ Key Capabilities
- Spec-as-Source: Declarative behavioral contract is the sole immutable source of truth; code is a reproducible build artifact.
- Zero-LLM Invariant Enforcement: Evaluated deterministically with memory-bounded, non-Turing complete Google CEL rules.
- Strict Protocol Binding: Generates typed
Protocolinterfaces (dist/interface.py) and synthetic mocks (dist/mocks.py). Human backend handlers inservices/are never overwritten. - Decoupled Dual-Synthesis:
AgentImplementerandAdversarialTesterpersonas operate independently to prevent correlated blindspots. - Autonomous Self-Healing: Captures execution tracebacks and CEL violation payloads, iteratively repairing candidate logic within 3 retries.
- Human Review Gate: Interactive unified Git diff requires developer approval before promotion; supports
--headless-cifor CI automation. - Budget Discipline: Built-in token accounting and cost tracking guarantee compilation remains strictly under $0.05 per verified agent.
🚀 Quickstart
Prerequisites
- Python 3.11+
- uv package manager
1. Installation
git clone https://github.com/stefanosello/contract-agent.git
cd contract-agent
uv sync
2. Compile an Agent Contract
Compile interactively with unified Git diff review:
uv run contract-agent compile tests/fixtures/benchmarks/01_billing_dispute.contract.yaml
Run in headless CI mode with zero-network mock provider:
uv run contract-agent compile tests/fixtures/benchmarks/01_billing_dispute.contract.yaml \
--output-dir dist \
--provider mock \
--headless-ci
Compile with live budget-efficient backends:
# DeepSeek-V3
export DEEPSEEK_API_KEY="your-api-key"
uv run contract-agent compile agent.contract.yaml --provider deepseek-v3
# Gemini Flash
export GEMINI_API_KEY="your-api-key"
uv run contract-agent compile agent.contract.yaml --provider gemini-flash
📝 Example Contract Specification
spec_version: "0.3"
metadata:
name: "billing_dispute_agent"
version: "1.0.0"
invariants:
- id: "INV-001"
target: "tool:execute_refund"
description: "Refund amount cannot exceed $100 without manager approval"
rule: "args.amount <= 100.0"
on_violation: "require_escalation"
- id: "INV-002"
target: "session:call_sequence"
description: "fetch_invoice must be called before execute_refund"
rule: "called_before('fetch_invoice', 'execute_refund')"
on_violation: "block_tool_call"
tools:
- name: "fetch_invoice"
description: "Fetches invoice details by invoice ID"
parameters:
type: "object"
required: ["invoice_id"]
properties:
invoice_id: { type: "string" }
- name: "execute_refund"
description: "Executes customer refund against invoice"
parameters:
type: "object"
required: ["invoice_id", "amount", "reason"]
properties:
invoice_id: { type: "string" }
amount: { type: "number" }
reason: { type: "string" }
🧪 Testing & Verification
# Run full test suite (67 unit, property, and integration tests)
uv run pytest
# Mathematical boundary fuzzing & mutation trapping
uv run pytest tests/property/ tests/mutation/
# Static type check (0 errors enforced)
uv run pyrefly check
# Code formatting & lint check
uv run ruff check src tests
🏛️ Governance
Development is strictly governed by the ContractAgent Constitution with deterministic Git pre-commit hooks enforcing:
- Maximum 200 LOC per commit
- Maximum 10 files per commit
- Protected
mainbranch with mandatory Pull Request protocol
Metadata
Release files for contract-agent 0.2.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| contract_agent-0.2.5.tar.gz | 214.6 kB | With provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| contract_agent-0.2.5-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 255.4 kB
Release files / contract_agent-0.2.5.tar.gz
| Download URL | contract_agent-0.2.5.tar.gz |
|---|---|
| Size | 214.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
55af36cc51a038590cd6114a0143306fe17dad13e13baa22a371039d397e64c7
|
|
BLAKE2b-256 checksum How to use checksums |
5a666a70f60ac3af53d5aa2fb353c6370bcaca01a827d672dbda3689e34e9151
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 23, 2026.
Transparency logRelease files / contract_agent-0.2.5-py3-none-any.whl
| Download URL | contract_agent-0.2.5-py3-none-any.whl |
|---|---|
| Size | 40.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
23303d9275489a1bad068e948c5cf0ceeb660cd12fbb174aa8c9146db1054718
|
|
BLAKE2b-256 checksum How to use checksums |
0949bf0986a8237a00858fc5846ca914fe7cceb759800a5462ff48cf921e1cfa
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 23, 2026.
Transparency logSecurity for contract-agent 0.2.5
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for contract_agent-0.2.5.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 23, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.