---
title: "Testing a policy"
description: "Check a policy file, simulate and explain a member's attempt, and diff a draft against the live policies, from the CLI, the API, or the web app."
---

> Documentation Index
> Fetch the complete documentation index at: https://buildrforge.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Testing a policy

A policy change applies from the next request, so test it before you save it. Forge offers four tools, each through the CLI, the API, and the MCP server:

| Tool | CLI | API | What it answers |
| --- | --- | --- | --- |
| Check | `forge policies check-policy` | `POST /orgs/:org_id/policy-checks` | Every problem in a file, and the file as Forge stores it |
| Simulate | `forge policies check-policy-simulation` | `POST /orgs/:org_id/policy-simulations` | Whether a member's attempt is allowed, under the live policies or a draft |
| Explain | `forge policies check-policy-explanation` | `POST /orgs/:org_id/policy-explanations` | Why: each check, pass, statement, and condition term |
| Diff | `forge policies check-policy-diff` | `POST /orgs/:org_id/policy-diffs` | What a draft allows or denies in a repo that the live policies don't, member by member |

Checking a file takes `managePolicies` on the org, or on the repo for a repo policy. Simulating, explaining, and diffing take `readPolicies` and `readMembers` on the org, because they show every policy that decides and name members. `forge orgs get-permissions --org "$ORG"` lists the org actions that you hold.

None of the tools saves anything. A **draft** is a policy file that you pass to simulate, explain, or diff, and they decide as saving it would. A draft with a `policy_id` is a new version of that policy: it replaces that policy alone, even when it renames it. A draft without one is a new policy, and joins the live policies beside every policy of the same name. So when a draft edits a policy, send its `policy_id`, or the old version still decides beside it.

## In the web app

The web app (which calls an org a **workspace**) shows the live policies read only, and simulates and explains attempts under them. Open **Policies** in an org's sidebar, which shows when you can read at least one of its policies:

- The list shows the org's policies, then each repo's, with who each applies to, what it allows or denies, and when it last changed.
- A policy's page shows its source. Its **History** tab lists every version with who saved it and when, and diffs any two versions line by line.
- **Simulate access** asks whether a member can take an action in a repo. You choose the member, the repo, the kind of resource and its action, the resource (such as a record that you find by its first text field), and whether the request arrives from the API or the MCP server. For a create or an update, you can add field values. **Why?** shows the explanation: each check, the org pass and the repo pass, the statements that matched and the one that decided, and the values that each condition term compared.
- **Everyone, one at a time** answers the same attempt for every member, so you can see who can take an action. Owners bypass policies, so the simulator answers them without asking.

The page's address holds the attempt, so you can share a result as a link. A repo's **Settings** tab also has a **Permissions** section that lists the repo's policies and the org policies that cover it. The web app doesn't create, change, restore, or delete policies, and it doesn't simulate drafts or org actions. Use the CLI or the MCP server for those.

## Send a policy file

Every tool takes a policy file as a JSON string in `source`, with `repo_id` for a repo policy. Keep the policy in a `.ts` file and build the request body with `jq`, then pass it on standard input with `--data-file -`:

```sh
jq -n --rawfile source invoices.ts --arg repo "$REPO" '{source: $source, repo_id: $repo}' \
  | forge policies check-policy --org "$ORG" --data-file -
```

Leave out `repo_id` for an org policy.

The examples on this page read ids from shell variables. `forge orgs list-orgs` answers the org's id for `$ORG`, `forge repos list-repos --org "$ORG"` the repo's for `$REPO`, `forge orgs list-members --org "$ORG"` a member's user id, such as `$SAM`, and `forge policies list-policies --org "$ORG"` a policy's for `$POLICY`.

## Check

`check-policy` runs the same checks that saving runs, and saves nothing. It answers each problem and, when the file has no errors, `source`: the file rendered back as Forge stores it, with names resolved.

```json
{
  "problems": [
    {
      "severity": "error",
      "code": "unknownField",
      "message": "integrations.procore.Invoice has no field submited_by.",
      "fix": "did you mean submitted_by?",
      "location": { "line": 12, "column": 9 },
      "policy": "Invoices",
      "statement": 0,
      "term": [1]
    }
  ],
  "source": null
}
```

[What the checker refuses](https://buildrforge.com/docs/permissions/writing-policies/#what-the-checker-refuses) lists every `code`. Saving a file with errors fails with `PolicyInvalid`, which carries the same problems.

## Simulate

`check-policy-simulation` decides one **attempt** by a member in a repo. The body names the repo, the member, the attempt, and optionally a draft:

```sh
jq -n --rawfile source invoices.ts --arg repo "$REPO" --arg user "$SAM" --arg invoice "$INVOICE" '{
  repo_id: $repo,
  user_id: $user,
  attempt: {kind: "model", action: "update", record_id: $invoice, fields: {status: "draft"}},
  draft: {repo_id: $repo, source: $source}
}' | forge policies check-policy-simulation --org "$ORG" --data-file -
```

```json
{ "verdict": "forbidden" }
```

The top-level `repo_id` names the repo that the attempt is in. A draft of a repo policy carries the same `repo_id`, and a draft of an org policy leaves it out: `draft: {source: $source}`. A draft that edits a policy also carries its id, `draft: {repo_id: $repo, policy_id: $policy, source: $source}`. Like a new version, it keeps its policy's level and repo, or is refused with `PolicyLevelMismatch`. An id of a policy that is deleted, or that you can't read, answers `PolicyNotFound`.

Add `channel` to decide as the request would arrive: `Api`, the default, or `Mcp`. Set `Mcp` to test a rule on `request.channel === "Mcp"`. An agent that calls the API or the CLI with its own token is `Api`, not `Mcp`; only a request through the MCP server is `Mcp`. `check-policy-explanation` takes the same field. Simulations decide only for org members over `Api` or `Mcp`, not for `extension(...)` principals or `request.channel === "Extension"`.

The verdict is one of these:

| Verdict | Meaning |
| --- | --- |
| `allowed` | The policies allow the attempt. |
| `forbidden` | The member can read the resource and lacks the action. |
| `hidden` | The member can't read the repo, the record, or another resource whose `read` action was simulated. A real request answers as not found for a hidden repo or record, omits unreadable schemas, extensions, and installations from lists, and answers `Forbidden` for direct reads of extensions, installations, or agent context. |
| `withheld` | Deciding it read something that you can't read, so the answer isn't shown. See [Withheld decisions](#withheld-decisions). |

An attempt names its resource's `kind` and an `action` from that kind's list in the [Action reference](https://buildrforge.com/docs/permissions/actions/). Every kind except `org` and `room` can be simulated:

| Kind | Attempt |
| --- | --- |
| Model, `read`, `delete`, `tag`, `untag` | `{"kind": "model", "action": "read", "record_id": "..."}` |
| Model, `update` | `{"kind": "model", "action": "update", "record_id": "...", "fields": {"amount": 500}}` |
| Model, `create` | `{"kind": "model", "action": "create", "model_api_name": "procore__invoice", "fields": {...}}` |
| Schema | `{"kind": "schema", "action": "update", "model_api_name": "procore__invoice"}` |
| Repo | `{"kind": "repo", "action": "merge"}` |
| Agent context | `{"kind": "agentInstructions", "action": "manage"}`, and the same with `"agentSkills"` |
| Extension or installation | `{"kind": "extension", "action": "publish", "extension": "integrations.procore"}`, or `"kind": "installation"` with `"action": "manage"` |
| Tool, workflow, schedule, or trigger | `{"kind": "tool", "action": "call", "extension": "integrations.procore", "name": "sync_projects"}`, or `"kind"` `"workflow"`, `"schedule"`, or `"trigger"` with `"action": "run"` |

An update's `fields` are its changes, and a create's are the new record's values. Every attempt except `repo`, `agentInstructions`, and `agentSkills` takes an optional `branch`, which is `main` when you leave it out.

A simulation answers only for what exists and what you can read. A record, model, extension, or installation that you can't read answers as missing, such as `CustomRecordNotFound`. Only a `create` of a schema or an extension, or a `manage` of an installation, may name one that doesn't exist yet. A record's `create` needs its model. A tool, workflow, schedule, or trigger must be one that its extension's version declares.

The verdict is the policies' alone. The write's formulas, references, cascades, and other records aren't checked, so a write that the simulation allows can still be refused.

## Explain

`check-policy-explanation` takes the same body as `check-policy-simulation` and answers why. It answers one of three shapes, by `_tag`:

- `Owner`: the member owns the org, so no policy decides.
- `Withheld`: deciding read something that you can't read. Nothing else shows.
- `Policies`: the `verdict` and each check that led to it.

A `Policies` explanation lists its `checks` in order, and stops at the first that denies:

| Field | What it holds |
| --- | --- |
| `step` | `repoRead` checks `read` on the repo, `recordRead` checks `read` on the record that a write meets, and `action` checks the action asked for. A denial before `action`, or of a `read` action, makes the verdict `hidden`, and any other denial makes it `forbidden`. |
| `action` | The action that the step checks. |
| `outcome` | `allow` or `deny`. |
| `org_pass`, `repo_pass` | Each pass's `outcome` and the `statements` that matched the resource, action, and member. |

Each statement has a `_tag` of `Allow` or `Deny`, names its `policy_name`, `policy_id`, `version_id`, and its `index` in the policy, and says whether it `applied`: an allow whose condition held on every run, or a deny whose condition wasn't false on some run. Its `runs` show each run of the condition:

- `side` is `before` and `after` for an update, `after` for a create, `before` for a delete, and `record` for any other action.
- `terms` lists each condition term's value, by its position `at`. The whole condition is at `[]`, its first operand at `[0]`, that operand's second at `[0, 1]`, and so on. A term that the run didn't need, such as the right side of `false && x`, is left out.
- A term's `value` has a `_tag`. `True`, `False`, and `Unknown` carry `operands`, what the term read for each operand: `{"_tag": "Value", "value": ...}`, or `{"_tag": "Missing"}` for a path through `null`. `Error` carries a `reason`: `unresolvedName`, `crossRepo`, `canReadDepth`, `canReadCycle`, `readBudget`, or `unsupportedValue`.

An `Error` behaves as unknown, so it never grants and a deny that meets it matches. Telling it apart from `Unknown` shows a broken rule rather than a missing value.

## Withheld decisions

A draft is a condition that you write, so a decision under one could answer for a value that you can't read, as often as you ask. Forge records what each decision reads: each record that a condition reads, what each `canRead` decides and reads, and the rows of each model that an `exists` scans or reaches through a reference. When you can't read one of those records, or a policy limits which rows of one of those models you read, the decision is withheld from you:

- `check-policy-simulation` answers `withheld`.
- `check-policy-explanation` answers a `Withheld` explanation, with no verdict, statement, or term.
- `check-policy-diff` leaves the change out, since even its direction says something of what it read.

The live policies decide what you can read, and the member that you simulate doesn't change it. To see a withheld decision, ask someone who can read what it reads, such as an org owner.

## Diff

`check-policy-diff` compares a draft against the live policies over a whole repo, and lists each action whose answer the draft changes, for every member who isn't an owner:

```sh
jq -n --rawfile source invoices.ts --arg repo "$REPO" \
  '{repo_id: $repo, draft: {repo_id: $repo, source: $source}}' \
  | forge policies check-policy-diff --org "$ORG" --data-file -
```

```json
{
  "data": [
    {
      "user_id": "user_...",
      "action": "read",
      "change": "allowed",
      "resource": {
        "kind": "model",
        "model_api_name": "procore__project_financials",
        "records": 1,
        "examples": ["procore__project_financials_..."]
      }
    }
  ],
  "next_cursor": null
}
```

`change` is `allowed` when the draft allows what the live policies deny, and `denied` the other way round. For each member, in user id order, `check-policy-diff` compares:

- Every action on the repo, its agent instructions, and its agent skills.
- Every schema action on each model, and every action on each extension and installation, that you can read.
- `read`, `update`, `delete`, `tag`, and `untag` on each record that you can read, on `branch`, which is `main` when you leave it out.

`check-policy-diff` has these limits:

- **It pages.** A page compares `limit` resources and records, 100 by default and 200 at most, across at most 10 members. Pass `next_cursor` back as `cursor` until it's `null`. A record change counts the records and gives up to three examples per member, action, model, and direction **of its page**, so add up a model's counts across pages.
- **Updates are compared on the record as it stands**, with `previous` and `next` both the stored record. A draft whose effect depends on a proposed value, such as `changed(...)` or a `previous` that differs from `next`, shows no update change.
- **Creates aren't compared**, and neither are tools, workflows, schedules, triggers, or rooms. Every comparison uses `request.channel === "Api"`.
- **Withheld changes aren't listed.**

An empty diff doesn't prove that an edit is still refused or allowed. Simulate the edit with its `fields`, and simulate tool calls and workflow runs, before you save.

## Save and roll back

A saved file holds exactly one policy: an `orgPolicy` without `repo_id`, or a `repoPolicy` with it. Each change appends a version:

```sh
# Create a policy
jq -n --rawfile source invoices.ts --arg repo "$REPO" '{source: $source, repo_id: $repo}' \
  | forge policies create-policy --org "$ORG" --data-file -

# Save a new version of it. Send base_version_id, the version you started from, to be protected against overwriting someone else's save; without it the save is not checked
BASE=$(forge policies get-policy --org "$ORG" --policy "$POLICY" | jq -r '.version.id')
jq -n --rawfile source invoices.ts --arg base "$BASE" '{source: $source, base_version_id: $base}' \
  | forge policies create-policy-version --org "$ORG" --policy "$POLICY" --data-file -

# List its versions, newest first, and roll back to one
forge policies list-policy-versions --org "$ORG" --policy "$POLICY"
forge policies restore-policy-version --org "$ORG" --policy "$POLICY" --version "$VERSION"

# Delete it. Restoring a version brings it back.
forge policies delete-policy --org "$ORG" --policy "$POLICY"
```

If someone else saved a version since the one you edited from, Forge saves nothing and answers 409 `PolicyVersionConflict`, which names the live version. Read the policy again, merge your change into its source, and save again with that version as `base_version_id`. A policy keeps its level: a new version of an org policy must hold an `orgPolicy`. `forge policies list-policies` and `get-policy` answer each policy with its latest version and its `source`. `list-policy-versions` answers each version with its own `source`, rendered with the org's current group, repo, and model names, so you can diff two versions line by line. A deletion's `source` is empty.

## A workflow for changes

To change a policy safely:

1. Write the draft and run `check-policy` until it reports no problems.
2. Run `check-policy-diff` on each repo that the policy covers, and read every page.
3. `check-policy-simulation` the writes, creates, and tool calls that `check-policy-diff` doesn't compare, for a member of each group that the policy names.
4. When a verdict surprises you, `check-policy-explanation` it.
5. Save the policy. If it misbehaves, `restore-policy-version` puts back the previous version from the next request.

Source: https://buildrforge.com/docs/permissions/testing/index.mdx
