Skip to content
This repository was archived by the owner on Jun 16, 2026. It is now read-only.

[DI Phase 1] Foundation + monorepo prep (tokenizers, daemon, SearchResult) #117

Description

Phase 1: DI Foundation + Monorepo Prep

Parent Epic: #116
Target: Week 1 (5-7 days)
Risk Level: Low

Build core DI infrastructure while preparing for monorepo split by extracting ready packages and moving shared types.

Goals

  • Core DI components (Container, Depends(), factories)
  • Type-safe dependency resolution
  • Override mechanism for testing
  • Extract ready packages (tokenizers, daemon)
  • Move SearchResult types to core
  • 100% test coverage on DI
  • Zero production changes

Why This Phase Matters

This phase creates the foundation that will eliminate 75% of circular dependencies in Phase 2. It also extracts packages that are already independent, giving us quick wins.

Implementation Checklist

Part A: Package Extraction (Days 1-2)

Extract immediately (already independent):

  • Create packages/codeweaver-tokenizers/

    • Move src/codeweaver/tokenizers/ → package
    • Create pyproject.toml
    • Test independence
    • Update imports
  • Create packages/codeweaver-daemon/

    • Move src/codeweaver/daemon/ → package
    • Create pyproject.toml
    • Test independence
    • Update imports

Move shared types:

  • Create src/codeweaver/core/search_types.py
  • Extract SearchResult from agent_api/find_code/types.py
  • Update imports across codebase
  • Validate no circular dependencies introduced

Part B: DI Infrastructure (Days 3-5)

Core components:

  • Create codeweaver/di/ package structure
    • __init__.py - Public API
    • container.py - Container implementation
    • depends.py - Depends marker
    • providers.py - Provider factories
    • types.py - Type definitions

Container Implementation:

  • Container class with registration mechanism
  • Resolution with caching (singleton support)
  • Override mechanism for testing
  • Lifecycle hooks (startup/shutdown)
  • Error handling and validation
  • Async support

Provider Factories:

  • get_embedding_provider() factory
  • get_sparse_embedding_provider() factory (optional)
  • get_reranking_provider() factory (optional)
  • get_vector_store() factory
  • Type aliases (EmbeddingDep, VectorStoreDep, etc.)

Key Pattern:

# Factory hides complexity
async def get_embedding_provider() -> EmbeddingProvider:
    """Factory auto-resolves from registry + settings."""
    from codeweaver.common.registry import get_provider_registry
    registry = get_provider_registry()
    # ... all the messy logic HERE, not in services
    return registry.get_embedding_provider_instance(...)

# Type alias for clean signatures
EmbeddingDep = Annotated[EmbeddingProvider, Depends(get_embedding_provider)]

Part C: Testing (Days 6-7)

Comprehensive testing:

  • Container registration tests
  • Container resolution tests
  • Override mechanism tests
  • Singleton behavior tests
  • Lifecycle hooks tests
  • Factory tests
  • Integration tests
  • 100% coverage on DI infrastructure

Test examples:

def test_container_override():
    container = Container()
    container.register(EmbeddingProvider, get_embedding_provider)
    container.override(EmbeddingProvider, MockEmbedding())
    result = await container.resolve(EmbeddingProvider)
    assert isinstance(result, MockEmbedding)

Part D: Documentation

  • API documentation (docstrings)
  • Usage examples
  • Testing guide
  • Architecture decision records
  • Migration guide (draft)

Package Placement

DI infrastructure location:

  • Initially: src/codeweaver/di/ (within main package)
  • Phase 3: Move to packages/codeweaver-core/ (foundation layer)

Provider factories:

  • Initially: src/codeweaver/di/providers.py
  • Phase 3: Stay with or move to packages/codeweaver-providers/

Why Extract Packages Now?

Tokenizers & Daemon:

  • ✅ Already independent (0 dependency violations)
  • ✅ Perfect test cases for monorepo structure
  • ✅ Quick wins that prove the approach
  • ✅ Won't be affected by DI migration

SearchResult types:

  • ✅ Breaks provider → agent_api dependency
  • ✅ Makes core types truly foundational
  • ✅ Enables clean package boundaries in Phase 3

Acceptance Criteria

  • Container can register/resolve dependencies
  • Override mechanism works for testing
  • All tests passing (100% coverage on DI)
  • Tokenizers extracted and building independently
  • Daemon extracted and building independently
  • SearchResult in core with no circular deps
  • No changes to existing production code (except imports)
  • Documentation complete
  • Code review approved
  • Performance baseline established

Deliverables

End of Week 1:

  1. ✅ Working DI system (tested, documented)
  2. ✅ 2 packages extracted (tokenizers, daemon)
  3. ✅ SearchResult types moved to core
  4. ✅ Zero production behavior changes
  5. ✅ Ready for Phase 2 migration

Connection to Monorepo Strategy

This phase creates the foundation for Week 1 of the integrated strategy:

  • DI foundation → Enables Phase 2 to break circular dependencies
  • Package extraction → Proves monorepo structure works
  • Type movement → Eliminates key coupling points

Success Metrics

Technical:

  • 0 test failures
  • 100% coverage on DI code
  • 2 packages building independently
  • SearchResult moved with 0 violations

Quality:

  • Clear documentation
  • Simple, obvious API design
  • FastAPI-familiar patterns
  • Easy to extend

Next Phase Preview

Phase 2 (#118) will use this foundation to:

  • Migrate Indexer to DI → Eliminates 20+ violations
  • Migrate search services → Eliminates 30+ violations
  • Update tests with overrides → Simplifies testing by 80%

Reference

  • Planning: INTEGRATED_DI_MONOREPO_STRATEGY.md (Week 1)
  • Examples: plans/DI_PROVIDER_EXAMPLES.md
  • Original plan: plans/dependency-injection-architecture-plan.md

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions