-
Notifications
You must be signed in to change notification settings - Fork 0
478 lines (388 loc) · 17.7 KB
/
gen-update-plot.yml
File metadata and controls
478 lines (388 loc) · 17.7 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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
name: "Gen: Update Plot (Repair Loop)"
run-name: "Repair: ${{ github.event.pull_request.head.ref || github.event.inputs.pr_number }}"
on:
pull_request:
types: [labeled]
workflow_dispatch:
inputs:
pr_number:
description: 'PR number to repair'
required: true
type: number
# Prevent race conditions when multiple repairs trigger for same PR
concurrency:
group: repair-${{ github.event.pull_request.number || github.event.inputs.pr_number }}
cancel-in-progress: false
jobs:
update-plot:
name: Regenerate Plot from AI Feedback
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
issues: write
steps:
- name: Check conditions
id: check
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
# Handle workflow_dispatch trigger
if [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then
PR_NUM="${{ github.event.inputs.pr_number }}"
PR_DATA=$(gh pr view "$PR_NUM" --repo ${{ github.repository }} --json headRefName,labels)
BRANCH=$(echo "$PR_DATA" | jq -r '.headRefName')
HAS_REJECTED=$(echo "$PR_DATA" | jq -r '.labels[].name' | grep -c "ai-rejected" || echo "0")
if [[ ! "$BRANCH" =~ ^auto/ ]]; then
echo "::notice::Skipping: Branch '$BRANCH' is not auto/*"
echo "should_run=false" >> $GITHUB_OUTPUT
exit 0
fi
if [[ "$HAS_REJECTED" == "0" ]]; then
echo "::notice::Skipping: PR does not have ai-rejected label"
echo "should_run=false" >> $GITHUB_OUTPUT
exit 0
fi
echo "pr_number=$PR_NUM" >> $GITHUB_OUTPUT
echo "branch=$BRANCH" >> $GITHUB_OUTPUT
echo "should_run=true" >> $GITHUB_OUTPUT
exit 0
fi
# Handle pull_request labeled trigger
LABEL="${{ github.event.label.name }}"
BRANCH="${{ github.event.pull_request.head.ref }}"
if [[ "$LABEL" != "ai-rejected" ]]; then
echo "::notice::Skipping: Label is '$LABEL', not 'ai-rejected'"
echo "should_run=false" >> $GITHUB_OUTPUT
exit 0
fi
if [[ ! "$BRANCH" =~ ^auto/ ]]; then
echo "::notice::Skipping: Branch '$BRANCH' is not auto/*"
echo "should_run=false" >> $GITHUB_OUTPUT
exit 0
fi
echo "pr_number=${{ github.event.pull_request.number }}" >> $GITHUB_OUTPUT
echo "branch=$BRANCH" >> $GITHUB_OUTPUT
echo "should_run=true" >> $GITHUB_OUTPUT
- name: Checkout repository
if: steps.check.outputs.should_run == 'true'
uses: actions/checkout@v6
- name: Extract spec ID, library, and sub-issue
if: steps.check.outputs.should_run == 'true'
id: extract_info
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
BRANCH="${{ steps.check.outputs.branch }}"
PR_NUM="${{ steps.check.outputs.pr_number }}"
# Get PR body
PR_BODY=$(gh pr view "$PR_NUM" --json body -q '.body' 2>/dev/null || echo "")
# Extract from branch: auto/{spec-id}/{library}
SPEC_ID=$(echo "$BRANCH" | cut -d'/' -f2)
LIBRARY=$(echo "$BRANCH" | cut -d'/' -f3)
# Extract sub-issue from PR body (format: **Sub-Issue:** #NUM)
SUB_ISSUE=$(echo "$PR_BODY" | grep -oP '\*\*Sub-Issue:\*\* #\K\d+' | head -1 || echo "")
# Extract main issue from PR body (format: **Parent Issue:** #NUM)
MAIN_ISSUE=$(echo "$PR_BODY" | grep -oP '\*\*Parent Issue:\*\* #\K\d+' | head -1 || echo "")
echo "spec_id=$SPEC_ID" >> $GITHUB_OUTPUT
echo "library=$LIBRARY" >> $GITHUB_OUTPUT
echo "sub_issue=$SUB_ISSUE" >> $GITHUB_OUTPUT
echo "main_issue=$MAIN_ISSUE" >> $GITHUB_OUTPUT
echo "pr_number=$PR_NUM" >> $GITHUB_OUTPUT
echo "Spec: $SPEC_ID, Library: $LIBRARY, Sub-Issue: #$SUB_ISSUE"
- name: Check attempt count
if: steps.check.outputs.should_run == 'true'
id: check_attempts
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
PR_NUMBER="${{ steps.extract_info.outputs.pr_number }}"
LABELS=$(gh pr view $PR_NUMBER --json labels -q '.labels[].name')
# Count existing attempts
ATTEMPT=0
if echo "$LABELS" | grep -q "ai-attempt-1"; then ATTEMPT=1; fi
if echo "$LABELS" | grep -q "ai-attempt-2"; then ATTEMPT=2; fi
if echo "$LABELS" | grep -q "ai-attempt-3"; then ATTEMPT=3; fi
NEXT_ATTEMPT=$((ATTEMPT + 1))
if [ $NEXT_ATTEMPT -gt 3 ]; then
echo "max_reached=true" >> $GITHUB_OUTPUT
echo "Maximum attempts (3) reached"
else
echo "max_reached=false" >> $GITHUB_OUTPUT
echo "attempt=$NEXT_ATTEMPT" >> $GITHUB_OUTPUT
echo "Starting attempt $NEXT_ATTEMPT"
fi
- name: Mark as not-feasible if max attempts reached
if: steps.check.outputs.should_run == 'true' && steps.check_attempts.outputs.max_reached == 'true'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
PR_NUMBER="${{ steps.extract_info.outputs.pr_number }}"
SUB_ISSUE="${{ steps.extract_info.outputs.sub_issue }}"
LIBRARY="${{ steps.extract_info.outputs.library }}"
SPEC_ID="${{ steps.extract_info.outputs.spec_id }}"
# Remove ai-rejected, add not-feasible to PR
gh pr edit $PR_NUMBER --remove-label "ai-rejected" --add-label "not-feasible"
# Update sub-issue
if [ -n "$SUB_ISSUE" ]; then
gh issue edit $SUB_ISSUE \
--remove-label "ai-rejected" \
--remove-label "reviewing" \
--add-label "not-feasible"
fi
# Post failure message
gh pr comment $PR_NUMBER --body "## Not Feasible
Maximum repair attempts (3) reached for **$LIBRARY** implementation of \`$SPEC_ID\`.
This PR requires manual intervention or may indicate that this plot type
is not feasible in $LIBRARY.
**Options:**
1. Manual review and fix
2. Close this PR and mark $LIBRARY as unsupported
3. Wait for improved AI capabilities
---
:robot: *[gen-update-plot workflow](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }})*"
- name: Checkout code
if: steps.check.outputs.should_run == 'true' && steps.check_attempts.outputs.max_reached != 'true'
uses: actions/checkout@v6
with:
ref: ${{ steps.check.outputs.branch }}
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}
- name: Set up Python
if: steps.check.outputs.should_run == 'true' && steps.check_attempts.outputs.max_reached != 'true'
uses: actions/setup-python@v6
with:
python-version: '3.13'
- name: Install uv
if: steps.check.outputs.should_run == 'true' && steps.check_attempts.outputs.max_reached != 'true'
uses: astral-sh/setup-uv@v7
- name: Install dependencies
if: steps.check.outputs.should_run == 'true' && steps.check_attempts.outputs.max_reached != 'true'
run: |
uv sync
- name: Setup Chrome for Highcharts
if: steps.check.outputs.should_run == 'true' && steps.check_attempts.outputs.max_reached != 'true' && steps.extract_info.outputs.library == 'highcharts'
uses: browser-actions/setup-chrome@v1
with:
chrome-version: stable
- name: Get AI feedback from sub-issue
if: steps.check.outputs.should_run == 'true' && steps.check_attempts.outputs.max_reached != 'true'
id: get_feedback
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
SUB_ISSUE="${{ steps.extract_info.outputs.sub_issue }}"
if [ -z "$SUB_ISSUE" ]; then
echo "has_feedback=false" >> $GITHUB_OUTPUT
exit 0
fi
# Get ALL comments from sub-issue (includes all previous attempts)
ALL_COMMENTS=$(gh api repos/${{ github.repository }}/issues/$SUB_ISSUE/comments \
--jq '[.[] | .body] | join("\n\n---\n\n")' 2>/dev/null || echo "")
# Get latest AI Review comment
LATEST_FEEDBACK=$(gh api repos/${{ github.repository }}/issues/$SUB_ISSUE/comments \
--jq '[.[] | select(.body | contains("AI Review"))] | last | .body // empty')
if [ -n "$LATEST_FEEDBACK" ]; then
echo "$LATEST_FEEDBACK" > /tmp/latest_feedback.md
echo "$ALL_COMMENTS" > /tmp/all_attempts.md
echo "has_feedback=true" >> $GITHUB_OUTPUT
else
echo "has_feedback=false" >> $GITHUB_OUTPUT
fi
- name: Find current plot file
if: steps.check.outputs.should_run == 'true' && steps.check_attempts.outputs.max_reached != 'true'
id: read_plot
run: |
SPEC_ID="${{ steps.extract_info.outputs.spec_id }}"
LIBRARY="${{ steps.extract_info.outputs.library }}"
# Find plot file for this library and spec
PLOT_FILE=$(find plots/$LIBRARY -name "default.py" -path "*/${SPEC_ID}/*" 2>/dev/null | head -1 || echo "")
if [ -n "$PLOT_FILE" ] && [ -f "$PLOT_FILE" ]; then
echo "plot_file=$PLOT_FILE" >> $GITHUB_OUTPUT
echo "plot_exists=true" >> $GITHUB_OUTPUT
else
echo "plot_exists=false" >> $GITHUB_OUTPUT
fi
# Check spec file
if [ -f "specs/${SPEC_ID}.md" ]; then
echo "spec_exists=true" >> $GITHUB_OUTPUT
else
echo "spec_exists=false" >> $GITHUB_OUTPUT
fi
- name: Update labels
if: steps.check.outputs.should_run == 'true' && steps.check_attempts.outputs.max_reached != 'true'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
PR_NUMBER="${{ steps.extract_info.outputs.pr_number }}"
SUB_ISSUE="${{ steps.extract_info.outputs.sub_issue }}"
ATTEMPT="${{ steps.check_attempts.outputs.attempt }}"
# Remove ai-rejected, add attempt label
gh pr edit $PR_NUMBER --remove-label "ai-rejected" --add-label "ai-attempt-${ATTEMPT}"
# Update sub-issue
if [ -n "$SUB_ISSUE" ]; then
gh issue edit $SUB_ISSUE \
--remove-label "ai-rejected" \
--remove-label "reviewing" \
--add-label "generating"
fi
- name: Regenerate plot code
if: steps.check.outputs.should_run == 'true' && steps.check_attempts.outputs.max_reached != 'true' && steps.get_feedback.outputs.has_feedback == 'true' && steps.read_plot.outputs.plot_exists == 'true'
env:
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
run: |
SPEC_ID="${{ steps.extract_info.outputs.spec_id }}"
LIBRARY="${{ steps.extract_info.outputs.library }}"
PLOT_FILE="${{ steps.read_plot.outputs.plot_file }}"
ATTEMPT="${{ steps.check_attempts.outputs.attempt }}"
echo "Regenerating $LIBRARY plot (Attempt $ATTEMPT)"
# Read current code
CURRENT_CODE=$(cat "$PLOT_FILE")
# Read spec
SPEC_CONTENT=$(cat "specs/${SPEC_ID}.md")
# Read library-specific rules
LIBRARY_RULES=$(cat "prompts/library/${LIBRARY}.md" 2>/dev/null || echo "No library-specific rules found")
# Read style guide
STYLE_GUIDE=$(cat "prompts/default-style-guide.md" 2>/dev/null || echo "No style guide found")
# Read all previous attempts
ALL_ATTEMPTS=$(cat /tmp/all_attempts.md 2>/dev/null || echo "No previous attempts")
# Read latest feedback
LATEST_FEEDBACK=$(cat /tmp/latest_feedback.md)
# Create improvement prompt
cat > /tmp/improve_prompt.txt << 'PROMPTEOF'
You are improving a plot implementation based on AI review feedback.
## Library: $LIBRARY
## Current Code:
```python
$CURRENT_CODE
```
## Specification:
$SPEC_CONTENT
## Library-Specific Rules:
$LIBRARY_RULES
## Visual Style Guide:
$STYLE_GUIDE
## Previous Attempts and Feedback (IMPORTANT - learn from these):
$ALL_ATTEMPTS
## Latest AI Review Feedback:
$LATEST_FEEDBACK
## Task:
Improve the code to address ALL feedback from the latest review. Pay special attention to:
1. Fix any critical issues mentioned (VQ-xxx, CQ-xxx, CR-xxx codes)
2. Review previous attempts to avoid repeating the same mistakes
3. Implement improvement suggestions where feasible
4. Maintain code quality and readability
5. Keep the same function signature
Return ONLY the improved Python code, no explanations or markdown formatting.
PROMPTEOF
# Replace placeholders
python3 << PYEOF
import re
with open('/tmp/improve_prompt.txt', 'r') as f:
content = f.read()
replacements = {
'\$LIBRARY': '''$LIBRARY''',
'\$CURRENT_CODE': '''$CURRENT_CODE''',
'\$SPEC_CONTENT': '''$SPEC_CONTENT''',
'\$LIBRARY_RULES': '''$LIBRARY_RULES''',
'\$STYLE_GUIDE': '''$STYLE_GUIDE''',
'\$ALL_ATTEMPTS': '''$ALL_ATTEMPTS''',
'\$LATEST_FEEDBACK': '''$LATEST_FEEDBACK'''
}
for key, value in replacements.items():
content = content.replace(key, value)
with open('/tmp/improve_prompt.txt', 'w') as f:
f.write(content)
PYEOF
# Call Claude API (with timeout)
RESPONSE=$(curl -s --max-time 120 --connect-timeout 10 https://api.anthropic.com/v1/messages \
-H "Content-Type: application/json" \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-d "{
\"model\": \"claude-sonnet-4-20250514\",
\"max_tokens\": 8192,
\"messages\": [{
\"role\": \"user\",
\"content\": $(cat /tmp/improve_prompt.txt | jq -Rs .)
}]
}")
# Validate API response
if ! echo "$RESPONSE" | jq -e '.content[0].text' > /dev/null 2>&1; then
echo "::error::API response invalid or empty"
echo "Response: $(echo "$RESPONSE" | jq -r '.error // .' 2>/dev/null || echo "$RESPONSE")"
exit 1
fi
# Extract improved code
IMPROVED_CODE=$(echo "$RESPONSE" | jq -r '.content[0].text // empty')
if [ -n "$IMPROVED_CODE" ]; then
# Clean up markdown if present
CLEAN_CODE=$(echo "$IMPROVED_CODE" | sed -n '/^```python/,/^```$/p' | sed '1d;$d')
if [ -z "$CLEAN_CODE" ]; then
CLEAN_CODE="$IMPROVED_CODE"
fi
# Write improved code
echo "$CLEAN_CODE" > "$PLOT_FILE"
echo "Updated: $PLOT_FILE"
else
echo "No improvement generated"
echo "API Response: $RESPONSE"
fi
- name: Commit and push changes
if: steps.check.outputs.should_run == 'true' && steps.check_attempts.outputs.max_reached != 'true'
run: |
SPEC_ID="${{ steps.extract_info.outputs.spec_id }}"
LIBRARY="${{ steps.extract_info.outputs.library }}"
ATTEMPT="${{ steps.check_attempts.outputs.attempt }}"
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
# Check if there are changes
if git diff --quiet; then
echo "No changes to commit"
else
git add plots/
git commit -m "fix($LIBRARY): address AI review feedback for $SPEC_ID (attempt $ATTEMPT/3)"
git push
echo "Changes pushed"
fi
- name: Post update to sub-issue
if: steps.check.outputs.should_run == 'true' && steps.check_attempts.outputs.max_reached != 'true'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
SUB_ISSUE="${{ steps.extract_info.outputs.sub_issue }}"
PR_NUMBER="${{ steps.extract_info.outputs.pr_number }}"
ATTEMPT="${{ steps.check_attempts.outputs.attempt }}"
LIBRARY="${{ steps.extract_info.outputs.library }}"
SPEC_ID="${{ steps.extract_info.outputs.spec_id }}"
PLOT_FILE="${{ steps.read_plot.outputs.plot_file }}"
if [ -z "$SUB_ISSUE" ]; then
echo "No sub-issue to update"
exit 0
fi
# Read the updated code
if [ -f "$PLOT_FILE" ]; then
UPDATED_CODE=$(cat "$PLOT_FILE")
else
UPDATED_CODE="File not found"
fi
# Create comment with updated code (for documentation/learning)
cat > /tmp/update_comment.md << COMMENTEOF
## Attempt $ATTEMPT/3 - $(date -u +"%Y-%m-%dT%H:%M:%SZ")
### Regenerated Code
\`\`\`python
$UPDATED_CODE
\`\`\`
### Status
- **PR:** #$PR_NUMBER
- **File:** \`$PLOT_FILE\`
- **Action:** Code regenerated based on AI feedback
Waiting for tests and new AI review...
---
:robot: *[gen-update-plot workflow](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }})*
COMMENTEOF
gh issue comment "$SUB_ISSUE" --body-file /tmp/update_comment.md
- name: Cleanup temporary files
if: always()
run: |
rm -f /tmp/improve_prompt.txt /tmp/latest_feedback.md /tmp/all_attempts.md /tmp/update_comment.md