Documentation
¶
Overview ¶
Package sandbox wraps agent shell commands in an OS-level containment layer — defense-in-depth UNDER the permission classifier, not a replacement for it. The classifier decides whether a command may run; the sandbox bounds what a permitted command can touch if it (or something it spawns) misbehaves.
Modes:
ReadOnly — read-only sessions (plan/scout shells): the filesystem is
readable but only tmp + tool caches are writable. Always on
for read-only sessions when the platform supports it.
Workspace — writes confined to the repo root + tmp + tool caches.
Opt-in for normal sessions via MEMCODE_SANDBOX=1.
Backends: macOS sandbox-exec (Seatbelt), Linux bubblewrap (bwrap) when installed. No backend → commands run unwrapped, exactly as before (fail open: the sandbox strengthens an existing gate, it must never brick a platform). MEMCODE_SANDBOX=0 force-disables wrapping everywhere.
Index ¶
Constants ¶
const EnvVar = "MEMCODE_SANDBOX"
EnvVar toggles sandboxing for NORMAL sessions: "1"/"true"/"workspace" wraps every bash call in Workspace mode; "0"/"off" force-disables all wrapping (including the read-only default) as the escape hatch.
Variables ¶
This section is empty.
Functions ¶
func NestingFailure ¶ added in v0.56.0
NestingFailure reports output showing a command failed only because it ran inside memcode's sandbox: it tried to start its own sandbox (Seatbelt can't nest; bubblewrap may lack namespaces), or to gain privileges with sudo (setuid programs don't run sandboxed on macOS).
func Supported ¶
Supported reports whether Wrap would ACTUALLY contain under p on this platform — callers that scale a permission gate to the containment (mcp_code_exec: sandboxed+no-network = Medium, unsandboxed = Dangerous) must know the difference between "asked for a sandbox" and "got one".
Types ¶
type Policy ¶
type Policy struct {
Mode Mode
Root string // repo root (Workspace mode's writable tree)
// Scratchpad is the session-scoped writable scratchpad directory for analytical modes.
Scratchpad string
// StrictScratchpad confines writes exclusively to Scratchpad (no broad /tmp or ~/.cache),
// hides host /tmp in bwrap via --tmpfs /tmp, and denies network.
StrictScratchpad bool
// DenyNetwork additionally cuts ALL network egress (Seatbelt deny network*,
// bwrap --unshare-net). Used by mcp_code_exec: with writes confined AND no
// network, generated code loses its exfiltration channel and can gate at
// Medium instead of Dangerous. Bash keeps network (builds/tests need it).
DenyNetwork bool
// Shield keeps memcode's own credentials and authority out of the command's
// reach, in every mode including Off: see shield.go. Set by PolicyFor unless
// sandboxing is force-disabled.
Shield *Shield
}
Policy is what a wrapped command may touch.
func PolicyFor ¶
PolicyFor decides the policy for a bash call: read-only sessions always get ReadOnly containment; normal sessions get Workspace only when opted in.
func ShieldOnly ¶ added in v0.56.0
ShieldOnly is the policy for a long-lived helper process memcode starts on the user's behalf (an MCP server): no write containment — it keeps its own caches and state — but the shield, unless sandboxing is force-disabled.
type Shield ¶ added in v0.56.0
type Shield struct {
Root string // the working tree (may itself live under ~/.memcode, e.g. a task worktree)
ProjectState string // this project's state dir
StateHome string // ~/.memcode (or MEMCODE_HOME)
NoAccess []string // directories neither readable nor writable
NoAccessFile []string // single files neither readable nor writable
Writable []string // under StateHome, still writable
Readable []string // under StateHome/projects, still readable (and writable)
}
Shield is the part of the sandbox that is on for every agent command, not only in opt-in containment. It is an OS-enforced boundary (Seatbelt on macOS, bubblewrap on Linux), unlike the output redaction and command classification layered above it, and it protects two things a prompt injection must never reach through a shell:
- credentials ordinary development never needs: memcode's own (~/.config/memcode) and the subscription logins memcode can ride on (Claude Code's and Codex's OAuth files) — unreadable;
- memcode's authority: everything under ~/.memcode is read-only except site sources, skills, the current project's scratch area and the working tree itself, so a command can't approve its own future commands, add a hook or an MCP server, or rewrite memory; other projects' state (transcripts, checkpoints) is unreadable.
Everything else (the repo, ~/.ssh for git, cloud CLI configs for deploys, the network) stays as available as before: Auto mode keeps working for ordinary development.
func ForAnalytical ¶ added in v0.56.8
ForAnalytical returns a Shield that additionally blocks common SSH/cloud/GPG credential directories for analytical scratchpad execution.