UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

219 lines (218 loc) • 16.2 kB
/** * THE WORKING-DIRECTORY THREAD — where a command stands at each of its segments, and what `PWD` / `OLDPWD` hold there. * ONE walk (`shell-read-plan.ts` drives it, segment by segment, beside the binding thread) over a CLOSED word set that * moves the base ({@link BaseShift}): `cd` in its forms — a bare relative target searched through `CDPATH` first, `cd -` * going to `OLDPWD`, a bare `cd` to `HOME`, `-P` / `set -P` leaving `PWD` a spelling the text does not hold — `pushd` / * `popd` with the stack the text pushed, and a literal `eval` text read as the text it is, in this shell. Any other text * that may move the base where the plan cannot follow it (`source`, a non-literal `eval`, a computed program word, a * call into a function body or a code body that may move, an option the stack reading does not follow) LOSES it: what * stands after it cannot be placed. The moves are a candidate SET — a move may fail: the unmoved place stays a candidate * unless an `&&` run proves the move ran. A move reaches a later segment where a binding of the same segment would: the * binding thread's one visibility rule (`BindingThread.reaches`) — a move inside a `( … )` group, a pipeline member that * is not the last, a background job or a function body is no move for a segment outside it ({@link WorkdirScope}). */ import type { Bare } from "./shell-read-argv.js"; import type { BindingTable, WorkingDirectoryState } from "./shell-read-bindings.js"; import type { Read } from "./shell-read-word.js"; import { type LexedSegment, type LexedWord } from "../../shell/lex.js"; import type { CommandTable } from "./shell-read-command-environment.js"; import { type FunctionCall } from "./shell-read-functions.js"; import { type StdinText } from "./shell-reentry-text.js"; /** A working directory the shell could be standing in, with the latest move this candidate assumes did NOT run * (`skippedCd`), and what the shell holds there: `pwd` — what `PWD` spells (absent: not the base's spelling — a physical * move, or a start the plan does not place), `old` — what `OLDPWD` holds (absent: what the shell inherited, unknown), * `oldFloor` — where `old` is absent, what `OLDPWD` may hold (the `PWD` a move left, a binding that may not have run aside), * `stack` — the directories the text pushed and has not popped, top first (absent: the text changed the stack in a way * this walk does not follow; what lies below the text's own entries is the shell's, unknown). */ export interface BaseCandidate { readonly base: string; readonly skippedCd: number; readonly pwd?: string; readonly old?: string; readonly oldFloor?: string; readonly dirStack?: readonly string[]; } /** How one segment moves the base — the closed word set. `cd`: a `cd` (`push`: a `pushd DIR`) to a target, the * declared-home spelling of an unsettled target for the deny floor; `swap`: `pushd` with no directory; `pop`: `popd`; * `stack`: the stack changes and the base does not (`pushd -n`, `popd -n`, `dirs -c`); `eval`: a literal `eval` text; * `lost`: a move this walk does not follow. */ export type BaseShift = { readonly kind: "cd"; readonly target: Bare | "home" | "previous"; readonly physical: boolean; readonly push?: true; } | { readonly kind: "swap" | "pop" | "stack" | "lost"; } | { readonly kind: "eval"; readonly text: string; }; /** What the walk asks of the plan around it: the declared home (a `~` target), whether the text binds a name where it takes * effect before the moving segment's move — before the segment, or by its own words, redirections and command environment * (`OLDPWD` — then `cd -` reads the table's value; `PWD` — then a move's `OLDPWD` is not the base the candidate stood in; a * binding that runs after the move does not reach it), and where a literal `eval` * text at a segment, started at candidates — a candidate, the floor of a walk that lost track, or a place the walk does not * know — ends ({@link WorkdirThread.end}), and where its walk went that its end does not say (`reached`). */ export interface WorkdirContext { readonly homeDir?: string; readonly textBinds: (name: string, si: number) => boolean; readonly evalEnd: (text: string, from: readonly BaseCandidate[] | "unmodelled", table: CommandTable, si: number) => { readonly end: readonly BaseCandidate[] | "unmodelled"; readonly physical: boolean; readonly reached: ReadonlySet<string>; readonly unsettled?: true; }; } /** One segment's move as the walk applies it: the shift, whether an `&&` run proves it ran, the first segment of the * reader's `&&` run, the names there, and whether `cdable_vars` may be on (a bare name may be a variable's value). */ export interface ShiftAt { readonly shift: BaseShift; readonly provable: boolean; readonly andRunStart: number; readonly table: CommandTable; readonly cdable: boolean; } export declare class WorkdirThread { private candidates; private lostAt; /** The reachable bases at the moment the walk lost track — the deny floor of a word it cannot place. */ private floors; /** `set -P` (or `-o physical`) may be on — a text it began in, an `eval` text that set it — : a later move leaves `PWD` a physical * spelling the text does not hold, and a `..` in a target resolves links the plan cannot see. */ private physical; /** The last segment whose move was applied or lost track ({@link WorkingDirectoryState.movedAt}). */ private movedAt; /** The start is a place the shell stands in — no placeholder start, no base walked from one (an `eval` text a nested text runs): `PWD` reads it. */ private readonly readable; constructor(start: readonly BaseCandidate[] | readonly string[] | "unmodelled", /** The physical mode may already be on where the text starts (an `eval` text run in a shell that set it). */ startsPhysical?: boolean); get lost(): boolean; /** The walk lost track at a move to a directory the text does not settle ({@link movesToUnsettled}, a literal `eval` text's * own such move): what the text does after it, it does from a directory it does not know * ({@link import("./shell-read-plan.js").ReadCandidate.unsettledStart}); so does a loop it does not count that moved it * (each beat moves again from where the last one went). Once set it stays: a closed scope's places stay * in the deny floor ({@link restore}), and a function body that moved may run at any later call. */ get unsettled(): boolean; private unsettledAt; /** The deny floor of the words the walk could not place: a closed scope that lost track keeps its floor for its words. */ get floorBases(): readonly string[] | undefined; private closedFloors; /** The floor's places the walk carries ({@link floorBases} without {@link mayLead}): what it hands a walk that runs it. */ get carriedFloor(): readonly string[]; /** Where a word MAY stand that only a device spelling's drive stand-in says, or a move from one ({@link floorOf}): judged beside * the floor, never a place the walk carries — not counted toward {@link BASE_CANDIDATE_MAX}, never where an `eval` text or a * closed scope is walked from, never handed to a fork's floor. */ private mayLead; /** The candidates a reader whose `&&` run starts at `andRunStart` may stand in. */ reachable(andRunStart: number): readonly BaseCandidate[]; /** Where the text ends: every candidate, or `unmodelled`. */ end(): readonly BaseCandidate[] | "unmodelled"; /** What `PWD` / `OLDPWD` hold for a reader whose `&&` run starts at `andRunStart`. */ where(andRunStart: number): WorkingDirectoryState; /** Once the walk lost track at a line it could not follow: the twin walk ({@link twinsAfter}) — never a place the walk stands, * only the names' floor ({@link where}). `undefined`: none (lost past its bound, or a move the twin cannot follow either). */ private twins; /** `set -P` / `set -o physical`: from here on a move's `PWD` is a physical spelling. */ setPhysical(): void; /** Where the text ends the physical mode may be on (an `eval` text hands it back to the shell that ran it). */ get mayBePhysical(): boolean; /** What the walk holds before a segment that runs apart from the shell after it ({@link WorkdirScope}). */ snapshot(): WorkdirScope; /** A scope closed: what its moves did never reached the shell outside it — the walk holds again what it held before them. * The places they went stay a deny floor of a walk that later loses track ({@link scopeReached}). */ restore(s: WorkdirScope): void; /** Places a closed scope's moves went (a function body run at a call goes there, `eval`'s text closed one): as a binding's * deny floor stays wherever the binding may have run, they join the deny floor where the walk loses track. */ private scopeReached; get reached(): ReadonlySet<string>; reach(bases: Iterable<string>): void; /** The walk once more, from `at` — candidates, or `lost` where this walk's floor says —, holding what this walk holds where * it ends: its mode, the places its scopes went, whether it went to a directory it does not know. Where a text written * earlier runs later (a function body at a call after it) it is walked from there. `floor`: places a walk started where it * does not know may stand, beside this walk's floor. */ fork(at: readonly BaseCandidate[] | "lost", floor?: readonly string[]): WorkdirThread; /** Where a text this walk runs later went (`places`) is where a word it could not place may stand: they join the deny floor, * where it has one. */ widen(places: Iterable<string>): void; /** Stop placing anything from segment `at` on: the reachable bases (and a target's floor spellings, the places a closed * scope went) become the deny floor. `unsettled`: the walk no longer knows where it stands either (a move it ran a number * of times it does not count); `twin`: the twin walk starts where the walk stood ({@link twins}). */ lose(at: number, reachable: readonly BaseCandidate[], floorTargets?: readonly string[], unsettled?: boolean, twin?: boolean): void; /** Apply segment `si`'s move. */ shift(si: number, at: ShiftAt, ctx: WorkdirContext): void; /** Where one candidate goes: the places (the CDPATH entries a bare target is searched through, then the target itself), * each with the `PWD`, `OLDPWD` and stack the move leaves — or `lost`. */ private movedFrom; } /** The walk's state before a segment's moves, where the segment runs apart from the shell after it (a `( … )` group, a * pipeline member that is not the last, a background job, a function body): handed back where the scope closes. */ export interface WorkdirScope { readonly candidates: readonly BaseCandidate[]; readonly lost: boolean; readonly floors: readonly string[] | undefined; readonly physical: boolean; readonly movedAt: number; readonly twins: readonly BaseCandidate[] | undefined; } /** Does bash search `CDPATH` for this `cd` target: not an absolute one, not `.` / `..`, not one starting `./` / `../`? */ export declare function searchesCdPath(dir: string): boolean; /** The base shift of a `cd` / `pushd` / `popd` / `dirs` segment, read off its arguments as the program receives them (and * the move the plan reports for it: a `cd` target, or `unmodelled` for a stack move it cannot name), or `undefined` for * another program. `targetOf` turns the target word into its candidate (and records it as a read where the caller does). */ export declare function baseShiftOf(program: string, args: readonly Read[], targetOf: (r: Read) => Bare): { shift: BaseShift; reported?: Bare | "home" | "previous" | "unmodelled"; } | undefined; /** May this text move the base of the shell that runs it: a program of the closed set or one running text in this shell, * a computed program word, a call into one of `functions` — or text the lexer cannot read? */ export declare function textMayMove(text: string, functions: ReadonlySet<string>): boolean; /** Does this text turn the physical mode on in the shell that runs it? An `eval` text that does is no text to skip. */ export declare function textSetsPhysical(text: string): boolean; /** Does this text call one of `functions` (a program word naming one) — or is it text the lexer cannot read? */ export declare function textCalls(text: string, functions: ReadonlySet<string>): boolean; /** May a segment with these words move the base ({@link textMayMove})? */ export declare function segmentMayMove(argv: readonly LexedWord[], functions: ReadonlySet<string>): boolean; /** What the walk reads of a whole text before it walks it: the text's functions (the names a program word can be one of; * whether it IS one at a segment is the binding thread's answer, `callAt`); per segment whether it may move the base * ({@link segmentMayMove}), may call a function, runs code bodies later (`trap`, `alias`, …), turns on the physical mode * (`set -P`, `set -o physical`); whether a function body may move the base; where `cdable_vars` goes on. */ export interface TextMoves { readonly functions: ReadonlySet<string>; /** The names whose bodies the plan has not read (an `eval` text defined them). */ readonly unread: ReadonlySet<string>; readonly mayMove: readonly boolean[]; /** Segment `si` may call a function (read before the walk, to bound where bodies run). */ readonly mayCallAt: readonly boolean[]; /** Segment `si` may run a function body ({@link segmentRunsBody}): it may call one, hands a code body to run later, runs text * the plan does not read, or hands a shell a text that may (a substitution). */ readonly runsBodyAt: readonly boolean[]; /** WHERE segment `si` runs a body ({@link BodyRuns}): where it stands, and the texts it hands someone that call one there. */ readonly bodyRunsAt: readonly BodyRuns[]; readonly bodiesAt: readonly (readonly string[])[]; readonly physicalAt: readonly boolean[]; readonly cdableFrom: number; readonly bodyMoves: boolean; /** The function segment `si`'s program word names where the walk stands (`table`) — {@link functionCallOf}. */ callAt(si: number, table: BindingTable): FunctionCall | undefined; /** The segment may call a function a literal `eval` text defined: its body is not read — it may move the base and bind any name. */ callsUnread(si: number, table: BindingTable): boolean; /** The segment runs a body that may move the base where the walk cannot follow it: a call when a body may move or whose body is not read, a code body that may move or call one. */ losesBaseAt(si: number, table: BindingTable): boolean; } /** Where a segment runs a function body: `here` — where it stands (it calls one, hands a code body to run later, runs text the * plan does not read, or hands someone a text the lexer cannot read) — and `texts`: each text it hands someone that calls one, * run where THAT text's own walk stands at its call (`bash -c 'cd .. && f'` runs `f` in the parent directory). `in`: * who runs the text — this shell (a literal `eval` text), a subshell of it at the segment's place (a substitution, an * evaluated subscript), or a child shell (its literal text, with the functions this shell exported to it: `functions`). */ export interface BodyRuns { readonly here: boolean; readonly texts: readonly { readonly text: string; readonly in: "shell" | "subshell" | "child"; readonly functions: ReadonlySet<string>; }[]; } export declare function textMovesOf(defined: ReadonlySet<string>, segments: readonly LexedSegment[], argvs: readonly (readonly LexedWord[])[], inFunctionBody: (si: number) => boolean, inherited?: { readonly functions: ReadonlySet<string>; readonly unread: ReadonlySet<string>; }, exportedInherited?: ReadonlySet<string>, fedAt?: (si: number) => readonly StdinText[]): TextMoves;