Skip to main content
A policy can be a program instead of a class. A profile names a list of programs for each method it answers at, and the runtime each one runs on. A program defines a function named for the method, which takes the same ctx a coded policy reads and returns its answer. Two places take a program: A server’s programs read no workspace files: at pre_workspace the workspace does not exist yet, and its ctx is the config and the caller.

A profile’s policy

Save these files together. Install Monty as described in Runtimes. The selected profile runs guard.py before each command and refuses rm.
workspace.yaml
guard.py
Each entry names its program and its runtime: a name, or {name, config} when the runtime needs settings. An entry with no runtime runs on monty. A method’s programs are asked in order, up to the first deny, and each answer counts as if a policy of its own gave it (when answers disagree): an ask from one program and a route from another both hold. A method with no program is never asked and costs nothing. In code, a profile takes the same block, with a ScriptSource for each program:

The program

A program defines a function named for its method at the top level, def pre_command(ctx) at pre_command, and Mirage calls it after the program runs. A program without one refuses every action it is asked about, with the reason defines no pre_command(ctx).
  • Input: ctx, the function’s argument, as plain data. Mirage carries it to the program in the environment variable MIRAGE_POLICY_CTX, which is reserved; the session never sees it. Beside it, Mirage passes only the entry’s own env: the session’s variables stay out of its environment. A runtime may add its own, as wasi adds PYTHONHOME and PYTHONDONTWRITEBYTECODE.
  • Session: the program starts in the session’s working directory, so os.getcwd() is the session’s. The session’s variables are in ctx["env"], not in os.environ. An import never loads a module from the session’s directory.
  • Answer: what the function returns. None is no opinion. On monty the function may be async def, to await host functions.
A deny or an ask without a reason gets the default one. A route may give one too, which explain shows. Mirage prints the return value on stdout as JSON and reads it back there, so anything else the program prints on stdout, such as a debug line, fails it; write those to stderr.

Variables

An entry’s env gives its program variables beside MIRAGE_POLICY_CTX, on every runtime. A value is a string, or a pointer to a credential source such as aws-sm or 1password, read the first time the program runs and kept for the workspace’s life:
workspace.yaml
A session record and a snapshot keep the pointer, never the value it read. A source that cannot answer refuses the action, as a failed program does. MIRAGE_POLICY_CTX cannot be set here.

Host functions

A program on monty can call functions the host hands the workspace, by name. They are passed in code, never in a document or over the server API, so a profile can call them but not add one. Programs on wasi and pyodide, and the agent’s own programs, do not see them.
guard.py
A function may be sync or async and takes and returns plain data: strings, numbers, booleans, None, lists and dicts. One that raises raises in the program, which may catch it; uncaught, the program fails and the action is refused. A function may take as long as it needs, within the program’s 10 seconds, and programs calling functions run side by side rather than one at a time.

ctx

Every field a coded policy reads, under the Python names, as plain data. For cat notes.txt | wc -l in /data, pre_execute gets:
Each entry of commands has the env that command runs with, its own NAME=value prefix included: for GPU=1 python3 train.py, only the python3 entry has GPU. pre_command gets one entry of commands, with the session fields beside it: A post_ method gets its pre_ method’s ctx plus what came back: Every ctx also has session_id, cwd, env, profile, mounts, the mount roots the session can see, and caller, whose session holds the session_id and cwd a coded policy reads off ctx.caller. The agent sets most of these fields; what the agent controls says which ones a program can trust.

Files and the session

A program reads the workspace as the session it judges for:
  • open(), os.listdir() and os.stat() go through the workspace. A hidden path does not exist, path rules apply, and a write is refused (EROFS).
  • Its own reads are not judged again by its profile’s pre_vfs or post_vfs.
So a program can judge a file by what is in it. This one disables rm, and a Python script whose code deletes files:
guard.py
The script reads the code as text, so it stops the usual calls, not every way Python can delete a file. A rule that must always hold belongs in a coded policy, which judges each file call itself.

When a program fails

A program that defines no function for its method, exits non-zero, runs past 10 seconds, returns anything but an answer its method can give, or names a runtime that cannot run here refuses the action. The reason names the profile and what went wrong, such as profile 'reviewer' policy exited 1, and the program’s stderr goes to the log. Silence on failure would let through exactly what the policy is there to judge. route_policy: is a deployment setting, so the same mistakes fail the line with a routing error instead.

Runtimes

Each runtime is the policy’s own, built from the name and settings the profile gives and never shared with the agent. Monty is the default, and it must be installed: mirage-ai[monty] in Python, @pydantic/monty in TypeScript. Without it, a program on monty refuses every action it judges and the refusal names the package to install. wasi needs a CPython WASI build: its folder as config: {home: ...}, or in MIRAGE_WASI_HOME. A program gets 10M of memory on monty and 128M on wasi and pyodide, unless its runtime’s config names max_memory (null for no cap). A program that needs more fails like any other. Monty runs a program in about a millisecond. wasi starts CPython on every run, 100 to 300 ms, so it suits the methods asked less often; pre_vfs, asked for every file call from outside a line, is usually monty’s.