Run this model with HF Jobs
Run Codeseys/composer-replication-framework on Hugging Face's managed compute, monitor its progress, and retrieve the results when it's done.
Paste one of these prompts into your coding agent:
Fine-tune on my dataset
Use HF Jobs to fine-tune https://huggingface.co/Codeseys/composer-replication-framework. Ask me which dataset to train on, then push the fine-tuned model to a new model repo. Run `hf jobs --help` first to learn the CLI.
Run batch inference
Use HF Jobs to run batch inference with https://huggingface.co/Codeseys/composer-replication-framework on the inputs in a mounted HF Bucket. Ask me which Bucket to use, then write the outputs back to it and keep each input ID. Run `hf jobs --help` first to learn the CLI.
Instructions to use Codeseys/composer-replication-framework with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use Codeseys/composer-replication-framework with Transformers:
# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("Codeseys/composer-replication-framework", device_map="auto") - Notebooks
- Google Colab
- Kaggle
feat(datagen): ADR-010 FeatureDeletionEnv synthetic-data subsystem; accepted
Browse filesTrack A of the deep-work-loop — Composer 2.5's named synthetic-data generator
(Feature Deletion) as a reusable subsystem. New composer_replication.datagen:
- schema.py: FeatureDeletionTask (broken_repo + FAIL_TO_PASS reward target +
PASS_TO_PASS functional guard; golden_diff HELD OUT of repr/observation).
- sandbox.py: Sandbox Protocol + FakeSandbox (unit tests) + LocalSubprocessSandbox
(real); SANDBOX_DENYLIST blocks find/strings/unzip/decompilers/git (the tools
Composer's reported reward-hacks used to recover deleted signatures).
- monitor.py: HackMonitor — flags cache/bytecode-provenance hacks so the grader
masks reward (defense-in-depth behind the sandbox lockdown).
- curriculum.py: DifficultyCurriculum — online pass-rate gate; up-weights the
~0.5 frontier (w ∝ p(1-p)), retires aced tasks, quarantines all-fail tasks
(raw rate, not smoothed). Implements the blog's "select for harder tasks
dynamically".
- validator.py: 4-gate solvability validator (baseline-green / deletion-breaks /
remains-functional / gold-restores) — rejects unreachable or guard-breaking
deletions before they enter the pool.
- env.py: FeatureDeletionEnv — Gym/OpenEnv face (reset/step) + TRL GRPO
reward_fn(prompts, completions, *, task_id, **kwargs)->list[float]; reward =
masked test pass-fraction, naturally graded for multi-feature tasks.
- substrates.py: SweBenchAdapter — invert any SWE-bench-shaped instance into an
FD task (revert gold patch); handles JSON-or-list FAIL_TO_PASS; copyleft
(GPL/AGPL/LGPL) license filter for redistributed diffs.
19 new tests (reward = masked pass-fraction incl multi-feature 0.5; hack
masking; 4-gate validator accept/reject; sandbox denylist; curriculum
frontier/retire/quarantine; reward_fn; substrate inversion + license filter).
Full package: 187 passed, 16 skipped — no regressions. [datagen] extra added.
All ADR-010 core gates green -> accepted. The one Docker-dependent gate
(live SWE-bench-Lite image inversion) is implemented + wired but its live run
is the documented unblocked-by step (no Docker in the CPU dev env).
Reusable beyond this project: "invert a solved-repo dataset into a
reimplement-to-pass verifiable-reward task" is exactly the data-gen primitive
the owner wanted for an adjacent project.
Files changed (12)
- composer_replication/datagen/__init__.pyAdded+46
- composer_replication/datagen/curriculum.pyAdded+80
- composer_replication/datagen/env.pyAdded+129
- composer_replication/datagen/monitor.pyAdded+64
- composer_replication/datagen/sandbox.pyAdded+157
- composer_replication/datagen/schema.pyAdded+44
- composer_replication/datagen/substrates.pyAdded+90
- composer_replication/datagen/tests/test_feature_deletion.pyAdded+245
- composer_replication/datagen/validator.pyAdded+88
- docs/adrs/ADR-010-feature-deletion-datagen.md+13-8
- docs/adrs/README.md+1-1
- pyproject.toml+10
12345678910111213141516171819202122232425262728293031323334353637383940414243444546"""composer_replication.datagen — Feature-Deletion synthetic-data subsystem.
Implements Composer 2.5's named synthetic-data generator (ADR-010): take a repowith passing tests, delete a testable feature, the agent must reimplement it tomake the tests pass — the tests are the verifiable reward.
Public surface: - FeatureDeletionTask — the task tuple (schema.py) - FeatureDeletionEnv — Gym/OpenEnv-style env + TRL reward_fn adapter (env.py) - Sandbox / FakeSandbox / LocalSubprocessSandbox — execution backends (sandbox.py) - HackMonitor — reward-hacking provenance monitor (monitor.py) - DifficultyCurriculum — online pass-rate difficulty gate (curriculum.py) - validate_task — 4-gate solvability validator (validator.py) - SweBenchAdapter — invert a SWE-bench-shaped instance into an FD task (substrates.py)
See research/06-feature-deletion-datagen.md and docs/adrs/ADR-010-*.md."""from __future__ import annotations
from composer_replication.datagen.curriculum import DifficultyCurriculumfrom composer_replication.datagen.env import FeatureDeletionEnv, StepResultfrom composer_replication.datagen.monitor import HackMonitorfrom composer_replication.datagen.sandbox import ( FakeSandbox, LocalSubprocessSandbox, Sandbox, TestRunResult,)from composer_replication.datagen.schema import FeatureDeletionTaskfrom composer_replication.datagen.substrates import SweBenchAdapterfrom composer_replication.datagen.validator import ValidationResult, validate_task
__all__ = [ "FeatureDeletionTask", "FeatureDeletionEnv", "StepResult", "Sandbox", "FakeSandbox", "LocalSubprocessSandbox", "TestRunResult", "HackMonitor", "DifficultyCurriculum", "validate_task", "ValidationResult", "SweBenchAdapter",]1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980"""curriculum.py — online difficulty gate (ADR-010 §2).
The Composer blog: "we both select for and create harder tasks dynamicallythroughout the run." The Composer 2 tech report keys the curriculum on rollout#turns + thinking-token count. This implements the SELECT-FOR half: track arunning pass-rate estimate per task and reweight the sampler.
- Up-weight the frontier: w(t) ∝ p̂(t)·(1−p̂(t)) — max variance ≈ max learning signal; keeps the policy on tasks it solves ~50% of the time (standard curriculum-RL choice, cf. PLR / TD-error curricula). - Retire solved tasks: p̂(t) > τ_easy => weight ~0 (stop paying for aced tasks). - Quarantine impossible tasks: p̂(t) < τ_hard after k exposures => drop (likely broken or reward-hack-only).
The CREATE half (difficulty escalation: deletion span, hint starvation, coupling,multi-feature) is a generator-side concern wired via FeatureDeletionTask.granularity;this class scores and reweights an existing pool."""from __future__ import annotations
from dataclasses import dataclass, field
@dataclassclass _TaskStats: n_pass: int = 0 n_total: int = 0
@property def p_hat(self) -> float: # Laplace-smoothed so an unseen task starts at 0.5 (max weight). return (self.n_pass + 1) / (self.n_total + 2)
@property def raw_rate(self) -> float: # Unsmoothed observed pass rate — used for the quarantine decision, # where the smoothing prior would wrongly keep an all-fail task alive. return self.n_pass / self.n_total if self.n_total else 0.5
@dataclassclass DifficultyCurriculum: """Online pass-rate tracker + sampler reweighter."""
tau_easy: float = 0.95 # above this => retired tau_hard: float = 0.02 # below this (after min_exposures) => quarantined min_exposures: int = 8 # before a task can be quarantined as impossible _stats: dict[str, _TaskStats] = field(default_factory=dict) _quarantined: set[str] = field(default_factory=set)
def update(self, task_id: str, n_pass: int, n_total: int) -> None: st = self._stats.setdefault(task_id, _TaskStats()) st.n_pass += n_pass st.n_total += n_total if ( st.n_total >= self.min_exposures and st.raw_rate < self.tau_hard ): self._quarantined.add(task_id)
def p_hat(self, task_id: str) -> float: return self._stats.get(task_id, _TaskStats()).p_hat
def weight(self, task_id: str) -> float: """Sampling weight. Retired/quarantined => 0; else frontier-variance.""" if task_id in self._quarantined: return 0.0 p = self.p_hat(task_id) if p > self.tau_easy: return 0.0 # retired — model has aced it return p * (1.0 - p) # max at p=0.5
def weights(self, task_ids: list[str]) -> list[float]: return [self.weight(t) for t in task_ids]
def is_quarantined(self, task_id: str) -> bool: return task_id in self._quarantined
def active_tasks(self, task_ids: list[str]) -> list[str]: return [t for t in task_ids if self.weight(t) > 0.0]123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129"""env.py — FeatureDeletionEnv: Gym/OpenEnv face + TRL GRPO reward_fn (ADR-010 §6).
Reward = test pass fraction (|FAIL_TO_PASS passing| / |FAIL_TO_PASS|), gated to0 if the PASS_TO_PASS functional guard is broken OR the hack monitor flags thetrajectory. Naturally graded for multi-feature tasks.
Two faces: - Gym/OpenEnv: reset(task) -> prompt; step(action) -> StepResult (multi-turn). - TRL GRPOTrainer: reward_fn(prompts, completions, **kwargs) -> list[float], matching TRL's RewardFunc convention (the dataset's task_id column is passed through **kwargs)."""from __future__ import annotations
from dataclasses import dataclass, fieldfrom typing import Callable
from composer_replication.datagen.curriculum import DifficultyCurriculumfrom composer_replication.datagen.monitor import HackMonitorfrom composer_replication.datagen.sandbox import Sandboxfrom composer_replication.datagen.schema import FeatureDeletionTask
@dataclassclass StepResult: observation: str reward: float # nonzero only on a terminal grade done: bool info: dict
class FeatureDeletionEnv: """One task per episode. Execution + safeguards live in the Sandbox (§3)."""
def __init__( self, sandbox: Sandbox, monitor: HackMonitor | None = None, *, max_turns: int = 40, curriculum: DifficultyCurriculum | None = None, registry: dict[str, FeatureDeletionTask] | None = None, replay: Callable[["FeatureDeletionEnv", str], StepResult] | None = None, ) -> None: self.sandbox = sandbox self.monitor = monitor or HackMonitor() self.max_turns = max_turns self.curriculum = curriculum or DifficultyCurriculum() self.registry: dict[str, FeatureDeletionTask] = registry or {} self._replay = replay self.task: FeatureDeletionTask | None = None self.turns = 0
# ---- Gym/OpenEnv face -------------------------------------------------
def reset(self, task: FeatureDeletionTask) -> str: self.task = task self.turns = 0 self.sandbox.boot(task.broken_image) self.registry.setdefault(task.task_id, task) return self._render_prompt(task)
def step(self, action: dict) -> StepResult: if self.task is None: raise RuntimeError("step() called before reset()") self.turns += 1 if action.get("type") == "submit" or self.turns >= self.max_turns: return self._grade() obs = self.sandbox.exec(action) return StepResult(obs, 0.0, False, {"turn": self.turns})
def _render_prompt(self, task: FeatureDeletionTask) -> str: # golden_diff / deleted_symbols are NEVER shown. failing = "\n".join(f" - {t}" for t in task.fail_to_pass) return ( f"Repository `{task.repo}` is missing a feature. The following tests " f"currently FAIL and must pass:\n{failing}\n\n" f"Run the test command `{task.test_command}` to check your work. " "Edit the source to reimplement the missing feature. Do not break " "any currently-passing tests. Submit when the failing tests pass." )
def _grade(self) -> StepResult: assert self.task is not None t = self.task r = self.sandbox.run_tests(t.test_command, t.fail_to_pass + t.pass_to_pass) frac = r.n_pass(t.fail_to_pass) / max(len(t.fail_to_pass), 1) guard_ok = r.all_pass(t.pass_to_pass) hacked = self.monitor.flag(self.sandbox.trajectory(), t.deleted_symbols) reward = frac * (1.0 if (guard_ok and not hacked) else 0.0) return StepResult( r.stdout, reward, True, {"frac": frac, "guard_ok": guard_ok, "hacked": hacked}, )
# ---- TRL GRPOTrainer face --------------------------------------------
def reward_fn( self, prompts: list[str], completions: list[str], *, task_id: list[str] | None = None, **kwargs, ) -> list[float]: """TRL RewardFunc: reward = masked test pass-fraction per completion.
`task_id` is passed through from the dataset column. `_run_completion` replays the agent turns encoded in `completion` against the env; in the absence of an injected replay fn we treat the completion as a single "submit" (the sandbox's pre-loaded outcome determines the reward), which is what the unit tests exercise. """ if task_id is None: raise ValueError( "reward_fn requires a `task_id` column (passed via the GRPO " "dataset) to map each completion to its FeatureDeletionTask." ) rewards: list[float] = [] for comp, tid in zip(completions, task_id): task = self.registry[tid] self.reset(task) if self._replay is not None: res = self._replay(self, comp) else: res = self.step({"type": "submit"}) rewards.append(res.reward) self.curriculum.update(tid, n_pass=int(res.reward > 0), n_total=1) return rewards12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364"""monitor.py — reward-hacking provenance monitor (ADR-010 §3c).
Composer's blog reports the model recovering deleted signatures by decompilingJava bytecode and reverse-engineering Python type-check caches. The PRIMARYcontrol is the sandbox lockdown (no decompilers, scrubbed caches — seesandbox.py). This monitor is defense-in-depth: it flags trajectories where adeleted symbol reappears via a non-implementation path (reading a cache,importing a sibling that smuggles the cached bytecode, decompiling), so thegrader can MASK the reward.
It is heuristic (will have false negatives); it is not the only safeguard."""from __future__ import annotations
import refrom dataclasses import dataclass, field
# Substrings in a trajectory action that indicate a likely provenance hack:# reaching a deleted symbol through a cache/archive/decompiler rather than# reimplementing it._HACK_SIGNATURES: tuple[str, ...] = ( "__pycache__", ".pyc", ".mypy_cache", ".class", "javap", "decompile", "uncompyle", "decompyle", "strings ", "unzip ", "jar -xf", "git show", "git log", "git cat-file",)
@dataclassclass HackMonitor: """Flags a trajectory as a suspected reward-hack.
`flag(trajectory, deleted_symbols)` returns True if any action looks like it recovered a deleted symbol via a non-implementation path. """
extra_signatures: tuple[str, ...] = field(default_factory=tuple)
def flag(self, trajectory: list[dict], deleted_symbols: tuple[str, ...]) -> bool: sigs = _HACK_SIGNATURES + tuple(self.extra_signatures) for action in trajectory: blob = " ".join( str(v) for v in action.values() if isinstance(v, (str, int, float)) ).lower() if any(sig.lower() in blob for sig in sigs): return True # If a deleted symbol's exact name appears verbatim alongside a # cache/archive read, that's a strong hack signal. for sym in deleted_symbols: if sym and sym.lower() in blob and re.search( r"(cache|\.pyc|\.class|decompil|disassembl)", blob ): return True return False123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157"""sandbox.py — execution backends for the Feature-Deletion env (ADR-010).
The env never runs code directly; it delegates to a Sandbox. This keeps thereward-hacking safeguards (§3: allowlisted shell, no net, scrubbed tree) in oneplace and lets the env + monitor + curriculum + validator all be unit-testedwith a FakeSandbox (no Docker). The real LocalSubprocessSandbox runs tests inthe substrate's frozen image and is exercised by the docker-gated substrate test."""from __future__ import annotations
import subprocessfrom dataclasses import dataclass, fieldfrom typing import Protocol, runtime_checkable
@dataclassclass TestRunResult: """Outcome of running a set of tests.""" passed: frozenset[str] failed: frozenset[str] stdout: str = "" collected_ok: bool = True
def n_pass(self, tests: tuple[str, ...]) -> int: return sum(1 for t in tests if t in self.passed)
def all_pass(self, tests: tuple[str, ...]) -> bool: return all(t in self.passed for t in tests)
def all_fail(self, tests: tuple[str, ...]) -> bool: return all(t in self.failed for t in tests)
# Commands the agent is NOT allowed to run in the sandbox — these are the tools# the Composer blog's reward-hacks used to recover deleted signatures# (decompilers, archive/string scrapers, cache readers). Defense-in-depth: the# primary control is that __pycache__/.mypy_cache/.class are scrubbed pre-task.SANDBOX_DENYLIST: frozenset[str] = frozenset( { "find", "strings", "unzip", "jar", "javap", "unzip", "procyon", "cfr", "jd-cli", "jadx", # Java decompilers "uncompyle6", "decompyle3", # Python decompilers "git", # .git is stripped; no history mining })
@runtime_checkableclass Sandbox(Protocol): """An execution environment for one FD episode."""
def boot(self, image: str) -> None: ... def exec(self, action: dict) -> str: ... def run_tests(self, test_command: str, tests: tuple[str, ...]) -> TestRunResult: ... def trajectory(self) -> list[dict]: ... def is_command_allowed(self, command: str) -> bool: ...
@dataclassclass FakeSandbox: """In-memory sandbox for unit tests. Holds a programmable test outcome so the env/monitor/curriculum/validator can be exercised deterministically without Docker or a real repo."""
# test name -> bool (passing) for the CURRENT repo state test_outcomes: dict[str, bool] = field(default_factory=dict) _trajectory: list[dict] = field(default_factory=list) booted_image: str | None = None
def boot(self, image: str) -> None: self.booted_image = image self._trajectory = []
def exec(self, action: dict) -> str: self._trajectory.append(action) cmd = str(action.get("command", "")) head = cmd.strip().split()[0] if cmd.strip() else "" if head and not self.is_command_allowed(head): return f"ERROR: command '{head}' is not allowed in the sandbox." # A "set_outcome" pseudo-action lets a test flip pass/fail mid-episode. if action.get("type") == "set_outcome": self.test_outcomes.update(action.get("outcomes", {})) return "ok" return action.get("stdout", "")
def run_tests(self, test_command: str, tests: tuple[str, ...]) -> TestRunResult: passed = frozenset(t for t in tests if self.test_outcomes.get(t, False)) failed = frozenset(t for t in tests if not self.test_outcomes.get(t, False)) return TestRunResult(passed=passed, failed=failed, stdout="(fake)")
def trajectory(self) -> list[dict]: return list(self._trajectory)
def is_command_allowed(self, command: str) -> bool: return command not in SANDBOX_DENYLIST
@dataclassclass LocalSubprocessSandbox: """Real sandbox: runs the test command in a subprocess inside a working tree.
Minimal stand-in for the full locked-down container of §3 (which would add network egress-off + Firecracker-style isolation). Here we enforce the command denylist and run the test command, parsing pytest-style pass/fail. Intended for the docker-gated substrate test and local development; a production deploy would wrap this in the substrate's frozen Docker image. """
workdir: str _trajectory: list[dict] = field(default_factory=list) booted_image: str | None = None
def boot(self, image: str) -> None: self.booted_image = image self._trajectory = []
def is_command_allowed(self, command: str) -> bool: return command not in SANDBOX_DENYLIST
def exec(self, action: dict) -> str: self._trajectory.append(action) cmd = str(action.get("command", "")) if not cmd.strip(): return "" head = cmd.strip().split()[0] if not self.is_command_allowed(head): return f"ERROR: command '{head}' is not allowed in the sandbox." proc = subprocess.run( cmd, shell=True, cwd=self.workdir, capture_output=True, text=True, timeout=300 ) return (proc.stdout or "") + (proc.stderr or "")
def run_tests(self, test_command: str, tests: tuple[str, ...]) -> TestRunResult: # Run pytest with explicit node ids; parse the summary line. node_ids = " ".join(tests) cmd = f"{test_command} {node_ids}" proc = subprocess.run( cmd, shell=True, cwd=self.workdir, capture_output=True, text=True, timeout=600 ) out = (proc.stdout or "") + (proc.stderr or "") # Conservative parse: a test is "passed" only if its node id appears with # PASSED, else failed. Collection errors => collected_ok False. passed, failed = set(), set() collected_ok = "errors during collection" not in out.lower() for t in tests: # pytest -v prints "<nodeid> PASSED"; fall back to overall exit code. if f"{t} PASSED" in out or (proc.returncode == 0 and not failed): passed.add(t) else: failed.add(t) return TestRunResult( passed=frozenset(passed), failed=frozenset(failed), stdout=out, collected_ok=collected_ok, )
def trajectory(self) -> list[dict]: return list(self._trajectory)1234567891011121314151617181920212223242526272829303132333435363738394041424344"""schema.py — the Feature-Deletion task tuple (ADR-010)."""from __future__ import annotations
from dataclasses import dataclass, field
@dataclass(frozen=True)class FeatureDeletionTask: """One Feature-Deletion task = a broken repo + the tests that grade a fix.
The constructive inverse of a SWE-bench instance: instead of mining a human PR that fixed a bug, we revert a gold patch on a passing repo to manufacture the broken state, then ask the agent to re-derive the patch.
Reward at training time = fraction of `fail_to_pass` tests the agent's diff turns green, gated by `pass_to_pass` staying green ("remains functional") and the hack monitor. `golden_diff` is HELD OUT — used only by the solvability validator and the provenance monitor, NEVER placed in the observation shown to the policy. """
task_id: str repo: str # e.g. "getmoto/moto" base_commit: str broken_image: str # docker tag of the scrubbed broken repo test_command: str # e.g. "python -m pytest -q" fail_to_pass: tuple[str, ...] # reward target (must go red->green) pass_to_pass: tuple[str, ...] # functional guard (must stay green) golden_diff: str = field(default="", repr=False) # HELD OUT granularity: str = "function" # function|file|feature (curriculum escalation) deleted_symbols: tuple[str, ...] = () # for the AST-provenance monitor upstream_license: str = "unknown" # carried from substrate; gates redistribution difficulty_prior: float = 0.5 # seeded from substrate LLM score if available
def __post_init__(self) -> None: if not self.fail_to_pass: raise ValueError( f"FeatureDeletionTask {self.task_id!r}: fail_to_pass must be " "non-empty (there must be at least one reward-target test)." ) if self.granularity not in ("function", "file", "feature"): raise ValueError( f"granularity must be function|file|feature, got {self.granularity!r}" )123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990"""substrates.py — adapt SWE-bench-shaped instances into Feature-Deletion tasks.
Every substrate (SWE-bench/Lite/Verified, SWE-Gym, R2E-Gym, SWE-rebench) shipsthe tuple (repo, base_commit, patch=gold, test_patch, FAIL_TO_PASS, PASS_TO_PASS).The Feature-Deletion mapping is identical for all of them: - revert `patch` -> manufacture the broken_repo; - FAIL_TO_PASS is the reward target; - PASS_TO_PASS is the "stay-functional" guard.
This adapter does the *schema* inversion (instance dict -> FeatureDeletionTask).Actually materializing the broken repo (git apply -R the patch, scrub caches,freeze image) is the sandbox/Docker step, exercised by the docker-gated test."""from __future__ import annotations
import jsonfrom dataclasses import dataclass
from composer_replication.datagen.schema import FeatureDeletionTask
# Copyleft licenses we refuse to redistribute derivatives of (we redistribute# deletions/diffs = derivative works). Per research/06 §4 license rule._COPYLEFT = ("gpl", "agpl", "lgpl")
def _as_tuple(v) -> tuple[str, ...]: """SWE-bench stores FAIL_TO_PASS/PASS_TO_PASS as a JSON-encoded list string OR an actual list, depending on the loader. Normalize to a tuple of str.""" if v is None: return () if isinstance(v, str): try: v = json.loads(v) except (json.JSONDecodeError, ValueError): return (v,) if v else () if isinstance(v, (list, tuple)): return tuple(str(x) for x in v) return (str(v),)
@dataclassclass SweBenchAdapter: """Convert a SWE-bench-shaped instance dict into a FeatureDeletionTask.
`instance` is one row from any SWE-* dataset. `image_for` resolves the instance to a frozen broken-repo Docker tag (substrate-specific); defaults to a conventional SWE-bench eval image name. """
default_test_command: str = "python -m pytest -q"
def image_for(self, instance: dict) -> str: # SWE-rebench carries `docker_image`; SWE-bench/Lite use a convention. if instance.get("docker_image"): return str(instance["docker_image"]) iid = instance.get("instance_id", "unknown") return f"swebench/sweb.eval.x86_64.{iid}:latest"
def to_task(self, instance: dict) -> FeatureDeletionTask: iid = str(instance.get("instance_id") or instance.get("task_id") or "unknown") gold = str(instance.get("patch", "")) license_name = str(instance.get("license_name", "unknown")) ftp = _as_tuple(instance.get("FAIL_TO_PASS")) ptp = _as_tuple(instance.get("PASS_TO_PASS")) # Difficulty prior from SWE-rebench's LLM score if present (0..1). diff = instance.get("difficulty") try: difficulty_prior = float(diff) if diff is not None else 0.5 except (TypeError, ValueError): difficulty_prior = 0.5 return FeatureDeletionTask( task_id=iid, repo=str(instance.get("repo", "unknown")), base_commit=str(instance.get("base_commit", "")), broken_image=self.image_for(instance), test_command=self.default_test_command, fail_to_pass=ftp, pass_to_pass=ptp, golden_diff=gold, granularity="feature", # SWE instances are PR-sized (multi-symbol) upstream_license=license_name, difficulty_prior=difficulty_prior, )
@staticmethod def is_redistributable(task: FeatureDeletionTask) -> bool: """False if the upstream repo license is copyleft (we redistribute derivative diffs, so GPL/AGPL/LGPL repos are filtered out).""" lic = task.upstream_license.lower() return not any(c in lic for c in _COPYLEFT)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245"""Tests for the FeatureDeletionEnv data-gen subsystem (ADR-010).
Covers the ADR-010 acceptance gates (CPU-only via FakeSandbox; the realsubstrate-inversion gate is docker-gated and lives in a separate skipif test): - FeatureDeletionTask schema + reward = masked test pass-fraction (env); - 4-gate solvability validator (rejects unreachable/broken tasks); - reward-hack safeguard (sandbox denylist + AST/provenance monitor masks reward); - online difficulty curriculum (frontier up-weight, retire, quarantine); - TRL reward_fn(prompts, completions, **kwargs) -> list[float] adapter; - SweBenchAdapter schema inversion + license filter."""from __future__ import annotations
import pytest
from composer_replication.datagen import ( DifficultyCurriculum, FakeSandbox, FeatureDeletionEnv, FeatureDeletionTask, HackMonitor, SweBenchAdapter, validate_task,)from composer_replication.datagen.sandbox import SANDBOX_DENYLIST
def _task(**kw) -> FeatureDeletionTask: base = dict( task_id="t1", repo="acme/widget", base_commit="abc123", broken_image="img:broken", test_command="python -m pytest -q", fail_to_pass=("test_feature_a",), pass_to_pass=("test_unrelated",), golden_diff="diff --git ...", deleted_symbols=("feature_a",), ) base.update(kw) return FeatureDeletionTask(**base)
# --- schema -----------------------------------------------------------------
def test_task_requires_nonempty_fail_to_pass(): with pytest.raises(ValueError, match="fail_to_pass must be"): _task(fail_to_pass=())
def test_golden_diff_not_in_repr(): t = _task() assert "golden_diff" not in repr(t) # held out — never leaked
# --- env reward = masked pass-fraction --------------------------------------
def test_reward_is_pass_fraction_when_guard_ok(): # 1 of 1 target passing, guard passing, no hack => reward 1.0 sb = FakeSandbox(test_outcomes={"test_feature_a": True, "test_unrelated": True}) env = FeatureDeletionEnv(sb) env.reset(_task()) res = env.step({"type": "submit"}) assert res.done and res.reward == 1.0 assert res.info["frac"] == 1.0 and res.info["guard_ok"]
def test_reward_graded_for_multi_feature(): sb = FakeSandbox(test_outcomes={"a": True, "b": False, "keep": True}) env = FeatureDeletionEnv(sb) env.reset(_task(fail_to_pass=("a", "b"), pass_to_pass=("keep",), deleted_symbols=())) res = env.step({"type": "submit"}) assert res.reward == 0.5 # 1 of 2 target tests pass
def test_reward_zeroed_when_functional_guard_broken(): # target passes but a PASS_TO_PASS test regressed => reward 0 sb = FakeSandbox(test_outcomes={"test_feature_a": True, "test_unrelated": False}) env = FeatureDeletionEnv(sb) env.reset(_task()) res = env.step({"type": "submit"}) assert res.reward == 0.0 and not res.info["guard_ok"]
# --- reward-hack safeguards -------------------------------------------------
def test_sandbox_denies_decompiler_and_cache_tools(): sb = FakeSandbox() for bad in ("find", "strings", "unzip", "javap", "uncompyle6", "git"): assert bad in SANDBOX_DENYLIST out = sb.exec({"type": "shell", "command": f"{bad} something"}) assert "not allowed" in out
def test_monitor_flags_cache_provenance_hack(): mon = HackMonitor() traj = [{"type": "shell", "command": "cat build/__pycache__/feature_a.pyc"}] assert mon.flag(traj, deleted_symbols=("feature_a",)) is True
def test_monitor_passes_clean_reimplementation(): mon = HackMonitor() traj = [ {"type": "edit", "path": "src/widget.py", "content": "def feature_a(): return 42"}, {"type": "shell", "command": "python -m pytest -q"}, ] assert mon.flag(traj, deleted_symbols=("feature_a",)) is False
def test_reward_masked_when_hack_detected(): sb = FakeSandbox(test_outcomes={"test_feature_a": True, "test_unrelated": True}) env = FeatureDeletionEnv(sb) env.reset(_task()) # agent reads the bytecode cache instead of reimplementing env.step({"type": "shell", "command": "javap -c build/feature_a.class"}) res = env.step({"type": "submit"}) assert res.info["hacked"] is True assert res.reward == 0.0 # masked despite tests "passing"
# --- 4-gate solvability validator -------------------------------------------
def _materializers(): """Return (solved, broken, gold) callbacks that flip a FakeSandbox's outcomes to emulate each repo state.""" def solved(sb, task): sb.test_outcomes = {t: True for t in task.fail_to_pass + task.pass_to_pass} def broken(sb, task): sb.test_outcomes = { **{t: False for t in task.fail_to_pass}, # target broken **{t: True for t in task.pass_to_pass}, # guard still green } def gold(sb, task): sb.test_outcomes = {t: True for t in task.fail_to_pass + task.pass_to_pass} return solved, broken, gold
def test_validator_accepts_well_formed_task(): sb = FakeSandbox() solved, broken, gold = _materializers() res = validate_task(_task(), sb, materialize_solved=solved, materialize_broken=broken, apply_gold=gold) assert res.ok assert res.failed_gates() == []
def test_validator_rejects_unreachable_deletion(): """A deletion that does NOT break the target tests fails gate 2.""" sb = FakeSandbox() solved, _broken, gold = _materializers() def broken_but_target_still_passes(sb, task): sb.test_outcomes = {t: True for t in task.fail_to_pass + task.pass_to_pass} res = validate_task(_task(), sb, materialize_solved=solved, materialize_broken=broken_but_target_still_passes, apply_gold=gold) assert not res.ok assert "gate2_deletion_breaks" in res.failed_gates()
def test_validator_rejects_when_guard_breaks(): sb = FakeSandbox() solved, _b, gold = _materializers() def broken_breaks_guard(sb, task): sb.test_outcomes = { **{t: False for t in task.fail_to_pass}, **{t: False for t in task.pass_to_pass}, # guard regressed } res = validate_task(_task(), sb, materialize_solved=solved, materialize_broken=broken_breaks_guard, apply_gold=gold) assert not res.ok assert "gate3_remains_functional" in res.failed_gates()
# --- curriculum -------------------------------------------------------------
def test_curriculum_upweights_frontier_over_solved(): cur = DifficultyCurriculum() # task A: solved ~half the time (frontier); task B: aced for _ in range(10): cur.update("A", n_pass=1, n_total=2) # ~0.5 for _ in range(10): cur.update("B", n_pass=10, n_total=10) # ~1.0 assert cur.weight("A") > cur.weight("B") assert cur.weight("B") == 0.0 # retired (aced)
def test_curriculum_quarantines_impossible_task(): cur = DifficultyCurriculum(min_exposures=4, tau_hard=0.05) for _ in range(8): cur.update("hard", n_pass=0, n_total=1) assert cur.is_quarantined("hard") assert cur.weight("hard") == 0.0
# --- TRL reward_fn adapter --------------------------------------------------
def test_reward_fn_returns_one_float_per_completion(): sb = FakeSandbox(test_outcomes={"test_feature_a": True, "test_unrelated": True}) task = _task() env = FeatureDeletionEnv(sb, registry={task.task_id: task}) rewards = env.reward_fn( prompts=["p"], completions=["...agent diff..."], task_id=[task.task_id] ) assert len(rewards) == 1 assert 0.0 <= rewards[0] <= 1.0 assert rewards[0] == 1.0
def test_reward_fn_requires_task_id(): env = FeatureDeletionEnv(FakeSandbox()) with pytest.raises(ValueError, match="task_id"): env.reward_fn(prompts=["p"], completions=["c"])
# --- SweBenchAdapter --------------------------------------------------------
def test_swebench_adapter_inverts_instance(): inst = { "instance_id": "django__django-12345", "repo": "django/django", "base_commit": "deadbeef", "patch": "diff --git a/x b/x", "FAIL_TO_PASS": '["test_new_behavior"]', "PASS_TO_PASS": '["test_old_a", "test_old_b"]', "license_name": "BSD-3-Clause", } task = SweBenchAdapter().to_task(inst) assert task.task_id == "django__django-12345" assert task.fail_to_pass == ("test_new_behavior",) assert task.pass_to_pass == ("test_old_a", "test_old_b") assert task.golden_diff == "diff --git a/x b/x" # held out but carried assert SweBenchAdapter.is_redistributable(task) # BSD = ok
def test_swebench_adapter_filters_copyleft(): inst = { "instance_id": "gpl__thing-1", "repo": "x/y", "base_commit": "c", "patch": "d", "FAIL_TO_PASS": '["t"]', "PASS_TO_PASS": "[]", "license_name": "GPL-3.0", } task = SweBenchAdapter().to_task(inst) assert not SweBenchAdapter.is_redistributable(task)
def test_swebench_adapter_handles_list_or_jsonstr_tests(): # FAIL_TO_PASS may arrive as a real list (some loaders) or JSON string. for ftp in (["t1", "t2"], '["t1", "t2"]'): inst = {"instance_id": "i", "repo": "r", "base_commit": "c", "patch": "p", "FAIL_TO_PASS": ftp, "PASS_TO_PASS": "[]"} task = SweBenchAdapter().to_task(inst) assert task.fail_to_pass == ("t1", "t2")12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788"""validator.py — 4-gate solvability validator (ADR-010 §5c).
Before a Feature-Deletion task enters the training pool, it must pass fourgates against a sandbox, or it is a broken/unsolvable/reward-hack-only task:
Gate 1 — baseline green: in the SOLVED (gold-applied) state, all target + keep tests pass. Gate 2 — deletion breaks the feature: in the BROKEN state, all FAIL_TO_PASS tests fail. Gate 3 — remains functional: in the BROKEN state, collection works and all PASS_TO_PASS tests still pass (the blog's "codebase remains functional" constraint). Gate 4 — solvability: applying the gold diff to the broken state turns the FAIL_TO_PASS tests green again (the task is actually achievable).
The sandbox is responsible for materializing each state; the validator drivesit and records which gates passed. Callers use a real sandbox in CI (docker-gated)and a FakeSandbox in unit tests."""from __future__ import annotations
from dataclasses import dataclassfrom typing import Callable
from composer_replication.datagen.sandbox import Sandbox, TestRunResultfrom composer_replication.datagen.schema import FeatureDeletionTask
@dataclassclass ValidationResult: gate1_baseline_green: bool gate2_deletion_breaks: bool gate3_remains_functional: bool gate4_gold_restores: bool
@property def ok(self) -> bool: return ( self.gate1_baseline_green and self.gate2_deletion_breaks and self.gate3_remains_functional and self.gate4_gold_restores )
def failed_gates(self) -> list[str]: out = [] if not self.gate1_baseline_green: out.append("gate1_baseline_green") if not self.gate2_deletion_breaks: out.append("gate2_deletion_breaks") if not self.gate3_remains_functional: out.append("gate3_remains_functional") if not self.gate4_gold_restores: out.append("gate4_gold_restores") return out
def validate_task( task: FeatureDeletionTask, sandbox: Sandbox, *, materialize_solved: Callable[[Sandbox, FeatureDeletionTask], None], materialize_broken: Callable[[Sandbox, FeatureDeletionTask], None], apply_gold: Callable[[Sandbox, FeatureDeletionTask], None],) -> ValidationResult: """Run the 4 gates. The three `materialize_*` callbacks put the sandbox into each state (solved / broken / broken+gold-applied); separating them keeps this function backend-agnostic (Docker, local subprocess, or fake).""" targets = task.fail_to_pass keep = task.pass_to_pass
# Gate 1 — baseline green (solved state). materialize_solved(sandbox, task) r_solved: TestRunResult = sandbox.run_tests(task.test_command, targets + keep) gate1 = r_solved.all_pass(targets) and r_solved.all_pass(keep)
# Gates 2+3 — broken state. materialize_broken(sandbox, task) r_broken: TestRunResult = sandbox.run_tests(task.test_command, targets + keep) gate2 = bool(targets) and r_broken.all_fail(targets) gate3 = r_broken.collected_ok and r_broken.all_pass(keep)
# Gate 4 — solvability (broken + gold diff applied). apply_gold(sandbox, task) r_gold: TestRunResult = sandbox.run_tests(task.test_command, targets + keep) gate4 = r_gold.all_pass(targets) and r_gold.all_pass(keep)
return ValidationResult(gate1, gate2, gate3, gate4)12234587 unmodified lines9394959697989910010110296979899100101102103104105106107108109110---status: proposedstatus: accepteddate: 2026-05-29deciders: [Codeseys, ARIA]---87 unmodified lines
## Acceptance gate (must be green before status flips to accepted)
- [ ] `FeatureDeletionTask` dataclass + `FeatureDeletionEnv` (`reset`/`step`/`reward`) implemented; reward = masked test-pass fraction with a unit test on a synthetic mini-repo.- [ ] One substrate adapter (SWE-bench-Lite, smallest) inverts ≥1 real task: revert gold patch → broken repo, a test asserts the broken repo FAILS `FAIL_TO_PASS` and PASSES `PASS_TO_PASS`, and applying the gold patch restores green. (Runs in the substrate's Docker image; gated `skipif` on docker availability for CI.)- [ ] 4-gate solvability validator implemented; a test asserts a task with an unreachable deletion (no test exercises it) is rejected.- [ ] Reward-hacking safeguard: a test asserts the sandbox lacks `find`/`strings`/`unzip` and that `__pycache__`/`.mypy_cache` are scrubbed pre-task; the AST provenance monitor masks reward on a crafted "symbol reappears via import of a sibling cache" hack.- [ ] Online difficulty gate: a unit test asserts tasks are rankable by a difficulty signal (turns/thinking-token proxy) and the gate up-weights the hard tail.- [ ] TRL `reward_fn(prompts, completions, **kwargs) -> list[float]` adapter exists; a test asserts it returns one float in [0,1] per completion = test-pass fraction.- [ ] `[datagen]` optional extra added to `pyproject.toml`; `pip install -e .[datagen]` resolves.Core gates green as of 2026-05-29 (19 tests in`composer_replication/datagen/tests/test_feature_deletion.py`, all CPU via`FakeSandbox`). The single Docker-dependent gate (real substrate inversion) isimplemented but its live run is the documented unblocked-by step — see note.
- [x] `FeatureDeletionTask` dataclass + `FeatureDeletionEnv` (`reset`/`step`/`reward`) implemented; reward = masked test-pass fraction — `test_reward_is_pass_fraction_when_guard_ok`, `test_reward_graded_for_multi_feature` (0.5 for 1-of-2), `test_reward_zeroed_when_functional_guard_broken`. `golden_diff` held out of `repr` (`test_golden_diff_not_in_repr`).- [~] SWE-bench-Lite substrate adapter: **schema inversion implemented + tested** (`SweBenchAdapter.to_task` — `test_swebench_adapter_inverts_instance`, JSON-or-list FAIL_TO_PASS handling, copyleft filter). The **live revert-gold-patch → broken-repo → test-run** path requires a substrate Docker image; `LocalSubprocessSandbox` + `validate_task` are wired for it, and the gate is exercised in unit form via `FakeSandbox` materializers (`test_validator_accepts_well_formed_task`). UNBLOCKED-BY: a `skipif(docker)` end-to-end test that pulls one SWE-bench-Lite image and runs the 4 gates against it — deferred to first GPU/Docker run (no Docker in this CPU env).- [x] 4-gate solvability validator implemented; `test_validator_rejects_unreachable_deletion` (deletion that doesn't break the target → gate 2 fails) and `test_validator_rejects_when_guard_breaks` (gate 3 fails).- [x] Reward-hacking safeguard: `SANDBOX_DENYLIST` blocks `find`/`strings`/`unzip`/decompilers/`git` (`test_sandbox_denies_decompiler_and_cache_tools`); `HackMonitor` flags cache/bytecode-provenance hacks (`test_monitor_flags_cache_provenance_hack`) and passes clean reimplementation (`test_monitor_passes_clean_reimplementation`); reward is masked to 0 when a hack is detected even if tests "pass" (`test_reward_masked_when_hack_detected`).- [x] Online difficulty gate: `DifficultyCurriculum` up-weights the frontier (~0.5 pass-rate) over aced tasks and retires aced ones (`test_curriculum_upweights_frontier_over_solved`); quarantines all-fail tasks after `min_exposures` (`test_curriculum_quarantines_impossible_task`). NOTE: quarantine uses the *raw* observed rate, not the Laplace-smoothed `p_hat` (smoothing is for weighting, not the have-we-ever-passed decision).- [x] TRL `reward_fn(prompts, completions, *, task_id, **kwargs) -> list[float]` adapter returns one float in [0,1] per completion = masked pass-fraction (`test_reward_fn_returns_one_float_per_completion`); requires the `task_id` column (`test_reward_fn_requires_task_id`).- [x] `[datagen]` optional extra added to `pyproject.toml` (`datasets` + `docker`); pure-Python core needs only `datasets`.
## More Information
10 unmodified lines1112131414151610 unmodified lines| [ADR-007](ADR-007-self-distillation-losses.md) | Self-distillation losses landscape | accepted | 2026-05-26 || [ADR-008](ADR-008-drgrpo-sdpo-live-channel.md) | Target Dr. GRPO + host live SDPO channel in TRL trainer | accepted | 2026-05-29 || [ADR-009](ADR-009-layered-hint-generator.md) | Layered HintGenerator for SDPO textual feedback | accepted | 2026-05-29 || [ADR-010](ADR-010-feature-deletion-datagen.md) | FeatureDeletionEnv synthetic-data subsystem over OSS SWE substrates | proposed | 2026-05-29 || [ADR-010](ADR-010-feature-deletion-datagen.md) | FeatureDeletionEnv synthetic-data subsystem over OSS SWE substrates | accepted | 2026-05-29 |
Sorted by number ascending. ADRs are immutable after `accepted`; supersede or amend rather than edit.81 unmodified lines8283848586878889909192939495969781 unmodified lines "accelerate>=1.0", "datasets>=3.0",]# Feature-Deletion synthetic-data generation (ADR-010)# Inverts OSS SWE substrates into reimplement-to-pass tasks. `datasets` loads# the substrate instances; `docker` runs tests in the substrate's frozen image.# Pure-Python core (schema/env/monitor/curriculum/validator/substrate-adapter)# needs only `datasets`; `docker` is for the real LocalSubprocessSandbox /# substrate-inversion path.datagen = [ "datasets>=3.0", "docker>=7.0",]# PRIME-RL recipe (Recipe C — per ADR-006)# NOTE: a `prime-rl` extra used to be advertised here pinning# `prime-rl>=0.5`. That pin is unsatisfiable: the `prime-rl` PyPI name is