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

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.2.6
File Size Uploaded Provenance
contract_agent-0.2.6.tar.gz 214.7 kB With provenance Details

Built distribution (wheel)

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

Total release size: 255.7 kB

Release files / contract_agent-0.2.6.tar.gz

Download URL contract_agent-0.2.6.tar.gz
Size 214.7 kB
Tags Source
SHA-256 checksum
How to use checksums
f6d6a8574f8dffe3348d8e72f212e3ad830f73c92b2438a9ae71510fb66f17cd
BLAKE2b-256 checksum
How to use checksums
b980611b7627f06f2a0f12c82ebc1b28d539c2961832544bf14cab28face5226
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.2.6-py3-none-any.whl

Download URL contract_agent-0.2.6-py3-none-any.whl
Size 40.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
da854a176e55f2da2dde96be39e66fa45d40b9c025293d9d7494db2e3af9737a
BLAKE2b-256 checksum
How to use checksums
574a0cb8b2ec18cd404afadf40c2f504a5b64054fa80bb68603e63dabcc285a5
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.2.6

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