Skip to content

Commit 2d2cb13

Browse files
committed
fix(server): retain child worktrees while ancestors have pending work
1 parent cfac8f2 commit 2d2cb13

2 files changed

Lines changed: 83 additions & 46 deletions

File tree

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

Lines changed: 66 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,8 @@ describe("subagent worktree ownership", () => {
788788
"recent-child-activity",
789789
"running-child",
790790
"pending-child",
791+
"running-parent",
792+
"pending-parent",
791793
"unsettled-child",
792794
"pinned-child",
793795
"fork",
@@ -822,7 +824,7 @@ describe("subagent worktree ownership", () => {
822824
? ThreadId.make("missing")
823825
: scenario === "cycle"
824826
? ThreadId.make("child")
825-
: scenario === "grandchild"
827+
: ["grandchild", "running-parent", "pending-parent"].includes(scenario)
826828
? ThreadId.make("middle")
827829
: root.id,
828830
rootThreadId: root.id,
@@ -837,6 +839,9 @@ describe("subagent worktree ownership", () => {
837839
});
838840
const middle = shell({
839841
id: ThreadId.make("middle"),
842+
status: scenario === "running-parent" ? "running" : "idle",
843+
pendingBackgroundTasks:
844+
scenario === "pending-parent" ? [{ taskId: "task", kind: "command" }] : [],
840845
lineage: {
841846
parentThreadId: root.id,
842847
rootThreadId: root.id,
@@ -867,48 +872,66 @@ describe("subagent worktree ownership", () => {
867872
}).pipe(Effect.provide(cleanupTestLayer), Effect.scoped),
868873
);
869874

870-
it.live.each(["parent-unsettled", "parent-resettled", "child-unsettled"] as const)(
871-
"cancels inherited settlement during measurement (%s)",
872-
(change) =>
873-
runCleanupTest(
874-
Effect.gen(function* () {
875-
const fixture = yield* pausedMeasurementFixture({
876-
...DEFAULT_SERVER_SETTINGS.storageCleanup,
877-
worktreeSettledAfterDays: 0,
878-
});
879-
const root = shell({ settledAt: at(-20 * DAY_MS), settledOverride: "settled" });
880-
const child = shell({
881-
id: ThreadId.make("child"),
882-
branch: "feature",
883-
worktreePath: fixture.worktree,
884-
lineage: {
885-
parentThreadId: root.id,
886-
rootThreadId: root.id,
887-
relationshipToParent: "subagent",
888-
},
889-
});
890-
fixture.setThreads([child, root]);
891-
const run = yield* fixture.service.runNow.pipe(Effect.forkChild);
892-
yield* Deferred.await(fixture.measuring);
893-
fixture.setThreads([
894-
{ ...child, settledOverride: change === "child-unsettled" ? "active" : null },
895-
change === "child-unsettled"
896-
? root
897-
: {
898-
...root,
899-
settledAt: change === "parent-resettled" ? at(0) : null,
900-
settledOverride: change === "parent-resettled" ? "settled" : "active",
901-
updatedAt: at(0),
902-
},
903-
]);
904-
yield* Deferred.succeed(fixture.resume, undefined);
905-
expect((yield* Fiber.join(run)).entries[0]).toMatchObject({
906-
outcome: "kept",
907-
reason: "Parent settlement changed since check",
908-
});
909-
expect(yield* fixture.fs.exists(fixture.worktree)).toBe(true);
910-
}),
911-
),
875+
it.live.each([
876+
"parent-unsettled",
877+
"parent-resettled",
878+
"child-unsettled",
879+
"intermediate-running",
880+
"intermediate-pending",
881+
] as const)("cancels inherited settlement during measurement (%s)", (change) =>
882+
runCleanupTest(
883+
Effect.gen(function* () {
884+
const fixture = yield* pausedMeasurementFixture({
885+
...DEFAULT_SERVER_SETTINGS.storageCleanup,
886+
worktreeSettledAfterDays: 0,
887+
});
888+
const root = shell({ settledAt: at(-20 * DAY_MS), settledOverride: "settled" });
889+
const middle = shell({
890+
id: ThreadId.make("middle"),
891+
lineage: {
892+
parentThreadId: root.id,
893+
rootThreadId: root.id,
894+
relationshipToParent: "subagent",
895+
},
896+
});
897+
const child = shell({
898+
id: ThreadId.make("child"),
899+
branch: "feature",
900+
worktreePath: fixture.worktree,
901+
lineage: {
902+
parentThreadId: middle.id,
903+
rootThreadId: root.id,
904+
relationshipToParent: "subagent",
905+
},
906+
});
907+
fixture.setThreads([child, middle, root]);
908+
const run = yield* fixture.service.runNow.pipe(Effect.forkChild);
909+
yield* Deferred.await(fixture.measuring);
910+
fixture.setThreads([
911+
{ ...child, settledOverride: change === "child-unsettled" ? "active" : null },
912+
{
913+
...middle,
914+
status: change === "intermediate-running" ? "running" : "idle",
915+
pendingBackgroundTasks:
916+
change === "intermediate-pending" ? [{ taskId: "task", kind: "command" }] : [],
917+
},
918+
change === "child-unsettled" || change.startsWith("intermediate-")
919+
? root
920+
: {
921+
...root,
922+
settledAt: change === "parent-resettled" ? at(0) : null,
923+
settledOverride: change === "parent-resettled" ? "settled" : "active",
924+
updatedAt: at(0),
925+
},
926+
]);
927+
yield* Deferred.succeed(fixture.resume, undefined);
928+
expect((yield* Fiber.join(run)).entries[0]).toMatchObject({
929+
outcome: "kept",
930+
reason: "Parent settlement changed since check",
931+
});
932+
expect(yield* fixture.fs.exists(fixture.worktree)).toBe(true);
933+
}),
934+
),
912935
);
913936

914937
it.effect.each([

‎apps/server/src/storageCleanup.ts‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,16 @@ export function storageCleanupThreadIdle(
125125
thread: OrchestrationV2ThreadShell,
126126
now: number,
127127
settings: LimitRecoverySettings,
128+
): boolean {
129+
return thread.worktreePath !== null && threadIdle(thread, now, settings);
130+
}
131+
132+
function threadIdle(
133+
thread: OrchestrationV2ThreadShell,
134+
now: number,
135+
settings: LimitRecoverySettings,
128136
): boolean {
129137
return (
130-
thread.worktreePath !== null &&
131138
thread.activeRunId === null &&
132139
(thread.status === "idle" ||
133140
thread.status === "completed" ||
@@ -217,6 +224,8 @@ function worktreeOwner<T extends Pick<OrchestrationV2ThreadShell, "id" | "lineag
217224
function worktreeSettlementChain(
218225
thread: OrchestrationV2ThreadShell,
219226
byId: ReadonlyMap<ThreadId, OrchestrationV2ThreadShell>,
227+
now: number,
228+
settings: LimitRecoverySettings,
220229
) {
221230
const chain = [thread];
222231
let current = thread;
@@ -232,7 +241,8 @@ function worktreeSettlementChain(
232241
parent === undefined ||
233242
chain.some((member) => member.id === parent.id) ||
234243
parent.projectId !== thread.projectId ||
235-
parent.lineage.rootThreadId !== thread.lineage.rootThreadId
244+
parent.lineage.rootThreadId !== thread.lineage.rootThreadId ||
245+
!threadIdle(parent, now, settings)
236246
)
237247
return [];
238248
chain.push(parent);
@@ -837,7 +847,9 @@ const make = Effect.gen(function* () {
837847
const group = groups.get(worktreePath) ?? [];
838848
const activityAt = Math.max(...group.map(storageCleanupActivityAt));
839849
const deleted = "workspaceRoot" in thread;
840-
const settlementChain = deleted ? [] : worktreeSettlementChain(thread, threadsById);
850+
const settlementChain = deleted
851+
? []
852+
: worktreeSettlementChain(thread, threadsById, now, serverSettings);
841853
const settledThread = settlementChain.at(-1);
842854
const members = deleted ? [thread, ...group] : group;
843855
const project = deleted
@@ -1087,6 +1099,8 @@ const make = Effect.gen(function* () {
10871099
(ancestor) => [ancestor.id, ancestor],
10881100
),
10891101
),
1102+
now,
1103+
serverSettings,
10901104
).at(-1);
10911105
if (
10921106
latestSettlement?.id !== settledThread?.id ||

0 commit comments

Comments
 (0)