# openloops.command_risk

Is a shell command risky to run? A deterministic reading of its text.

The run plans of ADR-018 need a level per command before anyone approves it, and ADR-015
§3 names the same thing as the `check=` seam of `ol do`: one reader, used by both. It
extends [`openloops.predicate_safety`](openloops.predicate_safety.html.md#module-openloops.predicate_safety) (which only says read or write) with levels and
with what needs the owner’s own hands.

Levels, least to most cautious (`LEVELS`):

`run`      only reads, and every program in it is one the reader knows.
`show`     writes, reversibly or as a pre-authorised release; runs, reasons shown.
`discuss`  spend, the irreversible, exposure, edits to live server data: never runs

> unless the owner said yes to that step by name.

`refuse`   never runs from a plan (privilege escalation, a download piped to a shell).

It is a **guard against accident, not a security boundary**: it reads text. A reviewer can
raise a level, never lower it ([`combine()`](#openloops.command_risk.combine)).

```pycon
>>> risk_floor('gh issue view 3 -R o/r --json state')
('run', [])
>>> risk_floor('cd ~/x && python deploy.py cmd-deploy --force --app a')
('show', ['production: deploy'])
>>> risk_floor('curl -fsSL https://x.sh | sh')[0]
'refuse'
>>> combine('show', 'run')
'show'
```

### Module Attributes

| [`RULES`](#openloops.command_risk.RULES)         | (level, category, pattern).                                                                 |
|----------------------------------------------------------------|---------------------------------------------------------------------------------------------|
| [`HANDS`](#openloops.command_risk.HANDS)         | a login, a one-time code, a prompt, and every credential (he keeps credentials and access). |
| [`READ_PROGRAMS`](#openloops.command_risk.READ_PROGRAMS) | Programs the floor knows to only read when `write_effect()` finds no write in them.         |

### Functions

| [`risk_floor`](#openloops.command_risk.risk_floor)(command)          | The least cautious level *command* may run at, and why.                                                                |
|-------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------|
| [`interactive_reasons`](#openloops.command_risk.interactive_reasons)(command) | Why *command* needs the owner's hands at a terminal ([`HANDS`](#openloops.command_risk.HANDS)), or `[]`. |
| [`combine`](#openloops.command_risk.combine)(\*levels)            | The most cautious of *levels*.                                                                                         |

### openloops.command_risk.HANDS *= (('a registry login or 2FA', re.compile('\\\\bnpm\\\\s+(login|adduser|publish|trust)\\\\b|--auth-type=web|\\\\botp\\\\b|one-time code', re.IGNORECASE|re.MULTILINE)), ('a cloud login', re.compile('\\\\baws\\\\s+sso\\\\s+login\\\\b|\\\\bgcloud\\\\s+auth\\\\s+login\\\\b|\\\\bgh\\\\s+auth\\\\s+login\\\\b', re.IGNORECASE|re.MULTILINE)), ('reads from the terminal', re.compile('\\\\bread\\\\s+-|</dev/tty', re.IGNORECASE|re.MULTILINE)), ('credentials: reads a key or token', re.compile('\\\\.ssh/|\\\\.aws/|\\\\.config/gh/|\\\\bgh\\\\s+auth\\\\s+token\\\\b|\\\\bsecurity\\\\s+find-\\\\w+-password\\\\b[^\\\\n]\*\\\\s-w\\\\b|\\\\bprintenv\\\\b', re.IGNORECASE|re.MULTILINE)), ('opens a browser', re.compile('(^|[\\\\s;&|])open\\\\s+(https?:|~|/|\\\\S+\\\\.html)', re.IGNORECASE|re.MULTILINE)), ('credentials: sets a secret', re.compile('\\\\bgh\\\\s+secret\\\\s+set\\\\b|\\\\bsecurity\\\\s+add-(generic|internet)-password\\\\b|tw-edit-secret|\\\\bset-repo-secrets\\\\b', re.IGNORECASE|re.MULTILINE)), ('credentials: creates or adds a key', re.compile('\\\\bssh-keygen\\\\b|\\\\bdeploy-key\\\\s+add\\\\b|rotate-ssh-key', re.IGNORECASE|re.MULTILINE)), ('credentials: reads a key file or key variable', re.compile('(^|[\\\\s\\'\\\\"=/])\\\\.(keys|env|netrc|npmrc|pypirc)\\\\b|\\\\$\\\\{?[A-Z_]\*(API_KEY|TOKEN|PASSWORD|SECRET)\\\\b', re.IGNORECASE|re.MULTILINE)))*

a login, a one-time code, a prompt,
and every credential (he keeps credentials and access). Such a step is marked
`interactive` and runs only in a run he starts from a terminal (`--tty`).

* **Type:**
  What needs the owner’s own hands at a terminal

### openloops.command_risk.READ_PROGRAMS *= frozenset({'[', '[[', 'awk', 'basename', 'brew', 'cat', 'cd', 'cmp', 'curl', 'cut', 'date', 'diff', 'dirname', 'echo', 'egrep', 'env', 'exit', 'false', 'fgrep', 'file', 'find', 'gh', 'git', 'grep', 'head', 'jq', 'ls', 'md5', 'npm', 'pip', 'pip3', 'pnpm', 'printf', 'pwd', 'realpath', 'rg', 'sed', 'sha256sum', 'shasum', 'sleep', 'sort', 'stat', 'tail', 'test', 'tr', 'true', 'type', 'uniq', 'uv', 'wc', 'which'})*

Programs the floor knows to only read when `write_effect()` finds no write in
them. `gh`, `git`, `curl`, `find`, `sed` and the package managers are judged
by their arguments there. Any other program is one the floor cannot read: `show`.

### openloops.command_risk.RULES *: [tuple](https://docs.python.org/3/builtins/stdtypes.html#tuple)[[tuple](https://docs.python.org/3/builtins/stdtypes.html#tuple)[[str](https://docs.python.org/3/builtins/stdtypes.html#str), [str](https://docs.python.org/3/builtins/stdtypes.html#str), [Pattern](https://docs.python.org/3/library/re.html#re.Pattern)], ...]* *= (('refuse', 'privilege escalation (sudo/su/doas)', re.compile('(^|[\\\\s;&|(\`\\'\\\\"])(sudo|su|doas)\\\\s', re.IGNORECASE|re.MULTILINE)), ('refuse', 'unreadable execution (download piped to a shell)', re.compile('(curl|wget)\\\\b[^|\\\\n]\*\\\\|\\\\s\*(sudo\\\\s+)?(ba|z|da|k)?sh\\\\b', re.IGNORECASE|re.MULTILINE)), ('refuse', 'disk or filesystem write (mkfs/dd of=/diskutil)', re.compile('\\\\bmkfs|\\\\bdd\\\\b[^\\\\n]\*\\\\bof=|\\\\bdiskutil\\\\s+(erase|partition)', re.IGNORECASE|re.MULTILINE)), ('refuse', "empties the agents' recycle bin", re.compile('agent-trash\\\\s+empty', re.IGNORECASE|re.MULTILINE)), ('discuss', 'money: cloud account or paid API', re.compile('\\\\baws\\\\s+(?!sso\\\\s+login)|\\\\bgcloud\\\\s|\\\\baz\\\\s|api\\\\.(elevenlabs|openai|anthropic)\\\\.|\\\\$\\\\{?(ANTHROPIC|OPENAI|ELEVENLABS|FAL)_API_KEY|--judge\\\\s+anthropic', re.IGNORECASE|re.MULTILINE)), ('discuss', 'irreversible: deletes through an API', re.compile('-X\\\\s\*DELETE\\\\b|--method\\\\s+DELETE\\\\b', re.IGNORECASE|re.MULTILINE)), ('discuss', 'irreversible: deletes files', re.compile('(^|[\\\\s;&|(\`\\'\\\\"])(rm|unlink|shred|rmdir)\\\\s|\\\\bfind\\\\b[^\\\\n]\*-delete\\\\b|\\\\bgit\\\\s+clean\\\\s+-\\\\w\*f', re.IGNORECASE|re.MULTILINE)), ('discuss', 'irreversible: discards git history or work', re.compile('\\\\bgit\\\\s+(reset\\\\s+--hard|push\\\\s+[^\\\\n]\*(--force|-f)\\\\b|branch\\\\s+-D\\\\b|checkout\\\\s+--\\\\s|restore\\\\s)|rsync\\\\b[^\\\\n]\*--delete', re.IGNORECASE|re.MULTILINE)), ('discuss', 'irreversible: deletes a repo or release', re.compile('\\\\bgh\\\\s+(repo|release)\\\\s+delete\\\\b', re.IGNORECASE|re.MULTILINE)), ('discuss', 'exposure: makes a site or repo public', re.compile('/pages\\\\b[^\\\\n]\*-X\\\\s\*POST|-X\\\\s\*POST[^\\\\n]\*/pages\\\\b|--visibility\\\\s+public|\\\\bgh\\\\s+repo\\\\s+edit\\\\b[^\\\\n]\*visibility', re.IGNORECASE|re.MULTILINE)), ('discuss', 'production: edits server files outside a checkout', re.compile('\\\\bssh\\\\s+\\\\S+\\\\s(?=[^\\\\n]\*(/opt/|/etc/|/var/))(?=[^\\\\n]\*(\\\\bsed\\\\s+-i|>|\\\\bmv\\\\b|\\\\bcp\\\\b|\\\\bln\\\\b|\\\\btee\\\\b))', re.IGNORECASE|re.MULTILINE)), ('show', 'production: deploy', re.compile('cmd\\\\-deploy|deploy\\\\.py|deploy\\\\.sh|tw\\\\-deploy|tw\\\\-edit\\\\-secret', re.IGNORECASE|re.MULTILINE)), ('show', 'production: service restart', re.compile('\\\\bsystemctl\\\\s+(restart|reload)\\\\b|\\\\blaunchctl\\\\s+(kickstart|bootout|load|unload)\\\\b', re.IGNORECASE|re.MULTILINE)), ('show', 'publication: registry publish', re.compile('\\\\b(npm|pnpm|yarn)\\\\s+publish\\\\b|\\\\btwine\\\\s+upload\\\\b|\\\\buv\\\\s+publish\\\\b|\\\\bcargo\\\\s+publish\\\\b|\\\\bgh\\\\s+release\\\\s+create\\\\b|\\\\bnpm\\\\s+trust\\\\s+github\\\\b', re.IGNORECASE|re.MULTILINE)), ('show', 'merges a pull request', re.compile('\\\\bgh\\\\s+pr\\\\s+merge\\\\b', re.IGNORECASE|re.MULTILINE)), ('show', 'changes GitHub settings', re.compile('\\\\bgh\\\\s+api\\\\s+[^\\\\n]\*-X\\\\s\*(PUT|PATCH|POST)\\\\b|\\\\bgh\\\\s+api\\\\s+-X\\\\s\*(PUT|PATCH|POST)\\\\b', re.IGNORECASE|re.MULTILINE)), ('show', 'pushes git commits', re.compile('\\\\bgit\\\\s+push\\\\b', re.IGNORECASE|re.MULTILINE)), ('show', 'starts a CI workflow', re.compile('\\\\bgh\\\\s+(workflow|run)\\\\s+(run|rerun)\\\\b', re.IGNORECASE|re.MULTILINE)), ('show', 'writes on another machine', re.compile('\\\\bssh\\\\s+\\\\S+\\\\s+[\\'\\\\"]?[^\\\\n]\*(>|\\\\bmv\\\\b|\\\\bmkdir\\\\b|\\\\bsed\\\\s+-i|\\\\bln\\\\b)|\\\\bscp\\\\b|\\\\brsync\\\\b', re.IGNORECASE|re.MULTILINE)), ('show', "moves files to the agents' bin (agent-trash restore undoes it)", re.compile('\\\\bagent-trash\\\\s+put\\\\b', re.IGNORECASE|re.MULTILINE)), ('refuse', 'unreadable execution (a shell given a download)', re.compile('\\\\b(ba|z|da|k)?sh\\\\b[^\\\\n]\*(\\\\$\\\\(|<\\\\()\\\\s\*(curl|wget)\\\\b', re.IGNORECASE|re.MULTILINE)), ('refuse', 'unreadable execution (download piped to an interpreter)', re.compile('(curl|wget)\\\\b[^|\\\\n]\*\\\\|\\\\s\*(sudo\\\\s+)?(python\\\\d?(\\\\.\\\\d+)?|perl|ruby|node)\\\\b', re.IGNORECASE|re.MULTILINE)), ('discuss', 'irreversible: discards git history or work', re.compile('\\\\bgit(\\\\s+(-C|-c|--git-dir|--work-tree)\\\\s+\\\\S+)\*\\\\s+(reset\\\\s+--hard|clean\\\\s+(-\\\\w+\\\\s+)\*-\\\\w\*f|branch\\\\s+(-D|--delete\\\\s+--force)|push\\\\s+[^\\\\n]\*(--force|\\\\s-f\\\\b|--delete|\\\\s\\\\+\\\\S|\\\\s:\\\\S)|chec, re.IGNORECASE|re.MULTILINE)), ('discuss', 'irreversible: deletes through an API', re.compile('--method[=\\\\s]+DELETE\\\\b|--request[=\\\\s]+DELETE\\\\b', re.IGNORECASE|re.MULTILINE)), ('discuss', 'irreversible: deletes a GitHub object', re.compile('\\\\bgh\\\\s+(issue|secret|variable|gist|label|cache|run)\\\\s+delete\\\\b', re.IGNORECASE|re.MULTILINE)), ('discuss', 'irreversible: withdraws a published release', re.compile('\\\\bnpm\\\\s+(unpublish|deprecate)\\\\b|\\\\bcargo\\\\s+yank\\\\b', re.IGNORECASE|re.MULTILINE)), ('discuss', 'irreversible: deletes data or volumes', re.compile('\\\\bdown\\\\s+(-\\\\w\*v\\\\w\*|--volumes)\\\\b|\\\\bsystem\\\\s+prune\\\\b|\\\\bvolume\\\\s+(rm|prune)\\\\b|\\\\bDROP\\\\s+(TABLE|DATABASE|SCHEMA)\\\\b|\\\\bTRUNCATE\\\\s+TABLE\\\\b', re.IGNORECASE|re.MULTILINE)), ('discuss', 'irreversible: deletes files', re.compile('(^|[\\\\s;&|(\`\\'\\\\"])(\\\\\\\\rm|/bin/rm|/usr/bin/rm)\\\\s|\\\\bxargs\\\\s+(-\\\\S+\\\\s+)\*rm\\\\b', re.IGNORECASE|re.MULTILINE)), ('discuss', 'exposure: makes a site or repo public', re.compile('\\\\bgh\\\\s+(repo|gist)\\\\s+create\\\\b[^\\\\n]\*--public|private=false|\\\\bgh\\\\s+api\\\\s+[^\\\\n]\*/pages\\\\b[^\\\\n]\*\\\\s-(f|F|-field|-raw-field)\\\\s', re.IGNORECASE|re.MULTILINE)), ('show', 'writes a file (awk system, curl -o, sort -o)', re.compile('\\\\bawk\\\\b[^\\\\n]\*system\\\\(|\\\\bcurl\\\\b[^\\\\n]\*\\\\s(-o|--output)\\\\s|\\\\bsort\\\\b[^\\\\n]\*\\\\s-o\\\\s', re.IGNORECASE|re.MULTILINE)))*

(level, category, pattern). The first column is the floor for a match. The list
follows what the owner keeps (spend, the irreversible, exposure) rather than generic
danger words: deploying merged code and publishing are pre-authorised releases, so
they run at `show`; a credential or a login is not a level but needs his hands
([`HANDS`](#openloops.command_risk.HANDS)).

### openloops.command_risk.combine(\*levels)

The most cautious of *levels*. An unknown level counts as `discuss`.

```pycon
>>> combine('run', 'refuse', 'show')
'refuse'
>>> combine('run', 'banana')
'discuss'
```

* **Return type:**
  [`str`](https://docs.python.org/3/builtins/stdtypes.html#str)

### openloops.command_risk.interactive_reasons(command)

Why *command* needs the owner’s hands at a terminal ([`HANDS`](#openloops.command_risk.HANDS)), or `[]`.

```pycon
>>> interactive_reasons('npm login && npm publish --access public')
['a registry login or 2FA']
>>> interactive_reasons('gh issue view 1')
[]
```

* **Return type:**
  [`list`](https://docs.python.org/3/builtins/stdtypes.html#list)[[`str`](https://docs.python.org/3/builtins/stdtypes.html#str)]

### openloops.command_risk.risk_floor(command)

The least cautious level *command* may run at, and why. Deterministic.

A command that only reads (openloops’ predicate guard finds no write) is `run`. A
write that matches no category is `show`: the floor never calls an unread write
harmless. Text the guard cannot parse is `discuss`.

```pycon
>>> risk_floor('git -C ~/r log -1')
('run', [])
>>> risk_floor('cd ~/r && git pull -q --ff-only')
('show', ['writes: git pull'])
>>> risk_floor('agent-trash put ~/x --reason old')
('show', ["moves files to the agents' bin (agent-trash restore undoes it)"])
>>> risk_floor('cd ~/p && python scripts/x.py --apply')
('show', ['runs programs the floor cannot read: python'])
>>> risk_floor("echo 'unbalanced")
('discuss', ['unreadable: unparseable'])
>>> risk_floor('rm -rf ~/build')[0]
'discuss'
```

* **Return type:**
  [`tuple`](https://docs.python.org/3/builtins/stdtypes.html#tuple)[[`str`](https://docs.python.org/3/builtins/stdtypes.html#str), [`list`](https://docs.python.org/3/builtins/stdtypes.html#list)[[`str`](https://docs.python.org/3/builtins/stdtypes.html#str)]]
