How dicklesworthstone/destructive_command_guard works
Purpose and Entrypoints
dcg is a Rust CLI/hook that intercepts AI-agent shell commands, classifies destructive operations, and returns agent-specific deny protocols before execution. It also provides direct evaluation, repository scanning, installation, configuration, and recovery workflows. Binary startup is src/main.rs; shared public wiring is src/lib.rs; command parsing/dispatch is centered in src/cli.rs.
Hook Ingestion and Agent Protocol Boundary
Hook mode reads JSON from stdin, recognizes supported agent payload shapes, extracts shell commands, and emits protocol-specific stdout while reserving stderr for human output. This boundary supports Claude-compatible hooks, Codex, Gemini, Copilot, Cursor, Hermes, Grok, and Antigravity. Core protocol and invocation handling live in:
• src/hook.rs
• src/agent.rs
• src/mcp.rs
Malformed input and deadline behavior are policy-sensitive: default behavior is fail-open, with an opt-in fail-closed mode for invalid hook payloads.
Policy Resolution and Exceptions
Before evaluation, layered configuration determines enabled packs, output behavior, agent profiles, scan settings, and heredoc limits. Configuration merges system, user, project, explicit-file, and environment sources. Persistent rule/command allowlists and short-lived single-use permits can bypass otherwise matched rules; rebase recovery narrowly relaxes selected Git discard rules.
• src/config.rs
• src/allowlist.rs
• src/pending_exceptions.rs
• src/rebase_recovery.rs
Command Analysis Pipeline
The principal control flow is: parse/extract command → normalize paths and shell representations → classify executable versus data/comment context → fast keyword rejection → evaluate safe patterns before destructive patterns → produce an allow/deny result and trace. Time budgets protect hook latency and cause expensive stages to degrade safely.
• src/normalize.rs
• src/context.rs
• src/evaluator.rs
• src/perf.rs
The evaluator is the semantic decision boundary: it applies allowlists, pack selection, safe overrides, destructive matches, confidence, and contextual evaluation results.
Rule Packs and Regex Backend
Rule coverage is modularized into built-in packs for Git/filesystem, infrastructure, cloud, containers, databases, Windows, and many external tools. Packs define IDs, keyword prefilters, safe/destructive patterns, reasons, and suggestions. Packs can be lazily selected by configuration and extended from external YAML packs.
• src/packs/mod.rs
• src/packs/core/git.rs
• src/packs/external.rs
• src/packs/regex_engine.rs
The regex layer selects linear regex where possible and advanced matching where required, while per-pack keyword filtering avoids evaluating irrelevant pattern sets.
Embedded Script and Repository Scanning
Commands containing heredocs, here-strings, or inline scripts enter a bounded secondary pipeline: trigger detection, extraction, language detection, AST-aware matching, and recursive shell evaluation. Repository scan mode instead extracts executable contexts from supported source/config formats and routes extracted commands through the same evaluator, producing text/JSON/Markdown/SARIF findings.
• src/heredoc.rs
• src/ast_matcher.rs
• src/scan.rs
• src/sarif.rs
Results, State, and Distribution
Decision rendering separates machine-facing protocol output from console diagnostics, explanations, remediation suggestions, and robot/structured formats. Optional local history/session state supports observability and user workflows. The runtime is a native Rust nightly binary; it uses local filesystem config/state and Git inspection, not a server. Distribution is through GitHub Releases, shell/PowerShell installers, checksums, and optional Sigstore verification.
• src/output/mod.rs
• src/output/denial.rs
• src/history/mod.rs
• install.sh
• install.ps1
Main entry points: evaluator.rs Semantic evaluator · cli.rs Command dispatch · hook.rs Hook ingestion · main.rs Binary startup
Components 27
CLI & hook boundary
- Binary startup
src/main.rs - Public wiring
src/lib.rs - Command dispatch · Routes hook, evaluation, scan, install, and recovery workflows.
src/cli.rs - Hook ingestion · Reads stdin and emits agent-specific protocol responses.
src/hook.rs - Agent protocols · Recognizes supported agent payloads and denial formats.
src/agent.rs - MCP integration
src/mcp.rs
Policy & exceptions
- Layered configuration · Merges system, user, project, explicit, and environment settings.
src/config.rs - Persistent allowlists
src/allowlist.rs - Single-use permits
src/pending_exceptions.rs - Rebase recovery · Relaxes selected Git discard rules during recovery.
src/rebase_recovery.rs
Command analysis
- Normalization · Normalizes paths and shell representations.
src/normalize.rs - Execution context · Separates executable commands from data and comments.
src/context.rs - Semantic evaluator · Applies policy, safe overrides, destructive matches, confidence, and traces.
src/evaluator.rs - Latency budgets · Safely degrades expensive stages under hook deadlines.
src/perf.rs - Embedded scripts · Bounds heredocs and inline scripts before recursive evaluation.
src/heredoc.rs - AST matching
src/ast_matcher.rs - Repository scanning · Routes extracted source and config commands through the evaluator.
src/scan.rs
Rules & matching
- Built-in rule packs · Provides modular Git, filesystem, cloud, container, database, Windows, and tool rules.
src/packs/mod.rs - Git rule pack
src/packs/core/git.rs - External YAML packs
src/packs/external.rs - Regex backend · Uses linear or advanced matching after keyword filtering.
src/packs/regex_engine.rs
Results & distribution
- Decision rendering · Separates machine protocol output from diagnostics and structured formats.
src/output/mod.rs - Denial presentation
src/output/denial.rs - Local history · Optional local observability and workflow state.
src/history/mod.rs - SARIF findings
src/sarif.rs - Shell installer
install.sh - PowerShell installer
install.ps1
Connections 30
- Binary startup → Public wiring: initializes
- Binary startup → Command dispatch: starts
- Command dispatch → Hook ingestion: hook mode
- Hook ingestion → Agent protocols: identifies payload
- Hook ingestion → MCP integration: supports
- Command dispatch → Layered configuration: loads policy
- Hook ingestion → Normalization: extracts commands
- Normalization → Execution context: canonical command
- Execution context → Semantic evaluator: executable context
- Embedded scripts → AST matching: detects language
- Execution context → Embedded scripts: embedded scripts
- AST matching → Semantic evaluator: recursive matches
- Latency budgets → Semantic evaluator: bounds stages
- Layered configuration → Semantic evaluator: pack and output policy
- Persistent allowlists → Semantic evaluator: bypasses matched rules
- Single-use permits → Semantic evaluator: single-use bypass
- Rebase recovery → Semantic evaluator: recovery exception
- Semantic evaluator → Built-in rule packs: selects rules
- Built-in rule packs → Git rule pack: includes
- External YAML packs → Built-in rule packs: extends
- Built-in rule packs → Regex backend: evaluates patterns
- Command dispatch → Repository scanning: scan mode
- Repository scanning → Semantic evaluator: extracted commands
- Semantic evaluator → Decision rendering: decision and trace
- Agent protocols → Decision rendering: protocol format
- Decision rendering → Denial presentation: renders remediation
- Decision rendering → Local history: records optional state
- Repository scanning → SARIF findings: emits findings
- Shell installer → Binary startup: installs binary
- PowerShell installer → Binary startup: installs binary