claude-flow-novice 2.14.36 → 2.14.37

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.
@@ -0,0 +1,232 @@
1
+ #!/bin/bash
2
+
3
+ ##############################################################################
4
+ # CFN Error Logging - CLI Integration Script
5
+ # Version: 1.0.0
6
+ #
7
+ # Integration script for CLI CFN Loop error logging
8
+ # Automatically triggers error capture on CLI failures
9
+ #
10
+ # Usage: Source this script in CLI commands or orchestrator scripts
11
+ # source /path/to/integrate-cli.sh
12
+ ##############################################################################
13
+
14
+ # CFN Error Logging CLI Integration
15
+ # This script provides helper functions for CLI CFN Loop error logging
16
+
17
+ # Get the script directory
18
+ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
19
+ ERROR_LOGGING_SCRIPT="$SCRIPT_DIR/invoke-error-logging.sh"
20
+
21
+ # Helper function
22
+ log() {
23
+ echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*"
24
+ }
25
+
26
+ # Ensure error logging script exists
27
+ if [ ! -f "$ERROR_LOGGING_SCRIPT" ]; then
28
+ echo "⚠️ Warning: CFN error logging script not found at $ERROR_LOGGING_SCRIPT"
29
+ return 1
30
+ fi
31
+
32
+ # Function to capture error on CFN Loop failure
33
+ cfn_capture_error() {
34
+ local task_id="$1"
35
+ local error_type="${2:-cli}"
36
+ local error_message="${3:-CFN Loop failed}"
37
+ local exit_code="${4:-$?}"
38
+ local context_json="${5:-}"
39
+
40
+ # Only capture if we have a task ID
41
+ if [ -n "$task_id" ]; then
42
+ "$ERROR_LOGGING_SCRIPT" \
43
+ --action capture \
44
+ --task-id "$task_id" \
45
+ --error-type "$error_type" \
46
+ --error-message "$error_message" \
47
+ --exit-code "$exit_code" \
48
+ --context "$context_json" \
49
+ >/dev/null 2>&1 || true
50
+ fi
51
+ }
52
+
53
+ # Function to generate error report
54
+ cfn_generate_report() {
55
+ local task_id="$1"
56
+ local format="${2:-markdown}"
57
+
58
+ if [ -n "$task_id" ]; then
59
+ "$ERROR_LOGGING_SCRIPT" \
60
+ --action report \
61
+ --task-id "$task_id" \
62
+ --format "$format" \
63
+ 2>/dev/null || echo "Failed to generate report for task: $task_id"
64
+ else
65
+ echo "❌ Error: Task ID required for report generation"
66
+ return 1
67
+ fi
68
+ }
69
+
70
+ # Function to show available error logs
71
+ cfn_list_errors() {
72
+ local since="${1:-24h}"
73
+ local format="${2:-table}"
74
+
75
+ "$ERROR_LOGGING_SCRIPT" \
76
+ --action list \
77
+ --since "$since" \
78
+ --format "$format" \
79
+ 2>/dev/null || echo "Failed to list error logs"
80
+ }
81
+
82
+ # Function to run system diagnostics
83
+ cfn_run_diagnostics() {
84
+ "$ERROR_LOGGING_SCRIPT" \
85
+ --action diagnostics \
86
+ 2>/dev/null || echo "Failed to run diagnostics"
87
+ }
88
+
89
+ # Function to validate dependencies
90
+ cfn_validate_dependencies() {
91
+ "$ERROR_LOGGING_SCRIPT" \
92
+ --action validate \
93
+ 2>/dev/null || echo "Failed to validate dependencies"
94
+ }
95
+
96
+ # Enhanced CLI command wrapper with error logging
97
+ cfn_cli_wrapper() {
98
+ local command="$1"
99
+ local task_id="$2"
100
+ shift 2
101
+
102
+ # Generate task ID if not provided
103
+ if [ -z "$task_id" ]; then
104
+ task_id="cfn-cli-$(date +%s%N | tail -c 7)-${RANDOM}"
105
+ fi
106
+
107
+ # Execute the command with error capture
108
+ if "$command"; then
109
+ local exit_code=$?
110
+ log "✅ CLI command succeeded for task: $task_id"
111
+ return $exit_code
112
+ else
113
+ local exit_code=$?
114
+ log "❌ CLI command failed for task: $task_id (exit code: $exit_code)"
115
+
116
+ # Capture error details
117
+ cfn_capture_error "$task_id" "cli" "CLI command failed: $command" "$exit_code"
118
+
119
+ # Generate and show report
120
+ echo ""
121
+ echo "📋 Generating error report..."
122
+ cfn_generate_report "$task_id"
123
+
124
+ return $exit_code
125
+ fi
126
+ }
127
+
128
+ # Error trap for automatic capture
129
+ # Usage: trap 'cfn_error_trap $TASK_ID "CLI operation failed"' ERR
130
+ cfn_error_trap() {
131
+ local task_id="$1"
132
+ local context="${2:-Unknown error occurred}"
133
+ local exit_code="${3:-$?}"
134
+
135
+ log "🚨 CFN Error Trap activated"
136
+ cfn_capture_error "$task_id" "trap" "$context" "$exit_code"
137
+
138
+ # Generate report automatically
139
+ if [ -n "$task_id" ]; then
140
+ echo ""
141
+ echo "📋 Auto-generated error report:"
142
+ cfn_generate_report "$task_id"
143
+ fi
144
+ }
145
+
146
+ # Pre-flight validation with error capture
147
+ cfn_preflight_check() {
148
+ local task_id="$1"
149
+
150
+ log "🔍 Running pre-flight validation for task: $task_id"
151
+
152
+ # Validate dependencies
153
+ if ! cfn_validate_dependencies; then
154
+ local exit_code=$?
155
+ cfn_capture_error "$task_id" "preflight" "Pre-flight validation failed" "$exit_code"
156
+ return $exit_code
157
+ fi
158
+
159
+ # Validate environment
160
+ local issues=()
161
+
162
+ # Check Redis
163
+ if ! redis-cli ping >/dev/null 2>&1; then
164
+ issues+=("Redis not connected")
165
+ fi
166
+
167
+ # Check available memory
168
+ if command -v free >/dev/null 2>&1; then
169
+ local available_mem=$(free -m 2>/dev/null | awk 'NR==2{print int($7)}' || echo "0")
170
+ if [ "$available_mem" -lt 512 ]; then
171
+ issues+=("Low memory (${available_mem}MB available)")
172
+ fi
173
+ fi
174
+
175
+ # Check disk space
176
+ if command -v df >/dev/null 2>&1; then
177
+ local available_space=$(df "$PROJECT_ROOT" 2>/dev/null | awk 'NR==2{print int($4/1024)}' || echo "0")
178
+ if [ "$available_space" -lt 100 ]; then
179
+ issues+=("Low disk space (${available_space}MB available)")
180
+ fi
181
+ fi
182
+
183
+ if [ ${#issues[@]} -gt 0 ]; then
184
+ log "⚠️ Pre-flight issues detected:"
185
+ for issue in "${issues[@]}"; do
186
+ log " - $issue"
187
+ done
188
+
189
+ # Don't fail on warnings, just capture
190
+ local context_json="{\"warnings\": [$(printf '"%s",' "${issues[@]}" | sed 's/,$//')],\"severity\": \"warning\"}"
191
+ cfn_capture_error "$task_id" "preflight-warnings" "Pre-flight validation completed with warnings" "0" "$context_json"
192
+ else
193
+ log "✅ Pre-flight validation passed"
194
+ fi
195
+ }
196
+
197
+ # Post-error cleanup with error logging
198
+ cfn_post_error_cleanup() {
199
+ local task_id="$1"
200
+ local exit_code="${2:-$?}"
201
+
202
+ log "🧹 Running post-error cleanup for task: $task_id"
203
+
204
+ # Capture final error state
205
+ cfn_capture_error "$task_id" "cleanup" "Post-error cleanup triggered" "$exit_code"
206
+
207
+ # Run cleanup
208
+ if [ -f "$ERROR_LOGGING_SCRIPT" ]; then
209
+ "$ERROR_LOGGING_SCRIPT" --action cleanup --retention-days 7 >/dev/null 2>&1 || true
210
+ fi
211
+
212
+ log "✅ Post-error cleanup completed"
213
+ }
214
+
215
+ # Export functions for use in other scripts
216
+ export -f cfn_capture_error
217
+ export -f cfn_generate_report
218
+ export -f cfn_list_errors
219
+ export -f cfn_run_diagnostics
220
+ export -f cfn_validate_dependencies
221
+ export -f cfn_cli_wrapper
222
+ export -f cfn_error_trap
223
+ export -f cfn_preflight_check
224
+ export -f cfn_post_error_cleanup
225
+
226
+ # Convenience aliases
227
+ alias cfn-logs='cfn_list_errors'
228
+ alias cfn-report='cfn_generate_report'
229
+ alias cfn-diag='cfn_run_diagnostics'
230
+ alias cfn-check='cfn_validate_dependencies'
231
+
232
+ log "CFN Error Logging CLI integration loaded"
@@ -0,0 +1,294 @@
1
+ #!/bin/bash
2
+
3
+ ##############################################################################
4
+ # CFN Error Logging - Docker Integration Script
5
+ # Version: 1.0.0
6
+ #
7
+ # Integration script for Docker CFN Loop error logging
8
+ # Automatically triggers error capture on Docker container failures
9
+ #
10
+ # Usage: Source this script in Docker commands or orchestrator scripts
11
+ # source /path/to/integrate-docker.sh
12
+ ##############################################################################
13
+
14
+ # CFN Error Logging Docker Integration
15
+ # This script provides helper functions for Docker CFN Loop error logging
16
+
17
+ # Get the script directory
18
+ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
19
+ ERROR_LOGGING_SCRIPT="$SCRIPT_DIR/invoke-error-logging.sh"
20
+
21
+ # Helper function
22
+ log() {
23
+ echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*"
24
+ }
25
+
26
+ # Ensure error logging script exists
27
+ if [ ! -f "$ERROR_LOGGING_SCRIPT" ]; then
28
+ echo "⚠️ Warning: CFN error logging script not found at $ERROR_LOGGING_SCRIPT"
29
+ return 1
30
+ fi
31
+
32
+ # Docker-specific environment detection
33
+ is_docker_environment() {
34
+ [ -f /.dockerenv ] || [ -n "${DOCKER_CONTAINER:-}" ] || grep -q 'docker\|container' /proc/1/cgroup 2>/dev/null
35
+ }
36
+
37
+ # Function to capture Docker container error
38
+ cfn_capture_docker_error() {
39
+ local task_id="$1"
40
+ local container_name="$2"
41
+ local error_type="${3:-docker}"
42
+ local error_message="${4:-Docker container failed}"
43
+ local exit_code="${5:-$?}"
44
+ local context_json="${6:-}"
45
+
46
+ # Add Docker-specific context
47
+ local docker_context="{"
48
+ docker_context+=","
49
+ docker_context+="\"container_name\": \"$container_name\","
50
+ docker_context+="\"docker_id\": \"${DOCKER_CONTAINER_ID:-unknown}\","
51
+ docker_context+="\"docker_image\": \"${DOCKER_IMAGE:-unknown}\","
52
+ docker_context+="\"docker_network\": \"${DOCKER_NETWORK:-unknown}\""
53
+
54
+ # Container resource information
55
+ if command -v docker >/dev/null 2>&1 && [ -n "$container_name" ]; then
56
+ local container_info=$(docker inspect "$container_name" 2>/dev/null || echo "{}")
57
+ local memory_usage=$(echo "$container_info" | jq -r '.[0].MemoryUsage // 0' 2>/dev/null || echo "0")
58
+ local cpu_usage=$(echo "$container_info" | jq -r '.[0].CPUUsage // 0' 2>/dev/null || echo "0")
59
+
60
+ docker_context+=","
61
+ docker_context+="\"memory_usage_mb\": $memory_usage,"
62
+ docker_context+="\"cpu_usage_percent\": $cpu_usage"
63
+ fi
64
+
65
+ docker_context+="}"
66
+
67
+ # Merge with provided context
68
+ if [ -n "$CONTEXT_JSON" ]; then
69
+ docker_context="$docker_context,$CONTEXT_JSON"
70
+ fi
71
+
72
+ docker_context+="}"
73
+
74
+ # Only capture if we have a task ID
75
+ if [ -n "$task_id" ]; then
76
+ "$ERROR_LOGGING_SCRIPT" \
77
+ --action capture \
78
+ --task-id "$task_id" \
79
+ --error-type "$error_type" \
80
+ --error-message "$error_message" \
81
+ --exit-code "$exit_code" \
82
+ --context "$docker_context" \
83
+ >/dev/null 2>&1 || true
84
+ fi
85
+ }
86
+
87
+ # Function to capture Docker logs
88
+ cfn_capture_docker_logs() {
89
+ local task_id="$1"
90
+ local container_name="$2"
91
+ local lines="${3:-100}"
92
+
93
+ if [ -n "$container_name" ] && command -v docker >/dev/null 2>&1; then
94
+ local log_file="$LOG_BASE_DIR/docker-logs-${task_id}-$(date +%s).txt"
95
+ mkdir -p "$LOG_BASE_DIR"
96
+
97
+ log "INFO Capturing Docker logs for container: $container_name"
98
+
99
+ docker logs --tail "$lines" "$container_name" 2>" "$log_file" || true
100
+
101
+ log "Docker logs captured: $log_file"
102
+ fi
103
+ }
104
+
105
+ # Function to capture Docker container state
106
+ cfn_capture_docker_state() {
107
+ local task_id="$1"
108
+ local container_name="$2"
109
+
110
+ if [ -n "$container_name" ] && command -v docker >/dev/null 2>&1; then
111
+ local state_file="$LOG_BASE_DIR/docker-state-${task_id}-$(date +%s).json"
112
+ mkdir -p "$LOG_BASE_DIR"
113
+
114
+ log "CAPTURING Capturing Docker container state for: $container_name"
115
+
116
+ docker inspect "$container_name" 2> "$state_file" || true
117
+
118
+ log "Docker state captured: $state_file"
119
+ fi
120
+ }
121
+
122
+ # Enhanced Docker command wrapper with error logging
123
+ cfn_docker_wrapper() {
124
+ local docker_command="$1"
125
+ local container_name="$2"
126
+ local task_id="$3"
127
+ shift 3
128
+
129
+ # Generate task ID if not provided
130
+ if [ -z "$task_id" ]; then
131
+ task_id="cfn-docker-$(date +%s%N | tail -c 7)-${RANDOM}"
132
+ fi
133
+
134
+ # Generate container name if not provided
135
+ if [ -z "$container_name" ]; then
136
+ container_name="cfn-${task_id}"
137
+ fi
138
+
139
+ log "Starting Docker container: $container_name (task: $task_id)"
140
+
141
+ # Execute Docker command with error capture
142
+ if $docker_command; then
143
+ local exit_code=$?
144
+ log "SUCCESS Docker command succeeded for task: $task_id (container: $container_name)"
145
+ return $exit_code
146
+ else
147
+ local exit_code=$?
148
+ log "ERROR Docker command failed for task: $task_id (container: $container_name, exit code: $exit_code)"
149
+
150
+ # Capture comprehensive Docker error information
151
+ cfn_capture_docker_error "$task_id" "$container_name" "docker" "Docker command failed: $docker_command" "$exit_code"
152
+ cfn_capture_docker_logs "$task_id" "$container_name"
153
+ cfn_capture_docker_state "$task_id" "$container_name"
154
+
155
+ # Generate and show report
156
+ echo ""
157
+ echo "INFO Generating Docker error report..."
158
+ cfn_generate_report "$task_id"
159
+
160
+ return $exit_code
161
+ fi
162
+ }
163
+
164
+ # Docker container monitoring
165
+ cfn_monitor_docker_container() {
166
+ local container_name="$1"
167
+ local task_id="$2"
168
+ local duration="${3:-60}" # Default 60 seconds
169
+
170
+ if [ -z "$container_name" ]; then
171
+ echo "ERROR Error: Container name required for monitoring"
172
+ return 1
173
+ fi
174
+
175
+ log "WATCHING Monitoring Docker container: $container_name (duration: ${duration}s)"
176
+
177
+ local start_time=$(date +%s)
178
+ local end_time=$((start_time + duration))
179
+ local current_time
180
+
181
+ while [ $current_time -lt $end_time ]; do
182
+ if command -v docker >/dev/null 2>&1 && docker ps --filter "name=$container_name" --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" 2>/dev/null | grep -q "$container_name"; then
183
+ # Container is running
184
+ local container_status=$(docker ps --filter "name=$container_name" --format "{{.Status}}" 2>/dev/null)
185
+ local resource_usage=$(docker stats --no-stream --format "table {{.Container}}\t{{.CPUPerc}}\t{{.MemUsage}}" --filter "name=$container_name" 2>/dev/null | tail -n +2)
186
+
187
+ if [ -n "$resource_usage" ]; then
188
+ log "MONITORING Container $container_name ($container_status): $resource_usage"
189
+ fi
190
+ else
191
+ # Container stopped or failed
192
+ log "⚠️ Container $container_name is no longer running"
193
+
194
+ # Capture final state and error information
195
+ cfn_capture_docker_error "$task_id" "$container_name" "docker-monitoring" "Container stopped during monitoring"
196
+ break
197
+ fi
198
+
199
+ sleep 5
200
+ current_time=$(date +%s)
201
+ done
202
+
203
+ log "WATCHING Docker monitoring completed for: $container_name"
204
+ }
205
+
206
+ # Docker cleanup with error logging
207
+ cfn_docker_cleanup() {
208
+ local task_id="$1"
209
+ local container_pattern="$2"
210
+
211
+ log "CLEANING Running Docker cleanup for task: $task_id"
212
+
213
+ # Stop and remove containers matching pattern
214
+ if [ -n "$container_pattern" ]; then
215
+ local containers=$(docker ps -a --filter "name=$container_pattern" --format "{{.Names}}" 2>/dev/null || echo "")
216
+
217
+ for container in $containers; do
218
+ log " Stopping container: $container"
219
+ docker stop "$container" >/dev/null 2>&1 || true
220
+ docker rm "$container" >/dev/null 2>/dev/null || true
221
+ done
222
+ fi
223
+
224
+ # Remove Docker networks if they belong to this task
225
+ local networks=$(docker network ls --filter "name=cfn-$task_id-*" --format "{{.Name}}" 2>/dev/null || echo "")
226
+
227
+ for network in $networks; do
228
+ log " Removing network: $network"
229
+ docker network rm "$network" >/dev/null 2>/dev/null || true
230
+ done
231
+
232
+ # Remove Docker volumes if they belong to this task
233
+ local volumes=$(docker volume ls --filter "name=cfn-$task_id-*" --format "{{.Name}}" 2>/dev/null || echo "")
234
+
235
+ for volume in $volumes; do
236
+ log " Removing volume: $volume"
237
+ docker volume rm "$volume" >/dev/null 2/dv/null || true
238
+ done
239
+
240
+ # Capture cleanup state
241
+ cfn_capture_docker_error "$task_id" "docker-cleanup" "Docker cleanup completed" "0"
242
+
243
+ # Run standard cleanup
244
+ if [ -f "$ERROR_LOGGING_SCRIPT" ]; then
245
+ "$ERROR_LOGGING_SCRIPT" --action cleanup --retention-days 7 >/dev/null 2>&1 || true
246
+ fi
247
+
248
+ log "SUCCESS Docker cleanup completed"
249
+ }
250
+
251
+ # Docker resource monitoring
252
+ cfn_monitor_docker_resources() {
253
+ local task_id="$1"
254
+
255
+ log "MONITORING Monitoring Docker resources for task: $task_id"
256
+
257
+ if command -v docker >/dev/null 2>&1; then
258
+ echo "Docker System Status:"
259
+ docker system df
260
+ echo ""
261
+
262
+ echo "Running Containers:"
263
+ docker ps --format "table {{.Names}}\t{{.Image}}\t{{.Status}}\t{{.Ports}}\t{{.Size}}"
264
+ echo ""
265
+
266
+ echo "Resource Usage:"
267
+ docker stats --no-stream --format "table {{.Container}}\t{{.CPUPerc}}\{{.MemUsage}}\t{{.NetIO}}" | head -10
268
+ echo ""
269
+
270
+ # CFN-specific containers
271
+ echo "CFN Loop Containers:"
272
+ docker ps --filter "name=cfn-*" --format "table {{.Names}}\t{{.Image}}\t{{.Status}}\t{{.CreatedAt}}" 2>/dev/null || echo "No CFN containers running"
273
+ fi
274
+ }
275
+
276
+ # Export functions for use in other scripts
277
+ export -f cfn_capture_docker_error
278
+ export -f cfn_capture_docker_logs
279
+ export -f cfn_capture_docker_state
280
+ export -f cfn_docker_wrapper
281
+ export -f cfn_monitor_docker_container
282
+ export -f cfn_docker_cleanup
283
+ export -f cfn_monitor_docker_resources
284
+
285
+ # Convenience aliases
286
+ if is_docker_environment; then
287
+ alias cfn-docker-logs='cfn_capture_docker_logs'
288
+ alias cfn-docker-state='cfn_capture_docker_state'
289
+ alias cfn-docker-monitor='cfn_monitor_docker_container'
290
+ alias cfn-docker-cleanup='cfn_docker_cleanup'
291
+ alias cfn-docker-resources='cfn_monitor_docker_resources'
292
+ fi
293
+
294
+ log "CFN Error Logging Docker integration loaded"