FinceptTerminal is a modern finance application offering advanced market analytics, investment research, and economic data tools, designed for interactive exploration and data-driven decision-making in a user-friendly environment.
C++ ai-agentsalgorithmic-tradingbloomberg-terminalcppfinancefinancial-marketsfintechgood-first-issueinvestmentinvestment-researchmachine-learningopensourcepythonqtquantitative-financestock-markettrading
feat(agents): set FINAGENT_DATA_DIR env on all Python spawn sites
tilakpatel226fb6bff7b447
diff --git a/fincept-qt/src/python/PythonRunner.cpp b/fincept-qt/src/python/PythonRunner.cppindex b9d728bb9..19a3dc144 100644--- a/fincept-qt/src/python/PythonRunner.cpp+++ b/fincept-qt/src/python/PythonRunner.cpp@@ -52,6 +52,43 @@ QString PythonRunner::scripts_dir() const {return scripts_dir_;}+// ── Standard Python environment ──────────────────────────────────────────────+// Shared by PythonRunner::run() and external direct-QProcess callers+// (e.g., AgentService's stdin/stream paths). Anything every Python spawn needs+// — encoding pins, FINCEPT_DATA_DIR, FINAGENT_DATA_DIR, base PYTHONPATH — lives+// here. Script-specific path additions (parent-of-pkg) stay in run().+QProcessEnvironment PythonRunner::build_python_env() const {+ QProcessEnvironment env = QProcessEnvironment::systemEnvironment();+ env.insert("PYTHONIOENCODING", "utf-8");+ env.insert("PYTHONDONTWRITEBYTECODE", "1");+ env.insert("PYTHONUNBUFFERED", "1");++ const QString install_dir = PythonSetupManager::instance().install_dir();+ env.insert("FINCEPT_DATA_DIR", install_dir);++ // FINAGENT_DATA_DIR: default to <FINCEPT_DATA_DIR>/finagent. Always overwrite+ // so we match what resources.py expects regardless of what the user shell+ // inherited — the app owns its own per-persona storage root.+ env.insert("FINAGENT_DATA_DIR", install_dir + "/finagent");++ // FINAGENT_RUNTIME_CACHE_SIZE: respect user override if already set; otherwise+ // leave unset so persona_registry.py uses its own default (8).+ // (No insert here by design.)++ // Base PYTHONPATH: scripts_dir on front, preserve whatever the user had.+#ifdef _WIN32+ const QChar kPathSep = ';';+#else+ const QChar kPathSep = ':';+#endif+ const QString existing_pypath = env.value("PYTHONPATH");+ QString new_pypath =+ existing_pypath.isEmpty() ? scripts_dir_ : (scripts_dir_ + kPathSep + existing_pypath);+ env.insert("PYTHONPATH", new_pypath);++ return env;+}+// ── Venv routing ─────────────────────────────────────────────────────────────// Routes scripts to the correct venv based on their name.@@ -308,30 +345,27 @@ void PythonRunner::start_next() {}}- // Set environment for clean Python execution- QProcessEnvironment env = QProcessEnvironment::systemEnvironment();- env.insert("PYTHONIOENCODING", "utf-8");- env.insert("PYTHONDONTWRITEBYTECODE", "1");- env.insert("PYTHONUNBUFFERED", "1");- env.insert("FINCEPT_DATA_DIR", PythonSetupManager::instance().install_dir());+ // Start from the shared base env (encoding pins, FINCEPT_DATA_DIR,+ // FINAGENT_DATA_DIR, base PYTHONPATH = scripts_dir_).+ QProcessEnvironment env = build_python_env();- const QString existing_pypath = env.value("PYTHONPATH");+ // Script-specific: if the script is inside a sub-package (contains '/'),+ // prepend the parent-of-pkg dir so relative imports resolve.+ if (!is_code && req.script.contains('/')) {#ifdef _WIN32- const QChar kPathSep = ';';+ const QChar kPathSep = ';';#else- const QChar kPathSep = ':';+ const QChar kPathSep = ':';#endif- QString new_pypath =- existing_pypath.isEmpty() ? scripts_dir_ : (scripts_dir_ + kPathSep + existing_pypath);-- if (!is_code && req.script.contains('/')) {QString script_dir = QFileInfo(scripts_dir_ + "/" + req.script).dir().absolutePath();QString parent_of_pkg = QFileInfo(script_dir).dir().absolutePath();- if (parent_of_pkg != scripts_dir_ && !new_pypath.contains(parent_of_pkg))- new_pypath = parent_of_pkg + kPathSep + new_pypath;+ QString pypath = env.value("PYTHONPATH");+ if (parent_of_pkg != scripts_dir_ && !pypath.contains(parent_of_pkg)) {+ pypath = parent_of_pkg + kPathSep + pypath;+ env.insert("PYTHONPATH", pypath);+ }}- env.insert("PYTHONPATH", new_pypath);proc->setProcessEnvironment(env);proc->setWorkingDirectory(scripts_dir_);diff --git a/fincept-qt/src/python/PythonRunner.h b/fincept-qt/src/python/PythonRunner.hindex a02a52084..30d6a6f7b 100644--- a/fincept-qt/src/python/PythonRunner.h+++ b/fincept-qt/src/python/PythonRunner.h@@ -1,6 +1,7 @@#pragma once#include <QObject>#include <QProcess>+#include <QProcessEnvironment>#include <QQueue>#include <QString>#include <QStringList>@@ -46,6 +47,15 @@ class PythonRunner : public QObject {/// Resolve path to scripts directoryQString scripts_dir() const;+ /// Build the standard Python process environment used by all finagent/PyFincept+ /// subprocess spawns. Sets PYTHONIOENCODING, PYTHONDONTWRITEBYTECODE,+ /// PYTHONUNBUFFERED, FINCEPT_DATA_DIR, FINAGENT_DATA_DIR,+ /// FINAGENT_RUNTIME_CACHE_SIZE, and PYTHONPATH (derived from scripts_dir()).+ /// Callers that run a script inside a sub-package may still need to prepend+ /// a "parent-of-pkg" directory to PYTHONPATH themselves — this helper only+ /// sets the shared base env.+ QProcessEnvironment build_python_env() const;+/// Check if python is availablebool is_available() const;diff --git a/fincept-qt/src/services/agents/AgentService.cpp b/fincept-qt/src/services/agents/AgentService.cppindex dd0206416..850f86894 100644--- a/fincept-qt/src/services/agents/AgentService.cpp+++ b/fincept-qt/src/services/agents/AgentService.cpp@@ -211,6 +211,16 @@ void AgentService::run_python_stdin(const QString& action, const QJsonObject& pa// Spawn QProcess directly for stdin writing (P4 exception like ExchangeService)auto* proc = new QProcess(this);+ // Share the standard Python env + cwd + Windows console suppression with+ // PythonRunner so every finagent spawn sees the same FINCEPT_DATA_DIR,+ // FINAGENT_DATA_DIR, and PYTHONPATH.+ proc->setProcessEnvironment(py.build_python_env());+ proc->setWorkingDirectory(py.scripts_dir());+#ifdef _WIN32+ proc->setCreateProcessArgumentsModifier([](QProcess::CreateProcessArguments* cpa) {+ cpa->flags |= 0x08000000; // CREATE_NO_WINDOW+ });+#endifQPointer<AgentService> self = this;auto timer = std::make_shared<QElapsedTimer>();timer->start();@@ -472,6 +482,16 @@ QString AgentService::run_agent_streaming(const QString& query, const QJsonObjecQString script_path = py.scripts_dir() + "/agents/finagent_core/main.py";auto* proc = new QProcess(this);+ // Share the standard Python env + cwd + Windows console suppression with+ // PythonRunner so every finagent spawn sees the same FINCEPT_DATA_DIR,+ // FINAGENT_DATA_DIR, and PYTHONPATH.+ proc->setProcessEnvironment(py.build_python_env());+ proc->setWorkingDirectory(py.scripts_dir());+#ifdef _WIN32+ proc->setCreateProcessArgumentsModifier([](QProcess::CreateProcessArguments* cpa) {+ cpa->flags |= 0x08000000; // CREATE_NO_WINDOW+ });+#endifQPointer<AgentService> self = this;auto timer = std::make_shared<QElapsedTimer>();auto accumulated = std::make_shared<QString>();@@ -656,6 +676,16 @@ QString AgentService::run_team(const QString& query, const QJsonObject& team_conQString script_path = py.scripts_dir() + "/agents/finagent_core/main.py";auto* proc = new QProcess(this);+ // Share the standard Python env + cwd + Windows console suppression with+ // PythonRunner so every finagent spawn sees the same FINCEPT_DATA_DIR,+ // FINAGENT_DATA_DIR, and PYTHONPATH.+ proc->setProcessEnvironment(py.build_python_env());+ proc->setWorkingDirectory(py.scripts_dir());+#ifdef _WIN32+ proc->setCreateProcessArgumentsModifier([](QProcess::CreateProcessArguments* cpa) {+ cpa->flags |= 0x08000000; // CREATE_NO_WINDOW+ });+#endifQPointer<AgentService> self = this;auto timer = std::make_shared<QElapsedTimer>();auto accumulated = std::make_shared<QString>();