@sema-agent/core
Version:
Stateless, task-oriented AI agent core
97 lines (96 loc) • 8.11 kB
TypeScript
import { startedShellOfCall } from "./call-shell.js";
import { type ShellCommandShape } from "./shell-run-candidates.js";
import { type StartedShell } from "../shell/lex.js";
import { type LineDescriptors } from "../tools/fs/shell-read-device.js";
/** The reader's answer: read-only, or not — with the first reason the reading stopped on (a sentence a
* trace or a card can show; never adjudication input). */
export type ReadOnlyShellVerdict = {
readonly readOnly: true;
} | {
readonly readOnly: false;
readonly reason: string;
};
/** How a caller asks: a shape it already read, the session's working directory, the whole line's descriptor
* bindings when it judges one segment of a longer line, the deployment's programs, and whether the line is read as it runs. */
export interface ReadOnlyShellOptions {
/** The command already read with {@link readShellCommand} (the gate reads every shell command once). */
readonly shape?: ShellCommandShape | undefined;
/** Where the session stands: a `cd` beside a `git` that lands here is no line objection. */
readonly cwd?: string | undefined;
/** The descriptor bindings of the WHOLE line, for a caller that judges one segment of it. */
readonly bound?: LineDescriptors | (() => LineDescriptors) | undefined;
/** The programs a deployment lets a face run: every program the line names (each segment's program past a `command` /
* `builtin` prefix, and the program an `xargs` segment hands its input to) must be one of them, else the
* line is not read-only for that face. It only ever narrows — a program here whose call writes is still not read-only. */
readonly readers?: ReadonlySet<string> | undefined;
/** Read the values the line itself settles ({@link readLineValues}): its leading literal assignments bind their names, `PWD`
* is `cwd` on a line that changes no directory, and a backquote the lexer reads as a literal character is that character.
* The answer is then "the line, run as written, only reads" — never "its text matches upstream's allow forms": a caller that
* clears a call without asking (the allow layer) does not set it. */
readonly knownValues?: boolean | undefined;
}
/** The reference implementation's question for a compound that changes directory and runs git (`cd-git-compound`). */
export declare const CD_GIT_COMPOUND_SENTENCE = "This command changes directory before running a version-control command, which can pick up untrusted hooks or repository configuration from the target directory. Approve only if you trust it.";
/**
* Does this line change directory and run git — a `cd` beside a `git` — other than by ONE plain `cd <path>` that lands
* where the session stands (`cwd`)? The reference's no-op reading, lexical here: the path is `.` / `..`, starts `./` /
* `../` / `/`, carries no expansion, quote-removed text only, and resolves to `cwd` itself. No `cwd` ⇒ no no-op.
*/
export declare function changesDirectoryBeforeGit(shape: ShellCommandShape, cwd?: string): boolean;
/**
* Read `command` as every read-only consumer must: read-only, or the first reason it is not. A shape already read
* by the caller may be handed in (the gate reads every shell command once).
*
* @contract read_only_shell.every_segment — a command line is read-only iff the lexer reads it whole (no
* unreadable segment, no group/subshell/control structure, no background `&`), no word anywhere carries a
* variable, arithmetic or command substitution, and EVERY segment is read-only on its own: its
* redirections only read or discard (`<` family, `/dev/null`, a descriptor duplication), its leading
* assignments name only variables from {@link READ_ONLY_ENV_NAMES} with literal values, and its program
* with its arguments is admitted by exactly one of three readings in order — a segment carrying a GLOB is
* admitted only under {@link READ_ONLY_GLOB_PROGRAMS}; else the structural reading (the bare-program set,
* the exact forms, `echo`/`ls`/`cd`/`find`/`printf`/`history`/`arch`/`ifconfig`); else the flag-vetted
* table with its per-row danger predicates, and the `uniq`/`jq` forms. A `cd` beside a `git` in one line
* is never read-only (the git command's repository is then not the session's) unless the `cd` lands where the session
* stands ({@link changesDirectoryBeforeGit}). Given {@link ReadOnlyShellOptions.readers}, a program the line names
* outside them is an objection of its own, met where the reading meets the program. Given
* {@link ReadOnlyShellOptions.knownValues}, the line is first read with the values it settles itself ({@link readLineValues}), and a
* backquote the lexer reads as a character is no objection; the rest of this reading applies to what that leaves. Anything the
* reading does not positively admit is NOT read-only — the answer is never a guess, and a `false` costs one question.
*/
export declare function readOnlyShellVerdict(command: string, opts?: ReadOnlyShellOptions): ReadOnlyShellVerdict;
/** {@link readOnlyShellVerdict} of a text `shell` runs (the engine's readers take the call's started shell). */
export declare function readOnlyShellVerdictIn(shell: StartedShell, command: string, opts?: ReadOnlyShellOptions): ReadOnlyShellVerdict;
/** {@link readOnlyShellVerdictIn} of a call's `command`, on the shell the call's face says runs it ({@link startedShellOfCall}). */
export declare function readOnlyCallVerdict(req: Parameters<typeof startedShellOfCall>[0], command: string, opts?: ReadOnlyShellOptions): ReadOnlyShellVerdict;
/**
* The option that keeps `command` from reading read-only, when the FIRST thing the reader refuses in it is an option outside
* the read-only set of a program its tables know (`sort -o out in`: `sort` alone is read-only, `-o` is not in its set). Absent
* when the line is read-only, and when the first refusal is anything else (an unknown program, a redirection, a malformed
* option value, a shape): those are worded elsewhere. Reads the line with no list of programs — the question is about the
* call, not about a face's list.
*/
export declare function optionOutsideReadOnlySet(command: string): {
readonly program: string;
readonly option: string;
} | undefined;
/**
* The sentence a refused shell call is shown with on a card: the classifier's own account (`classifierReason`), except where that
* account is a program's LIST refusal (`unlistedProgram`, from {@link CompoundReadonlyFacts}) and the reader finds an OPTION of that
* same program outside the read-only options of its table. A list refusal is the whole account only when the call has no read-only
* form anyway (`rm x`); for a program the reader's tables know — `sort in` reads and is never asked about — the list is not why
* `sort -o out in` is asked about, its option is, and the sentence says so. Any other refusal keeps the classifier's sentence.
*/
export declare function probeRefusalSentence(command: string, classifierReason: string, unlistedProgram: string | undefined): string;
/**
* The reader's LINE-level objection to `command` — what makes no segment of it read-only in its place, whatever the
* segment reads on its own: the line's structure (a line the reader stopped in included), or a `cd` beside a `git` ({@link changesDirectoryBeforeGit}). A caller
* that judges a compound's segments one by one ({@link readOnlyShellVerdict} over a segment's text) asks this of the
* line first.
*/
export declare function readOnlyLineObjection(shell: StartedShell, command: string, cwd?: string): string | undefined;
/**
* What the whole text of `command` binds — every redirection of every segment (a single one, an `exec`'s, a `{name}` one included),
* read by the station's own computation ({@link descriptorsOfPlan}): the one binding map the shape tier's operand reading shares with
* the station's rebinding rule. The plan is read with no listed readers: the bindings do not depend on which programs read.
*/
export declare function lineDescriptorsOfText(shell: StartedShell, command: string): LineDescriptors;