DLM Package Refactoring Audit
**Date:** 2025-12-08 **Status:** ๐ In Progress **Purpose:** Comprehensive audit and refactoring plan to reduce technical debt
Full Public Reader
DLM Package Refactoring Audit
Date: 2025-12-08
Status: ๐ In Progress
Purpose: Comprehensive audit and refactoring plan to reduce technical debt
---
Executive Summary
The DLM package has grown to 63,339 lines across 154 Python files with significant technical debt and consolidation opportunities.
Key Findings
1. Large Files: 10 files exceed 1,000 lines (should be <500)
2. Duplicate Functionality: Multiple embedders, loaders, and config systems
3. Unclear Organization: Mixed concerns (response/vangaurd, engine overlap)
4. Legacy Code: Old implementations (legacy_utils.py, deprecated embedders)
5. Test Fragmentation: Tests scattered across multiple locations
Critical Issues
| Issue | Severity | Files Affected | Impact |
|---|---|---|---|
| Pydantic v2 Incompatibility | ๐จ Critical | models/*.py, base.py | Blocks imports |
| Duplicate Embedders | โ High | engine/embedder.py (1604 lines), engine/ircp_embedder.py | Confusion |
| Mega Files | โ ๏ธ High | inference/artificial.py (3691 lines) | Unmaintainable |
| Legacy Utils | โ ๏ธ Medium | utils/legacy_utils.py (1518 lines) | Tech debt |
| Test Scatter | โ ๏ธ Medium | tests/, core/tests/ | Fragmentation |
---
Current Structure Analysis
Directory Overview
dlm/
โโโ core/ # Week 2-3 NEW (unified, clean)
โ โโโ coordinates.py 827 lines โ
โ โโโ embeddings.py 295 lines โ
โ โโโ data_loader.py 576 lines โ
โ โโโ adapters.py 296 lines โ
โ โโโ ircp/ Reference wrappers โ
โ
โโโ evaluation/ # Week 3 NEW (clean)
โ โโโ metrics.py 447 lines โ
โ โโโ validators.py 375 lines โ
โ
โโโ explainability/ # Week 3 NEW (clean)
โ โโโ analyzer.py 595 lines โ
โ โโโ debugger.py 450 lines โ
โ โโโ visualizer.py 350 lines โ
โ
โโโ pipeline/ # Week 3 NEW (clean)
โ โโโ checkpoint_manager.py 370 lines โ
โ โโโ data_pipeline.py 330 lines โ
โ โโโ training_pipeline.py 480 lines โ
โ
โโโ engine/ # LEGACY (overlaps with core)
โ โโโ embedder.py 1604 lines โ CONSOLIDATE
โ โโโ ircp_embedder.py 300 lines โ DEPRECATED
โ โโโ builder.py 1004 lines โ ๏ธ REVIEW
โ โโโ structure.py 851 lines โ ๏ธ REVIEW
โ โโโ loader.py 564 lines โ DUPLICATE (core/data_loader)
โ โโโ ... (11 more files)
โ
โโโ inference/ # ACTIVE (but needs cleanup)
โ โโโ artificial.py 3691 lines ๐จ TOO LARGE
โ โโโ prompt.py 2152 lines ๐จ TOO LARGE
โ โโโ generator.py 1432 lines โ ๏ธ LARGE
โ โโโ cloud_manager.py 834 lines โ ๏ธ REVIEW
โ โโโ ... (9 more files)
โ
โโโ relationship/ # ACTIVE (complex but necessary)
โ โโโ graph.py 1639 lines โ ๏ธ LARGE
โ โโโ merger.py 1146 lines โ ๏ธ LARGE
โ โโโ estimator.py 1064 lines โ ๏ธ LARGE
โ โโโ base.py 999 lines โ ๏ธ LARGE
โ
โโโ response/ # ACTIVE (user-facing techniques)
โ โโโ links.py 2083 lines ๐จ TOO LARGE
โ โโโ system.py 1520 lines ๐จ TOO LARGE
โ โโโ vangaurd/ 30+ files โ ๏ธ MANY FILES
โ โ โโโ motion.py 1782 lines ๐จ TOO LARGE
โ โ โโโ barista.py 670 lines
โ โ โโโ synth/ 15+ files โ ๏ธ ORGANIZE
โ โ โโโ ... (many more)
โ โโโ ... (13 more files)
โ
โโโ models/ # LEGACY (Pydantic v1)
โ โโโ All files ~2000 lines ๐จ PYDANTIC V2 ISSUE
โ โโโ ... (9 files)
โ
โโโ transformation/ # ACTIVE (coordinate transforms)
โ โโโ tree.py 695 lines
โ โโโ hypnotherapy.py 557 lines
โ โโโ ... (7 files)
โ
โโโ services/ # UTILITY (metrics, preprocessing)
โ โโโ metrics.py 1389 lines โ ๏ธ LARGE
โ โโโ ... (5 files)
โ
โโโ callbacks/ # ACTIVE (streaming, events)
โ โโโ manager.py 770 lines
โ โโโ streaming.py 654 lines
โ โโโ base.py 579 lines
โ
โโโ utils/ # MIXED
โ โโโ legacy_utils.py 1518 lines โ LEGACY
โ โโโ logger.py New logger โ
โ
โโโ tests/ # FRAGMENTED
โโโ tests/ Main test dir โ
โโโ core/tests/ Duplicate location โ ๏ธ---
Consolidation Opportunities
1. Embedders (CRITICAL)
Current State:
- `engine/embedder.py` (1604 lines) - Old implementation
- `engine/ircp_embedder.py` (300 lines) - Deprecated
- `core/embeddings.py` (295 lines) - NEW Week 2 implementation
Action:
- โ
KEEP: `core/embeddings.py` (already clean)
- โ DEPRECATE: `engine/embedder.py` โ Move to `_deprecated/`
- โ DELETE: `engine/ircp_embedder.py` (superseded by core/adapters.py)
2. Data Loaders (HIGH PRIORITY)
Current State:
- `core/data_loader.py` (576 lines) - NEW Week 3 implementation โ
- `engine/loader.py` (564 lines) - Old implementation
Action:
- โ
KEEP: `core/data_loader.py`
- โ DEPRECATE: `engine/loader.py` โ Move to `_deprecated/`
3. Configuration (MEDIUM PRIORITY)
Current State:
- `config.py` (510 lines) - NEW Week 2 unified config โ
- `response/config.py` (deprecated marker) โ
Action:
- โ
KEEP: `config.py`
- โ
ALREADY DEPRECATED: `response/config.py`
4. Coordinates (GOOD STATE)
Current State:
- `core/coordinates.py` (827 lines) - NEW Week 2 unified โ
- `transformation/coordinate.py` - Transform operations
-No conflicts
Action:
- โ
KEEP BOTH: Different purposes
5. Models (CRITICAL - Pydantic v2)
Current State:
- `models/*.py` - All use Pydantic v1 `@root_validator`
- Blocking all imports from `dlm` package
Action:
- ๐จ FIX IMMEDIATELY: Update to Pydantic v2
- Change `@root_validator` โ `@model_validator`
- Add `skip_on_failure=True` or migrate properly
---
File Size Refactoring
Files Exceeding 1000 Lines (Should be <500)
| File | Lines | Action |
|---|---|---|
| `inference/artificial.py` | 3691 | Split into 6-7 modules |
| `inference/prompt.py` | 2152 | Split into 4-5 modules |
| `response/links.py` | 2083 | Split by link type |
| `response/vangaurd/motion.py` | 1782 | Split by motion type |
| `relationship/graph.py` | 1639 | Split graph operations |
| `engine/embedder.py` | 1604 | DEPRECATE (use core/) |
| `response/system.py` | 1520 | Split by system type |
| `utils/legacy_utils.py` | 1518 | DEPRECATE/MIGRATE |
| `inference/generator.py` | 1432 | Split generators |
| `services/metrics.py` | 1389 | Split metric types |
| `base.py` | 1218 | Split base classes |
| `relationship/merger.py` | 1146 | Refactor mergers |
| `relationship/estimator.py` | 1064 | Refactor estimators |
| `engine/builder.py` | 1004 | Review necessity |
| `relationship/base.py` | 999 | Already complex |
---
Proposed New Structure
dlm/
โโโ core/ # Core functionality (Week 2-3)
โ โโโ __init__.py
โ โโโ coordinates.py โ
Keep
โ โโโ embeddings.py โ
Keep
โ โโโ data_loader.py โ
Keep
โ โโโ adapters.py โ
Keep
โ โโโ ircp/ โ
Keep
โ
โโโ training/ # Training pipeline (Week 3)
โ โโโ __init__.py
โ โโโ pipeline.py Merge from pipeline/
โ โโโ checkpoints.py From pipeline/checkpoint_manager.py
โ โโโ data.py From pipeline/data_pipeline.py
โ โโโ config.py Training-specific config
โ
โโโ evaluation/ # Evaluation & metrics
โ โโโ __init__.py
โ โโโ metrics.py โ
Keep
โ โโโ validators.py โ
Keep
โ โโโ quality.py NEW: From services/metrics.py
โ
โโโ explainability/ # Coordinate explainability
โ โโโ __init__.py
โ โโโ analyzer.py โ
Keep
โ โโโ debugger.py โ
Keep
โ โโโ visualizer.py โ
Keep
โ
โโโ inference/ # LLM inference (REFACTOR)
โ โโโ __init__.py
โ โโโ manager.py โ
Keep (ChainManager)
โ โโโ generator/ NEW SUBDIR
โ โ โโโ __init__.py
โ โ โโโ base.py From generator.py (split)
โ โ โโโ artificial.py From artificial.py (split)
โ โ โโโ cloud.py From cloud_manager.py
โ โโโ prompts/ NEW SUBDIR
โ โ โโโ __init__.py
โ โ โโโ templates.py From prompt.py (split)
โ โ โโโ manager.py From prompt_manager.py
โ โ โโโ builder.py From prompt.py (split)
โ โโโ session.py โ
Keep
โ โโโ state.py โ
Keep
โ โโโ validator.py โ
Keep
โ
โโโ relationships/ # Relationship estimation
โ โโโ __init__.py
โ โโโ graph/ NEW SUBDIR
โ โ โโโ __init__.py
โ โ โโโ builder.py From graph.py (split)
โ โ โโโ traversal.py From graph.py (split)
โ โ โโโ operations.py From graph.py (split)
โ โโโ estimator.py Keep but refactor
โ โโโ merger.py Keep but refactor
โ โโโ base.py โ
Keep
โ
โโโ response/ # Response generation techniques
โ โโโ __init__.py
โ โโโ builder.py โ
Keep
โ โโโ factory.py โ
Keep
โ โโโ system.py Refactor/split
โ โโโ links/ NEW SUBDIR
โ โ โโโ __init__.py
โ โ โโโ builder.py From links.py (split)
โ โ โโโ types.py From links.py (split)
โ โ โโโ validator.py From links.py (split)
โ โโโ embedding_provider.py โ
Keep
โ โโโ validators.py โ
Keep
โ โโโ techniques/ Rename from vangaurd/
โ โโโ __init__.py
โ โโโ synthesis/ From vangaurd/synth/
โ โโโ personas/ From vangaurd/word_weaver/
โ โโโ ... (organize)
โ
โโโ transformation/ # Coordinate transformations
โ โโโ __init__.py
โ โโโ coordinates.py โ
Keep
โ โโโ tree.py โ
Keep
โ โโโ ... (keep)
โ
โโโ models/ # Data models (FIX PYDANTIC)
โ โโโ __init__.py
โ โโโ message.py Fix Pydantic v2
โ โโโ chain.py Fix Pydantic v2
โ โโโ generation.py Fix Pydantic v2
โ โโโ ... (fix all)
โ
โโโ callbacks/ # Event callbacks
โ โโโ __init__.py
โ โโโ base.py โ
Keep
โ โโโ manager.py โ
Keep
โ โโโ streaming.py โ
Keep
โ
โโโ services/ # Utility services
โ โโโ __init__.py
โ โโโ preprocessing.py โ
Keep
โ โโโ temporal.py โ
Keep
โ โโโ context.py โ
Keep
โ โโโ split.py โ
Keep
โ
โโโ utils/ # Utilities
โ โโโ __init__.py
โ โโโ logger.py โ
Keep (new)
โ
โโโ _deprecated/ # OLD CODE (don't break imports yet)
โ โโโ engine/
โ โ โโโ embedder.py From engine/
โ โ โโโ loader.py From engine/
โ โ โโโ ircp_embedder.py From engine/
โ โโโ legacy_utils.py From utils/
โ โโโ README.md Deprecation notice
โ
โโโ tests/ # Consolidated tests
โ โโโ __init__.py
โ โโโ core/ Core tests
โ โโโ training/ Training tests
โ โโโ evaluation/ Evaluation tests
โ โโโ explainability/ Explainability tests
โ โโโ integration/ Integration tests
โ
โโโ examples/ # Usage examples
โ โโโ training/
โ โโโ explainability/
โ โโโ inference/
โ
โโโ __init__.py Main package init
โโโ config.py โ
Keep (Week 2 unified)
โโโ base.py Refactor/split
โโโ interface.py Review
DELETE:
- engine/ (mostly deprecated)
- utils/legacy_utils.py---
Action Plan
Phase 1: Critical Fixes (Immediate)
1. Fix Pydantic v2 Compatibility ๐จ
- Update all `@root_validator` โ `@model_validator`
- Test imports work
- Priority: CRITICAL
2. Mark Deprecated Code โ
- Move `engine/embedder.py` โ `_deprecated/`
- Move `engine/loader.py` โ `_deprecated/`
- Move `engine/ircp_embedder.py` โ `_deprecated/`
- Move `utils/legacy_utils.py` โ `_deprecated/`
- Add deprecation warnings
Phase 2: Consolidate Tests
3. Unify Test Location
- Move `core/tests/` โ `tests/core/`
- Ensure all tests pass
- Update imports
Phase 3: Split Large Files
4. Split inference/artificial.py (3691 lines)
- Create `inference/generator/` subdirectory
- Split by generator type
5. Split inference/prompt.py (2152 lines)
- Create `inference/prompts/` subdirectory
- Split templates/manager/builder
6. Split response/links.py (2083 lines)
- Create `response/links/` subdirectory
7. Split other 1000+ line files
- Prioritize by usage frequency
Phase 4: Organize Structure
8. Reorganize response/vangaurd/
- Rename to `response/techniques/`
- Better organization
9. Clean up engine/
- Keep only actively used files
- Move rest to `_deprecated/`
Phase 5: Documentation
10. Update Documentation
- README for new structure
- Migration guide
- API documentation
---
Impact Assessment
### Files to Keep (Week 2-3 NEW)
- โ
`core/*` (clean, tested)
- โ
`evaluation/*` (clean, tested)
- โ
`explainability/*` (clean, tested)
- โ
`pipeline/*` (clean, tested)
- โ
`config.py` (unified)
### Files to Deprecate
- โ `engine/embedder.py`
- โ `engine/loader.py`
- โ `engine/ircp_embedder.py`
- โ `utils/legacy_utils.py`
### Files to Refactor (Split)
- โ ๏ธ `inference/artificial.py` (3691 โ 500 each)
- โ ๏ธ `inference/prompt.py` (2152 โ 500 each)
- โ ๏ธ `response/links.py` (2083 โ 500 each)
- โ ๏ธ `response/vangaurd/motion.py` (1782 โ 500 each)
### Files to Fix
- ๐จ `models/*.py` (Pydantic v2)
---
Success Metrics
- [ ] All imports work without Pydantic errors
- [ ] No files exceed 1000 lines
- [ ] Clear separation of concerns
- [ ] All tests pass
- [ ] Documentation updated
- [ ] <10
- [ ] Clear deprecation path
---
Timeline
- Phase 1: 2-3 hours (Critical fixes)
- Phase 2: 1-2 hours (Test consolidation)
- Phase 3: 6-8 hours (Split large files)
- Phase 4: 3-4 hours (Reorganize)
- Phase 5: 2-3 hours (Documentation)
Total Estimated: 14-20 hours
---
Status: AUDIT COMPLETE - READY FOR EXECUTION
Promotion Decision
Attach run IDs, datasets, metrics, and reproduction commands.
Source Anchor
Comp-Core/backend/cc-trajectory/legacy/cc-tpo-original/cc-tpo/docs/refactoring/DLM_REFACTORING_AUDIT.md
Detected Structure
Method ยท Evaluation ยท Code Anchors ยท Architecture