UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

50 lines (49 loc) • 2.96 kB
import { deliverEngineNotice } from "../engine-notice.js"; import { entriesNotAppliedDetail } from "../entries-not-applied.js"; import { renderListEntry } from "../allow-entry.js"; import { noticeRuleText } from "../permission-rule-model.js"; import { toolPolicySkippedRules } from "../permission-rules.js"; import { announceDigest } from "./announce-once-ledger.js"; export const SKIPPED_RULES_SHOWN = 8; const WRITTEN_IN = { user: "your user settings", project: "the project's settings" }; const NOT_IN_FORCE = { deny: "calls it was written to refuse are not refused by it", ask: "calls it was written to ask about are not asked by it", }; const clauseOf = (r) => `the ${r.behavior} rule ${renderListEntry(r.rule)} (${WRITTEN_IN[r.layer]}) is NOT in force — ${NOT_IN_FORCE[r.behavior]}`; const keyOf = (r) => JSON.stringify([r.layer, r.behavior, r.code, r.rule]); function uniqueSkipped(rules) { const seen = new Set(); return rules.filter((r) => !seen.has(keyOf(r)) && seen.add(keyOf(r)) !== undefined); } export function skippedRulesNotice(rules, ids) { const unique = uniqueSkipped(rules); if (unique.length === 0) return undefined; const one = unique.length === 1; const shown = unique.slice(0, SKIPPED_RULES_SHOWN); const more = unique.length > shown.length ? `; and ${unique.length - shown.length} more` : ""; const { address } = ids; const delegated = address.sourceSessionId !== undefined; const whose = delegated ? `the run of a delegated agent${address.agentId !== undefined ? ` (${address.agentId})` : ""}` : "this run"; return { code: "config.entries_not_applied", message: `${unique.length} permission rule${one ? "" : "s"} could not be read and ${one ? "was" : "were"} skipped — ${whose} goes on without ${one ? "it" : "them"}: ` + `${shown.map(clauseOf).join("; ")}${more}. Fix or remove ${one ? "it" : "them"}.`, detail: { ...entriesNotAppliedDetail("rule_text_unreadable", unique.map((r) => ({ entry: noticeRuleText(r.rule), reason: r.code, behavior: r.behavior, layer: r.layer }))), ...address, runId: ids.runId, }, }; } export function announceSkippedPermissionRules(input) { const toldUpstream = new Set(toolPolicySkippedRules(...(input.chain ?? []).map((c) => c.policy)).map(keyOf)); const rules = uniqueSkipped(toolPolicySkippedRules(...input.own)).filter((r) => !toldUpstream.has(keyOf(r))); const notice = skippedRulesNotice(rules, { address: input.legNotice.route.address, runId: input.runId }); if (notice === undefined) return; const { sourceSessionId } = input.legNotice.route.address; const key = rules.map(keyOf).sort().join("\n") + (sourceSessionId !== undefined ? `\n\n${sourceSessionId}` : ""); input.onceLedger.oncePerSession(announceDigest(notice.code, key), () => deliverEngineNotice(input.onNotice, notice, input.legNotice.admission)); }