The app I work on at my day job is three frontend repos. Running it locally means four dev servers, two Node versions and two logins. Setting that up by hand took long enough that I put off switching tasks, and switching tasks is most of the job.
So I built a few small command-line tools. One makes a copy of the whole stack for a task. One runs a copy against a test environment and logs it in. One deploys from it. One finds what it left running and stops it. There are about 30 copies on my laptop today.
Agents wrote much of this code, with me directing. What I kept for myself was deciding what each tool must never do, and most of those rules came from the bugs below.
A copy is a folder of worktrees
workspace new search-filter makes a sibling folder next to the original. Inside it, each of the three repos is a git worktree on a fresh branch.
stack-search-filter/
repo-a/ worktree on search-filter
repo-b/ worktree on search-filter
repo-c/ worktree on search-filter
.claude/launch.json this copy's ports
_docs -> knowledge/ shared notes, linked in
Worktrees, not clones, because they share the original’s object store. Nothing is downloaded, and a new one exists about as fast as git can write the files.
The order of the steps matters more than the steps. The tool always copies from the true source: the folder whose first repo has a real .git directory, not a pointer to someone else’s. It fetches all three repos in parallel before it picks a base branch, and if one repo lacks that branch, it asks which to use for that repo. Every step it takes is recorded, and a failure undoes all of them. A half-made copy is worse than none, because the next copy counts it.
Editor and agent config are copied with symlinks kept as symlinks. My knowledge repo is linked into every copy, so the agent in each one reads the same notes.
Install is the slow part. Quick mode links each repo’s node_modules from the original and skips npm and bower entirely, which turns minutes into seconds. The cost is that every copy shares the same mutable dependencies. There’s a full install for when that matters.
1,911 lines of bash. I also built a VS Code extension for it on the day of the first commit, and never touched it again. The terminal won.
Ports the browser refuses
Two copies can’t both use port 4200. So each copy gets a slot of 100 ports: its index times 100, added to every base port. There’s no allocator and no daemon. The index is the number of copies that existed when it was made.
That worked until copy 18. Its new app came up on port 6000. The server said it was listening, and curl got a 200 back. Chrome refused the page, and what was left threw on its first read from browser storage. It looked like an app bug, so I chased the storage error first. Chrome refuses a short list of ports outright, and 6000 is on it because X11 used it decades ago. My tool picked ports by arithmetic, and the arithmetic had walked straight into one.
Drag the slider. Copy 17 has the same problem with a different server.
HTTP/1.1 200 OK
storage: ok
Copy 0: ports 3000, 3400, 4200, 4300. Every one is a port Chrome will open.
Refused in this range: 3659 (Apple SASL), 4045 (NFS lock), 4190 (Sieve), 5060 and 5061 (SIP), 6000 (X11).
The fix keeps the slot and moves inside it. The tool carries Chromium’s list of refused ports. If any of a copy’s ports lands on one, the whole block shifts by 10 and tries again, up to the edge of its slot.
def allocate(index, bases, stride=100, step=10):
for nudge in range(0, stride, step):
ports = [b + index * stride + nudge for b in bases]
if not any(p in REFUSED for p in ports):
write_launch_config(ports) # record, don't recompute
return ports
raise Error("no safe block in this slot")
The comment on the fifth line is the second lesson. Removing a copy in the middle changes the count, so a later copy’s index no longer matches its ports. The list command used to work the ports out from the index, and showed the wrong ones. Now each copy writes its ports into its own launch config when it’s made, and everything else reads them from there.
The tool that starts a copy runs the same check, and refuses to start on a refused port.
Every check outside the browser passes on port 6000. Only the page fails, and it fails somewhere else.
Worktree refs that lie
The bash tool did its job, but nothing else could use it. Its JSON handling was Python inside bash heredocs, which is fine at a prompt and impossible to import. When I wanted a desktop app to make and list copies, I ported the tool to a TypeScript library, and the command line became a thin layer over exported functions.
I didn’t check the port by reading it. Both tools ran against the same fixture repos, and the folders they made were compared byte for byte. They were identical. The registry of copies matched on 25 of 25 entries. The only difference was how a time zone was printed.
The registry is where the next bug was. For each copy it records what the task was: when it was worked on, which branches, how many commits are its own. The obvious question to ask git is git log --all. In a worktree, that answers for every copy at once.
git log --all- feat/login
- fix/grid
- chore/deps
Every copy's branches, from any copy.
git reflog, in stack-a- 09:12 checkout: main → feat/login
Only what this copy checked out, and when.
Worktrees share one store of branches with the original. That’s what makes them cheap, and it means “all branches” is the same list in every copy. What each worktree keeps for itself is its own HEAD, and that HEAD has its own reflog. Reading it gives one copy’s history: which branches it checked out, and when.
Two smaller rules came out of the same work. A commit counts as the copy’s own if it’s reachable from its HEAD and not on the remote’s default branch. And a copy is dirty only for changes the tool didn’t make itself: its own port edits don’t count.
Logging in across two apps
A copy on disk still isn’t a copy I can use. The older app and the newer app each need their own dev server, on different Node versions, both pointed at the same test environment. And the newer app takes its session from the older one. You sign in to the old app, and the session then has to reach the new app’s origin.
By hand that was two terminals, two config edits, a login, and a copy and paste between two sets of dev tools. Now it’s one command, start, with a variant and an environment. It’s the tool I run most: 283 uses in recent shell history, against 79 for the next one.
Step through it, with and without MFA.
Terminal → The tool: start
One command: a variant and a test environment. It checks the ports first, then patches each app's config for that environment.
The browser is the system’s own Chrome, started with a debugging port and a throwaway profile, and the tool drives it through the DevTools Protocol directly. I didn’t use Playwright: there’s no browser to download, and the same port can be shared with the editor’s debugger and my visual regression tool.
The login runs against test environments only, and it stops where a person has to be. If the environment asks for an MFA code or a CAPTCHA, the tool waits and keeps polling for the session to appear. It never tries to get past either.
The failures were at the edges again.
Newer Node versions resolve localhost to ::1 first. A check against one address can wait forever on a server that only listens on the other.
Deploy configs that drop state
Our test environments deploy from a release repo with one branch per environment. Each branch holds a config file: a release number, a list of services, and optional per-service branch overrides. A deploy is a commit to that file. CI does the rest.
My deploy tool makes that edit for me: pick the services, bump the release, show the diff, ask, commit, push. It’s 475 lines of bash, and the /deploy command in Claude Code passes its arguments straight through. That command was the first piece of my agent setup, in April.
The first version wrote the file from scratch on every deploy. It looked tidy, because the same inputs always gave the same file. Then an environment that had been pointed at a long-lived feature branch quietly went back to the default branch for one release. My deploy had rewritten the file without its override.
deploy test-env-2 --services web,worker
- RELEASE=v1.40.12
- SERVICES=api
- BUILD_ALL=false
- DEFAULT_BRANCH=main
- OVERRIDE_WEB=feature/long-lived
- RELEASE=v1.40.13
- SERVICES=web,worker
- BUILD_ALL=false
- DEFAULT_BRANCH=main
- OVERRIDE_WEB=feature/long-lived
The rule is old: read, change, write. Take the file as it is on the branch, change only the lines this deploy is about, and keep the rest.
I haven’t coded that fix yet. For now the rule lives in the agent’s notes: every deploy passes every existing override again, and the diff shows it before anything is pushed. I’d still rather the tool couldn’t get it wrong.
A related one: a bash loop over many branches hung on an auth prompt nobody could see. Bulk git now runs with GIT_TERMINAL_PROMPT=0, SSH in batch mode, and a timeout on every call.
Processes that come back
About 30 copies, each with dev servers, test runners and screenshot runs, leave a lot behind. Activity Monitor showed dozens of rows called node, with no labels and no ports. Killing the biggest one was guesswork, and sometimes it came straight back.
Three processes. Activity Monitor shows only the last one, as "node".
The node process is a leaf. Above it sits a bash -c wrapper, and above that a watcher whose job is to restart it. Kill the leaf and the watcher does its job.
So my process tool works on trees. It reads the whole process table and links every parent to its children, not just the node rows, so a bash or sh in the middle doesn’t cut a tree in two. A tree starts at the first node process with no node ancestor. Each tree gets its listening ports, its summed memory and a role, and kill sends SIGTERM to all of it after a y/N.
The role sets a badge. Dev servers are SAFE. Test runners and screenshot runs are CAUTION, because something may be halfway through. Editor language servers are SYSTEM, so I don’t take down my own editor by accident.
403 lines. The scan lives in its own module, so the desktop app can import it without the terminal UI.
Delete last
A copy holds more than its repos: notes, local config, screenshots from a debugging session. Removing one is the only step in the whole set that can lose work, so it’s the most careful.
Copies also cost disk. In October my laptop was down to 11 GB free out of 494. A report found about 217 GiB I could get back, and 105 GiB of it was one thing: Angular build caches, once per copy. That’s the price of a copy per task, and I hadn’t sized it.
The cleanup script sorts what it finds into two tiers. The first is caches that rebuild themselves. The second is heavier: idle node_modules, old screenshot runs, container images. It skips any copy with a dev server listening, and it deletes nothing without --yes. When it wants a whole copy gone, it calls remove, so the archive step applies there too.
Next time I’d start with the boundaries. Every bug here was at a place where a copy touches something shared: the browser, git’s branch store, another app’s login, a file other people edit, a process something else started. Now I list those places first, and decide what the tool may do at each before I write it.