-
Notifications
You must be signed in to change notification settings - Fork 0
316 lines (274 loc) · 12.4 KB
/
fix-issue.yml
File metadata and controls
316 lines (274 loc) · 12.4 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
# Fix a GitHub issue with an AI agent
# This workflow runs an AI agent to address an issue and optionally creates a PR
# with the changes. Unlike eval-agent-on-issue.yml, this is not an evaluation -
# it's a direct attempt to solve the issue.
#
# Required secrets: ANTHROPIC_API_KEY or CLAUDE_CODE_OAUTH_TOKEN
# Optional secrets: GH_PAT (for private cross-repo access)
#
# ============================================================================
# WORKFLOW STRATEGY: Direct Issue Resolution
# ============================================================================
# This workflow addresses an issue directly without evaluation:
#
# 1. Checkout the repository at the default branch
# 2. Run Claude Code agent on the issue
# 3. Agent commits changes (if any)
# 4. Optionally create a PR with the changes
#
# Unlike eval-agent-on-issue.yml:
# - No evaluation baseline (eval_base_branch)
# - No comparison to existing PR
# - Changes go directly to solution branch
# - Simple, direct issue fixing
#
# ============================================================================
name: Fix an issue with AI agent
run-name: "fix-agent: issue #${{ inputs.issue_number }}; model: ${{ inputs.model }}"
on:
workflow_dispatch:
inputs:
issue_number:
description: "The issue number to fix"
required: true
model:
description: "Claude model to use"
required: true
type: choice
options:
# Current models (4.5)
- claude-sonnet-4-5-20250929
- claude-haiku-4-5-20251001
- claude-opus-4-5-20251101
# Legacy models (4.x)
- claude-opus-4-1-20250805
- claude-sonnet-4-20250514
- claude-opus-4-20250514
# Legacy models (3.x)
- claude-3-7-sonnet-20250219
- claude-3-haiku-20240307
create_pr:
description: "Whether to create a PR with the changes. Defaults to true."
required: false
default: "true"
target_branch:
description: "Target branch for PR. Defaults to main. Use a feature branch name for draft PRs."
required: false
default: "main"
repo_url_prefix:
description: "URL prefix for issue links. Defaults to https://href.li/? for indirect links. Set to empty string for direct GitHub links."
required: false
default: "https://href.li/?"
container:
description: "Optional container image to run in. Leave empty for host runner."
required: false
default: ""
validation_command:
description: "Optional command to validate agent changes (e.g., 'make test'). Job fails if validation fails."
required: false
default: ""
artifact_retention_days:
description: "Number of days to retain workflow artifacts"
required: false
default: "90"
timeout_minutes:
description: "Job timeout in minutes"
required: false
default: "30"
env:
AGENT_VERSION: v1
jobs:
fix-issue:
runs-on: ubuntu-latest
timeout-minutes: ${{ fromJson(inputs.timeout_minutes) }}
container: ${{ inputs.container || null }}
permissions:
contents: write
pull-requests: write
steps:
- name: Set agent config name
run: |
# Sanitize inputs for branch name
MODEL_PART=$(echo "${{ inputs.model }}" | tr '/' '-')
AGENT_BRANCH_NAME="fix-agent-issue-${{ inputs.issue_number }}-${MODEL_PART}"
echo "agent_branch_name=${AGENT_BRANCH_NAME}" >> $GITHUB_ENV
echo "model_short=$(echo "${{ inputs.model }}" | sed -E 's/claude-([a-z]+)-([0-9]+)-([0-9]+).*/\1-\2.\3/')" >> $GITHUB_ENV
- name: Checkout repository at default branch
uses: actions/checkout@v4
with:
fetch-depth: 1
- name: Install uv
uses: astral-sh/setup-uv@v5
- name: Gather issue context
id: issue_context
env:
GH_TOKEN: ${{ secrets.GH_PAT || secrets.GITHUB_TOKEN }}
run: |
# Fetch issue details and all comments
gh issue view ${{ inputs.issue_number }} --json url,title,body,comments > __issue_context__.json
# Extract issue title and URL
ISSUE_TITLE=$(jq -r '.title' __issue_context__.json)
ISSUE_URL=$(jq -r '.url' __issue_context__.json)
echo "issue_title=${ISSUE_TITLE}" >> $GITHUB_OUTPUT
echo "issue_url=${ISSUE_URL}" >> $GITHUB_OUTPUT
echo "Issue #${{ inputs.issue_number }}: ${ISSUE_TITLE}"
- name: Create fix branch
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
# Create and checkout fix branch
git checkout -b "${{ env.agent_branch_name }}"
echo "✓ Created branch: ${{ env.agent_branch_name }}"
- name: Run Claude Code
uses: anthropics/claude-code-base-action@beta
with:
prompt: |
Your job is to address issue #${{ inputs.issue_number }}.
First, read the file __issue_context__.json in the current working directory to get the issue title, body, and comments.
IMPORTANT: You are working directly in ${{ github.repository }}.
This is a direct issue fix, not an evaluation.
On completion, commit your changes locally. Do NOT push - that will be handled automatically.
Don't commit files you did not edit.
After committing, create a file:
SOLUTION_NOTES.md: Description of your changes and rationale
I will take care of creating the PR and pushing to GitHub, you just need to create this file.
Don't commit it, and don't attempt to push.
If the original issue is not clear, ask for clarification in SOLUTION_NOTES.md, and do not
commit any changes.
Your SOLUTION_NOTES.md should be a human-readable description of your changes, the rationale,
and any decisions you made. Include any checklists you created for yourself and completed.
If you performed validation, background research, etc, be sure to mention this.
Follow any CODE_OF_CONDUCT.md or other relevant files in the repository.
allowed_tools: "Bash,Read,Write,Edit,Glob,Grep,LS,MultiEdit,NotebookEdit,TodoRead,TodoWrite,WebFetch,WebSearch"
model: ${{ inputs.model }}
timeout_minutes: ${{ inputs.timeout_minutes }}
claude_code_oauth_token: ${{ secrets.CLAUDE_CODE_OAUTH_TOKEN }}
anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
- name: Add signature to solution notes
run: |
# Metadata signature includes: model and link to workflow run
SIGNATURE="---
🤖 **Generated by Claude Code Agent**
- Model: \`${{ inputs.model }}\`
- Run: [View workflow run](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }})"
if [ -f "SOLUTION_NOTES.md" ]; then
echo "" >> SOLUTION_NOTES.md
echo "$SIGNATURE" >> SOLUTION_NOTES.md
fi
# Run validation command if specified (e.g., make test)
- name: Validate agent changes
if: ${{ inputs.validation_command != '' }}
run: |
echo "Running validation: ${{ inputs.validation_command }}"
echo "========================================"
${{ inputs.validation_command }}
echo "========================================"
echo "Validation passed"
- name: Commit and push branch
run: |
# Create commit with metadata
git commit --allow-empty -m "fix-agent: issue #${{ inputs.issue_number }} (${{ env.model_short }})
Model: ${{ inputs.model }}
Artifacts: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}#artifacts"
git push -u origin "${{ env.agent_branch_name }}"
echo "✓ Pushed branch: ${{ env.agent_branch_name }}"
- name: Create PR
id: create_pr
if: ${{ inputs.create_pr == 'true' }}
env:
GH_TOKEN: ${{ secrets.GH_PAT || secrets.GITHUB_TOKEN }}
run: |
# Apply repo_url_prefix to issue link
# By default, links are prefixed with https://href.li/? to create indirect
# redirects. This prevents GitHub's "reciprocal view" from cluttering the issue.
ISSUE_URL="${{ inputs.repo_url_prefix }}${{ steps.issue_context.outputs.issue_url }}"
# Get commit messages from agent's work
COMMIT_LOG=$(git log --oneline origin/${{ inputs.target_branch }}.."${{ env.agent_branch_name }}" --pretty=format:"- %s" || echo "No commits from agent")
echo "Agent commits:"
echo "$COMMIT_LOG"
# Read agent output file
SOLUTION_NOTES=""
if [ -f "SOLUTION_NOTES.md" ]; then
SOLUTION_NOTES=$(cat SOLUTION_NOTES.md)
fi
# ========================================================================
# Build PR body
# ========================================================================
PR_BODY="## Fixes
[#${{ inputs.issue_number }}: ${{ steps.issue_context.outputs.issue_title }}](${ISSUE_URL})
## Agent Changes
${COMMIT_LOG}"
if [ -n "$SOLUTION_NOTES" ]; then
PR_BODY="${PR_BODY}
## Solution Details
${SOLUTION_NOTES}"
fi
# Add config and footer
REPO_URL_PREFIX_DISPLAY="${{ inputs.repo_url_prefix }}"
if [ -z "$REPO_URL_PREFIX_DISPLAY" ]; then
REPO_URL_PREFIX_DISPLAY="(direct links - no prefix)"
fi
PR_BODY="${PR_BODY}
## Agent Config
| Parameter | Value |
|-----------|-------|
| Model | \`${{ inputs.model }}\` |
| URL prefix | \`${REPO_URL_PREFIX_DISPLAY}\` |
See workflow artifacts for full Claude trace."
# Create the PR
PR_URL=$(gh pr create \
--base "${{ inputs.target_branch }}" \
--head "${{ env.agent_branch_name }}" \
--title "fix: issue #${{ inputs.issue_number }} - ${{ steps.issue_context.outputs.issue_title }} (${{ env.model_short }})" \
--body "$PR_BODY" \
--draft)
# Extract PR number from URL
CREATED_PR_NUMBER=$(echo "$PR_URL" | grep -oE '[0-9]+$')
echo "created_pr_url=${PR_URL}" >> $GITHUB_OUTPUT
echo "created_pr_number=${CREATED_PR_NUMBER}" >> $GITHUB_OUTPUT
echo "Created PR #${CREATED_PR_NUMBER}: ${PR_URL}"
- name: Upload issue context
uses: actions/upload-artifact@v4
with:
name: issue-context-${{ github.run_id }}
path: __issue_context__.json
retention-days: ${{ inputs.artifact_retention_days }}
- name: Upload Claude output
uses: actions/upload-artifact@v4
with:
name: claude-response-${{ github.run_id }}
path: /home/runner/work/_temp/claude-execution-output.json
retention-days: ${{ inputs.artifact_retention_days }}
- name: Upload solution notes
uses: actions/upload-artifact@v4
if: ${{ hashFiles('SOLUTION_NOTES.md') != '' }}
with:
name: solution-notes-${{ github.run_id }}
path: SOLUTION_NOTES.md
retention-days: ${{ inputs.artifact_retention_days }}
- name: Save run metadata
run: |
cat > /tmp/run-metadata.json << 'EOF'
{
"run_id": ${{ github.run_id }},
"repo": "${{ github.repository }}",
"workflow": "${{ github.workflow }}",
"inputs": {
"issue_number": "${{ inputs.issue_number }}",
"model": "${{ inputs.model }}",
"target_branch": "${{ inputs.target_branch }}",
"repo_url_prefix": "${{ inputs.repo_url_prefix }}",
"create_pr": "${{ inputs.create_pr }}"
},
"branch": "${{ env.agent_branch_name }}",
"created_pr_number": "${{ steps.create_pr.outputs.created_pr_number || '' }}",
"created_pr_url": "${{ steps.create_pr.outputs.created_pr_url || '' }}",
"issue_url": "${{ steps.issue_context.outputs.issue_url }}"
}
EOF
- name: Upload run metadata
uses: actions/upload-artifact@v4
with:
name: run-metadata-${{ github.run_id }}
path: /tmp/run-metadata.json
retention-days: ${{ inputs.artifact_retention_days }}