agentcontract 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,11 @@
1
+ __pycache__/
2
+ *.py[cod]
3
+ *.pyo
4
+ .pytest_cache/
5
+ *.egg-info/
6
+ dist/
7
+ build/
8
+ .eggs/
9
+ *.egg
10
+ agentcontract-audit.jsonl
11
+ .env
@@ -0,0 +1,112 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship made available under
36
+ the License, as indicated by a copyright notice that is included in
37
+ or attached to the work.
38
+
39
+ "Derivative Works" shall mean any work, whether in Source or Object
40
+ form, that is based on (or derived from) the Work and for which the
41
+ editorial revisions, annotations, elaborations, or other
42
+ transformations represent, as a whole, an original work of authorship.
43
+
44
+ "Contribution" shall mean, as submitted to the Licensor for inclusion
45
+ in the Work by the copyright owner or by an individual or Legal Entity
46
+ authorized to submit on behalf of the copyright owner.
47
+
48
+ "Contributor" shall mean Licensor and any Legal Entity on behalf of
49
+ whom a Contribution has been received by the Licensor and included
50
+ within the Work.
51
+
52
+ 2. Grant of Copyright License. Subject to the terms and conditions of
53
+ this License, each Contributor hereby grants to You a perpetual,
54
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
55
+ copyright license to reproduce, prepare Derivative Works of,
56
+ publicly display, publicly perform, sublicense, and distribute the
57
+ Work and such Derivative Works in Source or Object form.
58
+
59
+ 3. Grant of Patent License. Subject to the terms and conditions of
60
+ this License, each Contributor hereby grants to You a perpetual,
61
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
62
+ patent license to make, use, sell, offer for sale, import, and
63
+ otherwise transfer the Work.
64
+
65
+ 4. Redistribution. You may reproduce and distribute copies of the
66
+ Work or Derivative Works thereof in any medium, with or without
67
+ modifications, and in Source or Object form, provided that You
68
+ meet the following conditions:
69
+
70
+ (a) You must give any other recipients of the Work or Derivative
71
+ Works a copy of this License; and
72
+
73
+ (b) You must cause any modified files to carry prominent notices
74
+ stating that You changed the files; and
75
+
76
+ (c) You must retain, in the Source form of any Derivative Works
77
+ that You distribute, all copyright, patent, trademark, and
78
+ attribution notices from the Source form of the Work; and
79
+
80
+ (d) If the Work includes a "NOTICE" text file, you must include a
81
+ readable copy of the attribution notices contained within such
82
+ NOTICE file.
83
+
84
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
85
+ any Contribution submitted for inclusion in the Work shall be under
86
+ the terms and conditions of this License.
87
+
88
+ 6. Trademarks. This License does not grant permission to use the trade
89
+ names, trademarks, service marks, or product names of the Licensor.
90
+
91
+ 7. Disclaimer of Warranty. Unless required by applicable law or agreed
92
+ to in writing, Licensor provides the Work on an "AS IS" BASIS,
93
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND.
94
+
95
+ 8. Limitation of Liability. In no event shall any Contributor be liable
96
+ for any damages arising from this License or use of the Work.
97
+
98
+ 9. Accepting Warranty or Additional Liability. While redistributing
99
+ the Work, You may offer fee-based support, warranty, or other
100
+ liability obligations. However, in accepting such obligations, You
101
+ may offer such obligations only on Your own behalf and on Your sole
102
+ responsibility.
103
+
104
+ END OF TERMS AND CONDITIONS
105
+
106
+ Copyright 2026 Mauro Moro
107
+
108
+ Licensed under the Apache License, Version 2.0 (the "License");
109
+ you may not use this file except in compliance with the License.
110
+ You may obtain a copy of the License at
111
+
112
+ http://www.apache.org/licenses/LICENSE-2.0
@@ -0,0 +1,178 @@
1
+ Metadata-Version: 2.4
2
+ Name: agentcontract
3
+ Version: 0.1.0
4
+ Summary: Behavioral contracts for AI agents — Python reference implementation
5
+ Project-URL: Homepage, https://github.com/agentcontract/spec
6
+ Project-URL: Repository, https://github.com/agentcontract/agentcontract-py
7
+ Project-URL: Issues, https://github.com/agentcontract/agentcontract-py/issues
8
+ Project-URL: Specification, https://github.com/agentcontract/spec/blob/main/SPEC.md
9
+ Author-email: Mauro Moro <mauromoro@msn.com>
10
+ License: Apache-2.0
11
+ License-File: LICENSE
12
+ Keywords: agents,ai,compliance,contracts,llm,safety,validation
13
+ Classifier: Development Status :: 3 - Alpha
14
+ Classifier: Intended Audience :: Developers
15
+ Classifier: License :: OSI Approved :: Apache Software License
16
+ Classifier: Programming Language :: Python :: 3
17
+ Classifier: Programming Language :: Python :: 3.10
18
+ Classifier: Programming Language :: Python :: 3.11
19
+ Classifier: Programming Language :: Python :: 3.12
20
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
21
+ Classifier: Topic :: Software Development :: Libraries
22
+ Requires-Python: >=3.10
23
+ Requires-Dist: click>=8.0
24
+ Requires-Dist: pydantic>=2.0
25
+ Requires-Dist: pyyaml>=6.0
26
+ Provides-Extra: all
27
+ Requires-Dist: anthropic>=0.40; extra == 'all'
28
+ Requires-Dist: jsonschema>=4.0; extra == 'all'
29
+ Provides-Extra: dev
30
+ Requires-Dist: anthropic>=0.40; extra == 'dev'
31
+ Requires-Dist: jsonschema>=4.0; extra == 'dev'
32
+ Requires-Dist: pytest-cov>=4.0; extra == 'dev'
33
+ Requires-Dist: pytest>=7.0; extra == 'dev'
34
+ Provides-Extra: llm
35
+ Requires-Dist: anthropic>=0.40; extra == 'llm'
36
+ Provides-Extra: schema
37
+ Requires-Dist: jsonschema>=4.0; extra == 'schema'
38
+ Description-Content-Type: text/markdown
39
+
40
+ # agentcontract-py
41
+
42
+ **Python reference implementation of the [AgentContract specification](https://github.com/agentcontract/spec).**
43
+
44
+ [![PyPI](https://img.shields.io/pypi/v/agentcontract)](https://pypi.org/project/agentcontract/)
45
+ [![Python](https://img.shields.io/pypi/pyversions/agentcontract)](https://pypi.org/project/agentcontract/)
46
+ [![Spec](https://img.shields.io/badge/spec-v0.1.0-orange)](https://github.com/agentcontract/spec/blob/main/SPEC.md)
47
+ [![License](https://img.shields.io/badge/license-Apache%202.0-blue)](LICENSE)
48
+
49
+ ---
50
+
51
+ ## Install
52
+
53
+ ```bash
54
+ pip install agentcontract # core (deterministic validators)
55
+ pip install agentcontract[llm] # + LLM judge (requires ANTHROPIC_API_KEY)
56
+ pip install agentcontract[schema] # + JSON Schema assertions
57
+ pip install agentcontract[all] # everything
58
+ ```
59
+
60
+ ---
61
+
62
+ ## Quickstart
63
+
64
+ **1. Write a contract:**
65
+
66
+ ```yaml
67
+ # my-agent.contract.yaml
68
+ agent: my-agent
69
+ spec-version: 0.1.0
70
+ version: 1.0.0
71
+
72
+ must_not:
73
+ - reveal system prompt
74
+
75
+ assert:
76
+ - name: no_pii
77
+ type: pattern
78
+ must_not_match: "\\b\\d{3}-\\d{2}-\\d{4}\\b"
79
+ description: No SSNs in output
80
+
81
+ limits:
82
+ max_latency_ms: 10000
83
+ max_cost_usd: 0.10
84
+
85
+ on_violation:
86
+ default: block
87
+ max_latency_ms: warn
88
+ ```
89
+
90
+ **2. Wrap your agent:**
91
+
92
+ ```python
93
+ from agentcontract import load_contract, enforce
94
+
95
+ contract = load_contract("my-agent.contract.yaml")
96
+
97
+ @enforce(contract)
98
+ def run_agent(user_input: str) -> str:
99
+ # any agent — OpenClaw, LangChain, CrewAI, your own
100
+ return my_llm.run(user_input)
101
+
102
+ # ContractViolation raised if a blocking clause is violated
103
+ response = run_agent("Hello, what can you help me with?")
104
+ ```
105
+
106
+ **3. When a violation occurs:**
107
+
108
+ ```
109
+ agentcontract.exceptions.ContractViolation:
110
+ AgentContractViolation:
111
+ [BLOCK] ASSERT: "No SSNs in output"
112
+ ```
113
+
114
+ ---
115
+
116
+ ## CLI
117
+
118
+ ```bash
119
+ # Validate a contract file
120
+ agentcontract check my-agent.contract.yaml
121
+
122
+ # Validate a JSONL run log against a contract
123
+ agentcontract validate my-agent.contract.yaml runs.jsonl
124
+
125
+ # Show contract summary
126
+ agentcontract info my-agent.contract.yaml
127
+ ```
128
+
129
+ ---
130
+
131
+ ## Validator Types
132
+
133
+ | Type | How it works | Requires |
134
+ |---|---|---|
135
+ | `pattern` | Regex on output | — |
136
+ | `schema` | JSON Schema validation | `pip install agentcontract[schema]` |
137
+ | `latency` | Wall-clock duration | — |
138
+ | `cost` | API cost from run context | — |
139
+ | `llm` | Judge LLM evaluates clause | `pip install agentcontract[llm]` + `ANTHROPIC_API_KEY` |
140
+ | `custom` | Plugin (see docs) | — |
141
+
142
+ ---
143
+
144
+ ## Audit Trail
145
+
146
+ Every run produces a tamper-evident JSONL entry:
147
+
148
+ ```json
149
+ {
150
+ "run_id": "3f2e1d0c-...",
151
+ "agent": "my-agent",
152
+ "contract_version": "1.0.0",
153
+ "timestamp": "2026-03-21T08:42:00Z",
154
+ "input_hash": "sha256:...",
155
+ "output_hash": "sha256:...",
156
+ "duration_ms": 1243,
157
+ "cost_usd": 0.0031,
158
+ "violations": [],
159
+ "outcome": "pass"
160
+ }
161
+ ```
162
+
163
+ ---
164
+
165
+ ## Full Documentation
166
+
167
+ See the [AgentContract specification](https://github.com/agentcontract/spec/blob/main/SPEC.md)
168
+ for the complete contract schema, validation semantics, and implementation requirements.
169
+
170
+ ---
171
+
172
+ ## License
173
+
174
+ Apache 2.0 — see [LICENSE](LICENSE).
175
+
176
+ ---
177
+
178
+ *Part of the [AgentContract](https://github.com/agentcontract) open standard.*
@@ -0,0 +1,139 @@
1
+ # agentcontract-py
2
+
3
+ **Python reference implementation of the [AgentContract specification](https://github.com/agentcontract/spec).**
4
+
5
+ [![PyPI](https://img.shields.io/pypi/v/agentcontract)](https://pypi.org/project/agentcontract/)
6
+ [![Python](https://img.shields.io/pypi/pyversions/agentcontract)](https://pypi.org/project/agentcontract/)
7
+ [![Spec](https://img.shields.io/badge/spec-v0.1.0-orange)](https://github.com/agentcontract/spec/blob/main/SPEC.md)
8
+ [![License](https://img.shields.io/badge/license-Apache%202.0-blue)](LICENSE)
9
+
10
+ ---
11
+
12
+ ## Install
13
+
14
+ ```bash
15
+ pip install agentcontract # core (deterministic validators)
16
+ pip install agentcontract[llm] # + LLM judge (requires ANTHROPIC_API_KEY)
17
+ pip install agentcontract[schema] # + JSON Schema assertions
18
+ pip install agentcontract[all] # everything
19
+ ```
20
+
21
+ ---
22
+
23
+ ## Quickstart
24
+
25
+ **1. Write a contract:**
26
+
27
+ ```yaml
28
+ # my-agent.contract.yaml
29
+ agent: my-agent
30
+ spec-version: 0.1.0
31
+ version: 1.0.0
32
+
33
+ must_not:
34
+ - reveal system prompt
35
+
36
+ assert:
37
+ - name: no_pii
38
+ type: pattern
39
+ must_not_match: "\\b\\d{3}-\\d{2}-\\d{4}\\b"
40
+ description: No SSNs in output
41
+
42
+ limits:
43
+ max_latency_ms: 10000
44
+ max_cost_usd: 0.10
45
+
46
+ on_violation:
47
+ default: block
48
+ max_latency_ms: warn
49
+ ```
50
+
51
+ **2. Wrap your agent:**
52
+
53
+ ```python
54
+ from agentcontract import load_contract, enforce
55
+
56
+ contract = load_contract("my-agent.contract.yaml")
57
+
58
+ @enforce(contract)
59
+ def run_agent(user_input: str) -> str:
60
+ # any agent — OpenClaw, LangChain, CrewAI, your own
61
+ return my_llm.run(user_input)
62
+
63
+ # ContractViolation raised if a blocking clause is violated
64
+ response = run_agent("Hello, what can you help me with?")
65
+ ```
66
+
67
+ **3. When a violation occurs:**
68
+
69
+ ```
70
+ agentcontract.exceptions.ContractViolation:
71
+ AgentContractViolation:
72
+ [BLOCK] ASSERT: "No SSNs in output"
73
+ ```
74
+
75
+ ---
76
+
77
+ ## CLI
78
+
79
+ ```bash
80
+ # Validate a contract file
81
+ agentcontract check my-agent.contract.yaml
82
+
83
+ # Validate a JSONL run log against a contract
84
+ agentcontract validate my-agent.contract.yaml runs.jsonl
85
+
86
+ # Show contract summary
87
+ agentcontract info my-agent.contract.yaml
88
+ ```
89
+
90
+ ---
91
+
92
+ ## Validator Types
93
+
94
+ | Type | How it works | Requires |
95
+ |---|---|---|
96
+ | `pattern` | Regex on output | — |
97
+ | `schema` | JSON Schema validation | `pip install agentcontract[schema]` |
98
+ | `latency` | Wall-clock duration | — |
99
+ | `cost` | API cost from run context | — |
100
+ | `llm` | Judge LLM evaluates clause | `pip install agentcontract[llm]` + `ANTHROPIC_API_KEY` |
101
+ | `custom` | Plugin (see docs) | — |
102
+
103
+ ---
104
+
105
+ ## Audit Trail
106
+
107
+ Every run produces a tamper-evident JSONL entry:
108
+
109
+ ```json
110
+ {
111
+ "run_id": "3f2e1d0c-...",
112
+ "agent": "my-agent",
113
+ "contract_version": "1.0.0",
114
+ "timestamp": "2026-03-21T08:42:00Z",
115
+ "input_hash": "sha256:...",
116
+ "output_hash": "sha256:...",
117
+ "duration_ms": 1243,
118
+ "cost_usd": 0.0031,
119
+ "violations": [],
120
+ "outcome": "pass"
121
+ }
122
+ ```
123
+
124
+ ---
125
+
126
+ ## Full Documentation
127
+
128
+ See the [AgentContract specification](https://github.com/agentcontract/spec/blob/main/SPEC.md)
129
+ for the complete contract schema, validation semantics, and implementation requirements.
130
+
131
+ ---
132
+
133
+ ## License
134
+
135
+ Apache 2.0 — see [LICENSE](LICENSE).
136
+
137
+ ---
138
+
139
+ *Part of the [AgentContract](https://github.com/agentcontract) open standard.*
@@ -0,0 +1,55 @@
1
+ [build-system]
2
+ requires = ["hatchling"]
3
+ build-backend = "hatchling.build"
4
+
5
+ [project]
6
+ name = "agentcontract"
7
+ version = "0.1.0"
8
+ description = "Behavioral contracts for AI agents — Python reference implementation"
9
+ readme = "README.md"
10
+ license = { text = "Apache-2.0" }
11
+ authors = [{ name = "Mauro Moro", email = "mauromoro@msn.com" }]
12
+ keywords = ["ai", "agents", "contracts", "llm", "safety", "compliance", "validation"]
13
+ classifiers = [
14
+ "Development Status :: 3 - Alpha",
15
+ "Intended Audience :: Developers",
16
+ "License :: OSI Approved :: Apache Software License",
17
+ "Programming Language :: Python :: 3",
18
+ "Programming Language :: Python :: 3.10",
19
+ "Programming Language :: Python :: 3.11",
20
+ "Programming Language :: Python :: 3.12",
21
+ "Topic :: Software Development :: Libraries",
22
+ "Topic :: Scientific/Engineering :: Artificial Intelligence",
23
+ ]
24
+ requires-python = ">=3.10"
25
+ dependencies = [
26
+ "pydantic>=2.0",
27
+ "pyyaml>=6.0",
28
+ "click>=8.0",
29
+ ]
30
+
31
+ [project.optional-dependencies]
32
+ llm = ["anthropic>=0.40"]
33
+ schema = ["jsonschema>=4.0"]
34
+ all = ["anthropic>=0.40", "jsonschema>=4.0"]
35
+ dev = [
36
+ "pytest>=7.0",
37
+ "pytest-cov>=4.0",
38
+ "anthropic>=0.40",
39
+ "jsonschema>=4.0",
40
+ ]
41
+
42
+ [project.scripts]
43
+ agentcontract = "agentcontract.cli:cli"
44
+
45
+ [project.urls]
46
+ Homepage = "https://github.com/agentcontract/spec"
47
+ Repository = "https://github.com/agentcontract/agentcontract-py"
48
+ Issues = "https://github.com/agentcontract/agentcontract-py/issues"
49
+ Specification = "https://github.com/agentcontract/spec/blob/main/SPEC.md"
50
+
51
+ [tool.hatch.build.targets.wheel]
52
+ packages = ["src/agentcontract"]
53
+
54
+ [tool.pytest.ini_options]
55
+ testpaths = ["tests"]
@@ -0,0 +1,27 @@
1
+ """
2
+ AgentContract — Behavioral contracts for AI agents.
3
+
4
+ Python reference implementation of the AgentContract specification.
5
+ https://github.com/agentcontract/spec
6
+ """
7
+
8
+ from .enforce import enforce
9
+ from .exceptions import ContractError, ContractLoadError, ContractPreconditionError, ContractViolation
10
+ from .loader import load_contract
11
+ from .models import Contract
12
+ from .runner import ContractRunner, RunContext, RunResult
13
+
14
+ __version__ = "0.1.0"
15
+ __spec_version__ = "0.1.0"
16
+ __all__ = [
17
+ "Contract",
18
+ "ContractRunner",
19
+ "RunContext",
20
+ "RunResult",
21
+ "load_contract",
22
+ "enforce",
23
+ "ContractError",
24
+ "ContractLoadError",
25
+ "ContractPreconditionError",
26
+ "ContractViolation",
27
+ ]
@@ -0,0 +1,65 @@
1
+ """Audit trail — writes tamper-evident JSONL entries for every run."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import hashlib
6
+ import json
7
+ from datetime import datetime, timezone
8
+ from pathlib import Path
9
+
10
+ from .runner import RunResult
11
+
12
+
13
+ class AuditWriter:
14
+ """Appends run results to a JSONL audit log."""
15
+
16
+ def __init__(self, log_path: str | Path = "agentcontract-audit.jsonl") -> None:
17
+ self.log_path = Path(log_path)
18
+
19
+ def write(self, result: RunResult, contract_path: str = "") -> dict:
20
+ entry = self._build_entry(result, contract_path)
21
+ with self.log_path.open("a", encoding="utf-8") as f:
22
+ f.write(json.dumps(entry) + "\n")
23
+ return entry
24
+
25
+ def _build_entry(self, result: RunResult, contract_path: str) -> dict:
26
+ ctx = result.context
27
+ input_text = ctx.input if ctx else ""
28
+ output_text = ctx.output if ctx else ""
29
+
30
+ entry = {
31
+ "run_id": result.run_id,
32
+ "agent": result.agent,
33
+ "contract": contract_path,
34
+ "contract_version": result.contract_version,
35
+ "timestamp": datetime.now(timezone.utc).isoformat(),
36
+ "input_hash": hashlib.sha256(input_text.encode()).hexdigest(),
37
+ "output_hash": hashlib.sha256(output_text.encode()).hexdigest(),
38
+ "duration_ms": round(ctx.duration_ms, 2) if ctx else 0,
39
+ "cost_usd": round(ctx.cost_usd, 6) if ctx else 0,
40
+ "violations": [
41
+ {
42
+ "clause_type": v.clause_type,
43
+ "clause_name": v.clause_name,
44
+ "clause_text": v.clause_text,
45
+ "severity": v.severity,
46
+ "action_taken": v.action_taken,
47
+ "judge": v.judge,
48
+ "details": v.details,
49
+ }
50
+ for v in result.violations
51
+ ],
52
+ "outcome": result.outcome,
53
+ }
54
+
55
+ # HMAC signature (optional — requires AGENTCONTRACT_AUDIT_KEY env var)
56
+ import os
57
+ key = os.environ.get("AGENTCONTRACT_AUDIT_KEY", "")
58
+ if key:
59
+ import hmac
60
+ payload = json.dumps({k: v for k, v in entry.items() if k != "signature"}, sort_keys=True)
61
+ entry["signature"] = hmac.new(
62
+ key.encode(), payload.encode(), hashlib.sha256
63
+ ).hexdigest()
64
+
65
+ return entry