Skip to content

Prevent archival of manually-created worktrees - #58275

Merged
rtfeldman merged 4 commits into
mainfrom
AI-352/worktree-archive-provenance
Jun 16, 2026
Merged

Prevent archival of manually-created worktrees#58275
rtfeldman merged 4 commits into
mainfrom
AI-352/worktree-archive-provenance

Conversation

@rtfeldman

@rtfeldman rtfeldman commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

This fixes archive cleanup for agent threads so Zed only removes worktrees it explicitly created, rather than treating every linked worktree under the configured managed directory as safe to delete. When Zed creates a worktree, it now records it in the local database along with the creation time of the worktree's git metadata directory (.git/worktrees/<name>/). Archive planning requires that record, and right before deleting anything, Zed re-stats the directory and compares creation times: if the worktree was removed and recreated outside Zed (or the time can't be read at all), deletion is skipped and the stale record is dropped. Every failure mode fails safe by leaving the directory untouched.

For remote (SSH) projects, the stat runs on the remote host via a new GitWorktreeCreatedAt request. Worktrees created by a different Zed install (another release channel, or another machine connecting to the same host) have no record in the local database and are therefore never auto-archived, which is the intended conservative behavior.

Closes #57155

Closes AI-352

Release Notes:

  • Fixed archiving an agent thread incorrectly deleting manually-created git worktrees.

@rtfeldman rtfeldman self-assigned this Jun 1, 2026
@cla-bot cla-bot Bot added the cla-signed The user has signed the Contributor License Agreement label Jun 1, 2026
@zed-community-bot zed-community-bot Bot added the staff Pull requests authored by a current member of Zed staff label Jun 1, 2026
@rtfeldman
rtfeldman force-pushed the AI-352/worktree-archive-provenance branch 2 times, most recently from 9259a21 to 7422d1c Compare June 1, 2026 22:01
@rtfeldman
rtfeldman force-pushed the AI-352/worktree-archive-provenance branch from 7422d1c to aec08a8 Compare June 1, 2026 22:15
@mwja

mwja commented Jun 2, 2026

Copy link
Copy Markdown

Just a question, would this mark worktrees created from the interface but unrelated to an agent chat?

I create my worktrees within Zed a lot of the time, but I wouldn't personally want them to be archived when closing an agent chat as they weren't created for that agent.

Or is this case too niche?


Thanks for the hard work! You guys are making my life better every day

Instead of writing a marker file into the worktree's git metadata
directory, record each Zed-created worktree in the local database along
with the creation time of its gitdir. Before deleting a worktree during
thread archival, re-stat the gitdir and refuse to delete if the time
doesn't match (the worktree was recreated outside Zed) or can't be read.
All failure modes leave the directory untouched.

This also replaces the is_zed_managed_worktree snapshot/proto plumbing
with a GitWorktreeCreatedAt request so the stat runs on the remote host
for SSH projects.
@rtfeldman
rtfeldman marked this pull request as ready for review June 16, 2026 14:27
@rtfeldman
rtfeldman added this pull request to the merge queue Jun 16, 2026
Merged via the queue into main with commit 2962291 Jun 16, 2026
34 checks passed
@rtfeldman
rtfeldman deleted the AI-352/worktree-archive-provenance branch June 16, 2026 15:39
This was referenced Jun 18, 2026
jolutz pushed a commit to jolutz/zed that referenced this pull request Aug 8, 2026
This fixes archive cleanup for agent threads so Zed only removes
worktrees it explicitly created, rather than treating every linked
worktree under the configured managed directory as safe to delete. When
Zed creates a worktree, it now records it in the local database along
with the creation time of the worktree's git metadata directory
(`.git/worktrees/<name>/`). Archive planning requires that record, and
right before deleting anything, Zed re-stats the directory and compares
creation times: if the worktree was removed and recreated outside Zed
(or the time can't be read at all), deletion is skipped and the stale
record is dropped. Every failure mode fails safe by leaving the
directory untouched.

For remote (SSH) projects, the stat runs on the remote host via a new
`GitWorktreeCreatedAt` request. Worktrees created by a different Zed
install (another release channel, or another machine connecting to the
same host) have no record in the local database and are therefore never
auto-archived, which is the intended conservative behavior.

Closes zed-industries#57155

Closes AI-352

Release Notes:

- Fixed archiving an agent thread incorrectly deleting manually-created
git worktrees.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla-signed The user has signed the Contributor License Agreement staff Pull requests authored by a current member of Zed staff

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Manually-created worktrees are deleted when agent conversations are archived

3 participants