instanceof), Rust has an Error enum, Python provides dedicated exception classes, and Go provides an *Error value with an ErrorKind discriminator matched via m.IsKind(err, kind) or errors.As. Ruby currently exposes Microsandbox::Error; where no dedicated subclass exists, its reference documents the stable message contract explicitly.
Matching errors
Spawn-time exec failures
exec() distinguishes between:
- A program that ran and exited non-zero: the call returns an
ExecOutputwith a non-zerocode. This is not an error in the SDK sense; it’s a normal result. - A program that never started: the binary doesn’t exist, isn’t executable, the working directory is unreachable, etc. The call returns or raises an SDK error.
ExecFailed payload, and Go exposes the same detail on streaming execution events. Common failure kinds include NotFound (binary missing on PATH), PermissionDenied, NotExecutable, BadCwd, BadArgs, ResourceLimit, UserSetupFailed, OutOfMemory, PtySetupFailed, and Other.
127 for NotFound, 126 for PermissionDenied and NotExecutable, and 1 otherwise.
Name conflicts
Creating a sandbox with a name that’s already in use (and withoutreplace) surfaces a typed error you can branch on to decide whether to recover (resume the existing one, regenerate the name, etc.).
connect_or_create and its language-specific equivalents to reuse the existing sandbox without changing its configuration. Pass replace() / replace=True / replace: true / --replace / WithReplace() only when you intend to stop the existing sandbox and create a new one. See Naming conflicts for the grace-period setting.
When a sandbox object is stale
ASandbox or SandboxHandle keeps the ID of the exact sandbox it represents. If that sandbox is removed and the name is reused, lifecycle methods refuse to act on the replacement and return a typed error.
Microsandbox::Error with the stable was replaced message is the Ruby-specific contract; the operation still refuses to act on the replacement.
Sandbox start failures
When a sandbox process exits before the agent relay is ready, creation returns or raises an SDK error. Rust exposes a structuredBootStart payload with the failure stage and underlying message.
Resource cleanup
Sandboxes hold compute resources, so release them when done. In TypeScript, preferawait using (Node 22+) which calls Sandbox.stop() automatically when the binding leaves scope. In Rust, Drop handles cleanup when the sandbox goes out of scope. In Go, pair every CreateSandbox with a defer that calls Stop + Close.