Skip to main content

ContractAgent Logo

ContractAgent

Spec-Driven Behavioral Contract & Verification Compiler for Tool-Using AI Agents

CI Status Coverage Python 3.11+ Pyrefly Ruff CEL Runtime Constitution License


⚡ 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 Protocol interfaces (dist/interface.py) and synthetic mocks (dist/mocks.py). Human backend handlers in services/ are never overwritten.
  • Decoupled Dual-Synthesis: AgentImplementer and AdversarialTester personas 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-ci for 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 main branch with mandatory Pull Request protocol

Metadata

Release files for contract-agent 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for contract-agent 0.1.0
File Size Uploaded Provenance
contract_agent-0.1.0.tar.gz 214.2 kB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for contract-agent 0.1.0
File Interpreter ABI Platform
contract_agent-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 254.8 kB

Release files / contract_agent-0.1.0.tar.gz

Download URL contract_agent-0.1.0.tar.gz
Size 214.2 kB
Tags Source
SHA-256 checksum
How to use checksums
7aca819f6a632806ddbbe5f0022f9b8fc1c334ec010196c65ee4215bff24183a
BLAKE2b-256 checksum
How to use checksums
391ebb128f40886f3f8dda93661f9d0c401ecc00b065fcfc0bcd9cbd4261cd26
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 log

Release files / contract_agent-0.1.0-py3-none-any.whl

Download URL contract_agent-0.1.0-py3-none-any.whl
Size 40.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9d6ceb1047d34b592466ab9a7cf2b6bba03421811e07759b1e2ef76b9d5f88e9
BLAKE2b-256 checksum
How to use checksums
17a2a6434a400358fe037b214c1f43028215793f30b2c9eea0d7c9fe6e408a71
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 log

Security for contract-agent 0.1.0

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.

Select from 2 files

Attestation for contract_agent-0.1.0.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 log

A 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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page