@sema-agent/core
Version:
Stateless, task-oriented AI agent core
60 lines (59 loc) • 3.49 kB
JavaScript
import { deliverEngineNotice } from "../engine-notice.js";
import { renderListEntry } from "../allow-entry.js";
import { noticeRuleText } from "../permission-rule-model.js";
import { inlineUntrusted } from "../untrusted-text.js";
import { announceDigest, tellOncePerSession } from "./announce-once-ledger.js";
export const ALLOW_RULE_NOT_USED_IN_AUTO = "permission.allow_rule_not_used_in_auto";
function whereKept(r) {
switch (r.source) {
case "user":
return "saved in your user rules";
case "project":
return "saved in this project's rules";
case "session":
return "saved for this conversation";
case "request":
return "in this run's allowed-tools list";
case "deployment":
return "in this deployment's allowed tools";
case "skill":
return `in the allowed-tools of the skill ${inlineUntrusted(r.skill ?? "", 80)}`;
default: {
const exhaustive = r.source;
return exhaustive;
}
}
}
function whyAndWayOut(r, call) {
const narrower = "To have a command approved without the classifier, save a narrower rule that names the whole command, such as Bash(npm run build); or switch to the default permission mode, where this rule applies as written.";
switch (r.reason) {
case "whole_shell":
return `it allows every shell command, which is too broad for auto mode to honor, so in auto mode it approves no ${inlineUntrusted(call.toolName, 80)} call on its own: unless another rule approves one, it goes to the auto-mode classifier, which may block it. ${narrower}`;
case "code_runner":
return `it allows a program that runs whatever code it is given, which is too broad for auto mode to honor, so in auto mode it approves no command on its own: unless another rule approves one, it goes to the auto-mode classifier, which may block it. ${narrower}`;
case "classify_all_shell":
return "this conversation was started with every shell command sent to the auto-mode classifier in auto mode, so no shell rule approves a command on its own here, and a narrower rule would not either. To have it apply, switch to the default permission mode, where this rule applies as written.";
default: {
const exhaustive = r.reason;
return exhaustive;
}
}
}
export function allowRuleNotUsedInAutoNotice(r, call, address, runId) {
return {
code: ALLOW_RULE_NOT_USED_IN_AUTO,
message: `The allow rule ${renderListEntry(r.entry)} (${whereKept(r)}) is not used in auto mode: ${whyAndWayOut(r, call)} Said once per conversation for this rule.`,
detail: { entry: noticeRuleText(r.entry), source: r.source, reason: r.reason, toolName: call.toolName, toolCallId: call.toolCallId, ...(r.skill !== undefined ? { skill: r.skill } : {}), ...address, runId },
};
}
export function createAutoStripNotice(input) {
const { onNotice, legNotice, runId, announced } = input;
const toldThisLeg = new Set();
return (r, call) => {
const digest = announceDigest(ALLOW_RULE_NOT_USED_IN_AUTO, `${r.source}\n${r.skill ?? ""}\n${r.entry}`);
if (toldThisLeg.has(digest))
return;
toldThisLeg.add(digest);
tellOncePerSession(announced, digest, () => deliverEngineNotice(onNotice, allowRuleNotUsedInAutoNotice(r, call, legNotice.route.address, runId), legNotice.admission));
};
}