Spaces:
Running
Running
File size: 13,277 Bytes
ccf348e 4eab431 ccf348e 4eab431 ccf348e 4eab431 ccf348e 4eab431 ccf348e 4eab431 02b5083 41b08a5 4eab431 ccf348e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 | # BLUM v1.0 Runtime Architecture Refactor Report
Generated: 2026-06-24
Updated: 2026-06-29 for the worker-runtime extraction sprint.
Scope: `backend/app`, `backend/alembic/versions`, `frontend/app`, `frontend/lib`, scheduler/runtime services, snapshot services, Learning Loop, Trading Game, Decision Intelligence, Capital Allocation, Alpha Recovery, Meta-Cognition, Chat and Performance diagnostics.
This sprint measures and reduces runtime coupling. It does not add financial features, does not claim performance is fixed, and does not change BLUM's trading or learning algorithms.
## Internal Architecture Map
```mermaid
graph TD
subgraph Frontend
LearningPage["Learning Page"]
PerformancePage["Performance Page"]
OtherPages["Other Next.js Pages"]
ApiClient["frontend/lib/api.ts"]
end
subgraph API
Routes["FastAPI routes.py (high fan-out)"]
Middleware["Timing/GET Hygiene Middleware"]
end
subgraph Runtime
CentralBrainRuntime["CentralBrainRuntime"]
BrainEventBus["BrainEventBus"]
SnapshotProducer["SnapshotProducerService"]
SnapshotWatchdog["SnapshotWatchdogService"]
JobState["BackgroundJobStateService"]
end
subgraph Background
Scheduler["APScheduler realtime.py"]
AutonomousEngine["AutonomousResearchEngine"]
LearningLoop["LearningLoopService"]
TradingGame["TradingGameSimulator"]
end
subgraph Intelligence
DecisionIntelligence["Decision/Business/Portfolio Intelligence"]
CapitalAllocation["Adaptive Capital Allocation"]
AlphaRecovery["Alpha Loss & Recovery"]
MetaCognition["Meta-Cognition"]
ReasoningCore["Reasoning Core"]
end
subgraph Persistence
Postgres["PostgreSQL"]
Snapshots["dashboard_snapshots"]
RuntimeEvents["brain_runtime_events"]
BackgroundState["background_job_state"]
PerformanceRecorder["PerformanceRecorder"]
end
LearningPage --> ApiClient
PerformancePage --> ApiClient
OtherPages --> ApiClient
ApiClient --> Routes
Routes --> Middleware
Routes --> CentralBrainRuntime
Routes --> SnapshotWatchdog
Routes --> SnapshotProducer
Routes --> Intelligence
Routes --> LearningLoop
Routes --> TradingGame
Scheduler --> JobState
Scheduler --> BrainEventBus
Scheduler --> SnapshotWatchdog
Scheduler --> SnapshotProducer
Scheduler --> AutonomousEngine
AutonomousEngine --> Intelligence
AutonomousEngine --> LearningLoop
AutonomousEngine --> TradingGame
SnapshotProducer --> Snapshots
SnapshotWatchdog --> RuntimeEvents
CentralBrainRuntime --> RuntimeEvents
CentralBrainRuntime --> BackgroundState
CentralBrainRuntime --> Snapshots
Middleware --> PerformanceRecorder
Intelligence --> Postgres
LearningLoop --> Postgres
TradingGame --> Postgres
classDef critical fill:#ff6b6b,stroke:#c92a2a,color:#fff
classDef warning fill:#ffd43b,stroke:#e67700
classDef clean fill:#51cf66,stroke:#2b8a3e,color:#fff
class Routes,Scheduler critical
class AutonomousEngine,Intelligence,LearningPage warning
class CentralBrainRuntime,BrainEventBus,SnapshotProducer,SnapshotWatchdog,JobState,Middleware,ApiClient,Snapshots,RuntimeEvents,BackgroundState,PerformanceRecorder clean
```
## Brooks-Lint Architecture Audit
Mode: Architecture Audit
Health Score: 55/100 before the first runtime refactor.
Health Score: 64/100 after worker-runtime extraction. The score improves because unrelated background jobs are no longer forced through one global scheduler lock, but `routes.py` and several composite jobs remain architectural debt.
### Critical Findings
**Dependency Disorder - API route fan-out**
Symptom: `backend/app/api/routes.py` imports almost every model and service, then exposes all modules directly through one router.
Source: Clean Architecture - Dependency Inversion Principle and Stable Dependencies Principle.
Consequence: every new financial engine increases route import fan-out, startup import time and change propagation risk.
Remedy: route new runtime behavior through small orchestration services (`CentralBrainRuntime`, `SnapshotWatchdogService`, `SnapshotProducerService`) and keep financial computation out of generic runtime endpoints.
**Runtime Coupling - global background lock**
Symptom: `backend/app/services/realtime.py` used one `_state["running"]` guard for every scheduled job.
Source: Clean Architecture - Single Responsibility Principle; Pragmatic Programmer - Orthogonality.
Consequence: a long snapshot refresh, market refresh or professional learning cycle could defer unrelated workers even when no real dependency existed.
Remedy: add `RuntimeWorkerCoordinator`, register workers explicitly and block only duplicate runs of the same worker.
**Change Propagation - scheduler runs monolithic jobs**
Symptom: `backend/app/services/realtime.py` schedules full research, market refresh, Learning Loop, model learning and Trading Game work as coarse jobs.
Source: Fowler - Shotgun Surgery / Divergent Change.
Consequence: a failure or slow stage makes the whole worker look failed or stale, and it is difficult to resume where work stopped.
Remedy: persist `background_job_state`, publish runtime events and enforce startup light mode so heavy work is background-first and observable.
### Warning Findings
**Accidental Complexity - snapshots exist but are not centrally produced**
Symptom: `dashboard_snapshots` existed, but producers were scattered and critical snapshots could be missing without a central health view.
Source: Brooks - Conceptual Integrity; Pragmatic Programmer - Orthogonality.
Consequence: frontend screens cannot reliably know whether missing UI data is a true data gap, stale cache or failed producer.
Remedy: add `SnapshotProducerService`, `SnapshotWatchdogService`, `/snapshots/health` and `missing_sections_json`.
**Domain Model Distortion - frontend can still observe compute endpoints**
Symptom: several `GET` endpoints accept `persist=false` flags and can be called with `persist=true`, making reads capable of writes.
Source: Evans - Ubiquitous Language / bounded context drift.
Consequence: a page load can silently become a recalculation path if query params change.
Remedy: instrument `GET_ENDPOINT_SIDE_EFFECT_DETECTED` and keep Learning Overview on `/api/learning-intelligence/summary` only.
**Cognitive Overload - no single runtime state**
Symptom: operational readiness is spread across `/system/status`, `/startup/status`, `/performance/diagnostics`, scheduler memory and individual dashboard endpoints.
Source: McConnell - High-quality routines and information hiding.
Consequence: it is hard to verify whether BLUM is learning, stale, blocked or merely missing snapshots.
Remedy: add `/brain/runtime-state` and `/learning/health` as read-only, composed views.
### Suggestions
**Knowledge Duplication - dashboard status language repeats**
Symptom: readiness/truth/snapshot warnings are assembled in multiple services.
Source: Pragmatic Programmer - DRY.
Consequence: UI panels may disagree on whether a section is stale or missing.
Remedy: centralize snapshot freshness and missing-section reporting in the runtime layer.
## Phase 1 Findings
### Slowest Endpoint Risk
The codebase already records endpoint timing through `PerformanceRecorder`. Actual slowest endpoints must be read from `/performance/diagnostics` in the running Space; this report does not invent measurements.
Structural candidates for slow endpoints:
- `/system/status`: counts a large number of tables on every request.
- `/api/learning-intelligence/dashboard`: full dashboard service, not snapshot-only.
- `/api/trading-game/ledger` with default `limit=200`.
- `/api/trading-game/equity/annotated` with default `limit=800`.
- `/api/decision-intelligence/*`, `/api/business-quality/*`, `/api/portfolio-intelligence/*` when recalculation is requested.
### GET Endpoint Hygiene Risks
Observed `GET` endpoints with optional persistence risk:
- `/api/trading-game/reality-check?persist=true`
- `/api/trading-game/intelligence-metrics?persist=true`
- `/api/trading-game/historical-vs-live?persist=true`
- `/api/sniper/candidates?persist=true`
- `/api/sniper/candidates/{ticker}?persist=true`
Mitigation added: middleware records `GET_ENDPOINT_SIDE_EFFECT_DETECTED` and returns `X-BLUM-GET-SIDE-EFFECT-RISK: true` when a `GET` request contains `persist=true`.
### Missing Snapshot Risks
Critical snapshots now tracked:
- `learning_summary`
- `trading_game_summary`
- `benchmark_summary`
- `intelligence_growth_summary`
- `truth_panel_summary`
- `decision_intelligence_summary`
- `business_quality_summary`
- `portfolio_intelligence_summary`
- `capital_allocation_summary`
- `alpha_recovery_summary`
- `meta_cognition_summary`
- `dashboard_overview_summary`
### Background Job Risks
Before this refactor, job state was mostly process memory plus performance events. Added persistent `background_job_state` and `brain_runtime_events` so restarts and UI diagnostics can see the last job, failure and duration.
### Startup Risk
Before this refactor, `BLUM_ENABLE_LIVE_STARTUP=true` could start heavy pipeline work. Added `BLUM_STARTUP_RUN_FULL_AUTONOMOUS=false` default and a snapshot warm-up path.
## Runtime Refactor Applied
Added:
- `CentralBrainRuntime`
- `BrainEventBus`
- `BackgroundJobStateService`
- `SnapshotProducerService`
- `SnapshotWatchdogService`
- `LearningHealthService`
- `RuntimeWorkerCoordinator`
- `WorkerDefinition` registry for the scheduler workers
Added endpoints:
- `GET /brain/runtime-state`
- `GET /snapshots/health`
- `POST /snapshots/produce`
- `GET /learning/health`
Added tables:
- `brain_runtime_events`
- `background_job_state`
Extended:
- `dashboard_snapshots.missing_sections_json`
Runtime extraction added:
- scheduler state now exposes `running_jobs`, `running_count` and `worker_registry`;
- unrelated workers can run independently;
- duplicate runs of the same worker are deferred as `same_worker_already_running`;
- deferrals are persisted as `module_deferred` events;
- stale `running` jobs from a previous container are marked `interrupted` at startup;
- previous-process failed jobs are archived as `previous_failed` so current health is not blocked by dead process state;
- Central Brain Runtime now reads worker state without importing `realtime.py`;
- the frequent professional Learning Loop lane defers heavy Market Sniper R-multiple simulation to deeper cycles.
## 2026-06-29 Dependency Graph Update
```mermaid
graph TD
Scheduler["APScheduler realtime.py"] --> WorkerRuntime["RuntimeWorkerCoordinator"]
WorkerRuntime --> JobState["background_job_state"]
WorkerRuntime --> EventBus["BrainEventBus"]
Scheduler --> Workers["Independent named workers"]
Workers --> FinancialServices["Existing financial services"]
FinancialServices --> DB["PostgreSQL"]
Workers --> SnapshotProducer["SnapshotProducerService"]
SnapshotProducer --> Snapshots["dashboard_snapshots"]
EventBus --> CentralBrain["CentralBrainRuntime"]
JobState --> CentralBrain
Snapshots --> CentralBrain
WorkerRuntime --> CentralBrain
CentralBrain --> API["/brain/runtime-state"]
Snapshots --> Frontend["Snapshot-first frontend"]
```
### Full Audit Highlights
- Backend service surface: 152 Python files under `backend`.
- Frontend source surface: 45 TypeScript/TSX files under `frontend`.
- Main API router: 257 route decorators in `backend/app/api/routes.py`.
- Highest fan-out service/router observed: `app.api.routes`, with direct imports from almost every runtime and financial module.
- Largest service files remain `financial_chat.py`, `learning_loop.py`, `reasoning_precision.py`, `market_sniper.py`, `trade_transparency.py`, `blum_financial_model.py`, `financial_brain_learning.py`, `meta_cognition.py`, `trading_game.py`, `alpha_recovery.py`, `learning_intelligence.py` and `trading_intelligence_lab.py`.
- Existing runtime/snapshot migrations are already present (`0024_runtime_architecture.py`, `0025_tg_runtime_snap.py`).
- The primary new bottleneck fixed in this sprint is scheduler-level coupling, not SQL latency.
## Performance Before / After
Measured before/after latency must come from `/performance/diagnostics` after deploy and warm-up. This commit does not claim fixed latency. It makes the runtime measurable and keeps heavy startup work behind configuration.
Expected architectural improvements:
- Learning Overview still performs one primary summary request.
- Snapshot health is readable without recalculation.
- Heavy startup autonomous execution is disabled by default.
- Scheduler jobs publish durable state and module events.
- GET side-effect risks are observable.
## Remaining Bottlenecks
- `routes.py` remains a high fan-out router and should eventually be split by bounded context.
- Some deep dashboard GETs still compute from latest rows instead of snapshots.
- The autonomous research engine still has sequential stages; this sprint records state but does not fully rewrite every stage into cursorized work units.
- `/system/status` still performs many table counts and should be converted to a cached runtime snapshot.
## Next Recommended Step
Use `/performance/diagnostics` after deployment to capture real p95 and top bottleneck data, then move the highest-cost read endpoints to snapshot-first implementations one by one.
|