Skip to content

Commit 4acd0b5

Browse files
fix(server): storage cleanup prunes idle No project folders under ~/.t3/scratch
Cleanup kept every thread without a branch as "Thread is running or has pending work", and every later step accepted only linked Git worktrees in the managed worktree folders. No project threads never have a branch, so their folders under the T3 home's scratch folder were never checked against retention, and leftovers there were never listed. A branchless thread whose folder sits directly in the scratch root now follows the inactive and settled retention rules, with the same idle, terminal, session and lease checks as a worktree, and is removed as a plain folder. Nothing can restore its files, so a folder is kept when it holds a Git repository at any depth (checked before and under the lease), when its thread has an enabled scheduled task, or under "Any local files" when it holds anything but node_modules. Scratch folders no thread uses are listed in the report. Turn start and terminal launch recreate a missing folder directly inside the No project's root, so a pruned thread can run again. Fixes #26406 Co-Authored-By: Claude Fable 5.2 <[email protected]>
1 parent 1340a56 commit 4acd0b5

6 files changed

Lines changed: 355 additions & 116 deletions

File tree

‎apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,57 @@ effectIt.effect("restores a missing worktree before a send only under the worksp
320320
}).pipe(Effect.provide(layerRestoreLeaseTest)),
321321
);
322322

323+
it.each([
324+
{ label: "directly in the scratch root", folder: "/tmp/t3-home/scratch/notes", recreated: true },
325+
{ label: "below another folder", folder: "/tmp/t3-home/scratch/notes/deep", recreated: false },
326+
{
327+
label: "in another project",
328+
folder: "/tmp/elsewhere/notes",
329+
workspaceRoot: "/tmp/elsewhere",
330+
recreated: false,
331+
},
332+
])("recreates a pruned No project folder only $label", async (input) => {
333+
const makeDirectory = vi.fn(() => Effect.void);
334+
await Effect.gen(function* () {
335+
const { restore } = yield* ThreadWorktreeRestore;
336+
yield* restore({
337+
id: ThreadId.make("thread_scratch_restore"),
338+
projectId: ProjectId.make("project_scratch_restore"),
339+
worktreePath: input.folder,
340+
branch: null,
341+
});
342+
}).pipe(
343+
Effect.provide(
344+
layerThreadWorktreeRestore.pipe(
345+
Layer.provide(
346+
Layer.mergeAll(
347+
Layer.succeed(FileSystem.FileSystem, {
348+
exists: () => Effect.succeed(false),
349+
realPath: (target: string) => Effect.succeed(target),
350+
makeDirectory,
351+
} as never),
352+
Path.layer,
353+
Layer.mock(ServerConfig.ServerConfig)({
354+
baseDir: "/tmp/t3-home",
355+
worktreesDir: managedWorktreesDir,
356+
} as ServerConfig.ServerConfig["Service"]),
357+
ServerSettings.layerTest(),
358+
Layer.mock(GitWorkflow.GitWorkflowService)({}),
359+
Layer.mock(ProjectService.ProjectService)({
360+
getById: () =>
361+
Effect.succeedSome({
362+
workspaceRoot: input.workspaceRoot ?? "/tmp/t3-home/scratch",
363+
} as never),
364+
}),
365+
),
366+
),
367+
),
368+
),
369+
Effect.runPromise,
370+
);
371+
expect(makeDirectory).toHaveBeenCalledTimes(input.recreated ? 1 : 0);
372+
});
373+
323374
function makeLocalCommandHarness(input: {
324375
readonly text: string;
325376
readonly nativeGoals?: boolean;

‎apps/server/src/project/ManagedProjectFolders.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ export class ScratchProjectError extends Schema.TaggedError<ScratchProjectError>
6666
}
6767
}
6868

69+
/** Where No project threads keep their folders, whether or not this T3 home allows them. */
70+
export const scratchFolderRoot = (baseDir: string, path: Path.Path) =>
71+
path.resolve(baseDir, "scratch");
72+
6973
export type ScratchError = ScratchUnavailableError | ScratchFolderError | ScratchProjectError;
7074

7175
// The spawner checks cwd before spawning, so NotFound from spawn itself means a missing executable.
@@ -255,7 +259,7 @@ const make = Effect.gen(function* () {
255259
Effect.flatMap(
256260
(repositoryRoot): Effect.Effect<Result.Result<string, ScratchUnavailableError>> => {
257261
if (repositoryRoot === null) {
258-
return Effect.succeed(Result.succeed(path.resolve(config.baseDir, "scratch")));
262+
return Effect.succeed(Result.succeed(scratchFolderRoot(config.baseDir, path)));
259263
}
260264
const unavailable = new ScratchUnavailableError({
261265
baseDir: config.baseDir,

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

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ const cleanupFixture = (
357357
let sessionLiveChecks = 0;
358358
let onLiveCheck: Effect.Effect<void> = Effect.void;
359359
let outboxStatus: string | null = null;
360+
let scheduledThreadIds: string[] = [];
360361
let fullStatusCalls = 0;
361362
let gitInspections = 0;
362363
let snapshotReads = 0;
@@ -467,7 +468,9 @@ const cleanupFixture = (
467468
...threads.flatMap((thread) => thread.worktreePath ?? []),
468469
...storedWorktreePaths,
469470
].map((worktreePath) => ({ worktreePath }))
470-
: [],
471+
: strings.join("").includes("scheduled_tasks")
472+
? scheduledThreadIds.map((threadId) => ({ threadId }))
473+
: [],
471474
)) as unknown as SqlClient.SqlClient),
472475
Effect.provideService(GitManager.GitManager, {
473476
invalidateStatus: () => Effect.void,
@@ -533,6 +536,9 @@ const cleanupFixture = (
533536
setOutboxStatus: (status: string | null) => {
534537
outboxStatus = status;
535538
},
539+
setScheduledThreadIds: (next: string[]) => {
540+
scheduledThreadIds = next;
541+
},
536542
setDeletedThreads: (next: OrchestrationV2ThreadShell[]) => {
537543
deletedThreads = next;
538544
},
@@ -2354,6 +2360,71 @@ describe("storage cleanup reports and local file policies", () => {
23542360
}),
23552361
),
23562362
);
2363+
it.effect.each([
2364+
{
2365+
name: "a Git repository in a subfolder",
2366+
policy: "tracked-changes",
2367+
repository: "clone/.git",
2368+
reason: "Contains a nested Git repository",
2369+
},
2370+
{
2371+
name: "a Git repository of its own",
2372+
policy: "tracked-changes",
2373+
repository: ".git",
2374+
reason: "Contains a nested Git repository",
2375+
},
2376+
{
2377+
name: "files under Any local files",
2378+
policy: "any-local-files",
2379+
reason: "Has local files (notes.md)",
2380+
},
2381+
{ name: "a scheduled task", scheduled: true, reason: "Thread has a scheduled task" },
2382+
] as const)(
2383+
"keeps an idle No project thread's scratch folder with $name",
2384+
({
2385+
policy,
2386+
repository,
2387+
scheduled,
2388+
reason,
2389+
}: {
2390+
readonly policy?: WorktreeKeepWhen;
2391+
readonly repository?: string;
2392+
readonly scheduled?: boolean;
2393+
readonly reason: string;
2394+
}) =>
2395+
runCleanupTest(
2396+
Effect.gen(function* () {
2397+
yield* TestClock.setTime(NOW_MS);
2398+
const fixture = yield* cleanupFixture();
2399+
const scratchRoot = `${fixture.config.baseDir}/scratch`;
2400+
const folder = `${scratchRoot}/quiet-otter`;
2401+
yield* fixture.fs.makeDirectory(
2402+
repository === undefined ? folder : `${folder}/${repository}`,
2403+
{ recursive: true },
2404+
);
2405+
yield* fixture.fs.writeFileString(`${folder}/notes.md`, "scratch\n");
2406+
// No thread owns this one, so it is listed and never removed.
2407+
const leftover = `${scratchRoot}/leftover`;
2408+
yield* fixture.fs.makeDirectory(leftover);
2409+
yield* fixture.fs.utimes(leftover, 1, 1);
2410+
fixture.setProjectRoots([scratchRoot]);
2411+
fixture.setStoredWorktreePaths([]);
2412+
if (policy !== undefined) fixture.setPolicy(policy);
2413+
if (scheduled) fixture.setScheduledThreadIds(["thread-1"]);
2414+
fixture.setThreads([
2415+
shell({ worktreePath: folder, settledAt: at(-20 * DAY_MS), status: "completed" }),
2416+
]);
2417+
const report = yield* fixture.service.runNow;
2418+
expect(
2419+
report.entries.map(({ path, outcome, reason }) => ({ path, outcome, reason })),
2420+
).toEqual([
2421+
{ path: leftover, outcome: "kept", reason: "No thread uses this folder" },
2422+
{ path: folder, outcome: "kept", reason },
2423+
]);
2424+
expect(yield* fixture.fs.exists(`${folder}/notes.md`)).toBe(true);
2425+
}),
2426+
),
2427+
);
23572428
it.effect("keeps a thread's worktree folder that lost its .git link", () =>
23582429
runCleanupTest(
23592430
Effect.gen(function* () {

0 commit comments

Comments
 (0)