@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.
@@ -1,7 +1,12 @@
1
- #!/bin/bash
1
+ #!/usr/bin/env bash
2
2
  # =============================================================================
3
3
  # Ralph Wiggum - Parallel Manager Module
4
4
  # Manages parallel execution of multiple Ralph sessions
5
+ #
6
+ # Single-writer pattern: Only the leader (main process) writes to shared state
7
+ # files. Background worker sessions report via individual temp files in
8
+ # .ralph/parallel/workers/worker-{id}.yaml. The leader merges reports after
9
+ # workers complete.
5
10
  # =============================================================================
6
11
 
7
12
  # Parallel manager state
@@ -21,6 +26,7 @@ PARALLEL_FAILED_COUNT=0
21
26
  PARALLEL_BASE_DIR=""
22
27
  PARALLEL_STATE_FILE=""
23
28
  PARALLEL_DEPENDENCY_GRAPH=""
29
+ PARALLEL_WORKER_DIR=""
24
30
 
25
31
  # =============================================================================
26
32
  # Initialization
@@ -32,6 +38,8 @@ init_parallel_manager() {
32
38
 
33
39
  PARALLEL_STATE_FILE="$PARALLEL_BASE_DIR/state.yaml"
34
40
  PARALLEL_DEPENDENCY_GRAPH="$PARALLEL_BASE_DIR/dependencies.json"
41
+ PARALLEL_WORKER_DIR="$PARALLEL_BASE_DIR/workers"
42
+ mkdir -p "$PARALLEL_WORKER_DIR"
35
43
 
36
44
  # Initialize state file
37
45
  cat > "$PARALLEL_STATE_FILE" << EOF
@@ -282,6 +290,9 @@ EOF
282
290
  [[ -n "$config" ]] && cmd="$cmd --config=$config"
283
291
 
284
292
  # Spawn in background
293
+ # Workers write to individual temp files (single-writer pattern).
294
+ # The leader merges results after all workers complete.
295
+ local worker_id="${story_id}-$$-$(date +%s)"
285
296
  (
286
297
  cd "$(pwd)" || exit 1
287
298
 
@@ -289,16 +300,21 @@ EOF
289
300
  output=$($cmd "$prompt" 2>&1)
290
301
  local status=$?
291
302
 
292
- # Save output
303
+ # Save output (per-session dir, no conflict)
293
304
  echo "$output" > "$session_dir/output.log"
294
305
 
295
- # Update session info
306
+ # Update per-session info (isolated file, no conflict)
296
307
  if command -v yq &>/dev/null; then
297
308
  yq -i ".status = \"$([ $status -eq 0 ] && echo completed || echo failed)\"" "$session_dir/info.yaml"
298
309
  yq -i ".exit_code = $status" "$session_dir/info.yaml"
299
310
  yq -i ".completed_at = \"$(date -u +"%Y-%m-%dT%H:%M:%SZ")\"" "$session_dir/info.yaml"
300
311
  fi
301
312
 
313
+ # Write worker report for leader to merge (avoids concurrent yq writes)
314
+ local result_status="failed"
315
+ [[ $status -eq 0 ]] && result_status="completed"
316
+ write_worker_report "$worker_id" "$session_id" "$story_id" "$result_status"
317
+
302
318
  exit $status
303
319
  ) &
304
320
 
@@ -324,14 +340,18 @@ EOF
324
340
  # Session Management
325
341
  # =============================================================================
326
342
 
327
- # Check and collect completed sessions
343
+ # Check and collect completed sessions.
344
+ # In the single-writer pattern, this function collects finished PIDs
345
+ # and then merges worker reports written by the background processes.
328
346
  collect_completed_sessions() {
329
347
  local completed=()
330
348
  local failed=()
349
+ local any_finished=false
331
350
 
332
351
  for pid in "${!PARALLEL_PIDS[@]}"; do
333
352
  if ! kill -0 "$pid" 2>/dev/null; then
334
- # Process has finished
353
+ any_finished=true
354
+
335
355
  local session_id="${PARALLEL_PIDS[$pid]}"
336
356
  local story_id="${PARALLEL_SESSIONS[$session_id]}"
337
357
 
@@ -341,15 +361,11 @@ collect_completed_sessions() {
341
361
 
342
362
  if [[ $exit_code -eq 0 ]]; then
343
363
  completed+=("$story_id")
344
- mark_story_completed "$story_id"
345
364
  PARALLEL_COMPLETED_COUNT=$((PARALLEL_COMPLETED_COUNT + 1))
346
- update_parallel_state "complete" "$session_id" "$story_id"
347
365
  print_success "Session completed: $session_id ($story_id)"
348
366
  else
349
367
  failed+=("$story_id")
350
- mark_story_failed "$story_id"
351
368
  PARALLEL_FAILED_COUNT=$((PARALLEL_FAILED_COUNT + 1))
352
- update_parallel_state "fail" "$session_id" "$story_id"
353
369
  print_error "Session failed: $session_id ($story_id)"
354
370
  fi
355
371
 
@@ -359,6 +375,11 @@ collect_completed_sessions() {
359
375
  fi
360
376
  done
361
377
 
378
+ # Merge worker reports (single-writer: only the leader writes to shared state)
379
+ if [[ "$any_finished" == "true" ]]; then
380
+ merge_worker_reports
381
+ fi
382
+
362
383
  # Return completed stories
363
384
  echo "${completed[*]}"
364
385
  }
@@ -467,10 +488,127 @@ process_stories_parallel() {
467
488
  print_parallel_summary
468
489
  }
469
490
 
491
+ # =============================================================================
492
+ # Single-Writer Pattern: Worker Reports
493
+ # =============================================================================
494
+
495
+ # Write a worker report (called by background workers instead of writing to
496
+ # shared state files directly). This avoids concurrent yq race conditions.
497
+ write_worker_report() {
498
+ local worker_id="$1"
499
+ local session_id="$2"
500
+ local story_id="$3"
501
+ local status="$4" # "completed" or "failed"
502
+
503
+ local report_file="${PARALLEL_WORKER_DIR}/worker-${worker_id}.yaml"
504
+ local ts
505
+ ts="$(date -u +"%Y-%m-%dT%H:%M:%SZ")"
506
+
507
+ cat > "$report_file" << EOF
508
+ worker_id: "${worker_id}"
509
+ session_id: "${session_id}"
510
+ story_id: "${story_id}"
511
+ status: "${status}"
512
+ reported_at: "${ts}"
513
+ EOF
514
+ }
515
+
516
+ # Merge all worker reports into the shared state file (leader only).
517
+ # This is the ONLY function that writes to PARALLEL_STATE_FILE and the
518
+ # dependency graph during parallel execution.
519
+ merge_worker_reports() {
520
+ local dry_run="${1:-false}"
521
+
522
+ if ! command -v yq &>/dev/null; then
523
+ print_error "yq required for merge" 2>/dev/null || echo "[ERROR] yq required"
524
+ return 1
525
+ fi
526
+
527
+ local report_files=("${PARALLEL_WORKER_DIR}"/worker-*.yaml)
528
+
529
+ if [[ ! -e "${report_files[0]:-}" ]]; then
530
+ print_info "No worker reports to merge" 2>/dev/null || echo "[INFO] No worker reports"
531
+ return 0
532
+ fi
533
+
534
+ local merged_count=0
535
+
536
+ for report_file in "${report_files[@]}"; do
537
+ [[ ! -f "$report_file" ]] && continue
538
+
539
+ local session_id story_id status
540
+ session_id=$(yq '.session_id' "$report_file" 2>/dev/null || echo "")
541
+ story_id=$(yq '.story_id' "$report_file" 2>/dev/null || echo "")
542
+ status=$(yq '.status' "$report_file" 2>/dev/null || echo "")
543
+
544
+ if [[ -z "$session_id" || -z "$story_id" || -z "$status" ]]; then
545
+ continue
546
+ fi
547
+
548
+ if [[ "$dry_run" == "true" ]]; then
549
+ print_info "DRY RUN - Would merge: session=$session_id story=$story_id status=$status" 2>/dev/null \
550
+ || echo "[INFO] DRY RUN - Would merge: session=$session_id story=$story_id status=$status"
551
+ ((merged_count++))
552
+ continue
553
+ fi
554
+
555
+ # Update state file (single writer - no race condition)
556
+ if [[ -f "$PARALLEL_STATE_FILE" ]]; then
557
+ yq -i ".sessions.active -= [\"$session_id\"]" "$PARALLEL_STATE_FILE"
558
+ if [[ "$status" == "completed" ]]; then
559
+ yq -i ".sessions.completed += [\"$session_id\"]" "$PARALLEL_STATE_FILE"
560
+ yq -i ".metrics.total_completed += 1" "$PARALLEL_STATE_FILE"
561
+ else
562
+ yq -i ".sessions.failed += [\"$session_id\"]" "$PARALLEL_STATE_FILE"
563
+ yq -i ".metrics.total_failed += 1" "$PARALLEL_STATE_FILE"
564
+ fi
565
+ fi
566
+
567
+ # Update dependency graph (single writer - no race condition)
568
+ if [[ -f "$PARALLEL_DEPENDENCY_GRAPH" ]] && command -v jq &>/dev/null; then
569
+ local updated
570
+ updated=$(jq --arg id "$story_id" --arg s "$status" '.nodes[$id].status = $s' "$PARALLEL_DEPENDENCY_GRAPH")
571
+ echo "$updated" > "$PARALLEL_DEPENDENCY_GRAPH"
572
+ fi
573
+
574
+ rm -f "$report_file"
575
+ ((merged_count++))
576
+ done
577
+
578
+ # Update efficiency after merge
579
+ if [[ "$dry_run" != "true" && -f "$PARALLEL_STATE_FILE" ]]; then
580
+ local total_completed total_spawned
581
+ total_completed=$(yq '.metrics.total_completed' "$PARALLEL_STATE_FILE")
582
+ total_spawned=$(yq '.metrics.total_spawned' "$PARALLEL_STATE_FILE")
583
+ if [[ $total_spawned -gt 0 ]]; then
584
+ local efficiency=$((total_completed * 100 / total_spawned))
585
+ yq -i ".metrics.efficiency = $efficiency" "$PARALLEL_STATE_FILE"
586
+ fi
587
+ fi
588
+
589
+ if [[ "$dry_run" == "true" ]]; then
590
+ print_info "DRY RUN - Would merge $merged_count worker reports" 2>/dev/null \
591
+ || echo "[INFO] DRY RUN - Would merge $merged_count reports"
592
+ else
593
+ print_success "Merged $merged_count worker reports" 2>/dev/null \
594
+ || echo "[SUCCESS] Merged $merged_count reports"
595
+ fi
596
+ }
597
+
598
+ # Clean up worker temp directory
599
+ cleanup_workers() {
600
+ if [[ -d "$PARALLEL_WORKER_DIR" ]]; then
601
+ rm -rf "$PARALLEL_WORKER_DIR"
602
+ mkdir -p "$PARALLEL_WORKER_DIR"
603
+ fi
604
+ }
605
+
470
606
  # =============================================================================
471
607
  # State Management
472
608
  # =============================================================================
473
609
 
610
+ # update_parallel_state is now used ONLY by the leader process for spawn events.
611
+ # Complete/fail events are handled via worker reports + merge_worker_reports().
474
612
  update_parallel_state() {
475
613
  local action="$1"
476
614
  local session_id="$2"
@@ -482,18 +620,22 @@ update_parallel_state() {
482
620
 
483
621
  case "$action" in
484
622
  spawn)
623
+ # Spawn is always called from the leader, safe to write directly
485
624
  yq -i ".sessions.active += [\"$session_id\"]" "$PARALLEL_STATE_FILE"
486
625
  yq -i ".metrics.total_spawned += 1" "$PARALLEL_STATE_FILE"
487
626
  ;;
488
- complete)
489
- yq -i ".sessions.active -= [\"$session_id\"]" "$PARALLEL_STATE_FILE"
490
- yq -i ".sessions.completed += [\"$session_id\"]" "$PARALLEL_STATE_FILE"
491
- yq -i ".metrics.total_completed += 1" "$PARALLEL_STATE_FILE"
492
- ;;
493
- fail)
494
- yq -i ".sessions.active -= [\"$session_id\"]" "$PARALLEL_STATE_FILE"
495
- yq -i ".sessions.failed += [\"$session_id\"]" "$PARALLEL_STATE_FILE"
496
- yq -i ".metrics.total_failed += 1" "$PARALLEL_STATE_FILE"
627
+ complete|fail)
628
+ # DEPRECATED for parallel mode: use write_worker_report() + merge_worker_reports()
629
+ # Kept for backward compatibility with sequential mode
630
+ if [[ "$action" == "complete" ]]; then
631
+ yq -i ".sessions.active -= [\"$session_id\"]" "$PARALLEL_STATE_FILE"
632
+ yq -i ".sessions.completed += [\"$session_id\"]" "$PARALLEL_STATE_FILE"
633
+ yq -i ".metrics.total_completed += 1" "$PARALLEL_STATE_FILE"
634
+ else
635
+ yq -i ".sessions.active -= [\"$session_id\"]" "$PARALLEL_STATE_FILE"
636
+ yq -i ".sessions.failed += [\"$session_id\"]" "$PARALLEL_STATE_FILE"
637
+ yq -i ".metrics.total_failed += 1" "$PARALLEL_STATE_FILE"
638
+ fi
497
639
  ;;
498
640
  esac
499
641
 
@@ -552,6 +694,17 @@ cmd_parallel_manager() {
552
694
  local command="${1:-status}"
553
695
  shift || true
554
696
 
697
+ # Parse flags
698
+ local dry_run="false"
699
+ local args=()
700
+ for arg in "$@"; do
701
+ case "$arg" in
702
+ --dry-run) dry_run="true" ;;
703
+ *) args+=("$arg") ;;
704
+ esac
705
+ done
706
+ set -- "${args[@]+"${args[@]}"}"
707
+
555
708
  case "$command" in
556
709
  init)
557
710
  init_parallel_manager
@@ -582,6 +735,34 @@ cmd_parallel_manager() {
582
735
  collect)
583
736
  collect_completed_sessions
584
737
  ;;
738
+ merge-reports)
739
+ merge_worker_reports "$dry_run"
740
+ ;;
741
+ cleanup-workers)
742
+ cleanup_workers
743
+ ;;
744
+ -h|--help|help)
745
+ echo "Ralph Wiggum - Parallel Manager"
746
+ echo ""
747
+ echo "Usage: parallel-manager.sh <command> [args] [--dry-run]"
748
+ echo ""
749
+ echo "Commands:"
750
+ echo " init Initialize parallel manager"
751
+ echo " status Show parallel manager status"
752
+ echo " process <stories> Process stories in parallel"
753
+ echo " spawn <id> [msg] Spawn a single session"
754
+ echo " wait [timeout] Wait for all sessions"
755
+ echo " collect Collect completed sessions"
756
+ echo " merge-reports Merge pending worker reports (leader only)"
757
+ echo " cleanup-workers Clean up worker temp directory"
758
+ echo ""
759
+ echo "Options:"
760
+ echo " --dry-run Preview without changes"
761
+ echo ""
762
+ echo "Single-Writer Pattern:"
763
+ echo " Background workers write to .ralph/parallel/workers/worker-{id}.yaml"
764
+ echo " Only the leader merges results into shared state files"
765
+ ;;
585
766
  *)
586
767
  echo "Usage: parallel-manager.sh <command> [args]"
587
768
  echo ""
@@ -592,6 +773,11 @@ cmd_parallel_manager() {
592
773
  echo " spawn <id> [msg] Spawn a single session"
593
774
  echo " wait [timeout] Wait for all sessions"
594
775
  echo " collect Collect completed sessions"
776
+ echo " merge-reports Merge worker reports (leader)"
777
+ echo " cleanup-workers Clean up worker temp files"
778
+ echo ""
779
+ echo "Options:"
780
+ echo " --dry-run Preview without changes"
595
781
  ;;
596
782
  esac
597
783
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@the-bearded-bear/claude-craft",
3
- "version": "5.6.0-next.8bfdffa",
3
+ "version": "5.7.0-next.bf058ea",
4
4
  "description": "A comprehensive framework for AI-assisted development with Claude Code. Install standardized rules, agents, and commands for your projects.",
5
5
  "main": "cli/index.js",
6
6
  "bin": {