From 6b8805bc3b9eb76519a19422476dc905f6060a3d Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Thu, 5 Mar 2026 20:52:59 +0000 Subject: [PATCH] Fix duplicated utility functions and unsafe constructor fallback - Extract shared command resolution helpers (stripWrappingQuotes, resolvePathEnvironmentVariable, resolveWindowsPathExtensions, resolveCommandCandidates, isExecutableFile) into a new commandResolution.ts module, eliminating duplication between processRunner.ts and open.ts. - Make CodexAppServerManager constructor require services parameter instead of using an unsafe cast that erases Effect service requirements when services are omitted. Co-authored-by: Julius Marminge --- apps/server/src/codexAppServerManager.ts | 10 +-- apps/server/src/commandResolution.ts | 71 +++++++++++++++++ apps/server/src/open.ts | 79 ++----------------- apps/server/src/processRunner.ts | 79 +++---------------- .../src/provider/Layers/CodexAdapter.test.ts | 14 +++- 5 files changed, 101 insertions(+), 152 deletions(-) create mode 100644 apps/server/src/commandResolution.ts diff --git a/apps/server/src/codexAppServerManager.ts b/apps/server/src/codexAppServerManager.ts index 5a1ed26dee..a32305d25a 100644 --- a/apps/server/src/codexAppServerManager.ts +++ b/apps/server/src/codexAppServerManager.ts @@ -169,15 +169,9 @@ export class CodexAppServerManager extends EventEmitter Promise; - constructor(services?: ServiceMap.ServiceMap) { + constructor(services: ServiceMap.ServiceMap) { super(); - this.runPromise = services - ? Effect.runPromiseWith(services) - : ((effect, options) => - Effect.runPromise( - effect as unknown as Effect.Effect, - options, - )) as typeof this.runPromise; + this.runPromise = Effect.runPromiseWith(services); } async startSession(input: ProviderSessionStartInput): Promise { diff --git a/apps/server/src/commandResolution.ts b/apps/server/src/commandResolution.ts new file mode 100644 index 0000000000..db830435da --- /dev/null +++ b/apps/server/src/commandResolution.ts @@ -0,0 +1,71 @@ +import { accessSync, constants, statSync } from "node:fs"; +import { extname } from "node:path"; + +export function stripWrappingQuotes(value: string): string { + return value.replace(/^"+|"+$/g, ""); +} + +export function resolvePathEnvironmentVariable(env: NodeJS.ProcessEnv): string { + return env.PATH ?? env.Path ?? env.path ?? ""; +} + +export function resolveWindowsPathExtensions(env: NodeJS.ProcessEnv): ReadonlyArray { + const rawValue = env.PATHEXT; + const fallback = [".COM", ".EXE", ".BAT", ".CMD"]; + if (!rawValue) return fallback; + + const parsed = rawValue + .split(";") + .map((entry) => entry.trim()) + .filter((entry) => entry.length > 0) + .map((entry) => (entry.startsWith(".") ? entry.toUpperCase() : `.${entry.toUpperCase()}`)); + return parsed.length > 0 ? Array.from(new Set(parsed)) : fallback; +} + +export function resolveCommandCandidates( + command: string, + platform: NodeJS.Platform, + windowsPathExtensions: ReadonlyArray, +): ReadonlyArray { + if (platform !== "win32") return [command]; + const extension = extname(command); + const normalizedExtension = extension.toUpperCase(); + + if (extension.length > 0 && windowsPathExtensions.includes(normalizedExtension)) { + const commandWithoutExtension = command.slice(0, -extension.length); + return Array.from( + new Set([ + command, + `${commandWithoutExtension}${normalizedExtension}`, + `${commandWithoutExtension}${normalizedExtension.toLowerCase()}`, + ]), + ); + } + + const candidates: string[] = []; + for (const candidateExtension of windowsPathExtensions) { + candidates.push(`${command}${candidateExtension}`); + candidates.push(`${command}${candidateExtension.toLowerCase()}`); + } + return Array.from(new Set(candidates)); +} + +export function isExecutableFile( + filePath: string, + platform: NodeJS.Platform, + windowsPathExtensions: ReadonlyArray, +): boolean { + try { + const stat = statSync(filePath); + if (!stat.isFile()) return false; + if (platform === "win32") { + const extension = extname(filePath); + if (extension.length === 0) return false; + return windowsPathExtensions.includes(extension.toUpperCase()); + } + accessSync(filePath, constants.X_OK); + return true; + } catch { + return false; + } +} diff --git a/apps/server/src/open.ts b/apps/server/src/open.ts index b933ad89a0..89dcffc67d 100644 --- a/apps/server/src/open.ts +++ b/apps/server/src/open.ts @@ -6,11 +6,17 @@ * * @module Open */ -import { accessSync, constants, statSync } from "node:fs"; -import { extname, join } from "node:path"; +import { join } from "node:path"; import { EDITORS, type EditorId, type ServerRuntimeEnvironment } from "@t3tools/contracts"; import { ServiceMap, Schema, Effect, Layer } from "effect"; +import { + isExecutableFile, + resolveCommandCandidates, + resolvePathEnvironmentVariable, + resolveWindowsPathExtensions, + stripWrappingQuotes, +} from "./commandResolution"; import { spawnDetachedProcess, spawnProcessSync } from "./processRunner"; import { detectServerRuntimeEnvironment } from "./runtimeEnvironment"; @@ -72,75 +78,6 @@ function fileManagerCommandForPlatform(platform: NodeJS.Platform): string { } } -function stripWrappingQuotes(value: string): string { - return value.replace(/^"+|"+$/g, ""); -} - -function resolvePathEnvironmentVariable(env: NodeJS.ProcessEnv): string { - return env.PATH ?? env.Path ?? env.path ?? ""; -} - -function resolveWindowsPathExtensions(env: NodeJS.ProcessEnv): ReadonlyArray { - const rawValue = env.PATHEXT; - const fallback = [".COM", ".EXE", ".BAT", ".CMD"]; - if (!rawValue) return fallback; - - const parsed = rawValue - .split(";") - .map((entry) => entry.trim()) - .filter((entry) => entry.length > 0) - .map((entry) => (entry.startsWith(".") ? entry.toUpperCase() : `.${entry.toUpperCase()}`)); - return parsed.length > 0 ? Array.from(new Set(parsed)) : fallback; -} - -function resolveCommandCandidates( - command: string, - platform: NodeJS.Platform, - windowsPathExtensions: ReadonlyArray, -): ReadonlyArray { - if (platform !== "win32") return [command]; - const extension = extname(command); - const normalizedExtension = extension.toUpperCase(); - - if (extension.length > 0 && windowsPathExtensions.includes(normalizedExtension)) { - const commandWithoutExtension = command.slice(0, -extension.length); - return Array.from( - new Set([ - command, - `${commandWithoutExtension}${normalizedExtension}`, - `${commandWithoutExtension}${normalizedExtension.toLowerCase()}`, - ]), - ); - } - - const candidates: string[] = []; - for (const extension of windowsPathExtensions) { - candidates.push(`${command}${extension}`); - candidates.push(`${command}${extension.toLowerCase()}`); - } - return Array.from(new Set(candidates)); -} - -function isExecutableFile( - filePath: string, - platform: NodeJS.Platform, - windowsPathExtensions: ReadonlyArray, -): boolean { - try { - const stat = statSync(filePath); - if (!stat.isFile()) return false; - if (platform === "win32") { - const extension = extname(filePath); - if (extension.length === 0) return false; - return windowsPathExtensions.includes(extension.toUpperCase()); - } - accessSync(filePath, constants.X_OK); - return true; - } catch { - return false; - } -} - function resolvePathDelimiter(platform: NodeJS.Platform): string { return platform === "win32" ? ";" : ":"; } diff --git a/apps/server/src/processRunner.ts b/apps/server/src/processRunner.ts index 29cd1d624a..370423e8f7 100644 --- a/apps/server/src/processRunner.ts +++ b/apps/server/src/processRunner.ts @@ -5,13 +5,19 @@ import { spawnSync, type StdioOptions, } from "node:child_process"; -import { statSync } from "node:fs"; import { extname, join } from "node:path"; import type { ServerRuntimeEnvironment } from "@t3tools/contracts"; import { Effect, Exit, Scope } from "effect"; import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process"; +import { + isExecutableFile, + resolveCommandCandidates, + resolvePathEnvironmentVariable, + resolveWindowsPathExtensions, + stripWrappingQuotes, +} from "./commandResolution"; import { detectServerRuntimeEnvironment } from "./runtimeEnvironment"; interface ProcessSpawnBaseOptions { @@ -93,71 +99,6 @@ function resolveRuntimeEnvironment( return runtimeEnvironment ?? detectServerRuntimeEnvironment(); } -function resolvePathEnvironmentVariable(env: NodeJS.ProcessEnv): string { - return env.PATH ?? env.Path ?? env.path ?? ""; -} - -function resolveWindowsPathExtensions(env: NodeJS.ProcessEnv): ReadonlyArray { - const rawValue = env.PATHEXT; - const fallback = [".COM", ".EXE", ".BAT", ".CMD"]; - if (!rawValue) return fallback; - - const parsed = rawValue - .split(";") - .map((entry) => entry.trim()) - .filter((entry) => entry.length > 0) - .map((entry) => (entry.startsWith(".") ? entry.toUpperCase() : `.${entry.toUpperCase()}`)); - return parsed.length > 0 ? Array.from(new Set(parsed)) : fallback; -} - -function resolveCommandCandidates( - command: string, - windowsPathExtensions: ReadonlyArray, -): ReadonlyArray { - const extension = extname(command); - const normalizedExtension = extension.toUpperCase(); - - if (extension.length > 0 && windowsPathExtensions.includes(normalizedExtension)) { - const commandWithoutExtension = command.slice(0, -extension.length); - return Array.from( - new Set([ - command, - `${commandWithoutExtension}${normalizedExtension}`, - `${commandWithoutExtension}${normalizedExtension.toLowerCase()}`, - ]), - ); - } - - const candidates: string[] = [command]; - for (const candidateExtension of windowsPathExtensions) { - candidates.push(`${command}${candidateExtension}`); - candidates.push(`${command}${candidateExtension.toLowerCase()}`); - } - return Array.from(new Set(candidates)); -} - -function stripWrappingQuotes(value: string): string { - return value.replace(/^"+|"+$/g, ""); -} - -function isExecutableFile( - filePath: string, - windowsPathExtensions: ReadonlyArray, -): boolean { - try { - const stat = statSync(filePath); - if (!stat.isFile()) return false; - const extension = extname(filePath); - if (extension.length === 0) return false; - if (windowsPathExtensions.length === 0) { - return true; - } - return windowsPathExtensions.includes(extension.toUpperCase()); - } catch { - return false; - } -} - function resolveEffectiveEnvironment(options: ProcessLaunchPlanOptions): NodeJS.ProcessEnv { const env = (options.env ?? {}) as NodeJS.ProcessEnv; if (options.inheritParentEnv === false) { @@ -175,7 +116,7 @@ function resolveWindowsCommand( env: NodeJS.ProcessEnv, ): ResolvedWindowsCommand | null { const windowsPathExtensions = resolveWindowsPathExtensions(env); - const candidates = resolveCommandCandidates(command, windowsPathExtensions); + const candidates = resolveCommandCandidates(command, "win32", windowsPathExtensions); const classify = (filePath: string): ResolvedWindowsCommand => { const extension = extname(filePath).toUpperCase(); @@ -187,7 +128,7 @@ function resolveWindowsCommand( if (command.includes("/") || command.includes("\\")) { for (const candidate of candidates) { - if (isExecutableFile(candidate, windowsPathExtensions)) { + if (isExecutableFile(candidate, "win32", windowsPathExtensions)) { return classify(candidate); } } @@ -202,7 +143,7 @@ function resolveWindowsCommand( for (const pathEntry of pathEntries) { for (const candidate of candidates) { const candidatePath = join(pathEntry, candidate); - if (isExecutableFile(candidatePath, windowsPathExtensions)) { + if (isExecutableFile(candidatePath, "win32", windowsPathExtensions)) { return classify(candidatePath); } } diff --git a/apps/server/src/provider/Layers/CodexAdapter.test.ts b/apps/server/src/provider/Layers/CodexAdapter.test.ts index 2dd6062e60..cd25cebcbd 100644 --- a/apps/server/src/provider/Layers/CodexAdapter.test.ts +++ b/apps/server/src/provider/Layers/CodexAdapter.test.ts @@ -15,7 +15,7 @@ import * as NodeServices from "@effect/platform-node/NodeServices"; import { afterAll, assert, it, vi } from "@effect/vitest"; import { assertFailure } from "@effect/vitest/utils"; -import { Effect, Fiber, Layer, Option, Stream } from "effect"; +import { Effect, Fiber, Layer, Option, ServiceMap, Stream } from "effect"; import { CodexAppServerManager, @@ -132,7 +132,9 @@ const providerSessionDirectoryTestLayer = Layer.succeed(ProviderSessionDirectory listSessionIds: () => Effect.succeed([]), }); -const validationManager = new FakeCodexManager(); +const validationManager = new FakeCodexManager( + ServiceMap.empty() as ServiceMap.ServiceMap, +); const validationLayer = it.layer( makeCodexAdapterLive({ manager: validationManager }).pipe( Layer.provideMerge(ServerConfig.layerTest(process.cwd(), process.cwd())), @@ -164,7 +166,9 @@ validationLayer("CodexAdapterLive validation", (it) => { ); }); -const sessionErrorManager = new FakeCodexManager(); +const sessionErrorManager = new FakeCodexManager( + ServiceMap.empty() as ServiceMap.ServiceMap, +); sessionErrorManager.sendTurnImpl.mockImplementation(async () => { throw new Error("Unknown session: sess-missing"); }); @@ -204,7 +208,9 @@ sessionErrorLayer("CodexAdapterLive session errors", (it) => { ); }); -const lifecycleManager = new FakeCodexManager(); +const lifecycleManager = new FakeCodexManager( + ServiceMap.empty() as ServiceMap.ServiceMap, +); const lifecycleLayer = it.layer( makeCodexAdapterLive({ manager: lifecycleManager }).pipe( Layer.provideMerge(ServerConfig.layerTest(process.cwd(), process.cwd())),