Skip to content

Commit b76a595

Browse files
StiensWoutcodexclaudeGrok 4.7
authored
perf(server): skip unnecessary worktree cleanup scans (pingdotgg#32933)
Co-authored-by: GPT 6.1 Sol <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: Grok 4.7 <[email protected]>
1 parent 564b2f9 commit b76a595

2 files changed

Lines changed: 157 additions & 73 deletions

File tree

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import * as Orchestrator from "./orchestration-v2/Orchestrator.ts";
3434
import * as TerminalManager from "./terminal/Manager.ts";
3535
import { CodexProviderCapabilitiesV2 } from "./orchestration-v2/Adapters/CodexAdapterV2.ts";
3636
import { ServerActivation } from "./serverActivation.ts";
37+
import { withWorkspaceLease } from "./workspace/workspaceLease.ts";
3738
import { describe, expect, it } from "@effect/vitest";
3839
import {
3940
EventId,
@@ -350,6 +351,7 @@ const cleanupFixture = (
350351
let onLiveCheck: Effect.Effect<void> = Effect.void;
351352
let outboxStatus: string | null = null;
352353
let fullStatusCalls = 0;
354+
let gitInspections = 0;
353355
let snapshotReads = 0;
354356
let terminalListener: ((event: TerminalMetadataStreamEvent) => Effect.Effect<void>) | undefined;
355357
let events: Stream.Stream<OrchestrationV2DomainEvent> = Stream.empty;
@@ -359,6 +361,7 @@ const cleanupFixture = (
359361
...git,
360362
execute: (input) =>
361363
Effect.gen(function* () {
364+
gitInspections++;
362365
// Before Git's own clean check: inside `git worktree remove`, or on
363366
// Windows the same status check the removal makes before deleting.
364367
if (
@@ -554,6 +557,7 @@ const cleanupFixture = (
554557
...paths,
555558
},
556559
}) ?? Effect.void,
560+
gitInspections: () => gitInspections,
557561
fullStatusCalls: () => fullStatusCalls,
558562
snapshotReads: () => snapshotReads,
559563
sessionLiveChecks: () => sessionLiveChecks,
@@ -2240,6 +2244,67 @@ describe("storage cleanup reports and local file policies", () => {
22402244
}),
22412245
),
22422246
);
2247+
it.live.each([
2248+
{ days: 8, reason: "Inactive for 1 of 8 days" },
2249+
{ days: null, reason: "No rules apply" },
2250+
])("keeps an ineligible age/delete-only worktree without Git ($days)", ({ days, reason }) =>
2251+
runCleanupTest(
2252+
Effect.gen(function* () {
2253+
const fixture = yield* cleanupFixture({
2254+
...DEFAULT_SERVER_SETTINGS.storageCleanup,
2255+
worktreeAfterDays: days,
2256+
worktreeOnDelete: true,
2257+
});
2258+
fixture.setThreads([
2259+
shell({
2260+
branch: "feature",
2261+
worktreePath: fixture.worktree,
2262+
latestUserMessageAt: DateTime.makeUnsafe(
2263+
DateTime.toEpochMillis(yield* DateTime.now) - DAY_MS,
2264+
),
2265+
}),
2266+
]);
2267+
yield* fixture.fs.writeFileString(`${fixture.worktree}/tracked.txt`, "local edit\n");
2268+
const report = yield* fixture.service.runNow;
2269+
expect(report.entries[0]).toMatchObject({
2270+
outcome: "kept",
2271+
reason,
2272+
bytes: null,
2273+
});
2274+
expect(fixture.gitInspections()).toBe(0);
2275+
expect(yield* fixture.fs.exists(fixture.worktree)).toBe(true);
2276+
}),
2277+
),
2278+
);
2279+
it.live("lets a turn acquire the checkout during accounting and protects its new activity", () =>
2280+
runCleanupTest(
2281+
Effect.gen(function* () {
2282+
const fixture = yield* pausedMeasurementFixture();
2283+
const cleanup = yield* fixture.service.runNow.pipe(Effect.forkChild);
2284+
yield* Deferred.await(fixture.measuring);
2285+
yield* withWorkspaceLease(
2286+
fixture.worktree,
2287+
Effect.sync(() => {
2288+
fixture.setThreads([
2289+
shell({
2290+
branch: "feature",
2291+
worktreePath: fixture.worktree,
2292+
status: "running",
2293+
}),
2294+
]);
2295+
}),
2296+
);
2297+
yield* Deferred.succeed(fixture.resume, undefined);
2298+
const report = yield* Fiber.join(cleanup);
2299+
expect(report.entries[0]).toMatchObject({
2300+
outcome: "kept",
2301+
reason: "Thread activity or shared worktree changed since check",
2302+
bytes: null,
2303+
});
2304+
expect(yield* fixture.fs.exists(fixture.worktree)).toBe(true);
2305+
}),
2306+
),
2307+
);
22432308
it.live("still removes a worktree when measuring its size fails", () =>
22442309
runCleanupTest(
22452310
Effect.gen(function* () {

‎apps/server/src/storageCleanup.ts‎

Lines changed: 92 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,20 @@ const make = Effect.gen(function* () {
836836
);
837837
if (root !== null && !isFilesystemRoot(root, path)) roots.push(root);
838838
}
839+
const worktreePathReason = Effect.fn("StorageCleanup.worktreePathReason")(function* (
840+
worktreePath: string,
841+
) {
842+
// Allow symlinked parents, never a symlinked worktree directory.
843+
const realPath = yield* fs.realPath(worktreePath);
844+
const realParent = yield* fs.realPath(path.dirname(worktreePath));
845+
if (realPath !== path.join(realParent, path.basename(worktreePath)))
846+
return "worktree is a symbolic link";
847+
if (!roots.some((root) => inside(root, realPath)))
848+
return "outside the managed worktree folder";
849+
if ((yield* fs.stat(path.join(worktreePath, ".git"))).type !== "File")
850+
return "not a linked worktree";
851+
return null;
852+
});
839853
const deletedRows = yield* sql<{ payload_json: string; workspaceRoot: string }>`
840854
SELECT t.payload_json, p.workspace_root AS "workspaceRoot"
841855
FROM orchestration_v2_projection_threads t
@@ -924,21 +938,6 @@ const make = Effect.gen(function* () {
924938
if (group.some((member) => member.branch !== thread.branch))
925939
return keep("a subagent thread's branch differs from this worktree's branch");
926940
if (hasTerminal(worktreePath)) return keep(waitForStop("open terminal"));
927-
// Compare canonical roots while allowing symlinked parents, never symlinked worktree directories.
928-
const realPath = yield* fs.realPath(worktreePath);
929-
const realParent = yield* fs.realPath(path.dirname(worktreePath));
930-
if (realPath !== path.join(realParent, path.basename(worktreePath)))
931-
return keep("worktree is a symbolic link");
932-
if (!roots.some((root) => inside(root, realPath)))
933-
return keep("outside the managed worktree folder");
934-
if (yield* containsProjectRoot(worktreePath, [project, ...snapshot.projects]))
935-
return keep("contains a project checkout");
936-
if ((yield* fs.stat(path.join(worktreePath, ".git"))).type !== "File")
937-
return keep("not a linked worktree");
938-
if (yield* branchChanged(worktreePath, thread.branch))
939-
return keep("repository or branch changed");
940-
const changes = yield* localChangesReason(worktreePath, settings);
941-
if (changes !== null) return keep(changes);
942941
if (
943942
[...group, ...settlementChain.slice(1)].some(
944943
(member) =>
@@ -948,7 +947,6 @@ const make = Effect.gen(function* () {
948947
)
949948
)
950949
return keep(waitForStop("settled moments ago"));
951-
const head = yield* git.resolveCommit({ cwd: worktreePath, revision: "HEAD" });
952950
const old =
953951
!deleted &&
954952
settings.worktreeAfterDays !== null &&
@@ -968,7 +966,31 @@ const make = Effect.gen(function* () {
968966
: old
969967
? `inactive for ${Math.floor((now - activityAt) / DAY_MS)} days`
970968
: `settled for ${settledDays} days`;
971-
if (!eligible && (settings.worktreeUnchanged || settings.worktreeOnMerge)) {
969+
const notDueReason =
970+
!deleted && settings.worktreeAfterDays !== null
971+
? `inactive for ${Math.floor((now - activityAt) / DAY_MS)} of ${settings.worktreeAfterDays} days`
972+
: settings.worktreeSettledAfterDays !== null
973+
? settledFor === null
974+
? "not settled"
975+
: `settled for ${settledDays} of ${settings.worktreeSettledAfterDays} days`
976+
: settings.worktreeOnMerge
977+
? "not merged"
978+
: settings.worktreeUnchanged
979+
? "has commits beyond the default branch"
980+
: "no rules apply";
981+
// Only Git-based rules can make a not-yet-due worktree eligible, so skip Git otherwise.
982+
if (!eligible && !settings.worktreeUnchanged && !settings.worktreeOnMerge)
983+
return keep(notDueReason);
984+
const pathReason = yield* worktreePathReason(worktreePath);
985+
if (pathReason !== null) return keep(pathReason);
986+
if (yield* containsProjectRoot(worktreePath, [project, ...snapshot.projects]))
987+
return keep("contains a project checkout");
988+
if (yield* branchChanged(worktreePath, thread.branch))
989+
return keep("repository or branch changed");
990+
const changes = yield* localChangesReason(worktreePath, settings);
991+
if (changes !== null) return keep(changes);
992+
const head = yield* git.resolveCommit({ cwd: worktreePath, revision: "HEAD" });
993+
if (!eligible) {
972994
const repositoryCwd = path.resolve(project.workspaceRoot);
973995
const remotes = yield* git.execute({
974996
operation: "StorageCleanup.listRemotes",
@@ -1063,18 +1085,7 @@ const make = Effect.gen(function* () {
10631085
}
10641086
if (!eligible && remote === null) return keep("has commits beyond the project branch");
10651087
}
1066-
if (!eligible)
1067-
return keep(
1068-
!deleted && settings.worktreeAfterDays !== null
1069-
? `inactive for ${Math.floor((now - activityAt) / DAY_MS)} of ${settings.worktreeAfterDays} days`
1070-
: settings.worktreeSettledAfterDays !== null
1071-
? settledFor === null
1072-
? "not settled"
1073-
: `settled for ${settledDays} of ${settings.worktreeSettledAfterDays} days`
1074-
: settings.worktreeOnMerge
1075-
? "not merged"
1076-
: "has commits beyond the default branch",
1077-
);
1088+
if (!eligible) return keep(notDueReason);
10781089
// Re-read this path's threads and sessions so new activity cancels removal; full per-worktree snapshots take seconds.
10791090
const blockedSinceCheck = Effect.fnUntraced(function* () {
10801091
const latestProjects = yield* projectStore.listShells();
@@ -1165,53 +1176,61 @@ const make = Effect.gen(function* () {
11651176
});
11661177
const blocked = yield* blockedSinceCheck();
11671178
if (blocked !== null) return keep(blocked);
1179+
// Size is an estimate; provider startup must not wait for directory traversal.
11681180
const bytes = yield* measureWorktree(worktreePath);
1169-
const blockedWhileMeasuring = yield* blockedSinceCheck();
1170-
if (blockedWhileMeasuring !== null) return keep(blockedWhileMeasuring);
1171-
if (yield* branchChanged(worktreePath, thread.branch))
1172-
return keep("repository or branch changed since check");
1173-
if (
1174-
(yield* git.resolveCommit({ cwd: worktreePath, revision: "HEAD" })).commitSha !==
1175-
head.commitSha
1176-
)
1177-
return keep("commit changed since check");
1178-
const finalChanges = yield* localChangesReason(worktreePath, settings);
1179-
if (finalChanges !== null) return keep(finalChanges);
1180-
const currentSettings = yield* settingsService.getSettings;
1181-
const current = resolveWorktreeCleanup(currentSettings, thread.projectId);
1182-
if (
1183-
currentSettings.autoResumeLimitedThreads !== serverSettings.autoResumeLimitedThreads ||
1184-
currentSettings.snoozeLimitedThreads !== serverSettings.snoozeLimitedThreads ||
1185-
Object.keys(settings).some(
1186-
(key) =>
1187-
current[key as keyof typeof settings] !== settings[key as keyof typeof settings],
1188-
)
1189-
)
1190-
return keep("settings changed since check");
1191-
// Clean only untracked files. Ordinary worktrees retain Git's final tracked-edit check.
1192-
if (settings.worktreeKeepWhen === "tracked-changes")
1193-
yield* cleanupGit(worktreePath, ["clean", "-ffdx"], "git clean");
1194-
const submodules = yield* submoduleRemoval(worktreePath, settings);
1195-
if (submodules.reason !== null) return keep(submodules.reason);
1196-
if (submodules.force) {
1197-
const changes = yield* localChangesReason(worktreePath, settings);
1198-
if (changes !== null) return keep(changes);
1199-
}
1200-
yield* removeLinkedWorktree({
1201-
operation: "StorageCleanup.cleanupGit",
1202-
cwd: project.workspaceRoot,
1181+
yield* withWorkspaceLease(
12031182
worktreePath,
1204-
// Git requires force for submodules; recursive checks above cannot make removal atomic.
1205-
force: submodules.force,
1206-
execute: git.execute,
1207-
run: (args) => cleanupGit(project.workspaceRoot, [...args], "git worktree remove"),
1208-
});
1209-
entries.push({ ...entry, outcome: "removed", reason: sentence(removalReason), bytes });
1210-
yield* gitManager.invalidateStatus(project.workspaceRoot);
1211-
// Preserve branch/path so ProviderTurnStartService can recreate the checkout on resume.
1212-
yield* Effect.logInfo("storage cleanup removed worktree", { threadId: thread.id });
1183+
Effect.gen(function* () {
1184+
const pathReason = yield* worktreePathReason(worktreePath);
1185+
if (pathReason !== null) return keep(pathReason);
1186+
const blockedWhileMeasuring = yield* blockedSinceCheck();
1187+
if (blockedWhileMeasuring !== null) return keep(blockedWhileMeasuring);
1188+
if (yield* branchChanged(worktreePath, thread.branch))
1189+
return keep("repository or branch changed since check");
1190+
if (
1191+
(yield* git.resolveCommit({ cwd: worktreePath, revision: "HEAD" })).commitSha !==
1192+
head.commitSha
1193+
)
1194+
return keep("commit changed since check");
1195+
const finalChanges = yield* localChangesReason(worktreePath, settings);
1196+
if (finalChanges !== null) return keep(finalChanges);
1197+
const currentSettings = yield* settingsService.getSettings;
1198+
const current = resolveWorktreeCleanup(currentSettings, thread.projectId);
1199+
if (
1200+
currentSettings.autoResumeLimitedThreads !==
1201+
serverSettings.autoResumeLimitedThreads ||
1202+
currentSettings.snoozeLimitedThreads !== serverSettings.snoozeLimitedThreads ||
1203+
Object.keys(settings).some(
1204+
(key) =>
1205+
current[key as keyof typeof settings] !== settings[key as keyof typeof settings],
1206+
)
1207+
)
1208+
return keep("settings changed since check");
1209+
// Clean only untracked files. Ordinary worktrees retain Git's final tracked-edit check.
1210+
if (settings.worktreeKeepWhen === "tracked-changes")
1211+
yield* cleanupGit(worktreePath, ["clean", "-ffdx"], "git clean");
1212+
const submodules = yield* submoduleRemoval(worktreePath, settings);
1213+
if (submodules.reason !== null) return keep(submodules.reason);
1214+
if (submodules.force) {
1215+
const changes = yield* localChangesReason(worktreePath, settings);
1216+
if (changes !== null) return keep(changes);
1217+
}
1218+
yield* removeLinkedWorktree({
1219+
operation: "StorageCleanup.cleanupGit",
1220+
cwd: project.workspaceRoot,
1221+
worktreePath,
1222+
// Git requires force for submodules; recursive checks above cannot make removal atomic.
1223+
force: submodules.force,
1224+
execute: git.execute,
1225+
run: (args) => cleanupGit(project.workspaceRoot, [...args], "git worktree remove"),
1226+
});
1227+
entries.push({ ...entry, outcome: "removed", reason: sentence(removalReason), bytes });
1228+
yield* gitManager.invalidateStatus(project.workspaceRoot);
1229+
// Preserve branch/path so ProviderTurnStartService can recreate the checkout on resume.
1230+
yield* Effect.logInfo("storage cleanup removed worktree", { threadId: thread.id });
1231+
}),
1232+
);
12131233
}).pipe(
1214-
(effect) => withWorkspaceLease(worktreePath, effect),
12151234
Effect.catch((error) =>
12161235
Effect.gen(function* () {
12171236
entries.push({

0 commit comments

Comments
 (0)