UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

63 lines (62 loc) • 4.12 kB
import { attest, attestationOf } from "./tool-policy.js"; import { callPathFaceOf } from "./permission-rule-model.js"; import { decisionAskClassesOf, ownAskClassesOf } from "./ask-origin.js"; import { inlineUntrusted } from "./untrusted-text.js"; import { ruleStoreUnreadableAskSentence } from "./rule-store-fault.js"; export function strongestAnswer(answers) { const rank = (a) => (a === undefined ? 0 : "unreadable" in a ? 4 : "behavior" in a ? { deny: 5, ask: 3, allow: 2 }[a.behavior] : 1); return answers.reduce((best, a) => (rank(a) > rank(best) ? a : best), undefined); } export function disclosedRuleSet(rules) { const shown = rules.slice(0, 5).map((r) => inlineUntrusted(r.rule, 200)); const rest = rules.length - shown.length; return rest > 0 ? `${shown.join(", ")} and ${rest} more` : shown.join(", "); } export function applyPersistedTightening(decision, read, call, seat = "persisted") { if (read?.unreadable === true) { if (decision.action === "deny") return { decision }; const callForm = read.reason !== undefined; const standing = !callForm && read.hit?.behavior === "ask" ? askRowOn(decision, read.hit, call, seat) : decision; const message = callForm ? `this call could not be read against the person's standing deny/ask rules (${read.reason}) — whether one of them forbids it is unknown, so it needs a real approval` : ruleStoreUnreadableAskSentence(read.faults); const unreadableCall = callForm ? { ruleUnreadable: "persisted_call" } : {}; const tightened = callForm ? "unreadable" : "store_unreadable"; if (standing.action === "allow") { return { decision: { action: "ask", message, decisionReason: "persisted_rule", requiresRealApproval: true, ...unreadableCall, ...(standing.updatedInput !== undefined ? { updatedInput: standing.updatedInput } : {}) }, tightened }; } const asked = { ...standing, requiresRealApproval: true, ...unreadableCall, message: `${standing.message !== undefined ? `${standing.message} ` : ""}(${message})` }; return { decision: ownAskClassesOf(standing, attestationOf(standing, call)).has("policy_rra") ? foldedOnto(asked, call, ["policy_rra"]) : asked, tightened }; } const hit = read?.hit; if (hit === undefined || hit.behavior === "allow") return { decision }; const shown = disclosedRuleSet(hit.rules); if (hit.behavior === "deny") { const denied = { action: "deny", message: `a persisted deny rule (${shown}) refuses this call`, decisionReason: "persisted_rule" }; const attested = hit.denial !== undefined && hit.denial.face === callPathFaceOf(call); return { decision: attested ? attest(denied, call, { pathDenial: hit.denial }) : denied, tightened: "deny" }; } if (decision.action === "deny") return { decision }; return { decision: askRowOn(decision, hit, call, seat), tightened: "ask" }; } function askRowOn(decision, hit, call, seat) { const shown = disclosedRuleSet(hit.rules); const said = seat === "policy" ? `an ask rule of this task's permission policy (${shown})` : `a persisted ask rule (${shown})`; const rowAsk = { action: "ask", message: `${said} requires approval for this call`, decisionReason: seat === "policy" ? "rule" : "persisted_rule", matchedAskRule: shown }; if (decision.action === "allow") return { ...rowAsk, ...(decision.updatedInput !== undefined ? { updatedInput: decision.updatedInput } : {}) }; const asked = { ...decision }; const holdsRank = asked.matchedAskRule !== undefined; const marked = { ...asked, ...(holdsRank ? {} : { matchedAskRule: shown }), message: `${asked.message !== undefined ? `${asked.message} ` : ""}(${said} also requires approval for this call)`, }; return holdsRank ? foldedOnto(marked, call, decisionAskClassesOf(rowAsk, call)) : marked; } function foldedOnto(d, call, classes) { return attest(d, call, { foldedClasses: [...new Set([...(attestationOf(d, call)?.foldedClasses ?? []), ...classes])] }); }