UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

66 lines (65 loc) • 3.69 kB
import { attest } from "./tool-policy.js"; import { callPathFaceOf } from "./permission-rule-model.js"; import { decisionAskClassesOf } from "./ask-origin.js"; import { inlineUntrusted } from "./untrusted-text.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) { if (read?.unreadable === true) { if (decision.action === "deny") return { decision }; const callForm = read.reason !== undefined; 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` : "the person's standing permission rules could not be read — whether one of them forbids this call is unknown, so it needs a real approval"; const unreadableCall = callForm ? { ruleUnreadable: "persisted_call" } : {}; const standingFence = decision.action === "ask" && decisionAskClassesOf({ ...decision, foldedAskClasses: undefined }).has("policy_rra") ? { foldedAskClasses: [...(decision.foldedAskClasses ?? []), "policy_rra"] } : {}; return { decision: decision.action === "allow" ? { action: "ask", message, decisionReason: "persisted_rule", requiresRealApproval: true, ...unreadableCall, ...(decision.updatedInput !== undefined ? { updatedInput: decision.updatedInput } : {}) } : { ...decision, requiresRealApproval: true, ...unreadableCall, ...standingFence, message: `${decision.message !== undefined ? `${decision.message} ` : ""}(${message})` }, tightened: callForm ? "unreadable" : "store_unreadable", }; } 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 && call !== undefined && hit.denial.face === callPathFaceOf(call); return { decision: attested ? attest(denied, call, { pathDenial: hit.denial }) : denied, tightened: "deny" }; } if (decision.action === "deny") return { decision }; if (decision.action === "allow") { return { decision: { action: "ask", message: `a persisted ask rule (${shown}) requires approval for this call`, decisionReason: "persisted_rule", matchedAskRule: shown, ...(decision.updatedInput !== undefined ? { updatedInput: decision.updatedInput } : {}), }, tightened: "ask", }; } const asked = { ...decision }; return { decision: { ...asked, ...(asked.matchedAskRule === undefined ? { matchedAskRule: shown } : {}), message: `${asked.message !== undefined ? `${asked.message} ` : ""}(a persisted ask rule (${shown}) also requires approval for this call)`, }, tightened: "ask", }; }