Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(etoro): add runtime verify envelope to check_status

Ofek Shaked187ca273b38f
diff --git a/agent/src/api/live_routes.py b/agent/src/api/live_routes.py
index 9b4d81c15..40f355563 100644
--- a/agent/src/api/live_routes.py
+++ b/agent/src/api/live_routes.py
@@ -229,6 +229,7 @@ _ENVIRONMENT_IDENTITIES = frozenset(
"host_separated",
"read_only_no_runtime_discriminator",
"simulated_locally",
+ "path_separated_key_bound",
"trd_env_acc_list",
}
)
diff --git a/agent/src/trading/connectors/etoro/client.py b/agent/src/trading/connectors/etoro/client.py
index e1234bd52..af6a93c18 100644
--- a/agent/src/trading/connectors/etoro/client.py
+++ b/agent/src/trading/connectors/etoro/client.py
@@ -10,7 +10,6 @@ from __future__ import annotations
import json
import logging
-import os
import time
import uuid
from dataclasses import asdict, dataclass
@@ -20,6 +19,7 @@ from urllib.parse import urljoin
import requests
+from src.config.accessor import get_env_config
from src.config.paths import get_runtime_root
logger = logging.getLogger(__name__)
@@ -111,10 +111,14 @@ class EtoroConfig:
_OVERRIDE_KEYS = ("api_key", "user_key", "profile")
-_ENV_KEY_MAP = {
- "api_key": "ETORO_API_KEY",
- "user_key": "ETORO_USER_KEY",
-}
+
+
+def _environment_values() -> dict[str, str]:
+ data = get_env_config().data
+ return {
+ "api_key": str(data.etoro_api_key or "").strip(),
+ "user_key": str(data.etoro_user_key or "").strip(),
+ }
def build_config(
@@ -145,8 +149,7 @@ def load_config() -> EtoroConfig:
base = json.loads(path.read_text(encoding="utf-8"))
except (OSError, ValueError, json.JSONDecodeError) as exc:
raise EtoroConfigError(f"invalid eToro config at {path}: {exc}") from exc
- for field, env_name in _ENV_KEY_MAP.items():
- env_val = os.getenv(env_name, "").strip() # noqa: env-gate — connector credential helper
+ for field, env_val in _environment_values().items():
if env_val:
base[field] = env_val
if not base:
@@ -175,6 +178,30 @@ def _missing_fields(cfg: EtoroConfig) -> list[str]:
return missing
+def credential_source() -> str | None:
+ """Return where credentials were loaded from for runtime UI metadata."""
+ path = config_path()
+ file_present = False
+ if path.exists():
+ try:
+ data = json.loads(path.read_text(encoding="utf-8"))
+ if str(data.get("api_key") or "").strip() or str(data.get("user_key") or "").strip():
+ file_present = True
+ except (OSError, ValueError, json.JSONDecodeError):
+ file_present = False
+ if any(_environment_values().values()):
+ return "environment"
+ if file_present:
+ return "runtime_file"
+ return None
+
+
+def _missing_credential_code(cfg: EtoroConfig) -> str:
+ if cfg.api_key or cfg.user_key:
+ return "credentials_partial"
+ return "credentials_missing"
+
+
def public_config(cfg: EtoroConfig) -> dict[str, Any]:
return {
"profile": cfg.profile,
diff --git a/agent/src/trading/connectors/etoro/sdk.py b/agent/src/trading/connectors/etoro/sdk.py
index 2e6b40120..f2c805a06 100644
--- a/agent/src/trading/connectors/etoro/sdk.py
+++ b/agent/src/trading/connectors/etoro/sdk.py
@@ -2,6 +2,7 @@
from __future__ import annotations
+from datetime import datetime, timezone
from typing import Any
from src.trading.connectors.etoro.client import (
@@ -12,12 +13,15 @@ from src.trading.connectors.etoro.client import (
PAPER_GUARD,
aggregate_portfolio_path,
build_config,
+ credential_source,
info_root,
load_config,
make_client,
public_config,
save_config,
trade_history_root,
+ _missing_credential_code,
+ _missing_fields,
)
from src.trading.connectors.etoro.copy_trading import (
copy_close,
@@ -79,32 +83,38 @@ def _base_payload(cfg: EtoroConfig) -> dict[str, Any]:
def check_status(config: EtoroConfig | None = None) -> dict[str, Any]:
- from src.trading.connectors.etoro.client import _missing_fields
-
+ """Check read-only readiness with stable, redaction-safe diagnostics."""
cfg = config or load_config()
+ configured = not _missing_fields(cfg)
report: dict[str, Any] = {
"status": "ok",
+ "configured": configured,
+ "credential_source": credential_source(),
+ "connection_state": "connected",
+ "error_code": None,
+ "error": None,
"config": public_config(cfg),
"sdk": {"package": "requests", "installed": True},
"paper_guard": PAPER_GUARD,
"base_url": BASE_URL,
}
- missing_fields = _missing_fields(cfg)
- if missing_fields:
- report["status"] = "error"
- report["error"] = f"eToro connector not configured: missing {', '.join(missing_fields)}."
- return report
+ if not configured:
+ code = _missing_credential_code(cfg)
+ fields = ", ".join(_missing_fields(cfg))
+ message = (
+ f"eToro credentials are partial; missing fields: {fields}."
+ if code == "credentials_partial"
+ else f"eToro connector not configured: missing {fields}."
+ )
+ return _status_error(report, code, message)
try:
me = get_user_profile(cfg)
portfolio = get_account_snapshot(cfg)
except (EtoroConfigError, EtoroAPIError) as exc:
- report["status"] = "error"
- report["error"] = str(exc)
- return report
+ code = _connection_error_code(exc)
+ return _status_error(report, code, _connection_error_message(code, str(exc)))
except Exception as exc: # noqa: BLE001
- report["status"] = "error"
- report["error"] = f"eToro connector check failed: {exc}"
- return report
+ return _status_error(report, "broker_error", f"eToro connector check failed: {exc}")
report["account"] = {
"profile": cfg.profile,
"portfolio_status": portfolio.get("status"),
@@ -112,9 +122,48 @@ def check_status(config: EtoroConfig | None = None) -> dict[str, Any]:
"real_cid": me.get("realCid") or me.get("realCID"),
"demo_cid": me.get("demoCid") or me.get("demoCID"),
}
+ account = portfolio.get("account") if isinstance(portfolio.get("account"), dict) else {}
+ pnl = account.get("pnl") if isinstance(account.get("pnl"), dict) else {}
+ if pnl.get("account_current_pnl") is not None:
+ report["account"]["account_current_pnl"] = pnl.get("account_current_pnl")
+ report["last_checked_at"] = datetime.now(timezone.utc).isoformat()
return report
+def _status_error(report: dict[str, Any], code: str, message: str) -> dict[str, Any]:
+ if code in ("credentials_missing", "credentials_partial"):
+ report["configured"] = False
+ report.update(
+ status="error",
+ connection_state=(
+ "not_configured" if code in ("credentials_missing", "credentials_partial") else "error"
+ ),
+ error_code=code,
+ error=message,
+ )
+ return report
+
+
+def _connection_error_code(exc: Exception) -> str:
+ if isinstance(exc, (ConnectionError, TimeoutError, OSError)):
+ return "network_unreachable"
+ if isinstance(exc, EtoroAPIError):
+ text = str(exc).lower()
+ if any(token in text for token in ("401", "403", "auth", "unauthorized", "forbidden")):
+ return "authentication_failed"
+ if "network error" in text:
+ return "network_unreachable"
+ return "broker_error"
+
+
+def _connection_error_message(code: str, detail: str) -> str:
+ if code == "authentication_failed":
+ return "eToro authentication failed."
+ if code == "network_unreachable":
+ return "eToro Public API is unreachable."
+ return detail or "eToro broker request failed."
+
+
def get_user_profile(config: EtoroConfig | None = None) -> dict[str, Any]:
"""Return authenticated user profile and granted API scopes (`GET /api/v1/me`)."""
cfg = config or load_config()
diff --git a/agent/tests/test_etoro_connector.py b/agent/tests/test_etoro_connector.py
index 109b5afad..5ef4eb485 100644
--- a/agent/tests/test_etoro_connector.py
+++ b/agent/tests/test_etoro_connector.py
@@ -136,6 +136,58 @@ def test_credentials_redacted_in_public_config() -> None:
assert payload["api_key_configured"] is True
+def test_check_status_not_configured() -> None:
+ cfg = EtoroConfig(profile="live-readonly", api_key="", user_key="")
+ result = etoro_sdk.check_status(cfg)
+ assert result["status"] == "error"
+ assert result["configured"] is False
+ assert result["connection_state"] == "not_configured"
+ assert result["error_code"] == "credentials_missing"
+
+
+def test_check_status_partial_credentials() -> None:
+ cfg = EtoroConfig(profile="live-readonly", api_key="k", user_key="")
+ result = etoro_sdk.check_status(cfg)
+ assert result["error_code"] == "credentials_partial"
+ assert result["connection_state"] == "not_configured"
+
+
+def test_check_status_connected(monkeypatch) -> None:
+ cfg = EtoroConfig(profile="live-readonly", api_key="k", user_key="u")
+
+ def _transport(method: str, url: str, **kwargs: Any) -> _FakeResponse:
+ if url.endswith("/api/v1/me"):
+ return _FakeResponse(200, {"scopes": ["etoro-public:trade.real:read"], "realCid": 1})
+ if "/aggregate-portfolio" in url:
+ return _FakeResponse(200, {"accountTotals": {"accountCurrentPnl": 1.0}})
+ if url.endswith("/portfolio"):
+ return _FakeResponse(200, {"clientPortfolio": {"credit": 1.0}})
+ raise AssertionError(f"unexpected {method} {url}")
+
+ set_client_factory(lambda c: EtoroClient(cfg, transport=_transport))
+ result = etoro_sdk.check_status(cfg)
+ assert result["status"] == "ok"
+ assert result["configured"] is True
+ assert result["connection_state"] == "connected"
+ assert result["error_code"] is None
+ assert result["last_checked_at"]
+ assert "etoro-public:trade.real:read" in result["account"]["scopes"]
+
+
+def test_check_status_auth_failure_keeps_configured(monkeypatch) -> None:
+ cfg = EtoroConfig(profile="live-readonly", api_key="k", user_key="u")
+
+ def _transport(method: str, url: str, **kwargs: Any) -> _FakeResponse:
+ return _FakeResponse(401, {"error": "unauthorized"})
+
+ set_client_factory(lambda c: EtoroClient(cfg, transport=_transport))
+ result = etoro_sdk.check_status(cfg)
+ assert result["status"] == "error"
+ assert result["configured"] is True
+ assert result["connection_state"] == "error"
+ assert result["error_code"] == "authentication_failed"
+
+
def test_get_account_snapshot_uses_aggregate_portfolio_for_pnl(monkeypatch) -> None:
cfg = EtoroConfig(profile="live", api_key="k", user_key="u")
captured_urls: list[str] = []
diff --git a/agent/tests/test_etoro_runtime.py b/agent/tests/test_etoro_runtime.py
new file mode 100644
index 000000000..972cd8ca9
--- /dev/null
+++ b/agent/tests/test_etoro_runtime.py
@@ -0,0 +1,152 @@
+"""Tests for eToro connector runtime status surfaced through /live/status."""
+
+from __future__ import annotations
+
+from pathlib import Path
+
+import pytest
+from fastapi.testclient import TestClient
+
+import api_server
+
+pytestmark = pytest.mark.unit
+
+
+def _client(tmp_path: Path, monkeypatch) -> TestClient:
+ monkeypatch.setattr(Path, "home", classmethod(lambda cls: tmp_path), raising=False)
+ monkeypatch.setattr(api_server, "_runner_tasks", {}, raising=False)
+ monkeypatch.setattr(api_server, "_runner_factory", None, raising=False)
+ api_server._connector_verify_cache.clear()
+ return TestClient(api_server.app, client=("127.0.0.1", 50001))
+
+
+def test_live_status_includes_etoro_sdk_profile(tmp_path: Path, monkeypatch) -> None:
+ client = _client(tmp_path, monkeypatch)
+ response = client.get("/live/status")
+ assert response.status_code == 200
+ brokers = {b["auth"]["broker"]: b for b in response.json()["brokers"]}
+ assert "etoro" in brokers
+ etoro = brokers["etoro"]
+ assert etoro["auth"]["transport"] == "broker_sdk"
+ assert etoro["auth"]["is_live_broker"] is False
+ assert etoro["mandate"] is None
+
+
+def test_etoro_missing_credentials_maps_not_configured(
+ tmp_path: Path, monkeypatch
+) -> None:
+ client = _client(tmp_path, monkeypatch)
+ monkeypatch.setattr(
+ api_server,
+ "_check_connector_status",
+ lambda profile_id, force=False: {
+ "status": "error",
+ "configured": False,
+ "connection_state": "not_configured",
+ "error_code": "credentials_missing",
+ "error": "eToro connector not configured: missing api_key, user_key.",
+ "connector": "etoro",
+ "transport": "broker_sdk",
+ "profile_id": "etoro-live-sdk-readonly",
+ },
+ )
+ response = client.get("/live/status", params={"broker": "etoro"})
+ assert response.status_code == 200
+ auth = response.json()["brokers"][0]["auth"]
+ assert auth["broker"] == "etoro"
+ assert auth["connection_state"] == "not_configured"
+ assert auth["configured"] is False
+
+
+def test_etoro_valid_check_maps_connected(tmp_path: Path, monkeypatch) -> None:
+ client = _client(tmp_path, monkeypatch)
+ monkeypatch.setattr(
+ api_server,
+ "_check_connector_status",
+ lambda profile_id, force=False: {
+ "status": "ok",
+ "configured": True,
+ "connection_state": "connected",
+ "error": None,
+ "connector": "etoro",
+ "transport": "broker_sdk",
+ "profile_id": "etoro-live-sdk-readonly",
+ "sdk": {"package": "requests", "installed": True},
+ "paper_guard": "path_separated_key_bound",
+ "credential_source": "runtime_file",
+ "last_checked_at": "2026-08-09T12:00:00+00:00",
+ },
+ )
+ response = client.get("/live/status", params={"broker": "etoro"})
+ assert response.status_code == 200
+ auth = response.json()["brokers"][0]["auth"]
+ assert auth["connection_state"] == "connected"
+ assert auth["profile_id"] == "etoro-live-sdk-readonly"
+ assert auth["sdk_installed"] is True
+ assert auth["environment_identity"] == "path_separated_key_bound"
+ assert auth["capabilities"] == [
+ "account.read",
+ "positions.read",
+ "orders.read",
+ "quotes.read",
+ "history.read",
+ ]
+ assert auth["readonly"] is True
+
+
+def test_live_status_propagates_only_sanitized_etoro_metadata(
+ tmp_path: Path, monkeypatch
+) -> None:
+ client = _client(tmp_path, monkeypatch)
+ sentinels = {
+ "api_key": "SENTINEL_API_KEY",
+ "user_key": "SENTINEL_USER_KEY",
+ }
+ monkeypatch.setattr(
+ api_server,
+ "_check_connector_status",
+ lambda profile_id, force=False: {
+ "status": "ok",
+ "profile_id": profile_id,
+ "configured": True,
+ "credential_source": "runtime_file",
+ "sdk_installed": True,
+ "sdk": {"package": "requests", "installed": True},
+ "environment_identity": "path_separated_key_bound",
+ "paper_guard": "path_separated_key_bound",
+ "capabilities": ["account.read"],
+ "readonly": True,
+ "last_checked_at": "2026-08-09T12:00:00+00:00",
+ "error_code": None,
+ "connection_state": "connected",
+ "config": sentinels,
+ "error": f"raw failure: {sentinels['user_key']}",
+ },
+ )
+ response = client.get("/live/status", params={"broker": "etoro"})
+ assert response.status_code == 200
+ auth = response.json()["brokers"][0]["auth"]
+ assert "config" not in auth
+ assert all(secret not in response.text for secret in sentinels.values())
+
+
+def test_verify_endpoint_for_etoro_readonly_profile(
+ tmp_path: Path, monkeypatch
+) -> None:
+ client = _client(tmp_path, monkeypatch)
+ monkeypatch.setattr(
+ api_server,
+ "_check_connector_status",
+ lambda profile_id, force=False: {
+ "status": "ok",
+ "configured": True,
+ "connection_state": "connected",
+ "error": None,
+ "connector": "etoro",
+ "transport": "broker_sdk",
+ "profile_id": profile_id,
+ },
+ )
+ response = client.post("/live/connectors/etoro-live-sdk-readonly/verify")
+ assert response.status_code == 200
+ assert response.json()["connection_state"] == "connected"