@sema-agent/core
Version:
Stateless, task-oriented AI agent core
104 lines (103 loc) • 7.77 kB
TypeScript
/**
* Whether a failed command ran — ONE reading of an execution failure, for every reader that runs a command through an execution
* environment and records or shows its failure: the Bash tool (both faces), the exit oracle's two legs (`runExecGate`), the
* managed-worktree git steps and a mod's `process.run`. A reader asks three questions of a failure and all three are answered here:
* did the command start, which word records it, and did the environment break its failure contract ({@link readExecFailure} over
* {@link EXEC_FAILURE_WORDS}); and, when the outcome is not known, what is said about it — {@link EXEC_OUTCOME_UNKNOWN_SENTENCE}, placed
* once by the reader that hands the text to a person or a model ({@link withOutcomeNote}). A step a reader composes from (a git
* step, a cleanup step) reports only the fact ({@link OutcomeFact}), never the sentence: two steps that each carried it made the
* person read it twice, and a reader that relied on a step to carry it said nothing when the step did not.
*/
import type { ExecutionErrorCode } from "../internal/harness-types.js";
import { type EngineNotice } from "./engine-notice.js";
/**
* What a failure word says about its command, read off the word's own contract:
* - `never_started` — the command never started, and the word can only arise before it starts (no shell, a failed spawn, a target
* that is not there or refused it, a confinement that could not be had): a command that wrote output started, so the word
* cannot arrive with output;
* - `state` — the command never started when the word arrives before any output, but the word names a standing state of the
* environment (a paused workspace, a rejected credential) that can also arrive while a started command runs: with output, the
* outcome is not known, and that is no breach;
* - `cut` — the command ran and was cut; what it wrote up to the cut is real;
* - `unknown` — whether it ran is not known.
*/
export type ExecFailureKind = "never_started" | "state" | "cut" | "unknown";
/**
* THE table: every buffered failure word, one row ({@link ExecutionErrorCode}). Both faces read it — a buffered `exec` failure and a
* stream (`execStream`) that throws — so the words the stream may throw only before its first chunk are this table's
* `never_started` rows that are also stream words, not a list of their own. A word an environment spells outside the table is
* read as `unknown` and is a breach ({@link readExecFailure}) — but a deployment's own well-spelled `env.` code, kept, outcome unknown.
*/
export declare const EXEC_FAILURE_WORDS: Readonly<Record<ExecutionErrorCode, ExecFailureKind>>;
/** Is `word` a row of {@link EXEC_FAILURE_WORDS}? */
export declare function isExecFailureWord(word: unknown): word is ExecutionErrorCode;
/** What a failed start says about its command: it never started, it ran and was cut (its output up to the cut is real), or not known. */
export type ExecOutcome = "not_started" | "cut" | "unknown";
/** How an environment broke its failure contract: a word outside the table, or a `never_started` word for a command that wrote output. */
export type ExecFailureBreach = "word_outside_set" | "never_started_after_output";
/** The failure members this reading reads — an `ExecutionError`, or anything an environment handed back in its place. */
export interface ExecFailureLike {
readonly code?: unknown;
readonly partialStdout?: unknown;
readonly partialStderr?: unknown;
}
/**
* One failure, read. `word` is the ONE word every reader records it by — the Bash card's `code` (and so `tool_end.errorCode`), the
* exit oracle's row: the environment's own word when the table holds it and it does not contradict itself, or a deployment's own
* `env.` code (outcome unknown, no breach); `"outcome_unknown"` for a
* `never_started` word that arrived with output; `"unknown"` for a word outside the table. `breach`, when present, says the
* environment broke its contract — the reader announces it ({@link ExecFailureBreachLedger}), and the environment's own word rides
* only that announcement.
*/
export interface ExecFailureReading {
readonly outcome: ExecOutcome;
readonly word: ExecutionErrorCode | `env.${string}`;
readonly kind: ExecFailureKind;
readonly wroteOutput: boolean;
readonly breach?: ExecFailureBreach;
}
/** Did the command write anything the environment handed back? An empty or absent capture is not output. */
export declare function capturedOutput(error: ExecFailureLike): boolean;
/** THE reading of a failed command. Never throws. `observed.wroteOutput`: the reader itself received output from the command while it
* ran (the environment's streaming callbacks, a stream's chunk) — the same evidence as a capture the environment attached. */
export declare function readExecFailure(error: ExecFailureLike, observed?: {
readonly wroteOutput: boolean;
}): ExecFailureReading;
/**
* THE sentence a reader shows when a failure leaves the outcome unknown: once, after its own account of what failed. Worded for any
* operation a reader ran (a command, a git step), so every reader can place the one sentence.
*/
export declare const EXEC_OUTCOME_UNKNOWN_SENTENCE = "Its outcome is unknown \u2014 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.";
/** What a step reports of an outcome it could not establish: the fact, never the sentence — the reader that composes the text the
* person or the model reads places the sentence once over every fact it reports ({@link withOutcomeNote}). */
export interface OutcomeFact {
readonly outcomeUnknown?: true;
}
/** The fact of one execution failure: `{ outcomeUnknown: true }` when the reading leaves the outcome unknown, else `{}`. */
export declare function outcomeFactOf(error: ExecFailureLike): OutcomeFact;
/** The fact a thrown value carries (a step that throws its failure, the managed-tree mint): its own `outcomeUnknown` member. */
export declare function outcomeFactOfThrown(thrown: unknown): OutcomeFact;
/**
* THE composition, for the reader that hands the text to a person or a model: `text` (the reader's whole account, the steps' words
* inside it), then {@link EXEC_OUTCOME_UNKNOWN_SENTENCE} once when any of `facts` left the outcome unknown, and `text` unchanged
* otherwise. The only place the sentence is joined to a reader's words.
*/
export declare function withOutcomeNote(text: string, ...facts: readonly (object | undefined)[]): string;
/** The reader faces a breach is found on: a buffered `exec`, a stream (`execStream`), a background start (`spawnBackground`). */
export type ExecFailureFace = "exec" | "execStream" | "spawnBackground";
/**
* Where a reader announces an environment that broke its failure contract (`config.execution_env_failure_contract_broken`): at most
* ONCE per environment for the reader's lifetime — one leg's Bash tool, one exit-oracle call — so a broken adapter that breaks it on
* every command costs one line, and later breaches it meets are not announced again. The announcement carries the environment's
* own word (`detail.code`, one bounded line); the reader's result carries the table's word ({@link ExecFailureReading.word}).
*/
export interface ExecFailureBreachLedger {
announce(onNotice: ((notice: EngineNotice) => void) | undefined, env: object, fact: {
readonly breach: ExecFailureBreach;
readonly code: unknown;
readonly face: ExecFailureFace;
readonly sessionId?: string;
}): void;
}
/** A fresh ledger, for one reader's lifetime. */
export declare function execFailureBreachLedger(): ExecFailureBreachLedger;