# Policy gate pattern

This file states the pattern `eni6ma-policy-gate-example` already runs. The subject is one submit. The gate accepts a proposal only when every check passes. The default is deny.

## Effect

The protected text is committed before the model runs. The committed text is the signed prompt bytes of each immutable region and each secured span. The model may read that text and return a proposal. The proposal may not replace those bytes. The agent and any later service accept the proposal only when those bytes still match the committed text and the action is inside the committed policy.

The same submit also carries HTTP6 profile `http6-w1-00`. The root receipt is the admission object. Its `admission` field stores `circuit_hash` and `bearings` 12, and that circuit proof stays bound to `request_hash`. The child grant `g-income` is narrower than the root: its actions are `infer` and its resource is `document-store`. `destination_apply` denies a proposal outside that set after `verify_receipt` has passed. The permit is issued before that write. It binds the operation id, the actor, the intent hash, and a deadline. A pass returns an outcome: the operation, the permit, the committed span hashes, and the effect. A reject returns a closure: the permit is closed and `effect` is null. A second submit with the same operation id and the same terms returns the first outcome and does not write again. A new record id with different terms for that operation id is a conflict.

The HTTP message signature covers method, target, content digest, profile, operation id, record id, action, resource, and purpose. That signature is not the circuit proof. The circuit proof is not that signature. TLS 1.3 is a front door in front of this listener. This listener does not implement a TLS stack.

The public vector suite `w1-public-vectors-00` is checked by the parser and the signer. Those checks cover the 46 signed objects and the 24 HTTP messages. The eleven cases still require the credit-ledger objects this example does not keep: `account`, `permit`, `outcome`, `revocation`, `closure`, and `checkpoint`. The example is not a conformant implementation of that suite.

Scope: one submit in this example. The check covers the protected spans named by the committed policy and the signed prompt, the acting grant, and the one permit for the operation id.

## Three objects

The committed input is the policy plus the prompt, including each protected span. The untrusted proposal is the JSON object the model returns. The receipt is the ledger row. The receipt stores `policy_hash`, `prompt_hash`, `protected_span_hashes`, `request_hash`, the circuit proof, and the check list. The proposal stays outside `request_hash`.

Scope: these three objects. The receipt records the committed input.

## Two sites

The agent checks the reply when the reply arrives. After `interpret_completion`, `confine_reply` keeps `executable`, `endpoint_id`, `region`, and `replacement`. It scans the raw completion for each protected span name. When the text after that name differs from the committed span, the reason is `protected_span_mismatch` and nothing is written. The raw completion is stored on the ledger row. The agent calls `verify_receipt` and calls `destination_apply` only when every check is `pass: true`. `destination_apply` calls `verify_receipt` again. It does not read `agent_action.decision`. If `executable` is absent from the committed policy, it returns without changing the document. `destination_apply` is the only writer of the document map. A destination checks the same receipt before it creates, reads, modifies, or deletes. The destination calls `POST /verify` with the receipt, the presented policy bytes, the presented prompt, and the proposal. The destination does not trust the agent's allow flag. `verify_receipt` does not read `agent_action.decision`.

`verify_receipt` recomputes `policy_hash`, `prompt_hash`, each protected-span hash, and `request_hash`. It checks the circuit proof for that `request_hash`. It runs the executable, endpoint, region, and size rules against the committed policy. A pass from `verify_receipt` is the check list with every entry `pass: true`.

Scope: the agent write and the destination create, read, modify, or delete. The agent's allow flag is outside both checks.

## Catalog

The sections below state the hashes, the policies, the resource verbs, and the stamps.

## Commit

Canonical JSON matches `eni6ma-id4-auto` `src/canon.rs`. Object keys are sorted. The separators are a comma and a colon with no extra space.

`policy_hash` is SHA-256 of the canonical policy bytes. `prompt_hash` is SHA-256 of the signed prompt bytes.

The signed body has six fields:

- `method`: `llm_apply`
- `endpoint_id`: the policy field `endpoint_id`
- `policy`: the committed policy object
- `prompt`: the signed prompt string
- `tau`: the application tau for this submit
- `nonce_uuid`: the application nonce for this submit

`request_hash` is SHA-256 of that canonical body.

The server stores the canonical policy as base64 (`policy_b64`) and stores `policy_hash` beside it. A later check decodes those bytes and rehashes them. The rehash must equal the stored `policy_hash`, and that hash must equal SHA-256 of the canonical committed policy.

Scope: the six fields above. The proposal is outside this hash.

## Proof

`validate_policy_gate(request_hash)` opens the sealed policy-gate blob, runs the auto-prover, and runs the verifier. The proof `bearings` array has length 12. The circuit check accepts when `Y = 1` and the checked proof matches that same `request_hash`.

The payload `tau` and `nonce_uuid` are the application values inside the signed body. `validate` derives the proof `tau` and the proof `nonce_uuid` from `request_hash`. A rule-check failure or an accept marks the application nonce `SPENT`. A failure of the policy hash, the prompt hash, the request hash, or the circuit check leaves that nonce `RESERVED`. The proof covers `request_hash`. The proof leaves the model reply outside the hash.

Scope: one `request_hash` for one submit. A different body produces a different hash and fails the request-hash check.

## Proposal

Grok returns one JSON object. The fields are `executable`, `endpoint_id`, `region`, `replacement`, and `policy_hash`. The model may copy the policy object into `policy`.

The gate reads `executable`, `endpoint_id`, `region`, and `replacement` from that object and compares them with the committed policy. The gate does not treat the echoed policy object or the echoed `policy_hash` as the committed policy. An echoed hash, or an echoed object whose canonical hash differs from the committed `policy_hash`, fails the policy hash check. The other checks still use the committed policy.

The proposal stays outside `request_hash`.

Scope: the fields in the returned JSON object. Text in the prompt does not add an operation.

## Gate

The gate accepts the proposal only when every check passes. Each catalog policy sets `default` to `deny`. An executable, an endpoint, or a region the committed policy does not list is refused.

The checks run in this order: policy hash, prompt hash, request hash, circuit Y, executable, endpoint, region, size, and nonce. The first failed check is the stamp reason.

A failure of policy hash, prompt hash, request hash, or circuit Y leaves the document unchanged and leaves the nonce `RESERVED`. A later rule failure leaves the document unchanged and marks the nonce `SPENT`. A pass calls `destination_apply`, which writes `replacement` into the named mutable region, and marks the nonce `SPENT`.

A proposal that targets an immutable region or a secured span in the prompt does not change those bytes. A secured span is a block marked `[[secured NAME]]` ... `[[end secured]]`, a line `secured NAME: ...`, or the region name `secured`. Examples of immutable names in the document-edit policy are `signature_page` and `clause_4`. A pass may write only a mutable target named by that same policy.

Before the model call, the agent hashes each immutable region and each secured span in the signed prompt and stores that map as `protected_span_hashes`. After the proposal returns, the agent recomputes those spans from the document. A difference fails the region check and does not write.

Scope: the committed policy and the signed prompt. The ledger records the echoed policy object and the comparison. The executable, endpoint, and region checks use the committed policy.

## Policies

The catalog has four policies. Each policy sets `default` to `deny` and `max_patch_bytes` to 256. The endpoint check requires the proposal `endpoint_id` to appear in both `upstream_endpoints` and `downstream_endpoints`.

### document-edit

`document-edit` is `fixtures/policy.json`.

| Field | Value |
| --- | --- |
| `endpoint_id` | `document-store` |
| `executables` | `apply_patch` |
| `upstream_endpoints` | `document-store` |
| `downstream_endpoints` | `document-store` |
| `mutable_regions` | `exhibit_summary` |
| `immutable_regions` | `signature_page`, `clause_4` |

A pass may replace `exhibit_summary`. A proposal whose region is `signature_page` or `clause_4` stamps `immutable_region`. Those bytes stay unchanged.

### code-read

| Field | Value |
| --- | --- |
| `endpoint_id` | `github.com` |
| `executables` | `http_get` |
| `upstream_endpoints` | `github.com` |
| `downstream_endpoints` | `github.com` |
| `mutable_regions` | `fetch_body` |
| `immutable_regions` | `src/main.rs` |

`code-read` may fetch the listed host. The mutable target is `fetch_body`. `code-read` may not write a file. `src/main.rs` is immutable. A proposal whose region is `src/main.rs` stamps `immutable_region`. That file text stays unchanged.

### code-write

| Field | Value |
| --- | --- |
| `endpoint_id` | `workspace` |
| `executables` | `apply_patch` |
| `upstream_endpoints` | `workspace` |
| `downstream_endpoints` | `workspace` |
| `mutable_regions` | `src/main.rs` |
| `immutable_regions` | empty |

`code-write` may modify the listed path `src/main.rs`. `code-write` may not call an external host. A proposal whose endpoint is `github.com` is absent from both endpoint lists and stamps `endpoint_denied`. The file bytes stay unchanged.

### income-statement

| Field | Value |
| --- | --- |
| `endpoint_id` | `document-store` |
| `executables` | `infer` |
| `upstream_endpoints` | `document-store` |
| `downstream_endpoints` | `document-store` |
| `mutable_regions` | `inference_note` |
| `immutable_regions` | `income_statement` |

The example file is a short income statement. The prompt asks for a one-line inference and also asks to change a total in the statement. The file bytes are the `[[secured income_statement]]` span inside the signed prompt, so `protected_span_hashes` covers those bytes. The expected action is `infer` at `document-store` on `inference_note`. A proposal that replaces `income_statement` stamps `immutable_region`. That span hash stays unchanged. `synthetic-statement-pass` writes only `inference_note`. `synthetic-statement-reject` targets `income_statement`.

## Resource operations

Create, read, modify, and delete are executables or endpoints in the committed policy. The catalog uses these names:

- Read: executable `http_get` and endpoint `github.com` on `code-read`.
- Modify: executable `apply_patch` and endpoint `document-store` or `workspace`.
- Create and delete: the policy includes the operation when `executables` or the endpoint lists contain that name. These three policies list neither a create executable nor a delete executable.

The model text does not add an operation. A verb or a host absent from the committed policy is refused. The document, the file text, and the fetch body stay unchanged.

Scope: names present in `executables`, `upstream_endpoints`, `downstream_endpoints`, `mutable_regions`, and `immutable_regions`.

## Ledger row

The ledger row records the committed policy, `policy_hash`, the policy object the model echoed (`model_policy`), the echoed hash (`model_policy_hash`), and `policy_hash_comparison`.

`policy_hash_comparison` is one of three values:

- `match`: the echoed hash and the echoed object, when present, rehash to the committed `policy_hash`.
- `mismatch`: an echoed hash or an echoed object differs from the committed `policy_hash`. The policy hash check fails.
- `absent`: the proposal omits both the echoed hash and the echoed object. The policy hash check uses the committed bytes only.

The row also stores `prompt_hash`, `protected_span_hashes`, `request_hash`, `expected_action`, `broken_clause`, and the check list. `protected_span_hashes` maps each protected span name to SHA-256 of its committed text. `expected_action` stores the allowed executable, the allowed endpoint, and the mutable region. `broken_clause` is `executables`, `endpoints`, `immutable_regions`, `max_patch_bytes`, or the span name. A pass leaves `broken_clause` empty. A live completion stores `raw_completion` on the same row. The row stores `application_tau` and `application_nonce` so a later caller can rebuild `request_hash` from presented policy bytes and a presented prompt. Each check is `{ "name", "pass" }`. `pass` is true for PASS and false for FAIL. The first FAIL is `agent_action.reason` and `ledger.reason`.

Scope: one row per submit. The row stores the comparison. The row does not replace the committed policy with the echoed object.

## Stamps

Each stamp names the check that failed, or the pass when every check passed, and the state the check leaves unchanged.

A hash or proof failure (the first four checks) leaves the document unchanged and leaves the nonce `RESERVED`. An executable, endpoint, region, or size failure leaves the document unchanged and marks the nonce `SPENT`. `allow` calls `destination_apply`, which writes only the mutable target, and marks the nonce `SPENT`.

### allow

Check: every check passed. `destination_apply` writes `replacement` only into a mutable region named by the committed policy. Unchanged state: the bytes of each immutable region and each secured span stay equal to the signed prompt. The nonce becomes `SPENT`.

### policy_hash_mismatch

Check: policy hash. The committed policy bytes do not rehash to `policy_hash`, or the policy hash the model echoed differs from the committed `policy_hash`. Unchanged state: the document stays unchanged. The nonce stays `RESERVED`.

### prompt_hash_mismatch

Check: prompt hash. The stored prompt hash differs from SHA-256 of the signed prompt. Unchanged state: the document stays unchanged. The nonce stays `RESERVED`.

### request_hash_mismatch

Check: request hash. The signed body hash differs from `request_hash` or from the proof request hash. Unchanged state: the document stays unchanged. The nonce stays `RESERVED`.

### proof_rejected

Check: circuit Y. The 12-bearing proof does not verify for `request_hash`. Unchanged state: the document stays unchanged. The nonce stays `RESERVED`.

### executable_denied

Check: executable. The proposal executable is absent from the committed policy `executables`. Unchanged state: the document stays unchanged. The nonce becomes `SPENT`.

### endpoint_denied

Check: endpoint. The proposal endpoint is not both an upstream endpoint and a downstream endpoint of the committed policy. Unchanged state: the document stays unchanged. The nonce becomes `SPENT`.

### immutable_region

Check: region. The proposal region is an immutable region, a secured span, or outside the mutable regions of the committed policy. A recomputed protected-span hash that differs from `protected_span_hashes` fails this check. Unchanged state: the targeted span stays unchanged, and the document stays unchanged. The nonce becomes `SPENT`.

An immutable region or a secured span stamps `immutable_region`. A region absent from `mutable_regions` and absent from the immutable and secured names stamps `region_denied`. Both results leave the document unchanged.

### patch_too_large

Check: size. The replacement length in bytes exceeds `max_patch_bytes`. Unchanged state: the document stays unchanged. The nonce becomes `SPENT`.

### nonce_spent

Check: nonce. That application nonce is already `SPENT`. Unchanged state: the document stays unchanged. The nonce stays `SPENT`. The gate does not apply the patch again.

### protected_span_mismatch

Check: the raw completion. The text after a protected span name differs from the committed span. The proposal may still name a mutable region. Unchanged state: the document stays unchanged. The nonce stays `RESERVED`. `broken_clause` is that span name. `raw_completion` stores the completion text.

### grok_unavailable

Check: the Grok call did not return a proposal. The nine checks did not run. Unchanged state: the document stays unchanged. The nonce stays `RESERVED`.

### Grok HTTP error

Check: the Grok call returned an HTTP error. The stamp is `grok_http_<status>:<code>`. When the error code is empty, the stamp uses the error type. When both are empty, the stamp is `grok_http_<status>`. The row stores `http_status`, `error_type`, and `error_code` on the model reply. The error message is not stored. The nine checks did not run. Unchanged state: the document stays unchanged. The nonce stays `RESERVED`.

## Limit

The check covers fields placed in the canonical policy and the signed prompt. Text the policy does not name is outside the check.

The canonical policy names the default, the endpoint, the executables, the upstream endpoints, the downstream endpoints, the mutable regions, the immutable regions, and `max_patch_bytes`. The signed prompt supplies the bytes of the regions it names, including `signature_page`, `clause_4`, `exhibit_summary`, `src/main.rs`, and `fetch_body` when those lines are present, and any secured reference block the prompt marks. A sentence in the prompt that names an operation does not add that operation to the policy.

A pass may write only a mutable target named by the committed policy. An executable, an endpoint, or a region the committed policy does not list is refused. Immutable and secured bytes stay unchanged on every stamp, including `allow`.
