Skip to content

Commit dce528d

Browse files
bfowlerclaude
andcommitted
fix(shared): relist PATH directories that change during a batch
Before reusing a listing, compare the directory's mtime with the one recorded when it was listed, and relist it if it changed. A command installed mid-scan is now found exactly as a direct probe would find it, at the cost of one directory stat per lookup instead of one per PATHEXT candidate (~1,600 stats and 275-437 ms on a 73-entry Windows PATH, vs ~34,000 stats and 7-11 s before this PR). Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 491a52f commit dce528d

3 files changed

Lines changed: 88 additions & 27 deletions

File tree

‎apps/server/src/process/externalLauncher.test.ts‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ import * as NodePath from "node:path";
66
import * as NodeServices from "@effect/platform-node/NodeServices";
77
import { assert, it } from "@effect/vitest";
88
import * as ConfigProvider from "effect/ConfigProvider";
9+
import * as DateTime from "effect/DateTime";
910
import * as Effect from "effect/Effect";
1011
import * as Fiber from "effect/Fiber";
1112
import * as FileSystem from "effect/FileSystem";
1213
import * as Layer from "effect/Layer";
14+
import * as Option from "effect/Option";
1315
import * as Path from "effect/Path";
1416
import * as Sink from "effect/Sink";
1517
import * as Stream from "effect/Stream";
@@ -1074,9 +1076,15 @@ it.effect.skipIf(windowsHost)("ignores unusable app bundles and keeps PATH launc
10741076
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
10751077
);
10761078

1079+
// Stubbed stats answer for PATH directories too, and discovery checks a
1080+
// directory's mtime before reusing its listing, so the stub carries one.
1081+
const stubFileInfo = {
1082+
type: "File",
1083+
mtime: Option.some(DateTime.toDateUtc(DateTime.makeUnsafe(0))),
1084+
} as FileSystem.File.Info;
1085+
10771086
it.effect("memoizes editor discovery and refreshes after the cache window", () => {
10781087
let statCalls = 0;
1079-
const fileInfo = { type: "File" } as FileSystem.File.Info;
10801088
const launcherLayer = ExternalLauncher.layer.pipe(
10811089
Layer.provide(
10821090
Layer.mergeAll(
@@ -1085,7 +1093,7 @@ it.effect("memoizes editor discovery and refreshes after the cache window", () =
10851093
stat: () =>
10861094
Effect.sync(() => {
10871095
statCalls += 1;
1088-
return fileInfo;
1096+
return stubFileInfo;
10891097
}),
10901098
}),
10911099
Path.layer,
@@ -1140,7 +1148,6 @@ it.effect("memoizes editor discovery and refreshes after the cache window", () =
11401148
// replayed it to every later connect for the whole TTL, so `server.getConfig`
11411149
// failed and no client could reconnect until the server restarted.
11421150
it.effect("rescans after an interrupted discovery instead of caching the interrupt", () => {
1143-
const fileInfo = { type: "File" } as FileSystem.File.Info;
11441151
let blockFirstScan = true;
11451152
let scans = 0;
11461153
const launcherLayer = ExternalLauncher.layer.pipe(
@@ -1157,7 +1164,7 @@ it.effect("rescans after an interrupted discovery instead of caching the interru
11571164
if (blockFirstScan) {
11581165
return yield* Effect.never;
11591166
}
1160-
return fileInfo;
1167+
return stubFileInfo;
11611168
}),
11621169
}),
11631170
Path.layer,
@@ -1218,9 +1225,7 @@ const stubbedWindowsDiscoveryLayer = (input: {
12181225
readDirectory: () => Effect.succeed([...input.listed]),
12191226
stat: (filePath) => {
12201227
input.onStat?.();
1221-
return input.stalls(filePath)
1222-
? Effect.never
1223-
: Effect.succeed({ type: "File" } as FileSystem.File.Info);
1228+
return input.stalls(filePath) ? Effect.never : Effect.succeed(stubFileInfo);
12241229
},
12251230
}),
12261231
Path.layer,

‎packages/shared/src/shell.test.ts‎

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -512,8 +512,36 @@ effectIt.layer(NodeServices.layer)("resolveCommandPath", (it) => {
512512
}),
513513
);
514514

515-
expect(listed).toEqual([missing, first, second]);
516-
expect(statted).toEqual([path.join(first, "cursor.CMD"), path.join(second, "explorer.EXE")]);
515+
// The missing directory is dated but never listed; directory mtime checks
516+
// aside, only names present in a listing are probed.
517+
expect(listed).toEqual([first, second]);
518+
expect(statted.filter((filePath) => ![missing, first, second].includes(filePath))).toEqual([
519+
path.join(first, "cursor.CMD"),
520+
path.join(second, "explorer.EXE"),
521+
]);
522+
}).pipe(
523+
Effect.provideService(HostProcessPlatform, "win32"),
524+
Effect.provideService(CommandResolutionCache, new Map()),
525+
),
526+
);
527+
528+
it.effect("relists a PATH directory that changed during the batch", () =>
529+
Effect.gen(function* () {
530+
const fs = yield* FileSystem.FileSystem;
531+
const path = yield* Path.Path;
532+
const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-path-listing-" });
533+
const env = { PATH: directory, PATHEXT: ".EXE" };
534+
535+
yield* Effect.gen(function* () {
536+
expect(yield* isCommandAvailable("absent", { env })).toBe(false);
537+
yield* fs.writeFileString(path.join(directory, "installed.EXE"), "");
538+
// mtime has millisecond precision; move it clearly past the listing
539+
// (numeric times are seconds; this is 2100-01-01).
540+
yield* fs.utimes(directory, 4_102_444_800, 4_102_444_800);
541+
expect(yield* resolveCommandPath("installed", { env })).toBe(
542+
path.join(directory, "installed.EXE"),
543+
);
544+
}).pipe(withPathDirectoryListings);
517545
}).pipe(
518546
Effect.provideService(HostProcessPlatform, "win32"),
519547
Effect.provideService(CommandResolutionCache, new Map()),

‎packages/shared/src/shell.ts‎

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import * as Clock from "effect/Clock";
88
import * as Data from "effect/Data";
99
import * as Effect from "effect/Effect";
1010
import * as FileSystem from "effect/FileSystem";
11+
import * as Option from "effect/Option";
1112
import * as Path from "effect/Path";
1213

1314
import { HostProcessEnvironment, HostProcessPlatform } from "./hostProcess.ts";
@@ -512,32 +513,58 @@ export const CommandResolutionCache = Context.Reference<Map<string, CommandResol
512513
},
513514
);
514515

515-
// A missing directory holds nothing. Any other listing failure (permissions, a
516-
// busy share) yields undefined so lookups probe that directory's candidates
517-
// directly, since a known file can still be reachable there.
518-
const listPathDirectory = (directory: string) =>
519-
FileSystem.FileSystem.use((fileSystem) => fileSystem.readDirectory(directory)).pipe(
520-
Effect.map(
521-
(entries): ReadonlySet<string> | undefined =>
522-
new Set(entries.map((entry) => entry.toLowerCase())),
523-
),
524-
Effect.catch((error) =>
525-
Effect.succeed(error.reason._tag === "NotFound" ? new Set<string>() : undefined),
526-
),
516+
interface PathDirectoryListing {
517+
readonly modified: number | null | undefined;
518+
/** Lowercased entry names, or undefined to probe candidates directly. */
519+
readonly names: ReadonlySet<string> | undefined;
520+
}
521+
522+
// The directory's mtime, null when it does not exist, or undefined when it
523+
// cannot be read or dated (permissions, a busy share).
524+
const pathDirectoryModified = (directory: string) =>
525+
FileSystem.FileSystem.use((fileSystem) => fileSystem.stat(directory)).pipe(
526+
Effect.map((info) => Option.getOrUndefined(info.mtime)?.getTime()),
527+
Effect.catch((error) => Effect.succeed(error.reason._tag === "NotFound" ? null : undefined)),
527528
);
528529

530+
// Dated before it is read, so an entry added in between shows up as a changed
531+
// mtime on the next check instead of hiding behind a matching one. A missing
532+
// directory holds nothing; one that cannot be dated or listed yields undefined
533+
// names so lookups probe its candidates directly.
534+
const listPathDirectory = (directory: string) =>
535+
Effect.gen(function* (): Effect.fn.Return<PathDirectoryListing, never, FileSystem.FileSystem> {
536+
const modified = yield* pathDirectoryModified(directory);
537+
if (modified === null) return { modified, names: new Set() };
538+
if (modified === undefined) return { modified, names: undefined };
539+
const entries = yield* FileSystem.FileSystem.use((fileSystem) =>
540+
fileSystem.readDirectory(directory),
541+
).pipe(Effect.orElseSucceed(() => undefined));
542+
return { modified, names: entries && new Set(entries.map((entry) => entry.toLowerCase())) };
543+
});
544+
529545
const PathDirectoryListings = Context.Reference<
530-
Cache.Cache<string, ReadonlySet<string> | undefined, never, FileSystem.FileSystem> | undefined
546+
Cache.Cache<string, PathDirectoryListing, never, FileSystem.FileSystem> | undefined
531547
>("@t3tools/shared/shell/PathDirectoryListings", { defaultValue: () => undefined });
532548

549+
// Every use re-checks the directory's mtime and relists it if it changed, so a
550+
// command installed mid-batch is found exactly as a direct probe would find it.
551+
// One directory stat per lookup still replaces a stat per PATHEXT candidate.
552+
const readPathDirectoryNames = Effect.fn("shell.readPathDirectoryNames")(function* (
553+
listings: Cache.Cache<string, PathDirectoryListing, never, FileSystem.FileSystem>,
554+
directory: string,
555+
) {
556+
const listing = yield* Cache.get(listings, directory);
557+
if (listing.names === undefined) return undefined;
558+
if ((yield* pathDirectoryModified(directory)) === listing.modified) return listing.names;
559+
yield* Cache.invalidate(listings, directory);
560+
return (yield* Cache.get(listings, directory)).names;
561+
});
562+
533563
/**
534564
* Run a batch of command lookups (such as editor discovery) that lists each
535565
* PATH directory once and only probes names the listing contains, instead of
536566
* stat-ing every PATH x PATHEXT candidate per command. A single lookup is
537-
* cheaper without it. Listings are a snapshot for the run: a command installed
538-
* into a directory after the run listed it is missed until the next run, and
539-
* that miss is cached like any other. Accepted for the tens of thousands of
540-
* stats this saves on long Windows PATHs.
567+
* cheaper without it.
541568
*/
542569
export const withPathDirectoryListings = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
543570
Effect.gen(function* () {
@@ -642,7 +669,8 @@ const resolveCommandPathForPlatform = Effect.fn("shell.resolveCommandPathForPlat
642669

643670
const listings = yield* PathDirectoryListings;
644671
for (const pathEntry of pathEntries) {
645-
const names = listings === undefined ? undefined : yield* Cache.get(listings, pathEntry);
672+
const names =
673+
listings === undefined ? undefined : yield* readPathDirectoryNames(listings, pathEntry);
646674
for (const candidate of commandCandidates) {
647675
// Listings are lowercased; the stat below still checks the exact
648676
// spelling for case-sensitive directories and rejects non-files.

0 commit comments

Comments
 (0)