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

# Explain

> What the agent sees of policy, and how it asks what a line or a VFS call would do before running it.

The agent meets policy two ways: the answer it reads when an action is
refused, and explain, which says what a line or a VFS call would do
without running it.

## What the agent reads

A refusal reads as a real terminal's. The reason travels beside it.

| Refused | The agent reads |
| - | - |
| a line or a command | `rm: Permission denied`, exit 126 |
| one path | the command's own error: `cat: /data/k: Permission denied`, exit 1 |
| a write to a read-only mount | the command's own error: `rm: cannot remove '/data/a': Read-only file system`, exit 1 |
| a VFS call | `EACCES` |
| a variable write | `KEY: permission denied`, exit 1 |
| an ask | the same as a deny, until a host approves; then the retry runs |
| a hidden path, or a link that leads to one | `No such file or directory`, as if it did not exist |

The reason is on the result's `decision`, never in stderr:

```python theme={null}
r = await ws.shell("git push --force origin main")
r.decision
# PolicyDecision(kind='deny', reason='force pushes go through an operator', policy='NoForcePush', ask_id=None)
```

The [agent tools](https://docs.mirage.strukto.ai/python/access/in-app.md#session-tools), which MCP and the
agent adapters serve, add one line to their text: `policy denied: <reason>`,
or `requires approval: <reason> (ask <id>)`. A hidden path adds nothing,
here or in explain: the agent never learns it exists.

### Which tools the agent gets

`session.tools.names()` lists only the tools the session's profile leaves
it: `shell` needs a command the allow list allows, `ls` and `grep`
those commands, `write` and `edit` a mount the session may write. MCP,
RPC and the agent adapters offer exactly those, so an agent is never
offered a tool every call of which would be refused.

## Explain

Explain asks every policy about a line or a VFS call and returns their
answer. Nothing runs, nothing is written and no ask is spent.

```python theme={null}
explain = (await ws.session("agent")).explain
line = await explain.shell("cat /data/keys/k")
call = await explain.vfs.read("/data/keys/k")
```

```typescript theme={null}
const explain = (await ws.session('agent')).explain
const line = await explain.shell('cat /data/keys/k')
const call = await explain.vfs.read('/data/keys/k')
```

`explain.vfs` has the same 23 calls as `session.vfs`. An agent can ask
too, through MCP, HTTP, RPC or the CLI:

| Entry point | A line | A VFS call |
| - | - | - |
| In-app | `session.explain.shell(line)` | `session.explain.vfs.<call>(...)` |
| [MCP](https://docs.mirage.strukto.ai/home/access/mcp.md#vfs-calls), with `?calls=all` | `shell` with `explain: true` | `vfs_<call>` with `explain: true` |
| [HTTP](https://docs.mirage.strukto.ai/api/operations/shell/run-shell-command.md) | `POST /shell?explain=true` | `POST /vfs/<call>?explain=true` |
| [RPC](https://docs.mirage.strukto.ai/home/access/rpc.md) | `shell` with `explain: true` | `vfs/<call>` with `explain: true` |
| [CLI](https://docs.mirage.strukto.ai/home/access/cli.md#run-lines-vfs-calls-and-tools) | `mirage shell -w ID -s SESSION -c LINE --explain` | `mirage workspace vfs CALL ARGS -w ID -s SESSION --explain` |

Every entry point returns the same JSON schema. Explain reads the line only, so
stdin, `cwd`, `runtime` and a background job are refused with it.

### What it evaluates

Explain stops before anything runs, evaluating only pre-execution checks:

| Explained | Evaluates | Never evaluates |
| - | - | - |
| A line | the profile's rules, `pre_execute`, `pre_command` | `pre_vfs`, every `post_` method |
| A VFS call | the profile's rules, `pre_vfs` | every `post_` method |

The `post_` methods need a result. A line's file calls reach `pre_vfs` only
when the line runs; explain a specific VFS call to see its admission result
without executing the line.

### What comes back

With a profile that denies `/data/keys/*`:

```json theme={null}
{
  "call": "read",
  "paths": ["/data/keys/k"],
  "outcome": "deny",
  "reason": "keys stay sealed",
  "source": "top",
  "decision": {"kind": "deny", "reason": "keys stay sealed", "policy": "PermissionsPolicy", "ask_id": null},
  "answers": [{"kind": "deny", "reason": "keys stay sealed", "policy": "PermissionsPolicy"}],
  "error": "EACCES"
}
```

| Field | Meaning |
| - | - |
| `outcome` | `allow`, `deny` or `ask` |
| `reason` | the deciding answer's reason, or empty |
| `source` | where the profile writes the rule (`top`, `mounts./runbook`, `commands.allow`); empty for a coded policy |
| `answers` | every policy that answered, in order; a route names its `runtime` and its `reason`, empty when it gave none |
| `decision` | the record the agent would get, or `null` |
| `error` | a VFS call only: the error it would raise (`EACCES`, `EROFS`), or empty |

A line adds `exit_code` and `stderr`, what the agent would read, and
`node`, the line as a tree of commands. Each command in the tree has its
own `outcome`, `answers`, `exit_code`, `stderr`, `runtime` and `words`,
the command line word by word as policies read it (see
[Context](https://docs.mirage.strukto.ai/home/policy/policies.md#context)). Under an `@line` runtime, every
command's `runtime` names it. A program only `@external` takes (`make`, `gcc`) names the runtime
that takes it.

### A line

`mirage shell --explain` prints the tree:

```text theme={null}
cat /data/a | wc -l && echo $(cat /data/keys/k)  [deny, exit 1: keys stay sealed]
  list: cat /data/a | wc -l && echo $(cat /data/keys/k)
    pipeline: cat /data/a | wc -l
      cat /data/a  [allow]
      wc -l  [allow]
    echo $(cat /data/keys/k)  [allow]
      substitution: cat /data/keys/k
        cat /data/keys/k  [deny, exit 1: keys stay sealed]  top
```

The line's `outcome`, `exit_code` and `stderr` are what holds the whole
line before any of it runs: a `pre_execute` answer, or a profile rule on
any command, the first such command giving the line its answer. A refusal
met only when its command runs shows on that command alone, and the line
stays `allow`: a word missing from the allow list, and a policy's
`pre_command` answer.

```text theme={null}
rm /data/a; echo done  [allow, exit 0]
  rm /data/a  [deny, exit 126]  commands.allow
  echo done  [allow]
```

### Limits

* A hidden path explains like any path no rule names.
* A policy that reads a file to decide still reads it.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.