UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

208 lines (207 loc) • 19.1 kB
/** * The start of shell text a model wrote ({@link ExecutionEnv.modelShell}): whether an environment declares one, a leg's one reading of it, * the question an environment asks a candidate shell and the reader of its answer, the table of the environment variables that * change how the shell reads a command (so configuration never supplies them), and the two notices that say so. */ import type { ExecutionEnv, ExecutionEnvModelShell } from "../internal/harness-types.js"; import { type BackgroundShellCapability } from "./background-shell.js"; import { type EngineNotice } from "./engine-notice.js"; import type { EngineText } from "../prompt-assembly/engine-text.js"; /** Whether `env` declares a start for model text: a `modelShell` member whose `exec` is a function. A member that cannot be read is none. */ export declare function hasModelShell(env: ExecutionEnv): env is ExecutionEnv & { readonly modelShell: ExecutionEnvModelShell; }; /** Whether the start has a background face: a `spawnBackground` function beside its `exec`. A member that throws when read is none. */ export declare function startsInBackground(start: ExecutionEnvModelShell | undefined): start is ExecutionEnvModelShell & Required<Pick<ExecutionEnvModelShell, "spawnBackground">>; /** What a static reader states of the start without asking it (`describeStaticWiring`), from the declaration the leg reads * ({@link declaredStart}): `mounted` is `false` undeclared or unusable by its shape, `true` declared with no `ready` (nothing to ask), * `undefined` — not stated — when its `ready` is still to be asked (the leg's reading decides); and a background face beside the * environment's background lifecycle. A leg states the same two facts from its reading. */ export declare function declaredModelShell(env: ExecutionEnv): { readonly mounted: boolean | undefined; readonly backgroundLaunch: boolean; }; /** The one refusal of a shell tool built without a `mounted` reading (`createBashTool`, `createBashReadonlyTool`, `createMonitorTool` * — `factory` names it): thrown where the tool is made, so a caller that bypassed the type, and an engine whose mount plan holds a * shell row without the reading (its own invariant broken), are loud. The tool never reads the member itself. */ export declare function mountedModelShellOf(reading: unknown, factory: string): MountedModelShell; /** The environment's background lifecycle, as the reading found it (`background.launch`): the environment a background start, a watch * and their registry rows are made on — `undefined` where the reading found none. Reads nothing. */ export declare function backgroundLifecycleOf(env: ExecutionEnv, reading: MountedModelShell): (ExecutionEnv & BackgroundShellCapability) | undefined; /** What a tool mounted on an environment without the member answers, before anything starts. */ export declare const MODEL_SHELL_UNDECLARED_REFUSAL = "this environment does not start commands on bash; no command can run here"; /** The starts a reading took: the `exec` and `spawnBackground` read once off the member, each called with that member as its * receiver; `spawnBackground` is absent when the member had none. */ export interface ModelShellStart { readonly exec: ExecutionEnvModelShell["exec"]; readonly spawnBackground?: NonNullable<ExecutionEnvModelShell["spawnBackground"]>; } /** The shell kinds a start's `ready` reports (the closed set of `ExecutionEnvModelShell.ready`'s value). */ type ReadyShell = Extract<Awaited<ReturnType<NonNullable<ExecutionEnvModelShell["ready"]>>>, { ok: true; }>["value"]["shell"]; /** Bash: what a member without `ready` is taken to be, the kind whose stated version must read and be 3.2 or later to mount (one not * stated, `""`, mounts; a `ready` that reports zsh mounts too, {@link kindReading}), and what `NodeExecutionEnv`'s `ready` reports for a bash it selected (one source for the reader * and the selector). */ export declare const STARTED_SHELL = "bash"; /** The variables that name a startup file the shell runs before the command (`BASH_ENV` for a non-interactive bash, `ENV` for sh): * rows of {@link SHELL_STATE_ENV}, and what a session call grant is never reused over ({@link ExecutionEnv.configuredStartupFileNames}). */ export declare const STARTUP_FILE_NAMES: readonly string[]; /** A `mounted` reading: the one fact of the leg every start path takes (frozen; nothing reads the member again). `shell` and `version` * as `ready` reported them (`""` is no stated version — what a `ready` that did not state one reports, and what a member without * `ready` is taken to be, a bash); `start` the starts read with them; * `background.launch` — the start has a background face and the environment its background lifecycle (`supported`), so a background * command or a watch may start; `background.detach` — that, and the environment adopts a running foreground command * (`supportsDetach`). A host with no leg reads one itself and hands it to every shell tool it mounts. */ export interface MountedModelShell { readonly kind: "mounted"; readonly shell: ReadyShell; readonly version: string; readonly start: ModelShellStart; readonly background: { readonly launch: boolean; readonly detach: boolean; }; } /** What the engine's shell tools declare for a reading's shell (`ToolSpec.startedShell`): nothing for bash, the shell for any other — * the roster row and the face stamped on each call carry it to the gate's readers (`startedShellOfCall`). */ export declare function startedShellDeclaration(shell: ReadyShell): { readonly startedShell?: "zsh"; }; /** A leg's reading of {@link ExecutionEnv.modelShell}: not declared, declared and ready ({@link MountedModelShell}), or declared and * unable to start (`cause` says why). `refusal`: `ready` answered a failure (any record, not only a local `Error`) whose code is a * deployment's own `env.` code — its code and its own message, screened as `cause` is — the * environment refusing itself, which a leg reads as its terminal (it fails in prepare with that code), not as a shell it lacks. */ export type ModelShellReading = { readonly kind: "undeclared"; } | MountedModelShell | { readonly kind: "unavailable"; readonly cause: string; readonly refusal?: { readonly code: `env.${string}`; readonly message: string; }; }; /** How long a leg's reading waits for `ExecutionEnv.modelShell.ready` to settle before it reads the start as unavailable. */ export declare const MODEL_SHELL_READY_TIMEOUT_MS = 5000; /** The one reading of an environment's start for model text ({@link MountedModelShell}: what every start path takes): its declaration * ({@link declaredStart}), then its `ready`, called with the group as its receiver; an absent `ready` is `mounted`, one that reports a * bash of 3.2 or later, or a zsh, or either with no version stated (`""`), is `mounted`. A member that throws when read, a group with no `exec` function, a `ready` that is no * function, reports a bash below 3.2 or a version that does not read, or no shell kind, answers a failure, rejects, throws, or does not * settle within {@link MODEL_SHELL_READY_TIMEOUT_MS} — each is `unavailable` with its cause; the * reading itself never throws. The words of a thrown value or a failure reach the cause through the engine's credential pass * (`egressThrownText`). A `ready` that blocks the thread synchronously is outside the bound: no timer can run until it returns. One parameter, so it is usable as an * array method's callback (`envs.map(readModelShell)`); a shorter bound is {@link readModelShellWithin}'s. */ export declare function readModelShell(env: ExecutionEnv): Promise<ModelShellReading>; /** {@link readModelShell} under a bound the caller gives (not exported from the package root: a second parameter of the public reading would * read an array method's index as the bound). The bound may only shorten the wait — a test's; a larger or unreadable one is the default. */ export declare function readModelShellWithin(env: ExecutionEnv, timeoutMs: number): Promise<ModelShellReading>; /** The question a candidate is asked, as `[path, ...SHELL_KIND_QUESTION_OPTIONS, SHELL_KIND_QUESTION]`, which a bash and a zsh both read: * a bash with brace expansion on and not in POSIX mode answers `bash M.N`; a zsh in its own default state (its options, no function, * no global or suffix alias, only its own two aliases) answers `zsh M.N[.P]`; any other shell, or either in another state, answers * nothing that reads as an answer. */ export declare const SHELL_KIND_QUESTION = "if [ -n \"${ZSH_VERSION-}\" ]; then [ \"$(emulate)\" = zsh ] && [[ \"$(builtin set -o)\" == \"$(builtin emulate zsh; builtin set -o)\" ]] && (( ${#functions} + ${#galiases} + ${#saliases} == 0 )) && [[ \"${(ok)aliases}\" == \"run-help which-command\" ]] && builtin print -rn -- \"zsh $ZSH_VERSION\"; else builtin shopt -qo braceexpand && ! builtin shopt -qo posix && builtin printf \"bash %s.%s\" \"${BASH_VERSINFO[0]}\" \"${BASH_VERSINFO[1]}\"; fi"; /** The options the question is asked with: no filename expansion, privileged mode (no startup file, no imported function), `-c`. */ export declare const SHELL_KIND_QUESTION_OPTIONS: readonly string[]; /** What a candidate's answer says: the shell it is and its version, or why it is neither (a reason that follows the candidate's path). * A bash below 3.2 is a reason, not an answer. */ export type ShellKindAnswer = { readonly ok: true; readonly shell: "bash" | "zsh"; readonly version: string; } | { readonly ok: false; readonly reason: string; }; /** Reads a candidate's stdout: exactly `bash M.N` (compared as integers against 3.2) or `zsh M.N[.P]`; anything else gave no answer. */ export declare function readShellKindAnswer(stdout: string): ShellKindAnswer; /** The statements a zsh start runs before the text it is handed, every word quoted (so no alias rewrites them): the options back to * zsh's own, every alias (global and suffix too), function (trap and hook functions too), trap, command hash and named directory a * global startup file left gone. What they do not clear is a known limit (a parameter, a module, a disabled builtin, `umask` and * resource limits it set; a function named `builtin`, which these words then call). */ export declare const ZSH_START_PREFIX: readonly string[]; /** The arguments a zsh start runs `program` with, the text it is handed appended after them: `-f` (no startup file of its own), `-c` * the prefix and then `eval` of the text — handed as the first positional parameter (so no quoting option a global startup file set * changes how it is passed), the parameters cleared before the text runs — and the program's own path as `$0`. */ export declare function zshStartArgs(program: string): string[]; /** The arguments a start on a shell of kind `shell` (as {@link ShellKindAnswer} and `ExecutionEnvModelShell.ready` name it) runs `program` * — the shell's own absolute path — with, the text it is handed appended after them: `["-c"]` for a bash, {@link zshStartArgs} for a zsh. * The one source the engine's own starts read and a host's lane reads to start the same shell the same way; a new array each call. A kind * that is neither, or a `program` that is not a string (read for a bash too, though only a zsh puts it in the arguments), is refused. */ export declare function shellStartArgs(shell: ReadyShell, program: string): string[]; /** What a start does with a value of a row that the host process's own environment holds (the configuration's — `shellEnv`, a * call's `env` — is never passed): `"dropped"` never passed, `"passes"` passed as it is, `"absolute-path"` passed only when the value * is an absolute path (a relative one would name a file in the directory each command starts in). */ export type ShellStateInherited = "dropped" | "passes" | "absolute-path"; /** One row of {@link SHELL_STATE_ENV}: a variable name (`match: "name"`), a name prefix (`"name-prefix"`), or a value prefix * (`"value-prefix"`: a variable of any name whose value starts with `text`). Names compare as the platform compares them. */ export interface ShellStateEnvRow { readonly match: "name" | "name-prefix" | "value-prefix"; readonly text: string; readonly inherited: ShellStateInherited; } /** The environment variables that change how the shell reads a command or run code before it: the one table every start (and a host * that composes the environment of one) reads, through {@link shellStateEnvRowOf}. A value starting `() {` is a function body bash 3.2 * imports under any name; `POSIXLY_CORRECT` and `POSIX_PEDANTIC` put bash in POSIX mode (measured on bash 3.2 and 5.3), which the * question excludes. */ export declare const SHELL_STATE_ENV: readonly ShellStateEnvRow[]; /** The row of {@link SHELL_STATE_ENV} that holds the variable `name` (with `value`), names compared as `platform` compares them, or * `undefined`. A name row is read before the value row. */ export declare function shellStateEnvRowOf(name: string, value?: string, platform?: NodeJS.Platform): ShellStateEnvRow | undefined; /** A variable a bash start does not pass: its name, where it came from, and the row that holds it. */ export interface DroppedShellState { readonly key: string; readonly from: "configured" | "inherited"; readonly row: ShellStateEnvRow; } /** One configured layer (`shellEnv`, a call's `env`) as a bash start receives it: read once, as the composition reads a grant * (`label` is its name there, and its refusal is thrown in the composition's words), into a copy without any entry a row holds. */ export declare function configuredShellLayer(layer: unknown, label: string, platform?: NodeJS.Platform): { readonly kept: Record<string, string | undefined> | undefined; readonly dropped: readonly DroppedShellState[]; }; /** Of the configured layers' lost entries (`layers` in the order they are laid: `shellEnv`, then a call's `env`), the ones that decide * their name: the last layer that gives a name decides it — its value lost keeps the name out of the child's startup input environment * (the host process's own value too); its value kept stands, whatever an earlier layer gave. Names compared as `platform` compares them. */ export declare function configuredNamesKeptOut(layers: readonly ReturnType<typeof configuredShellLayer>[], platform?: NodeJS.Platform): DroppedShellState[]; /** The composed startup input environment of a model shell start (configured layers already passed {@link configuredShellLayer}) by each * row's `inherited` rule. A name a configured layer gave and lost (`configuredDropped`) is omitted from the child's startup input * environment (never uncovering the host's own value; variables the shell itself creates or a startup file sets after startup are * outside this boundary; the host's dropped value is not told again — the configured entry already is). */ export declare function inheritedShellState(env: NodeJS.ProcessEnv, platform?: NodeJS.Platform, configuredDropped?: readonly DroppedShellState[]): { readonly kept: NodeJS.ProcessEnv; readonly dropped: readonly DroppedShellState[]; }; /** An environment without any variable a row holds, whatever its source. */ export declare function withoutAnyShellState(env: NodeJS.ProcessEnv, platform?: NodeJS.Platform): NodeJS.ProcessEnv; /** The names the question is asked with — all it needs to start a program and answer "what are you": `PATH` (a wrapper that execs a * shell by name finds it), `HOME`, `LANG` and every `LC_*`. Nothing else: not `shellEnv`, not any other inherited name (a credential, * `BASH_VERSION` / `ZSH_VERSION`, which tell the two shells apart). On win32 the runtime adds its own required variables to every * child (libuv: `SYSTEMROOT`, `WINDIR`, `TEMP`, …) whatever is passed. A host that asks {@link SHELL_KIND_QUESTION} itself gives the * candidate the same environment ({@link shellKindQuestionEnv}). */ export declare const SHELL_KIND_QUESTION_ENV_NAMES: readonly string[]; /** The environment a candidate is asked {@link SHELL_KIND_QUESTION} in, out of `env` (the host process's own, as the scrub hands it * on, read when the question is asked): only {@link SHELL_KIND_QUESTION_ENV_NAMES}, without any variable a row holds. Names * compared as `platform` compares them. Refuses a non-record `env` and any passed `platform` that is neither in `NodeJS.Platform` * nor the runtime's own `process.platform` (an omitted or `undefined` `platform` defaults to `process.platform` unchecked). */ export declare function shellKindQuestionEnv(env: NodeJS.ProcessEnv, platform?: NodeJS.Platform): NodeJS.ProcessEnv; /** A leg's one telling of its reading (as it is prepared): when its hands are on, its environment performs I/O, and the start is not * `mounted` — the shell tools it would have offered are not. */ export declare function announceModelShellUnmounted(reading: ModelShellReading, facts: Readonly<Partial<Record<"env.hands" | "env.performsIo", boolean>>>, onNotice: ((notice: EngineNotice) => void) | undefined): void; /** The most code points of the host's failure words the model reads in {@link shellToolAbsence} (the operator notice carries them whole). */ export declare const MODEL_SHELL_CAUSE_MAX = 300; /** A leg's answer for a model call of a shell tool its reading left out — the reason and the way out, in the run's locale (`text`, * the loop's catalog reader), as the detail of the not-found answer (`expected-absent`) — when the leg tells * `config.model_shell_unmounted` ({@link announceModelShellUnmounted}'s condition); for any other name, or any other leg, nothing * (`undefined`: the miss answers as it always did). Matching names come from mount-table rules needing `env.modelShell` * (`namesNeedingFact`, regardless of other mount conditions or selectors); the host's failure words reach the model on one bounded line. */ export declare function shellToolAbsence(reading: ModelShellReading, facts: Readonly<Partial<Record<"env.hands" | "env.performsIo", boolean>>>): ((name: string, text: EngineText) => string | undefined) | undefined; /** The leg's notice that the shell tools are not offered: its environment declares no start for model text, or its start is unavailable. */ export declare function modelShellUnmountedNotice(reading: Exclude<ModelShellReading, { readonly kind: "mounted"; }>): EngineNotice; /** The environment's notice that variables of {@link SHELL_STATE_ENV} were not passed to the model's shell — names, never values; the * way out is said by where each came from and what its row allows. `more`: the record kept all the names it keeps and lost others * (`ExecutionEnv.modelShellDropped.more`), said with the names or, when there is no new name to tell, alone. */ export declare function shellStateEnvDroppedNotice(dropped: readonly DroppedShellState[], more?: boolean): EngineNotice; export {};