@sema-agent/core
Version:
Stateless, task-oriented AI agent core
83 lines (82 loc) • 4.01 kB
JavaScript
import { deliverEngineNotice } from "./engine-notice.js";
import { foreignLine } from "./protocol-http-auth.js";
import { isDeploymentEnvCode } from "./remote-env.js";
export const EXEC_FAILURE_WORDS = Object.freeze({
shell_unavailable: "never_started",
spawn_error: "never_started",
target_unavailable: "never_started",
refused: "never_started",
sandbox_unavailable: "never_started",
suspended: "state",
auth_failed: "state",
timeout: "cut",
liveness_no_output: "cut",
aborted: "cut",
callback_error: "cut",
transport_lost: "unknown",
outcome_unknown: "unknown",
unknown: "unknown",
});
export function isExecFailureWord(word) {
return typeof word === "string" && Object.hasOwn(EXEC_FAILURE_WORDS, word);
}
export function capturedOutput(error) {
return (typeof error.partialStdout === "string" && error.partialStdout.length > 0) || (typeof error.partialStderr === "string" && error.partialStderr.length > 0);
}
export function readExecFailure(error, observed) {
const code = error.code;
const wroteOutput = capturedOutput(error) || observed?.wroteOutput === true;
if (isDeploymentEnvCode(code))
return { outcome: "unknown", word: code, kind: "unknown", wroteOutput };
if (!isExecFailureWord(code))
return { outcome: "unknown", word: "unknown", kind: "unknown", wroteOutput, breach: "word_outside_set" };
const kind = EXEC_FAILURE_WORDS[code];
if (kind === "never_started")
return wroteOutput ? { outcome: "unknown", word: "outcome_unknown", kind, wroteOutput, breach: "never_started_after_output" } : { outcome: "not_started", word: code, kind, wroteOutput };
if (kind === "state")
return { outcome: wroteOutput ? "unknown" : "not_started", word: code, kind, wroteOutput };
return { outcome: kind, word: code, kind, wroteOutput };
}
export const EXEC_OUTCOME_UNKNOWN_SENTENCE = "Its outcome is unknown — whether it ran, in part or in full, could not be determined; verify its effects before retrying, and retry only if running it twice is safe.";
export function outcomeFactOf(error) {
return readExecFailure(error).outcome === "unknown" ? { outcomeUnknown: true } : {};
}
export function outcomeFactOfThrown(thrown) {
try {
return typeof thrown === "object" && thrown !== null && thrown.outcomeUnknown === true ? { outcomeUnknown: true } : {};
}
catch {
return {};
}
}
export function withOutcomeNote(text, ...facts) {
if (!facts.some((f) => f?.outcomeUnknown === true))
return text;
const lead = (typeof text === "string" ? text : "").replace(/[\s.]+$/u, "");
return lead === "" ? EXEC_OUTCOME_UNKNOWN_SENTENCE : `${lead}. ${EXEC_OUTCOME_UNKNOWN_SENTENCE}`;
}
const BREACH_WORD_MAX = 120;
const BREACH_SENTENCE = {
word_outside_set: "The execution environment answered a failure with a word outside its closed set, so the failure is read as unknown.",
never_started_after_output: "The execution environment said a command never started, but the command had already written output, so its outcome is read as unknown. An environment may say a command never started only before the command writes anything.",
};
export function execFailureBreachLedger() {
const told = new WeakSet();
return {
announce(onNotice, env, fact) {
if (told.has(env))
return;
told.add(env);
deliverEngineNotice(onNotice, {
code: "config.execution_env_failure_contract_broken",
message: `${BREACH_SENTENCE[fact.breach]} Later breaches by the same environment are not announced again.`,
detail: {
breach: fact.breach,
code: typeof fact.code === "string" ? foreignLine(fact.code, BREACH_WORD_MAX) : "",
face: fact.face,
...(fact.sessionId !== undefined ? { sessionId: fact.sessionId } : {}),
},
});
},
};
}