fix(setup): reject invalid OMP profiles before fallback writes (#1562)

This commit is contained in:
Ysopwnable
2026-10-10 22:11:14 +08:00
committed by GitHub
parent a53689c55d
commit e4cc0f09de
6 changed files with 638 additions and 30 deletions
+66 -29
View File
@@ -1,6 +1,7 @@
import { dirname, isAbsolute, join, resolve, win32 } from "node:path";
import { fileURLToPath } from "node:url";
import { lstatSync } from "node:fs";
import { err, ok, type Result } from "../domain/result.js";
import {
HermesClientPathError,
resolveHermesClientHome,
@@ -39,7 +40,15 @@ export interface SetupClient {
| "unsupported";
}
type ClientPath = readonly string[] | ((paths: ClientPathContext) => string);
interface ClientPathFailure {
readonly path: string;
readonly message: string;
}
type ClientPathResult = Result<string, ClientPathFailure>;
type ClientPath =
| readonly string[]
| ((paths: ClientPathContext) => string | ClientPathResult);
interface ClientPathContext {
readonly home: string;
@@ -146,34 +155,32 @@ export const manualRegistrationRemediation = (
: undefined;
const OMP_PROFILE_NAME = /^[a-z0-9][a-z0-9._-]{0,63}$/u;
/**
* OMP's active named profile. OMP_PROFILE wins over the legacy PI_PROFILE even
* when empty; empty and "default" select the default profile. OMP refuses to
* start with an invalid name, so one leaves the default location in place.
*/
const ompProfile = ({ env }: ClientPathContext): string | undefined => {
const name = (env.OMP_PROFILE ?? env.PI_PROFILE)?.trim();
return name === undefined ||
name === "" ||
name === "default" ||
name.endsWith(".") ||
!OMP_PROFILE_NAME.test(name)
? undefined
: name;
};
const OMP_RESERVED_PROFILE =
/^(?:CON|PRN|AUX|NUL|COM[0-9]|LPT[0-9])(?:\..*)?$/iu;
/**
* The OMP agent directory whose `mcp.json` holds user-scope MCP servers. OMP
* joins PI_CONFIG_DIR to the home directory, and a named profile ignores
* PI_CODING_AGENT_DIR. Relative overrides use the setup process working directory.
*/
const ompAgentDirectory = (context: ClientPathContext): string => {
const ompAgentDirectory = (context: ClientPathContext): ClientPathResult => {
const root = join(context.home, context.env.PI_CONFIG_DIR || ".omp");
const profile = ompProfile(context);
if (profile !== undefined) return join(root, "profiles", profile, "agent");
// OMP_PROFILE wins even when empty. Match OMP's validation on every host.
const profile = (context.env.OMP_PROFILE ?? context.env.PI_PROFILE)?.trim();
if (profile !== undefined && profile !== "" && profile !== "default") {
if (
!OMP_PROFILE_NAME.test(profile) ||
profile.endsWith(".") ||
OMP_RESERVED_PROFILE.test(profile)
)
return err({
path: root,
message: `Invalid OMP profile ${JSON.stringify(profile)}: names must match ^[a-z0-9][a-z0-9._-]{0,63}$, cannot end with ".", and cannot be a Windows reserved device name (CON, PRN, AUX, NUL, COM0-9, LPT0-9, including extensions). Repair OMP_PROFILE or PI_PROFILE before configuring OMP.`,
});
return ok(join(root, "profiles", profile, "agent"));
}
const override = context.env.PI_CODING_AGENT_DIR;
return override ? resolve(override) : join(root, "agent");
return ok(override ? resolve(override) : join(root, "agent"));
};
/**
@@ -456,8 +463,10 @@ export const SUPPORTED_CLIENT_DEFINITIONS = [
{
name: "omp",
displayName: "OMP",
configPath: (context: ClientPathContext) =>
join(ompAgentDirectory(context), "mcp.json"),
configPath: (context: ClientPathContext) => {
const directory = ompAgentDirectory(context);
return directory.ok ? ok(join(directory.value, "mcp.json")) : directory;
},
markerPath: ompAgentDirectory,
format: "omp",
},
@@ -508,8 +517,14 @@ export const clientEvidencePaths = (
),
];
const resolvePath = (path: ClientPath, context: ClientPathContext): string =>
typeof path === "function" ? path(context) : join(context.home, ...path);
const resolvePath = (
path: ClientPath,
context: ClientPathContext,
): ClientPathResult => {
const resolved =
typeof path === "function" ? path(context) : join(context.home, ...path);
return typeof resolved === "string" ? ok(resolved) : resolved;
};
/** Describe every client location that setup, doctor, or uninstall may inspect. */
export const supportedClients = (
@@ -521,7 +536,27 @@ export const supportedClients = (
const definitions: readonly ClientDefinition[] = SUPPORTED_CLIENT_DEFINITIONS;
return definitions.map((definition) => {
try {
const configPath = resolvePath(definition.configPath, context);
const config = resolvePath(definition.configPath, context);
const marker = config.ok
? resolvePath(definition.markerPath, context)
: config;
if (!config.ok)
return {
name: definition.name,
displayName: definition.displayName,
format: definition.format,
configPath: config.error.path,
configPathError: config.error.message,
};
if (!marker.ok)
return {
name: definition.name,
displayName: definition.displayName,
format: definition.format,
configPath: marker.error.path,
configPathError: marker.error.message,
};
const configPath = config.value;
return {
name: definition.name,
displayName: definition.displayName,
@@ -536,7 +571,7 @@ export const supportedClients = (
...new Set([...definition.configPaths(context), configPath]),
],
}),
markerPath: resolvePath(definition.markerPath, context),
markerPath: marker.value,
format: definition.format,
};
} catch (cause: unknown) {
@@ -584,14 +619,16 @@ export const clientSkillDirectories = (
const definition = definitions.find(({ name }) => name === client);
let directory: string;
try {
directory =
const resolved =
definition?.skillPath === undefined
? join(home, ".agents", "skills")
? ok(join(home, ".agents", "skills"))
: resolvePath(definition.skillPath, {
home,
platform,
env: environment,
});
if (!resolved.ok) continue;
directory = resolved.value;
} catch (cause: unknown) {
// Registration diagnostics own an unresolved profile; never fall back to another skill root.
if (cause instanceof HermesClientPathError) continue;