Skip to content

Commit dee32ba

Browse files
committed
fix(server): keep worktrees for branchless subagents
1 parent 6d49225 commit dee32ba

2 files changed

Lines changed: 61 additions & 55 deletions

File tree

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

Lines changed: 58 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -561,13 +561,7 @@ describe("subagent worktree ownership", () => {
561561
root,
562562
]);
563563
const report = yield* fixture.service.runNow;
564-
const removed = [
565-
"idle",
566-
"grandchild",
567-
"null-branch",
568-
"isolated-owner",
569-
"fork-owner",
570-
].includes(scenario);
564+
const removed = ["idle", "grandchild", "isolated-owner", "fork-owner"].includes(scenario);
571565
const shared = [
572566
"unrelated",
573567
"fork",
@@ -586,58 +580,67 @@ describe("subagent worktree ownership", () => {
586580
? "Thread is running or has pending work"
587581
: shared
588582
? "Shared by 2 threads"
589-
: scenario === "recent-activity"
590-
? "Settled for 2 of 8 days"
591-
: scenario === "recent-inactive"
592-
? "Inactive for 2 of 8 days"
593-
: ["recent-grace", "recent-settle", "child-settled-now"].includes(scenario)
594-
? "Settled moments ago"
595-
: "Not settled",
583+
: scenario === "null-branch"
584+
? "A subagent thread has no branch to restore it from"
585+
: scenario === "recent-activity"
586+
? "Settled for 2 of 8 days"
587+
: scenario === "recent-inactive"
588+
? "Inactive for 2 of 8 days"
589+
: ["recent-grace", "recent-settle", "child-settled-now"].includes(scenario)
590+
? "Settled moments ago"
591+
: "Not settled",
596592
});
597593
}).pipe(Effect.provide(cleanupTestLayer), Effect.scoped),
598594
);
599595

600-
it.live.each(["running", "pending", "activity", "unrelated", "settled", "resettled"] as const)(
601-
"cancels removal when a child changes during measurement (%s)",
602-
(change) =>
603-
runCleanupTest(
604-
Effect.gen(function* () {
605-
const fixture = yield* pausedMeasurementFixture();
606-
const root = shell({ branch: "feature", worktreePath: fixture.worktree });
607-
const child = {
608-
...root,
609-
id: ThreadId.make("child"),
610-
settledAt: change === "resettled" ? at(-20 * DAY_MS) : null,
611-
lineage: {
612-
parentThreadId: root.id,
613-
rootThreadId: root.id,
614-
relationshipToParent: "subagent" as const,
615-
},
616-
};
617-
fixture.setThreads([child, root]);
618-
const run = yield* fixture.service.runNow.pipe(Effect.forkChild);
619-
yield* Deferred.await(fixture.measuring);
620-
fixture.setThreads([
621-
root,
622-
{
623-
...child,
624-
settledAt: change === "settled" ? at(-20 * DAY_MS) : child.settledAt,
625-
updatedAt: change === "settled" || change === "resettled" ? at(0) : child.updatedAt,
626-
status: change === "running" ? "running" : "idle",
627-
pendingBackgroundTasks:
628-
change === "pending" ? [{ taskId: "task", kind: "command" }] : [],
629-
latestRunCompletedAt: change === "activity" ? at(-DAY_MS) : null,
630-
},
631-
...(change === "unrelated" ? [shell({ ...root, id: ThreadId.make("other") })] : []),
632-
]);
633-
yield* Deferred.succeed(fixture.resume, undefined);
634-
expect((yield* Fiber.join(run)).entries[0]).toMatchObject({
635-
outcome: "kept",
636-
reason: "Thread activity or shared worktree changed since check",
637-
});
638-
expect(yield* fixture.fs.exists(fixture.worktree)).toBe(true);
639-
}),
640-
),
596+
it.live.each([
597+
"running",
598+
"pending",
599+
"activity",
600+
"unrelated",
601+
"settled",
602+
"resettled",
603+
"null-branch",
604+
] as const)("cancels removal when a child changes during measurement (%s)", (change) =>
605+
runCleanupTest(
606+
Effect.gen(function* () {
607+
const fixture = yield* pausedMeasurementFixture();
608+
const root = shell({ branch: "feature", worktreePath: fixture.worktree });
609+
const child = {
610+
...root,
611+
id: ThreadId.make("child"),
612+
settledAt: change === "resettled" ? at(-20 * DAY_MS) : null,
613+
lineage: {
614+
parentThreadId: root.id,
615+
rootThreadId: root.id,
616+
relationshipToParent: "subagent" as const,
617+
},
618+
};
619+
fixture.setThreads([child, root]);
620+
const run = yield* fixture.service.runNow.pipe(Effect.forkChild);
621+
yield* Deferred.await(fixture.measuring);
622+
fixture.setThreads([
623+
root,
624+
{
625+
...child,
626+
branch: change === "null-branch" ? null : child.branch,
627+
settledAt: change === "settled" ? at(-20 * DAY_MS) : child.settledAt,
628+
updatedAt: change === "settled" || change === "resettled" ? at(0) : child.updatedAt,
629+
status: change === "running" ? "running" : "idle",
630+
pendingBackgroundTasks:
631+
change === "pending" ? [{ taskId: "task", kind: "command" }] : [],
632+
latestRunCompletedAt: change === "activity" ? at(-DAY_MS) : null,
633+
},
634+
...(change === "unrelated" ? [shell({ ...root, id: ThreadId.make("other") })] : []),
635+
]);
636+
yield* Deferred.succeed(fixture.resume, undefined);
637+
expect((yield* Fiber.join(run)).entries[0]).toMatchObject({
638+
outcome: "kept",
639+
reason: "Thread activity or shared worktree changed since check",
640+
});
641+
expect(yield* fixture.fs.exists(fixture.worktree)).toBe(true);
642+
}),
643+
),
641644
);
642645
});
643646

‎apps/server/src/storageCleanup.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +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");
504506
if (hasTerminal(worktreePath)) return keep("open terminal");
505507
// Roots are canonical, so compare canonical paths. A symlinked parent
506508
// (a linked drive) is fine; a symlinked worktree directory is not.
@@ -647,6 +649,7 @@ const make = Effect.gen(function* () {
647649
const previous = group.find((member) => member.id === shell.id);
648650
return (
649651
previous === undefined ||
652+
shell.branch === null ||
650653
!Equal.equals(shell.settledAt, previous.settledAt) ||
651654
(shell.settledAt !== null &&
652655
!Equal.equals(shell.updatedAt, previous.updatedAt)) ||

0 commit comments

Comments
 (0)