forked from THUDM/slime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate.py
More file actions
361 lines (326 loc) · 13.8 KB
/
Copy pathgenerate.py
File metadata and controls
361 lines (326 loc) · 13.8 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
"""Coding-Agent RL: per-sample generate() function for slime.
--custom-generate-function-path examples.coding_agent_rl.generate.generate
generate() is a four-stage orchestrator: swe.prepare_workspace + harness.run
-> swe.git_diff -> swe.run_evaluation -> adapter.finish_session. The (harness,
adapter) pair is chosen by the SWE_AGENT env var (claude_code | codex); see
_AGENTS below.
Sandbox-side work is split across three layers: the provider-agnostic sandbox
contract (slime.agent.sandbox), the swappable harness lifecycle
(slime.agent.harness), and the SWE task layer (examples.coding_agent_rl.swe --
dataset parsing, workspace prep, diff, eval). LLM plumbing (Anthropic / OpenAI
<-> SGLang /generate, token capture, segment split) is the matching
slime.agent.adapters adapter. swe.get_metadata documents the dataset row schema
and produces the md dict consumed below.
"""
from __future__ import annotations
import asyncio
import logging
import os
import random
import secrets
import time
import traceback
from collections.abc import AsyncIterator
from contextlib import asynccontextmanager
from dataclasses import dataclass
from typing import Any
from slime.agent.adapters import AnthropicAdapter, OpenAIAdapter
from slime.agent.aiohttp_threaded import FilteredAccessLogger, run_app_in_thread
from slime.agent.harness import ClaudeCodeHarness, CodexHarness
from slime.agent.sandbox import E2BSandbox
from slime.utils.misc import SingletonMeta
from slime.utils.processing_utils import load_tokenizer
from slime.utils.types import Sample
from . import swe
logger = logging.getLogger(__name__)
logging.getLogger("e2b").setLevel(logging.WARNING)
_AGENTS = {
"claude_code": (ClaudeCodeHarness, AnthropicAdapter),
"codex": (CodexHarness, OpenAIAdapter),
}
AGENT_NAME = os.environ.get("SWE_AGENT", "claude_code")
if AGENT_NAME not in _AGENTS:
raise ValueError(f"SWE_AGENT={AGENT_NAME!r} not in {sorted(_AGENTS)}")
HARNESS_CLS, ADAPTER_CLS = _AGENTS[AGENT_NAME]
@dataclass(frozen=True)
class SweConfig:
eval_protocol: str # eval-path schema/grader (SWE_EVAL_PROTOCOL)
train_protocol: str # train-path schema/grader (SWE_TRAIN_PROTOCOL)
adapter_public_host: str | None
adapter_bind_host: str
adapter_port: int
fork_merge_threshold: int | None
agent_time_budget_sec: int
eval_timeout_sec: int
rollout_guard_sec: int
boot_concurrency: int
boot_retries: int
@classmethod
def from_env(cls) -> SweConfig:
agent_time_budget = int(os.environ.get("SWE_AGENT_TIME_BUDGET_SEC", "1800"))
eval_timeout = int(os.environ.get("SWE_EVAL_TIMEOUT_SEC", "600"))
guard = int(os.environ.get("SWE_ROLLOUT_GUARD_SEC", "0") or 0) or (agent_time_budget + eval_timeout + 180)
fork = int(v) if (v := os.environ.get("SLIME_FORK_MERGE_MAX_RESPONSE_TOKENS")) else None
return cls(
eval_protocol=os.environ.get("SWE_EVAL_PROTOCOL", swe.PROTOCOL_SCALESWE),
train_protocol=os.environ.get("SWE_TRAIN_PROTOCOL", swe.PROTOCOL_SCALESWE),
adapter_public_host=os.environ.get("ADAPTER_PUBLIC_HOST"),
adapter_bind_host=os.environ.get("ADAPTER_BIND_HOST", "0.0.0.0"),
adapter_port=int(os.environ.get("ADAPTER_PORT", "18001")),
fork_merge_threshold=fork,
agent_time_budget_sec=agent_time_budget,
eval_timeout_sec=eval_timeout,
rollout_guard_sec=guard,
boot_concurrency=int(os.environ.get("SWE_BOOT_CONCURRENCY", "16")),
boot_retries=int(os.environ.get("SWE_BOOT_RETRIES", "2")),
)
CONFIG = SweConfig.from_env()
_BOOT_SEM = asyncio.Semaphore(CONFIG.boot_concurrency)
@asynccontextmanager
async def boot_agent_sandbox(image: str, instance_id: str) -> AsyncIterator[E2BSandbox]:
"""Boot a fresh E2B sandbox and install the selected harness toolchain.
Create the sandbox from the dataset image, install Node 22 + the harness CLI
from host tarballs, retry transient boot/install failures, and close the
sandbox when the caller leaves the context.
"""
sb = None
last_err: Exception | None = None
for attempt in range(CONFIG.boot_retries):
cand = E2BSandbox(image)
try:
async with _BOOT_SEM:
await cand.__aenter__()
try:
await HARNESS_CLS().install_cli(cand)
except BaseException:
await cand.__aexit__(None, None, None)
raise
sb = cand
break
except Exception as e:
last_err = e
logger.warning(
"[coding_agent_rl] %s: provision attempt %d/%d failed: %s: %s",
instance_id,
attempt + 1,
CONFIG.boot_retries,
type(e).__name__,
str(e)[:200],
)
await asyncio.sleep(1 + attempt + random.random())
if sb is None:
assert last_err is not None
raise last_err
try:
yield sb
finally:
await sb.__aexit__(None, None, None)
class _AdapterService(metaclass=SingletonMeta):
def __init__(self, args) -> None:
self.tokenizer = load_tokenizer(args.hf_checkpoint, trust_remote_code=True)
self.max_context_len = int(getattr(args, "rollout_max_context_len", 0) or 0)
self.tool_parser = getattr(args, "sglang_tool_call_parser", None) or None
self.reasoning_parser = getattr(args, "sglang_reasoning_parser", None) or None
sglang_url = f"http://{args.sglang_router_ip}:{args.sglang_router_port}"
if not CONFIG.adapter_public_host:
raise RuntimeError(
"ADAPTER_PUBLIC_HOST is not set. Export it to the host IP that "
"sandboxes can reach for reverse-connection to the adapter; "
"without it the sandbox cannot dial back and the rollout aborts."
)
self.adapter = ADAPTER_CLS(
tokenizer=self.tokenizer,
sglang_url=sglang_url,
tool_parser=self.tool_parser,
reasoning_parser=self.reasoning_parser,
fork_threshold_tokens=CONFIG.fork_merge_threshold,
)
# handler_cancellation=True so a client disconnect cancels the handler
# coroutine, arming the fire-and-forget /abort_request in the adapter.
# Otherwise a cancelled client leaves an inflight sglang /generate that
# races the next release_memory_occupation and trips its idle assertion.
self.app_handle = run_app_in_thread(
self.adapter.app,
host=CONFIG.adapter_bind_host,
port=CONFIG.adapter_port,
thread_name="anthropic-adapter",
runner_kwargs={
"handler_cancellation": True,
"access_log_class": FilteredAccessLogger,
},
)
self.adapter_url = f"http://{CONFIG.adapter_public_host}:{self.app_handle.port}"
logger.info(
"[coding_agent_rl] tokenizer=%s adapter=%s max_context_len=%s tool_parser=%s reasoning_parser=%s",
args.hf_checkpoint,
self.adapter_url,
self.max_context_len,
self.tool_parser,
self.reasoning_parser,
)
async def generate(args, base_sample: Sample, sampling_params: dict[str, Any], evaluation: bool = False):
"""Per-sample agent function with wall-clock guard (see rollout_guard_sec)."""
state = _AdapterService(args)
protocol = CONFIG.eval_protocol if evaluation else CONFIG.train_protocol
md = swe.get_metadata(base_sample, protocol)
instance_id = md["instance_id"]
if not md["image"] or not md["workdir"]:
return _abort_result(base_sample, "missing_image_or_workdir", instance_id)
reason = swe.evaluability_check(md)
if reason:
return _abort_result(base_sample, f"unevaluatable:{reason}", instance_id)
session_id = base_sample.session_id = _session_id(base_sample, instance_id)
state.adapter.open_session(
session_id,
sampling_defaults=sampling_params,
max_context_tokens=state.max_context_len,
)
t0 = time.time()
try:
async with asyncio.timeout(CONFIG.rollout_guard_sec):
async with boot_agent_sandbox(md["image"], instance_id) as sb:
await swe.prepare_workspace(sb, md["workdir"], md)
agent_exit_code = await HARNESS_CLS().run(
sb,
workdir=md["workdir"],
session_id=session_id,
adapter_url=state.adapter_url,
time_budget_sec=CONFIG.agent_time_budget_sec,
prompt=swe.SWE_PROMPT,
)
diff_text = await swe.git_diff(sb, md["workdir"])
reward, applied_cleanly = await swe.run_evaluation(
md,
diff_text=diff_text,
timeout_sec=CONFIG.eval_timeout_sec,
)
if evaluation:
logger.info(
"[coding_agent_rl] %s: reward=%.2f applied=%s agent_exit_code=%d elapsed=%.1fs (eval-only)",
instance_id,
float(reward),
bool(applied_cleanly),
agent_exit_code,
time.time() - t0,
)
return _eval_result(
base_sample,
reward=float(reward),
applied_cleanly=bool(applied_cleanly),
agent_exit_code=agent_exit_code,
instance_id=instance_id,
)
samples = await state.adapter.finish_session(
session_id,
base_sample=base_sample,
reward=float(reward),
extra_metadata={
"grading_solved": float(reward) == 1.0,
"instance_id": instance_id,
},
)
if not samples:
return _abort_result(base_sample, "adapter_session_empty", instance_id)
for s in samples:
s.metadata = {**(s.metadata or {}), "agent_exit_code": agent_exit_code}
if agent_exit_code != 0:
reason = "time budget exceeded" if agent_exit_code < 0 else f"CLI error (exit {agent_exit_code})"
logger.warning(
"[coding_agent_rl] %s: agent_exit_code=%d (%s)",
instance_id,
agent_exit_code,
reason,
)
logger.info(
"[coding_agent_rl] %s: reward=%.2f applied=%s agent_exit_code=%d elapsed=%.1fs segments=%d",
instance_id,
float(reward),
bool(applied_cleanly),
agent_exit_code,
time.time() - t0,
len(samples),
)
return samples
except asyncio.TimeoutError:
_log_timeout_diagnostic(t0, instance_id)
return _abort_result(base_sample, "wall_clock_timeout", instance_id)
except Exception as e:
logger.warning(
"[coding_agent_rl] %s: rollout failed: %s\n%s",
instance_id,
e,
traceback.format_exc(),
)
return _abort_result(base_sample, f"exception:{type(e).__name__}", instance_id)
finally:
await state.adapter.drop_session(session_id, wait_timeout=30) # cleanup only, idempotent
await asyncio.sleep(10)
def _log_timeout_diagnostic(t0: float, instance_id: str) -> None:
# Dump pending-task names when the wall-clock guard fires. Must not crash.
try:
elapsed = time.time() - t0
pending = [t for t in asyncio.all_tasks() if not t.done()]
stuck = []
for t in pending[:5]: # cap to avoid log spam
coro = getattr(t, "_coro", None)
stuck.append(getattr(coro, "__qualname__", repr(coro)))
logger.warning(
"[coding_agent_rl] %s: wall_clock_timeout after %.1fs "
"(guard=%ds); %d tasks pending; sample of stuck: %s",
instance_id,
elapsed,
CONFIG.rollout_guard_sec,
len(pending),
stuck,
)
except Exception: # pragma: no cover - diag must never crash
pass
def _session_id(sample: Sample, instance_id: str) -> str:
if sample.session_id:
return sample.session_id
if sample.index is not None and sample.group_index is not None:
return f"cagent-{instance_id}-{sample.index}-{sample.group_index}"
return f"cagent-{instance_id}-{secrets.token_hex(8)}"
def _abort_result(sample: Sample, reason: str, instance_id: str) -> list[Sample]:
"""Mark ``sample`` aborted in place and return it in the list shape this
fan-out generate function always yields."""
sample.tokens = [0, 0]
sample.response = ""
sample.response_length = 1
sample.loss_mask = [0]
sample.rollout_log_probs = [0.0]
sample.reward = 0.0
sample.remove_sample = True
sample.status = Sample.Status.ABORTED
sample.metadata = {
**(sample.metadata or {}),
"abort_reason": reason,
"instance_id": instance_id,
}
logger.warning("[coding_agent_rl] %s aborted: %s", instance_id, reason)
return [sample]
def _eval_result(
sample: Sample,
*,
reward: float,
applied_cleanly: bool,
agent_exit_code: int | None,
instance_id: str,
) -> list[Sample]:
"""Eval-path placeholder: only ``reward`` matters for ``eval/sweb``."""
sample.tokens = [0, 0]
sample.response = ""
sample.response_length = 1
sample.loss_mask = [0]
sample.rollout_log_probs = [0.0]
sample.reward = float(reward)
sample.remove_sample = True
sample.status = Sample.Status.COMPLETED
sample.metadata = {
**(sample.metadata or {}),
"instance_id": instance_id,
"grading_solved": float(reward) == 1.0,
"applied_cleanly": applied_cleanly,
"agent_exit_code": agent_exit_code,
}
return [sample]