worktrunk icon indicating copy to clipboard operation
worktrunk copied to clipboard

Branch markers outlive their worktree and branch, so a reused branch name inherits a stale 🤖/💬

Open wsgavin opened this issue 1 month ago • 2 comments

Follow-up to #1733.

wt config state marker set stores the marker as worktrunk.state.<branch>.marker in the shared git config. Nothing removes that key when the worktree or the branch goes away, so a marker left behind by a session that never cleared it stays for good. #1733 was closed on the premise that "this goes away as soon as we remove the worktree, and even if we start a worktree with the same name, it gets reset". On 0.81.0 the key outlives both the worktree and the branch, and a new worktree on the same branch name shows the old marker.

Repro (0.81.0, macOS):

git init -q -b main /tmp/m && cd /tmp/m && git commit -q --allow-empty -m init
git worktree add -q -b feature ../m.feature
wt -C ../m.feature config state marker set 🤖
git worktree remove ../m.feature && git branch -D feature
git config --get-regexp '^worktrunk\.state\.'
# worktrunk.state.feature.marker {"marker":"🤖","set_at":...}   <- still there
git worktree add -q -b feature ../m.feature
wt list
# + feature      _ 🤖   <- the new worktree shows the old marker

This repro removes the worktree with plain git. I did not run wt remove. Reading the v0.81.0 source, though, nothing appears to unset the key on that path either. The marker is written at src/commands/config/state.rs:932-933. Every unset_config call on a worktrunk.state.* key is in src/commands/config/state.rs and runs only from wt config state ... clear (lines 1003, 1020, 1157, 1745, 1759, 1787, 1806). The other callers in src/git/repository/config.rs touch only worktrunk.history*, worktrunk.hints.* and worktrunk.default-branch.

In one repository that uses Claude Code worktrees heavily, git config --get-regexp 'worktrunk\.state\..*\.marker' listed 75 keys, and none of their branches still had a worktree. Its worktree branch names are deterministic (worktree-issue-<n>-<slug>), so a reopened issue inherits whatever marker the last session left.

How markers get stranded in the first place: a Claude Code session whose working directory moves during a turn (EnterWorktree / ExitWorktree) can set 🤖 on one branch at UserPromptSubmit, then send Stop and SessionEnd to another branch. The plugin avoids that by pinning to CLAUDE_PROJECT_DIR (#3956). Hook setups that follow the payload's cwd do not, and killed sessions (#1733) do not either.

Suggestions, smallest first:

  1. Unset worktrunk.state.<branch>.marker (and arguably the whole worktrunk.state.<branch> section) when wt remove deletes the branch, or when it removes the branch's last worktree.
  2. Have wt list skip a marker whose branch no longer exists, or document wt config state marker clear --branch <b> / --all as the cleanup for this case.
  3. Optionally, a wt config state prune that removes worktrunk.state.* sections for branches that no longer exist.

wsgavin avatar Nov 03 '26 22:11 wsgavin

Your repro holds for wt remove too: wt remove --yes feature deleted the branch and left worktrunk.state.feature.marker behind. #4684 is now merged and fixes that path (your suggestion 1). Every way wt deletes a branch, including wt remove, wt merge, and the picker, now also clears that branch's marker. A branch wt keeps also keeps its marker. If a branch has a vars entry named marker whose key happens to match a deleted branch's marker key, that entry is left alone. The new tests cover all of these cases, and CI (including codecov/patch) passed on the merged head.

I've left the issue open for two things:

  • Branches deleted outside wt. Your repro uses git worktree remove and git branch -D. That path still leaves the marker, and so do agent setups that delete branches without wt. Your suggestions 2 and 3 cover it: have wt list ignore a marker whose branch no longer exists, or add a wt config state prune. Both add behavior or a new command, so a maintainer needs to choose. Until then, wt config state marker clear --branch <name> removes a stale marker. --all also works, but it clears active markers along with the stale ones.
  • vars entries. worktrunk.state.<branch>.vars.* entries outlive the branch too. They are user data rather than activity status, so whether deleting a branch should also drop them is a separate decision.

worktrunk-bot avatar Nov 04 '26 00:11 worktrunk-bot

Closing this, because the maintainer has now decided the remaining case. #4704 is merged on main as ea0dbee6c. With it, wt switch --create clears any marker left on the branch it creates. So a reused branch name no longer inherits an old 🤖/💬, even when the earlier branch was deleted with plain git. #4684 already clears the marker on every path where wt deletes a branch.

The maintainer turned down the wt list filter (suggestion 2) when closing #4685: it needed a reflog lookup on every render, including the statusline. They also set what happens to a branch deleted and recreated with plain git: it keeps its marker until wt config state marker clear --branch <name>. The wt config state marker help now says this.

A wt config state prune command (suggestion 3) and dropping vars entries when their branch is deleted are both still open. Each would be a new behavior for a maintainer to approve. If you want either one, reopening or filing a focused request is the way to raise it.

worktrunk-bot avatar Nov 22 '26 09:11 worktrunk-bot