Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306/** Command-shape proof for read-only Bash inspection. Keeps command semantics separate from path provenance. Only command families needed by source inspection are accepted; every unknown form fails closed. @module */
import { gitIsReadOnly, } from './read-only-git-command.ts';import type { CommandInfo, } from './types.ts';
//region Command policy
/** Commands with an implemented read-only proof. */const PROVABLE_READ_ONLY_COMMANDS: ReadonlySet<string> = new Set([ 'find', 'git', 'paste', 'printf', 'rg', 'sort',]);
/** ripgrep options that execute an external helper. */const RIPGREP_EXECUTION_OPTIONS: ReadonlySet<string> = new Set([ '--hostname-bin', '--pre', '--pre-glob', '--search-zip', '-z',]);
/** GNU find actions that mutate state, execute commands, or write named files. */const FIND_NON_READ_ACTIONS: ReadonlySet<string> = new Set([ '-delete', '-exec', '-execdir', '-fls', '-fprintf', '-fprint', '-fprint0', '-ok', '-okdir',]);
/** GNU find traversal modes that can follow descendants outside proved roots. */const FIND_UNBOUNDED_LINK_OPTIONS: ReadonlySet<string> = new Set([ '-H', '-L', '-follow',]);
/** GNU sort options that select explicit output or helper program. */const SORT_NON_READ_OPTIONS: ReadonlySet<string> = new Set([ '--compress-program', '--output', '--temporary-directory', '-o', '-T',]);
/** Shell printf option that assigns variable instead of writing standard output. */const PRINTF_NON_READ_OPTIONS: ReadonlySet<string> = new Set(['-v',]);
//endregion Command policy
//region Option scanning
/** Check whether argument selects option, including attached option values. @param argument - current command argument @param option - exact short or long option name @returns whether argument uses option @example ```typescript optionMatches({ argument: '--pre=cat', option: '--pre' }); ``` */function optionMatches( { argument, option, }: { readonly argument: string; readonly option: string; },): boolean { if (argument === option) return true; if (option.startsWith('--',)) return argument.startsWith(`${option}=`,); return (option.length === 2) && argument.startsWith(option,) && (argument.length > option.length);}
/** Check arguments against blocked option names. @param args - command arguments @param blockedOptions - options that invalidate read-only proof @returns whether blocked option appears @example ```typescript hasBlockedOption({ args: ['--output=file'], blockedOptions: SORT_NON_READ_OPTIONS }); ``` */function hasBlockedOption( { args, blockedOptions, }: { readonly args: readonly string[]; readonly blockedOptions: ReadonlySet<string>; },): boolean { return args.some(function argumentUsesBlockedOption(argument,): boolean { for (const option of blockedOptions) { if (optionMatches({ argument, option, },)) return true; } return false; },);}
//endregion Option scanning
//region Family proofs
/** Prove ripgrep form cannot execute configured helper commands. @param args - ripgrep arguments @returns whether command shape is read-only @example ```typescript ripgrepIsReadOnly(['--line-number', 'needle', '.']); ``` */function ripgrepIsReadOnly( args: readonly string[],): boolean { /** Runtime environment containing optional implicit ripgrep configuration. */ const { env, } = process; if (env.RIPGREP_CONFIG_PATH !== undefined) return false; return !hasBlockedOption({ args, blockedOptions: RIPGREP_EXECUTION_OPTIONS, },);}
/** Prove GNU find expression has no mutation, subprocess, named-output, or link-following action. @param args - find arguments @returns whether command shape is read-only @example ```typescript findIsReadOnly(['/repo', '-type', 'f', '-print']); ``` */function findIsReadOnly( args: readonly string[],): boolean { if (hasBlockedOption({ args, blockedOptions: FIND_NON_READ_ACTIONS, },)) { return false; } return !hasBlockedOption({ args, blockedOptions: FIND_UNBOUNDED_LINK_OPTIONS, },);}
/** Prove GNU sort writes only standard output and invokes no compression helper. @param args - sort arguments @returns whether command shape is read-only @example ```typescript sortIsReadOnly([]); ``` */function sortIsReadOnly( args: readonly string[],): boolean { return !hasBlockedOption({ args, blockedOptions: SORT_NON_READ_OPTIONS, },);}
//endregion Family proofs
//region Public proof
/** Check whether command family has implemented positive read-only proof. @param name - parsed command name @returns whether command should enter read-proof policy @example ```typescript supportsReadOnlyProof('rg'); ``` */function supportsReadOnlyProof( name: string,): boolean { return PROVABLE_READ_ONLY_COMMANDS.has(name,);}
/** Prove parsed command shape cannot mutate files or execute nested helpers. Path scope and expansion provenance are checked by caller. @param command - parsed command record @returns whether command shape is read-only @example ```typescript commandIsReadOnly(command); ``` */function commandIsReadOnly( command: CommandInfo,): boolean { /** Command shape fields used by positive proof. */ const { args, envAssignments, name, redirects, } = command; if (envAssignments.length > 0) return false; if (redirects.some(function redirectWritesFile(redirect,): boolean { return redirect.writesFile; },)) { return false; } if (name === 'rg') return ripgrepIsReadOnly(args,); if (name === 'find') return findIsReadOnly(args,); if (name === 'sort') return sortIsReadOnly(args,); if (name === 'git') return gitIsReadOnly(args,); if (name === 'printf') { return !hasBlockedOption({ args, blockedOptions: PRINTF_NON_READ_OPTIONS, },); } return name === 'paste';}
//endregion Public proof
export { commandIsReadOnly, supportsReadOnlyProof,};