Skip to content
Source
Sign inSign up
pingdotgg/t3code

fix(server): deleting a thread also deletes its subagent threads #39200

Closedpalermo-git(GitHub profile, opens in a new tab)wants to merge 4 commits intomain fromrefs/pull/39200/headopened View on GitHub

palermo-git(GitHub profile, opens in a new tab)commented

Problem

thread.delete cascades to the thread's side chats but not to its subagent threads. After the parent is deleted, every child whose lineage says relationshipToParent: "subagent" stays live. It keeps its full history, so the purge in #32378 never reaches it. An unfinished child run, such as one still starting, is not cancelled, although its result can no longer be delivered (#30363: "A deleted parent never comes back to read the result").

To reproduce: start a Codex turn that spawns subagents, then delete the parent thread. The subagent threads stay in the shell snapshot. testkit/ThreadDelete.integration.test.ts replays this with the Codex subagent fixture.

Deleting a project already removes these children, because ProjectService.deleteChildThreads reads every thread in the project, subagent threads included. Only the single-thread delete leaks them. #23900 (worktree cleanup), #30363 (usage-limit auto-resume) and #30370 (the fallback after deleting the open thread) each work around the leftover children. On my install, 8 subagent threads were still live under deleted parents.

Change

  • The delete walks the thread's live side chats and subagent threads breadth first, under the existing deletion lock, so grandchildren are reached too. It takes each thread's lock before reading that thread's members, parent before child as withSubagentParentLock does. Each child goes through the same planThreadDeletion as the parent, so its runs are cancelled. Ids are deduplicated because thread locks are not reentrant. A child deleted before its parent is left as it was.
  • Subagent threads are found by their lineage, not by the parent's subagent records. Codex reports a subagent's app_thread.created before the parent's subagent record, so a delete between the two would miss the child. ProjectionStore.listLiveSubagentThreadIds reads the undeleted threads, archived or not, whose lineage names the thread as their subagent parent. Migration 108 adds the partial index it reads through; subagentThreadIndex.ts holds the expression and predicate that both use.
  • A subagent thread can still be reported after its parent's delete. For a subagent app_thread.created, the ingestor now takes the parent's lock before the child's, in the delete's order. If the parent is already deleted, it writes the child's thread.created and thread.deleted together. The worktree branch correction skips that child, because an update built from the created payload would restore it.
  • Archive and unarchive are unchanged; #33884 covers archive.

Scope

Fixes #39199. Relates to #17845.

#3878 had a subtree cascade, bundled with subtree archive and a startup repair service. It was folded into #3861, which was closed over the sidebar UX. This PR fixes only the delete.

Verification

Each file was run with vp test run from apps/server. "Fails on main" means the test was run against main at a1e7ac2b24 with this PR's server changes reverted.

  • testkit/ThreadDelete.integration.test.ts replays the real Codex subagent fixture, then deletes the parent. It fails on main with both subagent children still live; with this PR the shell snapshot is empty.
  • runtimeLayer.test.ts:
    • "deletes a parent's subagent threads and theirs with it" (a child with an unfinished run, and a grandchild): fails on main. Here it passes, and the child's run ends cancelled.
    • "keeps a thread that a parent's subagent record names but that is not its subagent": fails on main, passes here.
    • "deletes a subagent thread that two of its parent's subagent records name": fails on main, passes here.
    • "deletes subagent threads that have no subagent record on their parent" (one live, one archived): fails on main, passes here.
    • "leaves a subagent thread deleted before its parent as it was": passes on both.
  • ProviderEventIngestor.test.ts:
    • "records a subagent thread of a deleted parent as deleted": fails on main, where only thread.created is written. Its checkout branch was renamed, so it also covers the branch correction.
    • "holds a subagent thread's parent until the thread is committed": fails on main, where only the child is locked.
  • ProjectionStore.test.ts, "lists a thread's undeleted subagent threads by lineage through their index": returns the live and archived subagent threads only, not a deleted one, a fork or another thread's subagent. EXPLAIN QUERY PLAN shows the read searching the new index.
  • Each guard was removed in turn, and its test failed: the parent lock, the born-deleted child, the branch correction skip, the lineage lookup, the walk into grandchildren, the deleted_at filter, and an index predicate that differs from the read's.
  • runtimeLayer, ProviderEventIngestor, ProjectionStore, ProviderTurnControlService, ThreadDelete.integration, Migrations, reconcileV2PreviewMigration and Sqlite tests: 8 files, 320 tests pass.
  • vp run check:changed passes: format, lint, knip, typecheck, and 11 test files with 351 tests.

Made with Claude Opus 5.5 in Claude Code.

    1. Pushed8311200

    2. This PR conflicts with main. Rebase it or merge main into it. If it still conflicts after 7 days with no activity, it will be closed automatically.

      See the conflict window.

    3. Closing: this PR still conflicts with main and has had no activity for 7 days since the rebase request.

      To continue, rebase the branch and open a new PR that links this one. See the conflict window.

Sign in to join this conversation.