Files
reverse-engineering-anything/src/application/OpenCodeSystemLocation.ts
T
2afad5b789 fix(setup): honor OpenCode system, managed and policy configuration (#15081)
Discover the OpenCode system and managed layers and honor their effective
MCP policy when planning, publishing, diagnosing, or removing registrations.
Preserve the writable layer's dialect and verify effective registration
and policy after publication.

Fixes #15036.

Assisted-by: Kogasa (gensokyo-skills:kogasa-surprise-testing)
Co-authored-by: N0zoM1z0 <161784452+N0zoM1z0@users.noreply.github.com>
Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>
2026-12-01 00:48:10 +08:00

130 lines
4.2 KiB
TypeScript

import { PRODUCT_IDENTITY } from "../identity.js";
import { err, ok, type Result } from "../domain/result.js";
import type {
ClientConfigurationDocument,
ClientInlineConfiguration,
} from "./ClientConfigurationDocument.js";
import {
reachedReadOnlyFile,
reachedReadOnlyFileDetail,
readConfigurationFile,
} from "./ClientConfigurationFiles.js";
import {
currentUser,
readOpenCodeManagedPreferences,
} from "./OpenCodeManagedPreferences.js";
import {
existingOpenCodeConfigPath,
openCodeConfigurationPaths,
openCodeManagedPreferencePaths,
openCodeSystemConfigPaths,
} from "./SupportedClientLocations.js";
import type {
ClientLayeredLocation,
ClientPathContext,
ClientPathFailure,
} from "./SupportedClients.js";
/** The system and managed files as one read-only layer, in load order. */
const readUpperLayers = async (
context: ClientPathContext,
): Promise<
Result<ClientInlineConfiguration | undefined, ClientPathFailure>
> => {
const documents: (ClientConfigurationDocument & { readOnlyPath: string })[] =
[];
let declaring: string | undefined;
let last: string | undefined;
const add = (path: string, document: ClientConfigurationDocument) => {
documents.push({ ...document, readOnlyPath: path });
last = path;
const name = PRODUCT_IDENTITY.mcpServerKey;
if (
Object.hasOwn(document.servers, name) ||
Object.hasOwn(document.legacyServers, name)
)
declaring = path;
};
for (const path of openCodeSystemConfigPaths(context)) {
const read = await readConfigurationFile(
path,
"opencode",
context.resources,
);
if (!read.ok) {
// Without a scope to retain it, the failure must carry its owner out.
if (
read.error.cleanupFailure !== undefined &&
context.resources === undefined
)
throw read.error.cleanupFailure;
return err({ path, message: read.error.detail });
}
if (read.value !== undefined) add(path, read.value);
}
// Windows alone lets any local account create these files by default. Any
// of them may be what a message relies on, so the caution names them all.
const system = documents.map(({ readOnlyPath }) => readOnlyPath);
const caution =
context.platform === "win32" && system.length > 0
? `On Windows any local account can create files under %ProgramData% by default, so confirm an administrator owns ${system.join(" and ")} before trusting these settings.`
: undefined;
const managed = await readOpenCodeManagedPreferences(context);
if (!managed.ok) return managed;
if (managed.value !== undefined)
add(managed.value.path, managed.value.document);
const origin = declaring ?? last;
return ok(
origin === undefined
? undefined
: {
origin,
path: origin,
documents,
...(caution === undefined ? {} : { caution }),
},
);
};
/**
* The user files REA may write, beneath OpenCode's system files and macOS
* managed preferences, which override them and which REA never edits.
*/
export const openCodeLayeredLocation = async (
context: ClientPathContext,
): Promise<Result<ClientLayeredLocation, ClientPathFailure>> => {
const configPath = existingOpenCodeConfigPath(context);
const configPaths = openCodeConfigurationPaths(context);
const system = openCodeSystemConfigPaths(context);
const readOnlyFiles = [
...system,
...openCodeManagedPreferencePaths(context, currentUser()),
];
// A user path or link reaching one of these files would let REA edit it.
const reached = await reachedReadOnlyFile(
[configPath, ...configPaths],
readOnlyFiles,
context.platform,
);
if (reached !== undefined)
return err({
path: reached.path,
message: reachedReadOnlyFileDetail(reached),
});
const upper = await readUpperLayers(context);
if (!upper.ok) return upper;
return ok({
configPath,
configPaths,
// Later reads use the scope of the operation that asks for them.
readUpperLayers: (resources) => {
const { resources: _selection, ...rest } = context;
return readUpperLayers(
resources === undefined ? rest : { ...rest, resources },
);
},
readOnlyFiles,
...(upper.value === undefined ? {} : { inlineConfiguration: upper.value }),
});
};