Hooks let you run commands at specific points in the sandbox lifecycle — before the agent starts, after the worktree is ready, or inside the sandbox after it is created. This is where you install dependencies, copy secrets into the worktree, or perform any other setup that the agent needs before it begins working.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/mattpocock/sandcastle/llms.txt
Use this file to discover all available pages before exploring further.
The hooks option
Passhooks to run() with host and sandbox sub-objects. Each sub-object groups hooks by when they fire:
Available hooks
host.onWorktreeReady
Runs on the host after the worktree is created but before the sandbox container starts. Use this hook to copy files into the worktree that the agent will need — for example, a.env file that should not be committed to the repo.
{ command: string } objects. There is no sudo option for host hooks — commands run on the host as your user. Host hooks in onWorktreeReady run sequentially, in the order you list them.
host.onSandboxReady
Runs on the host after the sandbox container is created. This fires in parallel withsandbox.onSandboxReady. Use this for host-side operations that are only meaningful once the sandbox exists — such as registering the sandbox in an external system.
sandbox.onSandboxReady
Runs inside the sandbox after the container is created, in parallel withhost.onSandboxReady. This is the right place to install dependencies that the agent needs, such as running npm install.
{ command: string; sudo?: boolean } objects. Pass sudo: true to run the command as root inside the sandbox.
Lifecycle order
Hooks fire in this order:copyToWorktree
Files listed in the
copyToWorktree option are copied into the worktree on the host. This happens before the container starts.host.onWorktreeReady (sequential)
Host worktree hooks run one by one on the host, in the order you list them.
Copying files into the worktree
UsecopyToWorktree to copy host-relative file paths into the sandbox before the container starts. This is the cleanest way to inject secrets or generated files:
Example: install dependencies and inject a secret
This example copies a.env file into the worktree on the host, then runs npm install inside the sandbox:
.env file lands in the worktree before the container starts, so npm install runs with the correct environment variables already in place.