Skip to content

Commit c52225b

Browse files
authored
Merge branch 'main' into integration
2 parents 11c1ff1 + 9f5bbca commit c52225b

File tree

8 files changed

+102
-50
lines changed

8 files changed

+102
-50
lines changed

agent/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ Available options include:
3838
`--max-lint-info-length: int`: Maximum length of the lint information to use. [Default: `10000`]
3939
`--pre-commit-config-path: str`: Path to the pre-commit config file. This is needed for running `lint`. [Default: `.pre-commit-config.yaml`]
4040
`--agent-config-file: str`: Path to write the agent config. [Default: `.agent.yaml`]
41+
`--add-import-module-to-context: bool`: Add import module to context. [Default: `False`]
42+
`--record-test-for-each-commit: bool`: Record test results for each commit. [Default: `False`], if set to `True`, the test results will be saved in `experiment_log_dir/eval_results.json`
4143

4244
## Running Agent
4345
Use `agent run [OPTIONS] BRANCH` to execute an agent on a specific branch.

agent/class_types.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ class AgentConfig:
2121
pre_commit_config_path: str
2222
run_tests: bool
2323
max_iteration: int
24+
record_test_for_each_commit: bool

agent/cli.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ def config(
135135
False,
136136
help="Run the lint on the entire directory",
137137
),
138+
record_test_for_each_commit: bool = typer.Option(
139+
False,
140+
help="Record the test for each commit",
141+
),
138142
pre_commit_config_path: str = typer.Option(
139143
".pre-commit-config.yaml",
140144
help="Path to the pre-commit config file",
@@ -170,6 +174,7 @@ def config(
170174
"max_lint_info_length": max_lint_info_length,
171175
"run_entire_dir_lint": run_entire_dir_lint,
172176
"pre_commit_config_path": pre_commit_config_path,
177+
"record_test_for_each_commit": record_test_for_each_commit,
173178
}
174179

175180
write_agent_config(agent_config_file, agent_config)

agent/display.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,4 +443,6 @@ def __exit__(
443443
) as json_file:
444444
json.dump(summary_data, json_file, indent=4)
445445

446-
print("\nSummary has been written to processing_summary.json")
446+
print(
447+
f"\nSummary has been written to processing_summary_{self.branch_name}.json"
448+
)

agent/run_agent.py

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
get_lint_cmd,
1313
read_yaml_config,
1414
)
15+
import json
1516
import subprocess
1617
from agent.agents import AiderAgents
1718
from typing import Optional, Type, cast
@@ -20,7 +21,7 @@
2021
from commit0.harness.constants import SPLIT
2122
from commit0.harness.get_pytest_ids import main as get_tests
2223
from commit0.harness.constants import RUN_AGENT_LOG_DIR, RepoInstance
23-
from commit0.cli import read_commit0_dot_file
24+
from commit0.cli import read_commit0_config_file
2425
from pathlib import Path
2526
from datetime import datetime
2627
from agent.display import TerminalDisplay
@@ -45,6 +46,21 @@ def __exit__(
4546
os.chdir(self.cwd)
4647

4748

49+
def run_eval_after_each_commit(
50+
branch: str, backend: str, commit0_config_file: str
51+
) -> str:
52+
"""Run the eval command after each commit."""
53+
eval_cmd = f"python -m commit0 evaluate --branch {branch} --backend {backend} --commit0-config-file {commit0_config_file} --timeout 100"
54+
try:
55+
result = subprocess.run(
56+
eval_cmd, shell=True, capture_output=True, text=True, check=True
57+
)
58+
return result.stdout
59+
except subprocess.CalledProcessError as e:
60+
print(f"Error running eval command: {e}")
61+
return e.stdout if e.stdout else str(e)
62+
63+
4864
def run_agent_for_repo(
4965
repo_base_dir: str,
5066
agent_config: AgentConfig,
@@ -58,7 +74,7 @@ def run_agent_for_repo(
5874
) -> None:
5975
"""Run Aider for a given repository."""
6076
# get repo info
61-
commit0_config = read_commit0_dot_file(commit0_config_file)
77+
commit0_config = read_commit0_config_file(commit0_config_file)
6278

6379
assert "commit0" in commit0_config["dataset_name"]
6480
_, repo_name = example["repo"].split("/")
@@ -130,6 +146,7 @@ def run_agent_for_repo(
130146
)
131147
experiment_log_dir.mkdir(parents=True, exist_ok=True)
132148

149+
eval_results = {}
133150
# write agent_config to .agent.yaml in the log_dir for record
134151
agent_config_log_file = experiment_log_dir / ".agent.yaml"
135152
with open(agent_config_log_file, "w") as agent_config_file:
@@ -161,6 +178,12 @@ def run_agent_for_repo(
161178
test_log_dir,
162179
test_first=True,
163180
)
181+
if agent_config.record_test_for_each_commit:
182+
current_commit = local_repo.head.commit.hexsha
183+
eval_results[current_commit] = run_eval_after_each_commit(
184+
branch, backend, commit0_config_file
185+
)
186+
164187
# after running the agent, update the money display
165188
update_queue.put(
166189
(
@@ -188,6 +211,12 @@ def run_agent_for_repo(
188211
lint_log_dir,
189212
lint_first=True,
190213
)
214+
if agent_config.record_test_for_each_commit:
215+
current_commit = local_repo.head.commit.hexsha
216+
eval_results[current_commit] = run_eval_after_each_commit(
217+
branch, backend, commit0_config_file
218+
)
219+
191220
# after running the agent, update the money display
192221
update_queue.put(
193222
(
@@ -211,12 +240,22 @@ def run_agent_for_repo(
211240
repo_name, agent_config.use_lint_info, commit0_config_file
212241
)
213242
agent_return = agent.run(message, "", lint_cmd, [f], file_log_dir)
243+
if agent_config.record_test_for_each_commit:
244+
current_commit = local_repo.head.commit.hexsha
245+
eval_results[current_commit] = run_eval_after_each_commit(
246+
branch, backend, commit0_config_file
247+
)
248+
214249
update_queue.put(
215250
(
216251
"update_money_display",
217252
(repo_name, file_name, agent_return.last_cost),
218253
)
219254
)
255+
if agent_config.record_test_for_each_commit:
256+
with open(experiment_log_dir / "eval_results.json", "w") as f:
257+
json.dump(eval_results, f)
258+
220259
update_queue.put(("finish_repo", repo_name))
221260

222261

@@ -236,7 +275,7 @@ def run_agent(
236275
agent_config = AgentConfig(**config)
237276

238277
commit0_config_file = os.path.abspath(commit0_config_file)
239-
commit0_config = read_commit0_dot_file(commit0_config_file)
278+
commit0_config = read_commit0_config_file(commit0_config_file)
240279

241280
dataset = load_dataset(
242281
commit0_config["dataset_name"], split=commit0_config["dataset_split"]

agent/run_agent_no_rich.py

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,17 @@
1414
read_yaml_config,
1515
)
1616
import subprocess
17+
import json
1718
from agent.agents import AiderAgents
18-
from typing import Optional, Type, cast
19-
from types import TracebackType
19+
from typing import cast
2020
from agent.class_types import AgentConfig
2121
from commit0.harness.constants import SPLIT
2222
from commit0.harness.get_pytest_ids import main as get_tests
2323
from commit0.harness.constants import RUN_AGENT_LOG_DIR, RepoInstance
24-
from commit0.cli import read_commit0_dot_file
24+
from commit0.cli import read_commit0_config_file
2525
from pathlib import Path
2626
from datetime import datetime
27-
28-
29-
class DirContext:
30-
def __init__(self, d: str):
31-
self.dir = d
32-
self.cwd = os.getcwd()
33-
34-
def __enter__(self):
35-
os.chdir(self.dir)
36-
37-
def __exit__(
38-
self,
39-
exctype: Optional[Type[BaseException]],
40-
excinst: Optional[BaseException],
41-
exctb: Optional[TracebackType],
42-
) -> None:
43-
os.chdir(self.cwd)
27+
from agent.run_agent import DirContext, run_eval_after_each_commit
4428

4529

4630
def run_agent_for_repo(
@@ -55,7 +39,7 @@ def run_agent_for_repo(
5539
) -> None:
5640
"""Run Aider for a given repository."""
5741
# get repo info
58-
commit0_config = read_commit0_dot_file(commit0_config_file)
42+
commit0_config = read_commit0_config_file(commit0_config_file)
5943

6044
assert "commit0" in commit0_config["dataset_name"]
6145
_, repo_name = example["repo"].split("/")
@@ -123,6 +107,7 @@ def run_agent_for_repo(
123107
/ datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
124108
)
125109
experiment_log_dir.mkdir(parents=True, exist_ok=True)
110+
eval_results = {}
126111

127112
# write agent_config to .agent.yaml in the log_dir for record
128113
agent_config_log_file = experiment_log_dir / ".agent.yaml"
@@ -153,6 +138,11 @@ def run_agent_for_repo(
153138
test_log_dir,
154139
test_first=True,
155140
)
141+
if agent_config.record_test_for_each_commit:
142+
current_commit = local_repo.head.commit.hexsha
143+
eval_results[current_commit] = run_eval_after_each_commit(
144+
branch, backend, commit0_config_file
145+
)
156146
elif agent_config.run_entire_dir_lint:
157147
# when unit test feedback is available, iterate over test files
158148
for lint_file in lint_files:
@@ -171,6 +161,11 @@ def run_agent_for_repo(
171161
lint_log_dir,
172162
lint_first=True,
173163
)
164+
if agent_config.record_test_for_each_commit:
165+
current_commit = local_repo.head.commit.hexsha
166+
eval_results[current_commit] = run_eval_after_each_commit(
167+
branch, backend, commit0_config_file
168+
)
174169
else:
175170
# when unit test feedback is not available, iterate over target files to edit
176171
message = get_message(agent_config, repo_path, test_files=test_files)
@@ -185,6 +180,14 @@ def run_agent_for_repo(
185180
repo_name, agent_config.use_lint_info, commit0_config_file
186181
)
187182
_ = agent.run(message, "", lint_cmd, [f], file_log_dir)
183+
if agent_config.record_test_for_each_commit:
184+
current_commit = local_repo.head.commit.hexsha
185+
eval_results[current_commit] = run_eval_after_each_commit(
186+
branch, backend, commit0_config_file
187+
)
188+
if agent_config.record_test_for_each_commit:
189+
with open(experiment_log_dir / "eval_results.json", "w") as f:
190+
json.dump(eval_results, f)
188191

189192

190193
def run_agent(
@@ -205,7 +208,7 @@ def run_agent(
205208
agent_config = AgentConfig(**config)
206209

207210
commit0_config_file = os.path.abspath(commit0_config_file)
208-
commit0_config = read_commit0_dot_file(commit0_config_file)
211+
commit0_config = read_commit0_config_file(commit0_config_file)
209212

210213
dataset = load_dataset(
211214
commit0_config["dataset_name"], split=commit0_config["dataset_split"]

commit0/cli.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,12 @@ def check_valid(one: str, total: Union[list[str], dict[str, list[str]]]) -> None
8686
)
8787

8888

89-
def write_commit0_dot_file(dot_file_path: str, config: dict) -> None:
89+
def write_commit0_config_file(dot_file_path: str, config: dict) -> None:
9090
with open(dot_file_path, "w") as f:
9191
yaml.dump(config, f, default_flow_style=False)
9292

9393

94-
def read_commit0_dot_file(dot_file_path: str) -> dict:
94+
def read_commit0_config_file(dot_file_path: str) -> dict:
9595
# Check if the file exists before attempting to read it
9696
if not os.path.exists(dot_file_path):
9797
raise FileNotFoundError(
@@ -112,7 +112,7 @@ def setup(
112112
),
113113
dataset_split: str = typer.Option("test", help="Split of the Huggingface dataset"),
114114
base_dir: str = typer.Option("repos/", help="Base directory to clone repos to"),
115-
commit0_dot_file_path: str = typer.Option(
115+
commit0_config_file: str = typer.Option(
116116
".commit0.yaml", help="Storing path for stateful commit0 configs"
117117
),
118118
) -> None:
@@ -128,7 +128,7 @@ def setup(
128128
typer.echo(f"Dataset split: {highlight(dataset_split, Colors.ORANGE)}")
129129
typer.echo(f"Base directory: {highlight(base_dir, Colors.ORANGE)}")
130130
typer.echo(
131-
f"Commit0 dot file path: {highlight(commit0_dot_file_path, Colors.ORANGE)}"
131+
f"Commit0 dot file path: {highlight(commit0_config_file, Colors.ORANGE)}"
132132
)
133133

134134
commit0.harness.setup.main(
@@ -139,8 +139,8 @@ def setup(
139139
)
140140

141141
# after successfully setup, write the commit0 dot file
142-
write_commit0_dot_file(
143-
commit0_dot_file_path,
142+
write_commit0_config_file(
143+
commit0_config_file,
144144
{
145145
"dataset_name": dataset_name,
146146
"dataset_split": dataset_split,
@@ -153,7 +153,7 @@ def setup(
153153
@commit0_app.command()
154154
def build(
155155
num_workers: int = typer.Option(8, help="Number of workers"),
156-
commit0_dot_file_path: str = typer.Option(
156+
commit0_config_file: str = typer.Option(
157157
".commit0.yaml",
158158
help="Path to the commit0 dot file, where the setup config is stored",
159159
),
@@ -168,7 +168,7 @@ def build(
168168
"""Build Commit0 split you choose in Setup Stage."""
169169
check_commit0_path()
170170

171-
commit0_config = read_commit0_dot_file(commit0_dot_file_path)
171+
commit0_config = read_commit0_dot_file(commit0_config_file)
172172
if "commit0" in commit0_config["dataset_name"].lower():
173173
check_valid(commit0_config["repo_split"], SPLIT)
174174

@@ -230,7 +230,7 @@ def test(
230230
rebuild: bool = typer.Option(
231231
False, "--rebuild", help="Whether to rebuild an image"
232232
),
233-
commit0_dot_file_path: str = typer.Option(
233+
commit0_config_file: str = typer.Option(
234234
".commit0.yaml",
235235
help="Path to the commit0 dot file, where the setup config is stored",
236236
),
@@ -249,7 +249,7 @@ def test(
249249
) -> None:
250250
"""Run tests on a Commit0 repository."""
251251
check_commit0_path()
252-
commit0_config = read_commit0_dot_file(commit0_dot_file_path)
252+
commit0_config = read_commit0_config_file(commit0_config_file)
253253
if repo_or_repo_path.endswith("/"):
254254
repo_or_repo_path = repo_or_repo_path[:-1]
255255
if "commit0" in commit0_config["dataset_name"].lower():
@@ -306,7 +306,7 @@ def evaluate(
306306
coverage: Annotated[
307307
bool, typer.Option("--coverage", help="Whether to get coverage information")
308308
] = False,
309-
commit0_dot_file_path: str = typer.Option(
309+
commit0_config_file: str = typer.Option(
310310
".commit0.yaml",
311311
help="Path to the commit0 dot file, where the setup config is stored",
312312
),
@@ -317,7 +317,7 @@ def evaluate(
317317
if reference:
318318
branch = "reference"
319319

320-
commit0_config = read_commit0_dot_file(commit0_dot_file_path)
320+
commit0_config = read_commit0_config_file(commit0_config_file)
321321
if "commit0" in commit0_config["dataset_name"].lower():
322322
check_valid(commit0_config["repo_split"], SPLIT)
323323

@@ -347,7 +347,7 @@ def lint(
347347
files: Union[List[Path], None] = typer.Option(
348348
None, help="Files to lint. If not provided, all files will be linted."
349349
),
350-
commit0_dot_file_path: str = typer.Option(
350+
commit0_config_file: str = typer.Option(
351351
".commit0.yaml",
352352
help="Path to the commit0 dot file, where the setup config is stored",
353353
),
@@ -361,7 +361,7 @@ def lint(
361361
) -> None:
362362
"""Lint given files if provided, otherwise lint all files in the base directory."""
363363
check_commit0_path()
364-
commit0_config = read_commit0_dot_file(commit0_dot_file_path)
364+
commit0_config = read_commit0_config_file(commit0_config_file)
365365
appended_files = None
366366
if files is not None:
367367
appended_files = []
@@ -386,14 +386,14 @@ def save(
386386
owner: str = typer.Argument(..., help="Owner of the repository"),
387387
branch: str = typer.Argument(..., help="Branch to save"),
388388
github_token: str = typer.Option(None, help="GitHub token for authentication"),
389-
commit0_dot_file_path: str = typer.Option(
389+
commit0_config_file: str = typer.Option(
390390
".commit0.yaml",
391391
help="Path to the commit0 dot file, where the setup config is stored",
392392
),
393393
) -> None:
394394
"""Save Commit0 split you choose in Setup Stage to GitHub."""
395395
check_commit0_path()
396-
commit0_config = read_commit0_dot_file(commit0_dot_file_path)
396+
commit0_config = read_commit0_config_file(commit0_config_file)
397397
if "commit0" in commit0_config["dataset_name"].lower():
398398
check_valid(commit0_config["repo_split"], SPLIT)
399399

0 commit comments

Comments
 (0)