mirror of
https://github.com/morluto/rea.git
synced 2026-12-05 14:31:22 +00:00
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>
120 lines
4.5 KiB
TypeScript
120 lines
4.5 KiB
TypeScript
import { PRODUCT_IDENTITY } from "../identity.js";
|
|
import { ok, type Result } from "../domain/result.js";
|
|
import { isRecord } from "../kernel/isRecord.js";
|
|
import type { ArtifactResourceScope } from "../resources/index.js";
|
|
import {
|
|
currentUpperLayers,
|
|
inlineCaution,
|
|
readConfigurationFile,
|
|
type ClientConfigurationFileError,
|
|
} from "./ClientConfigurationFiles.js";
|
|
import type { SetupClient } from "./SupportedClients.js";
|
|
|
|
interface McpStatement {
|
|
readonly resource: string;
|
|
readonly effect: "allow" | "deny";
|
|
readonly file: string;
|
|
}
|
|
|
|
/** `mcp.use` statements; OpenCode drops statements that fail validation. */
|
|
const mcpStatements = (
|
|
document: Record<string, unknown>,
|
|
file: string,
|
|
): readonly McpStatement[] => {
|
|
const experimental = isRecord(document.experimental)
|
|
? document.experimental.policies
|
|
: undefined;
|
|
return [experimental, document.policies].flatMap((list) =>
|
|
(Array.isArray(list) ? (list as readonly unknown[]) : []).flatMap(
|
|
(statement) => {
|
|
if (!isRecord(statement)) return [];
|
|
const { action, resource, effect } = statement;
|
|
if (typeof resource !== "string") return [];
|
|
if (effect !== "allow" && effect !== "deny") return [];
|
|
// Releases before 2.2.3 wrote MCP rules as prefixed integration rules.
|
|
const named =
|
|
action === "mcp.use"
|
|
? resource
|
|
: action === "integration.use" && resource.startsWith("mcp:")
|
|
? resource.slice("mcp:".length)
|
|
: undefined;
|
|
return named === undefined ? [] : [{ resource: named, effect, file }];
|
|
},
|
|
),
|
|
);
|
|
};
|
|
|
|
/** Match the whole name using bounded state per name character. */
|
|
const matches = (
|
|
pattern: string,
|
|
name: string,
|
|
platform: NodeJS.Platform,
|
|
): boolean => {
|
|
if (platform === "win32") {
|
|
pattern = pattern.toLowerCase();
|
|
name = name.toLowerCase();
|
|
}
|
|
|
|
const positions = new Uint8Array(name.length + 1);
|
|
positions[0] = 1;
|
|
for (let offset = 0; offset < pattern.length; offset++) {
|
|
const character = pattern[offset] === "\\" ? "/" : pattern[offset];
|
|
if (character === "*") {
|
|
// A star can preserve a match or extend it to every following position.
|
|
for (let index = 1; index < positions.length; index++)
|
|
if (positions[index - 1] === 1) positions[index] = 1;
|
|
continue;
|
|
}
|
|
|
|
for (let index = name.length; index > 0; index--)
|
|
positions[index] =
|
|
positions[index - 1] === 1 &&
|
|
(character === "?" || character === name[index - 1])
|
|
? 1
|
|
: 0;
|
|
positions[0] = 0;
|
|
}
|
|
return positions[name.length] === 1;
|
|
};
|
|
|
|
/**
|
|
* Why OpenCode would not load REA's server. Policies from higher authority are
|
|
* evaluated later and the last match wins, so user files come first and system
|
|
* and managed files after them. Project and Console policies are not read; a
|
|
* project cannot lift a deny from these files.
|
|
*/
|
|
export const readOpenCodePolicyBlock = async (
|
|
client: SetupClient,
|
|
platform: NodeJS.Platform,
|
|
resources: ArtifactResourceScope | undefined,
|
|
): Promise<Result<string | undefined, ClientConfigurationFileError>> => {
|
|
const statements: McpStatement[] = [];
|
|
// OpenCode evaluates ordinary file policies in reverse load order. Managed
|
|
// policies are moved ahead of them by the loader, in reverse, so their
|
|
// original order determines the final authority.
|
|
for (const path of (client.configPaths ?? [client.configPath]).toReversed()) {
|
|
const read = await readConfigurationFile(path, "opencode", resources);
|
|
if (!read.ok) return read;
|
|
if (read.value !== undefined)
|
|
statements.push(...mcpStatements(read.value.document, path));
|
|
}
|
|
const current = await currentUpperLayers(client, resources);
|
|
if (!current.ok) return current;
|
|
for (const { document, readOnlyPath } of current.value.inlineConfiguration
|
|
?.documents ?? [])
|
|
if (readOnlyPath !== undefined)
|
|
statements.push(...mcpStatements(document, readOnlyPath));
|
|
const name = PRODUCT_IDENTITY.mcpServerKey;
|
|
const decisive = statements.findLast(({ resource }) =>
|
|
matches(resource, name, platform),
|
|
);
|
|
const fromUpper = current.value.inlineConfiguration?.documents.some(
|
|
({ readOnlyPath }) => readOnlyPath === decisive?.file,
|
|
);
|
|
return ok(
|
|
decisive?.effect === "deny"
|
|
? `OpenCode policy in ${decisive.file} denies mcp.use for the ${name} server, so OpenCode does not load it; REA does not edit policies. Change that statement, then rerun ${PRODUCT_IDENTITY.cliBinary} doctor.${fromUpper === true ? inlineCaution(current.value) : ""}`
|
|
: undefined,
|
|
);
|
|
};
|