# dicklesworthstone/destructive_command_guard architecture

> Architecture diagram of the GitHub repository dicklesworthstone/destructive_command_guard, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-13). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/dicklesworthstone/destructive_command_guard
- Repository: https://github.com/dicklesworthstone/destructive_command_guard

## Overview

#### 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: [`src/evaluator.rs`](https://github.com/dicklesworthstone/destructive_command_guard/blob/main/src/evaluator.rs) (Semantic evaluator), [`src/cli.rs`](https://github.com/dicklesworthstone/destructive_command_guard/blob/main/src/cli.rs) (Command dispatch), [`src/hook.rs`](https://github.com/dicklesworthstone/destructive_command_guard/blob/main/src/hook.rs) (Hook ingestion), [`src/main.rs`](https://github.com/dicklesworthstone/destructive_command_guard/blob/main/src/main.rs) (Binary startup)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["CLI & hook boundary"]
  node_main(("Binary startup<br/>Rust binary<br/>[main.rs]"))
  node_lib["Public wiring<br/>Rust library<br/>[lib.rs]"]
  node_cli["Command dispatch<br/>CLI router<br/>[cli.rs]"]
  node_hook["Hook ingestion<br/>JSON hook adapter<br/>[hook.rs]"]
  node_agent["Agent protocols<br/>protocol profiles<br/>[agent.rs]"]
  node_mcp["MCP integration<br/>protocol boundary<br/>[mcp.rs]"]
end

subgraph group_policy["Policy & exceptions"]
  node_config["Layered configuration<br/>policy config<br/>[config.rs]"]
  node_allowlist[("Persistent allowlists<br/>exception store<br/>[allowlist.rs]")]
  node_permits[("Single-use permits<br/>pending exceptions")]
  node_rebase["Rebase recovery<br/>narrow exception policy<br/>[rebase_recovery.rs]"]
end

subgraph group_analysis["Command analysis"]
  node_normalize["Normalization<br/>shell canonicalization<br/>[normalize.rs]"]
  node_context["Execution context<br/>command classifier<br/>[context.rs]"]
  node_evaluator{{"Semantic evaluator<br/>decision engine<br/>[evaluator.rs]"}}
  node_perf["Latency budgets<br/>performance guard<br/>[perf.rs]"]
  node_heredoc["Embedded scripts<br/>script extraction<br/>[heredoc.rs]"]
  node_ast["AST matching<br/>language matcher<br/>[ast_matcher.rs]"]
  node_scan["Repository scanning<br/>source command extractor<br/>[scan.rs]"]
end

subgraph group_rules["Rules & matching"]
  node_packs["Built-in rule packs<br/>rule registry<br/>[mod.rs]"]
  node_git_pack["Git rule pack<br/>built-in rules<br/>[git.rs]"]
  node_external_packs["External YAML packs<br/>rule extension loader<br/>[external.rs]"]
  node_regex["Regex backend<br/>pattern engine<br/>[regex_engine.rs]"]
end

subgraph group_reporting["Results & distribution"]
  node_output["Decision rendering<br/>result renderer<br/>[mod.rs]"]
  node_denial["Denial presentation<br/>remediation renderer<br/>[denial.rs]"]
  node_history[("Local history<br/>session state<br/>[mod.rs]")]
  node_sarif["SARIF findings<br/>scan report format<br/>[sarif.rs]"]
  node_install_sh["Shell installer<br/>distribution installer<br/>[install.sh]"]
  node_install_ps["PowerShell installer<br/>distribution installer<br/>[install.ps1]"]
end

node_main -->|"initializes"| node_lib
node_main -->|"starts"| node_cli
node_cli -->|"hook mode"| node_hook
node_hook -->|"identifies payload"| node_agent
node_hook -.->|"supports"| node_mcp
node_cli -->|"loads policy"| node_config
node_hook -->|"extracts commands"| node_normalize
node_normalize -->|"canonical command"| node_context
node_context -->|"executable context"| node_evaluator
node_heredoc -->|"detects language"| node_ast
node_context -.->|"embedded scripts"| node_heredoc
node_ast -->|"recursive matches"| node_evaluator
node_perf -.->|"bounds stages"| node_evaluator
node_config -->|"pack and output policy"| node_evaluator
node_allowlist -->|"bypasses matched rules"| node_evaluator
node_permits -->|"single-use bypass"| node_evaluator
node_rebase -.->|"recovery exception"| node_evaluator
node_evaluator -->|"selects rules"| node_packs
node_packs -->|"includes"| node_git_pack
node_external_packs -->|"extends"| node_packs
node_packs -->|"evaluates patterns"| node_regex
node_cli -->|"scan mode"| node_scan
node_scan -->|"extracted commands"| node_evaluator
node_evaluator -->|"decision and trace"| node_output
node_agent -->|"protocol format"| node_output
node_output -->|"renders remediation"| node_denial
node_output -.->|"records optional state"| node_history
node_scan -->|"emits findings"| node_sarif
node_install_sh -.->|"installs binary"| node_main
node_install_ps -.->|"installs binary"| node_main

click node_main "https://github.com/dicklesworthstone/destructive_command_guard/blob/main/src/main.rs"
click node_lib "https://github.com/dicklesworthstone/destructive_command_guard/blob/main/src/lib.rs"
click node_cli "https://github.com/dicklesworthstone/destructive_command_guard/blob/main/src/cli.rs"
click node_hook "https://github.com/dicklesworthstone/destructive_command_guard/blob/main/src/hook.rs"
click node_agent "https://github.com/dicklesworthstone/destructive_command_guard/blob/main/src/agent.rs"
click node_mcp "https://github.com/dicklesworthstone/destructive_command_guard/blob/main/src/mcp.rs"
click node_config "https://github.com/dicklesworthstone/destructive_command_guard/blob/main/src/config.rs"
click node_allowlist "https://github.com/dicklesworthstone/destructive_command_guard/blob/main/src/allowlist.rs"
click node_permits "https://github.com/dicklesworthstone/destructive_command_guard/blob/main/src/pending_exceptions.rs"
click node_rebase "https://github.com/dicklesworthstone/destructive_command_guard/blob/main/src/rebase_recovery.rs"
click node_normalize "https://github.com/dicklesworthstone/destructive_command_guard/blob/main/src/normalize.rs"
click node_context "https://github.com/dicklesworthstone/destructive_command_guard/blob/main/src/context.rs"
click node_evaluator "https://github.com/dicklesworthstone/destructive_command_guard/blob/main/src/evaluator.rs"
click node_perf "https://github.com/dicklesworthstone/destructive_command_guard/blob/main/src/perf.rs"
click node_heredoc "https://github.com/dicklesworthstone/destructive_command_guard/blob/main/src/heredoc.rs"
click node_ast "https://github.com/dicklesworthstone/destructive_command_guard/blob/main/src/ast_matcher.rs"
click node_packs "https://github.com/dicklesworthstone/destructive_command_guard/blob/main/src/packs/mod.rs"
click node_git_pack "https://github.com/dicklesworthstone/destructive_command_guard/blob/main/src/packs/core/git.rs"
click node_external_packs "https://github.com/dicklesworthstone/destructive_command_guard/blob/main/src/packs/external.rs"
click node_regex "https://github.com/dicklesworthstone/destructive_command_guard/blob/main/src/packs/regex_engine.rs"
click node_scan "https://github.com/dicklesworthstone/destructive_command_guard/blob/main/src/scan.rs"
click node_output "https://github.com/dicklesworthstone/destructive_command_guard/blob/main/src/output/mod.rs"
click node_denial "https://github.com/dicklesworthstone/destructive_command_guard/blob/main/src/output/denial.rs"
click node_history "https://github.com/dicklesworthstone/destructive_command_guard/blob/main/src/history/mod.rs"
click node_sarif "https://github.com/dicklesworthstone/destructive_command_guard/blob/main/src/sarif.rs"
click node_install_sh "https://github.com/dicklesworthstone/destructive_command_guard/blob/main/install.sh"
click node_install_ps "https://github.com/dicklesworthstone/destructive_command_guard/blob/main/install.ps1"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_main,node_lib,node_cli,node_hook,node_agent,node_mcp toneBlue
class node_config,node_allowlist,node_permits,node_rebase toneAmber
class node_normalize,node_context,node_evaluator,node_perf,node_heredoc,node_ast,node_scan toneMint
class node_packs,node_git_pack,node_external_packs,node_regex toneRose
class node_output,node_denial,node_history,node_sarif,node_install_sh,node_install_ps toneIndigo
```

## Components

### CLI & hook boundary

- [Binary startup](https://github.com/dicklesworthstone/destructive_command_guard/blob/main/src/main.rs): Rust binary · `src/main.rs`
- [Public wiring](https://github.com/dicklesworthstone/destructive_command_guard/blob/main/src/lib.rs): Rust library · `src/lib.rs`
- [Command dispatch](https://github.com/dicklesworthstone/destructive_command_guard/blob/main/src/cli.rs): CLI router · Routes hook, evaluation, scan, install, and recovery workflows. · `src/cli.rs`
- [Hook ingestion](https://github.com/dicklesworthstone/destructive_command_guard/blob/main/src/hook.rs): JSON hook adapter · Reads stdin and emits agent-specific protocol responses. · `src/hook.rs`
- [Agent protocols](https://github.com/dicklesworthstone/destructive_command_guard/blob/main/src/agent.rs): protocol profiles · Recognizes supported agent payloads and denial formats. · `src/agent.rs`
- [MCP integration](https://github.com/dicklesworthstone/destructive_command_guard/blob/main/src/mcp.rs): protocol boundary · `src/mcp.rs`

### Policy & exceptions

- [Layered configuration](https://github.com/dicklesworthstone/destructive_command_guard/blob/main/src/config.rs): policy config · Merges system, user, project, explicit, and environment settings. · `src/config.rs`
- [Persistent allowlists](https://github.com/dicklesworthstone/destructive_command_guard/blob/main/src/allowlist.rs): exception store · `src/allowlist.rs`
- [Single-use permits](https://github.com/dicklesworthstone/destructive_command_guard/blob/main/src/pending_exceptions.rs): pending exceptions · `src/pending_exceptions.rs`
- [Rebase recovery](https://github.com/dicklesworthstone/destructive_command_guard/blob/main/src/rebase_recovery.rs): narrow exception policy · Relaxes selected Git discard rules during recovery. · `src/rebase_recovery.rs`

### Command analysis

- [Normalization](https://github.com/dicklesworthstone/destructive_command_guard/blob/main/src/normalize.rs): shell canonicalization · Normalizes paths and shell representations. · `src/normalize.rs`
- [Execution context](https://github.com/dicklesworthstone/destructive_command_guard/blob/main/src/context.rs): command classifier · Separates executable commands from data and comments. · `src/context.rs`
- [Semantic evaluator](https://github.com/dicklesworthstone/destructive_command_guard/blob/main/src/evaluator.rs): decision engine · Applies policy, safe overrides, destructive matches, confidence, and traces. · `src/evaluator.rs`
- [Latency budgets](https://github.com/dicklesworthstone/destructive_command_guard/blob/main/src/perf.rs): performance guard · Safely degrades expensive stages under hook deadlines. · `src/perf.rs`
- [Embedded scripts](https://github.com/dicklesworthstone/destructive_command_guard/blob/main/src/heredoc.rs): script extraction · Bounds heredocs and inline scripts before recursive evaluation. · `src/heredoc.rs`
- [AST matching](https://github.com/dicklesworthstone/destructive_command_guard/blob/main/src/ast_matcher.rs): language matcher · `src/ast_matcher.rs`
- [Repository scanning](https://github.com/dicklesworthstone/destructive_command_guard/blob/main/src/scan.rs): source command extractor · Routes extracted source and config commands through the evaluator. · `src/scan.rs`

### Rules & matching

- [Built-in rule packs](https://github.com/dicklesworthstone/destructive_command_guard/blob/main/src/packs/mod.rs): rule registry · Provides modular Git, filesystem, cloud, container, database, Windows, and tool rules. · `src/packs/mod.rs`
- [Git rule pack](https://github.com/dicklesworthstone/destructive_command_guard/blob/main/src/packs/core/git.rs): built-in rules · `src/packs/core/git.rs`
- [External YAML packs](https://github.com/dicklesworthstone/destructive_command_guard/blob/main/src/packs/external.rs): rule extension loader · `src/packs/external.rs`
- [Regex backend](https://github.com/dicklesworthstone/destructive_command_guard/blob/main/src/packs/regex_engine.rs): pattern engine · Uses linear or advanced matching after keyword filtering. · `src/packs/regex_engine.rs`

### Results & distribution

- [Decision rendering](https://github.com/dicklesworthstone/destructive_command_guard/blob/main/src/output/mod.rs): result renderer · Separates machine protocol output from diagnostics and structured formats. · `src/output/mod.rs`
- [Denial presentation](https://github.com/dicklesworthstone/destructive_command_guard/blob/main/src/output/denial.rs): remediation renderer · `src/output/denial.rs`
- [Local history](https://github.com/dicklesworthstone/destructive_command_guard/blob/main/src/history/mod.rs): session state · Optional local observability and workflow state. · `src/history/mod.rs`
- [SARIF findings](https://github.com/dicklesworthstone/destructive_command_guard/blob/main/src/sarif.rs): scan report format · `src/sarif.rs`
- [Shell installer](https://github.com/dicklesworthstone/destructive_command_guard/blob/main/install.sh): distribution installer · `install.sh`
- [PowerShell installer](https://github.com/dicklesworthstone/destructive_command_guard/blob/main/install.ps1): distribution installer · `install.ps1`

## Connections

- 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
