Skip to content

Use the library sandbox helper in the matmul GRPO notebooks - #364

Merged
danielhanchen merged 3 commits into
mainfrom
harden-rl-sandbox
Aug 26, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
harden-rl-sandbox

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Companion to unslothai/unsloth-zoo#1105.

The 2048 and Sudoku RL notebooks already pull the sandbox helper from the library:

from unsloth import check_python_modules, create_locked_down_function

The gpt-oss and Gemma4 matmul GRPO family does not. It still carries its own inline copy:

def create_locked_down_function(function):
    output_function = {}
    exec(function, {}, output_function)
    new_matmul = output_function["matmul"]
    new_matmul = types.FunctionType(new_matmul.__code__, {})
    return new_matmul

That copy executes the model's generated Python with full builtins available. The types.FunctionType(..., {}) line reads as if it locks things down, but CPython injects the real builtins module into any globals mapping that has no __builtins__ key, so it does not. check_only_stdlib_imports does not cover the gap either: os, subprocess and socket are all stdlib and pass it, and the ok flag it returns is computed and then discarded by the reward functions, which only test if function is None or "error" in info.

So a completion of this shape runs on the user's Colab or Kaggle box:

def matmul(A, B):
    import os
    os.system("...")

The cell had spread to 35 files by copy paste. This PR switches the four sources of truth over to the library helper and regenerates the rest, so every one of them picks up the hardening in unslothai/unsloth-zoo#1105.

Sources of truth edited

  • original_template/gpt-oss-(20B)-GRPO.ipynb
  • original_template/gpt-oss-(20B)_A100-GRPO.ipynb
  • original_template/gpt_oss_(20B)_GRPO_BF16.ipynb
  • nb/Gemma4_(E2B)_GRPO.ipynb, which has no original_template counterpart and is therefore its own source

The other 31 files in the diff are regenerated output from update_all_notebooks.py and update_all_notebooks.py --amd.

What is deliberately left alone

check_only_stdlib_imports and the explanation around it stay exactly as they are. They still drive the no_cheating reward, which is what they were written for, and the teaching narrative about reward hacking is unchanged. The demo cells that exec a hardcoded sample string are also untouched, since that is a literal rather than model output.

No user facing change: same cells, same flow, same outputs.

Verification

  • Repo wide grep for exec(function returns nothing.
  • tests/test_python_scripts_regen_parity.py passes 888/888.
  • Sandbox behaviour is covered on the unsloth-zoo side. Escapes via import os, __import__, open, eval, getattr and ().__class__.__bases__[0].__subclasses__() are blocked, while the 2048 sample using import math and from typing import Callable, the matmul sample from this notebook, and the Sudoku sample all still run.

One note for whoever regenerates next

The molab step fails silently when marimo is not installed. It prints a warning and the generator still exits 0, which leaves the four molab files stale while the run looks like it succeeded. I only caught it by grepping for the old cell afterwards. Might be worth making that step fail loudly.

A full regeneration also produces a fair amount of drift unrelated to any given change: a marimo version bump, the announcement cell switching from Studio to Desktop, README rows for notebooks missing from the table, scripts/model_created_at.csv, and a handful of AMD and molab variants that had never been generated. I reverted all of that here to keep this diff to the security fix. The four molab files are the exception, since marimo rewrites whole files and the version bump cannot be separated out.

The 2048 and Sudoku RL notebooks already do

    from unsloth import check_python_modules, create_locked_down_function

but the gpt-oss and Gemma4 matmul GRPO family still carries its own inline
copy of the helper:

    def create_locked_down_function(function):
        output_function = {}
        exec(function, {}, output_function)
        new_matmul = output_function["matmul"]
        new_matmul = types.FunctionType(new_matmul.__code__, {})
        return new_matmul

That copy runs the model's generated Python with full builtins. The
types.FunctionType(..., {}) line looks like it locks things down but does
not: CPython injects the real builtins module into any globals mapping with
no __builtins__ key. check_only_stdlib_imports does not help either, since
os, subprocess and socket are all stdlib, and its ok flag is computed and
then discarded by the reward functions.

The cell had spread to 35 files by copy paste. This switches the four
sources of truth to the library helper and regenerates the rest, so all of
them pick up the hardening in unslothai/unsloth-zoo#1105.

Sources of truth edited:

- original_template/gpt-oss-(20B)-GRPO.ipynb
- original_template/gpt-oss-(20B)_A100-GRPO.ipynb
- original_template/gpt_oss_(20B)_GRPO_BF16.ipynb
- nb/Gemma4_(E2B)_GRPO.ipynb, which has no original_template counterpart and
  is therefore its own source

The remaining 31 files are regenerated output from update_all_notebooks.py
and update_all_notebooks.py --amd.

check_only_stdlib_imports and the surrounding explanation are left alone.
They still drive the no_cheating reward, which is what they were written
for. The demo cells that exec a hardcoded sample string are also untouched,
since that is a literal rather than model output.

Repo wide grep for "exec(function" now returns nothing.
tests/test_python_scripts_regen_parity.py passes 888/888.

One note for whoever regenerates next: the molab step fails silently if
marimo is not installed. It prints a warning and the generator still exits
0, which leaves the four molab files stale while the run looks like it
succeeded.
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: 265c1133cf

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

The molab step in update_all_notebooks.py fails silently when marimo is
missing: it prints a warning and the run still exits 0. I installed marimo to
get past that, took the current release, and regenerated with 0.24.0 while the
workflow pins 0.23.8.

marimo stamps its version into __generated_with, which the workflow comment
already warns about, so all four files carried "0.24.0" against a repo whose
every other molab file says "0.23.8". test_molab_generation_parity regenerates
with the pinned version and compares, so this was a parity break waiting for
CI to reach it.

Regenerated with marimo==0.23.8. Measured against a clean worktree of main
rather than assuming: main fails 125 of these, this branch failed 126, and the
one extra was Gemma4_(E2B)_GRPO. It is now 122, so the branch introduces no
parity regression and repairs three of the pre-existing ones as a side effect,
those being the other molab files it regenerates.

The remaining 122 are pre-existing drift on main, untouched here.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: 7c25a48fa9

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit 495e25f into main Aug 26, 2026
7 of 8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant