-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathrun.py
More file actions
179 lines (146 loc) · 7.54 KB
/
run.py
File metadata and controls
179 lines (146 loc) · 7.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
import atexit
import logging
import sys
import shutil
import time
import threading
from concurrent.futures import ThreadPoolExecutor, wait, FIRST_COMPLETED
from engine.agent_search import AgentSearch as Agent
from engine.executor import Interpreter
from engine.search_node import Journal
from omegaconf import OmegaConf
from rich.status import Status
from config import load_task_desc, prep_agent_workspace, save_run, load_cfg
from utils.visualization import journal_to_string_tree
from utils.seed import set_global_seed
from engine.coldstart import build_guidance_description
from utils.logging_config import setup_logging
import torch
def run():
cfg = load_cfg()
if cfg.torch_hub_dir:
torch.hub.set_dir(cfg.torch_hub_dir)
set_global_seed(cfg.agent.seed)
logger = setup_logging(cfg)
logger.info(f'Starting run "{cfg.exp_name}"')
task_desc = load_task_desc(cfg)
if cfg.coldstart.use_coldstart:
logger.info("Loading guidance from knowledge base")
cfg.coldstart.description = build_guidance_description(cfg)
logger.info(f"Guidance description: {cfg.coldstart.description}")
with Status("Preparing agent workspace (copying and extracting files) ..."):
prep_agent_workspace(cfg)
global_step = 0
def cleanup():
if global_step == 0:
shutil.rmtree(cfg.workspace_dir)
atexit.register(cleanup)
journal = Journal()
agent = Agent(
task_desc=task_desc,
cfg=cfg,
journal=journal,
)
interpreter = Interpreter(
cfg.workspace_dir, **OmegaConf.to_container(cfg.exec), cfg=cfg # type: ignore
)
global_step = len(journal)
status = Status("[green]Generating code...")
def exec_callback(*args, **kwargs):
status.update("[magenta]Executing code...")
res = interpreter.run(*args, **kwargs)
status.update("[green]Generating code...")
return res
def step_task(node=None):
if node:
logger.info(f"[step_task] Processing node: {node.id}")
else:
logger.info(f"[step_task] Processing virtual root node.")
return agent.step(exec_callback=exec_callback, node=node)
max_workers = interpreter.max_parallel_run
total_steps = cfg.agent.steps
initial_draft_count = cfg.agent.initial_drafts
logger.info(f"🚀 ThreadPool max_workers set to: {max_workers} (matching interpreter capacity)")
logger.info(f"🎯 Initial draft count: {initial_draft_count} (will be executed sequentially for diversity)")
lock = threading.Lock()
completed = 0
pending_draft_nodes = []
if initial_draft_count > 0 and total_steps > 0:
logger.info(f"📝 Phase 1: Sequential draft generation (code only, {initial_draft_count} drafts)")
def step_task_generate_only():
logger.info(f"[step_task_generate_only] Generating draft from virtual root")
return agent.step(exec_callback=exec_callback, node=None, execute_immediately=False)
for draft_idx in range(min(initial_draft_count, total_steps)):
try:
logger.info(f"🔨 Generating draft {draft_idx + 1}/{min(initial_draft_count, total_steps)} (code only)")
cur_node = step_task_generate_only()
pending_draft_nodes.append(cur_node)
logger.info(f"✅ Draft {draft_idx + 1} code generated: node.id={cur_node.id}, added to virtual_root.children")
except Exception as e:
logger.exception(f"❌ Exception during draft {draft_idx + 1} generation: {e}")
logger.info(f"✅ Phase 1 complete: {len(pending_draft_nodes)} draft codes generated")
if pending_draft_nodes or completed < total_steps:
logger.info(f"🚀 Phase 2: Pipelined parallel execution")
logger.info(f" - Pending draft executions: {len(pending_draft_nodes)}")
logger.info(f" - Remaining steps: {total_steps - completed}")
def execute_draft_node(node):
try:
executed_node = agent.execute_deferred_node(node, exec_callback)
logger.info(f"✅ Draft node {executed_node.id} executed: metric={executed_node.metric.value}")
return executed_node
except Exception as e:
logger.exception(f"❌ Exception during draft node {node.id} execution: {e}")
return None
executor = ThreadPoolExecutor(max_workers=max_workers)
interrupted = False
try:
futures = set()
for i, node in enumerate(pending_draft_nodes):
futures.add(executor.submit(execute_draft_node, node))
logger.info(f"📤 Submitted draft execution: {node.id}")
if i < len(pending_draft_nodes) - 1:
time.sleep(10)
logger.info(f"⏱️ Waiting 10s before next draft to stagger initialization...")
initial_step_tasks = min(max_workers, total_steps - completed) - len(pending_draft_nodes)
if initial_step_tasks > 0:
for _ in range(initial_step_tasks):
futures.add(executor.submit(step_task))
logger.info(f"📤 Submitted initial step_task to fill thread pool")
while completed < total_steps:
done, _ = wait(futures, return_when=FIRST_COMPLETED, timeout=1.0)
if not done:
continue # timeout, no completed futures, retry (allows SIGINT handling)
for fut in done:
futures.remove(fut)
try:
cur_node = fut.result()
if cur_node:
logger.info(f"✅ Task completed: node_id={cur_node.id}, step={cur_node.step}, is_buggy={cur_node.is_buggy}, metric={cur_node.metric.value if cur_node.metric else 'N/A'}")
else:
logger.warning(f"⚠️ Task returned None (execution failed)")
except Exception as e:
logger.exception(f"❌ Exception during task execution: {e}")
cur_node = None
with lock:
save_run(cfg, journal)
completed = len(journal) - 1 # Exclude virtual node
if completed == total_steps:
logger.info(journal_to_string_tree(journal))
if completed + len(futures) < total_steps:
futures.add(executor.submit(step_task, cur_node))
logger.info(f"📤 Submitted next task based on node {cur_node.id if cur_node else 'None'}")
logger.info(f"📊 Progress: {completed}/{total_steps} steps completed, {len(futures)} tasks running")
except KeyboardInterrupt:
interrupted = True
logger.info("KeyboardInterrupt received, terminating subprocesses and shutting down...")
interpreter.terminate_all_subprocesses()
executor.shutdown(wait=False, cancel_futures=True) if sys.version_info >= (3, 9) else executor.shutdown(wait=False)
raise
finally:
if not interrupted:
executor.shutdown(wait=True)
else:
logger.info(f"✅ All steps completed in Phase 1 (total_steps={total_steps} <= initial_draft_count={initial_draft_count})")
interpreter.cleanup_session(-1)
if __name__ == "__main__":
run()