# fix\(server\): clean up deleted threads worktrees with subagents · PR \#23900 · pingdotgg/t3code

[View on GitCafe](https://git.cafe/pingdotgg/t3code/pull/23900)

Repository: [pingdotgg/t3code](https://git.cafe/pingdotgg/t3code)

Visibility: public

State: merged

Source repository: pingdotgg/t3code

Source branch: fix/issue-17812

Target branch: main

Head commit: ad34e6c92ce601be54aff7c5c22901c8100503e9

[Discussion and reviews](https://git.cafe/pingdotgg/t3code/pull/23900)

[Commits](https://git.cafe/pingdotgg/t3code/pull/23900/commits?format=markdown)

[Files changed](https://git.cafe/pingdotgg/t3code/pull/23900/diffs?format=markdown)

## Description

---

## Problem

With only **Delete worktrees with deleted threads** enabled, deleting a thread that used a subagent leaves its worktree behind. Reproduced on Linux in the web app with a completed Codex native subagent: cleanup attributed the checkout to the hidden child and kept it after the parent was deleted.

## Change

Recognize a deleted thread as the worktree owner when its remaining users form its subagent chain and its project's deletion cleanup rule is enabled. Apply the existing activity, settle grace, sharing, and final revalidation checks to those children. This also releases existing orphaned worktrees while preserving other cleanup rules when deletion cleanup is off.

## Scope and approval

Fixes #17812

Triaged bug via the automated issue flow: [#17812](https://git.cafe/pingdotgg/t3code/issues/17812).

## Verification

- Red-first regression: native-child case failed with `kept / Has commits beyond the default branch`; no-child control passed. [Red test record](https://gh-file-drop-api-prod-mi5fy3sowv63ufte.pinglabs.workers.dev/f/6d4eb262a48c27d3/red-test-record.txt).
- `vp test run apps/server/src/storageCleanup.test.ts`: **122 passed**, including deletion-off age cleanup and subagent settle grace; rerun after merging main.
- Scoped lint on both changed files and server `tsc --noEmit -p .`: passed.
- Two independent reviewers returned MERGE after the follow-up correction; proof audit returned SUFFICIENT.

| Evidence | Before | After |
| --- | --- | --- |
| Existing orphan, deletion-only rule | [Runtime transcript](https://gh-file-drop-api-prod-mi5fy3sowv63ufte.pinglabs.workers.dev/f/eac14fd450c07c84/issue-17812-before.txt): checkout retained under native child | [Runtime transcript](https://gh-file-drop-api-prod-mi5fy3sowv63ufte.pinglabs.workers.dev/f/e7b623023d7661a5/after-runtime.txt), [UI report text](https://gh-file-drop-api-prod-mi5fy3sowv63ufte.pinglabs.workers.dev/f/cb2e478c1c21ecff/after-ui.txt): 1 removed, reason `Thread was deleted`; directory and Git worktree registration absent |
| Revision | [200dad3b39](https://github.com/pingdotgg/t3code/commit/200dad3b39) | [ad34e6c92c](https://github.com/pingdotgg/t3code/commit/ad34e6c92ce601be54aff7c5c22901c8100503e9) |

Server-only change; render fixture matrix is empty. Runtime verification used the real web client against isolated state. The copied fixture's divergent migration metadata was repaired before running current main; thread and event data were preserved, as disclosed in the transcript. No platform-specific code changed.

Models: GPT-6.1-Sol (Codex harness) and Claude Opus 5.5 (Claude Agent harness), orchestrated through T3 Code.

