@sema-agent/core
Version:
Stateless, task-oriented AI agent core
158 lines (157 loc) • 14 kB
TypeScript
/**
* Where a text the read plan (`shell-read-plan.ts`) plans starts: a nested text's start — its names, the deny floors the
* words handing it on carry, the user its shell runs as, its standard input and its working directory — and where each
* segment's standard input comes from. A nested plan starts at a placeholder ({@link nestedStartOf}) and is re-based onto
* the segment it sits in ({@link rebasePlan}) — onto the directory a launcher settles for it ({@link launchedAt}) where one
* does —, unless it starts in a working directory of its own.
*/
import { type LexedSegment } from "../../shell/lex.js";
import { type BindingTable, type InheritedBody, type InheritedFunctions, type NameReader, type WordFloor } from "./shell-read-bindings.js";
import { type ProgramPosition } from "./bash-program-position.js";
import { type TargetUser } from "./program-read-model.js";
import type { InputText } from "./shell-reentry-text.js";
import type { ReadCandidate, ShellReadPlan } from "./shell-read-plan.js";
import type { Read } from "./shell-read-word.js";
/** Where a nested text starts its names: a substitution body inherits the table of the segment it sits in (a subshell
* holds every variable of the shell that forks it); a text the shell runs later or elsewhere (a code body, `eval`'s
* text, a child shell's `-c`) starts from the text's own start — every name unknown, `HOME` the declared home unless
* the enclosing text binds it where that reaches the nested text. `enclosingBinds` answers for the enclosing texts: a
* binding that may run before the segment handing the text on, or by that segment, for a text run there now; any
* binding, for a text run later. */
export interface PlanStart {
readonly initial: NameReader;
readonly enclosingBinds: (name: string) => boolean;
/** `HOME` may already hold something other than the declared home where the nested text starts. */
readonly homeTouched: boolean;
/** The attributes, name references and deny floors in force where the nested text starts. */
readonly inherited: BindingTable["inherited"];
/** The working directories it starts in, where they are not the enclosing segment's (a login shell starts in its user's
* home; `"unmodelled"` — a home the text does not settle). */
readonly bases?: readonly string[] | "unmodelled";
/** The thread of the shell that runs the text, where the text runs in that shell or a fork of it ({@link HeldThread}). */
readonly thread?: HeldThread;
/** What the shell around the text holds where the text runs in that shell or a fork of it — a literal `eval` text, a
* substitution body, a text run later (a `trap` action): each function and builtin key as it stands there
* ({@link InheritedFunctions}). A text a new process runs (a child shell's `-c`) holds none: every builtin enabled, no function. */
readonly names?: InheritedFunctions;
/** What arrives on its own standard input (the input of the shell running it). */
readonly stdin?: readonly InputText[];
/** The functions the shell running this text inherited (`export -f`, an environment's `BASH_FUNC_*`), each with whether
* its body is text the engine read ({@link InheritedBody}), inherited by it and by ITS child shells: a child adds its own
* exports and passes the whole on, so a grandchild still sees a function exported two shells up (#1628). Only on a
* child-shell start; a substitution / eval runs in the same shell and reads its functions whole. */
readonly exportedFunctions?: ReadonlyMap<string, InheritedBody>;
/** Whether each variable is in the environment the shell running this text started with — what a child shell it starts receives
* unless the text exports it: `yes`, `no`, or `maybe` (the environment's names are not declared). Only on a child-shell start
* (the names the starting segment hands on: its own environment's and every one exported there); absent ⇒ the deployment's
* declared environment names ({@link ShellReadDeclarations.receivedEnvNames}). */
readonly received?: (name: string) => Received;
}
/** Is a variable in a shell's environment ({@link PlanStart.received})? */
export type Received = "yes" | "no" | "maybe";
/** The thread of the shell that runs a text, at the segment that hands it on: a literal `eval` text is not a new plan but the next
* segment of the same thread, so it starts in all of it and hands the working directory and the physical mode back; a substitution
* body (a fork) and a text run later start in what the shell holds and hand nothing back. `functions`: the names a call may mean
* (the shell's definition heads and the ones an `eval` text defines), `unread` those whose bodies the nested plan has not read — an
* `eval` text reads the shell's bodies through the names they bind, every other text has none of them in front of it. */
export interface HeldThread {
readonly physical: boolean;
readonly functions: ReadonlySet<string>;
readonly unread: ReadonlySet<string>;
/** The places the shell's closed scopes went (a function body it may call goes there): the deny floor of a walk in the text
* that loses track ({@link WorkdirThread.reached}). */
readonly reached?: ReadonlySet<string>;
}
/** What a shell holds at a segment, for a text that runs in it or in a fork of it ({@link PlanStart.names}): each function and builtin key
* as it stands there, and the names a call to one of its functions may change — the ones the text reads a call of: an `eval` text names
* the shell's functions (`bound`: what a call to one of them changes); a substitution body and a text run later read none of their bodies
* (the shell's functions are {@link HeldThread.unread} there: a call to one makes every name unknown), so they name none. */
export declare function heldAt(table: BindingTable, bound?: InheritedFunctions["bound"]): InheritedFunctions;
/** What a segment says of the start of a text it hands a shell to run later: the deny floors of the words it holds as
* written, the user the shell runs as ({@link TargetUser}), and the shell's standard input. */
export interface NestedStart {
readonly floors?: ReadonlyMap<string, WordFloor>;
readonly as?: TargetUser;
readonly stdin?: readonly InputText[];
}
/** Where a text run later starts ({@link NestedStart}): the later start, with the shell's standard input, the floors of the
* words that hand it on, and — run as another user — that user's `HOME` (the caller's, the target's, or either) and, for
* a login shell, the target's home as its working directory. */
export declare function startedAt(later: PlanStart, at: NestedStart | undefined): PlanStart;
/** A planner that plans each text once per start: a segment read in several readings hands most of its nested texts (a
* substitution's body, a launched shell's text) on alike in every reading. */
export declare function plannedOnce<K, P>(plan: (text: string, kind: K, at?: NestedStart) => P): (text: string, kind: K, at?: NestedStart) => P;
/**
* What arrives on each segment's standard input: the pipe it is reached through (`piped`, what the segment before it
* prints); else the input of the group (`{ …; }`, `( … )`) it stands in — a group a pipe feeds hands each member that
* pipe; else the input the text itself was handed (`start`: a shell's `-c` text reads that shell's standard input).
*/
export declare function stdinFeeds(segments: readonly LexedSegment[], start: readonly InputText[], piped: (si: number) => readonly InputText[]): (si: number) => readonly InputText[];
/**
* Where a nested text with no working directory of its own starts (a substitution body, a here-document's substitution, a
* launcher's command string): a PLACEHOLDER below the declared working directory, joined as its path family joins, whose last
* segment no text can spell: a NUL byte is in no path the shell can name. Every base the nested text derives from where it
* starts (a relative move, `-C` or `..` walk from it, the start itself) holds that segment; a place the text names itself
* (`cd /x`, `-C ~`, a `CDPATH` entry, a closed scope's place on the held thread) does not — the one mark {@link rebasePlan}
* moves by ({@link walkFromStart}), and the one a walk reads `PWD` by (a start holding it is no place the shell names).
*/
export declare function nestedStartOf(cwd: string): string;
/**
* The walk a base made from a placeholder start ({@link nestedStartOf}): what follows the placeholder's segment (`""` for
* the start itself), or `undefined` for a place the text names itself. Read off the segment, not the spelling above it, so
* a fold of the declared working directory (`/w/.`, `/w//`) leaves the walk readable. The walk is a relative spelling: the
* separator after the segment is taken with it, and a spelling joined under the start was relative (an absolute one is
* not joined), so the anchor step re-bases it by joining.
*/
export declare function walkFromStart(base: string): string | undefined;
/** Where a segment's launcher chain runs its program, where a launcher moves the base it runs in ({@link launchedInOf}): each
* launcher's working directory, in chain order — `null` for a value the text does not settle, a directory it does not
* know —, or `unmodelled`: a move the text does not say (another base moved, a value with two readings). */
/** The one spelling of the unmodelled word is `ReadCandidate["bases"]`'s — read from there, never respelled here. */
export type LaunchedIn = readonly (string | null)[] | Extract<ReadCandidate["bases"], string>;
/**
* {@link LaunchedIn} of a segment whose launcher chain moves the base (`undefined` where none does), read off the options
* the program position stepped ({@link ProgramPosition.baseMovedAt}): a value whose role is a working directory (`env -C`,
* `sudo -D`, `unshare -w`) is where that launcher runs what follows, the LAST one of a launcher's options winning (each
* implementation keeps the last before it changes directory). A value the text does not settle (a substitution, a name
* it does not bind to a literal, a pattern) is a directory the text does not know (`null`); a name the text binds to a
* literal — `HOME` included — is that literal, as a `cd` to it is. Any other move — a root, a
* program search, a directory taken from elsewhere (`nsenter -w` with no value), an optional value spelled `=V` (util-linux
* 2.41 drops the `=`, getopt and util-linux 2.38 keep it: two places), an empty value (no directory: the launcher runs
* nothing) — leaves it unsaid. Which word is whose option is the walk's reading and no other (env steps a lone `-` and reads
* on; a `--` ends the options).
*/
export declare function launchedInOf(pos: ProgramPosition, toks: readonly Read[]): LaunchedIn | undefined;
/**
* Where a launcher chain that moves the base ({@link LaunchedIn}) puts its program, from where the command line stands
* (`bases`, or the deny floor `floor` of a walk that lost track): each launcher's working directory in turn, as the process
* `chdir()`s to it ({@link chdirFrom}). `to` is where the text the program hands a shell starts ({@link rebasePlan} moves it
* there) — `unmodelled` where the text does not say (an unsettled move, a lost walk, a spelling naming no place), `floor`
* its deny floor then; `words` is the deny floor of the segment's own relative words, which may be the launcher's (where
* the command line stands) or the program's (where the launcher put it). `unsettled`: a launcher's value is one the text
* does not settle — the shell computes it where the command line stands, so the floor is where the launcher stood and where
* the command line stands, and what the program does from there it does from a directory the text does not know
* ({@link ReadCandidate.unsettledStart}). `undefined`: no launcher moves the base.
*/
export declare function launchedAt(launchedIn: LaunchedIn | undefined, bases: ReadCandidate["bases"], floor: readonly string[] | undefined, homeDir: string | undefined): {
readonly to: ReadCandidate["bases"];
readonly floor?: readonly string[];
readonly words?: readonly string[];
readonly unsettled?: true;
} | undefined;
/**
* A nested plan (a substitution body, a here-document's substitution, a launcher's command string) was planned from
* its placeholder start ({@link nestedStartOf}); the segment it sits in may stand elsewhere (`cd sub && cat <<EOF
* $(cat secret)`), so what it derived from the start is re-based onto the ENCLOSING segment's bases (for a text a launcher
* runs in a working directory it settles, `env -C <dir>`, onto that directory): a base spelled
* from the placeholder lands under each of them, an unsettled enclosing base leaves every relative candidate
* unplaced, and every other place (an absolute candidate, a place the nested text named itself) is untouched.
* Recursive: a nested plan's own nested plans derive from the same start. A candidate left unplaced is judged where the
* text stood when it lost track (its floor, {@link ReadCandidate.floorBases}): the places the nested text derived move with
* it like its bases, an enclosing segment that lost its base itself hands on its own floor (`floor`), and a derived place
* with nowhere to land is no place (an unplaced candidate with no floor is judged at the declared working directory).
* `unsettled`: the enclosing segment stands in a directory the text does not know — a candidate the nested text derived
* from its start is made from there ({@link ReadCandidate.unsettledStart}).
* A plan re-based onto the same place twice is one plan (a segment read in several readings hands its nested plans on alike).
*/
export declare function rebasePlan(plan: ShellReadPlan, to: ReadCandidate["bases"], floor?: readonly string[], unsettled?: boolean): ShellReadPlan;