Skip to content

fix(cb-15cw): prune-worktrees.sh keeps a tree whose bead a live session holds - #536

Merged
rmstdope merged 2 commits into
mainfrom
cb-15cw-prune-worktrees-sh-removes-a-live
Nov 10, 2026
Merged

rmstdope merged 2 commits into
mainfrom
cb-15cw-prune-worktrees-sh-removes-a-live

Conversation

@rmstdope

Copy link
Copy Markdown
Owner

Bead: cb-15cw (bugfix).

Bug: scripts/prune-worktrees.sh removed a live producer's worktree and branch when the tree was clean, at main and unwritten past STALE_MINUTES — exactly a producer still planning (Cyclops on cb-yf8h, 2026-11-10).

Reproduction: tests/prune-worktrees.sh cases 16–17: a clean, landed, 10-hour-old tree whose bead a live (stubbed) session holds is kept, with a reason naming the session; an unheld tree in the same sweep still goes; once the session is dead the same tree goes. Case 16 failed before the fix with removed cb-live.

Fix: once per sweep, read which bead each live session is on (state file bead, or its handover while the state names none; believed only when agent-alive succeeds — the same test release-bead --worktree uses for the fleet view's tidy, which already guarded this) and keep a tree whose bead one holds. The claim is still not the guard, so a crashed agent's tree is removed as before.

Validation: bash tests/prune-worktrees.sh, tests/project-sweeps.sh, tests/release-bead.sh green. bash tests/gate: everything green except the fleet-* PTY suites (fleet-keys, fleet-board, fleet-launch, fleet-type) timing out under load (load avg ~8, other fleet sessions building); each passes run alone, and none touches the pruner.

🤖 Generated with Claude Code

https://claude.ai/code/session_01WCDKf2aRvKRWd7G9dvQEJE

…on holds

A producer still planning has a clean, landed, quiet tree, and the pruner
removed it from under the session. The holder is read from the state
files and believed only when agent-alive says the session is running, so
a crashed session's tree still goes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WCDKf2aRvKRWd7G9dvQEJE
@rmstdope

Copy link
Copy Markdown
Owner Author

Review (cold read, reviewed_head: 8b523cb08a2f73ed1577e9ab39f6a6646663b150) — reviewed before merge under the producer's review practice, by an agent given the diff and the bead, and not the producer's reasoning. A bug bead has no plan; the diff was read against the bead's bug claim and the reproduction test (cases 16–17).

The fix addresses the stated cause, sits ahead of the four checks, and a crashed session's tree still goes. Case 16 would fail before the change. The fleet view's tidy already guards this through release-bead --worktree's alive_on_bead. None of the findings blocks a merge.

  1. Missing test for the handover fallback (tests/prune-worktrees.sh): no case covers a state file that names no bead while .cerebro/state/Cyclops.handover names cb-live, so deleting that branch of read_live_holders would leave the suite green.
  2. A second copy of "which bead is a live session on" (read_live_holders in prune-worktrees.sh vs alive_on_bead in release-bead): CLAUDE.md says "add a caller, never a second copy". The two already differ on a live session that has neither a bead nor a handover. Suggest a shared helper in worktree-safety.sh.
  3. Minor: case 17 asserts the directory is gone but not the branch cb-live-branch.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Note

Copilot was unable to complete this review at the selected tier due to model policy or plan restrictions.

🟢 Approval recommended

The change adds targeted logic to detect beads held by alive sessions and skip pruning corresponding worktrees, with regression tests covering both the “live holder keeps tree” and “once dead, tree prunes” scenarios and no remaining issues noted.

No open issues 🎉

What changed in this PR

Prevents scripts/prune-worktrees.sh from deleting a clean/landed/stale worktree and branch that are still in active use by a live agent session holding the corresponding bead.

Changes:

  • Detect beads held by alive sessions (from state bead or handover when absent) once per sweep and keep matching worktrees.
  • Emit a keep reason that identifies the live session holding the bead.
  • Add regression tests covering “live holder keeps tree” and “once dead, tree prunes” behavior.
File Description
tests/​prune-worktrees.sh Adds regression cases ensuring a live (stubbed) session holding a bead prevents pruning of an otherwise-eligible worktree, and that pruning resumes once the session is no longer alive.
scripts/​prune-worktrees.sh Updates the pruner to scan alive sessions for their current bead (via state or handover) and skip pruning any worktree whose bead is currently held.

Review effort: Balanced

…and the pruner

Answers the review: the handover fallback gets its own case, case 17
asserts the branch goes too, and the state-or-handover read moves to
scripts/session-bead.sh so the two callers cannot drift.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WCDKf2aRvKRWd7G9dvQEJE
@rmstdope

Copy link
Copy Markdown
Owner Author

Answers to the review at 8b523cb, all in 44ad073:

  1. Added case 18: a live session whose state file names no bead keeps the tree its handover names.
  2. Done. cerebro_session_bead in the new scripts/session-bead.sh is now the one reader of state-or-handover. release-bead's alive_on_bead is agent-alive && { empty || == id } over it, which gives the same answer in every case the old two-variable test covered (a handover naming another bead still answers no). Each caller keeps its own comparison, as suggested. It is a separate library rather than part of worktree-safety.sh because release-bead --ended does not source that file, and the question has nothing to do with worktrees.
  3. Case 17 now asserts cb-live-branch is deleted as well.

The diff is small and all the related suites are green (prune-worktrees, release-bead, worktree-safety, project-sweeps, fleet-retry, disk-preflight, consumer-fixture), so there is no follow-up review.

@rmstdope
rmstdope merged commit 9d7c512 into main Nov 10, 2026
4 checks passed
@rmstdope
rmstdope deleted the cb-15cw-prune-worktrees-sh-removes-a-live branch November 10, 2026 17:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants