chore: checkpoint before NT8 execution wiring fix

Current state: Strategy builds and loads correctly, passes 240+ tests,
backtest (Strategy Analyzer) works but zero trades execute on live/SIM.

Root cause identified: NT8OrderAdapter.ExecuteInNT8() is a stub - it logs
to an internal list but never calls EnterLong/EnterShort/SetStopLoss/
SetProfitTarget. Fix is ready in TASK_01_WIRE_NT8_EXECUTION.md.

Task files added (ready for Kilocode):
- TASK_01_WIRE_NT8_EXECUTION.md (CRITICAL - INT8ExecutionBridge + wiring)
- TASK_02_EMERGENCY_KILL_SWITCH.md (CRITICAL - kill switch + verbose logging)
- TASK_03_WIRE_CIRCUIT_BREAKER.md (HIGH - wire ExecutionCircuitBreaker)

Build Status: All 240+ tests passing, zero errors
Next: Run Kilocode against TASK_01, TASK_02, TASK_03 in order
This commit is contained in:
2026-03-10 15:49:59 -04:00
parent a87152effb
commit a283ef4673
45 changed files with 4256 additions and 129 deletions

View File

@@ -1,8 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using Microsoft.Extensions.Logging;
[assembly: InternalsVisibleTo("NT8.Core.Tests")]
[assembly: InternalsVisibleTo("NT8.Integration.Tests")]
namespace NT8.Core.Execution
{
/// <summary>
@@ -11,6 +15,7 @@ namespace NT8.Core.Execution
public class ExecutionCircuitBreaker
{
private readonly ILogger _logger;
private readonly NT8.Core.Logging.ILogger _sdkLogger;
private readonly object _lock = new object();
private CircuitBreakerStatus _status;
@@ -21,24 +26,49 @@ namespace NT8.Core.Execution
private readonly int _failureThreshold;
private readonly TimeSpan _retryTimeout;
// Track execution times for latency monitoring
private readonly Queue<TimeSpan> _executionTimes;
private readonly int _latencyWindowSize;
// Track order rejections
private readonly Queue<DateTime> _rejectionTimes;
private readonly int _rejectionWindowSize;
// Log helpers — route through whichever logger is available
private void LogDebug(string message) { if (_logger != null) _logger.LogDebug(message); else if (_sdkLogger != null) _sdkLogger.LogDebug(message); }
private void LogInfo(string message) { if (_logger != null) _logger.LogInformation(message); else if (_sdkLogger != null) _sdkLogger.LogInformation(message); }
private void LogWarn(string message) { if (_logger != null) _logger.LogWarning(message); else if (_sdkLogger != null) _sdkLogger.LogWarning(message); }
private void LogErr(string message) { if (_logger != null) _logger.LogError(message); else if (_sdkLogger != null) _sdkLogger.LogError(message); }
/// <summary>
/// Constructor for ExecutionCircuitBreaker
/// Constructor accepting NT8.Core.Logging.ILogger.
/// Use this overload from NinjaScript (.cs) files — no Microsoft.Extensions.Logging reference required.
/// </summary>
/// <param name="logger">Logger instance</param>
/// <param name="failureThreshold">Number of failures to trigger circuit breaker</param>
/// <param name="timeout">How long to stay open before half-open</param>
/// <param name="retryTimeout">Time to wait between retries</param>
/// <param name="latencyWindowSize">Size of latency tracking window</param>
/// <param name="rejectionWindowSize">Size of rejection tracking window</param>
public ExecutionCircuitBreaker(
NT8.Core.Logging.ILogger sdkLogger,
int failureThreshold = 3,
TimeSpan? timeout = null,
TimeSpan? retryTimeout = null,
int latencyWindowSize = 100,
int rejectionWindowSize = 10)
{
_sdkLogger = sdkLogger;
_logger = null;
_status = CircuitBreakerStatus.Closed;
_failureCount = 0;
_lastFailureTime = DateTime.MinValue;
_timeout = timeout ?? TimeSpan.FromSeconds(30);
_retryTimeout = retryTimeout ?? TimeSpan.FromSeconds(5);
_failureThreshold = failureThreshold;
_latencyWindowSize = latencyWindowSize;
_rejectionWindowSize = rejectionWindowSize;
_executionTimes = new Queue<TimeSpan>();
_rejectionTimes = new Queue<DateTime>();
}
/// <summary>
/// Constructor accepting Microsoft.Extensions.Logging.ILogger.
/// Use this overload from DLL projects and unit tests.
/// </summary>
internal ExecutionCircuitBreaker(
ILogger<ExecutionCircuitBreaker> logger,
int failureThreshold = 3,
TimeSpan? timeout = null,
@@ -50,6 +80,7 @@ namespace NT8.Core.Execution
throw new ArgumentNullException("logger");
_logger = logger;
_sdkLogger = null;
_status = CircuitBreakerStatus.Closed;
_failureCount = 0;
_lastFailureTime = DateTime.MinValue;
@@ -58,15 +89,11 @@ namespace NT8.Core.Execution
_failureThreshold = failureThreshold;
_latencyWindowSize = latencyWindowSize;
_rejectionWindowSize = rejectionWindowSize;
_executionTimes = new Queue<TimeSpan>();
_rejectionTimes = new Queue<DateTime>();
}
/// <summary>
/// Records execution time for monitoring
/// </summary>
/// <param name="latency">Execution latency</param>
/// <summary>Records execution time for latency monitoring.</summary>
public void RecordExecutionTime(TimeSpan latency)
{
try
@@ -74,31 +101,21 @@ namespace NT8.Core.Execution
lock (_lock)
{
_executionTimes.Enqueue(latency);
// Keep only the last N measurements
while (_executionTimes.Count > _latencyWindowSize)
{
_executionTimes.Dequeue();
}
// Check if we have excessive latency
if (_status == CircuitBreakerStatus.Closed && HasExcessiveLatency())
{
TripCircuitBreaker("Excessive execution latency detected");
}
}
}
catch (Exception ex)
{
_logger.LogError("Failed to record execution time: {Message}", ex.Message);
LogErr(string.Format("Failed to record execution time: {0}", ex.Message));
throw;
}
}
/// <summary>
/// Records order rejection for monitoring
/// </summary>
/// <param name="reason">Reason for rejection</param>
/// <summary>Records an order rejection.</summary>
public void RecordOrderRejection(string reason)
{
if (string.IsNullOrEmpty(reason))
@@ -109,31 +126,21 @@ namespace NT8.Core.Execution
lock (_lock)
{
_rejectionTimes.Enqueue(DateTime.UtcNow);
// Keep only the last N rejections
while (_rejectionTimes.Count > _rejectionWindowSize)
{
_rejectionTimes.Dequeue();
}
// Check if we have excessive rejections
if (_status == CircuitBreakerStatus.Closed && HasExcessiveRejections())
{
TripCircuitBreaker(String.Format("Excessive order rejections: {0}", reason));
}
TripCircuitBreaker(string.Format("Excessive order rejections: {0}", reason));
}
}
catch (Exception ex)
{
_logger.LogError("Failed to record order rejection: {Message}", ex.Message);
LogErr(string.Format("Failed to record order rejection: {0}", ex.Message));
throw;
}
}
/// <summary>
/// Determines if an order should be allowed based on circuit breaker state
/// </summary>
/// <returns>True if order should be allowed, false otherwise</returns>
/// <summary>Returns true if an order should be allowed through.</summary>
public bool ShouldAllowOrder()
{
try
@@ -143,26 +150,20 @@ namespace NT8.Core.Execution
switch (_status)
{
case CircuitBreakerStatus.Closed:
// Normal operation
return true;
case CircuitBreakerStatus.Open:
// Check if we should transition to half-open
if (DateTime.UtcNow >= _nextRetryTime)
{
_status = CircuitBreakerStatus.HalfOpen;
_logger.LogWarning("Circuit breaker transitioning to Half-Open state");
return true; // Allow one test order
}
else
{
_logger.LogDebug("Circuit breaker is Open - blocking order");
return false; // Block orders
LogWarn("Circuit breaker transitioning to Half-Open state");
return true;
}
LogDebug("Circuit breaker is Open - blocking order");
return false;
case CircuitBreakerStatus.HalfOpen:
// In half-open, allow limited operations to test if system recovered
_logger.LogDebug("Circuit breaker is Half-Open - allowing test order");
LogDebug("Circuit breaker is Half-Open - allowing test order");
return true;
default:
@@ -172,15 +173,12 @@ namespace NT8.Core.Execution
}
catch (Exception ex)
{
_logger.LogError("Failed to check if order should be allowed: {Message}", ex.Message);
LogErr(string.Format("Failed to check ShouldAllowOrder: {0}", ex.Message));
throw;
}
}
/// <summary>
/// Gets the current state of the circuit breaker
/// </summary>
/// <returns>Current circuit breaker state</returns>
/// <summary>Returns the current circuit breaker state.</summary>
public CircuitBreakerState GetState()
{
try
@@ -191,20 +189,17 @@ namespace NT8.Core.Execution
_status != CircuitBreakerStatus.Closed,
_status,
GetStatusReason(),
_failureCount
);
_failureCount);
}
}
catch (Exception ex)
{
_logger.LogError("Failed to get circuit breaker state: {Message}", ex.Message);
LogErr(string.Format("Failed to get state: {0}", ex.Message));
throw;
}
}
/// <summary>
/// Resets the circuit breaker to closed state
/// </summary>
/// <summary>Resets the circuit breaker to Closed state.</summary>
public void Reset()
{
try
@@ -214,20 +209,17 @@ namespace NT8.Core.Execution
_status = CircuitBreakerStatus.Closed;
_failureCount = 0;
_lastFailureTime = DateTime.MinValue;
_logger.LogInformation("Circuit breaker reset to Closed state");
LogInfo("Circuit breaker reset to Closed state");
}
}
catch (Exception ex)
{
_logger.LogError("Failed to reset circuit breaker: {Message}", ex.Message);
LogErr(string.Format("Failed to reset circuit breaker: {0}", ex.Message));
throw;
}
}
/// <summary>
/// Called when an operation succeeds while in Half-Open state
/// </summary>
/// <summary>Call after a successful order submission.</summary>
public void OnSuccess()
{
try
@@ -237,20 +229,18 @@ namespace NT8.Core.Execution
if (_status == CircuitBreakerStatus.HalfOpen)
{
Reset();
_logger.LogInformation("Circuit breaker reset after successful test operation");
LogInfo("Circuit breaker reset after successful test operation");
}
}
}
catch (Exception ex)
{
_logger.LogError("Failed to handle success in Half-Open state: {Message}", ex.Message);
LogErr(string.Format("Failed to handle OnSuccess: {0}", ex.Message));
throw;
}
}
/// <summary>
/// Called when an operation fails
/// </summary>
/// <summary>Call after a failed order submission.</summary>
public void OnFailure()
{
try
@@ -260,7 +250,6 @@ namespace NT8.Core.Execution
_failureCount++;
_lastFailureTime = DateTime.UtcNow;
// If we're in half-open and fail, go back to open
if (_status == CircuitBreakerStatus.HalfOpen ||
(_status == CircuitBreakerStatus.Closed && _failureCount >= _failureThreshold))
{
@@ -270,61 +259,35 @@ namespace NT8.Core.Execution
}
catch (Exception ex)
{
_logger.LogError("Failed to handle failure: {Message}", ex.Message);
LogErr(string.Format("Failed to handle OnFailure: {0}", ex.Message));
throw;
}
}
/// <summary>
/// Trips the circuit breaker to open state
/// </summary>
/// <param name="reason">Reason for tripping</param>
private void TripCircuitBreaker(string reason)
{
_status = CircuitBreakerStatus.Open;
_nextRetryTime = DateTime.UtcNow.Add(_timeout);
_logger.LogWarning("Circuit breaker TRIPPED: {Reason}. Will retry at {Time}",
reason, _nextRetryTime);
LogWarn(string.Format("Circuit breaker TRIPPED: {0}. Will retry at {1}", reason, _nextRetryTime));
}
/// <summary>
/// Checks if we have excessive execution latency
/// </summary>
/// <returns>True if latency is excessive</returns>
private bool HasExcessiveLatency()
{
if (_executionTimes.Count < 3) // Need minimum samples
if (_executionTimes.Count < 3)
return false;
// Calculate average latency
var avgLatency = TimeSpan.FromMilliseconds(_executionTimes.Average(ts => ts.TotalMilliseconds));
// If average latency is more than 5 seconds, consider it excessive
return avgLatency.TotalSeconds > 5.0;
}
/// <summary>
/// Checks if we have excessive order rejections
/// </summary>
/// <returns>True if rejections are excessive</returns>
private bool HasExcessiveRejections()
{
if (_rejectionTimes.Count < _rejectionWindowSize)
return false;
// If all recent orders were rejected (100% rejection rate in window)
var recentWindow = TimeSpan.FromMinutes(1); // Check last minute
var recentWindow = TimeSpan.FromMinutes(1);
var recentRejections = _rejectionTimes.Count(dt => DateTime.UtcNow - dt <= recentWindow);
// If we have maximum possible rejections in the window, it's excessive
return recentRejections >= _rejectionWindowSize;
}
/// <summary>
/// Gets the reason for current status
/// </summary>
/// <returns>Reason string</returns>
private string GetStatusReason()
{
switch (_status)
@@ -332,8 +295,7 @@ namespace NT8.Core.Execution
case CircuitBreakerStatus.Closed:
return "Normal operation";
case CircuitBreakerStatus.Open:
return String.Format("Tripped due to failures. Failures: {0}, Last: {1}",
_failureCount, _lastFailureTime);
return string.Format("Tripped due to failures. Count: {0}, Last: {1}", _failureCount, _lastFailureTime);
case CircuitBreakerStatus.HalfOpen:
return "Testing recovery after timeout";
default:
@@ -341,10 +303,7 @@ namespace NT8.Core.Execution
}
}
/// <summary>
/// Gets average execution time for monitoring
/// </summary>
/// <returns>Average execution time</returns>
/// <summary>Returns average execution latency.</summary>
public TimeSpan GetAverageExecutionTime()
{
try
@@ -353,21 +312,17 @@ namespace NT8.Core.Execution
{
if (_executionTimes.Count == 0)
return TimeSpan.Zero;
return TimeSpan.FromMilliseconds(_executionTimes.Average(ts => ts.TotalMilliseconds));
}
}
catch (Exception ex)
{
_logger.LogError("Failed to get average execution time: {Message}", ex.Message);
LogErr(string.Format("Failed to get average execution time: {0}", ex.Message));
throw;
}
}
/// <summary>
/// Gets rejection rate for monitoring
/// </summary>
/// <returns>Rejection rate as percentage</returns>
/// <summary>Returns rejection rate as a percentage.</summary>
public double GetRejectionRate()
{
try
@@ -376,19 +331,14 @@ namespace NT8.Core.Execution
{
if (_rejectionTimes.Count == 0)
return 0.0;
// Calculate rejections in last minute
var oneMinuteAgo = DateTime.UtcNow.AddMinutes(-1);
var recentRejections = _rejectionTimes.Count(dt => dt >= oneMinuteAgo);
// This is a simplified calculation - in practice you'd need to track
// total attempts to calculate accurate rate
return (double)recentRejections / _rejectionWindowSize * 100.0;
}
}
catch (Exception ex)
{
_logger.LogError("Failed to get rejection rate: {Message}", ex.Message);
LogErr(string.Format("Failed to get rejection rate: {0}", ex.Message));
throw;
}
}