Use casesUpdated

How do I stop two agent worktrees from using the same port?

Keep each branch's local preview on its own port, identify the process behind a URL, and compare the right running app with the right diff.

On this page

Git worktrees isolate files, not ports or databases. When two agents run a frontend at once, give each its own port and identify each branch by checkout and URL.

Canopy local server controls and detected process output
Canopy local server controls and detected process output

Name each checkout and its purpose

Create one worktree and branch per independent change, and label its Canopy component or agent session with the feature. Decide which checkout you're previewing and where its dependencies come from. A shared backend is fine for read-only UI work; stateful tests may need isolation.

Give each frontend a different port

Start each frontend on its own port with the framework's option: Vite's dev CLI takes --port, and next dev takes -p or --port. Check package.json first, since the command depends on the project's scripts. Save the working command in the Canopy component.

Read the actual URL from the process

Canopy's Servers panel shows process state, output and detected ports. A command can quietly pick another port, or fail, if its port is taken. Open the URL the process reports and confirm the branch with a visible change before judging the work.

Keep shared services explicit

A second frontend may still share the API, worker, database, cache or login session. If tests change shared data, run branches one at a time or separate the state. A worktree isn't a process or data sandbox.

Stop the right process and review the right diff

When a task finishes, stop its server from the right component, then review that worktree's diff and PR. Don't assume localhost:3000 is the branch you're reviewing. Branch, service output and URL together identify the running result.

Copyable resources

Example port plan

For a Vite project with two worktrees. Adapt scripts and ports to your repository.

Agent A · worktree: ../project-settings · branch: agent/settings
Command: npm run dev -- --port 4173
Preview: http://localhost:4173

Agent B · worktree: ../project-auth · branch: agent/auth
Command: npm run dev -- --port 4174
Preview: http://localhost:4174

For Next.js, next dev supports --port too.
Before reviewing: confirm each command's output shows the expected port and the page matches its branch.

Frequently asked questions

Do Git worktrees automatically prevent port conflicts?

No. Worktrees separate directories and branches. Local services still need distinct ports or sequential runs.

Can two worktrees share the same database?

Yes, if the project is set up that way, but stateful tests may interfere. Decide whether each branch needs its own data.

More questions and answers →

Sources and further reading