# KarmaDue for Gemini CLI: block bad installs before run_shell_command runs

Gemini CLI runs `BeforeTool` hooks before each tool call. This one reads the shell command Gemini is about to run, asks KarmaDue about the npm and PyPI packages in it, and blocks the command (exit code 2, reason on stderr) when KarmaDue says **block** or **ask your person**. Gemini hooks can't ask a person, so an ask becomes a block with the reason, which the agent sees as a tool error.

## Setup (2 steps)

1. Install the guard files (no account, no shims):

       curl -fsSL https://karmadue.expo.app/guard/install.sh | KARMADUE_NO_SHIMS=1 sh
       curl -fsSL https://karmadue.expo.app/recipes/gemini-cli/karmadue-gemini-hook.sh -o ~/.karmadue/karmadue-gemini-hook.sh && chmod +x ~/.karmadue/karmadue-gemini-hook.sh

2. Add this to `~/.gemini/settings.json` (user) or `<repo>/.gemini/settings.json` (project):

```json
{
  "hooks": {
    "BeforeTool": [
      { "matcher": "run_shell_command", "hooks": [
        { "name": "karmadue", "type": "command", "command": "$HOME/.karmadue/karmadue-gemini-hook.sh", "timeout": 30000, "description": "KarmaDue package check" }
      ] }
    ]
  }
}
```

The hook script is 6 lines: <https://karmadue.expo.app/recipes/gemini-cli/karmadue-gemini-hook.sh>. It renames Gemini's `run_shell_command` to `Bash`, passes the hook JSON to `karmadue-guard hook claude-code` (the parser that reads every step of a chained command), and turns a block or ask into exit code 2 with the reason on stderr. Only package identifiers (for example `npm:postmark-mcp@1.0.18`) leave the machine, not the command line.

**Update 2026-11-15, 15:15 UTC (v2 script).** The first version (13:48 UTC) called `karmadue-guard check`, which reads only the first step of a command, so `echo ok && npx -y postmark-mcp@1.0.18` passed unchecked. If you installed it before 15:15 UTC, download the script again (step 1, second line).

## What happened when we ran it (2026-11-15, 15:15 UTC, v2 script)

Gemini-shaped `BeforeTool` input (`tool_name: "run_shell_command"`, `tool_input.command`) piped into the script:

| Command | Exit | What the agent would see |
|---|---|---|
| `npx -y postmark-mcp@1.0.18` | 2 (blocked) | KarmaDue blocked this command: malicious-package report MAL-2025-47604 |
| `echo ok && npx -y postmark-mcp@1.0.18` | 2 (blocked) | same block (v1 let this through) |
| `cd x; npm i postmark-mcp@1.0.18` | 2 (blocked) | same block (v1 let this through) |
| `pip install reqeusts` | 2 (blocked) | ask your person: cannot establish that reqeusts exists on PyPI |
| `npm install is-number@7.0.0` | 0 | nothing (no objection) |
| `npm i is-number@7.0.0 && rm -rf /` | 0 | nothing: the hook checks packages only and does not judge the other steps (see Limits) |
| `ls -la` | 0 | nothing |

Other cases: a tool call with no `command` field passes (exit 0); unreadable JSON passes with "NOT CHECKED: the hook input could not be read" on stderr. When KarmaDue can't be reached, the command runs with "NOT CHECKED: could not reach KarmaDue" on stderr (fail-open). Set `KARMADUE_STRICT=1` in the hook's environment to block instead.

## Limits

- **Not run inside Gemini CLI yet.** There is no Gemini CLI login on our box, so this was tested against Gemini CLI's published hook contract (https://geminicli.com/docs/hooks/reference/: `BeforeTool`, `tool_input`, exit code 2 blocks and uses stderr as the reason), not a live Gemini session. If it behaves differently in your Gemini CLI version, tell us on the forum.
- **A missing or broken guard install blocks every shell command.** If `~/.karmadue/guard/karmadue-guard.js` is missing or crashes, Gemini CLI blocks each `run_shell_command` with a Node error as the reason (tested 2026-11-15 with an empty home directory: `ls` exit 2, "Cannot find module"). Only "KarmaDue unreachable" is fail-open. Check the install with `node ~/.karmadue/guard/karmadue-guard.js check "ls"`: it should exit 0 and say no npm or PyPI package names were found. Reinstall: https://karmadue.expo.app/docs/hooks.md
- Don't point Gemini CLI at `karmadue-guard hook claude-code` (or the other hook formats) directly. They expect a different JSON shape and return nothing for Gemini's input, so nothing is checked; the script translates it.
- Checks npm and PyPI packages in every step of a chained command. It does not judge the other steps (`rm -rf /` after a clean install passes). KarmaDue's public check asks a person for chained commands since fix106, but the hooks were not changed: https://karmadue.expo.app/docs/changelog.md
- A "go ahead" is not a safety guarantee. KarmaDue is founder-operated; its checks are not independent audits.

## Next

- All hook clients and formats: https://karmadue.expo.app/docs/hooks.md
- Questions, misses, false alarms: <https://karmadue.expo.app/forum>. Shared fixes: https://karmadue.expo.app/trees.md
