agentcontract-rs
Rust implementation of the AgentContract specification.
Install
# Cargo.toml
[]
= "0.1"
Quickstart
1. Write a contract:
# my-agent.contract.yaml
agent: my-agent
spec-version: "0.1.0"
version: "1.0.0"
assert:
- name: no_pii
type: pattern
must_not_match: '\b\d{3}-\d{2}-\d{4}\b'
description: No SSNs in output
limits:
max_latency_ms: 10000
max_cost_usd: 0.10
on_violation:
default: block
2. Enforce it in your agent:
use ;
3. When a violation occurs:
[block] no_pii: Forbidden pattern found at position 42: '123-45-6789'
CLI
# Check a contract file is valid
# Show contract metadata and assertions
# Validate a JSONL file of run contexts against a contract
runs.jsonl format (one JSON object per line):
{"input": "question", "output": "answer", "duration_ms": 1200, "cost_usd": 0.002}
{"input": "question2", "output": "SSN 123-45-6789", "duration_ms": 800, "cost_usd": 0.001}
Validator Types
| Type | How it works | Field |
|---|---|---|
pattern |
Regex on output | must_not_match, must_match |
latency |
duration_ms vs max_ms |
max_ms |
cost |
cost_usd vs max_usd |
max_usd |
llm |
LLM judge (roadmap) | — |
Limits (max_latency_ms, max_cost_usd, max_tokens) are checked automatically from the limits: block.
Audit Trail
Every run can be written to a tamper-evident JSONL file with a SHA-256 content hash:
use AuditWriter;
let writer = new;
writer.write.unwrap;
Each entry:
Architecture
agentcontract-rs/src/
├── lib.rs # Public API and re-exports
├── main.rs # CLI (check / info / validate)
├── models.rs # Contract, Assertion, Limits, OnViolation (serde)
├── loader.rs # load_contract() — YAML or JSON
├── runner.rs # ContractRunner, RunResult, ViolationRecord
├── audit.rs # AuditWriter — JSONL + SHA-256
└── validators/
├── mod.rs # RunContext, ValidationResult
├── pattern.rs # Regex must_not_match / must_match
├── latency.rs # duration_ms check
└── cost.rs # cost_usd check
Evaluation order follows spec §6.1:
limits → assert → must → must_not → ensures
Full Documentation
See the AgentContract specification.
Python implementation: pip install agentcontract → agentcontract-py
TypeScript implementation: npm install @agentcontract/core → agentcontract-ts
Roadmap
- LLM judge validator (optional
anthropicfeature flag) -
rollbackviolation action (snapshot/restore hook) -
requiresandinvariantclause evaluation - HMAC-signed audit trail (mirrors Python implementation)
- PyO3 bindings — expose Rust validator core to Python for performance
License
Apache 2.0 — Part of the AgentContract open standard.