Skip to main content

Devlog: 2026-03-17 — BestEx Engine Implementation

What Happened

Implemented the full BestEx (Best Execution) engine in the PowerSeller.SaaS.Modules.BestEx project, using parallel AI developer agents for independent workstreams after establishing the entity foundation.

Implementation Strategy

  1. Sequential foundation: 19 entity classes + EF Core configuration with composite keys
  2. Parallel agents (3 concurrent):
    • Agent A: Profile management service + 13 CRUD endpoints
    • Agent B: 24-step analysis pipeline + pricing calculator + feature adjustments + ranking
    • Agent C: Analysis run orchestration + results/errors/loans endpoints
  3. Sequential integration: Wire up BestExModule.cs, fix build errors, verify

What Was Built

Domain Entities (19 files)

EntityTablePurpose
BestExProfilermcat_bx_profile_namesAnalysis profile definition
ProfileStrategyrmcat_bx_profile_strategiesBuy-up/buy-down/excess strategies
ProfileParametersrmcat_bx_setup_parametersProfile configuration flags and filters
ProfileInstrumentMappingrmcat_bx_setup_instr_invInstrument/investor associations
ProfileLockWindowrmcat_bx_setup_lock_windowsLock window configuration
ProfileStatusrmcat_bx_setup_statusesEligible loan statuses
AnalysisResultrmcat_bestex_analysis67-column analysis output
AnalysisErrorrmcat_bestex_analysis_errorPer-loan error tracking
LoanloanLoan pipeline (key columns)
Instrumentpscat_instrumentsInstrument configuration
TodaysPricermcat_todays_pricesCurrent validated prices
TodaysParRatermcat_todays_par_ratesComputed par rates
PriceAdjustmentrmcat_price_adjustmentsAdjustment rules with SQL syntax
PriceAdjustmentValuermcat_price_adjustment_valuesAdjustment values per investor
PriceAdjustmentGrouprmcat_price_adjustment_groupsAdjustment group definitions
ProfitMarginrmcat_profit_marginProfit margin configuration
InvestorDeliveryWindowpscat_rm_inv_delivery_winDelivery window configuration
Zonepscat_rm_zonesInvestor zones
ZoneStatepscat_rm_zone_statesZone-to-state mappings

Services (6 files)

ServiceLinesPurpose
ProfileService~200Full CRUD for profiles and configuration
AnalysisPipelineService~47024-step analysis engine
PricingCalculator~130Pure math: par rate interpolation, DPCs, total price formula
FeatureAdjustmentService~170Adjustment rule evaluation with simplified syntax parser
RankingService~60SQL RANK() window function for scenario ranking
AnalysisRunService~170Orchestration, results pagination, summaries, history

Endpoints (3 files)

Endpoint GroupCountPath
Profile endpoints13/api/bestex/profiles/*
Analysis endpoints5/api/bestex/analysis/*
Loan endpoints3/api/bestex/loans/*

Infrastructure

  • TenantDbContext updated with module-based entity registration (RegisterEntityAssembly)
  • BestExEntityConfiguration defines all composite keys via IEntityTypeConfiguration
  • BestExModule.cs wires up all DI registrations and endpoint mappings

Pipeline Implementation Status

PhaseStepsStatus
1: Setup1-4Fully implemented
2: Pricing Windows5-8Implemented (pool check simplified)
3: Rate Optimization9-10Servicing defaults to "sell" (TODO)
4: Value Components11-13Price lookup + interpolation done; guarantee_fee from instrument config
5: Income and Cost14-17Simplified to 0 (TODOs for warehouse rate, finance cost, early delivery)
6: Final Calculations18-21Total price + profit calculated; cost basis = 100 (TODO)
7: Ranking and Output22-24Bulk insert + SQL RANK() done; report/archive are no-ops

12 TODO comments mark simplified steps referencing spec rule numbers.

Build Verification

  • Build: 0 warnings, 0 errors across all 6 projects
  • Tests: 1 passed, 0 failed
  • Solution builds cleanly in Docker container

Issues Encountered

  • AnalysisRunResult record defined in two files by parallel agents -- removed duplicate
  • Entity property naming mismatch (PriceAdjustmentGroupName vs PriceAdjustmentGroup) between entity classes and EF Core configuration -- fixed
  • Nullable value type pattern match needed for TreatWarningsAsErrors compliance
  • Api.Tests project missing using Xunit;

Technical Debt

  • 12 pipeline steps are simplified (income/cost components set to 0, cost basis hardcoded to 100, no archive)
  • Feature adjustment syntax evaluator handles only simple single-column comparisons -- no AND/OR/NOT/IN/BETWEEN
  • No integration tests against the local SQL Server yet
  • The AnalysisError entity is keyless (HasNoKey()) -- works for read/insert but can't be tracked by EF Core change tracker

What's Next

  1. Start Docker containers with --profile dev and verify API responds with all 21+ endpoints in Swagger
  2. Add PricingCalculator unit tests (par rate formula, total price formula)
  3. Fill in simplified pipeline steps as individual work items
  4. Connect to local SQL Server and test end-to-end with synthetic data