Skip to content

fix(studio): prevent terminal tool hangs in credential scan (#12048) - #12087

Merged
danielhanchen merged 20 commits into
unslothai:mainfrom
Souravrajvi0:feat/terminal-credential-scan-freeze-28a7
Sep 28, 2026
Merged

danielhanchen merged 20 commits into
unslothai:mainfrom
Souravrajvi0:feat/terminal-credential-scan-freeze-28a7

Conversation

@Souravrajvi0

@Souravrajvi0 Souravrajvi0 commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #12048 — terminal tool calls could stay in Running indefinitely (past Max Tool Call Duration) because the pre-dispatch studio credential guard could recurse without bound while expanding shell assignments in quoted strings (e.g. echo "A=$A B=$B"). That work runs synchronously on the backend event loop and holds the GIL, so no tool result log line is emitted and the UI never receives completion.

Changes

  • Resolve self-references against an earlier binding or an empty value while preserving concrete path pieces, including supported parameter-operator forms.
  • Bound recursive assignment expansion in _references_studio_credential_here, allowing long finite alias chains and refusing commands when expansion exhausts its budget or grows excessively.
  • Scan quoted assignment-like text separately so log messages cannot replace real bindings, while retaining detection in quoted shell programs.
  • Evaluate default and alternate self-references using the prior binding, distinguishing unset and empty values.
  • Add regression tests for quoted self-references, long credential and harmless alias chains, scan exhaustion, growing unresolved assignments, and concrete paths combined with self-references.

Testing

  • test_two_self_referential_assignments_in_one_quoted_string_do_not_hang in test_studio_auth_guard.py

  • Manual smoke: _references_studio_credential_here on ordinary echo/cat commands completes in <1ms

  • Repair verification: 235 focused tests passed on Python 3.13.14/Linux x86_64; live synthetic auth.db reads were refused in normal and Bypass Permissions execution. Verification details and retained earlier test evidence.

Two self-referential VAR=$VAR assignments inside one quoted string could
expand without a fixed point and recurse indefinitely in the pre-dispatch
credential guard, freezing the backend. Skip self-referential env captures
and cap assignment-expansion depth during the scan.
@danielhanchen

Copy link
Copy Markdown
Member

Reproduced this on main, where echo "hb=$hb fl=$fl" keeps growing a trailing quote in _expand_shell_assignments and the credential scan never returns, so the self-reference skip plus the depth cap covers the terminal hang in #12048. Will get this reviewed.

@mahiatlinux

mahiatlinux commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

Repaired at 34b1abf.

The quoted self-reference command now finishes, while credential reads hidden behind long alias chains, concrete self-reference path fragments, or assignment-shaped log messages are refused. Assignment expansion has a finite budget and fails closed if it cannot finish. Quoted inline shell programs remain covered. Default and alternate parameter operators use the prior binding and distinguish unset from empty values.

235 focused tests passed on Python 3.13.14/Linux x86_64, including the Studio auth guard suite and retained independent cases. Negative controls exposed the defects before each repair. All 18 default/alternate operator cases also agree with real Bash path expansion. Live terminal reads against an initialized database containing only a synthetic value now refuse the alias, self-reference, quoted-log, and parameter-operator bypasses in normal software-safeguard and Bypass Permissions modes, with no synthetic value in model output. Repository formatting, Ruff and diff checks passed.

The earlier Python 3.11/3.13/3.14, fuzz, concurrency, streaming and timeout qualification was reused as requested; the broad matrix was not rerun. The separate Python 3.14 long-expression recursion failure also occurs on base and remains unchanged. The previously noted prior-default limitation is resolved by the parameter-operator correction above.

Verification details, before/after execution results and reproducers. Review mirror.

@danielhanchen danielhanchen self-assigned this Sep 28, 2026
@unslothai unslothai deleted a comment from mahiatlinux Sep 28, 2026
@unslothai unslothai deleted a comment from mahiatlinux Sep 28, 2026
@unslothai unslothai deleted a comment from mahiatlinux Sep 28, 2026
danielhanchen added a commit to Datta0/unsloth-staging-3 that referenced this pull request Sep 28, 2026
danielhanchen added a commit to Datta0/unsloth-staging-3 that referenced this pull request Sep 28, 2026
@danielhanchen

danielhanchen commented Sep 28, 2026 •

Copy link
Copy Markdown
Member

Review record for #12087, carried over from the mirror it was reviewed on: danielhanchen/unsloth-staging-review#181.

Findings are quoted as posted and attributed to the account that posted them. The reactions shown are the triage recorded on the mirror; none were re-applied here. Commits are named as the mirror's, with this PR's equivalent where one was found.

Round 1: reviewed mirror 7bc62aa73 (no equivalent commit found on this PR)

chatgpt-codex-connector[bot] (on the mirror)

🛡️ Codex Security Review

Here are some automated security review suggestions for this pull request.

Reviewed commit: 7bc62aa735

No triage reaction was recorded on the mirror

Round 2: reviewed mirror fc03ec2b1 (no equivalent commit found on this PR)

chatgpt-codex-connector[bot] on studio/backend/core/inference/tools.py (on the mirror)

@@ -5318,10 +5406,61 @@ def repl_indirect(m):
         pointed = env.get(m.group(1))
         return env.get(pointed, m.group(0)) if pointed is not None else m.group(0)
 
-    command = _SHELL_PARAM_INDIRECT_RE.sub(repl_indirect, command)
-    command = _SHELL_PARAM_REPL_RE.sub(repl_pattern, command)
-    command = _SHELL_PARAM_CASE_RE.sub(repl_case, command)
-    return _SHELL_VAR_RE.sub(lambda m: env.get(m.group(1) or m.group(2), m.group(0)), command)
+    def expand(text):
+        if "$" not in text:
+            return text
+        text = _SHELL_PARAM_INDIRECT_RE.sub(repl_indirect, text)
+        text = _SHELL_PARAM_REPL_RE.sub(repl_pattern, text)
+        text = _SHELL_PARAM_CASE_RE.sub(repl_case, text)
+        return _SHELL_VAR_RE.sub(lambda m: env.get(m.group(1) or m.group(2), m.group(0)), text)
+
+    # Positional: each use sees the binding active where it stands; the last binding covers loops.
+    pieces, pos = [], 0
+    matches = list(_SHELL_ASSIGN_RE.finditer(command))
+    # A run of assignments is a command prefix only when a command word, not a separator, ends it:
+    # `A=1 B=2; echo $A` binds both in the shell, `A=1 B=2 echo $A` binds neither for the argument.
+    prefix = [False] * len(matches)
+    for i in range(len(matches) - 1, -1, -1):
+        m = matches[i]
+        if (quote_states is None or not quote_states[m.start(1)]) and (
+            _assignment_is_a_command_prefix(command, m.start(2))

P2 Badge Treat redirection-only assignments as persistent

When an assignment-only command has a redirection, such as x=../..; x=./project >out; cat "$x/auth/auth.db", Bash persists the second assignment in the current shell, so the final cat reads the project path. _assignment_is_a_command_prefix treats >out as a following command, causing the skip_prefix expansion to ignore the safe rebind and retain ../..; the credential guard therefore rejects an otherwise harmless command in every execution mode. Distinguish redirections from an actual command word when marking prefix assignments.

Useful? React with 👍 / 👎.

Rejected on the mirror

Reply from danielhanchen:

Declining: this is a fail-closed over-refusal of an assignment whose only following word is a redirection, which ordinary tool calls do not produce; treating it as a prefix keeps the outer binding and can only refuse, never allow, a credential read.

Round 3: reviewed mirror 9c17c94e5 (no equivalent commit found on this PR)

chatgpt-codex-connector[bot] (on the mirror)

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: 9c17c94e55

No triage reaction was recorded on the mirror

Verdict

The round converged at mirror 9c17c94e5 (no equivalent commit found on this PR).

This is the review as it stood at that commit. Anything pushed to this PR afterwards was not part of it.

danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Sep 28, 2026
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Sep 28, 2026
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Sep 28, 2026
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Sep 28, 2026
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Sep 28, 2026
@danielhanchen
danielhanchen merged commit 1499c15 into unslothai:main Sep 28, 2026
36 of 44 checks passed
shaojun pushed a commit to shaojun/unsloth that referenced this pull request Sep 29, 2026
…i#12048) (unslothai#12087)

* fix(studio): bound shell assignment expansion in credential scan

Two self-referential VAR=$VAR assignments inside one quoted string could
expand without a fixed point and recurse indefinitely in the pre-dispatch
credential guard, freezing the backend. Skip self-referential env captures
and cap assignment-expansion depth during the scan.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* fix incomplete credential assignment scans failing open

* preserve concrete paths in self-referential shell assignments

* separate quoted assignment scans from shell bindings

* respect prior bindings in self-referential parameter operators

* Keep self-referential studio home assignments naming the install

* Trim comments in the credential scan fix

* Keep self-doubling shell assignments from growing without bound

* Keep scoped empty assignments from erasing an outer binding

* Scan each variable use with the binding active at that point

* Trim comments after review

* Walk shell assignments once for both expansion orders

* Let a top-level empty assignment clear the binding

* Expand arguments with the outer binding past a command-prefix assignment

* Treat assignment-shaped arguments as binding nothing

* Compute assignment scope in one pass so many empty assignments scan in linear time

* Trim comments after review

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: mahiatlinux <[email protected]>
Co-authored-by: Daniel Han <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Tool Calls Completely Halt Randomly (Stuck in 'Running' State) Even Past Max Tool Call Duration

3 participants