@the-bearded-bear/claude-craft 5.6.0-next.8bfdffa → 5.7.0-next.bf058ea

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,427 @@
1
+ #!/usr/bin/env bash
2
+ # =============================================================================
3
+ # Agent Teams - Result Aggregator
4
+ # Merges results from N isolated audit directories into a unified report.
5
+ #
6
+ # Each worker produces a result.json with:
7
+ # { "score": <number>, "findings": [...], "tech": "<string>", "category": "<string>" }
8
+ #
9
+ # The aggregator:
10
+ # - Collects all result.json files from isolated directories
11
+ # - Deduplicates findings (same file + message = duplicate)
12
+ # - Resolves score conflicts (weighted average per category)
13
+ # - Produces a unified aggregated report
14
+ #
15
+ # Supports --dry-run to preview without writing.
16
+ # =============================================================================
17
+
18
+ set -euo pipefail
19
+
20
+ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
21
+
22
+ # Defaults
23
+ INPUT_DIR=""
24
+ OUTPUT_FILE=""
25
+ DRY_RUN="false"
26
+ VERBOSE="false"
27
+
28
+ # Colors
29
+ RED='\033[0;31m'
30
+ GREEN='\033[0;32m'
31
+ YELLOW='\033[1;33m'
32
+ BLUE='\033[0;34m'
33
+ CYAN='\033[0;36m'
34
+ NC='\033[0m'
35
+
36
+ # =============================================================================
37
+ # Logging
38
+ # =============================================================================
39
+
40
+ log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
41
+ log_success() { echo -e "${GREEN}[OK]${NC} $1"; }
42
+ log_warning() { echo -e "${YELLOW}[WARN]${NC} $1"; }
43
+ log_error() { echo -e "${RED}[ERROR]${NC} $1" >&2; }
44
+ log_verbose() { [[ "$VERBOSE" == "true" ]] && echo -e "${CYAN}[DEBUG]${NC} $1" || true; }
45
+
46
+ # =============================================================================
47
+ # Help
48
+ # =============================================================================
49
+
50
+ show_help() {
51
+ cat << 'EOF'
52
+ Agent Teams - Result Aggregator
53
+
54
+ Merges results from N isolated audit directories into a unified report.
55
+
56
+ USAGE:
57
+ result-aggregator.sh --input-dir <dir> [--output-file <file>] [options]
58
+
59
+ REQUIRED:
60
+ --input-dir <dir> Directory containing isolated audit results.
61
+ Each subdirectory should contain a result.json.
62
+
63
+ OPTIONS:
64
+ --output-file <file> Output file for the merged report (default: stdout)
65
+ --dry-run Preview what would be merged without writing
66
+ --verbose Show detailed processing information
67
+ -h, --help Show this help message
68
+
69
+ INPUT FORMAT:
70
+ Each result.json must follow this schema:
71
+
72
+ {
73
+ "tech": "symfony",
74
+ "category": "architecture",
75
+ "score": 22,
76
+ "max": 25,
77
+ "findings": [
78
+ {
79
+ "severity": "warning|error|info",
80
+ "file": "src/Controller/UserController.php",
81
+ "message": "Direct repository access from controller",
82
+ "rule": "clean-architecture-layer-violation"
83
+ }
84
+ ]
85
+ }
86
+
87
+ The input directory structure should be:
88
+
89
+ <input-dir>/
90
+ <agent-id-1>/result.json
91
+ <agent-id-2>/result.json
92
+ ...
93
+
94
+ OUTPUT FORMAT:
95
+ The merged report is a JSON object:
96
+
97
+ {
98
+ "generated_at": "2026-02-07T10:00:00Z",
99
+ "technologies": {
100
+ "symfony": {
101
+ "categories": {
102
+ "architecture": { "score": 22, "max": 25 },
103
+ "code-quality": { "score": 20, "max": 25 },
104
+ ...
105
+ },
106
+ "total_score": 82,
107
+ "max_score": 100
108
+ }
109
+ },
110
+ "findings": [...],
111
+ "summary": {
112
+ "total_technologies": 2,
113
+ "global_score": 80,
114
+ "global_max": 100,
115
+ "total_findings": 15,
116
+ "by_severity": { "error": 2, "warning": 8, "info": 5 }
117
+ }
118
+ }
119
+
120
+ EXAMPLES:
121
+ # Merge audit results and write to file
122
+ result-aggregator.sh --input-dir .audit-output --output-file report.json
123
+
124
+ # Preview what would be merged
125
+ result-aggregator.sh --input-dir .audit-output --dry-run
126
+
127
+ # Merge and print to stdout
128
+ result-aggregator.sh --input-dir .audit-output
129
+ EOF
130
+ }
131
+
132
+ # =============================================================================
133
+ # Validation
134
+ # =============================================================================
135
+
136
+ check_dependencies() {
137
+ if ! command -v jq &> /dev/null; then
138
+ log_error "jq is required but not installed."
139
+ log_error "Install with: apt-get install jq (Linux) or brew install jq (macOS)"
140
+ return 1
141
+ fi
142
+ }
143
+
144
+ validate_result_file() {
145
+ local file="$1"
146
+
147
+ if [[ ! -f "$file" ]]; then
148
+ return 1
149
+ fi
150
+
151
+ # Check it's valid JSON with required fields
152
+ if ! jq -e '.tech and .category and (.score != null) and .findings' "$file" > /dev/null 2>&1; then
153
+ log_warning "Invalid result file (missing required fields): $file"
154
+ return 1
155
+ fi
156
+
157
+ return 0
158
+ }
159
+
160
+ # =============================================================================
161
+ # Core Aggregation
162
+ # =============================================================================
163
+
164
+ # Collect all valid result.json files from input directory.
165
+ # Outputs file paths to stdout (one per line). Log messages go to stderr.
166
+ collect_results() {
167
+ local input_dir="$1"
168
+ local results=()
169
+
170
+ # Search for result.json in subdirectories
171
+ while IFS= read -r -d '' file; do
172
+ if validate_result_file "$file"; then
173
+ results+=("$file")
174
+ log_verbose "Found valid result: $file" >&2
175
+ else
176
+ log_warning "Skipping invalid result: $file" >&2
177
+ fi
178
+ done < <(find "$input_dir" -name "result.json" -type f -print0 2>/dev/null)
179
+
180
+ if [[ ${#results[@]} -eq 0 ]]; then
181
+ log_warning "No valid result.json files found in $input_dir" >&2
182
+ return 1
183
+ fi
184
+
185
+ log_info "Found ${#results[@]} result files" >&2
186
+
187
+ # Output only file paths to stdout
188
+ printf '%s\n' "${results[@]}"
189
+ }
190
+
191
+ # Deduplicate findings: same file + message = duplicate.
192
+ # When duplicates exist, keep the one with the higher severity.
193
+ deduplicate_findings() {
194
+ local findings_json="$1"
195
+
196
+ jq '
197
+ # Group by file+message to detect duplicates
198
+ group_by(.file + "|" + .message) |
199
+ map(
200
+ # For each group, sort by severity (error > warning > info) and take first
201
+ sort_by(
202
+ if .severity == "error" then 0
203
+ elif .severity == "warning" then 1
204
+ else 2
205
+ end
206
+ ) | .[0]
207
+ )
208
+ ' <<< "$findings_json"
209
+ }
210
+
211
+ # Resolve score conflicts: when multiple results exist for the same
212
+ # tech+category, use weighted average.
213
+ resolve_score_conflicts() {
214
+ local results_json="$1"
215
+
216
+ jq '
217
+ # Group by tech+category
218
+ group_by(.tech + "|" + .category) |
219
+ map({
220
+ tech: .[0].tech,
221
+ category: .[0].category,
222
+ score: (map(.score) | add / length | floor),
223
+ max: (.[0].max // 25),
224
+ findings: ([.[].findings] | flatten)
225
+ })
226
+ ' <<< "$results_json"
227
+ }
228
+
229
+ # Build the final aggregated report
230
+ build_report() {
231
+ local resolved_json="$1"
232
+ local all_findings_deduped="$2"
233
+
234
+ jq --arg ts "$(date -u +"%Y-%m-%dT%H:%M:%SZ")" --argjson findings "$all_findings_deduped" '
235
+ # Build technologies map
236
+ (group_by(.tech) | map({
237
+ key: .[0].tech,
238
+ value: {
239
+ categories: (map({key: .category, value: {score: .score, max: .max}}) | from_entries),
240
+ total_score: (map(.score) | add),
241
+ max_score: (map(.max) | add)
242
+ }
243
+ }) | from_entries) as $techs |
244
+
245
+ # Count findings by severity
246
+ ($findings | group_by(.severity) | map({key: .[0].severity, value: length}) | from_entries) as $by_severity |
247
+
248
+ # Calculate global score
249
+ (($techs | to_entries | map(.value.total_score) | add) // 0) as $global_score |
250
+ (($techs | to_entries | map(.value.max_score) | add) // 0) as $global_max |
251
+
252
+ {
253
+ generated_at: $ts,
254
+ technologies: $techs,
255
+ findings: $findings,
256
+ summary: {
257
+ total_technologies: ($techs | keys | length),
258
+ global_score: (if ($techs | keys | length) > 0
259
+ then ($global_score / ($techs | keys | length) | floor)
260
+ else 0 end),
261
+ global_max: (if ($techs | keys | length) > 0
262
+ then ($global_max / ($techs | keys | length) | floor)
263
+ else 0 end),
264
+ total_findings: ($findings | length),
265
+ by_severity: (($by_severity) // {})
266
+ }
267
+ }
268
+ ' <<< "$resolved_json"
269
+ }
270
+
271
+ # =============================================================================
272
+ # Main Aggregation Pipeline
273
+ # =============================================================================
274
+
275
+ aggregate() {
276
+ local input_dir="$1"
277
+ local output_file="${2:-}"
278
+ local dry_run="${3:-false}"
279
+
280
+ log_info "Aggregating results from: $input_dir"
281
+
282
+ # Step 1: Collect all valid result files
283
+ local result_files
284
+ result_files=$(collect_results "$input_dir") || {
285
+ log_error "No results to aggregate"
286
+ return 1
287
+ }
288
+
289
+ local file_count
290
+ file_count=$(echo "$result_files" | wc -l | tr -d ' ')
291
+
292
+ if [[ "$dry_run" == "true" ]]; then
293
+ log_info "DRY RUN - Would aggregate $file_count result files:"
294
+ echo "$result_files" | while IFS= read -r f; do
295
+ local tech category score
296
+ tech=$(jq -r '.tech' "$f" 2>/dev/null)
297
+ category=$(jq -r '.category' "$f" 2>/dev/null)
298
+ score=$(jq -r '.score' "$f" 2>/dev/null)
299
+ local max
300
+ max=$(jq -r '.max // 25' "$f" 2>/dev/null)
301
+ local finding_count
302
+ finding_count=$(jq '.findings | length' "$f" 2>/dev/null)
303
+ log_info " $tech/$category: $score/$max ($finding_count findings)"
304
+ done
305
+
306
+ if [[ -n "$output_file" ]]; then
307
+ log_info "DRY RUN - Would write merged report to: $output_file"
308
+ else
309
+ log_info "DRY RUN - Would output merged report to stdout"
310
+ fi
311
+ return 0
312
+ fi
313
+
314
+ # Step 2: Concatenate all results into a JSON array
315
+ local all_results="[]"
316
+ while IFS= read -r file; do
317
+ local content
318
+ content=$(jq '.' "$file" 2>/dev/null) || {
319
+ log_warning "Failed to parse: $file"
320
+ continue
321
+ }
322
+ all_results=$(jq --argjson item "$content" '. + [$item]' <<< "$all_results")
323
+ done <<< "$result_files"
324
+
325
+ log_verbose "Combined $(jq 'length' <<< "$all_results") results"
326
+
327
+ # Step 3: Resolve score conflicts (weighted average for same tech+category)
328
+ local resolved
329
+ resolved=$(resolve_score_conflicts "$all_results")
330
+ log_verbose "Resolved to $(jq 'length' <<< "$resolved") unique tech/category entries"
331
+
332
+ # Step 4: Collect and deduplicate all findings
333
+ local all_findings
334
+ all_findings=$(jq '[.[].findings] | flatten' <<< "$all_results")
335
+ local original_count
336
+ original_count=$(jq 'length' <<< "$all_findings")
337
+
338
+ local deduped_findings
339
+ deduped_findings=$(deduplicate_findings "$all_findings")
340
+ local deduped_count
341
+ deduped_count=$(jq 'length' <<< "$deduped_findings")
342
+
343
+ if [[ "$original_count" -ne "$deduped_count" ]]; then
344
+ local removed=$((original_count - deduped_count))
345
+ log_info "Deduplicated findings: $original_count -> $deduped_count ($removed duplicates removed)"
346
+ fi
347
+
348
+ # Step 5: Build final report
349
+ local report
350
+ report=$(build_report "$resolved" "$deduped_findings")
351
+
352
+ # Step 6: Output
353
+ if [[ -n "$output_file" ]]; then
354
+ local output_dir
355
+ output_dir=$(dirname "$output_file")
356
+ mkdir -p "$output_dir"
357
+ echo "$report" | jq '.' > "$output_file"
358
+ log_success "Merged report written to: $output_file"
359
+ else
360
+ echo "$report" | jq '.'
361
+ fi
362
+
363
+ # Summary
364
+ local tech_count score_avg
365
+ tech_count=$(jq '.summary.total_technologies' <<< "$report")
366
+ score_avg=$(jq '.summary.global_score' <<< "$report")
367
+ log_success "Aggregation complete: $tech_count technologies, average score: $score_avg"
368
+ }
369
+
370
+ # =============================================================================
371
+ # CLI
372
+ # =============================================================================
373
+
374
+ main() {
375
+ # Parse arguments
376
+ while [[ $# -gt 0 ]]; do
377
+ case "$1" in
378
+ --input-dir)
379
+ INPUT_DIR="$2"
380
+ shift 2
381
+ ;;
382
+ --output-file)
383
+ OUTPUT_FILE="$2"
384
+ shift 2
385
+ ;;
386
+ --dry-run)
387
+ DRY_RUN="true"
388
+ shift
389
+ ;;
390
+ --verbose)
391
+ VERBOSE="true"
392
+ shift
393
+ ;;
394
+ -h|--help|help)
395
+ show_help
396
+ exit 0
397
+ ;;
398
+ *)
399
+ log_error "Unknown option: $1"
400
+ echo ""
401
+ show_help
402
+ exit 1
403
+ ;;
404
+ esac
405
+ done
406
+
407
+ # Validate required arguments
408
+ if [[ -z "$INPUT_DIR" ]]; then
409
+ log_error "Missing required argument: --input-dir"
410
+ echo ""
411
+ show_help
412
+ exit 1
413
+ fi
414
+
415
+ if [[ ! -d "$INPUT_DIR" ]]; then
416
+ log_error "Input directory does not exist: $INPUT_DIR"
417
+ exit 1
418
+ fi
419
+
420
+ # Check dependencies
421
+ check_dependencies || exit 1
422
+
423
+ # Run aggregation
424
+ aggregate "$INPUT_DIR" "$OUTPUT_FILE" "$DRY_RUN"
425
+ }
426
+
427
+ main "$@"