feat: orchestrator owns v1 tasksets, ships task data to env servers (#3043)

* feat: orchestrator owns v1 tasksets, ships task data to env servers

Companion to PrimeIntellect-ai/verifiers#2039. A v1 env's taskset is loaded once,
in the orchestrator: finite tasksets become the train source's shuffled example
table (real tasks, not index ranges); an infinite taskset's generator is pulled
per example — no server-side materialization, no idx-addressed cache, no
requirement that every pool worker regenerate the same sequence. Each dispatched
rollout ships its task's data (task.data.full_dump()) and the env server
pydantic-validates and runs it.

The legacy (v0) bridge keeps its server-side dataset and task_idx addressing.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* chore: bump deps/verifiers to client-side-tasksets tip

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* chore: bump deps/verifiers to client-side-tasksets tip

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* chore: bump deps/verifiers to client-side-tasksets tip

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* chore: plain model_dump task payload; bump deps/verifiers (full_dump removed)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* feat: re-land client-side tasksets on the episode wire

Companion to the rebased PrimeIntellect-ai/verifiers#2039. A v1 env's taskset is
loaded once, in the orchestrator: finite tasksets become the train source's
shuffled example table (real tasks, not index ranges); an infinite taskset's
generator is pulled per example. Each dispatched env-rollout ships its task's
data (task.data.model_dump()) and the env server validates and runs it. The
legacy (v0) bridge keeps its server-side dataset, task_idx addressing, and the
run_group route.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* chore: bump deps/verifiers (non-optional taskset); drop redundant assert

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* chore: actually drop the redundant taskset assert

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* chore: bump deps/verifiers (content-identity resume)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* chore: bump deps/verifiers (simplified task_key)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* chore: bump deps/verifiers

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* chore: bump deps/verifiers

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* chore: bump deps/verifiers to main (client-side tasksets merged, vf#2039)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* refactor: one Env.tasks iterator instead of tasks/task_iter

Finite tasksets still materialize at start() (num_tasks needs the count and the
train source shuffles epochs), but the field is a single iterator either way —
consumers pull it uniformly and branch on num_tasks alone. Consumed once, by
TrainSource or EvalEnv.start.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Mika Senghaas
2026-07-22 13:45:15 -07:00
committed by GitHub
co-authored by Claude Fable 5
parent cd00986f71
commit f5946b098e
5 changed files with 102 additions and 51 deletions
+1 -1
+8 -1
View File
@@ -387,6 +387,7 @@ class RolloutDispatcher:
kind=kind,
env_name=env_name,
task_idx=example["task_idx"],
task=example.get("task"),
rollouts_to_schedule=group_size,
target_rollouts=group_size,
eval_step=eval_step,
@@ -438,6 +439,7 @@ class RolloutDispatcher:
cache_salt = None
if env.requires_group_scoring:
# Legacy-only route (a v1 env never group-scores) — addressed by row.
permits = group.rollouts_to_schedule
group.rollouts_to_schedule = 0
await self.acquire(permits)
@@ -451,15 +453,20 @@ class RolloutDispatcher:
)
)
else:
# A v1 env takes the task itself; the legacy bridge its dataset row.
if group.task is not None:
addressing = {"task_data": group.task.data.model_dump(mode="json")}
else:
addressing = {"task_idx": group.task_idx}
permits = 1
group.rollouts_to_schedule -= 1
await self.acquire(permits)
task = asyncio.create_task(
env.run(
client=client,
task_idx=group.task_idx,
model_name=model_name,
cache_salt=cache_salt,
**addressing,
)
)
+66 -34
View File
@@ -2,15 +2,19 @@
Each ``Env`` owns a v1 ``EnvServer`` (spawned as a child process, or an
external one given by ``config.address``) and an ``EnvClient`` to drive it. The
orchestrator never *runs* an environment: it asks the server for ``info``
(``num_tasks`` + whether group scoring is needed), then runs rollouts purely by
**task index**. The server answers one ``Episode`` per env-rollout, whose traces
we validate into ``Trace[WireTaskData]`` — real ``vf.Trace``\\ s (never loose
dicts) whose task keeps the env's task-specific
fields as extras (``WireTaskData`` allows them). The orchestrator never imports the
env package: the env's *type* and *runtime* both live only in the server, and the orchestrator
drives it purely by task index. (Nothing here reads typed env task fields — only ``task.idx``
and a full ``task.model_dump``, both of which ``WireTaskData`` preserves.)
orchestrator never *runs* an environment — the agents and their runtimes live only
in the server — but it does own the *taskset*: a v1 env's tasks are loaded here,
once, and each dispatched env-rollout ships its task's data on the request
(``task_data``); the server pydantic-validates it into the taskset's declared
``TaskData`` type and runs it. That keeps the server (and every worker in its
pool) stateless about data — no per-worker dataset loads, no idx-addressed task
cache — and gives the orchestrator real tasks to cycle, shuffle, and filter. Only
the legacy (v0) bridge, whose dataset genuinely lives server-side, is still driven
by ``task_idx`` (its count comes from ``info``).
The server answers one ``Episode`` per env-rollout, whose traces we validate into
``Trace[WireTaskData]`` — real ``vf.Trace``\\ s (never loose dicts) whose task
keeps the env's task-specific fields as extras (``WireTaskData`` allows them).
"""
from __future__ import annotations
@@ -22,6 +26,7 @@ import os
import queue
import sys
from collections.abc import Iterator, Sequence
from itertools import islice
from multiprocessing.process import BaseProcess
from pathlib import Path
from typing import Generic, TypeVar
@@ -40,9 +45,8 @@ from prime_rl.utils.logger import get_logger
# task.idx + task.model_dump).
ROLLOUT_TYPE = Rollout[vf.WireTaskData]
# Max wait for a spawned env server to bind and report its address. The child
# loads the taskset (possibly downloading a dataset) before reporting, so this
# is generous.
# Max wait for a spawned env server to bind and report its address. A legacy
# child loads its dataset before reporting, so this is generous.
ENV_SERVER_SPAWN_TIMEOUT = 600.0
@@ -78,14 +82,20 @@ def _run_env_server(
class Env:
"""Wraps a v1 env server + client. The orchestrator never loads the env."""
"""Wraps a v1 env server + client. The orchestrator owns the taskset (loaded once,
client-side); the server owns agent/harness execution."""
def __init__(self, config: EnvConfig):
self.config = config
self.sampling_args: dict = {}
self.num_tasks: int | None = 0
"""Task count reported by the server; ``None`` means the taskset is infinite."""
"""Task count; ``None`` means the taskset is infinite."""
self.requires_group_scoring: bool = False
self.tasks: Iterator[vf.Task] | None = None
"""The env's tasks, client-side; ``None`` for legacy (its dataset lives on the
server). A finite taskset is materialized at ``start()`` (``num_tasks`` is its
count) and iterated from there; an infinite one streams off its generator.
Consumed once — by ``TrainSource`` (train) or ``EvalEnv.start`` (eval)."""
self._env_client: EnvClient | None = None
self._env_server_process: BaseProcess | None = None
@@ -100,25 +110,36 @@ class Env:
return self._env_client
async def start(self, log_dir: Path, log_level: str | None = None, json_logging: bool = False) -> None:
"""Spawn the env server (if needed), connect, and cache its ``info``."""
"""Spawn the env server (if needed), connect, and load the taskset client-side
(legacy instead asks the server for ``info`` — its dataset is server-side)."""
external = self.config.address is not None
address = self.config.address or await self._spawn(log_dir, log_level or "INFO", json_logging)
get_logger().debug(f"Connecting {self.name} to env server {address}")
self._env_client = EnvClient(address=address)
# A spawned server already reported its address *after* binding + loading,
# so it's up — the untimed ``info`` below is enough. An external server has
# no such handshake, so poll until it answers before we block on ``info``.
# A spawned server already reported its address *after* binding, so it's up. An
# external server has no such handshake, so poll until it answers.
if external:
await self.env_client.wait_for_server_startup()
info = await self.env_client.info()
self.num_tasks = info.num_tasks
self.requires_group_scoring = info.requires_group_scoring
if self.config.is_legacy:
info = await self.env_client.info()
self.num_tasks = info.num_tasks
self.requires_group_scoring = info.requires_group_scoring
else:
taskset = vf.load_taskset(self.config.env.taskset)
if type(taskset).INFINITE:
self.tasks = iter(taskset.load())
self.num_tasks = None
else:
# Materialize off the event loop — load() may pull a dataset.
materialized = await asyncio.to_thread(lambda: list(taskset.load()))
self.tasks = iter(materialized)
self.num_tasks = len(materialized)
num_tasks = self.num_tasks if self.num_tasks is not None else "infinite"
get_logger().info(f"Env {self.name} ready: num_tasks={num_tasks} group_scoring={self.requires_group_scoring}")
async def _spawn(self, log_dir: Path, log_level: str, json_logging: bool) -> str:
"""Spawn a v1 EnvServer child process (it loads the env; we never do).
The server binds an OS-assigned port (``:0``) and reports the concrete
"""Spawn a v1 EnvServer child process (it runs the agents; the tasks come from
us). The server binds an OS-assigned port (``:0``) and reports the concrete
address back over a queue — no free-port guess, no TOCTOU race. Its output
goes to ``<log_dir>/<name>.log`` (``log_dir`` is already the train/eval-split
``.../logs/envs/{train,eval}`` the orchestrator passes in)."""
@@ -170,12 +191,20 @@ class Env:
return vf.SamplingConfig(**sampling)
async def run(
self, client: vf.ClientConfig, task_idx: int, model_name: str, cache_salt: str | None
self,
client: vf.ClientConfig,
model_name: str,
cache_salt: str | None,
task_data: dict | None = None,
task_idx: int | None = None,
) -> list[Rollout]:
"""Run one episode for ``task_idx``; return its typed Traces. A zero-trace
episode raises (the dispatcher synthesizes the error marker); a not-``ok``
episode marks its clean traces failed so partial episodes never train."""
"""Run one episode; return its typed Traces. A v1 env takes the task itself
(``task_data``); the legacy bridge is addressed by dataset row (``task_idx``).
A zero-trace episode raises (the dispatcher synthesizes the error marker); a
not-``ok`` episode marks its clean traces failed so partial episodes never
train."""
episode = await self.env_client.run(
task_data=task_data,
task_idx=task_idx,
client=client,
model=model_name,
@@ -236,13 +265,16 @@ class EvalEnv(Env):
async def start(self, log_dir: Path, log_level: str | None = None, json_logging: bool = False) -> None:
await super().start(log_dir=log_dir, log_level=log_level, json_logging=json_logging)
if self.num_tasks is None:
if self.config.num_examples < 0:
raise ValueError(f"Eval env {self.name} has an infinite taskset — set num_examples to bound it")
n = self.config.num_examples
else:
n = self.num_tasks if self.config.num_examples < 0 else min(self.config.num_examples, self.num_tasks)
self.examples = [{"task_idx": i} for i in range(n)]
n = self.config.num_examples
if self.tasks is None: # legacy: the dataset lives on the server — address it by row
count = self.num_tasks if n < 0 else min(n, self.num_tasks)
self.examples = [{"task_idx": i} for i in range(count)]
return
if self.num_tasks is None and n < 0:
raise ValueError(f"Eval env {self.name} has an infinite taskset — set num_examples to bound it")
# A fixed eval set, pulled off the tasks once and reused every epoch.
tasks = list(self.tasks) if n < 0 else list(islice(self.tasks, n))
self.examples = [{"task_idx": task.data.idx, "task": task} for task in tasks]
EnvT = TypeVar("EnvT", bound=Env)
+24 -15
View File
@@ -1,14 +1,18 @@
"""TrainSource: weighted round-robin across train envs, infinite pull.
Weights are each env's configured ``ratio`` (default 1, i.e. equal weight
per env). A finite env serves a shuffled task-index table, reshuffled on
cursor exhaustion; an infinite env (``num_tasks is None``) streams a
monotonic ``task_idx`` — the server generates tasks on demand, so every
pull is a fresh task and there are no epochs to shuffle."""
per env). A v1 env serves the tasks the orchestrator loaded client-side: a
finite one as a shuffled table (reshuffled on cursor exhaustion), an
infinite one (``num_tasks is None``) straight off its generator — every
pull is a fresh task and there are no epochs to shuffle. A legacy env's
dataset lives on its server, so it serves shuffled task *indices*."""
from __future__ import annotations
import random
from collections.abc import Iterator
import verifiers.v1 as vf
from prime_rl.orchestrator.envs import TrainEnvs
@@ -17,7 +21,8 @@ class TrainSource:
"""``next_example(available_permits)`` picks a weighted-RR env and
returns its next example (or ``None`` when the env's per-call permit
cost doesn't fit — the dispatch loop retries when permits free up).
Returned dicts carry ``env_name`` + ``task_idx``."""
Returned dicts carry ``env_name`` + ``task_idx`` (+ ``task`` for v1 envs,
whose data is shipped to the env server at dispatch)."""
def __init__(self, train_envs: TrainEnvs, *, seed: int | None) -> None:
self.rng = random.Random(seed)
@@ -25,22 +30,26 @@ class TrainSource:
if not self.envs:
raise ValueError("TrainSource needs at least one train env")
# A finite env's shuffled index table; ``None`` for an infinite env,
# whose cursor alone is the (monotonic) task_idx stream.
# A finite env's shuffled example table; ``None`` for an infinite env,
# whose generator (``self.iters``) is pulled per example.
self.examples: dict[str, list[dict] | None] = {}
self.iters: dict[str, Iterator[vf.Task]] = {}
self.cursors: dict[str, int] = {}
# Group-scoring envs reserve ``group_size`` permits up front;
# per-rollout envs need 1
self.env_costs: dict[str, int] = {}
for env in self.envs:
# The orchestrator never loads the env: sample over the task-index
# range the server reported via info() (num_tasks; None = infinite).
if env.num_tasks is None:
self.examples[env.name] = None
else:
if env.tasks is None: # legacy: sample over the index range from info()
rows: list[dict] = [{"task_idx": i, "env_name": env.name} for i in range(env.num_tasks)]
self.rng.shuffle(rows)
self.examples[env.name] = rows
elif env.num_tasks is None: # infinite: pull the generator per example
self.examples[env.name] = None
self.iters[env.name] = env.tasks
else:
rows = [{"task_idx": task.data.idx, "task": task, "env_name": env.name} for task in env.tasks]
self.rng.shuffle(rows)
self.examples[env.name] = rows
self.cursors[env.name] = 0
self.env_costs[env.name] = env.config.group_size if env.requires_group_scoring else 1
@@ -53,9 +62,9 @@ class TrainSource:
return None
rows = self.examples[env_name]
cursor = self.cursors[env_name]
if rows is None: # infinite env: the cursor is the task_idx
self.cursors[env_name] = cursor + 1
return {"task_idx": cursor, "env_name": env_name}
if rows is None: # infinite env: pull the next generated task
task = next(self.iters[env_name])
return {"task_idx": task.data.idx, "task": task, "env_name": env_name}
if cursor >= len(rows):
self.rng.shuffle(rows)
cursor = 0
+3
View File
@@ -63,6 +63,9 @@ class GroupState:
task_idx: int
rollouts_to_schedule: int
target_rollouts: int
task: vf.Task | None = None
"""The group's task (v1 envs — its data is shipped on every dispatch). ``None`` for
legacy envs, which are addressed by ``task_idx`` alone."""
emitted: int = 0
eval_step: int | None = None
pinned_client: vf.ClientConfig | None = None