Skip to content

feat(worktrees): run actions before removal - #9337

Closed
bradymwilliams wants to merge 3 commits into
pingdotgg:mainfrom
bradymwilliams:t3code/add-worktree-lifecycle-steps
Closed

bradymwilliams wants to merge 3 commits into
pingdotgg:mainfrom
bradymwilliams:t3code/add-worktree-lifecycle-steps

Conversation

@bradymwilliams

@bradymwilliams bradymwilliams commented Sep 3, 2026 •

Copy link
Copy Markdown

Projects can run an action when a worktree is created, but they have no matching hook before removal. Users must run cleanup commands by hand, and worktree deletion can leave generated resources behind.

This change adds one optional teardown action per project. T3 runs it in the worktree before removal and stops the removal when the command fails. The action editor, shared contracts, mobile labels, user documentation, and focused tests now include the new lifecycle step.

Safety model

  • Teardown is off by default and uses the existing project-action command trust boundary.
  • T3 runs at most one teardown action, only after the user requests worktree removal, and only while the worktree directory exists.
  • The existing process runner limits execution to 60 seconds and captured output to 8 MiB.
  • Spawn errors, timeouts, output-limit errors, and nonzero exits stop removal and return a structured error.
  • If the directory is already gone, T3 skips teardown and keeps the Git driver path that prunes stale worktree registrations.

Tests

  • 33 focused tests across 5 test files
  • Workflow coverage proves teardown runs before removal, missing directories skip teardown, and teardown failure prevents the Git removal call
  • Driver integration coverage proves stale worktree registrations are pruned
  • Type checks for server, web, mobile, contracts, and shared packages
  • Targeted lint, formatting, and diff checks
  • Manual web verification of setup and teardown behavior

Created with GPT-5.6 Sol using the Codex harness in T3 Code.


Note

Medium Risk
Worktree removal now executes configured shell commands and aborts removal on failure, which affects cleanup workflows and could leave worktrees in place if teardown errors.

Overview
Adds an optional teardown lifecycle for project actions, symmetric to existing setup-on-create behavior. Scripts can be marked run automatically before worktree removal in the web editor, settings, and t3.json; only one setup and one teardown action are kept at a time via clearProjectScriptLifecycleConflicts.

Server: ProjectSetupScriptRunner gains runBeforeWorktreeRemove, which finds the teardown script and runs its command in the worktree (host shell / PowerShell on Windows) with T3CODE_* env vars. GitWorkflowService.removeWorktree runs teardown only when the worktree path still exists, then removes the worktree; a non-zero exit surfaces as GitCommandError with command project teardown script and blocks deletion. Wiring adds ProcessRunner to the script runner layer.

Clients & contracts: Optional runOnWorktreeRemove on ProjectScript / file schema; mobile and web menus show (teardown) / badges. User docs describe setup/teardown and failure behavior. Tests cover runner, workflow ordering, and lifecycle helpers.

Reviewed by Cursor Bugbot for commit c00a042. Bugbot is set up for automated code reviews on this repo. Configure here.

Note

Add teardown scripts before worktree removal in GitWorkflowService

  • GitWorkflowService.removeWorktree now runs the configured project teardown script before invoking VCS removal for existing worktree paths; missing paths skip teardown and proceed to removal as before.
  • ProjectSetupScriptRunner gains runBeforeWorktreeRemove, which executes the selected teardown script in the worktree using PowerShell on win32 or the configured login shell elsewhere, and fails on process errors or nonzero exit codes.
  • The web editor, project settings, and import paths add a runOnWorktreeRemove flag with badges, lifecycle-conflict clearing that keeps setup and teardown independent, and primary-script selection that excludes lifecycle scripts.
  • Contracts add an optional runOnWorktreeRemove to ProjectScript and T3ProjectFileScript; older snapshots and project files decode with the field absent.
  • Behavioral Change: removeWorktree aborts VCS removal and returns a GitCommandError when teardown fails; reviewers should check GitWorkflowService.ts and ProjectSetupScriptRunner.ts for error mapping of nonzero exit codes.

Macroscope summarized c00a042.

@github-actions github-actions Bot added vouch:unvouched Author is not yet trusted in the VOUCHED list. size:L 100-499 changed lines (additions + deletions). labels Sep 3, 2026
@bradymwilliams

Copy link
Copy Markdown
Author

UI evidence

Before:

Action editor before the teardown option

After:

Action editor with the worktree removal option

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Want fixes drafted automatically? Bugbot Autofix can create code changes for findings. A team admin can enable Autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit f029b24. Configure here.

@macroscopeapp

macroscopeapp Bot commented Sep 3, 2026 •

Copy link
Copy Markdown
Contributor

Approvability

Verdict: Not approved

Macroscope's review found this PR not approvable — This PR adds an opt-in teardown workflow that executes configured shell commands and can block worktree deletion, alongside cross-cutting UI, contract, and server changes. The new process-execution and removal semantics warrant human review.

Notes:

  • No code objects were reviewed. Approvability was decided on eligibility alone.

You can add or adjust custom eligibility rules. Learn more.

@timschoch

Copy link
Copy Markdown

+1

Are we going to see this land sometime soon? I'm in the process of switching over from superset and this is driving me crazy. On wt up I start a docker container so each agent can work on their isolated db. without the teardown, these containers keep piling up and at some point my macbook just freezes. Because of this, I am sticking with superset for those repos, but I really would like to make the move

@juliusmarminge

Copy link
Copy Markdown
Member

Thanks for the PR. We're not taking changes to the orchestration and provider layers right now: that part of the server is being rewritten for V2, and merging into the current code would either conflict with or be thrown away by that work.

Closing for now. If this is still an issue once V2 lands, please reopen (or open a fresh PR against the new code) and we'll take a proper look.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:L 100-499 changed lines (additions + deletions). vouch:unvouched Author is not yet trusted in the VOUCHED list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants