@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
78 lines • 4.16 kB
TypeScript
/**
* Splits a shell command into the simple commands it would run, so the `bash` permission decision
* can judge every one of them, not only the first.
*
* The lexer follows bash's word and operator rules closely enough to find each simple command:
* `;`, `&&`, `||`, `|`, `|&`, `&` and line breaks end one; subshells `( … )`, command substitution
* `$( … )` and backticks, process substitution `<( … )` / `>( … )`, `${ … }` and `$(( … ))` and the
* bodies of unquoted here-documents are searched for the commands they run; redirections and their
* targets are dropped. Every command name is then normalized: leading assignments and reserved
* words are skipped, a path is reduced to its basename, and a wrapper (`env`, `sudo`, `xargs`,
* `bash -c`, `find -exec`, …) yields both its own name and the command it runs.
*
* Anything the lexer does not understand (an unterminated quote, a `case`, a function definition,
* a command name built by an expansion, an unknown wrapper option) makes the whole command
* `unparsed`, and the caller must not let any command-specific rule allow it.
*/
export type BashCommandAnalysis = {
readonly kind: "parsed";
readonly segments: readonly (readonly string[])[];
} | {
readonly kind: "unparsed";
};
/** One word of a simple command after quote removal. */
export interface BashCommandWord {
readonly text: string;
/** Part of the word comes from an expansion or a glob, so its runtime value is unknown. */
readonly dynamic: boolean;
/** Part of the word was quoted or escaped. */
readonly quoted: boolean;
/** The text before the word's first expansion (all of `text` for a literal word): `-$G` has `-`. */
readonly literalPrefix: string;
}
/** A wrapper (`sudo`, `env -i`, `bash -c`, `xargs`, ...) around a simple command, with its own words. */
export interface BashCommandWrapper {
readonly name: string;
readonly words: readonly BashCommandWord[];
}
/** One simple command a bash call would run, with the words the permission lines are made of. */
export interface BashCommandSegment {
/** Basename of the command word. */
readonly name: string;
/** The command word as written (`/usr/bin/pkill`, `./kill`, `kill`). */
readonly nameSpelling: string;
readonly nameIsPath: boolean;
/** The words after the name; empty for a wrapper's own entry, whose wrapped command is its own segment. */
readonly words: readonly BashCommandWord[];
/** Wrappers around this command, outermost first. */
readonly wrappers: readonly BashCommandWrapper[];
/** Commands of one pipeline (`a | b`, and every command a wrapper in it runs) share this number. */
readonly pipeline: number;
}
export type BashCommandWordsAnalysis = {
readonly kind: "parsed";
readonly segments: readonly BashCommandSegment[];
} | {
readonly kind: "unparsed";
};
/** Longer commands are not lexed at all: they are `unparsed`, so only a `*` rule can decide them. */
export declare const MAX_COMMAND_LENGTH: number;
/**
* Every simple command `command` would run, each as its normalized words (command name first), in
* source order; or `unparsed` when any part of the command is outside what this lexer understands.
*/
export declare function analyzeBashCommand(command: string): BashCommandAnalysis;
/**
* The same simple commands as `analyzeBashCommand`, with each one's words (dynamic and quoted flags kept), the
* spelling of its name, the wrappers around it and its pipeline. Each segment's `[name, ...words]` is exactly the
* line `analyzeBashCommand` returns for it.
*/
export declare function analyzeBashCommandWords(command: string): BashCommandWordsAnalysis;
/** Lexer steps the last `analyzeBashCommand` call used (for scaling tests). */
export declare function lastAnalysisSteps(): number;
/**
* Candidate command names for a command the lexer could not parse, used only so a deny rule still
* matches: every plain word, at every position, read as the start of a command.
*/
export declare function bestEffortCommandLines(command: string): string[][];
//# sourceMappingURL=bash-command-segments.d.ts.map