UNPKG

@code-yeongyu/senpi

Version:

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

119 lines • 6.02 kB
import * as path from "node:path"; import { containsPrefix } from "./alias-detector.js"; import { identityMatches, PATH_IDENTITY_VERSION } from "./path-identity.js"; import { Wildcard } from "./wildcard.js"; /** The permission classes with a path target; only these take the identity route (M4). */ export const PATH_PERMISSIONS = new Set(["read", "edit", "list", "grep"]); /** The static and approved rules that could match by identity: a path class, and a pattern other than `*`. */ export function pathRulesOf(rules) { return rules.filter((rule) => rule.pattern !== "*" && [...PATH_PERMISSIONS].some((member) => Wildcard.match(member, rule.permission))); } /** * True when a static or approved rule of a path class could match by identity (R3-H2 gate). With the * resolver unavailable only a request that brings its own current canonical (a JS cell) keeps the route: * it needs no worker, so its decision stays exact; every other request matches by spelling only. */ export function identityWanted(resolver, rules, permission, kernel) { if (!resolver || !PATH_PERMISSIONS.has(permission)) return false; if (resolver.unavailable !== undefined && kernel?.canonical?.v !== PATH_IDENTITY_VERSION) return false; return pathRulesOf(rules).length > 0; } /** * The identity input for one request (6.2, 6.5.1). Synchronous when the kernel supplied a current canonical * and no rule of the class is pending or timed out; otherwise it awaits the resolver worker. Never throws: * every resolver failure becomes `unresolvable`. */ export function prepareIdentity(context, permission, executed, cwd, kernel) { const none = { canonical: [] }; if (!context.identityWanted(permission, kernel) || !context.resolver) return none; if (kernel?.unresolvable !== undefined && !kernel.canonical) return { canonical: [], unresolvable: kernel.unresolvable }; const current = kernel?.canonical?.v === PATH_IDENTITY_VERSION ? kernel.canonical : undefined; // Unavailable resolver: the rule forms it managed to fill before failing are all there will be, and // nothing can time out, so the kernel canonical is used as is. if (current && context.resolver.unavailable !== undefined) { return { canonical: [{ path: current.path, foldCase: current.foldCase }] }; } const settle = context.cache.settleRules(permission); const timedOut = () => context.cache.timedOutDenies(permission); if (current && settle === undefined && timedOut().length === 0) { return { canonical: [{ path: current.path, foldCase: current.foldCase }] }; } const resolver = context.resolver; return (async () => { if (settle) await settle; let canonical = []; let unresolvable; if (current) canonical = [{ path: current.path, foldCase: current.foldCase }]; else { for (const value of executed) { try { canonical.push(await resolver.canonicalize(value, cwd)); } catch (error) { unresolvable = error.code ?? "EUNKNOWN"; } } } const failClosed = await failsClosed(context, permission, executed, canonical, current !== undefined); return { canonical, ...(unresolvable === undefined ? {} : { unresolvable }), ...(failClosed ? { failClosed } : {}), }; })(); } /** 6.5.1: deny only when a timed-out DENY rule of the class could reach this request through an alias. */ async function failsClosed(context, permission, executed, canonical, kernelExact) { const timedOut = context.cache.timedOutDenies(permission); if (timedOut.length === 0 || !context.resolver) return false; const resolver = context.resolver; const aliasInPlay = async (value, index) => { const known = canonical[index]; if (kernelExact && known) return known.path !== value; if (resolver.breakerState(resolver.volumeKey(value)) === "open") return true; return resolver.detect(value); }; for (const [index, value] of executed.entries()) { if (await aliasInPlay(value, index)) return true; const requestCanonical = canonical[index]?.path ?? (await resolver.walk(value).catch(() => undefined)); if (requestCanonical === undefined) return true; for (const rule of timedOut) { if (!context.cache.aliasBearing(rule.pattern)) continue; if (context.cache.anchoredPrefix(rule.pattern) === "") continue; // The rule side was walked once at rules load on the worker; a pending or failed walk is over-inclusive. const walked = context.cache.walkedPrefix(rule.pattern); if (typeof walked !== "string") return true; const foldCase = canonical[index]?.foldCase ?? (process.platform === "darwin" || process.platform === "win32"); if (containsPrefix(walked, requestCanonical, context.mountRewrite, foldCase, (v) => v)) return true; } } return false; } /** The identity predicate for one request: takes the RULE pattern only and tests every canonical path (H1). */ export function identityMatcher(cache, permission, identity, rewrite, resolverUnavailable = false) { if (!identity || !PATH_PERMISSIONS.has(permission) || identity.canonical.length === 0) return undefined; return (rulePattern) => { const cached = cache.stateOf(rulePattern); const entry = typeof cached === "string" && resolverUnavailable ? cache.lexicalForm(rulePattern) : cached; if (entry === undefined || typeof entry === "string") return false; return identity.canonical.some((request) => identityMatches(entry, request, Wildcard.match, path.sep, rewrite)); }; } //# sourceMappingURL=identity-decision.js.map