`wt switch` writes switch history to the shared .git/config on every switch, racing parallel worktrees
Every wt switch that moves to another worktree calls record_switch_source, which runs git config worktrunk.history <branch> (or unsets it) and git config worktrunk.history-path <path>. Both write the repository's shared .git/config, and they write even when the values are unchanged.
With several agents switching in parallel worktrees, these writes contend for .git/config.lock with each other and with any other command that writes config. A concurrent writer then fails on the lock. otto-nation/otto-workbench#2019 hit this: "Its red check was a worktrunk history-path write to the shared git config, made by a concurrent command in another worktree."
Callers are switch.rs L1171 and L1476; both discard the error, so a lost race on Worktrunk's side is silent, but the other writer still sees the lock. Skipping the write when the cached values already match would remove the redundant writes; moving switch history out of the shared config would remove the contention entirely. #4683 covers a different shared-config write.
Found by the feedback digest (#4813).
#4866 now skips a switch-history write when the shared config already holds the same values. Repeated switches from the same worktree, such as successive wt switch -c from the primary worktree, no longer take .git/config.lock. A regression test holds the lock and re-records the same source; on the old code it fails with git config worktrunk.history main failed (exit code 255).
After maintainer review, the comparison now reads the two history keys fresh rather than from the process's config snapshot, so a value another wt wrote in the meantime still gets overwritten. CI on that head is still running, and the follow-up session that pushed it is handling it.
The PR settles only part of this issue, so it uses Refs. A switch that actually changes the source, like ping-pong between worktrees or agents switching from different worktrees, still writes the shared config. Removing that contention means moving switch history out of .git/config, which changes where existing state lives. That is a maintainer decision.
@worktrunk-bot please move switch history out of the shared .git/config: store it in a file under .git/wt/, written atomically, drop the worktrunk.history and worktrunk.history-path git config keys, and list the new file in the FAQ's table of files worktrunk writes. That ends the config.lock contention between parallel worktrees.
This was written by Claude Code on behalf of max-sixty
@Oseenix's #4928 already implements this design: history lives in .git/wt/switch-history.json, written with utils::write_atomically, and the FAQ table lists the new file. So I finished that PR rather than opening a second one. The contributor allowed maintainer edits, and I pushed e69c4720590d941288fcee58f6286f6e08a20d59 there:
wt config state clearandprevious-branch clearnow unset leftoverworktrunk.history/worktrunk.history-pathkeys from older versions. Nothing else removes them. Each key is checked in the config snapshot first, so a repo without them never takesconfig.lock.- New unit tests cover that cleanup and the read/remove error paths. Those error paths were the six lines failing
codecov/patch. - Fixed a stale test comment that still described the git-config storage.
Old history isn't migrated. The first wt switch - after upgrading finds nothing, and the next switch records it again.