{"modulePath":"github.com/memcode-ai/memcode","version":"v0.56.16","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package sandbox\n\nPackage 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.\n\nModes:\n\n\tReadOnly   — read-only sessions (plan/scout shells): the filesystem is\n\t             readable but only tmp + tool caches are writable. Always on\n\t             for read-only sessions when the platform supports it.\n\tWorkspace  — writes confined to the repo root + tmp + tool caches.\n\t             Opt-in for normal sessions via MEMCODE_SANDBOX=1.\n\nBackends: 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.\n\n## Constants\n\n```go\nconst EnvVar = \"MEMCODE_SANDBOX\"\n```\nEnvVar 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.\n\n## Functions\n\n```go\nfunc NestingFailure(output string) bool\n```\nNestingFailure 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).\n\n```go\nfunc Supported(p Policy) bool\n```\nSupported 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\".\n\n```go\nfunc Wrap(command string, p Policy) (string, bool)\n```\nWrap returns a shell line that runs command under the policy's containment, and whether wrapping applies. ok=false means run the command unchanged (policy Off, or no backend on this platform).\n\n```go\nfunc WrapArgv(argv []string, p Policy) (string, bool)\n```\nWrapArgv is Wrap for a program and its arguments, quoted for the shell.\n\n## Types\n\n```go\ntype Mode int\n```\nMode is the containment level.\n\n```go\nconst (\n\tOff Mode = iota\n\tReadOnly\n\tWorkspace\n)\n```\n\n```go\ntype Policy struct {\n\tMode Mode\n\tRoot string // repo root (Workspace mode's writable tree)\n\t// Scratchpad is the session-scoped writable scratchpad directory for analytical modes.\n\tScratchpad string\n\t// StrictScratchpad confines writes exclusively to Scratchpad (no broad /tmp or ~/.cache),\n\t// hides host /tmp in bwrap via --tmpfs /tmp, and denies network.\n\tStrictScratchpad bool\n\t// DenyNetwork additionally cuts ALL network egress (Seatbelt deny network*,\n\t// bwrap --unshare-net). Used by mcp_code_exec: with writes confined AND no\n\t// network, generated code loses its exfiltration channel and can gate at\n\t// Medium instead of Dangerous. Bash keeps network (builds/tests need it).\n\tDenyNetwork bool\n\t// Shield keeps memcode's own credentials and authority out of the command's\n\t// reach, in every mode including Off: see shield.go. Set by PolicyFor unless\n\t// sandboxing is force-disabled.\n\tShield *Shield\n}\n```\nPolicy is what a wrapped command may touch.\n\n```go\nfunc PolicyFor(readOnly bool, root string) Policy\n```\nPolicyFor decides the policy for a bash call: read-only sessions always get ReadOnly containment; normal sessions get Workspace only when opted in.\n\n```go\nfunc ShieldOnly(root string) Policy\n```\nShieldOnly 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.\n\n```go\ntype Shield struct {\n\tRoot         string   // the working tree (may itself live under ~/.memcode, e.g. a task worktree)\n\tProjectState string   // this project's state dir\n\tStateHome    string   // ~/.memcode (or MEMCODE_HOME)\n\tNoAccess     []string // directories neither readable nor writable\n\tNoAccessFile []string // single files neither readable nor writable\n\tWritable     []string // under StateHome, still writable\n\tReadable     []string // under StateHome/projects, still readable (and writable)\n}\n```\nShield 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:\n\n  - 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;\n  - 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.\n\nEverything 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.\n\n```go\nfunc ForAnalytical(root string) *Shield\n```\nForAnalytical returns a Shield that additionally blocks common SSH/cloud/GPG credential directories for analytical scratchpad execution.\n\n```go\nfunc ShieldFor(root string) *Shield\n```\nShieldFor builds the shield for a session rooted at root.\n\n","path":"github.com/memcode-ai/memcode/internal/sandbox","name":"sandbox","synopsis":"Package sandbox wraps agent shell commands in an OS-level containment layer — defense-in-depth UNDER the permission classifier, not a replacement for it.","isRedistributable":true}
