@devries-cameron20 / t3code Mirror · read-only / commits / 71964e670f
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 23, 19:15 UTC

perf(shared): scan PATH once per command before spawning, not on every spawn (#12600)

SkiTee3000 committed Oct 6, 2026 at 07:12 UTC 71964e670ff212006ebb8de26585649c2adced68
2 files changed +93 -3
packages/shared/src/shell.test.ts
+55 -2
@@ -24,6 +24,7 @@ import {
24 resolveSpawnCommand,
25 resolveWindowsEnvironment,
26 SpawnExecutableResolution,
27 + type SpawnExecutableResolver,
28 WindowsShellEnvironment,
29 withPathDirectoryListings,
30 type WindowsShellEnvironmentReader,
@@ -515,7 +516,10 @@ effectIt.layer(NodeServices.layer)("resolveSpawnCommand", (it) => {
516 Effect.gen(function* () {
517 const command = yield* resolveSpawnCommand("node.exe", ["script.js", "hello & goodbye"], {
518 env: { PATH: "", PATHEXT: ".COM;.EXE;.BAT;.CMD" },
518 - }).pipe(Effect.provideService(HostProcessPlatform, "win32"));
519 + }).pipe(
520 + Effect.provideService(HostProcessPlatform, "win32"),
521 + Effect.provideService(CommandResolutionCache, new Map()),
522 + );
523
524 expect(command).toEqual({
525 command: "node.exe",
@@ -533,6 +537,7 @@ effectIt.layer(NodeServices.layer)("resolveSpawnCommand", (it) => {
537 { env: { PATH: "", PATHEXT: ".COM;.EXE;.BAT;.CMD" } },
538 ).pipe(
539 Effect.provideService(HostProcessPlatform, "win32"),
540 + Effect.provideService(CommandResolutionCache, new Map()),
541 Effect.provideService(
542 SpawnExecutableResolution,
543 () => "C:\\Program Files\\npm & tools\\vp.cmd",
@@ -559,6 +564,7 @@ effectIt.layer(NodeServices.layer)("resolveSpawnCommand", (it) => {
564 extendEnv: true,
565 }).pipe(
566 Effect.provideService(HostProcessPlatform, "win32"),
567 + Effect.provideService(CommandResolutionCache, new Map()),
568 Effect.provideService(HostProcessEnvironment, {
569 PATH: "C:\\Users\\tester\\AppData\\Roaming\\npm",
570 PATHEXT: ".COM;.EXE;.BAT;.CMD",
@@ -577,11 +583,58 @@ effectIt.layer(NodeServices.layer)("resolveSpawnCommand", (it) => {
583 }),
584 );
585
586 + it.effect("scans PATH once per command until the search environment changes", () =>
587 + Effect.gen(function* () {
588 + const scans: Array<string> = [];
589 + const scan: SpawnExecutableResolver = (name, _platform, env) => {
590 + scans.push(`${name}@${env.PATH}`);
591 + return name === "missing" ? undefined : `${env.PATH}\\${name}.exe`;
592 + };
593 + const resolve = (command: string, path: string, resolver = scan) =>
594 + resolveSpawnCommand(command, [], { env: { PATH: path, PATHEXT: ".EXE" } }).pipe(
595 + Effect.provideService(HostProcessPlatform, "win32"),
596 + Effect.provideService(SpawnExecutableResolution, resolver),
597 + );
598 +
599 + expect((yield* resolve("git", "C:\\one")).command).toBe("C:\\one\\git.exe");
600 + expect((yield* resolve("git", "C:\\one")).command).toBe("C:\\one\\git.exe");
601 + // A failed spawn is how a provider reports "not installed", so a miss
602 + // must clear the moment the binary appears.
603 + yield* resolve("missing", "C:\\one");
604 + yield* resolve("missing", "C:\\one");
605 + expect((yield* resolve("git", "C:\\two")).command).toBe("C:\\two\\git.exe");
606 + // Callers probe explicit paths they may have just written.
607 + yield* resolve("C:\\tools\\git.exe", "C:\\one");
608 + yield* resolve("C:\\tools\\git.exe", "C:\\one");
609 + expect(scans).toEqual([
610 + "git@C:\\one",
611 + "missing@C:\\one",
612 + "missing@C:\\one",
613 + "git@C:\\two",
614 + "C:\\tools\\git.exe@C:\\one",
615 + "C:\\tools\\git.exe@C:\\one",
616 + ]);
617 +
618 + yield* TestClock.adjust("31 seconds");
619 + yield* resolve("git", "C:\\one");
620 + expect(scans).toHaveLength(7);
621 +
622 + // Another resolver sharing the cache gets its own answer, not the cached one.
623 + const elsewhere = yield* resolve("git", "C:\\one", () => "D:\\elsewhere\\git.exe");
624 + expect(elsewhere.command).toBe("D:\\elsewhere\\git.exe");
625 + expect((yield* resolve("git", "C:\\one")).command).toBe("C:\\one\\git.exe");
626 + expect(scans).toHaveLength(7);
627 + }).pipe(Effect.provideService(CommandResolutionCache, new Map())),
628 + );
629 +
630 it.effect("does not fall back to a shell for unresolved Windows commands", () =>
631 Effect.gen(function* () {
632 const command = yield* resolveSpawnCommand("missing & calc", ["unsafe & value"], {
633 env: { PATH: "", PATHEXT: ".COM;.EXE;.BAT;.CMD" },
584 - }).pipe(Effect.provideService(HostProcessPlatform, "win32"));
634 + }).pipe(
635 + Effect.provideService(HostProcessPlatform, "win32"),
636 + Effect.provideService(CommandResolutionCache, new Map()),
637 + );
638
639 expect(command).toEqual({
640 command: "missing & calc",
packages/shared/src/shell.ts
+38 -1
@@ -559,6 +559,18 @@ export const withPathDirectoryListings = <A, E, R>(effect: Effect.Effect<A, E, R
559 return yield* effect.pipe(Effect.provideService(PathDirectoryListings, listings));
560 });
561
562 +// An injected resolver may answer differently for the same search, so its
563 +// entries are kept apart from every other resolver's.
564 +let spawnResolverCacheIdCount = 0;
565 +const spawnResolverCacheIds = new WeakMap<SpawnExecutableResolver, number>();
566 +function spawnResolverCacheId(resolver: SpawnExecutableResolver): number {
567 + const known = spawnResolverCacheIds.get(resolver);
568 + if (known !== undefined) return known;
569 + const id = spawnResolverCacheIdCount++;
570 + spawnResolverCacheIds.set(resolver, id);
571 + return id;
572 +}
573 +
574 function cacheCommandResolution(
575 cache: Map<string, CommandResolutionCacheEntry>,
576 cacheKey: string,
@@ -700,7 +712,32 @@ export const resolveSpawnCommand = Effect.fnUntraced(function* (
712 ? { ...hostEnvironment, ...options.env }
713 : options.env;
714 const resolveExecutable = yield* SpawnExecutableResolution;
703 - const resolvedCommand = resolveExecutable(command, platform, env) ?? command;
715 + // The scan is synchronous and runs before every child process, so it shares
716 + // the PATH scan cache above. Explicit paths stay uncached for the same reason,
717 + // and so do misses: a failed spawn is how providers report "not installed",
718 + // and that has to clear the moment the binary appears.
719 + const explicitPath = command.includes("/") || command.includes("\\");
720 + const cache = yield* CommandResolutionCache;
721 + const cacheKey = [
722 + "spawn",
723 + String(spawnResolverCacheId(resolveExecutable)),
724 + platform,
725 + resolvePathEnvironmentVariable(env),
726 + resolveWindowsPathExtensions(env).join(";"),
727 + command,
728 + ].join(COMMAND_RESOLUTION_CACHE_KEY_SEPARATOR);
729 + const nowNanos = yield* Clock.currentTimeNanos;
730 + const cached = explicitPath ? undefined : cache.get(cacheKey);
731 + let resolvedExecutable: string | null;
732 + if (cached !== undefined && cached.expiresAtNanos > nowNanos) {
733 + resolvedExecutable = cached.resolvedPath;
734 + } else {
735 + resolvedExecutable = resolveExecutable(command, platform, env) ?? null;
736 + if (!explicitPath && resolvedExecutable !== null) {
737 + cacheCommandResolution(cache, cacheKey, resolvedExecutable, nowNanos);
738 + }
739 + }
740 + const resolvedCommand = resolvedExecutable ?? command;
741 const extension = NodePath.win32.extname(resolvedCommand).toLowerCase();
742 if (extension !== ".cmd" && extension !== ".bat") {
743 return { command: resolvedCommand, args: [...args], shell: false };