@devries-cameron20 / t3code-1 / commits / b6307ee88a
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(server): clean up child worktrees after their parent settles (#30026)

Lakshmi Tanmay committed Nov 4, 2026 at 05:13 UTC b6307ee88a721d48887f2f31774bf1cddad833ca
2 files changed +224 -4
apps/server/src/storageCleanup.test.ts
+156
@@ -778,6 +778,162 @@ it.live("preserves late tracked edits when submodules were never initialized", (
778 );
779
780 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 + "running-parent",
792 + "pending-parent",
793 + "unsettled-child",
794 + "pinned-child",
795 + "fork",
796 + "missing-parent",
797 + "cycle",
798 + ] as const)("checks isolated subagent settlement (%s)", (scenario) =>
799 + Effect.gen(function* () {
800 + yield* TestClock.setTime(NOW_MS);
801 + const fixture = yield* cleanupFixture({
802 + ...DEFAULT_SERVER_SETTINGS.storageCleanup,
803 + worktreeSettledAfterDays: scenario === "immediate" ? 0 : 8,
804 + });
805 + const root = shell({
806 + settledAt:
807 + scenario === "active-parent"
808 + ? null
809 + : scenario === "recent-settle" || scenario === "settle-grace"
810 + ? at(-30_000)
811 + : scenario === "immediate"
812 + ? at(-120_000)
813 + : at(-20 * DAY_MS),
814 + settledOverride: scenario === "active-parent" ? null : "settled",
815 + updatedAt: scenario === "settle-grace" ? at(-30_000) : at(-10 * DAY_MS),
816 + });
817 + const child = shell({
818 + id: ThreadId.make("child"),
819 + branch: "feature",
820 + worktreePath: fixture.worktree,
821 + lineage: {
822 + parentThreadId:
823 + scenario === "missing-parent"
824 + ? ThreadId.make("missing")
825 + : scenario === "cycle"
826 + ? ThreadId.make("child")
827 + : ["grandchild", "running-parent", "pending-parent"].includes(scenario)
828 + ? ThreadId.make("middle")
829 + : root.id,
830 + rootThreadId: root.id,
831 + relationshipToParent: scenario === "fork" ? "fork" : "subagent",
832 + },
833 + status: scenario === "running-child" ? "running" : "completed",
834 + pendingBackgroundTasks:
835 + scenario === "pending-child" ? [{ taskId: "task", kind: "command" }] : [],
836 + latestRunCompletedAt: scenario === "recent-child-activity" ? at(-2 * DAY_MS) : null,
837 + settledOverride: scenario === "unsettled-child" ? "active" : null,
838 + pinnedAt: scenario === "pinned-child" ? at(-DAY_MS) : null,
839 + });
840 + const middle = shell({
841 + id: ThreadId.make("middle"),
842 + status: scenario === "running-parent" ? "running" : "idle",
843 + pendingBackgroundTasks:
844 + scenario === "pending-parent" ? [{ taskId: "task", kind: "command" }] : [],
845 + lineage: {
846 + parentThreadId: root.id,
847 + rootThreadId: root.id,
848 + relationshipToParent: "subagent",
849 + },
850 + });
851 + fixture.setThreads([child, middle, root]);
852 + const report = yield* fixture.service.runNow;
853 + const removed = ["child", "grandchild", "immediate"].includes(scenario);
854 + expect(yield* fixture.fs.exists(fixture.worktree)).toBe(!removed);
855 + expect(report.entries[0]).toMatchObject({
856 + threadId: child.id,
857 + outcome: removed ? "removed" : "kept",
858 + reason: removed
859 + ? scenario === "immediate"
860 + ? "Settled for 0 days"
861 + : "Settled for 20 days"
862 + : scenario === "settle-grace"
863 + ? "Settled moments ago"
864 + : scenario === "recent-settle"
865 + ? "Settled for 0 of 8 days"
866 + : scenario === "recent-child-activity"
867 + ? "Settled for 2 of 8 days"
868 + : scenario === "running-child" || scenario === "pending-child"
869 + ? "Thread is running or has pending work"
870 + : "Not settled",
871 + });
872 + }).pipe(Effect.provide(cleanupTestLayer), Effect.scoped),
873 + );
874 +
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 + ),
935 + );
936 +
937 it.effect.each([
938 "idle",
939 "grandchild",
apps/server/src/storageCleanup.ts
+68 -4
@@ -14,6 +14,7 @@ import type {
14 WorktreeCleanupRules,
15 StorageCleanupReport,
16 StorageCleanupReportEntry,
17 + ThreadId,
18 } from "@t3tools/contracts";
19 import { resolveWorktreeCleanup } from "@t3tools/shared/projectSettings";
20 import { makeDrainableWorker } from "@t3tools/shared/DrainableWorker";
@@ -124,9 +125,16 @@ export function storageCleanupThreadIdle(
125 thread: OrchestrationV2ThreadShell,
126 now: number,
127 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,
136 ): boolean {
137 return (
129 - thread.worktreePath !== null &&
138 thread.activeRunId === null &&
139 (thread.status === "idle" ||
140 thread.status === "completed" ||
@@ -213,6 +221,36 @@ function worktreeOwner<T extends Pick<OrchestrationV2ThreadShell, "id" | "lineag
221 return owner;
222 }
223
224 +function worktreeSettlementChain(
225 + thread: OrchestrationV2ThreadShell,
226 + byId: ReadonlyMap<ThreadId, OrchestrationV2ThreadShell>,
227 + now: number,
228 + settings: LimitRecoverySettings,
229 +) {
230 + const chain = [thread];
231 + let current = thread;
232 + while (current.settledAt === null) {
233 + if (
234 + current.lineage.relationshipToParent !== "subagent" ||
235 + current.settledOverride === "active" ||
236 + current.pinnedAt != null
237 + )
238 + return [];
239 + const parent = byId.get(current.lineage.parentThreadId!);
240 + if (
241 + parent === undefined ||
242 + chain.some((member) => member.id === parent.id) ||
243 + parent.projectId !== thread.projectId ||
244 + parent.lineage.rootThreadId !== thread.lineage.rootThreadId ||
245 + !threadIdle(parent, now, settings)
246 + )
247 + return [];
248 + chain.push(parent);
249 + current = parent;
250 + }
251 + return chain;
252 +}
253 +
254 /** Squash/rebase merges require the merged PR to name this exact head commit. */
255 export function storageCleanupPullRequestMerged(
256 pullRequest: Pick<
@@ -775,6 +813,7 @@ const make = Effect.gen(function* () {
813 ),
814 )).filter((thread) => thread.worktreePath !== null && thread.branch !== null);
815 const snapshot = yield* readThreads();
816 + const threadsById = new Map(snapshot.threads.map((thread) => [thread.id, thread]));
817 const refreshedDefaultRefs = new Map<string, Set<string>>();
818 const defaultBranches = new Map<string, string | null>();
819 const groups = Map.groupBy(
@@ -808,6 +847,10 @@ const make = Effect.gen(function* () {
847 const group = groups.get(worktreePath) ?? [];
848 const activityAt = Math.max(...group.map(storageCleanupActivityAt));
849 const deleted = "workspaceRoot" in thread;
850 + const settlementChain = deleted
851 + ? []
852 + : worktreeSettlementChain(thread, threadsById, now, serverSettings);
853 + const settledThread = settlementChain.at(-1);
854 const members = deleted ? [thread, ...group] : group;
855 const project = deleted
856 ? { workspaceRoot: thread.workspaceRoot }
@@ -862,7 +905,7 @@ const make = Effect.gen(function* () {
905 const changes = yield* localChangesReason(worktreePath, settings);
906 if (changes !== null) return keep(changes);
907 if (
865 - group.some(
908 + [...group, ...settlementChain.slice(1)].some(
909 (member) =>
910 member.settledAt !== null &&
911 now - Math.max(DateTime.toEpochMillis(member.updatedAt), activityAt) <
@@ -876,9 +919,9 @@ const make = Effect.gen(function* () {
919 settings.worktreeAfterDays !== null &&
920 activityAt < now - settings.worktreeAfterDays * DAY_MS;
921 const settledFor =
879 - deleted || thread.settledAt === null
922 + settledThread?.settledAt == null
923 ? null
881 - : now - Math.max(DateTime.toEpochMillis(thread.settledAt), activityAt);
924 + : now - Math.max(DateTime.toEpochMillis(settledThread.settledAt), activityAt);
925 const settledDays = settledFor === null ? null : Math.floor(settledFor / DAY_MS);
926 const settled =
927 settledFor !== null &&
@@ -1045,6 +1088,27 @@ const make = Effect.gen(function* () {
1088 })
1089 )
1090 return waitForStop("thread activity or shared worktree changed since check");
1091 + if (settlementChain.length > 1) {
1092 + const ancestors = yield* Effect.forEach(settlementChain.slice(1), (ancestor) =>
1093 + projections.getThreadShell(ancestor.id),
1094 + );
1095 + const latestSettlement = worktreeSettlementChain(
1096 + latestGroup.find((shell) => shell.id === thread.id)!,
1097 + new Map(
1098 + [...latestGroup, ...ancestors.filter((ancestor) => ancestor !== null)].map(
1099 + (ancestor) => [ancestor.id, ancestor],
1100 + ),
1101 + ),
1102 + now,
1103 + serverSettings,
1104 + ).at(-1);
1105 + if (
1106 + latestSettlement?.id !== settledThread?.id ||
1107 + !Equal.equals(latestSettlement?.settledAt, settledThread?.settledAt) ||
1108 + !Equal.equals(latestSettlement?.updatedAt, settledThread?.updatedAt)
1109 + )
1110 + return "parent settlement changed since check";
1111 + }
1112 // Sessions can outlive their run and can be shared across app threads.
1113 const sessionRows = yield* sql<{ payload_json: string }>`
1114 SELECT payload_json FROM orchestration_v2_projection_provider_sessions