# Permission decision and refusals

- **Owner**: `authority_tier == "owner"` and `principal_id == owner_principal_id()`. Whether someone is watching is not part of it.
- **Can approve**: `can_approve(req)`: owner, `speaker_kind == "owner"`, `interaction == "interactive"`, capability `approval.request`; or a foreground helper whose request carries `asks_through`, asked on its caller's card ([agent collaboration](https://fzkuji.github.io/OpenProgram/docs/reference/design/runtime/collaboration/agent-collaboration.html#permissions))
- **Bypass permissions**: The mode stored and sent as `bypass`; the default

## The decision path

`permission_decision(tool, req, args)` in `openprogram/runtime/permissions/policy.py` is the only function that decides whether a well-formed call runs. The first match ends it.

| Check, in order | Result |
| --- | --- |
| 1. Identity (`identity.py`, `decide`). Never fires for the owner's own turns: capability (`decide_tool_authority`), scope, page script in work a non-owner started; dangerous read paths in an always-on check-in; page scripts and destructive deletes in a handoff chain or an automatic Goal's turns, and in the sub-agents, nested programs and notice turns they start | deny: `AUTHORITY_*`, `IDENTITY_SCOPE`, `IDENTITY_PAGE_SCRIPT`, `IDENTITY_CHECK_IN_READ`, `IDENTITY_OUTSIDE_TEXT` |
| 2. Owner deny rule | deny: `RULE_DENY` |
| 3. Plan selected, tool marked `unsafe_in` plan | deny: `PLAN_MODE_DENY`, so a saved `bash(git:*)` cannot break Plan's read-only promise |
| 4. Owner ask rule | ask: `RULE_ASK` |
| 5. Agent with `hold_outbound`, sending tool | ask: `RULE_OUTBOUND_HOLD`, an implicit ask rule; see [Outbound hold](https://fzkuji.github.io/OpenProgram/docs/reference/design/runtime/security/outbound-hold.html) |
| 6. Owner allow rule | allow: `RULE_ALLOW` |
| 7. Bypass permissions | allow: `MODE_UNRESTRICTED` |
| 8. Plan, tool is `exit_plan_mode` | ask: `PLAN_EXIT` |
| 9. Call needs no decision | allow: `SURFACE_GRANT`, `SESSION_POLL`, `APP_GRANT_FREE`, `SCHEDULED_MEMORY`, `SAFE_TOOL` (reads, plans, and replies to the turn's own conversation; `send_file` is one, because attaching a file there is replying, so Auto never sends it to review; see [Chat attachments](https://fzkuji.github.io/OpenProgram/docs/reference/design/ui/chat/chat-attachments.html#approval)) |
| 10. Accept edits, safe edit inside the working directories | allow: `SAFE_EDIT` |
| 11. Auto | allow: `AUTO_APP_GRANT`, when an earlier review held the same screen; otherwise model review: `AUTO_CLASSIFY` |
| 12. Everything else | ask: `MODE_APPROVAL` |

Rules are empty by default and merge global, project and the execution branch (`permissions/rule.py`); `deny` beats `ask` beats `allow`, and source order matters only inside one behaviour.

Conversation branches share current files. Switching branches introduces no workspace-alignment permission gate; only an explicit Rewind requests file recovery.

Every ask goes through `_ask_or_nobody`: if `can_approve(req)` is false the outcome is `deny` with `NO_ONE_TO_APPROVE`, or `HELPER_CANNOT_ASK` for a helper Agent started by another. No card is published for a turn that would be refused at execution; `APPROVAL_LOCAL_OWNER_REQUIRED` remains a defensive assertion at resume.

**The Decision value**

| Field | Meaning |
| --- | --- |
| `outcome` | `allow`, `ask`, `auto` or `deny` |
| `code` | The stable reason code |
| `layer` | `identity`, `rule`, `mode`, or `setting` for a gate driven by a setting |
| `refusal` | A `Refusal`, set when the outcome is `deny` |
| `rule` | The rule text that decided, if any |
| `authority` | Structured evidence for an identity denial |

**Calls that are not well-formed, and Pages**

A malformed call never reaches the decision. `call_problem(name, args)` in `openprogram/programs/_authoring/prechecks.py` combines `guides.missing_required` (`GUIDE_REQUIRED`) and `prechecks.problem` (`INVALID_ARGUMENTS`); `permissions/approvals/approval.py` calls it first:

| Caller | On a problem |
| --- | --- |
| `_permission_preflight`, `_interaction_manifest` | Return `None`: no review, no card |
| `_gated_execute` | Ordinary tool error: `is_error=True`, no `denied` flag, no `mark_refused` |

Being in the wrapper, not the agent loop, it also covers Python-program inner tools and the MCP server.

Whether a turn may use Pages is `surface_grant_applies(name, req, args)`, a separate question. `runtime_attach.py` captures the Page snapshot when the decision for a call on the target that holds the conversation's Pages (`page_surface_call()`) is `allow` and `surface_grant_applies` holds, so a Program started under Bypass permissions still receives its Pages.

## Modes

| Stored value | Label | Behaviour |
| --- | --- | --- |
| `bypass` | Bypass permissions | Everything well-formed runs |
| `auto` | Auto | Free calls run; the rest go to model review. Review errors fail closed; 3 consecutive or 20 total blocks fall back to a card if someone can approve. |
| `ask` | Manual | Free calls run; the rest ask. App access asks on first touch and can be allowed for the conversation. |
| `plan` | Plan | As Manual, and tools marked `unsafe_in` plan are refused and hidden |
| `acceptEdits` | Accept edits | Unused option, still valid so stored conversations keep working |

## Where each kind of limit lives

| Kind | Examples | Place |
| --- | --- | --- |
| Identity | Capabilities, non-owner scope, page scripts, framework operations, cited memory | `permissions/identity.py`, `tool_authority.py`, `Refused` from the tool |
| Rules | Deny, ask and allow rules, hold outbound, hooks | `permissions/rule.py`, `runtime/events/tool_gate.py` |
| Mode | The table above | `_mode_decision` |
| Sandbox | Paths and network outside the sandbox, only when Sandbox is on | `host/sandbox/`, `programs/_host/execution.py` |
| Correctness | Argument checks, read before write, a file changed under an approval, a stale review | Tool code, returned to the model as an error or a retry |

`auto` machinery, `attended.py` (removes `ask_user_question` from unattended turns) and `state.py` are not on the decision path.

## Refusal contract

`permissions/refusal.py` is the single producer. The model receives one line:

```
[blocked by <layer>] <what> did not run: <why>. To change this: <change>.
```

- **`Refusal`**: `layer`, `code`, `what` (the call, in plain words), `why` (the reason, quoting the rule or naming the mode), `change` (where the owner changes this)
- **`Refused`**: What tool code raises; wraps a `Refusal` and is built only through this module's constructors. `_execute_bound` in `programs/_host/runtime.py`, `_gated_execute` and the agent loop turn it into the standard result, so an `AgentTool` call never raises it. A plain Python call of a tool's implementation does.
- **Result `details`**: `denied: true`, `reason_code`, `layer`, `rule`, `mode`, `outcome: "not_started"`, `execution_started: false`. The existing tool row displays the text.

| Code | Layer | `why` | `change` |
| --- | --- | --- | --- |
| `AUTHORITY_*` | identity | `<speaker> is not the owner and may not <capability>` | Channels account access, or MCP server exposed tools; in a group room, `channels accounts group-users add` with `--access full`, or `sender_access` `owner` for the account |
| `IDENTITY_SCOPE` | identity | `work started by <source> may not <action>` | The owner must start this work |
| `IDENTITY_CHECK_IN_READ` | identity | `an always-on check-in may not read this: <reason>` | Report what you need and let the owner look in a normal turn |
| `IDENTITY_PAGE_SCRIPT` | identity | `page scripts run only in work the owner started` | Use the page actions, or have the owner start this |
| `IDENTITY_OUTSIDE_TEXT` | identity | `this work came from text the owner did not write` | Say what was wanted in the report or final message, so the owner can do it |
| `IDENTITY_FRAMEWORK` | identity | `framework operations are the owner's` | The owner must start this work |
| `RULE_DENY` | rule | `your deny rule "<rule>" matches` | Permission rules in global, project or conversation |
| `HELPER_CANNOT_ASK` | rule, mode or sandbox | `this needs approval (<rule or mode>), and a sub-agent running in the background has no one to ask` | The helper continues without that step and names in its final reply what it needed; the caller does it or asks |
| `NO_ONE_TO_APPROVE` | rule, mode or sandbox | `this needs approval (<rule or mode>) and this <source> turn has no one to approve it` | Depends on what wanted the approval. Hold outbound: put the full draft in the final message or report; the owner can untick the hold on the Agent's Always-on tab. Ask rule: the owner edits or removes the named rule. Leaving the sandbox: set the mode at the bottom to Bypass permissions, or turn the Sandbox switch off. Otherwise: set the mode at the bottom to Bypass permissions, or add the named allow rule |
| `PLAN_MODE_DENY` | mode | `the conversation is in Plan mode, which only reads` | The mode selector at the bottom of the message box |
| `TOOL_UNAVAILABLE` | setting | `<why this turn cannot use it>`. Not an approval: a tool the conversation's tools array keeps for the epoch that this turn cannot use (no channel, nobody to answer, memory off). The section beside the message lists them; see the cache-prefix design | Carry on without it |
| `APPROVAL_DENIED` | mode or rule | `you declined it`, plus the note if any | Ask again, or change the mode |
| `AUTO_CLASSIFIER_*`, `AUTO_HISTORY_UNAVAILABLE` | mode | `Auto review <blocked / could not run>: <reason>` | The mode selector, or an allow rule |
| `SANDBOX_*` and sandbox policy | sandbox | `Sandbox is on and <path or network> is outside it` | The Sandbox switch for this conversation, or sandbox settings |
| `HOOK_DENIED` | hook | `a hook blocked it: <message>` | Hooks in Settings |
| `AGENT_BUDGET` | setting | `this chain has reached <limit> <generations, messages or agents>` | `Settings > Agent > <label>` (`agent.<key>`) |
| `COMPUTER_USE_REFUSED_APP` | setting | `<app> is on the refused list` | `Settings > computer_use.refused_apps` |
| `COMPUTER_USE_OWN_WINDOW` | mode | `in <mode> the Agent may not operate OpenProgram's own window, where approval cards appear` | The mode selector at the bottom of the message box |
| `COMPUTER_USE_PASSWORD_FIELD` | setting | `typing into password fields is turned off` | `Settings > computer_use.refuse_password_fields` |

**Stale state, MCP text, and the enforcing test**

| Item | Behaviour |
| --- | --- |
| Stale state | `PERMISSION_REVIEW_STALE`, `APPROVAL_FILE_CHANGED` and wait resume mismatches come back as a retry instruction through `refusal.stale(...)`, not as a block |
| MCP server | Its model-facing text, keyed by decision code, comes from `refusal.py` (`MCP_GATE_REFUSALS`), so `_trusted_approval_denial` recognises the same strings the gate produces |
| Single source test | `tests/unit/security/test_refusal_single_source.py` walks `openprogram/programs/`, `openprogram/runtime/permissions/`, `openprogram/runtime/agent/loop/` and `openprogram/extensions/mcp/server/`. It fails on a bracketed denial literal, `"denied": True` in a dict or `details=`, a `"sandbox policy:"` literal, or `raise PermissionError(` outside `refusal.py`. Each exception is on an explicit allowlist with its reason. |

## Implementation status

The decision path, `call_problem`, `identity.py`, `refusal.py`, `Refused`, the MCP table, the default mode and the selector are in place. Open:

- `_DELEGATED_SOURCES` still marks delegated risky calls for audit.
- A hook denial carries the hook's message but not its name.
