UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

28 lines (27 loc) • 1.34 kB
import { thrownLabel, thrownText } from "../thrown-value.js"; import { foreignLine } from "../protocol-http-auth.js"; import { redactThenCut } from "../untrusted-egress.js"; import { PROBE_REASON_MAX } from "../untrusted-text.js"; const POLICY_THROW_REPORT_MAX = 10_000; export function policyThrowLine(err) { const line = foreignLine(thrownText(err), PROBE_REASON_MAX); return line === "" ? "(no message)" : line; } export function policyCheckAbortedDeny() { return { action: "deny", message: "policy check aborted (task timed out or cancelled)" }; } export function failedJudgmentDeny(err, signal, report, seat, call, said) { if (signal?.aborted === true) return policyCheckAbortedDeny(); reportPolicyThrow(report, seat, call, err); return { action: "deny", message: said ?? `policy check threw — the tool policy chain failed while judging this call (not a timeout or cancellation), so it was denied fail-closed: ${policyThrowLine(err)}` }; } export function reportPolicyThrow(report, seat, call, err, signal) { if (signal?.aborted === true) return; try { report(new Error(`${seat} threw while judging "${call.toolName}" (toolCallId ${call.toolCallId}); the call was denied fail-closed: ${redactThenCut(thrownLabel(err), POLICY_THROW_REPORT_MAX)}`)); } catch { } }