-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathasync_scenario_builder.py
More file actions
450 lines (387 loc) · 16.1 KB
/
Copy pathasync_scenario_builder.py
File metadata and controls
450 lines (387 loc) · 16.1 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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
"""AsyncScenarioBuilder for constructing scenarios with a fluent API."""
from __future__ import annotations
from typing import Dict, List, Iterable, Optional
from typing_extensions import Self, Unpack, Literal, override
from ..types import ScenarioCreateParams, ScenarioEnvironmentParam
from ._types import ScenarioPreview, LongRequestOptions
from .._client import AsyncRunloop
from .async_scenario import AsyncScenario
from .async_snapshot import AsyncSnapshot
from .async_blueprint import AsyncBlueprint
from ..types.scoring_function_param import (
Scorer,
ScoringFunctionParam,
ScorerAstGrepScoringFunction,
ScorerCommandScoringFunction,
ScorerTestBasedScoringFunction,
ScorerBashScriptScoringFunction,
ScorerPythonScriptScoringFunction,
ScorerTestBasedScoringFunctionTestFile,
)
class AsyncScenarioBuilder:
"""Async builder for constructing scenarios with a fluent API.
Provides a step-by-step interface for configuring all aspects of a scenario
before pushing it to the platform.
Example:
>>> builder = (
... runloop.scenario.builder("my-scenario")
... .from_blueprint(blueprint)
... .with_working_directory("/app")
... .with_problem_statement("Fix the bug in main.py")
... .add_test_command_scorer("tests", test_command="pytest")
... )
>>> params = builder.build()
>>> scenario = await runloop.scenario.create(**params) # equivalent to builder.push()
"""
def __init__(self, name: str, client: AsyncRunloop) -> None:
"""Initialize the builder.
:param name: Name for the scenario
:type name: str
:param client: AsyncRunloop client instance
:type client: AsyncRunloop
"""
self._client = client
self._name = name
# Environment configuration
self._blueprint: Optional[AsyncBlueprint] = None
self._snapshot: Optional[AsyncSnapshot] = None
self._working_directory: Optional[str] = None
# Input context
self._problem_statement: Optional[str] = None
self._additional_context: Optional[object] = None
# Scoring
self._scorers: List[ScoringFunctionParam] = []
# Metadata and other options
self._metadata: Dict[str, str] = {}
self._reference_output: Optional[str] = None
self._required_env_vars: Optional[List[str]] = None
self._required_secrets: Optional[List[str]] = None
self._validation_type: Optional[Literal["UNSPECIFIED", "FORWARD", "REVERSE", "EVALUATION"]] = None
@override
def __repr__(self) -> str:
return f"<AsyncScenarioBuilder name={self._name!r}>"
@property
def name(self) -> str:
"""Return the scenario name.
:return: Scenario name
:rtype: str
"""
return self._name
def from_blueprint(self, blueprint: AsyncBlueprint) -> Self:
"""Set a blueprint to define the baseline environment for the scenario.
:param blueprint: Blueprint to use
:type blueprint: AsyncBlueprint
:return: Self for method chaining
:rtype: Self
"""
self._blueprint = blueprint
self._snapshot = None # Clear snapshot if blueprint is set
return self
def from_snapshot(self, snapshot: AsyncSnapshot) -> Self:
"""Set a snapshot to define the baseline environment for the scenario.
:param snapshot: Snapshot to use
:type snapshot: AsyncSnapshot
:return: Self for method chaining
:rtype: Self
"""
self._snapshot = snapshot
self._blueprint = None # Clear blueprint if snapshot is set
return self
def with_working_directory(self, directory: str) -> Self:
"""Set the working directory for the scenario.
:param directory: Working directory path
:type directory: str
:return: Self for method chaining
:rtype: Self
"""
self._working_directory = directory
return self
def with_problem_statement(self, statement: str) -> Self:
"""Set the problem statement for the scenario; this will be provided as input context to the agent.
:param statement: Problem statement text
:type statement: str
:return: Self for method chaining
:rtype: Self
"""
self._problem_statement = statement
return self
def with_additional_context(self, context: object) -> Self:
"""Set additional structured context for the scenario.
This can be used to provide additional information to the agent, such as hints, examples, or other relevant information.
:param context: Additional context (JSON-serializable)
:type context: object
:return: Self for method chaining
:rtype: Self
"""
self._additional_context = context
return self
def _add_scorer(self, name: str, weight: float, scorer: Scorer) -> Self:
"""Internal helper to add a scorer to the list.
:raises ValueError: If weight is not positive
"""
if weight <= 0:
raise ValueError(f"Scorer weight must be positive, got {weight}")
self._scorers.append({"name": name, "weight": weight, "scorer": scorer})
return self
def add_test_command_scorer(
self,
name: str,
*,
test_command: str,
weight: float = 1.0,
test_files: Optional[Iterable[ScorerTestBasedScoringFunctionTestFile]] = None,
) -> Self:
"""Add a test-based scorer that runs a test command.
:param name: Name of the scoring function
:type name: str
:param test_command: Command to run tests (e.g., "pytest")
:type test_command: str
:param weight: Weight for this scorer (normalized automatically)
:type weight: float
:param test_files: Optional test files to create before running
:type test_files: Optional[Iterable[ScorerTestBasedScoringFunctionTestFile]]
:return: Self for method chaining
:rtype: Self
"""
scorer: ScorerTestBasedScoringFunction = {
"type": "test_based_scorer",
"test_command": test_command,
}
if test_files:
scorer["test_files"] = test_files
return self._add_scorer(name, weight, scorer)
def add_shell_command_scorer(
self,
name: str,
*,
command: str,
weight: float = 1.0,
) -> Self:
"""Add a command scorer that runs a shell command.
:param name: Name of the scoring function
:type name: str
:param command: Shell command to execute
:type command: str
:param weight: Weight for this scorer (normalized automatically)
:type weight: float
:return: Self for method chaining
:rtype: Self
"""
scorer: ScorerCommandScoringFunction = {
"type": "command_scorer",
"command": command,
}
return self._add_scorer(name, weight, scorer)
def add_bash_script_scorer(
self,
name: str,
*,
bash_script: str,
weight: float = 1.0,
) -> Self:
"""Add a standalone bash script scorer.
The script should output "score=X.X" where X.X is a float between 0.0 and 1.0, inclusive.
:param name: Name of the scoring function
:type name: str
:param bash_script: Bash script content
:type bash_script: str
:param weight: Weight for this scorer (normalized automatically)
:type weight: float
:return: Self for method chaining
:rtype: Self
"""
scorer: ScorerBashScriptScoringFunction = {
"type": "bash_script_scorer",
"bash_script": bash_script,
}
return self._add_scorer(name, weight, scorer)
def add_python_script_scorer(
self,
name: str,
*,
python_script: str,
weight: float = 1.0,
python_version_constraint: Optional[str] = None,
requirements_contents: Optional[str] = None,
) -> Self:
"""Add a standalone Python script scorer.
The script is run in an isolated uv environment, and the dependencies are declared in the
`uv script header <https://docs.astral.sh/uv/guides/scripts/#declaring-script-dependencies>`__.
The script should print the score in the range [0.0, 1.0] to stdout.
:param name: Name of the scoring function
:type name: str
:param python_script: Python script content
:type python_script: str
:param weight: Weight for this scorer (normalized automatically)
:type weight: float
:param python_version_constraint: Python version (default "==3.12.10")
:type python_version_constraint: Optional[str]
:param requirements_contents: pip requirements.txt content
:type requirements_contents: Optional[str]
:return: Self for method chaining
:rtype: Self
"""
scorer: ScorerPythonScriptScoringFunction = {
"type": "python_script_scorer",
"python_script": python_script,
}
if python_version_constraint:
scorer["python_version_constraint"] = python_version_constraint
if requirements_contents:
scorer["requirements_contents"] = requirements_contents
return self._add_scorer(name, weight, scorer)
def add_ast_grep_scorer(
self,
name: str,
*,
pattern: str,
weight: float = 1.0,
search_directory: str = ".",
lang: Optional[str] = None,
) -> Self:
"""Add an AST grep scorer that matches code patterns.
:param name: Name of the scoring function
:type name: str
:param pattern: AST pattern to match
:type pattern: str
:param weight: Weight for this scorer (normalized automatically)
:type weight: float
:param search_directory: Directory to search (default ".")
:type search_directory: str
:param lang: Language of the pattern (optional)
:type lang: Optional[str]
:return: Self for method chaining
:rtype: Self
"""
scorer: ScorerAstGrepScoringFunction = {
"type": "ast_grep_scorer",
"pattern": pattern,
"search_directory": search_directory,
}
if lang:
scorer["lang"] = lang
return self._add_scorer(name, weight, scorer)
def with_metadata(self, metadata: Dict[str, str]) -> Self:
"""Set metadata for the scenario.
:param metadata: Key-value metadata
:type metadata: Dict[str, str]
:return: Self for method chaining
:rtype: Self
"""
self._metadata = metadata
return self
def with_reference_output(self, output: str) -> Self:
"""Set the reference solution or gold patch for validation.
After application, the scorer is expected to return a score of 1.0.
:param output: Reference solution or gold patch (e.g., git diff)
:type output: str
:return: Self for method chaining
:rtype: Self
"""
self._reference_output = output
return self
def with_required_env_vars(self, env_vars: List[str]) -> Self:
"""Set required environment variables.
:param env_vars: List of required environment variable names
:type env_vars: List[str]
:return: Self for method chaining
:rtype: Self
"""
self._required_env_vars = env_vars
return self
def with_required_secrets(self, secrets: List[str]) -> Self:
"""Set required secrets.
:param secrets: List of required secret names
:type secrets: List[str]
:return: Self for method chaining
:rtype: Self
"""
self._required_secrets = secrets
return self
def with_validation_type(self, validation_type: Literal["UNSPECIFIED", "FORWARD", "REVERSE", "EVALUATION"]) -> Self:
"""Set the validation strategy to specify how the reference solution or gold patch is applied to the scenario.
:param validation_type: Validation type
:type validation_type: Literal["UNSPECIFIED", "FORWARD", "REVERSE", "EVALUATION"]
:return: Self for method chaining
:rtype: Self
"""
self._validation_type = validation_type
return self
def _build_normalized_scorers(self) -> List[ScoringFunctionParam]:
"""Build normalized scorers list."""
total_weight = sum(s["weight"] for s in self._scorers)
return [{**s, "weight": s["weight"] / total_weight} for s in self._scorers]
def _build_environment_params(self) -> Optional[ScenarioEnvironmentParam]:
"""Build environment parameters."""
if not self._blueprint and not self._snapshot and not self._working_directory:
return None
return {
"blueprint_id": self._blueprint.id if self._blueprint else None,
"snapshot_id": self._snapshot.id if self._snapshot else None,
"working_directory": self._working_directory if self._working_directory else None,
}
def build(self) -> ScenarioCreateParams:
"""Build the scenario creation parameters.
Weights are automatically normalized to sum to 1.0.
:raises ValueError: If required fields are missing
:return: Parameters for scenario creation
:rtype: ScenarioCreateParams
"""
if not self._problem_statement:
raise ValueError("Problem statement is required. Call with_problem_statement() first.")
if not self._scorers:
raise ValueError(
"At least one scorer is required. "
"Call add_test_command_scorer(), add_bash_script_scorer(), or another scorer method first."
)
return {
"name": self._name,
"input_context": {
"problem_statement": self._problem_statement,
"additional_context": self._additional_context,
},
"scoring_contract": {
"scoring_function_parameters": self._build_normalized_scorers(),
},
"environment_parameters": self._build_environment_params(),
"metadata": self._metadata,
"reference_output": self._reference_output,
"required_environment_variables": self._required_env_vars,
"required_secret_names": self._required_secrets,
"validation_type": self._validation_type,
}
def preview(self) -> ScenarioPreview:
"""Preview the scenario configuration without pushing to the platform.
Returns the current configuration state as a ScenarioPreview object.
Does not validate or raise errors for missing required fields.
:return: Preview of the scenario configuration
:rtype: ScenarioPreview
"""
return ScenarioPreview.model_validate(
{
"name": self._name,
"input_context": {
"problem_statement": self._problem_statement,
"additional_context": self._additional_context,
},
"scoring_contract": {
"scoring_function_parameters": self._build_normalized_scorers(),
},
"environment": self._build_environment_params(),
"metadata": self._metadata,
"reference_output": self._reference_output,
"required_environment_variables": self._required_env_vars,
"required_secret_names": self._required_secrets,
"validation_type": self._validation_type,
}
)
async def push(self, **options: Unpack[LongRequestOptions]) -> AsyncScenario:
"""Create the scenario on the platform.
:param options: Optional long-running request configuration
:raises ValueError: If required fields are missing
:return: Created scenario wrapper
:rtype: AsyncScenario
"""
params = self.build()
scenario_view = await self._client.scenarios.create(**params, **options)
return AsyncScenario(self._client, scenario_view.id)