@sema-agent/core
Version:
Stateless, task-oriented AI agent core
111 lines (110 loc) • 5.2 kB
JavaScript
import { AsyncLocalStorage } from "node:async_hooks";
import { types } from "node:util";
import { canonicalize } from "./canonical-json.js";
import { declarerWord } from "./mcp-server-spec.js";
import { isHostPromise } from "./host-promise.js";
import { inlineUntrusted } from "./untrusted-text.js";
export const REVOKED_PEER_CODES = Object.freeze({ mcp: "mcp.server_revoked", a2a: "a2a.peer_revoked" });
export const A2A_REVOCATION_UNREADABLE_CODE = "a2a.revocation_unreadable";
export function revocationTargetName(target) {
return target.protocol === "mcp" ? target.server : target.peer;
}
export class ProtocolRevokedRefusal extends Error {
code;
target;
afterSentHop;
why;
details;
constructor(target, afterSentHop = false, why = "revoked") {
const name = `${declarerWord(target.protocol)} "${inlineUntrusted(revocationTargetName(target), 120)}"`;
super((why === "unreadable" ? `${name} is registered inside the trust boundary and the revocation ledger could not be read for it; ` : `${name} was revoked by the operator; `) +
(afterSentHop ? "the request's redirect was NOT followed" : "the request was NOT sent"));
this.name = "ProtocolRevokedRefusal";
this.code = why === "unreadable" ? A2A_REVOCATION_UNREADABLE_CODE : REVOKED_PEER_CODES[target.protocol];
this.target = target;
this.afterSentHop = afterSentHop;
this.why = why;
this.details = Object.freeze({ code: this.code, delivered: afterSentHop ? "unknown" : "no" });
}
}
const carried = new AsyncLocalStorage();
export function mcpRevocationTarget(spec) {
return typeof spec.fingerprint === "string" ? { protocol: "mcp", server: spec.name, fingerprint: spec.fingerprint } : { protocol: "mcp", server: spec.name };
}
export function carryRevocation(question, send) {
return carried.run(question, send);
}
function heldAbout(about) {
const held = carried.getStore();
return held !== undefined && (about === undefined || canonicalize(held.about) === canonicalize(about)) ? held : undefined;
}
export function openingQuestion(about) {
return heldAbout(about);
}
export function askCarriedRevocation(opened, about) {
const question = heldAbout(about) ?? opened;
if (question === undefined)
return;
const refused = question.ask();
if (refused !== undefined)
throw refused;
}
const SEAT_FORM = "the revocation seat is an object { revoked({ protocol, server | peer, fingerprint? }) } that answers synchronously";
function refusal(subject, problem) {
return Object.assign(new Error(`${subject} ${problem} — ${SEAT_FORM}`), { code: "config.invalid_argument" });
}
export function receiveProtocolRevocations(raw, subject) {
if (raw === undefined)
return undefined;
if (typeof raw !== "object" || raw === null || types.isProxy(raw))
throw refusal(subject, raw === null ? "is null" : typeof raw !== "object" ? `is a ${typeof raw}` : "is a Proxy");
const member = readMemberOf(raw, "revoked", subject);
if (typeof member !== "function") {
const retired = member === undefined && typeof readMemberOf(raw, "isRevoked", subject) === "function";
throw refusal(subject, retired ? "is the MCP-only ledger form `{ isRevoked(serverName, mount?) }`: name the method `revoked` and read the target's `protocol`, `server` / `peer` and `fingerprint`" : "has no `revoked` function");
}
return Object.freeze({ revoked: (target) => Reflect.apply(member, raw, [target]) });
}
export function libraryRevocationProbe(raw, subject) {
const ledger = receiveProtocolRevocations(raw, subject);
if (ledger === undefined)
return undefined;
const sink = readMemberOf(raw, "onProbeFailure", subject);
if (sink !== undefined && typeof sink !== "function")
throw refusal(subject, `has an \`onProbeFailure\` that is a ${sink === null ? "null" : typeof sink}, not a function`);
return revocationProbe(ledger, sink === undefined ? undefined : (error) => void Reflect.apply(sink, raw, [error]));
}
function readMemberOf(raw, name, subject) {
try {
return Reflect.get(raw, name);
}
catch {
throw refusal(subject, `threw while its \`${name}\` was read`);
}
}
export function revocationProbe(ledger, onProbeFailure) {
if (ledger === undefined)
return undefined;
let told = false;
return (target) => {
try {
const answer = ledger.revoked(Object.freeze({ ...target }));
if (typeof answer === "boolean")
return answer ? "revoked" : "standing";
if (typeof answer?.then === "function")
Promise.resolve(answer).catch(() => undefined);
throw new Error(`revoked returned ${isHostPromise(answer) ? "a Promise — the ledger seat is SYNCHRONOUS by contract" : `a non-boolean (${answer === null ? "null" : typeof answer})`}`);
}
catch (e) {
if (!told) {
told = true;
try {
onProbeFailure?.(e);
}
catch {
}
}
return "unreadable";
}
};
}