MicroFish/backend/app/services
Vivian Prabaswara 7f4ce46579 feat: Add performance optimizations for MiroFish simulation
Implements 5 major optimizations to handle 96+ rounds without memory issues:

1. Fix Memory Leak
   - Added max_rounds_kept limit (20 rounds in memory)
   - Auto-cleanup old rounds via add_round_summary()
   - Prevents OOM for long simulations

2. Checkpoint System
   - Save progress every 10 rounds (configurable)
   - Resume from last checkpoint if crash
   - Compressed storage with gzip
   - Keep max 5 checkpoints per platform

3. Decision Caching
   - Cache LLM decisions based on agent_id + context hash
   - 24-hour TTL by default
   - Reduces API calls by 20-40%
   - Thread-safe singleton pattern

4. Streaming Progress
   - Real-time progress via Server-Sent Events (SSE)
   - Broadcast round_complete, simulation_complete events
   - Better UX - no more 'stuck' perception
   - Easy client integration with EventSource

5. Batch Processing
   - Process agents in batches (10 per batch)
   - Memory cleanup between batches
   - Per-batch error handling
   - Cancellation support

Expected improvements:
- Memory usage: 60-75% reduction (1-2GB -> 300-500MB)
- API calls: 20-40% reduction with caching
- Resume capability: Checkpoint every 10 rounds
- Real-time progress: SSE streaming

New files:
- backend/app/services/checkpoint_manager.py
- backend/app/services/decision_cache.py
- backend/app/services/batch_processor.py
- backend/app/api/streaming.py
- backend/scripts/run_optimized_twitter_simulation.py
- OPTIMIZATION_GUIDE.md

Modified files:
- backend/app/services/simulation_runner.py
- backend/app/services/__init__.py
2026-07-09 01:20:39 +00:00
..
__init__.py feat: Add performance optimizations for MiroFish simulation 2026-07-09 01:20:39 +00:00
batch_processor.py feat: Add performance optimizations for MiroFish simulation 2026-07-09 01:20:39 +00:00
checkpoint_manager.py feat: Add performance optimizations for MiroFish simulation 2026-07-09 01:20:39 +00:00
decision_cache.py feat: Add performance optimizations for MiroFish simulation 2026-07-09 01:20:39 +00:00
graph_builder.py fix(i18n): pass locale to background threads via thread-local storage 2026-04-01 16:55:51 +08:00
oasis_profile_generator.py feat(i18n): replace remaining Chinese in config generator and profile generator 2026-04-01 17:19:12 +08:00
ontology_generator.py Merge pull request #428 from Ghostubborn/feat/i18n 2026-04-02 14:27:04 +08:00
report_agent.py feat(i18n): replace all user-visible Chinese in report_agent.py 2026-04-01 17:44:52 +08:00
simulation_config_generator.py feat(i18n): replace remaining Chinese in config generator and profile generator 2026-04-01 17:19:12 +08:00
simulation_ipc.py Implement Interview feature for agent interactions in simulations 2025-12-08 15:55:39 +08:00
simulation_manager.py feat(i18n): replace remaining hardcoded Chinese in progress callbacks 2026-04-01 16:53:29 +08:00
simulation_runner.py feat: Add performance optimizations for MiroFish simulation 2026-07-09 01:20:39 +00:00
text_processor.py Introduce Project ID for context management, finalizing the stateful API pipeline from file submission to graph construction. 2025-11-28 17:21:08 +08:00
zep_entity_reader.py feat(graph): implement pagination for fetching nodes and edges; add utility functions for streamlined data retrieval 2026-02-27 15:53:29 +08:00
zep_graph_memory_updater.py fix(i18n): pass locale to background threads via thread-local storage 2026-04-01 16:55:51 +08:00
zep_tools.py feat(i18n): replace all user-visible Chinese logger messages in zep_tools.py 2026-04-01 17:46:39 +08:00