"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(sandbox): block generated strategy code from importing the trading layer
Haozhe Wu50f92ba750a3
diff --git a/agent/backtest/runner.py b/agent/backtest/runner.pyindex d5c7b7b4d..1cadfc37b 100644--- a/agent/backtest/runner.py+++ b/agent/backtest/runner.py@@ -299,6 +299,43 @@ _FORBIDDEN_IMPORT_MODULES = frozenset("ctypes",})+# Project-internal subtrees that reach a broker. The red line is that no+# research or backtest path can arrive at a connector's ``place_order``, and+# until now that separation rested on the subprocess never being handed+# credentials rather than on the scanner refusing the import. It refuses now.+#+# These are matched on the dotted PREFIX rather than the root package, because+# the root is ``src`` — which also holds ``src.quantlib``, the finance-math+# layer strategies are explicitly meant to import (see the credit-analysis+# skill). Blocking the root would take that away to close this.+_FORBIDDEN_IMPORT_PREFIXES = (+ "src.trading",+ "src.live",+ "agent.src.trading",+ "agent.src.live",+ "trading.connectors",+ "live.sdk_order_gate",+)+++def _is_forbidden_module_path(name: str) -> bool:+ """Return True when a dotted module path names a forbidden module or subtree.++ Args:+ name: Dotted module path, e.g. ``socket`` or ``src.trading.service``.++ Returns:+ True when the root package is forbidden outright, or the path falls+ inside a forbidden subtree.+ """+ if not name:+ return False+ if name.split(".")[0] in _FORBIDDEN_IMPORT_MODULES:+ return True+ return any(+ name == prefix or name.startswith(prefix + ".")+ for prefix in _FORBIDDEN_IMPORT_PREFIXES+ )# ``os`` itself is allowed (os.path etc.), but these attributes shell out, spawn,# or read the process environment — none has a place in a signal engine._FORBIDDEN_OS_ATTRS = frozenset(@@ -345,6 +382,30 @@ def _attribute_root_name(node: ast.Attribute) -> str | None:return current.id if isinstance(current, ast.Name) else None+def _attribute_dotted_path(node: ast.Attribute) -> str | None:+ """Rebuild the dotted path of an attribute chain (``a.b.c`` -> ``a.b.c``).++ The root name alone cannot decide a forbidden-subtree check: ``src`` is+ shared by the blocked ``src.trading`` and the permitted ``src.quantlib``.++ Args:+ node: Attribute node at the end of the chain.++ Returns:+ The dotted path, or None when the chain is not rooted in a plain name+ (e.g. it starts from a call or subscript).+ """+ parts: list[str] = []+ current: ast.AST = node+ while isinstance(current, ast.Attribute):+ parts.append(current.attr)+ current = current.value+ if not isinstance(current, ast.Name):+ return None+ parts.append(current.id)+ return ".".join(reversed(parts))++def _reject_forbidden_open(node: ast.Call) -> None:"""Reject ``open()`` used to write files or read a non-relative-literal path."""func = node.func@@ -394,6 +455,9 @@ def _reject_forbidden_getattr(node: ast.Call) -> None:root: str | None = target.idelif isinstance(target, ast.Attribute):root = _attribute_root_name(target)+ dotted = _attribute_dotted_path(target)+ if dotted and _is_forbidden_module_path(dotted):+ raise ValueError(f"{func.id}() indirection onto {dotted!r} {_SCRUB_MSG}")else:root = Noneif root == "os" or root in _FORBIDDEN_IMPORT_MODULES:@@ -404,12 +468,18 @@ def _reject_forbidden_node(node: ast.AST) -> None:"""Raise ``ValueError`` if a single AST node performs a forbidden operation."""if isinstance(node, ast.Import):for alias in node.names:- if alias.name.split(".")[0] in _FORBIDDEN_IMPORT_MODULES:+ if _is_forbidden_module_path(alias.name):raise ValueError(f"Import of {alias.name!r} {_SCRUB_MSG}")elif isinstance(node, ast.ImportFrom):- root = (node.module or "").split(".")[0]- if root in _FORBIDDEN_IMPORT_MODULES:+ module = node.module or ""+ root = module.split(".")[0]+ if _is_forbidden_module_path(module):raise ValueError(f"Import from {node.module!r} {_SCRUB_MSG}")+ # "from src.trading import service" names the subtree in the alias,+ # not the module, so the prefix check above cannot see it.+ for alias in node.names:+ if _is_forbidden_module_path(f"{module}.{alias.name}" if module else alias.name):+ raise ValueError(f"Import of {module}.{alias.name!r} {_SCRUB_MSG}")if root == "os":for alias in node.names:if _is_forbidden_os_attr(alias.name):@@ -418,6 +488,9 @@ def _reject_forbidden_node(node: ast.AST) -> None:root = _attribute_root_name(node)if root in _FORBIDDEN_IMPORT_MODULES:raise ValueError(f"Use of {root}.{node.attr} {_SCRUB_MSG}")+ dotted = _attribute_dotted_path(node)+ if dotted and _is_forbidden_module_path(dotted):+ raise ValueError(f"Use of {dotted} {_SCRUB_MSG}")if root == "os" and _is_forbidden_os_attr(node.attr):raise ValueError(f"Use of os.{node.attr} {_SCRUB_MSG}")elif isinstance(node, ast.Name):diff --git a/agent/tests/test_backtest_runner_security.py b/agent/tests/test_backtest_runner_security.pyindex c1c26de84..6dc65f8b0 100644--- a/agent/tests/test_backtest_runner_security.py+++ b/agent/tests/test_backtest_runner_security.py@@ -111,6 +111,41 @@ _FORBIDDEN_IN_METHOD_BODY = [("urllib_urlopen", [" import urllib.request as u", " return u.urlopen('http://x')"]),("open_write", [" open('evil.txt', 'w').write('x')", " return []"]),("open_abs_read", [" return open('/etc/passwd').read()"]),+ # The red line: no research or backtest path may reach a connector's+ # place_order. The subprocess is handed the agent root on PYTHONPATH, so+ # the separation has to be enforced by the scanner, not by hoping the+ # module is unimportable.+ (+ "import_trading_service",+ [" import src.trading.service", " return []"],+ ),+ (+ "from_trading_service_import_place_order",+ [" from src.trading.service import place_order", " return []"],+ ),+ (+ "from_trading_import_service",+ [" from src.trading import service", " return []"],+ ),+ (+ "import_broker_connector",+ [" import src.trading.connectors.okx.sdk", " return []"],+ ),+ (+ "import_live_order_gate",+ [" from src.live.sdk_order_gate import check", " return []"],+ ),+ (+ "trading_attribute_chain",+ [" import src", " return src.trading.service.place_order(1)"],+ ),+ (+ "getattr_indirection_onto_trading",+ [+ " import src",+ " return getattr(src.trading.service, 'place_order')(1)",+ ],+ ),]@@ -133,6 +168,40 @@ def test_signal_engine_rejects_forbidden_op_in_method_body(tmp_path, case_id, bo_load_module_from_file(signal_file, _module_name())+ "import_line",+ [+ " from src.quantlib.fixedincome import bond_price",+ " import src.quantlib.credit",+ " from src.factors.registry import get_factor",+ ],+ ids=["quantlib_from", "quantlib_import", "factors_registry"],+)+def test_signal_engine_still_allows_project_math_imports(tmp_path, import_line) -> None:+ """Blocking the broker subtree must not cost strategies the math layer.++ ``src.trading`` and ``src.quantlib`` share a root package, so a root-level+ block would close the red-line gap by taking away the finance-math layer+ strategies are explicitly meant to import. The prefix match keeps them+ apart; this pins that it stays that way.+ """+ signal_file = tmp_path / "signal_engine.py"+ signal_file.write_text(+ "\n".join(+ [+ '"""Generated signal engine."""',+ "class SignalEngine:",+ " def generate(self, *args, **kwargs):",+ import_line,+ " return []",+ ]+ ),+ encoding="utf-8",+ )++ _load_module_from_file(signal_file, _module_name())++def test_signal_engine_rejects_forbidden_op_in_transitively_called_helper(tmp_path) -> None:# Payload hidden in a module-level helper that generate() calls — the# reachability walk must follow the call and reject it.diff --git a/agent/tests/test_paper_capped_connectors_refuse_live.py b/agent/tests/test_paper_capped_connectors_refuse_live.pynew file mode 100644index 000000000..fb26d5d07--- /dev/null+++ b/agent/tests/test_paper_capped_connectors_refuse_live.py@@ -0,0 +1,126 @@+"""Structural gate: paper-capped connectors refuse a live order before anything else.++``docs/paper-trading/PLAN.md`` promised a CRITICAL grep gate called+``test_paper_adapters_no_live_endpoints.py`` over ``src/paper_trading/adapters/``.+That layer never shipped — the connector architecture replaced it — so the gate+was never written against anything real, while two other documents went on+citing it as though it existed. This is that gate, against what did ship.++The invariant under test is the red line in CLAUDE.md: a broker exposing no+runtime paper/live discriminator is capped at paper and its order entry points+refuse a non-paper config *before* reaching the broker SDK. Checking the refusal+happens early is the whole point — a guard placed after the network call would+still pass a behavioural test that only asserts "it raised".++The connector list is derived from the filesystem, so adding a broker fails this+test until someone classifies it. That is the anti-drift property; do not+replace the enumeration with a hardcoded list.+"""++from __future__ import annotations++import ast+from pathlib import Path++import pytest++_CONNECTOR_ROOT = Path(__file__).resolve().parents[1] / "src" / "trading" / "connectors"++# Brokers with no runtime paper/live discriminator. Capped at paper; every+# order entry point must refuse a non-paper config up front.+_PAPER_CAPPED = frozenset({"longbridge", "dhan", "shoonya", "trading212"})++# Brokers with a structural discriminator (host separation, demo flag, account+# id format, trade environment). Live placement is allowed here, but only+# through the mandate gate — which `test_backtest_runner_security.py` and the+# order-guard suites cover; this gate deliberately does not re-assert it.+_LIVE_CAPABLE = frozenset(+ {"alpaca", "binance", "futu", "okx", "tiger", "robinhood", "ibkr", "mt5"}+)++_ORDER_ENTRY_POINTS = ("place_order", "cancel_order")++# A guard is a paper/environment test. Matching on the source segment keeps this+# readable and independent of how each connector spells its config object.+_GUARD_MARKERS = ("is_paper", 'environment != "paper"', "environment != 'paper'")++# How many leading statements may precede the guard. One is needed because most+# connectors must resolve `config or load_config()` before they can inspect it.+_MAX_STATEMENTS_BEFORE_GUARD = 1+++def _connector_names() -> list[str]:+ """Return every connector package name found on disk."""+ return sorted(+ path.name+ for path in _CONNECTOR_ROOT.iterdir()+ if path.is_dir() and not path.name.startswith("__")+ )+++def _order_functions(sdk_path: Path) -> dict[str, tuple[ast.FunctionDef, str]]:+ """Return the order entry points defined in a connector's ``sdk.py``."""+ source = sdk_path.read_text(encoding="utf-8")+ tree = ast.parse(source)+ found: dict[str, tuple[ast.FunctionDef, str]] = {}+ for node in tree.body:+ if isinstance(node, ast.FunctionDef) and node.name in _ORDER_ENTRY_POINTS:+ found[node.name] = (node, source)+ return found+++def _executable_body(func: ast.FunctionDef) -> list[ast.stmt]:+ """Return the function body with the docstring removed."""+ return [+ stmt+ for stmt in func.body+ if not (isinstance(stmt, ast.Expr) and isinstance(stmt.value, ast.Constant))+ ]+++def test_every_connector_is_classified() -> None:+ """A new broker must be classified before it can ship.++ This is the drift alarm. If it fails, decide whether the new connector has+ a runtime paper/live discriminator and add it to exactly one set above.+ """+ unclassified = set(_connector_names()) - _PAPER_CAPPED - _LIVE_CAPABLE++ assert not unclassified, (+ f"Unclassified broker connector(s): {sorted(unclassified)}. Add each to "+ "_PAPER_CAPPED (no runtime paper/live discriminator) or _LIVE_CAPABLE "+ "(has one). Do not guess — check how the connector distinguishes paper "+ "from live at runtime."+ )++[email protected]("broker", sorted(_PAPER_CAPPED))+def test_paper_capped_connector_refuses_non_paper_before_the_sdk(broker: str) -> None:+ """Each capped broker's order entry points guard on paper up front."""+ sdk_path = _CONNECTOR_ROOT / broker / "sdk.py"+ assert sdk_path.is_file(), f"{broker} has no sdk.py"++ functions = _order_functions(sdk_path)+ assert functions, f"{broker}/sdk.py defines no order entry point"++ for name, (func, source) in sorted(functions.items()):+ body = _executable_body(func)+ assert body, f"{broker}.{name} has an empty body"++ guard_index = None+ for index, stmt in enumerate(body):+ segment = ast.get_source_segment(source, stmt) or ""+ if any(marker in segment for marker in _GUARD_MARKERS):+ guard_index = index+ break++ assert guard_index is not None, (+ f"{broker}.{name} has no paper/environment guard. This broker is "+ "capped at paper because it exposes no runtime paper/live "+ "discriminator, so a non-paper config must be refused here."+ )+ assert guard_index <= _MAX_STATEMENTS_BEFORE_GUARD, (+ f"{broker}.{name} checks for paper at statement {guard_index + 1}, "+ f"after {guard_index} other statement(s). The guard must come first "+ "so nothing reaches the broker SDK before the refusal."+ )