-
Notifications
You must be signed in to change notification settings - Fork 395
Expand file tree
/
Copy pathcoding_agent_polyglot.py
More file actions
188 lines (159 loc) · 6.75 KB
/
coding_agent_polyglot.py
File metadata and controls
188 lines (159 loc) · 6.75 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
import argparse
import subprocess
import logging
from logging.handlers import RotatingFileHandler
import os
import threading
from llm_withtools import CLAUDE_MODEL, OPENAI_MODEL, chat_with_agent
from utils.git_utils import diff_versus_commit, reset_to_commit, apply_patch
# reset_to_commit(git_dname, commit)
# apply_patch(git_dname, patch_str)
# TEST COMMANDS for different languages
# IMPORTANT: TEST COMMAND SHOULD BE RUN UNDER git_tempdir!!
NPM_TEST_COMMANDS = [
["sh", "-c", "set -e"],
["sh", "-c", "[ ! -e node_modules ] && ln -s /npm-install/node_modules ."],
["sh", "-c", "[ ! -e package-lock.json ] && ln -s /npm-install/package-lock.json ."],
["sed", "-i", "s/\\bxtest(/test(/g", "*.spec.js"],
["npm", "run", "test"]
]
CPP_TEST_COMMANDS = [
["sh", "-c", "set -e"],
["sh", "-c", "[ ! -d \"build\" ] && mkdir build"],
["sh", "-c", "cd build"],
["cmake", "-DEXERCISM_RUN_ALL_TESTS=1", "-G", "Unix Makefiles", ".."],
["make"],
["sh", "-c", "cd ../"]
]
TEST_COMMANDS = {
"python": [["pytest", "-rA", "--tb=long"]],
"rust": [["cargo", "test", "--", "--include-ignored"]],
"go": [["go", "test", "./..."]],
"javascript": NPM_TEST_COMMANDS,
"cpp": CPP_TEST_COMMANDS,
"java": [["./gradlew", "test"]],
}
# 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,
language='python'
):
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.language = language
# Set the code model based on whether self-improvement is enabled
self.code_model = CLAUDE_MODEL if not self_improve else OPENAI_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))
new_msg_history = [
{
"role": "user",
"content": [
{
"type": "text",
"text": f"# Current Repo Edits\n{diff}",
}
],
}
]
return new_msg_history
def forward(self):
"""
The forward function for the AgenticSystem.
"""
task = f"""I have uploaded a code repository in the directory {self.git_tempdir}. Help solve the following problem.
<problem_description>
{self.problem_statement}
</problem_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.
"""
instruction = f"{task}\n\nPlease analyze the problem description carefully. Then make edits to the code files to complete the instruction."
init_edit = chat_with_agent(instruction, model=self.code_model, msg_history=[], logging=safe_log)
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="/dgm/", 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('--language', required=False, default="python", choices=['cpp', 'java', 'python', 'go', 'rust', 'javascript'], help='Task\'s programming language')
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,
language=args.language,
)
# Run the agentic system to try to solve the problem
agentic_system.forward()
# 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()