-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimplement.py
More file actions
342 lines (284 loc) · 13.5 KB
/
implement.py
File metadata and controls
342 lines (284 loc) · 13.5 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
"""Implement task — implement the approved plan, open PR, assign reviewer.
After implementation, assigns the issue author as PR reviewer and sets
status to ``pr_open`` for review monitoring.
"""
import logging
import subprocess
from pathlib import Path
from clayde.claude import InvocationTimeoutError, UsageLimitError, format_cost_line, invoke_claude
from clayde.config import DATA_DIR, get_github_client, get_settings
from clayde.git import ensure_repo
from clayde.prompts import collect_comments_after, render_template
from clayde.github import (
add_pr_reviewer,
create_pull_request,
fetch_comment,
fetch_issue,
fetch_issue_comments,
find_open_pr,
get_default_branch,
get_issue_author,
get_pr_title,
issue_ref,
parse_issue_url,
parse_pr_url,
post_comment,
)
from clayde.responses import ImplementResponse, parse_response
from clayde.safety import filter_comments
from clayde.state import IssueStatus, accumulate_cost, get_issue_state, pop_accumulated_cost, update_issue_state
from clayde.telemetry import get_tracer
log = logging.getLogger("clayde.tasks.implement")
def _delete_conversation_file(owner: str, repo: str, number: int) -> None:
"""Remove the conversation file for an issue so stale sessions don't persist."""
path = DATA_DIR / "conversations" / f"{owner}__{repo}__issue-{number}.json"
if path.exists():
path.unlink()
log.info("Deleted conversation file %s", path)
def run(issue_url: str) -> None:
tracer = get_tracer()
with tracer.start_as_current_span("clayde.task.implement") as span:
g = get_github_client()
owner, repo, number = parse_issue_url(issue_url)
issue_state = get_issue_state(issue_url)
span.set_attribute("issue.number", number)
span.set_attribute("issue.owner", owner)
span.set_attribute("issue.repo", repo)
resumed = issue_state.get("status") == IssueStatus.INTERRUPTED
span.set_attribute("implement.resumed_from_interrupted", resumed)
if resumed and _try_resume_from_existing_pr(g, owner, repo, number, issue_url, issue_state, span):
return
if not resumed:
_delete_conversation_file(owner, repo, number)
update_issue_state(issue_url, {"status": IssueStatus.IMPLEMENTING})
issue, default_branch, repo_path, branch_name, prompt, conversation_path = (
_prepare_implementation_context(g, owner, repo, number, issue_url, issue_state, resumed)
)
log.info("[%s: %s] Invoking Claude for implementation", issue_ref(owner, repo, number), issue.title)
try:
result = invoke_claude(
prompt,
repo_path,
branch_name=branch_name,
conversation_path=conversation_path,
)
except UsageLimitError as e:
log.warning("[%s: %s] Usage limit hit during implementation — will retry next cycle", issue_ref(owner, repo, number), issue.title)
accumulate_cost(issue_url, e.cost_eur)
span.set_attribute("implement.status", "limit")
update_issue_state(issue_url, {
"status": IssueStatus.INTERRUPTED,
"interrupted_phase": IssueStatus.IMPLEMENTING,
})
return
except InvocationTimeoutError as e:
log.warning("[%s: %s] Timed out during implementation — will resume next cycle", issue_ref(owner, repo, number), issue.title)
accumulate_cost(issue_url, e.cost_eur)
span.set_attribute("implement.status", "timeout")
update_issue_state(issue_url, {
"status": IssueStatus.INTERRUPTED,
"interrupted_phase": IssueStatus.IMPLEMENTING,
})
return
total_cost = pop_accumulated_cost(issue_url) + result.cost_eur
# Parse JSON response to extract summary
summary = None
try:
parsed = parse_response(result.output, ImplementResponse)
summary = parsed.summary
except ValueError as e:
log.warning("[%s: %s] Failed to parse implement response JSON: %s", issue_ref(owner, repo, number), issue.title, e)
pr_url = _find_or_create_pr(g, owner, repo, number, branch_name, default_branch, total_cost, issue,
repo_path=repo_path, summary=summary)
if pr_url:
_assign_reviewer_and_finish(g, owner, repo, number, issue_url, pr_url, span, cost_eur=total_cost)
else:
log.error("[%s: %s] Implementation produced no PR", issue_ref(owner, repo, number), issue.title)
log.error("Claude output (last 2000 chars): %s", (result.output or "")[-2000:])
_handle_no_pr(g, owner, repo, number, issue_url, issue_state, span)
def _try_resume_from_existing_pr(g, owner, repo, number, issue_url, issue_state, span) -> bool:
"""If a PR already exists for the WIP branch, finish without re-running Claude.
Returns True if we found an existing PR and finished; False otherwise.
"""
branch_name = issue_state.get("branch_name", f"clayde/issue-{number}")
existing_pr = find_open_pr(g, owner, repo, branch_name)
if not existing_pr:
return False
log.info("Resuming interrupted #%d — found existing PR %s", number, existing_pr)
accumulated = pop_accumulated_cost(issue_url)
_assign_reviewer_and_finish(
g, owner, repo, number, issue_url, existing_pr, span,
cost_eur=accumulated if accumulated > 0 else None,
)
return True
def _prepare_implementation_context(g, owner, repo, number, issue_url, issue_state, resumed):
"""Fetch all resources needed to run Claude and return them as a tuple."""
plan_comment_id = issue_state.get("plan_comment_id") or issue_state["preliminary_comment_id"]
issue = fetch_issue(g, owner, repo, number)
default_branch = get_default_branch(g, owner, repo)
repo_path = ensure_repo(owner, repo, default_branch)
plan_comment = fetch_comment(g, owner, repo, number, plan_comment_id)
plan_text = plan_comment.body
branch_name = issue_state.get("branch_name") or f"clayde/issue-{number}"
update_issue_state(issue_url, {"branch_name": branch_name})
if resumed:
_checkout_wip_branch(repo_path, branch_name)
all_comments = fetch_issue_comments(g, owner, repo, number)
visible_comments = filter_comments(all_comments)
discussion_text = collect_comments_after(visible_comments, plan_comment_id)
prompt = _build_prompt(issue, plan_text, discussion_text, owner, repo, number, repo_path, branch_name)
conversation_path = DATA_DIR / "conversations" / f"{owner}__{repo}__issue-{number}.json"
return issue, default_branch, repo_path, branch_name, prompt, conversation_path
def _ensure_branch_pushed(repo_path: Path, branch_name: str) -> bool:
"""Check if branch exists on remote; if not, try to push it.
Returns True if the branch is on the remote after this call.
"""
result = subprocess.run(
["git", "ls-remote", "--heads", "origin", branch_name],
cwd=str(repo_path), capture_output=True, text=True,
)
if result.stdout.strip():
return True
# Branch not on remote — check if it exists locally and push it
result = subprocess.run(
["git", "branch", "--list", branch_name],
cwd=str(repo_path), capture_output=True, text=True,
)
if not result.stdout.strip():
log.error("Branch %s does not exist locally or on remote", branch_name)
return False
log.info("Branch %s exists locally but not on remote — pushing", branch_name)
result = subprocess.run(
["git", "push", "origin", branch_name],
cwd=str(repo_path), capture_output=True, text=True,
)
if result.returncode != 0:
log.error("Failed to push branch %s: %s", branch_name, result.stderr)
return False
return True
def _find_or_create_pr(g, owner, repo, number, branch_name, default_branch, total_cost, issue,
repo_path: Path | None = None, summary: str | None = None) -> str | None:
"""Return the PR URL for branch_name, creating it if necessary."""
pr_url = find_open_pr(g, owner, repo, branch_name)
if pr_url:
return pr_url
if repo_path and not _ensure_branch_pushed(repo_path, branch_name):
log.error("Cannot create PR for #%d: branch %s not available on remote", number, branch_name)
return None
try:
if summary:
pr_body = f"Closes #{number}\n\n{summary}{format_cost_line(total_cost)}"
else:
pr_body = f"Closes #{number}{format_cost_line(total_cost)}"
pr_url = create_pull_request(
g, owner, repo,
title=f"Fix #{number}: {issue.title}",
body=pr_body,
head=branch_name,
base=default_branch,
)
log.info("Created PR: %s", pr_url)
return pr_url
except Exception as e:
log.error("Failed to create PR for #%d: %s", number, e)
return None
def _handle_no_pr(g, owner, repo, number, issue_url, issue_state, span) -> None:
"""Handle the case where implementation produced no PR — retry or give up."""
max_retries = get_settings().implement_max_retries
retry_count = issue_state.get("retry_count", 0) + 1
if retry_count >= max_retries:
log.error("Issue #%d failed after %d retries — giving up", number, retry_count)
_delete_conversation_file(owner, repo, number)
post_comment(g, owner, repo, number,
"Implementation failed to produce a PR after multiple retries. "
"Marking as failed — manual intervention needed.")
span.set_attribute("implement.status", "failed")
update_issue_state(issue_url, {"status": IssueStatus.FAILED, "retry_count": retry_count})
else:
post_comment(g, owner, repo, number,
f"Implementation ran but no PR was created "
f"(attempt {retry_count}/{max_retries}). "
"I'll retry on the next cycle.")
span.set_attribute("implement.status", "no_pr")
span.set_attribute("implement.retry_count", retry_count)
update_issue_state(issue_url, {
"status": IssueStatus.INTERRUPTED,
"interrupted_phase": IssueStatus.IMPLEMENTING,
"retry_count": retry_count,
})
def _assign_reviewer_and_finish(g, owner, repo, number, issue_url, pr_url, span,
cost_eur=None):
"""Post result, assign reviewer, set status to pr_open."""
_post_result(g, owner, repo, number, pr_url, cost_eur=cost_eur)
_, _, pr_number = parse_pr_url(pr_url)
pr_title = None
try:
pr_title = get_pr_title(g, owner, repo, pr_number)
except Exception as e:
log.warning("Failed to fetch PR title for PR %s: %s", pr_url, e)
try:
issue_author = get_issue_author(g, owner, repo, number)
settings = get_settings()
if issue_author.lower() == settings.github_username.lower():
log.info("Issue author is %s (self) — skipping reviewer assignment", issue_author)
else:
add_pr_reviewer(g, owner, repo, pr_number, issue_author)
except Exception as e:
log.warning("Failed to assign reviewer for PR %s: %s", pr_url, e)
state_update = {
"status": IssueStatus.PR_OPEN,
"pr_url": pr_url,
"last_seen_review_id": 0,
}
if pr_title:
state_update["pr_title"] = pr_title
update_issue_state(issue_url, state_update)
span.set_attribute("implement.status", "pr_open")
span.set_attribute("implement.pr_url", pr_url)
ref = issue_ref(owner, repo, number)
label = f"{ref}: {pr_title}" if pr_title else ref
log.info("[%s] PR open — monitoring for reviews: %s", label, pr_url)
def _checkout_wip_branch(repo_path, branch_name: str) -> None:
"""Checkout an existing WIP branch if it exists (locally or on remote)."""
cwd = str(repo_path)
result = subprocess.run(
["git", "branch", "--list", branch_name],
cwd=cwd, capture_output=True, text=True,
)
if result.stdout.strip():
subprocess.run(["git", "checkout", branch_name], cwd=cwd, capture_output=True)
subprocess.run(["git", "pull", "origin", branch_name], cwd=cwd, capture_output=True)
log.info("Resumed WIP branch %s (local)", branch_name)
return
result = subprocess.run(
["git", "ls-remote", "--heads", "origin", branch_name],
cwd=cwd, capture_output=True, text=True,
)
if result.stdout.strip():
subprocess.run(
["git", "checkout", "-b", branch_name, f"origin/{branch_name}"],
cwd=cwd, capture_output=True,
)
log.info("Resumed WIP branch %s (remote)", branch_name)
return
log.info("No existing WIP branch %s found — starting fresh", branch_name)
def _build_prompt(issue, plan_text: str, discussion_text: str, owner: str, repo: str,
number: int, repo_path: str, branch_name: str) -> str:
return render_template(
"implement.j2",
number=number,
title=issue.title,
owner=owner,
repo=repo,
body=issue.body or "(empty)",
plan_text=plan_text,
discussion_text=discussion_text,
repo_path=repo_path,
branch_name=branch_name,
)
def _post_result(g, owner: str, repo: str, number: int, pr_url: str,
cost_eur: float | None = None) -> None:
body = f"Implementation complete — PR opened: {pr_url}"
if cost_eur is not None:
body += format_cost_line(cost_eur)
post_comment(g, owner, repo, number, body)