# Prioritized Implementation Roadmap

**Based On**: COMPREHENSIVE_GAP_ANALYSIS_2026-01-11.md
**Generated**: 2026-01-11
**Last Updated**: 2026-01-21 (Turso database optimization added)
**Total Effort**: 542-718 hours (13.5-18 weeks) + 80-120 hours (Turso optimization)
**Strategy**: P0 → P1 → P2 → P3 execution

---

## Quick Reference: Priority Levels

| Priority | Definition | Timeline | Effort |
|----------|-------------|----------|--------|
| **P0** | CRITICAL - Must fix, blocks code reviews | 4-6 weeks | 175-229 hrs |
| **P1** | HIGH - Strong ROI, production impact | 2-3 weeks | 97-135 hrs |
| **P2** | MEDIUM - Good value, quality improvements | 3-5 weeks | 146-197 hrs |
| **P3** | LOW - Future enhancements, nice to have | 4-6 weeks | 180-225 hrs |

---

## Phase 1: P0 Critical Compliance (Weeks 1-6)

### Goal
Achieve 100% codebase compliance with AGENTS.md standards

### Success Criteria
- ✅ All files ≤ 500 LOC (20+ files)
- ✅ Unwrap/expect calls < 50 (from 598)
- ✅ Test pass rate > 95%
- ✅ Zero clippy warnings
- ✅ All quality gates passing

### Sprint 1: P0 File Size Compliance (Weeks 1-2)

#### Task 1.1: Memory-MCP Sandbox Files (Days 1-3)
**Effort**: 15-20 hours
**Priority**: P0
**Risk**: Low

**Files to Split**:
1. `do-memory-mcp/src/wasm_sandbox.rs` (683 LOC → ≤500)
   - Create: `src/sandbox/runtime.rs` (~300 LOC)
   - Create: `src/sandbox/instance.rs` (~250 LOC)
   - Keep: `src/wasm_sandbox.rs` (~133 LOC)
   - Effort: 6-8 hours

2. `do-memory-mcp/src/javy_compiler.rs` (679 LOC → ≤500)
   - Create: `src/compiler/phases.rs` (~250 LOC)
   - Create: `src/compiler/validation.rs` (~200 LOC)
   - Keep: `src/javy_compiler.rs` (~229 LOC)
   - Effort: 6-8 hours

3. `do-memory-mcp/src/unified_sandbox.rs` (533 LOC → ≤500)
   - Create: `src/sandbox/handler.rs` (~280 LOC)
   - Keep: `src/unified_sandbox.rs` (~253 LOC)
   - Effort: 4-6 hours

**Steps**:
```bash
# For each file:
1. Create new module directory
2. Extract related functions into new module
3. Update imports in original file
4. Run cargo build to verify
5. Run cargo test to verify
6. Run cargo clippy --all -- -D warnings
7. Verify file size
```

**Validation**:
- [ ] Each new module ≤ 500 LOC
- [ ] All tests passing
- [ ] Zero clippy warnings
- [ ] Documentation updated

#### Task 1.2: Storage Files (Day 4)
**Effort**: 10-12 hours
**Priority**: P0
**Risk**: Low

**Files to Split**:
1. `do-memory-storage-redb/src/cache.rs` (654 LOC → ≤500)
   - Extract cache operations: `cache/ops.rs` (~300 LOC)
   - Extract eviction logic: `cache/eviction.rs` (~200 LOC)
   - Keep: `cache.rs` (~154 LOC)
   - Effort: 5-7 hours

2. `do-memory-storage-turso/src/pool.rs` (589 LOC → ≤500)
   - Extract pool management: `pool/manager.rs` (~280 LOC)
   - Extract connection logic: `pool/connection.rs` (~200 LOC)
   - Keep: `pool.rs` (~109 LOC)
   - Effort: 4-6 hours

**Validation**:
- [ ] All modules ≤ 500 LOC
- [ ] All tests passing
- [ ] Zero clippy warnings

#### Task 1.3: Integration Testing (Days 5-7)
**Effort**: 15-20 hours
**Priority**: P0
**Risk**: Medium

**Tasks**:
1. Update tests for new module structures (8-10 hours)
2. Run full test suite (2-3 hours)
3. Fix any broken tests (4-6 hours)
4. Run integration tests (2-3 hours)
5. Performance regression testing (1-2 hours)

**Success Criteria**:
- ✅ All tests passing (>95% pass rate)
- ✅ No performance regressions
- ✅ Integration tests passing

### Sprint 2: Error Handling Audit (Weeks 3-5)

#### Task 2.1: Audit and Categorize Unwraps (Days 1-3)
**Effort**: 30-40 hours
**Note**: **URGENT** - 598 unwrap calls (3.6x higher than initially reported)
**Priority**: P0
**Risk**: Low

**Process**:
```bash
# Find all unwrap/expect calls
grep -r "unwrap()" do-memory-core/src
grep -r "expect(" do-memory-core/src

# Categorize:
# Category A: Hot path unwraps (keep)
# Category B: Configuration unwraps (convert to Result)
# Category C: Database unwraps (convert to proper error)
# Category D: Test unwraps (keep)
```

**Output**: Spreadsheet with categorization
- File path
- Line number
- Category (A/B/C/D)
- Recommended action

#### Task 2.2: Convert Configuration Unwraps (Days 4-6)
**Effort**: 20-25 hours
**Priority**: P0
**Risk**: Low
**Note**: **URGENT** - Scale up 2.5x due to 3.6x more unwraps than initially reported

**Pattern**:
```rust
// Before
let max_episodes = config.max_episodes.unwrap();

// After
let max_episodes = config.max_episodes
    .ok_or_else(|| Error::ConfigMissing("max_episodes"))?;
```

**Files to Update**:
- `do-memory-core/src/config.rs`
- `do-memory-core/src/types.rs`
- `do-memory-cli/src/config/*.rs`

**Validation**:
- [ ] All config unwraps converted
- [ ] Error messages descriptive
- [ ] Error tests updated

#### Task 2.3: Convert Database Unwraps (Days 7-9)
**Effort**: 20-25 hours
**Note**: Scale up 2.5x due to more unwraps
**Priority**: P0
**Risk**: Low

**Pattern**:
```rust
// Before
let episode = storage.get_episode(id).unwrap();

// After
let episode = storage.get_episode(id)?
    .ok_or_else(|| Error::EpisodeNotFound(id))?;
```

**Files to Update**:
- `do-memory-core/src/memory/learning.rs`
- `do-memory-core/src/retrieval/cache/lru.rs`
- `do-memory-core/src/sync.rs`

**Validation**:
- [ ] All database unwraps converted
- [ ] Error cases handled
- [ ] Error tests passing

#### Task 2.4: Final Validation (Days 10-12)
**Effort**: 15-20 hours
**Note**: Extended timeframe for comprehensive validation
**Priority**: P0
**Risk**: Low

**Tasks**:
1. Recount unwrap/expect calls (<50 target)
2. Run full test suite (>95% pass rate)
3. Run cargo clippy --all -- -D warnings
4. Run quality gates script
5. Performance regression check

**Success Criteria**:
- ✅ Unwrap count < 50
- ✅ Test pass rate > 95%
- ✅ Zero clippy warnings
- ✅ Quality gates passing

### Phase 1 Summary

**Total Effort**: 175-229 hours
**Timeline**: 4-6 weeks (extended from 3 weeks)
**Deliverables**:
- ✅ 100% file size compliance
- ✅ 92% reduction in unwrap calls (598 → <50) - **2.4x more work than initially estimated**
- ✅ Test pass rate validated (>95%)
- ✅ Zero clippy warnings

---

## Phase 2: P1 High-Value Optimizations (Weeks 4-6)

### Goal
Improve performance, security, and retrieval accuracy

### Success Criteria
- ✅ Zero security vulnerabilities
- ✅ Clone operations reduced by 45% (183 → <100)
- ✅ Binary size reduced by 29% (2.1 GB → <1.5 GB)
- ✅ 7-15% performance improvement
- ✅ 20-30% retrieval accuracy improvement

### Sprint 3: Security Fixes (Week 4)

#### Task 3.1: Update Unmaintained Dependencies (Days 1-2)
**Effort**: 8-12 hours
**Priority**: P1
**Risk**: Low

**Dependencies to Update**:
1. Update `postcard` to 1.1.4+
2. Update `wasmtime` versions to latest
3. Verify `atomic-polyfill` removed

**Steps**:
```bash
# Update Cargo.toml
[dependencies]
postcard = { version = "1.1.4", features = ["alloc"] }

# Update and rebuild
cargo update
cargo build --all

# Verify no atomic-polyfill
cargo tree | grep atomic-polyfill

# Run tests
cargo test --all

# Verify with audit
cargo audit
```

**Validation**:
- [ ] `atomic-polyfill` removed from dependency tree
- [ ] All tests passing
- [ ] `cargo audit` shows no vulnerabilities

#### Task 3.2: Consolidate Duplicate Dependencies (Days 3-4)
**Effort**: 10-15 hours
**Priority**: P1
**Risk**: Medium

**Duplicated Packages**:
1. `approx v0.4.0` → upgrade to `v0.5.1`
2. `nalgebra` versions → consolidate to latest
3. `argmin` versions → consolidate to latest
4. `argmin-math` versions → consolidate to latest
5. `rv` versions → consolidate to latest

**Steps**:
```bash
# Use [patch.crates-io] in workspace Cargo.toml
[patch.crates-io]
# Force specific versions
approx = { version = "0.5.1" }
nalgebra = { version = "0.34.1" }
argmin = { version = "0.11.0" }

# Update direct dependencies
cargo update

# Verify consolidation
cargo tree --duplicates

# Run tests
cargo test --all
```

**Validation**:
- [ ] No duplicate dependencies
- [ ] All tests passing
- [ ] Binary size reduced (measure before/after)

#### Task 3.3: Security Testing (Days 5-7)
**Effort**: 10-12 hours
**Priority**: P1
**Risk**: Low

**Tasks**:
1. Run full security audit: `cargo audit`
2. Run `cargo deny check`
3. Run penetration tests
4. Verify sandbox resource limits
5. Document security posture

**Success Criteria**:
- ✅ Zero critical vulnerabilities
- ✅ Zero high-severity vulnerabilities
- ✅ All security tests passing

### Sprint 4: Clone Reduction (Week 5)

#### Task 4.1: Implement Arc for Shared Data (Days 1-3)
**Effort**: 20-25 hours
**Priority**: P1
**Risk**: Low

**Pattern**:
```rust
// Before
pub struct SelfLearningMemory {
    episodes: Vec<Episode>,
}

// After
pub struct SelfLearningMemory {
    episodes: Vec<Arc<Episode>>,
}

// Clone becomes cheap:
let episode_ref = self.episodes[i].clone(); // Arc::clone, not Episode::clone
```

**Files to Update**:
1. `do-memory-core/src/episode.rs` - Wrap Episode in Arc
2. `do-memory-core/src/pattern.rs` - Wrap Pattern in Arc
3. `do-memory-core/src/memory/learning.rs` - Update usage
4. `do-memory-core/src/retrieval/cache/lru.rs` - Update cache keys

**Steps**:
```rust
// 1. Wrap types
pub type EpisodeRef = Arc<Episode>;

// 2. Update collections
let episodes: Vec<Arc<Episode>> = ...;

// 3. Update clones
let episode: EpisodeRef = episodes[i].clone(); // Cheap Arc::clone
```

**Validation**:
- [ ] All clones converted to Arc::clone where appropriate
- [ ] All tests passing
- [ ] Performance improved (measure before/after)

#### Task 4.2: Implement Cow for Conditional Cloning (Days 4-5)
**Effort**: 8-12 hours
**Priority**: P1
**Risk**: Low

**Pattern**:
```rust
// Before
pub fn process_episode(&self, episode: Episode) -> Result<Episode> {
    let modified = episode.clone(); // Always clones
    // modify if needed
    Ok(modified)
}

// After
pub fn process_episode(&self, episode: Cow<Episode>) -> Result<Cow<Episode>> {
    if needs_modification(episode.as_ref()) {
        let modified = episode.into_owned();
        // modify
        Ok(Cow::Owned(modified))
    } else {
        Ok(episode) // No clone needed!
    }
}
```

**Files to Update**:
1. `do-memory-core/src/extraction/extractor.rs`
2. `do-memory-core/src/patterns/clustering.rs`
3. `do-memory-core/src/retrieval/hierarchical.rs`

**Validation**:
- [ ] Conditional cloning implemented
- [ ] All tests passing
- [ ] Performance benchmarked

#### Task 4.3: Performance Validation (Days 6-7)
**Effort**: 8-10 hours
**Priority**: P1
**Risk**: Low

**Tasks**:
1. Run full benchmark suite
2. Measure performance improvements
3. Identify any regressions
4. Tune Arc/Cow usage if needed

**Expected Impact**: 7-15% overall performance improvement

### Sprint 5: Test Recovery & Features (Week 6)

#### Task 5.1: Fix Failing Tests (Days 1-2)
**Effort**: 10-15 hours
**Priority**: P1
**Risk**: Low

**Process**:
```bash
# Run tests
cargo test --all 2>&1 | grep "test result"

# Identify failing tests
# Analyze root causes
# Fix systematically
```

**Common Issues**:
- Test expectations updated after refactoring
- Mock behavior changed
- Integration test environment issues

**Success Criteria**:
- ✅ Test pass rate > 95%
- ✅ All critical tests passing

#### Task 5.2: Implement Hybrid Search (Days 3-5)
**Effort**: 30-40 hours
**Priority**: P1
**Risk**: Low

**Architecture**:
```rust
pub struct HybridRetriever {
    semantic: SemanticRetriever,
    keyword: KeywordRetriever,
    filter: FilterRetriever,
}

impl HybridRetriever {
    pub async fn retrieve(
        &self,
        query: &str,
        filters: &Filters,
    ) -> Result<Vec<RetrievedEpisode>> {
        // 1. Get semantic results
        let semantic = self.semantic.retrieve(query, filters).await?;

        // 2. Get keyword results
        let keyword = self.keyword.retrieve(query, filters).await?;

        // 3. Get filtered results
        let filtered = self.filter.retrieve(filters).await?;

        // 4. Combine and re-rank
        let combined = self.combine_and_rank(
            semantic,
            keyword,
            filtered,
        )?;

        Ok(combined)
    }
}
```

**Implementation Steps**:
1. Create keyword retriever (8-10 hours)
2. Implement result combination (8-10 hours)
3. Implement re-ranking (8-10 hours)
4. Add tests (6-10 hours)

**Expected Impact**: 20-30% retrieval accuracy improvement

#### Task 5.3: Final Validation (Days 6-7)
**Effort**: 8-10 hours
**Priority**: P1
**Risk**: Low

**Tasks**:
1. Run full test suite
2. Run benchmarks
3. Validate performance improvements
4. Update documentation

**Success Criteria**:
- ✅ Zero security vulnerabilities
- ✅ Clone count < 100
- ✅ Test pass rate > 95%
- ✅ 7-15% performance improvement
- ✅ 20-30% retrieval accuracy improvement

### Phase 2 Summary

**Total Effort**: 97-135 hours
**Timeline**: 3 weeks
**Deliverables**:
- ✅ Zero security vulnerabilities
- ✅ Clone count < 100 (45% reduction)
- ✅ Binary size < 1.5 GB (29% reduction)
- ✅ 7-15% performance improvement
- ✅ 20-30% retrieval accuracy improvement

---

## Phase 3: P2 Quality of Life (Weeks 7-9)

### Goal
Enhanced developer experience and production observability

### Success Criteria
- ✅ Query caching operational (2-3x speedup)
- ✅ Configuration 100% optimized
- ✅ Enhanced pattern algorithms (DBSCAN, BOCPD)
- ✅ Production observability (Prometheus + tracing)
- ✅ CLI enhancements (interactive mode, completion)

### Sprint 6: Algorithm Enhancements (Week 7)

#### Task 6.1: DBSCAN Integration (Days 1-3)
**Effort**: 15-20 hours
**Priority**: P2
**Risk**: Low

**Implementation**:
```rust
// do-memory-core/src/patterns/dbscan.rs
pub struct DBSCANAnomalyDetector {
    eps: f64,
    min_samples: usize,
    adaptive: bool,
}

impl DBSCANAnomalyDetector {
    pub async fn detect_anomalies(
        &self,
        episodes: &[Episode],
    ) -> Result<Vec<Anomaly>> {
        // 1. Convert episodes to feature vectors
        let features = self.extract_features(episodes);

        // 2. Apply DBSCAN clustering
        let clusters = self.dbscan(&features)?;

        // 3. Identify outliers (anomalies)
        let anomalies = self.identify_outliers(&clusters);

        Ok(anomalies)
    }
}
```

**Integration Steps**:
1. Implement DBSCAN algorithm (8-10 hours)
2. Integrate into learning cycle (4-5 hours)
3. Add tests (3-5 hours)

**Expected Impact**: 5-10% pattern quality improvement

#### Task 6.2: Changepoint Detection (Days 4-5)
**Effort**: 20-25 hours
**Priority**: P2
**Risk**: Medium

**Implementation**:
```rust
// do-memory-core/src/patterns/changepoint.rs
pub struct ChangepointDetector {
    threshold: f64,
    window_size: usize,
}

impl ChangepointDetector {
    pub async fn detect_changepoints(
        &self,
        metrics: &[Metrics],
    ) -> Result<Vec<Changepoint>> {
        // 1. Sliding window over metrics
        // 2. Apply PELT algorithm (via augurs)
        // 3. Filter by threshold
        Ok(changepoints)
    }
}
```

**Integration Steps**:
1. Use `augurs-changepoint` crate (10-12 hours)
2. Integrate into monitoring (6-8 hours)
3. Add tests (4-5 hours)

**Expected Impact**: 10-15% pattern accuracy improvement

#### Task 6.3: Validation & Testing (Days 6-7)
**Effort**: 10-12 hours
**Priority**: P2
**Risk**: Low

**Tasks**:
1. Run full test suite
2. Validate algorithm results
3. Performance benchmarking
4. Update documentation

**Success Criteria**:
- ✅ DBSCAN detecting anomalies
- ✅ BOCPD detecting changepoints
- ✅ 5-10% pattern quality improvement
- ✅ 10-15% pattern accuracy improvement

### Sprint 7: Configuration & CLI (Week 8)

#### Task 7.1: Configuration Polish (Days 1-3)
**Effort**: 28-42 hours
**Priority**: P2
**Risk**: Low

**Tasks**:
1. Wizard UX refinement (10-15 hours)
   - Better error messages with suggestions
   - Pre-populated defaults
   - Validation before submission
   - Save/load profiles

2. Performance optimization (8-12 hours)
   - Incremental config loading
   - Parallel validation
   - LRU cache for parsed configs

3. Enhanced documentation (10-15 hours)
   - Troubleshooting guide
   - Common use case examples
   - Video tutorials

**Expected Impact**: Improved user onboarding

#### Task 7.2: CLI Enhancements (Days 4-6)
**Effort**: 28-35 hours
**Priority**: P2
**Risk**: Low

**Tasks**:
1. Interactive mode (20-25 hours)
   ```rust
   // Interactive REPL
   pub struct InteractiveMode {
       memory: SelfLearningMemory,
   }

   impl InteractiveMode {
       pub async fn run(&mut self) -> Result<()> {
           loop {
               // Read command
               // Execute
               // Display results
           }
       }
   }
   ```

2. Completion scripts (8-10 hours)
   - Bash completion
   - Zsh completion
   - Fish completion

**Expected Impact**: Improved developer experience

#### Task 7.3: Testing & Validation (Days 7)
**Effort**: 10-12 hours
**Priority**: P2
**Risk**: Low

**Tasks**:
1. Run full test suite
2. User acceptance testing
3. Documentation verification

**Success Criteria**:
- ✅ Configuration 100% optimized
- ✅ Interactive mode working
- ✅ Shell completion scripts generated

### Sprint 8: Observability (Week 9)

#### Task 8.1: Prometheus Metrics (Days 1-3)
**Effort**: 15-20 hours
**Priority**: P2
**Risk**: Low

**Implementation**:
```rust
// do-memory-core/src/monitoring/prometheus.rs
pub struct PrometheusExporter {
    metrics: AgentMetrics,
    registry: prometheus::Registry,
}

impl PrometheusExporter {
    pub fn export(&self) -> String {
        // Convert metrics to Prometheus format
    }

    pub fn serve(self, addr: SocketAddr) -> Result<()> {
        // HTTP endpoint for scraping
        let app = Router::new()
            .route("/metrics", get(metrics_handler));
        // Serve...
    }
}
```

**Features**:
- Episode creation rate
- Pattern extraction latency
- Cache hit rate
- Storage operation latency
- Test coverage metrics

#### Task 8.2: Distributed Tracing (Days 4-5)
**Effort**: 20-25 hours
**Priority**: P2
**Risk**: Medium

**Implementation**:
```rust
// Use opentelemetry-rust
use opentelemetry::trace::TraceResult;
use opentelemetry::trace::Tracer;

#[tracing::instrument(skip(self))]
pub async fn start_episode(&self, task: String) -> Result<Uuid> {
    // Automatically traced
    let episode_id = Uuid::new_v4();
    // ...
    Ok(episode_id)
}
```

**Features**:
- Trace episode lifecycle
- Correlate storage operations
- Export to Jaeger/Zipkin

#### Task 8.3: Validation & Documentation (Days 6-7)
**Effort**: 10-12 hours
**Priority**: P2
**Risk**: Low

**Tasks**:
1. Test metrics endpoint
2. Verify tracing integration
3. Update deployment docs
4. Create monitoring guide

**Success Criteria**:
- ✅ Prometheus metrics exportable
- ✅ Distributed tracing operational
- ✅ Production observability ready

### Phase 3 Summary

**Total Effort**: 146-197 hours
**Timeline**: 3 weeks
**Deliverables**:
- ✅ Enhanced pattern algorithms (DBSCAN, BOCPD)
- ✅ Configuration 100% optimized
- ✅ CLI enhancements (interactive, completion)
- ✅ Production observability (Prometheus + tracing)

---

## Phase 4: P3 Advanced Features (Weeks 10+)

### Goal
Enterprise-ready features and advanced learning capabilities

### Success Criteria
- ✅ Contrastive learning for embeddings (+5-10% accuracy)
- ✅ Advanced pattern mining (+15-25% discovery)
- ✅ Cross-modal retrieval (+15-25% coverage)
- ✅ Docker & K8s deployment
- ✅ High availability features

### Sprint 9: Learning & Retrieval (Weeks 10-12)

#### Task 9.1: Contrastive Learning (Days 1-5)
**Effort**: 40-50 hours
**Priority**: P3
**Risk**: Medium

**Implementation**:
```rust
// do-memory-core/src/embeddings/contrastive.rs
pub struct ContrastiveLearner {
    encoder: Arc<dyn EmbeddingProvider>,
    temperature: f64,
    projection_head: Option<nn::Linear>,
}

impl ContrastiveLearner {
    pub async fn train(
        &mut self,
        episodes: &[Episode],
    ) -> Result<TrainingMetrics> {
        // 1. Sample pairs (positive, negative)
        // 2. Compute embeddings
        // 3. Apply contrastive loss
        // 4. Backpropagate
        // 5. Repeat for epochs
        Ok(metrics)
    }
}
```

**Steps**:
1. Implement contrastive loss (15-18 hours)
2. Training infrastructure (12-15 hours)
3. Integration with embedding provider (8-10 hours)
4. Tests and validation (5-7 hours)

**Expected Impact**: +5-10% retrieval accuracy improvement

#### Task 9.2: Advanced Pattern Mining (Days 6-8)
**Effort**: 30-40 hours
**Priority**: P3
**Risk**: High

**Implementation**:
```rust
// do-memory-core/src/patterns/mining.rs
pub struct PatternMiner {
    min_support: f64,
    max_length: usize,
}

impl PatternMiner {
    pub async fn mine_patterns(
        &self,
        episodes: &[Episode],
    ) -> Result<Vec<MinedPattern>> {
        // 1. Convert episodes to transactions
        // 2. Apply FP-Growth algorithm
        // 3. Filter by min_support
        // 4. Extract patterns
        Ok(patterns)
    }
}
```

**Steps**:
1. Implement FP-Growth (12-15 hours)
2. Pattern extraction (8-10 hours)
3. Integration (6-8 hours)
4. Tests (4-7 hours)

**Expected Impact**: +15-25% pattern discovery improvement

#### Task 9.3: Cross-Modal Retrieval (Days 9-12)
**Effort**: 50-60 hours
**Priority**: P3
**Risk**: High

**Implementation**:
```rust
// do-memory-core/src/retrieval/cross_modal.rs
pub struct CrossModalRetriever {
    text_encoder: Arc<dyn EmbeddingProvider>,
    code_encoder: Arc<dyn CodeEmbeddingProvider>,
    log_encoder: Arc<dyn LogEmbeddingProvider>,
}

impl CrossModalRetriever {
    pub async fn retrieve_from_code(
        &self,
        code: &str,
    ) -> Result<Vec<RetrievedEpisode>> {
        // 1. Encode code
        let code_embedding = self.code_encoder.encode(code)?;

        // 2. Search episode embeddings
        let episodes = self.search_episodes(&code_embedding)?;

        Ok(episodes)
    }
}
```

**Steps**:
1. Code embedding model (15-18 hours)
2. Log embedding model (10-12 hours)
3. Cross-modal fusion (12-15 hours)
4. Integration (8-10 hours)
5. Tests (5-7 hours)

**Expected Impact**: +15-25% retrieval coverage improvement

### Sprint 10: Deployment & Operations (Weeks 13-14)

#### Task 10.1: Docker & K8s Deployment (Days 1-4)
**Effort**: 25-30 hours
**Priority**: P3
**Risk**: Low

**Deliverables**:
1. Multi-stage Dockerfile (8-10 hours)
2. Docker Compose for local dev (5-6 hours)
3. Helm charts for K8s (8-10 hours)
4. Deployment guides (4-6 hours)

**Expected Impact**: Easier deployment

#### Task 10.2: High Availability Features (Days 5-8)
**Effort**: 35-45 hours
**Priority**: P3
**Risk**: Medium

**Features**:
1. Connection pool health checks (8-10 hours)
2. Automatic connection recovery (8-10 hours)
3. Circuit breaker enhancements (8-10 hours)
4. Scheduled backups (6-8 hours)
5. Automated restore testing (5-7 hours)

**Expected Impact**: Improved resilience

#### Task 10.3: Documentation & Testing (Days 9-10)
**Effort**: 15-20 hours
**Priority**: P3
**Risk**: Low

**Tasks**:
1. Update deployment docs (6-8 hours)
2. Create monitoring guide (4-6 hours)
3. Run integration tests (3-4 hours)
4. User acceptance testing (2-4 hours)

**Success Criteria**:
- ✅ Docker images buildable
- ✅ K8s deployment successful
- ✅ HA features operational

### Phase 4 Summary

**Total Effort**: 180-225 hours
**Timeline**: 4-6 weeks
**Deliverables**:
- ✅ Contrastive learning (+5-10% accuracy)
- ✅ Advanced pattern mining (+15-25% discovery)
- ✅ Cross-modal retrieval (+15-25% coverage)
- ✅ Docker & K8s deployment
- ✅ High availability features

---

## Summary

### Overall Timeline

| Phase | Duration | Effort | Priority | Key Deliverables |
|--------|----------|--------|-----------|-----------------|
| **Phase 1: P0 Compliance** | 4-6 weeks | 175-229 hrs | P0 | File compliance, error handling (598 unwraps) |
| **Phase 2: P1 Optimizations** | 3 weeks | 97-135 hrs | P1 | Security, performance, accuracy |
| **Phase 3: P2 Quality** | 3 weeks | 146-197 hrs | P2 | Algorithms, config, observability |
| **Phase 4: P3 Advanced** | 4-6 weeks | 180-225 hrs | P3 | Learning, deployment, HA |

**Total**: 14-21 weeks | 598-786 hours (up from 542-718 hours)

### Immediate Next Steps (This Week)

1. ✅ **Day 1-3**: Start P0 file splitting (do-memory-mcp sandbox files)
2. ✅ **Day 4-5**: Continue with storage files
3. ✅ **Day 6-7**: Integration testing

### Recommended Path

**Phase 1 (Weeks 1-6)** - DO FIRST
- Critical for codebase compliance
- Blocks code reviews
- Low risk, high value
- **NOTE**: Extended from 3 weeks to 4-6 weeks due to 598 unwrap calls (3.6x more than estimated)

**Phase 2 (Weeks 7-9)** - DO SECOND
- Strong ROI (security + performance + accuracy)
- Production-ready improvements
- Low-medium risk

**Phase 3 (Weeks 7-9)** - DO THIRD
- Enhanced developer experience
- Production observability
- Low-medium risk

**Phase 4 (Weeks 10+)** - DO LAST
- Enterprise features
- Advanced capabilities
- Medium-high risk

### Success Metrics

| Category | Metric | Target | Phase |
|----------|---------|--------|-------|
| **Code Quality** | Files > 500 LOC | 0 | P0 |
| | Unwrap calls | <50 | P0 |
| | Test pass rate | >95% | P0 |
| **Performance** | Clone operations | <100 | P1 |
| | Binary size | <1.5 GB | P1 |
| | Overall performance | +7-15% | P1 |
| **Security** | Vulnerabilities | 0 | P1 |
| | Duplicate deps | 0 | P1 |
| **Retrieval** | Accuracy (F1) | 40% | P1 |
| **Advanced** | Contrastive learning | +5-10% | P3 |
| | Pattern mining | +15-25% | P3 |
| | Cross-modal retrieval | +15-25% | P3 |

---

**Generated**: 2026-01-11
**Based On**: COMPREHENSIVE_GAP_ANALYSIS_2026-01-11.md
**Status**: Ready for execution
**Next Action**: Start Phase 1, Sprint 1, Task 1.1
