Sessions: deleting a session with a worktree should also delete the worktree
- Dominant language
- TypeScript
- Stars
- 193k
- Forks
- 42.4k
- PR merge metrics
- PR metrics pending
Description
### Description
Archiving a session that runs in an isolated git worktree correctly cleans up the worktree. In `src/vs/platform/agentHost/node/agentSideEffects.ts`:
```ts
case ActionType.SessionIsArchivedChanged: {
// Host-owned worktree lifecycle (agents stay unaware): remove the
// clean, branch-preserved worktree on archive and recreate it on
// unarchive. Serialized per session inside the controller so it can't
// interleave with a first-send worktree resolution.
if (this._worktree) {
const sessionUri = URI.parse(channel);
const sessionId = AgentSession.id(channel);
const worktreeOp = action.isArchived
? this._worktree.cleanupWorktreeOnArchive(sessionUri, sessionId)
: this._worktree.recreateWorktreeOnUnarchive(sessionUri, sessionId);
...
}
...
}
```
`cleanupWorktreeOnArchive` / `recreateWorktreeOnUnarchive` live in `src/vs/platform/agentHost/node/shared/worktreeIsolation.ts` and only get called from the archive/unarchive side effect above.
However, **deleting** a session (`ISessionsManagementService.deleteSession` -> provider `deleteSession`/`deleteSessions`, e.g. `baseAgentHostSessionsProvider.ts` `deleteSessions()` -> `connection.disposeSession(...)` -> `AgentService.disposeSession`) never calls into the worktree cleanup path, so a session's worktree is left behind on disk after the session itself is deleted.
### Suggested fix
When a session with an active worktree is deleted, remove its worktree the same way archiving does, e.g. by calling `this._worktree.cleanupWorktreeOnArchive(...)` (or an equivalent dedicated cleanup) from the session-deletion path in `AgentService`/`agentSideEffects.ts` before/while disposing the session, so no orphaned worktrees remain regardless of whether the user archives or deletes.
### Context
Raised internally:
> rob: How do automation sessions get archived, to clean up the worktree? Seems like they can only be deleted
> Ben Villalobos: currently you can only delete from the view directly. A quick way to do it would be to click into the session itself and click archive from the controls at the top
>
> does a worktree only get deleted on archive and not session deletion?
> rob: Ah I see. It should be cleaned up on deletion too
Contributor guide
Assessment
This issue has not been assessed yet.