|
| 1 | +#!/usr/bin/env bash |
| 2 | +# GENERATED by scripts/compiler/compile.sh — DO NOT EDIT BY HAND. |
| 3 | +# Source: behaviors/no-destructive-git/behavior.yaml |
| 4 | +# Behavior: no-destructive-git (core) |
| 5 | +# Event: PreToolUse |
| 6 | +# Matcher: Bash |
| 7 | +# Action: evaluate |
| 8 | +# Trigger: index 0 |
| 9 | +set -u |
| 10 | + |
| 11 | +if [ -n "${FORGE_LIB_PATH:-}" ]; then |
| 12 | + # shellcheck source=/dev/null |
| 13 | + . "${FORGE_LIB_PATH}" |
| 14 | +else |
| 15 | + HOOK_SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd) |
| 16 | + REPO_ROOT=$(cd "${HOOK_SCRIPT_DIR}/../../.." && pwd) |
| 17 | + # shellcheck source=/dev/null |
| 18 | + . "${REPO_ROOT}/scripts/runtime/lib.sh" |
| 19 | +fi |
| 20 | + |
| 21 | +BEHAVIOR_ID='no-destructive-git' |
| 22 | +BEHAVIOR_NAME='No Destructive Git Operations' |
| 23 | +DEFAULT_LEVEL='hard_block' |
| 24 | +ESCALATION_JSON='[]' |
| 25 | +BLOCK_REASON='Destructive git operation blocked: force push, hard reset, clean -f, and forced branch delete are not allowed.' |
| 26 | +RECOVERY_HINT='Destructive git operations are blocked. Use safe alternatives: git revert, git stash, git reset --soft, or create a new branch.' |
| 27 | +NUDGE_TEMPLATE='' |
| 28 | +WARNING_TEMPLATE='' |
| 29 | +EVENT_NAME='PreToolUse' |
| 30 | + |
| 31 | +PAYLOAD=$(cat) |
| 32 | +SESSION_ID=$(printf '%s' "$PAYLOAD" | forge_session_id) |
| 33 | +TOOL_NAME=$(printf '%s' "$PAYLOAD" | jq -r '.tool_name // empty') |
| 34 | +# For PreToolUse/PostToolUse, conditions see tool_input. |
| 35 | +# For UserPromptSubmit/Stop, merge top-level payload fields (e.g., .prompt) |
| 36 | +# into the condition context so DSL fields like `prompt` resolve. |
| 37 | +TOOL_INPUT_JSON=$(printf '%s' "$PAYLOAD" | jq -c '(.tool_input // {}) + (del(.session_id, .tool_name, .tool_input, .transcript_path, .cwd, .hook_event_name))') |
| 38 | +TOOL_INPUT_HASH=$(printf '%s' "$TOOL_INPUT_JSON" | forge_tool_input_hash) |
| 39 | +TOOL_INPUT_SUMMARY=$(printf '%s' "$TOOL_INPUT_JSON" | cut -c1-100 | tr -d '\n') |
| 40 | + |
| 41 | +# Session-scope disable (set via /forge behavior off --session) |
| 42 | +if forge_behavior_session_is_disabled "$SESSION_ID" "$BEHAVIOR_ID"; then |
| 43 | + exit 0 |
| 44 | +fi |
| 45 | + |
| 46 | +# Conditions (logic: all) |
| 47 | +CONDITIONS_JSON='[{"field":"command","operator":"regex_match","value":"git\\s+(push\\s+[^|;&]*(--force\\b|--force-with-lease\\b|-f\\b)|reset\\s+--hard\\b|clean\\s+-[a-z]*f[a-z]*\\b|branch\\s+-[a-zA-Z]*D[a-zA-Z]*\\b)"}]' |
| 48 | +if ! python3 - "$CONDITIONS_JSON" 'all' "$TOOL_INPUT_JSON" <<'PYCOND' |
| 49 | +import sys, json, re |
| 50 | +conditions = json.loads(sys.argv[1]) |
| 51 | +logic = sys.argv[2] |
| 52 | +tool_input = json.loads(sys.argv[3] or '{}') |
| 53 | +
|
| 54 | +def get_field(f): |
| 55 | + return tool_input.get(f, '') or '' |
| 56 | +
|
| 57 | +def check(c): |
| 58 | + field = c.get('field', '') |
| 59 | + op = c.get('operator', '') |
| 60 | + val = c.get('value', '') |
| 61 | + v = get_field(field) |
| 62 | + sv = str(v) if v is not None else '' |
| 63 | + if op == 'regex_match': |
| 64 | + try: |
| 65 | + return bool(re.search(val, sv)) |
| 66 | + except re.error: |
| 67 | + return False |
| 68 | + if op == 'contains': return val in sv |
| 69 | + if op == 'not_contains': return val not in sv |
| 70 | + if op == 'equals': return sv == str(val) |
| 71 | + if op == 'starts_with': return sv.startswith(val) |
| 72 | + if op == 'ends_with': return sv.endswith(val) |
| 73 | + if op == 'exists': return bool(sv) |
| 74 | + if op == 'not_exists': return not sv |
| 75 | + try: |
| 76 | + nv = float(sv) if sv != '' else 0.0 |
| 77 | + nval = float(val) |
| 78 | + except (TypeError, ValueError): |
| 79 | + return False |
| 80 | + if op == 'gt': return nv > nval |
| 81 | + if op == 'lt': return nv < nval |
| 82 | + if op == 'gte': return nv >= nval |
| 83 | + if op == 'lte': return nv <= nval |
| 84 | + return False |
| 85 | +
|
| 86 | +results = [check(c) for c in conditions] |
| 87 | +if logic == 'any': |
| 88 | + ok = any(results) if results else True |
| 89 | +else: |
| 90 | + ok = all(results) if results else True |
| 91 | +sys.exit(0 if ok else 1) |
| 92 | +PYCOND |
| 93 | +then |
| 94 | + exit 0 |
| 95 | +fi |
| 96 | + |
| 97 | +render_template() { |
| 98 | + local tpl="$1" counter="$2" level="$3" |
| 99 | + printf '%s' "$tpl" \ |
| 100 | + | sed "s|{behavior_id}|${BEHAVIOR_ID}|g" \ |
| 101 | + | sed "s|{behavior_name}|${BEHAVIOR_NAME}|g" \ |
| 102 | + | sed "s|{counter}|${counter}|g" \ |
| 103 | + | sed "s|{tool_name}|${TOOL_NAME}|g" \ |
| 104 | + | sed "s|{level}|${level}|g" \ |
| 105 | + | sed "s|{threshold}|${counter}|g" |
| 106 | +} |
| 107 | + |
| 108 | +emit_output() { |
| 109 | + local level="$1" counter="$2" |
| 110 | + case "$level" in |
| 111 | + silent) |
| 112 | + exit 0 |
| 113 | + ;; |
| 114 | + nudge) |
| 115 | + local msg |
| 116 | + msg=$(render_template "$NUDGE_TEMPLATE" "$counter" "$level") |
| 117 | + jq -cn --arg m "$msg" '{systemMessage: $m}' |
| 118 | + exit 0 |
| 119 | + ;; |
| 120 | + warning) |
| 121 | + local msg |
| 122 | + msg=$(render_template "$WARNING_TEMPLATE" "$counter" "$level") |
| 123 | + jq -cn --arg m "$msg" '{systemMessage: $m}' |
| 124 | + exit 0 |
| 125 | + ;; |
| 126 | + soft_block) |
| 127 | + # Write pending_block BEFORE emitting so the next invocation can |
| 128 | + # detect reinvocation after user override. |
| 129 | + forge_pending_block_set "$SESSION_ID" "$BEHAVIOR_ID" "$TOOL_INPUT_HASH" || true |
| 130 | + local msg |
| 131 | + msg=$(render_template "$BLOCK_REASON" "$counter" "$level") |
| 132 | + jq -cn --arg m "$msg" --arg evt "$EVENT_NAME" \ |
| 133 | + '{hookSpecificOutput: {hookEventName: $evt, permissionDecision: "deny"}, systemMessage: $m}' |
| 134 | + exit 0 |
| 135 | + ;; |
| 136 | + hard_block) |
| 137 | + local msg |
| 138 | + msg=$(render_template "$BLOCK_REASON" "$counter" "$level") |
| 139 | + jq -cn --arg m "$msg" --arg evt "$EVENT_NAME" \ |
| 140 | + '{hookSpecificOutput: {hookEventName: $evt, permissionDecision: "deny", override_allowed: false}, systemMessage: $m}' |
| 141 | + exit 0 |
| 142 | + ;; |
| 143 | + esac |
| 144 | + exit 0 |
| 145 | +} |
| 146 | + |
| 147 | +run_evaluate() { |
| 148 | + # Step 0: try override detection via reinvocation. If the pending_block |
| 149 | + # matches this incoming tool_input hash within the window, record the |
| 150 | + # override and pass through silently — do NOT increment counter. |
| 151 | + if forge_pending_block_try_override "$SESSION_ID" "$BEHAVIOR_ID" \ |
| 152 | + "$TOOL_NAME" "$TOOL_INPUT_HASH" "$TOOL_INPUT_SUMMARY"; then |
| 153 | + exit 0 |
| 154 | + fi |
| 155 | + |
| 156 | + local counter calculated previous effective |
| 157 | + counter=$(forge_counter_increment "$SESSION_ID" "$BEHAVIOR_ID" "$TOOL_NAME") |
| 158 | + [ -n "$counter" ] || { _forge_log "counter increment failed"; exit 0; } |
| 159 | + calculated=$(forge_resolve_level "$counter" "$DEFAULT_LEVEL" "$ESCALATION_JSON") |
| 160 | + previous=$(jq -r --arg sid "$SESSION_ID" --arg bid "$BEHAVIOR_ID" \ |
| 161 | + '.sessions[$sid].behaviors[$bid].effective_level // "silent"' "$FORGE_STATE_FILE") |
| 162 | + effective=$(forge_level_max "$previous" "$calculated") |
| 163 | + forge_effective_level_set "$SESSION_ID" "$BEHAVIOR_ID" "$effective" |
| 164 | + emit_output "$effective" "$counter" |
| 165 | +} |
| 166 | + |
| 167 | +# Action: evaluate |
| 168 | +run_evaluate |
0 commit comments