SLOPSHOPPER

arrester

After a hook, deny rule, or auto mode blocks a tool call, Arrester stops later calls that reach the same result another way or read or change your guards…

guardcommandstatustimer
v1.2.1MITupdated 2026-10-24dot-agi/arrester
A shopper browsing a rack in a slop shop
Preview · a replayed session in a sandbox
claude · ~/work/app · arrester
› fix the failing auth test and add an audit log call ⏺ Read(src/auth.ts) ⎿ Read 6 lines ⏺ Update(src/auth.ts) ⎿ Added 2 lines, removed 1 line ⏺ Bash(bun test) ⎿ 3 pass, 1 fail ● Done. refresh now rejects expired claims and logs an audit event. ✻ Worked for 42s · done 4:20 PM › /arrester ⎿ arrester: response: deny · a block stays open 30 min after it opens or after its last target or retry match ⎿ arrester: ⎿ arrester: Open blocks (0) ⎿ arrester: ⎿ arrester: Held calls (0) ⎿ arrester: ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── › ? for shortcuts
README

<img src="https://img.shields.io/badge/Claude_Code-2.1.296%2B-D97757" alt="Claude Code 2.1.296+"> <img src="https://img.shields.io/badge/type-mod-555555" alt="mod"> <a href="LICENSE"><img src="https://img.shields.io/badge/license-MIT-555555" alt="License: MIT"></a> <img src="https://img.shields.io/badge/version-1.2.1-555555" alt="Version: 1.2.1">

Terminal: a deny rule blocks cat notes.txt. Claude tries head -n 5 notes.txt, and Arrester stops it as a workaround of block b1.

A deny rule blocks cat. Arrester stops the head detour.

Why

A guard can block one tool call while a different tool still reaches the same file, as reported in Claude Code issue #29709. Arrester remembers supported blocks from a settings hook, a deny rule, auto mode, or your own “No” in the permission dialog, within the capture limits, and checks later calls against them. A safe alternative that matches none of Arrester's rules goes on to your guards. A recognized detour to the blocked target stops.

Features

  • Follows the guards you have. Uses the blocks of your existing hooks, deny rules, and auto mode, so you write no new rules.
  • Matches across tools. Recognizes common shell rewrites and file-tool calls that reach a blocked path.
  • Protects your guards. While a block from a hook, rule, or classifier is open, stops recognized reads or changes of guard files, and claude commands that can turn guards off.
  • Checks subagents. Applies the same rules to tool calls from subagents in the conversation.
  • Explains each stop. Tells Claude which block the call matched and why. /arrester shows the open blocks and the recent decisions.
  • Denies or asks. Refuses a match at once, or holds it until you answer.

How it works

flowchart TD
  A[Tool call] --> B{Open block?}
  B -->|No| D[Continue through existing guards]
  B -->|Yes| C{Matches a block rule?}
  C -->|No| D
  C -->|Yes| E{Response option}
  E -->|deny| F[Stop and explain]
  E -->|hold| G[Ask the user]
  G -->|Allow once| D
  G -->|Deny or no answer| F
  D -->|Observable guard block| H[Remember the block]
  • Capture: Arrester records the blocked call's paths, the access to each (read, write, or delete), and the shape of its command. When the deny reason names some of the paths but not all, and each word in it that looks like a path names one of them, Arrester records only the named paths. A command that changes its folder with cd, pushd, or popd keeps every path.
  • Match: A later call matches when it reaches a blocked target, repeats the blocked command with changed flags or argument order, reads or changes a guard, or is opaque: it runs code or names paths that Arrester cannot read. A retry that only drops flags skips the retry rule; the other rules and your guards still apply.
  • Guards: The guard files are your user and project settings files, the hook scripts that your settings name by an absolute path or by a supported relative path, Arrester's own folder, and your installed plugins (changes only). A nested claude command with --settings, --bare, --safe-mode, --dangerously-skip-permissions, --allow-dangerously-skip-permissions, or --permission-mode bypassPermissions also counts, and so do claude plugin disable, claude plugin uninstall, and claude plugin configure … --values-stdin, also as claude plugins. Any other nested claude run counts too, except a version, help, or tool command; see Known limits.
  • Your “No”: Arrester stops only an exact retry of the call you rejected, so a changed request can go on. The target, guard, and opaque rules do not apply to it.
  • Lifetime: A block closes after block_minutes (see Options), or when you run /arrester clear. /clear starts a fresh record. /branch keeps the open blocks of the session that you branch from, with their ids and close times, and an in-app /resume of a session that ended in the same process, also by /clear, brings back its blocks that are still open. The transcript names the blocks kept, and /arrester clear closes them. A resume in a new process, such as claude -r, starts fresh; see Known limits.
  • No open block: Arrester passes calls through without analyzing them.

See the usage guide for access rules, supported shell forms, and script handling.

Install

Works with Claude Code 2.1.296 and later. Tested with Claude Code 2.1.296.

Inside Claude Code:

/plugin marketplace add dot-agi/arrester
/plugin install arrester@arrester

From a shell:

claude plugin marketplace add dot-agi/arrester
claude plugin install arrester@arrester

To try it for one session from a clone:

git clone https://github.com/dot-agi/arrester
claude --plugin-dir ./arrester

Agents and scripts

Install from a shell. The part after the install turns on auto-update. It runs only when the install succeeds and the marketplace entry exists:

claude plugin install arrester --marketplace dot-agi/arrester --scope user --json \
  && f="${CLAUDE_CONFIG_DIR:-$HOME/.claude}/settings.json" \
  && jq -e --arg name arrester '.extraKnownMarketplaces[$name].source' "$f" > /dev/null \
  && tmp=$(mktemp "$f.XXXXXX") \
  && { jq --arg name arrester '.extraKnownMarketplaces[$name].autoUpdate = true' "$f" > "$tmp" && mv "$tmp" "$f" || { rm -f "$tmp"; false; }; }

Auto-update runs only in interactive sessions. A headless job updates at its start, before the session that uses the mod:

claude plugin update arrester@arrester --json

Check the result. The first command prints the installed version. The second prints the latest release:

claude plugin list --json | jq -r '.[] | select(.id == "arrester@arrester") | "\(.version) enabled=\(.enabled)"' \
  && git ls-remote --tags --refs https://github.com/dot-agi/arrester 'arrester--v*' | sed 's/.*--v//' | sort -V | tail -n 1
  • New code loads in the next session, or after /reload-plugins.
  • Running install again does not update. Use update.
  • On a host with no SSH key for GitHub, set CLAUDE_CODE_PLUGIN_PREFER_HTTPS=1 in the environment of every claude command and session.
  • The mv replaces the settings file, with mode 600. If settings.json is a symbolic link, edit its target instead.

Commands

CommandWhat it does
/arrester or /arrester statusShows the open blocks, the held calls, and the last ten decisions.
/arrester history [1-5]Shows ten retained decisions per page, newest page first, with clipped detail.
/arrester clearCloses every open block.
/arrester clear b2Closes block b2.

Only you can run clear. Arrester accepts it from your own prompt, from Remote Control, and from an SDK host: the desktop app, the VS Code panel, claude -p, or a program of your own on the Agent SDK. It refuses clear from every other origin, such as a scheduled task or another session.

Terminal: /arrester status shows open block b1 from deny rule Bash(cat notes.txt), its target, and three recent stops.

/arrester status lists the open block and recent stops.

Options

Set them in /config. From a shell, write the values to a JSON file and run claude plugin configure arrester@arrester --values-stdin < values.json.

OptionValuesDefaultWhat it does
responsedeny, holddenydeny refuses a match at once and tells Claude to stop and ask you. hold asks you first, with Deny and Allow once; Allow once still sends the call through every guard. Where nobody can answer (claude -p), hold refuses.
block_minutes1 to 144030How long a block stays open after it opened or after its last target or retry match.

How it compares

Arrester remembers supported guard blocks within the current conversation, and keeps them across /branch and an in-app /resume in the same Claude Code process. These tools provide complementary checks and controls.

Tool or featureWhat it doesWhat Arrester adds
Built-in /permissions Recently denied and the PermissionDenied hookLists auto mode denials, which you can retry with your approval; the hook runs on each auto mode denial.Checks later calls against a recorded block, including recognized routes through other tools.
Built-in protected pathsNever auto-approves writes to protected folders such as .git and .claude, except in bypassPermissions mode and in some sessions where that mode is available.While a block from a hook, rule, or classifier is open, also stops recognized reads of guard files and recognized guard-changing commands.
TermaxaApplies command policies, previews consequences, and uses a circuit breaker for repeated destructive intent across shells and sessions.Follows supported blocks from other guards and matches their specific file targets.
melodic-software guardrailsblock-hook-bypass checks Bash writes that bypass Write/Edit hooks; other guards check file changes.Remembers a prior block and checks later calls against its targets across tools.
destructive_command_guard (dcg)Uses rule packs to block destructive commands, with explanations and allow-once controls.Follows observable hook blocks and stops common attempts to reach the blocked target another way.
guard-and-guideUses configurable regular-expression rules to deny commands and suggest safer alternatives.Checks whether a later alternative matches an open block's target or another block rule.

Known limits

What it catches

  • A blocked file or folder reached another way, such as head after a blocked cat, the Read tool, an MCP call that names the path, or a search with a file filter or a depth limit that names it or a folder that holds it.
  • Files inside a folder after a blocked search of that folder by grep, rg, ag, ugrep, or the Grep tool, with no file filter, depth limit, or file listing, such as cat secrets/token.txt after a blocked grep -r . secrets.
  • Commands inside shell scripts; in a call that fits the minimal profile, also every branch of a case in the call itself (an older script that the call runs keeps the 1.1.3 reading), the elements of an array that the call assigns once at its top level, as in "${files[@]}", with expansions in double quotes and no builtin but echo, cd, true, false, and :, and literal paths in common file operations of interpreter code, such as python -c. A shell that starts with a printing option and runs a script file, such as bash -v check.sh, also reads the script.
  • While a block from a hook, rule, or classifier is open: reads and changes of settings and hook files, and nested claude runs.

What it does not catch

  • Paths that code builds at run time, in scripts, interpreter code, or command output, and code that Arrester does not read: functions and aliases from your shell profile, unlisted runners such as bundle exec, compiled programs, and remote commands. Arrester reads printing options only on the command line of a shell that runs a script file. So it records no read of a script printed after set -x or bash -x -c, or through options from the environment or a #! line, as in set -x; . check.sh or SHELLOPTS=xtrace bash check.sh.
  • Reads inside a folder after a blocked call that records no folder: a search with a file filter, a depth limit, or a file listing, such as rg -t py KEY src or rg --files src; a recursive copy, sync, archive, or compare, such as cp -r src /tmp or rsync -a src /tmp; a search of a broad folder, such as the project folder; or a search whose deny reason suggests another tool. A search with a file filter or a depth limit that names no folder, such as rg -t py KEY, counts as no read.
  • Copies into a folder from a glob, find, or xargs, such as cp /staging/* /work/, that replace a blocked file.
  • Hook denials in managed settings, a changed retry after your own “No”, which stops only the same call, and a path of a blocked call that the deny reason does not name, when the reason names another path of the call, such as config/app.yaml after a hook denied cat config/secrets.env config/app.yaml for config/secrets.env. The guard still checks that path. Some session switches also start with no open block: a resume in a new process, such as claude -r or claude --continue, the copy that /fork starts or /background moves, whose history still shows the block, and, with managed settings or a Team or Enterprise sign-in, /branch and an in-app /resume. A reload of Arrester forgets the sessions that ended before it, and Arrester keeps the records of the last 32 sessions that ended.

Arrester is not a sandbox and cannot recognize every shell rewrite. The full list of known limits gives each limit with examples and workarounds.

Most of these limits apply while a block from a hook, deny rule, or auto mode classifier is open. In hold mode, Allow once still runs every guard. In deny mode, inspect the call and its block with /arrester; clear that specific block only if you intend to end its protection.

  • Searches that name no folder: A search with no folder, such as rg pattern, grep -r pattern, or a Grep call without path, counts as a read of the session's folder. For rg and ag, Arrester skips hidden names there, as they do, unless a flag such as --hidden shows them. A search with a file filter, such as rg -g '*.ts' pattern, rg -t py pattern, grep -r --include='*.ts' pattern, ag -G '\.ts$' pattern, or ag --js pattern, counts as no read, so it can read a blocked file without a stop or a warning. A search with a depth limit, such as rg --max-depth 1 pattern, ag --depth 1 pattern, or ugrep -r --depth=2 pattern, also counts as no read, so it can read a blocked file within that depth without a stop or a warning. Some other reads also pass without a stop or a warning: hidden files that a ripgrep config file or an rg flag that Arrester does not read shows, and a search by a subagent that runs in its own folder. An rg or ag search can also stop falsely on a hidden file below a visible folder, such as apps/.env, or on a file that .gitignore hides. A Grep or Glob call gets no target stop from a block of a Read( deny rule, because Claude Code leaves those files out of its search. Ask Claude to name the search folder while a read block is open.
  • Copies into a folder: For a copy, move, or link into a folder, such as cp /work/staging/secret /work/, Arrester records the entry that it makes there, /work/secret. It cannot name that entry when the source's last name is a glob, as in cp /staging/* /work/, when the source comes from find -exec or from unknown xargs input, or for an rsync source that ends in /. Such a copy can replace a file under a write or deletion block without a stop or a warning. Under a deletion block, a recursive copy of a folder, such as cp -r /staging/d /work/ with /work/d/secret blocked, also passes. Arrester records the entry also when the destination is a file, which can cause a false stop. A copy, move, or link that cannot replace an entry or changes nothing adds no entry: cp -n, mv -n, --update=none, rsync -n, rsync --ignore-existing, and ln without a flag that replaces, such as -f. Such a call can still make a blocked file that does not exist yet without a stop or a warning. A call that gives -n together with -i or -f, such as cp -i -n, still adds the entry, so it can stop falsely. A copy of 128 or more named files into one folder passes the limit of 256 path effects, so it is opaque. Ask Claude to name the destination file, and check globbed or found copies into the folder of a protected file yourself.
  • A blocked search of a file or a folder by grep, egrep, fgrep, ugrep, rg, or ag, such as grep -r KEY /work/secrets, or a Grep call with that path, records it, so a later read of it, or of a file inside the folder, stops. A blocked search with a file filter or a depth limit records no folder, such as grep -r --include='*.ts' KEY /work/src, rg -g '*.ts' KEY /work/src, rg --max-depth 1 KEY /work/src, rg --ignore-file .ignore KEY /work/src, or a Grep call with glob or type. A blocked search with --files, --type-list, --help, or --version, such as rg --files /work/src, lists files or prints something else, so it records no folder either. A blocked recursive copy, sync, archive, or compare of a folder records no folder, such as cp -r /work/secrets /tmp/s, rsync -a --exclude='*.env' /work/src/ /tmp/b/, tar -czf /tmp/s.tgz /work/secrets, or diff -r /work/a /work/b. Reads inside such a folder pass without a stop or a warning, also of files that the blocked call would have read. Such a call on a regular file still records the file. A later search with a file filter or a depth limit still stops when it names a recorded file or folder, or a folder that holds one. The full list of known limits names each filter and depth flag. A blocked search records no folder that is /, the home folder, the session's folder, or the project folder, that holds one of them, or that holds a settings or hook file, so reads inside such a folder pass without a stop or a warning. A search that names no folder reads the session's folder, so it records none. A blocked search also records no folder when the deny reason suggests another tool: it says instead, prefer, please use, use rg, or ` try ` in any letter case, such as Use rg instead of grep. Then the suggested search of the folder, and reads inside it, pass without a stop or a warning, also when the reason says that the folder holds secrets too. A blocked recursive search of a glob path, such as rg KEY /work/config/*.env, and a blocked recursive listing, such as ls -R or a Glob call, record no target. A blocked listing or search that is not recursive records its path, also a glob path: after a blocked ls /work/secrets or grep KEY /work/config/*.env, a later read of /work/secrets/token.txt or /work/config/secret.env stops. A later search that holds a recorded folder reads it too, so it stops, such as rg KEY in the project. When a hook denies a search of a file only to suggest another tool, such as rg for grep`, that tool's search of the same file stops too.
  • Arrester reads paths from Bash, Monitor, Read, Write, Edit, NotebookEdit, LSP, Grep, Glob, SendFile, and SendUserFile calls, from a file: URI in a ReadMcpResourceTool call, and from MCP calls. An MCP call names a path only with a whole one-line string that is absolute, starts with ~/, or is a local file: URI. Arrester cannot know what the MCP tool does with it, so an MCP call that only reads a write-blocked or deletion-blocked file stops too. A named folder that exists matches no write or deletion block. An MCP call that changes or deletes a folder under a write or deletion block, a file inside such a folder, or a folder that holds a blocked file passes without a stop or a warning, and so do relative paths, paths inside longer text, and calls of other tools. An MCP call that names more than 256 paths is opaque.
  • Your “No” matches only an identical call: the same command text, working folder, and relevant tool arguments. A command that differs by one character goes on to your guards. Tool input past the serialization limits gets no exact identity, so your “No” on such a call matches no retry.
  • Managed-settings hook denials end before installed mods run, so Arrester cannot observe them. With managed settings on the machine, or with a Team or Enterprise sign-in, Arrester sees a hook that denies with JSON only through the permission verdict, which names the hook. There, a deny that Claude Code makes after a hook's answer can also open a hook block: the tool's own check or a safety check that denies after a hook's ask, or a hook's changed input that Claude Code refuses.
  • Arrester expands ~, $HOME, and $CLAUDE_PROJECT_DIR in a hook command; a hook with args gets only $CLAUDE_PROJECT_DIR. A relative hook path counts only as a command's program, such as ./hooks/check.sh, as the first path after an interpreter or a shell, such as python3 .claude/hooks/guard.py, or under .claude/. Arrester reads it in the session's folder and in the project folder when the block opens. A bare name such as python3 guard.py and a script found through PATH add no guard. A cd in the hook command does not count, so the path after it guards the wrong file or none. Write python3 "$CLAUDE_PROJECT_DIR"/hooks/guard.py to guard such a file.
  • In an interpreter script that last changed before the oldest open block, Arrester counts only a literal path that is a whole argument of a definite file operation, such as shutil.rmtree('/w/d'). Paths that such a script passes to subprocess, exec, spawn, system, or popen, builds from parts, nests in another call, or keeps in a Path(…) that no file operation uses directly, such as p = Path('/w/d') before p.read_text(), pass without a stop or a warning. So does the whole script when the call has no work budget left for it. In an older shell script, code that Arrester cannot read does not count, including code past the command-length or nesting limit. At capture, every script read from disk counts as older, and an older interpreter script is not scanned, so a block on an interpreter script on disk records no targets from inside it.
  • A nested claude run does not inherit the open blocks, so while a block from a hook, rule, or classifier is open, it stops: also as claude-code, in any letter case, by a claude/versions/<version> path, or through a runner such as npx. Only a first word of -v, --version, -h, --help, or a tool subcommand (mcp, plugin, plugins, doctor, update, upgrade, install, auth, or setup-token) passes, but not mcp serve, an mcp add form, or a first word or subcommand that does not resolve. So claude -p "summarize" and claude --verbose mcp list stop too; in hold mode, choose Allow once. A session that another program starts still escapes: the Agent SDK, bun x, setsid, arch, Python subprocess, another agent CLI, a function or an alias from your shell profile, or a copy of the program under another name.
  • Interpreter code can compute paths, use bare file names or names with spaces, or hide literals behind template expressions or comments tha
Source 13 files
hooks/register.ts 567 lines
1// Arrester's hooks module: every hook and every `$` call. It keeps the journal
2// in `$.state`, reads the session and the file system through `$`, and leaves
3// every decision about paths and words to the pure modules.
4
5import { atom, read, update } from 'claude-code'
6import type { Caught, EngineInterface, EventName, EventResult, FsStat, Hook, HookFailure, PluginOptions, Register, Timer, TraceOutcome } from 'claude-code'
7
8import { analyzeCall, envelopeCall, inputCall } from './analyze/call'
9import type { Call } from './analyze/call'
10import { basenameOf, dirnameOf, joinPath } from './analyze/paths'
11import { OPAQUE } from './analyze/shell'
12import type { Effect, Io, ScriptRead } from './analyze/types'
13import { fixedGuards, hookPaths, SETTINGS_SOURCES } from './guards'
14import { cleared, EMPTY_JOURNAL, isRecorded, isTargetFolder, namedTargets, nextClose, openBlocks, pruned, renewed, targetsOf, withBlock, withDecision, withHeld, withoutHeld } from './journal'
15import { blocksOf, findings, renewable } from './match'
16import type { Finding } from './match'
17import { ALLOW_ONCE, ARGUMENT_HINT, blockOfText, causeOf, clearedText, clearRefused, COMMAND_DESCRIPTION, commandFailed, couldNotCheck, couldNotKeep, couldNotReadScript, couldNotReadSettings, couldNotRecord, couldNotRegister, couldNotResolve, couldNotRestore, couldNotSave, couldNotSync, decisionLine, evictionNotice, historyText, keptLine, retryNotice, DENY, denyReason, failureOf, guardMessage, hasVerdict, HEADER, heldReason, holdEnded, holdUnanswered, homeUnset, hostText, incompleteCapture, hookFailedNoBlock, noVerdict, notCounted, phrase, question, statusLine, statusText, storedReason, summarize, uncheckedReason, USAGE, viewOf } from './text'
18import type { ArresterAction, ArresterGuard, ArresterJournal, ArresterSource } from '../types/index'
19
20/** The journal, the one `$.state` value. */
21const JOURNAL = atom({ plugin: 'arrester', key: 'journal' } as const, EMPTY_JOURNAL)
22
23/** The most filesystem placements in flight at once. */
24const PLACEMENT_WORKERS = 8
25
26/** The last known block state, used when re-entry forbids engine calls. */
27let hasOpenBlocks: boolean | undefined
28
29/** The origins whose `/arrester clear` is the user's own: the terminal, Remote Control, and the SDK host. */
30const USER_ORIGINS = new Set(['composer', 'bridge', 'sdk'])
31
32/** The longest script Arrester reads, in bytes. */
33const MAX_SCRIPT = 262_144
34
35/** The options as Arrester reads them. */
36type Config = { response: 'deny' | 'hold'; minutes: number }
37
38/** The arguments of a hook on event `N`. */
39type HookArgs<N extends EventName> = Parameters<Hook<N>>
40
41/** Where a path lands, and the kind of what is there when it exists. */
42type Placement = { real?: string; kind?: FsStat['kind'] }
43
44/** A path to place, and whether it names the entry itself. */
45type PlaceValue = { path: string; entry?: true }
46
47/** Placements by `placeKey`. */
48type Placements = Map<string, Placement>
49
50/** A `classic.PreToolUse` dispatch that Arrester passed on: the call as that event carried it, and the deny that settled beneath, if one did. */
51type PassedPreToolUse = { call: Call; deny: string | undefined }
52
53/** The most passed `classic.PreToolUse` dispatches kept for their `tool.call` hook; past it the oldest goes. */
54const MAX_PASSED = 64
55
56/** The passed `classic.PreToolUse` dispatches by tool use id, oldest first; the `tool.call` hook of the same call reads and drops each one. */
57const passedPreToolUses = new Map<string, PassedPreToolUse>()
58
59/** The timer that refreshes the status line at the next close; a reload drops it with the old module. */
60let timer: Timer | undefined
61
62/** How many syncs this module started: only the newest draws the status line and sets the timer, and the end of the session stops every sync in flight. */
63let syncs = 0
64
65/** Whether this module load reported that HOME is not set. */
66let isHomeReported = false
67
68/** The most ended sessions whose journal Arrester keeps; past it the oldest goes. */
69const MAX_ENDED = 32
70
71/** The journal of each session that ended in this process, by session id, oldest first: a branch or an in-app resume of that session takes it back. Module memory only, so a new process starts without it. */
72const endedJournals = new Map<string, ArresterJournal>()
73
74/** The session that ended last, until the next `classic.SessionStart` reads it: the parent of a branch. */
75let lastEnded: string | undefined
76
77/** Wires every hook. */
78export const register: Register = (on, options) => {
79  const config = configOf(options)
80
81  on('session.start', async ($, e, next) => {
82    await registerCommand($)
83    await sync($, config, 'session.start', true)
84    return next(e)
85  })
86
87  on('session.end', async ($, e, next) => {
88    syncs += 1
89    timer?.cancel()
90    timer = undefined
91    $.ui.status(undefined)
92    await keepJournal($, e.sessionId)
93    return next(e)
94  })
95
96  on('classic.SessionStart', async ($, e, next) => {
97    await switched($, config, e)
98    return next(e)
99  }).catch(($, e, next) => {
100    if (isReentry(next.error)) return next(e)
101    $.ui.log(hostText(couldNotRestore(failureOf(next.error))))
102    return next(e)
103  })
104
105  on('classic.PreToolUse', async ($, e, next) => {
106    const passed = passPreToolUse(envelopeCall(e))
107    try {
108      return await next(e)
109    } finally {
110      passed.deny = settledOf(next.trace)?.deny
111    }
112  }).catch(($, e, next) => {
113    return next(e)
114  })
115
116  on('tool.check', async ($, e, next) => {
117    try {
118      return await next(e)
119    } finally {
120      const verdict = settledOf(next.trace)
121      if (verdict?.decision === 'deny' && verdict.rule !== undefined && e.tool_use_id !== undefined) await recordBlock($, config, 'tool.check', inputCall(e.tool, e.input, e.tool_use_id, e.agentId), 'rule', verdict.reason ?? verdict.rule, verdict.rule, next.signal)
122      else if (verdict?.decision === 'deny' && verdict.rule === undefined && verdict.hook === 'PreToolUse' && e.tool_use_id !== undefined && !passedPreToolUses.has(e.tool_use_id)) await recordBlock($, config, 'tool.check', inputCall(e.tool, e.input, e.tool_use_id, e.agentId), 'hook', verdict.reason ?? 'denied', undefined, next.signal)
123    }
124  }).catch(($, e, next) => {
125    if (isReentry(next.error)) return next(e)
126    $.ui.log(hostText(couldNotRecord('tool.check', e.tool, failureOf(next.error))))
127    return next(e)
128  })
129
130  on('classic.PermissionDenied', async ($, e, next) => {
131    if (hasVerdict(e.reason)) await recordBlock($, config, 'classic.PermissionDenied', inputCall(e.tool_name, e.tool_input, e.tool_use_id), 'classifier', e.reason, undefined, next.signal)
132    else $.ui.log(noVerdict(e.reason), { to: 'debug' })
133    return next(e)
134  }).catch(($, e, next) => {
135    if (isReentry(next.error)) return next(e)
136    $.ui.log(hostText(couldNotRecord('classic.PermissionDenied', e.tool_name, failureOf(next.error))))
137    return next(e)
138  })
139
140  on('tool.call', ($, e, next) => checkCall($, config, e, next)).catch(($, e, next) => checkFailed($, config, e, next))
141
142  on('command.run', { command: 'arrester' }, ($, e) => runCommand($, config, e)).catch(($, e, next) => ({ text: hostText(commandFailed(failureOf(next.error))) }))
143}
144
145/** The options the engine validated against plugin.json. */
146function configOf(options: PluginOptions): Config {
147  return { response: options.response === 'hold' ? 'hold' : 'deny', minutes: Number(options.block_minutes) }
148}
149
150/** Drops expired blocks, and after a reload the held calls, with a transcript line for each; redraws the status line; sets the timer for the next close. A sync that a newer one or the end of the session overtook draws nothing. A failure is logged to the debug log. */
151async function sync(engine: EngineInterface, config: Config, event: string, dropHeld = false): Promise<void> {
152  syncs += 1
153  const ticket = syncs
154  try {
155    const now = await engine.clock.now()
156    const before = await read(engine, JOURNAL)
157    const journal = pruned(before, now, config.minutes, dropHeld) === before ? before : await update(engine, JOURNAL, value => pruned(value, now, config.minutes, dropHeld))
158    if (dropHeld) for (const held of before.held) engine.ui.log(hostText(holdEnded(held)))
159    if (ticket !== syncs) return
160    hasOpenBlocks = journal.blocks.length > 0
161    engine.ui.status(statusLine(journal.blocks.length, journal.held.length, journal.eviction?.blockId))
162    timer?.cancel()
163    const due = nextClose(journal, config.minutes)
164    timer = due === undefined ? undefined : engine.clock.after(Math.max(1, due - now), () => void sync(engine, config, 'clock.after'))
165  } catch (error) {
166    engine.ui.log(couldNotSync(event, causeOf(error)), { to: 'debug' })
167  }
168}
169
170/** Keeps the journal of the session that ends, by its id, for a branch or an in-app resume of it, and remembers the session as the parent of a branch; past MAX_ENDED sessions the oldest goes. A journal that cannot be read is logged to the transcript, and a branch or resume of that session starts with no open block. */
171async function keepJournal(engine: EngineInterface, sessionId: string): Promise<void> {
172  lastEnded = sessionId
173  endedJournals.delete(sessionId)
174  try {
175    endedJournals.set(sessionId, await read(engine, JOURNAL))
176  } catch (error) {
177    engine.ui.log(hostText(couldNotKeep(causeOf(error))))
178    return
179  }
180  for (const id of endedJournals.keys()) {
181    if (endedJournals.size <= MAX_ENDED) break
182    endedJournals.delete(id)
183  }
184}
185
186/** After a switch in this process, read from `classic.SessionStart` by the new session's id, since `$.session.id()` still answers the old one: a branch (`fork`) takes the journal of the session that ended just before, and an in-app resume the journal that the resumed session left. Each keeps the blocks still open, with their ids and times, and drops the held calls, whose hooks ended with that session; one transcript line names the open blocks kept. `/clear` and every other start keep the fresh journal. A journal that cannot be restored is logged to the transcript. The status line is redrawn after every switch. */
187async function switched(engine: EngineInterface, config: Config, input: HookArgs<'classic.SessionStart'>[1]): Promise<void> {
188  const parent = lastEnded
189  lastEnded = undefined
190  const from = input.source === 'fork' ? parent : input.source === 'resume' ? input.session_id : undefined
191  const saved = from === undefined ? undefined : endedJournals.get(from)
192  if (saved !== undefined) {
193    try {
194      const journal = pruned(saved, await engine.clock.now(), config.minutes, true)
195      await update(engine, JOURNAL, () => journal)
196      if (journal.blocks.length > 0) engine.ui.log(keptLine(journal.blocks.map(block => block.id)))
197    } catch (error) {
198      engine.ui.log(hostText(couldNotRestore(causeOf(error))))
199    }
200  }
201  await sync(engine, config, 'classic.SessionStart')
202}
203
204/** Opens a block for a call that a guard blocked, with its targets and the guard list placed on the file system; a recursive read whose root is a regular file counts as a read of that file, and a `fullSearch` whose root is a folder as a read of the folder, unless `isTargetFolder` spares it or the reason names another tool to use. Another recursive read of a folder, such as a filtered search, `cp -r`, `rsync`, `tar`, or `diff -r`, records no folder. When the call has two or more targets and the reason of a guard other than the user names some but not all of them, as `namedTargets` reads its `guardMessage`, the block keeps only the named ones. Every script on disk counts as older than the block, and an older interpreter script is not scanned. A call opens one block, also when that block was cleared since. */
205async function recordBlock(engine: EngineInterface, config: Config, event: string, call: Call, source: ArresterSource, reason: string, rule: string | undefined, signal: AbortSignal): Promise<void> {
206  const toolUseId = call.toolUseId
207  if (toolUseId === undefined || isRecorded(await read(engine, JOURNAL), toolUseId)) return
208  const home = await homeOf(engine, event)
209  const cwd = await engine.session.cwd()
210  const analysis = await analyzeCall(call, { cwd, home }, ioOf(engine, event, signal, Number.POSITIVE_INFINITY, true))
211  if (signal.aborted) return
212  const roots = await placeAll(engine, analysis.effects.filter(isSearchRoot), event, signal)
213  const guards = await guardPaths(engine, event, home, cwd)
214  const isFolderSearch = (effect: Effect): boolean => effect.fullSearch === true && isSearchRoot(effect) && roots.get(placeKey(effect))?.kind === 'dir'
215  const spared = analysis.effects.some(isFolderSearch) ? await sparedPaths(engine, event, signal, [home, cwd, await engine.session.root()], guards) : []
216  const effects = analysis.effects.map(effect => {
217    const found = isSearchRoot(effect) ? roots.get(placeKey(effect)) : undefined
218    const isTarget = found?.kind === 'file' || (found !== undefined && isFolderSearch(effect) && isTargetFolder(found.real === undefined ? [effect.path] : [effect.path, found.real], spared, reason))
219    return isTarget ? { ...effect, recursive: false } : effect
220  })
221  const all = targetsOf(effects)
222  const unplaced = all.filter(target => !roots.has(placeKey(target)))
223  const rest = await placeAll(engine, unplaced, event, signal)
224  const placed = withPlacements(all, new Map([...roots, ...rest]))
225  const targets = source === 'user' ? placed : namedTargets(placed, guardMessage(reason), typeof call.input.command === 'string' ? call.input.command : undefined, cwd, home)
226  if (signal.aborted) return
227  const at = await engine.clock.now()
228  const turn = await engine.session.turns()
229  const draft = { source, agentId: call.agentId, incomplete: analysis.opaque, tool: call.tool, summary: summarize(call), reason: storedReason(reason), ...(rule === undefined ? {} : { rule }), toolUseId, turn, at, exact: analysis.exact, skeleton: analysis.skeleton, flags: analysis.flags, segments: analysis.segments, targets }
230  let opened = false
231  const saved = await update(engine, JOURNAL, journal => {
232    const updated = withBlock(journal, draft, guards, config.minutes)
233    opened = updated !== journal
234    return updated
235  })
236  if (opened && saved.eviction?.toolUseId === toolUseId) engine.ui.log(hostText(evictionNotice(event, saved.eviction.blockId)))
237  const captured = saved.blocks.find(block => block.toolUseId === toolUseId)
238  if (opened && captured?.retryLimited !== undefined) engine.ui.log(hostText(retryNotice(event, captured.id, captured.retryLimited)))
239  hasOpenBlocks = true
240  if (opened && analysis.opaque !== undefined) engine.ui.log(hostText(incompleteCapture(event, analysis.opaque)))
241  await sync(engine, config, event)
242}
243
244/** The guards, placed on the file system: the fixed ones and each regular file that a command hook names, a relative name read in the session folder `cwd` and in the project folder. Working folders and paths whose kind is unknown add no guard. A settings source that cannot be read adds none and is logged. */
245async function guardPaths(engine: EngineInterface, event: string, home: string | undefined, cwd: string): Promise<ArresterGuard[]> {
246  const root = await engine.session.root()
247  const fixed = fixedGuards(home, root, engine.plugin.root)
248  const named = new Set<string>()
249  for (const source of SETTINGS_SOURCES) {
250    try {
251      for (const path of hookPaths(await engine.settings.read({ source }), home, root, cwd)) if (!fixed.some(guard => guard.path === path)) named.add(path)
252    } catch (error) {
253      engine.ui.log(couldNotReadSettings(event, source, causeOf(error)), { to: 'debug' })
254    }
255  }
256  const placedFixed = await Promise.all(fixed.map(async guard => withReal(guard, (await place(engine, event, guard.path)).real)))
257  const placedNamed = await Promise.all([...named].map(async path => ({ path, ...(await place(engine, event, path)) })))
258  return [...placedFixed, ...placedNamed.filter(found => found.kind === 'file').map(found => withReal({ path: found.path }, found.real))]
259}
260
261/** The paths that a folder target may not be or hold: each of `folders`, such as the home, session, and project folders, and each guard, also by its real path. */
262async function sparedPaths(engine: EngineInterface, event: string, signal: AbortSignal, folders: readonly (string | undefined)[], guards: readonly ArresterGuard[]): Promise<string[]> {
263  const values = folders.flatMap(path => (path === undefined ? [] : [{ path }]))
264  const placed = withPlacements(values, await placeAll(engine, values, event, signal))
265  return [...placed, ...guards].flatMap(value => (value.real === undefined ? [value.path] : [value.path, value.real]))
266}
267
268/** Where a path lands and what is there: its own real path and kind when it exists, also for a removed entry that is no symbolic link, so a case alias lands on the name on disk; for a removed link, a missing path, or an entry the file system refuses, its folder's real path plus its name; nothing when neither exists, or when the file system refuses the folder, which is logged to the debug log. */
269async function place(engine: EngineInterface, event: string, path: string, entry = false): Promise<Placement> {
270  if (entry) {
271    try {
272      const own = await engine.fs.stat(path, { resolve: true })
273      if (!own.isLink && own.realPath !== undefined) return { real: own.realPath, kind: own.kind }
274    } catch {
275      // A missing entry, or one the file system refuses, is placed by its folder below.
276    }
277  }
278  try {
279    if (!entry && await engine.fs.exists(path)) {
280      const stat = await engine.fs.stat(path, { resolve: true })
281      return { real: stat.realPath, kind: stat.kind }
282    }
283    const folder = dirnameOf(path)
284    if (folder === path || !(await engine.fs.exists(folder))) return {}
285    const real = (await engine.fs.stat(folder, { resolve: true })).realPath
286    return real === undefined ? {} : { real: joinPath(real, basenameOf(path)) }
287  } catch (error) {
288    engine.ui.log(couldNotResolve(event, path, causeOf(error)), { to: 'debug' })
289    return {}
290  }
291}
292
293/** The value with its real path added when the file system gave one that differs. */
294function withReal<T extends { path: string }>(placed: T, real: string | undefined): T & { real?: string } {
295  return real === undefined || real === placed.path ? placed : { ...placed, real }
296}
297
298/** The home folder from `HOME`, which expands `~`; when it is not set, the debug log says so once per module load. */
299async function homeOf(engine: EngineInterface, event: string): Promise<string | undefined> {
300  const home = await engine.env.get('HOME')
301  if (home !== undefined && home !== '') return home
302  if (!isHomeReported) {
303    isHomeReported = true
304    engine.ui.log(homeUnset(event), { to: 'debug' })
305  }
306  return undefined
307}
308
309/** The I/O the analysis may do: read the script a call runs, judged against `since`, and see the dispatch end; `capture` when it records a block. */
310function ioOf(engine: EngineInterface, event: string, signal: AbortSignal, since: number, capture = false): Io {
311  const io: Io = { readScript: path => readScript(engine, event, path, since), signal }
312  return capture ? { ...io, capture } : io
313}
314
315/** A script for the analysis: `missing`; its text when it is a regular file of at most MAX_SCRIPT bytes; else why it cannot be read, with a debug line for a refused read or a path that is no regular file. `isOld` when the file last changed before `since`. */
316async function readScript(engine: EngineInterface, event: string, path: string, since: number): Promise<ScriptRead> {
317  let isOld = false
318  let cause: string
319  try {
320    if (!(await engine.fs.exists(path))) return 'missing'
321    const stat = await engine.fs.stat(path)
322    isOld = stat.mtimeMs < since
323    if (stat.kind === 'file') return stat.size > MAX_SCRIPT ? { unreadable: OPAQUE.scriptTooLong, isOld } : { text: await engine.fs.read(path), isOld }
324    cause = `it is not a regular file but ${stat.kind}`
325  } catch (error) {
326    cause = causeOf(error)
327  }
328  engine.ui.log(couldNotReadScript(event, path, cause), { to: 'debug' })
329  return { unreadable: OPAQUE.scriptUnreadable, isOld }
330}
331
332/** Keeps a `classic.PreToolUse` dispatch as it starts, in place of an older one of the same call, for the `tool.call` hook of its call, and returns its entry, whose deny the dispatch sets once it settles; a call without a tool use id is not kept. */
333function passPreToolUse(call: Call): PassedPreToolUse {
334  const passed: PassedPreToolUse = { call, deny: undefined }
335  if (call.toolUseId === undefined) return passed
336  passedPreToolUses.delete(call.toolUseId)
337  passedPreToolUses.set(call.toolUseId, passed)
338  for (const id of passedPreToolUses.keys()) {
339    if (passedPreToolUses.size <= MAX_PASSED) break
340    passedPreToolUses.delete(id)
341  }
342  return passed
343}
344
345/** What the links beneath a hook settled to, read from its trace, nearest first: the answer of the first link whose own answer stood; nothing when a link rejected or none settled. */
346function settledOf<O>(trace: readonly { readonly outcome: TraceOutcome; readonly returned: O | undefined }[]): O | undefined {
347  for (const entry of trace) {
348    if (entry.outcome === 'rejected') return undefined
349    if ((entry.outcome === 'returned' || entry.outcome === 'caught') && entry.returned !== undefined) return entry.returned
350  }
351  return undefined
352}
353
354/** Opens a block when the hooks beneath Arrester's `classic.PreToolUse` hook denied the call, once the call settled; a block that cannot be recorded is logged to the transcript, and the deny stands. */
355async function observePreToolUse(engine: EngineInterface, config: Config, toolUseId: string | undefined, signal: AbortSignal): Promise<void> {
356  const passed = toolUseId === undefined ? undefined : passedPreToolUses.get(toolUseId)
357  if (toolUseId === undefined || passed === undefined) return
358  passedPreToolUses.delete(toolUseId)
359  const deny = passed.deny
360  if (typeof deny !== 'string') return
361  try {
362    await recordBlock(engine, config, 'classic.PreToolUse', passed.call, 'hook', deny, undefined, signal)
363  } catch (error) {
364    engine.ui.log(hostText(couldNotRecord('classic.PreToolUse', passed.call.tool, `throw: ${causeOf(error)}`)))
365  }
366}
367
368/** Opens a block when a call's result says a guard blocked it: a deny beneath Arrester's `classic.PreToolUse` hook, the user's rejection, or the text a hook or the classifier left. */
369async function observe(engine: EngineInterface, config: Config, call: Call, result: EventResult<'tool.call'>, signal: AbortSignal): Promise<void> {
370  await observePreToolUse(engine, config, call.toolUseId, signal)
371  if (result.isError !== true || typeof result.text !== 'string') return
372  const block = blockOfText(result.text)
373  if (block !== undefined) await recordBlock(engine, config, 'tool.call', call, block.source, block.reason, undefined, signal)
374}
375
376/** The `tool.call` hook: stops or holds a workaround of an open block; passes every other call on and reads its result. A path that an MCP call names is marked when it is a folder that exists. A dispatch abandoned during the check goes no further. */
377async function checkCall(engine: EngineInterface, config: Config, input: HookArgs<'tool.call'>[1], forward: HookArgs<'tool.call'>[2]): Promise<EventResult<'tool.call'>> {
378  const journal = await read(engine, JOURNAL)
379  const now = await engine.clock.now()
380  const open = openBlocks(journal, now, config.minutes)
381  hasOpenBlocks = open.length > 0
382  const call = envelopeCall(input)
383  if (open.length > 0) {
384    const since = Math.min(...open.map(block => block.at))
385    const analysis = await analyzeCall(call, { cwd: await engine.session.cwd(), home: await homeOf(engine, 'tool.call') }, ioOf(engine, 'tool.call', forward.signal, since))
386    if (analysis.ignored !== undefined) engine.ui.log(notCounted(analysis.ignored), { to: 'debug' })
387    const placements = await placeAll(engine, analysis.effects, 'tool.call', forward.signal)
388    const placed = withPlacements(analysis.effects, placements).map(effect => (effect.kind === 'name' && placements.get(placeKey(effect))?.kind === 'dir' ? { ...effect, folder: true as const } : effect))
389    if (forward.signal.aborted) return abandoned()
390    const found = findings(open, journal.guards, analysis, placed, call.tool)
391    const [first] = found
392    if (first !== undefined && config.response === 'hold') return hold(engine, config, input, forward, call, found, first, now)
393    if (first !== undefined) return stop(engine, config, call, found, first, now)
394  }
395  if (forward.signal.aborted) return abandoned()
396  const result = await forward(input)
397  await observe(engine, config, call, result, forward.signal)
398  return result
399}
400
401/** The `.catch` handler of the `tool.call` hook: replays a call that had settled and reports the block it could not record; refuses the call while blocks are open; passes it on while none is. */
402async function checkFailed(engine: EngineInterface, config: Config, input: HookArgs<'tool.call'>[1], forward: HookArgs<'tool.call'>[2] & Caught): Promise<EventResult<'tool.call'>> {
403  if (isReentry(forward.error)) return hasOpenBlocks === false ? forward(input) : { deny: uncheckedReason('re-entry prevents checking this call') }
404  const failure = failureOf(forward.error)
405  const tool = String(input.tool)
406  if (forward.called) {
407    engine.ui.log(hostText(couldNotRecord('tool.call', tool, failure)))
408    return forward(input)
409  }
410  const open = await openCount(engine, config)
411  if (open.count === 0) {
412    engine.ui.log(hookFailedNoBlock(failure), { to: 'debug' })
413    return forward(input)
414  }
415  engine.ui.log(hostText(couldNotCheck(tool, failure, open.count, open.cause)))
416  return { deny: uncheckedReason(failure) }
417}
418
419/** Whether a hook failed on re-entry, which engines before 2.1.301 still pass to a `.catch`; that `.catch` then makes no engine call. */
420function isReentry(failure: HookFailure): boolean {
421  const kind: string = failure.kind
422  return kind === 're-entry'
423}
424
425/** The answer to a dispatch that was abandoned, which goes no further: nobody reads it, since the dispatch went on without this hook. */
426function abandoned(): { deny: string } {
427  return { deny: uncheckedReason(OPAQUE.interrupted) }
428}
429
430/** Places every distinct path and entry mode once, with bounded concurrency, and returns each placement by `placeKey`. */
431async function placeAll(engine: EngineInterface, values: readonly PlaceValue[], event: string, signal: AbortSignal): Promise<Placements> {
432  const unique = [...new Map(values.map(value => [placeKey(value), value])).entries()]
433  const placements: Placements = new Map()
434  let cursor = 0
435  await Promise.all(Array.from({ length: Math.min(PLACEMENT_WORKERS, unique.length) }, async () => {
436    while (cursor < unique.length && !signal.aborted) {
437      const [key, value] = unique[cursor++]!
438      const placement = await place(engine, event, value.path, value.entry)
439      placements.set(key, placement)
440    }
441  }))
442  return placements
443}
444
445/** The values, each with the real path of its placement when the file system gave one that differs. */
446function withPlacements<T extends PlaceValue>(values: readonly T[], placements: Placements): (T & { real?: string })[] {
447  return values.map(value => withReal(value, placements.get(placeKey(value))?.real))
448}
449
450/** The key of a path and its entry mode among placements. */
451function placeKey(value: PlaceValue): string {
452  return `${value.path}\0${value.entry === true}`
453}
454
455/** A recursive read of a plain path, with no glob and no find selection: a search that names its root. */
456function isSearchRoot(effect: Effect): boolean {
457  return effect.kind === 'read' && effect.recursive && effect.glob === undefined && effect.selection === undefined
458}
459
460/** Refuses a workaround: renews the blocks that a target or retry finding names, records the decision, and answers the reason Claude reads. */
461async function stop(engine: EngineInterface, config: Config, call: Call, found: readonly Finding[], first: Finding, now: number): Promise<{ deny: string }> {
462  await decide(engine, config, call, { action: 'stopped', blockIds: blocksOf(found).map(block => block.id), detail: phrase(first), now }, journal => renewed(journal, renewable(found), now))
463  return { deny: denyReason(found, now) }
464}
465
466/** Runs a journal write for a stop or a hold; a write that fails is logged to the transcript, and what Arrester does to the call stands. */
467async function saving(engine: EngineInterface, call: Call, action: string, write: () => Promise<unknown>): Promise<void> {
468  try {
469    await write()
470  } catch (error) {
471    engine.ui.log(hostText(couldNotSave(call.tool, action, causeOf(error))))
472  }
473}
474
475/** A decision about a call: what Arrester did, the blocks it was for, its detail, and when the check that led to it ran. */
476type Decided = { action: Exclude<ArresterAction, 'cleared'>; blockIds: string[]; detail: string; now: number }
477
478/** Records a decision after `change` in one write, logs its transcript line, and redraws the status line; a failed write is logged, and the transcript line still says what Arrester did. */
479async function decide(engine: EngineInterface, config: Config, call: Call, decided: Decided, change: (journal: ArresterJournal) => ArresterJournal): Promise<void> {
480  const view = viewOf(call)
481  const { action, blockIds, detail, now } = decided
482  await saving(engine, call, action, async () => {
483    const decision = { at: await engine.clock.now(), action, blockIds, call: view, detail }
484    await update(engine, JOURNAL, journal => withDecision(change(journal), decision))
485  })
486  engine.ui.log(decisionLine({ at: now, action, blockIds, call: view, detail }, view))
487  await sync(engine, config, 'tool.call')
488}
489
490/** Counts open blocks, retaining the cause when the journal or clock cannot be read. */
491async function openCount(engine: EngineInterface, config: Config): Promise<{ count?: number; cause?: string }> {
492  try {
493    return { count: openBlocks(await read(engine, JOURNAL), await engine.clock.now(), config.minutes).length }
494  } catch (error) {
495    return { cause: causeOf(error) }
496  }
497}
498
499/** Holds a workaround for the user: asks, runs the call through the guards once on "Allow once", and refuses it on any other answer or none. */
500async function hold(engine: EngineInterface, config: Config, input: HookArgs<'tool.call'>[1], forward: HookArgs<'tool.call'>[2], call: Call, found: readonly Finding[], first: Finding, now: number): Promise<EventResult<'tool.call'>> {
501  const ids = blocksOf(found).map(block => block.id)
502  const id = input.tool_use_id
503  const ended = (journal: ArresterJournal): ArresterJournal => withoutHeld(journal, id)
504  await saving(engine, call, 'held', () => update(engine, JOURNAL, journal => withHeld(renewed(journal, renewable(found), now), { ...viewOf(call), toolUseId: id, blockIds: ids, since: now })))
505  await sync(engine, config, 'tool.call')
506  let answer: string
507  try {
508    answer = await Promise.race([engine.ui.ask(question(call, first, now), { options: [DENY, ALLOW_ONCE], header: HEADER }), aborted(forward.signal)])
509  } catch (error) {
510    const cause = causeOf(error)
511    engine.ui.log(holdUnanswered(cause), { to: 'debug' })
512    await decide(engine, config, call, { action: 'unanswered', blockIds: ids, detail: cause, now }, ended)
513    return { deny: heldReason({ kind: 'unanswered', cause }, found, now) }
514  }
515  if (answer !== ALLOW_ONCE) {
516    await decide(engine, config, call, { action: 'refused', blockIds: ids, detail: answer, now }, ended)
517    return { deny: heldReason(answer === DENY ? { kind: 'refused' } : { kind: 'typed', text: answer }, found, now) }
518  }
519  await decide(engine, config, call, { action: 'allowed-once', blockIds: ids, detail: ALLOW_ONCE, now }, ended)
520  if (forward.signal.aborted) return abandoned()
521  const result = await forward(input)
522  await observe(engine, config, call, result, forward.signal)
523  return result
524}
525
526/** A promise that rejects when the dispatch is abandoned, so a hold stops waiting. */
527function aborted(signal: AbortSignal): Promise<never> {
528  return new Promise((_, reject) => {
529    const end = (): void => reject(new Error('the call was interrupted'))
530    if (signal.aborted) end()
531    else signal.addEventListener('abort', end, { once: true })
532  })
533}
534
535/** Declares `/arrester` to run at once, even while a turn is in flight; a failure is logged to the transcript, and Arrester keeps checking calls. */
536async function registerCommand(engine: EngineInterface): Promise<void> {
537  try {
538    await engine.command.register({ name: 'arrester', description: COMMAND_DESCRIPTION, argumentHint: ARGUMENT_HINT, immediate: true })
539  } catch (error) {
540    engine.ui.log(hostText(couldNotRegister(causeOf(error))))
541  }
542}
543
544/** `/arrester`: the status text, or `clear` from the user's own origins. */
545async function runCommand(engine: EngineInterface, config: Config, input: HookArgs<'command.run'>[1]): Promise<{ text: string }> {
546  const [verb, id, ...extra] = input.args.split(/\s+/).filter(word => word !== '')
547  const now = await engine.clock.now()
548  if (verb === undefined || (verb === 'status' && id === undefined)) {
549    const journal = await read(engine, JOURNAL)
550    return { text: statusText(journal, openBlocks(journal, now, config.minutes), config.response, config.minutes, now) }
551  }
552  if (verb === 'history' && extra.length === 0) {
553    const page = id === undefined ? 1 : /^[1-5]$/.test(id) ? Number(id) : 0
554    return { text: historyText(await read(engine, JOURNAL), page, now) }
555  }
556  if (verb !== 'clear' || extra.length > 0) return { text: USAGE }
557  if (!USER_ORIGINS.has(input.origin.kind)) return { text: clearRefused(input.origin.kind) }
558  let closed: string[] = []
559  await update(engine, JOURNAL, journal => {
560    const result = cleared(journal, id, now, config.minutes)
561    closed = result.closed
562    return result.journal
563  })
564  await sync(engine, config, 'command.run')
565  return { text: clearedText(closed, id) }
566}
567
hooks/analyze/call.ts 324 lines
1// One tool call as Arrester reads it: the call taken from the
2// input of any event that carries one, its effects on paths, and its retry
3// skeleton and flags. Pure apart from `io.readScript`, which the shell
4// analysis uses.
5
6import { absolute, expandHome, foldPath, MAX_PATH, PATH_TOO_LONG } from './paths'
7import { analyzeShell, MAX_EFFECTS, OPAQUE } from './shell'
8import type { Analysis, Context, Effect, EffectKind, Io } from './types'
9
10/** A tool call: the tool as the model named it, its arguments, the call's id, and the subagent that made it. */
11export type Call = { tool: string; input: Readonly<Record<string, unknown>>; toolUseId: string | undefined; agentId: string | undefined }
12
13/** What a call does, with its whole skeleton and its flags for the retry rule. */
14export type CallAnalysis = Analysis & { skeleton: string; flags: string[]; exact?: string }
15
16const MAX_SKELETON = 1_000
17const INPUT_LIMIT = 'tool input exceeds the serialization depth or work limit'
18const RESERVED = new Set(['tool', 'tool_use_id', 'agentId', 'consent', 'requestMeta'])
19const NOISE = new Set(['description', 'timeout', 'timeout_ms', 'run_in_background', 'dangerouslyDisableSandbox', 'allowed_domains'])
20const SANDBOX_OFF = 'dangerouslyDisableSandbox'
21const FILE_TOOLS = new Map<string, { field: string; kind: EffectKind; recursive: boolean }>([
22  ['Read', { field: 'file_path', kind: 'read', recursive: false }],
23  ['Write', { field: 'file_path', kind: 'write', recursive: false }],
24  ['Edit', { field: 'file_path', kind: 'write', recursive: false }],
25  ['NotebookEdit', { field: 'notebook_path', kind: 'write', recursive: false }],
26  ['LSP', { field: 'filePath', kind: 'read', recursive: false }],
27  ['SendFile', { field: 'files', kind: 'read', recursive: false }],
28  ['SendUserFile', { field: 'files', kind: 'read', recursive: false }],
29  ['Grep', { field: 'path', kind: 'read', recursive: true }],
30  ['Glob', { field: 'path', kind: 'list', recursive: true }],
31])
32
33/** The start of a local `file:` URI, before its path: `file:/`, `file:///`, or `file://localhost/`, in any case. */
34const FILE_URI = /^file:(?:\/\/(?:localhost)?(?=\/)|(?=\/(?!\/)))/i
35
36/** True for a plain object, the shape tool arguments and settings values arrive in. */
37export function isRecord(value: unknown): value is Readonly<Record<string, unknown>> {
38  return typeof value === 'object' && value !== null && !Array.isArray(value)
39}
40
41/** The call in a `tool.call` or `classic.PreToolUse` input, whose arguments sit beside `tool`, `tool_use_id`, `agentId`, `consent`, and `requestMeta`. */
42export function envelopeCall(e: Readonly<Record<string, unknown>>): Call {
43  const input: Record<string, unknown> = {}
44  for (const [key, value] of Object.entries(e)) if (!RESERVED.has(key)) input[key] = value
45  return { tool: String(e.tool), input, toolUseId: typeof e.tool_use_id === 'string' ? e.tool_use_id : undefined, agentId: typeof e.agentId === 'string' ? e.agentId : undefined }
46}
47
48/** The call in a `tool.check` or `classic.PermissionDenied` input, whose arguments arrive as one value of unknown shape. */
49export function inputCall(tool: string, input: unknown, toolUseId: string | undefined, agentId?: string): Call {
50  return { tool, input: isRecord(input) ? input : {}, toolUseId, agentId }
51}
52
53/** The effects, segments, opaque reason, whole skeleton, and flags of a call; a Bash call that leaves the sandbox carries the flag `dangerouslyDisableSandbox`. */
54export async function analyzeCall(call: Call, ctx: Context, io: Io): Promise<CallAnalysis> {
55  const identity = exactIdentity(call, ctx)
56  const command = call.tool === 'Bash' || call.tool === 'Monitor' ? call.input.command : undefined
57  if (typeof command !== 'string') {
58    const effects = toolEffects(call, ctx)
59    const skeleton = toolSkeleton(call)
60    return { ...identity, ...effects, opaque: effects.opaque ?? identity.opaque ?? (skeleton === undefined ? INPUT_LIMIT : undefined), segments: [], skeleton: skeleton === undefined ? '' : fingerprint(skeleton), flags: [] }
61  }
62  const shell = await analyzeShell(command, ctx, io)
63  const off = call.input.dangerouslyDisableSandbox === true ? [SANDBOX_OFF] : []
64  const segments = shell.segments.map(segment => ({ ...segment, skeleton: fingerprint(segment.skeleton), flags: [...segment.flags, ...off].sort() }))
65  const flags = [...new Set(segments.flatMap(segment => segment.flags))].sort()
66  return { ...identity, ...shell, opaque: shell.opaque ?? identity.opaque, segments, skeleton: fingerprint(segments.map(segment => segment.skeleton).sort().join(' ; ')), flags }
67}
68
69/** The effects of a file tool on the paths its input names, opaque past the distinct-effect, input-work, or supported path-length limits; a Grep or Glob call without a path searches the current folder, and a Grep call without a `glob` or `type` filter reads its path as a `fullSearch`. An MCP tool names the paths in its input, and a ReadMcpResourceTool call reads the file of a local `file:` URI. */
70function toolEffects(call: Call, ctx: Context): Pick<Analysis, 'effects' | 'opaque'> {
71  if (call.tool.startsWith('mcp__')) return namedPaths(call.input, ctx.home)
72  if (call.tool === 'ReadMcpResourceTool') {
73    const path = typeof call.input.uri === 'string' && FILE_URI.test(call.input.uri.trim()) ? namedPath(call.input.uri, ctx.home) : undefined
74    return { effects: path === undefined ? [] : [{ path, kind: 'read', recursive: false }] }
75  }
76  const touch = FILE_TOOLS.get(call.tool)
77  if (touch === undefined) return { effects: [] }
78  const given = call.input[touch.field]
79  const value = touch.recursive && (given === undefined || given === '') ? '.' : given
80  if (Array.isArray(value) && value.length > 16_384) return { effects: [], opaque: OPAQUE.tooMany }
81  const names = Array.isArray(value) ? value.filter((name): name is string => typeof name === 'string') : typeof value === 'string' ? [value] : []
82  const fullSearch = call.tool === 'Grep' && !['glob', 'type'].some(field => call.input[field] !== undefined && call.input[field] !== null && call.input[field] !== '')
83  const effects: Effect[] = []
84  const seen = new Set<string>()
85  let work = 0
86  for (const name of names) {
87    work += name.length
88    if (work > 4_000_000) return { effects, opaque: OPAQUE.tooMany }
89    const expanded = expandHome(name, ctx.home)
90    const path = expanded === undefined ? undefined : absolute(expanded, ctx.cwd)
91    if (path !== undefined && path.length > MAX_PATH) return { effects, opaque: PATH_TOO_LONG }
92    if (path !== undefined && !seen.has(path)) {
93      if (effects.length >= MAX_EFFECTS) return { effects, opaque: OPAQUE.tooMany }
94      seen.add(path)
95      effects.push({ path, kind: touch.kind, recursive: touch.recursive, ...(fullSearch ? { fullSearch: true as const } : {}) })
96    }
97  }
98  return { effects }
99}
100
101/** The `name` effects of an MCP tool's input: each string, value or key, that `namedPath` reads as a path, each once, in the order of the input; opaque past the serialization budgets of `sortedJson` or past MAX_EFFECTS paths. */
102function namedPaths(input: unknown, home: string | undefined): Pick<Analysis, 'effects' | 'opaque'> {
103  const effects: Effect[] = []
104  const seen = new Set<string>()
105  const pending: { value: unknown; depth: number }[] = [{ value: input, depth: 0 }]
106  let nodes = 0
107  let remaining = 4_000_000
108  const name = (text: string): string | undefined => {
109    remaining -= text.length
110    if (remaining < 0) return INPUT_LIMIT
111    const path = namedPath(text, home)
112    if (path === undefined || seen.has(path)) return undefined
113    if (effects.length >= MAX_EFFECTS) return OPAQUE.tooMany
114    seen.add(path)
115    effects.push({ path, kind: 'name', recursive: false })
116    return undefined
117  }
118  while (pending.length > 0) {
119    const { value, depth } = pending.pop()!
120    if (++nodes > 16_384 || depth > 64) return { effects, opaque: INPUT_LIMIT }
121    const children: unknown[] = []
122    if (typeof value === 'string') {
123      const opaque = name(value)
124      if (opaque !== undefined) return { effects, opaque }
125    } else if (Array.isArray(value)) {
126      if (value.length + nodes + pending.length > 16_384) return { effects, opaque: INPUT_LIMIT }
127      children.push(...value)
128    } else if (isRecord(value)) {
129      for (const key in value) if (Object.prototype.hasOwnProperty.call(value, key)) {
130        if (children.length + nodes + pending.length >= 16_384) return { effects, opaque: INPUT_LIMIT }
131        const opaque = name(key)
132        if (opaque !== undefined) return { effects, opaque }
133        children.push(value[key])
134      }
135    }
136    for (let i = children.length - 1; i >= 0; i -= 1) pending.push({ value: children[i], depth: depth + 1 })
137  }
138  return { effects }
139}
140
141/** The path that a string names, trimmed: an absolute path, a `~/` path, or a local `file:` URI with its `%` escapes decoded, folded; undefined for anything else, for text over more than one line, and for a path longer than MAX_PATH. */
142function namedPath(text: string, home: string | undefined): string | undefined {
143  const word = text.trim()
144  if (/[\r\n]/.test(word)) return undefined
145  const uri = FILE_URI.exec(word)
146  const path = uri !== null ? unescaped(word.slice(uri[0].length)) : word.startsWith('/') ? word : word.startsWith('~/') ? expandHome(word, home) : undefined
147  if (path === undefined || !path.startsWith('/')) return undefined
148  const folded = foldPath(path)
149  return folded.length > MAX_PATH ? undefined : folded
150}
151
152/** Text with each run of `%` escapes decoded as UTF-8; a run that does not decode stays as written. */
153function unescaped(text: string): string {
154  return text.replace(/(?:%[0-9A-Fa-f]{2})+/g, run => {
155    try {
156      return decodeURIComponent(run)
157    } catch {
158      return run
159    }
160  })
161}
162
163/** `tool:<name>:` and the input as JSON with sorted keys, less the fields that do not change what the call does. */
164function toolSkeleton(call: Call): string | undefined {
165  const kept = keptInput(call, key => !NOISE.has(key) && !(call.tool === 'WebFetch' && key === 'prompt'))
166  const text = kept === undefined ? undefined : sortedJson(kept)
167  return text === undefined ? undefined : `tool:${call.tool}:${text}`
168}
169
170/** Filters top-level input fields with bounded key enumeration before serialization. */
171function keptInput(call: Call, keep: (key: string) => boolean): Record<string, unknown> | undefined {
172  const entries: [string, unknown][] = []
173  let count = 0
174  let size = 0
175  for (const key in call.input) if (Object.prototype.hasOwnProperty.call(call.input, key)) {
176    size += key.length
177    if (++count > 16_384 || size > 4_000_000) return undefined
178    if (keep(key)) entries.push([key, call.input[key]])
179  }
180  return Object.fromEntries(entries)
181}
182
183/** Serializes sorted JSON iteratively, returning no identity when depth, entries, or text exceed the budget. */
184export function sortedJson(value: unknown): string | undefined {
185  type Task = { value: unknown; depth: number } | { text: string }
186  const pending: Task[] = [{ value, depth: 0 }]
187  const output: string[] = []
188  let nodes = 0
189  let remaining = 4_000_000
190  while (pending.length > 0) {
191    const task = pending.pop()!
192    if ('text' in task) {
193      remaining -= task.text.length
194      if (remaining < 0) return undefined
195      output.push(task.text)
196      continue
197    }
198    if (++nodes > 16_384 || task.depth > 64) return undefined
199    const item = task.value
200    if (Array.isArray(item)) {
201      if (item.length + nodes + pending.length > 16_384) return undefined
202      output.push('[')
203      remaining -= 1
204      pending.push({ text: ']' })
205      for (let i = item.length - 1; i >= 0; i -= 1) {
206        pending.push({ value: item[i], depth: task.depth + 1 })
207        if (i > 0) pending.push({ text: ',' })
208      }
209    } else if (isRecord(item)) {
210      const keys: string[] = []
211      for (const key in item) if (Object.prototype.hasOwnProperty.call(item, key)) {
212        if (keys.length + nodes + pending.length >= 16_384 || key.length > remaining) return undefined
213        remaining -= key.length
214        keys.push(key)
215      }
216      keys.sort()
217      output.push('{')
218      remaining -= 1
219      pending.push({ text: '}' })
220      for (let i = keys.length - 1; i >= 0; i -= 1) {
221        const key = keys[i]!
222        pending.push({ value: item[key], depth: task.depth + 1 }, { text: `${JSON.stringify(key)}:` })
223        if (i > 0) pending.push({ text: ',' })
224      }
225    } else {
226      if (typeof item === 'string' && item.length > remaining) return undefined
227      if (typeof item === 'bigint') return undefined
228      pending.push({ text: JSON.stringify(item) ?? 'null' })
229    }
230  }
231  return output.join('')
232}
233
234/** A skeleton as Arrester keeps it: whole up to 1,000 characters, else its first 200, its length, and a 32-bit FNV-1a hash of all of it. */
235function fingerprint(skeleton: string): string {
236  if (skeleton.length <= MAX_SKELETON) return skeleton
237  let hash = 0x811c9dc5
238  for (let i = 0; i < skeleton.length; i += 1) hash = Math.imul(hash ^ skeleton.charCodeAt(i), 0x01000193) >>> 0
239  return `${skeleton.slice(0, 200)}…#${skeleton.length}:${hash.toString(16)}`
240}
241
242/** Hashes complete syntax-preserving input and context into a bounded exact-retry identity. */
243function exactIdentity(call: Call, ctx: Context): { exact?: string; opaque?: string } {
244  const shell = call.tool === 'Bash' || call.tool === 'Monitor'
245  const kept = keptInput(call, key => key !== 'requestMeta' && !(shell && ['description', 'timeout', 'timeout_ms'].includes(key)))
246  const text = kept === undefined ? undefined : sortedJson({ tool: call.tool, cwd: ctx.cwd, home: ctx.home, input: kept })
247  return text === undefined ? { opaque: INPUT_LIMIT } : { exact: `sha256:${digest(text)}` }
248}
249
250/** The SHA-256 round constants. */
251const ROUNDS = [
252  0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
253  0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
254  0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
255  0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
256  0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
257  0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
258  0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
259  0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
260]
261
262/** Rotates one 32-bit word right. */
263function rotate(word: number, bits: number): number {
264  return word >>> bits | word << (32 - bits)
265}
266
267/** SHA-256 over every UTF-8 byte, using one fixed-size compression buffer. */
268function digest(text: string): string {
269  const state = [0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19]
270  const words = new Int32Array(64)
271  let offset = 0
272  let bytes = 0
273  const put = (byte: number): void => {
274    words[offset >>> 2] = words[offset >>> 2]! | byte << (24 - (offset % 4) * 8)
275    offset += 1
276    bytes += 1
277    if (offset !== 64) return
278    for (let i = 16; i < 64; i += 1) {
279      const a = words[i - 15]!
280      const b = words[i - 2]!
281      words[i] = words[i - 16]! + (rotate(a, 7) ^ rotate(a, 18) ^ a >>> 3) + words[i - 7]! + (rotate(b, 17) ^ rotate(b, 19) ^ b >>> 10)
282    }
283    let [a, b, c, d, e, f, g, h] = state as [number, number, number, number, number, number, number, number]
284    for (let i = 0; i < 64; i += 1) {
285      const first = h + (rotate(e, 6) ^ rotate(e, 11) ^ rotate(e, 25)) + ((e & f) ^ (~e & g)) + ROUNDS[i]! + words[i]!
286      const second = (rotate(a, 2) ^ rotate(a, 13) ^ rotate(a, 22)) + ((a & b) ^ (a & c) ^ (b & c))
287      h = g
288      g = f
289      f = e
290      e = d + first | 0
291      d = c
292      c = b
293      b = a
294      a = first + second | 0
295    }
296    const next = [a, b, c, d, e, f, g, h]
297    for (let i = 0; i < 8; i += 1) state[i] = state[i]! + next[i]! | 0
298    words.fill(0, 0, 16)
299    offset = 0
300  }
301  for (const ch of text) {
302    const point = ch.codePointAt(0)!
303    if (point < 128) put(point)
304    else if (point < 2048) {
305      put(0xc0 | point >>> 6)
306      put(0x80 | point & 63)
307    } else if (point < 65536) {
308      put(0xe0 | point >>> 12)
309      put(0x80 | point >>> 6 & 63)
310      put(0x80 | point & 63)
311    } else {
312      put(0xf0 | point >>> 18)
313      put(0x80 | point >>> 12 & 63)
314      put(0x80 | point >>> 6 & 63)
315      put(0x80 | point & 63)
316    }
317  }
318  const length = bytes
319  put(128)
320  while (offset !== 56) put(0)
321  for (const half of [Math.floor(length / 0x20000000), length * 8 >>> 0]) for (const shift of [24, 16, 8, 0]) put(half >>> shift & 255)
322  return state.map(word => (word >>> 0).toString(16).padStart(8, '0')).join('')
323}
324
hooks/analyze/paths.ts 597 lines
1// Path arithmetic for Arrester: lexical folding, globs, braces, and how an
2// effect's path stands to a protected path. Pure: no `$`.
3
4import type { Effect } from './types'
5
6/** How an effect's path stands to a protected path. */
7export type Relation = 'same' | 'inside' | 'contains'
8
9/** What `relation` reads of an effect: a path, or a glob's prefix folder and remaining glob segments. */
10type Reach = Pick<Effect, 'path' | 'glob' | 'globOptions' | 'recursive' | 'selection'>
11
12/** The comparison work one check has done, in character steps. */
13export type Meter = { steps: number; uncertain?: true; limit?: number }
14
15/** The longest path operand that analysis supports. */
16export const MAX_PATH = 4_096
17
18/** A definite file operand exceeded the supported path length. */
19export const PATH_TOO_LONG = 'a file path exceeds the 4,096-character analysis limit'
20
21const REMOTE = /^[^/]*:/
22const GLOB_CHAR = /[*?[]/
23/** The most work one glob comparison takes before returning uncertainty. */
24const MAX_GLOB_STEPS = 65_536
25/** How deep brace groups may nest inside one another. */
26const MAX_BRACE_DEPTH = 32
27
28/** True when the text holds a glob character: `*`, `?`, or `[`. */
29export function hasGlob(text: string): boolean {
30  return GLOB_CHAR.test(text)
31}
32
33/** True for a URL or a remote `host:path` operand: a colon before any slash. */
34export function isRemote(word: string): boolean {
35  return REMOTE.test(word)
36}
37
38/** True when an operand of a program Arrester does not know looks like a path. */
39export function looksLikePath(word: string): boolean {
40  return !isRemote(word) && (word.includes('/') || word.startsWith('.') || word.startsWith('~'))
41}
42
43/** An absolute path with `.`, `..`, and repeated or trailing slashes folded. */
44export function foldPath(path: string): string {
45  const kept: string[] = []
46  for (const part of path.split('/')) {
47    if (part === '..') kept.pop()
48    else if (part !== '' && part !== '.') kept.push(part)
49  }
50  return `/${kept.join('/')}`
51}
52
53/** The absolute lexical path of `path` read in the absolute folder `dir`. */
54export function joinPath(dir: string, path: string): string {
55  return foldPath(path.startsWith('/') ? path : `${dir}/${path}`)
56}
57
58/** The absolute lexical path of `path` read in `cwd`; undefined for a relative path when the folder is unknown. */
59export function absolute(path: string, cwd: string | undefined): string | undefined {
60  if (path.startsWith('/')) return foldPath(path)
61  return cwd === undefined ? undefined : joinPath(cwd, path)
62}
63
64/** The folder that holds `path`; `/` for `/`. */
65export function dirnameOf(path: string): string {
66  return foldPath(`${path}/..`)
67}
68
69/** The last segment of `path`. */
70export function basenameOf(path: string): string {
71  return path.slice(path.lastIndexOf('/') + 1)
72}
73
74/** `~` and `~/…` with the home folder; undefined when the home folder is unknown; other words as they are. */
75export function expandHome(word: string, home: string | undefined): string | undefined {
76  if (word !== '~' && !word.startsWith('~/')) return word
77  return home === undefined ? undefined : `${home}${word.slice(1)}`
78}
79
80/** One piece of a brace expansion: literal text or a group of alternatives. */
81type Brace = { text: string } | { choices: Brace[][] }
82
83/** Every brace expansion of `text`, in order: comma lists, nested ones too; undefined past `max` words, which are counted before any is built, or past MAX_BRACE_DEPTH levels. */
84export function expandBraces(text: string, max: number): string[] | undefined {
85  if (!text.includes('{')) return [text]
86  const pieces = new BraceReader(text).read(0, text.length, 0)
87  if (pieces === undefined) return undefined
88  const count = countOf(pieces, max)
89  if (count > max || count * text.length > 4_000_000) return undefined
90  return build(pieces)
91}
92
93/** The brace pairs of a text, found in one pass, and the commas each pair holds at its own level. */
94class BraceReader {
95  private readonly text: string
96  private readonly closes = new Map<number, number>()
97  private readonly commas = new Map<number, number[]>()
98
99  constructor(text: string) {
100    this.text = text
101    const open: number[] = []
102    for (let i = 0; i < text.length; i += 1) {
103      const ch = text.charAt(i)
104      if (ch === '{') open.push(i)
105      else if (ch === '}' && open.length > 0) this.closes.set(open.pop() as number, i)
106      else if (ch === ',' && open.length > 0) {
107        const at = open[open.length - 1] as number
108        const commas = this.commas.get(at)
109        if (commas === undefined) this.commas.set(at, [i])
110        else commas.push(i)
111      }
112    }
113  }
114
115  /** The pieces of `text` from `start` to `end`; a pair with no comma at its level stays literal. */
116  read(start: number, end: number, depth: number): Brace[] | undefined {
117    if (depth > MAX_BRACE_DEPTH) return undefined
118    const pieces: Brace[] = []
119    let literal = ''
120    let i = start
121    while (i < end) {
122      const close = this.closes.get(i)
123      const group = close === undefined || close >= end ? undefined : this.group(i, close, depth)
124      if (group === null) return undefined
125      if (group === undefined) {
126        literal += this.text.charAt(i)
127        i += 1
128        continue
129      }
130      if (literal !== '') pieces.push({ text: literal })
131      literal = ''
132      pieces.push(group)
133      i = (close as number) + 1
134    }
135    if (literal !== '') pieces.push({ text: literal })
136    return pieces
137  }
138
139  /** The group of the pair from `open` to `close`; undefined when it is literal text; null when it nests too deep. */
140  private group(open: number, close: number, depth: number): Brace | undefined | null {
141    const commas = this.commas.get(open)
142    if (commas === undefined) return undefined
143    const choices: Brace[][] = []
144    const bounds = [open, ...commas, close]
145    for (let k = 0; k + 1 < bounds.length; k += 1) {
146      const choice = this.read((bounds[k] as number) + 1, bounds[k + 1] as number, depth + 1)
147      if (choice === undefined) return null
148      choices.push(choice)
149    }
150    return { choices }
151  }
152}
153
154/** How many words the pieces expand to, stopping once the count passes `max`. */
155function countOf(pieces: readonly Brace[], max: number): number {
156  let total = 1
157  for (const piece of pieces) {
158    if ('text' in piece) continue
159    let options = 0
160    for (const choice of piece.choices) options += countOf(choice, max)
161    total *= options
162    if (total > max) return max + 1
163  }
164  return total
165}
166
167/** The words the pieces expand to, in order. */
168function build(pieces: readonly Brace[]): string[] {
169  let words = ['']
170  for (const piece of pieces) {
171    const options = 'text' in piece ? [piece.text] : piece.choices.flatMap(build)
172    words = words.flatMap(word => options.map(option => word + option))
173  }
174  return words
175}
176
177/** A glob path as its literal prefix folder and remaining glob segments; undefined when no segment holds a glob character. */
178export function splitGlob(path: string): { path: string; glob: string } | undefined {
179  const segments = path.split('/').filter(segment => segment !== '')
180  const at = segments.findIndex(hasGlob)
181  const glob = segments[at]
182  return glob === undefined ? undefined : { path: `/${segments.slice(0, at).join('/')}`, glob: segments.slice(at).join('/') }
183}
184
185/** ASCII members of the standard POSIX character classes. */
186const CHARACTER_CLASSES: Readonly<Record<string, RegExp>> = {
187  alnum: /^[A-Za-z0-9]$/, alpha: /^[A-Za-z]$/, blank: /^[ \t]$/, cntrl: /^[\x00-\x1f\x7f]$/,
188  digit: /^[0-9]$/, graph: /^[!-~]$/, lower: /^[a-z]$/, print: /^[ -~]$/,
189  punct: /^[!-/:-@[-`{-~]$/, space: /^[ \t\r\n\v\f]$/, upper: /^[A-Z]$/, xdigit: /^[A-Fa-f0-9]$/,
190}
191
192/** The closing bracket of a glob expression, skipping named character classes and a leading literal bracket. */
193function bracketEnd(pattern: string, start: number): number {
194  let i = start + 1
195  if (pattern.charAt(i) === '!' || pattern.charAt(i) === '^') i += 1
196  if (pattern.charAt(i) === ']') i += 1
197  for (; i < pattern.length; i += 1) {
198    if (pattern.startsWith('[:', i)) {
199      const end = pattern.indexOf(':]', i + 2)
200      if (end < 0) return pattern.indexOf(']', i + 2)
201      i = end + 1
202      continue
203    }
204    if (pattern.charAt(i) === ']') return i
205  }
206  return -1
207}
208
209/** One step of a glob: the index after the token at `p` when it matches `ch`, else -1. */
210function stepGlob(pattern: string, p: number, ch: string): number {
211  const token = pattern.charAt(p)
212  if (token === '?') return p + 1
213  if (token !== '[') return token === ch ? p + 1 : -1
214  const end = bracketEnd(pattern, p)
215  if (end < 0) return ch === '[' ? p + 1 : -1
216  const negate = pattern.charAt(p + 1) === '!' || pattern.charAt(p + 1) === '^'
217  const body = pattern.slice(negate ? p + 2 : p + 1, end)
218  let hit = false
219  for (let i = 0; i < body.length; i += 1) {
220    if (body.startsWith('[:', i)) {
221      const end = body.indexOf(':]', i + 2)
222      if (end >= 0) {
223        hit = hit || CHARACTER_CLASSES[body.slice(i + 2, end)]?.test(ch) === true
224        i = end + 1
225        continue
226      }
227    }
228    if (body.charAt(i + 1) === '-' && i + 2 < body.length) {
229      hit = hit || (body.charAt(i) <= ch && ch <= body.charAt(i + 2))
230      i += 2
231    } else hit = hit || body.charAt(i) === ch
232  }
233  return hit === negate ? -1 : end + 1
234}
235
236/** The pattern with each run of `*` as one `*`, which matches the same names. */
237function compact(pattern: string): string {
238  return pattern.replace(/\*+/g, '*')
239}
240
241/** Charges actual comparison work without interpreting exhaustion as a match. */
242class Comparison {
243  private steps = 0
244
245  constructor(private readonly meter?: Meter) {}
246
247  /** True while this comparison remains inside its work budget. */
248  spend(units = 1): boolean {
249    this.steps += units
250    if (this.meter !== undefined) this.meter.steps += units
251    return this.steps <= MAX_GLOB_STEPS && (this.meter === undefined || this.meter.steps <= (this.meter.limit ?? Infinity))
252  }
253}
254
255/** Safe literal edges stop impossible comparisons before wildcard work. */
256function edges(pattern: string, tokens: readonly GlobToken[]): { prefix: string; suffix: string } {
257  const literal = (token: GlobToken): string | undefined => 'star' in token || pattern.charAt(token.at) === '?' || pattern.charAt(token.at) === '[' && token.end > token.at ? undefined : pattern.charAt(token.at)
258  let prefix = ''
259  let suffix = ''
260  for (const token of tokens) {
261    const ch = literal(token)
262    if (ch === undefined) break
263    prefix += ch
264  }
265  for (let i = tokens.length - 1; i >= 0; i -= 1) {
266    const ch = literal(tokens[i]!)
267    if (ch === undefined) break
268    suffix = ch + suffix
269  }
270  return { prefix, suffix }
271}
272
273/** Matches a complete name, or returns uncertainty when bounded comparison work expires. */
274export function globMatch(pattern: string, name: string, meter?: Meter, dot = false): boolean | 'uncertain' {
275  if (!dot && name.startsWith('.') && !pattern.startsWith('.')) return false
276  const work = new Comparison(meter)
277  if (!work.spend(pattern.length + name.length)) return 'uncertain'
278  const glob = compact(pattern)
279  const tokens = globTokens(glob, work)
280  if (tokens === undefined) return 'uncertain'
281  const edge = edges(glob, tokens)
282  if (!name.startsWith(edge.prefix) || !name.endsWith(edge.suffix)) return false
283  let p = 0
284  let n = 0
285  let star = -1
286  let mark = 0
287  while (n < name.length) {
288    if (!work.spend(glob.charAt(p) === '[' ? glob.length - p : 1)) return 'uncertain'
289    if (glob.charAt(p) === '*') {
290      star = p
291      mark = n
292      p += 1
293      continue
294    }
295    const next = p < glob.length ? stepGlob(glob, p, name.charAt(n)) : -1
296    if (next >= 0) {
297      p = next
298      n += 1
299    } else if (star >= 0) {
300      p = star + 1
301      mark += 1
302      n = mark
303    } else return false
304  }
305  while (glob.charAt(p) === '*') p += 1
306  return p === glob.length
307}
308
309/** One token of a glob segment: `*`, or one character position of the pattern (`?`, a class, or a literal). */
310type GlobToken = { star: true } | { at: number; end: number }
311
312/** The tokens of a compacted glob segment. */
313function globTokens(glob: string, work: Comparison): GlobToken[] | undefined {
314  const tokens: GlobToken[] = []
315  for (let p = 0; p < glob.length; ) {
316    if (!work.spend(glob.charAt(p) === '[' ? glob.length - p : 1)) return undefined
317    if (glob.charAt(p) === '*') {
318      tokens.push({ star: true })
319      p += 1
320      continue
321    }
322    const end = glob.charAt(p) === '[' ? bracketEnd(glob, p) : -1
323    tokens.push({ at: p, end: Math.max(p, end) })
324    p = end < 0 ? p + 1 : end + 1
325  }
326  return tokens
327}
328
329/** Finds one character accepted by every current token, including bracket intersections. */
330function sharedCharacter(globs: readonly string[], tokens: readonly (GlobToken | undefined)[], leadingDot: boolean, work: Comparison): boolean | 'uncertain' {
331  if (tokens.some(token => token === undefined)) return false
332  const fixed = tokens.flatMap((token, i) => {
333    if (token === undefined || 'star' in token) return []
334    const pattern = globs[i]!
335    const ch = pattern.charAt(token.at)
336    return ch === '?' || ch === '[' && token.end > token.at ? [] : [ch.charCodeAt(0)]
337  })
338  const candidates = new Set<number>(fixed.length > 0 ? [fixed[0]!] : [65535])
339  if (fixed.length === 0) for (let point = 1; point < 128; point += 1) candidates.add(point)
340  for (const [i, token] of (fixed.length > 0 ? [] : tokens).entries()) {
341    if (token === undefined || 'star' in token) continue
342    const pattern = globs[i]!
343    const end = token.end
344    if (!work.spend(Math.max(1, end - token.at + 1))) return 'uncertain'
345    for (let j = token.at; j <= Math.max(token.at, end); j += 1) {
346      const point = pattern.charCodeAt(j)
347      for (const value of [point - 1, point, point + 1]) if (value > 0 && value <= 65535) candidates.add(value)
348    }
349  }
350  for (const point of candidates) {
351    if (point === 47 || point === 46 && !leadingDot) continue
352    let matches = true
353    for (const [i, token] of tokens.entries()) {
354      if (token === undefined || 'star' in token) continue
355      const pattern = globs[i]!
356      if (!work.spend(pattern.charAt(token.at) === '[' ? pattern.length - token.at : 1)) return 'uncertain'
357      if (stepGlob(pattern, token.at, String.fromCharCode(point)) < 0) {
358        matches = false
359        break
360      }
361    }
362    if (matches) return true
363  }
364  return false
365}
366
367/** Requires one shared name across all patterns, with bounded product-state traversal. */
368function intersectGlobs(patterns: readonly string[], dots: readonly boolean[], meter?: Meter): boolean | 'uncertain' {
369  const work = new Comparison(meter)
370  if (!work.spend(patterns.reduce((sum, pattern) => sum + pattern.length + 1, 0))) return 'uncertain'
371  const globs = patterns.map(compact)
372  const tokens: GlobToken[][] = []
373  for (const glob of globs) {
374    const parsed = globTokens(glob, work)
375    if (parsed === undefined) return 'uncertain'
376    tokens.push(parsed)
377  }
378  const literalEdges = globs.map((glob, i) => edges(glob, tokens[i]!))
379  for (let i = 0; i < globs.length; i += 1) for (let j = 0; j < i; j += 1) {
380    const a = literalEdges[i]!
381    const b = literalEdges[j]!
382    if (!work.spend(a.prefix.length + b.prefix.length + a.suffix.length + b.suffix.length + 1)) return 'uncertain'
383    if (!a.prefix.startsWith(b.prefix) && !b.prefix.startsWith(a.prefix) || !a.suffix.endsWith(b.suffix) && !b.suffix.endsWith(a.suffix)) return false
384    if (globs[i]!.startsWith('.') && !globs[j]!.startsWith('.') && !dots[j] || globs[j]!.startsWith('.') && !globs[i]!.startsWith('.') && !dots[i]) return false
385  }
386  const pending = [{ at: globs.map(() => 0), started: false }]
387  const seen = new Set<string>()
388  while (pending.length > 0) {
389    if (!work.spend(globs.length + 1)) return 'uncertain'
390    const state = pending.pop()!
391    const key = `${state.started}:${state.at.join(',')}`
392    if (seen.has(key)) continue
393    seen.add(key)
394    if (state.at.every((at, i) => at === tokens[i]!.length)) return true
395    const current = state.at.map((at, i) => tokens[i]![at])
396    for (const [i, token] of current.entries()) if (token !== undefined && 'star' in token) {
397      if (!work.spend(globs.length)) return 'uncertain'
398      const next = [...state.at]
399      next[i] = next[i]! + 1
400      pending.push({ at: next, started: state.started })
401    }
402    const leadingDot = state.started || globs.every((glob, i) => dots[i] || glob.startsWith('.'))
403    const matches = sharedCharacter(globs, current, leadingDot, work)
404    if (matches === 'uncertain') return matches
405    if (matches) pending.push({ at: state.at.map((at, i) => current[i] !== undefined && 'star' in current[i]! ? at : at + 1), started: true })
406  }
407  return false
408}
409
410/** Whether one name matches both glob segments, preserving comparison uncertainty. */
411export function globsMeet(first: string, second: string, meter?: Meter, dot = false, otherDot = false): boolean | 'uncertain' {
412  return intersectGlobs([first, second], [dot, otherDot], meter)
413}
414
415/** The find names of a selection, all of which an entry must match. */
416export function selectionNames(selection: Reach['selection']): string[] {
417  return selection?.names ?? []
418}
419
420/** Requires all predicates to match the same literal name or glob candidate. */
421function namesMatch(names: readonly string[], name: string, glob: boolean, meter?: Meter, dot = false): boolean | 'uncertain' {
422  if (glob) return intersectGlobs([...names, name], [...names.map(() => true), dot], meter)
423  let uncertain = false
424  for (const pattern of names) {
425    const matches = globMatch(pattern, name, meter, true)
426    if (matches === false) return false
427    if (matches === 'uncertain') uncertain = true
428  }
429  return uncertain ? 'uncertain' : true
430}
431
432/** A path or a path segment as a case-insensitive comparison reads it. */
433export function foldCase(segment: string): string {
434  return segment.toLowerCase().replace(/\[:upper:\]/g, '[:lower:]')
435}
436
437/** How the effect stands to a protected path or glob, comparing each segment within the work budget. */
438export function relation(reach: Reach, target: string, meter?: Meter, targetGlob?: string, targetOptions?: Effect['globOptions']): Relation | 'uncertain' | undefined {
439  if (reach.selection !== undefined) return selectedRelation(reach, target, meter, targetGlob, targetOptions)
440  const dot = reach.globOptions?.dot === true
441  const targetDot = targetOptions?.dot === true
442  const nocase = reach.globOptions?.nocase || targetOptions?.nocase
443  const left = reach.path.split('/').filter(Boolean)
444  const right = target.split('/').filter(Boolean)
445  const leftPrefix = left.length
446  const rightPrefix = right.length
447  if (reach.glob !== undefined) left.push(...reach.glob.split('/'))
448  if (targetGlob !== undefined) right.push(...targetGlob.split('/'))
449  if (nocase) {
450    for (let i = 0; i < left.length; i += 1) left[i] = foldCase(left[i]!)
451    for (let i = 0; i < right.length; i += 1) right[i] = foldCase(right[i]!)
452  }
453  if (meter !== undefined) meter.steps += reach.path.length + target.length + (reach.glob?.length ?? 0) + (targetGlob?.length ?? 0)
454  if (left.slice(leftPrefix, -1).includes('**') || right.slice(rightPrefix, -1).includes('**')) return recursiveRelation(left, right, leftPrefix, rightPrefix, reach.recursive, meter, dot, targetDot)
455  let uncertain = false
456  for (let i = 0; i < Math.min(left.length, right.length); i += 1) {
457    const a = left[i] as string
458    const b = right[i] as string
459    const aGlob = i >= leftPrefix && hasGlob(a)
460    const bGlob = i >= rightPrefix && hasGlob(b)
461    const matches = aGlob && bGlob ? globsMeet(a, b, meter, dot, targetDot) : aGlob ? globMatch(a, b, meter, dot) : bGlob ? globMatch(b, a, meter, targetDot) : a === b
462    if (matches === false) return undefined
463    if (matches === 'uncertain') uncertain = true
464  }
465  if (uncertain) return 'uncertain'
466  if (left.length === right.length) return 'same'
467  if (left.length > right.length) return 'inside'
468  return reach.recursive ? 'contains' : undefined
469}
470
471/** Compares a find selection to the target and its ancestors, without inventing deletion of the starting tree. */
472function selectedRelation(reach: Reach, target: string, meter?: Meter, targetGlob?: string, targetOptions?: Effect['globOptions']): Relation | 'uncertain' | undefined {
473  const selection = reach.selection as NonNullable<Reach['selection']>
474  const minDepth = selection.minDepth ?? 0
475  const maxDepth = selection.maxDepth ?? Infinity
476  if (minDepth > maxDepth) return undefined
477  const names = selectionNames(selection)
478  const possible = selectionPossible(selection, meter)
479  if (possible === false) return undefined
480  if (possible === 'uncertain') return possible
481  if (selection.parentOperand !== undefined) return parentRelation(reach, target, meter, targetGlob, targetOptions)
482  const base = relation({ ...reach, selection: undefined, recursive: true }, target, meter, targetGlob, targetOptions)
483  if (base === undefined || base === 'uncertain') return base
484  if (base === 'inside') return base
485  if (base === 'same') {
486    const candidate = targetGlob?.split('/').at(-1) ?? basenameOf(target)
487    const rootGlob = reach.glob?.split('/').at(-1)
488    const predicates = rootGlob === undefined ? names : [...names, rootGlob]
489    const matches = namesMatch(predicates, candidate, targetGlob !== undefined && hasGlob(candidate), meter, targetOptions?.dot)
490    if (matches === 'uncertain') return matches
491    return minDepth === 0 && matches ? 'same' : maxDepth > 0 ? 'inside' : undefined
492  }
493  const descend = selection.descend === true
494  const rootDepth = reach.path.split('/').filter(Boolean).length + (reach.glob?.split('/').length ?? 0)
495  const parts = `${target}${targetGlob === undefined ? '' : `/${targetGlob}`}`.split('/').filter(Boolean)
496  const rootName = parts[rootDepth - 1] ?? ''
497  if (descend && !selection.file && minDepth === 0) {
498    const matches = namesMatch(names, rootName, targetGlob !== undefined && rootDepth > target.split('/').filter(Boolean).length && hasGlob(rootName), meter, targetOptions?.dot)
499    if (matches === 'uncertain') return matches
500    if (matches) return 'contains'
501  }
502  const suffix = parts.slice(rootDepth)
503  for (let i = 0; i < suffix.length; i += 1) {
504    if (i + 1 < minDepth || i + 1 > maxDepth) continue
505    const last = i === suffix.length - 1
506    if (!last && (selection.file || !descend)) continue
507    const part = suffix[i] as string
508    const matches = namesMatch(names, part, targetGlob !== undefined && rootDepth + i >= target.split('/').filter(Boolean).length && hasGlob(part), meter, targetOptions?.dot)
509    if (matches === 'uncertain') return matches
510    if (matches) return last ? 'same' : 'contains'
511  }
512  return undefined
513}
514
515/** Compares an execdir sibling operand below each possible selected entry's parent, retaining selection depth. */
516function parentRelation(reach: Reach, target: string, meter?: Meter, targetGlob?: string, targetOptions?: Effect['globOptions']): Relation | 'uncertain' | undefined {
517  const selection = reach.selection!
518  const rootDepth = reach.path.split('/').filter(Boolean).length + (reach.glob?.split('/').length ?? 0)
519  const parts = target.split('/').filter(Boolean)
520  const min = Math.max(0, (selection.minDepth ?? 1) - 1)
521  const max = (selection.maxDepth ?? Infinity) - 1
522  for (let depth = min; depth <= max && rootDepth + depth <= parts.length; depth += 1) {
523    const parent = `/${parts.slice(0, rootDepth + depth).join('/')}`
524    const base = relation({ path: reach.path, glob: reach.glob, recursive: true }, parent, meter)
525    if (base === undefined) continue
526    if (base === 'uncertain') return base
527    const operand = joinPath(parent, selection.parentOperand!)
528    const glob = selection.parentGlob ? splitGlob(operand) : undefined
529    const found = relation({ path: glob?.path ?? operand, glob: glob?.glob, globOptions: reach.globOptions, recursive: selection.descend === true }, target, meter, targetGlob, targetOptions)
530    if (found !== undefined) return found
531  }
532  return undefined
533}
534
535/** Matches recursive directory segments with bounded iterative states, preserving ordinary wildcard and leading-dot rules. */
536function recursiveRelation(left: readonly string[], right: readonly string[], leftPrefix: number, rightPrefix: number, recursive: boolean, meter?: Meter, dot = false, targetDot = false): Relation | 'uncertain' | undefined {
537  const pending: [number, number][] = [[0, 0]]
538  const seen = new Set<number>()
539  const work: Meter = { steps: 0 }
540  let found: Relation | undefined
541  let uncertain = false
542  while (pending.length > 0) {
543    work.steps += 1
544    if (work.steps > MAX_GLOB_STEPS) {
545      uncertain = true
546      break
547    }
548    const [i, j] = pending.pop() as [number, number]
549    const key = i * (right.length + 1) + j
550    if (seen.has(key)) continue
551    seen.add(key)
552    const a = left[i]
553    const b = right[j]
554    const aStar = i >= leftPrefix && i + 1 < left.length && a === '**'
555    const bStar = j >= rightPrefix && j + 1 < right.length && b === '**'
556    if (aStar) pending.push([i + 1, j])
557    if (bStar) pending.push([i, j + 1])
558    if (a === undefined || b === undefined) {
559      if (a === undefined && b === undefined) {
560        found = 'same'
561        break
562      }
563      if (b === undefined) found = 'inside'
564      else if (recursive && found === undefined) found = 'contains'
565      continue
566    }
567    if (aStar || bStar) {
568      if (aStar && (dot || !b.startsWith('.'))) pending.push([i, j + 1])
569      if (bStar && (targetDot || !a.startsWith('.'))) pending.push([i + 1, j])
570      continue
571    }
572    const aGlob = i >= leftPrefix && hasGlob(a)
573    const bGlob = j >= rightPrefix && hasGlob(b)
574    const matches = aGlob && bGlob ? globsMeet(a, b, work, dot, targetDot) : aGlob ? globMatch(a, b, work, dot) : bGlob ? globMatch(b, a, work, targetDot) : a === b
575    if (work.steps > MAX_GLOB_STEPS) {
576      uncertain = true
577      break
578    }
579    if (matches === 'uncertain') uncertain = true
580    else if (matches) pending.push([i + 1, j + 1])
581  }
582  if (meter !== undefined) meter.steps += work.steps
583  return found ?? (uncertain ? 'uncertain' : undefined)
584}
585
586/** A stable equality key retains every selection constraint without depending on property order. */
587export function selectionKey(selection: Reach['selection']): string | undefined {
588  if (selection === undefined) return undefined
589  return JSON.stringify([selection.parentOperand, selection.parentGlob === true, [...new Set(selectionNames(selection))].sort(), selection.file === true, selection.minDepth ?? 0, selection.maxDepth, selection.descend])
590}
591
592/** Whether a find conjunction has any possible shared name, with explicit bounded uncertainty. */
593export function selectionPossible(selection: Reach['selection'], meter?: Meter): boolean | 'uncertain' {
594  const names = selectionNames(selection)
595  return intersectGlobs(names, names.map(() => true), meter)
596}
597
hooks/analyze/shell.ts 4290 lines
1// The shell analysis: what a command line does to which paths,
2// the retry skeleton of each command, and why some of it cannot be read.
3// Pure apart from io.readScript; it never runs anything.
4
5import { scanCode } from './code'
6import type { Dialect } from './code'
7import { heredocWord, lex, spellingOf, TOO_LONG, UNPARSED, unknownSource, wordText } from './lexer'
8import type { Part, Redirect, Token, Word } from './lexer'
9import { absolute, basenameOf, dirnameOf, expandBraces, globMatch, hasGlob, isRemote, joinPath, looksLikePath, MAX_PATH, PATH_TOO_LONG, selectionKey, selectionNames, selectionPossible, splitGlob } from './paths'
10import type { Meter } from './paths'
11import { ALWAYS_RECURSIVE, ARCHIVE, ATTACHED, CLAUDE_OFF, CLAUDE_TOOLS, CODE_FIRST, CODE_FLAGS, COPY, DESTROY, family, GIT_SUB_VALUES, hasFlag, INTERPRETER, isDecoder, isKnown, KEYWORDS, LIST, MCP_RUNS, MODE_FIRST, PASS_THROUGH, PATHY, PATTERN_FIRST, PLUGIN_OFF, READ, RECURSIVE_FLAGS, RUNNERS, RUNUSER, SHELLS, TRIVIAL, UNLINKERS, valuesOf, WRAPPERS, WRITE } from './programs'
12import type { Runner, Values } from './programs'
13import type { Analysis, Context, Effect, EffectKind, Ignored, Io, Segment } from './types'
14
15/** Why some code cannot be read. */
16export const OPAQUE = {
17  variableProgram: 'the command names its program with a variable',
18  unresolvedDeletion: 'a deletion names its target through a value that Arrester cannot read',
19  variableCode: 'code comes from a variable or a command substitution',
20  unreadableStdin: 'a shell or interpreter runs code from a command that Arrester cannot read',
21  decoded: 'a decoded or transformed text runs as code',
22  scriptUnreadable: 'a script cannot be read',
23  scriptTooLong: 'a script is too long to read',
24  tooDeep: 'code nests deeper than Arrester reads',
25  shellProgram: 'su or runuser names a shell that Arrester cannot read',
26  tooMany: 'the call names too many paths to read',
27  interrupted: 'the call was interrupted',
28} as const
29
30/** The bypass of a nested `claude` run, which does not inherit the open blocks. */
31export const NESTED_SESSION = 'a nested session'
32
33/** How deep `sh -c`, `eval`, command substitutions, and scripts may nest. */
34const MAX_DEPTH = 3
35
36/** The most values one word may resolve to, counted before they are built. */
37const MAX_VALUES = 256
38
39/** The most word values one analysis resolves in all. */
40const MAX_TOTAL_VALUES = 16_384
41
42/** The most effects one analysis keeps. */
43export const MAX_EFFECTS = 256
44
45/** The longest value one word may build, counted before it is built: the longest command Arrester reads. */
46const MAX_TEXT = 100_000
47
48/** The most work one analysis does, in characters read, characters built, and values copied; past it the analysis stops and is opaque. */
49const MAX_WORK = 4_000_000
50
51/** The most work that the code of 1.2.0 does for one call, across both walks: each token, word character, tree node, copied entry, queue step, and table lookup of rule A4, its second walk, the newer reading, the array rule, and the profile. Past it the analysis stops and is opaque. */
52const NEW_WORK = 1_500_000
53
54/** The longest call that the code of 1.2.0 reads: a longer call that needs the newer reading keeps the result of 1.1.3, which did not parse it, and a longer call that runs a function that it defines, which rule A4 walks a second time, is opaque. */
55const NEW_LENGTH = 10_000
56
57/** One word value: its text, whether it represents glob matches, and a pattern that preserves quoted wildcard positions. */
58type Value = { selected?: true; text: string; glob: boolean; pattern?: string; globOptions?: Effect['globOptions']; selection?: Effect['selection'] }
59
60/** A word as the walker sees it: its text as typed, and its values (undefined when it does not resolve); `upstream` marks an operand that reaches below its paths, as one that `xargs` or `find -exec` takes from what came before; `process` marks a process substitution, whose generated file name is unknown; `possible` holds, for a word that does not resolve, the values that the default or the alternate of a parameter operator gives it. */
61type Operand = { raw: string; values: Value[] | undefined; upstream?: true; process?: true; possible?: Value[] }
62
63/** The shell state one command line runs in; `reaching` names the variables bound to operands that reach below their paths; `possible` holds the possible values of a variable that has no known value, from an assignment of a parameter operator's default; `functions` holds the bodies of the functions defined so far and `named` the text of each alias; `arrays` holds the elements of each array whose expansions resolve, as `assignArray` says; `lost` marks a shell where `HOME` and `PWD` are unknown, as `forgetAssigned` says. The links made and the files written are on disk, so every copy shares them. */
64type State = {
65  status?: boolean
66  terminated?: boolean
67  shell?: string
68  globOptions?: Effect['globOptions']
69  noglob?: boolean
70  globstar?: boolean
71  positional?: readonly Operand[]
72  execdir?: { root: string; selection: NonNullable<Effect['selection']> }
73  cwd: string | undefined
74  prev: string | undefined
75  stack: readonly (string | undefined)[]
76  vars: Map<string, Value[]>
77  reaching: Set<string>
78  exported: Set<string>
79  removed: Set<string>
80  emptyEnv: boolean
81  aliases: Map<string, string>
82  written: Map<string, string | undefined>
83  functions: Map<string, Simple>
84  named: Map<string, string>
85  possible: Map<string, Value[]>
86  arrays: Map<string, Operand[]>
87  lost?: true
88}
89
90/** The printing options on a shell's command line, in order: `verbose` prints the code that the shell reads, and `xtrace` the commands that it runs. */
91type Printing = { verbose?: boolean; xtrace?: boolean }
92
93/** One command with its redirections and optional compound body; `definition` names the function that the command defines, or is empty for a name that is not plain text, and `exits` marks a function body that names `return` or `exit`. `arithmetic` marks a subshell whose `(` another `(` follows at once, as in `((`, as `arithmeticOf` says. `cases` holds the word and the branches of a `case`, each with its patterns, its body, and the operator that ends it. */
94type Simple = { words: Word[]; redirects: Redirect[]; body?: Pipeline[]; subshell?: boolean; arithmetic?: true; branches?: { condition?: Pipeline[]; body: Pipeline[] }[]; definition?: string; exits?: true; loop?: { condition?: Pipeline[]; until?: boolean; name?: string; words?: Word[] }; cases?: { word: Word; clauses: { patterns: Word[]; body: Pipeline[]; end: string }[] } }
95
96/** One pipeline and the operator that follows it. */
97type Pipeline = { commands: Simple[]; negate: boolean; after: string }
98
99/** A redirection, its resolved target, and for a heredoc or a here-string the text it gives. */
100type Redirected = { redirect: Redirect; target: Operand; text?: { text: string; uncertain?: true } }
101
102/** A descriptor's open destination, including inherited output streams and closed descriptors. */
103type Destination = Redirected | 'stdout' | 'stderr' | undefined
104
105/** What a command reads on stdin, and what it hands the next stage: nothing, literal text, a file's content, a listing of paths at or below `paths`, decoded text, or anything else, which `closed` marks as a stdin that `<&-` closed. */
106type Stdin = { kind: 'none' } | { kind: 'literal'; text: string; uncertain?: true; positions?: number[] } | { kind: 'script'; path: string } | { kind: 'paths'; paths: Operand[]; separator: string } | { kind: 'decoder' } | { kind: 'other'; closed?: true }
107
108const NONE: Stdin = { kind: 'none' }
109const OTHER: Stdin = { kind: 'other' }
110const CLOSED: Stdin = { kind: 'other', closed: true }
111const DECODER: Stdin = { kind: 'decoder' }
112
113/** The value of a variable that rule A4 made unknown, as `setUnknown` says: a join of two shells keeps it unknown, and an assignment replaces it. */
114const UNKNOWN: Value[] = []
115
116/** A command's flags and the values of its value-taking flags. */
117type Options = { flags: string[]; given: Map<string, Operand[]>; ordered: { flag: string; value: Operand }[] }
118
119/** A command's flags, its operands, the values of its value-taking flags, and where `--` split the operands. */
120type Parsed = Options & { operands: Operand[]; split: number | undefined }
121
122/** The values that the command substitutions of one command gave, per part, so a word that brace expansion copies walks each one once. */
123type Substituted = Map<Part, string | undefined>
124
125/** What one analysis collects while it walks. */
126class Walk {
127  readonly effects: Effect[] = []
128  private readonly effectKeys = new Set<string>()
129  readonly segments: Segment[] = []
130  readonly home: string | undefined
131  readonly io: Io
132  opaque: string | undefined
133  ignored: Ignored | undefined
134  bypass: string | undefined
135  /** The older script being walked: code in it that cannot be read does not count. */
136  oldScript: string | undefined
137  depth = 0
138  /** How many word values the analysis has resolved so far. */
139  values = 0
140  /** How much work the analysis has done so far. */
141  work = 0
142  /** The aliases whose text is being walked, which do not expand again inside it. */
143  readonly expanding = new Set<string>()
144  /** Set once code that Arrester 1.1.3 did not parse parsed in the modern reading, which then reads all later code of the call, as `parseCommands` says. */
145  modern = false
146  /** How many scripts that the call wrote are being walked; their code keeps the reading of Arrester 1.1.3. */
147  legacy = 0
148  /** Set when code read as Arrester 1.1.3 read it ended a substitution at the `)` of a `case` pattern, which can hide the commands of the `case`; such a call that also needed the modern reading is opaque, as `walkCall` says. */
149  cut = false
150  /** Set when the walk met code that the narrow reading does not follow: an array form that does not resolve, outside an older script; a call of a function whose body is not walked; a `case` with a `;&` or `;;&` end; a command that a wrapper runs whose name is not literal text or is a builtin outside the profile; a read or a write of a file operand that does not resolve, or of a relative path in an unknown folder; or a loop that can read more than one line of literal input. Such a call that also needed the modern reading is opaque, as `walkCall` says. */
151  unread = false
152  /** The command trees that the walk parsed, outside an older script, which `walkCall` checks with `outsideProfile` once the call needs the modern reading. */
153  readonly parsed: Pipeline[][] = []
154  /** Set when a text that the walk parsed, outside an older script, holds `$'…'` or `$"…"`, which the profile does not hold. */
155  quoting = false
156  /** The names of the arrays that the code read so far assigns or declares; an expansion of one of them that does not resolve is opaque. */
157  readonly arrayNames = new Set<string>()
158  /** The arrays whose expansions resolve: each one that the call assigns one time, at its top level, with `NAME=( … )`, and changes nowhere, as `arraysOf` says. */
159  readonly resolvable = new Set<string>()
160  /** The top-level commands that assign the arrays in `resolvable`. */
161  readonly assignments = new Set<Simple>()
162  /** Every body that the code defines for each function name, for rule A4. */
163  readonly bodies = new Map<string, Set<Simple>>()
164  /** Set when a command calls a function that the code defines, so `analyzeShell` walks the call again. */
165  calls = false
166  /** Whether a call of a function makes unknown the variables that its body can assign, as `forgetAssigned` says; only the second walk does. */
167  readonly forgets: boolean
168  /** The work of the code of 1.2.0 so far, which both walks of a call share, as NEW_WORK says. */
169  readonly meter: { used: number }
170  /** The length of the text of the call, as NEW_LENGTH says. */
171  length = 0
172
173  constructor(home: string | undefined, io: Io, forgets = false, meter = { used: 0 }) {
174    this.home = home
175    this.io = io
176    this.forgets = forgets
177    this.meter = meter
178  }
179
180  /** Counts `units` of the work of the code of 1.2.0; past NEW_WORK the analysis stops and is opaque, as past MAX_WORK. */
181  tick(units: number): boolean {
182    this.meter.used += units
183    if (this.meter.used <= NEW_WORK) return true
184    this.opaque ??= OPAQUE.tooMany
185    this.work = MAX_WORK + 1
186    return false
187  }
188
189  /** Marks the analysis opaque, or inside an older script records what was not counted; the first reason stays. */
190  fail(reason: string): void {
191    if (this.oldScript === undefined) this.opaque ??= reason
192    else this.ignored ??= { path: this.oldScript, reason }
193  }
194
195  /** Charges `units` of work; past MAX_WORK the analysis stops and is opaque, inside an older script too, since the rest of the call goes unread. The whole second walk, and the walk of code once the call needs the newer reading, is work of 1.2.0, so there each unit also counts on the meter. */
196  charge(units: number): boolean {
197    if (this.io.signal.aborted) {
198      this.opaque ??= OPAQUE.interrupted
199      this.work = MAX_WORK + 1
200      return false
201    }
202    if ((this.forgets || this.modern) && !this.tick(units)) return false
203    this.work += units
204    if (this.work <= MAX_WORK) return true
205    this.opaque ??= OPAQUE.tooMany
206    return false
207  }
208
209  /** True once the work passed MAX_WORK. */
210  isSpent(): boolean {
211    return this.work > MAX_WORK
212  }
213
214  /** Counts `count` more word values; past MAX_TOTAL_VALUES the analysis is opaque. */
215  count(count: number): boolean {
216    this.values += count
217    if (this.values <= MAX_TOTAL_VALUES) return true
218    this.fail(OPAQUE.tooMany)
219    return false
220  }
221
222  /** Keeps an effect, a `fullSearch` one apart from the same effect without the mark; past MAX_EFFECTS the analysis is opaque. */
223  add(effect: Effect): void {
224    const key = JSON.stringify([effect.path, effect.kind, effect.recursive, effect.entry, effect.glob, effect.globOptions?.dot === true, effect.globOptions?.nocase === true, selectionKey(effect.selection), ...(effect.fullSearch === true ? [true] : [])])
225    if (!this.charge(key.length) || this.effectKeys.has(key)) return
226    if (this.effects.length < MAX_EFFECTS) {
227      this.effectKeys.add(key)
228      this.effects.push(effect)
229    }
230    else this.fail(OPAQUE.tooMany)
231  }
232}
233
234/** Everything a program handler reads. */
235type Run = { walk: Walk; state: State; program: string; args: Operand[]; stdin: Stdin; find?: { starts: Operand[]; expression: FindTerm[] } }
236
237const ASSIGNMENT = /^([A-Za-z_][A-Za-z0-9_]*)(\+?)=/
238/** An assignment of one element of an array, `NAME[…]=` or `NAME[…]+=`. */
239const ELEMENT = /^([A-Za-z_][A-Za-z0-9_]*)\[[^\]]*\]\+?=/
240const ASSIGNERS = new Set(['export', 'declare', 'local', 'readonly'])
241const DIRECTORY_CHANGERS = new Set(['cd', 'pushd', 'popd'])
242const DEVICES = /^\/dev\/(null|stdout|stderr|tty|fd\/[0-9]+)$/
243const DUPLICATE = /^([0-9]+|-)$/
244const TRUNCATING = new Set(['>', '>|', '&>', '>&'])
245const SYMBOLIC_MODE = /^[ugoa]*[-+=][rwxXst]*$/
246const ESCAPED = /\\(x[0-9a-fA-F]|[0-7])|%b/
247const GIT_GLOBAL_VALUES = new Set(['-C', '-c', '--git-dir', '--work-tree', '--namespace'])
248const MARK = /\uE000([0-9]+)\uE001/g
249const MARKED = /[\uE000\uE001]/
250const IN_PLACE_CLUSTER = /^-[A-Za-z]*[iI]$/
251const CLAUDE_NAME = /^claude(?:-code)?$/i
252const CLAUDE_VERSION = /\/claude\/versions\/[^/]+$/i
253
254/** Analyzes one shell command line. A call that runs a function that it defines is walked a second time, where a call makes unknown the variables that its body can assign (rule A4); the effects of both walks count, and either one can make the call opaque, so the rule only adds stops. The retry skeletons are those of the first walk, which reads calls as Arrester 1.1.3 did. */
255export async function analyzeShell(command: string, ctx: Context, io: Io): Promise<Analysis> {
256  const walk = await walkCall(command, ctx, io, false)
257  if (walk.calls && walk.opaque === undefined && !io.signal.aborted && command.length > NEW_LENGTH) walk.fail(TOO_LONG)
258  else if (walk.calls && walk.opaque === undefined && !io.signal.aborted) {
259    const second = await walkCall(command, ctx, io, true, walk.meter)
260    for (const effect of second.effects) walk.add(effect)
261    walk.opaque ??= second.opaque
262    walk.ignored ??= second.ignored
263    walk.bypass ??= second.bypass
264  }
265  return { effects: walk.effects, segments: walk.segments, opaque: walk.opaque, ignored: walk.ignored, bypass: walk.bypass }
266}
267
268/** One walk of a command line; with `forgets`, a call makes unknown the variables that its body can assign. A call that needed the modern reading is opaque with the reason of 1.1.3, which did not parse some of its code, when it holds a substitution that the reading of Arrester 1.1.3 ended at the `)` of a `case` pattern, code that the narrow reading does not follow, as `Walk.unread` says, or anywhere in the code that the walk parsed, a construct outside the minimal profile: `$'…'` or `$"…"` (`Walk.quoting`), a backslash-newline in the text of the call, or what `outsideProfile` finds. */
269async function walkCall(command: string, ctx: Context, io: Io, forgets: boolean, meter?: { used: number }): Promise<Walk> {
270  const walk = new Walk(ctx.home, io, forgets, meter)
271  walk.length = command.length
272  await runText(walk, command, freshState(ctx.cwd), NONE, true)
273  if (walk.modern && (walk.cut || walk.unread || walk.quoting || command.includes('\\\n') || walk.parsed.some(commands => outsideProfile(walk, commands) !== undefined))) walk.fail(UNPARSED)
274  return walk
275}
276
277/** A fresh shell with its folder and HOME inherited from the environment. */
278function freshState(cwd: string | undefined): State {
279  return { cwd, prev: undefined, stack: [], positional: [], vars: new Map(), reaching: new Set(), exported: new Set(['HOME']), removed: new Set(), emptyEnv: false, aliases: new Map(), written: new Map(), functions: new Map(), named: new Map(), possible: new Map(), arrays: new Map() }
280}
281
282/** A copy of the state for a subshell or nested code; the links made and the files written stay shared, as on disk. */
283function fork(walk: Walk, state: State): State {
284  if (!walk.charge(8 * (state.vars.size + state.reaching.size + state.exported.size + state.removed.size + state.functions.size + state.named.size + state.possible.size + state.arrays.size + 1))) return freshState(state.cwd)
285  if (!walk.tick(1 + state.arrays.size)) return freshState(state.cwd)
286  return { ...state, vars: new Map(state.vars), reaching: new Set(state.reaching), exported: new Set(state.exported), removed: new Set(state.removed), functions: new Map(state.functions), named: new Map(state.named), possible: new Map(state.possible), arrays: new Map(state.arrays) }
287}
288
289/** The state of a child process: exported variables and their possible values, the functions, and the shared files, in the current folder; arrays do not pass to it. */
290function sharedFresh(walk: Walk, state: State): State {
291  if (!walk.charge(state.exported.size + state.removed.size + state.functions.size + 1)) return freshState(state.cwd)
292  const child = { ...freshState(state.cwd), execdir: state.execdir, aliases: state.aliases, written: state.written, removed: new Set(state.removed), emptyEnv: state.emptyEnv, functions: new Map(state.functions), ...(state.lost ? { lost: true as const } : {}) }
293  child.exported = new Set(state.exported)
294  if (!state.exported.has('HOME')) child.removed.add('HOME')
295  for (const name of state.exported) {
296    const value = state.vars.get(name)
297    if (value !== undefined) child.vars.set(name, value)
298    if (state.reaching.has(name)) child.reaching.add(name)
299    const possible = state.possible.get(name)
300    if (possible !== undefined) child.possible.set(name, possible)
301  }
302  return child
303}
304
305/** Reads and walks a piece of shell text; `top` marks the text of the call itself. */
306async function runText(walk: Walk, text: string, state: State, input: Stdin = NONE, top = false): Promise<void> {
307  if (!walk.charge(text.length)) return
308  const commands = parseCommands(walk, modern => lex(text, modern), top)
309  if (commands !== undefined) await runList(walk, commands, state, input)
310}
311
312/** The tokens of a piece of shell text, or why it cannot be read. */
313type Lexed = ReturnType<typeof lex>
314
315/** The commands of a piece of shell text, read as Arrester 1.1.3 read it, else in the modern reading, which adds `case` and arrays; undefined when neither reading parses, which fails with the reason of the 1.1.3 reading. Once a modern reading parsed, the call stopped on 1.1.3, so later code reads in the modern reading only. An older script and a script that the call wrote keep the 1.1.3 reading. Each tree that it returns outside an older script goes to `walk.parsed`. `tokens` gives the tokens of each reading; `top` marks the text of the call itself. */
316function parseCommands(walk: Walk, tokens: (modern: boolean) => Lexed, top = false): Pipeline[] | undefined {
317  const keeps = walk.oldScript !== undefined || walk.legacy > 0
318  let reason: string | undefined
319  if (keeps || !walk.modern) {
320    const lexed = tokens(false)
321    const commands = readCommands(lexed, false)
322    if (typeof commands !== 'string') {
323      if ('legacy' in lexed) walk.cut = true
324      if (walk.oldScript === undefined) {
325        walk.parsed.push(commands)
326        if ('quoting' in lexed) walk.quoting = true
327      }
328      return commands
329    }
330    reason = commands
331    if (keeps) {
332      walk.fail(reason)
333      return undefined
334    }
335  }
336  if (walk.length > NEW_LENGTH) {
337    walk.fail(reason ?? UNPARSED)
338    return undefined
339  }
340  const lexed = tokens(true)
341  if (!walk.tick(tokenWork(lexed))) return undefined
342  const commands = readCommands(lexed, true)
343  if (typeof commands === 'string') {
344    walk.fail(reason ?? commands)
345    return undefined
346  }
347  walk.modern = true
348  arraysOf(walk, commands, top)
349  walk.parsed.push(commands)
350  if ('quoting' in lexed) walk.quoting = true
351  return commands
352}
353
354/** The work of one reading of a text: one for each token, and the length of each word. */
355function tokenWork(lexed: Lexed): number {
356  if ('error' in lexed) return 1
357  let work = 1
358  for (const token of lexed.tokens) work += 1 + (token.type === 'word' ? wordText(token.word).length : 0)
359  return work
360}
361
362/** The commands of the tokens in one reading, or why they do not parse. */
363function readCommands(lexed: Lexed, modern: boolean): Pipeline[] | string {
364  if ('error' in lexed) return lexed.error
365  try { return new Commands(lexed.tokens, modern).list() }
366  catch (error) { return error instanceof Error ? error.message : UNPARSED }
367}
368
369/** Walks code one level deeper; past MAX_DEPTH the analysis is opaque. */
370async function nested(walk: Walk, text: string, state: State, input: Stdin = NONE): Promise<void> {
371  if (walk.depth >= MAX_DEPTH) {
372    walk.fail(OPAQUE.tooDeep)
373    return
374  }
375  walk.depth += 1
376  await runText(walk, text, state, input)
377  walk.depth -= 1
378}
379
380/** An and-or list in progress: how many pipelines ran, whether only `&&` joined them so far, whether one that may not have run moved the folder. */
381type List = { count: number; isPure: boolean; isMoved: boolean; operator?: '&&' | '||' }
382
383/** A list that starts now. */
384function newList(): List {
385  return { count: 0, isPure: true, isMoved: false }
386}
387
388/** Keeps bounded variable alternatives when a conditional command may have been skipped; a variable that one side made unknown, as `setUnknown` says, stays unknown. Where only one side has values, the walk of 1.1.3 keeps them, and outside an older script that join marks the walk `unread`, or, once the call needs the newer reading, makes the variable unknown. */
389function mergeVariables(walk: Walk, state: State, before: State): void {
390  if (state.terminated) {
391    Object.assign(state, before)
392    state.terminated = before.terminated
393    return
394  }
395  if (!walk.charge(state.vars.size + before.vars.size + state.removed.size + before.removed.size + state.exported.size + before.exported.size)) return
396  const names = new Set([...state.vars.keys(), ...before.vars.keys(), ...state.removed, ...before.removed])
397  for (const name of names) {
398    if (state.vars.get(name) === UNKNOWN || before.vars.get(name) === UNKNOWN) {
399      setUnknown(state, name)
400      continue
401    }
402    const previous = variable(walk, before, name)
403    const current = variable(walk, state, name)
404    if (previous === current) continue
405    if ((previous === undefined || current === undefined) && walk.oldScript === undefined) {
406      if (walk.modern) {
407        setUnknown(state, name)
408        continue
409      }
410      walk.unread = true
411    }
412    const values = alternatives(walk, [...previous ?? [], ...current ?? []])
413    if (values.length > MAX_VALUES) {
414      walk.fail(OPAQUE.tooMany)
415      return
416    }
417    if (values.length > 0) state.vars.set(name, values)
418    if (before.reaching.has(name)) state.reaching.add(name)
419  }
420  for (const name of before.exported) state.exported.add(name)
421  for (const name of state.removed) if (!before.removed.has(name)) state.removed.delete(name)
422  if (!walk.charge(before.functions.size + before.named.size + before.possible.size)) return
423  for (const [name, body] of before.functions) if (!state.functions.has(name)) state.functions.set(name, body)
424  for (const [name, text] of before.named) if (!state.named.has(name)) state.named.set(name, text)
425  for (const [name, values] of before.possible) {
426    const merged = state.vars.has(name) ? [] : alternatives(walk, [...state.possible.get(name) ?? [], ...values])
427    if (merged.length > 0 && merged.length <= MAX_VALUES) state.possible.set(name, merged)
428  }
429  if (state.positional !== before.positional) mergePositional(walk, state, before)
430  state.globOptions = { dot: before.globOptions?.dot || state.globOptions?.dot, nocase: before.globOptions?.nocase || state.globOptions?.nocase }
431  state.noglob = state.noglob && before.noglob
432  state.globstar = state.globstar || before.globstar
433  if (before.lost) state.lost = true
434  if (walk.tick(1 + state.arrays.size)) for (const [name, elements] of state.arrays) if (before.arrays.get(name) !== elements) state.arrays.delete(name)
435  state.status = undefined
436}
437
438/** Joins corresponding positional values; different argument counts stay unresolved until replaced. */
439function mergePositional(walk: Walk, state: State, before: State): void {
440  const current = state.positional
441  const previous = before.positional
442  state.positional = undefined
443  if (current === undefined || previous === undefined || current.length !== previous.length) return
444  if (!walk.charge(current.length)) return
445  const merged: Operand[] = []
446  for (const [index, arg] of current.entries()) {
447    const prior = previous[index]!
448    const values = arg.values === undefined || prior.values === undefined ? undefined : alternatives(walk, [...arg.values, ...prior.values])
449    if ((values?.length ?? 0) > MAX_VALUES) {
450      walk.fail(OPAQUE.tooMany)
451      return
452    }
453    merged.push({ raw: arg.raw, values, upstream: arg.upstream || prior.upstream })
454  }
455  setPositional(walk, state, merged)
456}
457
458/** The builtins after which an array assignment parses, when a flag with `a` or `A` comes before it. */
459const ARRAY_DECLARERS = new Set(['declare', 'local', 'typeset', 'readonly'])
460
461/** The operators that end a `case` branch. */
462const CASE_ENDS = [';;', ';&', ';;&']
463
464/** Parses compound boundaries before execution, with a fixed nesting limit; with `modern`, it also parses `case`, and an array assignment parses only where `checkArrays` says, never in a `for` list, a `case` word or pattern, or a redirection. */
465class Commands {
466  private at = 0
467  private depth = 0
468
469  constructor(private readonly tokens: readonly Token[], private readonly modern = false) {}
470
471  /** Returns an unquoted keyword or a control operator at the cursor. */
472  private text(offset = 0): string {
473    const token = this.tokens[this.at + offset]
474    if (token?.type === 'op') return token.op === '\n' ? ';' : token.op
475    return token?.type === 'word' && token.word.every(part => part.kind === 'text' && !part.quoted) ? wordText(token.word) : ''
476  }
477
478  /** Consumes a required delimiter, rejecting incomplete syntax. */
479  private take(text: string): void {
480    if (this.text() !== text) throw new Error(UNPARSED)
481    this.at += 1
482  }
483
484  /** Reads a command list up to a compound delimiter. */
485  list(stops: readonly string[] = []): Pipeline[] {
486    if (++this.depth > 32) throw new Error(OPAQUE.tooDeep)
487    const result: Pipeline[] = []
488    while (this.at < this.tokens.length && !stops.includes(this.text())) {
489      if (this.text() === ';') { this.at += 1; continue }
490      let negate = false
491      while (this.text() === '!') { negate = !negate; this.at += 1 }
492      const commands = [this.command()]
493      while (this.text() === '|' || this.text() === '|&') {
494        this.at += 1
495        while (this.tokens[this.at]?.type === 'op' && (this.tokens[this.at] as { op: string }).op === '\n') this.at += 1
496        commands.push(this.command())
497      }
498      let after = ''
499      if ([';', '&', '&&', '||'].includes(this.text())) {
500        after = this.text()
501        this.at += 1
502      } else if (this.at < this.tokens.length && !stops.includes(this.text())) throw new Error(UNPARSED)
503      result.push({ commands, negate, after })
504    }
505    this.depth -= 1
506    return result
507  }
508
509  /** Reads one simple command, group, condition, or function definition. */
510  private command(nesting = 0): Simple {
511    if (nesting >= 32) throw new Error(OPAQUE.tooDeep)
512    const command: Simple = { words: [], redirects: [] }
513    const text = this.text()
514    if ((text === 'function' && this.tokens[this.at + 1]?.type === 'word') || (text !== '' && this.text(1) === '(' && this.text(2) === ')')) {
515      this.at += text === 'function' ? 1 : 0
516      const name = this.text()
517      this.at += 1
518      if (this.text() === '(') { this.at += 1; this.take(')') }
519      const start = this.at
520      const body = this.command(nesting + 1)
521      if (body.body === undefined) throw new Error(UNPARSED)
522      return { ...body, definition: name, ...(this.tokens.slice(start, this.at).some(namesExit) ? { exits: true as const } : {}) }
523    }
524    if (text === '(' || text === '{') {
525      const opening = this.tokens[this.at]
526      this.at += 1
527      command.body = this.list([text === '(' ? ')' : '}'])
528      command.subshell = text === '('
529      if (opening?.type === 'op' && opening.joined === true) command.arithmetic = true
530      this.take(text === '(' ? ')' : '}')
531    } else if (text === 'while' || text === 'until') {
532      this.at += 1
533      command.loop = { condition: this.list(['do']), until: text === 'until' }
534      this.take('do')
535      command.body = this.list(['done'])
536      this.take('done')
537    } else if (text === 'for') {
538      this.at += 1
539      const name = this.text()
540      if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(name)) throw new Error(UNPARSED)
541      this.at += 1
542      while (this.text() === ';') this.at += 1
543      command.loop = { name }
544      if (this.text() === 'in') {
545        this.at += 1
546        command.loop.words = []
547        while (this.tokens[this.at]?.type === 'word') {
548          command.loop.words.push((this.tokens[this.at] as { type: 'word'; word: Word }).word)
549          this.at += 1
550        }
551      }
552      while (this.text() === ';') this.at += 1
553      this.take('do')
554      command.body = this.list(['done'])
555      this.take('done')
556    } else if (text === 'if') {
557      command.branches = []
558      do {
559        this.at += 1
560        const condition = this.list(['then'])
561        this.take('then')
562        command.branches.push({ condition, body: this.list(['elif', 'else', 'fi']) })
563      } while (this.text() === 'elif')
564      if (this.text() === 'else') {
565        this.at += 1
566        command.branches.push({ body: this.list(['fi']) })
567      }
568      this.take('fi')
569    } else if (this.modern && text === 'case') command.cases = this.caseOf()
570    else {
571      while (this.at < this.tokens.length) {
572        const token = this.tokens[this.at] as Token
573        if (token.type === 'op') break
574        if (token.type === 'word') command.words.push(token.word)
575        else command.redirects.push(token.redirect)
576        this.at += 1
577      }
578      if (command.words.length === 0 && command.redirects.length === 0) throw new Error(UNPARSED)
579      if (this.modern) checkArrays(command.words)
580    }
581    while (this.tokens[this.at]?.type === 'redirect') {
582      command.redirects.push((this.tokens[this.at] as { type: 'redirect'; redirect: Redirect }).redirect)
583      this.at += 1
584    }
585    if (this.modern && [...command.loop?.words ?? [], ...command.redirects.map(redirect => redirect.target)].some(isArray)) throw new Error(UNPARSED)
586    return command
587  }
588
589  /** Reads `case WORD in … esac` from the `case` at the cursor: newlines may come before `in` and between branches; each branch has patterns joined by `|`, with an optional `(` before them and a `)` after, a body, and `;;`, `;&`, or `;;&`, which the last branch may leave out before `esac`. */
590  private caseOf(): NonNullable<Simple['cases']> {
591    this.at += 1
592    const word = this.tokens[this.at]
593    if (word?.type !== 'word') throw new Error(UNPARSED)
594    this.at += 1
595    this.newlines()
596    this.take('in')
597    const clauses: NonNullable<Simple['cases']>['clauses'] = []
598    for (;;) {
599      this.newlines()
600      if (this.text() === 'esac') break
601      if (this.text() === '(') this.at += 1
602      const patterns: Word[] = []
603      for (;;) {
604        const pattern = this.tokens[this.at]
605        if (pattern?.type !== 'word') throw new Error(UNPARSED)
606        patterns.push(pattern.word)
607        this.at += 1
608        if (this.text() !== '|') break
609        this.at += 1
610      }
611      this.take(')')
612      const body = this.list([...CASE_ENDS, 'esac'])
613      const end = this.text()
614      if (end !== 'esac' && !CASE_ENDS.includes(end)) throw new Error(UNPARSED)
615      clauses.push({ patterns, body, end: end === 'esac' ? ';;' : end })
616      if (end === 'esac') break
617      this.at += 1
618    }
619    this.take('esac')
620    if ([word.word, ...clauses.flatMap(clause => clause.patterns)].some(isArray)) throw new Error(UNPARSED)
621    return { word: word.word, clauses }
622  }
623
624  /** Skips newlines at the cursor. */
625  private newlines(): void {
626    while (this.tokens[this.at]?.type === 'op' && (this.tokens[this.at] as { op: string }).op === '\n') this.at += 1
627  }
628}
629
630/** Checks that each array assignment of a simple command stands where the shell reads one: in a command of assignments only, or after `declare`, `local`, `typeset`, or `readonly` with a flag that holds `a` or `A`. */
631function checkArrays(words: readonly Word[]): void {
632  const leading = leadingAssignments(words)
633  const program = wordText(words[leading] ?? [])
634  let flagged = false
635  for (const [index, word] of words.entries()) {
636    if (word.some(part => part.kind === 'array') && leading !== words.length && !(index > leading && ARRAY_DECLARERS.has(program) && flagged)) throw new Error(UNPARSED)
637    if (index > leading && /^-[A-Za-z]*[aA]/.test(wordText(word))) flagged = true
638  }
639}
640
641/** Walks and-or lists while preserving skipped status and bounded variable alternatives. */
642async function runList(walk: Walk, commands: readonly Pipeline[], state: State, input: Stdin): Promise<Stdin> {
643  let list = newList()
644  let output = NONE
645  let pipelines = 0
646  const parent = state
647  for (let index = 0; index < commands.length; index += 1) {
648    if (parent.terminated) break
649    const pipeline = commands[index]!
650    if (index === 0 || !['&&', '||'].includes(commands[index - 1]!.after)) {
651      let end = index
652      while (end + 1 < commands.length && ['&&', '||'].includes(commands[end]!.after)) end += 1
653      if (!walk.charge(end - index + 1)) return OTHER
654      state = commands[end]!.after === '&' ? fork(walk, parent) : parent
655    }
656    if (!walk.charge(1)) return OTHER
657    const wanted = list.operator === undefined ? undefined : list.operator === '&&'
658    if (!state.terminated && (wanted === undefined || state.status === undefined || state.status === wanted)) {
659      const alternative = wanted !== undefined && state.status === undefined ? fork(walk, state) : undefined
660      const before = state.cwd
661      const positions = input.kind === 'literal' ? input.positions ?? [0] : []
662      const produced = await runPipeline(walk, pipeline.commands, state, input)
663      if (pipeline.negate && state.status !== undefined) state.status = !state.status
664      if (alternative !== undefined) {
665        if (input.kind === 'literal' && state.terminated) input.positions = positions
666        else mergeInput(walk, input, positions)
667        mergeVariables(walk, state, alternative)
668      }
669      output = pipelines++ === 0 ? produced : OTHER
670      if (list.count > 0 && state.cwd !== before) {
671        if (list.isPure) list.isMoved = true
672        else state.cwd = undefined
673      }
674      list.count += 1
675    }
676    if (pipeline.after === '||') {
677      list.operator = '||'
678      list.isPure = false
679      if (list.isMoved) state.cwd = undefined
680      list.isMoved = false
681    } else if (pipeline.after === '&&') list.operator = '&&'
682    else {
683      if (pipeline.after === '&') parent.status = true
684      else if (list.isMoved) state.cwd = undefined
685      list = newList()
686    }
687  }
688  return output
689}
690
691/** Executes a group, a `case`, or an if branch, merging state only when a condition is unknown. */
692async function runCompound(walk: Walk, command: Simple, state: State, input: Stdin): Promise<Stdin> {
693  if (command.loop !== undefined) return runLoop(walk, command, state, input)
694  if (command.cases !== undefined) return runCase(walk, command.cases, state, input)
695  if (command.body !== undefined) {
696    const scope = command.subshell ? fork(walk, state) : state
697    const output = await runList(walk, command.body, scope, input)
698    state.status = scope.status
699    return output
700  }
701  const alternatives: State[] = []
702  const positions: number[] = []
703  let output = NONE
704  let executed = false
705  for (const branch of command.branches ?? []) {
706    if (!walk.charge(1)) return OTHER
707    if (branch.condition !== undefined) await runList(walk, branch.condition, state, input)
708    else state.status = true
709    if (state.terminated) break
710    if (state.status === false) continue
711    if (state.status === true) {
712      output = await runList(walk, branch.body, state, input)
713      executed = true
714      break
715    }
716    const scope = fork(walk, state)
717    const before = input.kind === 'literal' ? input.positions ?? [0] : []
718    await runList(walk, branch.body, scope, input)
719    if (input.kind === 'literal') {
720      if (!scope.terminated && walk.charge(input.positions?.length ?? 1)) positions.push(...input.positions ?? [0])
721      input.positions = before
722    }
723    alternatives.push(scope)
724  }
725  mergeInput(walk, input, positions)
726  if (!executed && !state.terminated) state.status = true
727  for (const alternative of alternatives) {
728    if (alternative.terminated) continue
729    mergeVariables(walk, state, alternative)
730    if (state.cwd !== alternative.cwd) state.cwd = undefined
731    output = OTHER
732  }
733  return output
734}
735
736/** Walks a `case`: the commands in its word and its patterns run, and a pattern is no path. Every branch counts, as a branch of an `if` whose condition is unknown does, each in a copy of the shell, and the copies join the shell where no pattern matched, as at an `if`. A branch that `;&` ends also starts the next branch, and one that `;;&` ends every later branch, so their shells join the start of those branches; the narrow reading does not follow the input that such a branch passes on, so such an end marks the walk `unread`. */
737async function runCase(walk: Walk, cases: NonNullable<Simple['cases']>, state: State, input: Stdin): Promise<Stdin> {
738  const cache: Substituted = new Map()
739  await resolveWhole(walk, cases.word, state, cache)
740  if (cases.clauses.some(clause => clause.end !== ';;')) walk.unread = true
741  const ends: State[] = []
742  const later: State[] = []
743  const positions: number[] = []
744  let next: State | undefined
745  for (const clause of cases.clauses) {
746    if (!walk.charge(1 + later.length) || !walk.tick(1 + later.length + clause.patterns.length)) return OTHER
747    for (const pattern of clause.patterns) await resolveWhole(walk, pattern, state, cache)
748    const scope = fork(walk, state)
749    for (const other of next === undefined ? later : [...later, next]) {
750      if (other.terminated) continue
751      mergeVariables(walk, scope, other)
752      if (scope.cwd !== other.cwd) scope.cwd = undefined
753    }
754    const before = input.kind === 'literal' ? input.positions ?? [0] : []
755    await runList(walk, clause.body, scope, input)
756    if (input.kind === 'literal') {
757      if (!scope.terminated && walk.charge(input.positions?.length ?? 1)) positions.push(...input.positions ?? [0])
758      input.positions = before
759    }
760    ends.push(scope)
761    if (clause.end === ';;&') later.push(scope)
762    next = clause.end === ';&' ? scope : undefined
763  }
764  mergeInput(walk, input, positions)
765  state.status = true
766  let output = NONE
767  for (const end of ends) {
768    if (end.terminated) continue
769    mergeVariables(walk, state, end)
770    if (state.cwd !== end.cwd) state.cwd = undefined
771    output = OTHER
772  }
773  return output
774}
775
776/** The names of the positional and special parameters, which a call does not make unknown. */
777const POSITIONAL = /^(?:[0-9]+|[@*#?$!-])$/
778
779/** Runs the stages of one pipeline, handing each stage what the one before produced. Each stage of a longer pipeline runs in a subshell, and a folder change in the last stage leaves the folder unknown, since some shells run that stage in this one; for the same reason, rule A4 applies to each call of a function or an alias in the last stage, as `forgetAssigned` says. */
780async function runPipeline(walk: Walk, commands: readonly Simple[], state: State, input: Stdin): Promise<Stdin> {
781  const [only] = commands
782  if (commands.length === 1 && only !== undefined) {
783    return runSimple(walk, only, state, input)
784  }
785  let piped = input
786  let isDecoded = false
787  let last = state
788  for (const command of commands) {
789    if (!walk.charge(1)) return OTHER
790    last = fork(walk, state)
791    if (walk.isSpent()) return OTHER
792    piped = await runSimple(walk, command, last, isDecoded ? DECODER : piped)
793    isDecoded = isDecoded || piped.kind === 'decoder'
794  }
795  if (last.cwd !== state.cwd) state.cwd = undefined
796  state.status = last.status
797  if (walk.forgets) {
798    const calls = changesOf(walk, [{ commands: commands.slice(-1), negate: false, after: '' }], noChanges(true)).calls
799    forgetAssigned(walk, state, [...calls].filter(name => walk.bodies.has(name) || state.named.has(name)))
800  }
801  return piped
802}
803
804/** How many words at the front of a command assign variables, read from their text as typed. */
805function leadingAssignments(words: readonly Word[]): number {
806  const index = words.findIndex(word => !ASSIGNMENT.test(wordText(word)))
807  return index < 0 ? words.length : index
808}
809
810/** `return` or `exit` as a name of its own anywhere in a text, also inside quotes or a substitution. */
811const EXITS = /(?:^|[^A-Za-z0-9_])(?:return|exit)(?![A-Za-z0-9_])/
812
813/** Whether a token names `return` or `exit` anywhere: in a word, a redirection's target, or a heredoc. */
814function namesExit(token: Token): boolean {
815  if (token.type === 'word') return EXITS.test(wordText(token.word))
816  return token.type === 'redirect' && (EXITS.test(wordText(token.redirect.target)) || EXITS.test(token.redirect.heredoc?.text ?? ''))
817}
818
819/** Runs one simple command: assignments, wrappers, the program, and the redirections; returns what it hands the next stage. A function definition is kept for its calls, unless its body names `return` or `exit`, which forgets the name; every body stays known for rule A4, which `forgetAssigned` applies after a call in the second walk. A command whose program word names an alias also walks the alias text with the rest of the command. An array assignment walks as `assignArray` says. */
820async function runSimple(walk: Walk, command: Simple, state: State, piped: Stdin): Promise<Stdin> {
821  if (command.definition !== undefined) {
822    if (command.definition !== '' && walk.tick(1)) {
823      const bodies = walk.bodies.get(command.definition) ?? new Set<Simple>()
824      bodies.add(command)
825      walk.bodies.set(command.definition, bodies)
826    }
827    if (command.exits) state.functions.delete(command.definition)
828    else if (command.definition !== '' && walk.charge(1)) state.functions.set(command.definition, { ...command, definition: undefined })
829    state.status = true
830    return NONE
831  }
832  const outer = state
833  const original = command
834  const cache: Substituted = new Map()
835  let start = 0
836  while (KEYWORDS.has(wordText(command.words[start] ?? []))) start += 1
837  if (start > 0) command = { ...command, words: command.words.slice(start) }
838  const leading = leadingAssignments(command.words)
839  const alias = await expandAlias(walk, command, leading, state, piped)
840  const isDeclaration = ASSIGNERS.has(wordText(command.words[leading] ?? []))
841  const words: Operand[] = []
842  for (const word of command.words.slice(leading)) {
843    if (isArray(word)) {
844      await assignArray(walk, original, word, state, cache)
845      words.push({ raw: wordText(word), values: undefined })
846    } else if (isDeclaration && ASSIGNMENT.test(wordText(word))) words.push(await resolveWhole(walk, word, state, cache))
847    else for (const field of await resolveFields(walk, word, state, cache)) words.push(field)
848  }
849  const redirects: Redirected[] = []
850  const descriptors = new Map<number, Destination>([[1, 'stdout'], [2, 'stderr']])
851  const redirectState = { ...state }
852  const redirectRun: Run = { walk, state: redirectState, program: textOf(words[0]), args: [], stdin: piped }
853  for (const redirect of command.redirects) {
854    const bound = await redirected(walk, redirect, state, cache)
855    if (state.execdir === undefined) bound.target = mapOperand(bound.target, text => absolute(text, redirectState.cwd))
856    // Descriptor numbers and device names are not relative filesystem paths.
857    if (redirect.op === '>&' || redirect.op === '<&') bound.target = await resolveWhole(walk, redirect.target, state, cache)
858    redirects.push(bound)
859    openRedirect(redirectRun, bound, descriptors)
860  }
861  if (leading > 0 && words.length > 0) state = fork(walk, state)
862  for (const word of command.words.slice(0, leading)) {
863    if (isArray(word)) {
864      await assignArray(walk, original, word, state, cache)
865      continue
866    }
867    const setting = await resolveWhole(walk, word, state, cache)
868    assign(walk, state, setting)
869    if (words.length > 0) state.exported.add(ASSIGNMENT.exec(setting.raw)![1]!)
870  }
871  const stdin = stdinOf(redirects, state.cwd, piped) ?? piped
872  if (command.body !== undefined || command.branches !== undefined || command.cases !== undefined) {
873    const output = await runCompound(walk, command, state, stdin)
874    finishRedirects(redirectRun, descriptors, output)
875    return descriptors.get(1) === 'stdout' ? output : NONE
876  }
877  const result = await runCommand(walk, words, state, stdin)
878  if (result.called !== undefined) {
879    walk.calls = true
880    if (walk.forgets) forgetAssigned(walk, outer, [result.called])
881  }
882  if (alias !== undefined && walk.forgets) forgetAssigned(walk, outer, [alias])
883  if (state !== outer && ['read', 'printf'].includes(result.program)) {
884    const temporary = new Set(command.words.slice(0, leading).map(word => ASSIGNMENT.exec(wordText(word))![1]))
885    if (walk.charge(state.vars.size + outer.vars.size)) for (const name of new Set([...state.vars.keys(), ...outer.vars.keys()])) {
886      if (temporary.has(name)) continue
887      const values = state.vars.get(name)
888      if (values === undefined) outer.vars.delete(name)
889      else outer.vars.set(name, values)
890      if (state.reaching.has(name)) outer.reaching.add(name)
891      else outer.reaching.delete(name)
892      if (!state.removed.has(name)) outer.removed.delete(name)
893    }
894  }
895  outer.terminated = state.terminated
896  outer.status = result.status ?? (result.program === 'false' ? false : result.program === 'true' || result.program === ':' || (words.length === 0 && cache.size === 0 && redirects.length === 0) ? true : undefined)
897  redirectRun.program = result.program
898  finishRedirects(redirectRun, descriptors, ['', ':', 'true', 'false'].includes(result.program) ? { kind: 'literal', text: '' } : result.output)
899  return descriptors.get(1) === 'stdout' ? result.output : NONE
900}
901
902/** Whether a word is an array assignment, `NAME=( … )` or `NAME+=( … )`. */
903function isArray(word: Word): boolean {
904  return word.some(part => part.kind === 'array')
905}
906
907/** Walks an array assignment: its elements resolve now, as the words of a command do, so the commands in them run. The array keeps them, so `"${NAME[@]}"`, `${NAME[@]}`, `"${NAME[*]}"`, and `${NAME[*]}` resolve to them, only when `command` is the top-level command that assigns an array of `walk.resolvable` with `=`. Any other array has elements that Arrester does not know, so an expansion of it is opaque. The name has no known scalar value either. */
908async function assignArray(walk: Walk, command: Simple, word: Word, state: State, cache: Substituted): Promise<void> {
909  const match = ASSIGNMENT.exec(wordText(word))
910  const array = word.find(part => part.kind === 'array')
911  if (match === null || array?.kind !== 'array') return
912  const name = match[1]!
913  const elements: Operand[] = []
914  if (!walk.tick(1 + array.elements.length)) return
915  for (const element of array.elements) elements.push(...await resolveFields(walk, element, state, cache))
916  walk.arrayNames.add(name)
917  state.vars.delete(name)
918  state.possible.delete(name)
919  state.removed.delete(name)
920  state.reaching.delete(name)
921  if (match[2] === '' && walk.assignments.has(command) && walk.resolvable.has(name)) state.arrays.set(name, elements)
922  else state.arrays.delete(name)
923}
924
925/** Records the arrays that a tree of commands assigns, declares with `-a` or `-A`, reads with `read -a`, `read -A`, `mapfile`, or `readarray`, sets with `set -A`, or assigns an element of; for the text of the call itself (`top`), also the arrays whose expansions resolve: an array that one top-level command of assignments only assigns with `NAME=( … )`, unconditionally, once, and with no `[key]=value` element, and that no other code of the call can assign, as `changesOf` counts. A command runs unconditionally when no `&&` or `||` comes before it, nothing runs it in the background or in a pipeline, and no `!` negates it. */
926function arraysOf(walk: Walk, commands: readonly Pipeline[], top: boolean): void {
927  visitCommands(walk, commands, command => {
928    let leading = leadingAssignments(command.words)
929    while (ELEMENT.test(wordText(command.words[leading] ?? []))) {
930      walk.arrayNames.add(ELEMENT.exec(wordText(command.words[leading]!))![1]!)
931      leading += 1
932    }
933    const program = wordText(command.words[leading] ?? [])
934    const args = command.words.slice(leading + 1).map(word => wordText(word))
935    const declares = ['mapfile', 'readarray'].includes(program) || ((ARRAY_DECLARERS.has(program) || program === 'read') && args.some(flag => /^-[A-Za-z]*[aA]/.test(flag)))
936    for (const [index, word] of command.words.entries()) {
937      const name = /^([A-Za-z_][A-Za-z0-9_]*)(?:\+?=|$)/.exec(wordText(word))?.[1]
938      if (name !== undefined && (isArray(word) || (declares && index > leading))) walk.arrayNames.add(name)
939    }
940    if (['mapfile', 'readarray'].includes(program)) walk.arrayNames.add('MAPFILE')
941    const set = program === 'set' ? args.findIndex(flag => /^[-+]A$/.test(flag)) : -1
942    if (set >= 0 && /^[A-Za-z_][A-Za-z0-9_]*$/.test(args[set + 1] ?? '')) walk.arrayNames.add(args[set + 1]!)
943  })
944  if (!top) return
945  const candidates = new Map<string, Simple | undefined>()
946  for (const [index, pipeline] of commands.entries()) {
947    const [only] = pipeline.commands
948    const isConditional = index > 0 && ['&&', '||'].includes(commands[index - 1]!.after)
949    if (only === undefined || pipeline.commands.length > 1 || pipeline.negate || pipeline.after === '&' || isConditional) continue
950    if (only.words.length === 0 || leadingAssignments(only.words) !== only.words.length || !walk.tick(commandWork(only))) continue
951    for (const word of only.words) {
952      const match = ASSIGNMENT.exec(wordText(word))
953      const array = word.find(part => part.kind === 'array')
954      if (match === null || array?.kind !== 'array' || match[2] === '+') continue
955      const keyed = array.elements.some(element => /^\[[^\]]*\]\+?=/.test(wordText(element)))
956      candidates.set(match[1]!, candidates.has(match[1]!) || keyed ? undefined : only)
957    }
958  }
959  if (candidates.size === 0) return
960  const changes = changesOf(walk, commands)
961  if (changes.all) return
962  for (const [name, command] of candidates) {
963    if (command === undefined || changes.names.get(name) !== 1) continue
964    walk.resolvable.add(name)
965    walk.assignments.add(command)
966  }
967}
968
969/** The work of reading one command again: one for the command, and one and the length of each word that the change reads in it: its words, the words of its loop header, the word and the patterns of its `case`, and the target and the heredoc of each redirection. */
970function commandWork(command: Simple): number {
971  let work = 1
972  const words = [...command.words, ...command.loop?.words ?? [], ...command.cases === undefined ? [] : [command.cases.word, ...command.cases.clauses.flatMap(clause => clause.patterns)], ...command.redirects.map(redirect => redirect.target)]
973  for (const word of words) work += 1 + wordText(word).length
974  for (const redirect of command.redirects) work += 1 + (redirect.heredoc?.text.length ?? 0)
975  return work
976}
977
978/** Calls `visit` for each command of a tree: the commands of each pipeline and of the compound bodies, conditions, loops, `case` branches, and function bodies in them, until the work runs out. */
979function visitCommands(walk: Walk, commands: readonly Pipeline[], visit: (command: Simple) => void): void {
980  const pending = commands.flatMap(pipeline => pipeline.commands)
981  while (pending.length > 0) {
982    const command = pending.pop()!
983    const work = commandWork(command)
984    if (!walk.charge(work) || !walk.tick(work)) return
985    visit(command)
986    const lists = [...command.body ?? [], ...command.loop?.condition ?? [], ...(command.branches ?? []).flatMap(branch => [...branch.condition ?? [], ...branch.body]), ...(command.cases?.clauses ?? []).flatMap(clause => clause.body)]
987    for (const pipeline of lists) pending.push(...pipeline.commands)
988  }
989}
990
991/** What code can assign: how many times it names each variable in an assignment, whether it can assign any variable, and the program words that it runs; with `exact`, only what it assigns in the shell that runs it, as `changesOf` says. `locals` names the variables that it declares as a function's own, as DECLARERS says. `shell` names the shell that runs the code, when it is bash or zsh, so its builtins count as that shell's do, as DECLARERS and NAMERS say. `getopts` assigns OPTARG, OPTIND, and only the operand after the option string, or each later operand when the option string is not literal text. */
992type Changes = { names: Map<string, number>; all: boolean; calls: Set<string>; exact: boolean; locals: Set<string>; shell?: Shell }
993
994/** No changes yet; `exact` and `shell` as `Changes` say. */
995function noChanges(exact = false, shell?: Shell): Changes {
996  return { names: new Map(), all: false, calls: new Set(), exact, locals: new Set(), ...(shell === undefined ? {} : { shell }) }
997}
998
999/** The names that the assignments and the arithmetic steps in a text assign, in one pass of linear time. An assignment is a name that no letter, digit, `_`, `$`, or `-` comes before, then white space, an optional subscript up to the next `]` with white space after it, an optional operator (`-`, `+`, `*`, `/`, `%`, `&`, `|`, `^`, `:`, `<<`, or `>>`), and `=` with no `=` after it, as in `NAME=`, `F[0]+=`, and `${NAME:=word}`. A step is such a name, with no `-` rule, before `++` or `--`, or `++` or `--`, white space, and a name. Each found form ends where the next search starts, so the text of a subscript that a found form holds counts no name. One pass from the end gives, for each position, the next `]` and the next character that is no white space, so a subscript or a run of white space costs constant work at each name. Assignments come first, then steps. */
1000function arithmeticNames(text: string): string[] {
1001  const length = text.length
1002  const close = new Int32Array(length + 1)
1003  const solid = new Int32Array(length + 1)
1004  close[length] = -1
1005  solid[length] = length
1006  for (let at = length - 1; at >= 0; at -= 1) {
1007    const code = text.charCodeAt(at)
1008    close[at] = code === 93 ? at : close[at + 1]!
1009    solid[at] = isSpace(code) ? solid[at + 1]! : at
1010  }
1011  const isStart = (at: number): boolean => at < length && isNameStart(text.charCodeAt(at))
1012  const nameEnd = (from: number): number => {
1013    let to = from
1014    while (to < length && isNamePart(text.charCodeAt(to))) to += 1
1015    return to
1016  }
1017  const afterSubscript = (from: number): number | undefined => {
1018    if (text[from] !== '[') return from
1019    const end = close[from]!
1020    return end < 0 ? undefined : solid[end + 1]!
1021  }
1022  const names: string[] = []
1023  for (let at = 0; at < length;) {
1024    if (!isStart(at) || (at > 0 && (isNamePart(text.charCodeAt(at - 1)) || text[at - 1] === '$' || text[at - 1] === '-'))) {
1025      at += 1
1026      continue
1027    }
1028    const end = nameEnd(at)
1029    const operator = afterSubscript(solid[end]!)
1030    let after: number | undefined
1031    if (operator !== undefined) {
1032      if ('-+*/%&|^:'.includes(text[operator] ?? '') && text[operator + 1] === '=' && text[operator + 2] !== '=') after = operator + 2
1033      else if ((text.startsWith('<<', operator) || text.startsWith('>>', operator)) && text[operator + 2] === '=' && text[operator + 3] !== '=') after = operator + 3
1034      else if (text[operator] === '=' && text[operator + 1] !== '=') after = operator + 1
1035    }
1036    if (after === undefined) at += 1
1037    else {
1038      names.push(text.slice(at, end))
1039      at = after
1040    }
1041  }
1042  for (let at = 0; at < length;) {
1043    if (isStart(at) && (at === 0 || !(isNamePart(text.charCodeAt(at - 1)) || text[at - 1] === '$'))) {
1044      const end = nameEnd(at)
1045      const step = afterSubscript(solid[end]!)
1046      if (step !== undefined && (text.startsWith('++', step) || text.startsWith('--', step))) {
1047        names.push(text.slice(at, end))
1048        at = step + 2
1049        continue
1050      }
1051    }
1052    if ((text.startsWith('++', at) || text.startsWith('--', at)) && isStart(solid[at + 2]!)) {
1053      const end = nameEnd(solid[at + 2]!)
1054      names.push(text.slice(solid[at + 2]!, end))
1055      at = end
1056      continue
1057    }
1058    at += 1
1059  }
1060  return names
1061}
1062
1063/** A shell whose builtins rule A4 tells apart: bash, also as version 3.2, and zsh. Code that runs in another shell, or where the shell is not known, counts what the builtins of either assign. */
1064type Shell = 'bash' | 'zsh'
1065
1066/** The shells that code with these changes can run in: the one that `bash -c` or `zsh -c` starts, else both. */
1067function shellsOf(changes: Changes): readonly Shell[] {
1068  return changes.shell === undefined ? ['bash', 'zsh'] : [changes.shell]
1069}
1070
1071/** The signed option letters of a table entry, such as `-g +x`. */
1072function optionSet(text = ''): ReadonlySet<string> {
1073  return new Set(text.split(' ').filter(Boolean))
1074}
1075
1076/** How one shell's builtin of the declare family reads its options, for rule A4, as the bash 5.3 `help` texts, zshbuiltins(1) of zsh 5.9, and replays in bash 5.3, bash 3.2, and zsh 5.9 show. Each field holds signed option letters. A word with a letter outside `valid` makes the builtin fail and assign nothing; with a letter of `none` it names functions or prints and assigns nothing; with one of `any` it can assign any variable through a name reference; with one of `patterns` its names are patterns, so it can assign any variable when it gives a value or converts. A declaration in a function body is the function's own unless `global` says that the builtin always declares globally or a letter of `globals` comes. A name without a value changes only with a letter of `converts`, as the second name after `-T`, which needs two, or with a letter of `widths` and a width; with `converting` it always changes, unless a letter of `keeps` comes. With `effective`, as in zsh, only the last of `-L` and `+L` counts for a letter L of `globals`, `converts`, `widths`, and `keeps`; else any of them counts. A letter of `any` counts only when its sign comes last. With a letter of `conflicts` after both signs the builtin fails. With `plusOperands`, a word that starts with `+` is an operand; with `digits`, an option word may hold a number. */
1077type Declarer = { valid: ReadonlySet<string>; none: ReadonlySet<string>; any: ReadonlySet<string>; patterns: ReadonlySet<string>; globals: ReadonlySet<string>; converts: ReadonlySet<string>; widths: ReadonlySet<string>; keeps: ReadonlySet<string>; conflicts: ReadonlySet<string>; global?: true; converting?: true; effective?: true; plusOperands?: true; digits?: true }
1078
1079/** A Declarer from its letters as text. */
1080function declarer(entry: { valid: string; none?: string; any?: string; patterns?: string; globals?: string; converts?: string; widths?: string; keeps?: string; conflicts?: string; global?: true; converting?: true; effective?: true; plusOperands?: true; digits?: true }): Declarer {
1081  return { ...entry, valid: optionSet(entry.valid), none: optionSet(entry.none), any: optionSet(entry.any), patterns: optionSet(entry.patterns), globals: optionSet(entry.globals), converts: optionSet(entry.converts), widths: optionSet(entry.widths), keeps: optionSet(entry.keeps), conflicts: optionSet(entry.conflicts) }
1082}
1083
1084/** The option letters of zsh `local`, after `-` or `+`, as zshbuiltins(1) lists them. */
1085const ZSH_LOCAL_LETTERS = [...'aAEFhHilLprRtuUxZ'].flatMap(letter => [`-${letter}`, `+${letter}`]).join(' ')
1086
1087/** The builtins of the declare family in each shell, as Declarer says. */
1088const DECLARERS: Readonly<Record<Shell, ReadonlyMap<string, Declarer>>> = (() => {
1089  const bashDeclare = declarer({ valid: '-a -A -c -f -F -g -G -i -I -l -n -p -r -t -u -x +a +A +c +f +F +g +G +i +I +l +n +p +r +t +u +x', none: '-f -F -p +p', any: '-n', globals: '-g -G', conflicts: 'a A' })
1090  const bashExport = declarer({ valid: '-a -A -f -n -p', none: '-f', global: true, plusOperands: true })
1091  const zshConverts = { converts: '-a -A -E -F -i -l -u', widths: '-L -R -Z', effective: true as const, digits: true as const }
1092  const zshTypeset = declarer({ valid: '-a -A -E -f -F -g -h -H -i -k -l -L -m -p -r -R -t -T -u -U -x -z -Z +a +A +E +f +F +g +h +H +i +k +l +L +m +p +r +R +t +u +U +x +z +Z', none: '-f +f -p +p', patterns: '-m +m', globals: '-g -x +x', ...zshConverts })
1093  const zshExport = declarer({ valid: '-a -E -f -F -h -H -i -l -L -p -r -R -t -T -u -U -Z +a +E +f +F +h +H +i +l +L +p +r +R +t +u +U +Z', none: '-f +f -p +p', global: true, ...zshConverts })
1094  const zshReadonly = declarer({ valid: '-a -A -E -f -F -g -h -H -i -l -L -p -R -t -T -u -U -x -Z +a +A +E +f +F +g +h +H +i +l +L +p +R +t +u +U +x +Z', none: '-f +f -p +p', globals: '-g -x +x', ...zshConverts })
1095  const zshInteger = declarer({ valid: '-g -h -H -i -l -L -r -R -t -u -x -Z +g +h +H +i +l +L +r +R +t +u +x +Z', globals: '-g -x +x', converting: true, keeps: '+i', effective: true, digits: true })
1096  const zshFloat = declarer({ valid: '-E -F -g -h -H -l -L -r -R -t -u -x -Z +E +F +g +h +H +l +L +r +R +t +u +x +Z', globals: '-g -x +x', converting: true, keeps: '+E', effective: true, digits: true })
1097  return {
1098    bash: new Map([['declare', bashDeclare], ['typeset', bashDeclare], ['local', bashDeclare], ['export', bashExport], ['readonly', bashExport]]),
1099    zsh: new Map([['declare', zshTypeset], ['typeset', zshTypeset], ['local', declarer({ valid: ZSH_LOCAL_LETTERS, none: '-p +p', digits: true })], ['private', declarer({ valid: `${ZSH_LOCAL_LETTERS} -m +m`, none: '-p +p', patterns: '-m +m', digits: true })], ['export', zshExport], ['readonly', zshReadonly], ['integer', zshInteger], ['float', zshFloat]]),
1100  }
1101})()
1102
1103/** How one shell's other builtin that names variables reads its arguments, for rule A4, from the same sources as DECLARERS. Each option field holds signed option letters. A word with a letter outside `valid`, when the entry has it, makes the builtin fail and assign nothing, and so does a letter of `none`. A letter of `takes` takes the rest of its word, or else the next word, as its value; the value of a letter of `named` names a variable that the builtin assigns, and that of a letter of `calls` a function that it runs in this shell. A letter of `any` lets it assign any variable, and so does a call without a letter of `listing`, when the entry has it. `operands` says which operands name variables that it assigns, `fixed` the variables that it always assigns, and `fallback` those that it assigns when no operand or option names one. With `strict`, a literal operand that is no name makes it fail: with `fail` it assigns nothing, and with `stop` only the names before that operand; with `prompt`, its first operand may end with `?` and a prompt, and an operand that is a number, which an option before it can take, names nothing. With `pids`, it also assigns NAME_PID for each NAME; with `plusOptions`, a word that starts with `+` holds options, else it is an operand. With `last`, only the last value of its `named` letters counts. With `words`, only those whole words are options, and any other word starts the operands; with `firstOnly`, only its first word can be an option; with `endsAtName`, its options end after a value of a `named` letter, and with `endsWith`, after the word that holds that letter. With `single`, more than one operand makes it fail; so does more than one with the letter `oneWith`, and with the letter `nameless` its operands name nothing. */
1104type Assigner = { valid?: ReadonlySet<string>; none: ReadonlySet<string>; takes: ReadonlySet<string>; named: ReadonlySet<string>; calls: ReadonlySet<string>; any: ReadonlySet<string>; listing?: ReadonlySet<string>; operands?: 'all' | 'first'; fixed: readonly string[]; fallback?: (letters: readonly string[]) => readonly string[]; strict?: 'fail' | 'stop'; prompt?: true; pids?: true; plusOptions?: true; last?: true; words?: ReadonlySet<string>; firstOnly?: true; endsAtName?: true; endsWith?: string; single?: true; oneWith?: string; nameless?: string }
1105
1106/** An Assigner from its letters as text. */
1107function assigner(entry: { valid?: string; none?: string; takes?: string; named?: string; calls?: string; any?: string; listing?: string; operands?: 'all' | 'first'; fixed?: string; fallback?: (letters: readonly string[]) => readonly string[]; strict?: 'fail' | 'stop'; prompt?: true; pids?: true; plusOptions?: true; last?: true; words?: string; firstOnly?: true; endsAtName?: true; endsWith?: string; single?: true; oneWith?: string; nameless?: string }): Assigner {
1108  return { ...entry, valid: entry.valid === undefined ? undefined : optionSet(entry.valid), none: optionSet(entry.none), takes: optionSet(entry.takes), named: optionSet(entry.named), calls: optionSet(entry.calls), any: optionSet(entry.any), listing: entry.listing === undefined ? undefined : optionSet(entry.listing), fixed: [...optionSet(entry.fixed)], words: entry.words === undefined ? undefined : optionSet(entry.words) }
1109}
1110
1111/** The other builtins that name or assign variables in each shell, as Assigner says. Every other builtin of SHELL_COMMANDS assigns no variable, apart from those of ASSIGNS_ANY and `eval`, `trap`, `alias`, `let`, `[[`, `getopts`, `emulate`, and `zstyle`, which `commandChanges` reads itself. */
1112const NAMERS: Readonly<Record<Shell, ReadonlyMap<string, Assigner>>> = (() => {
1113  const mapfile = assigner({ valid: '-C -c -d -n -O -s -t -u', takes: '-C -c -d -n -O -s -u', any: '-C', operands: 'first', fallback: () => ['MAPFILE'] })
1114  const folder = assigner({ fixed: 'OLDPWD' })
1115  const fc = assigner({ takes: '-e', listing: '-l' })
1116  const select = assigner({ valid: '', operands: 'first', fixed: 'REPLY' })
1117  const zshRead = { none: '-c -e -l', oneWith: '-A', operands: 'all' as const, strict: 'fail' as const, prompt: true as const, fallback: (letters: readonly string[]) => (letters.includes('-A') ? ['reply'] : ['REPLY']) }
1118  return {
1119    bash: new Map([
1120      ['read', assigner({ valid: '-a -d -e -E -i -n -N -p -r -s -t -u', takes: '-a -d -i -n -N -p -t -u', named: '-a', last: true, nameless: '-a', operands: 'all', strict: 'stop', fallback: () => ['REPLY'] })],
1121      ['mapfile', mapfile], ['readarray', mapfile],
1122      ['printf', assigner({ valid: '-v', takes: '-v', named: '-v', last: true })],
1123      ['wait', assigner({ valid: '-f -n -p', takes: '-p', named: '-p', last: true })],
1124      ['compgen', assigner({ valid: '-a -A -b -c -C -d -e -f -F -g -G -j -k -o -P -s -S -u -v -V -W -X', takes: '-A -C -F -G -o -P -S -V -W -X', named: '-V', calls: '-F', last: true })],
1125      ['unset', assigner({ valid: '-f -n -v', none: '-f', operands: 'all' })],
1126      ['enable', assigner({ takes: '-f', any: '-f' })],
1127      ['coproc', assigner({ operands: 'first', fixed: 'COPROC COPROC_PID', pids: true })],
1128      ['select', select], ['fc', fc], ['cd', folder], ['pushd', folder], ['popd', folder],
1129    ]),
1130    zsh: new Map([
1131      ['read', assigner({ valid: '-A -c -d -e -E -k -l -n -p -q -r -s -t -u -z', takes: '-d -u', ...zshRead })],
1132      ['getln', assigner({ valid: '-A -c -e -E -l -n', ...zshRead })],
1133      ['printf', assigner({ valid: '-v', words: '-v', takes: '-v', named: '-v', last: true })],
1134      ['print', assigner({ valid: '-a -b -c -C -D -e -E -f -i -l -m -n -N -o -O -P -r -R -s -S -u -v -x -X -z', none: '-e -E -s -S -z', takes: '-C -f -u -v -x -X', named: '-v', last: true, endsWith: '-R' })],
1135      ['set', assigner({ none: '-b -c -i -j -m -n -q -t -z -Z +b +c +j +q +z', takes: '-A +A -o +o', named: '-A +A', endsAtName: true, plusOptions: true })],
1136      ['vared', assigner({ valid: '-a -A -c -e -f -g -h -i -m -M -p -r -t', takes: '-f -i -M -m -p -r -t', operands: 'all', strict: 'fail', single: true })],
1137      ['zformat', assigner({ valid: '-a -f -F', takes: '-a -f -F', named: '-a -f -F', firstOnly: true })],
1138      ['strftime', assigner({ valid: '-n -q -r -s', takes: '-s', named: '-s' })],
1139      ['zstat', assigner({ valid: '-A -f -F -g -H -l -L -n -N -o -r -s -t -T', takes: '-A -f -F -H', named: '-A -H' })],
1140      ['unset', assigner({ valid: '-f -m -v', none: '-f', any: '-m', operands: 'all', strict: 'fail' })],
1141      ['foreach', assigner({ operands: 'first' })],
1142      ['select', select], ['fc', fc], ['cd', folder], ['chdir', folder], ['pushd', folder], ['popd', folder],
1143    ]),
1144  }
1145})()
1146
1147/** The variables that `=~` in `[[` assigns in each shell. */
1148const MATCHES: Readonly<Record<Shell, readonly string[]>> = { bash: ['BASH_REMATCH'], zsh: ['MATCH', 'MBEGIN', 'MEND', 'match', 'mbegin', 'mend'] }
1149
1150/** The builtins that can assign any variable in each shell: they run code that Arrester does not read at that point, load modules that add parameters, or name variables in ways that it does not follow. */
1151const ASSIGNS_ANY: Readonly<Record<Shell, ReadonlySet<string>>> = { bash: new Set(['source', '.']), zsh: new Set(['source', '.', 'r', 'zmodload', 'zparseopts', 'zregexparse']) }
1152
1153/** The words before a program that run it in this shell. */
1154const PREFIXES = new Set(['builtin', 'command', 'time'])
1155
1156/** The zsh precommand modifiers, which also run the program in this shell; `repeat` takes a count first. */
1157const ZSH_PREFIXES = new Set(['noglob', 'nocorrect', '-', 'repeat'])
1158
1159/** The operators of `[[` whose operands are arithmetic, where an assignment can stand. */
1160const ARITHMETIC_TESTS = new Set(['-eq', '-ne', '-lt', '-le', '-gt', '-ge'])
1161
1162/** What the commands of a tree can assign. By default, in this shell or in a subshell: each assignment and arithmetic step in their text, also in substitutions and in heredocs that the shell expands, `for` loop names, `{NAME}` before a redirection, the names that NAMES_VARIABLES and NAMED_BY_OPTION name, and the code that `eval`, `trap`, and `alias` give. A builtin of ASSIGNS_ANY, a name reference that `-n` declares, a name that is not literal text, a command whose program word is not literal text, which can run any builtin or function, and code that does not parse can assign any variable. With `exact`, as rule A4 counts, only what the commands assign in the shell that runs the tree, and no text that is data: the assignments before a program, `${NAME=word}` and `${NAME:=word}`, the assignments and steps of arithmetic (`$(( … ))`, `(( … ))`, `let`, and the arithmetic operands of `[[`), also in heredocs that the shell expands, `for` loop names, `{NAME}` before a redirection, the names of NAMES_VARIABLES, NAMED_BY_OPTION, and `getopts`, and the code of `eval` and `trap`, but not the text of an `alias` command, which runs only where the alias is used; the code of a subshell, a substitution, a stage of a pipeline before its last, a list in the background, and the body of a function that the tree defines does not count. */
1163function changesOf(walk: Walk, commands: readonly Pipeline[], changes: Changes = noChanges(), depth = 0): Changes {
1164  const visit = (command: Simple): void => commandChanges(walk, command, changes, depth)
1165  if (changes.exact) visitShell(walk, commands, visit)
1166  else visitCommands(walk, commands, visit)
1167  return changes
1168}
1169
1170/** Calls `visit` for each command of a tree that runs in the shell that runs the tree, as `visitCommands` does, but without the bodies of subshells and of the functions that the tree defines, the stages of a pipeline before its last, and the lists that run in the background. */
1171function visitShell(walk: Walk, commands: readonly Pipeline[], visit: (command: Simple) => void): void {
1172  const pending: (readonly Pipeline[])[] = [commands]
1173  while (pending.length > 0) {
1174    const list = pending.pop()!
1175    let start = 0
1176    if (!walk.tick(list.length)) return
1177    for (const [index, pipeline] of list.entries()) {
1178      if (['&&', '||'].includes(pipeline.after)) continue
1179      const group = list.slice(start, index + 1)
1180      start = index + 1
1181      if (pipeline.after === '&') continue
1182      if (!walk.tick(group.length)) return
1183      for (const { commands: stages } of group) {
1184        const command = stages[stages.length - 1]
1185        if (command === undefined || command.definition !== undefined) continue
1186        const work = commandWork(command)
1187        if (!walk.charge(work) || !walk.tick(work)) return
1188        visit(command)
1189        if (command.subshell) continue
1190        pending.push(command.body ?? [], command.loop?.condition ?? [], ...(command.branches ?? []).flatMap(branch => [branch.condition ?? [], branch.body]), ...(command.cases?.clauses ?? []).map(clause => clause.body))
1191      }
1192    }
1193  }
1194}
1195
1196/** Adds what one command of a tree can assign to `changes`, as `changesOf` says. */
1197function commandChanges(walk: Walk, command: Simple, changes: Changes, depth: number): void {
1198  const add = (name: string): void => {
1199    changes.names.set(name, (changes.names.get(name) ?? 0) + 1)
1200  }
hooks/analyze/types.ts 28 lines
1// Types the analysis modules share. Pure: no `$`.
2
3import type { ArresterTarget } from '../../types/index'
4
5/** What a tool call does to one path; `name` is a path that an MCP call names, whose access Arrester cannot know. */
6export type EffectKind = 'list' | 'read' | 'write' | 'destroy' | 'name'
7
8/** One path effect: its glob or find selection, access, reach below the path, and final-entry removal; `fullSearch` when grep, egrep, fgrep, ugrep, rg, ag, or a Grep call searches below the path with no file filter, ignore file, size limit, depth limit, or listing flag. */
9export type Effect = { path: string; glob?: string; globOptions?: ArresterTarget['globOptions']; kind: EffectKind; recursive: boolean; entry?: true; selection?: ArresterTarget['selection']; fullSearch?: true }
10
11/** One simple command's retry skeleton and its flags, sorted and each once. */
12export type Segment = { skeleton: string; flags: string[]; preview?: true }
13
14/** Code in an older script that Arrester could not read, and so did not count. */
15export type Ignored = { path: string; reason: string }
16
17/** What the analysis of one call or one piece of code found; `bypass` names a launch flag or a plugin command that can turn guards off, or a nested `claude` run. */
18export type Analysis = { effects: Effect[]; segments: Segment[]; opaque?: string; ignored?: Ignored; bypass?: string }
19
20/** Where a call runs: the session's current folder and the home folder. */
21export type Context = { cwd: string; home: string | undefined }
22
23/** How a script read went: no such file, its text, or the reason it cannot be read; `isOld` when the file last changed before the oldest open block opened. */
24export type ScriptRead = 'missing' | { text: string; isOld: boolean } | { unreadable: string; isOld: boolean }
25
26/** The only I/O the analysis does: reading a script that a call runs, and noticing an abandoned dispatch; `capture` when the analysis records a block, where an older interpreter script is not scanned. */
27export type Io = { readScript: (path: string) => Promise<ScriptRead>; signal: AbortSignal; capture?: true }
28
hooks/guards.ts 106 lines
1// Guard paths and guard names: the files whose read or change
2// counts as touching a guard while a block is open. Pure: no `$`.
3
4import { isRecord } from './analyze/call'
5import { lex } from './analyze/lexer'
6import type { Word } from './analyze/lexer'
7import { basenameOf, foldPath, joinPath, looksLikePath } from './analyze/paths'
8import { INTERPRETER, KEYWORDS, SHELLS } from './analyze/programs'
9import type { ArresterGuard } from '../types/index'
10
11/** The settings sources whose command hooks name guard files, lowest precedence first. */
12export const SETTINGS_SOURCES = ['user', 'project', 'local', 'flag', 'policy'] as const
13
14/** The fixed guard paths: the settings files of the user and of the project, and Arrester's own folder, for any touch; the installed plugins, for a change only. */
15export function fixedGuards(home: string | undefined, root: string, pluginRoot: string): ArresterGuard[] {
16  const userPaths = home === undefined ? [] : [`${home}/.claude/settings.json`]
17  const projectPaths = ['settings.json', 'settings.local.json'].map(name => `${root}/.claude/${name}`)
18  const guards: ArresterGuard[] = [...new Set([...userPaths, ...projectPaths, pluginRoot].map(foldPath))].map(path => ({ path }))
19  return home === undefined ? guards : [...guards, { path: foldPath(`${home}/.claude/plugins`), changeOnly: true }]
20}
21
22/** The options after which an interpreter or a shell takes code or a module, not a script file. */
23const CODE_OPTIONS = new Set(['-c', '-e', '-m'])
24
25/** A shell assignment word, such as `MODE=strict`. */
26const ASSIGNMENT = /^[A-Za-z_][A-Za-z0-9_]*\+?=/
27
28/** The paths that the command hooks of one settings source name: each absolute word, and each relative path-like word that is a command's program, the script operand of an interpreter or a shell, or a path under `.claude/`, read in the session folder `cwd` and in the project folder `root`. A hook with `args` runs `command` as one program with each argument as written, so only `$CLAUDE_PROJECT_DIR` is replaced there; a hook without them is a shell command, split into commands and words with its quotes, where `~` and `$HOME` expand too. */
29export function hookPaths(settings: Readonly<Record<string, unknown>>, home: string | undefined, root: string, cwd: string): string[] {
30  const paths: string[] = []
31  for (const hook of commandHooks(settings.hooks)) {
32    const args = hook.args
33    const commands = Array.isArray(args) ? [[hook.command, ...args].map(word => (typeof word === 'string' ? word.replace(/\$\{CLAUDE_PROJECT_DIR\}|\$CLAUDE_PROJECT_DIR/g, () => root) : undefined))] : typeof hook.command === 'string' ? shellCommands(hook.command, home, root) : []
34    for (const words of commands) paths.push(...commandPaths(words, root, cwd))
35  }
36  return paths
37}
38
39/** The paths that one command's words name, its program first: each absolute word, and a relative path-like word that is the program, the script operand, or under `.claude/`, joined to `cwd` and to `root`; an unreadable word is undefined and names none. */
40function commandPaths(words: readonly (string | undefined)[], root: string, cwd: string): string[] {
41  const script = scriptIndex(words)
42  return words.flatMap((word, index) => {
43    if (word === undefined) return []
44    if (word.startsWith('/')) return [foldPath(word)]
45    if (!looksLikePath(word) || (index !== 0 && index !== script && !word.split('/').slice(0, -1).includes('.claude'))) return []
46    return [...new Set([cwd, root])].map(base => joinPath(base, word))
47  })
48}
49
50/** Where the script operand of an interpreter or a shell is: its first path-like word that is no option; -1 for another program, or when `-c`, `-e`, `-m`, or an unreadable word comes first. */
51function scriptIndex(words: readonly (string | undefined)[]): number {
52  const program = words[0] === undefined ? undefined : basenameOf(words[0])
53  if (program === undefined || !(INTERPRETER.test(program) || SHELLS.has(program))) return -1
54  for (const [index, word] of words.entries()) {
55    if (index === 0) continue
56    if (word === undefined || CODE_OPTIONS.has(word)) return -1
57    if (!word.startsWith('-') && looksLikePath(word)) return index
58  }
59  return -1
60}
61
62/** The words of each simple command of a shell command, with supported variables expanded, respecting quoting; leading assignments and keywords are dropped, so the program comes first. An unreadable word is undefined; redirections give no word; a command that does not parse gives none. */
63function shellCommands(command: string, home: string | undefined, root: string): (string | undefined)[][] {
64  const lexed = lex(command)
65  if ('error' in lexed) return []
66  const commands: (string | undefined)[][] = [[]]
67  for (const token of lexed.tokens) {
68    const words = commands[commands.length - 1]!
69    if (token.type === 'op') commands.push([])
70    else if (token.type === 'word' && !(words.length === 0 && isPrefix(token.word))) words.push(literal(token.word, home, root))
71  }
72  return commands
73}
74
75/** True for a word that comes before a command's program: an assignment or a shell keyword. */
76function isPrefix(word: Word): boolean {
77  const [first] = word
78  return first?.kind === 'text' && !first.quoted && (ASSIGNMENT.test(first.text) || (word.length === 1 && KEYWORDS.has(first.text)))
79}
80
81/** A literal shell word with supported variables expanded, respecting quoting; undefined when it cannot be read. */
82function literal(word: Word, home: string | undefined, root: string): string | undefined {
83  let text = ''
84  for (const [index, part] of word.entries()) {
85    if (part.kind === 'var') {
86      const value = part.name === 'CLAUDE_PROJECT_DIR' ? root : part.name === 'HOME' ? home : undefined
87      if (value === undefined) return undefined
88      text += value
89    } else if (part.kind === 'text') {
90      if (index === 0 && !part.quoted && /^~(?:\/|$)/.test(part.text)) {
91        if (home === undefined) return undefined
92        text += home + part.text.slice(1)
93      } else text += part.text
94    } else return undefined
95  }
96  return text
97}
98
99/** The command hooks of a settings `hooks` value: each entry of `type: "command"` in each matcher group of each event. */
100function commandHooks(hooks: unknown): Readonly<Record<string, unknown>>[] {
101  if (!isRecord(hooks)) return []
102  const groups = Object.values(hooks).flatMap(list => (Array.isArray(list) ? list : []))
103  const entries = groups.flatMap(group => (isRecord(group) && Array.isArray(group.hooks) ? group.hooks : []))
104  return entries.filter((hook): hook is Readonly<Record<string, unknown>> => isRecord(hook) && hook.type === 'command')
105}
106
hooks/journal.ts 236 lines
1// The journal transforms: pure functions over the one `$.state` value,
2// which `update` applies inside the host's versioned write. Pure: no `$`.
3
4import { basenameOf, foldCase, foldPath, globMatch, joinPath, relation, selectionKey } from './analyze/paths'
5import type { Meter } from './analyze/paths'
6import type { Effect, Segment } from './analyze/types'
7import type { ArresterAccess, ArresterBlock, ArresterDecision, ArresterGuard, ArresterHeld, ArresterJournal, ArresterTarget } from '../types/index'
8
9/** A block as the capture gathers it, before the journal gives it an id and bounds its flags. */
10type BlockDraft = Omit<ArresterBlock, 'id' | 'lastHitAt' | 'flagsComplete' | 'segments'> & { segments: Segment[] }
11
12/** The journal of a session where no block has opened. */
13export const EMPTY_JOURNAL: ArresterJournal = { seq: 0, blocks: [], held: [], decisions: [], guards: [], recorded: [] }
14
15const MAX_BLOCKS = 50
16const MAX_DECISIONS = 50
17const MAX_SEGMENTS = 16
18const MAX_FLAGS = 256
19const MAX_FLAG_BYTES = 65_536
20const MAX_TARGETS = 256
21const MAX_RECORDED = 100
22const MINUTE = 60_000
23const STRENGTH: Readonly<Record<ArresterAccess, number>> = { read: 1, write: 2, destroy: 3 }
24/** A deny reason that names another tool to use, such as `Use rg instead of grep`: the guard objected to the tool, not to the folder. */
25const SUGGESTS_TOOL = /\binstead\b|\bprefer(?:s|red)?\b|\bplease use\b|\buse `?rg\b|\btry `/i
26/** What a word of a deny reason loses at its two ends: quotes, backticks, and brackets. */
27const WORD_EDGES = new Set([...'\'"`‘’“”()[]{}'])
28/** What one form of a word of a deny reason also loses at its end. */
29const WORD_ENDS = new Set([...'.,;:!?'])
30/** Where a command splits into the words that spell its paths: blanks, quotes, and shell operators. */
31const SPELLING_BREAKS = /[\s'"`;&|()<>=]+/
32/** The programs that change the folder of a shell; a blocked command that names one keeps every target. */
33const FOLDER_CHANGERS = new Set(['cd', 'pushd', 'popd'])
34/** What a command loses before Arrester looks for a program that changes its folder: its quotes, the `$` before a quote, and its backslashes. */
35const QUOTING = /\$?['"]|\\/g
36/** Where a command without its quoting splits into the names of the programs that it can run. */
37const NAME_BREAKS = /[^A-Za-z0-9_.-]+/
38/** The most distinct words of a deny reason that naming reads; a reason with more keeps every target. */
39const MAX_REASON_WORDS = 256
40/** The most glob steps that naming targets takes; past it, the reason keeps every target. */
41const MAX_NAME_STEPS = 1_000_000
42
43/** When a block closes: `block_minutes` after it opened or after its last caught workaround. */
44function closesAt(block: ArresterBlock, minutes: number): number {
45  return Math.max(block.at, block.lastHitAt) + minutes * MINUTE
46}
47
48/** The blocks still open at `now`, oldest first. */
49export function openBlocks(journal: ArresterJournal, now: number, minutes: number): ArresterBlock[] {
50  return journal.blocks.filter(block => now < closesAt(block, minutes))
51}
52
53/** True when the call `toolUseId` opened a block already, even one that has closed since. */
54export function isRecorded(journal: ArresterJournal, toolUseId: string): boolean {
55  return journal.recorded.includes(toolUseId) || journal.blocks.some(block => block.toolUseId === toolUseId)
56}
57
58/** The journal with a block opened at `draft.at`: the next id, its segments and flags capped, expired blocks pruned, the oldest past 50 closed, the guards replaced, and the call recorded; unchanged when the call opened a block already. */
59export function withBlock(journal: ArresterJournal, draft: BlockDraft, guards: readonly ArresterGuard[], minutes: number): ArresterJournal {
60  if (isRecorded(journal, draft.toolUseId)) return journal
61  const seq = journal.seq + 1
62  let bytes = 0
63  const bounded = (flags: string[]): { flags: string[]; flagsComplete: boolean } => {
64    let size = 0
65    for (const flag of flags) {
66      size += 4
67      for (const ch of flag) {
68        const point = ch.codePointAt(0)!
69        size += point < 128 ? 1 : point < 2048 ? 2 : point < 65536 ? 3 : 4
70      }
71      if (size + bytes > MAX_FLAG_BYTES) break
72    }
73    if (flags.length > MAX_FLAGS || size + bytes > MAX_FLAG_BYTES) return { flags: [], flagsComplete: false }
74    bytes += size
75    return { flags, flagsComplete: true }
76  }
77  const whole = bounded(draft.flags)
78  const segments = draft.segments.slice(0, MAX_SEGMENTS).map(segment => ({ ...segment, ...bounded(segment.flags) }))
79  const limited = !whole.flagsComplete || segments.some(segment => !segment.flagsComplete)
80  const retryLimited = [draft.retryLimited, ...(limited ? ['flag storage exceeds 256 flags or 65,536 total bytes; fuzzy retries with incomplete flags are unavailable'] : [])].filter(Boolean).join('; ') || undefined
81  const block: ArresterBlock = { ...draft, id: `b${seq}`, lastHitAt: draft.at, ...whole, segments, retryLimited }
82  const recorded = [...journal.recorded, draft.toolUseId].slice(-MAX_RECORDED)
83  const blocks = [...openBlocks(journal, draft.at, minutes), block]
84  const evicted = blocks.length > MAX_BLOCKS ? blocks[0] : undefined
85  const eviction = evicted === undefined ? journal.eviction : { blockId: evicted.id, at: draft.at, toolUseId: draft.toolUseId }
86  return { ...journal, seq, blocks: blocks.slice(-MAX_BLOCKS), guards: [...guards], recorded, eviction }
87}
88
89/** The journal with the blocks a workaround matched renewed: their window starts again at `now`, or stays at a later match that a parallel check saved first. */
90export function renewed(journal: ArresterJournal, ids: readonly string[], now: number): ArresterJournal {
91  return { ...journal, blocks: journal.blocks.map(block => (ids.includes(block.id) ? { ...block, lastHitAt: Math.max(block.lastHitAt, now) } : block)) }
92}
93
94/** The journal with one more decision; the oldest past 50 is dropped. */
95export function withDecision(journal: ArresterJournal, decision: ArresterDecision): ArresterJournal {
96  return { ...journal, decisions: [...journal.decisions, decision].slice(-MAX_DECISIONS) }
97}
98
99/** The journal with a call held for the user. */
100export function withHeld(journal: ArresterJournal, held: ArresterHeld): ArresterJournal {
101  return { ...journal, held: [...journal.held, held] }
102}
103
104/** The journal without the held call `toolUseId`: its hold has ended. */
105export function withoutHeld(journal: ArresterJournal, toolUseId: string): ArresterJournal {
106  return { ...journal, held: journal.held.filter(held => held.toolUseId !== toolUseId) }
107}
108
109/** The journal with the open block `id` closed, or every open block when `id` is undefined, and the ids it closed; a clear that closes something is recorded. */
110export function cleared(journal: ArresterJournal, id: string | undefined, now: number, minutes: number): { journal: ArresterJournal; closed: string[] } {
111  const open = openBlocks(journal, now, minutes)
112  const closed = open.filter(block => id === undefined || block.id === id).map(block => block.id)
113  if (closed.length === 0) return { journal, closed }
114  const kept = { ...journal, blocks: open.filter(block => !closed.includes(block.id)) }
115  return { journal: withDecision(kept, { at: now, action: 'cleared', blockIds: closed, detail: 'by the user' }), closed }
116}
117
118/** The journal at `now` with expired blocks dropped, and every held call too when `dropHeld` (a reload ends the hooks that held them); the same object when nothing changes. */
119export function pruned(journal: ArresterJournal, now: number, minutes: number, dropHeld: boolean): ArresterJournal {
120  const blocks = openBlocks(journal, now, minutes)
121  const isHeldDropped = dropHeld && journal.held.length > 0
122  if (blocks.length === journal.blocks.length && !isHeldDropped) return journal
123  return { ...journal, blocks, held: isHeldDropped ? [] : journal.held }
124}
125
126/** When the next open block closes; undefined when none is open. */
127export function nextClose(journal: ArresterJournal, minutes: number): number | undefined {
128  const times = journal.blocks.map(block => closesAt(block, minutes))
129  return times.length === 0 ? undefined : Math.min(...times)
130}
131
132/** Whether a blocked full search of a folder may keep the folder as a read target: its deny `reason` names no other tool to use, and no spelling of it is `/` or is or holds one of the `spared` paths, such as the home, session, and project folders and the guard files. */
133export function isTargetFolder(spellings: readonly string[], spared: readonly string[], reason: string): boolean {
134  return !SUGGESTS_TOOL.test(reason) && spellings.every(folder => folder !== '/' && !spared.some(path => path === folder || path.startsWith(`${folder}/`)))
135}
136
137/** The targets of a blocked call: the first 256 paths its effects touch, each with the strongest access; recursive reads and lists, and paths that an MCP call names, give none. */
138export function targetsOf(effects: readonly Effect[]): ArresterTarget[] {
139  const targets = new Map<string, ArresterTarget>()
140  for (const effect of effects) {
141    if (effect.kind === 'name' || ((effect.kind === 'read' || effect.kind === 'list') && effect.recursive)) continue
142    const access: ArresterAccess = effect.kind === 'list' ? 'read' : effect.kind
143    const key = JSON.stringify([effect.path, effect.glob, effect.globOptions?.dot === true, effect.globOptions?.nocase === true, effect.entry === true, selectionKey(effect.selection)])
144    const found = targets.get(key)
145    if (found !== undefined) {
146      if (STRENGTH[access] > STRENGTH[found.access]) found.access = access
147    } else if (targets.size < MAX_TARGETS) targets.set(key, { path: effect.path, access, ...(effect.globOptions === undefined ? {} : { globOptions: effect.globOptions }), ...(effect.glob === undefined ? {} : { glob: effect.glob }), ...(effect.entry === true ? { entry: true } : {}), ...(effect.selection === undefined ? {} : { selection: effect.selection }) })
148  }
149  return [...targets.values()]
150}
151
152/** What naming a target reads besides the word: the words of the blocked command, and the shared glob budget. */
153type Naming = { spelled: ReadonlySet<string>; meter: Meter }
154
155/** The targets of a blocked call that its deny `reason` names, when there are two or more and the reason names some but not all of them; else every target. Every target stays when the blocked `command` names `cd`, `pushd`, or `popd`. Each word of the reason, cut at blanks, is read in the two forms of `formsOf`, and names a target when one of its forms does: when the form is the target's absolute path or real path, its spelling in the blocked `command`, its path relative to the session folder `cwd`, or its file name, or one of these for a folder that holds the target; a relative form is read in `cwd` and `~` in `home`. A form with `*` names a target with no glob whose file name or path it matches as a glob; a glob target is named only by its glob text or a folder that holds it. Every target stays when the reason has more than MAX_REASON_WORDS distinct words, when a form holds `**`, when a word with a form that looks like a path names no target in either form, when a glob target can match a form of a word that does not name it, or past the glob budget. */
156export function namedTargets(targets: readonly ArresterTarget[], reason: string, command: string | undefined, cwd: string, home: string | undefined): ArresterTarget[] {
157  if (targets.length < 2 || (command !== undefined && movesFolder(command))) return [...targets]
158  const words = new Map<string, string[]>()
159  for (const word of reason.split(/\s+/)) {
160    const forms = [...new Set(formsOf(word))].filter(form => form !== '')
161    if (forms.length > 0) words.set(forms.join(' '), forms)
162    if (words.size > MAX_REASON_WORDS) return [...targets]
163  }
164  const naming: Naming = { spelled: new Set(command?.split(SPELLING_BREAKS) ?? []), meter: { steps: 0, limit: MAX_NAME_STEPS } }
165  const named = new Set<ArresterTarget>()
166  for (const forms of words.values()) {
167    if (forms.some(form => form.includes('**'))) return [...targets]
168    const read = forms.map(form => ({ form, resolved: form === '~' || form.startsWith('~/') ? (home === undefined ? undefined : foldPath(`${home}${form.slice(1)}`)) : joinPath(cwd, form) }))
169    const hits = new Set(targets.filter(target => read.some(({ form, resolved }) => names(form, resolved, target, naming))))
170    if (hits.size === 0 && forms.some(form => isPathWord(form, naming))) return [...targets]
171    if (targets.some(target => !hits.has(target) && read.some(({ form, resolved }) => globCanMatch(form, resolved, target, naming.meter)))) return [...targets]
172    if (naming.meter.steps > MAX_NAME_STEPS) return [...targets]
173    for (const hit of hits) named.add(hit)
174  }
175  const kept = targets.filter(target => named.has(target))
176  return kept.length > 0 && kept.length < targets.length ? kept : [...targets]
177}
178
179/** Whether a blocked command can change its folder: without its quoting, it names `cd`, `pushd`, or `popd` as a word, in any part. */
180function movesFolder(command: string): boolean {
181  return command.replace(QUOTING, '').split(NAME_BREAKS).some(word => FOLDER_CHANGERS.has(word))
182}
183
184/** Whether a form of a word of a deny reason looks like a path: it holds `/` or `*`, or it is a word of the blocked command. */
185function isPathWord(word: string, naming: Naming): boolean {
186  return word.includes('/') || word.includes('*') || naming.spelled.has(word)
187}
188
189/** The two forms of a word of a deny reason, both without the quotes, backticks, and brackets at its two ends: the first also without the `.`, `,`, `;`, `:`, `!`, and `?` at its end; the second keeps those that come before the last closing quote or bracket of the word, or all of them when the word ends in none, so `` `secret.`, `` gives `secret` and `secret.`. */
190function formsOf(word: string): [string, string] {
191  let start = 0
192  let end = word.length
193  let kept = end
194  while (start < end && WORD_EDGES.has(word.charAt(start))) start += 1
195  while (end > start && (WORD_EDGES.has(word.charAt(end - 1)) || WORD_ENDS.has(word.charAt(end - 1)))) {
196    end -= 1
197    if (WORD_EDGES.has(word.charAt(end))) kept = end
198  }
199  return [word.slice(start, end), word.slice(start, kept)]
200}
201
202/** Whether a form of a word of a deny reason, `resolved` as a path, names a target, or a folder that holds it, by its path or real path: as an absolute path, or a relative one read in the session folder, or `~` read in the home folder; as a file or folder name; as a spelling of the blocked command that the path ends with; or, with `*`, as a glob. A glob target's glob text names it, in the same forms, but no other part of the glob does, and no glob word. */
203function names(word: string, resolved: string | undefined, target: ArresterTarget, naming: Naming): boolean {
204  const paths = target.real === undefined ? [target.path] : [target.path, target.real]
205  const spelling = naming.spelled.has(word) ? word.replace(/^(?:\.\/)+/, '').replace(/\/+$/, '') : ''
206  return paths.some(path => {
207    const place = target.glob === undefined ? path : `${path === '/' ? '' : path}/${target.glob}`
208    if (resolved !== undefined && (place === resolved || path === resolved || path.startsWith(resolved === '/' ? '/' : `${resolved}/`))) return true
209    if (!word.includes('/') && (word === target.glob || path.split('/').includes(word))) return true
210    if (spelling !== '' && !spelling.startsWith('/') && (place.endsWith(`/${spelling}`) || path.endsWith(`/${spelling}`) || path.includes(`/${spelling}/`))) return true
211    return target.glob === undefined && word.includes('*') && globNames(word, resolved, place, naming.meter)
212  })
213}
214
215/** Whether a glob target can match a form of a word of a deny reason as a file: the form `resolved` as a path, or a form without `/` as a file name that the last segment of the glob matches; past the budget, or when the comparison is uncertain, it can. */
216function globCanMatch(word: string, resolved: string | undefined, target: ArresterTarget, meter: Meter): boolean {
217  const glob = target.glob
218  if (glob === undefined) return false
219  if (meter.steps > MAX_NAME_STEPS) return true
220  const paths = target.real === undefined ? [target.path] : [target.path, target.real]
221  if (resolved !== undefined && paths.some(path => relation({ path: resolved, recursive: false }, path, meter, glob, target.globOptions) !== undefined)) return true
222  if (word.includes('/')) return false
223  const last = glob.slice(glob.lastIndexOf('/') + 1)
224  const nocase = target.globOptions?.nocase === true
225  return globMatch(nocase ? foldCase(last) : last, nocase ? foldCase(word) : word, meter, target.globOptions?.dot === true) !== false
226}
227
228/** Whether a word with `*` matches the file name of a place, or the place as a path when it is read as `resolved`, with each name a glob; past the budget it counts as a match. */
229function globNames(word: string, resolved: string | undefined, place: string, meter: Meter): boolean {
230  if (globMatch(word, basenameOf(place), meter, true) !== false) return true
231  if (resolved === undefined) return false
232  const pattern = resolved.split('/')
233  const parts = place.split('/')
234  return pattern.length === parts.length && pattern.every((part, i) => globMatch(part, parts[i] ?? '', meter, true) !== false)
235}
236
hooks/match.ts 225 lines
1// Which open blocks a call matches, and why, by four rules: target, retry,
2// guard, and opaque. Pure: no `$`.
3
4import type { CallAnalysis } from './analyze/call'
5import { foldCase, relation } from './analyze/paths'
6import type { Meter, Relation } from './analyze/paths'
7import { OPAQUE } from './analyze/shell'
8import type { Effect, EffectKind } from './analyze/types'
9import type { ArresterAccess, ArresterBlock, ArresterGuard, ArresterTarget } from '../types/index'
10
11/** An effect with the real path of its path, when the file system gave one that differs; `folder` marks a `name` effect on a folder that exists. */
12export type PlacedEffect = Effect & { real?: string; folder?: true }
13
14/** One reason a call matches a block; paths are written for people, a glob as its folder and segment. A guard finding names the guard the effect touched, the launch flag or plugin command that can turn guards off, or a nested `claude` run. */
15export type Finding =
16  | { rule: 'target'; block: ArresterBlock; kind: EffectKind; path: string; relation: Relation; target: string; access: ArresterAccess }
17  | { rule: 'retry'; block: ArresterBlock }
18  | { rule: 'guard'; block: ArresterBlock; kind: EffectKind; path: string; relation: Relation; guard: string }
19  | { rule: 'guard'; block: ArresterBlock; bypass: string }
20  | { rule: 'opaque'; block: ArresterBlock; reason: string }
21
22/** The most comparison steps one check takes; past them it stops comparing and the call is opaque, so a crafted call cannot hold the check. */
23const MAX_STEPS = 20_000_000
24
25/** Every finding of a call of `tool` against the open blocks: target findings for each block that is not a user rejection and retry findings for each block, oldest first, then guard and opaque findings against the newest block that is not a user rejection. A Grep or Glob call gets no target finding from a block of a `Read(` rule, since Claude Code leaves the files of Read deny rules out of those searches. */
26export function findings(open: readonly ArresterBlock[], guards: readonly ArresterGuard[], call: CallAnalysis, effects: readonly PlacedEffect[], tool = ''): Finding[] {
27  const found: Finding[] = []
28  const meter: Meter = { steps: 0 }
29  const candidates = targetCandidates(open, effects, meter)
30  const isSearchTool = tool === 'Grep' || tool === 'Glob'
31  for (const block of open) {
32    for (const effect of block.source === 'user' || (isSearchTool && block.rule?.startsWith('Read(') === true) ? [] : effects) {
33      if (meter.steps > MAX_STEPS) break
34      const hit = targetHit(block, effect, meter, candidates.get(effect))
35      if (hit !== undefined) found.push({ rule: 'target', block, kind: effect.kind, path: shown(effect.path, effect.glob), relation: hit.relation, target: shown(hit.target.path, hit.target.glob), access: hit.target.access })
36    }
37    if (isRetry(block, call)) found.push({ rule: 'retry', block })
38  }
39  const newest = open.filter(block => block.source !== 'user').at(-1)
40  if (newest === undefined) return found
41  for (const effect of effects) {
42    if (meter.steps > MAX_STEPS) break
43    const hit = guardHit(effect, guards, meter)
44    if (hit !== undefined) found.push({ rule: 'guard', block: newest, kind: effect.kind, path: shown(effect.path, effect.glob), relation: hit.relation, guard: hit.guard })
45  }
46  if (call.bypass !== undefined) found.push({ rule: 'guard', block: newest, bypass: call.bypass })
47  const reason = call.opaque ?? (meter.uncertain ? 'path comparison exceeded its work budget; target overlap is uncertain' : undefined) ?? (meter.steps > MAX_STEPS ? OPAQUE.tooMany : undefined)
48  if (reason !== undefined) found.push({ rule: 'opaque', block: newest, reason })
49  return found
50}
51
52/** Literal-prefix index of retained targets, including their real spellings. */
53type TargetNode = { children: Map<string, TargetNode>; targets: Set<ArresterTarget> }
54
55/** Selects only targets whose literal prefixes overlap an effect, charging index work to the shared budget. A case-insensitive glob on either side compares its folders without case, so case-insensitive targets sit in an index of case-folded folders, and a case-insensitive effect looks up the other targets by their folded folders too. */
56function targetCandidates(open: readonly ArresterBlock[], effects: readonly PlacedEffect[], meter: Meter): Map<PlacedEffect, Set<ArresterTarget>> {
57  const retained = open.flatMap(block => (block.source === 'user' ? [] : block.targets))
58  const sensitive = retained.filter(target => target.globOptions?.nocase !== true)
59  const loose = retained.filter(target => target.globOptions?.nocase === true)
60  const insensitive = targetIndex(loose, true, meter)
61  if (insensitive === undefined) return new Map()
62  let exact: TargetNode | undefined
63  let folded: TargetNode | undefined
64  const cache = new Map<string, Set<ArresterTarget>>()
65  const result = new Map<PlacedEffect, Set<ArresterTarget>>()
66  for (const effect of effects) {
67    const nocase = effect.globOptions?.nocase === true
68    const key = `${effect.path}\0${effect.real ?? ''}\0${nocase}`
69    let candidates = cache.get(key)
70    if (candidates === undefined) {
71      const root = nocase ? (folded ??= targetIndex(sensitive, true, meter)) : (exact ??= targetIndex(sensitive, false, meter))
72      if (root === undefined) return new Map()
73      candidates = new Set()
74      for (const path of spellings(effect.path, effect.real)) {
75        meter.steps += path.length
76        gather(root, path, nocase, candidates, meter)
77        if (loose.length > 0) gather(insensitive, path, true, candidates, meter)
78      }
79      cache.set(key, candidates)
80    }
81    result.set(effect, candidates)
82  }
83  return result
84}
85
86/** The index of the targets by the folders of each spelling, case-folded when `fold` is set; undefined past the step budget. */
87function targetIndex(targets: readonly ArresterTarget[], fold: boolean, meter: Meter): TargetNode | undefined {
88  const root: TargetNode = { children: new Map(), targets: new Set() }
89  for (const target of targets) {
90    for (const path of spellings(target.path, target.real)) {
91      meter.steps += path.length
92      if (meter.steps > MAX_STEPS) return undefined
93      let node = root
94      for (const part of folders(path, fold)) {
95        let child = node.children.get(part)
96        if (child === undefined) {
97          child = { children: new Map(), targets: new Set() }
98          node.children.set(part, child)
99        }
100        node = child
101      }
102      node.targets.add(target)
103    }
104  }
105  return root
106}
107
108/** Adds to `candidates` the indexed targets at or above the path and every target below it. */
109function gather(root: TargetNode, path: string, fold: boolean, candidates: Set<ArresterTarget>, meter: Meter): void {
110  let node: TargetNode | undefined = root
111  for (const part of folders(path, fold)) {
112    meter.steps += node.targets.size
113    if (meter.steps > MAX_STEPS) break
114    for (const target of node.targets) candidates.add(target)
115    node = node.children.get(part)
116    if (node === undefined) break
117  }
118  const pending = node === undefined ? [] : [node]
119  while (pending.length > 0 && meter.steps <= MAX_STEPS) {
120    const next = pending.pop()!
121    meter.steps += 1 + next.targets.size + next.children.size
122    for (const target of next.targets) candidates.add(target)
123    pending.push(...next.children.values())
124  }
125}
126
127/** The folders of a path, each case-folded as a case-insensitive comparison reads it when `fold` is set. */
128function folders(path: string, fold: boolean): string[] {
129  return (fold ? foldCase(path) : path).split('/').filter(Boolean)
130}
131
132/** The blocks that findings name, in order, each once. */
133export function blocksOf(found: readonly Finding[]): ArresterBlock[] {
134  return [...new Set(found.map(finding => finding.block))]
135}
136
137/** The ids of the blocks that a target or retry finding names: the blocks a caught workaround renews; a guard or opaque finding alone renews none. */
138export function renewable(found: readonly Finding[]): string[] {
139  return blocksOf(found.filter(finding => finding.rule === 'target' || finding.rule === 'retry')).map(block => block.id)
140}
141
142/** A path as people read it: a glob as its folder and its glob segment. */
143export function shown(path: string, glob: string | undefined): string {
144  if (glob === undefined) return path
145  return path === '/' ? `/${glob}` : `${path}/${glob}`
146}
147
148/** The target rule: the first target of the block that the effect meets in a way its access counts. */
149function targetHit(block: ArresterBlock, effect: PlacedEffect, meter: Meter, candidates: ReadonlySet<ArresterTarget> | undefined): { target: ArresterTarget; relation: Relation } | undefined {
150  if (candidates === undefined || candidates.size === 0) return undefined
151  for (const target of block.targets) {
152    if (!candidates.has(target)) continue
153    if (meter.steps > MAX_STEPS) break
154    const found = meet(effect, target.path, target.real, target.glob, meter, target.selection, target.globOptions)
155    if (found === 'uncertain') {
156      meter.uncertain = true
157      continue
158    }
159    if (found !== undefined && counts(target.access, effect.kind, found, effect.folder === true)) return { target, relation: found }
160  }
161  return undefined
162}
163
164/** Whether an effect of `kind` that stands in `found` to a target of `access` gets the result the guard blocked; a path that an MCP call names counts for a read target, and for another target when it is the same path and no folder that exists. */
165function counts(access: ArresterAccess, kind: EffectKind, found: Relation, folder: boolean): boolean {
166  if (kind === 'name') return access === 'read' || (found === 'same' && !folder)
167  if (kind === 'destroy') return true
168  if (kind === 'write') return found === 'same' || (access === 'write' && found === 'contains')
169  if (kind === 'read') return access === 'read'
170  return access === 'read' && found !== 'contains'
171}
172
173/** The retry rule: the call repeats the blocked command as a whole, or holds the one command the block had, unless it only drops some of the blocked flags. A block from the user's own rejection matches only the same call again: the complete syntax-preserving input and context identity. */
174function isRetry(block: ArresterBlock, call: CallAnalysis): boolean {
175  const isWhole = call.skeleton !== '' && call.skeleton === block.skeleton
176  const exact = call.exact !== undefined && call.exact === block.exact
177  if (block.source === 'user') return exact
178  const preview = call.segments.some(segment => segment.preview)
179  if (exact && !preview) return true
180  if (block.flagsComplete && isWhole) {
181    if (!preview && !dropsFlags(call.flags, block.flags)) return true
182    if (preview && call.segments.some(segment => !segment.preview && block.segments.some(blocked => blocked.flagsComplete && blocked.skeleton === segment.skeleton && !dropsFlags(segment.flags, blocked.flags)))) return true
183  }
184  const [only, ...others] = block.segments
185  return only !== undefined && others.length === 0 && call.segments.some(segment => !segment.preview && only.flagsComplete && segment.skeleton === only.skeleton && !dropsFlags(segment.flags, only.flags))
186}
187
188/** True when the later flags are some, not all, of the blocked ones: the retry drops flags, and a guard that objected to the command itself sees it again. */
189function dropsFlags(later: readonly string[], blocked: readonly string[]): boolean {
190  return later.length < blocked.length && later.every(flag => blocked.includes(flag))
191}
192
193/** The guard rule compares only recorded guard paths; containing reads and change-only guard reads do not count. */
194function guardHit(effect: PlacedEffect, guards: readonly ArresterGuard[], meter: Meter): { guard: string; relation: Relation } | undefined {
195  const changes = effect.kind === 'write' || effect.kind === 'destroy'
196  for (const guard of guards) {
197    if (guard.changeOnly === true && !changes) continue
198    const found = meet(effect, guard.path, guard.real, undefined, meter)
199    if (found === 'uncertain') meter.uncertain = true
200    if (found === 'same' || found === 'inside' || (found === 'contains' && changes)) return { guard: guard.path, relation: found }
201  }
202  return undefined
203}
204
205/** How an effect stands to a protected path: the first relation among the pairs of {written, real} spellings, effect first. */
206function meet(effect: PlacedEffect, path: string, real: string | undefined, glob: string | undefined, meter: Meter, selection?: ArresterTarget['selection'], globOptions?: ArresterTarget['globOptions']): Relation | 'uncertain' | undefined {
207  for (const spelling of spellings(effect.path, effect.real)) {
208    for (const place of spellings(path, real)) {
209      if (selection !== undefined) {
210        const selected = relation({ path: place, glob, globOptions, recursive: true, selection }, spelling, meter, effect.glob, effect.globOptions)
211        if (selected === undefined) continue
212        if (selected === 'uncertain') return selected
213      }
214      const found = relation({ path: spelling, glob: effect.glob, globOptions: effect.globOptions, recursive: effect.recursive, selection: effect.selection }, place, meter, glob, globOptions)
215      if (found !== undefined) return found
216    }
217  }
218  return undefined
219}
220
221/** A path, and its real path when that differs. */
222function spellings(path: string, real: string | undefined): string[] {
223  return real === undefined || real === path ? [path] : [path, real]
224}
225
hooks/text.ts 367 lines
1// Every text Arrester shows or sends: the reasons Claude reads, the question,
2// the transcript lines, the status line, the /arrester output, the
3// diagnostics, and the reading of block messages in tool results. Pure: no `$`.
4
5import type { HookFailure } from 'claude-code'
6
7import type { Call } from './analyze/call'
8import { sortedJson } from './analyze/call'
9import { NESTED_SESSION } from './analyze/shell'
10import type { EffectKind, Ignored } from './analyze/types'
11import { blocksOf, shown } from './match'
12import type { Finding } from './match'
13import type { ArresterBlock, ArresterCall, ArresterDecision, ArresterHeld, ArresterJournal, ArresterSource } from '../types/index'
14
15/** The menu line of `/arrester`. */
16export const COMMAND_DESCRIPTION = 'Show the calls Arrester keeps blocked, or close blocks with clear'
17/** The hint after `/arrester` in the menu. */
18export const ARGUMENT_HINT = '[history [page] | clear [id]]'
19/** What `/arrester` prints for input it does not take. */
20export const USAGE = 'Usage: /arrester, /arrester history [1-5], /arrester clear, or /arrester clear <id>'
21/** The options and the chip of the hold question. */
22export const DENY = 'Deny'
23export const ALLOW_ONCE = 'Allow once'
24export const HEADER = 'Arrester'
25
26/** How a hold ended without "Allow once". */
27type HoldEnd = { kind: 'refused' } | { kind: 'typed'; text: string } | { kind: 'unanswered'; cause: string }
28
29const MAX_SUMMARY = 120
30const MAX_REASON = 300
31const MAX_QUESTION = 600
32const LABELS: Readonly<Record<ArresterSource, string>> = { hook: 'settings hook', rule: 'deny rule', classifier: 'auto mode classifier', user: 'user rejection' }
33const VERBS: Readonly<Record<EffectKind, string>> = { destroy: 'deletes or discards', write: 'writes', read: 'reads', list: 'lists', name: 'names' }
34const REJECTED = "The user doesn't want to proceed with this tool use."
35const HOOK_ERROR = /^PreToolUse:\S+ hook error: \[/
36const HOOK_PREFIX = /^PreToolUse:\S+ hook error: /
37const COMMAND_END = ']: '
38const CLASSIFIER = 'Permission for this action was denied by the Claude Code auto mode classifier. Reason: '
39const ADVICE = '. If you have other tasks'
40const STOP = 'Do not try another way to get the same result.'
41
42/** `text` on one line, cut to `max` characters with `…`. */
43function line(text: string, max: number): string {
44  const flat = text.replace(/\s+/g, ' ').trim()
45  return flat.length <= max ? flat : `${flat.slice(0, max - 1)}…`
46}
47
48/** A call in one line of at most 120 characters: its command or path, else its input as JSON. */
49export function summarize(call: Call): string {
50  const input = call.input
51  const main = [input.command, input.file_path, input.notebook_path, input.pattern, input.url].find(value => typeof value === 'string')
52  const text = typeof main === 'string' ? `${main} ${typeof input.path === 'string' ? input.path : ''}` : sortedJson(input) ?? '[input exceeds analysis limit]'
53  return line(text, MAX_SUMMARY)
54}
55
56/** A guard's reason without the hook error prefix, and without the hook command when the prefix names one. */
57export function guardMessage(reason: string): string {
58  const hook = blockOfText(reason)
59  return hook?.source === 'hook' ? hook.reason : reason.replace(HOOK_PREFIX, '')
60}
61
62/** A guard's reason as a block keeps it: its `guardMessage` on one line of at most 300 characters. */
63export function storedReason(reason: string): string {
64  return line(guardMessage(reason), MAX_REASON)
65}
66
67/** The tool, summary, and subagent of a call, as the journal shows it. */
68export function viewOf(call: Call): ArresterCall {
69  const view = { tool: call.tool, summary: summarize(call) }
70  return call.agentId === undefined ? view : { ...view, agentId: call.agentId }
71}
72
73/** The message of something thrown or rejected. */
74export function causeOf(error: unknown): string {
75  return error instanceof Error ? error.message : String(error)
76}
77
78/** A hook failure as the diagnostics name it: its kind and its message. */
79export function failureOf(failure: HookFailure): string {
80  return failure.message === undefined ? failure.kind : `${failure.kind}: ${failure.message}`
81}
82
83/** Whether an auto mode denial carries a classifier verdict; the two no-verdict reasons do not. */
84export function hasVerdict(reason: string): boolean {
85  return !reason.startsWith('Classifier unavailable') && !reason.startsWith('Auto mode could not evaluate this action')
86}
87
88/** The block a tool result's text reports, read from the stable prefixes only: the user's rejection; a settings hook's exit-code-2 error, whose reason follows the first `]: ` after the hook command; or a classifier verdict, which ends before the advice that follows it. */
89export function blockOfText(text: string): { source: ArresterSource; reason: string } | undefined {
90  if (text.startsWith(REJECTED)) return { source: 'user', reason: text }
91  const hook = HOOK_ERROR.exec(text)
92  if (hook !== null) {
93    const rest = text.slice(hook[0].length)
94    const end = rest.indexOf(COMMAND_END)
95    return { source: 'hook', reason: end < 0 ? rest : rest.slice(end + COMMAND_END.length) }
96  }
97  if (!text.startsWith(CLASSIFIER)) return undefined
98  const rest = text.slice(CLASSIFIER.length)
99  const end = rest.indexOf(ADVICE)
100  const verdict = end < 0 ? rest : rest.slice(0, end)
101  return hasVerdict(verdict) ? { source: 'classifier', reason: verdict } : undefined
102}
103
104/** One finding in words. */
105export function phrase(finding: Finding): string {
106  if (finding.rule === 'retry' && finding.block.source === 'user') return 'it repeats the call the user rejected'
107  if (finding.rule === 'retry') return 'it repeats the blocked command with other flags or argument order'
108  if (finding.rule === 'opaque') return `Arrester cannot fully analyze this call (${finding.reason})`
109  if (finding.rule === 'guard') {
110    if ('bypass' in finding) return finding.bypass === NESTED_SESSION ? 'it runs a nested claude, which does not inherit the open blocks' : `it runs claude ${finding.bypass}, which can turn off guards`
111    if (finding.relation === 'contains') return `it ${VERBS[finding.kind]} ${finding.path}, which contains the guard file ${finding.guard}`
112    return `it ${finding.kind === 'name' ? 'names' : finding.kind === 'read' || finding.kind === 'list' ? 'reads or lists' : 'changes'} ${finding.path}, a guard file`
113  }
114  const where = finding.relation === 'same' ? '' : `, which ${finding.relation === 'inside' ? 'is inside' : 'contains'} ${finding.target}`
115  return `it ${VERBS[finding.kind]} ${finding.path}${where} (blocked: ${finding.access})`
116}
117
118/** Who blocked a call: the guard, with the rule for a deny rule. */
119function label(block: ArresterBlock): string {
120  return block.rule === undefined ? LABELS[block.source] : `${LABELS[block.source]} ${block.rule}`
121}
122
123/** How long ago a moment was, in whole minutes. */
124function ago(ms: number): string {
125  return `${Math.max(0, Math.floor(ms / 60_000))} min ago`
126}
127
128/** A call in a line: its tool, the subagent that made it, and its summary. */
129function who(call: ArresterCall): string {
130  return `${call.tool}${call.agentId === undefined ? '' : ` in agent ${call.agentId}`}: ${call.summary}`
131}
132
133/** The Blocked and Match lines of a reason: the blocks the first three findings name, and those findings. */
134function evidence(found: readonly Finding[], now: number): string[] {
135  const shownFindings = found.slice(0, 3)
136  return [
137    ...blocksOf(shownFindings).map(block => `Blocked: ${block.id}, ${label(block)}, ${ago(now - block.at)}: ${block.tool} \`${block.summary}\`. Reason: ${block.reason}`),
138    `Match: ${shownFindings.map(finding => line(phrase(finding), 600)).join('; ')}.`,
139  ]
140}
141
142/** The reason Claude reads when Arrester stops a workaround. */
143export function denyReason(found: readonly Finding[], now: number): string {
144  const blocks = blocksOf(found.slice(0, 3))
145  const close = blocks.length === 1 ? `The user can close this block with /arrester clear ${blocks.map(block => block.id).join('')}.` : 'The user can close these blocks with /arrester clear.'
146  return [
147    'Arrester stopped this call. It looks like a workaround of a call that a guard blocked.',
148    ...evidence(found, now),
149    `${STOP} Stop, tell the user what was blocked and why, and wait for their instructions. ${close}`,
150  ].join('\n')
151}
152
153/** The reason Claude reads when a hold ends in a refusal. */
154export function heldReason(end: HoldEnd, found: readonly Finding[], now: number): string {
155  const first =
156    end.kind === 'refused'
157      ? 'Arrester held this call and the user chose Deny.'
158      : end.kind === 'typed'
159        ? `Arrester held this call and the user did not allow it. The user answered: "${line(end.text, MAX_REASON)}".`
160        : `Arrester held this call and got no answer (${end.cause}), so it refused it.`
161  return [first, ...evidence(found, now), `${STOP} Wait for the user's instructions.`].join('\n')
162}
163
164/** The reason Claude reads when the check itself failed while blocks are open. */
165export function uncheckedReason(cause: string): string {
166  return `Arrester could not check this call against its open blocks (${cause}), so it refused it. Tell the user. The user can close the blocks with /arrester clear.`
167}
168
169/** The hold question, at most 600 characters, the summaries cut first; it ends with its question mark. */
170export function question(call: Call, first: Finding, now: number): string {
171  const block = first.block
172  const ask = (room: number): string => [
173    `Arrester held a ${call.tool} call in ${call.agentId === undefined ? 'the main session' : `agent ${call.agentId}`}. It may get the same result as block ${block.id} (${label(block)}, ${ago(now - block.at)}: ${line(block.summary, room)}).`,
174    `Call: ${line(summarize(call), room)}`,
175    `Why: ${phrase(first)}.`,
176    'Allow this call once?',
177  ].join('\n')
178  const fits = [MAX_SUMMARY, 60, 20].map(ask).find(text => text.length <= MAX_QUESTION)
179  if (fits !== undefined) return fits
180  const tail = '\nAllow this call once?'
181  return `${ask(20).slice(0, MAX_QUESTION - tail.length - 1)}…${tail}`
182}
183
184/** The transcript line of a stop or of the end of a hold; Claude does not read it. */
185export function decisionLine(decision: ArresterDecision, call: ArresterCall): string {
186  const ids = decision.blockIds.join(', ')
187  if (decision.action === 'stopped') return `stopped ${who(call)} (workaround of ${ids}: ${line(decision.detail, 1500)})`
188  if (decision.action === 'allowed-once') return `allowed once ${who(call)} (held for ${ids})`
189  if (decision.action === 'refused') return `refused ${who(call)} (held for ${ids}; the user answered: ${line(decision.detail, 1500)})`
190  return `refused ${who(call)} (held for ${ids}; no answer: ${line(decision.detail, 1500)})`
191}
192
193/** The transcript line when a session keeps open blocks from the session before a `/branch` or an in-app `/resume`; Claude does not read it. */
194export function keptLine(ids: readonly string[]): string {
195  return ids.length === 1 ? `this session keeps open block ${ids.join('')} from before the switch. /arrester clear closes it.` : `this session keeps open blocks ${ids.join(', ')} from before the switch. /arrester clear closes them.`
196}
197
198/** The status line: open blocks and held calls, or nothing when there are none. */
199export function statusLine(open: number, held: number, evicted?: string): string | undefined {
200  if (open === 0 && held === 0 && evicted === undefined) return undefined
201  const blocks = `${open} open block${open === 1 ? '' : 's'}`
202  const current = held === 0 ? blocks : `${blocks} · ${held} held`
203  return evicted === undefined ? current : `${current} · ${evicted} evicted, protection lost`
204}
205
206/** The `/arrester` text: the options, the open blocks, the held calls, and the last ten decisions. */
207export function statusText(journal: ArresterJournal, open: readonly ArresterBlock[], response: string, minutes: number, now: number): string {
208  const recent = journal.decisions.slice(-10)
209  return [
210    `response: ${response} · a block stays open ${minutes} min after it opens or after its last target or retry match`,
211    '',
212    ...(journal.eviction === undefined ? [] : [`Capacity: ${evictionText(journal.eviction.blockId)} (${ago(now - journal.eviction.at)})`, '']),
213    `Open blocks (${open.length})`,
214    ...open.flatMap(block => [
215      `  ${block.id}  ${ago(now - block.at)}, turn ${block.turn}  ${label(block)}  ${block.tool}${block.agentId === undefined ? '' : ` in agent ${block.agentId}`}: ${block.summary}`,
216      `      reason: ${block.reason}`,
217      ...(block.retryLimited === undefined ? [] : [`      retry coverage: ${block.retryLimited}`]),
218      ...(block.source === 'user' && block.exact === undefined ? ['      exact retry identity unavailable: the tool input exceeds the serialization limits, so this block matches no retry'] : []),
219      ...(block.incomplete === undefined ? [] : [`      incomplete capture: ${block.incomplete}`]),
220      `      targets: ${block.targets.length === 0 ? 'none (retries only)' : block.targets.slice(0, 32).map(target => `${line(shown(target.path, target.glob), 240)} (${target.access})`).join(', ') + (block.targets.length > 32 ? `; ${block.targets.length - 32} more retained targets` : '')}`,
221    ]),
222    '',
223    `Held calls (${journal.held.length})`,
224    ...journal.held.map(held => `  ${ago(now - held.since)}  ${held.blockIds.join(', ')}  ${who(held)}`),
225    '',
226    `Recent decisions (${recent.length})`,
227    ...recent.map(decision => `  ${ago(now - decision.at)}  ${decision.action.padEnd(12)} ${decision.blockIds.join(', ')}  ${decision.call === undefined ? '' : `${who(decision.call)}   `}${line(decision.detail, 600)}`),
228    '',
229    'Close blocks with /arrester clear [id].',
230  ].join('\n')
231}
232
233/** What `/arrester clear` prints. */
234export function clearedText(closed: readonly string[], id: string | undefined): string {
235  if (id !== undefined) return closed.length === 0 ? `No open block ${id}.` : `Closed block ${id}.`
236  return `Closed ${closed.length} block${closed.length === 1 ? '' : 's'}.`
237}
238
239/** What `/arrester clear` prints for an origin that is not the user's own. */
240export function clearRefused(kind: string): string {
241  return `/arrester clear needs the user at the prompt; this request came from ${kind}.`
242}
243
244/** Diagnostic: the check failed while blocks are open. */
245export function couldNotCheck(tool: string, failure: string, open: number | undefined, cause?: string): string {
246  const because = open === undefined ? `the open blocks could not be read${cause === undefined ? '' : ` (${cause})`}` : `${open} block${open === 1 ? ' is' : 's are'} open`
247  return `arrester: tool.call: could not check ${tool} (${failure}); refused it because ${because}`
248}
249
250/** Diagnostic: the hook failed and no block was open. */
251export function hookFailedNoBlock(failure: string): string {
252  return `arrester: tool.call: hook failed (${failure}); no block was open, so the call went on`
253}
254
255/** Diagnostic: a capture point could not record a block. */
256export function couldNotRecord(event: string, tool: string, failure: string): string {
257  return `arrester: ${event}: could not record the block for ${tool} (${failure}); workarounds of it will not be caught`
258}
259
260/** Diagnostic: a settings source could not be read while guards were computed. */
261export function couldNotReadSettings(event: string, source: string, message: string): string {
262  return `arrester: ${event}: could not read ${source} settings (${message}); hook files named there are not guarded`
263}
264
265/** Diagnostic: a script exists but could not be read. */
266export function couldNotReadScript(event: string, path: string, message: string): string {
267  return `arrester: ${event}: could not read the script ${path} (${message})`
268}
269
270/** Diagnostic: a path could not be placed on the file system. */
271export function couldNotResolve(event: string, path: string, message: string): string {
272  return `arrester: ${event}: could not resolve ${path} (${message}); compared as written`
273}
274
275/** Diagnostic: the `/arrester` hook failed. */
276export function commandFailed(failure: string): string {
277  return `arrester: command.run: /arrester failed (${failure})`
278}
279
280/** Diagnostic: `/arrester` could not be registered. */
281export function couldNotRegister(message: string): string {
282  return `arrester: session.start: could not register /arrester (${message})`
283}
284
285/** Diagnostic: the journal of the session that ends could not be kept for a branch or an in-app resume of it. */
286export function couldNotKeep(message: string): string {
287  return `arrester: session.end: could not keep the open blocks (${message}); a branch or resume of this session starts with none`
288}
289
290/** Diagnostic: a branch or an in-app resume could not restore the journal of the session before the switch. */
291export function couldNotRestore(message: string): string {
292  return `arrester: classic.SessionStart: could not restore the open blocks (${message}); this session starts with none`
293}
294
295/** Diagnostic: the status line could not be refreshed. */
296export function couldNotSync(event: string, message: string): string {
297  return `arrester: ${event}: could not refresh the status line (${message})`
298}
299
300/** Diagnostic: the hold question got no answer. */
301export function holdUnanswered(message: string): string {
302  return `arrester: tool.call: the hold got no answer (${message}); the call was refused`
303}
304
305/** Diagnostic: an auto mode denial without a classifier verdict. */
306export function noVerdict(reason: string): string {
307  return `arrester: classic.PermissionDenied: no classifier verdict (${reason}); no block opened`
308}
309
310/** Diagnostic: HOME is not set, so `~` paths resolve to nothing. */
311export function homeUnset(event: string): string {
312  return `arrester: ${event}: HOME is not set; ~ paths do not resolve; shell deletions may be stopped as unreadable`
313}
314
315/** Diagnostic: a stop or a hold could not be saved in the journal; what Arrester did to the call stands. */
316export function couldNotSave(tool: string, action: string, message: string): string {
317  return `arrester: tool.call: could not save the ${action} record for ${tool} (${message}); /arrester and the status line may not show it`
318}
319
320/** Diagnostic: code in a script older than the open blocks could not be read, so it was not counted. */
321export function notCounted(ignored: Ignored): string {
322  return `arrester: tool.call: ${ignored.path} changed before the open blocks, so code in it that Arrester cannot read (${ignored.reason}) was not counted`
323}
324
325/** Diagnostic: a reload ended a hold before the user answered it. */
326export function holdEnded(held: ArresterHeld): string {
327  return `arrester: session.start: a reload ended the hold of ${who(held)} (held for ${held.blockIds.join(', ')}); Arrester did not decide that call`
328}
329
330/** Omits the name supplied by the host on transcript and command rows. */
331export function hostText(text: string): string {
332  return text.replace(/^arrester: /, '')
333}
334
335/** Diagnostic: capture retained only the effects that analysis could read. */
336export function incompleteCapture(event: string, reason: string): string {
337  return `arrester: ${event}: incomplete capture (${reason}); some targets may be missing`
338}
339
340/** A bounded capacity notice names the block whose protection ended. */
341function evictionText(id: string): string {
342  return `${id} evicted at the 50-block capacity; its protection is lost`
343}
344
345/** Ten clipped decisions per page, newest page first, covering all retained history. */
346export function historyText(journal: ArresterJournal, page: number, now: number): string {
347  const decisions = journal.decisions.slice(-50).reverse()
348  const pages = Math.max(1, Math.ceil(decisions.length / 10))
349  if (!Number.isInteger(page) || page < 1 || page > pages) return USAGE
350  const rows = decisions.slice((page - 1) * 10, page * 10)
351  return [
352    `Decision history: page ${page}/${pages} (${decisions.length} retained)`,
353    ...rows.map(decision => line(`${ago(now - decision.at)} ${decision.action} ${decision.blockIds.join(', ')} ${decision.call === undefined ? '' : who(decision.call)} ${decision.detail}`, 160)),
354    'Use /arrester history <page>. Older evicted decisions are unavailable.',
355  ].join('\n')
356}
357
358/** Diagnostic: a successful capture evicted a block before its normal expiry. */
359export function evictionNotice(event: string, id: string): string {
360  return `arrester: ${event}: ${evictionText(id)}`
361}
362
363/** Diagnostic: a successful capture could not retain complete fuzzy retry coverage. */
364export function retryNotice(event: string, id: string, reason: string): string {
365  return `arrester: ${event}: ${id}: ${reason}`
366}
367
hooks/analyze/code.ts 316 lines
1// The interpreter code scan associates literal paths with file operations.
2// Pure: no `$`. Bounded lookahead keeps each scan linear.
3
4import { absolute, hasGlob, isRemote, looksLikePath, MAX_PATH, PATH_TOO_LONG, splitGlob } from './paths'
5import type { Effect, EffectKind } from './types'
6
7/** Words that make recognized file paths in the code a deletion: removal, moves, and running other programs, whose effects Arrester cannot see. */
8const DESTROY_WORDS: readonly string[] = ['rmtree', 'removedirs', 'remove_tree', 'os.remove', 'os.replace', 'unlink', 'rmdir', 'rmSync', '.rm(', 'rimraf', 'FileUtils.rm', 'File.delete', 'Deno.remove', 'rename', 'shutil.move', 'truncate', 'system(', 'subprocess', 'child_process', 'execSync', 'exec(', 'spawn', 'popen', 'Deno.Command', 'Deno.run']
9
10/** Words that make recognized file paths in the code a write. */
11const WRITE_WORDS: readonly string[] = ['write_text', 'write_bytes', 'writeFile', 'appendFile', 'createWriteStream', 'mkdir', 'makedirs', 'chmod', 'chown', 'copyfile', 'copytree', 'copyFile', 'shutil.copy', 'cpSync', 'symlink', 'File.write']
12
13/** Names of calls that read a file without a word in the lists above. */
14const READ_CALL = /(?:open|glob|readFile(?:Sync)?|read_text|read_bytes|readdir(?:Sync)?|readlink|stat|lstat|access|exists|read)$/
15
16/** A Python `shutil` copy, whose `src=` and `dst=` arguments are keywords. In other languages, `src = …` in a call is an assignment. */
17const SHUTIL_COPY = /(?:^|[^.]|shutil\.)(?:copy2?|copyfile|copytree|copymode|copystat)$/
18
19/** One method called on a path object, up to its `(`. */
20const METHOD = /\s*\.\s*([A-Za-z_][A-Za-z0-9_]*)\s*\(/y
21/** The next method called in a chain on a path object, past attributes such as `.parent`, up to its `(`. */
22const LINK = /\s*(?:\.\s*[A-Za-z_][A-Za-z0-9_]*\s*)*?\.\s*([A-Za-z_][A-Za-z0-9_]*)\s*\(/y
23const METHOD_WINDOW = 120
24
25/** Path methods that return the same file, so a file operation after them still acts on the path. */
26const SAME_FILE = new Set(['resolve', 'absolute', 'expanduser'])
27
28/** The end of the path expression after such a method: the end of an argument, a bracket, or a statement. */
29const CHAIN_END = /\s*(?:[)\]},;]|$)/y
30
31const MODE = /^[rwxabt+<>]{1,4}$/
32const PERL_MODE = /^\+?[<>]{1,2}/
33const WRITE_MODE = /[wax+>]/
34const SPECIFIER = /(?:\brequire\(|\bimport\(|\bfrom|\bimport)\s*$/
35const SPECIFIER_WINDOW = 16
36const BARE = /^\.{0,2}\/?$/
37
38/** What comes before a whole literal argument: `(`, `,`, a keyword and its `=`, or a bare `unlink`, `rmdir`, or `open`. */
39const WHOLE_BEFORE = /(?:[(,]|[A-Za-z_][A-Za-z0-9_]*\s*=|\b(?:unlink|rmdir|open))\s*$/
40/** What comes after a whole literal argument: `)` or `,`. */
41const WHOLE_AFTER = /^\s*[),]/
42const ARGUMENT_WINDOW = 64
43
44/** One string literal of the code: its text and source bounds; `cut` marks a template literal cut at its first `${`. */
45type Literal = { text: string; start: number; end: number; cut?: true }
46
47/** The comment syntax that the scan blanks: Python `#`, or JavaScript `//` and `/* … *\/`. */
48export type Dialect = 'python' | 'js'
49
50/** How to scan: the comment dialect of the code, and whether only definite file operations with a whole literal path count. */
51type ScanOptions = { dialect?: Dialect; definiteOnly?: boolean }
52
53/** The string literal that opens at `i`: `'…'`, `"…"`, tripled quotes, or a template literal, whose text stops at its first `${`. */
54function literalAt(code: string, i: number): Literal {
55  const quote = code.charAt(i)
56  const close = quote !== '`' && code.startsWith(quote.repeat(3), i) ? quote.repeat(3) : quote
57  let j = i + close.length
58  let text = ''
59  let isCut = false
60  while (j < code.length && !code.startsWith(close, j)) {
61    if (quote === '`' && code.startsWith('${', j)) isCut = true
62    const step = code.charAt(j) === '\\' && j + 1 < code.length ? 2 : 1
63    if (!isCut) text += code.charAt(j + step - 1)
64    j += step
65  }
66  return isCut ? { text, start: i, end: j + close.length, cut: true } : { text, start: i, end: j + close.length }
67}
68
69/** True when a string literal opens at `i`. */
70function isQuote(code: string, i: number): boolean {
71  const ch = code.charAt(i)
72  return ch === "'" || ch === '"' || ch === '`'
73}
74
75/** The string literals of the code: `'…'`, `"…"`, tripled quotes, and a template literal up to its first `${`. */
76function literals(code: string): Literal[] {
77  const found: Literal[] = []
78  let i = 0
79  while (i < code.length) {
80    if (!isQuote(code, i)) {
81      i += 1
82      continue
83    }
84    const literal = literalAt(code, i)
85    found.push(literal)
86    i = literal.end
87  }
88  return found
89}
90
91/** Where the comment of `dialect` that opens at `i`, the start of a line after its indentation, ends; undefined when none opens there. A `/*` opens a comment only when a `*\/` closes it at or before `lastClose`, the start of the last `*\/` in the code. */
92function commentEnd(code: string, i: number, dialect: Dialect, lastClose: number): number | undefined {
93  const ch = code.charAt(i)
94  if (ch !== '#' && ch !== '/') return undefined
95  const line = (): number => {
96    const end = code.indexOf('\n', i)
97    return end < 0 ? code.length : end
98  }
99  if (dialect === 'python') return ch === '#' ? line() : undefined
100  if (code.startsWith('//', i)) return line()
101  if (!code.startsWith('/*', i) || lastClose < i + 2) return undefined
102  return code.indexOf('*/', i + 2) + 2
103}
104
105/** The code with the text of each comment of `dialect` that starts a line, after any indentation, replaced by spaces, every index and line break kept; strings are skipped as `literals` reads them. A comment after code on the same line stays code. */
106function blankComments(code: string, dialect: Dialect): string {
107  const parts: string[] = []
108  const lastClose = code.lastIndexOf('*/')
109  let kept = 0
110  let i = 0
111  let isLineStart = true
112  while (i < code.length) {
113    if (isQuote(code, i)) {
114      i = literalAt(code, i).end
115      isLineStart = false
116      continue
117    }
118    const end = isLineStart ? commentEnd(code, i, dialect, lastClose) : undefined
119    if (end === undefined) {
120      const ch = code.charAt(i)
121      isLineStart = ch === '\n' || (isLineStart && (ch === ' ' || ch === '\t'))
122      i += 1
123      continue
124    }
125    parts.push(code.slice(kept, i), code.slice(i, end).replace(/[^\r\n]/g, ' '))
126    kept = end
127    i = end
128    isLineStart = false
129  }
130  parts.push(code.slice(kept))
131  return parts.join('')
132}
133
134/** The mode a literal gives `open(`: a short mode string, or the prefix of a two-argument Perl open. */
135function modeOf(literal: Literal): string | undefined {
136  return MODE.test(literal.text) ? literal.text : PERL_MODE.exec(literal.text)?.[0]
137}
138
139/** The access of a recognized operation, independent of neighboring calls. */
140function operationKind(name: string): EffectKind {
141  if (DESTROY_WORDS.some(word => name.includes(word.replace(/\($/, '')))) return 'destroy'
142  return WRITE_WORDS.some(word => name.includes(word)) ? 'write' : 'read'
143}
144
145/** True when a call or a method of this name reads, writes, or deletes a file: a name that READ_CALL or one of the word lists names. */
146function isFileOperation(name: string): boolean {
147  return READ_CALL.test(name) || operationKind(name) !== 'read'
148}
149
150/** Access shared by a file call and its path literals, including a later open mode; `bare` marks a path object that no file operation uses, such as a lone `Path(…)` or one that `.with_suffix(` changes. */
151type Access = { kind: EffectKind; glob: boolean; potential?: boolean; bare?: true }
152
153/** One literal path and the operation that accesses it. */
154type FileLiteral = { literal: Literal; access: Access; definite: boolean }
155
156/** One open call or bracket: the access of its arguments, the index and keyword of its current argument, and its kind. `named` marks a Python `shutil` copy. `twoPaths` marks `cpSync`, whose arguments after its destination, such as its options, name no file. `found` counts the literals found before it opened. `pathOpen` is the access of the path object whose `.open(` this is. `link` marks a method chained on a path object, with the access kind of the arguments of each method in that chain. */
157type CallState = { access?: Access; multiple: boolean; argument: number; keyword: string; open: boolean; constructor: boolean; copy: boolean; named: boolean; twoPaths: boolean; pair: boolean; found: number; pathOpen?: Access; link?: EffectKind }
158
159/** The keyword of the current argument of a Python `shutil` copy, or `''`. */
160function copyKeyword(call: CallState): string {
161  return call.named ? call.keyword : ''
162}
163
164/** The access of the current argument: a copy reads its source, named by `src=` in a Python `shutil` copy or else first, and gives the call's access to the rest, up to the destination of `cpSync`. */
165function argumentAccess(call: CallState): Access | undefined {
166  if (!call.copy || call.access === undefined) return call.access
167  if (call.twoPaths && call.argument > 1) return undefined
168  const keyword = copyKeyword(call)
169  return keyword === 'src' || (keyword !== 'dst' && call.argument === 0) ? { kind: 'read', glob: false } : call.access
170}
171
172/** The method that `pattern` finds at `at`, and the index of its `(`. */
173function nextMethod(code: string, at: number, pattern: RegExp): { name: string; open: number } | undefined {
174  pattern.lastIndex = 0
175  const match = pattern.exec(code.slice(at, at + METHOD_WINDOW))
176  return match === null ? undefined : { name: match[1]!, open: at + pattern.lastIndex - 1 }
177}
178
179/** The first method chained after a path object, past methods that keep the same file, such as `.resolve()`, and the index of its `(`. A file operation, such as `.unlink()`, gives its own access. A method that changes the path, such as `.with_name()`, reads, whatever follows it. So does the last method that keeps the same file when the path goes on after it, as in `.resolve().parent`. */
180function chainedOperation(code: string, at: number): { name: string; open: number } | undefined {
181  const rest = code.slice(at, at + METHOD_WINDOW)
182  let kept: { name: string; open: number } | undefined
183  let k = 0
184  for (;;) {
185    METHOD.lastIndex = k
186    const match = METHOD.exec(rest)
187    if (match === null) break
188    const method = { name: match[1]!, open: at + METHOD.lastIndex - 1 }
189    if (!SAME_FILE.has(method.name)) return method
190    let depth = 1
191    for (k = METHOD.lastIndex; k < rest.length && depth > 0; k += 1) depth += rest.charAt(k) === '(' ? 1 : rest.charAt(k) === ')' ? -1 : 0
192    if (depth > 0) return undefined
193    kept = method
194  }
195  CHAIN_END.lastIndex = k
196  return kept === undefined || CHAIN_END.test(rest) ? undefined : kept
197}
198
199/** True when the literal names a module (`require('…')`, `import('…')`, `from '…'`): code to load, not a file the code changes. */
200function isSpecifier(code: string, literal: Literal): boolean {
201  return SPECIFIER.test(code.slice(Math.max(0, literal.start - SPECIFIER_WINDOW), literal.start))
202}
203
204/** Selects literals within file operations, leaving printed text and other data out of the effects. */
205function fileLiterals(code: string): FileLiteral[] {
206  const strings = literals(code)
207  const found: FileLiteral[] = []
208  const calls: CallState[] = []
209  // The indices in `found` of the literals that hold each access object, in order.
210  const holders = new Map<Access, number[]>()
211  let pathOpen: { at: number; access: Access } | undefined
212  // The `(` of the next method in a chain on a path object, and the access kind of the arguments of that chain.
213  let chain: { at: number; kind: EffectKind } | undefined
214  let next = 0
215  let name = ''
216  for (let i = 0; i < code.length; i += 1) {
217    const literal = strings[next]
218    if (literal?.start === i) {
219      const call = calls.at(-1)
220      const bare = /(?:unlink|rmdir|open)\s*$/.exec(code.slice(Math.max(0, i - 32), i))?.[0].trim()
221      if (call?.open && call.access !== undefined) {
222        const mode = modeOf(literal)
223        if (mode !== undefined && WRITE_MODE.test(mode)) call.access.kind = 'write'
224      }
225      if (call?.pathOpen !== undefined && (call.keyword === 'mode' || (call.keyword === '' && call.argument === 0)) && MODE.test(literal.text) && WRITE_MODE.test(literal.text)) call.pathOpen.kind = 'write'
226      const access = bare === undefined ? call && argumentAccess(call) : { kind: operationKind(bare), glob: false }
227      if (access !== undefined && (bare !== undefined || call?.multiple || call?.argument === 0)) {
228        found.push({ literal, definite: !access.potential && (bare !== undefined || call?.argument === 0 || call?.pair === true && (call.argument < 2 || call.keyword === 'src' || call.keyword === 'dst') || call?.open === true && /(?:my\s+\$|FH\b)/.test(code.slice(Math.max(0, literal.start - 80), literal.start))), access })
229        const held = holders.get(access)
230        if (held === undefined) holders.set(access, [found.length - 1])
231        else held.push(found.length - 1)
232      }
233      i = literal.end - 1
234      next += 1
235      name = ''
236      continue
237    }
238    const ch = code.charAt(i)
239    if (/[A-Za-z0-9_.$]/.test(ch)) name = (name + ch).slice(-80)
240    else if (ch === '(' || ch === '[') {
241      const parent = calls.at(-1)
242      const inherited = parent !== undefined && (parent.multiple || parent.argument === 0) ? argumentAccess(parent) : undefined
243      const kind = operationKind(name)
244      const operation = READ_CALL.test(name) || /Path$/.test(name)
245        || kind !== 'read' && !/(?:write_text|write_bytes)$/.test(name)
246      const constructor = /(?:^|[^A-Za-z0-9_$])Path\s*$/.test(code.slice(Math.max(0, i - 16), i))
247      const link = chain?.at === i ? chain.kind : undefined
248      const bare = /Path$/.test(name) && !isFileOperation(name)
249      const access = constructor && inherited !== undefined ? inherited : operation ? { kind, glob: /glob$/.test(name), potential: /(?:system|subprocess|child_process|exec|spawn|popen|Deno\.(?:Command|run))/.test(name), ...(bare ? { bare } : {}) } : inherited === undefined || name === '' ? inherited : { ...inherited, kind: link ?? inherited.kind, potential: true }
250      calls.push({ access, multiple: !operation && inherited !== undefined || /(?:open|copy|cpSync|move|rename|replace|symlink|subprocess|exec|spawn|system|popen)/i.test(name), argument: 0, keyword: '', open: /open$/.test(name), constructor, copy: /(?:copy|cpSync)/.test(name), named: SHUTIL_COPY.test(name), twoPaths: /cpSync/.test(name), pair: /(?:copy|cpSync|move|rename|replace|symlink)/.test(name), found: found.length, pathOpen: pathOpen?.at === i ? pathOpen.access : undefined, link })
251      name = ''
252    } else if (ch === ')' || ch === ']') {
253      const call = calls.pop()
254      const method = call?.constructor && call.access !== undefined ? chainedOperation(code, i + 1) : undefined
255      if (call?.access !== undefined && method !== undefined) {
256        // The method decides the access of the path, apart from the call around it, which can share the access object.
257        // Each literal leaves the shared object once, so the work stays linear.
258        const own: Access = { kind: operationKind(method.name), glob: call.access.glob, ...(isFileOperation(method.name) ? {} : { bare: true as const }) }
259        const held = holders.get(call.access) ?? []
260        while (held.length > 0 && held.at(-1)! >= call.found) {
261          const k = held.pop()!
262          found[k] = { ...found[k]!, access: own }
263        }
264        if (method.name === 'open') pathOpen = { at: method.open, access: own }
265      }
266      // The arguments of each method chained on the path, such as `'.x'` in `.with_suffix('.x')`, take the kind of the method
267      // right after `Path(…)`, never the access of the call around the path. A file operation in those arguments keeps its own access.
268      const after = call?.link !== undefined ? nextMethod(code, i + 1, LINK) : call?.constructor && call.access !== undefined ? nextMethod(code, i + 1, METHOD) : undefined
269      if (after !== undefined) chain = { at: after.open, kind: call?.link ?? operationKind(after.name) }
270      name = ''
271    } else if (ch === ',') {
272      const call = calls.at(-1)
273      if (call !== undefined) {
274        call.argument += 1
275        call.keyword = ''
276      }
277      name = ''
278    } else if (ch === '=') {
279      const call = calls.at(-1)
280      if (call !== undefined && code.charAt(i + 1) !== '=' && /^[A-Za-z_][A-Za-z0-9_]*$/.test(name)) call.keyword = name
281      name = ''
282    } else if (!/\s/.test(ch)) name = ''
283  }
284  return found
285}
286
287/** True when the literal is a whole argument: after `(`, `,`, a keyword `=`, or a bare `unlink`, `rmdir`, or `open`, and before `)` or `,`. */
288function isWholeArgument(code: string, literal: Literal): boolean {
289  const before = code.slice(Math.max(0, literal.start - ARGUMENT_WINDOW), literal.start)
290  return WHOLE_BEFORE.test(before) && WHOLE_AFTER.test(code.slice(literal.end, literal.end + ARGUMENT_WINDOW))
291}
292
293/** The effects of a piece of interpreter code run in `cwd`; module names and the bare `.`, `..`, and `/` name no file. Comments of `dialect` are blanked first. With `definiteOnly`, only a whole literal argument of a definite file operation counts, and no literal of a bare path object, cut at `${`, or that folds to `/` does. */
294export function scanCode(code: string, cwd: string | undefined, options: ScanOptions = {}): { effects: Effect[]; opaque?: string } {
295  const text = options.dialect === undefined ? code : blankComments(code, options.dialect)
296  const effects: Effect[] = []
297  let opaque: string | undefined
298  for (const { literal, access, definite } of fileLiterals(text)) {
299    const { kind } = access
300    if (isSpecifier(text, literal)) continue
301    if (options.definiteOnly && (!definite || access.bare || literal.cut || !isWholeArgument(text, literal))) continue
302    const operand = literal.text.replace(PERL_MODE, '')
303    const resolved = absolute(operand, cwd)
304    if (definite && !isRemote(operand) && (resolved?.length ?? operand.length) > MAX_PATH) opaque = PATH_TOO_LONG
305    for (const word of literal.text.split(/\s+/)) {
306      const token = word.replace(PERL_MODE, '')
307      if (BARE.test(token) || !looksLikePath(token)) continue
308      const path = absolute(token, cwd)
309      if (path === undefined || path.length > MAX_PATH || (options.definiteOnly && path === '/')) continue
310      const globbed = access.glob && hasGlob(path) ? splitGlob(path) : undefined
311      effects.push({ path: globbed?.path ?? path, glob: globbed?.glob, kind, recursive: kind === 'destroy' })
312    }
313  }
314  return { effects, opaque }
315}
316
hooks/analyze/lexer.ts 600 lines
1// The shell lexer: words as parts, control operators, redirections with
2// their file descriptors, and heredoc bodies, in one left-to-right scan with
3// no backtracking regular expression. Pure: no `$`.
4
5/** One piece of a shell word; `quoted` marks an expansion inside double quotes, which is neither split nor globbed. A `param` part is a parameter operator: `-`, `=`, or `+`, each also with `:`, `#`, `##`, `%`, or `%%` with its word, or `length` for `${#name}`. The modern reading also gives an `array` part, the `( … )` of an array assignment with its elements as words; an `elements` part, `${name[@]}`, or with `joined` `${name[*]}`; and an `unknown` part that names the array of every other `${name[…]…}` form. */
6export type Part =
7  | { kind: 'text'; text: string; quoted: boolean }
8  | { kind: 'var'; name: string; quoted: boolean }
9  | { kind: 'param'; name: string; op: string; word: Word; quoted: boolean }
10  | { kind: 'sub'; command: string; quoted: boolean }
11  | { kind: 'process'; command: string; isOutput: boolean }
12  | { kind: 'array'; elements: Word[] }
13  | { kind: 'elements'; name: string; joined: boolean; quoted: boolean }
14  | { kind: 'unknown'; name?: string }
15
16/** A shell word: its parts in order. */
17export type Word = Part[]
18
19/** A redirection: its operator, the file descriptor it applies to, its target, and its heredoc body once the lexer has read it. */
20export type Redirect = { op: string; fd: number; target: Word; heredoc?: { text: string; quoted: boolean } }
21
22/** One token of a command line; `joined` marks a `(` that another `(` follows at once, as in `((`, which the shell reads as arithmetic, unlike `( (`; in the modern reading also across backslash-newlines, which the shell removes first. */
23export type Token =
24  | { type: 'word'; word: Word }
25  | { type: 'op'; op: string; joined?: true }
26  | { type: 'redirect'; redirect: Redirect }
27
28/** The tokens of a command line, or why it cannot be read; `legacy` marks a reading that ended a substitution at the `)` of a `case` pattern, as `Lexer.legacy` says, and `quoting` a text that holds `$'…'` or `$"…"`, as `Lexer.quoting` says. */
29type Lexed = { tokens: Token[]; legacy?: true; quoting?: true } | { error: string }
30
31/** The longest command Arrester reads. */
32const MAX_COMMAND = 100_000
33/** How deep command substitutions may nest inside one another before the command counts as unreadable. */
34const MAX_NESTING = 32
35export const TOO_LONG = 'the command is too long to read'
36/** Why a command that does not parse cannot be read. */
37export const UNPARSED = 'the command does not parse'
38
39const CONTROL = ['&&', '||', ';;', '|&', '&', '|', ';', '(', ')']
40/** The control operators of the modern reading, which adds the `;&` and `;;&` ends of a `case` branch. */
41const MODERN_CONTROL = ['&&', '||', ';;&', ';;', ';&', '|&', '&', '|', ';', '(', ')']
42/** The text of a word that starts an array assignment when a `(` follows it. */
43const ARRAY_NAME = /^[A-Za-z_][A-Za-z0-9_]*\+?=$/
44/** The start of a `${…}` form of an array: an optional `#` or `!`, a name, and `[`. */
45const ARRAY_FORM = /([#!]?)([A-Za-z_][A-Za-z0-9_]*)\[/y
46/** The words after which a new command starts, so a `case` or an `esac` there is a keyword. */
47const COMMAND_STARTS = new Set(['then', 'do', 'else', 'elif', 'if', 'while', 'until', '{', '!'])
48const REDIRECTS = ['&>>', '<<<', '<<-', '&>', '>>', '>|', '>&', '<&', '<>', '<<', '>', '<']
49const WORD_END = new Set([' ', '\t', '\n', ';', '&', '|', '(', ')', '<', '>'])
50const NAME = /^[A-Za-z_][A-Za-z0-9_]*/
51/** A parameter name inside `${…}`, read at a position: a name, digits, or one special character. */
52const BRACED_NAME = /[A-Za-z_][A-Za-z0-9_]*|[0-9]+|[@*#?$!-]/y
53/** The operators of a `${…}` that the analysis resolves, longest first. */
54const OPERATORS = [':-', ':=', ':+', '##', '%%', '-', '=', '+', '#', '%']
55const FD = /^[0-9]+(?=[<>])/
56const SPECIAL = new Set(['@', '*', '#', '?', '$', '!', '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'])
57const ESCAPES: Readonly<Record<string, string>> = { n: '\n', t: '\t', '\\': '\\', "'": "'", '"': '"' }
58const IN_DOUBLE_QUOTES = '"\\$`\n'
59const IN_HEREDOC = '\\$`\n'
60
61/** Thrown inside the lexer when the command does not parse; `lex` turns it into an error. */
62class Unparsed extends Error {}
63
64/** Lexes one command line, as Arrester 1.1.3 did, or with `modern` also with arrays, the `;&` and `;;&` ends of a `case` branch, and a `case` inside a substitution. */
65export function lex(command: string, modern = false): Lexed {
66  if (command.length > MAX_COMMAND) return { error: TOO_LONG }
67  const lexer = new Lexer(command, 0, 0, modern)
68  return lexer.run() ? { tokens: lexer.tokens, ...(lexer.legacy ? { legacy: true as const } : {}), ...(lexer.quoting ? { quoting: true as const } : {}) } : { error: UNPARSED }
69}
70
71/** The parts of a heredoc body whose delimiter is unquoted, which the shell expands like double-quoted text with `"` plain, and whether the reading is `legacy`, as `lex` says; undefined when an expansion in it does not parse. `modern` reads it as `lex` says. */
72export function heredocWord(text: string, modern = false): { word: Word; legacy?: true } | undefined {
73  const lexer = new Lexer(text, 0, 0, modern)
74  const word = lexer.expandable()
75  return word === undefined ? undefined : { word, ...(lexer.legacy ? { legacy: true as const } : {}) }
76}
77
78/** The source text of each `unknown` part that an arithmetic expansion or an unread `${…}` form gave, which the scan for assignments reads. */
79const SOURCES = new WeakMap<Part, string>()
80
81/** The source text of an `unknown` part, or an empty text when the lexer gave none. */
82export function unknownSource(part: Part): string {
83  return SOURCES.get(part) ?? ''
84}
85
86/** The source spelling of each expansion part, such as `$name`, `${name}`, `$(…)`, or a backtick substitution. */
87const SPELLINGS = new WeakMap<Part, string>()
88
89/** The source spelling of an expansion part, or an empty text when the lexer gave none. */
90export function spellingOf(part: Part): string {
91  return SPELLINGS.get(part) ?? ''
92}
93
94/** The text of a part: literal text as it is, expansions written as typed. */
95function partText(part: Part): string {
96  if (part.kind === 'text') return part.text
97  if (part.kind === 'var') return `$${part.name}`
98  if (part.kind === 'param') return part.op === 'length' ? `\${#${part.name}}` : `\${${part.name}${part.op}${wordText(part.word)}}`
99  if (part.kind === 'sub') return `$(${part.command})`
100  if (part.kind === 'array') return `(${part.elements.map(wordText).join(' ')})`
101  if (part.kind === 'elements') return `\${${part.name}[${part.joined ? '*' : '@'}]}`
102  return part.kind === 'process' ? `${part.isOutput ? '>' : '<'}(${part.command})` : '$(( ))'
103}
104
105/** The text of a word with its quotes removed; what a skeleton's unresolved operand uses. */
106export function wordText(word: Word): string {
107  return word.map(partText).join('')
108}
109
110/** Adds text to the word, joined to the last part when that part is text with the same quoting. */
111function pushText(parts: Word, text: string, quoted: boolean): void {
112  const last = parts[parts.length - 1]
113  if (last !== undefined && last.kind === 'text' && last.quoted === quoted) last.text += text
114  else parts.push({ kind: 'text', text, quoted })
115}
116
117/** Where a `case` inside a substitution stands: before its word, before `in`, at a pattern, or in a branch body. */
118type CaseState = 'word' | 'in' | 'pattern' | 'body'
119
120/** The text of a word that is unquoted literal text, else undefined. */
121function plainText(word: Word): string | undefined {
122  return word.every(part => part.kind === 'text' && !part.quoted) ? wordText(word) : undefined
123}
124
125/** Moves the `case` states of a substitution past the token just read, so the scan knows when a `)` ends a pattern and not the substitution: `case` at the start of a command opens one, `in` starts its patterns, `)` its branch body, `;;`, `;&`, or `;;&` the next pattern, and `esac` closes it. */
126function caseStep(cases: CaseState[], tokens: readonly Token[]): void {
127  const token = tokens[tokens.length - 1]
128  const previous = tokens[tokens.length - 2]
129  const text = token?.type === 'op' ? token.op : token?.type === 'word' ? plainText(token.word) : undefined
130  const isStart = previous === undefined || previous.type === 'op' || (previous.type === 'word' && COMMAND_STARTS.has(plainText(previous.word) ?? ''))
131  const last = cases.length - 1
132  const state = cases[last]
133  if (state === 'word') cases[last] = 'in'
134  else if (state === 'in') {
135    if (text === 'in') cases[last] = 'pattern'
136  } else if (state === 'pattern') {
137    if (text === 'esac') cases.pop()
138    else if (text === ')') cases[last] = 'body'
139  } else if (text === 'case' && isStart) cases.push('word')
140  else if (state === 'body' && (text === ';;' || text === ';&' || text === ';;&')) cases[last] = 'pattern'
141  else if (state === 'body' && text === 'esac' && isStart) cases.pop()
142}
143
144/** Whether the parts of a word so far are an unquoted `NAME=` or `NAME+=`, which a `(` turns into an array assignment. */
145function isArrayName(parts: Word): boolean {
146  const [only] = parts
147  return parts.length === 1 && only?.kind === 'text' && !only.quoted && ARRAY_NAME.test(only.text)
148}
149
150/** One scan of one command line, or of the command inside a substitution that starts at `start`. With `modern`, it also reads arrays, the `;&` and `;;&` operators, and a `case` inside a substitution; without it, it reads as Arrester 1.1.3 did. */
151class Lexer {
152  readonly tokens: Token[] = []
153  private readonly pending: { redirect: Redirect; raw: string }[] = []
154  /** The source spelling of each expansion part, which a heredoc delimiter keeps as written. */
155  private readonly spellings = new Map<Part, string>()
156  private readonly src: string
157  private readonly nesting: number
158  private readonly modern: boolean
159  private i: number
160  /** How deep the `${…}` being read nests in other ones. */
161  private braces = 0
162  /** Set when, without `modern`, a substitution here ended at the `)` of a `case` pattern, as Arrester 1.1.3 ended it, where the shell and the modern reading go on to the `esac`. */
163  legacy = false
164  /** Set when the text holds `$'…'` or `$"…"`, also in a substitution; the reading does not change. */
165  quoting = false
166
167  constructor(src: string, start: number, nesting: number, modern: boolean) {
168    this.src = src
169    this.i = start
170    this.nesting = nesting
171    this.modern = modern
172  }
173
174  /** Scans the command; false when it does not parse or a heredoc has no end. */
175  run(): boolean {
176    try {
177      this.scan(false)
178    } catch (error) {
179      if (error instanceof Unparsed) return false
180      throw error
181    }
182    return this.pending.length === 0
183  }
184
185  /** The whole source read as a heredoc body that the shell expands; undefined when it does not parse. */
186  expandable(): Word | undefined {
187    const parts: Word = []
188    try {
189      this.quoted(parts, undefined, IN_HEREDOC)
190    } catch (error) {
191      if (error instanceof Unparsed) return undefined
192      throw error
193    }
194    return parts
195  }
196
197  /** The main loop: blanks, newlines, comments, redirections, operators, and words. Inside a substitution it stops at the `)` that closes it and returns that index; in the modern reading, the `)` of a `case` pattern does not close it, and without it, such a close marks the reading `legacy`. */
198  private scan(isInner: boolean): number {
199    let depth = 0
200    const cases: CaseState[] = []
201    const tracks = isInner
202    while (this.i < this.src.length) {
203      const ch = this.src.charAt(this.i)
204      if (ch === ' ' || ch === '\t') {
205        this.i += 1
206        continue
207      }
208      if (ch === '\\' && this.src.charAt(this.i + 1) === '\n') {
209        this.i += 2
210        continue
211      }
212      if (ch === '\n') {
213        this.i += 1
214        this.tokens.push({ type: 'op', op: '\n' })
215        if (tracks) caseStep(cases, this.tokens)
216        this.heredocs()
217        continue
218      }
219      if (ch === '#') {
220        const end = this.src.indexOf('\n', this.i)
221        this.i = end < 0 ? this.src.length : end
222        continue
223      }
224      if ((ch === '<' || ch === '>') && this.src.charAt(this.i + 1) === '(') {
225        this.tokens.push({ type: 'word', word: this.word() })
226        continue
227      }
228      const fd = FD.exec(this.src.slice(this.i, this.i + 10))?.[0]
229      const at = this.i + (fd?.length ?? 0)
230      const redirect = REDIRECTS.find(op => this.src.startsWith(op, at))
231      if (redirect !== undefined) {
232        this.i = at
233        this.redirect(redirect, fd === undefined ? undefined : Number(fd))
234        continue
235      }
236      const control = (this.modern ? MODERN_CONTROL : CONTROL).find(op => this.src.startsWith(op, this.i))
237      if (control !== undefined) {
238        const isPattern = cases.at(-1) === 'pattern'
239        const inPattern = isPattern && this.modern
240        if (isInner && control === ')' && depth === 0 && !inPattern) {
241          if (this.pending.length > 0) throw new Unparsed()
242          if (isPattern) this.legacy = true
243          return this.i
244        }
245        if (control === '(' && !inPattern) depth += 1
246        else if (control === ')' && !inPattern) depth -= 1
247        this.i += control.length
248        this.tokens.push({ type: 'op', op: control, ...(control === '(' && this.src.charAt(this.afterContinuations(this.i)) === '(' ? { joined: true as const } : {}) })
249        if (tracks) caseStep(cases, this.tokens)
250        continue
251      }
252      this.tokens.push({ type: 'word', word: this.word() })
253      if (tracks) caseStep(cases, this.tokens)
254    }
255    if (isInner) throw new Unparsed()
256    return this.i
257  }
258
259  /** A redirection and its target word; `<<` and `<<-` wait for their body after the next newline. Without a number, `<` forms read descriptor 0 and the others write descriptor 1. */
260  private redirect(op: string, fd: number | undefined): void {
261    this.i += op.length
262    while (this.src.charAt(this.i) === ' ' || this.src.charAt(this.i) === '\t') this.i += 1
263    const ch = this.src.charAt(this.i)
264    const isProcess = (ch === '<' || ch === '>') && this.src.charAt(this.i + 1) === '('
265    if (this.i >= this.src.length || (WORD_END.has(ch) && !isProcess)) throw new Unparsed()
266    const start = this.i
267    const redirect: Redirect = { op, fd: fd ?? (op.startsWith('<') ? 0 : 1), target: this.word() }
268    if (op === '<<' || op === '<<-') this.pending.push({ redirect, raw: this.src.slice(start, this.i) })
269    this.tokens.push({ type: 'redirect', redirect })
270  }
271
272  /** One word, up to an unquoted blank or operator. In the modern reading, a `(` right after an unquoted `NAME=` or `NAME+=` opens an array assignment, which must end the word; in an element of an array (`inArray`), such a nested array does not parse, before the lexer reads into it. */
273  private word(inArray = false): Word {
274    const parts: Word = []
275    while (this.i < this.src.length) {
276      const ch = this.src.charAt(this.i)
277      const isProcess = (ch === '<' || ch === '>') && this.src.charAt(this.i + 1) === '('
278      if (this.modern && ch === '(' && isArrayName(parts)) {
279        if (inArray) throw new Unparsed()
280        parts.push({ kind: 'array', elements: this.array() })
281        if (this.i < this.src.length && !WORD_END.has(this.src.charAt(this.i))) throw new Unparsed()
282        break
283      }
284      if (WORD_END.has(ch) && !isProcess) break
285      const at = this.i
286      if (isProcess) parts.push({ kind: 'process', command: this.inner(this.i + 1), isOutput: ch === '>' })
287      else if (ch === "'") pushText(parts, this.single(), true)
288      else if (ch === '"') this.double(parts)
289      else if (ch === '\\') {
290        if (this.src.charAt(this.i + 1) !== '\n') pushText(parts, this.src.charAt(this.i + 1), true)
291        this.i += 2
292      } else if (ch === '$') this.dollar(parts, false)
293      else if (ch === '`') parts.push({ kind: 'sub', command: this.backtick(), quoted: false })
294      else {
295        pushText(parts, ch, false)
296        this.i += 1
297      }
298      this.spell(parts, at)
299    }
300    return parts
301  }
302
303  /** The elements of an array assignment, from the `(` at the cursor to the `)` that closes it: words with blanks, newlines, and comments between them. Another operator, a nested array, and a newline before a pending heredoc body do not parse. */
304  private array(): Word[] {
305    const elements: Word[] = []
306    this.i += 1
307    for (;;) {
308      if (this.i >= this.src.length) throw new Unparsed()
309      const ch = this.src.charAt(this.i)
310      if (ch === ')') {
311        this.i += 1
312        return elements
313      }
314      if (ch === '\n' && this.pending.length > 0) throw new Unparsed()
315      if (ch === ' ' || ch === '\t' || ch === '\n') this.i += 1
316      else if (ch === '\\' && this.src.charAt(this.i + 1) === '\n') this.i += 2
317      else if (ch === '#') {
318        const end = this.src.indexOf('\n', this.i)
319        this.i = end < 0 ? this.src.length : end
320      } else if (WORD_END.has(ch) && !((ch === '<' || ch === '>') && this.src.charAt(this.i + 1) === '(')) throw new Unparsed()
321      else elements.push(this.word(true))
322    }
323  }
324
325  /** Keeps the source spelling, from `at`, of an expansion part that the last step added, also for `spellingOf`. */
326  private spell(parts: Word, at: number): void {
327    const part = parts[parts.length - 1]
328    if (part === undefined || part.kind === 'text' || this.spellings.has(part)) return
329    this.spellings.set(part, this.src.slice(at, this.i))
330    SPELLINGS.set(part, this.src.slice(at, this.i))
331  }
332
333  /** The text of a single-quoted string. */
334  private single(): string {
335    const end = this.src.indexOf("'", this.i + 1)
336    if (end < 0) throw new Unparsed()
337    const text = this.src.slice(this.i + 1, end)
338    this.i = end + 1
339    return text
340  }
341
342  /** A double-quoted string, from its opening quote to its closing one. */
343  private double(parts: Word): void {
344    this.i += 1
345    this.quoted(parts, '"', IN_DOUBLE_QUOTES)
346    this.i += 1
347  }
348
349  /** Quoted text and expansions up to `end`, or to the end of the source when `end` is undefined; a backslash escapes only the characters in `escapable`. */
350  private quoted(parts: Word, end: string | undefined, escapable: string): void {
351    const start = parts.length
352    for (;;) {
353      if (this.i >= this.src.length) {
354        if (end === undefined) {
355          if (parts.length === start) pushText(parts, '', true)
356          return
357        }
358        throw new Unparsed()
359      }
360      const ch = this.src.charAt(this.i)
361      if (ch === end) {
362        if (parts.length === start) pushText(parts, '', true)
363        return
364      }
365      const next = this.src.charAt(this.i + 1)
366      const at = this.i
367      if (ch === '\\' && next !== '' && escapable.includes(next)) {
368        if (next !== '\n') pushText(parts, next, true)
369        this.i += 2
370      } else if (ch === '$') this.dollar(parts, true)
371      else if (ch === '`') parts.push({ kind: 'sub', command: this.backtick(), quoted: true })
372      else {
373        pushText(parts, ch, true)
374        this.i += 1
375      }
376      this.spell(parts, at)
377    }
378  }
379
380  /** The index at or after `at` past each backslash-newline, which the shell removes before it reads `((`; only in the modern reading, so the reading of 1.1.3 stays as it was. */
381  private afterContinuations(at: number): number {
382    let index = at
383    while (this.modern && this.src.startsWith('\\\n', index)) index += 2
384    return index
385  }
386
387  /** A `$` expansion: arithmetic, a command substitution, a braced or plain variable, ANSI-C text, or a translated string; in the modern reading, `$(` and `(` with only backslash-newlines between them start arithmetic too. */
388  private dollar(parts: Word, quoted: boolean): void {
389    const next = this.src.charAt(this.i + 1)
390    if (next === '(' && this.src.charAt(this.afterContinuations(this.i + 2)) === '(') {
391      const start = this.i
392      this.arithmetic(this.i + 1)
393      const part: Part = { kind: 'unknown' }
394      SOURCES.set(part, this.src.slice(start, this.i))
395      parts.push(part)
396    } else if (next === '(') parts.push({ kind: 'sub', command: this.inner(this.i + 1), quoted })
397    else if (next === '{') parts.push(this.braced(quoted))
398    else if (next === "'" && !quoted) {
399      this.quoting = true
400      pushText(parts, this.ansiC(), true)
401    } else if (next === '"' && !quoted) {
402      this.quoting = true
403      this.i += 1
404      this.double(parts)
405    } else if (SPECIAL.has(next)) {
406      this.i += 2
407      parts.push({ kind: 'var', name: next, quoted })
408    } else {
409      const name = NAME.exec(this.src.slice(this.i + 1, this.i + 257))?.[0]
410      if (name === undefined) {
411        pushText(parts, '$', quoted)
412        this.i += 1
413      } else {
414        this.i += 1 + name.length
415        parts.push({ kind: 'var', name, quoted })
416      }
417    }
418  }
419
420  /** `${…}`, read to the `}` that closes it: the parameter it names when the body is a name and nothing more, a parameter part for `${#name}` and for an operator in OPERATORS with its word, else an unknown part, which keeps its source text. In the modern reading, `${name[@]}` and `${name[*]}` are an `elements` part, and every other form with `[` after a name, also after `#` or `!`, is an unknown part that names the array. */
421  private braced(quoted: boolean): Part {
422    if (this.braces >= MAX_NESTING) throw new Unparsed()
423    this.braces += 1
424    const start = this.i
425    this.i += 2
426    ARRAY_FORM.lastIndex = this.i
427    const form = this.modern ? ARRAY_FORM.exec(this.src) : null
428    const bracket = ARRAY_FORM.lastIndex
429    const subscript = form === null ? '' : this.src.slice(bracket, bracket + 3)
430    const counted = this.src.charAt(this.i) === '#' ? this.parameterName(this.i + 1) : undefined
431    let part: Part
432    if (form !== null && form[1] === '' && (subscript === '@]}' || subscript === '*]}')) {
433      this.i = bracket + 3
434      part = { kind: 'elements', name: form[2]!, joined: subscript === '*]}', quoted }
435    } else if (form !== null) {
436      this.operand(quoted)
437      part = { kind: 'unknown', name: form[2]! }
438    } else if (counted !== undefined && this.src.charAt(this.i + 1 + counted.length) === '}') {
439      this.i += counted.length + 2
440      part = { kind: 'param', name: counted, op: 'length', word: [], quoted }
441    } else {
442      const name = this.parameterName(this.i)
443      const op = name === undefined ? undefined : OPERATORS.find(operator => this.src.startsWith(operator, this.i + name.length))
444      if (name !== undefined && this.src.charAt(this.i + name.length) === '}') {
445        this.i += name.length + 1
446        part = { kind: 'var', name, quoted }
447      } else {
448        this.i += (name?.length ?? 0) + (op?.length ?? 0)
449        const word = this.operand(quoted)
450        part = name === undefined || op === undefined ? { kind: 'unknown' } : { kind: 'param', name, op, word, quoted }
451      }
452    }
453    if (part.kind === 'unknown') SOURCES.set(part, this.src.slice(start, this.i))
454    this.braces -= 1
455    return part
456  }
457
458  /** The parameter name that starts at `at`: a name, digits, or one special character; undefined when none does. */
459  private parameterName(at: number): string | undefined {
460    BRACED_NAME.lastIndex = at
461    return BRACED_NAME.exec(this.src)?.[0]
462  }
463
464  /** The word of a parameter operator, up to the `}` that closes its `${`: quotes, escapes, and expansions as in a word, blanks and operators as text. In double quotes, a `'` is plain text and a `"` opens quoted text whose quotes are removed. */
465  private operand(quoted: boolean): Word {
466    const parts: Word = []
467    for (;;) {
468      if (this.i >= this.src.length) throw new Unparsed()
469      const ch = this.src.charAt(this.i)
470      if (ch === '}') {
471        this.i += 1
472        return parts
473      }
474      const at = this.i
475      const next = this.src.charAt(this.i + 1)
476      if (ch === "'" && !quoted) pushText(parts, this.single(), true)
477      else if (ch === '"') this.double(parts)
478      else if (ch === '\\' && next === '') throw new Unparsed()
479      else if (ch === '\\' && (!quoted || `${IN_DOUBLE_QUOTES}}`.includes(next))) {
480        if (next !== '\n') pushText(parts, next, true)
481        this.i += 2
482      } else if (ch === '$') this.dollar(parts, quoted)
483      else if (ch === '`') parts.push({ kind: 'sub', command: this.backtick(), quoted })
484      else {
485        pushText(parts, ch, quoted)
486        this.i += 1
487      }
488      this.spell(parts, at)
489    }
490  }
491
492  /** `$'…'` decoded: `\n`, `\t`, `\\`, `\'`, `\"`, `\xHH`, and `\NNN` (octal). */
493  private ansiC(): string {
494    let text = ''
495    let j = this.i + 2
496    while (j < this.src.length && this.src.charAt(j) !== "'") {
497      if (this.src.charAt(j) !== '\\') {
498        text += this.src.charAt(j)
499        j += 1
500        continue
501      }
502      const escape = this.src.slice(j + 1, j + 4)
503      const hex = /^x[0-9a-fA-F]{1,2}/.exec(escape)?.[0]
504      const octal = /^[0-7]{1,3}/.exec(escape)?.[0]
505      if (hex !== undefined) {
506        text += String.fromCharCode(parseInt(hex.slice(1), 16))
507        j += 1 + hex.length
508      } else if (octal !== undefined) {
509        text += String.fromCharCode(parseInt(octal, 8))
510        j += 1 + octal.length
511      } else {
512        const ch = this.src.charAt(j + 1)
513        text += ESCAPES[ch] ?? ch
514        j += 2
515      }
516    }
517    if (j >= this.src.length) throw new Unparsed()
518    this.i = j + 1
519    return text
520  }
521
522  /** Skips `$((…))`, nested parentheses and quotes respected. */
523  private arithmetic(open: number): void {
524    let depth = 0
525    let j = open
526    while (j < this.src.length) {
527      const ch = this.src.charAt(j)
528      if (ch === '\\') j += 2
529      else if (ch === "'" || ch === '"') j = this.closeQuote(j) + 1
530      else {
531        if (ch === '(') depth += 1
532        if (ch === ')') depth -= 1
533        if (depth === 0) {
534          this.i = j + 1
535          return
536        }
537        j += 1
538      }
539    }
540    throw new Unparsed()
541  }
542
543  /** The command inside `$(…)`, `<(…)`, or `>(…)` whose parenthesis opens at `open`, found with a lexer of its own, so quotes, comments, and heredocs inside it count. */
544  private inner(open: number): string {
545    if (this.nesting >= MAX_NESTING) throw new Unparsed()
546    const lexer = new Lexer(this.src, open + 1, this.nesting + 1, this.modern)
547    const close = lexer.scan(true)
548    if (lexer.legacy) this.legacy = true
549    if (lexer.quoting) this.quoting = true
550    this.i = close + 1
551    return this.src.slice(open + 1, close)
552  }
553
554  /** The index of the quote that closes the one at `at`. */
555  private closeQuote(at: number): number {
556    const quote = this.src.charAt(at)
557    let j = at + 1
558    while (j < this.src.length && this.src.charAt(j) !== quote) j += quote === '"' && this.src.charAt(j) === '\\' ? 2 : 1
559    if (j >= this.src.length) throw new Unparsed()
560    return j
561  }
562
563  /** The command inside backticks. */
564  private backtick(): string {
565    let text = ''
566    let j = this.i + 1
567    while (j < this.src.length && this.src.charAt(j) !== '`') {
568      const next = this.src.charAt(j + 1)
569      if (this.src.charAt(j) === '\\' && next !== '' && '`\\$'.includes(next)) {
570        text += next
571        j += 2
572      } else {
573        text += this.src.charAt(j)
574        j += 1
575      }
576    }
577    if (j >= this.src.length) throw new Unparsed()
578    this.i = j + 1
579    return text
580  }
581
582  /** Reads the bodies of the heredocs started on the line that just ended. The delimiter is the target word with its quotes removed and its expansions as written; quote removal that changes the word means some part of it is quoted. */
583  private heredocs(): void {
584    for (const { redirect, raw } of this.pending.splice(0)) {
585      const delimiter = redirect.target.map(part => (part.kind === 'text' ? part.text : this.spellings.get(part) ?? partText(part))).join('')
586      const lines: string[] = []
587      for (;;) {
588        if (this.i >= this.src.length) throw new Unparsed()
589        const end = this.src.indexOf('\n', this.i)
590        const raw = this.src.slice(this.i, end < 0 ? this.src.length : end)
591        this.i = end < 0 ? this.src.length : end + 1
592        const line = redirect.op === '<<-' ? raw.replace(/^\t+/, '') : raw
593        if (line === delimiter) break
594        lines.push(`${line}\n`)
595      }
596      redirect.heredoc = { text: lines.join(''), quoted: delimiter.replaceAll('\\\n', '') !== raw.replaceAll('\\\n', '') }
597    }
598  }
599}
600
hooks/analyze/programs.ts 246 lines
1// The program tables of the shell analysis. Pure: no `$`.
2
3/** Program classes. */
4export const LIST = new Set(['ls', 'stat', 'du', 'file', 'test', '[', '[[', 'tree', 'realpath', 'readlink', 'dirname', 'basename', 'pwd', 'which'])
5/** Readers whose words are paths, so a retry skeleton makes them absolute. */
6const PATH_READ = ['cat', 'head', 'tail', 'less', 'more', 'bat', 'wc', 'cut', 'sort', 'uniq', 'cmp', 'diff', 'xxd', 'od', 'hexdump', 'strings', 'base64', 'md5', 'md5sum', 'shasum', 'sha256sum']
7/** Readers: also `tac` and `shuf`, whose `tac -s` separator and `shuf -e` words are data, so `PATHY` leaves them out. */
8export const READ = new Set([...PATH_READ, 'tac', 'shuf'])
9export const CODE_FIRST = new Set(['awk', 'jq', 'yq', 'sed'])
10export const PATTERN_FIRST = new Set(['grep', 'egrep', 'fgrep', 'rg', 'ag', 'ugrep'])
11export const WRITE = new Set(['touch', 'mkdir', 'mkfifo'])
12export const MODE_FIRST = new Set(['chmod', 'chown', 'chgrp'])
13export const COPY = new Set(['cp', 'install', 'rsync', 'scp'])
14export const DESTROY = new Set(['rm', 'rmdir', 'unlink', 'shred', 'srm', 'trash', 'trash-put', 'truncate', 'rimraf'])
15export const ARCHIVE = new Set(['tar', 'zip', 'unzip'])
16export const SHELLS = new Set(['sh', 'bash', 'zsh', 'dash', 'ksh', 'fish'])
17export const INTERPRETER = /^(python[0-9.]*|pypy3?|node|nodejs|tsx|ts-node|bun|deno|ruby|php|perl)$/
18
19/** Programs that add no skeleton; of these only the list class adds effects. `dirname` and `basename` are in both: they only change text, so they touch no path, and their output lists their operands. */
20export const TRIVIAL = new Set(['cd', 'pushd', 'popd', 'export', 'declare', 'local', 'readonly', 'set', 'unset', 'true', 'false', ':', 'sleep', 'echo', 'printf', 'for', 'case', 'esac', 'done', 'fi', 'in', 'function', 'select', 'return', 'exit', 'shift', 'wait', 'trap', 'alias', 'hash', 'type', 'dirname', 'basename'])
21
22/** Shell keywords removed from the front of a command. */
23export const KEYWORDS = new Set(['!', 'if', 'then', 'elif', 'else', 'do', 'while', 'until', '{', '}'])
24
25/** Programs whose operands are paths, so a skeleton writes them as absolute paths. */
26export const PATHY = new Set([...LIST, ...PATH_READ, ...CODE_FIRST, ...PATTERN_FIRST, ...WRITE, ...MODE_FIRST, ...COPY, ...DESTROY, ...ARCHIVE, 'mv', 'ln', 'tee'])
27
28/** Programs that always reach below their operands. */
29export const ALWAYS_RECURSIVE = new Set(['du', 'tree', 'rg', 'ag', 'ugrep', 'srm', 'trash', 'trash-put', 'rimraf'])
30
31/** Programs that remove a symbolic link itself, not what it leads to, when they do not reach below it. */
32export const UNLINKERS = new Set(['rm', 'unlink', 'rmdir'])
33
34/** Programs that pass on the lines they read, or some of them, when they name no file. */
35export const PASS_THROUGH = new Set(['sort', 'uniq', 'head', 'tail', 'tac', 'shuf'])
36
37const GREP_RECURSIVE = { letters: 'rR', longs: ['--recursive', '--dereference-recursive'] }
38const MODE_RECURSIVE = { letters: 'R', longs: ['--recursive'] }
39
40/** The short letters and long names that make a program reach below its operands. */
41export const RECURSIVE_FLAGS: ReadonlyMap<string, { letters: string; longs: readonly string[] }> = new Map([
42  ['ls', { letters: 'R', longs: ['--recursive'] }],
43  ['grep', GREP_RECURSIVE],
44  ['egrep', GREP_RECURSIVE],
45  ['fgrep', GREP_RECURSIVE],
46  ['diff', { letters: 'r', longs: ['--recursive'] }],
47  ['rm', { letters: 'rR', longs: ['--recursive'] }],
48  ['chmod', MODE_RECURSIVE],
49  ['chown', MODE_RECURSIVE],
50  ['chgrp', MODE_RECURSIVE],
51  ['cp', { letters: 'rRa', longs: ['--recursive', '--archive'] }],
52  ['rsync', { letters: 'ra', longs: ['--recursive', '--archive'] }],
53  ['scp', { letters: 'r', longs: [] }],
54])
55
56/** The value-taking flags of a program, by how many words each takes; ATTACHED marks a flag whose value can only join its own word (`-i.bak`). */
57export type Values = Readonly<Record<string, number>>
58
59/** A flag whose value can only join its own word, as `sed -i.bak` and `perl -pi.orig` take theirs, and is empty otherwise. */
60export const ATTACHED = -1
61
62/** A wrapper that runs the rest of the line: its value-taking flags, the operands before the command, and the flags that set the folder the command runs in. */
63type Wrapper = { values: Values; operands: number; chdir: readonly string[] }
64
65/** Wrappers that run the rest of the line. */
66export const WRAPPERS: ReadonlyMap<string, Wrapper> = new Map<string, Wrapper>([
67  ['sudo', { values: { '-u': 1, '-g': 1, '-C': 1, '-D': 1, '-h': 1, '-p': 1, '-r': 1, '-t': 1, '-T': 1, '-U': 1, '--chdir': 1 }, operands: 0, chdir: ['-D', '--chdir'] }],
68  ['doas', { values: { '-u': 1, '-C': 1 }, operands: 0, chdir: [] }],
69  ['env', { values: { '-u': 1, '-C': 1, '-S': 1, '--unset': 1, '--chdir': 1, '--split-string': 1 }, operands: 0, chdir: ['-C', '--chdir'] }],
70  ['command', { values: {}, operands: 0, chdir: [] }],
71  ['builtin', { values: {}, operands: 0, chdir: [] }],
72  ['exec', { values: { '-a': 1 }, operands: 0, chdir: [] }],
73  ['nohup', { values: {}, operands: 0, chdir: [] }],
74  ['time', { values: { '-f': 1, '-o': 1 }, operands: 0, chdir: [] }],
75  ['timeout', { values: { '-s': 1, '-k': 1, '--signal': 1, '--kill-after': 1 }, operands: 1, chdir: [] }],
76  ['nice', { values: { '-n': 1, '--adjustment': 1 }, operands: 0, chdir: [] }],
77  ['ionice', { values: { '-c': 1, '-n': 1, '-p': 1, '--class': 1, '--classdata': 1 }, operands: 0, chdir: [] }],
78  ['stdbuf', { values: { '-i': 1, '-o': 1, '-e': 1 }, operands: 0, chdir: [] }],
79  ['caffeinate', { values: { '-t': 1, '-w': 1 }, operands: 0, chdir: [] }],
80  ['watch', { values: { '-n': 1, '--interval': 1 }, operands: 0, chdir: [] }],
81  ['xargs', { values: { '-n': 1, '-P': 1, '-I': 1, '-J': 1, '-L': 1, '-s': 1, '-d': 1, '-E': 1, '--eof': 1, '-a': 1, '-i': ATTACHED, '--arg-file': 1, '--max-args': 1, '--max-lines': 1, '--max-procs': 1, '--delimiter': 1 }, operands: 0, chdir: [] }],
82])
83
84/** The value-taking flags of `su` and of `runuser` without `-u`. */
85const SU_VALUES = { '-c': 1, '--command': 1, '--session-command': 1, '-s': 1, '--shell': 1, '-g': 1, '--group': 1, '-G': 1, '--supp-group': 1, '-w': 1, '--whitelist-environment': 1 }
86
87/** `runuser -u USER`, which runs the command after it directly, as `sudo` does. */
88export const RUNUSER: Wrapper = { values: { ...SU_VALUES, '-u': 1, '--user': 1 }, operands: 0, chdir: [] }
89
90/** A program that runs the command after it, behind one of its subcommands when it names any, each of one or more words split by a blank: its value-taking flags, the flags that set the folder, the flags whose value is shell code, the flags that run the command through a shell, and whether the command is a package whose program has the package's name. */
91export type Runner = { subs: readonly string[]; values: Values; chdir: readonly string[]; code: readonly string[]; shell: readonly string[]; isPackage: boolean }
92
93const PACKAGE_VALUES = { '-p': 1, '--package': 1 }
94
95/** The value-taking flags of `uv run`, `uv tool run`, and `uvx`. */
96const UV_VALUES = { '--with': 1, '-w': 1, '--with-editable': 1, '--with-requirements': 1, '--python': 1, '-p': 1, '--project': 1, '--directory': 1, '--package': 1, '--extra': 1, '--group': 1, '--env-file': 1, '--index': 1, '--default-index': 1, '--index-url': 1, '--extra-index-url': 1, '--from': 1 }
97
98/** What `pnpm exec`, `pnpm dlx`, and `pnpx` share: their value-taking flags, folder flags, and shell flags. */
99const PNPM = { values: { '-C': 1, '--dir': 1, '--filter': 1, '-F': 1, '--package': 1 }, chdir: ['-C', '--dir'], code: [], shell: ['-c', '--shell-mode'], isPackage: true }
100
101/** Package and environment runners: `uv run`, `uvx`, `npx`, `npm exec`, `pnpm exec`, `pnpm dlx`, and the like. */
102export const RUNNERS: ReadonlyMap<string, Runner> = new Map<string, Runner>([
103  ['uv', { subs: ['run', 'tool run'], values: UV_VALUES, chdir: ['--directory'], code: [], shell: [], isPackage: false }],
104  ['poetry', { subs: ['run'], values: { '-C': 1, '--directory': 1, '-P': 1, '--project': 1 }, chdir: ['-C', '--directory'], code: [], shell: [], isPackage: false }],
105  ['npx', { subs: [], values: { ...PACKAGE_VALUES, '-c': 1, '--call': 1 }, chdir: [], code: ['-c', '--call'], shell: [], isPackage: true }],
106  ['bunx', { subs: [], values: PACKAGE_VALUES, chdir: [], code: [], shell: [], isPackage: true }],
107  ['pnpm', { subs: ['exec', 'dlx'], ...PNPM }],
108  ['uvx', { subs: [], values: UV_VALUES, chdir: ['--directory'], code: [], shell: [], isPackage: true }],
109  ['pipenv', { subs: ['run'], values: {}, chdir: [], code: [], shell: [], isPackage: false }],
110  ['npm', { subs: ['exec'], values: { '--package': 1, '-c': 1, '--call': 1, '-w': 1, '--workspace': 1 }, chdir: [], code: ['-c', '--call'], shell: [], isPackage: true }],
111  ['yarn', { subs: ['dlx'], values: PACKAGE_VALUES, chdir: [], code: [], shell: [], isPackage: true }],
112  ['pnpx', { subs: [], ...PNPM }],
113])
114
115const GREP_VALUES = { '-e': 1, '-f': 1, '-m': 1, '-A': 1, '-B': 1, '-C': 1, '-d': 1, '-D': 1, '--regexp': 1, '--file': 1, '--max-count': 1, '--after-context': 1, '--before-context': 1, '--context': 1, '--include': 1, '--exclude': 1, '--exclude-dir': 1, '--label': 1 }
116const COPY_VALUES = { '-t': 1, '-S': 1, '--target-directory': 1, '--suffix': 1 }
117const SHELL_VALUES = { '-o': 1, '-O': 1, '--rcfile': 1, '--init-file': 1 }
118
119/** Flags whose value is the next word (two words for jq's `--arg NAME VALUE`), per program; a flag not listed takes no value. */
120const VALUE_FLAGS: ReadonlyMap<string, Values> = new Map<string, Values>([
121  ['grep', GREP_VALUES],
122  ['egrep', GREP_VALUES],
123  ['fgrep', GREP_VALUES],
124  ['ugrep', GREP_VALUES],
125  ['rg', { '-e': 1, '-f': 1, '-g': 1, '-t': 1, '-T': 1, '-m': 1, '-A': 1, '-B': 1, '-C': 1, '-j': 1, '-M': 1, '-E': 1, '-d': 1, '-r': 1, '--regexp': 1, '--file': 1, '--glob': 1, '--iglob': 1, '--type': 1, '--type-not': 1, '--type-add': 1, '--type-clear': 1, '--max-count': 1, '--max-depth': 1, '--sort': 1, '--sortr': 1, '--pre': 1, '--pre-glob': 1, '--dfa-size-limit': 1, '--encoding': 1, '--engine': 1, '--regex-size-limit': 1, '--threads': 1, '--ignore-file': 1, '--max-filesize': 1, '--after-context': 1, '--before-context': 1, '--context': 1, '--color': 1, '--colors': 1, '--context-separator': 1, '--field-context-separator': 1, '--field-match-separator': 1, '--hostname-bin': 1, '--hyperlink-format': 1, '--max-columns': 1, '--path-separator': 1, '--replace': 1, '--generate': 1 }],
126  ['ag', { '-G': 1, '-A': 1, '-B': 1, '-C': 1, '-m': 1, '--ignore': 1, '--depth': 1 }],
127  ['sed', { '-e': 1, '-f': 1, '-l': 1, '-i': ATTACHED, '-I': ATTACHED, '--expression': 1, '--file': 1 }],
128  ['awk', { '-f': 1, '-v': 1, '-F': 1 }],
129  ['jq', { '-f': 1, '--from-file': 1, '--arg': 2, '--argjson': 2, '--slurpfile': 2, '--rawfile': 2, '--indent': 1 }],
130  ['yq', { '-f': 1, '--from-file': 1, '-o': 1, '-p': 1, '--output-format': 1, '--input-format': 1 }],
131  ['head', { '-n': 1, '-c': 1 }],
132  ['tail', { '-n': 1, '-c': 1 }],
133  ['tac', { '-s': 1, '--separator': 1 }],
134  ['shuf', { '-n': 1, '-o': 1, '-i': 1, '--head-count': 1, '--output': 1, '--input-range': 1, '--random-source': 1 }],
135  ['cut', { '-d': 1, '-f': 1, '-c': 1, '-b': 1 }],
136  ['sort', { '-k': 1, '-t': 1, '-o': 1, '-T': 1, '-S': 1, '--output': 1 }],
137  ['uniq', { '-f': 1, '-s': 1, '-w': 1 }],
138  ['xxd', { '-l': 1, '-s': 1, '-c': 1, '-g': 1 }],
139  ['od', { '-A': 1, '-t': 1, '-j': 1, '-N': 1 }],
140  ['hexdump', { '-n': 1, '-s': 1, '-e': 1, '-f': 1 }],
141  ['strings', { '-n': 1, '-t': 1 }],
142  ['base64', { '-b': 1, '-w': 1, '-i': 1, '-o': 1, '--input': 1, '--output': 1 }],
143  ['shasum', { '-a': 1 }],
144  ['du', { '-d': 1, '-B': 1, '-t': 1, '--max-depth': 1 }],
145  ['stat', { '-f': 1, '-c': 1, '-t': 1 }],
146  ['tree', { '-L': 1, '-P': 1, '-I': 1, '-o': 1 }],
147  ['ls', { '-I': 1, '-w': 1, '-T': 1 }],
148  ['touch', { '-r': 1, '-t': 1, '-d': 1 }],
149  ['mkdir', { '-m': 1 }],
150  ['mkfifo', { '-m': 1 }],
151  ['chmod', { '--reference': 1 }],
152  ['chown', { '--reference': 1 }],
153  ['chgrp', { '--reference': 1 }],
154  ['cp', COPY_VALUES],
155  ['mv', COPY_VALUES],
156  ['ln', COPY_VALUES],
157  ['install', { ...COPY_VALUES, '-m': 1, '-o': 1, '-g': 1, '--mode': 1, '--owner': 1, '--group': 1 }],
158  ['rsync', { '-e': 1, '-f': 1, '--rsh': 1, '--exclude': 1, '--include': 1, '--filter': 1, '--files-from': 1, '--chmod': 1, '--exclude-from': 1, '--include-from': 1 }],
159  ['scp', { '-i': 1, '-P': 1, '-o': 1, '-F': 1, '-c': 1, '-l': 1, '-S': 1, '-J': 1 }],
160  ['truncate', { '-s': 1, '-r': 1, '--size': 1, '--reference': 1 }],
161  ['shred', { '-n': 1, '-s': 1, '--iterations': 1, '--size': 1 }],
162  ['tar', { '-f': 1, '-C': 1, '-T': 1, '-X': 1, '--file': 1, '--directory': 1, '--exclude': 1, '--files-from': 1 }],
163  ['zip', { '-x': 1, '-i': 1, '-b': 1, '-P': 1 }],
164  ['unzip', { '-d': 1, '-x': 1, '-P': 1 }],
165  ['curl', { '-d': 1, '--data': 1, '--data-binary': 1, '--data-urlencode': 1, '--data-raw': 1, '--data-ascii': 1, '--json': 1, '-F': 1, '--form': 1, '-T': 1, '--upload-file': 1, '-K': 1, '--config': 1, '-o': 1, '--output': 1, '--output-dir': 1, '-D': 1, '--dump-header': 1, '-c': 1, '--cookie-jar': 1, '-X': 1, '--request': 1, '-H': 1, '--header': 1, '-u': 1, '--user': 1, '-A': 1, '--user-agent': 1, '-e': 1, '--referer': 1, '-b': 1, '--cookie': 1, '-m': 1, '--max-time': 1, '-w': 1, '--write-out': 1, '-x': 1, '--proxy': 1 }],
166  ['wget', { '-O': 1, '-P': 1, '-o': 1, '-a': 1, '-U': 1, '-t': 1, '-T': 1, '-e': 1, '-i': 1, '--output-document': 1, '--directory-prefix': 1, '--output-file': 1, '--post-file': 1, '--body-file': 1, '--post-data': 1, '--body-data': 1, '--header': 1, '--input-file': 1 }],
167  ['sh', SHELL_VALUES],
168  ['bash', SHELL_VALUES],
169  ['zsh', SHELL_VALUES],
170  ['dash', SHELL_VALUES],
171  ['ksh', SHELL_VALUES],
172  ['fish', SHELL_VALUES],
173  ['python', { '-c': 1, '-m': 1, '-W': 1, '-X': 1 }],
174  ['node', { '-e': 1, '-p': 1, '-r': 1, '-C': 1, '--eval': 1, '--print': 1, '--require': 1, '--import': 1, '--loader': 1 }],
175  ['bun', { '-e': 1, '-p': 1, '--eval': 1, '--print': 1 }],
176  ['ruby', { '-e': 1, '-I': 1, '-r': 1 }],
177  ['php', { '-r': 1, '-f': 1, '-d': 1 }],
178  ['perl', { '-e': 1, '-E': 1, '-I': 1, '-i': ATTACHED, '-M': ATTACHED, '-m': ATTACHED, '-F': ATTACHED }],
179  ['su', SU_VALUES],
180  ['runuser', SU_VALUES],
181])
182
183/** The value-taking flags of git's subcommands that Arrester reads. */
184export const GIT_SUB_VALUES: ReadonlyMap<string, Values> = new Map<string, Values>([
185  ['clean', { '-e': 1, '--exclude': 1 }],
186  ['rm', { '--pathspec-from-file': 1 }],
187  ['checkout', { '-b': 1, '-B': 1, '--orphan': 1 }],
188  ['restore', { '-s': 1, '--source': 1 }],
189  ['stash', { '-m': 1, '--message': 1 }],
190])
191
192/** The flags whose value is code, per interpreter family. */
193export const CODE_FLAGS: ReadonlyMap<string, readonly string[]> = new Map([
194  ['python', ['-c']],
195  ['node', ['-e', '--eval', '-p', '--print']],
196  ['bun', ['-e', '--eval', '-p', '--print']],
197  ['ruby', ['-e']],
198  ['php', ['-r']],
199  ['perl', ['-e', '-E']],
200])
201
202/** `claude` launch flags that load other settings or turn off permission checks, hooks, or mods. */
203export const CLAUDE_OFF = new Set(['--settings', '--dangerously-skip-permissions', '--allow-dangerously-skip-permissions', '--bare', '--safe-mode'])
204
205/** `claude plugin` subcommands that turn a plugin off. */
206export const PLUGIN_OFF = new Set(['disable', 'uninstall'])
207
208/** The first words of a `claude` command line that start no session: version and help flags, and tool subcommands. */
209export const CLAUDE_TOOLS = new Set(['-v', '--version', '-h', '--help', 'mcp', 'plugin', 'plugins', 'doctor', 'update', 'upgrade', 'install', 'auth', 'setup-token'])
210
211/** `claude mcp` subcommands that start a server, or add one whose command runs later. */
212export const MCP_RUNS = new Set(['serve', 'add', 'add-json', 'add-from-claude-desktop'])
213
214const DECODERS = new Set(['uudecode', 'gunzip', 'zcat', 'bzcat', 'xzcat'])
215const HANDLED = new Set(['find', 'git', 'gh', 'curl', 'wget', 'dd', 'eval', 'source', '.', 'openssl', 'gzip', 'claude', 'su', 'runuser', ...DECODERS])
216
217/** The value-taking flags of a program; none when the table does not list it. */
218export function valuesOf(program: string): Values {
219  return VALUE_FLAGS.get(program) ?? {}
220}
221
222/** True when the flags hold one of `letters` in a short cluster (`-rf`) or one of the long names. */
223export function hasFlag(flags: readonly string[], letters: string, longs: readonly string[] = []): boolean {
224  return flags.some(flag => longs.includes(flag) || (/^-[A-Za-z]+$/.test(flag) && [...letters].some(letter => flag.includes(letter))))
225}
226
227/** True when the program decodes or unpacks text. */
228export function isDecoder(program: string, flags: readonly string[]): boolean {
229  if (program === 'base64') return hasFlag(flags, 'dD', ['--decode'])
230  if (program === 'xxd') return hasFlag(flags, 'r', ['--revert'])
231  if (program === 'openssl') return flags.includes('-d')
232  if (program === 'gzip') return hasFlag(flags, 'd', ['--decompress'])
233  return DECODERS.has(program)
234}
235
236/** The interpreter table key: Python variants share `python`; Node and TypeScript runners share `node`. */
237export function family(program: string): string {
238  if (/^(python|pypy)/.test(program)) return 'python'
239  return ['nodejs', 'tsx', 'ts-node'].includes(program) ? 'node' : program
240}
241
242/** True when Arrester reads the program itself, so a path to it is not a script run. */
243export function isKnown(program: string): boolean {
244  return [PATHY, TRIVIAL, SHELLS, HANDLED].some(set => set.has(program)) || INTERPRETER.test(program) || WRAPPERS.has(program) || RUNNERS.has(program)
245}
246