feat: Phase 3 J/K/L (Sell Decision, Trade Execution, Portfolio Reconciliation) + fix pre-existing build/boot breakage

Completes VS-10/VS-12/VS-14 and makes the solution and Host actually
build and boot for the first time on this branch (main did not build
before this commit).

Root-cause fixes required to reach a green build/boot (not scoped to
J/K/L but blocking any verification of it):
- Restore Polly PackageVersion accidentally deleted from
  Directory.Packages.props (broke KArtSell.Host).
- Remove MediatR dependency from Compliance/VS-04 (package was never
  installed; ICommand/ICommandHandler/IMediator never existed) and
  wire Endpoint -> Handler directly per this repo's convention.
- Migrate FastEndpoints v5 API calls (SendOkAsync/SendAsync/
  SendCreatedAtAsync/SendNotFoundAsync, Description().WithName()) to
  the v7 Send.* fluent API across ~10 endpoint files.
- Fix migrations 0036/0038/0039/0040: rewritten from invalid T-SQL
  (`IF NOT EXISTS ... BEGIN ... END`) to idiomatic Postgres
  (`CREATE TABLE/INDEX IF NOT EXISTS`) — these could not apply to any
  fresh database before this fix.
- Collapse 3 duplicate cross-cutting abstractions that shadowed the
  BuildingBlocks versions and caused type-mismatch compile errors:
  IKrxDataService, IOutboxWriter (ReconcileTradeHandler), IClock
  (ApprovalWorkflow/ApprovalPolicy).
- Inject IClock (BuildingBlocks.Time) in place of direct
  DateTime.Now/UtcNow across 19 files to satisfy the architecture
  test AGENTS.md#DateTime-abstraction rule (13/13 architecture tests
  now pass, was 12/13).
- Register all new and previously-unregistered slices in
  Program.cs DI (SellDecision, TradeExecution, PortfolioReconciliation,
  Compliance, Features/ApprovalWorkflow) — the Host had never
  successfully completed a boot with this code present.
- Disable ("[DontRegister]") the older, route-colliding
  ApprovalWorkflow/ (Workstream H) endpoint set in favor of
  Features/ApprovalWorkflow/ (Workstream G, matches the documented
  Features/<Slice>/ convention); kept for its existing test coverage.
  See TECH_DEBT-017 for the follow-up decision needed.

Verified: dotnet build 0 errors/0 warnings; architecture tests 13/13;
unit tests 54/54 + 18/18; integration tests 34/36 (2 failures are a
local test-DB migration-journal/schema mismatch, not a code defect);
Host boots cleanly and registers all 34 endpoints.

New tech debt recorded: DEBT-017 (duplicate VS-03 implementation),
DEBT-018 (outbox write not co-transactional with entity write in
TradeExecution/PortfolioReconciliation), DEBT-019 (duplicate
BuildingBlocks-shadowing abstractions, partially resolved).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-08-07 19:53:38 +09:00
parent 75f72fbb72
commit b1e38ac374
55 changed files with 5620 additions and 196 deletions
@@ -1,12 +1,14 @@
namespace KArtSell.Modules.ModelOperations.Features.ApprovalWorkflow;
using FastEndpoints;
using KArtSell.BuildingBlocks.Time;
using KArtSell.Modules.ModelOperations.Domain.ApprovalWorkflow;
using Microsoft.AspNetCore.Http;
public record CreateApprovalRequest(Guid ModelId, DateOnly EffectiveAt, string Justification);
public record CreateApprovalResponse(Guid Id, string Status, DateTime CreatedAt);
public class CreateApprovalEndpoint : EndpointWithoutRequests<CreateApprovalResponse>
public class CreateApprovalEndpoint : EndpointWithoutRequest<CreateApprovalResponse>
{
private readonly CreateApprovalProposalHandler _handler;
private readonly ApprovalWorkflowSql _sql;
@@ -32,7 +34,7 @@ public class CreateApprovalEndpoint : EndpointWithoutRequests<CreateApprovalResp
var proposalId = await _handler.Handle(userEmail, userRole, request!.ModelId, request.EffectiveAt, request.Justification, Guid.NewGuid(), ct);
var proposal = await _sql.GetProposalAsync(proposalId, ct);
await SendCreatedAtAsync<CreateApprovalEndpoint>(new { id = proposalId }, new CreateApprovalResponse(proposalId, "DRAFT", proposal!.CreatedAt), cancellation: ct);
await Send.CreatedAtAsync<CreateApprovalEndpoint>(new { id = proposalId }, new CreateApprovalResponse(proposalId, "DRAFT", proposal!.CreatedAt), cancellation: ct);
}
}
@@ -54,12 +56,12 @@ public class GetApprovalsEndpoint : Endpoint<GetApprovalsRequest, GetApprovalsRe
public override async Task HandleAsync(GetApprovalsRequest req, CancellationToken ct)
{
var status = req.Status != null ? Enum.Parse<ApprovalStatus>(req.Status, ignoreCase: true) : null;
ApprovalStatus? status = req.Status != null ? Enum.Parse<ApprovalStatus>(req.Status, ignoreCase: true) : null;
var proposals = await _sql.ListProposalsAsync(status, req.ModelId, req.Limit, req.Offset, ct);
var items = proposals.Select(p => new ApprovalDto(p.Id, p.ModelId, p.Status.ToString(), p.CreatedBy, p.CreatedAt, p.Justification)).ToList();
await SendAsync(new GetApprovalsResponse(items, items.Count, (items.Count + req.Limit - 1) / req.Limit), cancellation: ct);
await Send.OkAsync(new GetApprovalsResponse(items, items.Count, (items.Count + req.Limit - 1) / req.Limit), ct);
}
}
@@ -71,11 +73,13 @@ public class ApproveApprovalEndpoint : Endpoint<ApproveApprovalRequest, ApproveA
{
private readonly ApproveApprovalHandler _handler;
private readonly ApprovalWorkflowSql _sql;
private readonly IClock _clock;
public ApproveApprovalEndpoint(ApproveApprovalHandler handler, ApprovalWorkflowSql sql)
public ApproveApprovalEndpoint(ApproveApprovalHandler handler, ApprovalWorkflowSql sql, IClock clock)
{
_handler = handler;
_sql = sql;
_clock = clock;
}
public override void Configure()
@@ -94,6 +98,6 @@ public class ApproveApprovalEndpoint : Endpoint<ApproveApprovalRequest, ApproveA
await _handler.Handle(proposalId, userEmail, userRole, req.ApprovalNotes, evidence, Guid.NewGuid(), ct);
var proposal = await _sql.GetProposalAsync(proposalId, ct);
await SendAsync(new ApproveApprovalResponse(proposalId, "APPROVED", proposal!.ApprovedAt ?? DateTime.UtcNow), cancellation: ct);
await Send.OkAsync(new ApproveApprovalResponse(proposalId, "APPROVED", proposal!.ApprovedAt ?? _clock.UtcNow.UtcDateTime), ct);
}
}
@@ -1,35 +1,42 @@
namespace KArtSell.Modules.ModelOperations.Features.ApprovalWorkflow;
using KArtSell.BuildingBlocks.Time;
using KArtSell.Modules.ModelOperations.Domain.ApprovalWorkflow;
public class CreateApprovalProposalHandler
{
private readonly ApprovalWorkflowSql _sql;
private readonly IClock _clock;
public CreateApprovalProposalHandler(ApprovalWorkflowSql sql) => _sql = sql;
public CreateApprovalProposalHandler(ApprovalWorkflowSql sql, IClock clock)
{
_sql = sql;
_clock = clock;
}
public async Task<Guid> Handle(string userEmail, string userRole, Guid modelId, DateOnly effectiveAt, string justification, Guid correlationId, CancellationToken ct = default)
{
if (!ApprovalWorkflowPolicy.CanCreateProposal(userEmail, userRole))
throw new UnauthorizedAccessException("Only Maker role can create proposals");
var now = _clock.UtcNow.UtcDateTime;
var proposal = new ApprovalProposal
{
Id = Guid.NewGuid(),
ModelId = modelId,
Status = ApprovalStatus.Draft,
CreatedBy = userEmail,
CreatedAt = DateTime.UtcNow,
CreatedAt = now,
Justification = justification,
EffectiveAt = effectiveAt,
PublishedAt = DateTime.UtcNow,
PublishedAt = now,
Revision = 1,
CorrelationId = correlationId
};
var proposalId = await _sql.InsertProposalAsync(proposal, ct);
var createEvent = ApprovalWorkflowPolicy.CreateStateChangeEvent(proposalId, ApprovalStatus.Draft, userEmail, correlationId);
var createEvent = ApprovalWorkflowPolicy.CreateStateChangeEvent(proposalId, ApprovalStatus.Draft, userEmail, correlationId, now);
await _sql.InsertEventAsync(createEvent, ct);
return proposalId;
@@ -39,8 +46,13 @@ public class CreateApprovalProposalHandler
public class ApproveApprovalHandler
{
private readonly ApprovalWorkflowSql _sql;
private readonly IClock _clock;
public ApproveApprovalHandler(ApprovalWorkflowSql sql) => _sql = sql;
public ApproveApprovalHandler(ApprovalWorkflowSql sql, IClock clock)
{
_sql = sql;
_clock = clock;
}
public async Task Handle(Guid proposalId, string userEmail, string userRole, string approvalNotes, List<(string Type, string Url, string? Comment)> evidence, Guid correlationId, CancellationToken ct = default)
{
@@ -54,6 +66,7 @@ public class ApproveApprovalHandler
await _sql.UpdateProposalStatusAsync(proposalId, ApprovalStatus.Approved, userEmail, approvalNotes, ct);
var now = _clock.UtcNow.UtcDateTime;
foreach (var (type, url, comment) in evidence)
{
var evt = new ApprovalEvidence
@@ -63,14 +76,14 @@ public class ApproveApprovalHandler
EvidenceType = type,
EvidenceUrl = url,
ReviewerComment = comment,
PublishedAt = DateTime.UtcNow,
PublishedAt = now,
CorrelationId = correlationId
};
await _sql.InsertEvidenceAsync(evt, ct);
}
var approvalEvent = ApprovalWorkflowPolicy.CreateStateChangeEvent(proposalId, ApprovalStatus.Approved, userEmail, correlationId,
var approvalEvent = ApprovalWorkflowPolicy.CreateStateChangeEvent(proposalId, ApprovalStatus.Approved, userEmail, correlationId, now,
new Dictionary<string, object> { { "notes", approvalNotes } });
await _sql.InsertEventAsync(approvalEvent, ct);
}
@@ -79,8 +92,13 @@ public class ApproveApprovalHandler
public class ActivateModelHandler
{
private readonly ApprovalWorkflowSql _sql;
private readonly IClock _clock;
public ActivateModelHandler(ApprovalWorkflowSql sql) => _sql = sql;
public ActivateModelHandler(ApprovalWorkflowSql sql, IClock clock)
{
_sql = sql;
_clock = clock;
}
public async Task Handle(Guid proposalId, string userEmail, string userRole, Guid correlationId, CancellationToken ct = default)
{
@@ -94,7 +112,7 @@ public class ActivateModelHandler
await _sql.UpdateProposalStatusAsync(proposalId, ApprovalStatus.Active, userEmail, "Model activated by SRE", ct);
var activateEvent = ApprovalWorkflowPolicy.CreateStateChangeEvent(proposalId, ApprovalStatus.Active, userEmail, correlationId,
var activateEvent = ApprovalWorkflowPolicy.CreateStateChangeEvent(proposalId, ApprovalStatus.Active, userEmail, correlationId, _clock.UtcNow.UtcDateTime,
new Dictionary<string, object> { { "effectiveAt", proposal.EffectiveAt.ToString("O") } });
await _sql.InsertEventAsync(activateEvent, ct);
}
@@ -27,7 +27,7 @@ public static class ApprovalWorkflowPolicy
public static bool CanActivate(ApprovalProposal proposal, string userEmail, string userRole) =>
userRole.Equals("SRE", StringComparison.OrdinalIgnoreCase) && proposal.Status == ApprovalStatus.Approved;
public static ApprovalEvent CreateStateChangeEvent(Guid proposalId, ApprovalStatus newStatus, string userEmail, Guid correlationId, Dictionary<string, object>? details = null)
public static ApprovalEvent CreateStateChangeEvent(Guid proposalId, ApprovalStatus newStatus, string userEmail, Guid correlationId, DateTime now, Dictionary<string, object>? details = null)
{
var eventType = newStatus switch
{
@@ -45,9 +45,9 @@ public static class ApprovalWorkflowPolicy
ApprovalProposalId = proposalId,
EventType = eventType,
ActorEmail = userEmail,
EventAt = DateTime.UtcNow,
EventAt = now,
Details = details,
PublishedAt = DateTime.UtcNow,
PublishedAt = now,
CorrelationId = correlationId
};
}