Skip to main content
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.

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. 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). 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: 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:
  • 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 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.

Examples

pre_execute

python3 /jobs/train.py runs on the workspace’s box machine. See Route Machine.

post_execute

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

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

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

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 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:
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 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). 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.
The create answers 403 with the reason, and nothing is built.

Answers

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.

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, mirage workspace allow or POST /asks/{ask_id}. 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 and policy.ts run a profile’s scripts.