# Domain model

How this package's terms fit together, from the smallest units to the decision a tool call ends in.
The [glossary](glossary.md) defines each term; this page defines nothing of its own and links every term to its entry.

## The questions each term answers

| Question                                    | Term                                                                                                       |
| ------------------------------------------- | ---------------------------------------------------------------------------------------------------------- |
| Which accesses does this rule apply to?     | [rule key](glossary.md#rule-key): a [surface](glossary.md#surface) and a [pattern](glossary.md#pattern)    |
| What does this rule say?                    | [action](glossary.md#action)                                                                               |
| Where did this rule come from?              | [rule origin](glossary.md#rule-origin), naming a [policy source](glossary.md#policy-source)                |
| Which rules are in effect right now?        | [composed policy](glossary.md#composed-policy)                                                             |
| What is one check about?                    | [access intent](glossary.md#access-intent), matched by its [candidate values](glossary.md#candidate-value) |
| Which rule did a check land on?             | [matched rule](glossary.md#matched-rule), and its [resolved action](glossary.md#resolved-action)           |
| How do several results become one?          | [combining rule](glossary.md#combining-rule)                                                               |
| What did one gate ask about this tool call? | [request](glossary.md#request)                                                                             |
| Who decided the request?                    | [decider](glossary.md#decider)                                                                             |
| What was the final outcome?                 | [decision](glossary.md#decision)                                                                           |

## Fundamental units

**A rule** pairs a [rule key](glossary.md#rule-key) with an [action](glossary.md#action):

```text
bash   git *   →   allow
│      │            └─ action
│      └─ pattern
└─ surface
```

**A policy source** is where rules come from:

- a [policy file](glossary.md#policy-file), which fills a [scope](glossary.md#scope): a `config.json`, a `config.local.json`, or an agent definition's frontmatter;
- the session, whose grants make [session rules](glossary.md#session-rule);
- this package, which supplies the default and baseline policies.

**An access intent** is what one check is about: one path, one bash [command unit](glossary.md#command-unit), or a tool's input.

**A decider** is what decided a request: a rule, a link, the human, auto-approve, another session, or nobody (see [decider](glossary.md#decider)).

## How they compose

### From rules to the composed policy

A [policy](glossary.md#policy) is one policy source's rules.
The policies are stacked in [layer](glossary.md#layer) order, and the stack is the [composed policy](glossary.md#composed-policy) for one agent at one moment:

```text
composed policy  (agent: reviewer, now)
├─ default layer    default policy        * * → ask
├─ baseline layer   baseline policy       (MCP metadata allows, when any mcp allow exists)
├─ config layer     global policy         ~/.pi/agent/extensions/pi-permission-system/config.json
│                   global-local policy   ~/.pi/agent/extensions/pi-permission-system/config.local.json
│                   project policy        <cwd>/.pi/extensions/pi-permission-system/config.json
│                   project-local policy  <cwd>/.pi/extensions/pi-permission-system/config.local.json
│                   agent policy          ~/.pi/agent/agents/reviewer.md
│                   project-agent policy  <cwd>/.pi/agents/reviewer.md
└─ session layer    session policy        this session's grants
```

A [floor](glossary.md#fail-closed-floor) rewrites the composed policy in place: while it applies, every `allow` outside the session layer reads `ask`.

### From a tool call to a decision

```text
tool call                 one model invocation (Pi's tool_call)
└─ gate ×6                skill read, path, external directory, bash external directory, bash path, tool
   └─ request             raised when the gate's checks leave something to decide or record; carries a requestId
      ├─ check ×1..n      one access intent against the composed policy
      │  └─ candidate value ×1..n   the access intent's spellings
      │     → matched rule → resolved action
      ├─ combined into the request's resolved action
      └─ allow or deny: decided by the matched rule
         ask: escalated to the chain, decided by a link, the terminal, or nobody
         → decision, with its decider
```

### Combining rules

Each level of that tree combines its results by one [combining rule](glossary.md#combining-rule):

| Level                                      | Combines                                                                 | Rule                                                                |
| ------------------------------------------ | ------------------------------------------------------------------------ | ------------------------------------------------------------------- |
| a check                                    | the rules matching any candidate value                                   | [last-match-wins](glossary.md#last-match-wins)                      |
| a surface family                           | a path's `_read` and `_write` checks                                     | [most-restrictive-wins](glossary.md#most-restrictive-wins)          |
| a bash chain                               | its [command units](glossary.md#command-unit)                            | most-restrictive-wins                                               |
| a request                                  | its checks                                                               | most-restrictive-wins                                               |
| a [wrapper unit](glossary.md#wrapper-unit) | its own `allow` and its [executed command](glossary.md#executed-command) | floored to `ask`, kept, or replaced by the executed command's check |

## A worked example

Two policy files and one session grant:

```jsonc
// ~/.pi/agent/extensions/pi-permission-system/config.json            (scope: global)
{ "permission": { "bash": { "*": "ask", "git *": "allow" } } }

// <cwd>/.pi/extensions/pi-permission-system/config.local.json         (scope: project-local)
{ "permission": { "bash": { "git push *": "deny" } } }

// this session: the human answered "for this session" on `npm test`, which records `npm test*`
// this session: /permission-system:auto-approve on
```

The composed policy, in order:

| #   | Rule                       | Layer   | Rule origin reported            |
| --- | -------------------------- | ------- | ------------------------------- |
| 1   | `* *` → `ask`              | default | `builtin` (no scope sets `"*"`) |
| 2   | `bash *` → `ask`           | config  | `global`                        |
| 3   | `bash git *` → `allow`     | config  | `global`                        |
| 4   | `bash git push *` → `deny` | config  | `project-local`                 |
| 5   | `bash npm test*` → `allow` | session | `session`                       |

Four commands, each decided at the tool gate by one check:

| Command           | Matched rule        | Resolved action | Decider                               | Decision |
| ----------------- | ------------------- | --------------- | ------------------------------------- | -------- |
| `git status`      | #3, `global`        | `allow`         | the rule                              | allow    |
| `git push origin` | #4, `project-local` | `deny`          | the rule                              | deny     |
| `npm test`        | #5, `session`       | `allow`         | the session rule                      | allow    |
| `rm build`        | #2, `global`        | `ask`           | auto-approve, after every link defers | allow    |

For `rm build`, the `permissions:decision` event reports `origin: "global"`, the asking rule's rule origin, and the review log names auto-approve as `decidedBy`.

### With a floor

If the project's `config.json` is present but invalid, the [fail-closed floor](glossary.md#fail-closed-floor) applies.
Rule #3 becomes `bash git *` → `ask` and its rule origin becomes `fail-closed`, so `git status` now asks and the record no longer says the rule came from the global policy.
Rule #4 is a `deny` and rule #5 is in the session layer, so `git push origin` is still denied and `npm test` still allowed.

### A bash chain

`git status && echo $(rm -rf build)` is one request at the tool gate with three command units, one check each:

| Command unit           | Kind                                   | Matched rule | Resolved action |
| ---------------------- | -------------------------------------- | ------------ | --------------- |
| `git status`           | plain                                  | #3           | `allow`         |
| `echo $(rm -rf build)` | plain, enclosing a substitution        | #2           | `ask`           |
| `rm -rf build`         | [nested unit](glossary.md#nested-unit) | #2           | `ask`           |

Most-restrictive-wins makes the request `ask`, reported under the first asking unit, `echo $(rm -rf build)`; auto-approve then allows it.

`time git status` is a wrapper unit, but its own check lands on #2 and resolves `ask`, so the wrapper's rule does not apply: that rule acts only on an `allow`.
Had a rule allowed `time *`, a check of the executed command, `git status`, would have replaced the wrapper's result, because `time` only changes how its command runs.

## Comparison with other systems

The same ideas appear in other authorization systems under different names, and two of their words mean something else here.

| This package                      | AWS IAM                               | Cedar                                              | XACML 3.0                                     |
| --------------------------------- | ------------------------------------- | -------------------------------------------------- | --------------------------------------------- |
| rule                              | statement                             | policy (one `permit` or `forbid`)                  | rule                                          |
| policy (one source's rules)       | policy, a JSON document of statements | —                                                  | policy                                        |
| composed policy                   | the policies that apply to a request  | the set of policies a request is evaluated against | policy set                                    |
| action (`allow` / `ask` / `deny`) | `Effect` (`Allow` / `Deny`)           | effect (`permit` / `forbid`)                       | `Effect` (`Permit` / `Deny`)                  |
| combining rule                    | an explicit `Deny` trumps an `Allow`  | a `forbid` overrides a `permit`                    | rule- and policy-combining algorithms         |
| gate                              | —                                     | —                                                  | policy enforcement point (PEP)                |
| check                             | policy evaluation                     | authorization                                      | evaluation at the policy decision point (PDP) |

Two collisions to keep in mind:

- IAM's `Action` element names the operation a statement covers, such as `s3:GetObject`, which is closer to our rule key than to our [action](glossary.md#action).
- IAM's `Effect`, Cedar's effect, and XACML's `Effect` are `Allow`/`Deny`, which is our action; our [effect](glossary.md#effect) is a file access's direction, `read` or `write`.

Open Policy Agent splits what reaches a decision differently: its policies are Rego rules, and the data they read, loaded from outside, is its base documents.
Our `commandEffects` and `shellTools` play that second part, describing commands rather than ruling on them.

Sources:

- AWS IAM: [IAM JSON policy element reference](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements.html), [Policies and permissions](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies.html), [The difference between explicit and implicit denies](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_evaluation-logic_AccessPolicyLanguage_Interplay.html).
- Cedar: [Basic policy construction](https://docs.cedarpolicy.com/policies/syntax-policy.html), [Authorization](https://docs.cedarpolicy.com/auth/authorization.html).
- XACML: [XACML Version 3.0 core specification](https://docs.oasis-open.org/xacml/3.0/xacml-3.0-core-spec-os-en.html), §2.3 (combining algorithms) and §3.3.1.2 (effect).
- Open Policy Agent: [Philosophy](https://www.openpolicyagent.org/docs/philosophy).

## Not yet settled

Some terms are still open, among them channel, runtime knob, and whether "ruleset" stays; the glossary lists them under [Open questions](glossary.md#open-questions).
