@sema-agent/core
Version:
Stateless, task-oriented AI agent core
592 lines (591 loc) • 48.4 kB
TypeScript
/**
* The shell read JUDGE and its DECISION TABLE — the one place a shell command's read candidates become a
* verdict, for every shell face (the full `Bash`, the read-only `Bash`, `Monitor`) and both moments (the
* gate station before approval, the rewrite re-judge after it).
*
* · `judgeRead` judges ONE established candidate: the deny half first, over every view the candidate has
* (its canonical target, its folded spelling, the kernel spelling of a `..` operand, the folded spelling
* of a candidate that could not be established, the hops of its leaf symlink chain), then containment over
* the canonical target alone. The
* deny judge is the boundary's own `denyMatch` closure (`readDenySeat`, read-deny.ts) — the same function
* object every read face mounts; nothing here compiles a pattern or re-derives a view set. The containment
* judge is `withinAnyRoot` over the roots the boundary declares — a WRITE target's over the write roots, at the
* seat it is written, under the write face in force ({@link judgeWrite}).
* · `foldShellReadFindings` folds a command's findings — every segment, every nested plan — to ONE word by
* the priority `identity_changed > deny_hit > target_unavailable > unanswered > outside_roots > unestablishable > declined > none`.
* · `SHELL_READ_DECISION` is the total table (finding × seat → disposition) that is the ONLY source of
* "refuse / mandated ask / ordinary ask / vouch" for a shell read — and for the file tools' read of one target, on their
* own seat (`file_tool`), so where a path may be read is one table for both stations. A seat is a FACE word, not a tool name
* and not a permission mode: which shell face the call runs on (the full shell, the read-only shell) or
* which moment judges it (the re-judge after approval). The permission mode is not read here — what a
* mode does with the ask this table mints is decided once, downstream, where every ask is arbitrated.
*
* The floor this module is (`shell.read_boundary.friction_floor`) is stated on {@link judgeRead}.
*/
import type { BashReadonlyRootBoundary } from "./bash-readonly-classifier.js";
import type { ReadCandidate, ShellReadPlan, ShellReadSegment } from "./shell-read-plan.js";
import type { Incomplete, ObservedFilesystem } from "./shell-read-environment.js";
import type { WriteSeat } from "./shell-read-write-targets.js";
import { type ContainmentFace } from "./containment-face.js";
import { type JudgedPathRow, type ReadDenyFinding, type ReadDenyRootAnswer } from "./read-deny.js";
import type { AssertAllKeysHandled } from "../../core/ask-origin.js";
/** WHAT the judge found about a command — a closed set, registered in `docs/CLOSED-SETS.md`. */
export declare const SHELL_READ_FINDINGS: readonly ["deny_hit", "outside_roots", "unestablishable", "declined", "identity_changed", "unanswered", "target_unavailable", "none"];
export type ShellReadFinding = (typeof SHELL_READ_FINDINGS)[number];
/** WHERE a read is judged — the seats the decision table is keyed on, one per FACE: `full_shell` the
* writable shell (`Bash`, `Monitor`) under every permission mode and every shell-gate floor; `readonly`
* the read-only shell face (`handsReadOnly`); `rejudge` the post-approval re-judgment of rewritten bytes
* and the pre-spawn identity re-check; `file_tool` the file tools' read station (Read / Grep / Glob — the catalog
* tools whose path target is a read), which judges one target per call — the same table, so where a
* path may be read has one reader. */
export declare const SHELL_READ_SEATS: readonly ["full_shell", "readonly", "rejudge", "file_tool"];
export type ShellReadSeat = (typeof SHELL_READ_SEATS)[number];
/** The fold priority: a command's finding is the highest-ranked finding of any candidate in it. */
export declare const SHELL_READ_FINDING_RANK: {
readonly identity_changed: 7;
readonly deny_hit: 6;
readonly target_unavailable: 5;
readonly unanswered: 4;
readonly outside_roots: 3;
readonly unestablishable: 2;
readonly declined: 1;
readonly none: 0;
};
/** Compile-time fence: `never` while every finding has a rank. */
export type RankTableCoversEveryFinding = AssertAllKeysHandled<Exclude<ShellReadFinding, keyof typeof SHELL_READ_FINDING_RANK>>;
/** What a seat does with a finding. `refuse` = a deny at the station (or a veto after a person's yes);
* `mandated` = an ask no automatic lane may clear; `ordinary` = an ask any answerer may clear (a person,
* the auto-mode classifier, a stored allow rule) but no answerless engine table may; `vouch` = the station
* has no question. */
export type ShellReadDisposition = "refuse" | "mandated" | "ordinary" | "vouch";
/** One cell of the decision table: a disposition, or — where the FORM of the question decides it — its readings: `narrowed` (raised
* under the `roots` read face: the fence the deployment narrowed, and the engine default), `open` (under the open read face, or a
* written target alone) and, where it differs, `redirect` (every containment finding is an output redirection outside the write
* roots). */
export type ShellReadCell = ShellReadDisposition | {
readonly narrowed: ShellReadDisposition;
readonly open: ShellReadDisposition;
readonly redirect?: ShellReadDisposition;
};
/** A cell, read for the form of the question ({@link ShellReadCellContext}): the redirect reading where the cell has one and every
* containment finding is a redirection, otherwise the read face's (absent ⇒ narrowed, the engine default). */
export declare function dispositionOfCell(cell: ShellReadCell, context?: ShellReadCellContext): ShellReadDisposition;
/**
* THE decision table — total over findings × seats; no cell is n/a. Row by row:
* · `deny_hit` — refused on every seat, for a spelling the station can establish: a deny row is a floor
* no root grant and no approval moves. A spelling the station cannot establish is not a deny hit (it is
* the `unestablishable` row) — the floor holds what it can place, and says so rather than guessing. A read
* of a process-private /proc file, or a tree walk rooted where such files are, is this row too, on no deny row
* ({@link ReadFinding.processPrivate}).
* · `outside_roots` — asked where a person can be asked (the full shell), refused where none can (the
* read-only face has no approval channel; a rewrite that leaves the roots after approval was seen by
* nobody). The full shell's ask is MANDATED, except for a command whose every containment finding is an OUTPUT
* REDIRECTION outside the write roots (the cell's `redirect` reading): the permission question a stored allow answers,
* the ORDINARY ask. The reference implementation's redirection check (CC 2.1.296 `lvr` (cli296.js:249645) @18502854,
* called @18508831) asks such a target as a plain permission ask that is not rule-overridable: a whole-tool `Bash` allow
* (`$pn` @18717070) and bypass (`$pn` @18716980) release it, a prefix allow does not (`vX`'s ask returns before the allow
* rules, @18567915). A command that also reads outside the roots, or writes a program operand there (`tee`, `cp`, …),
* keeps the mandate.
* · `unestablishable` — the floor could not say where the command reads, so it is not vouched anywhere: asked
* where a person can be asked (the full shell), refused where none can be asked. One line divides the full shell's cell. A
* read whose path the station does not SEE — the path is only a value or program text (a variable whose value the text does
* not hold, a substitution's output, an interpreter's inline program, a lost working directory, a re-entered shell), or sits
* in text the lexer does not read — and a written target alone are the ORDINARY ask on either read face: the reference
* implementation's plain permission ask (CC
* 2.1.296 `sR` (cli296.js:242691) @13116941, reason `other`), which a whole-tool allow (`N9` (cli296.js:249675) @18717070), bypass (`$pn` (cli296.js:249675) @18716980)
* and the auto-mode classifier release and a prefix allow does not: the engine does not know where such a command reads,
* which is no finding that it reads outside. The cell's NARROWED reading (the `roots` face, the engine default: the
* deployment asked to be asked) — the MANDATED ask no automatic lane answers — is for a read the station can ASK THE
* ENVIRONMENT about: the path is a word of the command and where it lands the environment answers — a word placed outside the
* roots, kept even where the fold does not let it win (in a re-entered or printed text), and a placed word whose landing the
* environment left unresolved ({@link EstablishedRead.unresolved}: a lookup that could not resolve it, or a `~user` word,
* which only the account database answers), the environment's answer missing as it is for `unanswered`
* ({@link ShellReadFold.outsideOrUnresolved}).
* · `declined` — the one deliberate fail-open of this table: the scan could not read the program bash
* will run, or the operands arrive on a stream. On the full shell it is an ORDINARY ask — an answerer
* may settle it, and an out-of-root read hidden inside such a command is then not asked again. The
* read-only face refuses: its allowlist is a deployment knob, so this cell is reachable and must not be
* n/a.
* · `identity_changed` — a candidate established at approval time resolves elsewhere at spawn time:
* refused everywhere (it is only ever produced on the `rejudge` seat).
* · `unanswered` — the judge did not FINISH: it threw or timed out, the lookup it stands on was absent, or — recorded at the
* one door to the environment ({@link import("./shell-read-environment.js").Incomplete}) — an environment lookup gave no
* answer (an error that is no conclusive "not there"), the station stopped at its own bound (more directories or names than
* it enumerates, a link chain past its hop bound), or a home view could not be read — or a recursive read's root may hold a
* home-anchored deny row that no home places (the environment declares none, or its placement did not answer), a home the
* station cannot place either. Any one makes the whole command
* `unanswered`, whatever the other words found. Not a fact about the command but the state of the station, so it is not
* the `unestablishable` row's question (a text the station cannot place — a word the shell rewrites, a lost working
* directory, a pattern its matcher does not model). Absence is not
* "nothing there": the full shell asks it MANDATED on every read face — a standing allow was given on the premise that the
* judge works, and a broken judge is not cleared by it — and the read-only face refuses.
* · `target_unavailable` — the target refused a lookup before it started (the door's {@link import("./shell-read-environment.js").Incomplete.refusal}:
* offline, busy, not bound — nothing started, nothing can run there now, a retry is safe once it is back). Not the silence: the
* environment answered, for the whole command. Refused on every seat with the environment's own sentence — the refusal the Bash
* tool gives the same word at its start — and asked of nobody: a person's yes would let through a command no station judged,
* to run if the target opened in between.
* · `none` — vouched: the station has no question (the full shell's shape tier may still ask).
*
* The FILE TOOLS' column (`file_tool`) is the one question that station puts — a target outside the read roots, the ORDINARY
* ask (a person, a stored path allow, the auto-mode classifier or a permission mode answers it; the card offers the directory)
* — and the tool's own refusal for every other finding: a file tool reads one named target, so a target it cannot place, a
* deny-listed one or one that resolves elsewhere by the time it reads is refused there, and is never a question. Its
* out-of-root word differs from the full shell's (`ordinary` beside `mandated`): the one place the two stations answer one
* target differently, written here rather than in either station.
*/
export declare const SHELL_READ_DECISION: {
readonly deny_hit: {
readonly full_shell: "refuse";
readonly readonly: "refuse";
readonly rejudge: "refuse";
readonly file_tool: "refuse";
};
readonly outside_roots: {
readonly full_shell: {
readonly narrowed: "mandated";
readonly open: "mandated";
readonly redirect: "ordinary";
};
readonly readonly: "refuse";
readonly rejudge: "refuse";
readonly file_tool: "ordinary";
};
readonly unestablishable: {
readonly full_shell: {
readonly narrowed: "mandated";
readonly open: "ordinary";
};
readonly readonly: "refuse";
readonly rejudge: "refuse";
readonly file_tool: "refuse";
};
readonly declined: {
readonly full_shell: "ordinary";
readonly readonly: "refuse";
readonly rejudge: "refuse";
readonly file_tool: "refuse";
};
readonly identity_changed: {
readonly full_shell: "refuse";
readonly readonly: "refuse";
readonly rejudge: "refuse";
readonly file_tool: "refuse";
};
readonly unanswered: {
readonly full_shell: "mandated";
readonly readonly: "refuse";
readonly rejudge: "refuse";
readonly file_tool: "refuse";
};
readonly target_unavailable: {
readonly full_shell: "refuse";
readonly readonly: "refuse";
readonly rejudge: "refuse";
readonly file_tool: "refuse";
};
readonly none: {
readonly full_shell: "vouch";
readonly readonly: "vouch";
readonly rejudge: "vouch";
readonly file_tool: "vouch";
};
};
/** Compile-time fence: `never` while every finding has a row. */
export type DecisionTableCoversEveryFinding = AssertAllKeysHandled<Exclude<ShellReadFinding, keyof typeof SHELL_READ_DECISION>>;
/** Compile-time fence: `never` while every seat has a cell on the `none` row (the `satisfies` above holds
* every other row to the same shape). */
export type DecisionTableCoversEverySeat = AssertAllKeysHandled<Exclude<ShellReadSeat, keyof (typeof SHELL_READ_DECISION)["none"]>>;
/** The disposition of one command-level finding on one seat — the table, read. A seat word outside
* {@link SHELL_READ_SEATS} (an untyped caller, or one still passing a retired word) is refused loudly: an
* absent disposition would read as "no verdict", which no caller can act on safely. */
export declare function decideShellRead<F extends ShellReadFinding>(finding: F, seat: ShellReadSeat, context?: ShellReadCellContext): RowDisposition<F>;
/** The dispositions the table's row for `F` holds — on any seat, under any form of the question (the table's own literal words). */
export type RowDisposition<F extends ShellReadFinding> = CellWords<(typeof SHELL_READ_DECISION)[F][ShellReadSeat]>;
/** What the table is read for beyond (finding, seat): the two facts of a judged command a cell can depend on. */
export interface ShellReadCellContext {
/** Every containment finding is an output redirection outside the write roots (a cell's `redirect` reading). Absent ⇒ no. */
readonly redirectsOnly?: boolean;
/** The question was raised under a NARROWED (`roots`) read face about a command holding a word placed outside the roots or a
* read the environment left unresolved ({@link ShellReadFold.outsideOrUnresolved}); `false` for the open face and for a
* command whose unplaced words are the text's own or written targets alone. Absent ⇒ narrowed, the engine default. */
readonly narrowedReadFace?: boolean;
}
/** Every disposition the table gives `finding` — on any seat, under any form of the question; empty for a word outside
* {@link SHELL_READ_FINDINGS}. A disposition outside it is no reading of the table: what a reader of a station's answer
* checks the answer against. */
export declare function dispositionsOfRow(finding: string): ReadonlySet<ShellReadDisposition>;
/** The words a cell can read as. */
type CellWords<C> = C extends string ? C : C[keyof C];
/** The findings whose row the table vouches on, on any seat and under any form. */
type FindingsTheTableVouches = {
[F in ShellReadFinding]: "vouch" extends RowDisposition<F> ? F : never;
}[ShellReadFinding];
/** Compile-time fence: `never` while `vouch` is a cell of the `none` row alone — what lets a vouch carry the finding `none` and
* nothing else ({@link import("./shell-read-check.js").ShellReadVerdict}). */
export type VouchOnlyOnTheNoneRow = AssertAllKeysHandled<Exclude<FindingsTheTableVouches, "none">>;
/**
* The boundary a shell read is judged against: the declared roots (canonical), the working directory and
* home the plan's bases start from, the deny seat (the SAME `denyMatch` closure every read face mounts),
* the containment face, and — for the read-only face — the engine's own overflow-spool fence as one more
* member of "inside" (`alsoInside`), asked once per target that landed outside the roots.
*/
export interface ShellReadBoundary extends BashReadonlyRootBoundary {
readonly alsoInside?: (canonicalTarget: string) => Promise<boolean>;
/** A partition of the rules the deny seats are compiled from could not be read: a guard of unknown reach. The station
* guards ({@link stationGuards}) and vouches for nothing against this boundary — what it would vouch for is answered as
* a judgment it could not make. */
readonly unreadPartitions?: true;
/** The deny table's WRITE face (`compileWriteDeny`, read-deny.ts): judges a command's write targets (candidates marked
* `face: "write"`) and returns the matched row's rule, or null. PRESENT-IFF there is something to judge. */
readonly writeDenyMatch?: (resolvedPath: string) => string | null;
/** Does the row `writeDenyMatch` named (its rule) guard the READ face too — a `Read(…)` rule on the write face
* ({@link import("./read-deny.js").WriteDenyMatcher.guardsReads})? Read once per hit, so the refusal says which family refused. */
readonly writeDenyGuardsReads?: (pattern: string) => boolean;
/** The write table's rows named by or below a subtree removal/rename target (`WriteDenyMatcher.rowUnderRoot`). */
readonly writeDenyUnder?: (resolvedPath: string) => ReadDenyRootAnswer;
/** The write table's unanchored rows (`WriteDenyMatcher.unanchoredRows`, never empty here) and the first of them whose leading
* name one directory entry name matches (`WriteDenyMatcher.rowLedByName`): what the station searches a recursive removal's
* subtree for, under its own bound ({@link import("./shell-read-check.js").SUBTREE_SEARCH_MS}). PRESENT-IFF such a row. */
readonly writeDenyNamed?: {
readonly rows: readonly string[];
readonly ledBy: (entryName: string) => string | null;
};
/** The write table's rows AT or BELOW a path (`WriteDenyMatcher.rowAtOrBelow`): read by `writesPlaced` via {@link denyRowAtOrBelow}. */
readonly writeDenyAtOrBelow?: (resolvedPath: string) => string | null;
/** The read table's rows AT or BELOW a path (`readDenySeat`'s `denyAtOrBelow`, read-deny.ts): read by the station's
* `writesPlaced` fact only, through {@link denyRowAtOrBelow}. */
readonly denyAtOrBelow?: (resolvedPath: string) => string | null;
/** The path rows for the READ face beside the deny table (the run's tables, `readFaceRows`) — one order for every seat:
* deny (the table, judged first) > ask > allow. `allow` — a person's stored `Read(<path>)` / `Edit(<path>)` allows (a
* `Read(//dir/**)` allow is a read ROOT instead; none while a seat beyond this boundary stands over reads, nor on a leg that
* refuses reads outside its directories) — makes an out-of-root READ inside where one row admits EVERY view of it
* and the read resolved (the narrow reading the gate's path lane gives the Read tool); `ask` — the `Read(<path>)` asks of
* the person's store and of the task's permission policy (`policy: true`: a deployment's or a caller's), also where their
* heads lead — is met by ANY view of any word the deny judgment reads, inside the roots and on the open face too
* ({@link ReadFinding.ask}), whatever allow admits it. A whole-tool `Read` allow is not a member: it speaks for the Read tool alone. PRESENT-IFF a
* row of either. */
readonly readRuleRows?: {
readonly allow: readonly JudgedPathRow[];
readonly ask: readonly ReadAskRow[];
};
/** The path ask rows of the WRITE face (the person's stored `Edit(<path>)` asks, also where their heads lead) — a member of
* its own, never one of {@link readRuleRows}: met by a target the command's program WRITES ({@link EstablishedRead.written}),
* on any view the deny judge reads of it, and — for a subtree it removes or moves — by a row lying at or below it, as the
* write deny rows are ({@link ReadFinding.writeAsk}). A FACT the station reports, the same under every permission mode; it
* is no guard of the station ({@link stationGuards} and {@link writeFaceGuards} do not count it), so it raises no finding
* of its own. PRESENT-IFF such a row. */
readonly writeAskRows?: readonly ReadAskRow[];
/** {@link writeAskRows} also where their heads lead, looked up when asked — handed only under a mode that counts the rows: the
* station asks it once the command is found to WRITE something, and reads the fact again over its answer
* ({@link withWriteAskRows}); a lookup that fails fails the judgment (the station's own fail-closed answer). */
readonly writeAskRowsLed?: () => Promise<readonly ReadAskRow[]>;
/** The WRITE roots (canonical) — the task root and every additional working directory, never a read-only grant: where a
* command's write targets must land ({@link judgeRead}'s write face: a target written outside them is an
* `outside_roots` finding), and so where a mode that accepts file commands may place them. Absent ⇒ where a write may
* land is unknown: a written target is `unestablishable` and nothing is placed. */
readonly writeRoots?: readonly string[];
/** The WRITE face in force — the same seat the structured write tools read (the run's resolved write face, read once per
* judgment where the boundary is built): `"open"` skips the write roots' containment for a finding, as it skips the
* structured tools' root fence ({@link skipsRootsContainment}; absent ⇒ roots). Placement still reads the write roots:
* a mode accepting file commands accepts them inside its own directories, whatever the face. */
readonly writeFace?: ContainmentFace;
/** The leg's ONE write judgment of an ABSOLUTE write target — the one a write tool's target meets (its spelling, its real
* target and every hop of its symlink chain, under the leg's one exempt root), over the write-protection TABLE and the
* deployment's guarded write rows (`ToolPolicy.writeGuardRows`, every policy of the leg's chain) together; `canonical` ⇔
* the path is a key the station established (judged as a key, never as a spelling). `"protected"`: the table covers it —
* the target is not placed and the verdict says so ({@link import("./shell-read-check.js").ShellReadVerdict.writeProtected});
* `"guarded"`: a deployment row covers it — the target is not placed; `undefined`: nothing covers it. Read by the
* station's write facts only. A judgment that throws fails the station's answer (the caller's fail-closed arm). Every lookup
* it makes goes through `fs` — the command's door — and through no environment of its own: the station's one decision about
* what a silence means reads the answers the judgment got as well as the ones it did not. `spellingOnly` ⇔ judge the path as
* written and nothing else — the table's spelling view, no lookup, no deployment row: `"protected"` when the text itself names
* a row of the table (what the station tells a written target's two readings apart by,
* {@link import("./shell-read-check.js").ShellReadVerdict.writeProtectedOnDisk}). */
readonly writeTargetJudge?: (target: string, opts: {
readonly fs: ObservedFilesystem;
readonly signal?: AbortSignal;
readonly canonical?: true;
readonly spellingOnly?: true;
}) => Promise<"protected" | "guarded" | undefined>;
}
/** How far one candidate got before it was judged. `lexical` = the folded spelling is the judged target
* (an isolated sandbox, or a resolver that declares no authority and did not answer); `canonical` = the
* environment resolved it; `expansion` = one path a pattern expanded to (a traversal root is never listed below);
* `unestablishable` = the floor could not say where it lands (`cause` says why). */
export type ReadEstablishment = "lexical" | "canonical" | "expansion" | "unestablishable";
export interface EstablishedRead {
readonly candidate: ReadCandidate;
readonly established: ReadEstablishment;
/** The judged target: an absolute path (canonical where established so, the folded spelling otherwise).
* Absent exactly when `established === "unestablishable"`. */
readonly target?: string;
/** Every spelling the deny judge sees BESIDE the target: the folded spelling, the kernel spelling of a
* `..` operand, the pattern an expansion came from, the placeholder-carrying fold of a substitution. */
readonly spellings: readonly string[];
/** The members of {@link spellings} a reading of the FILESYSTEM produced rather than the command's text — the home's view of
* a landed target (read-deny-home.ts), added so the deny judge places a home-anchored row wherever the target really is; the
* name of a GNU sed `--follow-symlinks` backup when the operand's links supplied it. Every other spelling is the command's
* own text (folded, or as the kernel walks it). Absent ⇒ none. */
readonly derivedSpellings?: readonly string[];
/** The hops of the landed target's LEAF symlink chain — each an unresolved spelling some link on the way wrote — where the
* program follows the target's links (the landing seat): the views a call's target reading judges beside the canonical
* and the lexical one, read through the same chain reader. Deny views only: not a spelling the
* command wrote, not a containment candidate, not a write view of its own. Absent ⇒ none read. */
readonly hops?: readonly string[];
/** Beside an unplaced read whose start may be any directory at or above its floor — a word made from a directory the text
* does not know ({@link ReadCandidate.unsettledStart}), an object path relative to a repository top it does not settle
* ({@link ReadCandidate.repositoryTop}) —: the spelling at each directory above the floor. Views of the word itself, met by
* the deny rows' match and by nothing else: not where the text stood (that is {@link spellings}, which the removal guard
* judges), not a containment candidate, and not a place a row may lie below (a recursive read's root, a moved or copied
* target) — each reaches `/`, below which every row lies. Absent ⇒ none. */
readonly ancestorViews?: readonly string[];
/** The pattern or traversal root an `expansion` came from, as folded. */
readonly origin?: string;
/** What the environment said the target IS, where a read asked — only a traversal root asks (it lists the root
* once; a link's answer is its target's). Absent = nothing asked; a plain read never lists. */
readonly node?: "directory" | "file";
/** Why the candidate could not be established. */
readonly cause?: string;
/** The candidate's spelling WAS placed and the environment could not resolve where it leads (the whole chain unanswered by
* a resolver that declares itself authoritative, a component it could not follow, or the directory a pattern expands in that
* it could not resolve or list; for a READ, also a `~user` word, which only the account database places) — the reading's
* `target_unresolved` fact, as opposed to a text the station cannot read (a rewritten word, an unsettled working
* directory). Present only on an `unestablishable` read. (That the environment gave no answer is the establishment's
* `incomplete`, which makes the command `unanswered` whether or not a candidate carries this mark.) */
readonly unresolved?: true;
/** WHERE the command's program writes this target — present only on a write target the program itself writes, at the
* seat this read was established at (`landing`: through the path's links; `entry`: the directory entry, a link there
* not followed). Absent on a read, and on a write-face target the program only names (a copy's source). `child`: the
* candidate's own mark ({@link import("./shell-read-write-targets.js").JudgedCandidate.child}), carried to the read. */
readonly written?: {
readonly seat: WriteSeat;
readonly child?: true;
};
}
/** What the judge found about ONE candidate. `kind` is one of the five candidate-level findings (`declined` is a segment fact,
* `identity_changed` a re-establishment fact — neither is a candidate's; `unanswered` is a fact of the whole establishment, and
* a candidate's where a recursive read's root may hold a deny row no home places, {@link judgeRead}). */
export interface ReadFinding {
readonly read: EstablishedRead;
readonly kind: Extract<ShellReadFinding, "deny_hit" | "outside_roots" | "unestablishable" | "unanswered" | "none">;
/** The deny row that matched, the view it matched on, and — for a write target — the face (`write`) that refused. `under`: the
* view is a recursive read's root and the row lies BELOW it (the root holds the row's location; the root itself matches no
* row) — judged on locations, so nothing says a file is there. */
readonly deny?: {
readonly pattern: string;
readonly matchedView: string;
readonly face?: "write";
readonly readRow?: true;
readonly under?: true;
readonly lookup?: ReadDenyFinding["lookup"];
};
/** A `deny_hit` on no row: the read's target is a process-private /proc file (`isProcSensitiveFile`, safety.ts — the set the
* file read tools refuse), or a tree walk's root holds such files (`holdsProcSensitiveFile`); the view it was matched on. */
readonly processPrivate?: {
readonly matchedView: string;
};
/** An `unanswered` finding whose reason is that a deny row could not be asked about the read (a recursive read's root may hold a
* home row no home places; a spelling passes the floor's bound, so it may name a value no row was asked about) — what the
* station could not check is the deny list, not where the read lands. The account's closing clause reads it. */
readonly denyRowUnasked?: true;
readonly containment: "inside" | "outside" | "open" | "unknown";
/** The ask row this READ meets ({@link ShellReadBoundary.readRuleRows}): the rule's text, and `policy` when the row is the
* task's permission policy's. Beside any kind but a deny hit — the command's verdict carries it as a fact, and the gate
* asks it as that rule's own question. */
readonly ask?: {
readonly rule: string;
readonly policy?: true;
};
/** The write-face ask row ({@link ShellReadBoundary.writeAskRows}) this WRITE target meets: the rule's text, and `policy` when
* the row is the task's permission policy's. Beside any kind but a deny hit — a fact, which no finding of this read carries
* for it. */
readonly writeAsk?: {
readonly rule: string;
readonly policy?: true;
};
}
/** An ask row of a path face (the read face's, or the write face's): a path row, marked `policy` when the task's permission
* policy carries it (a deployment's or a caller's) rather than the person's store — the seat the question names. */
export type ReadAskRow = JudgedPathRow & {
readonly policy?: true;
};
/**
* Judge ONE established candidate. Deny first, over every view (a hit on any view decides — a symlink whose
* SPELLING names a guarded path is refused even when its target is elsewhere, and a substitution's folded
* spelling is judged although the candidate could not be established); containment over the canonical
* target alone, and only where the boundary's face closes it.
*
* @contract shell.read_boundary.friction_floor — what this judge answers is a question about TEXT and the
* FILESYSTEM the command will run in: which files the words of a command may name, resolved against the
* declared roots, the declared home and the observed working directory, and — where the environment can
* answer — where those spellings really land (symlinks resolved, patterns expanded within a bound). A
* recursive read's root is one of those words: it is judged by what the command NAMES (the root itself and the
* placed rows the root contains), never by a tree walked below it. It is not a question about what the
* process will open. A deployment's containment for what a shell may reach is the `ExecutionEnv` the
* command runs in (a sandbox, a container, a remote adapter); everything here is the friction floor under
* that containment, and the read-deny table this station judges with travels to that environment on every
* command start as `readFloor` (an environment that confines processes keeps it or refuses the start). Under
* an isolated sandbox the floor is lexical (the sandbox is the boundary and its filesystem is not this
* process's to resolve). A deny-listed read is REFUSED on every seat; an out-of-root read asks where a
* person can be asked and is refused where none can; a read the floor cannot establish (an expansion it
* cannot finish, a spelling the shell rewrites, a working directory it cannot place) is asked or refused
* the same way — never vouched. Containment judges what a READ program opens: a program path is executed,
* not disclosed, and a leading environment word's value reaches the program through its environment, so
* neither is a containment candidate — both still meet the deny judge, on every spelling.
*/
export declare function judgeRead(read: EstablishedRead, boundary: ShellReadBoundary): ReadFinding;
/**
* Does a deny row lie AT or BELOW this write target, on any view the judge reads? Then a command that moves, copies or
* removes the target changes what the row covers, and a mode must not answer it unasked. WHICH tables answer is what the
* command does to the content below the target: a move or a copy carries it somewhere else (`mv .docker d` leaves
* `d/config.json` outside `.docker/config.json`; `mv <home> x` takes every home-anchored row's file along), so the READ
* table's rows answer too (`carriesContent`); a removal carries nothing anywhere — it is a write and nothing else, and
* only the write table answers it (the reference implementation's path check reads the read rows for a copy's or a
* move's sources and for sed, never for `rm` / `rmdir`). The read table answers per row (`denyAtOrBelow`); the write
* table's rows are rule rows, whose reach below a path is not enumerable, so any of them answers.
*/
export declare function denyRowAtOrBelow(read: EstablishedRead, boundary: ShellReadBoundary, carriesContent: boolean): boolean;
/**
* The deny floor of every home word the plan does not place: each candidate carrying a `floorSpelled` (the word
* spelled with the DECLARED home) gets a SHADOW candidate at that spelling, established exactly as a read is — the
* canonical target, a pattern's matches (a glob the word carries unquoted), a traversal root's tree — and judged for
* deny rows only (`denyOnly`). A deny row is a floor the text's rebinding may never reach past; the shadow adds no
* other finding, so a read that is not a deny row still asks through its own unplaced candidate.
*/
export declare function denyFloorShadowsOf(candidates: readonly ReadCandidate[]): ReadCandidate[];
/** One segment's judged reads and declines, with its nested plans judged the same way. */
export interface JudgedSegment {
readonly segment: ShellReadSegment;
readonly findings: readonly ReadFinding[];
readonly nested: readonly JudgedPlan[];
/** What a literal printer says the segment's shell is fed ({@link ShellReadSegment.printed}), judged on its spelling alone
* (no environment is asked about a text that may not run): the fold reads its deny hits and nothing else; no other reader
* of a judged plan reads it. */
readonly printed: readonly JudgedPlan[];
}
export interface JudgedPlan {
readonly plan: ShellReadPlan;
readonly segments: readonly JudgedSegment[];
/** {@link stationGuards} of the boundary the plan was judged against — what a READ finding folds by. */
readonly guards: boolean;
/** {@link writeFaceGuards} of the same boundary — what a WRITE target's finding folds by. */
readonly writeGuards: boolean;
/** What the command could not finish (the station's one door, decided after the write guards asked); on the top plan only. */
readonly incomplete?: readonly Incomplete[];
}
/**
* Does the station GUARD anything for this boundary — a closed face (roots: containment is what it guards; an absent
* face reads as roots, as {@link containmentOf} does), or a deny table with rows (`denyMatch` is present exactly then),
* or rows it could not read (`unreadPartitions`: a guard of unknown reach), or an ask row (a read it cannot place may
* be the read that row asks about)?
* Where it guards nothing (an open face, no deny row, no ask row), a read it could not establish has nothing to be confirmed
* against: no finding. The deny judgment itself is untouched (with no row there is nothing to hit). The READ face's
* question only: a write target folds by {@link writeFaceGuards}.
*/
export declare function stationGuards(boundary: Pick<ShellReadBoundary, "face" | "denyMatch" | "unreadPartitions" | "readRuleRows">): boolean;
/**
* Does the station guard anything on the WRITE face — the same shape for the write seats: the write face in force runs
* the roots-containment step ({@link skipsRootsContainment}, the structured write tools' reading; absent ⇒ roots), or
* the write deny table has rows. Where it guards nothing (an open write face, no write row), a write target the station
* could not establish has nothing to be confirmed against — as a structured write under that face is not fenced.
*/
export declare function writeFaceGuards(boundary: Pick<ShellReadBoundary, "writeFace" | "writeDenyMatch">): boolean;
/** Does the station guard anything on EITHER face? What a finding about the text's own structure folds by — a text the
* lexer could not read, a program it cannot name, program text it does not read, a working directory it lost, a text the
* shell re-enters — since what such a text reads AND what it writes are both unknown. */
export declare function anyFaceGuards(boundary: Pick<ShellReadBoundary, "face" | "denyMatch" | "unreadPartitions" | "readRuleRows" | "writeFace" | "writeDenyMatch">): boolean;
/** The command-level fold and the finding that produced it. `witness` is absent for `none`; for `declined`
* the segment that declined stands in for a candidate. */
export interface ShellReadFold {
readonly finding: ShellReadFinding;
readonly witness?: ReadFinding;
readonly declined?: ShellReadSegment;
/** EVERY segment of the text that re-enters a shell (`sh -c S`, `eval S`, a launcher handing text to a shell, a shell fed on its
* standard input) and so is at least `unestablishable`, in text order — `declined` holds only the first finding of the rank, and the
* account of this question names each program, not the first. Present only with an `unestablished` fold. */
readonly reentered?: readonly ShellReadSegment[];
/** What could not be settled, in words, for a finding that has no witness read or declined segment to say it: for `unanswered`
* the first thing the establishment recorded ({@link Incomplete.detail}); for `target_unavailable` the environment's own sentence
* for its refusal ({@link Incomplete.refusal}, as the door read it); for an `unestablishable` text the plan refused
* ({@link ShellReadPlan.unreadable}) the sentence the plan itself carries — a construct the lexer cannot close, a word whose
* value the text does not hold, a value a parameter expansion re-reads at run time. */
readonly cause?: string;
/** Some read of the text could not be established (an `unestablishable`, `unanswered` or `declined` finding anywhere, winning or not). */
readonly unestablished?: true;
/** Some word anywhere — in a re-entered or printed text too, which vouch for nothing but still name what they name — is one the
* station PLACED outside the roots, or a READ whose landing the ENVIRONMENT could not resolve ({@link EstablishedRead.unresolved},
* a `~user` word among them). A path the station does not see — a value, program text, text the lexer does not read (a
* rewritten word, a substitution, a lost working directory, a re-entered shell text, a parameter's default text) — is
* neither. The cell of the row reads it with the read face in force ({@link decideShellRead}). */
readonly outsideOrUnresolved?: true;
/** The first write-face ask row a written target meets ({@link ReadFinding.writeAsk}), in the fold's own walk — every segment, every
* nested plan, and what a literal printer says the shell is fed (a text that may run names what it names, as its deny hits do). */
readonly writeAsk?: NonNullable<ReadFinding["writeAsk"]>;
}
/**
* Fold a judged plan to ONE finding by {@link SHELL_READ_FINDING_RANK}, over every segment and every nested
* plan. Where the station guards nothing an `unestablishable`, `unanswered`, `target_unavailable` or `declined` finding is no finding:
* there is nothing to confirm it against (a call the target refused then meets the refusal at its start, in the same sentence). Which guard is asked is the face the finding is about — a read candidate's by the read face's
* ({@link JudgedPlan.guards}), a write target's by the write face's ({@link JudgedPlan.writeGuards}), and a finding about
* the text's own structure (no candidate stands in for it) by either ({@link anyFaceGuards}). Two structural rules ride the fold:
* · a plan the lexer refused (`unreadable`) is `unestablishable`, unconditionally — a quote-blind re-read
* yields more words, never zero, so an unreadable text is never "no candidates";
* · a SHELL re-entry segment (`sh -c S`, `eval S`, `env -S 'S'`, a launcher handing text to a shell) is at
* least `unestablishable` — what runs is text the shell parses, not this plan — and its nested plan may
* only RAISE that: the nested text's deny-relation answers count — a deny hit (a refusal is a refusal whatever
* runs it) and a root that may hold a row no home places — and so does a removal's target placed outside the write roots;
* its other containment and its other silence do not (an inner `cat inside.txt` vouches nothing about the outer text). A substitution body (`$(…)`, a backtick, `<(…)`) is nested
* WITHOUT re-entry and folds in full, as does a command line a program hands on ({@link ShellReadPlan.handed}: `find …
* -exec rm -rf {} +` writes where `rm -rf <start>/*` does) — beside its segment's own `unestablishable`. What a literal printer says the shell is fed ({@link JudgedSegment.printed}) is a
* text that may not run at all: its deny HITS count, and nothing else it finds or fails to find does.
*/
export declare function foldShellReadFindings(judged: JudgedPlan): ShellReadFold;
/** Does any target of the judged plan — a segment's, a nested plan's, a printed text's — get WRITTEN by its program? */
export declare function writesAnything(judged: JudgedPlan): boolean;
/** The same judged plan with every written target's write-face ask fact read again over `rows` ({@link writeAskOf}) — where the fact
* is a finding's at all (a target the judge reached: no deny hit, no judgment the deny rows could not finish); nothing else moves. */
export declare function withWriteAskRows(judged: JudgedPlan, rows: readonly ReadAskRow[]): JudgedPlan;
/** Judge every established read of a plan, in place. `establish` runs per segment so a caller can bound it. */
export declare function judgeEstablishedPlan(plan: ShellReadPlan, established: ReadonlyMap<ShellReadSegment, readonly EstablishedRead[]>, boundary: ShellReadBoundary, nestedOf: (plan: ShellReadPlan) => JudgedPlan, printedOf: (plan: ShellReadPlan) => JudgedPlan): JudgedPlan;
/** Every finding of a judged plan, flattened (segments and nested plans alike). */
export declare function allFindings(judged: JudgedPlan): ReadFinding[];
/** One lexical step up, family-aware. A family ROOT is its own parent — the fixpoint the climb below ends
* on, and the one spelling this file can recognise as "the whole filesystem" without I/O. */
export declare function parentDirLexically(p: string): string;
/**
* The deepest directory containing both, or `undefined` when they share none (two path families).
*
* LINEAR, and that is the point: both paths are already folded, so the answer is their common prefix cut
* at a separator. Climbing one directory at a time re-folds the whole remaining path each step — the same
* answer at quadratic cost, measured at 4.9 s of synchronous work for one 32 KB command naming a
* 16 000-segment operand. The family-aware comparator keeps the last word: a prefix it cannot confirm
* contains both paths yields no candidate.
*/
export declare function deepestCommonDir(a: string, b: string): string | undefined;
/**
* The ONE directory whose addition to the declared roots would answer every `outside_roots` finding of a
* command — offered only when re-judging the same established reads with that root added leaves no
* finding at all (MEASURED, not argued: a deny row or an unestablishable candidate beside the out-of-root
* one leaves the ask standing whatever root is granted, and then nothing is offered; so does a target written outside
* the WRITE roots, which no read root answers — a read grant grants no write). `exact` ⇒ the value
* is the one path the command NAMED, offered verbatim, because the containing directory would hand out the
* home or a root. A `..` spelling gets no offer: the folded and the kernel reading of it can disagree about
* whether the target is back inside, which no replay of the text settles.
*/
export declare function readRootGrantCandidateOf(judged: JudgedPlan, boundary: ShellReadBoundary): {
readonly dir: string;
readonly covers?: "exact";
} | undefined;
/**
* The grant a read-root card offers for the directory `candidate` it would add. A directory that would hand out more
* than was read is not offered: a family root grants the whole filesystem, and one CONTAINING the declared home or a
* root grants the workspace. ONE fallback for both rather than no offer at all — the single path that WAS read
* (`single`), named VERBATIM (`covers: "exact"`); with several paths read, no offer.
*/
export declare function grantOfferFor(candidate: string, single: string | undefined, bounds: Pick<ShellReadBoundary, "roots" | "homeDir">): {
dir: string;
covers?: "exact";
} | undefined;
/** The same judged plan under another boundary — the established reads are kept, only the judgment reruns. */
export declare function rejudge(judged: JudgedPlan, boundary: ShellReadBoundary): JudgedPlan;
export {};