Compare commits

..

7 Commits

Author SHA1 Message Date
90b6ad400d fix: make get_db_path() idempotent to prevent recursive _dev suffix
Root Cause:
The get_db_path() function was being called multiple times in the
initialization chain, causing recursive suffix addition:
  data/jobs.db -> data/jobs_dev.db -> data/jobs_dev_dev.db

This resulted in tables being created in data/jobs_dev_dev.db while
the application tried to access data/jobs_dev.db (empty database).

Fix:
Added idempotency check in get_db_path() to detect and skip
transformation if path already contains "_dev.db" suffix.

Evidence from Diagnostics:
- alpha.20 logs showed: Input='data/jobs_dev.db', Resolved='data/jobs_dev_dev.db'
- Tables were created in jobs_dev_dev.db but accessed from jobs_dev.db
- This caused "no such table: jobs" errors despite successful initialization

All 28 integration tests pass with this fix.

Fixes #6
2025-11-02 15:52:25 -05:00
6e4b2a4cc5 debug: add connection-level diagnostics to trace database access
Enhanced diagnostics to trace database path resolution and table existence
at connection time. This will help identify if get_db_connection() is
resolving paths correctly and accessing the right database file.

Added diagnostics to:
- get_db_connection(): Show input path, resolved path, file existence, and tables found
- initialize_dev_database(): Verify tables exist after creation

This will reveal whether the path resolution is working correctly or if
there's a timing/caching issue with database file access.
2025-11-02 15:46:36 -05:00
18bd4d169d debug: add comprehensive diagnostic logging for database initialization
Following systematic debugging methodology after 5 failed fix attempts.
Adding extensive print-based diagnostics to trace execution flow in Docker.

Instrumentation added to:
- api/main.py: Module import, app creation, lifespan function, module-level init
- api/database.py: initialize_dev_database() entry/exit and decision points

This diagnostic version will help identify:
1. Whether module-level code executes in Docker
2. Which initialization layer is failing
3. Database paths being resolved
4. Environment variable values

Tests confirmed passing with diagnostic logging.
2025-11-02 15:41:47 -05:00
8b91c75b32 fix: add module-level database initialization for uvicorn reliability
Add database initialization at module load time to ensure it runs
regardless of how uvicorn handles the lifespan context manager.

Issue: The lifespan function wasn't being triggered consistently when
uvicorn loads the app module, causing "no such table: jobs" errors.

Solution: Initialize database when the module is imported (after app
creation), providing a reliable fallback that works in all deployment
scenarios.

This provides defense-in-depth:
1. Lifespan function (ideal path)
2. Module-level initialization (fallback/guarantee)

Both paths check deployment mode and call the appropriate init function.
2025-11-02 15:36:12 -05:00
bdb3f6a6a2 refactor: move database initialization from entrypoint to application
Move database initialization logic from shell script to Python application
lifespan, following separation of concerns and improving maintainability.

Benefits:
- Single source of truth for database initialization (api/main.py lifespan)
- Better testability - Python code vs shell scripts
- Clearer logging with structured messages
- Easier to debug and maintain
- Infrastructure (entrypoint.sh) focuses on service orchestration
- Application (api/main.py) owns its data layer

Changes:
- Removed database init from entrypoint.sh
- Enhanced lifespan function with detailed logging
- Simplified entrypoint script (now 4 steps instead of 5)
- All tests pass (28/28 API endpoint tests)
2025-11-02 15:32:53 -05:00
3502a7ffa8 fix: respect dev mode in entrypoint database initialization
- Update entrypoint.sh to check DEPLOYMENT_MODE before initializing database
- DEV mode: calls initialize_dev_database() which resets the database
- PROD mode: calls initialize_database() which preserves existing data
- Adds clear logging to show which mode is being used

This ensures the dev database is properly reset on container startup,
matching the behavior of the lifespan function in api/main.py.
2025-11-02 15:30:11 -05:00
68d9f241e1 fix: use closure to capture db_path in lifespan context manager
- Fix lifespan function to access db_path from create_app scope via closure
- Prevents "no such table: jobs" error by ensuring database initialization runs
- Previous version tried to access app.state.db_path before it was set

The issue was that app.state is set after FastAPI instantiation, but the
lifespan function needs the db_path during startup. Using closure allows
the lifespan function to capture db_path from the create_app function scope.
2025-11-02 15:24:29 -05:00
4 changed files with 117 additions and 19 deletions

View File

@@ -32,15 +32,26 @@ def get_db_connection(db_path: str = "data/jobs.db") -> sqlite3.Connection:
"""
# Resolve path based on deployment mode
resolved_path = get_db_path(db_path)
print(f"🔍 DIAGNOSTIC [get_db_connection]: Input path='{db_path}', Resolved path='{resolved_path}'")
# Ensure data directory exists
db_path_obj = Path(resolved_path)
db_path_obj.parent.mkdir(parents=True, exist_ok=True)
# Check if database file exists
file_exists = db_path_obj.exists()
print(f"🔍 DIAGNOSTIC [get_db_connection]: Database file exists: {file_exists}")
conn = sqlite3.connect(resolved_path, check_same_thread=False)
conn.execute("PRAGMA foreign_keys = ON")
conn.row_factory = sqlite3.Row
# Verify tables exist
cursor = conn.cursor()
cursor.execute("SELECT name FROM sqlite_master WHERE type='table'")
tables = [row[0] for row in cursor.fetchall()]
print(f"🔍 DIAGNOSTIC [get_db_connection]: Tables in database: {tables}")
return conn
@@ -244,24 +255,44 @@ def initialize_dev_database(db_path: str = "data/trading_dev.db") -> None:
Args:
db_path: Path to dev database file
"""
print(f"🔍 DIAGNOSTIC: initialize_dev_database() CALLED with db_path={db_path}")
from tools.deployment_config import should_preserve_dev_data
if should_preserve_dev_data():
preserve = should_preserve_dev_data()
print(f"🔍 DIAGNOSTIC: should_preserve_dev_data() returned: {preserve}")
if preserve:
print(f" PRESERVE_DEV_DATA=true, keeping existing dev database: {db_path}")
# Ensure schema exists even if preserving data
if not Path(db_path).exists():
db_exists = Path(db_path).exists()
print(f"🔍 DIAGNOSTIC: Database exists check: {db_exists}")
if not db_exists:
print(f"📁 Dev database doesn't exist, creating: {db_path}")
initialize_database(db_path)
print(f"🔍 DIAGNOSTIC: initialize_dev_database() RETURNING (preserve mode)")
return
# Delete existing dev database
if Path(db_path).exists():
db_exists = Path(db_path).exists()
print(f"🔍 DIAGNOSTIC: Database exists (before deletion): {db_exists}")
if db_exists:
print(f"🗑️ Removing existing dev database: {db_path}")
Path(db_path).unlink()
print(f"🔍 DIAGNOSTIC: Database deleted successfully")
# Create fresh dev database
print(f"📁 Creating fresh dev database: {db_path}")
initialize_database(db_path)
print(f"🔍 DIAGNOSTIC: initialize_dev_database() COMPLETED successfully")
# Verify tables were created
print(f"🔍 DIAGNOSTIC: Verifying tables exist in {db_path}")
verify_conn = sqlite3.connect(db_path)
verify_cursor = verify_conn.cursor()
verify_cursor.execute("SELECT name FROM sqlite_master WHERE type='table'")
tables = [row[0] for row in verify_cursor.fetchall()]
verify_conn.close()
print(f"🔍 DIAGNOSTIC: Tables found: {tables}")
def cleanup_dev_database(db_path: str = "data/trading_dev.db", data_path: str = "./data/dev_agent_data") -> None:

View File

@@ -131,23 +131,49 @@ def create_app(
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Initialize database on startup, cleanup on shutdown if needed"""
print("=" * 80)
print("🔍 DIAGNOSTIC: LIFESPAN FUNCTION CALLED!")
print("=" * 80)
from tools.deployment_config import is_dev_mode, get_db_path
from api.database import initialize_dev_database, initialize_database
# Startup
if is_dev_mode():
# Startup - use closure to access db_path from create_app scope
logger.info("🚀 FastAPI application starting...")
logger.info("📊 Initializing database...")
print(f"🔍 DIAGNOSTIC: Lifespan - db_path from closure: {db_path}")
deployment_mode = is_dev_mode()
print(f"🔍 DIAGNOSTIC: Lifespan - is_dev_mode() returned: {deployment_mode}")
if deployment_mode:
# Initialize dev database (reset unless PRESERVE_DEV_DATA=true)
dev_db_path = get_db_path(app.state.db_path)
logger.info(" 🔧 DEV mode detected - initializing dev database")
print("🔍 DIAGNOSTIC: Lifespan - DEV mode detected")
dev_db_path = get_db_path(db_path)
print(f"🔍 DIAGNOSTIC: Lifespan - Resolved dev database path: {dev_db_path}")
print(f"🔍 DIAGNOSTIC: Lifespan - About to call initialize_dev_database({dev_db_path})")
initialize_dev_database(dev_db_path)
print(f"🔍 DIAGNOSTIC: Lifespan - initialize_dev_database() completed")
log_dev_mode_startup_warning()
else:
# Ensure production database schema exists
initialize_database(app.state.db_path)
logger.info(" 🏭 PROD mode - ensuring database schema exists")
print("🔍 DIAGNOSTIC: Lifespan - PROD mode detected")
print(f"🔍 DIAGNOSTIC: Lifespan - About to call initialize_database({db_path})")
initialize_database(db_path)
print(f"🔍 DIAGNOSTIC: Lifespan - initialize_database() completed")
logger.info("✅ Database initialized")
logger.info("🌐 API server ready to accept requests")
print("🔍 DIAGNOSTIC: Lifespan - Startup complete, yielding control")
print("=" * 80)
yield
# Shutdown (if needed in future)
pass
logger.info("🛑 FastAPI application shutting down...")
print("🔍 DIAGNOSTIC: LIFESPAN SHUTDOWN CALLED")
app = FastAPI(
title="AI-Trader Simulation API",
@@ -512,13 +538,51 @@ def create_app(
# Create default app instance
print("=" * 80)
print("🔍 DIAGNOSTIC: Module api.main is being imported/executed")
print("=" * 80)
app = create_app()
print(f"🔍 DIAGNOSTIC: create_app() completed, app object created: {app}")
# Ensure database is initialized when module is loaded
# This handles cases where lifespan might not be triggered properly
print("🔍 DIAGNOSTIC: Starting module-level database initialization check...")
logger.info("🔧 Module-level database initialization check...")
from tools.deployment_config import is_dev_mode, get_db_path
from api.database import initialize_dev_database, initialize_database
_db_path = app.state.db_path
print(f"🔍 DIAGNOSTIC: app.state.db_path = {_db_path}")
deployment_mode = is_dev_mode()
print(f"🔍 DIAGNOSTIC: is_dev_mode() returned: {deployment_mode}")
if deployment_mode:
print("🔍 DIAGNOSTIC: DEV mode detected - initializing dev database at module load")
logger.info(" 🔧 DEV mode - initializing dev database at module load")
_dev_db_path = get_db_path(_db_path)
print(f"🔍 DIAGNOSTIC: Resolved dev database path: {_dev_db_path}")
print(f"🔍 DIAGNOSTIC: About to call initialize_dev_database({_dev_db_path})")
initialize_dev_database(_dev_db_path)
print(f"🔍 DIAGNOSTIC: initialize_dev_database() completed successfully")
else:
print("🔍 DIAGNOSTIC: PROD mode - ensuring database exists at module load")
logger.info(" 🏭 PROD mode - ensuring database exists at module load")
print(f"🔍 DIAGNOSTIC: About to call initialize_database({_db_path})")
initialize_database(_db_path)
print(f"🔍 DIAGNOSTIC: initialize_database() completed successfully")
print("🔍 DIAGNOSTIC: Module-level database initialization complete")
logger.info("✅ Module-level database initialization complete")
print("=" * 80)
if __name__ == "__main__":
import uvicorn
# Note: Database initialization happens in startup_event()
# DEV mode warning will be displayed there as well
# Note: Database initialization happens in lifespan AND at module load
# for maximum reliability
uvicorn.run(app, host="0.0.0.0", port=8080)

View File

@@ -36,12 +36,7 @@ fi
echo "✅ Environment variables validated"
# Step 1: Initialize database
echo "📊 Initializing database..."
python -c "from api.database import initialize_database; initialize_database('data/jobs.db')"
echo "✅ Database initialized"
# Step 2: Merge and validate configuration
# Step 1: Merge and validate configuration
echo "🔧 Merging and validating configuration..."
python -c "from tools.config_merger import merge_and_validate; merge_and_validate()" || {
echo "❌ Configuration validation failed"
@@ -50,7 +45,7 @@ python -c "from tools.config_merger import merge_and_validate; merge_and_validat
export CONFIG_PATH=/tmp/runtime_config.json
echo "✅ Configuration validated and merged"
# Step 3: Start MCP services in background
# Step 2: Start MCP services in background
echo "🔧 Starting MCP services..."
cd /app
python agent_tools/start_mcp_services.py &
@@ -59,11 +54,11 @@ MCP_PID=$!
# Setup cleanup trap before starting uvicorn
trap "echo '🛑 Stopping services...'; kill $MCP_PID 2>/dev/null; exit 0" EXIT SIGTERM SIGINT
# Step 4: Wait for services to initialize
# Step 3: Wait for services to initialize
echo "⏳ Waiting for MCP services to start..."
sleep 3
# Step 5: Start FastAPI server with uvicorn (this blocks)
# Step 4: Start FastAPI server with uvicorn (this blocks)
# Note: Container always uses port 8080 internally
# The API_PORT env var only affects the host port mapping in docker-compose.yml
echo "🌐 Starting FastAPI server on port 8080..."

View File

@@ -69,8 +69,16 @@ def get_db_path(base_db_path: str) -> str:
Example:
PROD: "data/trading.db" -> "data/trading.db"
DEV: "data/trading.db" -> "data/trading_dev.db"
Note:
This function is idempotent - calling it multiple times on the same
path will not add multiple _dev suffixes.
"""
if is_dev_mode():
# Check if already has _dev suffix (idempotent)
if "_dev.db" in base_db_path:
return base_db_path
# Insert _dev before .db extension
if base_db_path.endswith(".db"):
return base_db_path[:-3] + "_dev.db"