@sema-agent/core
Version:
Stateless, task-oriented AI agent core
46 lines (45 loc) • 4.03 kB
JavaScript
import { delimitUntrusted } from "../../core/untrusted-text.js";
import { withOutcomeNote } from "../../core/exec-outcome.js";
import { BACKGROUND_SHELL_ERROR_CODES } from "../../core/background-shell.js";
import { envErrorDetail } from "./env-error-detail.js";
import { bashFailureCard, bashMaxOutputChars, clipShellOutput, shellRecoveryHint, writeShellOverflowFile } from "./fs-shared.js";
export async function capturedBeforeFailure(env, error, streamed) {
const attached = typeof error.partialStdout === "string" || typeof error.partialStderr === "string";
const rawStdout = attached ? (typeof error.partialStdout === "string" ? error.partialStdout : "") : (streamed?.stdout ?? "");
const rawStderr = attached ? (typeof error.partialStderr === "string" ? error.partialStderr : "") : (streamed?.stderr ?? "");
const overflowFile = rawStdout.length > bashMaxOutputChars() || rawStderr.length > bashMaxOutputChars() ? await writeShellOverflowFile(env, rawStdout, rawStderr) : undefined;
const stdout = clipShellOutput(rawStdout);
const stderr = clipShellOutput(rawStderr);
const captured = (stdout ? `--- partial stdout ---\n${stdout}` : "") + (stderr ? `${stdout ? "\n" : ""}--- partial stderr ---\n${stderr}` : "");
return { stdout, stderr, captured, overflowFile };
}
export function backgroundFailureWord(code, onOutside) {
if (typeof code === "string" && BACKGROUND_SHELL_ERROR_CODES.some((w) => w === code))
return code;
onOutside();
return "unknown";
}
export function stateNotStartedResult(word, toolName, message, card) {
return word === "suspended"
? { content: `Error (${toolName}): the execution environment is suspended (its workspace VM is paused), so the command did NOT run and no command can run in this leg. Do not retry it here — the task must be resumed first; report that the workspace is suspended. (${envErrorDetail(message)})`, details: { ...card, envSuspended: true }, isError: true }
: { content: `Error (${toolName}): authentication to the execution environment was rejected, so the command did NOT run. Do NOT retry — this is a permanent credential failure that an operator must fix; report it instead of trying other commands. (${envErrorDetail(message)})`, details: { ...card, authFailed: true }, isError: true };
}
export function stateWhileRunning(word, message) {
return word === "suspended"
? [`the execution environment became suspended (its workspace VM is paused) while the command was running, so no command can run in this leg until the task is resumed — do not retry it here; report that the workspace is suspended (${envErrorDetail(message)}).`, { envSuspended: true }]
: [`authentication to the execution environment was rejected while the command was running — a permanent credential failure an operator must fix; do not retry, report it instead of trying other commands (${envErrorDetail(message)}).`, { authFailed: true }];
}
const ENV_CODE_SHOWN_MAX = 128;
export function envRefusedLead(failure, code, note) {
note?.(failure);
return [`the execution environment refused itself (${code.slice(0, ENV_CODE_SHOWN_MAX)}): ${envErrorDetail(failure.message)}.`, { envRefused: true }];
}
export async function outcomeUnknownResult(env, failure, word, toolName, lead, flags, readOnly, streamed) {
const cut = await capturedBeforeFailure(env, failure, streamed);
const body = cut.captured.length > 0 ? `\nOutput the command wrote before the failure:\n${delimitUntrusted("partial command output", cut.captured)}` : "";
return {
content: `Error (${toolName.slice(0, 64)}): ${withOutcomeNote(lead, { outcomeUnknown: true })}${body}${cut.overflowFile !== undefined ? shellRecoveryHint(cut.overflowFile, readOnly) : ""}`,
details: { ...bashFailureCard(word), ...flags, outcomeUnknown: true, stdout: cut.stdout, stderr: cut.stderr, exitCode: null, ...(cut.overflowFile !== undefined ? { output_file: cut.overflowFile } : {}) },
isError: true,
};
}