Skip to content

Commit f59048e

Browse files
committed
adding logging
1 parent df47275 commit f59048e

File tree

5 files changed

+89
-36
lines changed

5 files changed

+89
-36
lines changed

baselines/class_types.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ class AiderConfig:
2424
use_lint_info: bool
2525
max_lint_info_length: int
2626
pre_commit_config_path: str
27+
run_tests: bool

baselines/configs/aider.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ aider_config:
99
use_unit_tests_info: false
1010
use_reference_info: false
1111
use_lint_info: true
12-
pre_commit_config_path: .pre-commit-config.yaml
12+
pre_commit_config_path: .pre-commit-config.yaml
13+
run_tests: true

baselines/configs/base.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ commit0_config:
77
base_dir: /Users/willjiang/Desktop/ai2dev/commit0/repos
88
dataset_name: "wentingzhao/commit0_docstring"
99
dataset_split: "test"
10-
repo_split: "lite"
10+
repo_split: "simpy"
1111
num_workers: 10
1212

1313
aider_config:
@@ -23,6 +23,7 @@ aider_config:
2323
use_lint_info: false
2424
max_lint_info_length: 10000
2525
pre_commit_config_path: .pre-commit-config.yaml
26+
run_tests: True
2627

2728
hydra:
2829
run:

baselines/run_aider.py

Lines changed: 83 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import logging
22
import os
33
import subprocess
4-
4+
from pathlib import Path
55
import hydra
66
from datasets import load_dataset
77
import traceback
@@ -15,12 +15,8 @@
1515
from commit0.harness.get_pytest_ids import main as get_tests
1616
from tqdm import tqdm
1717
from concurrent.futures import ThreadPoolExecutor, as_completed
18-
19-
20-
logging.basicConfig(
21-
level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
22-
)
23-
logger = logging.getLogger(__name__)
18+
from commit0.harness.constants import RUN_AIDER_LOG_DIR
19+
from commit0.harness.docker_build import setup_logger
2420

2521

2622
def get_aider_cmd(
@@ -29,6 +25,7 @@ def get_aider_cmd(
2925
message_to_aider: str,
3026
test_cmd: str,
3127
lint_cmd: str,
28+
log_dir: Path,
3229
) -> str:
3330
"""Get the Aider command based on the given context."""
3431
base_cmd = f'aider --model {model} --file {files} --message "{message_to_aider}"'
@@ -37,9 +34,45 @@ def get_aider_cmd(
3734
if test_cmd:
3835
base_cmd += f" --auto-test --test --test-cmd '{test_cmd}'"
3936
base_cmd += " --yes"
37+
38+
# Store Aider input and chat history in log directory
39+
input_history_file = log_dir / ".aider.input.history"
40+
chat_history_file = log_dir / ".aider.chat.history.md"
41+
42+
base_cmd += f" --input-history-file {input_history_file}"
43+
base_cmd += f" --chat-history-file {chat_history_file}"
4044
return base_cmd
4145

4246

47+
def execute_aider_cmd(
48+
aider_cmd: str,
49+
logger: logging.Logger,
50+
) -> None:
51+
"""Execute the Aider command."""
52+
try:
53+
process = subprocess.Popen(
54+
aider_cmd,
55+
shell=True,
56+
stdout=subprocess.PIPE,
57+
stderr=subprocess.PIPE,
58+
universal_newlines=True,
59+
)
60+
stdout, stderr = process.communicate()
61+
logger.info(f"STDOUT: {stdout}")
62+
logger.info(f"STDERR: {stderr}")
63+
except subprocess.CalledProcessError as e:
64+
logger.error(f"Command failed with exit code {e.returncode}")
65+
logger.error(f"STDOUT: {e.stdout}")
66+
logger.error(f"STDERR: {e.stderr}")
67+
68+
except OSError as e:
69+
if e.errno == 63: # File name too long error
70+
logger.error("Command failed due to file name being too long")
71+
logger.error(f"Command: {''.join(aider_cmd)}")
72+
else:
73+
logger.error(f"OSError occurred: {e}")
74+
75+
4376
def run_aider_for_repo(
4477
commit0_config: Commit0Config | None,
4578
aider_config: AiderConfig | None,
@@ -56,7 +89,7 @@ def run_aider_for_repo(
5689
repo_name = repo_name.replace(".", "-")
5790

5891
# Call the commit0 get-tests command to retrieve test files
59-
test_files_str = get_tests(repo_name, stdout=True)
92+
test_files_str = get_tests(repo_name, stdout=False)
6093

6194
test_files = sorted(list(set([i.split(":")[0] for i in test_files_str])))
6295

@@ -75,39 +108,55 @@ def run_aider_for_repo(
75108
else:
76109
lint_cmd = ""
77110

78-
for test_file in test_files:
79-
test_cmd = f"python -m commit0 test {repo_name} {test_file}"
111+
if aider_config.run_tests:
112+
for test_file in test_files:
113+
test_cmd = f"python -m commit0 test {repo_name} {test_file}"
114+
# set up logging
115+
test_file_name = test_file.replace(".py", "").replace("/", "__")
116+
log_dir = RUN_AIDER_LOG_DIR / repo_name / "ai" / test_file_name
117+
log_dir.mkdir(parents=True, exist_ok=True)
118+
log_file = log_dir / "run_aider.log"
119+
logger = setup_logger(repo_name, log_file)
120+
121+
aider_cmd = get_aider_cmd(
122+
aider_config.llm_name,
123+
target_edit_files_cmd_args,
124+
message_to_aider,
125+
test_cmd,
126+
lint_cmd,
127+
log_dir,
128+
)
129+
130+
# write aider command to log file
131+
aider_cmd_file = Path(log_dir / "aider_cmd.sh")
132+
aider_cmd_file.write_text(aider_cmd)
133+
134+
# write test command to log file
135+
test_cmd_file = Path(log_dir / "test_cmd.sh")
136+
test_cmd_file.write_text(test_cmd)
137+
138+
execute_aider_cmd(aider_cmd, logger)
139+
140+
else:
141+
# set up logging
142+
log_dir = RUN_AIDER_LOG_DIR / repo_name / "ai" / "no_test"
143+
log_dir.mkdir(parents=True, exist_ok=True)
144+
log_file = log_dir / "run_aider.log"
145+
logger = setup_logger(repo_name, log_file)
80146

81147
aider_cmd = get_aider_cmd(
82148
aider_config.llm_name,
83149
target_edit_files_cmd_args,
84150
message_to_aider,
85-
test_cmd,
151+
"",
86152
lint_cmd,
153+
log_dir,
87154
)
155+
# write aider command to log file
156+
aider_cmd_file = Path(log_dir / "aider_cmd.sh")
157+
aider_cmd_file.write_text(aider_cmd)
88158

89-
try:
90-
process = subprocess.Popen(
91-
aider_cmd,
92-
shell=True,
93-
stdout=subprocess.PIPE,
94-
stderr=subprocess.PIPE,
95-
universal_newlines=True,
96-
)
97-
stdout, stderr = process.communicate()
98-
logger.info(f"STDOUT: {stdout}")
99-
logger.info(f"STDERR: {stderr}")
100-
except subprocess.CalledProcessError as e:
101-
logger.error(f"Command failed with exit code {e.returncode}")
102-
logger.error(f"STDOUT: {e.stdout}")
103-
logger.error(f"STDERR: {e.stderr}")
104-
105-
except OSError as e:
106-
if e.errno == 63: # File name too long error
107-
logger.error("Command failed due to file name being too long")
108-
logger.error(f"Command: {''.join(aider_cmd)}")
109-
else:
110-
logger.error(f"OSError occurred: {e}")
159+
execute_aider_cmd(aider_cmd, logger)
111160

112161

113162
def pre_aider_processing(aider_config: AiderConfig) -> None:
@@ -150,7 +199,7 @@ def main() -> None:
150199
and example["repo"].split("/")[-1]
151200
in SPLIT.get(commit0_config.repo_split, [])
152201
)
153-
][:1]
202+
]
154203

155204
pre_aider_processing(aider_config)
156205

commit0/harness/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class Files(TypedDict):
2020
BASE_IMAGE_BUILD_DIR = Path("logs/build_images/base")
2121
REPO_IMAGE_BUILD_DIR = Path("logs/build_images/repo")
2222
RUN_PYTEST_LOG_DIR = Path("logs/pytest")
23+
RUN_AIDER_LOG_DIR = Path("logs/aider")
2324

2425
# Constants - Test Types, Statuses, Commands
2526
FAIL_TO_PASS = "FAIL_TO_PASS"

0 commit comments

Comments
 (0)