UNPKG

@code-yeongyu/senpi

Version:

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

60 lines • 2.9 kB
import { createHash } from "node:crypto"; const sessionDependent = new WeakSet(); const sessionVariable = /(?:cwd|session|^PWD$|^OLDPWD$)/i; const sessionTemplate = /\$\{[^}]*(?:cwd|session|PWD)[^}]*\}/i; /** Preserve interpolation provenance without changing serialized config/cache identity. */ export function inheritMcpSharingScope(source, target) { if (sessionDependent.has(source) || hasSessionValues(source)) sessionDependent.add(target); } function hasSessionValues(config, cwd) { const values = [config.command, ...(config.args ?? []), ...Object.values(config.env ?? {})]; return (Object.keys(config.env ?? {}).some((key) => sessionVariable.test(key)) || [...values, config.cwd].some((value) => typeof value === "string" && sessionTemplate.test(value)) || (cwd !== undefined && values.some((value) => typeof value === "string" && value.includes(cwd)))); } export function shareable(config, cwd) { return config.type === "http" || (!sessionDependent.has(config) && !hasSessionValues(config, cwd)); } /** Transport identity excludes per-owner exposure and idle/keep-alive policy. */ export function sharedMcpKey(options, agentDir) { const { config, env } = options; const bearer = config.bearerTokenEnv; const identity = { ...(options.projectPolicy === undefined ? {} : { declarationDigest: options.projectPolicy.identity, policyDigest: options.projectPolicy.policyDigest, skillAuthorityDigest: options.projectPolicy.provenanceDigest, }), credentialIdentity: options.credentialIdentity, agentDir, name: options.serverName, type: config.type, url: config.type === "http" ? config.url : undefined, command: config.type === "stdio" ? config.command : undefined, args: config.type === "stdio" ? config.args : undefined, cwd: config.type === "stdio" ? (config.cwd ?? process.cwd()) : undefined, env: config.type === "stdio" ? { ...env, ...config.env } : undefined, headers: config.headers, auth: config.auth, oauth: config.oauth, bearer: bearer === undefined ? undefined : (env?.[bearer] ?? process.env[bearer]), connectTimeoutMs: config.connectTimeoutMs, logLevel: config.logLevel, }; return createHash("sha256").update(stableJson(identity)).digest("hex"); } function stableJson(value) { if (Array.isArray(value)) return `[${value.map(stableJson).join(",")}]`; if (value !== null && typeof value === "object") { return `{${Object.entries(value) .sort(([a], [b]) => a.localeCompare(b)) .map(([key, item]) => `${JSON.stringify(key)}:${stableJson(item)}`) .join(",")}}`; } return JSON.stringify(value) ?? "null"; } //# sourceMappingURL=sharing-policy.js.map