Skip to content

Studio: build the blocked-command alternation once instead of per call - #10927

Merged
danielhanchen merged 2 commits into
mainfrom
speed-up-blocked-command-scan
Sep 14, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
speed-up-blocked-command-scan

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

_find_blocked_commands rebuilt its blocked-word regex on every command it screened. _BLOCKED_COMMANDS is a module-level frozenset assigned at import and never mutated, so that alternation is a compile-time constant and rebuilding it was pure waste: an re.escape per blocked name per command, then a fresh pattern string handed to re.findall for its cache to look up.

# before, inside _find_blocked_commands, per call
words_alt = "|".join(re.escape(w) for w in sorted(_BLOCKED_COMMANDS))
pattern = rf"...({words_alt})..."
blocked.update(re.findall(pattern, lowered))

It is the top entry when profiling the scan over a 2637 command corpus:

ncalls   tottime  cumtime
566856     0.194    0.463  re/__init__.py:305(escape)
583380     0.139    0.602  tools.py:1539(<genexpr>)

Measurement

Interleaved pairs against main, alternating trees to cancel host drift:

median min p99
main 101.0, 96.5, 108.4, 101.6, 97.6 us 90.7 to 94.4 us 240 to 414 us
this branch 86.0, 80.5, 87.8, 76.8, 86.3 us 74.2 to 82.1 us 211 to 257 us

About 15 percent off the median, and the branch wins all five pairs. The absolute figures run high because the host is under heavy load right now, so the pairing is the part worth reading, not the numbers themselves.

This is per command screened, not per token, so it changes nothing about inference throughput. It earns its place because the scan runs on every command a model issues.

Behaviour

No change, and not taken on trust. Verdicts were dumped on both sides for 1829 payloads covering the test corpus plus the command-position substitution shapes from #10860, path-qualified spellings (/usr/bin/rm, ./rm), the .exe variants, and ordinary commands. Zero differences.

tests/test_sandbox_tools.py tests/test_permission_mode.py
tests/test_bypass_permissions.py tests/test_windows_bash_shell.py
2199 passed, 5 skipped

ruff check and scripts/run_ruff_format.py both pass.

Note

The None branch is there because _BLOCKED_COMMANDS is built conditionally per platform (line 192), so an empty set stays representable rather than compiling an alternation with nothing in it.

Found while benchmarking #10860 before merge. Independent of that change and of #10907 and #10895, which touch different functions in the same file.

_find_blocked_commands rebuilt its blocked-word regex on every command it screened.
_BLOCKED_COMMANDS is a module-level frozenset assigned at import and never mutated,
so that alternation is a compile-time constant and the work was pure waste: an
re.escape per blocked name per command, then a fresh re.compile behind re.findall's
cache lookup.

It showed up as the top entry when profiling the scan over a 2637 command corpus,
567k calls to re.escape and 0.60s cumulative out of 3.9s. Hoisting it to a
module-level compile takes the median from about 101 us to about 86 us per command,
measured on interleaved pairs against main (the hoist wins all five pairs; absolute
figures run high because the host is loaded, so only the pairing is meaningful).

No behaviour change, and not taken on trust: verdicts were dumped for 1829 payloads
on both sides, covering the corpus plus the command-position substitution shapes
from #10860, path-qualified and .exe spellings, and ordinary commands. Zero
differences. tests/test_sandbox_tools.py, tests/test_permission_mode.py,
tests/test_bypass_permissions.py and tests/test_windows_bash_shell.py stay green
(2199 passed, 5 skipped).

This is per command screened, not per token, so it changes nothing about inference
throughput. It is worth doing because the scan runs on every command a model issues.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 14, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-14T13:39:07.013550Z f1c8b4d Manual request
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 14, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex security review

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for security reviews. Please try again later.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: f1c8b4d347

ℹ️ 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 added a commit to shimmyshimmer/unsloth-staging-4 that referenced this pull request Sep 14, 2026
danielhanchen added a commit to shimmyshimmer/unsloth-staging-4 that referenced this pull request Sep 14, 2026
@danielhanchen
danielhanchen merged commit c9e8c70 into main Sep 14, 2026
39 of 43 checks passed
@danielhanchen
danielhanchen deleted the speed-up-blocked-command-scan branch September 14, 2026 22:56
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