claude-flow-novice 2.14.36 → 2.15.0
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.
- package/.claude/commands/cfn-loop-cli.md +491 -456
- package/.claude/commands/switch-api.md +33 -12
- package/.claude/skills/cfn-agent-spawning/get-agent-provider-env.sh +107 -0
- package/.claude/skills/cfn-agent-spawning/parse-agent-provider.sh +59 -0
- package/.claude/skills/cfn-docker-agent-spawning/spawn-agent.sh +24 -6
- package/.claude/skills/cfn-loop-orchestration/helpers/spawn-agents.sh +18 -9
- package/.claude/skills/cfn-redis-coordination/invoke-waiting-mode.sh +220 -220
- package/claude-assets/agents/cfn-dev-team/developers/backend-developer.md +5 -0
- package/claude-assets/agents/custom/claude-code-expert.md +151 -2
- package/claude-assets/agents/docker-coordinators/cfn-docker-v3-coordinator.md +43 -3
- package/claude-assets/commands/cfn-loop-cli.md +491 -456
- package/claude-assets/commands/switch-api.md +33 -12
- package/claude-assets/skills/cfn-agent-spawning/get-agent-provider-env.sh +107 -0
- package/claude-assets/skills/cfn-agent-spawning/parse-agent-provider.sh +59 -0
- package/claude-assets/skills/cfn-docker-agent-spawning/spawn-agent.sh +24 -6
- package/claude-assets/skills/cfn-error-logging/SKILL.md +339 -0
- package/claude-assets/skills/cfn-error-logging/cleanup-error-logs.sh +334 -0
- package/claude-assets/skills/cfn-error-logging/integrate-cli.sh +232 -0
- package/claude-assets/skills/cfn-error-logging/integrate-docker.sh +294 -0
- package/claude-assets/skills/cfn-error-logging/invoke-error-logging.sh +839 -0
- package/claude-assets/skills/cfn-error-logging/test-error-logging.sh +475 -0
- package/claude-assets/skills/cfn-loop-orchestration/helpers/spawn-agents.sh +18 -9
- package/claude-assets/skills/cfn-process-instrumentation/instrument-process.sh +5 -3
- package/claude-assets/skills/cfn-redis-coordination/invoke-waiting-mode.sh +220 -220
- package/claude-assets/skills/cfn-task-mode-sanitize/task-mode-env-sanitizer.sh +21 -9
- package/claude-assets/skills/cfn-validation-runner-instrumentation/wrapped-executor.sh +3 -1
- package/dist/hello.js +27 -3
- package/dist/hello.js.map +1 -1
- package/dist/server.js +194 -0
- package/dist/server.js.map +1 -0
- package/dist/server.test.js +207 -0
- package/dist/server.test.js.map +1 -0
- package/package.json +2 -1
- package/scripts/switch-api.sh +140 -12
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
#!/bin/bash
|
|
2
|
+
|
|
3
|
+
##############################################################################
|
|
4
|
+
# CFN Error Logging - Cleanup and Management Script
|
|
5
|
+
# Version: 1.0.0
|
|
6
|
+
#
|
|
7
|
+
# Automated cleanup and management for CFN error logs
|
|
8
|
+
# Manages log rotation, compression, and retention policies
|
|
9
|
+
#
|
|
10
|
+
# Usage: ./cleanup-error-logs.sh [--retention-days 7] [--dry-run] [--force]
|
|
11
|
+
##############################################################################
|
|
12
|
+
|
|
13
|
+
set -euo pipefail
|
|
14
|
+
|
|
15
|
+
# Configuration
|
|
16
|
+
LOG_BASE_DIR="/tmp/cfn_error_logs"
|
|
17
|
+
DEFAULT_RETENTION_DAYS=7
|
|
18
|
+
MAX_TOTAL_SIZE_MB=100
|
|
19
|
+
COMPRESS_THRESHOLD_DAYS=1
|
|
20
|
+
|
|
21
|
+
# Parse arguments
|
|
22
|
+
RETENTION_DAYS="$DEFAULT_RETENTION_DAYS"
|
|
23
|
+
DRY_RUN=false
|
|
24
|
+
FORCE=false
|
|
25
|
+
|
|
26
|
+
while [[ $# -gt 0 ]]; do
|
|
27
|
+
case $1 in
|
|
28
|
+
--retention-days)
|
|
29
|
+
RETENTION_DAYS="$2"
|
|
30
|
+
shift 2
|
|
31
|
+
;;
|
|
32
|
+
--dry-run)
|
|
33
|
+
DRY_RUN=true
|
|
34
|
+
shift
|
|
35
|
+
;;
|
|
36
|
+
--force)
|
|
37
|
+
FORCE=true
|
|
38
|
+
shift
|
|
39
|
+
;;
|
|
40
|
+
--help|-h)
|
|
41
|
+
cat << EOF
|
|
42
|
+
CFN Error Logging - Cleanup Script
|
|
43
|
+
|
|
44
|
+
Usage: $0 [OPTIONS]
|
|
45
|
+
|
|
46
|
+
Options:
|
|
47
|
+
--retention-days N Delete logs older than N days (default: 7)
|
|
48
|
+
--dry-run Show what would be deleted without actually deleting
|
|
49
|
+
--force Skip confirmation prompts
|
|
50
|
+
--help, -h Show this help message
|
|
51
|
+
|
|
52
|
+
Examples:
|
|
53
|
+
$0 # Standard cleanup (7-day retention)
|
|
54
|
+
$0 --retention-days 3 # Delete logs older than 3 days
|
|
55
|
+
$0 --dry-run # Preview what would be deleted
|
|
56
|
+
$0 --force --retention-days 1 # Force delete logs older than 1 day
|
|
57
|
+
EOF
|
|
58
|
+
exit 0
|
|
59
|
+
;;
|
|
60
|
+
*)
|
|
61
|
+
echo "❌ Unknown option: $1"
|
|
62
|
+
echo "Use --help for usage information"
|
|
63
|
+
exit 1
|
|
64
|
+
;;
|
|
65
|
+
esac
|
|
66
|
+
done
|
|
67
|
+
|
|
68
|
+
# Logging function
|
|
69
|
+
log() {
|
|
70
|
+
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*"
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
# Ensure base directory exists
|
|
74
|
+
mkdir -p "$LOG_BASE_DIR"
|
|
75
|
+
|
|
76
|
+
# Get current disk usage of error logs
|
|
77
|
+
get_disk_usage() {
|
|
78
|
+
if command -v du >/dev/null 2>&1; then
|
|
79
|
+
du -sm "$LOG_BASE_DIR" 2>/dev/null | cut -f1 || echo "0"
|
|
80
|
+
else
|
|
81
|
+
echo "0"
|
|
82
|
+
fi
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
# Get total size in MB with detailed breakdown
|
|
86
|
+
get_size_breakdown() {
|
|
87
|
+
local total_size=0
|
|
88
|
+
local error_logs=0
|
|
89
|
+
local reports=0
|
|
90
|
+
local compressed=0
|
|
91
|
+
local other=0
|
|
92
|
+
|
|
93
|
+
if [ -d "$LOG_BASE_DIR" ]; then
|
|
94
|
+
# Error logs
|
|
95
|
+
if [ -d "$LOG_BASE_DIR/logs" ]; then
|
|
96
|
+
error_logs=$(du -sm "$LOG_BASE_DIR/logs" 2>/dev/null | cut -f1 || echo "0")
|
|
97
|
+
fi
|
|
98
|
+
|
|
99
|
+
# Reports
|
|
100
|
+
if [ -d "$LOG_BASE_DIR/reports" ]; then
|
|
101
|
+
reports=$(du -sm "$LOG_BASE_DIR/reports" 2>/dev/null | cut -f1 || echo "0")
|
|
102
|
+
fi
|
|
103
|
+
|
|
104
|
+
# Compressed logs
|
|
105
|
+
if [ -d "$LOG_BASE_DIR/compressed" ]; then
|
|
106
|
+
compressed=$(du -sm "$LOG_BASE_DIR/compressed" 2>/dev/null | cut -f1 || echo "0")
|
|
107
|
+
fi
|
|
108
|
+
|
|
109
|
+
# Other files
|
|
110
|
+
other=$(du -sm "$LOG_BASE_DIR" 2>/dev/null | cut -f1 || echo "0")
|
|
111
|
+
total_size=$((error_logs + reports + compressed + other))
|
|
112
|
+
fi
|
|
113
|
+
|
|
114
|
+
echo "$total_size,$error_logs,$reports,$compressed,$other"
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
# List files to be deleted
|
|
118
|
+
list_files_to_delete() {
|
|
119
|
+
local cutoff_date
|
|
120
|
+
cutoff_date=$(date -d "$RETENTION_DAYS days ago" +%s 2>/dev/null || date -v-"$RETENTION_DAYS"d +%s)
|
|
121
|
+
|
|
122
|
+
find "$LOG_BASE_DIR" -type f -name "*.json" -o -name "*.md" -o -name "*.txt" | while read -r file; do
|
|
123
|
+
local file_date
|
|
124
|
+
file_date=$(stat -c %Y "$file" 2>/dev/null || stat -f %m "$file" 2>/dev/null || echo "0")
|
|
125
|
+
|
|
126
|
+
if [ "$file_date" -lt "$cutoff_date" ]; then
|
|
127
|
+
echo "$file"
|
|
128
|
+
fi
|
|
129
|
+
done
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
# Compress old logs
|
|
133
|
+
compress_old_logs() {
|
|
134
|
+
local cutoff_date
|
|
135
|
+
cutoff_date=$(date -d "$COMPRESS_THRESHOLD_DAYS days ago" +%s 2>/dev/null || date -v-"$COMPRESS_THRESHOLD_DAYS"d +%s)
|
|
136
|
+
|
|
137
|
+
mkdir -p "$LOG_BASE_DIR/compressed"
|
|
138
|
+
|
|
139
|
+
log "🗜️ Compressing logs older than $COMPRESS_THRESHOLD_DAYS days..."
|
|
140
|
+
|
|
141
|
+
local compressed_count=0
|
|
142
|
+
find "$LOG_BASE_DIR" -type f -name "*.json" -not -path "*/compressed/*" | while read -r file; do
|
|
143
|
+
local file_date
|
|
144
|
+
file_date=$(stat -c %Y "$file" 2>/dev/null || stat -f %m "$file" 2>/dev/null || echo "0")
|
|
145
|
+
|
|
146
|
+
if [ "$file_date" -lt "$cutoff_date" ]; then
|
|
147
|
+
local basename
|
|
148
|
+
basename=$(basename "$file" .json)
|
|
149
|
+
local compressed_file="$LOG_BASE_DIR/compressed/${basename}.json.gz"
|
|
150
|
+
|
|
151
|
+
if [ ! -f "$compressed_file" ]; then
|
|
152
|
+
if gzip -c "$file" > "$compressed_file" 2>/dev/null; then
|
|
153
|
+
if [ "$DRY_RUN" != true ]; then
|
|
154
|
+
rm -f "$file"
|
|
155
|
+
fi
|
|
156
|
+
compressed_count=$((compressed_count + 1))
|
|
157
|
+
log " Compressed: $(basename "$file") → $(basename "$compressed_file")"
|
|
158
|
+
fi
|
|
159
|
+
fi
|
|
160
|
+
fi
|
|
161
|
+
done
|
|
162
|
+
|
|
163
|
+
if [ "$compressed_count" -gt 0 ]; then
|
|
164
|
+
log "✅ Compressed $compressed_count log files"
|
|
165
|
+
else
|
|
166
|
+
log "ℹ️ No files needed compression"
|
|
167
|
+
fi
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
# Delete empty directories
|
|
171
|
+
cleanup_empty_dirs() {
|
|
172
|
+
if [ "$DRY_RUN" != true ]; then
|
|
173
|
+
find "$LOG_BASE_DIR" -type d -empty -delete 2>/dev/null || true
|
|
174
|
+
fi
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
# Show cleanup summary
|
|
178
|
+
show_summary() {
|
|
179
|
+
local before_size="$1"
|
|
180
|
+
local after_size="$2"
|
|
181
|
+
local files_deleted="$3"
|
|
182
|
+
|
|
183
|
+
local size_saved=$((before_size - after_size))
|
|
184
|
+
|
|
185
|
+
echo ""
|
|
186
|
+
echo "📊 Cleanup Summary:"
|
|
187
|
+
echo " Files deleted: $files_deleted"
|
|
188
|
+
echo " Space saved: ${size_saved}MB"
|
|
189
|
+
echo " Current usage: ${after_size}MB"
|
|
190
|
+
|
|
191
|
+
if [ "$after_size" -gt "$MAX_TOTAL_SIZE_MB" ]; then
|
|
192
|
+
echo " ⚠️ Warning: Error logs still exceed recommended size (${MAX_TOTAL_SIZE_MB}MB)"
|
|
193
|
+
echo " Consider running with --retention-days $(($RETENTION_DAYS - 1)) to reduce further"
|
|
194
|
+
fi
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
# Main cleanup function
|
|
198
|
+
run_cleanup() {
|
|
199
|
+
log "🧹 Starting CFN error log cleanup (retention: $RETENTION_DAYS days)"
|
|
200
|
+
|
|
201
|
+
# Get initial size
|
|
202
|
+
local size_breakdown
|
|
203
|
+
size_breakdown=$(get_size_breakdown)
|
|
204
|
+
local before_size
|
|
205
|
+
before_size=$(echo "$size_breakdown" | cut -d, -f1)
|
|
206
|
+
|
|
207
|
+
log "📊 Current disk usage: ${before_size}MB"
|
|
208
|
+
|
|
209
|
+
if [ "$before_size" -eq 0 ]; then
|
|
210
|
+
log "ℹ️ No error logs found to clean up"
|
|
211
|
+
return 0
|
|
212
|
+
fi
|
|
213
|
+
|
|
214
|
+
# Show size breakdown
|
|
215
|
+
local error_logs reports compressed other
|
|
216
|
+
IFS=, read -r error_logs reports compressed other <<< "$size_breakdown"
|
|
217
|
+
|
|
218
|
+
log "📁 Size breakdown:"
|
|
219
|
+
if [ "$error_logs" -gt 0 ]; then
|
|
220
|
+
log " Error logs: ${error_logs}MB"
|
|
221
|
+
fi
|
|
222
|
+
if [ "$reports" -gt 0 ]; then
|
|
223
|
+
log " Reports: ${reports}MB"
|
|
224
|
+
fi
|
|
225
|
+
if [ "$compressed" -gt 0 ]; then
|
|
226
|
+
log " Compressed: ${compressed}MB"
|
|
227
|
+
fi
|
|
228
|
+
if [ "$other" -gt 0 ]; then
|
|
229
|
+
log " Other: ${other}MB"
|
|
230
|
+
fi
|
|
231
|
+
|
|
232
|
+
# Compress old logs first
|
|
233
|
+
compress_old_logs
|
|
234
|
+
|
|
235
|
+
# List files to delete
|
|
236
|
+
local files_to_delete
|
|
237
|
+
files_to_delete=$(list_files_to_delete)
|
|
238
|
+
|
|
239
|
+
if [ -z "$files_to_delete" ]; then
|
|
240
|
+
log "ℹ️ No files older than $RETENTION_DAYS days found"
|
|
241
|
+
|
|
242
|
+
# Show final size after compression
|
|
243
|
+
local final_size
|
|
244
|
+
final_size=$(get_disk_usage)
|
|
245
|
+
|
|
246
|
+
if [ "$before_size" -ne "$final_size" ]; then
|
|
247
|
+
show_summary "$before_size" "$final_size" "0"
|
|
248
|
+
fi
|
|
249
|
+
|
|
250
|
+
cleanup_empty_dirs
|
|
251
|
+
return 0
|
|
252
|
+
fi
|
|
253
|
+
|
|
254
|
+
local file_count
|
|
255
|
+
file_count=$(echo "$files_to_delete" | wc -l)
|
|
256
|
+
|
|
257
|
+
log "📋 Found $file_count files older than $RETENTION_DAYS days:"
|
|
258
|
+
|
|
259
|
+
if [ "$DRY_RUN" = true ]; then
|
|
260
|
+
echo "$files_to_delete" | head -10 | while read -r file; do
|
|
261
|
+
log " Would delete: $(basename "$file") ($(du -sh "$file" 2>/dev/null | cut -f1 || echo "unknown"))"
|
|
262
|
+
done
|
|
263
|
+
|
|
264
|
+
if [ "$file_count" -gt 10 ]; then
|
|
265
|
+
log " ... and $((file_count - 10)) more files"
|
|
266
|
+
fi
|
|
267
|
+
|
|
268
|
+
log "🔍 DRY RUN - No files were actually deleted"
|
|
269
|
+
else
|
|
270
|
+
# Show sample of files to be deleted
|
|
271
|
+
echo "$files_to_delete" | head -5 | while read -r file; do
|
|
272
|
+
log " Deleting: $(basename "$file") ($(du -sh "$file" 2>/dev/null | cut -f1 || echo "unknown"))"
|
|
273
|
+
done
|
|
274
|
+
|
|
275
|
+
if [ "$file_count" -gt 5 ]; then
|
|
276
|
+
log " ... and $((file_count - 5)) more files"
|
|
277
|
+
fi
|
|
278
|
+
|
|
279
|
+
# Confirmation prompt (unless forced)
|
|
280
|
+
if [ "$FORCE" != true ]; then
|
|
281
|
+
echo ""
|
|
282
|
+
read -p "Delete these $file_count files? [y/N] " -n 1 -r
|
|
283
|
+
echo ""
|
|
284
|
+
|
|
285
|
+
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
|
|
286
|
+
log "❌ Cancelled by user"
|
|
287
|
+
return 0
|
|
288
|
+
fi
|
|
289
|
+
fi
|
|
290
|
+
|
|
291
|
+
# Delete files
|
|
292
|
+
local deleted_count=0
|
|
293
|
+
echo "$files_to_delete" | while read -r file; do
|
|
294
|
+
if rm -f "$file" 2>/dev/null; then
|
|
295
|
+
deleted_count=$((deleted_count + 1))
|
|
296
|
+
fi
|
|
297
|
+
done
|
|
298
|
+
|
|
299
|
+
log "✅ Deleted $deleted_count files"
|
|
300
|
+
fi
|
|
301
|
+
|
|
302
|
+
# Clean up empty directories
|
|
303
|
+
cleanup_empty_dirs
|
|
304
|
+
|
|
305
|
+
# Get final size
|
|
306
|
+
local after_size
|
|
307
|
+
after_size=$(get_disk_usage)
|
|
308
|
+
|
|
309
|
+
show_summary "$before_size" "$after_size" "$file_count"
|
|
310
|
+
|
|
311
|
+
log "✅ Cleanup completed"
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
# Check if running as root (warn about permission issues)
|
|
315
|
+
if [ "$EUID" -eq 0 ]; then
|
|
316
|
+
log "⚠️ Running as root - this may affect log ownership"
|
|
317
|
+
fi
|
|
318
|
+
|
|
319
|
+
# Check disk space before cleanup
|
|
320
|
+
local available_space
|
|
321
|
+
available_space=$(df "$LOG_BASE_DIR" 2>/dev/null | awk 'NR==2{print int($4/1024)}' || echo "0")
|
|
322
|
+
|
|
323
|
+
if [ "$available_space" -lt 50 ]; then
|
|
324
|
+
log "⚠️ Low disk space (${available_space}MB available) - forcing cleanup"
|
|
325
|
+
FORCE=true
|
|
326
|
+
fi
|
|
327
|
+
|
|
328
|
+
# Run cleanup
|
|
329
|
+
run_cleanup
|
|
330
|
+
|
|
331
|
+
# Log cleanup completion to system log if possible
|
|
332
|
+
if command -v logger >/dev/null 2>&1; then
|
|
333
|
+
logger -t "cfn-error-logging" "Cleanup completed: retention=${RETENTION_DAYS}d, deleted=${file_count:-0} files"
|
|
334
|
+
fi
|
|
@@ -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"
|