@sema-agent/core
Version:
Stateless, task-oriented AI agent core
55 lines (54 loc) • 3.76 kB
JavaScript
import { callerKey } from "../caller-text.js";
import { CAPABILITY_KEYS, UNDECLARED_CAPABILITY_READING, capabilityReceiptOf } from "../remote-env.js";
import { deliverEngineNotice } from "../engine-notice.js";
const OPTIONAL_METHOD_KEYS = ["writeFileExclusive", "writeFileGuarded", "setFileMode", "readLink", "canonicalChain", "fileIdentity", "openBound"];
const REMOTE_OPTIONAL_METHOD_KEYS = ["syncWorkspace", "releaseToRow"];
export function malformedCapabilityDeclarations(env) {
const out = [];
for (const key of [...OPTIONAL_METHOD_KEYS, ...REMOTE_OPTIONAL_METHOD_KEYS]) {
let value;
try {
value = Reflect.get(env, key);
}
catch {
out.push({ key, valueType: "unreadable", expected: "function" });
continue;
}
if (value === undefined || typeof value === "function")
continue;
out.push({ key, valueType: value === null ? "null" : typeof value, expected: "function" });
}
for (const finding of capabilityReceiptOf(env).findings)
out.push(finding);
return out;
}
function declaredAs({ key, valueType, expected }) {
if (expected === "function")
return `declares ${key} as a ${valueType}, not a function`;
if (expected === "object")
return valueType === "unreadable" ? "declares a capabilities block that cannot be read" : `declares capabilities as a ${valueType}, not an object`;
if (expected === "absent")
return `declares the capabilities member ${callerKey(key)}, which is no capability`;
if (valueType === "undefined")
return `does not declare capabilities.${key}`;
if (valueType === "unreadable")
return `declares capabilities.${key} with a value that cannot be read`;
return `declares capabilities.${key} as a ${valueType}, not a boolean`;
}
export function announceMalformedCapabilities(env, onNotice) {
for (const finding of malformedCapabilityDeclarations(env)) {
const { key, valueType, expected } = finding;
const row = expected === "boolean" ? UNDECLARED_CAPABILITY_READING[key] : undefined;
deliverEngineNotice(onNotice, {
code: "config.execution_env_capability_invalid",
message: expected === "function"
? `The execution environment ${declaredAs(finding)}. This seat is declared by being present as a callable method and undeclared by being absent, so every reader takes the absent reading — the reach the adapter meant to declare is silently the old one. Fix the adapter — this is a wiring fault, not a supported way to leave the seat unset.`
: expected === "absent"
? `The execution environment ${declaredAs(finding)}: the block holds exactly ${CAPABILITY_KEYS.join(", ")}, so no reader takes this member, and a misspelled key leaves the key it meant undeclared. Fix the adapter — this is a wiring fault, not a supported extension of the block.`
: expected === "object"
? `The execution environment ${declaredAs(finding)}. Every capability is required and none can be read, so every reader takes its key's undeclared reading (${CAPABILITY_KEYS.map((k) => `${k} as ${JSON.stringify(UNDECLARED_CAPABILITY_READING[k].reads)}`).join(", ")}). Fix the adapter — this is a wiring fault, not a supported way to leave the capabilities unset.`
: `The execution environment ${declaredAs(finding)}. Every capability is required and has no default reading, so the engine reads it as ${JSON.stringify(row?.reads)}: ${row?.so}. Fix the adapter — this is a wiring fault, not a supported way to leave the capability unset.`,
detail: { key, valueType },
});
}
}