Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

address review (claude): assert the reap claim-skip through the REAL reaper (#4668)

[._.]/ Adam Eivy6fac5779e844
diff --git a/server/lib/worktreeOwnership.js b/server/lib/worktreeOwnership.js
index 21263a5524..b97f1ea193 100644
--- a/server/lib/worktreeOwnership.js
+++ b/server/lib/worktreeOwnership.js
@@ -4,10 +4,12 @@
*
* Worktree operations are destructive: adoption moves a directory and reapers
* remove one. The callers therefore share this pure gate instead of carrying
- * slightly different copies of "managed root, agent id, claim, liveness, lock".
- * Callers can explicitly opt into the differences that are intentional: a
- * reaper may include `.claude/worktrees/`, and stale claims may be reclaimed
- * only by branch reconciliation.
+ * slightly different copies of "managed root, agent id, lock, liveness, claim".
+ * That list is also the ORDER the gate applies them in, which is itself policy —
+ * see `worktreeOwnershipReason` for why the claim comes last. Callers can
+ * explicitly opt into the differences that are intentional: a reaper may
+ * include `.claude/worktrees/`, stale claims may be reclaimed only by branch
+ * reconciliation, and only the dispatch side reads a live claim as unowned.
*/
import { win32 } from 'path';
diff --git a/server/services/worktreeManager.test.js b/server/services/worktreeManager.test.js
index 81dd281824..150e971a73 100644
--- a/server/services/worktreeManager.test.js
+++ b/server/services/worktreeManager.test.js
@@ -488,37 +488,6 @@ describe('Orphaned Worktree Detection', () => {
});
});
-// reapMergedWorktrees knows only "merged + clean", which is ALSO true of a claim
-// a human paused ten minutes ago. Reclaiming one turns on idle time and on
-// whether the branch was provably shipped, both of which only branchReconcile
-// computes — so this reaper deliberately passes neither `allowStaleClaim` nor an
-// age, and a claim tree is refused under BOTH of its managed roots. This mirrors
-// the gate call it makes so the skip cannot be lost to a future reordering.
-describe('reapMergedWorktrees claim policy', () => {
- const COS_ROOT = '/repo/data/cos/worktrees';
- const CLAUDE_ROOT = '/repo/.claude/worktrees';
- const managedRoots = [
- { path: COS_ROOT, requireAgentId: true },
- { path: CLAUDE_ROOT, requireAgentId: false },
- ];
- const reason = (path) => worktreeOwnershipReason({
- path, locked: false, activeAgentIds: new Set(), roots: managedRoots, requireKnownLiveness: true,
- });
-
- it('refuses a claim worktree under either managed root', () => {
- // Under the CoS root the agent-id policy refuses it first; under the
- // `.claude/worktrees/` root, which admits arbitrary names, the claim hold
- // itself does. Either way the reaper skips it with a reported reason.
- expect(reason(`${COS_ROOT}/claim-issue-42`)).toBe('worktree-missing-agent-id');
- expect(reason(`${CLAUDE_ROOT}/claim-issue-42`)).toBe('worktree-human-claim');
- });
-
- it('still reaps the non-claim trees it exists to clean', () => {
- expect(reason(`${COS_ROOT}/agent-abc12345`)).toBeNull();
- expect(reason(`${CLAUDE_ROOT}/review-fix`)).toBeNull();
- });
-});
-
describe('isHumanClaimWorktree', () => {
it('is true for /claim worktree dir names', () => {
expect(isHumanClaimWorktree('claim-extract-compare-helpers')).toBe(true);
diff --git a/server/services/worktreeReap.test.js b/server/services/worktreeReap.test.js
index 0e9921117c..a97893da24 100644
--- a/server/services/worktreeReap.test.js
+++ b/server/services/worktreeReap.test.js
@@ -231,6 +231,29 @@ describe.skipIf(SKIP_HEAVY_INTEGRATION)('reapMergedWorktrees', () => {
expect(branches).toContain('dry-br');
});
+ // This reaper knows only "merged + clean", which is equally true of a claim a
+ // human paused ten minutes ago. Reclaiming one turns on idle time AND on
+ // whether the branch was provably shipped — both computed by branchReconcile,
+ // not here — so it deliberately passes neither `allowStaleClaim` nor an age
+ // and a claim tree is always skipped. Asserted through the real reaper, so a
+ // later change to its roots or gate options cannot quietly lose the skip.
+ it('never reaps a human /claim worktree, even merged + clean', async () => {
+ const claimPath = await addWorktree(dir, 'claim-issue-42', 'claim/issue-42');
+ const agentPath = await addWorktree(dir, 'agent-abc12345', 'cos/task-1/agent-abc12345');
+ await execGit(['merge', '--no-ff', 'claim/issue-42', '--no-edit'], dir);
+ await execGit(['merge', '--no-ff', 'cos/task-1/agent-abc12345', '--no-edit'], dir);
+
+ const result = await reapMergedWorktrees(dir, { includeClaudeTrees: true });
+
+ // The equally merged+clean agent tree IS reaped, so the claim skip is the
+ // claim policy and not some unrelated gate refusing both.
+ expect(result.reaped.map(r => r.branch)).toContain('cos/task-1/agent-abc12345');
+ expect(result.reaped.map(r => r.branch)).not.toContain('claim/issue-42');
+ expect(skipReason(result, claimPath)).toBe('worktree-human-claim');
+ expect(existsSync(claimPath)).toBe(true);
+ expect(existsSync(agentPath)).toBe(false);
+ });
+
it('excludes .claude trees when includeClaudeTrees is false', async () => {
const path = await addWorktree(dir, 'excluded', 'excluded-br');
await execGit(['merge', '--no-ff', 'excluded-br', '--no-edit'], dir);