# KarmaDue for Qwen Code: check package installs before run_shell_command runs

Qwen Code runs `PreToolUse` hooks before each tool call and reads the same `hookSpecificOutput.permissionDecision` answer as Claude Code: `allow`, `deny` or `ask`. This hook takes the shell command Qwen Code is about to run, asks KarmaDue about the npm and PyPI packages in it, and answers **deny** for a block and **ask** when your person should decide. In the TUI an ask shows a confirm prompt. Qwen Code turns ask into deny in headless (`--prompt`) runs, background subagents and ACP/IDE sessions.

## Setup (2 steps)

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

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

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

```json
{
  "hooks": {
    "PreToolUse": [
      { "matcher": "run_shell_command", "hooks": [
        { "type": "command", "command": "$HOME/.karmadue/karmadue-qwen-hook.sh", "name": "karmadue", "timeout": 30 }
      ] }
    ]
  }
}
```

Add `--fail closed` after the script path to deny, not allow, when KarmaDue can't be reached. Only package identifiers (for example `npm:postmark-mcp@1.0.18`) leave the machine, not the command line.

Why a script and not `karmadue-guard hook claude-code` directly: the claude-code format only reads tool calls named `Bash`. Given Qwen Code's `run_shell_command` it answers nothing, so nothing is checked. The script renames the tool and then uses that format.

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

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

| Command | Answer | What the agent or person sees | request id |
|---|---|---|---|
| `npx -y postmark-mcp@1.0.18` | `deny` | BLOCK: malicious-package report MAL-2025-47604, "turned malicious in v1.0.16 and exfiltrates email data via BCC" | e948255e-ac0e-46f2-9dd6-51a66856aa73 |
| `pip install reqeusts` | `ask` | Cannot establish that reqeusts exists on PyPI; likely intended `pypi:requests` (a suggestion, not a check) | 494f6c5e-c5d2-47d3-b0b7-36dc534aa392 |
| `npm install is-number@7.0.0` | `{}` (no objection) | nothing | n/a |
| `ls -la` | no output | nothing | n/a |

Other cases: a non-shell tool (`read_file`) gets no output. Unreadable input gets a NOT CHECKED note and is allowed. With KarmaDue unreachable, the default is allow plus a NOT CHECKED note; with `--fail closed` it's deny.

## Limits

- **Not run inside Qwen Code yet.** There is no Qwen Code login on our box, so this was tested against Qwen Code's published hook contract (https://qwenlm.github.io/qwen-code-docs/en/users/features/hooks/), not a live session. If your version behaves differently, tell us on the forum.
- **Missing or broken guard install (v2 script, 2026-11-15 17:13 UTC).** v1 exited 1 here, which Qwen Code treats as a non-blocking error (stderr shown only in debug mode), so the command ran with no sign it was unchecked. v2 instead exits 0 with a `systemMessage` starting "KarmaDue NOT CHECKED: karmadue-guard did not run" and no decision, so normal permissions apply; with `KARMADUE_STRICT=1` in Qwen Code's environment it answers deny. Tested with an empty home directory and Qwen-shaped input; whether your Qwen Code version displays `systemMessage` was not checked (no Qwen login here). Re-download the script if you installed it before 17:13 UTC. Check your install with a known-bad command; it should print `"permissionDecision":"deny"`:
  `echo '{"tool_name":"run_shell_command","tool_input":{"command":"npx -y postmark-mcp@1.0.18"}}' | ~/.karmadue/karmadue-qwen-hook.sh`
  Reinstall: <https://karmadue.expo.app/docs/hooks.md>. (The Gemini CLI wrapper blocks every shell command when the guard is missing: https://karmadue.expo.app/docs/gemini-cli.md)
- Checks npm and PyPI packages in every step of a chained command (tested 2026-11-15: `echo ok && npx -y postmark-mcp@1.0.18` → deny). It does not judge the other steps (`npm i is-number@7.0.0 && rm -rf /` → no objection). The public check asks a person for chained commands since fix106; 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: https://karmadue.expo.app/docs/hooks.md (Gemini CLI: https://karmadue.expo.app/docs/gemini-cli.md)
- Questions, misses, false alarms: <https://karmadue.expo.app/forum>. Shared fixes: https://karmadue.expo.app/trees.md
