@sema-agent/core
Version:
Stateless, task-oriented AI agent core
102 lines (101 loc) • 4.75 kB
JavaScript
import { parsePermissionRule } from "./permission-rule-syntax.js";
import { isUsablePathBase, pathRuleHeadOf, pathRuleReachOf, ruleBasesNeeded } from "./permission-rule-model.js";
import { pathFamilyOf, spellingOfIdentity } from "../tools/fs/safety.js";
import { absoluteTargetIdentityOf } from "./effective-path-target.js";
import { locationLeadsTo, ruleHeadParts } from "../tools/fs/read-deny.js";
import { carriedMembersOf } from "./tool-policy.js";
import { assertNoRetiredToolPolicyKeys } from "./retired-keys.js";
export { judgeRuleRows, rowsWhereTheyLead } from "../tools/fs/read-deny.js";
const NOT_REACHED = { reach: "not_reached" };
export function bindPathRules(specs, anchors) {
return specs.map((spec) => {
const a = typeof anchors === "function" ? anchors(spec) : anchors;
const family = pathFamilyOf(a);
const head = pathRuleHeadOf(spec.parsed, a);
const unanchored = ruleBasesNeeded(spec.parsed).filter((b) => !isUsablePathBase(b === "cwd" ? (a.cwd ?? a.root) : a[b], family));
const reach = (absolutePath) => {
const tree = a.root !== undefined || a.cwd !== undefined ? a : { root: absolutePath };
const id = absoluteTargetIdentityOf(absolutePath, tree);
return id === undefined ? NOT_REACHED : pathRuleReachOf(spec.parsed, id, a, pathFamilyOf(tree));
};
return {
spec,
behavior: spec.parsed.behavior,
rule: spec.ruleText,
tool: spec.parsed.tool,
pattern: parsePermissionRule(spec.ruleText).ruleContent ?? "",
faces: spec.faces ?? [],
reach,
reaches: (absolutePath) => reach(absolutePath).reach !== "not_reached",
...(head !== undefined ? { head: spellingOfIdentity(head, family) } : {}),
...(unanchored.length > 0 ? { unanchored } : {}),
};
});
}
export function policyAnchors(declared, install) {
const root = declared.root ?? install?.root;
const cwd = declared.cwd ?? root;
const home = declared.home ?? install?.home;
return { ...(root !== undefined ? { root } : {}), ...(cwd !== undefined ? { cwd } : {}), ...(home !== undefined ? { home } : {}) };
}
export function persistedRowAnchors(scope, chainTop) {
const root = scope.kind === "project" ? scope.root : chainTop.root;
return { ...(root !== undefined ? { root, cwd: root } : {}), ...(chainTop.home !== undefined ? { home: chainTop.home } : {}) };
}
export async function projectScopeIdentity(rules, canonicalRoot, leads) {
if (canonicalRoot === undefined)
return undefined;
const roots = rules.flatMap((r) => (r.scope.kind === "project" ? [{ head: r.scope.root }] : []));
const led = roots.length > 0 ? await leads.resolve(roots) : new Map();
return { canonicalRoot, canonicalOf: (root) => locationLeadsTo(root, led) };
}
export function createHeadLeads(leadsTo) {
const pending = new Map();
const settled = new Map();
return {
async resolve(rows) {
const parts = ruleHeadParts(rows).filter((p) => !settled.has(p));
await Promise.all(parts.map((part) => {
let p = pending.get(part);
if (p === undefined) {
p = leadsTo(part).catch(() => undefined);
pending.set(part, p);
}
return p.then((led) => settled.set(part, led));
}));
return settled;
},
};
}
const INSTALLED = new WeakMap();
const INSTALLATIONS = new WeakMap();
export function installPolicyAnchors(policy, anchors) {
if (policy === undefined)
return undefined;
assertNoRetiredToolPolicyKeys(policy);
const from = INSTALLED.get(policy)?.from ?? policy;
const rules = from.pathRules;
if (rules === undefined)
return from;
const key = JSON.stringify([anchors.root ?? null, anchors.cwd ?? null, anchors.home ?? null]);
const byAnchors = INSTALLATIONS.get(from) ?? new Map();
INSTALLATIONS.set(from, byAnchors);
const prior = byAnchors.get(key);
if (prior !== undefined)
return prior;
const seated = Object.freeze({ ...anchors });
const installed = {
...carriedMembersOf(from),
pathRules: () => rules(seated),
check: (req, signal) => from.check({ ...req, pathRuleAnchors: seated }, signal),
};
INSTALLED.set(installed, { from, anchors: seated });
byAnchors.set(key, installed);
return installed;
}
export function installedFrom(policy) {
return policy === undefined ? undefined : INSTALLED.get(policy)?.from;
}
export function installationAnchorsOf(policy) {
return policy === undefined ? undefined : INSTALLED.get(policy)?.anchors;
}