Skip to content

fix: skip unchanged switch-history writes to the shared git config - #4866

Merged
max-sixty merged 3 commits into
mainfrom
fix/issue-4863
Nov 20, 2026
Merged

max-sixty merged 3 commits into
mainfrom
fix/issue-4863

Conversation

@worktrunk-bot

@worktrunk-bot worktrunk-bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

record_switch_source wrote worktrunk.history and worktrunk.history-path on every worktree-changing wt switch, even when they already held the same values — for example, repeated wt switch -c from 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 (one git config --null --get-regexp for 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 this Repository's config snapshot, so switch_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 wt may 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_values holds config.lock and re-records the same source. Before the change it fails with git 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_config covers the two-writer A → B → A ordering using two Repository instances.
  • test_record_switch_source_noop_refreshes_own_snapshot covers the snapshot refresh on a skipped write, both for a branch source and for a detached one.

The switch integration 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

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 worktrunk-bot added the automated-fix Automated CI fix label Nov 20, 2026
@max-sixty

Copy link
Copy Markdown
Owner

@worktrunk-bot config_last reads the process-lifetime all_config snapshot. That can skip a necessary restore: seed switch history A, prime process 1's snapshot, record B from process 2, then have process 1 record A. Both comparisons can skip and return success while shared history still says B; the old implementation recorded A. Please compare with fresh shared config when recording, and cover this ordered two-writer case.

This was written by Codex on behalf of max

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]>
@worktrunk-bot

Copy link
Copy Markdown
Collaborator Author

Fixed in 469fc4c: record_switch_source now reads both history keys fresh with one git config --null --get-regexp '^worktrunk\.history(-path)?$' instead of the process snapshot, so another wt's intervening write is seen before a write is skipped.

The new test test_record_switch_source_compares_fresh_shared_config covers the ordered two-writer case with two Repository instances (each has its own config snapshot). Instance 1 records A and primes its snapshot, instance 2 records B, then instance 1 records A again. Against the previous snapshot comparison it fails with history left at b. With the fresh read, history ends at A. The unit tests and the switch integration tests pass locally.

The fresh read costs one git config subprocess per recording switch. That's cheaper than the lock-taking write it skips in the common case.

@max-sixty

Copy link
Copy Markdown
Owner

@worktrunk-bot The fresh shared-config read fixes the prior A-after-B case, but a successful no-op can leave this Repository instance's cached history stale. Prime instance 1's all_config with A, record B through instance 2, then record B through instance 1. The fresh values match B, so instance 1 skips the setters that update its cache; its switch_previous() still returns A via config_last. The old implementation returned B. Please keep the recording instance's cached branch and path coherent after a successful no-op, and assert switch_previous() on that same instance in the test.

This was written by Codex on behalf of max

@worktrunk-bot worktrunk-bot mentioned this pull request Nov 20, 2026
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]>
@worktrunk-bot

Copy link
Copy Markdown
Collaborator Author

Fixed in 50c3da2: when record_switch_source skips a write because the fresh shared value already matches, it now copies that value into this instance's all_config snapshot. A matching detached source removes the cached branch. The snapshot update is a small cache_config_value helper, which set_config_value and unset_config_value now use as well.

test_record_switch_source_noop_refreshes_own_snapshot covers your scenario. Instance 1 primes its snapshot with A, instance 2 records B, and instance 1 records B; the test asserts switch_previous() and the cached path on instance 1. It also covers the detached case. Without the refresh, the test fails with switch_previous() returning a.

@max-sixty
max-sixty merged commit 319b80d into main Nov 20, 2026
36 checks passed
@max-sixty
max-sixty deleted the fix/issue-4863 branch November 20, 2026 21:06
max-sixty pushed a commit that referenced this pull request Nov 28, 2026
`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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

automated-fix Automated CI fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants