-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask_processor.py
155 lines (127 loc) · 5.14 KB
/
task_processor.py
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
import os
import signal
import sys
import time
import zipfile
import struct # Required by some subprocesses
import subprocess
from utils.command import run_command
def flush_agents():
run_command("@2501 agents --flush")
def get_cli_version():
"""Get the CLI version by running the CLI version command."""
result = subprocess.run(['@2501', '--version'],
capture_output=True, text=True, check=True)
return result.stdout.strip()
def get_engine_version():
"""Get the engine version by running the engine version command."""
result = subprocess.run(['@2501', 'engine-version'],
capture_output=True, text=True, check=True)
return result.stdout.strip()
def process_task(task, files_dir, max_retries=3, agent_config='CODING_AGENT'):
"""
Process a single task and record the result in the benchmark report.
Args:
task (dict): The task dictionary.
files_dir (str): The directory containing the files.
max_retries (int): Maximum number of retries for the task.
agent_config (str): The agent configuration to use.
"""
start_time = time.time()
task_id = task['id']
input_command = task['input']
test_command = task.get('test_command', "")
test_script = task.get('test_script', "")
print(f"Processing task {task_id}")
# Unzip the corresponding zip file
zip_path = os.path.join(files_dir, f"{task_id}.zip")
if os.path.exists(zip_path):
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
zip_ref.extractall(files_dir)
print(f"Unzipped file: {zip_path}")
else:
# If the zip file does not exist, create the dir
os.makedirs(os.path.join(files_dir, task_id), exist_ok=True)
attempts = 0
passed = False
error_message = None
duration_ms = 0
accuracy = 0
prompt_limiter = "IMPORTANT: You are being benchmarked, don\\'t output prose or comments. Only provide the shortest answer possible."
prompt_limiter = ""
# limit the output generated by the AI
if input_command.endswith("'"):
input_command = input_command[:-1] + " " + prompt_limiter + "'"
else:
input_command += " " + prompt_limiter
while attempts < max_retries:
attempts += 1
try:
if attempts > 1:
print(f"Retrying task {task_id} (attempt {attempts})")
flush_agents()
# Execute the input command
print(f"Executing command: @2501 {input_command}")
stdout, stderr, returncode = run_command(
f"cd {files_dir}/{task_id} && @2501 init --config {agent_config} && @2501 {input_command}")
print(f"Command returncode: {returncode} | stdout: {stdout}")
if stderr.strip(): print(f"Command stderr: {stderr}")
if returncode != 0:
print(f"Command failed with return code {returncode} | Error output: {stderr}")
continue
test_local = locals()
passed = False
output = None
# Run the test command or script
if test_command:
print(f"Executing script at {test_command}")
# execute the script at path, with args
out, err, code = run_command(test_command)
print(f"Test command returncode: {code} | stdout: {out}")
if err.strip(): print(f"Test command stderr: {err}")
passed = int(code) == 0
output = passed and "PASS" or "FAIL"
elif test_script:
print(f"Executing in-line test script")
signal.signal(signal.SIGALRM, signal_handler)
signal.alarm(120) # 2 minutes timeout
try:
exec(test_script, globals(), test_local)
output = test_local.get('output', 'FAIL').strip().upper()
passed = output == "PASS"
except KeyboardInterrupt:
print('Interrupted! Terminating.')
sys.exit(0)
finally:
signal.alarm(0)
print(f"Test {task_id} | Passed: {passed}")
break
except Exception as e:
print(f"Test failed: {str(e)}", file=sys.stderr)
error_message = str(e)
# Retry only it's a server error
if "The server has returned an error" in str(e):
continue
else:
break
duration_ms = int((time.time() - start_time) * 1000)
accuracy = 1.0 / attempts if passed else 0.0
if accuracy is None:
accuracy = 1.0 if passed else 0.0
result_entry = {
"task_id": task_id,
"task_name": task_id,
"input_command": input_command,
"script": test_command or test_script,
"passed": passed,
"retries": attempts-1,
"metrics": {
"duration_ms": duration_ms,
"accuracy": accuracy,
},
"error_message": error_message,
}
return result_entry
def signal_handler(signum, frame):
raise TimeoutException(f"Timed out! {signum}")
class TimeoutException(Exception): pass