dcg · pre-execution shell hook · three real intercepts

三段真实拦截:agent 想跑 rm -rf / git reset --hard / DROP TABLE,dcg 在执行前挡住

下面三张卡片来自同一次 dcg 0.6.6 官方二进制真实运行:三条不同类别的破坏命令通过 Claude Code / Codex CLI / Cursor 的 PreToolUse Bash hook 送进 dcg,全部在执行前被拦下,agent 收到 deny 后重新规划出安全替代。core.git / core.filesystem 一装完就在拦;database.postgresql 属于 opt-in pack,DROP TABLE 之前先 dcg init 把它加进 [packs] enabledstderr 面板长度按规则变——有的规则把 EXPLANATION + Safer alternatives 一起印在里面,有的只印 Pattern / Pack / Regex;每张卡忠实分开标注 stderr、hook JSON、dcg explain 三种来源。

dcg v0.6.6 · x86_64-linux-musl · packs core.git · core.filesystem (default) · database.postgresql (opt-in) · mode deny (pre-exec) · 3 / 3 blocked before execution
CClaude Code agent hook · Bash PreToolUse core.git

Agent 试图执行

$ git reset --hard HEAD~5

dcg 面板 · stderr verbatim (EXPLANATION + Safer alternatives 直接印在面板里)

+----------------------------------------------------------------------------+
|                                    |
+----------------------------------------------------------------------------+
|  git reset --hard HEAD~5                                                   |
|  ^^^^^^^^^^^^^^^^                                                          |
|                                                                            |
|  EXPLANATION:                                                              |
|  git reset --hard discards ALL uncommitted changes in your working         |
|  directory AND staging area. This is one of the most dangerous git         |
|  commands because changes that were never committed cannot be recovered    |
|  by any means.                                                             |
|                                                                            |
|  Safer alternatives:                                                       |
|  - git reset --soft <ref>: Move HEAD but keep all changes staged           |
|  - git reset --mixed <ref>: Move HEAD, unstage changes, keep working dir   |
|  - git stash: Save changes before resetting                                |
|                                                                            |
|  Pattern: reset-hard                                                       |
|  Pack: core.git (severity: CRITICAL)                                    |
+----------------------------------------------------------------------------+
rule · core.git:reset-hard severity · CRITICAL decision · deny
XCodex CLI agent hook · Bash PreToolUse core.filesystem

Agent 试图执行

$ rm -rf ./src

dcg 面板 · stderr verbatim (此规则不打 EXPLANATION,只印 Pattern / Pack / Regex — 与其他富规则不同)

+----------------------------------------------------------------------------+
|                                    |
+----------------------------------------------------------------------------+
|  rm -rf ./src                                                              |
|     ^^^                                                                    |
|                                                                            |
|                                                                            |
|  Pattern: rm-rf-general                                                    |
|  Pack: core.filesystem (severity: HIGH)                                |
|  Regex: rm\s+-[a-zA-Z]*[rR][a-zA-Z]*f|rm\s+-[a-zA-Z]*f[a-zA-Z]*[rR]        |
+----------------------------------------------------------------------------+

Learn more:
  $ dcg explain "rm -rf ./src"
  $ dcg allowlist add core.filesystem:rm-rf-general --project

hook JSON · agent 真读到的 (stdout permissionDecisionReason, 与 stderr 无关,一定有 Reason)

Reason:      rm -rf is destructive and requires human approval.
             Explain what you want to delete and why, then ask the
             user to run the command manually.

Explanation: Matched destructive pattern core.filesystem:rm-rf-general.
             No additional explanation is available yet. See pack
             documentation for details.

Rule:        core.filesystem:rm-rf-general
Command:     rm -rf ./src

dcg explain · 独立命令 (Suggestions 段, 比 stderr 面板更完整)

Suggestions
• Preview first: List contents first with `ls -la` to verify target
• Safer alternative: Use `rm -ri` for interactive confirmation of each file
    $ rm -ri path/
• Workflow fix: Move to trash instead: `mv path ~/.local/share/Trash/`
rule · core.filesystem:rm-rf-general severity · HIGH decision · deny
RCursor agent hook · Bash PreToolUse database.postgresql · opt-in

Agent 试图执行

$ psql -c "DROP TABLE users;"

dcg 面板 · stderr verbatim (EXPLANATION + Safer alternatives 直接印在面板里)

+----------------------------------------------------------------------------+
|                                    |
+----------------------------------------------------------------------------+
|  psql -c "DROP TABLE users;"                                               |
|           ^^^^^^^^^^                                                       |
|                                                                            |
|  EXPLANATION:                                                              |
|  DROP TABLE removes the table structure and ALL data:                      |
|                                                                            |
|  - All rows are deleted                                                    |
|  - Indexes, constraints, triggers are removed                              |
|  - Foreign keys referencing this table may fail                            |
|  - CASCADE drops dependent objects too                                     |
|                                                                            |
|  IF EXISTS only prevents errors - it still drops the table!                |
|                                                                            |
|  Backup table first:                                                       |
|    pg_dump -t tablename dbname > table_backup.sql                          |
|                                                                            |
|  Pattern: drop-table                                                       |
|  Pack: database.postgresql (severity: HIGH)                            |
+----------------------------------------------------------------------------+
rule · database.postgresql:drop-table severity · HIGH decision · deny