@sema-agent/core
Version:
Stateless, task-oriented AI agent core
202 lines (201 loc) • 14.3 kB
TypeScript
/**
* FIND'S ACTIONS — what one `find` argv removes and hands on, read off its expression (GNU findutils `find(1)`, BSD `find(1)`):
* the STARTING POINTS (every word in front of the expression — which starts at a dash word, a lone `(` or a lone `!` — after the
* row's own leading options), the ACTIONS that remove or
* hand on (`-delete`; `-exec` / `-execdir` / `-ok` / `-okdir`, each running from the word after the action up to its `;`, or its `+`
* right after a `{}`), and the expression they stand in, grouped as `find` groups it ({@link FindExpr}). One rule says how far an
* action reaches ({@link reachAt}): it reaches EVERY entry below a starting point unless every way the expression reaches it passes
* a test that narrows it there ({@link FindTest}) — the line `rm` is judged by too (`rm -rf ./*` removes everything below `.`,
* `rm -rf ./build/*` does not). Which dash word takes a value is the program read model's (`readOptionRole` on the `find` row);
* nothing here reads shell text — the words arrive as the planner lexed them.
*/
import { type ProgramRow } from "./program-read-model.js";
import { type LexedWord } from "../../shell/lex.js";
/** One word of `find`'s argv: its raw spelling, what `find` receives (its literal value, else its folded spelling), and whether
* the shell rewrites it (any piece not literal: an expansion, a pattern — what `find` receives is not the spelling). */
export interface FindWord {
readonly raw: string;
readonly spelled: string;
readonly rewritten?: true;
}
/** The one reading of a lexed word as a {@link FindWord} — the planner's and the removal guard's. `{}` is literal (bash brace-expands
* only a list or a sequence). */
export declare function findWordOf(w: LexedWord): FindWord;
/** The starting point a walk that names none starts at (GNU's default). */
export declare const FIND_DEFAULT_START: FindWord;
/** One command an action hands on: the argv positions `[from, to)` (its program first), and the action that hands it. */
export interface FindHanded {
readonly action: "-exec" | "-execdir" | "-ok" | "-okdir";
readonly from: number;
readonly to: number;
}
/** One action of the expression that removes or hands on: `-delete`, or a command handed on. */
export type FindAction = {
readonly action: "-delete";
} | FindHanded;
/**
* A test of the expression, by what it says about the entries an action behind it reaches — the closed set read as NARROWING
* ({@link narrowsAt}); every other test (`-type f`, `-mtime`, `-newer`, `-size`, `-mindepth`, `-user`, …) and every other action
* (`-print`, `-prune`, …) is `other`. `name`: `-name` / `-iname`, a pattern over the entry's own name. `path`: `-path` / `-ipath` /
* `-wholename` / `-iwholename` (a pattern) and `-regex` / `-iregex` (a regular expression), over the whole path `find` matches —
* the starting point as written, then the entry below it; a regular expression carries the dialect the command reads it in
* ({@link RegexDialect}). `empty`: `-empty`. `dirs`: `-type d`. A value the shell rewrites is
* not in the command: `other`. Beside the narrowing set, what a predicate is on ANY entry ({@link certainlyRunOn}): `true` — the
* predicates that hold everywhere (`-true`, the printing actions, the walk's options: `-depth`, `-maxdepth`, `-follow`, …);
* `false` (`-false`); `prune` (`-prune`: holds, and the walk does not go below the entry); `quit` (`-quit`: the walk ends).
*/
/**
* The dialect a command has `find` read a regular expression in, by what it writes: `basic` — no `-E` and no `-regextype` in
* front of the test, or a basic one (`emacs`, `posix-basic`, `sed`, …): `\(` `\)` a group and `*` a count, on which GNU's
* default and BSD's basic dialect agree; `extended` — BSD's `-E` or an extended `-regextype` (`posix-extended`, `awk`, `gnu-awk`,
* …): `(` `)` a group, `|` a choice, `*` `?` `+` counts. Absent where the command names a dialect this reader does not know.
*/
export type RegexDialect = "basic" | "extended";
export type FindTest = {
readonly kind: "name";
readonly value: string;
readonly caseless: boolean;
} | {
readonly kind: "path";
readonly value: string;
readonly caseless: boolean;
readonly regex: boolean;
readonly dialect?: RegexDialect;
} | {
readonly kind: "empty" | "dirs" | "other" | "true" | "false" | "prune" | "quit";
};
/** The expression as `find` groups it: `( … )` first, then `!` / `-not`, then `-a` / `-and` (or two terms side by side), then
* `-o` / `-or`, then `,`. An action is its index in {@link FindActions.actions}. */
export type FindExpr = {
readonly op: "and" | "or" | ",";
readonly of: readonly FindExpr[];
} | {
readonly op: "not";
readonly of: FindExpr;
} | {
readonly op: "test";
readonly test: FindTest;
} | {
readonly op: "action";
readonly at: number;
};
/**
* `starts`: the argv positions of the starting points (none: the walk starts at {@link FIND_DEFAULT_START}). `startsFromFile`: the
* starting points are read from a file (`-files0-from`) and none of them is in the command. `actions`: the actions that remove or
* hand on, in the order written (`deletes` / `handed`: the two kinds of them). `expr`: the expression; `undefined` where it does
* not parse — `find` refuses it and runs nothing, and every action is read as reaching every entry. `depths`: the levels below a
* starting point the walk acts at ({@link FindDepths}). `certainDepth`: the deepest level below a starting point whose every entry the walk
* evaluates, where an option lets it pass some by (options of the whole walk wherever they are written; the least counts) — 1
* under `-xdev` and BSD `-x` (a mount point right below the start is evaluated, nothing under it is walked), 0 under `-mount`
* (GNU findutils 4.11 passes a mount point by itself) and BSD `-X` (it passes by a name holding a blank, a quote or a
* backslash); absent otherwise.
*/
export interface FindActions {
readonly starts: readonly number[];
readonly startsFromFile: boolean;
readonly actions: readonly FindAction[];
readonly deletes: boolean;
readonly handed: readonly FindHanded[];
readonly expr: FindExpr | undefined;
readonly depths: FindDepths;
readonly certainDepth?: number;
}
/**
* The levels below a starting point the walk acts at (the starting point is level 0): `-mindepth N` (no action at a level above
* N) and `-maxdepth N` (no level below N is walked) — options of the whole walk wherever they are written (GNU, BSD), the last of
* each counting. Each is absent where none is written, its number where the last one's value is a literal number, and
* `unknown` where the shell rewrites it (or `find` would refuse it): then nothing is known of that bound.
*/
export interface FindDepths {
readonly min?: number | "unknown";
readonly max?: number | "unknown";
}
/** Read one `find` argv (the words after the program word). */
export declare function findActionsOf(row: ProgramRow, args: readonly FindWord[]): FindActions;
/**
* Does this test, at this starting point, keep the action behind it from reaching every entry below the starting point? A NAME
* pattern whose text, bracket expressions taken out, holds a character no wildcard is (`*.tmp`; not `*`, `?*`, `[!.]*`); a PATH
* pattern or a regular expression over the path that 7.70.0 read as narrowing ({@link narrowsOn7700}) and that this reader does not
* PROVE reaches every entry there ({@link reachProofOf}: `.*`, `.*x*`, `\./.*` at `.`, `./*` at `.` are proved; `.*~`, a run then
* `/....` are not narrowing on 7.70.0 already) — what the reader does not read, or reads only in part, is judged as 7.70.0 judged it;
* `-empty` (only entries that hold no data); `-type d` behind `-delete` only (`-delete` cannot remove a directory that holds
* anything, and removes no file there — `-exec rm -rf` removes what such a directory holds).
*/
export declare function narrowsAt(t: FindTest, start: FindWord, forDelete: boolean): boolean;
/**
* Does this path pattern or regular expression PROVABLY reach every entry below the starting point — the name of the proof (the
* arm the comparison probe prints), or `undefined` where the reader proves nothing (then 7.70.0's reading judges it,
* {@link narrowsAt}). A proof is a shape the pattern matches every path of ({@link reachShapes}, {@link globReachShape}) that
* matches every entry below the start ({@link shapeReachesEvery}).
*/
export declare function reachProofOf(t: Extract<FindTest, {
kind: "path";
}>, start: FindWord): string | undefined;
/** Does this test fail on the starting point itself — `-false`, a name or path pattern its own spelling does not match (a starting point
* the shell rewrites is read as failing: its name is not in the command — known limit)? A regular expression, `-empty` and
* `-type d` may hold for it (the conservative side). */
export declare function failsOnStart(t: FindTest, start: FindWord): boolean;
/** How far one action reaches from one starting point: `every` entry below it (no test in front of it narrows it there,
* {@link narrowsAt}), and whether it may reach the starting point itself (the walk acts at level 0 — no `-mindepth` of 1 or
* more, {@link FindDepths} — and no test in front of it fails there, {@link failsOnStart}). */
export interface FindReach {
readonly every: boolean;
readonly start: boolean;
}
/** {@link FindReach} for each of the actions, at one starting point. An expression that does not parse reaches everything. */
export declare function reachAt(a: FindActions, start: FindWord): readonly FindReach[];
/**
* A directory at or below a starting point, as the expression reads it ({@link certainlyRunOn}): the paths `find` matches a path
* test against there — the starting point as `find` prints it, then the way down to the directory, one path per spelling —, its
* own name, and its level below the starting point (the starting point itself is level 0).
*/
export interface FindDir {
readonly paths: readonly string[];
readonly name: string;
readonly level: number;
}
/** The directories from a starting point down to the one `rel` (its names, in order, below the starting point) leads to: the
* starting point is printed as written (with and without its trailing separators), or — where the shell rewrites it — as `value`,
* the place the plan established it at. `way` ends at the directory's parent. */
export declare function findDirsDown(start: FindWord, value: string, rel: readonly string[]): {
readonly way: readonly FindDir[];
readonly dir: FindDir;
};
/**
* For each action: does the walk from a starting point certainly reach `dir` and run the action on it — the reading the removal
* guard judges a location it names below a starting point by, where a test narrows the action there ({@link reachAt}). Certain
* only: the expression parses and writes no `-quit`; `dir`'s level is within the walk's depth bounds, each of them known, and at
* most the level whose every entry the walk evaluates ({@link FindActions.certainDepth}: a mount point, a name `-X` passes by); no
* directory on `way` (the starting point, level 0, down to `dir`'s parent) is pruned, or may be; and the action is evaluated on
* `dir` whatever is not known — each test on the way to it holds or fails by what the command spells ({@link truthOn}: where 7.70.0
* settled a regular expression, as it did; a choice in the command's dialect where one branch settles it, the walk then
* certainly running the action there — on GNU's `find` for `\|`). Anything less is no claim: the action is not read as running there.
*/
export declare function certainlyRunOn(a: FindActions, way: readonly FindDir[], dir: FindDir): readonly boolean[];
/** The starting points as words ({@link FIND_DEFAULT_START} where the command names none). */
export declare const findStartsOf: (a: FindActions, args: readonly FindWord[]) => readonly FindWord[];
/** The starting points (as words) from which a `-delete` reaches every entry below — the walk the removal guard judges. */
export declare function deletesEverythingFrom(a: FindActions, args: readonly FindWord[]): readonly FindWord[];
/**
* The command lines `find` hands on, one per handed command and per spelling of what `{}` stands for at each starting point: the
* command's words as written, joined, with each `{}` in what `find` receives replaced ({@link withEntry}) by what it stands for
* there ({@link reachAt}) — the entries below the starting point, then the starting point itself where it may be reached: below
* it, every entry where the action reaches every entry (`<start>/*`), else one entry, spelled literally (`<start>/'{}'` — the
* same reading `rm <start>/<name>` gets). The starting point is an entry of its own walk, whatever the action reaches below it:
* `{}/work` there is `<start>/work`, which no entry below it spells. Starting points read from a file leave `{}` as written: the
* entries are not in the command. `-execdir` runs its command in each entry's own directory: `{}` lands on the same entry, and
* its other relative words are read where `find` stands (the conservative side holds for `{}` only).
*/
export declare function findHandedLines(row: ProgramRow, args: readonly FindWord[]): string[];
/** One command line `find` hands on ({@link findHandedLines}), with where it comes from: the action (its index in
* {@link FindActions.actions}), the starting point (its index in {@link findStartsOf}) and what `{}` stands for in it — `every`
* entry below the starting point, `one` entry spelled literally, the starting point itself (`start`), or entries a file names
* (`listed`: `{}` as written). */
export interface FindHandedCommand {
readonly line: string;
readonly action: number;
readonly start: number;
readonly entry: "every" | "one" | "start" | "listed";
}
/** The handed command lines one per action, starting point and entry, in that order ({@link findHandedLines}: their lines). */
export declare function findHandedCommands(row: ProgramRow, args: readonly FindWord[]): FindHandedCommand[];