Skip to content

Commit e36ecb3

Browse files
maria-rcksEnmani
andcommitted
fix(shared): keep Windows command discovery off the event loop
Co-authored-by: Enmani <[email protected]>
1 parent 3d78e91 commit e36ecb3

9 files changed

Lines changed: 148 additions & 53 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.testkit.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1487,7 +1487,9 @@ const resolveClaudeRecordingExecutablePath = Effect.fn("resolveClaudeRecordingEx
14871487
function* (environment: NodeJS.ProcessEnv) {
14881488
const resolveExecutable = yield* SpawnExecutableResolution;
14891489
const platform = yield* HostProcess.Platform;
1490-
const resolved = resolveExecutable("claude", platform, environment);
1490+
const resolved = yield* Effect.promise(async () =>
1491+
resolveExecutable("claude", platform, environment),
1492+
);
14911493
if (resolved === undefined) {
14921494
return undefined;
14931495
}

‎apps/server/src/provider/Drivers/ClaudeExecutable.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function withWindowsResolution(input: {
1818
return <A, E, R>(effect: Effect.Effect<A, E, R>) =>
1919
effect.pipe(
2020
Effect.provideService(HostProcess.Platform, "win32"),
21-
Effect.provideService(SpawnExecutableResolution, () => input.resolvedCommand),
21+
Effect.provideService(SpawnExecutableResolution, async () => input.resolvedCommand),
2222
Effect.provideService(ClaudeExecutableFileCheck, (filePath) => existing.has(filePath)),
2323
);
2424
}

‎apps/server/src/provider/Drivers/ClaudeExecutable.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ export const resolveClaudeSdkExecutablePath = Effect.fn("resolveClaudeSdkExecuta
4646

4747
const resolveExecutable = yield* SpawnExecutableResolution;
4848
const isFile = yield* ClaudeExecutableFileCheck;
49-
const resolved = resolveExecutable(binaryPath, platform, environment) ?? binaryPath;
49+
const resolved =
50+
(yield* Effect.promise(async () => resolveExecutable(binaryPath, platform, environment))) ??
51+
binaryPath;
5052
const extension = NodePath.win32.extname(resolved).toLowerCase();
5153
if (!WINDOWS_SHIM_EXTENSIONS.has(extension)) {
5254
return resolved;

‎packages/provider-acp-registry/src/server/AcpRegistrySupport.test.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ describe("AcpRegistrySupport", () => {
317317
registryUrl,
318318
}).pipe(
319319
Effect.provideService(HostProcess.Platform, "win32"),
320-
Effect.provideService(SpawnExecutableResolution, () => `C:\\bin\\dsh${extension}`),
320+
Effect.provideService(SpawnExecutableResolution, async () => `C:\\bin\\dsh${extension}`),
321321
);
322322
const localSettings = decodeAcpRegistrySettings({ source: "local", commandPath: "dsh" });
323323
for (const failure of [
@@ -802,6 +802,7 @@ describe("AcpRegistrySupport", () => {
802802
name: "Codex Darwin",
803803
} satisfies AcpRegistrySupport.AcpRegistryAgent;
804804

805+
const lookups: string[] = [];
805806
return Effect.gen(function* () {
806807
const fileSystem = yield* FileSystem.FileSystem;
807808
const cacheDir = yield* fileSystem.makeTempDirectoryScoped({
@@ -811,9 +812,15 @@ describe("AcpRegistrySupport", () => {
811812
cacheDir,
812813
toolsDir: `${cacheDir}/tools`,
813814
registryUrl,
814-
});
815+
}).pipe(
816+
Effect.provideService(SpawnExecutableResolution, async (command) => {
817+
lookups.push(command);
818+
return "/bin/npm";
819+
}),
820+
);
815821
const result = yield* resolver.search({ query: "codex" });
816822

823+
expect(lookups).toEqual(["npm"]);
817824
expect(result.agents.map((agent) => agent.id)).toEqual(["codex-acp", "other-agent"]);
818825
expect(result.agents[0]).toMatchObject({
819826
authors: ["OpenAI", "Zed Industries"],

‎packages/provider-acp-registry/src/server/AcpRegistrySupport.ts‎

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,13 @@ export const makeAcpRegistryCatalog = Effect.fn("AcpRegistryCatalog.make")(funct
651651
const platform = yield* HostProcess.Platform;
652652
const architecture = yield* HostProcess.Architecture;
653653
const hostEnvironment = yield* HostProcess.Environment;
654-
const resolveExecutable = yield* SpawnExecutableResolution;
654+
const executableResolver = yield* SpawnExecutableResolution;
655+
const resolveExecutable = Effect.fn("AcpRegistryCatalog.resolveExecutable")(function* (
656+
command: string,
657+
environment: NodeJS.ProcessEnv,
658+
) {
659+
return yield* Effect.promise(async () => executableResolver(command, platform, environment));
660+
});
655661
const platformTarget = resolveAcpRegistryPlatformTarget(platform, architecture);
656662
const registryUrl = input.registryUrl ?? ACP_REGISTRY_URL;
657663
const registryDirectory = path.join(input.cacheDir, "acp-registry");
@@ -1154,9 +1160,8 @@ export const makeAcpRegistryCatalog = Effect.fn("AcpRegistryCatalog.make")(funct
11541160
const commandName = npmCommandName(agent, packageIdentity.name, manifest.value);
11551161
if (commandName === undefined) return Option.none<AcpRegistryPackageInstallReceipt>();
11561162
const binDirectory = platform === "win32" ? globalPrefix : path.join(globalPrefix, "bin");
1157-
const executablePath = resolveExecutable(
1163+
const executablePath = yield* resolveExecutable(
11581164
commandName,
1159-
platform,
11601165
withPreferredPath(environment, binDirectory, platform),
11611166
);
11621167
return executablePath === undefined
@@ -1203,9 +1208,11 @@ export const makeAcpRegistryCatalog = Effect.fn("AcpRegistryCatalog.make")(funct
12031208
truncatedOutputReason: "install_failed",
12041209
}).pipe(Effect.flatMap((output) => parsePackageManagerPath(managerPath, output)));
12051210
const commandEnvironment = withPreferredPath(environment, binDirectory, platform);
1206-
const executablePath = packageCommandCandidates(agent, packageIdentity.name)
1207-
.map((candidate) => resolveExecutable(candidate, platform, commandEnvironment))
1208-
.find((candidate): candidate is string => candidate !== undefined);
1211+
let executablePath: string | undefined;
1212+
for (const candidate of packageCommandCandidates(agent, packageIdentity.name)) {
1213+
executablePath = yield* resolveExecutable(candidate, commandEnvironment);
1214+
if (executablePath !== undefined) break;
1215+
}
12091216
return executablePath === undefined
12101217
? Option.none<AcpRegistryPackageInstallReceipt>()
12111218
: Option.some<AcpRegistryPackageInstallReceipt>({
@@ -1228,7 +1235,7 @@ export const makeAcpRegistryCatalog = Effect.fn("AcpRegistryCatalog.make")(funct
12281235
environment: NodeJS.ProcessEnv,
12291236
) {
12301237
const managerName = packageManagerFor(distribution)!;
1231-
const managerPath = resolveExecutable(managerName, platform, environment);
1238+
const managerPath = yield* resolveExecutable(managerName, environment);
12321239
if (managerPath === undefined) {
12331240
return yield* new AcpRegistryError({
12341241
reason: "runner_unavailable",
@@ -1629,22 +1636,27 @@ export const makeAcpRegistryCatalog = Effect.fn("AcpRegistryCatalog.make")(funct
16291636
const search: AcpRegistryCatalog["Service"]["search"] = Effect.fn("AcpRegistryCatalog.search")(
16301637
function* (input) {
16311638
const registry = yield* recentRegistry();
1632-
const ranked = registry.agents.flatMap((agent) => {
1639+
const ranked = [];
1640+
const managerAvailability = new Map<string, boolean>();
1641+
for (const agent of registry.agents) {
16331642
const distribution = resolveAcpRegistryDistribution({
16341643
agent,
16351644
preference: "auto",
16361645
platformTarget,
16371646
});
16381647
const rank = searchRank(agent, input.query);
1639-
const packageManager =
1640-
distribution === undefined ? undefined : packageManagerFor(distribution.kind);
1641-
const packageManagerAvailable =
1642-
packageManager === undefined ||
1643-
resolveExecutable(packageManager, platform, hostEnvironment) !== undefined;
1644-
return distribution === undefined || rank === undefined || !packageManagerAvailable
1645-
? []
1646-
: [{ agent, distribution, rank }];
1647-
});
1648+
if (distribution === undefined || rank === undefined) continue;
1649+
const packageManager = packageManagerFor(distribution.kind);
1650+
if (packageManager !== undefined) {
1651+
let available = managerAvailability.get(packageManager);
1652+
if (available === undefined) {
1653+
available = (yield* resolveExecutable(packageManager, hostEnvironment)) !== undefined;
1654+
managerAvailability.set(packageManager, available);
1655+
}
1656+
if (!available) continue;
1657+
}
1658+
ranked.push({ agent, distribution, rank });
1659+
}
16481660
ranked.sort(
16491661
(left, right) =>
16501662
left.rank - right.rank ||
@@ -1715,7 +1727,7 @@ export const makeAcpRegistryCatalog = Effect.fn("AcpRegistryCatalog.make")(funct
17151727
if (settings.source === "local") {
17161728
const command = settings.commandPath.trim();
17171729
if (!command) return { status: "unconfigured" } as const;
1718-
const executable = resolveExecutable(command, platform, environment ?? hostEnvironment);
1730+
const executable = yield* resolveExecutable(command, environment ?? hostEnvironment);
17191731
if (executable !== undefined) yield* validateLocalExecutable(executable);
17201732
return executable === undefined
17211733
? ({
@@ -1743,8 +1755,7 @@ export const makeAcpRegistryCatalog = Effect.fn("AcpRegistryCatalog.make")(funct
17431755
const commandOverride = settings.commandPath.trim();
17441756
if (commandOverride.length > 0) {
17451757
const available =
1746-
resolveExecutable(commandOverride, platform, environment ?? hostEnvironment) !==
1747-
undefined;
1758+
(yield* resolveExecutable(commandOverride, environment ?? hostEnvironment)) !== undefined;
17481759
return available
17491760
? ({
17501761
status: "ready",
@@ -1805,7 +1816,7 @@ export const makeAcpRegistryCatalog = Effect.fn("AcpRegistryCatalog.make")(funct
18051816

18061817
const runner = packageManagerFor(distribution.kind)!;
18071818
const available =
1808-
resolveExecutable(runner, platform, environment ?? hostEnvironment) !== undefined;
1819+
(yield* resolveExecutable(runner, environment ?? hostEnvironment)) !== undefined;
18091820
return available
18101821
? ({
18111822
status: "ready",
@@ -1835,7 +1846,7 @@ export const makeAcpRegistryCatalog = Effect.fn("AcpRegistryCatalog.make")(funct
18351846
});
18361847
}
18371848
const env = environment ?? hostEnvironment;
1838-
const command = resolveExecutable(executable, platform, env);
1849+
const command = yield* resolveExecutable(executable, env);
18391850
if (command === undefined) {
18401851
return yield* new AcpRegistryError({
18411852
reason: "runner_unavailable",
@@ -1865,7 +1876,7 @@ export const makeAcpRegistryCatalog = Effect.fn("AcpRegistryCatalog.make")(funct
18651876
const effectiveEnvironment = environment ?? hostEnvironment;
18661877
const commandOverride = settings.commandPath.trim();
18671878
if (commandOverride.length > 0) {
1868-
const resolvedOverride = resolveExecutable(commandOverride, platform, effectiveEnvironment);
1879+
const resolvedOverride = yield* resolveExecutable(commandOverride, effectiveEnvironment);
18691880
if (resolvedOverride === undefined) {
18701881
return yield* new AcpRegistryError({
18711882
reason: "runner_unavailable",

‎packages/provider-muse/src/server/sdk.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ describe("Muse launch", () => {
263263
const onWindows = (resolved: string | undefined, env: NodeJS.ProcessEnv = { ComSpec: CMD }) =>
264264
museLaunch("muse", env).pipe(
265265
Effect.provideService(HostProcess.Platform, "win32"),
266-
Effect.provideService(SpawnExecutableResolution, () => resolved),
266+
Effect.provideService(SpawnExecutableResolution, async () => resolved),
267267
);
268268

269269
it.effect("runs an exe directly and a launcher script under cmd.exe on Windows", () =>

‎packages/provider-muse/src/server/sdk.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ export const museLaunch = Effect.fn("museLaunch")(function* (
116116
if ((yield* HostProcess.Platform) !== "win32") return { command: binaryPath, args: [] };
117117
const env = environment ?? (yield* HostProcess.Environment);
118118
const resolveExecutable = yield* SpawnExecutableResolution;
119-
const resolved = resolveExecutable(binaryPath, "win32", env) ?? binaryPath;
119+
const resolved =
120+
(yield* Effect.promise(async () => resolveExecutable(binaryPath, "win32", env))) ?? binaryPath;
120121
if (!/\.(?:cmd|bat)$/i.test(resolved)) return { command: resolved, args: [] };
121122
const systemRoot = windowsEnvironmentValue(env, "SystemRoot");
122123
return {

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

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,73 @@ describe("npmShimExecutable", () => {
721721
});
722722

723723
effectIt.layer(NodeServices.layer)("resolveSpawnCommand", (it) => {
724+
it.effect("yields while discovering a Windows executable", () =>
725+
Effect.scoped(
726+
Effect.gen(function* () {
727+
const fs = yield* FileSystem.FileSystem;
728+
const path = yield* Path.Path;
729+
const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-spawn-" });
730+
const executable = path.join(directory, "tool.EXE");
731+
yield* fs.writeFileString(executable, "");
732+
let settled = false;
733+
const pending = Promise.resolve(
734+
SpawnExecutableResolution.defaultValue()(executable, "win32", { PATHEXT: ".EXE;.CMD" }),
735+
).then((value) => {
736+
settled = true;
737+
return value;
738+
});
739+
try {
740+
yield* Effect.promise(() => Promise.resolve());
741+
expect(settled).toBe(false);
742+
} finally {
743+
expect(yield* Effect.promise(() => pending)).toBe(executable);
744+
}
745+
}),
746+
),
747+
);
748+
749+
it.effect("preserves PATH and PATHEXT order and discovers executables after a miss", () =>
750+
Effect.scoped(
751+
Effect.gen(function* () {
752+
const fs = yield* FileSystem.FileSystem;
753+
const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-spawn-" });
754+
const fixture = NodePath.join(directory, "tool.EXE");
755+
yield* fs.writeFileString(fixture, "");
756+
const stat = yield* Effect.promise(() => NodeFS.promises.stat(fixture));
757+
const files = new Set<string>();
758+
using _stat = vi.spyOn(NodeFS.promises, "stat").mockImplementation(async (candidate) => {
759+
if (!files.has(String(candidate))) throw new Error("not found");
760+
return stat;
761+
});
762+
const lookup = resolveSpawnCommand("tool", ["status"], {
763+
env: { PATH: "C:\\first;C:\\later", PATHEXT: ".EXE;.CMD" },
764+
});
765+
expect((yield* lookup).command).toBe("tool");
766+
files.add("C:\\first\\tool.CMD");
767+
files.add("C:\\first\\tool.EXE");
768+
files.add("C:\\later\\tool.EXE");
769+
expect(yield* lookup).toEqual({
770+
command: "C:\\first\\tool.EXE",
771+
args: ["status"],
772+
shell: false,
773+
});
774+
files.delete("C:\\first\\tool.EXE");
775+
expect((yield* lookup).command).toBe("C:\\first\\tool.EXE");
776+
expect(
777+
yield* Effect.promise(async () =>
778+
SpawnExecutableResolution.defaultValue()("tool", "win32", {
779+
PATH: "C:\\first;C:\\later",
780+
PATHEXT: ".EXE;.CMD",
781+
}),
782+
),
783+
).toBe("C:\\first\\tool.CMD");
784+
}).pipe(
785+
Effect.provideService(HostProcess.Platform, "win32"),
786+
Effect.provideService(CommandResolutionCache, new Map()),
787+
),
788+
),
789+
);
790+
724791
it.effect("runs Windows executables directly without a shell", () =>
725792
Effect.gen(function* () {
726793
const command = yield* resolveSpawnCommand("node.exe", ["script.js", "hello & goodbye"], {

0 commit comments

Comments
 (0)