-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
run.py
549 lines (472 loc) · 21.4 KB
/
run.py
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
546
547
548
549
from __future__ import annotations
import logging
from sweagent import CONFIG_DIR
from sweagent.utils.log import add_file_handler, get_logger
try:
import rich
except ModuleNotFoundError as e:
msg = (
"You probably either forgot to install the dependencies "
"or forgot to activate your conda or virtual environment."
)
raise RuntimeError(msg) from e
import json
import re
import subprocess
import traceback
from typing import Any
import rich.console
import rich.markdown
import rich.panel
try:
from rich_argparse import RichHelpFormatter
except ImportError:
msg = "Please install the rich_argparse package with `pip install rich_argparse`."
raise ImportError(msg)
import datetime
from dataclasses import dataclass
from getpass import getuser
from pathlib import Path
import yaml
from rich.markdown import Markdown
from simple_parsing import parse
from simple_parsing.helpers.flatten import FlattenedAccess
from simple_parsing.helpers.serialization.serializable import FrozenSerializable
from swebench.harness.constants import KEY_INSTANCE_ID, KEY_MODEL, KEY_PREDICTION
from unidiff import PatchSet
from sweagent.agent.agents import Agent, AgentArguments
from sweagent.agent.models import ModelArguments
from sweagent.environment.swe_env import EnvironmentArguments, SWEEnv
from sweagent.environment.utils import (
InvalidGithubURL,
extract_flag_format,
get_associated_commit_urls,
get_data_path_name,
get_gh_issue_data,
parse_gh_issue_url,
)
__doc__: str = """ Run inference. Usage examples:
```bash
# Run over a github issue:
python run.py --model_name "gpt4" --data_path "https://github.com/pvlib/pvlib-python/issues/1603" --config_file "config/default_from_url.yaml"
# Apply a patch in a local repository to an issue specified as Markdown file and run a custom installer script in the container
python run.py --model_name "gpt4" --data_path "/path/to/my_issue.md" --repo_path "/path/to/my/local/repo" --environment_setup "/path/to/setup.sh" --config_file "config/default_from_url.yaml" --apply_patch_locally
```
**For more information**: https://princeton-nlp.github.io/SWE-agent/usage/cl_tutorial/
"""
logger = get_logger("swe-agent-run")
logging.getLogger("simple_parsing").setLevel(logging.WARNING)
@dataclass(frozen=True)
class ActionsArguments(FlattenedAccess, FrozenSerializable):
"""Run real-life actions (opening PRs, etc.) if we can solve the issue."""
# Open a PR with the patch if we can solve the issue
open_pr: bool = False
# When working with local repository: Apply patch
apply_patch_locally: bool = False
# Option to be used with open_pr: Skip action if there are already commits claiming
# to fix the issue. Please only set this to False if you are sure the commits are
# not fixes or if this is your own repository!
skip_if_commits_reference_issue: bool = True
# OBSOLETE. Do not use, will raise error. Please specify --repo_path instead.
push_gh_repo_url: str = ""
def __post_init__(self):
if self.push_gh_repo_url:
msg = "push_gh_repo_url is obsolete. Use repo_path instead"
raise ValueError(msg)
@dataclass(frozen=True)
class ScriptArguments(FlattenedAccess, FrozenSerializable):
"""Configure the control flow of the run.py script"""
environment: EnvironmentArguments
agent: AgentArguments
actions: ActionsArguments
# Only run instances that completely match this regex
instance_filter: str = ".*"
# Skip instances with existing trajectories
skip_existing: bool = True
# Suffix for the run name (used for example in trajectory directory naming)
suffix: str = ""
# Raise unhandled exceptions during the run (useful for debugging)
raise_exceptions: bool = False
# Dump the entire config to the log
print_config: bool = True
# Run the agent in CTF mode (SWE-agent: EnIGMA)
ctf: bool = False
@property
def run_name(self) -> str:
"""Generate a unique name for this run based on the arguments."""
model_name = self.agent.model.model_name.replace(":", "-")
data_stem = get_data_path_name(self.environment.data_path)
assert self.agent.config_file is not None # mypy
config_stem = Path(self.agent.config_file).stem
temp = self.agent.model.temperature
top_p = self.agent.model.top_p
per_instance_cost_limit = self.agent.model.per_instance_cost_limit
install_env = self.environment.install_environment
return (
f"{model_name}__{data_stem}__{config_stem}__t-{temp:.2f}__p-{top_p:.2f}"
+ f"__c-{per_instance_cost_limit:.2f}__install-{int(install_env)}"
+ (f"__{self.suffix}" if self.suffix else "")
)
class _ContinueLoop(Exception):
"""Used for internal control flow"""
class MainHook:
"""Hook structure for the web server or other addons to interface with"""
@staticmethod
def _is_promising_patch(info: dict[str, Any]) -> bool:
"""Do we actually believe that the patch will solve the issue?
Or are we just submitting the last patch we generated before hitting an error?
"""
# The exit status can also be `submitted (exit_cost)` etc.
return info["exit_status"] == "submitted" and info.get("submission") is not None
def on_init(self, *, args: ScriptArguments, agent: Agent, env: SWEEnv, traj_dir: Path):
"""Called when hook is initialized"""
def on_start(self):
"""Called at the beginning of `Main.main`"""
def on_end(self):
"""Called at the end of `Main.main`"""
def on_instance_start(self, *, index: int, instance: dict[str, Any]):
"""Called at the beginning of each instance loop in `Main.run`"""
def on_instance_skipped(
self,
):
"""Called when an instance is skipped in `Main.run`"""
def on_instance_completed(self, *, info, trajectory):
"""Called when an instance is completed in `Main.run`"""
class SaveApplyPatchHook(MainHook):
"""This hook saves patches to a separate directory and optionally applies them to a local repository."""
def on_init(self, *, args: ScriptArguments, agent: Agent, env: SWEEnv, traj_dir: Path):
self._traj_dir = traj_dir
self._apply_patch_locally = args.actions.apply_patch_locally
self._instance = None
def on_instance_start(self, *, index: int, instance: dict[str, Any]):
self._instance = instance
def on_instance_completed(self, *, info, trajectory):
assert self._instance is not None # mypy
instance_id = self._instance["instance_id"]
patch_path = self._save_patch(instance_id, info)
if patch_path:
if not self._apply_patch_locally:
return
if not self._is_promising_patch(info):
return
assert self._instance # mypy
if self._instance["repo_type"] != "local":
return
local_dir = Path(self._instance["repo"])
self._apply_patch(patch_path, local_dir)
@staticmethod
def _print_patch_message(patch_output_file: Path):
console = rich.console.Console()
msg = [
"SWE-agent has produced a patch that it believes will solve the issue you submitted!",
"Use the code snippet below to inspect or apply it!",
]
panel = rich.panel.Panel.fit(
"\n".join(msg),
title="🎉 Submission successful 🎉",
)
console.print(panel)
content = [
"```bash",
"# The patch has been saved to your local filesystem at:",
f"PATCH_FILE_PATH='{patch_output_file.resolve()}'",
"# Inspect it:",
'cat "${PATCH_FILE_PATH}"',
"# Apply it to a local repository:",
"cd <your local repo root>",
'git apply "${PATCH_FILE_PATH}"',
"```",
]
console.print(rich.markdown.Markdown("\n".join(content)))
def _save_patch(self, instance_id: str, info) -> Path | None:
"""Create patch files that can be applied with `git am`.
Returns:
The path to the patch file, if it was saved. Otherwise, returns None.
"""
patch_output_dir = self._traj_dir / "patches"
patch_output_dir.mkdir(exist_ok=True, parents=True)
patch_output_file = patch_output_dir / f"{instance_id}.patch"
if info.get("submission") is None:
logger.info("No patch to save.")
return None
model_patch = info["submission"]
patch_output_file.write_text(model_patch)
if self._is_promising_patch(info):
# Only print big congratulations if we actually believe
# the patch will solve the issue
self._print_patch_message(patch_output_file)
return patch_output_file
def _apply_patch(self, patch_file: Path, local_dir: Path) -> None:
"""Apply a patch to a local directory."""
assert local_dir.is_dir()
assert patch_file.exists()
# The resolve() is important, because we're gonna run the cmd
# somewhere else
cmd = ["git", "apply", str(patch_file.resolve())]
try:
subprocess.run(cmd, cwd=local_dir, check=True)
except subprocess.CalledProcessError as e:
logger.error(f"Failed to apply patch {patch_file} to {local_dir}: {e}")
return
logger.info(f"Applied patch {patch_file} to {local_dir}")
class OpenPRHook(MainHook):
"""This hook opens a PR if the issue is solved and the user has enabled the option."""
def on_init(self, *, args: ScriptArguments, agent: Agent, env: SWEEnv, traj_dir: Path):
self._env = env
self._token: str = env._github_token
self._data_path = args.environment.data_path
self._open_pr = args.actions.open_pr
self._skip_if_commits_reference_issue = args.actions.skip_if_commits_reference_issue
def on_instance_completed(self, *, info, trajectory):
if self._open_pr and self.should_open_pr(info):
self._env.open_pr(trajectory=trajectory)
def should_open_pr(self, info: dict[str, Any]) -> bool:
"""Does opening a PR make sense?"""
if not info.get("submission"):
logger.info("Not opening PR because no submission was made.")
return False
if info["exit_status"] != "submitted":
logger.info("Not opening PR because exit status was %s and not submitted.", info["exit_status"])
return False
try:
issue = get_gh_issue_data(self._data_path, token=self._token)
except InvalidGithubURL:
logger.info("Currently only GitHub is supported to open PRs to. Skipping PR creation.")
return False
if issue.state != "open":
logger.info(f"Issue is not open (state={issue.state}. Skipping PR creation.")
return False
if issue.assignee:
logger.info("Issue is already assigned. Skipping PR creation. Be nice :)")
return False
if issue.locked:
logger.info("Issue is locked. Skipping PR creation.")
return False
org, repo, issue_number = parse_gh_issue_url(self._data_path)
associated_commits = get_associated_commit_urls(org, repo, issue_number, token=self._token)
if associated_commits:
commit_url_strs = ", ".join(associated_commits)
if self._skip_if_commits_reference_issue:
logger.info(f"Issue already has associated commits (see {commit_url_strs}). Skipping PR creation.")
return False
else:
logger.warning(
"Proceeding with PR creation even though there are already commits "
f"({commit_url_strs}) associated with the issue. Please only do this for your own repositories "
"or after verifying that the existing commits do not fix the issue.",
)
return True
class Main:
def __init__(self, args: ScriptArguments):
self.traj_dir = Path("trajectories") / Path(getuser()) / args.run_name
self.traj_dir.mkdir(parents=True, exist_ok=True)
timestamp = datetime.datetime.now().strftime("%y%m%d%H%M%S")
log_path = self.traj_dir / f"run-{timestamp}.log"
logger.info("Logging to %s", log_path)
add_file_handler(log_path)
if args.print_config:
logger.info(f"📙 Arguments: {args.dumps_yaml()}")
self.args = args
self.agent = Agent("primary", args.agent)
self.env = SWEEnv(args.environment)
self._save_arguments()
default_hooks = [
SaveApplyPatchHook(),
OpenPRHook(),
]
self.hooks: list[MainHook] = []
for hook in default_hooks:
self.add_hook(hook)
def add_hook(self, hook: MainHook):
hook.on_init(args=self.args, agent=self.agent, env=self.env, traj_dir=self.traj_dir)
self.hooks.append(hook)
def run(self, index: int) -> None:
# Reset environment
instance_id = self.env.data[index]["instance_id"]
for hook in self.hooks:
hook.on_instance_start(index=index, instance=self.env.data[index])
assert isinstance(instance_id, str) # mypy
if self.should_skip(instance_id):
for hook in self.hooks:
hook.on_instance_skipped()
raise _ContinueLoop
logger.info("▶️ Beginning task " + str(index))
observation, info = self.env.reset(index)
if info is None:
raise _ContinueLoop
# Get info, patch information
issue = getattr(self.env, "query", None)
files = []
assert self.env.record is not None # mypy
if "patch" in self.env.record:
files = "\n".join([f"- {x.path}" for x in PatchSet(self.env.record["patch"]).modified_files])
# Get test files, F2P tests information
test_files = []
if "test_patch" in self.env.record:
test_patch_obj = PatchSet(self.env.record["test_patch"])
test_files = "\n".join([f"- {x.path}" for x in test_patch_obj.modified_files + test_patch_obj.added_files])
tests = ""
if "FAIL_endTO_PASS" in self.env.record:
tests = "\n".join([f"- {x}" for x in self.env.record["FAIL_TO_PASS"]])
setup_args = {"issue": issue, "files": files, "test_files": test_files, "tests": tests}
challenge = self.env.challenge
if challenge is not None:
setup_args["flag_format"] = extract_flag_format(challenge["flag"])
setup_args["name"] = challenge["name"]
setup_args["description"] = challenge["description"]
setup_args["category_friendly"] = challenge["category_friendly"]
setup_args["points"] = challenge["points"]
setup_args["files"] = challenge["files"] or "No files included in this challenge."
setup_args["box"] = challenge.get("server_name")
setup_args["port"] = challenge.get("port")
setup_args["server_description"] = challenge.get("server_description")
info, trajectory = self.agent.run(
setup_args=setup_args,
env=self.env,
observation=observation,
traj_dir=self.traj_dir,
return_type="info_trajectory",
)
self._save_predictions(instance_id, info, challenge)
for hook in self.hooks:
hook.on_instance_completed(info=info, trajectory=trajectory)
def main(self):
for hook in self.hooks:
hook.on_start()
for index in range(len(self.env.data)):
try:
self.run(index)
except _ContinueLoop:
continue
except KeyboardInterrupt:
logger.info("Exiting InterCode environment...")
self.env.close()
break
except SystemExit:
logger.critical("❌ Exiting because SystemExit was called")
self.env.close()
logger.info("Container closed")
raise
except Exception as e:
logger.warning(traceback.format_exc())
if self.args.raise_exceptions:
self.env.close()
raise e
if self.env.record:
logger.warning(f"❌ Failed on {self.env.record['instance_id']}: {e}")
else:
logger.warning("❌ Failed on unknown instance")
self.env.reset_container()
continue
self.env.close()
for hook in self.hooks:
hook.on_end()
def _save_arguments(self) -> None:
"""Save the arguments to a yaml file to the run's trajectory directory."""
log_path = self.traj_dir / "args.yaml"
if log_path.exists():
try:
other_args = self.args.load_yaml(log_path)
if self.args.dumps_yaml() != other_args.dumps_yaml(): # check yaml equality instead of object equality
logger.warning("**************************************************")
logger.warning("Found existing args.yaml with different arguments!")
logger.warning("**************************************************")
except Exception:
logger.warning(f"Failed to load existing args.yaml: {traceback.format_exc()}")
with log_path.open("w") as f:
self.args.dump_yaml(f)
def should_skip(self, instance_id: str) -> bool:
"""Check if we should skip this instance based on the instance filter and skip_existing flag."""
# Skip instances that don't match the instance filter
if re.match(self.args.instance_filter, instance_id) is None:
logger.info(f"⏭️ Instance filter not matched. Skipping instance {instance_id}")
return True
# If flag is set to False, don't skip
if not self.args.skip_existing:
return False
# Check if there's an existing trajectory for this instance
log_path = self.traj_dir / (instance_id + ".traj")
if not log_path.exists():
return False
content = log_path.read_text()
if not content.strip():
logger.warning("Found empty trajectory: %s. Removing.", log_path)
log_path.unlink()
return False
data = json.loads(content)
# If the trajectory has no exit status, it's incomplete and we will redo it
exit_status = data["info"].get("exit_status", None)
if exit_status == "early_exit" or exit_status is None:
logger.warning(f"Found existing trajectory with no exit status: {log_path}. Removing.")
log_path.unlink()
return False
logger.info(f"⏭️ Skipping existing trajectory: {log_path}")
return True
def _save_predictions(self, instance_id: str, info, challenge: dict[str, str] | None):
output_file = self.traj_dir / "all_preds.jsonl"
model_patch = info["submission"] if "submission" in info else None
datum = {
KEY_MODEL: Path(self.traj_dir).name,
KEY_INSTANCE_ID: instance_id,
KEY_PREDICTION: model_patch,
}
if challenge is not None:
challenge_datum = {
"challenge_name": challenge["name"],
"challenge_category": challenge["category"],
"challenge_path": challenge["file_path"],
}
datum.update(challenge_datum)
with open(output_file, "a+") as fp:
print(json.dumps(datum), file=fp, flush=True)
logger.info(f"Saved predictions to {output_file}")
def get_args(args=None) -> ScriptArguments:
"""Parse command line arguments and return a ScriptArguments object.
Args:
args: Optional list of arguments to parse. If not provided, uses sys.argv.
"""
defaults = ScriptArguments(
suffix="",
environment=EnvironmentArguments(
image_name="sweagent/swe-agent:latest",
data_path="princeton-nlp/SWE-bench_Lite",
split="dev",
verbose=True,
install_environment=True,
cache_task_images=False,
),
skip_existing=True,
agent=AgentArguments(
model=ModelArguments(
model_name="gpt4",
total_cost_limit=0.0,
per_instance_cost_limit=3.0,
temperature=0.0,
top_p=0.95,
),
config_file=CONFIG_DIR / "default.yaml",
),
actions=ActionsArguments(open_pr=False, skip_if_commits_reference_issue=True),
ctf=False,
)
# Nicer yaml dumping of multiline strings
def multiline_representer(dumper, data):
"""configures yaml for dumping multiline strings
Ref: https://stackoverflow.com/questions/8640959/how-can-i-control-what-scalar-form-pyyaml-uses-for-my-data
"""
if data.count("\n") > 0: # check for multiline string
return dumper.represent_scalar("tag:yaml.org,2002:str", data, style="|")
return dumper.represent_scalar("tag:yaml.org,2002:str", data)
yaml.add_representer(str, multiline_representer)
return parse(
ScriptArguments,
default=defaults,
add_config_path_arg=False,
args=args,
formatter_class=RichHelpFormatter,
description=Markdown(__doc__),
)
def main(args: ScriptArguments):
Main(args).main()
if __name__ == "__main__":
main(get_args())