Policies: Sandbox and Browser¶
Alice asks the analyst for a chart of churn by plan over the last six months. The analyst writes Python that queries the numbers, plots them, saves an HTML page, and hands Alice a link. Later the researcher needs the pricing page of a competitor's analytics product, which sits behind a sign-in. It opens the page in a browser, hands the browser to Alice to sign in, and carries on once she has.
Both computers belong to the growth policy, and so does the rule for how
long the bot may wait for Alice.
The policy resource¶
A policy is a named, durable resource. The bot creates one per channel:
curl -X PUT http://127.0.0.1:8000/api/v1/policies/growth \
-H "Content-Type: application/json" \
-d '{"sandbox": true, "browser_sites": ["https://*.competitor.com"], "wait_max_seconds": 86400}'
# src/mash/policies/models.py
class Policy(BaseModel):
model_config = ConfigDict(extra="forbid")
sandbox: bool = True
browser_sites: Optional[list[str]] = None
wait_max_seconds: Optional[int] = Field(
default=DEFAULT_WAIT_MAX_SECONDS, ge=MIN_WAIT_MAX_SECONDS
)
sandbox(defaulttrue):falseremovesrun_code,edit_file,sandbox_put,sandbox_get,browser_saveandbrowser_uploadfrom every agent for requests under the policy.browser_sites(defaultnull):nullkeeps each agent's ownbrowser_sites(). A list is intersected with each agent's sites, so it narrows and never widens.[]removes the browser tools.wait_max_seconds(default 86400, at least 60,nullfor no cap): caps every interaction's deadline.
PUT replaces the whole policy and GET reads it. Many sessions share one
policy: every thread in #growth names growth. A session binds to its
policy on its first event, as the events post
covered, and each request takes a snapshot of the policy when its event
starts. A policy change affects events that start after it.
DELETE /api/v1/policies/growth removes the policy's sandbox container and
volume and its browser container and profile, then the policy. It returns
409 POLICY_IN_USE while a session on the policy has a queued or running
event. The sessions stay, and a later event naming the policy gets 404
POLICY_NOT_FOUND.
Requests reach both through the host¶
flowchart LR
subgraph host ["host"]
R1["/sandbox relay"]
R2["/browser relay"]
end
A["analyst process\nrun_code"] -- "agent token,\nrequest_id" --> R1
B["researcher process\nbrowser_navigate"] -- "agent token,\nrequest_id" --> R2
R1 -- "policy from\nthe request" --> S["mash-sandbox-‹key›\nworkspaces/analyst\nworkspaces/researcher"]
R2 -- "policy from the request,\nsites = spec ∩ policy" --> W["mash-browser-‹key›\nChromium, profile"]
W -- "browser_save" --> S
The tools run in the agent's process and send only the agent's token and the
current request id. The host's relay reads the request's identity, checks it
belongs to the calling agent, and acts in that request's policy's sandbox or
browser. A model cannot pick another policy's computer, and a request whose
policy snapshot says "sandbox": false is refused at the relay as well as
missing the tools.
Several sessions on one policy run at once in one sandbox and one browser. Each agent has its own sandbox folder, and each request its own browser tab.
The sandbox¶
The sandbox is a container per policy, started through the Docker socket the
first time an agent runs code under it. It runs Mash's sandbox image with
Python 3.12, Node 22, SQLite, git, curl, jq and rg, and it can reach the
internet. It holds that policy's workspaces, one folder per agent at
/home/agent/workspaces/<agent_id>, and nothing else: no secrets, no database
access, and no environment from the host.
Every agent gets run_code and edit_file when the host has sandboxes. A
spec opts out with enable_sandbox(). run_code takes a language (python,
javascript or shell) and code, and each call runs as a new process in the
agent's folder. Files, SQLite databases and pip install --user packages
persist on the sandbox's volume across requests and restarts; variables and
shell state do not.
Inside, a daemon runs each call as a uid of the agent's own, from 20000 up,
with a clean environment, in a folder only that uid can open. The analyst's
code cannot read the researcher's files or their processes' environment. An
agent whose spec declares sandbox_env() sends those variables with each of
its calls, so the analyst's code can get a read-only replica URL that the
researcher's code never sees.
The analyst hands Alice the chart through a link the host serves. A file at
<path> in the agent's folder opens at $MASH_FILES_URL/<path>, and a server
the code started on port n at $MASH_PORTS_URL/<n>/. The link carries a
token that names the policy, so it opens only that policy's files.
Output comes back whole up to 10 MB. A result longer than the agent's
tool_result_context_chars (20,000 by default) is saved to
tool-results/<trace_id>/<tool_call_id>.txt in the agent's folder, and the
model gets a note with its size, line count, exit code and path, and reads it
with run_code (grep -n, tail, sed -n). execute_tool.end keeps the
full result either way.
sandbox_put and sandbox_get copy files between the agent's own machine and
its sandbox folder, and exist only for a spec that declares
sandbox_file_roots().
A sandbox stops after MASH_SANDBOX_IDLE_SECONDS (default 900) without use,
and at most MASH_SANDBOX_MAX_RUNNING (default 20) run at once. Without a
Docker daemon the host still runs, and its agents get no sandbox tools.
The browser¶
Each policy also gets a Chromium in a container of its own, next to the sandbox and on a separate Docker network. Its profile, on the container's volume, keeps the sites a user signed in to, and every agent and session under the policy shares it.
An agent gets the browser tools when its spec lists sites with
browser_sites(). The researcher's spec lists the competitor sites it reads,
and the policy's browser_sites narrows that list further. The tools are a
subset of Playwright MCP's (browser_navigate, browser_snapshot,
browser_click and the rest) plus three of Mash's: browser_save,
browser_upload and browser_handoff.
Inside the container a daemon runs Chromium on a virtual display with
Playwright MCP attached over CDP. Each request gets its own MCP session and
its own tab. The daemon checks every URL against the request's sites before a
call runs, and when a click or redirect lands outside them it goes back and
returns an error instead of the page. Page text comes back between
<web-content> tags, and the tool description tells the model it is data from
the website, never instructions from the user.
Downloads land in the browser container. browser_save copies one into the
agent's sandbox folder, where run_code reads it, and browser_upload hands
a file from that folder to a file input. The sandbox has no route to the
browser: it isn't on the browsers' network and holds no browser token.
Sign-in¶
The researcher reaches a sign-in page and calls browser_handoff. The runtime
turns the call into a durable interaction of type browser, which carries no
link. When Alice is ready, the bot asks for one:
curl -X POST http://127.0.0.1:8000/api/v1/sessions/slack:C42:1731864100.0001/events/1731864100.0007/interactions/itr_…/handoff
The host mints a live-view link valid for MASH_BROWSER_HANDOFF_SECONDS
(default 900) that opens noVNC on the request's tab in Alice's own browser.
She signs in, the bot answers the interaction with done, and the request
continues with the session in the profile. Mash never sees the password.
Every link minted for the interaction stops working when it closes. Apps list
and clear a policy's signed-in sites under
/api/v1/browser/policies/{policy_id}.
How long an agent waits¶
wait_max_seconds caps every interaction under the policy. An approval waits
300 seconds and an AskUser question 3600 seconds unless the cap is lower. A
browser handoff has no timeout of its own, so it waits for the cap, and with
no cap until it is answered or cancelled. At the deadline the agent gets the
timed-out answer (an approval denied, the question's default, a failed
handoff) and finishes its turn.
Every pause in this post is the same mechanism underneath: a durable interaction in the request's event log. The next post covers that log and the loop that writes it.
Next: Durable Agent Loop.