# Policy Engine (/docs/preference-management/policy-engine)

Policy Engine answers one question for your applications: may we use this person's data for this purpose? You write the rules as code. Transcend runs them against the person's stored preferences and returns a decision for each purpose.

Use Policy Engine when a stored opt-in or opt-out is not enough to decide. For example, you can deny a purpose in one region, require a recent choice, or send unclear cases to review.

## How it works [#how-it-works]

Your application calls the Permissions API on <GlossaryTerm id="sombra">Sombra</GlossaryTerm> with a user identifier and optional context, such as the user's region. Sombra loads the user's preference record from the Preference Store. It adds every configured purpose, including the ones the user never set. Sombra then runs your active policy and returns its decisions.

Policies are written in Rego, the policy language of Open Policy Agent (OPA). Policy Engine uses Rego v1. Sombra runs your policies with OPA, so your application never calls OPA directly. Transcend meters each successful Permissions API call as one decision.

## Bundles, versions, and activation [#bundles-versions-and-activation]

You ship policies as a **bundle**: a folder with an OPA `.manifest` file, `.rego` files, and optional `data.json` files.

- **Upload** creates an immutable bundle version. A new version does nothing until you activate it.
- **Activate** makes one version live. Each bundle has at most one active version.
- **Roll back** by activating an earlier version. Every uploaded version stays available.
- **Deactivate** stops serving the bundle. Calls that need it then fail.

The Permissions API reads only the bundle named `permissions`. Publish your Permissions policy under that name.

Transcend rejects an upload when the compressed bundle is larger than 5 KB or the uncompressed files total more than 50 KB. Transcend also checks every `.rego` file and rejects a bundle that does not compile.

<Callout type="info" title="Package names">
  Write packages under the root your `.manifest` declares, such as `package permissions.purposes.analytics`. Transcend moves each bundle into a private namespace for your organization when it loads. A policy can read data only from its own bundle.
</Callout>

## Policy input [#policy-input]

Sombra builds the input document. Your application supplies only `context`. The input matches a published JSON Schema, which the Transcend CLI copies into each Permissions bundle as `input.schema.json`.

<PermissionsPolicyInputFields />

Each entry in `preferences` has these fields:

<PermissionsPolicyInputFields part="preference" />

For example:

<PermissionsPolicyInputExample />

## Policy result [#policy-result]

Sombra evaluates the `purposes` package under your bundle root, such as `data.permissions.purposes`. Each child package is one purpose. Its last name segment becomes the key in the response.

Each purpose package must define a `result` object with a `decision` field:

| `decision` | Meaning                                                 |
| ---------- | ------------------------------------------------------- |
| `allow`    | Your application may use the data for this purpose.     |
| `deny`     | Your application may not use the data for this purpose. |
| `escalate` | Your policy cannot decide. Send the case for review.    |

Other fields in `result`, such as `reason_code`, pass through to the caller unchanged. A policy that evaluates the example input above can return:

```json
{
  "result": {
    "analytics": {
      "result": { "decision": "allow", "reason_code": "explicit_allow" }
    },
    "sale_of_info": {
      "result": { "decision": "escalate", "reason_code": "undecided_in_california" }
    }
  },
  "decision_id": "50602cdf-43b9-4cf7-b669-c77b2f6b1082"
}
```

Every key under `purposes` must be a purpose package with a `result`. A helper rule placed directly in the `purposes` package breaks the response. Put helpers in a separate package, such as `permissions.helpers`.

## Failure behavior [#failure-behavior]

Policy Engine fails closed. When it cannot produce a valid decision, the Permissions API returns an error instead of a guess. Treat any response other than 200 as "not permitted."

| Status | Cause                                                                                                |
| ------ | ---------------------------------------------------------------------------------------------------- |
| 400    | The request body is invalid, or the identifier is not configured or matches more than one record.    |
| 503    | No `permissions` bundle is active, or Sombra is still loading your bundle.                           |
| 503    | A purpose has no valid `result.decision`. The error says `Policy engine returned an invalid result`. |

The first request for your organization after Sombra starts can return 503 for a few seconds while your bundle loads. Retry with a short backoff. A newly activated version also takes effect after a short delay.

## Scopes [#scopes]

Policy Engine uses three scopes. Each scope includes the one above it.

<Scopes names={['viewPolicyEngineBundles', 'managePolicyEngineBundles', 'activatePolicyEngineBundles']} />

The Permissions API reads preference records, so its API key needs one of these scopes:

<Scopes names={['viewManagedConsentDatabaseAdminApi', 'manageStoredPreferences']} />

## Guides [#guides]

<FolderIndex />
