Преглед изворни кода

Harden strategy store identifiers

Lukas Goldschmidt пре 1 месец
родитељ
комит
09e86b0ef6
1 измењених фајлова са 6 додато и 1 уклоњено
  1. 6 1
      src/trader_mcp/strategy_store.py

+ 6 - 1
src/trader_mcp/strategy_store.py

@@ -52,7 +52,7 @@ def init_db() -> None:
         conn.execute(
             """
             CREATE TABLE IF NOT EXISTS strategy_instances (
-                id TEXT PRIMARY KEY,
+                id TEXT NOT NULL PRIMARY KEY,
                 name TEXT NOT NULL DEFAULT '',
                 strategy_type TEXT NOT NULL,
                 account_id TEXT NOT NULL,
@@ -82,6 +82,9 @@ def init_db() -> None:
         if "state_json" not in columns:
             conn.execute("ALTER TABLE strategy_instances ADD COLUMN state_json TEXT NOT NULL DEFAULT '{}' ")
 
+        # Clean up any legacy or partially-written rows that slipped in with a missing identifier.
+        conn.execute("DELETE FROM strategy_instances WHERE id IS NULL OR trim(id) = ''")
+
         # Backfill missing market identity for existing rows.
         # You can treat this as a lightweight bootstrap; it won’t override rows that already have market set.
         conn.execute(
@@ -112,6 +115,8 @@ def get_strategy_instance(instance_id: str) -> StrategyRecord | None:
 
 def add_strategy_instance(*, id: str, strategy_type: str, account_id: str, client_id: str | None = None, mode: str = "off", market_symbol: str | None = None, base_currency: str | None = None, counter_currency: str | None = None, config: dict[str, Any] | None = None, started_at: str | None = None, activated_at: str | None = None) -> StrategyRecord:
     init_db()
+    if not isinstance(id, str) or not id.strip():
+        raise ValueError("strategy instance id is required")
     now = _utc_now()
     config = config or {}
     with get_connection() as conn: