Skip to content

Commit 6d49225

Browse files
committed
fix(server): preserve cleanup owner branches and allow fork owners
1 parent f2b6eb7 commit 6d49225

2 files changed

Lines changed: 22 additions & 7 deletions

File tree

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,7 @@ describe("subagent worktree ownership", () => {
469469
"grandchild",
470470
"null-branch",
471471
"isolated-owner",
472+
"fork-owner",
472473
"missing-parent",
473474
"cycle",
474475
"two-tops",
@@ -498,12 +499,13 @@ describe("subagent worktree ownership", () => {
498499
branch: "feature",
499500
worktreePath: fixture.worktree,
500501
settledAt: scenario === "child-settled-only" ? null : at(-20 * DAY_MS),
501-
...(scenario === "isolated-owner"
502+
...(scenario === "isolated-owner" || scenario === "fork-owner"
502503
? {
503504
lineage: {
504505
parentThreadId: ThreadId.make("ancestor"),
505506
rootThreadId: ThreadId.make("ancestor"),
506-
relationshipToParent: "subagent" as const,
507+
relationshipToParent:
508+
scenario === "fork-owner" ? ("fork" as const) : ("subagent" as const),
507509
},
508510
}
509511
: {}),
@@ -545,7 +547,7 @@ describe("subagent worktree ownership", () => {
545547
fixture.setThreads(
546548
scenario === "grandchild"
547549
? [grandchild, child, root]
548-
: scenario === "isolated-owner"
550+
: scenario === "isolated-owner" || scenario === "fork-owner"
549551
? [child, root, shell({ id: ThreadId.make("ancestor") })]
550552
: [child, root],
551553
);
@@ -559,7 +561,13 @@ describe("subagent worktree ownership", () => {
559561
root,
560562
]);
561563
const report = yield* fixture.service.runNow;
562-
const removed = ["idle", "grandchild", "null-branch", "isolated-owner"].includes(scenario);
564+
const removed = [
565+
"idle",
566+
"grandchild",
567+
"null-branch",
568+
"isolated-owner",
569+
"fork-owner",
570+
].includes(scenario);
563571
const shared = [
564572
"unrelated",
565573
"fork",
@@ -675,6 +683,14 @@ describe("storage cleanup reports and local file policies", () => {
675683
),
676684
);
677685
it.live.each([
686+
[
687+
"its owner's branch is cleared",
688+
(worktree: string) => [shell({ branch: null, worktreePath: worktree, updatedAt: at(0) })],
689+
],
690+
[
691+
"its owner's branch changes",
692+
(worktree: string) => [shell({ branch: "other", worktreePath: worktree, updatedAt: at(0) })],
693+
],
678694
[
679695
"its thread starts",
680696
(worktree: string) => [

‎apps/server/src/storageCleanup.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,13 @@ export function storageCleanupActivityAt(thread: OrchestrationV2ThreadShell): nu
134134
);
135135
}
136136

137-
/** Only a complete subagent chain shares ownership; forks remain separate. */
137+
/** Only a complete subagent chain within this worktree shares ownership. */
138138
function worktreeOwner(threads: ReadonlyArray<OrchestrationV2ThreadShell>) {
139139
if (threads.length === 1) return threads[0];
140140
const byId = new Map(threads.map((thread) => [thread.id, thread]));
141141
const tops = threads.filter((thread) => !byId.has(thread.lineage.parentThreadId!));
142142
if (tops.length !== 1) return undefined;
143143
const owner = tops[0]!;
144-
if (owner.lineage.parentThreadId !== null && owner.lineage.relationshipToParent !== "subagent")
145-
return undefined;
146144
for (const thread of threads) {
147145
let current = thread;
148146
const seen = new Set<string>();
@@ -644,6 +642,7 @@ const make = Effect.gen(function* () {
644642
if (
645643
latestGroup.length !== group.length ||
646644
latestOwner?.id !== thread.id ||
645+
latestOwner?.branch !== thread.branch ||
647646
latestGroup.some((shell) => {
648647
const previous = group.find((member) => member.id === shell.id);
649648
return (

0 commit comments

Comments
 (0)