unsloth/studio/backend/tests/test_rag_loopback_trust_env.py
Daniel Han 0ea727a0b2
Studio RAG: disable trust_env on loopback llama-server httpx clients (#6775)
* Studio RAG: disable trust_env on loopback llama-server httpx clients

The RAG embedder health probe (embed_llama_server.py), its pooled httpx.Client, and the vision captioner (captioner.py) call the local 127.0.0.1 llama-server with httpx's default trust_env=True, so an ambient HTTP(S)_PROXY that returns 503 for loopback breaks embedder startup and captioning. Set trust_env=False on these loopback clients, matching the existing fix on the main llama_cpp and inference clients. External provider calls are untouched.

Follow-up to the loopback trust_env fix; covers the remaining local llama-server clients in the RAG path.

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

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

* Studio RAG tests: accept trust_env kwarg in captioner httpx.post mocks

The loopback captioner now passes trust_env=False; update the _vision_complete
fake_post stubs to accept it and assert it is False.

* Trim comments in Studio RAG trust_env fix (comment-only)

* RAG trust_env test: explicit UTF-8 read + scan all package .py files

Addresses review: utf-8 open avoids a Windows decode error, and scanning every
.py in core/rag catches any future file that adds an httpx call.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2026-07-01 03:58:08 -07:00

52 lines
1.9 KiB
Python

"""AST test locking in the RAG loopback trust_env fix: every httpx client/call in the RAG
package (all target the local 127.0.0.1 llama-server) must set trust_env=False."""
import ast
import os
RAG_DIR = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "core", "rag")
HTTPX_CALLEES = {"get", "post", "stream", "request", "Client", "AsyncClient"}
def _httpx_calls(path):
with open(path, encoding = "utf-8") as f:
tree = ast.parse(f.read(), filename = path)
calls = []
for node in ast.walk(tree):
if not isinstance(node, ast.Call):
continue
func = node.func
if (
isinstance(func, ast.Attribute)
and func.attr in HTTPX_CALLEES
and isinstance(func.value, ast.Name)
and func.value.id == "httpx"
):
calls.append(node)
return calls
def _sets_trust_env_false(call):
for kw in call.keywords:
if kw.arg == "trust_env" and isinstance(kw.value, ast.Constant) and kw.value.value is False:
return True
return False
def test_rag_loopback_httpx_clients_disable_trust_env():
# Scan every .py in the package so a new file with an httpx call can't bypass this.
checked = 0
for fname in sorted(f for f in os.listdir(RAG_DIR) if f.endswith(".py")):
path = os.path.join(RAG_DIR, fname)
for call in _httpx_calls(path):
checked += 1
assert _sets_trust_env_false(call), (
f"httpx.{call.func.attr} at {fname}:{call.lineno} must set trust_env=False "
f"(loopback llama-server client must not honor ambient HTTP(S)_PROXY)"
)
assert checked >= 3, f"expected at least 3 loopback httpx calls, found {checked}"
if __name__ == "__main__":
test_rag_loopback_httpx_clients_disable_trust_env()
print("OK: all RAG loopback httpx clients set trust_env=False")