# 02 — CIRCUITOS Y CONEXIONES DE LOS LABORATORIOS CIENTÍFICOS (ORC-27)

## 1. Arquitectura de Registro, Adaptadores y Despacho

```mermaid
flowchart TD
    Req["Solicitud Científica / API / Coordinador"] --> Coord["ScientificCoordinator (app/core/coordinator.py)"]
    Coord --> Plan["ResearchService.plan_research_inquiry (app/core/ai_provider.py)"]
    Coord --> Reg["LabRegistry (app/core/lab_registry.py)"]
    Reg --> AlgAdp["AlgorithmLabAdapter (app/core/lab_adapters.py)"]
    Reg --> AIAdp["AILabAdapter (app/core/lab_adapters.py)"]

    AlgAdp --> AlgSvc["AlgorithmLabService (app/lab_algorithms/service.py)"]
    AlgAdp --> TechMgr["GlobalTechResearchManager (app/lab_algorithms/tech_research.py)"]
    AlgAdp --> QuantEng["QuantitativeResearchEngine (app/lab_algorithms/financial.py)"]

    AIAdp --> AISvc["AILabService (app/lab_ai/service.py)"]
    AISvc --> Trainer["AIModelTrainer (app/lab_ai/trainer.py)"]
    AISvc --> RobSim["RoboticsSimulationEngine (app/lab_ai/robotics_sim.py)"]

    Coord --> Collab["InterLabCoordinator (app/core/collaboration.py)"]
    Collab --> AlgSvc
    Collab --> AISvc

    AlgSvc --> Mem["ScientificMemory + SQLite (app/core/scientific_memory.py)"]
    AISvc --> Mem
    TechMgr --> Mem
    Collab --> Mem
```

---

## 2. Trazabilidad de Circuitos por `task_type`

| `task_type` | Punto de Entrada en `ScientificCoordinator` | Adaptador (`LabAdapter`) | Servicio / Motor Ejecutor | Persistencia en `ScientificMemory` | Estado del Circuito |
| :--- | :--- | :--- | :--- | :--- | :--- |
| `ALGORITHM_OPTIMIZATION` / `ALGORITHM_BENCHMARK` | `process_next_task()` (líneas 598–618) | `AlgorithmLabAdapter.execute_experiment()` | `AlgorithmLabService.run_optimization_experiment()` -> `AlgorithmBenchmarkingEngine.benchmark_optimization()` | `Experiment`, `ExperimentRun`, `ScientificEvidence`, `ReportGenerator` | **VERIFICADO (PASS)** |
| `ALGORITHM_PREDICTIVE` | `process_next_task()` (añadido en ORC-27) | `AlgorithmLabAdapter.execute_experiment()` (añadido en ORC-27) | `AlgorithmLabService.run_predictive_experiment()` -> `AlgorithmBenchmarkingEngine.benchmark_predictive()` | `Experiment`, `ExperimentRun`, `ScientificEvidence`, `ReportGenerator` | **CORREGIDO Y VERIFICADO (PASS)** |
| `ALGORITHM_INVESTIGATION` | `process_next_task()` (líneas 707–723) | `AlgorithmLabAdapter.execute_experiment()` | `AlgorithmLabService.investigate_new_algorithm_flow()` (multi-semilla `[42, 101, 2024]`) | `ResearchProject`, `Hypothesis`, `Experiment`, `ExperimentRun`, `ScientificEvidence` | **VERIFICADO (PASS)** |
| `QUANTITATIVE_RESEARCH` | `process_next_task()` (líneas 662–673) | `AlgorithmLabAdapter.execute_experiment()` | `QuantitativeResearchEngine.walk_forward_backtest()` + `AlgorithmLabService.run_financial_research()` | `Experiment`, `ExperimentRun`, `ScientificEvidence`, `ReportGenerator` | **CORREGIDO Y VERIFICADO (PASS)** *(Antes lanzaba `AttributeError: walk_forward_backtest`)* |
| `TECH_RESEARCH` / `DATASET_ANALYSIS` | `process_next_task()` (líneas 674–706) | `AlgorithmLabAdapter.execute_experiment()` | `GlobalTechResearchManager.create_tech_inquiry()` -> `NumericalDatasetAnalyzer.analyze_dataset()` | `TechResearchInquiry`, `Experiment`, `ExperimentRun`, `ScientificEvidence` | **VERIFICADO (PASS)** |
| `AI_TRAINING` / `AI_TRAIN` | `process_next_task()` (líneas 619–645) | `AILabAdapter.execute_experiment()` | `AILabService.train_and_evaluate_model()` -> `AIModelTrainer` + `ModelEvaluator` | `Experiment`, `AIModelRecord`, `ExperimentRun`, `ScientificArtifact`, `ScientificEvidence` | **VERIFICADO (PASS)** |
| `AI_ROBOTICS_SIM` | `process_next_task()` (líneas 646–661) | `AILabAdapter.execute_experiment()` | `AILabService.run_robotics_simulation()` -> `RoboticsSimulationEngine` | `Experiment`, `ExperimentRun`, `ScientificEvidence`, `ReportGenerator` | **CORREGIDO Y VERIFICADO (PASS)** *(Antes lanzaba `TypeError` por argumentos `hypothesis_id` y `steps`)* |
| `AI_ARCHITECTURE_INVESTIGATION` | `process_next_task()` (líneas 724–740) | `AILabAdapter.execute_experiment()` | `AILabService.investigate_ai_architecture_flow()` (`split_train_val_test` 60/20/20) | `ResearchProject`, `Hypothesis`, `Experiment`, `AIModelRecord`, `ScientificEvidence` | **VERIFICADO (PASS)** |
| `INTER_LAB_COLLABORATION` | `process_next_task()` (líneas 761–804) | N/A (Coordinación directa) | `InterLabCoordinator.execute_and_submit_evaluation()` -> `AlgorithmLabService` / `AILabService` | `InterLabCollaboration`, `Experiment`, `ScientificEvidence` | **CORREGIDO Y VERIFICADO (PASS)** |
| `DISCOVERY_CYCLE` | `process_next_task()` (líneas 741–760) | N/A (Coordinación directa) | `ScientificCoordinator.run_discovery_cycle()` (2 rondas acotadas con conservación de ganador) | `ResearchProject`, `Hypothesis`, `Experiment`, `ScientificEvidence` | **VERIFICADO (PASS)** |

---

## 3. Desconexiones Detectadas y Resueltas en ORC-27

1. **Circuito `QUANTITATIVE_RESEARCH` (`AlgorithmLabAdapter` y `ScientificCoordinator` -> `QuantitativeResearchEngine`):**
   - **Problema previo:** Ambos invocaban `QuantitativeResearchEngine.walk_forward_backtest(strategy_name=..., train_ratio=...)`, método inexistente en `app/lab_algorithms/financial.py`.
   - **Resolución:** Se implementó `QuantitativeResearchEngine.walk_forward_backtest()` integrando `assert_no_live_trading()`, `generate_or_load_benchmark_series()`, `walk_forward_validation()` y `backtest_strategy()`, y se conectó con `AlgorithmLabService.run_financial_research()` para persistir el experimento y reporte SHA-256 en `ScientificMemory`.
2. **Circuito `AI_ROBOTICS_SIM` (`AILabAdapter` y `ScientificCoordinator` -> `AILabService.run_robotics_simulation`):**
   - **Problema previo:** El coordinador y el adaptador pasaban `hypothesis_id` y `steps` a `AILabService.run_robotics_simulation(project_id, simulation_type)`, causando `TypeError`.
   - **Resolución:** Se amplió la firma de `AILabService.run_robotics_simulation` para aceptar `hypothesis_id`, `steps` y `random_seed`, ejecutando `assert_simulation_only()`, propagando `steps` al filtro de Kalman y devolviendo `report_sha256`.
3. **Circuito `ALGORITHM_PREDICTIVE` y Topologías `rosenbrock` / `ackley`:**
   - **Problema previo:** `AlgorithmLabAdapter.capabilities` declaraba `predictive_regressors` y `benchmark_problems: ["rastrigin", "sphere", "rosenbrock", "ackley"]`, pero el adaptador no enrutaba `ALGORITHM_PREDICTIVE` y `AlgorithmBenchmarkingEngine` solo evaluaba `rastrigin` y `sphere`.
   - **Resolución:** Se añadieron las funciones objetivo exactas para `rosenbrock` y `ackley` en `AlgorithmBenchmarkingEngine` y `AlgorithmLabService`, y se habilitó el tipo de tarea `ALGORITHM_PREDICTIVE` en `AlgorithmLabAdapter` y `ScientificCoordinator`.

