Repository navigation
fix: skip unchanged switch-history writes to the shared git config - #4866
Conversation
A switch from the same worktree as last time rewrote worktrunk.history and worktrunk.history-path with identical values, taking config.lock each time and failing any concurrent config writer in another worktree. Each key is now written only when its value changes. Refs #4863
|
@worktrunk-bot
|
The process-lifetime config snapshot can be outdated by another wt that recorded a different source after this process loaded it, so comparing against it could skip a necessary restore. Read the two history keys fresh before deciding whether to write. Co-Authored-By: Claude <[email protected]>
|
Fixed in 469fc4c: The new test The fresh read costs one |
|
@worktrunk-bot The fresh shared-config read fixes the prior A-after-B case, but a successful no-op can leave this
|
When another process already recorded the same switch source, the skipped write left this Repository's bulk config map holding the older value, so a later switch_previous() in the same process returned it. Mirror the fresh value into the snapshot whenever a write is skipped. Co-Authored-By: Claude <[email protected]>
|
Fixed in 50c3da2: when
|
`wt switch` recorded the worktree it left in `worktrunk.history` and `worktrunk.history-path` in the repository's shared `.git/config`. #4866 skipped unchanged writes, but every changed value still took `config.lock`, so switches in parallel worktrees could fail concurrent config writers elsewhere. Switch history now lives in `.git/wt/switch-history.json`, replaced atomically through `utils::write_atomically` without any lock. Branch and path are written together, so they can no longer disagree. The `Repository` API (`record_switch_source`, `set_switch_previous`, `clear_switch_previous`, `switch_previous`, `switch_previous_worktree`) keeps its signatures, so callers are unchanged. Values recorded in git config by older versions are no longer read: the first `wt switch -` after upgrading finds no history, and the next switch records it again. Tests: the three `config.lock` unit tests from #4866 are replaced by one that holds `config.lock` while recording, reading and clearing history, and asserts `.git/config` is byte-identical afterwards. Integration tests that seeded history with `git config worktrunk.history` now use `TestRepo::set_switch_previous`. Closes #4863 --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: worktrunk-bot <[email protected]>
record_switch_sourcewroteworktrunk.historyandworktrunk.history-pathon every worktree-changingwt switch, even when they already held the same values — for example, repeatedwt switch -cfrom the primary worktree. Each write takes the shared.git/config.lock, so a concurrent config writer in another worktree could fail on the lock. Each key is now compared against a fresh read of the shared config (onegit config --null --get-regexpfor both keys) and written, or unset for a detached source, only when it changes. When the write is skipped, the fresh value is still copied into thisRepository's config snapshot, soswitch_previous()on the same instance agrees with what's on disk.The fresh read matters because the process-lifetime snapshot can be out of date: another
wtmay have recorded a different source since this process loaded its config, and comparing against the snapshot would skip a restore that's needed.Three unit tests cover this:
test_record_switch_source_skips_unchanged_valuesholdsconfig.lockand re-records the same source. Before the change it fails withgit config worktrunk.history main failed (exit code 255); after it, the call succeeds. A changed value still attempts the write.test_record_switch_source_compares_fresh_shared_configcovers the two-writer A → B → A ordering using twoRepositoryinstances.test_record_switch_source_noop_refreshes_own_snapshotcovers the snapshot refresh on a skipped write, both for a branch source and for a detached one.The
switchintegration tests pass locally.What this leaves: a switch that does change the source (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 and is a maintainer decision.Refs #4863 — automated triage