-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathrun_agent.py
More file actions
545 lines (495 loc) · 21.6 KB
/
run_agent.py
File metadata and controls
545 lines (495 loc) · 21.6 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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
import os
import yaml
import multiprocessing
from datasets import load_dataset
from git import Repo
from agent.agent_utils import (
create_branch,
get_message,
get_message_function_by_function,
run_eval_after_each_commit,
get_target_edit_files,
get_changed_files_from_commits,
update_message_with_dependencies,
get_lint_cmd,
read_yaml_config,
)
from agent.agents import AgentReturn
import json
import subprocess
from agent.agents import AiderAgents
from typing import Optional, Type, cast
from types import TracebackType
from agent.class_types import AgentConfig
from commit0.harness.constants import SPLIT
from commit0.harness.get_pytest_ids import main as get_tests
from commit0.harness.constants import RUN_AGENT_LOG_DIR, RepoInstance
from commit0.cli import read_commit0_config_file
from pathlib import Path
from datetime import datetime
from agent.display import TerminalDisplay
import queue
import time
class DirContext:
def __init__(self, d: str):
self.dir = d
self.cwd = os.getcwd()
def __enter__(self):
os.chdir(self.dir)
def __exit__(
self,
exctype: Optional[Type[BaseException]],
excinst: Optional[BaseException],
exctb: Optional[TracebackType],
) -> None:
os.chdir(self.cwd)
def run_agent_multiple_times_on_same_inquiry(
agent: AiderAgents,
repo: Repo,
branch: str,
message: str,
fnames: list[str],
test_cmd: str,
test_first: bool,
lint_cmd: str,
lint_first: bool,
log_dir: Path,
repeat_times_for_each_inquiry: int,
backend: str,
commit0_config_file: str,
) -> AgentReturn | None:
"""Run agent multiple times on the same inquiry and return the best performing agent return"""
if repeat_times_for_each_inquiry == 1:
return agent.run(
message, test_cmd, lint_cmd, fnames, log_dir, test_first, lint_first
)
else:
commit_before_run = repo.head.commit.hexsha
commit_results = {}
best_commit_diff = ""
best_eval_result = float("-inf")
best_agent_return = None
for attempt in range(repeat_times_for_each_inquiry):
agent_return = agent.run(
message,
test_cmd,
lint_cmd,
fnames,
log_dir,
test_first,
lint_first,
attempt,
)
current_commit = repo.head.commit.hexsha
eval_result = run_eval_after_each_commit(
branch, backend, commit0_config_file
)
# Get diff and store results
diff = repo.git.diff(commit_before_run, current_commit)
commit_results[current_commit] = {"eval_result": eval_result, "diff": diff}
# print("current_commit: ", current_commit)
# print("commit_results: ", eval_result.split("average pass rate: ")[-1] if "average pass rate: " in eval_result else 0)
# with open("/home/nan/commit0_rebuttal/tmp.json", "w") as fuck:
# json.dump(commit_results, fuck)
# Track best performing commit's diff
score = float(
eval_result.split("average pass rate: ")[-1]
if "average pass rate: " in eval_result
else 0
)
if best_commit_diff is None:
# if score > best_eval_result:
best_eval_result = score
best_commit_diff = diff
best_agent_return = agent_return
else:
if score > best_eval_result:
best_eval_result = score
best_commit_diff = diff
best_agent_return = agent_return
repo.git.reset("--hard", commit_before_run)
with open(log_dir.resolve() / "eval_results.json", "w") as f:
json.dump(commit_results, f, indent=4)
patch_path = os.path.abspath(str(log_dir.resolve() / "best_diff.patch"))
with open(patch_path, "w") as f:
f.write(best_commit_diff + "\n")
repo.git.execute(["git", "apply", patch_path])
repo.git.add(fnames)
repo.git.commit("-m", f"Applied best performing changes for {fnames}")
return best_agent_return
def run_agent_for_repo(
repo_base_dir: str,
agent_config: AgentConfig,
example: RepoInstance,
branch: str,
update_queue: multiprocessing.Queue,
override_previous_changes: bool = False,
backend: str = "modal",
log_dir: str = str(RUN_AGENT_LOG_DIR.resolve()),
commit0_config_file: str = "",
) -> None:
"""Run Aider for a given repository."""
# get repo info
commit0_config = read_commit0_config_file(commit0_config_file)
assert "commit0" in commit0_config["dataset_name"]
_, repo_name = example["repo"].split("/")
# before starting, display all information to terminal
update_queue.put(("start_repo", (repo_name, 0)))
# repo_name = repo_name.lower()
# repo_name = repo_name.replace(".", "-")
repo_path = os.path.join(repo_base_dir, repo_name)
repo_path = os.path.abspath(repo_path)
try:
local_repo = Repo(repo_path)
except Exception:
raise Exception(
f"{repo_path} is not a git repo. Check if base_dir is correctly specified."
)
if agent_config.agent_name == "aider":
agent = AiderAgents(agent_config.max_iteration, agent_config.model_name)
else:
raise NotImplementedError(
f"{agent_config.agent_name} is not implemented; please add your implementations in baselines/agents.py."
)
# Check if there are changes in the current branch
if local_repo.is_dirty():
# Stage all changes
local_repo.git.add(A=True)
# Commit changes with the message "left from last change"
local_repo.index.commit("left from last change")
# # if branch_name is not provided, create a new branch name based on agent_config
# if branch is None:
# branch = args2string(agent_config)
create_branch(local_repo, branch, example["base_commit"])
# in cases where the latest commit of branch is not commit 0
# set it back to commit 0
latest_commit = local_repo.commit(branch)
if latest_commit.hexsha != example["base_commit"] and override_previous_changes:
local_repo.git.reset("--hard", example["base_commit"])
# get target files to edit and test files to run
target_edit_files, import_dependencies = get_target_edit_files(
local_repo,
example["src_dir"],
example["test"]["test_dir"],
branch,
example["reference_commit"],
agent_config.use_topo_sort_dependencies,
)
lint_files = get_changed_files_from_commits(
local_repo, "HEAD", example["base_commit"]
)
# Call the commit0 get-tests command to retrieve test files
test_files_str = [xx for x in get_tests(repo_name, verbose=0) for xx in x]
test_files = sorted(list(set([i.split(":")[0] for i in test_files_str])))
# prepare the log dir
experiment_log_dir = (
Path(log_dir)
/ repo_name
/ branch
/ datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
)
experiment_log_dir.mkdir(parents=True, exist_ok=True)
eval_results = {}
# write agent_config to .agent.yaml in the log_dir for record
agent_config_log_file = experiment_log_dir / ".agent.yaml"
with open(agent_config_log_file, "w") as agent_config_file:
yaml.dump(agent_config, agent_config_file)
with DirContext(repo_path):
if agent_config is None:
raise ValueError("Invalid input")
agent_return = None
if agent_config.run_tests:
update_queue.put(("start_repo", (repo_name, len(test_files))))
# when unit test feedback is available, iterate over test files
for test_file in test_files:
update_queue.put(("set_current_file", (repo_name, test_file)))
test_cmd = f"python -m commit0 test {repo_path} {test_file} --branch {branch} --backend {backend} --commit0-config-file {commit0_config_file} --timeout 100"
test_file_name = test_file.replace(".py", "").replace("/", "__")
test_log_dir = experiment_log_dir / test_file_name
lint_cmd = get_lint_cmd(
repo_name, agent_config.use_lint_info, commit0_config_file
)
message = get_message(agent_config, repo_path, test_files=[test_file])
# display the test file to terminal
agent_return = run_agent_multiple_times_on_same_inquiry(
agent=agent,
repo=local_repo,
branch=branch,
message=message,
fnames=[test_file],
test_cmd=test_cmd,
test_first=True,
lint_cmd=lint_cmd,
lint_first=False,
log_dir=test_log_dir,
repeat_times_for_each_inquiry=agent_config.repeat_times_for_each_inquiry,
backend=backend,
commit0_config_file=commit0_config_file,
)
if agent_config.record_test_for_each_commit:
current_commit = local_repo.head.commit.hexsha
eval_results[current_commit] = run_eval_after_each_commit(
branch, backend, commit0_config_file
)
# after running the agent, update the money display
update_queue.put(
(
"update_money_display",
(
repo_name,
test_file,
agent_return.last_cost if agent_return is not None else 0,
),
)
)
elif agent_config.run_entire_dir_lint:
update_queue.put(("start_repo", (repo_name, len(lint_files))))
# when unit test feedback is available, iterate over test files
for lint_file in lint_files:
update_queue.put(("set_current_file", (repo_name, lint_file)))
lint_file_name = lint_file.replace(".py", "").replace("/", "__")
lint_log_dir = experiment_log_dir / lint_file_name
lint_cmd = get_lint_cmd(
repo_name, agent_config.use_lint_info, commit0_config_file
)
# display the test file to terminal
agent_return = run_agent_multiple_times_on_same_inquiry(
agent=agent,
repo=local_repo,
branch=branch,
message="",
fnames=[lint_file],
test_cmd="",
test_first=False,
lint_cmd=lint_cmd,
lint_first=True,
log_dir=lint_log_dir,
repeat_times_for_each_inquiry=agent_config.repeat_times_for_each_inquiry,
backend=backend,
commit0_config_file=commit0_config_file,
)
if agent_config.record_test_for_each_commit:
current_commit = local_repo.head.commit.hexsha
eval_results[current_commit] = run_eval_after_each_commit(
branch, backend, commit0_config_file
)
# after running the agent, update the money display
update_queue.put(
(
"update_money_display",
(
repo_name,
lint_file,
agent_return.last_cost if agent_return is not None else 0,
),
)
)
else:
# when unit test feedback is not available, iterate over target files to edit
update_queue.put(("start_repo", (repo_name, len(target_edit_files))))
for f in target_edit_files:
update_queue.put(("set_current_file", (repo_name, f)))
file_name = f.replace(".py", "").replace("/", "__")
file_log_dir = experiment_log_dir / file_name
lint_cmd = get_lint_cmd(
repo_name, agent_config.use_lint_info, commit0_config_file
)
if agent_config.implementation_strategy == "function_by_function":
messages = get_message_function_by_function(
agent_config, repo_path, f, test_files
)
for message in messages:
agent_return = run_agent_multiple_times_on_same_inquiry(
agent=agent,
repo=local_repo,
branch=branch,
message=message,
fnames=[f],
test_cmd="",
test_first=False,
lint_cmd=lint_cmd,
lint_first=False,
log_dir=file_log_dir,
repeat_times_for_each_inquiry=agent_config.repeat_times_for_each_inquiry,
backend=backend,
commit0_config_file=commit0_config_file,
)
elif agent_config.implementation_strategy == "module_by_module":
message = get_message(
agent_config, repo_path, test_files=test_files, input_file=f
)
if agent_config.add_import_module_to_context:
dependencies = import_dependencies.get(f, [])
message = update_message_with_dependencies(
message, dependencies
)
agent_return = run_agent_multiple_times_on_same_inquiry(
agent=agent,
repo=local_repo,
branch=branch,
message=message,
fnames=[f],
test_cmd="",
test_first=False,
lint_cmd=lint_cmd,
lint_first=False,
log_dir=file_log_dir,
repeat_times_for_each_inquiry=agent_config.repeat_times_for_each_inquiry,
backend=backend,
commit0_config_file=commit0_config_file,
)
else:
raise ValueError(
f"Invalid implementation strategy: {agent_config.implementation_strategy}"
)
if agent_config.record_test_for_each_commit:
current_commit = local_repo.head.commit.hexsha
eval_results[current_commit] = run_eval_after_each_commit(
branch, backend, commit0_config_file
)
update_queue.put(
(
"update_money_display",
(
repo_name,
file_name,
agent_return.last_cost if agent_return is not None else 0,
),
)
)
if agent_config.record_test_for_each_commit:
with open(experiment_log_dir / "eval_results.json", "w") as f:
json.dump(eval_results, f)
update_queue.put(("finish_repo", repo_name))
def run_agent(
branch: str,
override_previous_changes: bool,
backend: str,
agent_config_file: str,
commit0_config_file: str,
log_dir: str,
max_parallel_repos: int,
display_repo_progress_num: int,
) -> None:
"""Main function to run Aider for a given repository."""
config = read_yaml_config(agent_config_file)
agent_config = AgentConfig(**config)
commit0_config_file = os.path.abspath(commit0_config_file)
commit0_config = read_commit0_config_file(commit0_config_file)
dataset = load_dataset(
commit0_config["dataset_name"], split=commit0_config["dataset_split"]
)
filtered_dataset = [
example
for example in dataset
if commit0_config["repo_split"] == "all"
or (
isinstance(example, dict)
and "repo" in example
and isinstance(example["repo"], str)
and example["repo"].split("/")[-1]
in SPLIT.get(commit0_config["repo_split"], [])
)
]
assert len(filtered_dataset) > 0, "No examples available"
if agent_config.add_import_module_to_context:
# Install Chrome for Playwright for browser-based agents
try:
subprocess.run(["playwright", "install", "chromium"], check=True)
print("Chrome installed successfully for Playwright")
except subprocess.CalledProcessError as e:
print(f"Error installing Chrome for Playwright: {e}")
except FileNotFoundError:
print("Playwright not found. Make sure it's installed and in your PATH.")
with TerminalDisplay(len(filtered_dataset)) as display:
not_started_repos = [
cast(RepoInstance, example)["repo"].split("/")[-1]
for example in filtered_dataset
]
display.set_not_started_repos(not_started_repos)
start_time = time.time()
display.update_repo_progress_num(
min(display_repo_progress_num, max_parallel_repos)
)
display.update_backend_display(backend)
display.update_log_dir_display(log_dir)
display.update_agent_display(
agent_config.agent_name,
agent_config.model_name,
agent_config.run_tests,
agent_config.use_topo_sort_dependencies,
agent_config.use_repo_info,
agent_config.use_unit_tests_info,
agent_config.use_spec_info,
agent_config.use_lint_info,
)
display.update_branch_display(branch)
with multiprocessing.Manager() as manager:
update_queue = manager.Queue()
with multiprocessing.Pool(processes=max_parallel_repos) as pool:
results = []
# Use apply_async to submit jobs and add progress bar updates
for example in filtered_dataset:
result = pool.apply_async(
run_agent_for_repo,
args=(
commit0_config["base_dir"],
agent_config,
cast(RepoInstance, example),
branch,
update_queue,
override_previous_changes,
backend,
log_dir,
commit0_config_file,
),
)
results.append(result)
last_time_update = 0
while any(not r.ready() for r in results):
try:
while not update_queue.empty():
action, data = update_queue.get_nowait()
if action == "start_repo":
repo_name, total_files = data
display.start_repo(repo_name, total_files)
elif action == "finish_repo":
repo_name = data
display.finish_repo(repo_name)
elif action == "set_current_file":
repo_name, file_name = data
display.set_current_file(repo_name, file_name)
elif action == "update_money_display":
repo_name, file_name, money_spent = data
display.update_money_display(
repo_name, file_name, money_spent
)
except queue.Empty:
pass
# Update time display every second
current_time = time.time()
if current_time - last_time_update >= 1:
elapsed_time = int(current_time - start_time)
display.update_time_display(elapsed_time)
last_time_update = current_time
time.sleep(0.1) # Small delay to prevent busy-waiting
# Final update after all repos are processed
while not update_queue.empty():
action, data = update_queue.get()
if action == "start_repo":
repo_name, total_files = data
display.start_repo(repo_name, total_files)
elif action == "finish_repo":
repo_name = data
display.finish_repo(repo_name)
elif action == "set_current_file":
repo_name, file_name = data
display.set_current_file(repo_name, file_name)
elif action == "update_money_display":
repo_name, file_name, money_spent = data
display.update_money_display(repo_name, file_name, money_spent)
# Final time update
elapsed_time = int(time.time() - start_time)
display.update_time_display(elapsed_time)
for result in results:
result.get()