claude-flow-novice 2.15.1 → 2.15.3

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 (107) hide show
  1. package/.claude/cfn-extras/agents/google-sheets-specialist.md +614 -0
  2. package/.claude/commands/cfn/create-handoff.md +224 -0
  3. package/.claude/hooks/cfn-BACKUP_USAGE.md +243 -243
  4. package/.claude/hooks/cfn-invoke-security-validation.sh +69 -69
  5. package/.claude/hooks/cfn-post-edit-cfn-retrospective.sh +78 -78
  6. package/.claude/hooks/cfn-post-edit.config.json +44 -44
  7. package/.claude/skills/agent-lifecycle/SKILL.md +60 -0
  8. package/.claude/skills/agent-lifecycle/execute-lifecycle-hook.sh +573 -0
  9. package/.claude/skills/agent-lifecycle/simple-audit.sh +31 -0
  10. package/.claude/skills/cfn-hybrid-routing/check-dependencies.sh +51 -51
  11. package/.claude/skills/cfn-loop-validation/orchestrate-cfn-loop.sh +252 -252
  12. package/.claude/skills/cfn-redis-coordination/agent-recovery.sh +74 -74
  13. package/.claude/skills/cfn-redis-coordination/get-context.sh +112 -112
  14. package/.claude/skills/cfn-transparency-middleware/middleware-config.sh +28 -28
  15. package/.claude/skills/cfn-transparency-middleware/performance-benchmark.sh +78 -78
  16. package/.claude/skills/cfn-transparency-middleware/test-integration.sh +161 -161
  17. package/.claude/skills/cfn-transparency-middleware/test-transparency-skill.sh +367 -367
  18. package/.claude/skills/cfn-transparency-middleware/tests/input-validation.sh +92 -92
  19. package/.claude/skills/cfn-transparency-middleware/wrap-agent.sh +131 -131
  20. package/claude-assets/agents/cfn-dev-team/coordinators/handoff-coordinator.md +662 -0
  21. package/claude-assets/agents/cfn-dev-team/dev-ops/docker-specialist.md +29 -0
  22. package/claude-assets/cfn-extras/agents/google-sheets-specialist.md +614 -0
  23. package/claude-assets/commands/cfn/create-handoff.md +224 -0
  24. package/claude-assets/hooks/cfn-BACKUP_USAGE.md +243 -243
  25. package/claude-assets/hooks/cfn-invoke-security-validation.sh +69 -69
  26. package/claude-assets/hooks/cfn-post-edit-cfn-retrospective.sh +78 -78
  27. package/claude-assets/hooks/cfn-post-edit.config.json +44 -44
  28. package/claude-assets/hooks/cfn-post-execution/memory-cleanup.sh +19 -19
  29. package/claude-assets/hooks/cfn-pre-execution/memory-check.sh +19 -19
  30. package/claude-assets/skills/agent-lifecycle/execute-lifecycle-hook.sh +572 -572
  31. package/claude-assets/skills/agent-lifecycle/simple-audit.sh +30 -30
  32. package/claude-assets/skills/cfn-automatic-memory-persistence/persist-agent-output.sh +48 -48
  33. package/claude-assets/skills/cfn-automatic-memory-persistence/query-agent-history.sh +34 -34
  34. package/claude-assets/skills/cfn-deliverable-validation/confidence-calculator.sh +261 -261
  35. package/claude-assets/skills/cfn-expert-update/update-expert.sh +345 -345
  36. package/claude-assets/skills/cfn-hybrid-routing/check-dependencies.sh +51 -51
  37. package/claude-assets/skills/cfn-intervention-detector/detect-intervention.sh +110 -110
  38. package/claude-assets/skills/cfn-intervention-orchestrator/execute-intervention.sh +58 -58
  39. package/claude-assets/skills/cfn-loop-validation/orchestrate-cfn-loop.sh +252 -252
  40. package/claude-assets/skills/cfn-loop2-output-processing/process-validator-output.sh +275 -275
  41. package/claude-assets/skills/cfn-memory-management/check-memory.sh +159 -159
  42. package/claude-assets/skills/cfn-memory-management/cleanup-memory.sh +196 -196
  43. package/claude-assets/skills/cfn-node-heap-sizer/task-mode-heap-limiter.sh +325 -325
  44. package/claude-assets/skills/cfn-playbook-auto-update/auto-update-playbook.sh +85 -85
  45. package/claude-assets/skills/cfn-redis-coordination/agent-recovery.sh +74 -74
  46. package/claude-assets/skills/cfn-redis-coordination/get-context.sh +112 -112
  47. package/claude-assets/skills/cfn-scope-simplifier/simplify-scope.sh +67 -67
  48. package/claude-assets/skills/cfn-specialist-injection/recommend-specialist.sh +56 -56
  49. package/claude-assets/skills/cfn-standardized-error-handling/capture-agent-error.sh +86 -86
  50. package/claude-assets/skills/cfn-standardized-error-handling/test-error-handling.sh +165 -165
  51. package/claude-assets/skills/cfn-task-config-init/initialize-config.sh +264 -264
  52. package/claude-assets/skills/cfn-task-decomposition/task-decomposer.sh +278 -278
  53. package/claude-assets/skills/cfn-transparency-middleware/middleware-config.sh +28 -28
  54. package/claude-assets/skills/cfn-transparency-middleware/performance-benchmark.sh +78 -78
  55. package/claude-assets/skills/cfn-transparency-middleware/test-integration.sh +161 -161
  56. package/claude-assets/skills/cfn-transparency-middleware/test-transparency-skill.sh +367 -367
  57. package/claude-assets/skills/cfn-transparency-middleware/tests/input-validation.sh +92 -92
  58. package/claude-assets/skills/cfn-transparency-middleware/wrap-agent.sh +131 -131
  59. package/claude-assets/skills/docker-build/SKILL.md +96 -203
  60. package/claude-assets/skills/docker-build/build.sh +73 -73
  61. package/claude-assets/skills/integration/agent-handoff.sh +494 -0
  62. package/claude-assets/skills/integration/file-operations.sh +414 -0
  63. package/claude-assets/skills/workflow-codification/APPROVAL_WORKFLOW.md +806 -0
  64. package/claude-assets/skills/workflow-codification/COST_TRACKING.md +637 -0
  65. package/claude-assets/skills/workflow-codification/EDGE_CASE_TRACKING.md +404 -0
  66. package/claude-assets/skills/workflow-codification/README_PHASE4.md +457 -0
  67. package/claude-assets/skills/workflow-codification/SKILL.md +110 -0
  68. package/claude-assets/skills/workflow-codification/analyze-patterns.sh +899 -0
  69. package/claude-assets/skills/workflow-codification/approval-workflow.sh +514 -0
  70. package/claude-assets/skills/workflow-codification/generate-skill-update.sh +525 -0
  71. package/claude-assets/skills/workflow-codification/review-skill.sh +643 -0
  72. package/claude-assets/skills/workflow-codification/templates/email-notification.txt +114 -0
  73. package/claude-assets/skills/workflow-codification/templates/slack-notification.md +85 -0
  74. package/claude-assets/skills/workflow-codification/test-integration.sh +281 -0
  75. package/claude-assets/skills/workflow-codification/track-cost-savings.sh +445 -0
  76. package/claude-assets/skills/workflow-codification/track-edge-case.sh +323 -0
  77. package/dist/agents/agent-loader.js +165 -146
  78. package/dist/agents/agent-loader.js.map +1 -1
  79. package/dist/cli/config-manager.js +91 -109
  80. package/dist/cli/config-manager.js.map +1 -1
  81. package/dist/integration/DatabaseHandoff.js +507 -0
  82. package/dist/integration/DatabaseHandoff.js.map +1 -0
  83. package/dist/integration/StandardAdapter.js +291 -0
  84. package/dist/integration/StandardAdapter.js.map +1 -0
  85. package/dist/lib/agent-output-parser.js +518 -0
  86. package/dist/lib/agent-output-parser.js.map +1 -0
  87. package/dist/lib/agent-output-validator.js +950 -0
  88. package/dist/lib/agent-output-validator.js.map +1 -0
  89. package/dist/lib/artifact-registry.js +443 -0
  90. package/dist/lib/artifact-registry.js.map +1 -0
  91. package/dist/lib/config-validator.js +687 -0
  92. package/dist/lib/config-validator.js.map +1 -0
  93. package/dist/types/agent-output.js +44 -0
  94. package/dist/types/agent-output.js.map +1 -0
  95. package/dist/types/config.js +28 -0
  96. package/dist/types/config.js.map +1 -0
  97. package/package.json +2 -1
  98. package/scripts/artifact-cleanup.sh +392 -0
  99. package/scripts/build-linux.sh +78 -0
  100. package/scripts/deploy-production.sh +355 -355
  101. package/scripts/docker-playwright-fix.sh +311 -311
  102. package/scripts/docker-rebuild-all-agents.sh +127 -127
  103. package/scripts/memory-leak-prevention.sh +305 -305
  104. package/scripts/migrate-artifacts.sh +563 -0
  105. package/scripts/migrate-yaml-to-json.sh +465 -0
  106. package/scripts/run-marketing-tests.sh +42 -42
  107. package/scripts/update_paths.sh +46 -46
@@ -1,275 +1,275 @@
1
- #!/bin/bash
2
- set -euo pipefail
3
-
4
- # Loop 2 Output Processing: Enhanced Validator Output Processing
5
- # BUG #27 FIX: Enforce structured output template and reject defaults
6
- # BUG #30 FIX: Add context sanitization and environment validation
7
-
8
- SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
9
-
10
- # Parse arguments (SAME AS execute-and-extract.sh)
11
- AGENT_TYPE=""
12
- TASK_ID=""
13
- AGENT_ID=""
14
- CONTEXT=""
15
- ITERATION=1
16
- TIMEOUT=900
17
-
18
- while [[ $# -gt 0 ]]; do
19
- case $1 in
20
- --agent-type) AGENT_TYPE="$2"; shift 2 ;;
21
- --task-id) TASK_ID="$2"; shift 2 ;;
22
- --agent-id) AGENT_ID="$2"; shift 2 ;;
23
- --context) CONTEXT="$2"; shift 2 ;;
24
- --iteration) ITERATION="$2"; shift 2 ;;
25
- --timeout) TIMEOUT="$2"; shift 2 ;;
26
- *) echo "ERROR: Unknown parameter: $1" >&2; exit 1 ;;
27
- esac
28
- done
29
-
30
- # Validate required parameters
31
- if [ -z "$AGENT_TYPE" ] || [ -z "$TASK_ID" ] || [ -z "$AGENT_ID" ] || [ -z "$CONTEXT" ]; then
32
- echo "ERROR: Missing required parameters" >&2
33
- echo "Usage: $0 --agent-type TYPE --task-id ID --agent-id ID --context CONTEXT [--iteration N] [--timeout SECONDS]" >&2
34
- exit 1
35
- fi
36
-
37
- # BUG #30 FIX: Context sanitization function
38
- # Sanitizes context by removing dangerous characters and validating JSON structure
39
- validate_and_sanitize_context() {
40
- local context="$1"
41
- local timestamp=$(date -u +%Y-%m-%dT%H:%M:%SZ)
42
-
43
- echo "[Validator] [$timestamp] Sanitizing context (length: ${#context} chars)" >&2
44
- echo "[Validator] [$timestamp] Context preview: ${context:0:100}..." >&2
45
-
46
- # Strip dangerous characters: null bytes, backticks, $, backslash
47
- # Also strip control characters and other special chars that can break shell parsing
48
- local sanitized=$(echo "$context" | tr -d '\0' | sed 's/[`$\\]//g' | sed 's/[[:cntrl:]]/ /g')
49
-
50
- # Log sanitization results
51
- local removed_chars=$((${#context} - ${#sanitized}))
52
- if [ "$removed_chars" -gt 0 ]; then
53
- echo "[Validator] [$timestamp] Sanitized context: removed $removed_chars dangerous characters" >&2
54
- else
55
- echo "[Validator] [$timestamp] No dangerous characters found in context" >&2
56
- fi
57
-
58
- # Validate that context is not empty after sanitization
59
- if [ -z "$sanitized" ]; then
60
- echo "ERROR: Context became empty after sanitization" >&2
61
- return 1
62
- fi
63
-
64
- echo "$sanitized"
65
- }
66
-
67
- # BUG #30 FIX: Environment validation function
68
- # Validates that required environment variables exist before spawning agents
69
- validate_environment() {
70
- local timestamp=$(date -u +%Y-%m-%dT%H:%M:%SZ)
71
- local missing_vars=()
72
-
73
- echo "[Validator] [$timestamp] Validating environment variables" >&2
74
-
75
- # Check required variables
76
- if [ -z "${REDIS_HOST:-}" ]; then
77
- missing_vars+=("REDIS_HOST")
78
- fi
79
-
80
- if [ -z "${HOME:-}" ]; then
81
- missing_vars+=("HOME")
82
- fi
83
-
84
- if [ -z "${PATH:-}" ]; then
85
- missing_vars+=("PATH")
86
- fi
87
-
88
- # Log validation results
89
- if [ ${#missing_vars[@]} -gt 0 ]; then
90
- echo "ERROR: Missing required environment variables: ${missing_vars[*]}" >&2
91
- echo "[Validator] [$timestamp] Current environment state:" >&2
92
- echo "[Validator] REDIS_HOST=${REDIS_HOST:-<not set>}" >&2
93
- echo "[Validator] HOME=${HOME:-<not set>}" >&2
94
- echo "[Validator] PATH=${PATH:-<not set>}" >&2
95
- return 1
96
- fi
97
-
98
- echo "[Validator] [$timestamp] Environment validation passed" >&2
99
- echo "[Validator] REDIS_HOST=${REDIS_HOST}" >&2
100
- echo "[Validator] HOME=${HOME}" >&2
101
- echo "[Validator] PATH=${PATH:0:100}..." >&2
102
-
103
- return 0
104
- }
105
-
106
- # BUG #27 FIX: Add structured output template to context
107
- ENHANCED_CONTEXT="$CONTEXT
108
-
109
- **REQUIRED OUTPUT FORMAT:**
110
-
111
- You MUST structure your validation output as follows:
112
-
113
- ## Validation Confidence: [0.00-1.00]
114
-
115
- ### CRITICAL Issues
116
- - [List any critical issues that must be fixed]
117
- - [Use bullet points with dash prefix]
118
-
119
- ### WARNING Issues
120
- - [List warnings that should be addressed]
121
- - [Each on a new line with dash prefix]
122
-
123
- ### SUGGESTION Items
124
- - [List improvement suggestions]
125
- - [Optional enhancements]
126
-
127
- **Example:**
128
- ## Validation Confidence: 0.87
129
-
130
- ### CRITICAL Issues
131
- - Missing error handling in invoke-gate-ack.sh:88
132
- - Security vulnerability in input validation
133
-
134
- ### WARNING Issues
135
- - Inconsistent naming convention in test file
136
- - Missing JSDoc comments
137
-
138
- ### SUGGESTION Items
139
- - Consider adding retry backoff strategy
140
- - Could use Promise.all for parallel operations
141
-
142
- **Important:**
143
- - Confidence MUST be explicit numeric value (0.00-1.00)
144
- - Categorize ALL feedback items by severity
145
- - If no issues found, state \"No issues found\" under each category
146
- - Do NOT use default confidence scores without justification
147
- "
148
-
149
- # BUG #30 FIX: Validate environment before spawning
150
- if ! validate_environment; then
151
- echo "ERROR: Environment validation failed, cannot spawn validator agent" >&2
152
- exit 1
153
- fi
154
-
155
- # BUG #30 FIX: Sanitize context before passing to agent
156
- SANITIZED_CONTEXT=$(validate_and_sanitize_context "$ENHANCED_CONTEXT")
157
- if [ $? -ne 0 ]; then
158
- echo "ERROR: Context sanitization failed" >&2
159
- exit 1
160
- fi
161
-
162
- # BUG #30 FIX: Enhanced agent spawning with controlled environment and error capture
163
- TIMESTAMP=$(date -u +%Y-%m-%dT%H:%M:%SZ)
164
- echo "[Validator] [$TIMESTAMP] Spawning $AGENT_TYPE with sanitized context" >&2
165
- echo "[Validator] [$TIMESTAMP] Sanitized context length: ${#SANITIZED_CONTEXT} chars" >&2
166
-
167
- # Capture both stdout and stderr separately, plus exit code
168
- SPAWN_TMP_OUT=$(mktemp)
169
- SPAWN_TMP_ERR=$(mktemp)
170
- EXIT_CODE=0
171
-
172
- # Use env -i for controlled environment with explicit variable passing
173
- env -i \
174
- HOME="$HOME" \
175
- PATH="$PATH" \
176
- REDIS_HOST="$REDIS_HOST" \
177
- timeout "$TIMEOUT" npx claude-flow-novice agent "$AGENT_TYPE" \
178
- --task-id "$TASK_ID" \
179
- --agent-id "$AGENT_ID" \
180
- --context "$SANITIZED_CONTEXT" \
181
- > "$SPAWN_TMP_OUT" 2> "$SPAWN_TMP_ERR" || EXIT_CODE=$?
182
-
183
- AGENT_OUTPUT=$(cat "$SPAWN_TMP_OUT")
184
- AGENT_STDERR=$(cat "$SPAWN_TMP_ERR")
185
-
186
- # BUG #30 FIX: Enhanced error logging on spawn failure
187
- if [ $EXIT_CODE -ne 0 ]; then
188
- TIMESTAMP=$(date -u +%Y-%m-%dT%H:%M:%SZ)
189
- echo "[Validator] [$TIMESTAMP] ⚠️ Agent spawn failed with exit code: $EXIT_CODE" >&2
190
- echo "[Validator] [$TIMESTAMP] Agent STDOUT (length: ${#AGENT_OUTPUT}):" >&2
191
- echo "$AGENT_OUTPUT" >&2
192
- echo "[Validator] [$TIMESTAMP] Agent STDERR (length: ${#AGENT_STDERR}):" >&2
193
- echo "$AGENT_STDERR" >&2
194
- echo "[Validator] [$TIMESTAMP] Context used (first 200 chars): ${SANITIZED_CONTEXT:0:200}..." >&2
195
- fi
196
-
197
- # Cleanup temp files
198
- rm -f "$SPAWN_TMP_OUT" "$SPAWN_TMP_ERR"
199
-
200
- # Log successful spawn
201
- if [ $EXIT_CODE -eq 0 ]; then
202
- TIMESTAMP=$(date -u +%Y-%m-%dT%H:%M:%SZ)
203
- echo "[Validator] [$TIMESTAMP] Agent spawn successful (output length: ${#AGENT_OUTPUT} chars)" >&2
204
- fi
205
-
206
- # Parse confidence using multi-pattern detection
207
- CONFIDENCE=$("$SCRIPT_DIR/parse-feedback.sh" --extract-confidence "$AGENT_OUTPUT" 2>/dev/null || echo "0.0")
208
-
209
- # BUG #27 FIX: Enhanced confidence validation
210
- CONFIDENCE_SOURCE="unknown"
211
- if [ -z "$CONFIDENCE" ] || [ "$CONFIDENCE" = "null" ] || (( $(echo "$CONFIDENCE == 0.0" | bc -l) )); then
212
- CONFIDENCE=0.70
213
- CONFIDENCE_SOURCE="default-fallback"
214
- echo "[Validator] WARNING: No confidence found in output, using default 0.70" >&2
215
- elif (( $(echo "$CONFIDENCE == 0.70" | bc -l) )); then
216
- # Check if this is explicit 0.70 or default
217
- if echo "$AGENT_OUTPUT" | grep -qE "confidence:?\s*0?\.70"; then
218
- CONFIDENCE_SOURCE="explicit-0.70"
219
- else
220
- CONFIDENCE_SOURCE="default-fallback"
221
- echo "[Validator] WARNING: Confidence defaulted to 0.70 (no explicit score found)" >&2
222
- fi
223
- else
224
- CONFIDENCE_SOURCE="explicit"
225
- fi
226
-
227
- # Parse feedback from output
228
- FEEDBACK_RAW=$("$SCRIPT_DIR/parse-feedback.sh" --extract-feedback "$AGENT_OUTPUT" 2>/dev/null || echo '{"critical":[],"warnings":[],"suggestions":[]}')
229
-
230
- # Ensure feedback is valid JSON
231
- if ! echo "$FEEDBACK_RAW" | jq empty 2>/dev/null; then
232
- echo "[Validator] WARNING: Invalid feedback JSON, using default" >&2
233
- FEEDBACK_RAW='{"critical":[],"warnings":[],"suggestions":[]}'
234
- fi
235
-
236
- # BUG #27 FIX: Count feedback items
237
- CRITICAL_COUNT=$(echo "$FEEDBACK_RAW" | jq '.critical | length')
238
- WARNING_COUNT=$(echo "$FEEDBACK_RAW" | jq '.warnings | length')
239
- SUGGESTION_COUNT=$(echo "$FEEDBACK_RAW" | jq '.suggestions | length')
240
- TOTAL_FEEDBACK=$((CRITICAL_COUNT + WARNING_COUNT + SUGGESTION_COUNT))
241
-
242
- # BUG #27 FIX: Detect default output pattern (0.70 confidence + zero feedback)
243
- if (( $(echo "$CONFIDENCE == 0.70" | bc -l) )) && [ "$TOTAL_FEEDBACK" -eq 0 ]; then
244
- echo "[Validator] ⚠️ WARNING: Validator produced default output (0.70 confidence, 0 feedback items)" >&2
245
- echo "[Validator] This may indicate the validator didn't properly analyze the code" >&2
246
- VALIDATION_WARNING="default-output-detected"
247
- else
248
- VALIDATION_WARNING="none"
249
- fi
250
-
251
- # BUG #27 FIX: If feedback exists but confidence is default, log warning
252
- if [ "$TOTAL_FEEDBACK" -gt 0 ] && (( $(echo "$CONFIDENCE == 0.70" | bc -l) )) && [ "$CONFIDENCE_SOURCE" = "default-fallback" ]; then
253
- echo "[Validator] ⚠️ WARNING: Feedback found ($TOTAL_FEEDBACK items) but confidence defaulted to 0.70" >&2
254
- echo "[Validator] Validator may not be using structured output format" >&2
255
- fi
256
-
257
- # Build output JSON with enhanced metadata
258
- cat <<EOF
259
- {
260
- "agent_id": "$AGENT_ID",
261
- "agent_type": "$AGENT_TYPE",
262
- "confidence": $CONFIDENCE,
263
- "confidence_source": "$CONFIDENCE_SOURCE",
264
- "feedback": $FEEDBACK_RAW,
265
- "feedback_counts": {
266
- "critical": $CRITICAL_COUNT,
267
- "warnings": $WARNING_COUNT,
268
- "suggestions": $SUGGESTION_COUNT,
269
- "total": $TOTAL_FEEDBACK
270
- },
271
- "validation_warning": "$VALIDATION_WARNING",
272
- "iteration": $ITERATION,
273
- "timestamp": "$(date -u +%Y-%m-%dT%H:%M:%SZ)"
274
- }
275
- EOF
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+
4
+ # Loop 2 Output Processing: Enhanced Validator Output Processing
5
+ # BUG #27 FIX: Enforce structured output template and reject defaults
6
+ # BUG #30 FIX: Add context sanitization and environment validation
7
+
8
+ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
9
+
10
+ # Parse arguments (SAME AS execute-and-extract.sh)
11
+ AGENT_TYPE=""
12
+ TASK_ID=""
13
+ AGENT_ID=""
14
+ CONTEXT=""
15
+ ITERATION=1
16
+ TIMEOUT=900
17
+
18
+ while [[ $# -gt 0 ]]; do
19
+ case $1 in
20
+ --agent-type) AGENT_TYPE="$2"; shift 2 ;;
21
+ --task-id) TASK_ID="$2"; shift 2 ;;
22
+ --agent-id) AGENT_ID="$2"; shift 2 ;;
23
+ --context) CONTEXT="$2"; shift 2 ;;
24
+ --iteration) ITERATION="$2"; shift 2 ;;
25
+ --timeout) TIMEOUT="$2"; shift 2 ;;
26
+ *) echo "ERROR: Unknown parameter: $1" >&2; exit 1 ;;
27
+ esac
28
+ done
29
+
30
+ # Validate required parameters
31
+ if [ -z "$AGENT_TYPE" ] || [ -z "$TASK_ID" ] || [ -z "$AGENT_ID" ] || [ -z "$CONTEXT" ]; then
32
+ echo "ERROR: Missing required parameters" >&2
33
+ echo "Usage: $0 --agent-type TYPE --task-id ID --agent-id ID --context CONTEXT [--iteration N] [--timeout SECONDS]" >&2
34
+ exit 1
35
+ fi
36
+
37
+ # BUG #30 FIX: Context sanitization function
38
+ # Sanitizes context by removing dangerous characters and validating JSON structure
39
+ validate_and_sanitize_context() {
40
+ local context="$1"
41
+ local timestamp=$(date -u +%Y-%m-%dT%H:%M:%SZ)
42
+
43
+ echo "[Validator] [$timestamp] Sanitizing context (length: ${#context} chars)" >&2
44
+ echo "[Validator] [$timestamp] Context preview: ${context:0:100}..." >&2
45
+
46
+ # Strip dangerous characters: null bytes, backticks, $, backslash
47
+ # Also strip control characters and other special chars that can break shell parsing
48
+ local sanitized=$(echo "$context" | tr -d '\0' | sed 's/[`$\\]//g' | sed 's/[[:cntrl:]]/ /g')
49
+
50
+ # Log sanitization results
51
+ local removed_chars=$((${#context} - ${#sanitized}))
52
+ if [ "$removed_chars" -gt 0 ]; then
53
+ echo "[Validator] [$timestamp] Sanitized context: removed $removed_chars dangerous characters" >&2
54
+ else
55
+ echo "[Validator] [$timestamp] No dangerous characters found in context" >&2
56
+ fi
57
+
58
+ # Validate that context is not empty after sanitization
59
+ if [ -z "$sanitized" ]; then
60
+ echo "ERROR: Context became empty after sanitization" >&2
61
+ return 1
62
+ fi
63
+
64
+ echo "$sanitized"
65
+ }
66
+
67
+ # BUG #30 FIX: Environment validation function
68
+ # Validates that required environment variables exist before spawning agents
69
+ validate_environment() {
70
+ local timestamp=$(date -u +%Y-%m-%dT%H:%M:%SZ)
71
+ local missing_vars=()
72
+
73
+ echo "[Validator] [$timestamp] Validating environment variables" >&2
74
+
75
+ # Check required variables
76
+ if [ -z "${REDIS_HOST:-}" ]; then
77
+ missing_vars+=("REDIS_HOST")
78
+ fi
79
+
80
+ if [ -z "${HOME:-}" ]; then
81
+ missing_vars+=("HOME")
82
+ fi
83
+
84
+ if [ -z "${PATH:-}" ]; then
85
+ missing_vars+=("PATH")
86
+ fi
87
+
88
+ # Log validation results
89
+ if [ ${#missing_vars[@]} -gt 0 ]; then
90
+ echo "ERROR: Missing required environment variables: ${missing_vars[*]}" >&2
91
+ echo "[Validator] [$timestamp] Current environment state:" >&2
92
+ echo "[Validator] REDIS_HOST=${REDIS_HOST:-<not set>}" >&2
93
+ echo "[Validator] HOME=${HOME:-<not set>}" >&2
94
+ echo "[Validator] PATH=${PATH:-<not set>}" >&2
95
+ return 1
96
+ fi
97
+
98
+ echo "[Validator] [$timestamp] Environment validation passed" >&2
99
+ echo "[Validator] REDIS_HOST=${REDIS_HOST}" >&2
100
+ echo "[Validator] HOME=${HOME}" >&2
101
+ echo "[Validator] PATH=${PATH:0:100}..." >&2
102
+
103
+ return 0
104
+ }
105
+
106
+ # BUG #27 FIX: Add structured output template to context
107
+ ENHANCED_CONTEXT="$CONTEXT
108
+
109
+ **REQUIRED OUTPUT FORMAT:**
110
+
111
+ You MUST structure your validation output as follows:
112
+
113
+ ## Validation Confidence: [0.00-1.00]
114
+
115
+ ### CRITICAL Issues
116
+ - [List any critical issues that must be fixed]
117
+ - [Use bullet points with dash prefix]
118
+
119
+ ### WARNING Issues
120
+ - [List warnings that should be addressed]
121
+ - [Each on a new line with dash prefix]
122
+
123
+ ### SUGGESTION Items
124
+ - [List improvement suggestions]
125
+ - [Optional enhancements]
126
+
127
+ **Example:**
128
+ ## Validation Confidence: 0.87
129
+
130
+ ### CRITICAL Issues
131
+ - Missing error handling in invoke-gate-ack.sh:88
132
+ - Security vulnerability in input validation
133
+
134
+ ### WARNING Issues
135
+ - Inconsistent naming convention in test file
136
+ - Missing JSDoc comments
137
+
138
+ ### SUGGESTION Items
139
+ - Consider adding retry backoff strategy
140
+ - Could use Promise.all for parallel operations
141
+
142
+ **Important:**
143
+ - Confidence MUST be explicit numeric value (0.00-1.00)
144
+ - Categorize ALL feedback items by severity
145
+ - If no issues found, state \"No issues found\" under each category
146
+ - Do NOT use default confidence scores without justification
147
+ "
148
+
149
+ # BUG #30 FIX: Validate environment before spawning
150
+ if ! validate_environment; then
151
+ echo "ERROR: Environment validation failed, cannot spawn validator agent" >&2
152
+ exit 1
153
+ fi
154
+
155
+ # BUG #30 FIX: Sanitize context before passing to agent
156
+ SANITIZED_CONTEXT=$(validate_and_sanitize_context "$ENHANCED_CONTEXT")
157
+ if [ $? -ne 0 ]; then
158
+ echo "ERROR: Context sanitization failed" >&2
159
+ exit 1
160
+ fi
161
+
162
+ # BUG #30 FIX: Enhanced agent spawning with controlled environment and error capture
163
+ TIMESTAMP=$(date -u +%Y-%m-%dT%H:%M:%SZ)
164
+ echo "[Validator] [$TIMESTAMP] Spawning $AGENT_TYPE with sanitized context" >&2
165
+ echo "[Validator] [$TIMESTAMP] Sanitized context length: ${#SANITIZED_CONTEXT} chars" >&2
166
+
167
+ # Capture both stdout and stderr separately, plus exit code
168
+ SPAWN_TMP_OUT=$(mktemp)
169
+ SPAWN_TMP_ERR=$(mktemp)
170
+ EXIT_CODE=0
171
+
172
+ # Use env -i for controlled environment with explicit variable passing
173
+ env -i \
174
+ HOME="$HOME" \
175
+ PATH="$PATH" \
176
+ REDIS_HOST="$REDIS_HOST" \
177
+ timeout "$TIMEOUT" npx claude-flow-novice agent "$AGENT_TYPE" \
178
+ --task-id "$TASK_ID" \
179
+ --agent-id "$AGENT_ID" \
180
+ --context "$SANITIZED_CONTEXT" \
181
+ > "$SPAWN_TMP_OUT" 2> "$SPAWN_TMP_ERR" || EXIT_CODE=$?
182
+
183
+ AGENT_OUTPUT=$(cat "$SPAWN_TMP_OUT")
184
+ AGENT_STDERR=$(cat "$SPAWN_TMP_ERR")
185
+
186
+ # BUG #30 FIX: Enhanced error logging on spawn failure
187
+ if [ $EXIT_CODE -ne 0 ]; then
188
+ TIMESTAMP=$(date -u +%Y-%m-%dT%H:%M:%SZ)
189
+ echo "[Validator] [$TIMESTAMP] ⚠️ Agent spawn failed with exit code: $EXIT_CODE" >&2
190
+ echo "[Validator] [$TIMESTAMP] Agent STDOUT (length: ${#AGENT_OUTPUT}):" >&2
191
+ echo "$AGENT_OUTPUT" >&2
192
+ echo "[Validator] [$TIMESTAMP] Agent STDERR (length: ${#AGENT_STDERR}):" >&2
193
+ echo "$AGENT_STDERR" >&2
194
+ echo "[Validator] [$TIMESTAMP] Context used (first 200 chars): ${SANITIZED_CONTEXT:0:200}..." >&2
195
+ fi
196
+
197
+ # Cleanup temp files
198
+ rm -f "$SPAWN_TMP_OUT" "$SPAWN_TMP_ERR"
199
+
200
+ # Log successful spawn
201
+ if [ $EXIT_CODE -eq 0 ]; then
202
+ TIMESTAMP=$(date -u +%Y-%m-%dT%H:%M:%SZ)
203
+ echo "[Validator] [$TIMESTAMP] Agent spawn successful (output length: ${#AGENT_OUTPUT} chars)" >&2
204
+ fi
205
+
206
+ # Parse confidence using multi-pattern detection
207
+ CONFIDENCE=$("$SCRIPT_DIR/parse-feedback.sh" --extract-confidence "$AGENT_OUTPUT" 2>/dev/null || echo "0.0")
208
+
209
+ # BUG #27 FIX: Enhanced confidence validation
210
+ CONFIDENCE_SOURCE="unknown"
211
+ if [ -z "$CONFIDENCE" ] || [ "$CONFIDENCE" = "null" ] || (( $(echo "$CONFIDENCE == 0.0" | bc -l) )); then
212
+ CONFIDENCE=0.70
213
+ CONFIDENCE_SOURCE="default-fallback"
214
+ echo "[Validator] WARNING: No confidence found in output, using default 0.70" >&2
215
+ elif (( $(echo "$CONFIDENCE == 0.70" | bc -l) )); then
216
+ # Check if this is explicit 0.70 or default
217
+ if echo "$AGENT_OUTPUT" | grep -qE "confidence:?\s*0?\.70"; then
218
+ CONFIDENCE_SOURCE="explicit-0.70"
219
+ else
220
+ CONFIDENCE_SOURCE="default-fallback"
221
+ echo "[Validator] WARNING: Confidence defaulted to 0.70 (no explicit score found)" >&2
222
+ fi
223
+ else
224
+ CONFIDENCE_SOURCE="explicit"
225
+ fi
226
+
227
+ # Parse feedback from output
228
+ FEEDBACK_RAW=$("$SCRIPT_DIR/parse-feedback.sh" --extract-feedback "$AGENT_OUTPUT" 2>/dev/null || echo '{"critical":[],"warnings":[],"suggestions":[]}')
229
+
230
+ # Ensure feedback is valid JSON
231
+ if ! echo "$FEEDBACK_RAW" | jq empty 2>/dev/null; then
232
+ echo "[Validator] WARNING: Invalid feedback JSON, using default" >&2
233
+ FEEDBACK_RAW='{"critical":[],"warnings":[],"suggestions":[]}'
234
+ fi
235
+
236
+ # BUG #27 FIX: Count feedback items
237
+ CRITICAL_COUNT=$(echo "$FEEDBACK_RAW" | jq '.critical | length')
238
+ WARNING_COUNT=$(echo "$FEEDBACK_RAW" | jq '.warnings | length')
239
+ SUGGESTION_COUNT=$(echo "$FEEDBACK_RAW" | jq '.suggestions | length')
240
+ TOTAL_FEEDBACK=$((CRITICAL_COUNT + WARNING_COUNT + SUGGESTION_COUNT))
241
+
242
+ # BUG #27 FIX: Detect default output pattern (0.70 confidence + zero feedback)
243
+ if (( $(echo "$CONFIDENCE == 0.70" | bc -l) )) && [ "$TOTAL_FEEDBACK" -eq 0 ]; then
244
+ echo "[Validator] ⚠️ WARNING: Validator produced default output (0.70 confidence, 0 feedback items)" >&2
245
+ echo "[Validator] This may indicate the validator didn't properly analyze the code" >&2
246
+ VALIDATION_WARNING="default-output-detected"
247
+ else
248
+ VALIDATION_WARNING="none"
249
+ fi
250
+
251
+ # BUG #27 FIX: If feedback exists but confidence is default, log warning
252
+ if [ "$TOTAL_FEEDBACK" -gt 0 ] && (( $(echo "$CONFIDENCE == 0.70" | bc -l) )) && [ "$CONFIDENCE_SOURCE" = "default-fallback" ]; then
253
+ echo "[Validator] ⚠️ WARNING: Feedback found ($TOTAL_FEEDBACK items) but confidence defaulted to 0.70" >&2
254
+ echo "[Validator] Validator may not be using structured output format" >&2
255
+ fi
256
+
257
+ # Build output JSON with enhanced metadata
258
+ cat <<EOF
259
+ {
260
+ "agent_id": "$AGENT_ID",
261
+ "agent_type": "$AGENT_TYPE",
262
+ "confidence": $CONFIDENCE,
263
+ "confidence_source": "$CONFIDENCE_SOURCE",
264
+ "feedback": $FEEDBACK_RAW,
265
+ "feedback_counts": {
266
+ "critical": $CRITICAL_COUNT,
267
+ "warnings": $WARNING_COUNT,
268
+ "suggestions": $SUGGESTION_COUNT,
269
+ "total": $TOTAL_FEEDBACK
270
+ },
271
+ "validation_warning": "$VALIDATION_WARNING",
272
+ "iteration": $ITERATION,
273
+ "timestamp": "$(date -u +%Y-%m-%dT%H:%M:%SZ)"
274
+ }
275
+ EOF