Host paths¶
A task often works on files of the host: a repository to test, data to read, a
project for an agent to change. Three markers, given in Annotated on a Path
input, tell FastSandbox what to do with the directory the caller passes:
WorkspaceDir mounts it as the workspace of the
sandbox, Mounted mounts it as a further directory, and
Uploaded copies its contents in. This page covers the
three, and clone mode.
Mounting and copying¶
from pathlib import Path
from typing import Annotated
from fastsandbox import Context, FastSandbox, Mounted, Uploaded, WorkspaceDir
app = FastSandbox("paths")
@app.task(kit="shell")
async def inventory(
ctx: Context,
repo: Annotated[Path, WorkspaceDir()],
data: Annotated[Path, Mounted("/data", read_only=True)],
fixtures: Annotated[Path, Uploaded("/work/fixtures")],
) -> list[str]:
"""Lists what the sandbox sees of three host directories."""
listings = []
for path in (str(repo.resolve()), "/data", "/work/fixtures"):
output = await ctx.sandbox.exec(["ls", "-A", path], check=True)
listings.append(output.stdout.strip())
return listings
fastsandbox run docs_src/tasks/host_paths.py inventory --repo . --data ~/datasets --fixtures tests/fixtures
| Marker | What happens to the directory | Inside the sandbox | Changes made in the sandbox |
|---|---|---|---|
WorkspaceDir() |
Mounted as the workspace, read-write | At its own absolute path | Reach the host |
Mounted(target, read_only=False) |
Mounted as a further directory, or file | At target, else at its own absolute path |
Reach the host, unless read_only=True |
Uploaded(target) |
Its contents copied before the task runs | Under target |
Stay in the sandbox |
- A task has at most one
WorkspaceDir: a run with two raisesTaskError. - The daemon mounts further directories only beside a workspace: a run that
mounts a
Mountedinput without aWorkspaceDirraisesInvalidSpecError, on themountsfield of the spec of its sandbox, before any sandbox is created. A task that only reads data takes a workspace for what it writes, or copies the data in withUploaded. - The paths are expanded (
~) and resolved to absolute paths on the host before they are mounted or copied. - The parameter keeps the value the caller gave: a relative path such as
.stays relative. To name the workspace inside the sandbox, use its absolute path,repo.resolve(), asinventorydoes. Uploadedcreatestargetas root, owned by the default user of the kit, so that it may lie where that user cannot write, such as/work. The copy is the sandbox's own, and goes with it.- An input whose value is
None, for an optionalPath | None = None, is neither mounted nor copied.
Mounts are shared with the host
What the task, or an agent, changes in a mounted directory changes the
files of the host, during the run. Use Uploaded, Mounted(...,
read_only=True) or clone mode when the host must stay as it is.
Clone mode¶
WorkspaceDir(clone=True) gives the sandbox a private clone of the Git
repository of the directory, with its history, at the same path, instead of the
directory itself; the repository of the host is mounted read-only at
/run/sandbox/source. The sandbox can change and commit what it likes, the
repository of the host stays as it was.
The clone goes with the sandbox when the run ends: what the task wants to keep
must leave as its result. Here an agent fixes an issue on the clone, and the
task returns its commits as a patch, for the caller to review and apply with
git am:
from pathlib import Path
from typing import Annotated
from fastsandbox import Context, FastSandbox, WorkspaceDir
app = FastSandbox("patches")
@app.agent("claude", timeout="20m")
async def propose_patch(
ctx: Context, repo: Annotated[Path, WorkspaceDir(clone=True)], issue: str
) -> str:
"""Has Claude Code fix an issue in a private clone, and returns its commits as a patch."""
workdir = str(repo.resolve())
head = await ctx.sandbox.exec(["git", "rev-parse", "HEAD"], working_dir=workdir, check=True)
await ctx.agent.prompt(f"{issue}\n\nFix it and commit the fix.", working_dir=workdir)
# The clone goes with the sandbox: its new commits leave as a patch.
patch = await ctx.sandbox.exec(
["git", "format-patch", "--stdout", f"{head.stdout.strip()}..HEAD"],
working_dir=workdir,
check=True,
)
return patch.stdout
client.create(workspace=..., clone=True) gives
the same mode to a sandbox created with the client; the shell-clone example
of the repository also fetches the commits of a clone from the host with Git.