Spaces:
Sleeping
Sleeping
deploy from github
Browse files- core/simulation.py +46 -25
core/simulation.py
CHANGED
|
@@ -77,6 +77,8 @@ class Simulation:
|
|
| 77 |
|
| 78 |
def run(self, run_number: int = None) -> Dict:
|
| 79 |
"""Execute a complete simulation run."""
|
|
|
|
|
|
|
| 80 |
self.initialize_run(run_number)
|
| 81 |
|
| 82 |
print(f"\n=== Starting run {self.current_run_number} with {self.turns_per_run} turns ===")
|
|
@@ -241,39 +243,58 @@ class Simulation:
|
|
| 241 |
|
| 242 |
def _save_action(self, agent: Agent, turn: int, decision: Dict, thinking: str):
|
| 243 |
"""Save agent action to database."""
|
| 244 |
-
self.supabase
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 253 |
|
| 254 |
def _save_states(self, turn: int):
|
| 255 |
"""Save agent and pool states to database."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 256 |
# Save agent states
|
| 257 |
for agent in self.agents:
|
| 258 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 259 |
run_id=self.current_run_id,
|
| 260 |
turn=turn,
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
strategy=agent.infer_strategy()
|
| 266 |
))
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
run_id=self.current_run_id,
|
| 271 |
-
turn=turn,
|
| 272 |
-
reserve_a=self.pool.reserve_a,
|
| 273 |
-
reserve_b=self.pool.reserve_b,
|
| 274 |
-
price_ab=self.pool.price_ab,
|
| 275 |
-
total_liquidity=self.pool.total_liquidity
|
| 276 |
-
))
|
| 277 |
|
| 278 |
def _calculate_metrics(self) -> Dict:
|
| 279 |
"""Calculate run metrics."""
|
|
|
|
| 77 |
|
| 78 |
def run(self, run_number: int = None) -> Dict:
|
| 79 |
"""Execute a complete simulation run."""
|
| 80 |
+
print(f"[DEBUG] run() called, supabase is {'connected' if self.supabase else 'None'}")
|
| 81 |
+
|
| 82 |
self.initialize_run(run_number)
|
| 83 |
|
| 84 |
print(f"\n=== Starting run {self.current_run_number} with {self.turns_per_run} turns ===")
|
|
|
|
| 243 |
|
| 244 |
def _save_action(self, agent: Agent, turn: int, decision: Dict, thinking: str):
|
| 245 |
"""Save agent action to database."""
|
| 246 |
+
if not self.supabase:
|
| 247 |
+
return
|
| 248 |
+
try:
|
| 249 |
+
self.supabase.save_action(ActionData(
|
| 250 |
+
run_id=self.current_run_id,
|
| 251 |
+
turn=turn,
|
| 252 |
+
agent_name=agent.name,
|
| 253 |
+
action_type=decision.get("action", "unknown"),
|
| 254 |
+
payload=decision.get("payload", {}),
|
| 255 |
+
reasoning_trace=decision.get("reasoning", ""),
|
| 256 |
+
thinking_trace=thinking
|
| 257 |
+
))
|
| 258 |
+
print(f"[DEBUG] Saved action for {agent.name}: {decision.get('action', 'unknown')}")
|
| 259 |
+
except Exception as e:
|
| 260 |
+
print(f"[ERROR] Failed to save action for {agent.name}: {e}")
|
| 261 |
|
| 262 |
def _save_states(self, turn: int):
|
| 263 |
"""Save agent and pool states to database."""
|
| 264 |
+
if not self.supabase:
|
| 265 |
+
print(f"[DEBUG] Skipping state save - no supabase")
|
| 266 |
+
return
|
| 267 |
+
|
| 268 |
+
print(f"[DEBUG] Saving states for turn {turn}, run_id={self.current_run_id}")
|
| 269 |
# Save agent states
|
| 270 |
for agent in self.agents:
|
| 271 |
+
try:
|
| 272 |
+
self.supabase.save_agent_state(AgentStateData(
|
| 273 |
+
run_id=self.current_run_id,
|
| 274 |
+
turn=turn,
|
| 275 |
+
agent_name=agent.name,
|
| 276 |
+
token_a_balance=agent.token_a,
|
| 277 |
+
token_b_balance=agent.token_b,
|
| 278 |
+
profit=agent.calculate_profit(),
|
| 279 |
+
strategy=agent.infer_strategy()
|
| 280 |
+
))
|
| 281 |
+
print(f"[DEBUG] Saved state for {agent.name}")
|
| 282 |
+
except Exception as e:
|
| 283 |
+
print(f"[ERROR] Failed to save agent state for {agent.name}: {e}")
|
| 284 |
+
|
| 285 |
+
# Save pool state
|
| 286 |
+
try:
|
| 287 |
+
self.supabase.save_pool_state(PoolStateData(
|
| 288 |
run_id=self.current_run_id,
|
| 289 |
turn=turn,
|
| 290 |
+
reserve_a=self.pool.reserve_a,
|
| 291 |
+
reserve_b=self.pool.reserve_b,
|
| 292 |
+
price_ab=self.pool.price_ab,
|
| 293 |
+
total_liquidity=self.pool.total_liquidity
|
|
|
|
| 294 |
))
|
| 295 |
+
print(f"[DEBUG] Saved pool state")
|
| 296 |
+
except Exception as e:
|
| 297 |
+
print(f"[ERROR] Failed to save pool state: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 298 |
|
| 299 |
def _calculate_metrics(self) -> Dict:
|
| 300 |
"""Calculate run metrics."""
|