5.8 KiB
Codebase Consolidation & DRY Cleanup — Design
Date: 2026-03-01
Branch: feature/codebase-consolidation
Estimated savings: ~13,100 lines
Context
Following the ML inference cleanup (-8,281 lines), a comprehensive codebase audit
revealed 4 major cleanup areas across 543K LOC (crates+services). The ml crate
alone is 247K LOC but is well-structured; the biggest opportunities are in
shared infrastructure, compliance boilerplate, and service-level glue code.
Area 1: DRY Violations (~600 lines saved)
Problem
Three type definitions exist as exact duplicates in multiple crates:
| Type | Canonical Location | Duplicate Location |
|---|---|---|
RetryStrategy enum |
common/src/error.rs:159 |
trading_engine/src/types/error.rs:18 |
ErrorSeverity enum |
common/src/error.rs:132 |
data/src/error.rs:441, database/src/error.rs:119 |
RetryConfig struct |
common/src/resilience/retry.rs:40 |
storage/src/model_helpers.rs:141 |
Design
- Keep canonical definitions in
common/ - Delete duplicates from
trading_engine,data,database,storage - Re-export from
commoncrate root if not already - Update all
usestatements in consuming code - Verify with
cargo check --workspace
Risk
Low. These are exact duplicates with identical APIs. The only risk is missed import sites, which the compiler will catch.
Area 2: common/src/types.rs Split (structural, ~4,909 LOC)
Problem
common/src/types.rs is a 4,909-line god file containing 6+ unrelated domains:
type aliases, service types, market data types, order types, connection types,
error types, and currency types. This makes the file hard to navigate and
violates the project's 500-line-per-file convention.
Design
Split into focused modules under common/src/types/:
| Module | Content | Est. LOC |
|---|---|---|
aliases.rs |
AsyncResult, SharedHashMap, PositionMap, etc. |
~80 |
service.rs |
ServiceId, ServiceStatus, ConfigVersion |
~120 |
market_data.rs |
MarketDataEvent, QuoteEvent, TradeEvent, BarEvent, orderbook types |
~1,500 |
order.rs |
OrderType, OrderSide, OrderStatus, OrderEvent, BrokerType |
~400 |
connection.rs |
ConnectionInfo, ConnectionEvent, ConnectionStatus, ResourceLimits |
~200 |
error.rs |
CommonTypeError + trait impls |
~200 |
currency.rs |
Currency enum + Display/Default |
~100 |
mod.rs |
pub use re-exports for backward compatibility |
~30 |
Backward Compatibility
types/mod.rs will pub use everything, so existing use common::types::*
and use common::types::QuoteEvent imports continue working unchanged.
Risk
Low. Pure structural refactor with re-exports. No API changes.
Area 3: trading_engine/compliance/ Audit (~11,000 lines saved)
Problem
The compliance directory contains 16,068 LOC across 9 files:
iso27001_compliance.rs(3,217) — ISMS scope, organization info, facilitiescompliance_reporting.rs(2,406) — Report generationsox_compliance.rs(2,262) — Sarbanes-Oxley controlsaudit_trails.rs(1,847) — Audit trail storagetransaction_reporting.rs(1,714) — MiFID/Dodd-Frank TCAautomated_reporting.rs(1,628) — Automated report schedulingbest_execution.rs(1,101) — 29 struct/enums for best execution analysismod.rs(1,107) — Module orchestrationregulatory_api.rs(786) — API endpoints
Much of this appears to be enterprise scaffolding (ISO27001 OrganizationInfo,
Location, FacilityType, ContactInfo, ISMSScope) that is unlikely to be
called from production code paths in an HFT futures trading system.
Design
- Audit usage: grep for each public type/function to find callers outside tests
- Categorize: active (called in production) vs dormant (tests-only or zero callers)
- Delete dormant code: Remove types and functions with zero production callers
- Consolidate active code: Merge remaining functions into fewer, smaller files
- Keep essentials: audit trail logging, basic compliance checks that risk/ references
Risk
Medium. Need to verify each type's usage carefully. Some compliance types may be
referenced transitively through trait bounds. The #[allow(unused_variables)]
at the top of best_execution.rs is a red flag suggesting it may be entirely unused.
Area 4: trading_service/enhanced_ml.rs Cleanup (~1,500 lines saved)
Problem
At 3,027 LOC, this file implements the ML gRPC service with:
- Runtime model tracking (
RuntimeModelInfo) duplicatingml::ModelMetadata - Model loading code that should delegate to
EnsembleModelAdapter - Known
Status::unavailablestubs for retrain and feature_importance - PPO/TFT inference code with hardcoded fallbacks
Design
- Audit for legacy patterns matching deleted
MLFeatureExtractor/SimpleDQNAdapter - Consolidate
RuntimeModelInfowith existingml::ModelMetadatatypes - Simplify model loading to use
ml::ensemble::build_production_strategy() - Remove dead code paths that duplicate what
EnsembleModelAdapternow handles - Keep the gRPC service interface — just slim the implementation
Risk
Medium. This file implements active gRPC endpoints. Changes must preserve the proto contract. Focus on internal simplification, not API changes.
Execution Order
- DRY violations (creates canonical shared types)
- types.rs split (structural, no deps on other areas)
- compliance/ audit (largest deletion target)
- enhanced_ml.rs cleanup (builds on Area 1 patterns)
Verification
After each area:
SQLX_OFFLINE=true cargo check --workspace— zero errorsSQLX_OFFLINE=true cargo clippy -p <affected_crates> --lib -- -D warningsSQLX_OFFLINE=true cargo test -p <affected_crates> --libgit diff --stat main— track cumulative line savings
Final target: -13,000+ net lines deleted while preserving all production functionality.