Skip to content

Commit 96309de

Browse files
committed
Add run_manager code, not completely working
1 parent 8de0084 commit 96309de

File tree

3 files changed

+105
-54
lines changed

3 files changed

+105
-54
lines changed

agent/agent_utils.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,3 +571,13 @@ def read_yaml_config(config_file: str) -> dict:
571571
raise FileNotFoundError(f"The config file '{config_file}' does not exist.")
572572
with open(config_file, "r") as f:
573573
return yaml.load(f, Loader=yaml.FullLoader)
574+
575+
576+
def parse_tasks(text: str) -> list[str]:
577+
"""Parse the tasks from the manager output."""
578+
tasks = []
579+
for line in text.strip().splitlines():
580+
if not line.strip()[0].isdigit():
581+
continue
582+
tasks.append(line.strip())
583+
return tasks

agent/agents.py

Lines changed: 60 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,59 @@ class AgentTeams(Agents):
6161
def __init__(self, max_iteration: int, model_name: str):
6262
super().__init__(max_iteration)
6363
self.model = Model(model_name)
64+
65+
def run_manager(
66+
self,
67+
message: str,
68+
fnames: list[str],
69+
log_dir: Path,
70+
) -> AgentReturn:
71+
"""Start agent manager"""
72+
73+
log_dir = log_dir.resolve()
74+
log_dir.mkdir(parents=True, exist_ok=True)
75+
input_history_file = log_dir / ".manager.input.history"
76+
chat_history_file = log_dir / ".manager.chat.history.md"
77+
78+
# Set up logging
79+
log_file = log_dir / "manager.log"
80+
logging.basicConfig(
81+
filename=log_file,
82+
level=logging.INFO,
83+
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
84+
)
85+
86+
# Redirect print statements to the log file
87+
sys.stdout = open(log_file, "a")
88+
sys.stderr = open(log_file, "a")
89+
90+
# Configure httpx and backoff logging
91+
handle_logging("httpx", log_file)
92+
handle_logging("backoff", log_file)
93+
94+
io = InputOutput(
95+
yes=False,
96+
input_history_file=input_history_file,
97+
chat_history_file=chat_history_file,
98+
)
99+
manager = Coder.create(
100+
main_model=self.model,
101+
read_only_fnames=fnames,
102+
io=io,
103+
)
104+
manager.max_reflection = self.max_iteration
105+
manager.stream = True
106+
107+
manager.run(message)
108+
109+
sys.stdout.close()
110+
sys.stderr.close()
111+
# Restore original stdout and stderr
112+
sys.stdout = sys.__stdout__
113+
sys.stderr = sys.__stderr__
114+
115+
return AgentReturn(log_file)
116+
64117
def run(
65118
self,
66119
message: str,
@@ -81,11 +134,11 @@ def run(
81134
auto_lint = False
82135
log_dir = log_dir.resolve()
83136
log_dir.mkdir(parents=True, exist_ok=True)
84-
input_history_file = log_dir / ".team.input.history"
85-
chat_history_file = log_dir / ".team.chat.history.md"
137+
input_history_file = log_dir / ".coder.input.history"
138+
chat_history_file = log_dir / ".coder.chat.history.md"
86139

87140
# Set up logging
88-
log_file = log_dir / "team.log"
141+
log_file = log_dir / "coder.log"
89142
logging.basicConfig(
90143
filename=log_file,
91144
level=logging.INFO,
@@ -96,11 +149,6 @@ def run(
96149
sys.stdout = open(log_file, "a")
97150
sys.stderr = open(log_file, "a")
98151

99-
# Log the message
100-
agent_message_log_file = log_dir / "agent_message.log"
101-
with open(agent_message_log_file, "a") as f:
102-
f.write(f"Message Sent: {message}\n\n")
103-
104152
# Configure httpx and backoff logging
105153
handle_logging("httpx", log_file)
106154
handle_logging("backoff", log_file)
@@ -110,26 +158,12 @@ def run(
110158
input_history_file=input_history_file,
111159
chat_history_file=chat_history_file,
112160
)
113-
manager = Coder.create(
114-
main_model=self.model,
115-
fnames=fnames,
116-
auto_lint=auto_lint,
117-
auto_test=auto_test,
118-
lint_cmds={"python": lint_cmd},
119-
test_cmd=test_cmd,
120-
io=io,
121-
)
122-
manager.max_reflection = 1
123-
manager.stream = True
124-
125-
# Run the agent
126-
manager_message = "First, add every file in the repo to your conversation. Second, write a plan of attack to implement the entire repo and return this plan."
127-
manager.run(manager_message)
128-
129161

130162
coder = Coder.create(
131163
main_model=self.model,
132-
fnames=fnames,
164+
165+
#make the coder import files on its own for now
166+
fnames=[],
133167
auto_lint=auto_lint,
134168
auto_test=auto_test,
135169
lint_cmds={"python": lint_cmd},
@@ -138,12 +172,7 @@ def run(
138172
)
139173
coder.max_reflection = self.max_iteration
140174
coder.stream = True
141-
142-
# Run the agent
143-
with open(chat_history_file, 'r', encoding='utf-8') as file:
144-
plan = file.read()
145-
coder_message = "follow this implementation plan: "+plan
146-
coder.run(coder_message)
175+
coder.run(message)
147176

148177
sys.stdout.close()
149178
sys.stderr.close()

agent/run_agent.py

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
update_message_with_dependencies,
1313
get_lint_cmd,
1414
read_yaml_config,
15+
parse_tasks,
1516
)
1617
import subprocess
1718
from agent.agents import AiderAgents
@@ -293,8 +294,8 @@ def run_team_for_repo(
293294
f"{repo_path} is not a git repo. Check if base_dir is correctly specified."
294295
)
295296

296-
manager = AiderAgents(1, agent_config.model_name)
297-
coder = AiderAgents(agent_config.max_iteration, agent_config.model_name)
297+
manager = AgentTeams(agent_config.max_iteration, agent_config.model_name)
298+
coder = AgentTeams(agent_config.max_iteration, agent_config.model_name)
298299

299300
# Check if there are changes in the current branch
300301
if local_repo.is_dirty():
@@ -345,7 +346,8 @@ def run_team_for_repo(
345346
with open(agent_config_log_file, "w") as agent_config_file:
346347
yaml.dump(agent_config, agent_config_file)
347348

348-
manager_message = "Write a concise plan of attack to implement the entire repo, but don't actually do any coding. The plan should not include any reccommendations to add files and should be a maximum of 500 words."
349+
# /ask will make aider not write any code, but only a plan
350+
manager_message = "/ask You are a manager in charge of writing a plan to complete the implementations for all functions (i.e., those with pass statements) and pass the unit tests. Write a concise plan of attack to implement the entire repo, but don't actually do any coding. Please output the plan in the format of a list of numbered steps. Each step should specify a file to edit and a high-level description of the change to make. For example, '1.) file.py: add a function to calculate the sum of two numbers'. Note that we only need to edit the files that contain functions with pass statements, ie. those in the current context. Give me only the plan, with no extraneous text."
349351

350352
with DirContext(repo_path):
351353
if agent_config is None:
@@ -364,36 +366,46 @@ def run_team_for_repo(
364366
dependencies = import_dependencies[f]
365367
file_name = "all"
366368
file_log_dir = experiment_log_dir / file_name
367-
lint_cmd = get_lint_cmd(repo_name, agent_config.use_lint_info)
369+
lint_cmd = get_lint_cmd(repo_name, agent_config.use_lint_info, commit0_config_file)
368370

371+
372+
"""
373+
#uncommenting below works, but the manager.run_manager line doesnt work idk why
369374
370-
agent_return = manager.run(manager_message, "", lint_cmd, target_edit_files, file_log_dir)
371-
with open(agent_return.log_file, 'r', encoding='utf-8') as file:
372-
plan = file.read()
373-
coder_message = "follow this implementation plan: "+plan
374-
375+
coder_message = f"Complete the following task, implementing the relevant incomplete functions (i.e., those with pass statements). You may add the specified file to the context if necessary:"
376+
375377
agent_return = coder.run(coder_message, "", lint_cmd, target_edit_files, file_log_dir)
378+
"""
379+
380+
agent_return = manager.run_manager(manager_message, target_edit_files, file_log_dir)
381+
382+
#TODO: uncomment below after figuring out why manager.run_manager doesnt work
383+
384+
# with open(agent_return.log_file, 'r', encoding='utf-8') as file:
385+
# plan = file.read()
386+
387+
# update_queue.put(
388+
# (
389+
# "update_money_display",
390+
# (repo_name, file_name, agent_return.last_cost),
391+
# )
392+
# )
393+
394+
# tasks = parse_tasks(plan)
376395

377-
# for f in target_edit_files:
378-
# update_queue.put(("set_current_file", (repo_name, f)))
379-
# dependencies = import_dependencies[f]
380-
# message = update_message_with_dependencies(coder_message, dependencies)
381-
# file_name = f.replace(".py", "").replace("/", "__")
382-
# file_log_dir = experiment_log_dir / file_name
383-
# lint_cmd = get_lint_cmd(repo_name, agent_config.use_lint_info)
384-
# agent_return = coder.run(message, "", lint_cmd, [f], file_log_dir)
396+
# for task in tasks:
397+
# coder_message = f"Complete the following task, implementing the relevant incomplete functions (i.e., those with pass statements). You may add the specified file to the context if necessary: \n{task}"
398+
399+
# agent_return = coder.run(coder_message, "", lint_cmd, target_edit_files, file_log_dir)
400+
# #TODO: fix the display (right now it just displys one file)
401+
402+
385403
# update_queue.put(
386404
# (
387405
# "update_money_display",
388406
# (repo_name, file_name, agent_return.last_cost),
389407
# )
390408
# )
391-
update_queue.put(
392-
(
393-
"update_money_display",
394-
(repo_name, file_name, agent_return.last_cost),
395-
)
396-
)
397409

398410

399411

0 commit comments

Comments
 (0)