fix: 3 bugs — synthesis crash, stale running cycle, invalid tickers

- knowledge.py: trade_line crash when pnl_pct is None in dict
  (t.get('pnl_pct',0) returns None if key exists with None value — use 'or 0')
- database.py: cleanup_stale_running_cycles() marks any 'running' cycle
  as 'error' on startup (uvicorn reload mid-cycle left status stuck)
- main.py: call cleanup_stale_running_cycles() at startup with warning log
- database.py: _normalize_ticker() converts GPT-4o exchange:symbol format
  (NSE:RELIANCE → RELIANCE.NS, BSE:X → X.BO, etc.) so yfinance stops
  spamming 404 errors for every MTM request

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
OpenSquared
2026-06-17 12:15:32 +02:00
parent 27d6b598e8
commit dc3bc667eb
3 changed files with 37 additions and 3 deletions

View File

@@ -1,7 +1,7 @@
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from routers import market_data, geopolitical, options, backtest, ai, portfolio, config, patterns, journal, cycle as cycle_router, profiles as profiles_router, reasoning as reasoning_router, knowledge as knowledge_router
from services.database import init_db, get_config
from services.database import init_db, get_config, cleanup_stale_running_cycles
import os
import uvicorn
@@ -23,6 +23,13 @@ app.add_middleware(
@app.on_event("startup")
def startup():
init_db()
# Clean up cycles that were left in 'running' state by a previous crashed process
stale = cleanup_stale_running_cycles()
if stale:
import logging
logging.getLogger(__name__).warning(
f"[Startup] Cleaned up {stale} stale 'running' cycle(s) — server likely reloaded mid-cycle"
)
key = get_config("openai_api_key") or ""
if key:
os.environ["OPENAI_API_KEY"] = key