gaurv007 commited on
Commit
8a49c31
·
verified ·
1 Parent(s): f4d1656

Upload alpha_factory/infra/factor_store.py

Browse files
Files changed (1) hide show
  1. alpha_factory/infra/factor_store.py +13 -0
alpha_factory/infra/factor_store.py CHANGED
@@ -80,6 +80,8 @@ class FactorStore:
80
  iteration: int = 1,
81
  ):
82
  """Insert a new alpha candidate (before BRAIN results arrive)."""
 
 
83
  self.conn.execute("""
84
  INSERT OR REPLACE INTO alphas (alpha_id, expression, neutralization, decay,
85
  fields_used, operators_used, archetype, theme, anomaly_tag,
@@ -107,38 +109,47 @@ class FactorStore:
107
  metrics.margin_pct, fitness_score, alpha_id])
108
 
109
  def update_verdict(self, alpha_id: str, verdict: Verdict, memo: str = ""):
 
110
  self.conn.execute("""
111
  UPDATE alphas SET verdict = ?, gatekeeper_memo = ? WHERE alpha_id = ?
112
  """, [verdict.value, memo, alpha_id])
113
 
114
  def update_correlation(self, alpha_id: str, max_corr: float):
 
115
  self.conn.execute("""
116
  UPDATE alphas SET max_corr_to_library = ? WHERE alpha_id = ?
117
  """, [max_corr, alpha_id])
118
 
119
  def get_all_themes(self) -> list[str]:
 
120
  result = self.conn.execute("SELECT theme FROM alphas WHERE theme IS NOT NULL").fetchall()
121
  return [r[0] for r in result]
122
 
123
  def get_all_anomaly_tags(self) -> list[str]:
 
124
  result = self.conn.execute("SELECT anomaly_tag FROM alphas WHERE anomaly_tag IS NOT NULL").fetchall()
125
  return [r[0] for r in result]
126
 
127
  def get_dead_themes(self) -> list[str]:
 
128
  result = self.conn.execute("""
129
  SELECT theme FROM dead_themes WHERE cooldown_until > CURRENT_TIMESTAMP
130
  """).fetchall()
131
  return [r[0] for r in result]
132
 
133
  def exists(self, alpha_id: str) -> bool:
 
134
  result = self.conn.execute("SELECT 1 FROM alphas WHERE alpha_id = ?", [alpha_id]).fetchone()
135
  return result is not None
136
 
137
  def get_expression_hashes(self) -> set[str]:
 
138
  result = self.conn.execute("SELECT alpha_id FROM alphas").fetchall()
139
  return {r[0] for r in result}
140
 
141
  def count_consecutive_kills(self) -> int:
 
 
142
  results = self.conn.execute("""
143
  SELECT verdict FROM alphas
144
  ORDER BY submitted_at DESC, rowid DESC
@@ -153,12 +164,14 @@ class FactorStore:
153
  return count
154
 
155
  def kill_theme(self, theme: str, last_sharpe: float, reason: str, cooldown_days: int = 180):
 
156
  self.conn.execute("""
157
  INSERT INTO dead_themes (theme, universe, last_sharpe, reason, cooldown_until)
158
  VALUES (?, 'TOP3000', ?, ?, CURRENT_TIMESTAMP + INTERVAL ? DAY)
159
  """, [theme, last_sharpe, reason, cooldown_days])
160
 
161
  def get_library_stats(self) -> dict:
 
162
  total = self.conn.execute("SELECT COUNT(*) FROM alphas").fetchone()[0]
163
  promoted = self.conn.execute("SELECT COUNT(*) FROM alphas WHERE verdict = 'promote'").fetchone()[0]
164
  killed = self.conn.execute("SELECT COUNT(*) FROM alphas WHERE verdict = 'kill'").fetchone()[0]
 
80
  iteration: int = 1,
81
  ):
82
  """Insert a new alpha candidate (before BRAIN results arrive)."""
83
+ # NOTE: All SQL uses parameterized queries (? placeholders) to prevent injection.
84
+ # The expression field is user-controlled (from LLM output) but is passed as a param.
85
  self.conn.execute("""
86
  INSERT OR REPLACE INTO alphas (alpha_id, expression, neutralization, decay,
87
  fields_used, operators_used, archetype, theme, anomaly_tag,
 
109
  metrics.margin_pct, fitness_score, alpha_id])
110
 
111
  def update_verdict(self, alpha_id: str, verdict: Verdict, memo: str = ""):
112
+ """Set the final verdict for an alpha."""
113
  self.conn.execute("""
114
  UPDATE alphas SET verdict = ?, gatekeeper_memo = ? WHERE alpha_id = ?
115
  """, [verdict.value, memo, alpha_id])
116
 
117
  def update_correlation(self, alpha_id: str, max_corr: float):
118
+ """Update max correlation to library."""
119
  self.conn.execute("""
120
  UPDATE alphas SET max_corr_to_library = ? WHERE alpha_id = ?
121
  """, [max_corr, alpha_id])
122
 
123
  def get_all_themes(self) -> list[str]:
124
+ """Get themes of all alphas in the store."""
125
  result = self.conn.execute("SELECT theme FROM alphas WHERE theme IS NOT NULL").fetchall()
126
  return [r[0] for r in result]
127
 
128
  def get_all_anomaly_tags(self) -> list[str]:
129
+ """Get anomaly tags of all alphas."""
130
  result = self.conn.execute("SELECT anomaly_tag FROM alphas WHERE anomaly_tag IS NOT NULL").fetchall()
131
  return [r[0] for r in result]
132
 
133
  def get_dead_themes(self) -> list[str]:
134
+ """Get themes that are in cooldown."""
135
  result = self.conn.execute("""
136
  SELECT theme FROM dead_themes WHERE cooldown_until > CURRENT_TIMESTAMP
137
  """).fetchall()
138
  return [r[0] for r in result]
139
 
140
  def exists(self, alpha_id: str) -> bool:
141
+ """Check if an alpha already exists (dedup)."""
142
  result = self.conn.execute("SELECT 1 FROM alphas WHERE alpha_id = ?", [alpha_id]).fetchone()
143
  return result is not None
144
 
145
  def get_expression_hashes(self) -> set[str]:
146
+ """Get all alpha_ids for dedup."""
147
  result = self.conn.execute("SELECT alpha_id FROM alphas").fetchall()
148
  return {r[0] for r in result}
149
 
150
  def count_consecutive_kills(self) -> int:
151
+ """Count consecutive kills from most recent (for kill switch)."""
152
+ # Use rowid as tiebreaker for deterministic ordering when timestamps are equal
153
  results = self.conn.execute("""
154
  SELECT verdict FROM alphas
155
  ORDER BY submitted_at DESC, rowid DESC
 
164
  return count
165
 
166
  def kill_theme(self, theme: str, last_sharpe: float, reason: str, cooldown_days: int = 180):
167
+ """Add a theme to the dead list with cooldown."""
168
  self.conn.execute("""
169
  INSERT INTO dead_themes (theme, universe, last_sharpe, reason, cooldown_until)
170
  VALUES (?, 'TOP3000', ?, ?, CURRENT_TIMESTAMP + INTERVAL ? DAY)
171
  """, [theme, last_sharpe, reason, cooldown_days])
172
 
173
  def get_library_stats(self) -> dict:
174
+ """Summary statistics for the factor store."""
175
  total = self.conn.execute("SELECT COUNT(*) FROM alphas").fetchone()[0]
176
  promoted = self.conn.execute("SELECT COUNT(*) FROM alphas WHERE verdict = 'promote'").fetchone()[0]
177
  killed = self.conn.execute("SELECT COUNT(*) FROM alphas WHERE verdict = 'kill'").fetchone()[0]