forked from abh1nash/ace-daw
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathrun-agent.sh
More file actions
executable file
·254 lines (219 loc) · 9.22 KB
/
run-agent.sh
File metadata and controls
executable file
·254 lines (219 loc) · 9.22 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
#!/bin/bash
WT="$1"; TOOL="$2"; ISSUE="$3"; TITLE="$4"; REPO="ace-step/ACE-Step-DAW"
BRANCH="fix/issue-$ISSUE"
MAX_ROUNDS=5 # Max fix iterations before giving up
CI_POLL_INTERVAL=60 # Seconds between CI status checks
CI_POLL_TIMEOUT=900 # Max seconds to wait for CI (15 min)
LOG="/tmp/pm-activity.log"
SESSION_FILE="$WT/.agent-session-id" # Persist session ID across rounds
log() { echo "[$(date)] [agent-$ISSUE] $*" >> "$LOG"; echo "$*"; }
cd "$WT" || exit 1
# ── Helper: run Claude with session persistence ──
# First call: creates session. Subsequent calls: resume same session.
run_claude() {
local prompt="$1"
local session_id
local retries=0
while [ $retries -lt 3 ]; do
local output
if [ -f "$SESSION_FILE" ]; then
# Resume existing session — agent has full context of prior work
session_id=$(cat "$SESSION_FILE")
log "Resuming Claude session $session_id"
output=$(~/.local/bin/claude --print --resume "$session_id" \
--permission-mode bypassPermissions --fallback-model sonnet \
"$prompt" 2>&1)
else
# First run — create a named session with a stable UUID
session_id=$(uuidgen | tr '[:upper:]' '[:lower:]')
echo "$session_id" > "$SESSION_FILE"
log "Starting Claude session $session_id"
output=$(~/.local/bin/claude --print --session-id "$session_id" \
--permission-mode bypassPermissions --fallback-model sonnet \
"$prompt" 2>&1)
fi
if echo "$output" | grep -qiE "403.*forbidden|Request not allowed|authentication_failed|500|502|503|overloaded|rate.limit|timeout|ETIMEDOUT|ECONNRESET"; then
retries=$((retries + 1))
log "Claude transient error, retry $retries/3"
sleep $((retries * 15))
else
echo "$output"
return 0
fi
done
log "Claude failed 3x, falling back to Codex"
# Fallback: start a codex session instead
rm -f "$SESSION_FILE"
TOOL="codex"
run_codex "$prompt"
}
# ── Helper: run Codex with session persistence ──
# First call: creates session. Subsequent calls: resume same session.
run_codex() {
local prompt="$1"
if [ -f "$SESSION_FILE" ]; then
local session_id
session_id=$(cat "$SESSION_FILE")
log "Resuming Codex session $session_id"
codex exec resume "$session_id" "$prompt"
else
# First run — capture session ID from codex output
# Use --json + -o to get structured output with session info
local output_file="$WT/.codex-output.tmp"
codex exec -C "$WT" -s danger-full-access \
-o "$output_file" "$prompt"
# Codex stores sessions by cwd; use --last for resume
# Save a marker so we know to use --last next time
echo "codex-last" > "$SESSION_FILE"
fi
}
# ── Helper: dispatch to the right tool ──
run_agent() {
local prompt="$1"
if [ "$TOOL" = "codex" ]; then
run_codex "$prompt"
else
run_claude "$prompt"
fi
}
# ── Helper: wait for CI to finish, return conclusion ──
wait_for_ci() {
local pr_num="$1"
local elapsed=0
while [ $elapsed -lt $CI_POLL_TIMEOUT ]; do
sleep $CI_POLL_INTERVAL
elapsed=$((elapsed + CI_POLL_INTERVAL))
local status
status=$(gh pr checks "$pr_num" --repo "$REPO" 2>/dev/null)
if echo "$status" | grep -q "pending\|in_progress\|queued"; then
log "CI still running ($elapsed/${CI_POLL_TIMEOUT}s)..."
continue
fi
if echo "$status" | grep -qi "fail\|error"; then
echo "failure"
return 0
fi
echo "success"
return 0
done
echo "timeout"
}
# ── Helper: collect all feedback (CI failures + review comments) ──
collect_feedback() {
local pr_num="$1"
local feedback=""
# 1. CI failure logs
local failed_run
failed_run=$(gh run list --branch "$BRANCH" --repo "$REPO" --status failure \
--limit 1 --json databaseId -q '.[0].databaseId' 2>/dev/null)
if [ -n "$failed_run" ]; then
local ci_log
ci_log=$(gh run view "$failed_run" --repo "$REPO" --log-failed 2>/dev/null | tail -80)
[ -n "$ci_log" ] && feedback+="## CI Failure Log
$ci_log
"
fi
# 2. PR review comments (Copilot, humans, any reviewer)
local review_comments
review_comments=$(gh api "repos/$REPO/pulls/$pr_num/comments" \
--jq '.[] | "**\(.user.login)** on `\(.path):\(.line)`:\n\(.body)\n---"' 2>/dev/null)
[ -n "$review_comments" ] && feedback+="## Code Review Comments (address ALL of these)
$review_comments
"
# 3. PR reviews requesting changes
local reviews
reviews=$(gh api "repos/$REPO/pulls/$pr_num/reviews" \
--jq '.[] | select(.state != "APPROVED" and .state != "COMMENTED") | "**\(.user.login)** [\(.state)]:\n\(.body)\n---"' 2>/dev/null)
[ -n "$reviews" ] && feedback+="## Reviews Requesting Changes
$reviews
"
echo "$feedback"
}
# Detect stale session — if file is older than 2 hours, session is likely dead
if [ -f "$SESSION_FILE" ]; then
SESSION_AGE=$(( $(date +%s) - $(stat -f %m "$SESSION_FILE" 2>/dev/null || echo 0) ))
if [ "$SESSION_AGE" -gt 7200 ]; then
log "Session file is ${SESSION_AGE}s old (>2hr), starting fresh"
rm -f "$SESSION_FILE"
fi
fi
# ═══════════════════════════════════════════
# Phase 1: Initial implementation
# ═══════════════════════════════════════════
PROMPT=$(cat "$WT/agent-prompt.txt")
log "Phase 1: initial implementation for #$ISSUE"
run_agent "$PROMPT"
# Post-agent: verify + push + rebase + push + PR
cd "$WT" || exit 0
AHEAD=$(git rev-list origin/main..HEAD --count 2>/dev/null)
[ "$AHEAD" = "0" ] && { log "No commits produced, exiting"; exit 0; }
# Push raw commits first (safety net — work is on remote even if rebase fails)
git push origin "$BRANCH" --force-with-lease 2>/dev/null
# Then rebase for clean history
git fetch origin main 2>/dev/null
git rebase origin/main 2>/dev/null || { git rebase --abort 2>/dev/null; log "Rebase conflict, raw commits preserved on remote"; }
# Push rebased version
git push origin "$BRANCH" --force-with-lease 2>/dev/null || { log "Push failed after rebase"; }
# Create PR (or get existing PR number)
# Unregister from registry
bash /Users/junmingong/.openclaw/workspace/acestep-daw/scripts/agents/registry.sh unregister "$ISSUE" 2>/dev/null
gh pr create --repo "$REPO" --title "feat: #$ISSUE — $TITLE" \
--body "Closes #$ISSUE" --base main --head "$BRANCH" 2>/dev/null
PR_NUM=$(gh pr list --repo "$REPO" --head "$BRANCH" --json number -q '.[0].number' 2>/dev/null)
[ -z "$PR_NUM" ] && { log "Could not find PR for $BRANCH, exiting"; exit 0; }
log "PR #$PR_NUM created for #$ISSUE"
# ═══════════════════════════════════════════
# Phase 2: Feedback loop — same agent owns PR until merge
# ═══════════════════════════════════════════
ROUND=0
while [ $ROUND -lt $MAX_ROUNDS ]; do
ROUND=$((ROUND + 1))
log "Round $ROUND/$MAX_ROUNDS: waiting for CI on PR #$PR_NUM"
CI_RESULT=$(wait_for_ci "$PR_NUM")
log "CI result: $CI_RESULT"
# Check if PR was already merged
PR_STATE=$(gh pr view "$PR_NUM" --repo "$REPO" --json state -q '.state' 2>/dev/null)
[ "$PR_STATE" = "MERGED" ] && { log "PR #$PR_NUM merged! Agent done."; exit 0; }
# If CI passed, wait for review comments to arrive
if [ "$CI_RESULT" = "success" ]; then
log "CI green. Waiting 120s for reviews..."
sleep 120
fi
FEEDBACK=$(collect_feedback "$PR_NUM")
# No feedback + CI green = done
if [ -z "$FEEDBACK" ] && [ "$CI_RESULT" = "success" ]; then
log "CI green + no review feedback. PR #$PR_NUM ready to merge."
exit 0
fi
# Build fix prompt — no feedback but CI failed
if [ -z "$FEEDBACK" ]; then
FEEDBACK="## CI Failed
CI failed but no detailed logs captured. Run locally:
npx tsc --noEmit && npm test && npm run build
Fix any errors you find."
fi
# ── Resume the SAME agent to fix ──
FIX_PROMPT="Your PR #$PR_NUM has feedback that must be addressed (round $ROUND/$MAX_ROUNDS).
$FEEDBACK
Fix EVERY issue. Run quality gates before committing:
npx tsc --noEmit && npm test && npm run build
Then: git add -A && git commit -m 'fix: address feedback round $ROUND for #$ISSUE'
Do NOT push."
log "Resuming agent for fix round $ROUND"
cd "$WT" || exit 0
run_agent "$FIX_PROMPT"
# Push the fix
cd "$WT" || exit 0
# Push raw commits first (safety net — work is on remote even if rebase fails)
git push origin "$BRANCH" --force-with-lease 2>/dev/null
# Then rebase for clean history
git fetch origin main 2>/dev/null
git rebase origin/main 2>/dev/null || { git rebase --abort 2>/dev/null; log "Rebase conflict, raw commits preserved on remote"; }
# Push rebased version
git push origin "$BRANCH" --force-with-lease 2>/dev/null || { log "Push failed after rebase"; }
log "Fix pushed (round $ROUND), looping back to wait for CI"
done
log "WARN: PR #$PR_NUM not green after $MAX_ROUNDS rounds. Recording blocker."
echo "- [ ] PR #$PR_NUM (#$ISSUE): failed $MAX_ROUNDS fix rounds — needs human review" >> "$WT/.llm/BLOCKERS.md" 2>/dev/null
git add .llm/BLOCKERS.md 2>/dev/null && git commit -m "chore: record blocker for #$ISSUE" 2>/dev/null
git push origin "$BRANCH" --force-with-lease 2>/dev/null