Skip to content

Commit b71f087

Browse files
committed
fix(server): keep worktrees for mismatched subagent branches
1 parent dee32ba commit b71f087

2 files changed

Lines changed: 19 additions & 7 deletions

File tree

‎apps/server/src/storageCleanup.test.ts‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,7 @@ describe("subagent worktree ownership", () => {
468468
"idle",
469469
"grandchild",
470470
"null-branch",
471+
"stale-branch",
471472
"isolated-owner",
472473
"fork-owner",
473474
"missing-parent",
@@ -512,7 +513,12 @@ describe("subagent worktree ownership", () => {
512513
});
513514
const child = shell({
514515
id: ThreadId.make("child"),
515-
branch: scenario === "null-branch" ? null : "feature",
516+
branch:
517+
scenario === "null-branch"
518+
? null
519+
: scenario === "stale-branch"
520+
? "t3/15a6e5ae"
521+
: "feature",
516522
worktreePath: fixture.worktree,
517523
lineage: {
518524
parentThreadId:
@@ -580,8 +586,8 @@ describe("subagent worktree ownership", () => {
580586
? "Thread is running or has pending work"
581587
: shared
582588
? "Shared by 2 threads"
583-
: scenario === "null-branch"
584-
? "A subagent thread has no branch to restore it from"
589+
: scenario === "null-branch" || scenario === "stale-branch"
590+
? "A subagent thread's branch differs from this worktree's branch"
585591
: scenario === "recent-activity"
586592
? "Settled for 2 of 8 days"
587593
: scenario === "recent-inactive"
@@ -601,6 +607,7 @@ describe("subagent worktree ownership", () => {
601607
"settled",
602608
"resettled",
603609
"null-branch",
610+
"stale-branch",
604611
] as const)("cancels removal when a child changes during measurement (%s)", (change) =>
605612
runCleanupTest(
606613
Effect.gen(function* () {
@@ -623,7 +630,12 @@ describe("subagent worktree ownership", () => {
623630
root,
624631
{
625632
...child,
626-
branch: change === "null-branch" ? null : child.branch,
633+
branch:
634+
change === "null-branch"
635+
? null
636+
: change === "stale-branch"
637+
? "t3/15a6e5ae"
638+
: child.branch,
627639
settledAt: change === "settled" ? at(-20 * DAY_MS) : child.settledAt,
628640
updatedAt: change === "settled" || change === "resettled" ? at(0) : child.updatedAt,
629641
status: change === "running" ? "running" : "idle",

‎apps/server/src/storageCleanup.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -501,8 +501,8 @@ const make = Effect.gen(function* () {
501501
(!deleted && !group.every((member) => storageCleanupThreadIdle(member, now)))
502502
)
503503
return keep("thread is running or has pending work");
504-
if (group.some((member) => member.branch === null))
505-
return keep("a subagent thread has no branch to restore it from");
504+
if (group.some((member) => member.branch !== thread.branch))
505+
return keep("a subagent thread's branch differs from this worktree's branch");
506506
if (hasTerminal(worktreePath)) return keep("open terminal");
507507
// Roots are canonical, so compare canonical paths. A symlinked parent
508508
// (a linked drive) is fine; a symlinked worktree directory is not.
@@ -649,7 +649,7 @@ const make = Effect.gen(function* () {
649649
const previous = group.find((member) => member.id === shell.id);
650650
return (
651651
previous === undefined ||
652-
shell.branch === null ||
652+
shell.branch !== thread.branch ||
653653
!Equal.equals(shell.settledAt, previous.settledAt) ||
654654
(shell.settledAt !== null &&
655655
!Equal.equals(shell.updatedAt, previous.updatedAt)) ||

0 commit comments

Comments
 (0)