Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

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

Daniel Hanc9e8c7079d27
diff --git a/studio/backend/core/inference/tools.py b/studio/backend/core/inference/tools.py
index 481bb0e28b9..5f05021cf8d 100644
--- a/studio/backend/core/inference/tools.py
+++ b/studio/backend/core/inference/tools.py
@@ -1306,6 +1306,21 @@ def _is_start_title(token: str) -> bool:
)
+# `_BLOCKED_COMMANDS` is a never-mutated frozenset, so this alternation is a constant. Rebuilding it
+# per call cost an `re.escape` per blocked name: over a 2637 command corpus that was 567k calls and
+# 0.60s of 3.9s. None only if the set is empty.
+_BLOCKED_WORD_RE = (
+ re.compile(
+ r"(?:^|[;&|`\n(]\s*|[$]\(\s*|<\(\s*)"
+ r"(?:[\w./\\-]*/|[a-zA-Z]:[/\\][\w./\\-]*)?"
+ r"(" + "|".join(re.escape(w) for w in sorted(_BLOCKED_COMMANDS)) + r")"
+ r"(?:\.(?:exe|com|bat|cmd))?\b"
+ )
+ if _BLOCKED_COMMANDS
+ else None
+)
+
+
def _find_blocked_commands(command: str) -> set[str]:
"""Detect blocked commands at shell command position only.
@@ -1540,14 +1555,8 @@ def _find_blocked_commands(command: str) -> set[str]:
# Regex catches blocked words at command boundaries shlex misses: inside $(rm -rf), <(rm), backtick chains, or
# "foo;rm". Anchored to command-position delimiters, so it doesn't match in argument position.
lowered = command.lower()
- if _BLOCKED_COMMANDS:
- words_alt = "|".join(re.escape(w) for w in sorted(_BLOCKED_COMMANDS))
- pattern = (
- rf"(?:^|[;&|`\n(]\s*|[$]\(\s*|<\(\s*)"
- rf"(?:[\w./\\-]*/|[a-zA-Z]:[/\\][\w./\\-]*)?"
- rf"({words_alt})(?:\.(?:exe|com|bat|cmd))?\b"
- )
- blocked.update(re.findall(pattern, lowered))
+ if _BLOCKED_WORD_RE is not None:
+ blocked.update(_BLOCKED_WORD_RE.findall(lowered))
# A substitution at command position synthesizes the executed word, so `$(ls /usr/bin | grep
# "^reb")` never reaches the scan above; screen the body instead. A variable launders the same