constitutional-agent 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,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Cognitive Thought Engine LLC
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,356 @@
1
+ Metadata-Version: 2.4
2
+ Name: constitutional-agent
3
+ Version: 0.1.0
4
+ Summary: WHY-layer constitutional governance for autonomous AI agents
5
+ License: MIT
6
+ Project-URL: Homepage, https://github.com/CognitiveThoughtEngine/constitutional-agent-governance
7
+ Project-URL: Documentation, https://github.com/CognitiveThoughtEngine/constitutional-agent-governance#readme
8
+ Project-URL: Repository, https://github.com/CognitiveThoughtEngine/constitutional-agent-governance
9
+ Project-URL: Issues, https://github.com/CognitiveThoughtEngine/constitutional-agent-governance/issues
10
+ Keywords: ai,agents,governance,constitutional,llm,autonomy
11
+ Classifier: Development Status :: 3 - Alpha
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: License :: OSI Approved :: MIT License
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3.11
16
+ Classifier: Programming Language :: Python :: 3.12
17
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
18
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
19
+ Requires-Python: >=3.11
20
+ Description-Content-Type: text/markdown
21
+ License-File: LICENSE
22
+ Requires-Dist: pydantic<3,>=2.6
23
+ Requires-Dist: pyyaml<7,>=6.0
24
+ Provides-Extra: dev
25
+ Requires-Dist: pytest>=8.0; extra == "dev"
26
+ Requires-Dist: pytest-cov>=4.0; extra == "dev"
27
+ Requires-Dist: mypy>=1.8; extra == "dev"
28
+ Requires-Dist: ruff>=0.3; extra == "dev"
29
+ Dynamic: license-file
30
+
31
+ # constitutional-agent
32
+
33
+ [![Tests](https://github.com/CognitiveThoughtEngine/constitutional-agent-governance/actions/workflows/tests.yml/badge.svg)](https://github.com/CognitiveThoughtEngine/constitutional-agent-governance/actions/workflows/tests.yml)
34
+ [![PyPI](https://img.shields.io/pypi/v/constitutional-agent)](https://pypi.org/project/constitutional-agent/)
35
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)
36
+ [![Python 3.11+](https://img.shields.io/badge/python-3.11+-blue.svg)](https://www.python.org/downloads/)
37
+
38
+ **The governance layer your AI agent is missing.**
39
+
40
+ ```bash
41
+ pip install constitutional-agent
42
+ ```
43
+
44
+ ---
45
+
46
+ ## The Problem: WHO and HOW Are Solved. WHY Is Not.
47
+
48
+ AI agent governance has three structurally distinct layers. Most organizations have the first two. Almost none have the third.
49
+
50
+ | Tier | Question | Tools | What It Misses |
51
+ |------|----------|-------|----------------|
52
+ | **WHO** | Is this agent authorized to act? | Microsoft Entra Agent ID, Okta, AWS IAM, Glasswing | An authorized agent making harmful, irrational, or unconstitutional decisions |
53
+ | **HOW** | Is this action permitted by policy? | Microsoft AGT, NeMo Guardrails, LangChain, OWASP Agentic AI | Structurally bad decisions that are technically policy-compliant; scenarios no policy covers |
54
+ | **WHY** | Does this decision align with our constitutional principles? | **This library** | — |
55
+
56
+ WHO governance gets the agent through the door. HOW governance enforces the rules written by administrators. Neither asks whether the agent's decision is *right* — aligned with the organization's mission, economic survival, and constitutional values. That's the WHY layer. It has been missing from every open-source governance toolkit until now.
57
+
58
+ ---
59
+
60
+ ## Case Study: Four Failures Constitutional Governance Would Have Caught
61
+
62
+ On April 7, 2026, a developer published one of the most honest accounts of autonomous agent failure on the internet: [My AI agent finally made money. It took 200 runs and 41 days.](https://dev.to/agenthustler/my-ai-agent-finally-made-money-it-took-200-runs-and-41-days-36jk)
63
+
64
+ $6.74 earned. 200 runs. 41 days. Four failure modes that constitutional governance would have caught.
65
+
66
+ | Failure | Duration | Constitutional Gate | Caught By |
67
+ |---------|---------|-------------------|-----------|
68
+ | Broken Lightning wallet — accepted payments, never settled | Weeks | HC-11 + EpistemicGate | `hours_since_settlement_confirmation > 24` → STOP |
69
+ | Mispriced Lightning actors at $0.00005 (wrong by orders of magnitude) | ~30 runs | EpistemicGate | `assumption_volatility` high — external verification required before execution |
70
+ | Shadow-banned by HN, kept posting for 30 runs | 30+ runs | RiskGate | `channel_health = 0%` → FAIL — stop spending on dead channels |
71
+ | Strategy locked on MCP servers for 30 runs, zero conversion | 30+ runs | ConstitutionalGate | `lessons_learned_weekly = 0` → FAIL — document what you learned or stop |
72
+
73
+ Constitutional governance doesn't guarantee faster revenue. It guarantees you don't spend 30 runs posting into a shadow-banned account after week 2.
74
+
75
+ **Full audit:** [examples/agenthustler_audit.md](examples/agenthustler_audit.md)
76
+
77
+ ---
78
+
79
+ ## Quick Start
80
+
81
+ ```python
82
+ from constitutional_agent import Constitution
83
+
84
+ constitution = Constitution.from_defaults()
85
+
86
+ result = constitution.evaluate({
87
+ # Hard constraint context
88
+ "failing_tests": 0,
89
+ "hours_since_last_execution": 4,
90
+ "gate_override_without_amendment": False,
91
+
92
+ # Epistemic: is the agent's reasoning sound?
93
+ "verification_pass_rate": 0.85,
94
+ "uncertainty_disclosure_rate": 0.90,
95
+
96
+ # Risk: are outbound actions safe?
97
+ "channel_health": 0.92,
98
+ "security_critical_events": 0,
99
+
100
+ # Economic: is the business healthy?
101
+ "stage": "pre_revenue",
102
+ "runway_months": 8.5,
103
+ "user_return_rate": 0.22,
104
+
105
+ # Constitutional: is the agent learning?
106
+ "lessons_learned_weekly": 3,
107
+ "amendments_per_month": 2,
108
+ })
109
+
110
+ if result.system_state.value == "FREEZE":
111
+ print(f"BLOCKED: {result.blocking_gate.reason}")
112
+ elif result.system_state.value == "THROTTLE":
113
+ print(f"THROTTLE: {[g.gate for g in result.hold_gates]}")
114
+ else:
115
+ print(f"State: {result.system_state.value}") # RUN or COMPOUND
116
+ ```
117
+
118
+ ---
119
+
120
+ ## Core Concepts
121
+
122
+ ### Gates
123
+
124
+ Gates are pre-execution constitutional checks. They evaluate every decision against first principles — not a policy lookup table. When no policy covers a scenario, a policy system passes it. When no policy covers a scenario, a gate evaluates it against constitutional intent and decides.
125
+
126
+ **Gate states:**
127
+ - `PASS` — Decision is constitutionally sound. Proceed.
128
+ - `HOLD` — Conditions are marginal. **THROTTLE** — conserve resources, skip discretionary actions.
129
+ - `FAIL` — Conditions are violated. **FREEZE** — stop all discretionary spend until resolved.
130
+
131
+ **System states (composite from all gate results):**
132
+ - `COMPOUND` — All gates PASS + all stretch targets met. Maximum growth mode.
133
+ - `RUN` — All gates PASS. Normal autonomous operation.
134
+ - `THROTTLE` — Any gate HOLD. Conserve resources.
135
+ - `FREEZE` — Any gate FAIL. Stop all discretionary spend.
136
+ - `STOP` — Hard constraint violated. Human intervention required immediately.
137
+
138
+ ### Hard Constraints
139
+
140
+ Hard constraints are absolute prohibitions. Unlike gates (which can be amended through a governance process), hard constraints **cannot be overridden by any agent action, amendment, or human instruction** — only by the highest authority (CEO/board) through a formal ratification process.
141
+
142
+ Hard constraint violations short-circuit to `STOP` state — not FREEZE. The difference: FREEZE is a recoverable system state. STOP requires a human to acknowledge and clear the violation before any execution resumes.
143
+
144
+ **Built-in hard constraints:**
145
+
146
+ | ID | Prohibition |
147
+ |----|-------------|
148
+ | HC-1 | No deploy or promotion when automated tests fail |
149
+ | HC-2 | No spend exceeding approved budget without human authorization |
150
+ | HC-3 | Runway must never drop below the hard survival floor |
151
+ | HC-4 | No fabricated or estimated data presented as measured fact |
152
+ | HC-5 | No irreversible action without explicit confirmation |
153
+ | HC-6 | No SQL built by string concatenation with user input |
154
+ | HC-7 | No timing-unsafe secret comparisons |
155
+ | HC-8 | No unauthenticated email sender domains |
156
+ | HC-9 | No false time claims in user-facing communications |
157
+ | HC-10 | No bare exception handlers in governance or safety code |
158
+ | HC-11 | No agent outage exceeding 24 hours without human notification |
159
+ | HC-12 | No manual override of constitutional gates without ratified amendment |
160
+
161
+ ### Amendments
162
+
163
+ Constitutional governance is not static. Rules must evolve as context changes. The amendment process enables formal evolution without losing foundational constraints.
164
+
165
+ **Key properties:**
166
+ - Agents can **propose** amendments — they cannot **ratify** them
167
+ - Ratification requires the designated authority (not the proposing agent)
168
+ - Hard constraint (HC-*) amendments require the highest authority
169
+ - All amendments are versioned and logged
170
+
171
+ ```python
172
+ # Propose (agent can do this)
173
+ amendment_id = constitution.propose_amendment(
174
+ description="Reduce EpistemicGate hold threshold from 0.70 to 0.65",
175
+ rationale="External verification latency increased. 0.65 still provides adequate safety.",
176
+ affected_sections=["EpistemicGate"],
177
+ proposed_by="my_agent_v2",
178
+ )
179
+
180
+ # Ratify (requires designated human authority — not the proposing agent)
181
+ constitution.ratify_amendment(
182
+ amendment_id=amendment_id,
183
+ ratified_by="cto@yourorg.com",
184
+ evidence={"latency_data": "p99 verification latency: 4.2s"}
185
+ )
186
+ ```
187
+
188
+ ---
189
+
190
+ ## The Six Gates
191
+
192
+ | Gate | Prevents | Key Metrics | Example Failure Without It |
193
+ |------|----------|-------------|---------------------------|
194
+ | **EpistemicGate** | False certainty | `verification_pass_rate`, `uncertainty_disclosure_rate`, `assumption_volatility` | Agent acts on unverified cost assumption. All downstream economics are wrong for 30 cycles |
195
+ | **RiskGate** | Trust damage | `misuse_risk_index`, `channel_health`, `irreversibility_score` | Agent posts to shadow-banned channel for 30 runs. Zero visibility. Full spend wasted |
196
+ | **GovernanceGate** | Metric gaming | `control_bypass_attempts`, `audit_coverage`, `metric_anomaly_score` | Agent optimizes audit metric without improving actual audit coverage. Governance is theater |
197
+ | **EconomicGate** | Financial ruin | `runway_months`, `gross_margin`, `cac`, `user_return_rate` | Agent burns 4 months of runway on a campaign. No runway gate. CEO discovers afterward |
198
+ | **AutonomyGate** | Human dependency | `human_minutes_per_day`, `decisions_per_day`, `agent_activation_rate` | "Autonomous" agent requires CEO approval for 70% of decisions. 3 hours of human time daily |
199
+ | **ConstitutionalGate** | Stagnation | `lessons_learned_weekly`, `amendments_per_month`, `bug_recurrence_rate` | Agent repeats same failed strategy 30 times. Zero lessons documented. Zero strategy change |
200
+
201
+ ### Gate Details
202
+
203
+ **EpistemicGate** — Prevents false certainty
204
+
205
+ Evaluates whether the agent has earned confidence in its reasoning. An agent that acts on self-generated beliefs without external verification, never discloses uncertainty, or ignores disagreement signals is epistemically unsound. The EG gate enforces reasoning quality before execution.
206
+
207
+ ```python
208
+ from constitutional_agent import EpistemicGate
209
+
210
+ gate = EpistemicGate()
211
+ result = gate.evaluate({
212
+ "verification_pass_rate": 0.45, # FAIL — below 0.50
213
+ "uncertainty_disclosure_rate": 0.90,
214
+ "assumption_volatility": 0.10,
215
+ "disagreement_persistence": 0.05,
216
+ })
217
+ # GateResult(gate="EpistemicGate", state=FAIL,
218
+ # reason="Low external verification rate (0.45 < 0.50)...")
219
+ ```
220
+
221
+ **RiskGate** — Prevents trust damage
222
+
223
+ Evaluates the safety of outbound actions. Critically: it monitors `channel_health` — the fraction of actions on a given channel that produce the expected outcome. An agent posting to a shadow-banned platform has 0% channel health. The RiskGate blocks further spend on dead channels.
224
+
225
+ **GovernanceGate** — Prevents gaming
226
+
227
+ Detects when an agent is optimizing for governance metrics rather than underlying outcomes. Zero tolerance for control bypass attempts. High bar for audit coverage (95%) — gaps in logging hide problems.
228
+
229
+ **EconomicGate** — Prevents financial ruin
230
+
231
+ The only open-source governance gate that evaluates financial sustainability. Two modes: `pre_revenue` (value creation metrics: return rate, completion rate, runway) and `post_revenue` (unit economics: margin, CAC, churn, LTV:CAC). Runway floor is enforced in both modes.
232
+
233
+ **AutonomyGate** — Ensures Level 4+ operation
234
+
235
+ Measures whether agents are actually deciding and executing independently. Flags both extremes: agents that require too much human input (not autonomous) and agents that never escalate when they should. The target is minimum viable escalation rate.
236
+
237
+ **ConstitutionalGate** — Ensures self-improvement
238
+
239
+ A governance system that never changes is brittle. An agent that repeats failures without learning is not improving. This gate enforces that the constitutional system is alive: lessons are being extracted, amendments are being ratified, and the agent's knowledge base is staying fresh.
240
+
241
+ ---
242
+
243
+ ## Hard Constraints vs. Policies
244
+
245
+ This distinction matters more than any other architectural decision in governance.
246
+
247
+ | | Policies (HOW layer) | Hard Constraints (WHY layer) |
248
+ |--|---------------------|------------------------------|
249
+ | **Defined by** | Administrators in YAML/OPA/Cedar | Constitutional law in code |
250
+ | **Coverage** | Scenarios explicitly written | All scenarios (evaluated against intent) |
251
+ | **Override** | Possible by updating policy file | Impossible by any agent action |
252
+ | **Gap surface** | Every unwritten scenario is ungoverned | Constitutional intent covers novel scenarios |
253
+ | **Amendment** | Change the YAML | Formal ratification by highest authority |
254
+ | **Failure mode** | "No policy for this" → passes | "Check errored" → treated as violated (fail-CLOSED) |
255
+
256
+ ```python
257
+ # HOW layer (policy enforcement — external):
258
+ if action in blocked_actions:
259
+ raise PolicyViolation("blocked by policy")
260
+ # Novel scenario: no entry in blocked_actions → passes ungoverned
261
+
262
+ # WHY layer (constitutional enforcement — embedded):
263
+ result = epistemic_gate.evaluate(action_context)
264
+ if result.state == GateState.FAIL:
265
+ raise ConstitutionalViolation(result.reason)
266
+ # Novel scenario: evaluated against epistemic soundness principles → gate decides
267
+ ```
268
+
269
+ ---
270
+
271
+ ## Installation
272
+
273
+ ```bash
274
+ pip install constitutional-agent
275
+ ```
276
+
277
+ **Requirements:** Python 3.11+, pydantic >= 2.6, pyyaml >= 6.0
278
+
279
+ **From source:**
280
+ ```bash
281
+ git clone https://github.com/CognitiveThoughtEngine/constitutional-agent-governance
282
+ cd constitutional-agent-governance
283
+ pip install -e ".[dev]"
284
+ ```
285
+
286
+ ---
287
+
288
+ ## Configuration
289
+
290
+ Load from a `governance.yaml` file:
291
+
292
+ ```python
293
+ constitution = Constitution.load("governance.yaml")
294
+ ```
295
+
296
+ Or use production-validated defaults:
297
+
298
+ ```python
299
+ constitution = Constitution.from_defaults()
300
+ ```
301
+
302
+ See [governance.yaml](governance.yaml) for the full schema with all configurable thresholds. See [examples/governance.yaml](examples/governance.yaml) for an annotated example with a content-publishing agent.
303
+
304
+ ---
305
+
306
+ ## Links
307
+
308
+ - **35-Check Governance Checklist:** [checklist/CONSTITUTIONAL_GOVERNANCE_CHECKLIST.md](checklist/CONSTITUTIONAL_GOVERNANCE_CHECKLIST.md)
309
+ - **agenthustler case study audit:** [examples/agenthustler_audit.md](examples/agenthustler_audit.md)
310
+ - **Working example:** [examples/basic_agent.py](examples/basic_agent.py)
311
+ - **Full configuration schema:** [governance.yaml](governance.yaml)
312
+ - **GitHub:** [github.com/CognitiveThoughtEngine/constitutional-agent-governance](https://github.com/CognitiveThoughtEngine/constitutional-agent-governance)
313
+ - **Constitutional Governance Review** (2-hour assessment, written report): [cteinvest.com](https://cteinvest.com)
314
+
315
+ **Research preprints (DOI):**
316
+ - DLI Framework: [10.5281/zenodo.18217577](https://doi.org/10.5281/zenodo.18217577)
317
+ - Harness Design: [10.5281/zenodo.19343034](https://doi.org/10.5281/zenodo.19343034)
318
+ - Community Security Governance: [10.5281/zenodo.19343108](https://doi.org/10.5281/zenodo.19343108)
319
+
320
+ ---
321
+
322
+ ## The Reference Implementation
323
+
324
+ This library is extracted from the HRAO-E Constitutional Framework, production-validated over 95 days:
325
+
326
+ - **52 agents** operating under constitutional governance per cycle
327
+ - **64 constitutional amendments** ratified through formal process
328
+ - **1,808 test functions**, 0 failed
329
+ - **17 hard constraints** enforced programmatically (not in policy files)
330
+ - **Real economic pressure**: $720/month burn, 10.1-month runway, $0 MRR
331
+
332
+ The framework has been cited in NIST submissions (800-2, Agent Identity) and acknowledged by CAISI. Five preprints published on Zenodo.
333
+
334
+ ---
335
+
336
+ ## Contributing
337
+
338
+ Constitutional governance improves through formal amendment — not unilateral change. The same principle applies here.
339
+
340
+ Submit a PR with:
341
+ 1. What you're changing and why
342
+ 2. Which gate or constraint is affected
343
+ 3. Evidence that the threshold change improves constitutional soundness
344
+
345
+ Hard constraint changes require a comment from a maintainer before merge. Gate threshold changes require evidence (test results, production data, or cited research).
346
+
347
+ ---
348
+
349
+ ## License
350
+
351
+ MIT — fork it, adapt it, cite it.
352
+
353
+ ---
354
+
355
+ *Constitutional governance is the WHY layer.*
356
+ *WHO = identity. HOW = behavior. WHY = values that survive any execution.*