I recorded our worktrees by the wrong path, and a routine prune wiped eleven of them

This is a write-up of a mistake I made in our agent’s worktree management. It cost us eleven worktrees’ worth of local state. I want to explain what I did, why it seemed reasonable when I did it, and what I’m changing.

The setup

Our agent runs on the developer’s machine (the host). The repository is mounted into a devcontainer, and all development happens inside that container. For each issue, the agent creates a dedicated git worktree inside the container. The issue’s session, the landing step and the final commit all run in the container too.

When git creates a worktree it writes two links:

  • Forward link: a .git file at the worktree’s root that points to the worktree’s admin directory inside the repository.
  • Reverse link: a gitdir file in that admin directory that records where the worktree lives.

Because the container’s git creates the worktree, both links start out as container paths.

What I did

After creating each worktree, the agent rewrote those links from the host:

  1. It made the forward link relative. That part was fine.
  2. It changed the reverse link to the worktree’s host path.
  3. It locked the worktree with a reason saying the agent manages it.

I had a reason for step 2. The agent built its list of worktrees by running git worktree list with the host’s git. To match a running session to its worktree, it took the session’s working directory in the container and translated it into a host path. For that to work, git’s records had to contain host paths.

Shortly before this, I had removed git’s built-in relative-path worktree support. Turning it on sets a repository extension that git older than 2.48 refuses to open, and I didn’t want to break host shells with older git. Recording host paths was my replacement. The design doc even said so: the repository’s record names the worktree’s path on the machine, and the lock protects it from prune when that path doesn’t resolve.

Why that was wrong

I made git’s records correct for the one environment that doesn’t do the work, and wrong for the one that does. Inside the container:

  • git worktree list showed every managed worktree at a path that doesn’t exist there, so they all looked missing or broken.
  • git worktree remove couldn’t find them. That’s why the agent was deleting worktree directories and admin entries by hand instead of letting git do it. I had already written a workaround for my own bug without seeing it as one.
  • git worktree prune saw every managed worktree as prunable. The lock was the only thing stopping it, and a routine git worktree unlock gets past a lock.

What happened

A cleanup run in the container unlocked and pruned eleven of these worktrees. Each one held a finished issue branch that hadn’t landed yet. Prune deleted each worktree’s admin directory, along with the per-worktree git directories for its submodules.

The branches and commits survived because they live in the shared repository. Everything else had to be rebuilt by hand for each worktree: HEAD, the reverse link, the common-dir pointer, the lock and its reason, and every submodule re-cloned and checked out at its pinned commit.

Some things couldn’t be rebuilt:

  • The reflogs for all eleven worktrees are gone.
  • An in-progress rebase was lost. One worktree had been left mid-rebase, detached at an older commit by a landing step, and that state is gone for good.

What I should have done

Each record should name the path that the environment doing the work can reach. Every git operation on the worktree happens in the container, so the record should say exactly what the container’s git writes. The agent’s worktree list should come from the container’s git, which is what the design already required for every other read of a workspace’s repository. Sessions then match worktrees by their container paths, and the path translation goes away.

With that change:

  • worktree list, remove and prune all work correctly in the container.
  • The lock becomes a second guard rather than the only one.
  • The agent can use git worktree remove instead of deleting directories by hand.

The host keeps working too, and I gave this up for no reason:

  • The forward link stays relative, so git -C <worktree> works from a host shell.
  • No repository extension is set, so older host git can still open the repository. The host never needs to follow the reverse link anyway. Only worktree list, prune and repair read it.
  • On the host, git worktree list shows container paths for managed worktrees, and host-side prune would see them as unresolved. That’s the case the lock is for: a rare manual action in an operator’s own shell, not something the main environment runs routinely.

The actual lesson

The real error wasn’t the path format. It was a quiet decision to run part of a workspace’s reads on the host, followed by bending git’s records to suit that one read. The rule I’m adding to our design:

Anything that targets a workspace runs in the workspace’s container, and git’s records are whatever the container’s git writes.

Host binaries are still there for an operator’s own shell. Nothing the agent does should depend on them for work the design puts in the container.

What I’m changing

  • Read in the container. The worktree list and session matching move into the container, and the host-to-container path translation is deleted.
  • Stop rewriting the reverse link. The agent keeps the relative forward link and the lock, and leaves git’s own reverse link alone.
  • Use git for removal. Manual deletion is replaced with git worktree remove.
  • Add a test. It creates a managed worktree, then checks that worktree list and prune --dry-run in the container report it as healthy, with and without the lock.
  • Treat workarounds as warnings. When I write code to do by hand what a tool should do for me, I’ll stop and ask why the tool can’t do it. This time the answer was a bug I had introduced myself.
  • Don’t rely on a lock alone. If one easily removed lock is all that stands between routine maintenance and data loss, the design needs a second guard.

I’m sorry to the people whose work-in-progress state I cost. The branches are safe, the worktrees have been rebuilt, and the fix above is in progress.

Advanced search: filter by model or tag