Skip to main content
Every time Agentastic creates a new worktree for an agent, you can run a setup script to bring that worktree to a working state automatically. Use setup scripts to install dependencies, copy environment files, run database migrations, or perform any other initialization your project needs. This page covers both the worktree setup script (per-repository) and the container setup script (per-machine).

Worktree setup script

The worktree setup script runs inside each new worktree directory immediately after Agentastic creates it. It is defined per repository, so different projects can have different setup routines.

Location and permissions

Create the script in your repository at:
Make it executable before committing:

Environment variables

Agentastic injects the following variables into your script at runtime:

Basic example

Advanced example

This script detects the package manager, copies all .env files, and handles Ruby, Python, and Rails projects in addition to Node.js:

Behavior notes

  • The script runs from the new worktree directory as its working directory.
  • Execution is non-blocking — worktree creation succeeds even if the script exits with an error.
  • Script output appears in the Worktree Setup task panel.
  • You can re-run the setup script at any time from the task panel.

Container setup script

The container setup script runs on your host machine before Agentastic creates a container. Instead of performing setup actions directly, it outputs a JSON object that Agentastic uses to configure the container.

Location

How it works

1

Agentastic runs your script

Agentastic executes the script with environment variables describing the worktree being containerized.
2

Your script prints JSON to stdout

The JSON object tells Agentastic which paths to mount, which environment variables to inject, and how to configure the container.
3

Agentastic parses the JSON and creates the container

The container is created with the mounts, environment variables, network mode, and shell you specified.

Output format

Your script must print a JSON object to stdout with the following structure:

Default script

Agentastic creates a default script the first time you use containers. To open it for editing:

Example customization

Add a shared npm cache mount and a custom environment variable to the default script:

Environment variables

Agentastic passes these variables to the container setup script:

Resetting the container setup script

If your container setup script produces invalid output or you want to start fresh:
  1. Open Settings > Agents
  2. Click Reset Setup Script

Teardown scripts

Agentastic does not currently run teardown scripts automatically. To clean up a worktree when you are done:
  1. Stop any running processes in the worktree
  2. Remove the worktree via Settings > Agents or by running git worktree remove <path>
  3. Containers are stopped and removed automatically when you close the associated worktree

Best practices

Write idempotent scripts

Your script may be run more than once — for example, if you trigger it manually from the task panel. Make each step safe to repeat:

Handle errors gracefully

Use set -euo pipefail to catch errors early, or handle individual failures explicitly:

Log progress

Print messages at each stage so you can follow along in the task panel:

Test your script manually

Before committing, run the script yourself with the expected environment variables to catch any issues:

Troubleshooting

Check three things:
  1. The file exists at .agentastic/setup.sh
  2. It is executable: chmod +x .agentastic/setup.sh
  3. It has no syntax errors: bash -n .agentastic/setup.sh
Add shell debugging flags at the top of your script:
The output appears in the Worktree Setup task panel.
Print them at the start of your script to verify they are being injected:
Test the script directly and pipe its output through jq to find syntax errors:
If jq reports a parse error, fix the JSON syntax in your script and test again.