UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

164 lines (163 loc) • 13.6 kB
import type { StartedShell } from "../../shell/lex.js"; import type { EstablishedRead } from "./shell-read-judge.js"; import { type EstablishedPlan } from "./shell-read-establish.js"; import { type ProgramPositionRefusal } from "./bash-program-position.js"; import type { ExecutionEnv } from "../../internal/harness-types.js"; import type { ToolCallRequest } from "../../core/tool-policy.js"; /** The removal actions (`removal-actions.ts`: the one table both faces a removal meets read). */ export { SHELL_REMOVAL_ACTIONS, SHELL_REMOVAL_COMMANDS, type ShellRemovalCommand } from "./removal-actions.js"; /** What makes a removal target dangerous, in the order a card names it. */ export type DangerousRemovalKind = "filesystem_root" | "home" | "workspace" | "top_level" | "mounted_drive"; /** The protected locations a removal is judged against, each at its spelling and at the location the environment answers * for it (a link, an alias or a mapped drive leads to one location: it is protected however it is reached). `homes`: the * environment's declared home; `workspace`: the task root, the additional working directories — and, judged at one removal * ({@link removalSeatAt}), the working directory; `startedIn`: the directory the command starts in (its spelling and its * answered location), the removal's working directory wherever the plan does not prove the removal's shell left it * (absent: `workspace` holds every one); `departures`: the plan's working-directory bases PROVEN to stand outside * `startedIn` ({@link departuresOf}), each as the plan spells it (absent or empty: no base is). */ export interface RemovalSeat { readonly homes: readonly string[]; readonly workspace: readonly string[]; readonly startedIn?: readonly string[]; readonly departures?: ReadonlySet<string>; } /** One dangerous removal a command makes: the removal program, the operand as written, and the location judged * protected — the cell the guard judged ({@link JudgedRemoval}), which the question names. */ export interface DangerousRemoval { readonly kind: DangerousRemovalKind; readonly program: string; readonly operand: string; readonly target: string; } /** * Where the environment answers a directory a removal empties is: the location `<dir>/*` reaches through a link, a link to * a link, an alias or a mapped drive — `answered(dir)`, the environment's answer for the directory as spelled ({@link * answeredLocation}), `undefined` where it gave none. The removal is judged there by the same pattern (`<answered dir>/*`), * so it reads as the same removal written by the directory's own name. */ type AnsweredDirectory = (dir: string) => string | undefined; /** * The ONE dangerous-removal predicate: is removing this absolute path removing a protected location? The reference * implementation's set — the filesystem root (a drive root, a UNC share root), a home directory * (every spelling the seat holds), a workspace directory or any of its ANCESTORS (the task root, the working directory, an * additional working directory — whether the home lies inside the task root or the task root inside the home), a top-level * directory (one segment below the root) — and, for an `rm` that walks what it removes (`rm`: present; `recursive`: it * selects a recursive option; or the path names every entry of a directory), a mounted drive ({@link MOUNTED_DRIVE_KEY}): * the folder that holds drives, a drive, or an entry at a drive's top level. `every`: the run that names every entry for the * operand this path is a cell of ({@link everyEntryRunOf}; a run of `*` by default). Judged by the keys as written, then — only * where they find nothing — by the keys' drive reading ({@link KeyReading}). `undefined` for a relative path or any other target. */ export declare function dangerousRemovalOf(path: string, seat: RemovalSeat, rm?: { readonly recursive: boolean; }, every?: RegExp): DangerousRemovalKind | undefined; /** What one removal finding asks: a protected location (`dangerous_delete`; `untrackedFrom` — the segment the working * directory stops being tracked at, where the target is relative and was judged where the text stood then; kind * `unread_starts` — a `find` whose starting points are not read and that acts, `find-unread-lead.ts`: `operand` the unread * word, `target` the segment); a recursive * forced removal whose target the * guard cannot verify (`unverifiable_delete`) — an operand the shell rewrites from a value the plan does not settle * (`rewritten`), the operands of a removal its argv stream fills in (`stream`), or an operand relative to the directory a * launcher option moved the removal into (`relocated`); or a shell handed text the engine cannot read * (`unverifiable_shell_text`) — a text a program produces (`produced`: what a pipe or a `<(…)` carries, an argv a stream * fills in, a substitution, a program word taken from a positional parameter), a text nobody read (`unread`: one the lexer could not read, all of it or the rest from where it stopped, {@link unreadPlan}), or a * segment whose program the walk could not name (`position`, {@link programUnread}: the program-position reader's own refusal * of the segment's argv, the reading the planner declined it by). */ export type RemovalFinding = (Omit<DangerousRemoval, "kind"> & { readonly kind: DangerousRemovalKind | "unread_starts"; readonly guard: "dangerous_delete"; readonly untrackedFrom?: string; }) | { readonly guard: "unverifiable_delete"; readonly program: string; readonly operand: string; readonly source: "rewritten" | "stream" | "relocated"; } | { readonly guard: "unverifiable_shell_text"; readonly program: string; readonly text: string; readonly source: "produced" | "unread"; } | { readonly guard: "unverifiable_shell_text"; readonly program: string; readonly refusal: ProgramPositionRefusal; readonly source: "position"; }; /** * The names a removal target may be built on and still be verified: a root the SHELL provides, never one a command's * output fed — `rm -rf "$TMPDIR"/<literal path below it>` is not asked about. `TMPDIR` is the reference's carve-out (its * rule exempts "a well-known-safe temp pattern"); `HOME` is sema's (a registered divergence, looser than the reference, * whose rule names `$HOME` as the blast radius: it is a root the shell provides too). A declared home is read by the * value table itself — `$HOME` is then its value, and the target is judged where it lands; `PWD` is no root of this kind: * its value is where the working-directory thread says the command stands. */ export declare const KNOWN_SAFE_ROOTS: readonly string[]; /** * The removal findings of an established plan — ONE walk, nested plans included (a `bash -c` / `sh -c` text, a substitution * body), two words. For every removal segment ({@link removalProgramOf}: an `rm` / `rmdir` the text names, or a program it * does not settle, read as `rm`) whose argv the program does not refuse outright ({@link optionsReceived}), each of its * OPERANDS (every write target that is not one of its option words: a word some implementation's reading of the * program's row writes, {@link writeReadings}) as the plan established it on the WRITE face (the entry the program * unlinks, and the deny-floor shadow of a home word the plan did not place) is judged by {@link dangerousRemovalOf}, * save one the program refuses by its own first step ({@link refusedByRemoval}: `.`, `..`). In a recursive forced removal ({@link selectsOptions}), an operand * the shell rewrites from a value the plan's table does not settle — a name with no value here or one set in a form the * table cannot read, a substitution, a parameter operator, a positional parameter — is an unverifiable target, save a * literal path below a known-safe root the shell still provides there ({@link belowKnownSafeRoot}); so are the operands * of a removal its argv stream fills in (`… | xargs rm -rf`), and an operand the plan cannot place — in a removal a * launcher moved into another base, or in text such a removal hands a shell (`env -C <dir> rm -rf build`). A relative * operand past a directory move the working-directory thread cannot follow is judged where the text stood when it lost * track (the reference resolves it against the original working directory: KL-1403-b), and a card it earns says so. A `read` fills its name from the input: a value * the table does not settle (stricter than the reference, whose rule counts `read VAR` as an in-window assignment). A * name the table settles is its value, so `D=./build; rm -rf "$D/../build"` is judged * where it lands, as `rm -rf ./build` is. A shell handed a text the engine cannot read — one a program produces * ({@link runtimeScriptOf}), or one nobody read ({@link unreadPlan}, {@link programUnread}) — is an unreadable text of its * own, and so is a segment whose program word takes its value from the command's own text ({@link programWordFromOwnText}: a positional * parameter, a name the text set, a substitution): which program runs is built when the command runs. Text nobody read never * stands in for the text that was read: what the lexer read in full before it stopped is judged as any text's (bash runs * those lines), beside the finding of the rest. The read-face copy of an operand is not judged: it follows a link the removal does not follow. * A removal WALK ({@link removalWalkOf}: `find … -delete`) is judged at its starting points ({@link walkFindingsOf}); a command * a program hands on (`find -exec …`) is a nested plan of its segment, walked here like any other. */ export declare function removalFindingsOf(shell: StartedShell, established: EstablishedPlan, seat: RemovalSeat, safeRoots?: readonly string[], answered?: AnsweredDirectory): RemovalFinding[]; /** Does the removal program refuse this operand by its own first step — its last path component is `.` or `..`, trailing * separators stripped (POSIX `rm` step 1, which GNU, BSD and BusyBox `rm` keep; `rmdir(2)` fails on such a component * too)? It removes nothing there, wherever the name lands. The name is the one the program RECEIVES: a pattern's match * (the spelling an expansion carries beside its origin: `.*` hands it `.` and `..` beside the dotfiles, and only those two * are refused), else the word as the plan spells it; a word the shell rewrites from a value the plan does not settle * names nothing known, and is never refused here. */ export declare function refusedByRemoval(read: EstablishedRead): boolean; /** Does this plan (nested plans included) run a removal program, hand a shell a script built when it runs, or hold text * nobody could read ({@link unreadPlan}, {@link programUnread})? The guard establishes nothing for a command that does none. */ export declare function planRunsRemoval(plan: EstablishedPlan["plan"], nested?: boolean): boolean; /** What the removal guard asks for one command: the guard word and the card's whole sentence ({@link REMOVAL_GUARD_SENTENCE}) for the * first finding of the first word in {@link REMOVAL_GUARD_ORDER}; `undefined` when none. */ export interface RemovalVerdict { readonly guard: RemovalFinding["guard"]; readonly text: string; } /** * The seat the dangerous-removal arm of the removal guard (`createUnverifiableDeletePolicy`, tool-policy.ts) reads, beside the request's own `root` / * `cwd` (the task root and the live working directory): the execution environment (its declared home — the `HOME` the * shell child receives, the same seat the read station folds `~` / `$HOME` with — and its resolver, so a target that * resolves through a link is judged where it lands), and the leg's additional working directories. */ export interface RemovalGuardSeat { readonly env?: ExecutionEnv; readonly additionalRoots?: () => readonly string[]; } /** * The removal guard's READING of one command — undefined when no segment of it (nested shell text included) runs a * removal program or hands a shell a script built when it runs, and every text in it was read, so a command that removes * nothing costs the gate no suspension (a text the lexer could not read to its end is not one: the rest is a finding of its * own, and the lines before its syntax error, which bash runs, are judged as any). Otherwise `judge` establishes the plan the read * station plans (the same planner, the same value table, the same write-target reading) lexically against the request's * `cwd` and the seat's home, and — where an environment is seated — through its resolver too, and returns what either * establishment finds ({@link removalFindingsOf}: the protected-location word first, then the unverifiable target, then the * unreadable text). One location is one location however it is reached: with an environment seated, every protected location * is held at its spelling and where the environment answers it is ({@link RemovalSeat}), and a directory a removal empties is * judged where the environment answers it is ({@link AnsweredDirectory}); a location it gives no answer for is judged by its * spelling alone. * `safeRoots`: the known-safe roots ({@link KNOWN_SAFE_ROOTS}, with a deployment's own added by the policy). */ export declare function removalReadingOf(command: string, req: Pick<ToolCallRequest, "cwd" | "root" | "face">, seat: RemovalGuardSeat | undefined, safeRoots?: readonly string[]): { judge(signal?: AbortSignal): Promise<RemovalVerdict | undefined>; } | undefined;