Add session-owned worktree cleanup and TTL enforcement for branches directory #401

Closed
opened 2026-07-07 09:36:43 -05:00 by jcwalker3 · 2 comments
Owner

Problem:
LLM workflows create many session-owned worktrees under branches/, including review worktrees, baseline worktrees, merge-simulation worktrees, issue worktrees, and conflict-fix worktrees. These are not always cleaned up, especially when a run stops early, races another session, hits a validation failure, or loses shell/cwd state. Over time, branches/ fills with stale folders and makes later workflow decisions harder and riskier.

Required behavior:
Every workflow that creates a worktree must register ownership, purpose, target issue/PR, expected branch/head SHA, creation time, and cleanup policy. Completed clean worktrees should be removed automatically or flagged for safe cleanup. Dirty, active, or leased worktrees must be preserved and reported.

Acceptance criteria:

  1. Every session-owned worktree under branches/ must have metadata:

    • path,
    • workflow type,
    • issue number if applicable,
    • PR number if applicable,
    • branch/head SHA,
    • creator identity/profile,
    • created timestamp,
    • last-used timestamp,
    • cleanup eligibility.
  2. Review/baseline/merge-simulation worktrees are removed automatically at successful workflow completion.

  3. If cleanup fails, the final report must list the leftover worktree and reason.

  4. A cleanup audit command classifies all branches/ entries as:

    • active open PR,
    • active issue work,
    • dirty local worktree,
    • clean stale removable,
    • detached review leftover,
    • unsafe/unknown.
  5. Clean stale removable worktrees can be removed one-by-one after verification.

  6. Dirty worktrees are never deleted automatically.

  7. Active PR/review worktrees are never deleted automatically.

  8. Worktrees with active leases are never deleted automatically.

  9. Cleanup must prove:

    • worktree path,
    • branch/HEAD,
    • clean/dirty status,
    • no active PR/lease,
    • removal result.
  10. Final verification must include git worktree list or equivalent.

  11. Tests cover:

  • successful review cleanup,
  • failed review leaving worktree reported,
  • dirty worktree preservation,
  • active PR worktree preservation,
  • stale clean worktree removal,
  • detached review worktree classification,
  • TTL-expired but dirty worktree,
  • lease-protected worktree,
  • cleanup report accuracy.
  1. Documentation updates all workflows that create worktrees:
  • review-merge-pr,
  • work-issue,
  • conflict-fix,
  • baseline validation,
  • merge simulation.

Non-goals:

  • Do not delete remote branches automatically.
  • Do not delete dirty worktrees automatically.
  • Do not remove worktrees tied to open PRs.
  • Do not rely on prompt-only cleanup; enforce cleanup through workflow helpers/tests.
Problem: LLM workflows create many session-owned worktrees under `branches/`, including review worktrees, baseline worktrees, merge-simulation worktrees, issue worktrees, and conflict-fix worktrees. These are not always cleaned up, especially when a run stops early, races another session, hits a validation failure, or loses shell/cwd state. Over time, `branches/` fills with stale folders and makes later workflow decisions harder and riskier. Required behavior: Every workflow that creates a worktree must register ownership, purpose, target issue/PR, expected branch/head SHA, creation time, and cleanup policy. Completed clean worktrees should be removed automatically or flagged for safe cleanup. Dirty, active, or leased worktrees must be preserved and reported. Acceptance criteria: 1. Every session-owned worktree under `branches/` must have metadata: * path, * workflow type, * issue number if applicable, * PR number if applicable, * branch/head SHA, * creator identity/profile, * created timestamp, * last-used timestamp, * cleanup eligibility. 2. Review/baseline/merge-simulation worktrees are removed automatically at successful workflow completion. 3. If cleanup fails, the final report must list the leftover worktree and reason. 4. A cleanup audit command classifies all `branches/` entries as: * active open PR, * active issue work, * dirty local worktree, * clean stale removable, * detached review leftover, * unsafe/unknown. 5. Clean stale removable worktrees can be removed one-by-one after verification. 6. Dirty worktrees are never deleted automatically. 7. Active PR/review worktrees are never deleted automatically. 8. Worktrees with active leases are never deleted automatically. 9. Cleanup must prove: * worktree path, * branch/HEAD, * clean/dirty status, * no active PR/lease, * removal result. 10. Final verification must include `git worktree list` or equivalent. 11. Tests cover: * successful review cleanup, * failed review leaving worktree reported, * dirty worktree preservation, * active PR worktree preservation, * stale clean worktree removal, * detached review worktree classification, * TTL-expired but dirty worktree, * lease-protected worktree, * cleanup report accuracy. 12. Documentation updates all workflows that create worktrees: * review-merge-pr, * work-issue, * conflict-fix, * baseline validation, * merge simulation. Non-goals: * Do not delete remote branches automatically. * Do not delete dirty worktrees automatically. * Do not remove worktrees tied to open PRs. * Do not rely on prompt-only cleanup; enforce cleanup through workflow helpers/tests.
jcwalker3 added the status:in-progress label 2026-07-07 10:45:11 -05:00
Author
Owner

Issue claim heartbeat

<!-- gitea-issue-claim-heartbeat:v1 --> **Issue claim heartbeat** - kind: claim - issue: #401 - branch: feat/issue-401-worktree-cleanup-ttl - phase: claimed - profile: prgs-author - pr: none - blocker: none - next_action: create worktree and begin implementation
Author
Owner

Issue claim heartbeat

<!-- gitea-issue-claim-heartbeat:v1 --> **Issue claim heartbeat** - kind: claim - issue: #401 - branch: feat/issue-401-worktree-cleanup-ttl - phase: claimed - profile: prgs-author - pr: none - blocker: none - next_action: create worktree and begin implementation
sysadmin removed the status:in-progress label 2026-07-08 10:11:17 -05:00
Sign in to join this conversation.
No labels
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Scaled-Tech-Consulting/Gitea-Tools#401