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 <[email protected]>
This commit is contained in:
1 parent
b249442175
commit
6b8805bc3b
5 files changed
+101
-152
No files matched your search
@@ -169,15 +169,9 @@ export class CodexAppServerManager extends EventEmitter<CodexAppServerManagerEve
|
||||
options?: Effect.RunOptions | undefined,
|
||||
) => Promise<A>;
|
||||
|
||||
constructor(services?: ServiceMap.ServiceMap<NodeServices.NodeServices>) {
|
||||
constructor(services: ServiceMap.ServiceMap<NodeServices.NodeServices>) {
|
||||
super();
|
||||
this.runPromise = services
|
||||
? Effect.runPromiseWith(services)
|
||||
: ((effect, options) =>
|
||||
Effect.runPromise(
|
||||
effect as unknown as Effect.Effect<unknown, never>,
|
||||
options,
|
||||
)) as typeof this.runPromise;
|
||||
this.runPromise = Effect.runPromiseWith(services);
|
||||
}
|
||||
|
||||
async startSession(input: ProviderSessionStartInput): Promise<ProviderSession> {
|
||||
|
||||
@@ -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<string> {
|
||||
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<string>,
|
||||
): ReadonlyArray<string> {
|
||||
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<string>,
|
||||
): 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;
|
||||
}
|
||||
}
|
||||
+8
-71
@@ -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<string> {
|
||||
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<string>,
|
||||
): ReadonlyArray<string> {
|
||||
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<string>,
|
||||
): 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" ? ";" : ":";
|
||||
}
|
||||
|
||||
@@ -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<string> {
|
||||
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<string>,
|
||||
): ReadonlyArray<string> {
|
||||
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<string>,
|
||||
): 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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<NodeServices.NodeServices>,
|
||||
);
|
||||
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<NodeServices.NodeServices>,
|
||||
);
|
||||
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<NodeServices.NodeServices>,
|
||||
);
|
||||
const lifecycleLayer = it.layer(
|
||||
makeCodexAdapterLive({ manager: lifecycleManager }).pipe(
|
||||
Layer.provideMerge(ServerConfig.layerTest(process.cwd(), process.cwd())),
|
||||
|
||||
Reference in new issue
Block a user