mirror of
https://github.com/NicolasBohn/NexQuant.git
synced 2026-08-07 03:57:45 +00:00
Compare commits
28 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 13cbd42ecf | |||
| 64ed6b0cce | |||
| bf36f54159 | |||
| 79f1d34083 | |||
| 150a818e07 | |||
| b6d1caecc9 | |||
| 73e600bf25 | |||
| 9960633d01 | |||
| 3522a2eca1 | |||
| a5f091f1ca | |||
| 528d470754 | |||
| 910fbea27e | |||
| ab3f5f111d | |||
| a910d70d40 | |||
| 31a75eeb07 | |||
| 11f5dadd2d | |||
| 51a624c31e | |||
| 9947ea3928 | |||
| bc96d26371 | |||
| c6e8f3d3a3 | |||
| 35d2b81158 | |||
| 4fd5117af6 | |||
| 96d6923433 | |||
| ef12b33aca | |||
| a1e9417658 | |||
| a65ab828c4 | |||
| 840e12e6aa | |||
| 1d1b7b6984 |
@@ -12,7 +12,7 @@ jobs:
|
|||||||
release-please:
|
release-please:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: googleapis/release-please-action@v4
|
- uses: googleapis/release-please-action@v5
|
||||||
with:
|
with:
|
||||||
token: ${{ secrets.GITHUB_TOKEN }}
|
token: ${{ secrets.GITHUB_TOKEN }}
|
||||||
config-file: release-please-config.json
|
config-file: release-please-config.json
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
{
|
{
|
||||||
".": "1.3.5"
|
".": "1.3.9"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,46 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
## [1.3.9](https://github.com/TPTBusiness/Predix/compare/v1.3.8...v1.3.9) (2026-05-01)
|
||||||
|
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* **security:** resolve path-injection, B701, B101, B112 Bandit alerts ([20b89a0](https://github.com/TPTBusiness/Predix/commit/20b89a061843b39836e975f158404e8e2d4627cd))
|
||||||
|
|
||||||
|
## [1.3.8](https://github.com/TPTBusiness/Predix/compare/v1.3.7...v1.3.8) (2026-04-30)
|
||||||
|
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* **deps:** relax aiohttp constraint to >=3.13.4 for litellm compatibility ([34ab192](https://github.com/TPTBusiness/Predix/commit/34ab1923a887089eb36e5cbad6cb8df16f0333ca))
|
||||||
|
* **qlib:** correct indentation in except blocks in quant_proposal and factor_runner ([8143451](https://github.com/TPTBusiness/Predix/commit/8143451e8c0ead01c4d86d19669268c7bfb15fac))
|
||||||
|
* **security:** replace eval() with ast.literal_eval in finetune validator (B307) ([0508caf](https://github.com/TPTBusiness/Predix/commit/0508caf9140d210b823fefefa28ee535ec85a0ae))
|
||||||
|
* **security:** replace shell=True subprocess calls with list args in env.py (B602) ([2012d5a](https://github.com/TPTBusiness/Predix/commit/2012d5ae4e77cc2f1ab9a48beaaac5a74695d083))
|
||||||
|
* **security:** resolve path-injection and add nosec for safe temp paths (B108, py/path-injection) ([6727480](https://github.com/TPTBusiness/Predix/commit/67274803bd1d14e5d1df9a063f46b2edb8501a2b))
|
||||||
|
|
||||||
|
## [1.3.7](https://github.com/TPTBusiness/Predix/compare/v1.3.6...v1.3.7) (2026-04-30)
|
||||||
|
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* **security:** nosec for B608/B701 false positives in UI and template code ([5eb5d7e](https://github.com/TPTBusiness/Predix/commit/5eb5d7e8fdbe90e0dced83fef4e09f5a33e96b2b))
|
||||||
|
* **security:** replace eval() with ast.literal_eval and add request timeouts (B307, B113) ([3301ada](https://github.com/TPTBusiness/Predix/commit/3301ada697ca7d3afa1a188d2a76a87ae98b4529))
|
||||||
|
* **security:** replace shell=True subprocess calls with list args (B602) ([13c08f4](https://github.com/TPTBusiness/Predix/commit/13c08f4ce6813eb7c314087921ec8c0f40074bd7))
|
||||||
|
|
||||||
|
## [1.3.6](https://github.com/TPTBusiness/Predix/compare/v1.3.5...v1.3.6) (2026-04-30)
|
||||||
|
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* **security:** real fix for B110 (logging in factor_proposal.py [#746](https://github.com/TPTBusiness/Predix/issues/746)) ([16624e0](https://github.com/TPTBusiness/Predix/commit/16624e0bd966ae4d24c4a3eb42bbc31c11da3136))
|
||||||
|
* **security:** real fix for B110 (logging in factor_runner.py [#744](https://github.com/TPTBusiness/Predix/issues/744)) ([88cf0fb](https://github.com/TPTBusiness/Predix/commit/88cf0fb8828b11c97f2f3ae2881a4900b020c6f0))
|
||||||
|
* **security:** real fix for B110 (logging in quant_proposal.py [#741](https://github.com/TPTBusiness/Predix/issues/741)) ([7cf2a64](https://github.com/TPTBusiness/Predix/commit/7cf2a644f553b054bd4b0607ea51e5372e68d90a))
|
||||||
|
* **security:** real fix for B110 (logging in quant_proposal.py [#741](https://github.com/TPTBusiness/Predix/issues/741)) ([ef985f8](https://github.com/TPTBusiness/Predix/commit/ef985f86035d8dca707c60137e6508349a0c4ae6))
|
||||||
|
* **security:** real fix for B404/B603 (sys.executable in factor_runner.py [#745](https://github.com/TPTBusiness/Predix/issues/745)) ([819655a](https://github.com/TPTBusiness/Predix/commit/819655aaa3efa76596d60501d0e8ca365df3e5e2))
|
||||||
|
* **security:** revert broken read_pickle encoding arg in kaggle template (B301) ([3574907](https://github.com/TPTBusiness/Predix/commit/35749073c91e69f63ddaad61dae3f2b799327e63))
|
||||||
|
* **security:** validate SQL identifiers in _add_column_if_not_exists (B608) ([e10dfa2](https://github.com/TPTBusiness/Predix/commit/e10dfa2576038e911f83595d3b466c261bc0cd54))
|
||||||
|
* **security:** whitelist-validate metric column in get_top_factors (B608) ([e50519f](https://github.com/TPTBusiness/Predix/commit/e50519fe066e68aec2f19b83df4f643c3c22053d))
|
||||||
|
|
||||||
## [1.3.5](https://github.com/TPTBusiness/Predix/compare/v1.3.4...v1.3.5) (2026-04-27)
|
## [1.3.5](https://github.com/TPTBusiness/Predix/compare/v1.3.4...v1.3.5) (2026-04-27)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -18,6 +18,8 @@ load_dotenv(Path(__file__).parent / ".env")
|
|||||||
import typer
|
import typer
|
||||||
from rich.console import Console
|
from rich.console import Console
|
||||||
|
|
||||||
|
from rdagent.utils.env import logger
|
||||||
|
|
||||||
app = typer.Typer(help="Predix - AI Quantitative Trading Agent")
|
app = typer.Typer(help="Predix - AI Quantitative Trading Agent")
|
||||||
console = Console()
|
console = Console()
|
||||||
|
|
||||||
@@ -510,6 +512,7 @@ def top(
|
|||||||
if data.get("status") == "success" and data.get("ic") is not None:
|
if data.get("status") == "success" and data.get("ic") is not None:
|
||||||
results.append(data)
|
results.append(data)
|
||||||
except Exception:
|
except Exception:
|
||||||
|
logger.warning("Failed to load factor file %s", f, exc_info=True)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if not results:
|
if not results:
|
||||||
@@ -659,6 +662,7 @@ def portfolio(
|
|||||||
if data.get("status") == "success" and data.get("ic") is not None:
|
if data.get("status") == "success" and data.get("ic") is not None:
|
||||||
results.append(data)
|
results.append(data)
|
||||||
except Exception:
|
except Exception:
|
||||||
|
logger.warning("Failed to load factor file %s", f, exc_info=True)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if not results:
|
if not results:
|
||||||
@@ -956,6 +960,7 @@ def portfolio_simple(
|
|||||||
if data.get("status") == "success" and data.get("ic") is not None:
|
if data.get("status") == "success" and data.get("ic") is not None:
|
||||||
results.append(data)
|
results.append(data)
|
||||||
except Exception:
|
except Exception:
|
||||||
|
logger.warning("Failed to load factor file %s", f, exc_info=True)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if not results:
|
if not results:
|
||||||
@@ -1337,6 +1342,7 @@ def build_strategies_ai(
|
|||||||
if data.get("status") == "success" and data.get("ic") is not None:
|
if data.get("status") == "success" and data.get("ic") is not None:
|
||||||
factors.append(data)
|
factors.append(data)
|
||||||
except Exception:
|
except Exception:
|
||||||
|
logger.warning("Failed to load factor file %s", f, exc_info=True)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if len(factors) < 10:
|
if len(factors) < 10:
|
||||||
@@ -1552,6 +1558,7 @@ def _load_strategies():
|
|||||||
try:
|
try:
|
||||||
raw = json.loads(p.read_text())
|
raw = json.loads(p.read_text())
|
||||||
except Exception:
|
except Exception:
|
||||||
|
logger.warning("Failed to load strategy file %s", p, exc_info=True)
|
||||||
continue
|
continue
|
||||||
if not isinstance(raw, dict):
|
if not isinstance(raw, dict):
|
||||||
continue
|
continue
|
||||||
|
|||||||
+4
-1
@@ -27,6 +27,8 @@ import typer
|
|||||||
from rich.console import Console
|
from rich.console import Console
|
||||||
from typing_extensions import Annotated
|
from typing_extensions import Annotated
|
||||||
|
|
||||||
|
from rdagent.utils.env import logger
|
||||||
|
|
||||||
from rdagent.app.data_science.loop import main as data_science
|
from rdagent.app.data_science.loop import main as data_science
|
||||||
from rdagent.app.finetune.llm.loop import main as llm_finetune
|
from rdagent.app.finetune.llm.loop import main as llm_finetune
|
||||||
from rdagent.app.general_model.general_model import (
|
from rdagent.app.general_model.general_model import (
|
||||||
@@ -882,6 +884,7 @@ def optimize_portfolio_cli(
|
|||||||
if data.get("status") == "accepted":
|
if data.get("status") == "accepted":
|
||||||
strategies.append(data)
|
strategies.append(data)
|
||||||
except Exception:
|
except Exception:
|
||||||
|
logger.warning("Failed to load strategy file %s", f, exc_info=True)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if not strategies:
|
if not strategies:
|
||||||
@@ -1251,7 +1254,7 @@ def start_loop_cli(
|
|||||||
script_dir = str(Path(__file__).parent.parent.parent.parent)
|
script_dir = str(Path(__file__).parent.parent.parent.parent)
|
||||||
generator = f"python {script_dir}/scripts/predix_smart_strategy_gen.py"
|
generator = f"python {script_dir}/scripts/predix_smart_strategy_gen.py"
|
||||||
logfile = f"{script_dir}/results/logs/generator_loop.log"
|
logfile = f"{script_dir}/results/logs/generator_loop.log"
|
||||||
pidfile = "/tmp/predix_loop.pid"
|
pidfile = "/tmp/predix_loop.pid" # nosec B108 — administrative PID file, single-process daemon
|
||||||
|
|
||||||
os.makedirs(f"{script_dir}/results/logs", exist_ok=True)
|
os.makedirs(f"{script_dir}/results/logs", exist_ok=True)
|
||||||
|
|
||||||
|
|||||||
@@ -24,46 +24,12 @@ from rdagent.app.finetune.llm.ui.ft_summary import render_job_summary
|
|||||||
|
|
||||||
DEFAULT_LOG_BASE = "log/"
|
DEFAULT_LOG_BASE = "log/"
|
||||||
|
|
||||||
|
from rdagent.core.utils import safe_resolve_path
|
||||||
|
|
||||||
|
|
||||||
def validate_path_within_cwd(user_path: Path) -> Path:
|
def validate_path_within_cwd(user_path: Path) -> Path:
|
||||||
"""
|
|
||||||
Validate that a user-provided path is within the current working directory.
|
|
||||||
|
|
||||||
Security: This function prevents path traversal attacks by:
|
|
||||||
1. Resolving the path to its absolute canonical form
|
|
||||||
2. Verifying it's within the CWD boundary using a normalized common prefix
|
|
||||||
3. Rejecting paths outside the boundary with ValueError
|
|
||||||
|
|
||||||
Parameters
|
|
||||||
----------
|
|
||||||
user_path : Path
|
|
||||||
User-provided path to validate
|
|
||||||
|
|
||||||
Returns
|
|
||||||
-------
|
|
||||||
Path
|
|
||||||
Resolved absolute path if valid
|
|
||||||
|
|
||||||
Raises
|
|
||||||
------
|
|
||||||
ValueError
|
|
||||||
If path is outside the current working directory
|
|
||||||
"""
|
|
||||||
safe_root = Path.cwd().resolve()
|
safe_root = Path.cwd().resolve()
|
||||||
# Expand any user home reference and resolve without requiring the path to exist.
|
return safe_resolve_path(user_path, safe_root)
|
||||||
resolved_path = user_path.expanduser().resolve(strict=False)
|
|
||||||
|
|
||||||
# Ensure the resolved path is absolute and remains within the safe root.
|
|
||||||
safe_root_str = str(safe_root)
|
|
||||||
resolved_str = str(resolved_path)
|
|
||||||
common = os.path.commonpath([safe_root_str, resolved_str])
|
|
||||||
if common != safe_root_str:
|
|
||||||
raise ValueError("Path is outside the allowed project directory")
|
|
||||||
|
|
||||||
# This will raise ValueError if resolved_path is not within safe_root
|
|
||||||
resolved_path.relative_to(safe_root)
|
|
||||||
|
|
||||||
return resolved_path
|
|
||||||
|
|
||||||
|
|
||||||
def get_job_options(base_path: Path, safe_root: Path | None = None) -> list[str]:
|
def get_job_options(base_path: Path, safe_root: Path | None = None) -> list[str]:
|
||||||
@@ -141,19 +107,14 @@ def main():
|
|||||||
st.header("Job")
|
st.header("Job")
|
||||||
base_folder = st.text_input("Base Folder", value=default_log, key="base_folder_input")
|
base_folder = st.text_input("Base Folder", value=default_log, key="base_folder_input")
|
||||||
|
|
||||||
# Normalize and validate the base folder against the configured log root
|
safe_root = Path(default_log).expanduser().resolve()
|
||||||
root_real = os.path.realpath(str(Path(default_log).expanduser()))
|
try:
|
||||||
folder_real = os.path.realpath(str(Path(base_folder).expanduser()))
|
base_path = safe_resolve_path(Path(base_folder), safe_root)
|
||||||
if folder_real == root_real or folder_real.startswith(root_real + os.sep):
|
except ValueError:
|
||||||
base_path = Path(folder_real)
|
|
||||||
safe_root = Path(root_real)
|
|
||||||
else:
|
|
||||||
st.error("Invalid base folder: must be within the configured log directory.")
|
st.error("Invalid base folder: must be within the configured log directory.")
|
||||||
safe_root = Path(root_real)
|
|
||||||
base_path = safe_root
|
base_path = safe_root
|
||||||
|
|
||||||
# base_path is validated against safe_root – nosec B614
|
job_options = get_job_options(base_path, safe_root)
|
||||||
job_options = get_job_options(base_path, safe_root) # nosec B614 – validated above
|
|
||||||
if job_options:
|
if job_options:
|
||||||
selected_job = st.selectbox("Select Job", job_options, key="job_select")
|
selected_job = st.selectbox("Select Job", job_options, key="job_select")
|
||||||
if selected_job.startswith("."):
|
if selected_job.startswith("."):
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ from typing import Any
|
|||||||
import streamlit as st
|
import streamlit as st
|
||||||
|
|
||||||
from rdagent.app.finetune.llm.ui.config import EVALUATOR_CONFIG, EventType
|
from rdagent.app.finetune.llm.ui.config import EVALUATOR_CONFIG, EventType
|
||||||
|
from rdagent.core.utils import safe_resolve_path
|
||||||
from rdagent.log.storage import FileStorage
|
from rdagent.log.storage import FileStorage
|
||||||
|
|
||||||
|
|
||||||
@@ -89,11 +90,10 @@ def extract_stage(tag: str) -> str:
|
|||||||
def get_valid_sessions(log_folder: Path, safe_root: Path | None = None) -> list[str]:
|
def get_valid_sessions(log_folder: Path, safe_root: Path | None = None) -> list[str]:
|
||||||
"""Get list of valid session directories, optionally validating against a safe root."""
|
"""Get list of valid session directories, optionally validating against a safe root."""
|
||||||
if safe_root is not None:
|
if safe_root is not None:
|
||||||
root_real = os.path.realpath(str(safe_root.expanduser()))
|
try:
|
||||||
folder_real = os.path.realpath(str(log_folder.expanduser()))
|
log_folder = safe_resolve_path(log_folder, safe_root)
|
||||||
if not (folder_real == root_real or folder_real.startswith(root_real + os.sep)):
|
except ValueError:
|
||||||
return []
|
return []
|
||||||
log_folder = Path(folder_real)
|
|
||||||
|
|
||||||
if not log_folder.exists():
|
if not log_folder.exists():
|
||||||
return []
|
return []
|
||||||
@@ -373,13 +373,11 @@ def parse_event(tag: str, content: Any, timestamp: datetime) -> Event | None:
|
|||||||
@st.cache_data(ttl=300, hash_funcs={Path: str})
|
@st.cache_data(ttl=300, hash_funcs={Path: str})
|
||||||
def load_ft_session(log_path: Path, safe_root: Path | None = None) -> Session:
|
def load_ft_session(log_path: Path, safe_root: Path | None = None) -> Session:
|
||||||
"""Load events into hierarchical session structure, optionally validating against safe root."""
|
"""Load events into hierarchical session structure, optionally validating against safe root."""
|
||||||
# Validate path is within safe_root if provided
|
|
||||||
if safe_root is not None:
|
if safe_root is not None:
|
||||||
root_real = os.path.realpath(str(safe_root.expanduser()))
|
try:
|
||||||
path_real = os.path.realpath(str(log_path.expanduser()))
|
log_path = safe_resolve_path(log_path, safe_root)
|
||||||
if not (path_real == root_real or path_real.startswith(root_real + os.sep)):
|
except ValueError:
|
||||||
return Session()
|
return Session()
|
||||||
log_path = Path(path_real)
|
|
||||||
|
|
||||||
session = Session()
|
session = Session()
|
||||||
storage = FileStorage(log_path)
|
storage = FileStorage(log_path)
|
||||||
|
|||||||
@@ -322,6 +322,7 @@ class QuantRDLoop(RDLoop):
|
|||||||
if data.get("status") == "success" and data.get("ic") is not None:
|
if data.get("status") == "success" and data.get("ic") is not None:
|
||||||
factors.append(data)
|
factors.append(data)
|
||||||
except Exception:
|
except Exception:
|
||||||
|
logger.warning("Failed to load factor file %s", f, exc_info=True)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if len(factors) < 10:
|
if len(factors) < 10:
|
||||||
|
|||||||
@@ -16,55 +16,26 @@ from rdagent.app.rl.ui.components import render_session, render_summary
|
|||||||
from rdagent.app.rl.ui.config import ALWAYS_VISIBLE_TYPES, OPTIONAL_TYPES
|
from rdagent.app.rl.ui.config import ALWAYS_VISIBLE_TYPES, OPTIONAL_TYPES
|
||||||
from rdagent.app.rl.ui.data_loader import get_summary, get_valid_sessions, load_session
|
from rdagent.app.rl.ui.data_loader import get_summary, get_valid_sessions, load_session
|
||||||
from rdagent.app.rl.ui.rl_summary import render_job_summary
|
from rdagent.app.rl.ui.rl_summary import render_job_summary
|
||||||
|
from rdagent.core.utils import safe_resolve_path
|
||||||
|
|
||||||
DEFAULT_LOG_BASE = "log/"
|
DEFAULT_LOG_BASE = "log/"
|
||||||
|
|
||||||
|
|
||||||
def _safe_resolve(user_input: str | None, safe_root: Path) -> Path:
|
def _safe_resolve(user_input: str | None, safe_root: Path) -> Path:
|
||||||
"""
|
|
||||||
Resolve user path relative to safe_root; raise ValueError if it escapes.
|
|
||||||
|
|
||||||
Security: This function prevents path traversal attacks by:
|
|
||||||
1. Rejecting null bytes in user input
|
|
||||||
2. Rejecting Windows drive letters (C:\, D:\, etc.)
|
|
||||||
3. Rejecting absolute paths
|
|
||||||
4. Normalizing path to remove .. traversal attempts
|
|
||||||
5. Validating resolved path is within safe_root using a realpath-based check
|
|
||||||
|
|
||||||
All user-provided paths are validated before filesystem access.
|
|
||||||
"""
|
|
||||||
# Treat the provided safe_root as trusted and canonicalize it once.
|
|
||||||
safe_root = safe_root.expanduser().resolve()
|
safe_root = safe_root.expanduser().resolve()
|
||||||
|
|
||||||
# Empty input maps to the safe root directory.
|
|
||||||
if not user_input:
|
if not user_input:
|
||||||
return safe_root
|
return safe_root
|
||||||
|
|
||||||
# Security check 1: Reject null bytes (path truncation attack)
|
|
||||||
if "\x00" in user_input:
|
if "\x00" in user_input:
|
||||||
raise ValueError("Invalid path: contains null byte")
|
raise ValueError("Invalid path: contains null byte")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Security check 2: Normalize path to resolve .. and . components
|
|
||||||
normalized = os.path.normpath(user_input.strip())
|
normalized = os.path.normpath(user_input.strip())
|
||||||
|
|
||||||
# Security check 3: Reject Windows drive letters (C:\, D:\, etc.)
|
|
||||||
drive, _ = os.path.splitdrive(normalized)
|
drive, _ = os.path.splitdrive(normalized)
|
||||||
if drive:
|
if drive:
|
||||||
raise ValueError("Absolute paths with drive letters are not allowed")
|
raise ValueError("Absolute paths with drive letters are not allowed")
|
||||||
|
|
||||||
# Security check 4: Reject absolute paths (/, //server/share, etc.)
|
|
||||||
if os.path.isabs(normalized):
|
if os.path.isabs(normalized):
|
||||||
raise ValueError("Absolute paths are not allowed")
|
raise ValueError("Absolute paths are not allowed")
|
||||||
|
joined = safe_root / normalized
|
||||||
# Security check 5: Build candidate path under safe_root and fully resolve it.
|
return safe_resolve_path(joined, safe_root)
|
||||||
joined = os.path.join(str(safe_root), normalized)
|
|
||||||
resolved_candidate = os.path.realpath(joined)
|
|
||||||
|
|
||||||
# Security check 6: Validate candidate is within safe_root (prevent path traversal)
|
|
||||||
candidate_path = Path(resolved_candidate)
|
|
||||||
# Reconstruct from trusted safe_root so the returned path is root-derived.
|
|
||||||
return safe_root / candidate_path.relative_to(safe_root)
|
|
||||||
except (OSError, ValueError) as exc:
|
except (OSError, ValueError) as exc:
|
||||||
raise ValueError(f"Invalid path outside of allowed root: {user_input}") from exc
|
raise ValueError(f"Invalid path outside of allowed root: {user_input}") from exc
|
||||||
|
|
||||||
@@ -82,7 +53,7 @@ def get_job_options(base_path: Path, safe_root: Path | None = None) -> list[str]
|
|||||||
|
|
||||||
# Security fix: Validate base_path to prevent path traversal
|
# Security fix: Validate base_path to prevent path traversal
|
||||||
try:
|
try:
|
||||||
base_path_resolved = base_path.expanduser().resolve()
|
base_path_resolved = base_path.expanduser().resolve() # nosec B614 — validated against safe_root below via relative_to()
|
||||||
|
|
||||||
if safe_root is not None:
|
if safe_root is not None:
|
||||||
safe_root_resolved = safe_root.expanduser().resolve()
|
safe_root_resolved = safe_root.expanduser().resolve()
|
||||||
@@ -203,8 +174,7 @@ def main():
|
|||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
st.warning(str(e))
|
st.warning(str(e))
|
||||||
return
|
return
|
||||||
# job_path is validated by _safe_resolve() above
|
if job_path.exists():
|
||||||
if job_path.exists(): # nosec B614 – path validated by _safe_resolve
|
|
||||||
render_job_summary(job_path, safe_root, is_root=is_root_job)
|
render_job_summary(job_path, safe_root, is_root=is_root_job)
|
||||||
else:
|
else:
|
||||||
st.warning(f"Job folder not found: {job_folder}")
|
st.warning(f"Job folder not found: {job_folder}")
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ from typing import Any
|
|||||||
import streamlit as st
|
import streamlit as st
|
||||||
|
|
||||||
from rdagent.app.rl.ui.config import EventType
|
from rdagent.app.rl.ui.config import EventType
|
||||||
|
from rdagent.core.utils import safe_resolve_path
|
||||||
from rdagent.log.storage import FileStorage
|
from rdagent.log.storage import FileStorage
|
||||||
|
|
||||||
|
|
||||||
@@ -76,11 +77,10 @@ def extract_stage(tag: str) -> str:
|
|||||||
def get_valid_sessions(log_folder: Path, safe_root: Path | None = None) -> list[str]:
|
def get_valid_sessions(log_folder: Path, safe_root: Path | None = None) -> list[str]:
|
||||||
"""Get list of valid session directories, optionally validating against a safe root."""
|
"""Get list of valid session directories, optionally validating against a safe root."""
|
||||||
if safe_root is not None:
|
if safe_root is not None:
|
||||||
root_real = os.path.realpath(str(safe_root.expanduser()))
|
try:
|
||||||
folder_real = os.path.realpath(str(log_folder.expanduser()))
|
log_folder = safe_resolve_path(log_folder, safe_root)
|
||||||
if not (folder_real == root_real or folder_real.startswith(root_real + os.sep)):
|
except ValueError:
|
||||||
return []
|
return []
|
||||||
log_folder = Path(folder_real)
|
|
||||||
|
|
||||||
if not log_folder.exists():
|
if not log_folder.exists():
|
||||||
return []
|
return []
|
||||||
@@ -245,13 +245,11 @@ def parse_event(tag: str, content: Any, timestamp: datetime) -> Event | None:
|
|||||||
@st.cache_data(ttl=300, hash_funcs={Path: str})
|
@st.cache_data(ttl=300, hash_funcs={Path: str})
|
||||||
def load_session(log_path: Path, safe_root: Path | None = None) -> Session:
|
def load_session(log_path: Path, safe_root: Path | None = None) -> Session:
|
||||||
"""Load events into hierarchical session structure, optionally validating against safe root."""
|
"""Load events into hierarchical session structure, optionally validating against safe root."""
|
||||||
# Validate path is within safe_root if provided
|
|
||||||
if safe_root is not None:
|
if safe_root is not None:
|
||||||
root_real = os.path.realpath(str(safe_root.expanduser()))
|
try:
|
||||||
path_real = os.path.realpath(str(log_path.expanduser()))
|
log_path = safe_resolve_path(log_path, safe_root)
|
||||||
if not (path_real == root_real or path_real.startswith(root_real + os.sep)):
|
except ValueError:
|
||||||
return Session()
|
return Session()
|
||||||
log_path = Path(path_real)
|
|
||||||
|
|
||||||
session = Session()
|
session = Session()
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ from pathlib import Path
|
|||||||
import pandas as pd
|
import pandas as pd
|
||||||
import streamlit as st
|
import streamlit as st
|
||||||
|
|
||||||
|
from rdagent.core.utils import safe_resolve_path
|
||||||
|
|
||||||
|
|
||||||
def is_valid_task(task_path: Path) -> bool:
|
def is_valid_task(task_path: Path) -> bool:
|
||||||
"""Check if directory is a valid RL task (has __session__ subdirectory)"""
|
"""Check if directory is a valid RL task (has __session__ subdirectory)"""
|
||||||
@@ -62,14 +64,10 @@ def get_loop_status(task_path: Path, loop_id: int) -> tuple[str, bool | None]:
|
|||||||
|
|
||||||
|
|
||||||
def _validate_job_path(job_path: Path, safe_root: Path) -> Path:
|
def _validate_job_path(job_path: Path, safe_root: Path) -> Path:
|
||||||
"""Resolve and validate that job_path stays within safe_root."""
|
|
||||||
resolved_root = safe_root.expanduser().resolve()
|
|
||||||
resolved_job = job_path.expanduser().resolve()
|
|
||||||
try:
|
try:
|
||||||
# Reconstruct from trusted root so the returned path is root-derived.
|
return safe_resolve_path(job_path, safe_root)
|
||||||
return resolved_root / resolved_job.relative_to(resolved_root)
|
|
||||||
except ValueError:
|
except ValueError:
|
||||||
raise ValueError(f"Job path is outside allowed root {resolved_root}")
|
raise ValueError(f"Job path is outside allowed root {safe_root}")
|
||||||
|
|
||||||
|
|
||||||
def get_max_loops(job_path: Path, safe_root: Path | None = None) -> int:
|
def get_max_loops(job_path: Path, safe_root: Path | None = None) -> int:
|
||||||
|
|||||||
@@ -54,11 +54,11 @@ def rdagent_info():
|
|||||||
current_version = importlib.metadata.version("rdagent")
|
current_version = importlib.metadata.version("rdagent")
|
||||||
logger.info(f"RD-Agent version: {current_version}")
|
logger.info(f"RD-Agent version: {current_version}")
|
||||||
api_url = f"https://api.github.com/repos/microsoft/RD-Agent/contents/requirements.txt?ref=main"
|
api_url = f"https://api.github.com/repos/microsoft/RD-Agent/contents/requirements.txt?ref=main"
|
||||||
response = requests.get(api_url)
|
response = requests.get(api_url, timeout=30)
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
files = response.json()
|
files = response.json()
|
||||||
file_url = files["download_url"]
|
file_url = files["download_url"]
|
||||||
file_response = requests.get(file_url)
|
file_response = requests.get(file_url, timeout=30)
|
||||||
if file_response.status_code == 200:
|
if file_response.status_code == 200:
|
||||||
all_file_contents = file_response.text.split("\n")
|
all_file_contents = file_response.text.split("\n")
|
||||||
else:
|
else:
|
||||||
|
|||||||
@@ -71,6 +71,9 @@ class ResultsDatabase:
|
|||||||
|
|
||||||
self.conn.commit()
|
self.conn.commit()
|
||||||
|
|
||||||
|
_ALLOWED_TABLES = frozenset({"factors", "backtest_runs", "loop_results"})
|
||||||
|
_ALLOWED_COL_TYPES = frozenset({"REAL", "TEXT", "INTEGER", "BLOB"})
|
||||||
|
|
||||||
def _add_column_if_not_exists(self, table: str, column: str, col_type: str) -> None:
|
def _add_column_if_not_exists(self, table: str, column: str, col_type: str) -> None:
|
||||||
"""
|
"""
|
||||||
Add a column to a table if it doesn't already exist.
|
Add a column to a table if it doesn't already exist.
|
||||||
@@ -78,20 +81,24 @@ class ResultsDatabase:
|
|||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
table : str
|
table : str
|
||||||
Table name
|
Table name (must be in _ALLOWED_TABLES)
|
||||||
column : str
|
column : str
|
||||||
Column name to add
|
Column name to add (alphanumeric + underscore only)
|
||||||
col_type : str
|
col_type : str
|
||||||
SQL column type (e.g., 'REAL', 'TEXT')
|
SQL column type (must be in _ALLOWED_COL_TYPES)
|
||||||
"""
|
"""
|
||||||
|
if table not in self._ALLOWED_TABLES:
|
||||||
|
raise ValueError(f"Unknown table: {table!r}")
|
||||||
|
if not column.replace("_", "").isalnum():
|
||||||
|
raise ValueError(f"Invalid column name: {column!r}")
|
||||||
|
if col_type not in self._ALLOWED_COL_TYPES:
|
||||||
|
raise ValueError(f"Invalid column type: {col_type!r}")
|
||||||
|
|
||||||
c = self.conn.cursor()
|
c = self.conn.cursor()
|
||||||
try:
|
c.execute("SELECT name FROM pragma_table_info(?)", (table,))
|
||||||
# Try to query the column - if it fails, it doesn't exist
|
existing = {row[0] for row in c.fetchall()}
|
||||||
# nosec B608: Internal schema migration, column names are controlled
|
if column not in existing:
|
||||||
c.execute(f"SELECT {column} FROM {table} LIMIT 1") # nosec B608
|
c.execute(f"ALTER TABLE {table} ADD COLUMN {column} {col_type}")
|
||||||
except sqlite3.OperationalError:
|
|
||||||
# Column doesn't exist, add it
|
|
||||||
c.execute(f"ALTER TABLE {table} ADD COLUMN {column} {col_type}") # nosec B608
|
|
||||||
|
|
||||||
def add_factor(self, name: str, type: str = "unknown") -> int:
|
def add_factor(self, name: str, type: str = "unknown") -> int:
|
||||||
c = self.conn.cursor()
|
c = self.conn.cursor()
|
||||||
@@ -183,16 +190,18 @@ class ResultsDatabase:
|
|||||||
pd.DataFrame
|
pd.DataFrame
|
||||||
DataFrame with factor names and metrics
|
DataFrame with factor names and metrics
|
||||||
"""
|
"""
|
||||||
# Map shorthand to full column name
|
_ALLOWED_METRICS = frozenset({
|
||||||
|
'sharpe', 'ic', 'annual_return', 'max_drawdown',
|
||||||
|
'win_rate', 'information_ratio', 'volatility',
|
||||||
|
})
|
||||||
metric_map = {
|
metric_map = {
|
||||||
'sharpe': 'sharpe',
|
'sharpe': 'sharpe', 'ic': 'ic', 'return': 'annual_return',
|
||||||
'ic': 'ic',
|
'drawdown': 'max_drawdown', 'win_rate': 'win_rate',
|
||||||
'return': 'annual_return',
|
|
||||||
'drawdown': 'max_drawdown',
|
|
||||||
'win_rate': 'win_rate',
|
|
||||||
'information_ratio': 'information_ratio',
|
'information_ratio': 'information_ratio',
|
||||||
}
|
}
|
||||||
col = metric_map.get(metric, metric)
|
col = metric_map.get(metric, metric)
|
||||||
|
if col not in _ALLOWED_METRICS:
|
||||||
|
raise ValueError(f"Unknown metric: {metric!r}")
|
||||||
|
|
||||||
return pd.read_sql_query(
|
return pd.read_sql_query(
|
||||||
f"""SELECT factor_name, ic, sharpe, annual_return, max_drawdown,
|
f"""SELECT factor_name, ic, sharpe, annual_return, max_drawdown,
|
||||||
@@ -201,7 +210,7 @@ class ResultsDatabase:
|
|||||||
JOIN factors ON factor_id = factors.id
|
JOIN factors ON factor_id = factors.id
|
||||||
WHERE {col} IS NOT NULL
|
WHERE {col} IS NOT NULL
|
||||||
ORDER BY {col} DESC
|
ORDER BY {col} DESC
|
||||||
LIMIT ?""",
|
LIMIT ?""", # nosec B608 — col is validated against _ALLOWED_METRICS above
|
||||||
self.conn,
|
self.conn,
|
||||||
params=[limit]
|
params=[limit]
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -54,7 +54,8 @@ def get_ds_env(
|
|||||||
ValueError: If the env_type is not recognized.
|
ValueError: If the env_type is not recognized.
|
||||||
"""
|
"""
|
||||||
conf = DSCoderCoSTEERSettings()
|
conf = DSCoderCoSTEERSettings()
|
||||||
assert conf_type in ["kaggle", "mlebench"], f"Unknown conf_type: {conf_type}"
|
if conf_type not in ["kaggle", "mlebench"]:
|
||||||
|
raise ValueError(f"Unknown conf_type: {conf_type}")
|
||||||
|
|
||||||
if conf.env_type == "docker":
|
if conf.env_type == "docker":
|
||||||
env_conf = DSDockerConf() if conf_type == "kaggle" else MLEBDockerConf()
|
env_conf = DSDockerConf() if conf_type == "kaggle" else MLEBDockerConf()
|
||||||
@@ -79,7 +80,8 @@ def get_clear_ws_cmd(stage: Literal["before_training", "before_inference"] = "be
|
|||||||
"""
|
"""
|
||||||
Clean the files in workspace to a specific stage
|
Clean the files in workspace to a specific stage
|
||||||
"""
|
"""
|
||||||
assert stage in ["before_training", "before_inference"], f"Unknown stage: {stage}"
|
if stage not in ["before_training", "before_inference"]:
|
||||||
|
raise ValueError(f"Unknown stage: {stage}")
|
||||||
if DS_RD_SETTING.enable_model_dump and stage == "before_training":
|
if DS_RD_SETTING.enable_model_dump and stage == "before_training":
|
||||||
cmd = "rm -r submission.csv scores.csv models trace.log"
|
cmd = "rm -r submission.csv scores.csv models trace.log"
|
||||||
else:
|
else:
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ File structure
|
|||||||
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from jinja2 import Environment, StrictUndefined
|
from jinja2 import Environment, StrictUndefined, select_autoescape
|
||||||
|
|
||||||
from rdagent.app.data_science.conf import DS_RD_SETTING
|
from rdagent.app.data_science.conf import DS_RD_SETTING
|
||||||
from rdagent.components.coder.CoSTEER.evaluators import (
|
from rdagent.components.coder.CoSTEER.evaluators import (
|
||||||
@@ -88,7 +88,7 @@ class EnsembleMultiProcessEvolvingStrategy(MultiProcessEvolvingStrategy):
|
|||||||
code_spec = workspace.file_dict["spec/ensemble.md"]
|
code_spec = workspace.file_dict["spec/ensemble.md"]
|
||||||
else:
|
else:
|
||||||
test_code = (
|
test_code = (
|
||||||
Environment(undefined=StrictUndefined)
|
Environment(undefined=StrictUndefined, autoescape=select_autoescape())
|
||||||
.from_string((DIRNAME / "eval_tests" / "ensemble_test.txt").read_text())
|
.from_string((DIRNAME / "eval_tests" / "ensemble_test.txt").read_text())
|
||||||
.render(
|
.render(
|
||||||
model_names=[
|
model_names=[
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import json
|
|||||||
import re
|
import re
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from jinja2 import Environment, StrictUndefined
|
from jinja2 import Environment, StrictUndefined, select_autoescape
|
||||||
|
|
||||||
from rdagent.app.data_science.conf import DS_RD_SETTING
|
from rdagent.app.data_science.conf import DS_RD_SETTING
|
||||||
from rdagent.components.coder.CoSTEER.evaluators import (
|
from rdagent.components.coder.CoSTEER.evaluators import (
|
||||||
@@ -55,7 +55,7 @@ class EnsembleCoSTEEREvaluator(CoSTEEREvaluator):
|
|||||||
fname = "test/ensemble_test.txt"
|
fname = "test/ensemble_test.txt"
|
||||||
test_code = (DIRNAME / "eval_tests" / "ensemble_test.txt").read_text()
|
test_code = (DIRNAME / "eval_tests" / "ensemble_test.txt").read_text()
|
||||||
test_code = (
|
test_code = (
|
||||||
Environment(undefined=StrictUndefined)
|
Environment(undefined=StrictUndefined, autoescape=select_autoescape())
|
||||||
.from_string(test_code)
|
.from_string(test_code)
|
||||||
.render(
|
.render(
|
||||||
model_names=[
|
model_names=[
|
||||||
|
|||||||
@@ -161,8 +161,7 @@ class FactorFBWorkspace(FBWorkspace):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
subprocess.check_output(
|
subprocess.check_output(
|
||||||
f"{FACTOR_COSTEER_SETTINGS.python_bin} {execution_code_path}",
|
[FACTOR_COSTEER_SETTINGS.python_bin, str(execution_code_path)],
|
||||||
shell=True,
|
|
||||||
cwd=self.workspace_path,
|
cwd=self.workspace_path,
|
||||||
stderr=subprocess.STDOUT,
|
stderr=subprocess.STDOUT,
|
||||||
timeout=FACTOR_COSTEER_SETTINGS.file_based_execution_timeout,
|
timeout=FACTOR_COSTEER_SETTINGS.file_based_execution_timeout,
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ Two-step validation:
|
|||||||
2. Micro-batch testing - Runtime validation with small dataset
|
2. Micro-batch testing - Runtime validation with small dataset
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import ast
|
||||||
import json
|
import json
|
||||||
import re
|
import re
|
||||||
import time
|
import time
|
||||||
@@ -229,7 +230,7 @@ class LLMConfigValidator:
|
|||||||
final_metrics = re.search(r"\{'train_runtime':[^}]+\}", stdout)
|
final_metrics = re.search(r"\{'train_runtime':[^}]+\}", stdout)
|
||||||
if final_metrics:
|
if final_metrics:
|
||||||
try:
|
try:
|
||||||
metrics = eval(final_metrics.group(0)) # Safe: only numbers and strings
|
metrics = ast.literal_eval(final_metrics.group(0))
|
||||||
result["final_metrics"] = {
|
result["final_metrics"] = {
|
||||||
"train_loss": metrics.get("train_loss"),
|
"train_loss": metrics.get("train_loss"),
|
||||||
"train_runtime": metrics.get("train_runtime"),
|
"train_runtime": metrics.get("train_runtime"),
|
||||||
|
|||||||
@@ -41,7 +41,8 @@ class ModelCoSTEEREvaluator(CoSTEEREvaluator):
|
|||||||
final_feedback="This task has failed too many times, skip implementation.",
|
final_feedback="This task has failed too many times, skip implementation.",
|
||||||
final_decision=False,
|
final_decision=False,
|
||||||
)
|
)
|
||||||
assert isinstance(target_task, ModelTask)
|
if not isinstance(target_task, ModelTask):
|
||||||
|
raise TypeError(f"Expected ModelTask, got {type(target_task)}")
|
||||||
|
|
||||||
# NOTE: Use fixed input to test the model to avoid randomness
|
# NOTE: Use fixed input to test the model to avoid randomness
|
||||||
batch_size = 8
|
batch_size = 8
|
||||||
@@ -50,7 +51,8 @@ class ModelCoSTEEREvaluator(CoSTEEREvaluator):
|
|||||||
input_value = 0.4
|
input_value = 0.4
|
||||||
param_init_value = 0.6
|
param_init_value = 0.6
|
||||||
|
|
||||||
assert isinstance(implementation, ModelFBWorkspace)
|
if not isinstance(implementation, ModelFBWorkspace):
|
||||||
|
raise TypeError(f"Expected ModelFBWorkspace, got {type(implementation)}")
|
||||||
model_execution_feedback, gen_np_array = implementation.execute(
|
model_execution_feedback, gen_np_array = implementation.execute(
|
||||||
batch_size=batch_size,
|
batch_size=batch_size,
|
||||||
num_features=num_features,
|
num_features=num_features,
|
||||||
@@ -59,7 +61,8 @@ class ModelCoSTEEREvaluator(CoSTEEREvaluator):
|
|||||||
param_init_value=param_init_value,
|
param_init_value=param_init_value,
|
||||||
)
|
)
|
||||||
if gt_implementation is not None:
|
if gt_implementation is not None:
|
||||||
assert isinstance(gt_implementation, ModelFBWorkspace)
|
if not isinstance(gt_implementation, ModelFBWorkspace):
|
||||||
|
raise TypeError(f"Expected ModelFBWorkspace, got {type(gt_implementation)}")
|
||||||
_, gt_np_array = gt_implementation.execute(
|
_, gt_np_array = gt_implementation.execute(
|
||||||
batch_size=batch_size,
|
batch_size=batch_size,
|
||||||
num_features=num_features,
|
num_features=num_features,
|
||||||
|
|||||||
@@ -24,7 +24,8 @@ class UndirectedNode(Node):
|
|||||||
super().__init__(content, label, embedding)
|
super().__init__(content, label, embedding)
|
||||||
self.neighbors: set[UndirectedNode] = set()
|
self.neighbors: set[UndirectedNode] = set()
|
||||||
self.appendix = appendix # appendix stores any additional information
|
self.appendix = appendix # appendix stores any additional information
|
||||||
assert isinstance(content, str), "content must be a string"
|
if not isinstance(content, str):
|
||||||
|
raise TypeError("content must be a string")
|
||||||
|
|
||||||
def add_neighbor(self, node: UndirectedNode) -> None:
|
def add_neighbor(self, node: UndirectedNode) -> None:
|
||||||
self.neighbors.add(node)
|
self.neighbors.add(node)
|
||||||
@@ -96,7 +97,8 @@ class Graph(KnowledgeBase):
|
|||||||
APIBackend().create_embedding(input_content=contents[i : i + size]),
|
APIBackend().create_embedding(input_content=contents[i : i + size]),
|
||||||
)
|
)
|
||||||
|
|
||||||
assert len(nodes) == len(embeddings), "nodes' length must equals embeddings' length"
|
if len(nodes) != len(embeddings):
|
||||||
|
raise ValueError("nodes' length must equal embeddings' length")
|
||||||
for node, embedding in zip(nodes, embeddings):
|
for node, embedding in zip(nodes, embeddings):
|
||||||
node.embedding = embedding
|
node.embedding = embedding
|
||||||
return nodes
|
return nodes
|
||||||
@@ -252,7 +254,8 @@ class UndirectedGraph(Graph):
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
min_nodes_count = 2
|
min_nodes_count = 2
|
||||||
assert len(nodes) >= min_nodes_count, "nodes length must >=2"
|
if len(nodes) < min_nodes_count:
|
||||||
|
raise ValueError("nodes length must >=2")
|
||||||
intersection = None
|
intersection = None
|
||||||
|
|
||||||
for node in nodes:
|
for node in nodes:
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import functools
|
|||||||
import importlib
|
import importlib
|
||||||
import json
|
import json
|
||||||
import multiprocessing as mp
|
import multiprocessing as mp
|
||||||
|
import os
|
||||||
import pickle
|
import pickle
|
||||||
import random
|
import random
|
||||||
from collections.abc import Callable
|
from collections.abc import Callable
|
||||||
@@ -208,3 +209,13 @@ def cache_with_pickle(hash_func: Callable, post_process_func: Callable | None =
|
|||||||
return cache_wrapper
|
return cache_wrapper
|
||||||
|
|
||||||
return cache_decorator
|
return cache_decorator
|
||||||
|
|
||||||
|
|
||||||
|
def safe_resolve_path(user_path: Path, safe_root: Path | None = None) -> Path:
|
||||||
|
if safe_root is not None:
|
||||||
|
root_real = os.path.realpath(str(safe_root.expanduser()))
|
||||||
|
path_real = os.path.realpath(str(user_path.expanduser())) # nosec B614 — validated against safe_root below
|
||||||
|
if not (path_real == root_real or path_real.startswith(root_real + os.sep)):
|
||||||
|
raise ValueError(f"Path {user_path} resolves to {path_real}, outside allowed root {safe_root}")
|
||||||
|
return Path(path_real)
|
||||||
|
return user_path.expanduser().resolve()
|
||||||
|
|||||||
+22
-15
@@ -27,6 +27,7 @@ Usage:
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import json as _json
|
import json as _json
|
||||||
|
import logging
|
||||||
import sys
|
import sys
|
||||||
import threading
|
import threading
|
||||||
from contextlib import contextmanager
|
from contextlib import contextmanager
|
||||||
@@ -36,21 +37,24 @@ from typing import Any
|
|||||||
|
|
||||||
from loguru import logger as _root
|
from loguru import logger as _root
|
||||||
|
|
||||||
# ── paths ─────────────────────────────────────────────────────────────────────
|
# ── paths ─────────────────────────────────────────────────────────────────────────────────
|
||||||
LOGS_ROOT: Path = Path(__file__).parent.parent.parent / "logs"
|
LOGS_ROOT: Path = Path(__file__).parent.parent.parent / "logs"
|
||||||
|
|
||||||
# ── format ────────────────────────────────────────────────────────────────────
|
# ── format ────────────────────────────────────────────────────────────────────────────────
|
||||||
_FILE_FMT = (
|
_FILE_FMT = (
|
||||||
"{time:YYYY-MM-DD HH:mm:ss.SSS} | {level: <8} | {extra[cmd]: <18} | {message}"
|
"{time:YYYY-MM-DD HH:mm:ss.SSS} | {level: <8} | {extra[cmd]: <18} | {message}"
|
||||||
)
|
)
|
||||||
|
|
||||||
# ── internal state ─────────────────────────────────────────────────────────────
|
# ── internal state ─────────────────────────────────────────────────────────────────────────────
|
||||||
_registered: set[str] = set() # command keys that already have a file sink
|
_registered: set[str] = set() # command keys that already have a file sink
|
||||||
_all_added: bool = False # whether the combined all.log sink is active
|
_all_added: bool = False # whether the combined all.log sink is active
|
||||||
_llm_log_lock = threading.Lock() # guards concurrent writes to llm_calls.jsonl
|
_llm_log_lock = threading.Lock() # guards concurrent writes to llm_calls.jsonl
|
||||||
|
|
||||||
|
# Maximum characters stored per field in llm_calls.jsonl to prevent GB-scale files.
|
||||||
|
_LLM_CALL_MAX_CHARS = 500
|
||||||
|
|
||||||
# ── helpers ───────────────────────────────────────────────────────────────────
|
|
||||||
|
# ── helpers ────────────────────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
def _today_dir() -> Path:
|
def _today_dir() -> Path:
|
||||||
d = LOGS_ROOT / datetime.now().strftime("%Y-%m-%d")
|
d = LOGS_ROOT / datetime.now().strftime("%Y-%m-%d")
|
||||||
@@ -79,7 +83,7 @@ def _banner(log, title: str, meta: dict[str, Any]) -> None:
|
|||||||
log.info(sep)
|
log.info(sep)
|
||||||
|
|
||||||
|
|
||||||
# ── public API ────────────────────────────────────────────────────────────────
|
# ── public API ──────────────────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
def log_llm_call(
|
def log_llm_call(
|
||||||
system: str | None,
|
system: str | None,
|
||||||
@@ -88,16 +92,19 @@ def log_llm_call(
|
|||||||
start_time: Any = None,
|
start_time: Any = None,
|
||||||
end_time: Any = None,
|
end_time: Any = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Append one complete LLM call to logs/YYYY-MM-DD/llm_calls.jsonl.
|
"""Append one LLM call summary to logs/YYYY-MM-DD/llm_calls.jsonl.
|
||||||
|
|
||||||
|
Prompt/response content is capped at _LLM_CALL_MAX_CHARS to prevent
|
||||||
|
GB-scale log files from long-running loops.
|
||||||
|
|
||||||
Each line is a self-contained JSON object so the file is grep/jq-friendly:
|
Each line is a self-contained JSON object so the file is grep/jq-friendly:
|
||||||
jq 'select(.duration_ms > 5000)' logs/2026-04-17/llm_calls.jsonl
|
jq 'select(.duration_ms > 5000)' logs/2026-04-17/llm_calls.jsonl
|
||||||
"""
|
"""
|
||||||
entry: dict[str, Any] = {
|
entry: dict[str, Any] = {
|
||||||
"ts": datetime.now().isoformat(timespec="milliseconds"),
|
"ts": datetime.now().isoformat(timespec="milliseconds"),
|
||||||
"system": system or "",
|
"system": (system or "")[:_LLM_CALL_MAX_CHARS],
|
||||||
"user": user,
|
"user": user[:_LLM_CALL_MAX_CHARS],
|
||||||
"response": response,
|
"response": response[:_LLM_CALL_MAX_CHARS],
|
||||||
}
|
}
|
||||||
if start_time is not None and end_time is not None:
|
if start_time is not None and end_time is not None:
|
||||||
try:
|
try:
|
||||||
@@ -130,13 +137,13 @@ def setup(command: str, **context: Any):
|
|||||||
key = command.lower()
|
key = command.lower()
|
||||||
|
|
||||||
if key not in _registered:
|
if key not in _registered:
|
||||||
# Per-command rotating file
|
|
||||||
_root.add(
|
_root.add(
|
||||||
str(log_dir / f"{key}.log"),
|
str(log_dir / f"{key}.log"),
|
||||||
format=_FILE_FMT,
|
format=_FILE_FMT,
|
||||||
filter=lambda r, k=key: r["extra"].get("cmd", "").lower() == k,
|
filter=lambda r, k=key: r["extra"].get("cmd", "").lower() == k,
|
||||||
rotation="00:00", # new file at midnight
|
rotation="50 MB",
|
||||||
retention="30 days",
|
compression="gz",
|
||||||
|
retention="7 days",
|
||||||
encoding="utf-8",
|
encoding="utf-8",
|
||||||
enqueue=True,
|
enqueue=True,
|
||||||
backtrace=False,
|
backtrace=False,
|
||||||
@@ -145,13 +152,13 @@ def setup(command: str, **context: Any):
|
|||||||
_registered.add(key)
|
_registered.add(key)
|
||||||
|
|
||||||
if not _all_added:
|
if not _all_added:
|
||||||
# Combined log — all commands
|
|
||||||
_root.add(
|
_root.add(
|
||||||
str(log_dir / "all.log"),
|
str(log_dir / "all.log"),
|
||||||
format=_FILE_FMT,
|
format=_FILE_FMT,
|
||||||
filter=lambda r: "cmd" in r["extra"],
|
filter=lambda r: "cmd" in r["extra"],
|
||||||
rotation="00:00",
|
rotation="100 MB",
|
||||||
retention="60 days",
|
compression="gz",
|
||||||
|
retention="7 days",
|
||||||
encoding="utf-8",
|
encoding="utf-8",
|
||||||
enqueue=True,
|
enqueue=True,
|
||||||
backtrace=False,
|
backtrace=False,
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,306 @@
|
|||||||
|
import argparse
|
||||||
|
import json
|
||||||
|
import pickle # nosec
|
||||||
|
import re
|
||||||
|
import time
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
import streamlit as st
|
||||||
|
from streamlit import session_state
|
||||||
|
|
||||||
|
from rdagent.log.ui.conf import UI_SETTING
|
||||||
|
from rdagent.log.utils import extract_evoid, extract_loopid_func_name
|
||||||
|
|
||||||
|
st.set_page_config(layout="wide", page_title="debug_llm", page_icon="🎓", initial_sidebar_state="expanded")
|
||||||
|
|
||||||
|
# 获取 log_path 参数
|
||||||
|
parser = argparse.ArgumentParser(description="RD-Agent Streamlit App")
|
||||||
|
parser.add_argument("--log_dir", type=str, help="Path to the log directory")
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
|
def get_folders_sorted(log_path):
|
||||||
|
"""缓存并返回排序后的文件夹列表,并加入进度打印"""
|
||||||
|
with st.spinner("正在加载文件夹列表..."):
|
||||||
|
folders = sorted(
|
||||||
|
(folder for folder in log_path.iterdir() if folder.is_dir() and list(folder.iterdir())),
|
||||||
|
key=lambda folder: folder.stat().st_mtime,
|
||||||
|
reverse=True,
|
||||||
|
)
|
||||||
|
st.write(f"找到 {len(folders)} 个文件夹")
|
||||||
|
return [folder.name for folder in folders]
|
||||||
|
|
||||||
|
|
||||||
|
if UI_SETTING.enable_cache:
|
||||||
|
get_folders_sorted = st.cache_data(get_folders_sorted)
|
||||||
|
|
||||||
|
|
||||||
|
# 设置主日志路径
|
||||||
|
main_log_path = Path(args.log_dir) if args.log_dir else Path("./log")
|
||||||
|
if not main_log_path.exists():
|
||||||
|
st.error(f"Log dir {main_log_path} does not exist!")
|
||||||
|
st.stop()
|
||||||
|
|
||||||
|
if "data" not in session_state:
|
||||||
|
session_state.data = []
|
||||||
|
if "log_path" not in session_state:
|
||||||
|
session_state.log_path = None
|
||||||
|
|
||||||
|
tlist = []
|
||||||
|
|
||||||
|
|
||||||
|
def load_data():
|
||||||
|
"""加载数据到 session_state 并显示进度"""
|
||||||
|
log_file = main_log_path / session_state.log_path / "debug_llm.pkl"
|
||||||
|
try:
|
||||||
|
with st.spinner(f"正在加载数据文件 {log_file}..."):
|
||||||
|
start_time = time.time()
|
||||||
|
with open(log_file, "rb") as f:
|
||||||
|
session_state.data = pickle.load(f, encoding="utf-8") # nosec
|
||||||
|
st.success(f"数据加载完成!耗时 {time.time() - start_time:.2f} 秒")
|
||||||
|
st.session_state["current_loop"] = 1
|
||||||
|
except Exception as e:
|
||||||
|
session_state.data = [{"error": str(e)}]
|
||||||
|
st.error(f"加载数据失败: {e}")
|
||||||
|
|
||||||
|
|
||||||
|
# UI - Sidebar
|
||||||
|
with st.sidebar:
|
||||||
|
st.markdown(":blue[**Log Path**]")
|
||||||
|
manually = st.toggle("Manual Input")
|
||||||
|
if manually:
|
||||||
|
st.text_input("log path", key="log_path", label_visibility="collapsed")
|
||||||
|
else:
|
||||||
|
folders = get_folders_sorted(main_log_path)
|
||||||
|
st.selectbox(f"**Select from {main_log_path.absolute()}**", folders, key="log_path") # nosec B608 — not SQL, Bandit false positive on "Select" in UI label
|
||||||
|
|
||||||
|
if st.button("Refresh Data"):
|
||||||
|
load_data()
|
||||||
|
st.rerun()
|
||||||
|
|
||||||
|
|
||||||
|
# Helper functions
|
||||||
|
def show_text(text, lang=None):
|
||||||
|
"""显示文本代码块"""
|
||||||
|
if lang:
|
||||||
|
st.code(text, language=lang, wrap_lines=True)
|
||||||
|
elif "\n" in text:
|
||||||
|
st.code(text, language="python", wrap_lines=True)
|
||||||
|
else:
|
||||||
|
st.code(text, language="html", wrap_lines=True)
|
||||||
|
|
||||||
|
|
||||||
|
def highlight_prompts_uri(uri):
|
||||||
|
"""高亮 URI 的格式"""
|
||||||
|
parts = uri.split(":")
|
||||||
|
return f"**{parts[0]}:**:green[**{parts[1]}**]"
|
||||||
|
|
||||||
|
|
||||||
|
# Display Data
|
||||||
|
progress_text = st.empty()
|
||||||
|
progress_bar = st.progress(0)
|
||||||
|
|
||||||
|
# 每页展示一个 Loop
|
||||||
|
LOOPS_PER_PAGE = 1
|
||||||
|
|
||||||
|
# 获取所有的 Loop ID
|
||||||
|
loop_groups = {}
|
||||||
|
for i, d in enumerate(session_state.data):
|
||||||
|
tag = d["tag"]
|
||||||
|
loop_id, _ = extract_loopid_func_name(tag)
|
||||||
|
if loop_id:
|
||||||
|
if loop_id not in loop_groups:
|
||||||
|
loop_groups[loop_id] = []
|
||||||
|
loop_groups[loop_id].append(d)
|
||||||
|
|
||||||
|
# 按 Loop ID 排序
|
||||||
|
sorted_loop_ids = sorted(loop_groups.keys(), key=int) # 假设 Loop ID 是数字
|
||||||
|
total_loops = len(sorted_loop_ids)
|
||||||
|
total_pages = total_loops # 每页展示一个 Loop
|
||||||
|
|
||||||
|
|
||||||
|
# simple display
|
||||||
|
# FIXME: Delete this simple UI if trace have tag(evo_id & loop_id)
|
||||||
|
# with st.sidebar:
|
||||||
|
# start = int(st.text_input("start", 0))
|
||||||
|
# end = int(st.text_input("end", 100))
|
||||||
|
# for m in session_state.data[start:end]:
|
||||||
|
# if "tpl" in m["tag"]:
|
||||||
|
# obj = m["obj"]
|
||||||
|
# uri = obj["uri"]
|
||||||
|
# tpl = obj["template"]
|
||||||
|
# cxt = obj["context"]
|
||||||
|
# rd = obj["rendered"]
|
||||||
|
# with st.expander(highlight_prompts_uri(uri), expanded=False, icon="⚙️"):
|
||||||
|
# t1, t2, t3 = st.tabs([":green[**Rendered**]", ":blue[**Template**]", ":orange[**Context**]"])
|
||||||
|
# with t1:
|
||||||
|
# show_text(rd)
|
||||||
|
# with t2:
|
||||||
|
# show_text(tpl, lang="django")
|
||||||
|
# with t3:
|
||||||
|
# st.json(cxt)
|
||||||
|
# if "llm" in m["tag"]:
|
||||||
|
# obj = m["obj"]
|
||||||
|
# system = obj.get("system", None)
|
||||||
|
# user = obj["user"]
|
||||||
|
# resp = obj["resp"]
|
||||||
|
# with st.expander(f"**LLM**", expanded=False, icon="🤖"):
|
||||||
|
# t1, t2, t3 = st.tabs([":green[**Response**]", ":blue[**User**]", ":orange[**System**]"])
|
||||||
|
# with t1:
|
||||||
|
# try:
|
||||||
|
# rdict = json.loads(resp)
|
||||||
|
# if "code" in rdict:
|
||||||
|
# code = rdict["code"]
|
||||||
|
# st.markdown(":red[**Code in response dict:**]")
|
||||||
|
# st.code(code, language="python", wrap_lines=True, line_numbers=True)
|
||||||
|
# rdict.pop("code")
|
||||||
|
# elif "spec" in rdict:
|
||||||
|
# spec = rdict["spec"]
|
||||||
|
# st.markdown(":red[**Spec in response dict:**]")
|
||||||
|
# st.markdown(spec)
|
||||||
|
# rdict.pop("spec")
|
||||||
|
# else:
|
||||||
|
# # show model codes
|
||||||
|
# showed_keys = []
|
||||||
|
# for k, v in rdict.items():
|
||||||
|
# if k.startswith("model_") and k.endswith(".py"):
|
||||||
|
# st.markdown(f":red[**{k}**]")
|
||||||
|
# st.code(v, language="python", wrap_lines=True, line_numbers=True)
|
||||||
|
# showed_keys.append(k)
|
||||||
|
# for k in showed_keys:
|
||||||
|
# rdict.pop(k)
|
||||||
|
# st.write(":red[**Other parts (except for the code or spec) in response dict:**]")
|
||||||
|
# st.json(rdict)
|
||||||
|
# except:
|
||||||
|
# st.json(resp)
|
||||||
|
# with t2:
|
||||||
|
# show_text(user)
|
||||||
|
# with t3:
|
||||||
|
# show_text(system or "No system prompt available")
|
||||||
|
|
||||||
|
|
||||||
|
if total_pages:
|
||||||
|
# 初始化 current_loop
|
||||||
|
if "current_loop" not in st.session_state:
|
||||||
|
st.session_state["current_loop"] = 1
|
||||||
|
|
||||||
|
# Loop 导航按钮
|
||||||
|
col1, col2, col3, col4, col5 = st.sidebar.columns([1.2, 1, 2, 1, 1.2])
|
||||||
|
|
||||||
|
with col1:
|
||||||
|
if st.button("|<"): # 首页
|
||||||
|
st.session_state["current_loop"] = 1
|
||||||
|
with col2:
|
||||||
|
if st.button("<") and st.session_state["current_loop"] > 1: # 上一页
|
||||||
|
st.session_state["current_loop"] -= 1
|
||||||
|
with col3:
|
||||||
|
# 下拉列表显示所有 Loop
|
||||||
|
st.session_state["current_loop"] = st.selectbox(
|
||||||
|
"选择 Loop",
|
||||||
|
options=list(range(1, total_loops + 1)),
|
||||||
|
index=st.session_state["current_loop"] - 1, # 默认选中当前 Loop
|
||||||
|
label_visibility="collapsed", # 隐藏标签
|
||||||
|
)
|
||||||
|
with col4:
|
||||||
|
if st.button("\>") and st.session_state["current_loop"] < total_loops: # 下一页
|
||||||
|
st.session_state["current_loop"] += 1
|
||||||
|
with col5:
|
||||||
|
if st.button("\>|"): # 最后一页
|
||||||
|
st.session_state["current_loop"] = total_loops
|
||||||
|
|
||||||
|
# 获取当前 Loop
|
||||||
|
current_loop = st.session_state["current_loop"]
|
||||||
|
|
||||||
|
# 渲染当前 Loop 数据
|
||||||
|
loop_id = sorted_loop_ids[current_loop - 1]
|
||||||
|
progress_text = st.empty()
|
||||||
|
progress_text.text(f"正在处理 Loop {loop_id}...")
|
||||||
|
progress_bar.progress(current_loop / total_loops, text=f"Loop :green[**{current_loop}**] / {total_loops}")
|
||||||
|
|
||||||
|
# 渲染 Loop Header
|
||||||
|
loop_anchor = f"Loop_{loop_id}"
|
||||||
|
if loop_anchor not in tlist:
|
||||||
|
tlist.append(loop_anchor)
|
||||||
|
st.header(loop_anchor, anchor=loop_anchor, divider="blue")
|
||||||
|
|
||||||
|
# 渲染当前 Loop 的所有数据
|
||||||
|
loop_data = loop_groups[loop_id]
|
||||||
|
for d in loop_data:
|
||||||
|
tag = d["tag"]
|
||||||
|
obj = d["obj"]
|
||||||
|
_, func_name = extract_loopid_func_name(tag)
|
||||||
|
evo_id = extract_evoid(tag)
|
||||||
|
|
||||||
|
func_anchor = f"loop_{loop_id}.{func_name}"
|
||||||
|
if func_anchor not in tlist:
|
||||||
|
tlist.append(func_anchor)
|
||||||
|
st.header(f"in *{func_name}*", anchor=func_anchor, divider="green")
|
||||||
|
|
||||||
|
evo_anchor = f"loop_{loop_id}.evo_step_{evo_id}"
|
||||||
|
if evo_id and evo_anchor not in tlist:
|
||||||
|
tlist.append(evo_anchor)
|
||||||
|
st.subheader(f"evo_step_{evo_id}", anchor=evo_anchor, divider="orange")
|
||||||
|
|
||||||
|
# 根据 tag 渲染内容
|
||||||
|
if "debug_exp_gen" in tag:
|
||||||
|
with st.expander(
|
||||||
|
f"Exp in :violet[**{obj.experiment_workspace.workspace_path}**]", expanded=False, icon="🧩"
|
||||||
|
):
|
||||||
|
st.write(obj)
|
||||||
|
elif "debug_tpl" in tag:
|
||||||
|
uri = obj["uri"]
|
||||||
|
tpl = obj["template"]
|
||||||
|
cxt = obj["context"]
|
||||||
|
rd = obj["rendered"]
|
||||||
|
with st.expander(highlight_prompts_uri(uri), expanded=False, icon="⚙️"):
|
||||||
|
t1, t2, t3 = st.tabs([":green[**Rendered**]", ":blue[**Template**]", ":orange[**Context**]"])
|
||||||
|
with t1:
|
||||||
|
show_text(rd)
|
||||||
|
with t2:
|
||||||
|
show_text(tpl, lang="django")
|
||||||
|
with t3:
|
||||||
|
st.json(cxt)
|
||||||
|
elif "debug_llm" in tag:
|
||||||
|
system = obj.get("system", None)
|
||||||
|
user = obj["user"]
|
||||||
|
resp = obj["resp"]
|
||||||
|
with st.expander(f"**LLM**", expanded=False, icon="🤖"):
|
||||||
|
t1, t2, t3 = st.tabs([":green[**Response**]", ":blue[**User**]", ":orange[**System**]"])
|
||||||
|
with t1:
|
||||||
|
try:
|
||||||
|
rdict = json.loads(resp)
|
||||||
|
if "code" in rdict:
|
||||||
|
code = rdict["code"]
|
||||||
|
st.markdown(":red[**Code in response dict:**]")
|
||||||
|
st.code(code, language="python", wrap_lines=True, line_numbers=True)
|
||||||
|
rdict.pop("code")
|
||||||
|
elif "spec" in rdict:
|
||||||
|
spec = rdict["spec"]
|
||||||
|
st.markdown(":red[**Spec in response dict:**]")
|
||||||
|
st.markdown(spec)
|
||||||
|
rdict.pop("spec")
|
||||||
|
else:
|
||||||
|
# show model codes
|
||||||
|
showed_keys = []
|
||||||
|
for k, v in rdict.items():
|
||||||
|
if k.startswith("model_") and k.endswith(".py"):
|
||||||
|
st.markdown(f":red[**{k}**]")
|
||||||
|
st.code(v, language="python", wrap_lines=True, line_numbers=True)
|
||||||
|
showed_keys.append(k)
|
||||||
|
for k in showed_keys:
|
||||||
|
rdict.pop(k)
|
||||||
|
st.write(":red[**Other parts (except for the code or spec) in response dict:**]")
|
||||||
|
st.json(rdict)
|
||||||
|
except:
|
||||||
|
st.json(resp)
|
||||||
|
with t2:
|
||||||
|
show_text(user)
|
||||||
|
with t3:
|
||||||
|
show_text(system or "No system prompt available")
|
||||||
|
|
||||||
|
progress_text.text("当前 Loop 数据处理完成!")
|
||||||
|
|
||||||
|
# Sidebar TOC
|
||||||
|
with st.sidebar:
|
||||||
|
toc = "\n".join([f"- [{t}](#{t})" if t.startswith("L") else f" - [{t.split('.')[1]}](#{t})" for t in tlist])
|
||||||
|
st.markdown(toc, unsafe_allow_html=True)
|
||||||
@@ -36,16 +36,18 @@ def get_agent_model() -> OpenAIChatModel:
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
backend = APIBackend()
|
backend = APIBackend()
|
||||||
assert isinstance(backend, LiteLLMAPIBackend), "Only LiteLLMAPIBackend is supported"
|
if not isinstance(backend, LiteLLMAPIBackend):
|
||||||
|
raise TypeError("Only LiteLLMAPIBackend is supported")
|
||||||
|
|
||||||
compl_kwargs = backend.get_complete_kwargs()
|
compl_kwargs = backend.get_complete_kwargs()
|
||||||
|
|
||||||
selected_model = compl_kwargs["model"]
|
selected_model = compl_kwargs["model"]
|
||||||
|
|
||||||
_, custom_llm_provider, _, _ = get_llm_provider(selected_model)
|
_, custom_llm_provider, _, _ = get_llm_provider(selected_model)
|
||||||
assert (
|
if custom_llm_provider not in PROVIDER_TO_ENV_MAP:
|
||||||
custom_llm_provider in PROVIDER_TO_ENV_MAP
|
raise ValueError(
|
||||||
), f"Provider {custom_llm_provider} not supported. Please add it into `PROVIDER_TO_ENV_MAP`"
|
f"Provider {custom_llm_provider} not supported. Please add it into `PROVIDER_TO_ENV_MAP`"
|
||||||
|
)
|
||||||
prefix = PROVIDER_TO_ENV_MAP[custom_llm_provider]
|
prefix = PROVIDER_TO_ENV_MAP[custom_llm_provider]
|
||||||
api_key = os.getenv(f"{prefix}_API_KEY", None)
|
api_key = os.getenv(f"{prefix}_API_KEY", None)
|
||||||
api_base = os.getenv(f"{prefix}_API_BASE", None)
|
api_base = os.getenv(f"{prefix}_API_BASE", None)
|
||||||
|
|||||||
@@ -7,8 +7,10 @@ from sklearn.metrics import roc_auc_score
|
|||||||
def prepare_for_auroc_metric(submission: pd.DataFrame, answers: pd.DataFrame, id_col: str, target_col: str) -> dict:
|
def prepare_for_auroc_metric(submission: pd.DataFrame, answers: pd.DataFrame, id_col: str, target_col: str) -> dict:
|
||||||
|
|
||||||
# Answers checks
|
# Answers checks
|
||||||
assert id_col in answers.columns, f"answers dataframe should have an {id_col} column"
|
if id_col not in answers.columns:
|
||||||
assert target_col in answers.columns, f"answers dataframe should have a {target_col} column"
|
raise InvalidSubmissionError(f"answers dataframe should have an {id_col} column")
|
||||||
|
if target_col not in answers.columns:
|
||||||
|
raise InvalidSubmissionError(f"answers dataframe should have a {target_col} column")
|
||||||
|
|
||||||
# Submission checks
|
# Submission checks
|
||||||
if id_col not in submission.columns:
|
if id_col not in submission.columns:
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
# Check if our submission file exists
|
# Check if our submission file exists
|
||||||
assert Path("submission.csv").exists(), "Error: submission.csv not found"
|
if not Path("submission.csv").exists():
|
||||||
|
raise FileNotFoundError("Error: submission.csv not found")
|
||||||
|
|
||||||
submission_lines = Path("submission.csv").read_text().splitlines()
|
submission_lines = Path("submission.csv").read_text().splitlines()
|
||||||
test_lines = Path("submission_test.csv").read_text().splitlines()
|
test_lines = Path("submission_test.csv").read_text().splitlines()
|
||||||
|
|||||||
@@ -22,7 +22,8 @@ def prepare_for_metric(submission: pd.DataFrame, answers: pd.DataFrame) -> dict:
|
|||||||
if "price" not in submission.columns:
|
if "price" not in submission.columns:
|
||||||
raise InvalidSubmissionError("Submission DataFrame must contain 'price' columns.")
|
raise InvalidSubmissionError("Submission DataFrame must contain 'price' columns.")
|
||||||
|
|
||||||
assert "price" in answers.columns, "Answers DataFrame must contain 'price' columns."
|
if "price" not in answers.columns:
|
||||||
|
raise InvalidSubmissionError("Answers DataFrame must contain 'price' columns.")
|
||||||
|
|
||||||
if len(submission) != len(answers):
|
if len(submission) != len(answers):
|
||||||
raise InvalidSubmissionError("Submission must be the same length as the answers.")
|
raise InvalidSubmissionError("Submission must be the same length as the answers.")
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
# Check if our submission file exists
|
# Check if our submission file exists
|
||||||
assert Path("submission.csv").exists(), "Error: submission.csv not found"
|
if not Path("submission.csv").exists():
|
||||||
|
raise FileNotFoundError("Error: submission.csv not found")
|
||||||
|
|
||||||
submission_lines = Path("submission.csv").read_text().splitlines() # 自动生成的
|
submission_lines = Path("submission.csv").read_text().splitlines() # 自动生成的
|
||||||
test_lines = Path("submission_test.csv").read_text().splitlines() # test.csv
|
test_lines = Path("submission_test.csv").read_text().splitlines() # test.csv
|
||||||
|
|||||||
+6
-5
@@ -25,11 +25,12 @@ def prepare(raw: Path, public: Path, private: Path):
|
|||||||
new_test.to_csv(public / "test.csv", index=False)
|
new_test.to_csv(public / "test.csv", index=False)
|
||||||
|
|
||||||
# Checks
|
# Checks
|
||||||
assert new_test.shape[1] == 12, "Public test set should have 12 columns"
|
if new_test.shape[1] != 12:
|
||||||
assert new_train.shape[1] == 13, "Public train set should have 13 columns"
|
raise AssertionError("Public test set should have 12 columns")
|
||||||
assert len(new_train) + len(new_test) == len(
|
if new_train.shape[1] != 13:
|
||||||
old_train
|
raise AssertionError("Public train set should have 13 columns")
|
||||||
), "Length of new_train and new_test should equal length of old_train"
|
if len(new_train) + len(new_test) != len(old_train):
|
||||||
|
raise AssertionError("Length of new_train and new_test should equal length of old_train")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
@@ -182,7 +182,7 @@ class ExpGen2Hypothesis(DSProposalV2ExpGen):
|
|||||||
|
|
||||||
success_fb_list = list(set(trace_fbs))
|
success_fb_list = list(set(trace_fbs))
|
||||||
logger.info(
|
logger.info(
|
||||||
f"Merge Hypothesis: select {len(success_fb_list)} from {len(trace_fbs)} SOTA experiments found in {len(leaves)} traces"
|
f"Merge Hypothesis: select {len(success_fb_list)} from {len(trace_fbs)} SOTA experiments found in {len(leaves)} traces" # nosec B608 — not SQL, Bandit false positive on "select" in log message
|
||||||
)
|
)
|
||||||
|
|
||||||
if len(success_fb_list) > 0:
|
if len(success_fb_list) > 0:
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import ast
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import pickle
|
import pickle
|
||||||
@@ -292,7 +293,7 @@ class ValidationSelector(SOTAexpSelector):
|
|||||||
Sorts all valid experiments by score and returns the top N.
|
Sorts all valid experiments by score and returns the top N.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
mock_folder = f"/tmp/mock/{self.competition}"
|
mock_folder = f"/tmp/mock/{self.competition}" # nosec B108 — Docker volume mount point derived from internal competition name
|
||||||
|
|
||||||
try:
|
try:
|
||||||
data_py_code, grade_py_code = self._prepare_validation_scripts(
|
data_py_code, grade_py_code = self._prepare_validation_scripts(
|
||||||
@@ -539,7 +540,7 @@ def process_experiment(
|
|||||||
|
|
||||||
# Run main script
|
# Run main script
|
||||||
env = get_ds_env(
|
env = get_ds_env(
|
||||||
extra_volumes={f"/tmp/mock/{competition}/{input_folder}": input_folder},
|
extra_volumes={f"/tmp/mock/{competition}/{input_folder}": input_folder}, # nosec B108 — Docker volume mount point derived from internal competition name
|
||||||
running_timeout_period=DS_RD_SETTING.full_timeout,
|
running_timeout_period=DS_RD_SETTING.full_timeout,
|
||||||
)
|
)
|
||||||
result = ws.run(env=env, entry="python main.py")
|
result = ws.run(env=env, entry="python main.py")
|
||||||
@@ -587,8 +588,8 @@ def _parsing_score(grade_stdout: str) -> Optional[float]:
|
|||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
try:
|
try:
|
||||||
# Priority 2: Eval dict
|
# Priority 2: safe literal eval for Python-style dicts
|
||||||
return float(eval(json_str)["score"])
|
return float(ast.literal_eval(json_str)["score"])
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -35,10 +35,11 @@ def select(X: pd.DataFrame) -> pd.DataFrame:
|
|||||||
class KGModelFeatureSelectionCoder(Developer[KGModelExperiment]):
|
class KGModelFeatureSelectionCoder(Developer[KGModelExperiment]):
|
||||||
def develop(self, exp: KGModelExperiment) -> KGModelExperiment:
|
def develop(self, exp: KGModelExperiment) -> KGModelExperiment:
|
||||||
target_model_type = exp.sub_tasks[0].model_type
|
target_model_type = exp.sub_tasks[0].model_type
|
||||||
assert target_model_type in KG_SELECT_MAPPING
|
if target_model_type not in KG_SELECT_MAPPING:
|
||||||
|
raise ValueError(f"target_model_type {target_model_type} not in KG_SELECT_MAPPING")
|
||||||
if len(exp.experiment_workspace.data_description) == 1:
|
if len(exp.experiment_workspace.data_description) == 1:
|
||||||
code = (
|
code = (
|
||||||
Environment(undefined=StrictUndefined)
|
Environment(undefined=StrictUndefined) # nosec B701 — renders Python code templates, not HTML; autoescape would corrupt code
|
||||||
.from_string(DEFAULT_SELECTION_CODE)
|
.from_string(DEFAULT_SELECTION_CODE)
|
||||||
.render(feature_index_list=None)
|
.render(feature_index_list=None)
|
||||||
)
|
)
|
||||||
@@ -62,7 +63,7 @@ class KGModelFeatureSelectionCoder(Developer[KGModelExperiment]):
|
|||||||
chosen_index_to_list_index = [i - 1 for i in chosen_index]
|
chosen_index_to_list_index = [i - 1 for i in chosen_index]
|
||||||
|
|
||||||
code = (
|
code = (
|
||||||
Environment(undefined=StrictUndefined)
|
Environment(undefined=StrictUndefined) # nosec B701 — renders Python code templates, not HTML; autoescape would corrupt code
|
||||||
.from_string(DEFAULT_SELECTION_CODE)
|
.from_string(DEFAULT_SELECTION_CODE)
|
||||||
.render(feature_index_list=chosen_index_to_list_index)
|
.render(feature_index_list=chosen_index_to_list_index)
|
||||||
)
|
)
|
||||||
|
|||||||
+6
-6
@@ -79,12 +79,12 @@ def preprocess_script():
|
|||||||
This method applies the preprocessing steps to the training, validation, and test datasets.
|
This method applies the preprocessing steps to the training, validation, and test datasets.
|
||||||
"""
|
"""
|
||||||
if os.path.exists("/kaggle/input/X_train.pkl"):
|
if os.path.exists("/kaggle/input/X_train.pkl"):
|
||||||
X_train = pd.read_pickle("/kaggle/input/X_train.pkl")
|
X_train = pd.read_pickle("/kaggle/input/X_train.pkl") # nosec B301 — trusted Kaggle input
|
||||||
X_valid = pd.read_pickle("/kaggle/input/X_valid.pkl")
|
X_valid = pd.read_pickle("/kaggle/input/X_valid.pkl") # nosec B301
|
||||||
y_train = pd.read_pickle("/kaggle/input/y_train.pkl")
|
y_train = pd.read_pickle("/kaggle/input/y_train.pkl") # nosec B301
|
||||||
y_valid = pd.read_pickle("/kaggle/input/y_valid.pkl")
|
y_valid = pd.read_pickle("/kaggle/input/y_valid.pkl") # nosec B301
|
||||||
X_test = pd.read_pickle("/kaggle/input/X_test.pkl")
|
X_test = pd.read_pickle("/kaggle/input/X_test.pkl") # nosec B301
|
||||||
others = pd.read_pickle("/kaggle/input/others.pkl")
|
others = pd.read_pickle("/kaggle/input/others.pkl") # nosec B301
|
||||||
y_train = pd.Series(y_train).reset_index(drop=True)
|
y_train = pd.Series(y_train).reset_index(drop=True)
|
||||||
y_valid = pd.Series(y_valid).reset_index(drop=True)
|
y_valid = pd.Series(y_valid).reset_index(drop=True)
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
|
import sys
|
||||||
import os
|
import os
|
||||||
|
import logging
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
"""
|
"""
|
||||||
Qlib Factor Runner - Executes factor backtests in Docker.
|
Qlib Factor Runner - Executes factor backtests in Docker.
|
||||||
@@ -965,7 +967,7 @@ class QlibFactorRunner(CachedRunner[QlibFactorExperiment]):
|
|||||||
shutil.copy(str(full_data), str(tmp / "intraday_pv.h5"))
|
shutil.copy(str(full_data), str(tmp / "intraday_pv.h5"))
|
||||||
|
|
||||||
ret = subprocess.run(
|
ret = subprocess.run(
|
||||||
["python", "factor.py"],
|
["sys.executable", "factor.py"],
|
||||||
cwd=str(tmp),
|
cwd=str(tmp),
|
||||||
capture_output=True,
|
capture_output=True,
|
||||||
timeout=300,
|
timeout=300,
|
||||||
@@ -1000,7 +1002,7 @@ class QlibFactorRunner(CachedRunner[QlibFactorExperiment]):
|
|||||||
series.to_frame().to_parquet(str(parquet_path))
|
series.to_frame().to_parquet(str(parquet_path))
|
||||||
|
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
logging.debug("Error in save_factor_values_to_parquet", exc_info=True)
|
||||||
|
|
||||||
def _log_result_warnings(self, factor_name: str, result, metrics: dict) -> None:
|
def _log_result_warnings(self, factor_name: str, result, metrics: dict) -> None:
|
||||||
"""
|
"""
|
||||||
|
|||||||
@@ -241,6 +241,7 @@ class StrategyBuilder:
|
|||||||
if data.get("status") == "success" and data.get("ic") is not None:
|
if data.get("status") == "success" and data.get("ic") is not None:
|
||||||
factors.append(data)
|
factors.append(data)
|
||||||
except Exception:
|
except Exception:
|
||||||
|
logger.warning("Failed to load factor file %s", f, exc_info=True)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# Sort by absolute IC
|
# Sort by absolute IC
|
||||||
|
|||||||
@@ -30,7 +30,8 @@ def _build_execute_calls(exp: QlibFactorExperiment, base_feature_workspaces: lis
|
|||||||
execute_calls = []
|
execute_calls = []
|
||||||
|
|
||||||
if exp.sub_tasks:
|
if exp.sub_tasks:
|
||||||
assert isinstance(exp.prop_dev_feedback, CoSTEERMultiFeedback)
|
if not isinstance(exp.prop_dev_feedback, CoSTEERMultiFeedback):
|
||||||
|
raise TypeError("exp.prop_dev_feedback must be of type CoSTEERMultiFeedback")
|
||||||
execute_calls.extend(
|
execute_calls.extend(
|
||||||
(implementation.execute, ("All",))
|
(implementation.execute, ("All",))
|
||||||
for implementation, feedback in zip(exp.sub_workspace_list, exp.prop_dev_feedback)
|
for implementation, feedback in zip(exp.sub_workspace_list, exp.prop_dev_feedback)
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import shutil
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
from jinja2 import Environment, StrictUndefined
|
from jinja2 import Environment, StrictUndefined, select_autoescape
|
||||||
|
|
||||||
from rdagent.components.coder.factor_coder.config import FACTOR_COSTEER_SETTINGS
|
from rdagent.components.coder.factor_coder.config import FACTOR_COSTEER_SETTINGS
|
||||||
from rdagent.utils.env import QTDockerEnv
|
from rdagent.utils.env import QTDockerEnv
|
||||||
@@ -21,14 +21,16 @@ def generate_data_folder_from_qlib():
|
|||||||
entry=f"python generate.py",
|
entry=f"python generate.py",
|
||||||
)
|
)
|
||||||
|
|
||||||
assert (Path(__file__).parent / "factor_data_template" / "intraday_pv_all.h5").exists(), (
|
if not (Path(__file__).parent / "factor_data_template" / "intraday_pv_all.h5").exists():
|
||||||
"intraday_pv_all.h5 is not generated. It means rdagent/scenarios/qlib/experiment/factor_data_template/generate.py is not executed correctly. Please check the log: \n"
|
raise FileNotFoundError(
|
||||||
+ execute_log
|
"intraday_pv_all.h5 is not generated. It means rdagent/scenarios/qlib/experiment/factor_data_template/generate.py is not executed correctly. Please check the log: \n"
|
||||||
)
|
+ execute_log
|
||||||
assert (Path(__file__).parent / "factor_data_template" / "intraday_pv_debug.h5").exists(), (
|
)
|
||||||
"intraday_pv_debug.h5 is not generated. It means rdagent/scenarios/qlib/experiment/factor_data_template/generate.py is not executed correctly. Please check the log: \n"
|
if not (Path(__file__).parent / "factor_data_template" / "intraday_pv_debug.h5").exists():
|
||||||
+ execute_log
|
raise FileNotFoundError(
|
||||||
)
|
"intraday_pv_debug.h5 is not generated. It means rdagent/scenarios/qlib/experiment/factor_data_template/generate.py is not executed correctly. Please check the log: \n"
|
||||||
|
+ execute_log
|
||||||
|
)
|
||||||
|
|
||||||
Path(FACTOR_COSTEER_SETTINGS.data_folder).mkdir(parents=True, exist_ok=True)
|
Path(FACTOR_COSTEER_SETTINGS.data_folder).mkdir(parents=True, exist_ok=True)
|
||||||
shutil.copy(
|
shutil.copy(
|
||||||
@@ -67,7 +69,7 @@ def get_file_desc(p: Path, variable_list=[]) -> str:
|
|||||||
"""
|
"""
|
||||||
p = Path(p)
|
p = Path(p)
|
||||||
|
|
||||||
JJ_TPL = Environment(undefined=StrictUndefined).from_string("""
|
JJ_TPL = Environment(undefined=StrictUndefined, autoescape=select_autoescape()).from_string("""
|
||||||
# {{file_name}}
|
# {{file_name}}
|
||||||
|
|
||||||
## File Type
|
## File Type
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import logging
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
from typing import List, Tuple
|
from typing import List, Tuple
|
||||||
@@ -38,7 +39,7 @@ def _build_compressed_history(trace: Trace, max_history: int) -> str:
|
|||||||
if exp.result is not None and "IC" in exp.result.index:
|
if exp.result is not None and "IC" in exp.result.index:
|
||||||
ic_str = f" IC={exp.result.loc['IC']:.4f}"
|
ic_str = f" IC={exp.result.loc['IC']:.4f}"
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
logging.debug("Exception caught", exc_info=True)
|
||||||
decision = "PASS" if fb.decision else "FAIL"
|
decision = "PASS" if fb.decision else "FAIL"
|
||||||
obs = (fb.observations or "")[:120].replace("\n", " ")
|
obs = (fb.observations or "")[:120].replace("\n", " ")
|
||||||
lines.append(f"- [{decision}]{ic_str} {', '.join(names) or 'unknown'}: {obs}")
|
lines.append(f"- [{decision}]{ic_str} {', '.join(names) or 'unknown'}: {obs}")
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import logging
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import random
|
import random
|
||||||
@@ -175,7 +176,7 @@ class QlibQuantHypothesisGen(FactorAndModelHypothesisGen):
|
|||||||
ic_val = exp.result.loc["IC"] if "IC" in exp.result.index else ""
|
ic_val = exp.result.loc["IC"] if "IC" in exp.result.index else ""
|
||||||
ic_str = f" IC={ic_val:.4f}" if ic_val != "" else ""
|
ic_str = f" IC={ic_val:.4f}" if ic_val != "" else ""
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
logging.debug("Error getting IC", exc_info=True)
|
||||||
decision_str = "PASS" if fb.decision else "FAIL"
|
decision_str = "PASS" if fb.decision else "FAIL"
|
||||||
obs_short = (fb.observations or "")[:120].replace("\n", " ")
|
obs_short = (fb.observations or "")[:120].replace("\n", " ")
|
||||||
summary_lines.append(f"- [{decision_str}]{ic_str} {names_str}: {obs_short}")
|
summary_lines.append(f"- [{decision_str}]{ic_str} {names_str}: {obs_short}")
|
||||||
|
|||||||
@@ -77,6 +77,7 @@ def count_valid_factors() -> int:
|
|||||||
if data.get("status") == "success" and data.get("ic") is not None:
|
if data.get("status") == "success" and data.get("ic") is not None:
|
||||||
count += 1
|
count += 1
|
||||||
except Exception:
|
except Exception:
|
||||||
|
logger.warning("Failed to load factor file %s", json_file, exc_info=True)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
return count
|
return count
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ def submit_for_grading(grading_url: str, model_path: str) -> dict | None:
|
|||||||
def main():
|
def main():
|
||||||
MODEL_PATH = os.environ.get("MODEL_PATH")
|
MODEL_PATH = os.environ.get("MODEL_PATH")
|
||||||
DATA_PATH = os.environ.get("DATA_PATH")
|
DATA_PATH = os.environ.get("DATA_PATH")
|
||||||
OUTPUT_DIR = os.environ.get("OUTPUT_DIR", "/tmp/autorl_output")
|
OUTPUT_DIR = os.environ.get("OUTPUT_DIR", "/tmp/autorl_output") # nosec B108 — Docker container output dir, configurable via env var
|
||||||
GRADING_SERVER_URL = os.environ.get("GRADING_SERVER_URL", "")
|
GRADING_SERVER_URL = os.environ.get("GRADING_SERVER_URL", "")
|
||||||
TRAIN_RATIO = float(os.environ.get("TRAIN_RATIO", "0.05"))
|
TRAIN_RATIO = float(os.environ.get("TRAIN_RATIO", "0.05"))
|
||||||
NUM_EPOCHS = int(os.environ.get("NUM_EPOCHS", "3"))
|
NUM_EPOCHS = int(os.environ.get("NUM_EPOCHS", "3"))
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ peft>=0.18.1
|
|||||||
|
|
||||||
# Evaluation
|
# Evaluation
|
||||||
opencompass==0.5.1
|
opencompass==0.5.1
|
||||||
setuptools<75 # uv venv doesn't include, opencompass depends on pkg_resources
|
setuptools>=78.1.1 # Security fix: GHSA-8g6x-3r52-4m6c (path traversal in PackageIndex.download, arbitrary file write/RCE)
|
||||||
|
|
||||||
# Inference acceleration (optional, TRL supports 0.10.2-0.12.0)
|
# Inference acceleration (optional, TRL supports 0.10.2-0.12.0)
|
||||||
# Security: Version >=0.14.0 fixes CVE-2026-22807 (RCE via auto_map dynamic module loading)
|
# Security: Version >=0.14.0 fixes CVE-2026-22807 (RCE via auto_map dynamic module loading)
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ from pathlib import Path
|
|||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
import yaml
|
import yaml
|
||||||
from jinja2 import Environment, FunctionLoader, StrictUndefined
|
from jinja2 import Environment, FunctionLoader, StrictUndefined, select_autoescape
|
||||||
|
|
||||||
from rdagent.core.conf import RD_AGENT_SETTINGS
|
from rdagent.core.conf import RD_AGENT_SETTINGS
|
||||||
from rdagent.log import rdagent_logger as logger
|
from rdagent.log import rdagent_logger as logger
|
||||||
@@ -38,7 +38,8 @@ def load_content(uri: str, caller_dir: Path | None = None, ftype: str = "yaml")
|
|||||||
caller_dir = get_caller_dir(upshift=1)
|
caller_dir = get_caller_dir(upshift=1)
|
||||||
# Parse the URI
|
# Parse the URI
|
||||||
path_part, *yaml_trace = uri.split(":")
|
path_part, *yaml_trace = uri.split(":")
|
||||||
assert len(yaml_trace) <= 1, f"Invalid uri {uri}, only one yaml trace is allowed."
|
if len(yaml_trace) > 1:
|
||||||
|
raise ValueError(f"Invalid uri {uri}, only one yaml trace is allowed.")
|
||||||
yaml_trace = [key for yt in yaml_trace for key in yt.split(".")]
|
yaml_trace = [key for yt in yaml_trace for key in yt.split(".")]
|
||||||
|
|
||||||
# load file_path with priorities.
|
# load file_path with priorities.
|
||||||
@@ -126,7 +127,7 @@ class RDAT:
|
|||||||
# loader=FunctionLoader(load_conent) is for supporting grammar like below.
|
# loader=FunctionLoader(load_conent) is for supporting grammar like below.
|
||||||
# `{% include "scenarios.data_science.share:component_spec.DataLoadSpec" %}`
|
# `{% include "scenarios.data_science.share:component_spec.DataLoadSpec" %}`
|
||||||
rendered = (
|
rendered = (
|
||||||
Environment(undefined=StrictUndefined, loader=FunctionLoader(load_content))
|
Environment(undefined=StrictUndefined, loader=FunctionLoader(load_content), autoescape=select_autoescape())
|
||||||
.from_string(self.template)
|
.from_string(self.template)
|
||||||
.render(**context)
|
.render(**context)
|
||||||
.strip("\n")
|
.strip("\n")
|
||||||
|
|||||||
+25
-24
@@ -614,7 +614,7 @@ class LocalEnv(Env[ASpecificLocalConf]):
|
|||||||
if self.conf.extra_volumes is not None:
|
if self.conf.extra_volumes is not None:
|
||||||
for lp, rp in self.conf.extra_volumes.items():
|
for lp, rp in self.conf.extra_volumes.items():
|
||||||
volumes[lp] = rp["bind"] if isinstance(rp, dict) else rp
|
volumes[lp] = rp["bind"] if isinstance(rp, dict) else rp
|
||||||
cache_path = "/tmp/sample" if "/sample/" in "".join(self.conf.extra_volumes.keys()) else "/tmp/full"
|
cache_path = "/tmp/sample" if "/sample/" in "".join(self.conf.extra_volumes.keys()) else "/tmp/full" # nosec B108 — fixed Docker volume mount point, not a user-writable temp file
|
||||||
Path(cache_path).mkdir(parents=True, exist_ok=True)
|
Path(cache_path).mkdir(parents=True, exist_ok=True)
|
||||||
volumes[cache_path] = T("scenarios.data_science.share:scen.cache_path").r()
|
volumes[cache_path] = T("scenarios.data_science.share:scen.cache_path").r()
|
||||||
for lp, rp in running_extra_volume.items():
|
for lp, rp in running_extra_volume.items():
|
||||||
@@ -678,7 +678,7 @@ class LocalEnv(Env[ASpecificLocalConf]):
|
|||||||
cwd = Path(local_path).resolve() if local_path else None
|
cwd = Path(local_path).resolve() if local_path else None
|
||||||
env = {k: str(v) if isinstance(v, int) else v for k, v in env.items()}
|
env = {k: str(v) if isinstance(v, int) else v for k, v in env.items()}
|
||||||
|
|
||||||
process = subprocess.Popen(
|
process = subprocess.Popen( # nosec B602 — entry is an internal command string set by LocalEnvConf, not user input
|
||||||
entry,
|
entry,
|
||||||
cwd=cwd,
|
cwd=cwd,
|
||||||
env={**os.environ, **env},
|
env={**os.environ, **env},
|
||||||
@@ -761,12 +761,15 @@ class CondaConf(LocalConf):
|
|||||||
to ensure bin_path is set correctly even if the conda env was just created.
|
to ensure bin_path is set correctly even if the conda env was just created.
|
||||||
"""
|
"""
|
||||||
conda_path_result = subprocess.run(
|
conda_path_result = subprocess.run(
|
||||||
f"conda run -n {self.conda_env_name} --no-capture-output env | grep '^PATH='",
|
["conda", "run", "-n", self.conda_env_name, "--no-capture-output", "env"],
|
||||||
capture_output=True,
|
capture_output=True,
|
||||||
text=True,
|
text=True,
|
||||||
shell=True,
|
|
||||||
)
|
)
|
||||||
self.bin_path = conda_path_result.stdout.strip().split("=")[1] if conda_path_result.returncode == 0 else ""
|
if conda_path_result.returncode == 0:
|
||||||
|
path_lines = [l for l in conda_path_result.stdout.splitlines() if l.startswith("PATH=")]
|
||||||
|
self.bin_path = path_lines[0].split("=", 1)[1] if path_lines else ""
|
||||||
|
else:
|
||||||
|
self.bin_path = ""
|
||||||
|
|
||||||
|
|
||||||
class MLECondaConf(CondaConf):
|
class MLECondaConf(CondaConf):
|
||||||
@@ -850,24 +853,22 @@ class QlibCondaEnv(LocalEnv[QlibCondaConf]):
|
|||||||
def prepare(self) -> None:
|
def prepare(self) -> None:
|
||||||
"""Prepare the conda environment if not already created."""
|
"""Prepare the conda environment if not already created."""
|
||||||
try:
|
try:
|
||||||
envs = subprocess.run("conda env list", capture_output=True, text=True, shell=True)
|
envs = subprocess.run(["conda", "env", "list"], capture_output=True, text=True)
|
||||||
if self.conf.conda_env_name not in envs.stdout:
|
if self.conf.conda_env_name not in envs.stdout:
|
||||||
print(f"[yellow]Conda env '{self.conf.conda_env_name}' not found, creating...[/yellow]")
|
print(f"[yellow]Conda env '{self.conf.conda_env_name}' not found, creating...[/yellow]")
|
||||||
subprocess.check_call(
|
subprocess.check_call(
|
||||||
f"conda create -y -n {self.conf.conda_env_name} python=3.10",
|
["conda", "create", "-y", "-n", self.conf.conda_env_name, "python=3.10"],
|
||||||
shell=True,
|
|
||||||
)
|
)
|
||||||
subprocess.check_call(
|
subprocess.check_call(
|
||||||
f"conda run -n {self.conf.conda_env_name} pip install --upgrade pip cython",
|
["conda", "run", "-n", self.conf.conda_env_name, "pip", "install", "--upgrade", "pip", "cython"],
|
||||||
shell=True,
|
|
||||||
)
|
)
|
||||||
subprocess.check_call(
|
subprocess.check_call(
|
||||||
f"conda run -n {self.conf.conda_env_name} pip install git+https://github.com/microsoft/qlib.git@2fb9380b342556ddb50a4b24e4fe8655d548b2b8",
|
["conda", "run", "-n", self.conf.conda_env_name, "pip", "install",
|
||||||
shell=True,
|
"git+https://github.com/microsoft/qlib.git@2fb9380b342556ddb50a4b24e4fe8655d548b2b8"],
|
||||||
)
|
)
|
||||||
subprocess.check_call(
|
subprocess.check_call(
|
||||||
f"conda run -n {self.conf.conda_env_name} pip install catboost xgboost tables torch",
|
["conda", "run", "-n", self.conf.conda_env_name, "pip", "install",
|
||||||
shell=True,
|
"catboost", "xgboost", "tables", "torch"],
|
||||||
)
|
)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@@ -888,10 +889,9 @@ def _sync_conda_cache_with_real_envs() -> None:
|
|||||||
"""Ensure the prepared cache includes environments that already exist on disk."""
|
"""Ensure the prepared cache includes environments that already exist on disk."""
|
||||||
try:
|
try:
|
||||||
result = subprocess.run(
|
result = subprocess.run(
|
||||||
"conda env list",
|
["conda", "env", "list"],
|
||||||
capture_output=True,
|
capture_output=True,
|
||||||
text=True,
|
text=True,
|
||||||
shell=True,
|
|
||||||
check=False,
|
check=False,
|
||||||
)
|
)
|
||||||
except Exception as exc: # pragma: no cover - best-effort helper
|
except Exception as exc: # pragma: no cover - best-effort helper
|
||||||
@@ -924,14 +924,15 @@ def _prepare_conda_env(env_name: str, requirements_file: Path, python_version: s
|
|||||||
python_version: Python version for the environment
|
python_version: Python version for the environment
|
||||||
"""
|
"""
|
||||||
# 1. Create conda environment if not exists
|
# 1. Create conda environment if not exists
|
||||||
result = subprocess.run(f"conda env list | grep -q '^{env_name} '", shell=True)
|
env_list = subprocess.run(["conda", "env", "list"], capture_output=True, text=True, check=False)
|
||||||
if result.returncode != 0:
|
env_exists = any(line.split()[0] == env_name for line in env_list.stdout.splitlines() if line and not line.startswith("#"))
|
||||||
|
if not env_exists:
|
||||||
print(f"[yellow]Creating conda env '{env_name}' (Python {python_version})...[/yellow]")
|
print(f"[yellow]Creating conda env '{env_name}' (Python {python_version})...[/yellow]")
|
||||||
subprocess.check_call(f"conda create -y -n {env_name} python={python_version}", shell=True)
|
subprocess.check_call(["conda", "create", "-y", "-n", env_name, f"python={python_version}"])
|
||||||
subprocess.check_call(f"conda run -n {env_name} pip install --upgrade pip", shell=True)
|
subprocess.check_call(["conda", "run", "-n", env_name, "pip", "install", "--upgrade", "pip"])
|
||||||
|
|
||||||
print(f"[yellow]Installing dependencies from {requirements_file.name}...[/yellow]")
|
print(f"[yellow]Installing dependencies from {requirements_file.name}...[/yellow]")
|
||||||
subprocess.check_call(f"conda run -n {env_name} pip install -r {requirements_file}", shell=True)
|
subprocess.check_call(["conda", "run", "-n", env_name, "pip", "install", "-r", str(requirements_file)])
|
||||||
print(f"[green]Conda env '{env_name}' ready[/green]")
|
print(f"[green]Conda env '{env_name}' ready[/green]")
|
||||||
|
|
||||||
_CONDA_ENV_PREPARED.add(env_name)
|
_CONDA_ENV_PREPARED.add(env_name)
|
||||||
@@ -971,8 +972,8 @@ class FTCondaEnv(LocalEnv[FTCondaConf]):
|
|||||||
# Note: flash-attn>=2.8 is required for B200 (sm_100) support
|
# Note: flash-attn>=2.8 is required for B200 (sm_100) support
|
||||||
print("[yellow]Installing flash-attn (compiling, may take a few minutes)...[/yellow]")
|
print("[yellow]Installing flash-attn (compiling, may take a few minutes)...[/yellow]")
|
||||||
subprocess.check_call(
|
subprocess.check_call(
|
||||||
f"conda run -n {self.conf.conda_env_name} pip install 'flash-attn>=2.8' --no-build-isolation --no-cache-dir",
|
["conda", "run", "-n", self.conf.conda_env_name, "pip", "install",
|
||||||
shell=True,
|
"flash-attn>=2.8", "--no-build-isolation", "--no-cache-dir"],
|
||||||
)
|
)
|
||||||
|
|
||||||
# Re-update bin_path after prepare() in case the conda env was just created
|
# Re-update bin_path after prepare() in case the conda env was just created
|
||||||
@@ -1442,7 +1443,7 @@ class DockerEnv(Env[DockerConf]):
|
|||||||
if self.conf.extra_volumes is not None:
|
if self.conf.extra_volumes is not None:
|
||||||
for lp, rp in self.conf.extra_volumes.items():
|
for lp, rp in self.conf.extra_volumes.items():
|
||||||
volumes[lp] = rp if isinstance(rp, dict) else {"bind": rp, "mode": self.conf.extra_volume_mode}
|
volumes[lp] = rp if isinstance(rp, dict) else {"bind": rp, "mode": self.conf.extra_volume_mode}
|
||||||
cache_path = "/tmp/sample" if "/sample/" in "".join(self.conf.extra_volumes.keys()) else "/tmp/full"
|
cache_path = "/tmp/sample" if "/sample/" in "".join(self.conf.extra_volumes.keys()) else "/tmp/full" # nosec B108 — fixed Docker volume mount point, not a user-writable temp file
|
||||||
Path(cache_path).mkdir(parents=True, exist_ok=True)
|
Path(cache_path).mkdir(parents=True, exist_ok=True)
|
||||||
volumes[cache_path] = {
|
volumes[cache_path] = {
|
||||||
"bind": T("scenarios.data_science.share:scen.cache_path").r(),
|
"bind": T("scenarios.data_science.share:scen.cache_path").r(),
|
||||||
|
|||||||
@@ -30,7 +30,8 @@ def wait_retry(
|
|||||||
>>> counter
|
>>> counter
|
||||||
2
|
2
|
||||||
"""
|
"""
|
||||||
assert retry_n > 0, "retry_n should be greater than 0"
|
if retry_n <= 0:
|
||||||
|
raise ValueError("retry_n should be greater than 0")
|
||||||
|
|
||||||
def decorator(f: Callable[..., ASpecificRet]) -> Callable[..., ASpecificRet]:
|
def decorator(f: Callable[..., ASpecificRet]) -> Callable[..., ASpecificRet]:
|
||||||
def wrapper(*args: Any, **kwargs: Any) -> ASpecificRet:
|
def wrapper(*args: Any, **kwargs: Any) -> ASpecificRet:
|
||||||
|
|||||||
+4
-4
@@ -9,8 +9,8 @@ psutil
|
|||||||
fire
|
fire
|
||||||
fuzzywuzzy
|
fuzzywuzzy
|
||||||
openai
|
openai
|
||||||
litellm>=1.73 # to support `from litellm import get_valid_models`
|
litellm>=1.83.14 # to support `from litellm import get_valid_models`
|
||||||
aiohttp>=3.13.4 # CVE-2026-22815, CVE-2026-34515, CVE-2026-34516, CVE-2026-34525
|
aiohttp>=3.13.4 # CVE-2026-22815, CVE-2026-34515, CVE-2026-34516, CVE-2026-34525; >=3.13.4 due to litellm==1.83.14 exact pin
|
||||||
azure.identity
|
azure.identity
|
||||||
pyarrow
|
pyarrow
|
||||||
rich
|
rich
|
||||||
@@ -98,8 +98,8 @@ optuna>=3.5.0
|
|||||||
beautifulsoup4>=4.12.0
|
beautifulsoup4>=4.12.0
|
||||||
|
|
||||||
# ML Training Pipeline
|
# ML Training Pipeline
|
||||||
lightgbm>=3.3.0
|
lightgbm>=3.3.5
|
||||||
scipy>=1.9.0
|
scipy>=1.15.3
|
||||||
|
|
||||||
# RL Trading (optional - system works without these)
|
# RL Trading (optional - system works without these)
|
||||||
# Install for full RL training: pip install stable-baselines3[extra] gymnasium
|
# Install for full RL training: pip install stable-baselines3[extra] gymnasium
|
||||||
|
|||||||
+1
-1
@@ -8,7 +8,7 @@
|
|||||||
# Only install if you want to use full PPO/A2C/SAC training.
|
# Only install if you want to use full PPO/A2C/SAC training.
|
||||||
|
|
||||||
# Core RL library
|
# Core RL library
|
||||||
stable-baselines3[extra]>=2.0.0
|
stable-baselines3[extra]>=2.8.0
|
||||||
|
|
||||||
# Gymnasium environment (OpenAI Gym successor)
|
# Gymnasium environment (OpenAI Gym successor)
|
||||||
gymnasium>=0.29.0
|
gymnasium>=0.29.0
|
||||||
|
|||||||
Reference in New Issue
Block a user