UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

188 lines • 8.57 kB
import { realpathSync } from "node:fs"; import { basename, dirname, resolve } from "node:path"; import { getMcpGrantPresentation, selectMcpGrant } from "../../../mcp-grant-presentations.js"; import { skillProjectRoot } from "../../../skill-access.js"; import { hashConfig, normalizeServer } from "./config.js"; import { plainMcpDeclaration, plainMcpServerConfig } from "./config-references.js"; import { addGrant, declarationDigest, read, resolveGrantsStorePath, } from "./env-grants-store.js"; import { ConnectError } from "./errors.js"; import { projectMcpEnvironment } from "./project-env.js"; import { inheritMcpSharingScope } from "./sharing-policy.js"; import { parseSkillMcpDeclarations } from "./skills.js"; const policies = new WeakMap(); const decisions = new Map(); export const getProjectMcpAuthority = (config) => policies.get(config); export function effectiveGrantNames(snapshot, identity) { return snapshot.grants[identity]?.names ?? []; } function currentRegistration(name, declaration) { if (declaration.skillPath === undefined) return declaration; const current = parseSkillMcpDeclarations([ { name: declaration.skillName, filePath: declaration.skillPath, baseDir: dirname(declaration.skillPath), provenance: declaration.provenance, allowedSet: declaration.allowedSet, sourceInfo: declaration.scope === undefined ? undefined : { scope: declaration.scope }, }, ]).servers.get(name); if (current !== undefined) return current; throw new ConnectError("Project MCP declaration is no longer available; reload its skills", { serverName: name, phase: "create", retriable: true, }); } function identityFor(name, declaration, cwd) { return declarationDigest({ sourcePath: realpathSync.native(declaration.sourcePath), sourceKind: basename(declaration.sourcePath) === "mcp.json" ? "sidecar" : "frontmatter", projectRoot: realpathSync.native(declaration.provenance?.projectRoot ?? skillProjectRoot(cwd)), serverName: name, raw: declaration.raw, cwdBase: realpathSync.native(cwd), }); } export async function prepareProjectSkillServer(name, original, owner, existing) { const storePath = resolveGrantsStorePath(owner.options.agentDir); const trusted = owner.options.projectTrusted ?? owner.context.isProjectTrusted(); let declaration = currentRegistration(name, original); let identity = identityFor(name, declaration, owner.context.cwd); const previous = existing?.config === undefined ? undefined : policies.get(existing.config); const provenanceDigest = declarationDigest({ provenance: declaration.provenance, allowedSet: declaration.allowedSet?.digest, }); if (previous?.identity === identity && previous.storePath === storePath && previous.provenanceDigest === provenanceDigest && existing !== undefined) { const snapshot = await read(storePath); owner.notice(identity, projectMcpEnvironment(plainMcpDeclaration(name, declaration.raw), trusted, effectiveGrantNames(snapshot, identity), owner.options.env).withheld); return existing; } const project = declaration.provenance?.projectRoot ?? skillProjectRoot(owner.context.cwd); const select = () => owner.context.mode === "tui" ? owner.ui()?.select : getMcpGrantPresentation(owner.context.sessionManager?.getSessionId?.()); const calculate = (raw, names) => projectMcpEnvironment(plainMcpDeclaration(name, raw), trusted, names, owner.options.env); const decide = async (current, digest, signal) => { const key = `${storePath}\0${digest}`; let pending = decisions.get(key); if (pending === undefined) { pending = (async () => { const snapshot = await read(storePath); const { withheld } = calculate(current.raw, effectiveGrantNames(snapshot, digest)); if (withheld.length === 0 || signal?.aborted) return; owner.notice(digest, withheld); const granted = await selectMcpGrant(select(), `Project MCP server ${name} (skill ${current.skillName}) wants: ${withheld.join(", ")}`, signal); if (!granted || signal?.aborted) return; const latest = currentRegistration(name, current); if (identityFor(name, latest, owner.context.cwd) !== digest) return; try { await addGrant(storePath, digest, { project, skill: current.skillName, server: name }, withheld); owner.ui()?.notify?.(`Saved MCP environment grant for ${name}: ${withheld.join(", ")}`, "info"); } catch { owner .ui() ?.notify?.(`Could not save MCP environment grant for ${name}; starting without ${withheld.join(", ")}`, "error"); } })(); decisions.set(key, pending); void pending .finally(() => { if (decisions.get(key) === pending) decisions.delete(key); }) .catch(() => { }); } await pending; }; for (;;) { await decide(declaration, identity); const latest = currentRegistration(name, declaration); const latestIdentity = identityFor(name, latest, owner.context.cwd); declaration = latest; if (latestIdentity === identity) break; identity = latestIdentity; } const snapshot = await read(storePath); const names = effectiveGrantNames(snapshot, identity); const materialize = (currentNames) => { const policy = calculate(declaration.raw, currentNames); owner.notice(identity, policy.withheld); const config = { ...plainMcpServerConfig(name, normalizeServer(policy.declaration)), env: {}, directTools: [], exposure: "search", }; if (config.type === "stdio") config.cwd = resolve(owner.context.cwd, config.cwd ?? "."); inheritMcpSharingScope(declaration.raw, config); return { config, env: policy.env, grantedNames: currentNames }; }; const initial = materialize(names); let first = true; const authority = { identity, storePath, initial, provenanceDigest: declarationDigest({ provenance: declaration.provenance, allowedSet: declaration.allowedSet?.digest, }), policyDigest: declarationDigest({ names, generation: snapshot.generation }), resolve: async (signal, allowDecision = true) => { signal.throwIfAborted(); const latest = currentRegistration(name, declaration); if (identityFor(name, latest, owner.context.cwd) !== identity) { throw new ConnectError("Project MCP declaration changed; reload its skills", { serverName: name, phase: "create", retriable: true, }); } if (allowDecision) { if (!first) await decide(latest, identity, signal); first = false; } signal.throwIfAborted(); const current = await read(storePath); signal.throwIfAborted(); if (identityFor(name, currentRegistration(name, declaration), owner.context.cwd) !== identity) { throw new ConnectError("Project MCP declaration changed; reload its skills", { serverName: name, phase: "create", retriable: true, }); } return materialize(effectiveGrantNames(current, identity)); }, }; policies.set(initial.config, authority); return { name, source: "skill", sourcePath: declaration.sourcePath, config: initial.config, configHash: declarationDigest({ config: hashConfig(initial.config), identity, policy: authority.policyDigest, provenance: authority.provenanceDigest, }), state: initial.config.enabled ? "enabled" : "disabled", transport: initial.config.type, }; } //# sourceMappingURL=project-skill-policy.js.map