Sub topic 8
Dependencies, ports and untracked files
This chapter assumes you can create a worktree for each agent, as covered in the earlier chapters, and that you run agents in the background from a shell. A new worktree contains only files that Git tracks. Anything that lives outside version control is missing, and anything that uses a fixed machine resource can collide with another agent.
What a new worktree lacks
git worktree add checks out the tracked files of one commit. It does not copy ignored or untracked files from your main checkout. Three kinds of files usually matter:
- Dependency folders such as
node_modulesor.venv. - Local configuration such as
.env, which holds secrets and is listed in.gitignore. - Build output and caches.
An agent that starts in a bare worktree will often fail on its first test run. It may also try to fix the failure by editing files it should not touch.
Dependencies
Install dependencies separately in each worktree. Sharing one folder through a symlink is tempting. It breaks when two agents change the lockfile or install different package versions, because both write to the same place.
cd ../myrepo-auth
npm cinpm ci installs exactly what the lockfile says, so every worktree gets the same result. For Python, create one virtual environment per worktree.
cd ../myrepo-auth
python -m venv .venv
.venv/bin/pip install -r requirements.txtInstalling several times costs disk space and time. Many package managers keep a global download cache, so repeat installs mostly copy files from that cache.
Untracked configuration
Copy the files the project needs to run. Copy them instead of linking them, so an agent that edits .env does not change the setting for every other agent.
for task in auth billing search; do
cp ../myrepo/.env "../myrepo-$task/.env"
doneCheck that .env is in .gitignore. Otherwise an agent may commit your secrets to its branch.
Ports
Two dev servers cannot listen on the same port. If every agent starts the app on port 3000, the second one fails or, worse, the agent talks to another agent's server and reports results from the wrong code.
Give each worktree its own port through its .env file.
port=3001
for task in auth billing search; do
echo "PORT=$port" >> "../myrepo-$task/.env"
port=$((port + 1))
doneThis assumes the application reads PORT from the environment. Apply the same idea to database ports, debugger ports and test servers. Databases are harder: if all agents write to one local database, they share state that Git cannot isolate. Give each worktree its own database name, for example myrepo_auth.
How the pieces fit
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `command not found` or missing module in a new worktree | Dependencies were not installed | Run the install command in that worktree |
| App crashes with a missing variable | `.env` was not copied | Copy it before starting the agent |
| `EADDRINUSE` or "address already in use" | Two agents use the same port | Assign a unique `PORT` per worktree |
| Tests in one worktree fail after another agent runs | Shared local database | Use a separate database per worktree |
Exercise
Three agents each run the same dev server, and the second and third fail with "address already in use". You have already copied .env into each worktree. What should you change, and why not just stop the first server?
Show answer
Give each worktree a unique PORT value in its own .env file, for example 3001, 3002 and 3003. Stopping the first server only moves the conflict to whichever agent starts next, and it removes the point of running the agents in parallel.