Repository navigation
Expand file tree
/
Copy pathbenchmark_executor.py
More file actions
434 lines (396 loc) · 14.9 KB
/
Copy pathbenchmark_executor.py
File metadata and controls
434 lines (396 loc) · 14.9 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#!/usr/bin/env python3
"""Single-shot model executor used by the llm-router quality benchmark."""
from __future__ import annotations
import contextlib
import json
import os
import re
import signal
import subprocess
import tempfile
import time
from dataclasses import dataclass
from pathlib import Path
from typing import Any
ENV_PATTERN = re.compile(r"\$\{([A-Za-z_][A-Za-z0-9_]*)\}")
ALLOWED_ENVIRONMENT_NAMES = frozenset({
"ALL_PROXY",
"APPDATA",
"COLORTERM",
"COMSPEC",
"FORCE_COLOR",
"HOME",
"HTTP_PROXY",
"HTTPS_PROXY",
"LANG",
"LANGUAGE",
"LOCALAPPDATA",
"LOGNAME",
"NODE_EXTRA_CA_CERTS",
"NO_COLOR",
"NO_PROXY",
"PATH",
"PATHEXT",
"SHELL",
"SSL_CERT_DIR",
"SSL_CERT_FILE",
"SYSTEMROOT",
"TEMP",
"TERM",
"TERM_PROGRAM",
"TERM_PROGRAM_VERSION",
"TMP",
"TMPDIR",
"TZ",
"USER",
"USERPROFILE",
"WINDIR",
"XDG_CACHE_HOME",
"XDG_CONFIG_HOME",
"XDG_DATA_HOME",
"XDG_RUNTIME_DIR",
"XDG_STATE_HOME",
})
# Locale only. Credential-bearing prefixes such as ANTHROPIC_ and CLAUDE_ stay
# out on purpose: the same baseline reaches every route, so inheriting them
# would hand a Codex or MiniMax run the Anthropic credentials of the machine.
# A route that needs one declares it under headless.env.
ALLOWED_ENVIRONMENT_PREFIXES = ("LC_",)
CLAUDE_MAX_EFFORT_PATTERN = re.compile(
r"arquitet|architecture|architectural|produto|product|idea|ideia|brainstorm|"
r"copy|venda|sales|marketing|rede social|social media|criativ|creative|roadmap|"
r"planej|planning|design|spec|lan[cç]amento|launch",
re.IGNORECASE,
)
CLAUDE_XHIGH_EFFORT_PATTERN = re.compile(
r"discuss|debate|trade.?off|pr[oó]s e contras|"
r"compare (?:op[cç][oõ]es|alternativas|abordagens)|policy|pol[ií]tica|argument|"
r"falsific|open.?ended|decis[aã]o operacional",
re.IGNORECASE,
)
class ConfigError(RuntimeError):
"""Raised when the benchmark executor configuration is invalid."""
@dataclass
class ProcessResult:
status: str
exit_code: int | None
output: str
stdout: str
stderr: str
duration_seconds: float
def expand_env_string(value: str) -> str:
def replace(match: re.Match[str]) -> str:
name = match.group(1)
if name not in os.environ:
raise ConfigError(f"missing required environment variable: {name}")
return os.environ[name]
return ENV_PATTERN.sub(replace, value)
def runtime_environment() -> dict[str, str]:
"""Return the runtime-only slice of the parent environment.
Routes declare every credential they need under `headless.env`, so an
executor inherits paths, locale, proxy and TLS settings but no secret at
all. Without this filter a Claude run also received MINIMAX_API_KEY and
ZAI_API_KEY, and a Codex run received the Anthropic credentials.
The name list matches opencode/lib/claude_agent.mjs, but the prefixes do
not: there the subprocess is always Claude, while one baseline here feeds
four different providers.
"""
return {
name: value
for name, value in os.environ.items()
if name.upper() in ALLOWED_ENVIRONMENT_NAMES
or name.upper().startswith(ALLOWED_ENVIRONMENT_PREFIXES)
}
def select_claude_effort(prompt: str) -> str:
"""Select Claude effort for one benchmark prompt."""
if CLAUDE_MAX_EFFORT_PATTERN.search(prompt):
return "max"
if CLAUDE_XHIGH_EFFORT_PATTERN.search(prompt):
return "xhigh"
return "max"
def parse_json_object(value: str) -> dict[str, Any]:
text = value.strip()
if text.startswith("```"):
lines = text.splitlines()
if lines:
lines = lines[1:]
if lines and lines[-1].strip() == "```":
lines = lines[:-1]
text = "\n".join(lines).strip()
try:
parsed = json.loads(text)
if isinstance(parsed, dict):
return parsed
except json.JSONDecodeError:
pass
decoder = json.JSONDecoder()
for index, char in enumerate(text):
if char != "{":
continue
try:
parsed, _ = decoder.raw_decode(text[index:])
except json.JSONDecodeError:
continue
if isinstance(parsed, dict):
return parsed
raise ValueError("output does not contain a valid JSON object")
def stop_process_group(process: subprocess.Popen[str]) -> tuple[str, str]:
try:
os.killpg(process.pid, signal.SIGTERM)
except ProcessLookupError:
pass
try:
return process.communicate(timeout=2)
except subprocess.TimeoutExpired:
try:
os.killpg(process.pid, signal.SIGKILL)
except ProcessLookupError:
pass
return process.communicate()
def normalize_output(output_format: str, stdout: str, output_file: str | None) -> str:
if output_format == "text":
return stdout.strip()
if output_format == "claude_json":
payload = parse_json_object(stdout)
result = payload.get("result")
if not isinstance(result, str):
raise ValueError("claude_json output does not contain a textual result field")
return result.strip()
if output_format == "codex_last_message":
if not output_file:
raise ValueError("codex_last_message requires {output_file} in argv")
try:
return Path(output_file).read_text(encoding="utf-8").strip()
except OSError as error:
raise ValueError(
f"could not read the final Codex message: {error}"
) from error
raise ValueError(f"unknown output_format: {output_format}")
def load_config(path: Path) -> dict[str, Any]:
try:
with path.open(encoding="utf-8") as stream:
config = json.load(stream)
except OSError as error:
raise ConfigError(f"could not read config: {error}") from error
except json.JSONDecodeError as error:
raise ConfigError(f"invalid JSON config: {error}") from error
if not isinstance(config, dict):
raise ConfigError("config must be a JSON object")
return config
class BenchmarkExecutor:
def __init__(self, config_path: Path, config: dict[str, Any], cwd: Path) -> None:
self.config_path = config_path
self.config = config
self.cwd = cwd
self.project_root = self._find_git_root(cwd) or cwd
self.routes = self._load_routes(config.get("routes"))
@staticmethod
def _require_dict(value: Any, path: str) -> dict[str, Any]:
if not isinstance(value, dict):
raise ConfigError(f"{path} must be an object")
return value
@staticmethod
def _require_string(value: Any, path: str) -> str:
if not isinstance(value, str) or not value:
raise ConfigError(f"{path} must be a non-empty string")
return value
@staticmethod
def _positive_number(value: Any, path: str) -> float:
if not isinstance(value, (int, float)) or isinstance(value, bool) or value <= 0:
raise ConfigError(f"{path} must be a positive number")
return float(value)
@staticmethod
def _load_routes(routes: Any) -> dict[str, dict[str, Any]]:
if not isinstance(routes, list) or not routes:
raise ConfigError("routes must be a non-empty list")
result: dict[str, dict[str, Any]] = {}
for route in routes:
if not isinstance(route, dict):
raise ConfigError("each routes item must be an object")
name = route.get("name")
if not isinstance(name, str) or not name:
raise ConfigError("routes[].name must be a non-empty string")
if name in result:
raise ConfigError(f"duplicate route: {name}")
result[name] = route
return result
@staticmethod
def _find_git_root(cwd: Path) -> Path | None:
try:
result = subprocess.run(
["git", "rev-parse", "--show-toplevel"],
cwd=cwd,
text=True,
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
timeout=10,
check=False,
)
except (FileNotFoundError, subprocess.TimeoutExpired, OSError):
return None
if result.returncode != 0 or not result.stdout.strip():
return None
return Path(result.stdout.strip()).resolve()
def _configured_env(self, raw_env: Any, path: str) -> dict[str, str]:
raw_env = self._require_dict(raw_env, path)
env = runtime_environment()
for name, value in raw_env.items():
if not isinstance(name, str) or not name:
raise ConfigError(f"invalid name in {path}")
if isinstance(value, str):
env[name] = expand_env_string(value)
continue
if isinstance(value, dict) and isinstance(value.get("from_env"), str):
source = value["from_env"]
if source not in os.environ or not os.environ[source]:
raise ConfigError(f"missing required environment variable: {source}")
env[name] = os.environ[source]
continue
raise ConfigError(f"invalid environment value for {name}")
return env
def _resolve_env(self, headless: dict[str, Any]) -> dict[str, str]:
return self._configured_env(headless.get("env", {}), "routes[].headless.env")
def _prepare_argv(
self,
raw_argv: Any,
output_file: str | None,
process_cwd: Path,
runtime_values: dict[str, str] | None = None,
) -> list[str]:
if not isinstance(raw_argv, list) or not raw_argv or not all(
isinstance(item, str) for item in raw_argv
):
raise ConfigError("headless.argv must be a non-empty list of strings")
values = {
"cwd": str(process_cwd),
"project_root": str(self.project_root),
"output_file": output_file or "",
}
if runtime_values:
values.update(runtime_values)
argv: list[str] = []
for item in raw_argv:
expanded = expand_env_string(item)
for key, value in values.items():
expanded = expanded.replace("{" + key + "}", value)
argv.append(expanded)
return argv
@staticmethod
def _profile_runtime_values(
profile: dict[str, Any], role: str, prompt: str
) -> dict[str, str]:
effort_policy = profile.get("effort_policy")
if effort_policy is None:
return {}
if role != "worker":
raise ConfigError("headless.effort_policy can only be used by the worker")
if effort_policy != "claude_dynamic":
raise ConfigError(f"unknown headless.effort_policy: {effort_policy}")
return {"effort": select_claude_effort(prompt)}
def execute_model(self, route_name: str, role: str, prompt: str) -> ProcessResult:
started = time.monotonic()
output_file: str | None = None
try:
if route_name not in self.routes:
raise ConfigError(f"unknown route: {route_name}")
route = self.routes[route_name]
headless = self._require_dict(
route.get("headless"), f"routes[{route_name}].headless"
)
profile = self._require_dict(
headless.get(role), f"routes[{route_name}].headless.{role}"
)
timeout = self._positive_number(
profile.get("timeout_seconds"),
f"routes[{route_name}].headless.{role}.timeout_seconds",
)
raw_argv = profile.get("argv")
needs_output_file = (
profile.get("output_format") == "codex_last_message"
or isinstance(raw_argv, list)
and any("{output_file}" in str(item) for item in raw_argv)
)
if needs_output_file:
descriptor, output_file = tempfile.mkstemp(
prefix="llm-router-", suffix=".out"
)
os.close(descriptor)
runtime_values = self._profile_runtime_values(profile, role, prompt)
argv = self._prepare_argv(raw_argv, output_file, self.cwd, runtime_values)
env = self._resolve_env(headless)
process = subprocess.Popen(
argv,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
cwd=self.cwd,
env=env,
start_new_session=True,
)
try:
stdout, stderr = process.communicate(input=prompt, timeout=timeout)
except subprocess.TimeoutExpired:
stdout, stderr = stop_process_group(process)
return ProcessResult(
"timeout",
process.returncode,
"",
stdout or "",
stderr or "",
time.monotonic() - started,
)
except BaseException:
stop_process_group(process)
raise
if process.returncode != 0:
return ProcessResult(
"process_error",
process.returncode,
"",
stdout,
stderr,
time.monotonic() - started,
)
try:
output = normalize_output(
self._require_string(
profile.get("output_format"), "headless.output_format"
),
stdout,
output_file,
)
except ValueError as error:
return ProcessResult(
"process_error",
process.returncode,
"",
stdout,
f"{stderr}\n{error}".strip(),
time.monotonic() - started,
)
return ProcessResult(
"success",
process.returncode,
output,
stdout,
stderr,
time.monotonic() - started,
)
except (ConfigError, FileNotFoundError, OSError) as error:
return ProcessResult(
"process_error",
None,
"",
"",
str(error),
time.monotonic() - started,
)
finally:
# The scratch file only carries the final message between the child
# process and normalize_output, so it is runtime state of this call
# and never user content. Every exit path drops it, including the
# timeout and process_error returns above.
if output_file is not None:
with contextlib.suppress(OSError):
os.unlink(output_file)