@paulduvall/claude-dev-toolkit 0.0.1-alpha.2 → 0.0.1-alpha.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. package/README.md +44 -6
  2. package/commands/active/xarchitecture.md +393 -0
  3. package/commands/active/xconfig.md +127 -0
  4. package/commands/active/xdebug.md +130 -0
  5. package/commands/active/xdocs.md +178 -0
  6. package/commands/active/xgit.md +149 -0
  7. package/commands/active/xpipeline.md +152 -0
  8. package/commands/active/xquality.md +96 -0
  9. package/commands/active/xrefactor.md +198 -0
  10. package/commands/active/xrelease.md +142 -0
  11. package/commands/active/xsecurity.md +92 -0
  12. package/commands/active/xspec.md +174 -0
  13. package/commands/active/xtdd.md +151 -0
  14. package/commands/active/xtest.md +89 -0
  15. package/commands/experiments/xact.md +742 -0
  16. package/commands/experiments/xanalytics.md +113 -0
  17. package/commands/experiments/xanalyze.md +70 -0
  18. package/commands/experiments/xapi.md +161 -0
  19. package/commands/experiments/xatomic.md +112 -0
  20. package/commands/experiments/xaws.md +85 -0
  21. package/commands/experiments/xcicd.md +337 -0
  22. package/commands/experiments/xcommit.md +122 -0
  23. package/commands/experiments/xcompliance.md +182 -0
  24. package/commands/experiments/xconstraints.md +89 -0
  25. package/commands/experiments/xcoverage.md +90 -0
  26. package/commands/experiments/xdb.md +102 -0
  27. package/commands/experiments/xdesign.md +121 -0
  28. package/commands/experiments/xevaluate.md +111 -0
  29. package/commands/experiments/xfootnote.md +12 -0
  30. package/commands/experiments/xgenerate.md +117 -0
  31. package/commands/experiments/xgovernance.md +149 -0
  32. package/commands/experiments/xgreen.md +66 -0
  33. package/commands/experiments/xiac.md +118 -0
  34. package/commands/experiments/xincident.md +137 -0
  35. package/commands/experiments/xinfra.md +115 -0
  36. package/commands/experiments/xknowledge.md +115 -0
  37. package/commands/experiments/xmaturity.md +120 -0
  38. package/commands/experiments/xmetrics.md +118 -0
  39. package/commands/experiments/xmonitoring.md +128 -0
  40. package/commands/experiments/xnew.md +898 -0
  41. package/commands/experiments/xobservable.md +114 -0
  42. package/commands/experiments/xoidc.md +165 -0
  43. package/commands/experiments/xoptimize.md +115 -0
  44. package/commands/experiments/xperformance.md +112 -0
  45. package/commands/experiments/xplanning.md +131 -0
  46. package/commands/experiments/xpolicy.md +115 -0
  47. package/commands/experiments/xproduct.md +98 -0
  48. package/commands/experiments/xreadiness.md +75 -0
  49. package/commands/experiments/xred.md +55 -0
  50. package/commands/experiments/xrisk.md +128 -0
  51. package/commands/experiments/xrules.md +124 -0
  52. package/commands/experiments/xsandbox.md +120 -0
  53. package/commands/experiments/xscan.md +102 -0
  54. package/commands/experiments/xsetup.md +123 -0
  55. package/commands/experiments/xtemplate.md +116 -0
  56. package/commands/experiments/xtrace.md +212 -0
  57. package/commands/experiments/xux.md +171 -0
  58. package/commands/experiments/xvalidate.md +104 -0
  59. package/commands/experiments/xworkflow.md +113 -0
  60. package/hooks/README.md +231 -0
  61. package/hooks/file-logger.sh +98 -0
  62. package/hooks/lib/argument-parser.sh +422 -0
  63. package/hooks/lib/config-constants.sh +230 -0
  64. package/hooks/lib/context-manager.sh +549 -0
  65. package/hooks/lib/error-handler.sh +412 -0
  66. package/hooks/lib/execution-engine.sh +627 -0
  67. package/hooks/lib/file-utils.sh +375 -0
  68. package/hooks/lib/subagent-discovery.sh +465 -0
  69. package/hooks/lib/subagent-validator.sh +597 -0
  70. package/hooks/on-error-debug.sh +221 -0
  71. package/hooks/pre-commit-quality.sh +204 -0
  72. package/hooks/pre-write-security.sh +107 -0
  73. package/hooks/prevent-credential-exposure.sh +265 -0
  74. package/hooks/subagent-trigger-simple.sh +193 -0
  75. package/hooks/subagent-trigger.sh +253 -0
  76. package/lib/hook-installer-core.js +2 -2
  77. package/package.json +3 -1
  78. package/scripts/postinstall.js +28 -10
  79. package/templates/README.md +100 -0
  80. package/templates/basic-settings.json +30 -0
  81. package/templates/comprehensive-settings.json +206 -0
  82. package/templates/hybrid-hook-config.yaml +133 -0
  83. package/templates/security-focused-settings.json +62 -0
  84. package/templates/subagent-hooks.yaml +188 -0
@@ -0,0 +1,412 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # Error Handling Module for Subagent-Hook Integration
4
+ #
5
+ # This module provides standardized error handling, logging, and recovery
6
+ # mechanisms for the subagent-hook integration system.
7
+
8
+ # Source required modules
9
+ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
10
+ source "$SCRIPT_DIR/config-constants.sh"
11
+ source "$SCRIPT_DIR/file-utils.sh"
12
+
13
+ ##################################
14
+ # Logging Functions
15
+ ##################################
16
+
17
+ log_message() {
18
+ local level="$1"
19
+ local message="$2"
20
+ local timestamp
21
+
22
+ timestamp="$(date +'%Y-%m-%d %H:%M:%S')"
23
+
24
+ # Ensure log file exists
25
+ ensure_log_files || return $?
26
+
27
+ # Format log entry
28
+ local log_entry="[$timestamp] [$HOOK_NAME] [$level] $message"
29
+
30
+ # Write to log file and display to user
31
+ echo "$log_entry" | tee -a "$LOG_FILE"
32
+
33
+ return $EXIT_SUCCESS
34
+ }
35
+
36
+ log_info() {
37
+ local message="$1"
38
+ log_message "INFO" "$message"
39
+ }
40
+
41
+ log_warning() {
42
+ local message="$1"
43
+ log_message "WARN" "$message" >&2
44
+ }
45
+
46
+ log_error() {
47
+ local message="$1"
48
+ log_message "ERROR" "$message" >&2
49
+ }
50
+
51
+ log_critical() {
52
+ local message="$1"
53
+ log_message "CRITICAL" "$message" >&2
54
+ }
55
+
56
+ log_debug() {
57
+ local message="$1"
58
+ # Only log debug messages if debug mode is enabled
59
+ if [[ "${DEBUG:-false}" == "true" ]]; then
60
+ log_message "DEBUG" "$message"
61
+ fi
62
+ }
63
+
64
+ ##################################
65
+ # Violation Logging
66
+ ##################################
67
+
68
+ log_violation() {
69
+ local violation_type="$1"
70
+ local details="$2"
71
+ local timestamp
72
+
73
+ timestamp="$(date +'%Y-%m-%d %H:%M:%S')"
74
+
75
+ # Ensure violation log exists
76
+ ensure_log_files || return $?
77
+
78
+ local violation_entry="[$timestamp] VIOLATION: $violation_type - $details"
79
+
80
+ # Write to both violation log and main log
81
+ echo "$violation_entry" | tee -a "$VIOLATION_LOG" >> "$LOG_FILE"
82
+
83
+ return $EXIT_SUCCESS
84
+ }
85
+
86
+ ##################################
87
+ # Error Handler Functions
88
+ ##################################
89
+
90
+ handle_missing_subagent() {
91
+ local subagent_name="$1"
92
+
93
+ log_error "Subagent not found: $subagent_name"
94
+
95
+ # Provide helpful guidance
96
+ echo "Available subagents:" >&2
97
+ if [[ -d "$SUBAGENTS_DIR" ]]; then
98
+ find "$SUBAGENTS_DIR" -name "*$SUBAGENT_FILE_EXTENSION" -type f 2>/dev/null | \
99
+ sed "s|$SUBAGENTS_DIR/||g" | \
100
+ sed "s|$SUBAGENT_FILE_EXTENSION||g" | \
101
+ sed 's/^/ - /' >&2
102
+ fi
103
+
104
+ return $EXIT_SUBAGENT_NOT_FOUND
105
+ }
106
+
107
+ handle_validation_failure() {
108
+ local subagent_name="$1"
109
+ local reason="$2"
110
+
111
+ log_error "Subagent validation failed: $subagent_name - $reason"
112
+
113
+ echo "Validation failure details:" >&2
114
+ echo " Subagent: $subagent_name" >&2
115
+ echo " Reason: $reason" >&2
116
+ echo " Fix: Check subagent file format and required fields" >&2
117
+
118
+ return $EXIT_VALIDATION_FAILED
119
+ }
120
+
121
+ handle_execution_failure() {
122
+ local subagent_name="$1"
123
+ local error_details="$2"
124
+
125
+ log_error "Subagent execution failed: $subagent_name - $error_details"
126
+
127
+ echo "Execution failure:" >&2
128
+ echo " Subagent: $subagent_name" >&2
129
+ echo " Error: $error_details" >&2
130
+
131
+ return $EXIT_EXECUTION_FAILED
132
+ }
133
+
134
+ handle_timeout() {
135
+ local subagent_name="$1"
136
+ local timeout_seconds="$2"
137
+
138
+ log_error "Subagent execution timeout: $subagent_name after ${timeout_seconds}s"
139
+
140
+ echo "Timeout occurred:" >&2
141
+ echo " Subagent: $subagent_name" >&2
142
+ echo " Timeout: ${timeout_seconds}s" >&2
143
+ echo " Fix: Increase timeout or optimize subagent" >&2
144
+
145
+ return $EXIT_TIMEOUT
146
+ }
147
+
148
+ handle_security_violation() {
149
+ local violation_type="$1"
150
+ local details="$2"
151
+ local file_path="${3:-unknown}"
152
+
153
+ log_critical "Security violation detected: $violation_type"
154
+ log_violation "$violation_type" "$details (file: $file_path)"
155
+
156
+ echo "🚨 SECURITY VIOLATION DETECTED!" >&2
157
+ echo "Type: $violation_type" >&2
158
+ echo "Details: $details" >&2
159
+ echo "File: $file_path" >&2
160
+ echo "" >&2
161
+ echo "The operation has been BLOCKED for security reasons." >&2
162
+
163
+ # Send notification if webhook is configured
164
+ send_security_notification "$violation_type" "$details" "$file_path"
165
+
166
+ return $EXIT_SECURITY_VIOLATION
167
+ }
168
+
169
+ handle_filesystem_error() {
170
+ local operation="$1"
171
+ local path="$2"
172
+ local error_msg="$3"
173
+
174
+ log_error "Filesystem operation failed: $operation on $path - $error_msg"
175
+
176
+ echo "Filesystem error:" >&2
177
+ echo " Operation: $operation" >&2
178
+ echo " Path: $path" >&2
179
+ echo " Error: $error_msg" >&2
180
+
181
+ return $EXIT_GENERAL_ERROR
182
+ }
183
+
184
+ ##################################
185
+ # Recovery Functions
186
+ ##################################
187
+
188
+ attempt_recovery() {
189
+ local error_type="$1"
190
+ local context="$2"
191
+
192
+ log_info "Attempting recovery for error: $error_type"
193
+
194
+ case "$error_type" in
195
+ "missing_directories")
196
+ ensure_required_directories
197
+ return $?
198
+ ;;
199
+ "corrupted_config")
200
+ log_warning "Config file appears corrupted, using defaults"
201
+ return $EXIT_SUCCESS
202
+ ;;
203
+ "temp_file_cleanup")
204
+ cleanup_temp_files
205
+ return $EXIT_SUCCESS
206
+ ;;
207
+ *)
208
+ log_warning "No recovery method available for: $error_type"
209
+ return $EXIT_GENERAL_ERROR
210
+ ;;
211
+ esac
212
+ }
213
+
214
+ ##################################
215
+ # Cleanup Functions
216
+ ##################################
217
+
218
+ safe_exit() {
219
+ local exit_code="$1"
220
+ local cleanup_context="${2:-general}"
221
+
222
+ log_debug "Initiating safe exit with code: $exit_code"
223
+
224
+ # Cleanup temporary files
225
+ cleanup_temp_files
226
+
227
+ # Cleanup specific context files if provided
228
+ if [[ -n "$CONTEXT_FILE" ]] && [[ -f "$CONTEXT_FILE" ]]; then
229
+ cleanup_specific_temp_file "$CONTEXT_FILE"
230
+ fi
231
+
232
+ # Log exit
233
+ if [[ "$exit_code" -eq "$EXIT_SUCCESS" ]]; then
234
+ log_info "Operation completed successfully"
235
+ else
236
+ log_error "Operation failed with exit code: $exit_code"
237
+ fi
238
+
239
+ exit "$exit_code"
240
+ }
241
+
242
+ emergency_cleanup() {
243
+ log_critical "Emergency cleanup initiated"
244
+
245
+ # Remove all temporary files
246
+ cleanup_temp_files
247
+
248
+ # Kill any background processes if they exist
249
+ local bg_processes
250
+ bg_processes=$(jobs -p 2>/dev/null)
251
+ if [[ -n "$bg_processes" ]]; then
252
+ echo "$bg_processes" | xargs kill 2>/dev/null || true
253
+ fi
254
+
255
+ log_info "Emergency cleanup completed"
256
+ }
257
+
258
+ ##################################
259
+ # Notification Functions
260
+ ##################################
261
+
262
+ send_security_notification() {
263
+ local violation_type="$1"
264
+ local details="$2"
265
+ local file_path="$3"
266
+ local webhook_url="${SECURITY_WEBHOOK_URL:-}"
267
+
268
+ if [[ -z "$webhook_url" ]]; then
269
+ return $EXIT_SUCCESS
270
+ fi
271
+
272
+ local payload
273
+ payload=$(cat <<EOF
274
+ {
275
+ "text": "🚨 SECURITY ALERT: Subagent hook violation",
276
+ "attachments": [{
277
+ "color": "danger",
278
+ "fields": [
279
+ {"title": "Violation Type", "value": "$violation_type", "short": true},
280
+ {"title": "File", "value": "$file_path", "short": true},
281
+ {"title": "Details", "value": "$details", "short": false},
282
+ {"title": "User", "value": "$USER", "short": true},
283
+ {"title": "Timestamp", "value": "$(date)", "short": true},
284
+ {"title": "Host", "value": "$(hostname)", "short": true}
285
+ ]
286
+ }]
287
+ }
288
+ EOF
289
+ )
290
+
291
+ # Send notification (don't fail if notification fails)
292
+ if command -v curl >/dev/null 2>&1; then
293
+ curl -s -X POST "$webhook_url" \
294
+ -H "Content-Type: application/json" \
295
+ -d "$payload" >/dev/null 2>&1 || true
296
+ fi
297
+
298
+ log_debug "Security notification sent"
299
+ return $EXIT_SUCCESS
300
+ }
301
+
302
+ ##################################
303
+ # Error Context Functions
304
+ ##################################
305
+
306
+ get_error_context() {
307
+ local error_type="$1"
308
+
309
+ cat <<EOF
310
+ Error Context:
311
+ Hook: $HOOK_NAME
312
+ Version: $SUBAGENT_HOOK_VERSION
313
+ User: $USER
314
+ Working Directory: $(pwd)
315
+ Git Branch: $(git branch --show-current 2>/dev/null || echo 'not-in-git')
316
+ Timestamp: $(date -u +"%Y-%m-%dT%H:%M:%SZ")
317
+ Process ID: $$
318
+ Error Type: $error_type
319
+ Environment:
320
+ CLAUDE_TOOL: ${CLAUDE_TOOL:-unset}
321
+ CLAUDE_FILE: ${CLAUDE_FILE:-unset}
322
+ CLAUDE_HOOK_TRIGGER: ${CLAUDE_HOOK_TRIGGER:-unset}
323
+ EOF
324
+ }
325
+
326
+ log_error_with_context() {
327
+ local error_message="$1"
328
+ local error_type="${2:-general}"
329
+
330
+ log_error "$error_message"
331
+ log_debug "$(get_error_context "$error_type")"
332
+ }
333
+
334
+ ##################################
335
+ # Trap Handlers
336
+ ##################################
337
+
338
+ setup_error_traps() {
339
+ # Set up error trapping
340
+ trap 'handle_script_error $LINENO $? $BASH_COMMAND' ERR
341
+ trap 'emergency_cleanup; safe_exit 130' INT TERM
342
+ trap 'cleanup_temp_files' EXIT
343
+ }
344
+
345
+ handle_script_error() {
346
+ local line_no="$1"
347
+ local exit_code="$2"
348
+ local failed_command="$3"
349
+
350
+ log_critical "Script error at line $line_no: $failed_command (exit: $exit_code)"
351
+
352
+ # Try to provide helpful context
353
+ case "$failed_command" in
354
+ *"mkdir"*|*"chmod"*|*"chown"*)
355
+ handle_filesystem_error "permission_error" "unknown" "check file/directory permissions"
356
+ ;;
357
+ *"curl"*|*"wget"*)
358
+ log_error "Network operation failed - check connectivity"
359
+ ;;
360
+ *)
361
+ log_error "Unknown error occurred"
362
+ ;;
363
+ esac
364
+
365
+ emergency_cleanup
366
+ safe_exit "$exit_code"
367
+ }
368
+
369
+ ##################################
370
+ # Validation Functions
371
+ ##################################
372
+
373
+ validate_error_handler_dependencies() {
374
+ local missing_commands=()
375
+
376
+ # Check for required commands
377
+ local required_commands=("date" "tee" "find" "sed" "hostname")
378
+
379
+ for cmd in "${required_commands[@]}"; do
380
+ if ! command -v "$cmd" >/dev/null 2>&1; then
381
+ missing_commands+=("$cmd")
382
+ fi
383
+ done
384
+
385
+ if [[ ${#missing_commands[@]} -gt 0 ]]; then
386
+ echo "ERROR: Missing required commands: ${missing_commands[*]}" >&2
387
+ return $EXIT_GENERAL_ERROR
388
+ fi
389
+
390
+ return $EXIT_SUCCESS
391
+ }
392
+
393
+ ##################################
394
+ # Initialize Error Handling
395
+ ##################################
396
+
397
+ initialize_error_handling() {
398
+ # Validate dependencies
399
+ validate_error_handler_dependencies || return $?
400
+
401
+ # Ensure required directories exist
402
+ ensure_required_directories || return $?
403
+
404
+ # Ensure log files exist
405
+ ensure_log_files || return $?
406
+
407
+ # Set up error traps
408
+ setup_error_traps
409
+
410
+ log_debug "Error handling system initialized"
411
+ return $EXIT_SUCCESS
412
+ }