@sema-agent/core
Version:
Stateless, task-oriented AI agent core
116 lines (115 loc) • 8.33 kB
JavaScript
import { PERMISSION_RULES_DAMAGED_BUCKET_CODE, PERMISSION_RULES_LATER_GRAMMAR_CODE, PERMISSION_RULES_UNREADABLE_BUCKET_CODE } from "./permission-rule-store.js";
export const RULE_STORE_FAULT_REASONS = Object.freeze(["not_readable", "read_failed", "unsafe_permissions", "owner_mismatch", "damaged", "later_grammar", "no_answer", "unclassified"]);
function ownMember(err, key) {
try {
if (typeof err !== "object" || err === null)
return undefined;
const d = Object.getOwnPropertyDescriptor(err, key);
return d !== undefined && "value" in d ? d.value : undefined;
}
catch {
return undefined;
}
}
export function ruleStoreFaultReasonOf(err) {
const code = ownMember(err, "code");
if (code === PERMISSION_RULES_LATER_GRAMMAR_CODE)
return "later_grammar";
if (code === PERMISSION_RULES_UNREADABLE_BUCKET_CODE) {
const errno = ownMember(err, "errno");
return errno === "EACCES" || errno === "EPERM" ? "not_readable" : "read_failed";
}
if (code === PERMISSION_RULES_DAMAGED_BUCKET_CODE) {
const unsafe = ownMember(err, "unsafe");
return unsafe === "permissions" ? "unsafe_permissions" : unsafe === "owner" ? "owner_mismatch" : "damaged";
}
return "unclassified";
}
const CLAUSE = Object.freeze({
not_readable: "the account that runs this agent is not allowed to read the folder or file that holds them",
read_failed: "the system reported an error while reading the file that holds them",
unsafe_permissions: "other accounts can write to the folder that holds them, so what is in it cannot be trusted",
owner_mismatch: "the folder that holds them belongs to another account",
damaged: "the stored rules, or what holds them, are damaged or not in the form this engine expects",
later_grammar: "a newer version of this engine wrote them",
no_answer: "the rules store did not answer in time",
unclassified: "the rules store reported an error",
});
const LOG_POINTER = Object.freeze({ details: "The operator's error log has the details.", folder: "The operator's error log says which folder." });
const WAY_OUT = Object.freeze({
not_readable: { act: "Give the account that runs this agent read access to that folder again.", log: "folder" },
read_failed: { act: "Check the disk that holds them.", log: "details" },
unsafe_permissions: { act: "Take write access to that folder away from other accounts (group and others).", log: "folder" },
owner_mismatch: { act: "Make the account that runs this agent the owner of that folder again, or restore a copy of it that account owns.", log: "folder" },
damaged: { act: "Restore a good copy of them, or clear them (clearing them gives up every saved rule).", log: "details" },
later_grammar: { act: "Run the newer version of this engine that wrote them, or restore an older copy." },
no_answer: { act: "Try again; if it keeps happening, check that the rules store is reachable." },
unclassified: { log: "details" },
});
export function ruleStoreSourcesWord(sources) {
return sources.length <= 1 ? sources.join("") : `${sources.slice(0, -1).join(", ")} and ${sources[sources.length - 1]}`;
}
function faultsClause(faults) {
if (faults.length === 1) {
const only = faults[0];
return { head: only.sources.length > 0 ? ` (${ruleStoreSourcesWord(only.sources)})` : "", clause: CLAUSE[only.reason] };
}
return { head: "", clause: faults.map((f) => `${f.sources.length > 0 ? `${ruleStoreSourcesWord(f.sources)}: ` : ""}${CLAUSE[f.reason]}`).join("; ") };
}
function waysOut(faults) {
const outs = faults.map((f) => WAY_OUT[f.reason]);
const acts = outs.flatMap((o) => (o.act !== undefined ? [o.act] : []));
const logs = outs.flatMap((o) => (o.log !== undefined ? [LOG_POINTER[o.log]] : []));
return [...new Set([...acts, ...logs])].join(" ");
}
const UNSAID = Object.freeze([Object.freeze({ sources: Object.freeze([]), reason: "unclassified" })]);
export function ruleStoreUnreadableNoticeSentence(at, faults, relaxedMode) {
const { head, clause } = faultsClause(faults !== undefined && faults.length > 0 ? faults : UNSAID);
const fixes = waysOut(faults !== undefined && faults.length > 0 ? faults : UNSAID);
const modeNote = relaxedMode === "auto" ? " (the classifier judges such a call)" : "";
const cost = relaxedMode !== undefined
? `Under this run's permission mode (${relaxedMode}), the unread rules by themselves do not make the engine ask${modeNote}: until a read of them succeeds, only the deny and ask rows most recently read from them this run apply (none, if no read has succeeded) — a deny rule you saved since may not be in force.`
: "Until a read of them succeeds, none of their allow rows (Read directory rows included) applies, while the deny and ask rows most recently read from them this run still apply — a deny row you removed since that read keeps refusing until then. While they cannot be read, every call is asked about rather than allowed, whatever its tool (rules nobody can read may name any), until a read of them succeeds.";
return `Your saved permission rules${head} could not be read ${at === "prepare" ? "when this run started" : "for a call on this run"}: ${clause}. ${cost} ${fixes} Every later check reads them again.`;
}
export function ruleStoreUnreadableRelaxedSentence(mode, at, faults, rows) {
const { head, clause } = faultsClause(faults !== undefined && faults.length > 0 ? faults : UNSAID);
const fixes = waysOut(faults !== undefined && faults.length > 0 ? faults : UNSAID);
if (rows !== undefined) {
return `${rows} deny or ask row${rows === 1 ? "" : "s"} of the person's saved permission rules${head} could not be read ${at === "prepare" ? "when a run started" : "for a call of a run"} (the rule itself — its tool, its text or its behavior — is not plain data): ${clause}. Under the run's permission mode (${mode}) the calls ${rows === 1 ? "it" : "they"} may reach are released with nobody asked, unless a readable deny refuses them or a readable ask row asks; every other row applies as read. ${fixes}`;
}
const modeNote = mode === "auto" ? " (the classifier judges such a call)" : "";
return `The person's saved permission rules${head} could not be read ${at === "prepare" ? "when a run started" : "for a call of a run"}: ${clause}. Under the run's permission mode (${mode}) the unread rules by themselves do not make the engine ask${modeNote} (the person is told by their own notice): until a read of them succeeds, only the deny and ask rows of the last read that succeeded apply (none, if no read has) — a deny rule saved since may not be in force. ${fixes}`;
}
export function ruleStoreUnreadableAskSentence(faults) {
const { head, clause } = faultsClause(faults !== undefined && faults.length > 0 ? faults : UNSAID);
return `your saved permission rules${head} could not be read (${clause}), so whether one of them forbids this call is unknown and it needs a person's approval — if nobody can be asked, tell the person what is wrong with the rules store and carry on with the parts of the task that do not need this call`;
}
export function ruleStoreFaultsKey(faults) {
return faults.map((f) => `${f.sources.join("+")}:${f.reason}`).sort().join("|");
}
export function isRuleStoreFaultReason(value) {
return RULE_STORE_FAULT_REASONS.some((reason) => reason === value);
}
const SOURCE_ORDER = ["user", "project", "session"];
export function readRuleStoreFaults(value) {
try {
if (!Array.isArray(value) || value.length === 0 || value.length > SOURCE_ORDER.length)
return undefined;
const out = [];
for (const raw of value) {
const sources = ownMember(raw, "sources");
const reason = ownMember(raw, "reason");
if (!Array.isArray(sources) || !isRuleStoreFaultReason(reason))
return undefined;
const read = sources.filter((s) => SOURCE_ORDER.some((known) => known === s));
if (read.length !== sources.length)
return undefined;
out.push({ sources: SOURCE_ORDER.filter((s) => read.includes(s)), reason });
}
return out;
}
catch {
return undefined;
}
}