Skip to content

afx cleanup: add a flag (--full) to also remove worktree + delete branch #852

Open

Description

@amrmelsayed

Problem

afx cleanup -p <id> only kills the Tower terminal and unregisters the builder from the agent farm. The worktree and branch are deliberately preserved, with the cleanup output printing the manual follow-up commands:

[info] Worktree preserved at: /Users/.../.builders/pir-XXXX
[info] To remove: git worktree remove "/Users/.../.builders/pir-XXXX"
[info] Branch preserved: builder/pir-XXXX
[info] To delete: git branch -d "builder/pir-XXXX"

The full teardown sequence is always three steps:

afx cleanup -p <id>
git worktree remove --force .builders/<branch>
git branch -d builder/<branch>

The help text — "Clean up a builder worktree and branch" — describes the aspiration of the command, not its actual behavior. There's a small footgun here: a reader who's only seen the help text will assume the command does what it says, then discover halfway through cleanup that they need two more commands.

Why preservation is the right default

This isn't an argument to change the default. Today's behavior protects two real cases that hit us twice in one Shannon session (2026-05-25):

Both times the architect agent paused to inspect the scratch before removing. That's exactly the discipline preservation enforces — if cleanup removed everything in one step, mid-iteration scratch would be silently lost.

Proposed change

Add an opt-in flag that does the full teardown in one command, after explicit acknowledgment:

afx cleanup -p <id> --full
# or: --remove-worktree --delete-branch (composable)

Suggested behavior:

  1. --full (or both --remove-worktree + --delete-branch): perform the existing cleanup, then run git worktree remove and git branch -d in sequence.
  2. If the worktree has uncommitted/untracked changes when --full is invoked:
    • Refuse by default with the existing "To remove" hint AND a one-line listing of which file(s) are uncommitted, so the user can decide whether to inspect.
    • --full --force (or --full -f) overrides — runs git worktree remove --force.
  3. If the branch hasn't been merged into the current HEAD when --full is invoked:
    • Refuse by default, point the user at the unmerged commit list.
    • Re-use the existing -f, --force flag for branch deletion (currently scoped to "branch not merged").
  4. Keep the existing default (no flag) preserving both, exactly as today.

The composable form (--remove-worktree / --delete-branch) is nice because some workflows want to keep the branch around for a while after the worktree is gone (e.g., GitHub PR archived for reference, local worktree clutter removed).

Out of scope

  • Changing the existing default (still preserve unless asked).
  • Auto-discarding uncommitted scratch (the value of preservation IS the human-in-the-loop decision on what's scratch vs work).
  • Touching the -f, --force semantics for the "branch not merged" case — leave that as-is to avoid regressing today's safety guard.

Help text fix (separate, can ride along)

Independently of whether --full lands, the current help text overpromises:

Clean up a builder worktree and branch

A more accurate one-liner would be:

Stop the builder; print teardown commands for worktree + branch (use --full to also run them)

…or whatever phrasing fits once the flag lands.

References

Unassigned — for the team to discuss the right flag shape and default behavior.

Activity

  1. waleedkadous commented on May 25, 2026

    @waleedkadous
    Contributor
  2. amrmelsayed commented on May 27, 2026

    @amrmelsayed
    CollaboratorAuthor

    Agree with @waleedkadous 's read — drop --full

    --full adds nothing the composable form doesn't already provide, and it has one structural downside worth flagging: it doesn't compose with future additions. If a third teardown step is ever added (cleanup orphan terminal_sessions rows in ~/.agent-farm/global.db, prune builder thread logs in codev/state/, etc.), --full becomes semantically ambiguous — does it include the new thing or not? Either choice is a footgun. Pure composable flags don't have this problem; they just gain a third sibling.

    Two slightly different shapes worth picking between:

    Form 1 — pure composable, two flags:

    afx cleanup -p <id>                                    # today's behavior
    afx cleanup -p <id> --remove-worktree                  # + git worktree remove
    afx cleanup -p <id> --delete-branch                    # + git branch -d
    afx cleanup -p <id> --remove-worktree --delete-branch  # full teardown

    Form 2 — one flag (questions whether "delete branch, keep worktree" is real):

    afx cleanup -p <id>                       # today
    afx cleanup -p <id> --remove-worktree     # + git worktree remove
                                              # branch deletion stays a deliberate `git branch -d` step

    The issue body argues against Form 2 by citing "some workflows want to keep the branch around for a while after the worktree is gone" — but that's exactly what Form 2 does by default. The reverse case (delete branch but keep worktree) is the weird one that needs justifying. If nobody actually wants that, Form 2 has the smallest possible surface.

    Independent: ship the help-text fix now

    The help-text fix ("Clean up a builder worktree and branch" → something that matches actual behaviour) is independent of the flag design and could land as a trivial PR today. That's the most direct footgun for new users — they read the help, type the command, then discover halfway through cleanup that they need two more commands. Fixing the docs doesn't wait on the design discussion.

    Issue-body cleanup observation

    Per house convention, codev issues should be framed in codev's own terms — the current body references specific external-repo PR numbers (in the "References" section and the inline examples) which aren't meaningful to a future reader of this codev issue. A rewrite that keeps the motivation ("two consecutive cases where worktrees held uncommitted scratch worth inspecting before deletion") without naming the external repo or its PR numbers would generalize cleanly.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions