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 Terminal: give the heredoc quote scanner its own substitution helper (#17442)

Daniel Han322c4c1a5054
diff --git a/studio/backend/core/inference/tools.py b/studio/backend/core/inference/tools.py
index fe4782781da..b80108c090f 100644
--- a/studio/backend/core/inference/tools.py
+++ b/studio/backend/core/inference/tools.py
@@ -6723,7 +6723,7 @@ def _quote_end(line: str, i: int) -> int:
if quote == '"' and line[j] == "\\":
j += 2
elif quote == '"' and line.startswith("$(", j):
- j = _substitution_end(line, j + 2)
+ j = _quoted_substitution_end(line, j + 2)
if j < 0:
return -1
elif quote == '"' and line[j] == "`":
@@ -6735,7 +6735,7 @@ def _quote_end(line: str, i: int) -> int:
return j if j < n else -1
-def _substitution_end(line: str, j: int) -> int:
+def _quoted_substitution_end(line: str, j: int) -> int:
depth, n = 1, len(line)
while j < n:
ch = line[j]
diff --git a/studio/backend/tests/test_permission_mode.py b/studio/backend/tests/test_permission_mode.py
index d14dde9e4ba..f6d033a1b96 100644
--- a/studio/backend/tests/test_permission_mode.py
+++ b/studio/backend/tests/test_permission_mode.py
@@ -1226,6 +1226,21 @@ def test_code_fed_to_an_interpreter_through_a_heredoc_is_screened(command, high_
assert is_high_risk_tool_call("terminal", {"command": command}) is high_risk
+def test_the_heredoc_quote_scanner_keeps_its_own_substitution_helper():
+ # tools.py also defines _substitution_end for the Windows nul rewrite (index of the ")" and len()
+ # when unterminated); a second definition of that name silently replaced this one module-wide.
+ from core.inference import tools
+
+ assert tools._quoted_substitution_end("a)", 0) == 2
+ assert tools._quoted_substitution_end("(b))", 0) == 4
+ assert tools._quoted_substitution_end("a", 0) == -1
+ assert tools._quote_end('"$(echo "a)")"', 0) == 13
+ # An unterminated quote inside the substitution keeps the whole string unterminated.
+ assert tools._quote_end('"( )\'$("`")"', 0) == -1
+ assert tools._substitution_end("a)", 0) == 1
+ assert tools._substitution_end("a", 0) == 1
+
+
def test_heredoc_option_scan_is_linear():
started = time.perf_counter()
is_high_risk_tool_call("terminal", {"command": "python3" + " -Xa" * 40 + " x"})