File size: 20,637 Bytes
f83d2aa | 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 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 | """
server/task_generator.py
Generates coding tasks with:
1. Name randomization per episode seed — prevents session 2 reconstructing from
pretrained knowledge without reading the handoff note.
2. Injected hidden adversarial tests — prevents visible-test overfitting.
3. Handoff-critical calibration — tasks are designed so session 1 cannot
fully finish within the step limit.
"""
import copy
import json
import os
import random
from dataclasses import dataclass, field
from typing import Dict, List, Optional
# ---------------------------------------------------------------------------
# Canonical → variant name bank (expanded per task template)
# ---------------------------------------------------------------------------
NAME_BANK: Dict[str, List[str]] = {
# Data structures
"merge_intervals": ["combine_ranges", "fuse_spans", "join_segments"],
"Stack": ["Accumulator", "PushPop", "LifoStore"],
"push": ["enqueue_item", "add_entry", "store_val"],
"pop": ["dequeue_item", "remove_entry", "fetch_val"],
# Rate limiting
"RateLimiter": ["ThrottleGuard", "RequestBucket", "AccessGate"],
"is_allowed": ["check_permit", "can_proceed", "gate_request"],
# LRU Cache
"LRUCache": ["BoundedCache", "EvictStore", "MruVault"],
"get": ["fetch", "retrieve", "lookup"],
"put": ["store", "insert", "upsert"],
# Data processing
"process_data": ["transform_records", "handle_payload", "digest_input"],
"normalize": ["standardize", "rescale", "calibrate"],
# Graph
"TopologicalSort": ["DependencyOrder", "DAGResolver", "LayerSorter"],
"add_edge": ["link_nodes", "connect_dep", "wire_pair"],
# Retry
"RetryExecutor": ["FaultTolerant", "BackoffRunner", "ResilienceWrap"],
"execute": ["run_with_retry", "attempt_call", "safe_invoke"],
}
# ---------------------------------------------------------------------------
# Data classes
# ---------------------------------------------------------------------------
@dataclass
class Task:
task_id: str
difficulty: str
description: str
starter_code: Dict[str, str] # filename → source
test_code: str # visible pytest suite
hidden_test_code: str # adversarial hidden suite
files: Dict[str, str] = field(default_factory=dict) # runtime state
# ---------------------------------------------------------------------------
# Task templates (inline — no external files required for dev/demo)
# ---------------------------------------------------------------------------
TASK_TEMPLATES = {
# -----------------------------------------------------------------------
# EASY tasks
# -----------------------------------------------------------------------
"easy_merge_intervals": {
"difficulty": "easy",
"description": (
"Implement merge_intervals(intervals: list[list[int]]) -> list[list[int]] "
"in solution.py. The function receives a list of [start, end] intervals "
"and must return a merged list with no overlaps. "
"Session 1 should implement the sort + sweep logic and pass visible tests. "
"Session 2 must handle edge cases (empty input, single interval, touching "
"but non-overlapping intervals)."
),
"starter_code": {
"solution.py": (
"def merge_intervals(intervals):\n"
" # TODO: implement\n"
" pass\n"
)
},
"test_code": (
"from solution import merge_intervals\n\n"
"def test_basic():\n"
" assert merge_intervals([[1,3],[2,6],[8,10],[15,18]]) == [[1,6],[8,10],[15,18]]\n\n"
"def test_overlapping():\n"
" assert merge_intervals([[1,4],[4,5]]) == [[1,5]]\n\n"
"def test_no_overlap():\n"
" assert merge_intervals([[1,2],[3,4]]) == [[1,2],[3,4]]\n"
),
"hidden_test_code": (
"from solution import merge_intervals\n\n"
"def test_empty():\n"
" assert merge_intervals([]) == []\n\n"
"def test_single():\n"
" assert merge_intervals([[5,5]]) == [[5,5]]\n"
),
},
"easy_stack": {
"difficulty": "easy",
"description": (
"Implement a Stack class in solution.py with push(val), pop() -> val, "
"peek() -> val, is_empty() -> bool, and size() -> int. "
"pop() and peek() on empty stack should raise IndexError. "
"Session 1: implement and pass visible tests. "
"Session 2: add __repr__ and make the class iterable."
),
"starter_code": {
"solution.py": (
"class Stack:\n"
" def __init__(self):\n"
" # TODO\n"
" pass\n"
)
},
"test_code": (
"from solution import Stack\n\n"
"def test_push_pop():\n"
" s = Stack()\n"
" s.push(1); s.push(2)\n"
" assert s.pop() == 2\n\n"
"def test_empty_pop():\n"
" import pytest\n"
" s = Stack()\n"
" with pytest.raises(IndexError):\n"
" s.pop()\n\n"
"def test_size():\n"
" s = Stack()\n"
" s.push(10); s.push(20)\n"
" assert s.size() == 2\n"
),
"hidden_test_code": (
"from solution import Stack\n\n"
"def test_repr():\n"
" s = Stack()\n"
" s.push(1)\n"
" assert '1' in repr(s)\n\n"
"def test_iterable():\n"
" s = Stack()\n"
" for v in [3,2,1]:\n"
" s.push(v)\n"
" assert list(s) == [1, 2, 3]\n"
),
},
"easy_running_median": {
"difficulty": "easy",
"description": (
"Implement RunningMedian in solution.py. It must support add(num) and "
"get_median() -> float. Uses two heaps internally. "
"Session 1: implement heap-based median, pass visible tests. "
"Session 2: add reset() and from_list(nums) classmethod."
),
"starter_code": {
"solution.py": (
"class RunningMedian:\n"
" def __init__(self):\n"
" # TODO: two-heap approach\n"
" pass\n"
)
},
"test_code": (
"from solution import RunningMedian\n\n"
"def test_basic():\n"
" rm = RunningMedian()\n"
" rm.add(1); rm.add(2); rm.add(3)\n"
" assert rm.get_median() == 2.0\n\n"
"def test_even():\n"
" rm = RunningMedian()\n"
" rm.add(1); rm.add(2)\n"
" assert rm.get_median() == 1.5\n"
),
"hidden_test_code": (
"from solution import RunningMedian\n\n"
"def test_reset():\n"
" rm = RunningMedian()\n"
" rm.add(5)\n"
" rm.reset()\n"
" rm.add(1)\n"
" assert rm.get_median() == 1.0\n\n"
"def test_from_list():\n"
" rm = RunningMedian.from_list([3, 1, 2])\n"
" assert rm.get_median() == 2.0\n"
),
},
# -----------------------------------------------------------------------
# MEDIUM tasks
# -----------------------------------------------------------------------
"medium_rate_limiter": {
"difficulty": "medium",
"description": (
"Implement a token-bucket RateLimiter in solution.py. "
"Constructor: RateLimiter(rate: int, capacity: int). "
"is_allowed(n_tokens=1) -> bool: returns True if n_tokens can be consumed, "
"refilling at 'rate' tokens per second. "
"Use time.monotonic() for timestamps. "
"Session 1: core token-bucket logic + visible tests. "
"Session 2: add burst_remaining() -> int and thread-safety via threading.Lock."
),
"starter_code": {
"solution.py": (
"import time\n\n"
"class RateLimiter:\n"
" def __init__(self, rate: int, capacity: int):\n"
" # TODO\n"
" pass\n"
)
},
"test_code": (
"import time\n"
"from solution import RateLimiter\n\n"
"def test_basic_allow():\n"
" rl = RateLimiter(10, 10)\n"
" assert rl.is_allowed() is True\n\n"
"def test_exhaustion():\n"
" rl = RateLimiter(1, 3)\n"
" assert rl.is_allowed(3) is True\n"
" assert rl.is_allowed() is False\n\n"
"def test_refill():\n"
" rl = RateLimiter(10, 10)\n"
" rl.is_allowed(10)\n"
" time.sleep(0.2)\n"
" assert rl.is_allowed(2) is True\n\n"
"def test_over_capacity():\n"
" rl = RateLimiter(5, 5)\n"
" assert rl.is_allowed(6) is False\n\n"
"def test_zero_tokens():\n"
" rl = RateLimiter(5, 5)\n"
" assert rl.is_allowed(0) is True\n"
),
"hidden_test_code": (
"import threading, time\n"
"from solution import RateLimiter\n\n"
"def test_burst_remaining():\n"
" rl = RateLimiter(10, 10)\n"
" rl.is_allowed(4)\n"
" assert rl.burst_remaining() == 6\n\n"
"def test_thread_safe():\n"
" rl = RateLimiter(100, 100)\n"
" results = []\n"
" def task():\n"
" results.append(rl.is_allowed(10))\n"
" threads = [threading.Thread(target=task) for _ in range(10)]\n"
" for t in threads: t.start()\n"
" for t in threads: t.join()\n"
" assert results.count(True) == 10\n"
),
},
"medium_lru_cache": {
"difficulty": "medium",
"description": (
"Implement LRUCache(capacity: int) in solution.py. "
"get(key) -> int: return value or -1 if not present. "
"put(key, value): insert, evicting LRU entry if at capacity. "
"Both O(1) using dict + doubly-linked list. "
"Session 1: core get/put + visible tests. "
"Session 2: add keys() -> list (in MRU→LRU order) and "
"clear() method."
),
"starter_code": {
"solution.py": (
"class LRUCache:\n"
" def __init__(self, capacity: int):\n"
" # TODO: doubly-linked list + dict\n"
" pass\n"
)
},
"test_code": (
"from solution import LRUCache\n\n"
"def test_basic():\n"
" c = LRUCache(2)\n"
" c.put(1, 1); c.put(2, 2)\n"
" assert c.get(1) == 1\n"
" c.put(3, 3)\n"
" assert c.get(2) == -1\n\n"
"def test_overwrite():\n"
" c = LRUCache(2)\n"
" c.put(1, 10); c.put(1, 20)\n"
" assert c.get(1) == 20\n\n"
"def test_capacity_one():\n"
" c = LRUCache(1)\n"
" c.put(1, 1); c.put(2, 2)\n"
" assert c.get(1) == -1\n"
" assert c.get(2) == 2\n\n"
"def test_miss():\n"
" c = LRUCache(3)\n"
" assert c.get(99) == -1\n\n"
"def test_no_eviction_under_cap():\n"
" c = LRUCache(5)\n"
" for i in range(5):\n"
" c.put(i, i*10)\n"
" for i in range(5):\n"
" assert c.get(i) == i*10\n"
),
"hidden_test_code": (
"from solution import LRUCache\n\n"
"def test_keys_order():\n"
" c = LRUCache(3)\n"
" c.put(1,1); c.put(2,2); c.put(3,3)\n"
" c.get(1)\n"
" assert c.keys()[0] == 1\n\n"
"def test_clear():\n"
" c = LRUCache(3)\n"
" c.put(1,1); c.put(2,2)\n"
" c.clear()\n"
" assert c.get(1) == -1\n"
),
},
# -----------------------------------------------------------------------
# HARD tasks
# -----------------------------------------------------------------------
"hard_topological_sort": {
"difficulty": "hard",
"description": (
"Implement a TopologicalSort class in solution.py for a DAG. "
"add_edge(u, v): add directed edge u→v. "
"sort() -> list: return topological order (raise CycleError if cycle). "
"has_path(src, dst) -> bool: BFS/DFS reachability. "
"Also implement parallel_layers() -> list[list]: return nodes grouped "
"by execution layer (Kahn's algorithm variant). "
"Session 1: add_edge + sort + CycleError + visible tests. "
"Session 2: has_path + parallel_layers + hidden tests."
),
"starter_code": {
"solution.py": (
"from collections import defaultdict, deque\n\n"
"class CycleError(Exception):\n"
" pass\n\n"
"class TopologicalSort:\n"
" def __init__(self):\n"
" self.graph = defaultdict(list)\n"
" self.nodes = set()\n\n"
" def add_edge(self, u, v):\n"
" # TODO\n"
" pass\n\n"
" def sort(self):\n"
" # TODO: Kahn's algorithm\n"
" pass\n"
)
},
"test_code": (
"import pytest\n"
"from solution import TopologicalSort, CycleError\n\n"
"def test_linear():\n"
" ts = TopologicalSort()\n"
" ts.add_edge('a','b'); ts.add_edge('b','c')\n"
" order = ts.sort()\n"
" assert order.index('a') < order.index('b') < order.index('c')\n\n"
"def test_cycle():\n"
" ts = TopologicalSort()\n"
" ts.add_edge('a','b'); ts.add_edge('b','a')\n"
" with pytest.raises(CycleError):\n"
" ts.sort()\n\n"
"def test_diamond():\n"
" ts = TopologicalSort()\n"
" ts.add_edge('a','b'); ts.add_edge('a','c')\n"
" ts.add_edge('b','d'); ts.add_edge('c','d')\n"
" order = ts.sort()\n"
" assert order[0] == 'a' and order[-1] == 'd'\n\n"
"def test_isolated_node():\n"
" ts = TopologicalSort()\n"
" ts.add_edge('a','b')\n"
" order = ts.sort()\n"
" assert set(order) == {'a','b'}\n\n"
"def test_empty():\n"
" ts = TopologicalSort()\n"
" assert ts.sort() == []\n\n"
"def test_single_node():\n"
" ts = TopologicalSort()\n"
" ts.add_edge('x','x')\n"
" with pytest.raises(CycleError):\n"
" ts.sort()\n\n"
"def test_large_dag():\n"
" ts = TopologicalSort()\n"
" for i in range(9):\n"
" ts.add_edge(str(i), str(i+1))\n"
" order = ts.sort()\n"
" assert order == [str(i) for i in range(10)]\n\n"
"def test_multi_root():\n"
" ts = TopologicalSort()\n"
" ts.add_edge('a','c'); ts.add_edge('b','c')\n"
" order = ts.sort()\n"
" assert order.index('a') < order.index('c')\n"
" assert order.index('b') < order.index('c')\n"
),
"hidden_test_code": (
"from solution import TopologicalSort\n\n"
"def test_has_path_true():\n"
" ts = TopologicalSort()\n"
" ts.add_edge('a','b'); ts.add_edge('b','c')\n"
" assert ts.has_path('a','c') is True\n\n"
"def test_has_path_false():\n"
" ts = TopologicalSort()\n"
" ts.add_edge('a','b')\n"
" assert ts.has_path('b','a') is False\n\n"
"def test_parallel_layers():\n"
" ts = TopologicalSort()\n"
" ts.add_edge('a','c'); ts.add_edge('b','c')\n"
" layers = ts.parallel_layers()\n"
" assert set(layers[0]) == {'a','b'}\n"
" assert layers[-1] == ['c']\n"
),
},
}
# Holdout tasks (simplified for eval only)
HOLDOUT_TEMPLATES = {
"holdout_two_sum": TASK_TEMPLATES["easy_merge_intervals"], # placeholder
"holdout_word_count": TASK_TEMPLATES["easy_stack"],
"holdout_retry_exec": TASK_TEMPLATES["medium_rate_limiter"],
}
# ---------------------------------------------------------------------------
# TaskGenerator
# ---------------------------------------------------------------------------
class TaskGenerator:
"""
Samples tasks from the template bank, applies name randomization per seed,
and injects hidden adversarial tests.
"""
def __init__(self, difficulty: str = "medium"):
assert difficulty in {"easy", "medium", "hard"}, f"Invalid difficulty: {difficulty}"
self.difficulty = difficulty
self._bank = {
k: v for k, v in TASK_TEMPLATES.items()
if v["difficulty"] == difficulty
}
self._holdout = HOLDOUT_TEMPLATES
def sample(
self,
task_id: Optional[str] = None,
seed: Optional[int] = None,
) -> Task:
if seed is not None:
random.seed(seed)
if task_id and task_id in TASK_TEMPLATES:
template = copy.deepcopy(TASK_TEMPLATES[task_id])
chosen_id = task_id
else:
chosen_id = random.choice(list(self._bank.keys()))
template = copy.deepcopy(self._bank[chosen_id])
task = self._build_task(chosen_id, template)
task = self._randomize_names(task)
return task
def sample_holdout(self, task_id: Optional[str] = None) -> Task:
"""Sample from holdout set (never used in training)."""
if task_id and task_id in self._holdout:
template = copy.deepcopy(self._holdout[task_id])
chosen_id = task_id
else:
chosen_id = random.choice(list(self._holdout.keys()))
template = copy.deepcopy(self._holdout[chosen_id])
return self._build_task(chosen_id, template)
# ------------------------------------------------------------------
# Private helpers
# ------------------------------------------------------------------
@staticmethod
def _build_task(task_id: str, template: dict) -> Task:
return Task(
task_id=task_id,
difficulty=template["difficulty"],
description=template["description"],
starter_code=dict(template["starter_code"]),
test_code=template["test_code"],
hidden_test_code=template["hidden_test_code"],
files=dict(template["starter_code"]), # runtime mutable copy
)
@staticmethod
def _randomize_names(task: Task) -> Task:
"""
Randomly remap canonical names to episode-specific variants.
This prevents Session 2 from reconstructing the solution purely from
pretrained knowledge without reading the handoff note.
"""
mapping = {}
for canonical, variants in NAME_BANK.items():
mapping[canonical] = random.choice(variants)
def apply(text: str) -> str:
for canon, variant in mapping.items():
text = text.replace(canon, variant)
return text
task.description = apply(task.description)
task.files = {k: apply(v) for k, v in task.files.items()}
task.starter_code = {k: apply(v) for k, v in task.starter_code.items()}
task.test_code = apply(task.test_code)
# Hidden tests use canonical names — not randomized (consistent eval)
return task
|