Skill Tracer
Skills › Compound Engineering › ce-optimize › parallel-probe.sh

parallel-probe.sh

134 lines · skills/ce-optimize/scripts/parallel-probe.sh at 3449230

1#!/bin/bash
2
3# Parallelism Probe
4# Detects common parallelism blockers in the target project.
5# Output is advisory -- the skill presents results to the user for approval.
6#
7# Usage: parallel-probe.sh <project_directory> [measurement_command] [measurement_workdir] [shared_file ...]
8#
9# Arguments:
10# project_directory - Root directory of the project to probe
11# measurement_command - The measurement command from the spec (optional, for port detection)
12# measurement_workdir - Measurement working directory relative to project root (default: .)
13# shared_file - Explicitly declared shared files that parallel runs depend on
15# Output:
16# JSON to stdout with:
17# mode: "parallel" | "serial" | "user-decision"
18# blockers: [ { type, description, suggestion } ]
20set -euo pipefail
22PROJECT_DIR="${1:?Error: project_directory argument required}"
23MEASUREMENT_CMD="${2:-}"
24MEASUREMENT_WORKDIR="${3:-.}"
26shift 3 2>/dev/null || shift $# 2>/dev/null || true
27SHARED_FILES=()
28if [[ $# -gt 0 ]]; then
29 SHARED_FILES=("$@")
30fi
32cd "$PROJECT_DIR" || {
33 echo '{"mode":"serial","blockers":[{"type":"error","description":"Cannot access project directory","suggestion":"Check path"}]}'
34 exit 0
37PY=""
38for c in python3 python py; do
39 if command -v "$c" >/dev/null 2>&1 && "$c" -c '' >/dev/null 2>&1; then
40 PY="$c"
41 break
42 fi
43done
44if [ -z "$PY" ]; then
45 echo '{"mode":"serial","blockers":[{"type":"missing_dependency","description":"A working Python 3 interpreter is required for structured probe output","suggestion":"Install Python 3 (python3, python, or py on PATH) or skip the probe and review parallel-readiness manually"}],"blocker_count":1}'
46 exit 0
47fi
49BLOCKERS="[]"
50SCAN_PATHS=()
52add_blocker() {
53 local type="$1"
54 local desc="$2"
55 local suggestion="$3"
56 BLOCKERS=$(echo "$BLOCKERS" | "$PY" -c "
57import json, sys
58b = json.load(sys.stdin)
59b.append({'type': '$type', 'description': '''$desc''', 'suggestion': '''$suggestion'''})
60print(json.dumps(b))
61" 2>/dev/null || echo "$BLOCKERS")
64add_scan_path() {
65 local candidate="$1"
67 if [[ -z "$candidate" ]]; then
68 return
69 fi
71 if [[ -e "$candidate" ]]; then
72 SCAN_PATHS+=("$candidate")
73 fi
76add_scan_path "$MEASUREMENT_WORKDIR"
78if [[ ${#SHARED_FILES[@]} -gt 0 ]]; then
79 for shared_file in "${SHARED_FILES[@]}"; do
80 add_scan_path "$shared_file"
81 done
82fi
84if [[ ${#SCAN_PATHS[@]} -eq 0 ]]; then
85 SCAN_PATHS=(".")
86fi
88# Check 1: Hardcoded ports in measurement command
89if [[ -n "$MEASUREMENT_CMD" ]]; then
90 # Look for common port patterns in the command itself
91 if echo "$MEASUREMENT_CMD" | grep -qE '(--port(?:\s+|=)[0-9]+|:\s*[0-9]{4,5}|PORT=[0-9]+|localhost:[0-9]+)'; then
92 add_blocker "port" "Measurement command contains hardcoded port reference" "Parameterize port via environment variable (e.g., PORT=\$EVAL_PORT)"
93 fi
94fi
96# Check 2: SQLite databases in the measurement workdir or declared shared files
97SQLITE_FILES=$(find "${SCAN_PATHS[@]}" -maxdepth 4 -type f \( -name '*.db' -o -name '*.sqlite' -o -name '*.sqlite3' \) ! -path '*/.git/*' ! -path '*/node_modules/*' ! -path '*/.claude/*' ! -path '*/.context/*' ! -path '*/.worktrees/*' 2>/dev/null | head -10 || true)
98if [[ -n "$SQLITE_FILES" ]]; then
99 FILE_COUNT=$(echo "$SQLITE_FILES" | wc -l | tr -d ' ')
100 add_blocker "shared_file" "Found $FILE_COUNT SQLite database file(s)" "Copy database files into each experiment worktree"
101fi
103# Check 3: Lock/PID files in the measurement workdir or declared shared files
104LOCK_FILES=$(find "${SCAN_PATHS[@]}" -maxdepth 4 -type f \( -name '*.lock' -o -name '*.pid' \) ! -path '*/.git/*' ! -path '*/node_modules/*' ! -path '*/.claude/*' ! -path '*/.context/*' ! -path '*/.worktrees/*' ! -name 'package-lock.json' ! -name 'yarn.lock' ! -name 'bun.lock' ! -name 'bun.lockb' ! -name 'Gemfile.lock' ! -name 'poetry.lock' ! -name 'Cargo.lock' 2>/dev/null | head -10 || true)
105if [[ -n "$LOCK_FILES" ]]; then
106 FILE_COUNT=$(echo "$LOCK_FILES" | wc -l | tr -d ' ')
107 add_blocker "lock_file" "Found $FILE_COUNT lock/PID file(s) that may cause contention" "Ensure measurement command cleans up lock files, or run in serial mode"
108fi
110# Check 4: Exclusive resource hints in the measurement command
111if [[ -n "$MEASUREMENT_CMD" ]] && echo "$MEASUREMENT_CMD" | grep -qiE '(cuda|gpu|tensorflow|torch|nvidia-smi|CUDA_VISIBLE_DEVICES)'; then
112 add_blocker "exclusive_resource" "Measurement command appears to use GPU or another exclusive accelerator" "GPU is typically an exclusive resource -- consider serial mode or device parameterization"
113fi
115# Determine mode
116BLOCKER_COUNT=$(echo "$BLOCKERS" | "$PY" -c "import json,sys; print(len(json.load(sys.stdin)))" 2>/dev/null || echo "0")
118if [[ "$BLOCKER_COUNT" == "0" ]]; then
119 MODE="parallel"
120elif echo "$BLOCKERS" | "$PY" -c "import json,sys; b=json.load(sys.stdin); exit(0 if any(x['type']=='exclusive_resource' for x in b) else 1)" 2>/dev/null; then
121 MODE="serial"
122else
123 MODE="user-decision"
124fi
126# Output JSON result
127"$PY" -c "
128import json
129print(json.dumps({
130 'mode': '$MODE',
131 'blockers': $BLOCKERS,
132 'blocker_count': $BLOCKER_COUNT
133}, indent=2))