-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathcoding_agent.py
More file actions
273 lines (229 loc) · 8.54 KB
/
coding_agent.py
File metadata and controls
273 lines (229 loc) · 8.54 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
# This file is adapted from https://github.com/jennyzzt/dgm.
import argparse
import logging
import os
import subprocess
import threading
from logging.handlers import RotatingFileHandler
from time import time
from llm_withtools import (CLAUDE_MODEL, OPENAI_MODEL, chat_with_agent,
convert_msg_history)
from utils.eval_utils import (get_report_score, msg_history_to_report,
score_tie_breaker)
from utils.git_utils import apply_patch, diff_versus_commit, reset_to_commit
# Thread-local storage for logger instances
thread_local = threading.local()
def get_thread_logger():
"""
Get the logger instance specific to the current thread.
Returns None if no logger has been set for this thread.
"""
return getattr(thread_local, "logger", None)
def set_thread_logger(logger):
"""
Set the logger instance for the current thread.
"""
thread_local.logger = logger
def setup_logger(log_file="./chat_history.md", level=logging.INFO):
"""
Set up a logger with both file and console handlers.
"""
# Create logger with a unique name based on thread ID
logger = logging.getLogger(f"AgenticSystem-{threading.get_ident()}")
logger.setLevel(level)
# Remove existing handlers to avoid duplicates
logger.handlers = []
# Create formatters
file_formatter = logging.Formatter("%(message)s")
# Create and set up file handler
os.makedirs(os.path.dirname(log_file), exist_ok=True)
file_handler = RotatingFileHandler(
log_file, maxBytes=10 * 1024 * 1024, backupCount=5
)
file_handler.setLevel(level)
file_handler.setFormatter(file_formatter)
# Add handlers to logger
logger.addHandler(file_handler)
# Store logger in thread-local storage
set_thread_logger(logger)
return logger
def safe_log(message, level=logging.INFO):
"""
Thread-safe logging function that ensures messages go to the correct logger.
"""
logger = get_thread_logger()
if logger:
logger.log(level, message)
else:
print(f"Warning: No logger found for thread {threading.get_ident()}")
class AgenticSystem:
def __init__(
self,
problem_statement,
git_tempdir,
base_commit,
chat_history_file="./chat_history.md",
test_description=None,
self_improve=False,
instance_id=None,
model=CLAUDE_MODEL,
):
self.problem_statement = problem_statement
self.git_tempdir = git_tempdir
self.base_commit = base_commit
self.chat_history_file = chat_history_file
self.test_description = test_description
self.self_improve = self_improve
self.instance_id = instance_id if not self_improve else "hgm"
self.code_model = model
# Initialize logger and store it in thread-local storage
self.logger = setup_logger(chat_history_file)
# Clear the log file
with open(chat_history_file, "w") as f:
f.write("")
def get_current_edits(self):
diff = str(diff_versus_commit(self.git_tempdir, self.base_commit))
return diff
def get_regression_tests(self):
"""
Get the regression tests from the repository.
"""
instruction = f"""I have uploaded a Python code repository in the directory {self.git_tempdir}.
<problem_description>
{self.problem_statement}
</problem_description>
<test_description>
{self.test_description}
</test_description>
Your task is to identify regression tests in the {self.git_tempdir} directory that should pass both before and after addressing the <problem_description>. I have already taken care of the required dependencies.
At the end, please provide a summary that includes where the regression tests are located, what they are testing, and how they can be executed.
"""
new_msg_history, _ = chat_with_agent(
instruction, model=self.code_model, msg_history=[], logging=safe_log
)
new_msg_history = convert_msg_history(new_msg_history, self.code_model)
regression_tests_summary = new_msg_history[-1]
try:
regression_tests_summary = regression_tests_summary["content"]
except:
try:
regression_tests_summary = str(regression_tests_summary)
except:
pass
return regression_tests_summary
def run_regression_tests(self, regression_tests_summary):
"""
Run the regression tests and get the test report.
"""
code_diff = self.get_current_edits()
instruction = f"""I have uploaded a Python code repository in the directory {self.git_tempdir}. There is an attempt to address the problem statement. Please review the changes and run the regression tests.
<problem_description>
{self.problem_statement}
</problem_description>
<attempted_solution>
{code_diff}
</attempted_solution>
<test_description>
{self.test_description}
</test_description>
<regression_tests_summary>
{regression_tests_summary}
</regression_tests_summary>
Your task is to run the regression tests in the {self.git_tempdir} directory to ensure that the changes made to the code address the <problem_description>.
"""
new_msg_history, _ = chat_with_agent(
instruction, model=self.code_model, msg_history=[], logging=safe_log
)
test_report = msg_history_to_report(
self.instance_id, new_msg_history, model=self.code_model
)
return test_report
def forward(self, timeout=3600):
timeout -= 60
start_time = time()
"""
The forward function for the AgenticSystem.
"""
instruction = f"""I have uploaded a Python code repository in the directory {self.git_tempdir}. Help solve the following problem.
<problem_description>
{self.problem_statement}
</problem_description>
<test_description>
{self.test_description}
</test_description>
Your task is to make changes to the files in the {self.git_tempdir} directory to address the <problem_description>. I have already taken care of the required dependencies.
"""
chat_history, n_llm_calls_used = chat_with_agent(
instruction,
model=self.code_model,
msg_history=[],
logging=safe_log,
timeout=timeout - (time() - start_time),
)
chat_history_str = str(chat_history)
def main():
parser = argparse.ArgumentParser(
description="Process repository with an agentic system."
)
parser.add_argument(
"--problem_statement", required=True, help="The problem statement to process"
)
parser.add_argument(
"--git_dir", required=True, help="Path to git repository directory"
)
parser.add_argument(
"--base_commit", required=True, help="Base commit hash to compare against"
)
parser.add_argument(
"--chat_history_file", required=True, help="Path to chat history file"
)
parser.add_argument(
"--outdir", required=False, default="/hgm/", help="Output directory"
)
parser.add_argument(
"--test_description",
default=None,
required=False,
help="Description of how to test the repository",
)
parser.add_argument(
"--self_improve",
default=False,
action="store_true",
help="Whether to self-improve the repository or solving swe",
)
parser.add_argument("--instance_id", default=None, help="Instance ID for SWE issue")
parser.add_argument(
"--model",
required=False,
default=OPENAI_MODEL,
help="LLM model to use for processing",
)
parser.add_argument(
"--timeout", type=int, default=3600, help="Timeout for LLM calls in seconds"
)
args = parser.parse_args()
# Process the repository
agentic_system = AgenticSystem(
problem_statement=args.problem_statement,
git_tempdir=args.git_dir,
base_commit=args.base_commit,
chat_history_file=args.chat_history_file,
test_description=args.test_description,
self_improve=args.self_improve,
instance_id=args.instance_id,
model=args.model,
)
# Run the agentic system to try to solve the problem
agentic_system.forward(args.timeout)
# Get code diff and save to model_patch.diff
model_patch = diff_versus_commit(args.git_dir, args.base_commit)
model_patch_outfile = (
os.path.join(args.outdir, "model_patch.diff")
if args.outdir
else "model_patch.diff"
)
with open(model_patch_outfile, "w") as f:
f.write(model_patch)
if __name__ == "__main__":
main()