-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconductor.sh
More file actions
executable file
·438 lines (358 loc) · 15 KB
/
conductor.sh
File metadata and controls
executable file
·438 lines (358 loc) · 15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
#!/bin/bash
set -euo pipefail
# AIDD Conductor — Overnight autonomous feature builder
# Reads feature-queue.yml, spawns claude -p per feature in worktrees
#
# Usage: aidd-conductor [--once] [--dry-run]
# --once Run one pass through the queue then exit (for cron)
# --dry-run Show what would be done without doing it
AIDD_HOME="${AIDD_HOME:-$HOME/.aidd}"
source "$AIDD_HOME/.env" 2>/dev/null || true
# Resolve claude binary path (subshells may not inherit user PATH)
CLAUDE_BIN=$(command -v claude 2>/dev/null || echo "")
if [ -z "$CLAUDE_BIN" ]; then
# Common install locations
for p in "$HOME/.local/bin/claude" "$HOME/.npm-global/bin/claude" "/usr/local/bin/claude"; do
[ -x "$p" ] && CLAUDE_BIN="$p" && break
done
fi
if [ -z "$CLAUDE_BIN" ]; then
echo "ERROR: claude CLI not found. Install: npm install -g @anthropic-ai/claude-code"
exit 1
fi
# Parse flags
ONCE=false
DRY_RUN=false
for arg in "$@"; do
case $arg in
--once) ONCE=true ;;
--dry-run) DRY_RUN=true ;;
esac
done
# Resolve project config — aidd.yml is optional, all fields have defaults
if [ ! -f "aidd.yml" ] && [ ! -f "feature-queue.yml" ]; then
echo "ERROR: No aidd.yml or feature-queue.yml found. Run 'aidd init' first."
exit 1
fi
# Auto-detect project name from directory if not in config
DIR_NAME=$(basename "$PWD")
PROJECT_NAME=$(yq -r '.project.name // ""' aidd.yml 2>/dev/null || echo "")
PROJECT_NAME="${PROJECT_NAME:-$DIR_NAME}"
# Commands — auto-detect test runner if not configured
TEST_CMD=$(yq -r '.commands.test // ""' aidd.yml 2>/dev/null || echo "")
LINT_CMD=$(yq -r '.commands.lint // ""' aidd.yml 2>/dev/null || echo "")
if [ -z "$TEST_CMD" ]; then
# Auto-detect test command
if [ -f "pytest.ini" ] || [ -f "pyproject.toml" ] || [ -f "setup.py" ]; then
TEST_CMD="python3 -m pytest tests/ -x -q"
elif [ -f "package.json" ]; then
if grep -q '"vitest"' package.json 2>/dev/null; then
TEST_CMD="npx vitest run"
elif grep -q '"jest"' package.json 2>/dev/null; then
TEST_CMD="npx jest"
elif grep -q '"test"' package.json 2>/dev/null; then
TEST_CMD="npm test"
fi
elif [ -f "go.mod" ]; then
TEST_CMD="go test ./..."
elif [ -f "Cargo.toml" ]; then
TEST_CMD="cargo test"
fi
fi
# Agent settings with defaults
MAX_PARALLEL=$(yq -r '.agent.max_parallel // 1' aidd.yml 2>/dev/null || echo "1")
MODEL=$(yq -r '.agent.model // "sonnet"' aidd.yml 2>/dev/null || echo "sonnet")
PERMISSION_MODE=$(yq -r '.agent.permission_mode // "auto"' aidd.yml 2>/dev/null || echo "auto")
TIMEOUT_HOURS=$(yq -r '.agent.timeout_hours // 4' aidd.yml 2>/dev/null || echo "4")
# Paths with defaults
WORKTREE_DIR=$(yq -r '.paths.worktrees // ".worktrees"' aidd.yml 2>/dev/null || echo ".worktrees")
WORKTREE_DIR="${WORKTREE_DIR%/}" # strip trailing slash
STATE_DIR=$(yq -r '.paths.state // ".aidd"' aidd.yml 2>/dev/null || echo ".aidd")
# Prompt customization (optional overrides)
PRE_INSTRUCTIONS=$(yq -r '.prompt_instructions.pre_implement // ""' aidd.yml 2>/dev/null || echo "")
POST_INSTRUCTIONS=$(yq -r '.prompt_instructions.post_implement // ""' aidd.yml 2>/dev/null || echo "")
CUSTOM_PROMPT_TEMPLATE=$(yq -r '.prompt_template // ""' aidd.yml 2>/dev/null || echo "")
# Built-in default prompt template
DEFAULT_PROMPT_TEMPLATE='You are executing an autonomous implementation task for {project_name}.
Plan: {plan_path}
Spec: {spec_path}
{pre_implement_instructions}
Instructions:
1. Read the plan file — this is your source of truth
2. If CLAUDE.md exists, read it for project conventions
3. Follow TDD: test first → implement → verify
4. After each passing phase, commit with message: "[aidd:{feature_id}] phase N: <description>"
5. If stuck after 3 attempts on any task, stop and explain what is blocking you
Test command: {test_cmd}
Lint command: {lint_cmd}
{feedback}
Do NOT install new dependencies unless the plan says to.
{post_implement_instructions}'
# Use custom template if provided, otherwise use built-in default
PROMPT_TEMPLATE="${CUSTOM_PROMPT_TEMPLATE:-$DEFAULT_PROMPT_TEMPLATE}"
mkdir -p "$WORKTREE_DIR" "$STATE_DIR/logs" "$STATE_DIR/progress"
# ---- Telegram Notifications ----
notify() {
local message="$1"
local keyboard="${2:-}"
if [ -n "${AIDD_TELEGRAM_TOKEN:-}" ] && [ -n "${AIDD_TELEGRAM_CHAT_ID:-}" ]; then
local payload
payload=$(jq -n \
--arg chat_id "${AIDD_TELEGRAM_CHAT_ID}" \
--arg text "$message" \
--argjson reply_markup "${keyboard:-null}" \
'{chat_id: $chat_id, text: $text, parse_mode: "Markdown"} +
(if $reply_markup then {reply_markup: $reply_markup} else {} end)'
)
curl -s -X POST "https://api.telegram.org/bot${AIDD_TELEGRAM_TOKEN}/sendMessage" \
-H "Content-Type: application/json" \
-d "$payload" > /dev/null 2>&1 || true
fi
echo "[NOTIFY] $message"
}
# ---- Pre-Flight Checks ----
preflight() {
local feature_id="$1"
local spec plan
spec=$(yq -r ".features[] | select(.id == \"$feature_id\") | .spec" feature-queue.yml)
plan=$(yq -r ".features[] | select(.id == \"$feature_id\") | .plan" feature-queue.yml)
# Check disk space (2GB minimum)
local free_gb
free_gb=$(df -m . | awk 'NR==2 {print int($4/1024)}' 2>/dev/null || echo "999")
if [ "$free_gb" -lt 2 ]; then
echo "PREFLIGHT FAIL: Only ${free_gb}GB free disk space"
return 1
fi
# Check plan file exists
if [ ! -f "$plan" ]; then
echo "PREFLIGHT FAIL: Plan file not found: $plan"
return 1
fi
# Check spec file exists (if specified)
if [ -n "$spec" ] && [ "$spec" != "null" ] && [ ! -f "$spec" ]; then
echo "PREFLIGHT FAIL: Spec file not found: $spec"
return 1
fi
# Check dependencies are merged
local deps
deps=$(yq -r ".features[] | select(.id == \"$feature_id\") | .depends_on[]?" feature-queue.yml 2>/dev/null || true)
for dep in $deps; do
local dep_status
dep_status=$(yq -r ".features[] | select(.id == \"$dep\") | .status" feature-queue.yml)
if [ "$dep_status" != "merged" ]; then
echo "PREFLIGHT FAIL: Dependency '$dep' not merged (status: $dep_status)"
return 1
fi
done
return 0
}
# ---- Build Prompt ----
build_prompt() {
local feature_id="$1"
local plan spec feedback
plan=$(yq -r ".features[] | select(.id == \"$feature_id\") | .plan" feature-queue.yml)
spec=$(yq -r ".features[] | select(.id == \"$feature_id\") | .spec // \"\"" feature-queue.yml)
feedback=$(yq -r ".features[] | select(.id == \"$feature_id\") | .feedback // \"\"" feature-queue.yml)
local prompt="$PROMPT_TEMPLATE"
# Variable substitution
prompt="${prompt//\{project_name\}/$PROJECT_NAME}"
prompt="${prompt//\{plan_path\}/$plan}"
prompt="${prompt//\{spec_path\}/$spec}"
prompt="${prompt//\{feature_id\}/$feature_id}"
prompt="${prompt//\{test_cmd\}/$TEST_CMD}"
prompt="${prompt//\{lint_cmd\}/$LINT_CMD}"
prompt="${prompt//\{pre_implement_instructions\}/$PRE_INSTRUCTIONS}"
prompt="${prompt//\{post_implement_instructions\}/$POST_INSTRUCTIONS}"
if [ -n "$feedback" ] && [ "$feedback" != "null" ] && [ "$feedback" != "" ]; then
prompt="${prompt//\{feedback\}/IMPORTANT - Previous attempt was rejected. Feedback: $feedback}"
else
prompt="${prompt//\{feedback\}/}"
fi
echo "$prompt"
}
# ---- Run One Feature ----
run_feature() {
local feature_id="$1"
local plan worktree_path log_file
plan=$(yq -r ".features[] | select(.id == \"$feature_id\") | .plan" feature-queue.yml)
worktree_path="$WORKTREE_DIR/$feature_id"
log_file="$STATE_DIR/logs/$feature_id.log"
echo "[$(date '+%H:%M:%S')] Starting feature: $feature_id"
# Build prompt first (needed for dry-run output)
local prompt
prompt=$(build_prompt "$feature_id")
if [ "$DRY_RUN" = true ]; then
echo " [DRY RUN] Would create worktree at $worktree_path"
echo " [DRY RUN] Would run claude -p --model $MODEL --permission-mode $PERMISSION_MODE"
echo " [DRY RUN] Prompt:"
echo " ---"
echo "$prompt" | sed 's/^/ /'
echo " ---"
return 0
fi
# Create worktree
if [ -d "$worktree_path" ]; then
echo " Worktree exists, cleaning up..."
git worktree remove "$worktree_path" --force 2>/dev/null || true
git branch -D "feature/$feature_id" 2>/dev/null || true
fi
git worktree add "$worktree_path" -b "feature/$feature_id" 2>/dev/null
# Update status
yq -i "(.features[] | select(.id == \"$feature_id\") | .status) = \"running\"" feature-queue.yml
# Initialize progress file
cat > "$STATE_DIR/progress/$feature_id.json" <<EOF
{"feature_id":"$feature_id","status":"running","current_phase":0,"total_phases":0,"phases_completed":[],"stuck_reason":null,"last_updated":"$(date -u +%Y-%m-%dT%H:%M:%SZ)"}
EOF
notify "🚀 *$PROJECT_NAME* — Starting: \`$feature_id\`"
# Spawn claude -p in worktree
local exit_code=0
local prompt_file
prompt_file=$(mktemp)
echo "$prompt" > "$prompt_file"
# Use gtimeout (brew) or timeout (linux) or fallback to no timeout
local timeout_cmd=""
if command -v gtimeout &>/dev/null; then
timeout_cmd="gtimeout ${TIMEOUT_HOURS}h"
elif command -v timeout &>/dev/null; then
timeout_cmd="timeout ${TIMEOUT_HOURS}h"
fi
# Clean env: unset CLAUDECODE (nesting guard) and SSE_PORT (session binding)
$timeout_cmd env -u CLAUDECODE -u CLAUDE_CODE_SSE_PORT -u CLAUDE_CODE_ENTRYPOINT bash -c "
cd '$worktree_path' && \
'$CLAUDE_BIN' -p \"\$(cat '$prompt_file')\" \
--permission-mode '$PERMISSION_MODE' \
--model '$MODEL' \
--verbose
" > "$log_file" 2>&1 || exit_code=$?
rm -f "$prompt_file"
# Check result
if [ $exit_code -eq 0 ]; then
# Verify tests pass in worktree
if [ -n "$TEST_CMD" ]; then
if (cd "$worktree_path" && eval "$TEST_CMD" > /dev/null 2>&1); then
handle_success "$feature_id"
else
handle_stuck "$feature_id" "Tests failing after implementation"
fi
else
handle_success "$feature_id"
fi
elif [ $exit_code -eq 124 ]; then
handle_stuck "$feature_id" "Timeout after ${TIMEOUT_HOURS} hours"
else
# Check progress file for stuck reason
local stuck_reason
stuck_reason=$(jq -r '.stuck_reason // "Agent exited with code '$exit_code'"' "$STATE_DIR/progress/$feature_id.json" 2>/dev/null || echo "Agent exited with code $exit_code")
handle_stuck "$feature_id" "$stuck_reason"
fi
}
handle_success() {
local feature_id="$1"
local worktree_path="$WORKTREE_DIR/$feature_id"
yq -i "(.features[] | select(.id == \"$feature_id\") | .status) = \"review\"" feature-queue.yml
# Create PR if gh is available
local pr_url=""
if command -v gh &> /dev/null; then
(cd "$worktree_path" && git push -u origin "feature/$feature_id" 2>/dev/null) || true
pr_url=$(cd "$worktree_path" && gh pr create \
--title "feat($feature_id): autonomous implementation" \
--body "Implemented by AIDD conductor. Review and approve." \
--base main 2>/dev/null) || true
fi
local test_info=""
if [ -f "$STATE_DIR/progress/$feature_id.json" ]; then
local passing failing
passing=$(jq '[.phases_completed[].tests_passing] | add // 0' "$STATE_DIR/progress/$feature_id.json" 2>/dev/null || echo "?")
failing=$(jq '[.phases_completed[].tests_failing] | add // 0' "$STATE_DIR/progress/$feature_id.json" 2>/dev/null || echo "?")
test_info="Tests: ${passing} passing, ${failing} failing."
fi
local kb_success
kb_success=$(jq -n --arg fid "$feature_id" '{inline_keyboard: [
[{text: "Approve", callback_data: ("approve:" + $fid)},
{text: "Reject", callback_data: ("reject:" + $fid)}],
[{text: "Logs", callback_data: ("logs:" + $fid)},
{text: "Status", callback_data: "cmd:status"}]
]}')
notify "✅ *$PROJECT_NAME* — Complete: \`$feature_id\`
${test_info}
${pr_url:+PR: $pr_url}" "$kb_success"
echo "[$(date '+%H:%M:%S')] Feature complete: $feature_id"
}
handle_stuck() {
local feature_id="$1"
local reason="$2"
yq -i "(.features[] | select(.id == \"$feature_id\") | .status) = \"stuck\"" feature-queue.yml
# Get last few lines of log for context
local log_tail=""
if [ -f "$STATE_DIR/logs/$feature_id.log" ]; then
log_tail=$(tail -5 "$STATE_DIR/logs/$feature_id.log" 2>/dev/null | head -c 500 || true)
fi
local kb_stuck
kb_stuck=$(jq -n --arg fid "$feature_id" '{inline_keyboard: [
[{text: "Skip (re-queue)", callback_data: ("skip:" + $fid)},
{text: "Logs", callback_data: ("logs:" + $fid)}],
[{text: "Status", callback_data: "cmd:status"}]
]}')
notify "❌ *$PROJECT_NAME* — Stuck: \`$feature_id\`
Reason: $reason" "$kb_stuck"
echo "[$(date '+%H:%M:%S')] Feature stuck: $feature_id — $reason"
}
# ---- Cleanup ----
cleanup_feature() {
local feature_id="$1"
local worktree_path="$WORKTREE_DIR/$feature_id"
if [ -d "$worktree_path" ]; then
git worktree remove "$worktree_path" --force 2>/dev/null || true
fi
git branch -D "feature/$feature_id" 2>/dev/null || true
rm -f "$STATE_DIR/progress/$feature_id.json"
}
# ---- Main Conductor Loop ----
conductor_pass() {
if [ ! -f "feature-queue.yml" ]; then
echo "No feature-queue.yml found."
return
fi
# Count running features
local running_count
running_count=$(yq -r '[.features[] | select(.status == "running")] | length' feature-queue.yml)
if [ "$running_count" -ge "$MAX_PARALLEL" ]; then
echo "[$(date '+%H:%M:%S')] $running_count agents running (max: $MAX_PARALLEL). Waiting."
return
fi
# Find next eligible feature
local feature_ids
feature_ids=$(yq -r '.features[] | select(.status == "queued") | .id' feature-queue.yml)
for feature_id in $feature_ids; do
# Check preflight
local preflight_result
preflight_result=$(preflight "$feature_id" 2>&1) || {
echo " Skipping $feature_id: $preflight_result"
continue
}
# Run it
run_feature "$feature_id"
# Recheck parallel limit
running_count=$(yq -r '[.features[] | select(.status == "running")] | length' feature-queue.yml)
if [ "$running_count" -ge "$MAX_PARALLEL" ]; then
break
fi
done
}
# ---- Entry Point ----
echo "========================================"
echo " AIDD Conductor — $PROJECT_NAME"
echo " $(date)"
echo "========================================"
if [ "$ONCE" = true ] || [ "$DRY_RUN" = true ]; then
conductor_pass
echo ""
[ "$DRY_RUN" = true ] && echo "Dry run complete." || echo "Single pass complete."
else
notify "🔄 *$PROJECT_NAME* — AIDD Conductor started"
while true; do
conductor_pass
echo "[$(date '+%H:%M:%S')] Sleeping 5 minutes..."
sleep 300
done
fi