> ## 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.

# Policies

> Mirage provides a powerful, scriptable policy layer that controls every command, CLI and POSIX call in the virtual terminal, and can even choose the underlying runtime.

A policy is a Python or TypeScript class that controls what the agent
does. Before an action runs, and again once it returns, the policy gets
the full context: the line, each command with its expanded arguments, the
paths it touches, the session, and what came back. It then lets the
action through (`None`), refuses it, asks a host, caps its output, or
chooses the underlying runtime.

```python Python theme={null}
from mirage import RAMVFS, Workspace
from mirage.policy import CommandContext, Deny, Policy

class NoForcePush(Policy):
    async def pre_command(self, ctx: CommandContext) -> Deny | None:
        texts = [w.text for w in ctx.words]
        if texts[:2] == ["git", "push"] and "--force" in texts:
            return Deny("force pushes go through an operator")
        return None

no_force_push = NoForcePush()
ws = Workspace({"/": RAMVFS()}, policies=[no_force_push])

r = await ws.shell("git push --force origin main")
# b'git: Permission denied\n' 126
print(r.stderr, r.exit_code)

# ws.policies.add(no_force_push) puts it back
ws.policies.remove(no_force_push)
```

```typescript TypeScript theme={null}
import { RAMVFS, Workspace, type Policy } from '@struktoai/mirage-node'

const noForcePush: Policy = {
  preCommand(ctx) {
    const texts = (ctx.words ?? []).map((w) => w.text)
    const force = texts[0] === 'git' && texts[1] === 'push' && texts.includes('--force')
    return force ? { kind: 'deny', reason: 'force pushes go through an operator' } : null
  },
}

const ws = new Workspace({ '/': new RAMVFS() }, { policies: [noForcePush] })

const r = await ws.shell('git push --force origin main')
// git: Permission denied 126
console.log(r.stderrText, r.exitCode)

// ws.policies.add(noForcePush) puts it back
ws.policies.remove(noForcePush)
```

## Lifecycle

A policy implements any of eight methods, in pairs. The `pre_` method runs
before an action, while the `post_` method runs after it with the same context
and the execution result. A `post_` hook cannot undo executed actions. It can
inspect results and return `Deny` or `Ask`; only `post_execute` can also return
`Limit` to cap output. These responses do not replace returned output or
suppress existing error text.

```mermaid theme={null}
flowchart LR
    subgraph line["A line"]
        direction LR
        PE["pre_execute"] --> PC["pre_command"] --> Run["command runs"] --> PoC["post_command"] --> PoE["post_execute"]
        PoC -- "next command" --> PC
    end
    subgraph call["A VFS call"]
        direction LR
        PV["pre_vfs"] --> Op["call runs or cache serves"] --> PoV["post_vfs"]
    end
    Run -. file operations .-> PV
    subgraph connection["A connection"]
        direction LR
        PN["pre_network"] --> Send["bytes flow"] --> PoN["post_network"]
    end
    Run -. any client in the line .-> PN
```

| Python | TypeScript | Runs | Can return |
| - | - | - | - |
| `pre_execute` | `preExecute` | once per line, before any of it runs | Deny, Ask, Route |
| `pre_command` | `preCommand` | before each command or CLI, once the line is parsed and expanded | Deny, Ask, Route |
| `post_command` | `postCommand` | after each command, once its output drained | Deny, Ask |
| `post_execute` | `postExecute` | once per line, after it ends | Deny, Ask, Limit |
| `pre_vfs` | `preVfs` | before each VFS call, including command and runtime file operations | Deny, Ask |
| `post_vfs` | `postVfs` | after each VFS call | Deny, Ask |
| `pre_network` | `preNetwork` | before a connection the workspace opens carries any byte | Deny, Ask, Limit |
| `post_network` | `postNetwork` | once that connection is over, however it ended | nothing; it observes |
| `pre_workspace` | `preWorkspace` | on a server, before a create, clone or load builds anything | Deny |

| What | Example | Covered by |
| - | - | - |
| A line | `cat /data/a.txt && rm /data/b` | `pre_execute`, `post_execute` |
| A command or CLI | `rm /data/b` | `pre_command`, `post_command` |
| A VFS call | `session.vfs.read("/data/a.txt")` | `pre_vfs`, `post_vfs` |
| A connection | `curl https://api.example.com/x`, a CLI's API call, a mount's read | `pre_network`, `post_network` |

| Entry point | Sends |
| - | - |
| `ws.shell`, `session.shell` | a line, with its commands and file operations |
| the `shell`, `ls` and `grep` tools | a line, with its commands and file operations |
| `mirage shell`, HTTP `/shell`, RPC `shell`, SSH | a line, with its commands and file operations |
| `session.vfs` | a VFS call |
| the `read`, `write`, `edit` and `glob` tools | a VFS call |
| `mirage workspace vfs`, HTTP `/vfs/<call>`, RPC `vfs/<call>` | a VFS call |
| FUSE, FSKit, SFTP | a VFS call |

File policies also apply when a command or file-based CLI uses `ctx.vfs`.
An account CLI calls its service directly, so command policy authorizes
that API operation; it has no VFS path to check. Its writes invalidate
caches for the mounts reached by that account. A policy's own file reads
remain exempt from coded file hooks to avoid recursion.

## Context

Each method gets one argument, `ctx`. Every `ctx` carries the session,
plus what its method is about, and a `post_` method adds what came back.
TypeScript spells the fields in camelCase (`ctx.sessionId`, `ctx.exitCode`).

| Methods | What it is about | `post_` adds |
| - | - | - |
| `pre_execute`, `post_execute` | `line` and `commands`: every command of the line | `exit_code`, `output`, `results` |
| `pre_command`, `post_command` | `line` and the command: `command`, `words`, `paths`, `walks` | `exit_code`, `stderr`, `records` |
| `pre_vfs`, `post_vfs` | `call`, `path`, `write` | `result` |
| `pre_network`, `post_network` | `host`, `address`, `port`, and who opened it: `command`, `program`, `words`, `line`, `mount` | `bytes_sent`, `bytes_received`, `seconds`, `ended` |
| `pre_workspace` | `config`: the config as the caller wrote it, secrets still pointers | |

`pre_command` is `pre_execute` narrowed to one command: each entry of
`ctx.commands` is the context `pre_command` gets for that command, read
before the line runs, so a word only the run can expand (`$X`) is not
resolved there yet.

Every `ctx` also carries:

| Field | What it is |
| - | - |
| `session_id` | the session the action runs as |
| `session` | the session, read only: `get(name)`, `snapshot()`, `cwd()`, `is_readonly(name)`, `profile()` |
| `vfs` | the session's files, read only: `read`, `stat`, `readdir`, `exists`, and where the mounts are (`mount_of`, `is_mount`, `submounts`), hides applied |
| `caller` | who makes the call (a `PolicyCaller`), a frozen copy: `caller.session` is the session above, with its `session_id` and `cwd` when the method was asked, and `caller.account` the account a server holds the workspace for (the one its requests sign in as), null outside a server or without accounts |

`words` is the command line word by word, after expansion. Each word has
a `text`, a `kind` (`name`, `subcommand`, `option`, `value`, `operand` or
`redirect`), the `option` a value belongs to, and the `path` it names.
For `tar -cf a.tar -C /mnt /data`:

| `text` | `kind` | `option` | `path` |
| - | - | - | - |
| `tar` | `name` | | |
| `-cf` | `option` | | |
| `a.tar` | `value` | `-cf` | `/a.tar` |
| `-C` | `option` | | |
| `/mnt` | `value` | `-C` | `/mnt` |
| `/data` | `operand` | | `/data` |

* `paths` is every path the command reaches, flat: its words' paths, the
  targets of links it names, and the working directory a bare `ls` reads.
* `line` is the line the command was parsed from: the line the agent
  typed, or for `eval`, `$( )` and a function body, that text.
* `results` is the line's execution tree: a node per command with its
  exit code, stderr and records.
* `records` are the calls a command made, one dict each: `op`, `path`,
  `source`, `bytes` and timing.
* `call` is the function called: its `name`, `effect` (read, write,
  metadata), `target`, and whether it `creates` or reaches the `subtree`.
  Every function a VFS marks reaches `pre_vfs` and `post_vfs`:
  `read_tree`, `search` and `copy` too.

A method only reads. A write it makes through Mirage is refused (`EROFS`
for a file, `EACCES` for a variable), and its reads are not judged again.
The session's history is `/.bash_history`, which a method reads through
`ctx.vfs` like any file.

### What the agent controls

Most of a `ctx` comes from what the agent did, so a policy reads it as a
claim to check, not as a credential:

| The agent sets | Through |
| - | - |
| `line`, `command`, `words`, `argv`, `paths`, `walks` | the line it types, after expansion |
| `cwd`, `session.cwd()`, `caller.session.cwd` | `cd` |
| `env`, `session.get()`, `session.snapshot()` | `export`, assignments and `readonly` |
| `call`, `path`, `write` at `pre_vfs` | the file call it made |
| `exit_code`, `stderr`, `output`, `records`, `result` | what its own command or call produced |

The host sets `session_id` (`caller.session.session_id` too), `profile`,
`mounts`, `prefix` and `mode`, and what `vfs` shows: the agent cannot
change them from inside the session. Decide who may do what from those, and
from a variable only when the workspace's or the profile's `env` sets it with
`readonly: true`. `session.is_readonly()` is also true for a name the agent
marked with `readonly NAME` itself.

A policy written as a program, on monty or Python on wasm, gets the same
`ctx` as JSON: see [Scripts](https://docs.mirage.strukto.ai/home/policy/scripts.md).

## Examples

### pre\_execute

```python Python theme={null}
from mirage.policy import Policy, Route, RouteContext

class JobsOnTheBox(Policy):
    async def pre_execute(self, ctx: RouteContext) -> Route | None:
        paths = [p.virtual for c in ctx.commands for p in c.paths]
        if any(p.startswith("/jobs/") for p in paths):
            return Route(machine="box")
        return None
```

```typescript TypeScript theme={null}
const jobsOnTheBox: Policy = {
  preExecute(ctx) {
    const jobs = ctx.commands.some((c) => c.paths.some((p) => p.virtual.startsWith('/jobs/')))
    return jobs ? { kind: 'route', machine: 'box' } : null
  },
}
```

`python3 /jobs/train.py` runs on the workspace's `box`
[machine](https://docs.mirage.strukto.ai/home/setup/machine/overview.md). See
[Route Machine](https://docs.mirage.strukto.ai/home/policy/route-machine.md).

### post\_execute

```python Python theme={null}
from mirage import Limit
from mirage.policy import ExecuteResultContext, Policy

class FirstHundredLines(Policy):
    async def post_execute(self, ctx: ExecuteResultContext) -> Limit:
        return Limit(max_lines=100)
```

```typescript TypeScript theme={null}
const firstHundredLines: Policy = {
  postExecute() {
    return new Limit({ maxLines: 100 })
  },
}
```

A longer output stops at 100 lines, and stderr says
`output truncated at limit (100 lines); ...`. A Deny here drops the
output: exit 126 and a decision record.

### pre\_command

`NoForcePush`, at the top of this page.

### post\_command

```python Python theme={null}
from mirage.policy import CommandResultContext, Deny, Policy

class NoSecretsShown(Policy):
    async def post_command(self, ctx: CommandResultContext) -> Deny | None:
        if any(r.get("path", "").startswith("/vault/") for r in ctx.records):
            return Deny("vault reads need an operator")
        return None
```

```typescript TypeScript theme={null}
const noSecretsShown: Policy = {
  postCommand(ctx) {
    const vault = ctx.records.some((r) => typeof r.path === 'string' && r.path.startsWith('/vault/'))
    return vault ? { kind: 'deny', reason: 'vault reads need an operator' } : null
  },
}
```

`cat /vault/k && echo next` exits 126 with `cat: Permission denied`, and
`echo` does not run. Output already sent stays sent. A runtime that takes
a line whole runs its commands itself, so no result comes back to judge:
while a `post_command` method is installed, such a line is refused.

### pre\_vfs

```python Python theme={null}
from mirage.policy import Deny, Policy, VfsContext

class ReadOnlyReports(Policy):
    async def pre_vfs(self, ctx: VfsContext) -> Deny | None:
        if ctx.write and ctx.path.virtual.startswith("/reports/"):
            return Deny("reports are read-only")
        return None
```

```typescript TypeScript theme={null}
const readOnlyReports: Policy = {
  preVfs(ctx) {
    return ctx.write && ctx.path.virtual.startsWith('/reports/')
      ? { kind: 'deny', reason: 'reports are read-only' }
      : null
  },
}
```

`session.vfs.write("/reports/q3.md", ...)` raises `EACCES`. The same
policy refuses a command redirect (`echo x > /reports/q3.md`) and writes
through runtime file APIs, FUSE, FSKit and SFTP.

### post\_vfs

```python Python theme={null}
from mirage.policy import Deny, Policy, VfsResultContext

class NoKeysOut(Policy):
    async def post_vfs(self, ctx: VfsResultContext) -> Deny | None:
        if isinstance(ctx.result, bytes) and b"PRIVATE KEY" in ctx.result:
            return Deny("holds a private key")
        return None
```

```typescript TypeScript theme={null}
const noKeysOut: Policy = {
  postVfs(ctx) {
    const text = ctx.result instanceof Uint8Array ? new TextDecoder().decode(ctx.result) : ''
    return text.includes('PRIVATE KEY') ? { kind: 'deny', reason: 'holds a private key' } : null
  },
}
```

Reading a file that holds a private key with `session.vfs.read` raises
`EACCES`; commands and runtime file reads meet the same check. Cached
reads and retained handles are checked against current policy before
returning their bytes or metadata. These checks do not fetch the retained
value again. Bytes already delivered to a program remain in its memory.

`post_vfs` can refuse a read but not shorten it: the file comes
back whole or not at all. To cap how much a command prints, use
`post_execute`.

### Variables

A variable write reaches no method: a variable does nothing until a
command, a file call or a connection uses it, and each of those methods
reads the session (`ctx.session`, and a command's `env`), so a rule about
a value goes in the method that uses it.

### pre\_network and post\_network

`pre_network` runs once for each connection the workspace opens, before it
carries any byte, whoever opens it: `curl`, `wget` or `git`, a builtin or
installed CLI, a mount's VFS, a runtime or an SDK in the process. The
workspace's [`network`](https://docs.mirage.strukto.ai/home/configuration/workspace#network) lists judge it first,
and a connection they refuse reaches no policy. The context says where it
goes (`host`, empty for a bare address; `address`; `port`) and who opened
it: `command`, `program` and `words` as `pre_command` reads them, `line`,
and `mount`, the prefix of the mount whose VFS opened it. URL, method and
status are not in it: under https only the program and the server see
them.

A `pre_network` answer refuses the connection (Deny, or Ask, since a
running command cannot wait on a question) or caps it: a `Limit` cuts it
once it carries `max_bytes`, sent and received together, or stays open
`timeout_seconds`. The tightest Limit among the policies wins. A refused or
cut connection is reset, so the program reads a broken connection, never a
clean end: `curl` exits 7 for a refusal.

`post_network` runs once the connection is over, once for each policy
whose `pre_network` let it through, however it ended: `ended` is `closed`,
`refused` (by a later policy), `limit` or `error`, with `bytes_sent`,
`bytes_received` and `seconds`. It observes only. A client that keeps a
connection open and sends on it again is judged once, as it opens. When the
address a program asked for does not answer, each other address its lookup
gave is a connection of its own, judged before it is tried. A socket the
program bound itself before connecting cannot pass the gateway, so while a
policy listens it is refused.

Together the two hold a workspace to a byte budget:

```python Python theme={null}
from mirage.policy import (
    Deny,
    Limit,
    NetworkContext,
    NetworkResultContext,
    Policy,
)

class Budget(Policy):
    def __init__(self, total: int) -> None:
        self.left = total

    async def pre_network(self, ctx: NetworkContext) -> Deny | Limit:
        if self.left <= 0:
            return Deny("the egress budget is spent")
        return Limit(max_bytes=self.left, timeout_seconds=300)

    async def post_network(self, ctx: NetworkResultContext) -> None:
        self.left -= ctx.bytes_sent + ctx.bytes_received
```

```typescript TypeScript theme={null}
let left = 500_000_000
const budget: Policy = {
  preNetwork() {
    if (left <= 0) return { kind: 'deny', reason: 'the egress budget is spent' }
    return new Limit({ maxBytes: left, timeoutSeconds: 300 })
  },
  postNetwork(ctx) {
    left -= ctx.bytesSent + ctx.bytesReceived
    return null
  },
}
```

A `host` machine, which starts a process on this machine, would
connect around the gateway, so while a policy judges connections it refuses
to run. A program on a [machine](https://docs.mirage.strukto.ai/home/setup/machine/overview.md) connects from
that machine, and no policy sees its connections.

In a browser a page sees no socket: `fetch` is judged by host name with an
empty `address`, and the counts are the bodies' bytes. While a policy
listens for connections, a request whose upload passes the Limit, or whose
size a page cannot know (a form, a stream), is refused before it is sent; a
redirect the page would follow is refused, since it goes where no policy
saw; and a `WebSocket` is refused, since its handshake cannot wait for an
answer.

### pre\_workspace

Only a server asks `pre_workspace`, with the policies it was started with
(see [Server policies](https://docs.mirage.strukto.ai/home/access/http.md#server-policies)). It sees the
config before any mount is built or any secret is read: a create's whole
config, a clone's or a load's `override`. `ctx` carries `config` and
`caller` only.

```python Python theme={null}
from mirage.policy import Deny, Policy, WorkspaceContext

class NoHostFiles(Policy):
    async def pre_workspace(self, ctx: WorkspaceContext) -> Deny | None:
        mounts = ctx.config.get("mounts") or {}
        if any(block.get("vfs") == "disk" for block in mounts.values()):
            return Deny("disk mounts are not offered here")
        if "machines" in ctx.config:
            return Deny("machines are set by the operator")
        return None
```

```typescript TypeScript theme={null}
const noHostFiles: Policy = {
  preWorkspace(ctx) {
    const mounts = (ctx.config.mounts ?? {}) as Record<string, { vfs?: string }>
    if (Object.values(mounts).some((block) => block.vfs === 'disk')) {
      return { kind: 'deny', reason: 'disk mounts are not offered here' }
    }
    if ('machines' in ctx.config) {
      return { kind: 'deny', reason: 'machines are set by the operator' }
    }
    return null
  },
}
```

The create answers 403 with the reason, and nothing is built.

## Answers

| Return | Effect |
| - | - |
| `None` | no opinion |
| `Deny(reason)` | refuses the action |
| `Deny(reason, path=p)` | fails the command with `p`'s own error; the rest of the line still runs |
| `Ask(reason)` | asks the host; see [Asks](#asks) |
| `Route(machine)` | chooses the machine that runs it |
| `Limit(max_lines=, max_bytes=)` | cuts the output at the bound, with a notice on stderr; with `on_exceed` set to error, no output and exit 1 |

TypeScript returns `{ kind: 'deny', reason }`, with
`path` for a path, `{ kind: 'ask', reason }`,
`{ kind: 'route', machine }` and `new Limit({ maxLines })`. A method that
raises refuses too. What the agent reads for each is on
[Explain](https://docs.mirage.strukto.ai/home/policy/explain.md#what-the-agent-reads).

## Asks

An Ask records a question in the workspace's ledger:

* With `on_ask` (`onAsk`), an inline allow answer continues the current request;
  a deny answer refuses it.
* Without an inline answer, including when the callback returns no decision,
  the request returns pending. The host records a decision in the ledger, allowing the caller to retry.

Ledger answers apply as follows:

* `allow`, scope `once` (default): the retry runs and spends it.
* `allow`, scope `session`: everything the ask covers, for the rest of
  that session.
* `deny`: the retry is refused; running it again asks again.

A host answers through [`ws.decisions`](https://docs.mirage.strukto.ai/python/access/in-app#ws-decisions),
[`mirage workspace allow`](https://docs.mirage.strukto.ai/home/access/cli#asks) or
[`POST /asks/{ask_id}`](https://docs.mirage.strukto.ai/api/operations/asks/respond-to-ask). An Ask from a `post_`
method comes after the action ran, so nothing is undone: what came back is
held, and the approved retry runs it again.

## When answers disagree

* Any Deny beats any Ask, so answering an ask never reopens a refusal.
* The first Deny speaks. The built-ins come first, then the session's
  profile, then its script, then `policies=`, then `add()`.
* Every Ask is put to the host, and the action runs once all are
  approved.
* Limits merge to the tightest value of each field.
* Two Routes to different machines refuse the line.
* A policy may give several answers at once, a tuple in Python or an
  array in TypeScript; each counts as if a policy of its own gave it.

## Examples on GitHub

[policy.py](https://github.com/strukto-ai/mirage/blob/main/examples/python/policy/policy.py)
and
[policy.ts](https://github.com/strukto-ai/mirage/blob/main/examples/typescript/policy/policy.ts)
run a profile's scripts.


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