# Implement the Agentic Constitution

The [Agentic Constitution v1.1](../constitution.md) by Adrian Dunkley is the source of the 18 article obligations. The [structured article index](../policy.json) assigns a stable rule ID to each obligation.

- [JavaScript gate](constitution-gate.mjs) evaluates a proposed action and returns `allow`, `request_approval`, `pause` or `refuse`.
- [Lean 4 model](Constitution.lean) formalises the decision order after the operator has verified and classified the inputs.

## Use the JavaScript gate

Keep the `implementation/` directory next to the existing [action gate](../action-gate.mjs). Both files are standard JavaScript modules and require no packages.

```js
import { evaluateConstitution } from "./implementation/constitution-gate.mjs";

const decision = evaluateConstitution(proposedAction, trustedChecks, new Date());
```

Supply an action with an `id` and a lowercase SHA-256 `digest` of the exact payload the executor would use. Compute that digest in your trusted application. Include the tool, destination, parameters and the identity or account that would perform the action.

The trusted application supplies the following fields.

| Field | Input |
| --- | --- |
| `actionId`, `actionDigest` | The action the evidence covers |
| `mandateId`, `mandateRevision`, `mandateExpiresAt` | Identifiers and UTC expiry from the current authorised mandate |
| `prohibited`, `stopRequested` | Booleans from the rule and stop controls |
| `authorityVerified`, `withinScope`, `rightsAssessed`, `securityChecksPassed`, `evidenceSufficient`, `resourceBudgetAvailable` | Verification results from the responsible controls |
| `articleChecks` | Entries keyed `AC-01` to `AC-18` |
| `approvalRequired` | Boolean set by the action and risk policy |
| `approval` | When required, a verified record with `approverId`, `actionId`, `actionDigest`, `mandateId`, `mandateRevision`, `expiresAt` and `verified` |

For each article, supply a `status` of `verified` or `violation` with a non-empty `evidenceRef`. Keep raw personal information out of the references. Where an article is conditional, `not_applicable` also requires a `rationale` and an evidence reference. Conditional entries are limited to AC-05, AC-06, AC-07, AC-08, AC-09, AC-13, AC-16 and AC-17. Leave uncertain checks open and the gate will pause the action.

An `allow` applies to the evaluated action and inputs. Recheck the mandate revision, action digest, stop control, budget and approval when the action is about to execute. Enforce those checks and any budget reservation at the executor so another request cannot change the authorisation between the decision and the action. Record the returned decision and article IDs with the operation’s audit record.

## Check the Lean model

With [Elan](https://lean-lang.org/install/) installed, run this in the `implementation/` directory.

```sh
lean Constitution.lean
```

The model proves that a bound prohibited action is refused, an active stop or expired mandate pauses otherwise eligible action, incomplete article evidence pauses action, and missing required approval requests review. It also proves that an allow decision requires the bound action, clear prohibition check, complete controls and an approval state that permits the action. The model names all 18 articles and proves that each needs accepted evidence before an action is allowed. A documented exception is accepted only for a conditional article. The proofs apply to the Lean decision function and its stated inputs. Apply the constitutional articles and verify the evidence in your own deployment.

In the Lean model, `actionBound` covers evidence and mandate binding, `stopClear` is true only for a verified inactive stop control, and `mandateLive` covers a valid, unexpired mandate. Set `coreVerified` only when the core controls pass and the prohibition check has an explicit result. The model adds any established article violation to the prohibition result.
