File size: 7,251 Bytes
fb361ec | 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 | #!/usr/bin/env python3
"""Run one attempt per task with native benchmark verification."""
import argparse
import asyncio
import ipaddress
import json
import os
from pathlib import Path
import shutil
import subprocess
import sys
from harness.bootstrap import ROOT
SPLITS = ('glm5_500', 'qwen3_coder_30b_500', 'qwen3_30b_instruct2507_500')
async def execute(config, rows, concurrency):
from harness.network import install_policy
install_policy()
from harbor.job import Job
from harbor.models.job.config import JobConfig
semaphore = asyncio.Semaphore(concurrency)
async def one(index, row):
async with semaphore:
import copy
value = copy.deepcopy(config)
value['job_name'] = f'task-{index:04d}'
value['tasks'] = [config['tasks'][index]]
if row['benchmark'] != 'swe_review':
value['agents'][0]['kwargs']['task_path'] = config['tasks'][index]['path']
try:
job = await Job.create(JobConfig.model_validate(value))
result = await job.run()
(Path(config['jobs_dir']) / (value['job_name'] + '.json')).write_text(result.model_dump_json(indent=2))
except Exception as error:
# Preserve failure and continue other tasks; scoring reports missing native results.
target = Path(config['jobs_dir']) / (value['job_name'] + '.error.json')
target.parent.mkdir(parents=True, exist_ok=True)
target.write_text(json.dumps({'task': row['task'], 'error_type': type(error).__name__, 'message': str(error)}))
await asyncio.gather(*(one(i, row) for i, row in enumerate(rows)))
def main():
p = argparse.ArgumentParser(description=__doc__)
p.add_argument('benchmark', choices=('tb21', 'pro', 'deepswe', 'swe_review'))
p.add_argument('--model', required=True)
p.add_argument('--split', choices=SPLITS, default='glm5_500')
p.add_argument('--output', type=Path, required=True)
p.add_argument('--concurrency', type=int, default=4)
p.add_argument('--limit', type=int)
p.add_argument('--context', type=int, default=200000)
p.add_argument('--max-tokens', type=int, default=32768)
p.add_argument('--reasoning-effort', default='high')
p.add_argument('--temperature', type=float, default=0.7)
p.add_argument('--gateway-host', help='Container-reachable IPv4 of this evaluation host; default Docker bridge gateway')
p.add_argument('--execute', action='store_true', help='Without this flag, write a plan only')
p.add_argument('--build-images', action='store_true', help='For SWE-Review, explicitly rebuild archived task Dockerfiles instead of using imported final images')
a = p.parse_args()
if not 1 <= a.concurrency <= 64 or (a.limit is not None and a.limit <= 0):
p.error('Positive task limit and concurrency in 1..64 required')
base, key = os.environ.get('MODEL_BASE_URL'), os.environ.get('MODEL_API_KEY')
if a.execute and (not base or not key):
p.error('Set MODEL_BASE_URL and MODEL_API_KEY before --execute')
manifest = json.loads((ROOT / 'tasks' / 'manifest.json').read_text())
rows = [r for r in manifest if r['benchmark'] == a.benchmark and (a.benchmark != 'swe_review' or r['split'] == a.split)]
expected = len(rows)
rows = rows[:a.limit] if a.limit else rows
out = a.output.resolve()
out.mkdir(parents=True, exist_ok=False)
plan = {'benchmark': a.benchmark, 'split': a.split if a.benchmark == 'swe_review' else None,
'model': a.model, 'expected_tasks': expected, 'selected_tasks': [r['task'] for r in rows],
'attempts_per_task': 1, 'concurrency': a.concurrency, 'context': a.context,
'max_tokens': a.max_tokens, 'temperature': a.temperature, 'reasoning_effort': a.reasoning_effort,
'agent': 'openhands-sdk==1.16.1' if a.benchmark == 'swe_review' else 'opencode==1.19.5',
'partial': len(rows) != expected}
plan['swe_review_image_mode'] = 'build_archived_context' if a.build_images else 'imported_final_images'
(out / 'PLAN.json').write_text(json.dumps(plan, indent=2))
print(json.dumps(plan, indent=2), flush=True)
if not a.execute:
return
gateway_host = a.gateway_host or json.loads(subprocess.check_output(['docker', 'network', 'inspect', 'bridge'], text=True))[0]['IPAM']['Config'][0]['Gateway']
ipaddress.IPv4Address(gateway_host)
os.environ.update(BENCH_OUTPUT=str(out), BENCH_MODEL=a.model, BENCH_CONTEXT=str(a.context),
BENCH_MAX_TOKENS=str(a.max_tokens), BENCH_REASONING=a.reasoning_effort,
BENCH_GATEWAY_HOST=gateway_host, COLLECTION_TEMPERATURE=str(a.temperature), HARBOR_TELEMETRY='0')
from harness.gateway import Gateway
with Gateway(out, a.model, base, key) as gateway:
os.environ.update(COLLECTION_RELAY_PORT=str(gateway.port), LLM_BASE_URL=f'http://{gateway_host}:{gateway.port}/v1/review',
LLM_API_KEY=gateway.token, OPENHANDS_LLM_NATIVE_TOOL_CALLING='true')
task_paths = []
for row in rows:
source = ROOT / row['path']
destination = out / 'task_inputs' / row['task']
shutil.copytree(source, destination)
text = (destination / 'task.toml').read_text()
text = text.replace('172.17.0.1', gateway_host)
if a.benchmark == 'swe_review' and not a.build_images:
image = 'agent-benchmarks/swe-review:' + row['split'] + '-' + row['task']
subprocess.run(['docker', 'image', 'inspect', image], check=True, stdout=subprocess.DEVNULL)
text = text.replace('[environment]', '[environment]\ndocker_image = ' + json.dumps(image), 1)
(destination / 'task.toml').write_text(text)
task_paths.append({'path': str(destination)})
kwargs = {'version': '1.16.1', 'max_iterations': 100, 'reasoning_effort': a.reasoning_effort,
'temperature': a.temperature, 'send_reasoning_content_models': [a.model]}
if a.benchmark != 'swe_review':
kwargs = {'version': '1.19.5', 'benchmark': {'tb21': 'tb', 'deepswe': 'deep', 'pro': 'pro'}[a.benchmark]}
config = {'job_name': 'evaluation', 'jobs_dir': str(out / 'trials'), 'n_attempts': 1,
'n_concurrent_trials': 1, 'quiet': True,
'timeout_multiplier': 3.0 if a.benchmark == 'swe_review' else 1.0,
'environment': {'import_path': 'harness.docker:StrictDocker', 'delete': True,
'cpu_enforcement_policy': 'limit', 'memory_enforcement_policy': 'limit'},
'agents': [{'import_path': 'harness.review_agent:ReviewOpenHands' if a.benchmark == 'swe_review' else 'harness.agent:IsolatedOpenCode',
'model_name': 'openai/' + a.model if a.benchmark == 'swe_review' else 'collection/' + a.model,
'kwargs': kwargs, 'override_setup_timeout_sec': 1200}],
'tasks': task_paths, 'retry': {'max_retries': 0}}
(out / 'job.json').write_text(json.dumps(config, indent=2))
asyncio.run(execute(config, rows, a.concurrency))
subprocess.run([sys.executable, str(ROOT / 'score.py'), '--output', str(out)], check=True)
if __name__ == '__main__':
main()
|