Skip to content

Commit 6807c3f

Browse files
t3dotggclaude
andauthored
fix(server): file search no longer crashes the server, and a failed Edit from here keeps your files (#35291)
Co-authored-by: Claude Fable 5.2 <[email protected]>
1 parent 3ffd38e commit 6807c3f

6 files changed

Lines changed: 127 additions & 212 deletions

File tree

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,10 @@ it.effect.each([
177177
{ order: "cleanup-first", restoreFiles: true, leavesFolder: true },
178178
{ order: "cleanup-first", restoreFiles: false, leavesFolder: true },
179179
{ order: "cleanup-first", restoreFiles: true, leavesFolder: true, shared: true },
180-
])("locks rollback with %s", ({ order, restoreFiles, unmanaged, leavesFolder, shared }) =>
180+
{ order: "cleanup-first", restoreFiles: true, leavesFolder: true, createFails: true },
181+
])("locks rollback with %s", (row) =>
181182
Effect.gen(function* () {
183+
const { order, restoreFiles, unmanaged, leavesFolder, shared, createFails } = row;
182184
const fileSystem = yield* FileSystem.FileSystem;
183185
const path = yield* Path.Path;
184186
const workspaceRoot = yield* fileSystem.realPath(
@@ -249,6 +251,7 @@ it.effect.each([
249251
path: worktreePath,
250252
});
251253
calls.push("create");
254+
if (createFails) return yield* Effect.die("the branch is gone");
252255
yield* fileSystem
253256
.makeDirectory(path.join(worktreePath, ".git"), { recursive: true })
254257
.pipe(Effect.orDie);
@@ -362,6 +365,16 @@ it.effect.each([
362365
}
363366
return;
364367
}
368+
if (createFails) {
369+
yield* Fiber.join(rollback).pipe(Effect.exit);
370+
assert.deepEqual(calls, ["cleanup", "prune", "create"]);
371+
assert.equal(
372+
yield* fileSystem.readFileString(path.join(worktreePath, "unsaved.txt")),
373+
"keep",
374+
);
375+
assert.deepEqual(yield* fileSystem.readDirectory(path.dirname(worktreePath)), ["feature"]);
376+
return;
377+
}
365378
yield* Fiber.join(rollback);
366379
if (laterCleanup !== undefined) yield* Fiber.join(laterCleanup);
367380
const rollbackCalls = restoreFiles

‎apps/server/src/orchestration-v2/CheckpointRollbackService.ts‎

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
import * as Context from "effect/Context";
1111
import * as DateTime from "effect/DateTime";
1212
import * as Effect from "effect/Effect";
13+
import * as Exit from "effect/Exit";
1314
import * as FileSystem from "effect/FileSystem";
1415
import * as Layer from "effect/Layer";
1516
import * as Option from "effect/Option";
@@ -246,16 +247,52 @@ export const layer: Layer.Layer<
246247
) {
247248
const project = yield* projects.get(context.thread.projectId);
248249
if (Option.isSome(project)) {
250+
const recreate = gitWorkflow.pruneWorktrees({ cwd: project.value.workspaceRoot }).pipe(
251+
Effect.andThen(
252+
gitWorkflow.createWorktree({
253+
cwd: project.value.workspaceRoot,
254+
refName: branch,
255+
path: worktreePath,
256+
}),
257+
),
258+
);
249259
if (leftover) {
250260
if (!(yield* isolated)) return yield* sharedWorkspace();
251-
yield* fileSystem.remove(worktreePath, { recursive: true, force: true });
261+
// Set aside, not removed: a checkout that fails puts the folder and its files back.
262+
yield* Effect.acquireUseRelease(
263+
Effect.gen(function* () {
264+
const aside = yield* fileSystem.makeTempDirectory({
265+
directory: path.dirname(worktreePath),
266+
prefix: ".t3-replaced-",
267+
});
268+
yield* fileSystem.rename(worktreePath, path.join(aside, "folder"));
269+
return aside;
270+
}),
271+
() => recreate,
272+
(aside, exit) =>
273+
(Exit.isSuccess(exit)
274+
? Effect.void
275+
: fileSystem
276+
.remove(worktreePath, { recursive: true, force: true })
277+
.pipe(
278+
Effect.andThen(
279+
fileSystem.rename(path.join(aside, "folder"), worktreePath),
280+
),
281+
)
282+
).pipe(
283+
Effect.andThen(fileSystem.remove(aside, { recursive: true, force: true })),
284+
Effect.catchCause((cause) =>
285+
Effect.logWarning("Edit from here left a worktree folder set aside", {
286+
worktreePath,
287+
setAside: path.join(aside, "folder"),
288+
cause,
289+
}),
290+
),
291+
),
292+
);
293+
} else {
294+
yield* recreate;
252295
}
253-
yield* gitWorkflow.pruneWorktrees({ cwd: project.value.workspaceRoot });
254-
yield* gitWorkflow.createWorktree({
255-
cwd: project.value.workspaceRoot,
256-
refName: branch,
257-
path: worktreePath,
258-
});
259296
}
260297
}
261298
}

‎apps/server/src/workspace/WorkspaceEntries.test.ts‎

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// @effect-diagnostics nodeBuiltinImport:off
22
import * as NodeFSP from "node:fs/promises";
33
import * as NodeServices from "@effect/platform-node/NodeServices";
4+
import { FileFinder } from "@ff-labs/fff-node";
45
import { it, afterEach, describe, expect } from "@effect/vitest";
56
import * as Effect from "effect/Effect";
67
import * as FileSystem from "effect/FileSystem";
@@ -12,7 +13,6 @@ import { vi } from "vite-plus/test";
1213
import * as ServerConfig from "../config.ts";
1314
import * as HostProcess from "@t3tools/shared/HostProcess";
1415
import * as VcsProcess from "../vcs/VcsProcess.ts";
15-
import { WorkspaceFinderOpener } from "./WorkspaceFinderThread.ts";
1616
import * as WorkspaceEntries from "./WorkspaceEntries.ts";
1717
import * as WorkspacePaths from "./WorkspacePaths.ts";
1818

@@ -793,42 +793,39 @@ it.layer(layerTest, { excludeTestServices: true })("WorkspaceEntries", (it) => {
793793
let scansOutlastWait = false;
794794
let finders = 0;
795795
let rescans = 0;
796-
const openFinder = yield* WorkspaceFinderOpener;
797-
const countingOpener: typeof openFinder = async (options) => {
798-
const opened = await openFinder(options);
799-
if (!opened.ok) return opened;
800-
finders += 1;
801-
const call: typeof opened.value.call = (method, ...args) => {
802-
if (method === "scanFiles") rescans += 1;
803-
if (method === "waitForIndexReady" && scansOutlastWait) {
804-
return Promise.resolve({ ok: true, value: false } as never);
805-
}
806-
return opened.value.call(method, ...args);
807-
};
808-
return { ok: true, value: { ...opened.value, call } };
809-
};
810-
const result = yield* Effect.gen(function* () {
811-
const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries;
812-
const searchNeedle = workspaceEntries.searchContents({
813-
cwd,
814-
query: "needle",
815-
limit: 100,
816-
caseSensitive: true,
817-
wholeWord: false,
818-
useRegex: false,
819-
});
820-
yield* searchNeedle;
821-
822-
scansOutlastWait = true;
823-
yield* workspaceEntries.refresh(cwd);
824-
return yield* searchNeedle;
825-
}).pipe(
826-
Effect.provide(
827-
Layer.fresh(WorkspaceEntries.layer).pipe(
828-
Layer.provide(Layer.succeed(WorkspaceFinderOpener, countingOpener)),
829-
),
830-
),
831-
);
796+
const createFinder = FileFinder.create.bind(FileFinder);
797+
vi.spyOn(FileFinder, "create").mockImplementation((options) => {
798+
const created = createFinder(options);
799+
if (created.ok) {
800+
const finder = created.value;
801+
finders += 1;
802+
const scanFiles = finder.scanFiles.bind(finder);
803+
finder.scanFiles = () => {
804+
rescans += 1;
805+
return scanFiles();
806+
};
807+
const waitForIndexReady = finder.waitForIndexReady.bind(finder);
808+
finder.waitForIndexReady = (timeoutMs) =>
809+
scansOutlastWait
810+
? Promise.resolve({ ok: true, value: false })
811+
: waitForIndexReady(timeoutMs);
812+
}
813+
return created;
814+
});
815+
const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries;
816+
const searchNeedle = workspaceEntries.searchContents({
817+
cwd,
818+
query: "needle",
819+
limit: 100,
820+
caseSensitive: true,
821+
wholeWord: false,
822+
useRegex: false,
823+
});
824+
yield* searchNeedle;
825+
826+
scansOutlastWait = true;
827+
yield* workspaceEntries.refresh(cwd);
828+
const result = yield* searchNeedle;
832829

833830
expect(result.matches.map((match) => match.path)).toEqual(["src/notes.ts"]);
834831
// The second search ran on the rescanned live finder, not a rebuild.

‎apps/server/src/workspace/WorkspaceFinderThread.ts‎

Lines changed: 0 additions & 116 deletions
This file was deleted.

‎apps/server/src/workspace/WorkspaceSearchIndex.test.ts‎

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import type { FileFinder, FileItem, GrepCursor, GrepOptions, GrepResult } from "@ff-labs/fff-node";
1+
import {
2+
FileFinder,
3+
type FileItem,
4+
type GrepCursor,
5+
type GrepOptions,
6+
type GrepResult,
7+
} from "@ff-labs/fff-node";
28
import * as NodeServices from "@effect/platform-node/NodeServices";
39
import { afterEach, expect, it } from "@effect/vitest";
410
import * as HostProcess from "@t3tools/shared/HostProcess";
@@ -7,23 +13,12 @@ import * as FileSystem from "effect/FileSystem";
713
import * as Path from "effect/Path";
814
import { vi } from "vite-plus/test";
915

10-
import { WorkspaceFinderOpener } from "./WorkspaceFinderThread.ts";
1116
import * as WorkspaceSearchIndex from "./WorkspaceSearchIndex.ts";
1217

1318
afterEach(() => {
1419
vi.restoreAllMocks();
1520
});
1621

17-
const withInProcessFinder = (finder: FileFinder) =>
18-
Effect.provideService(WorkspaceFinderOpener, async () => ({
19-
ok: true as const,
20-
value: {
21-
call: async (method, ...args) =>
22-
(finder[method] as (...input: typeof args) => never)(...args),
23-
close: async () => undefined,
24-
},
25-
}));
26-
2722
function fileItem(relativePath: string): FileItem {
2823
return {
2924
relativePath,
@@ -58,10 +53,9 @@ it.effect("filters image searches before applying the result limit", () =>
5853
waitForIndexReady: vi.fn(async () => ({ ok: true as const, value: true })),
5954
fileSearch,
6055
} as unknown as FileFinder;
56+
vi.spyOn(FileFinder, "create").mockReturnValueOnce({ ok: true, value: finder });
6157

62-
const searchIndex = yield* WorkspaceSearchIndex.make("/workspace/project").pipe(
63-
withInProcessFinder(finder),
64-
);
58+
const searchIndex = yield* WorkspaceSearchIndex.make("/workspace/project");
6559
const resultWithoutKind = yield* searchIndex.search("", 200, undefined, true);
6660
const resultWithDirectoryKind = yield* searchIndex.search("", 200, "directory", true);
6761

@@ -122,10 +116,9 @@ it.effect("continues whole-word searches after a filtered grep page", () =>
122116
waitForIndexReady: vi.fn(async () => ({ ok: true as const, value: true })),
123117
grep,
124118
} as unknown as FileFinder;
119+
vi.spyOn(FileFinder, "create").mockReturnValueOnce({ ok: true, value: finder });
125120

126-
const searchIndex = yield* WorkspaceSearchIndex.make("/workspace/project", "content").pipe(
127-
withInProcessFinder(finder),
128-
);
121+
const searchIndex = yield* WorkspaceSearchIndex.make("/workspace/project", "content");
129122
const result = yield* searchIndex.searchContents({
130123
query: "needle",
131124
limit: 1,

0 commit comments

Comments
 (0)