Skip to content

Commit cfac8f2

Browse files
committed
fix(server): clean up child worktrees after their parent settles
1 parent cefb544 commit cfac8f2

2 files changed

Lines changed: 186 additions & 3 deletions

File tree

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

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,139 @@ it.live("preserves late tracked edits when submodules were never initialized", (
778778
);
779779

780780
describe("subagent worktree ownership", () => {
781+
it.effect.each([
782+
"child",
783+
"grandchild",
784+
"immediate",
785+
"active-parent",
786+
"recent-settle",
787+
"settle-grace",
788+
"recent-child-activity",
789+
"running-child",
790+
"pending-child",
791+
"unsettled-child",
792+
"pinned-child",
793+
"fork",
794+
"missing-parent",
795+
"cycle",
796+
] as const)("checks isolated subagent settlement (%s)", (scenario) =>
797+
Effect.gen(function* () {
798+
yield* TestClock.setTime(NOW_MS);
799+
const fixture = yield* cleanupFixture({
800+
...DEFAULT_SERVER_SETTINGS.storageCleanup,
801+
worktreeSettledAfterDays: scenario === "immediate" ? 0 : 8,
802+
});
803+
const root = shell({
804+
settledAt:
805+
scenario === "active-parent"
806+
? null
807+
: scenario === "recent-settle" || scenario === "settle-grace"
808+
? at(-30_000)
809+
: scenario === "immediate"
810+
? at(-120_000)
811+
: at(-20 * DAY_MS),
812+
settledOverride: scenario === "active-parent" ? null : "settled",
813+
updatedAt: scenario === "settle-grace" ? at(-30_000) : at(-10 * DAY_MS),
814+
});
815+
const child = shell({
816+
id: ThreadId.make("child"),
817+
branch: "feature",
818+
worktreePath: fixture.worktree,
819+
lineage: {
820+
parentThreadId:
821+
scenario === "missing-parent"
822+
? ThreadId.make("missing")
823+
: scenario === "cycle"
824+
? ThreadId.make("child")
825+
: scenario === "grandchild"
826+
? ThreadId.make("middle")
827+
: root.id,
828+
rootThreadId: root.id,
829+
relationshipToParent: scenario === "fork" ? "fork" : "subagent",
830+
},
831+
status: scenario === "running-child" ? "running" : "completed",
832+
pendingBackgroundTasks:
833+
scenario === "pending-child" ? [{ taskId: "task", kind: "command" }] : [],
834+
latestRunCompletedAt: scenario === "recent-child-activity" ? at(-2 * DAY_MS) : null,
835+
settledOverride: scenario === "unsettled-child" ? "active" : null,
836+
pinnedAt: scenario === "pinned-child" ? at(-DAY_MS) : null,
837+
});
838+
const middle = shell({
839+
id: ThreadId.make("middle"),
840+
lineage: {
841+
parentThreadId: root.id,
842+
rootThreadId: root.id,
843+
relationshipToParent: "subagent",
844+
},
845+
});
846+
fixture.setThreads([child, middle, root]);
847+
const report = yield* fixture.service.runNow;
848+
const removed = ["child", "grandchild", "immediate"].includes(scenario);
849+
expect(yield* fixture.fs.exists(fixture.worktree)).toBe(!removed);
850+
expect(report.entries[0]).toMatchObject({
851+
threadId: child.id,
852+
outcome: removed ? "removed" : "kept",
853+
reason: removed
854+
? scenario === "immediate"
855+
? "Settled for 0 days"
856+
: "Settled for 20 days"
857+
: scenario === "settle-grace"
858+
? "Settled moments ago"
859+
: scenario === "recent-settle"
860+
? "Settled for 0 of 8 days"
861+
: scenario === "recent-child-activity"
862+
? "Settled for 2 of 8 days"
863+
: scenario === "running-child" || scenario === "pending-child"
864+
? "Thread is running or has pending work"
865+
: "Not settled",
866+
});
867+
}).pipe(Effect.provide(cleanupTestLayer), Effect.scoped),
868+
);
869+
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+
),
912+
);
913+
781914
it.effect.each([
782915
"idle",
783916
"grandchild",

‎apps/server/src/storageCleanup.ts‎

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type {
1414
WorktreeCleanupRules,
1515
StorageCleanupReport,
1616
StorageCleanupReportEntry,
17+
ThreadId,
1718
} from "@t3tools/contracts";
1819
import { resolveWorktreeCleanup } from "@t3tools/shared/projectSettings";
1920
import { makeDrainableWorker } from "@t3tools/shared/DrainableWorker";
@@ -213,6 +214,33 @@ function worktreeOwner<T extends Pick<OrchestrationV2ThreadShell, "id" | "lineag
213214
return owner;
214215
}
215216

217+
function worktreeSettlementChain(
218+
thread: OrchestrationV2ThreadShell,
219+
byId: ReadonlyMap<ThreadId, OrchestrationV2ThreadShell>,
220+
) {
221+
const chain = [thread];
222+
let current = thread;
223+
while (current.settledAt === null) {
224+
if (
225+
current.lineage.relationshipToParent !== "subagent" ||
226+
current.settledOverride === "active" ||
227+
current.pinnedAt != null
228+
)
229+
return [];
230+
const parent = byId.get(current.lineage.parentThreadId!);
231+
if (
232+
parent === undefined ||
233+
chain.some((member) => member.id === parent.id) ||
234+
parent.projectId !== thread.projectId ||
235+
parent.lineage.rootThreadId !== thread.lineage.rootThreadId
236+
)
237+
return [];
238+
chain.push(parent);
239+
current = parent;
240+
}
241+
return chain;
242+
}
243+
216244
/** Squash/rebase merges require the merged PR to name this exact head commit. */
217245
export function storageCleanupPullRequestMerged(
218246
pullRequest: Pick<
@@ -775,6 +803,7 @@ const make = Effect.gen(function* () {
775803
),
776804
)).filter((thread) => thread.worktreePath !== null && thread.branch !== null);
777805
const snapshot = yield* readThreads();
806+
const threadsById = new Map(snapshot.threads.map((thread) => [thread.id, thread]));
778807
const refreshedDefaultRefs = new Map<string, Set<string>>();
779808
const defaultBranches = new Map<string, string | null>();
780809
const groups = Map.groupBy(
@@ -808,6 +837,8 @@ const make = Effect.gen(function* () {
808837
const group = groups.get(worktreePath) ?? [];
809838
const activityAt = Math.max(...group.map(storageCleanupActivityAt));
810839
const deleted = "workspaceRoot" in thread;
840+
const settlementChain = deleted ? [] : worktreeSettlementChain(thread, threadsById);
841+
const settledThread = settlementChain.at(-1);
811842
const members = deleted ? [thread, ...group] : group;
812843
const project = deleted
813844
? { workspaceRoot: thread.workspaceRoot }
@@ -862,7 +893,7 @@ const make = Effect.gen(function* () {
862893
const changes = yield* localChangesReason(worktreePath, settings);
863894
if (changes !== null) return keep(changes);
864895
if (
865-
group.some(
896+
[...group, ...settlementChain.slice(1)].some(
866897
(member) =>
867898
member.settledAt !== null &&
868899
now - Math.max(DateTime.toEpochMillis(member.updatedAt), activityAt) <
@@ -876,9 +907,9 @@ const make = Effect.gen(function* () {
876907
settings.worktreeAfterDays !== null &&
877908
activityAt < now - settings.worktreeAfterDays * DAY_MS;
878909
const settledFor =
879-
deleted || thread.settledAt === null
910+
settledThread?.settledAt == null
880911
? null
881-
: now - Math.max(DateTime.toEpochMillis(thread.settledAt), activityAt);
912+
: now - Math.max(DateTime.toEpochMillis(settledThread.settledAt), activityAt);
882913
const settledDays = settledFor === null ? null : Math.floor(settledFor / DAY_MS);
883914
const settled =
884915
settledFor !== null &&
@@ -1045,6 +1076,25 @@ const make = Effect.gen(function* () {
10451076
})
10461077
)
10471078
return waitForStop("thread activity or shared worktree changed since check");
1079+
if (settlementChain.length > 1) {
1080+
const ancestors = yield* Effect.forEach(settlementChain.slice(1), (ancestor) =>
1081+
projections.getThreadShell(ancestor.id),
1082+
);
1083+
const latestSettlement = worktreeSettlementChain(
1084+
latestGroup.find((shell) => shell.id === thread.id)!,
1085+
new Map(
1086+
[...latestGroup, ...ancestors.filter((ancestor) => ancestor !== null)].map(
1087+
(ancestor) => [ancestor.id, ancestor],
1088+
),
1089+
),
1090+
).at(-1);
1091+
if (
1092+
latestSettlement?.id !== settledThread?.id ||
1093+
!Equal.equals(latestSettlement?.settledAt, settledThread?.settledAt) ||
1094+
!Equal.equals(latestSettlement?.updatedAt, settledThread?.updatedAt)
1095+
)
1096+
return "parent settlement changed since check";
1097+
}
10481098
// Sessions can outlive their run and can be shared across app threads.
10491099
const sessionRows = yield* sql<{ payload_json: string }>`
10501100
SELECT payload_json FROM orchestration_v2_projection_provider_sessions

0 commit comments

Comments
 (0)