From 49bcd0b43c6cd8f3d7d59bea22d06cb9b7669ccf Mon Sep 17 00:00:00 2001 From: mo Date: Wed, 26 Aug 2026 01:03:12 -0400 Subject: [PATCH] [GAP-025]: runner order lifecycle logging complete - src/NT8.Adapters/Strategies/NT8StrategyBase.cs: add ORDER lifecycle FileLog and FILL signal name - tests/NT8.Core.Tests/Adapters/NT8StrategyBaseOrderLifecycleTests.cs: add source-text regression coverage - SPRINT_BOARD.md: GAP-025 prompted -> implemented/deployed - docs/00-governance/current_status.md: record GAP-025 implementation - docs/00-governance/decisions.md: record DEC-20260826-04 Build: clean | Tests: 572 passing | Dual-deployed: yes, backup 20260826_010153 --- SPRINT_BOARD.md | 6 +- docs/00-governance/current_status.md | 10 +- docs/00-governance/decisions.md | 11 +++ .../Strategies/NT8StrategyBase.cs | 15 ++- .../NT8StrategyBaseOrderLifecycleTests.cs | 98 +++++++++++++++++++ 5 files changed, 134 insertions(+), 6 deletions(-) create mode 100644 tests/NT8.Core.Tests/Adapters/NT8StrategyBaseOrderLifecycleTests.cs diff --git a/SPRINT_BOARD.md b/SPRINT_BOARD.md index 1613276..5c8aef0 100644 --- a/SPRINT_BOARD.md +++ b/SPRINT_BOARD.md @@ -1,5 +1,5 @@ # NT8 SDK — Sprint Board -**Last Updated:** 2026-08-26 (GAP-025/GAP-030 prompted; GAP-062 deployed by operator request — see DEC-20260826-02) +**Last Updated:** 2026-08-26 (GAP-025 implemented/deployed; GAP-030 prompted next) **Truth source:** compiler output, test output, deployed-file comparisons, and runtime artifacts override board claims. ## Current gate state @@ -147,10 +147,10 @@ The dirty working tree — several modified/untracked files including live strat ## 📋 QUEUE — no deploy to the gate machine during a run -**[GAP-025] Runner/scaler order lifecycle invisible in the persistent session log** | Sprint 2.7 | 🟡 PROMPTED +**[GAP-025] Runner/scaler order lifecycle invisible in the persistent session log** | Sprint 2.7 | ✅ IMPLEMENTED — deployed 2026-08-26 - Reading `OnOrderUpdate`/`OnExecutionUpdate` (`NT8StrategyBase.cs` ~1270-1345) on 2026-08-26 found the actual gap behind the old "runner no-fill anomaly" framing: `OnOrderUpdate` never writes to `FileLog` for any order state except `Rejected` (everything else — submitted, accepted, working, cancelled — is Print-only, invisible outside NT8's live Output window), and the `FILL` line in `OnExecutionUpdate` doesn't include the order/signal name, so scaler vs. runner fills can't be told apart from the file log at all. - Scoped diagnostic-only: adds an `ORDER {signal} State=... Filled=.../... AvgFillPrice=...` FileLog line to `OnOrderUpdate` for every transition, and adds the signal name to the existing `FILL` line. Zero change to order submission, sizing, or execution-adapter logic. -- Prompt: `TASK_GAP025_RUNNER_ORDER_LIFECYCLE_LOGGING_PROMPT.md`. +- Prompt: `TASK_GAP025_RUNNER_ORDER_LIFECYCLE_LOGGING_PROMPT.md`. Implemented as additive logging only: `OnOrderUpdate` now writes realtime `ORDER SDK_... State=... Filled=.../... AvgFillPrice=...` lines for SDK-managed order transitions, and the existing `FILL` line now includes `Signal=...` so scaler/runner fills are distinguishable. Existing circuit-breaker, execution-adapter, and portfolio-risk callbacks were left unchanged. Verification: focused Adapter tests 81/81, `verify-build.bat` 572/572. Deploy backup `20260826_010153`. **[GAP-022-FOLLOWUP] Connection-State Test Seam** | Sprint 2.7 | ⏳ QUEUED — branch-only during Gate Run #2 - Add an injectable connection-state seam around `NT8StrategyBase`. diff --git a/docs/00-governance/current_status.md b/docs/00-governance/current_status.md index 9d9fd85..b4946a9 100644 --- a/docs/00-governance/current_status.md +++ b/docs/00-governance/current_status.md @@ -1,5 +1,13 @@ # Current Status — NT8 SDK -**Last Updated:** 2026-08-26 (GAP-062 implemented; deploy skipped pending SIM before/after observation) +**Last Updated:** 2026-08-26 (GAP-025 implemented; GAP-030 next) + +--- + +## 2026-08-26 — GAP-025 implemented; runner/scaler order lifecycle logging added + +Implemented and deployed `TASK_GAP025_RUNNER_ORDER_LIFECYCLE_LOGGING_PROMPT.md` as additive logging only. `NT8StrategyBase.OnOrderUpdate()` now writes realtime `ORDER SDK_... State=... Filled=.../... AvgFillPrice=...` lines for SDK-managed order transitions, making submitted/accepted/working/cancelled/rejected lifecycle state visible in the persistent session log. `OnExecutionUpdate()` keeps the existing `FILL` line shape but adds `Signal=...` so scaler (`SDK_*_S_*`) and runner (`SDK_*_R_*`) fills are distinguishable after the fact. + +Verification passed: focused Adapter tests 81/81 and `verify-build.bat` 572/572. Existing order-management behavior was not changed: `SubmitOrderToNT8`, managed order calls, `_circuitBreaker.RecordOrderRejection`, `_executionAdapter.ProcessOrderUpdate`, `_executionAdapter.ProcessExecution`, and `PortfolioRiskManager.ReportFill` remain intact. Deploy completed with backup `20260826_010153`; `NinjaTrader.Custom.dll`/`.pdb` were invalidated. --- diff --git a/docs/00-governance/decisions.md b/docs/00-governance/decisions.md index f7faf82..f4989f1 100644 --- a/docs/00-governance/decisions.md +++ b/docs/00-governance/decisions.md @@ -13,6 +13,17 @@ Use this file to record durable project decisions that affect implementation pat ## Decision Log +### DEC-20260826-04 +- **ID:** DEC-20260826-04 +- **Date:** 2026-08-26 +- **Status:** implemented and deployed by operator request +- **Context:** GAP-025 was prompted by `DEC-20260826-03` after reading current `NT8StrategyBase` order/execution handling. The persistent session log could show `SUBMIT` and `FILL`, but not order lifecycle transitions, and `FILL` omitted `execution.Order.Name`, making scaler and runner legs indistinguishable after the fact. +- **Decision:** Add realtime-only `FileLog` output in `OnOrderUpdate()` for SDK-managed order names (`SDK_...`) using `ORDER {signal} State={state} Filled={filled}/{quantity} AvgFillPrice={averageFillPrice}`. Extend the existing `FILL` line in `OnExecutionUpdate()` with `Signal={execution.Order.Name}`. Leave order submission and callback behavior unchanged. +- **Proof:** New source-text tests confirm the `ORDER` line is logged before the Rejected-only branch so non-rejected transitions are covered, the `FILL` line includes the signal name and remains historical-replay gated, and `_circuitBreaker.RecordOrderRejection`, `_executionAdapter.ProcessOrderUpdate`, `_executionAdapter.ProcessExecution`, and `PortfolioRiskManager.ReportFill` remain present with the expected call shapes. +- **Verification:** `dotnet test tests\NT8.Core.Tests\NT8.Core.Tests.csproj --configuration Release --filter "FullyQualifiedName~Adapters" --verbosity minimal` passed 81/81. `verify-build.bat` passed 572/572. Deploy completed with backup `20260826_010153`; `NinjaTrader.Custom.dll`/`.pdb` were invalidated. +- **Related Files:** `TASK_GAP025_RUNNER_ORDER_LIFECYCLE_LOGGING_PROMPT.md`, `src/NT8.Adapters/Strategies/NT8StrategyBase.cs`, `tests/NT8.Core.Tests/Adapters/NT8StrategyBaseOrderLifecycleTests.cs`, `SPRINT_BOARD.md`, `docs/00-governance/current_status.md`. +- **Action Required:** Manual NinjaScript compile after deploy. Next SIM trade with an active runner should show separate `ORDER SDK_..._S_...` and `ORDER SDK_..._R_...` lifecycle lines plus `FILL ... Signal=...` lines. + ### DEC-20260826-03 - **ID:** DEC-20260826-03 - **Date:** 2026-08-26 diff --git a/src/NT8.Adapters/Strategies/NT8StrategyBase.cs b/src/NT8.Adapters/Strategies/NT8StrategyBase.cs index 9ce93f8..22395a4 100644 --- a/src/NT8.Adapters/Strategies/NT8StrategyBase.cs +++ b/src/NT8.Adapters/Strategies/NT8StrategyBase.cs @@ -1285,6 +1285,16 @@ namespace NinjaTrader.NinjaScript.Strategies if (string.IsNullOrEmpty(order.Name) || !order.Name.StartsWith("SDK_")) return; + if (EnableFileLogging && State == State.Realtime) + { + FileLog(string.Format("ORDER {0} State={1} Filled={2}/{3} AvgFillPrice={4:F2}", + order.Name, + orderState, + filled, + quantity, + averageFillPrice)); + } + // Record NT8 rejections in circuit breaker if (orderState == NinjaTrader.Cbi.OrderState.Rejected && _circuitBreaker != null) { @@ -1323,11 +1333,12 @@ namespace NinjaTrader.NinjaScript.Strategies if (State != State.Realtime) return; - FileLog(string.Format("FILL {0} {1} @ {2:F2} | OrderId={3}", + FileLog(string.Format("FILL {0} {1} @ {2:F2} | OrderId={3} Signal={4}", execution.MarketPosition, execution.Quantity, execution.Price, - execution.OrderId)); + execution.OrderId, + execution.Order.Name)); var fill = new NT8.Core.Common.Models.OrderFill( orderId, diff --git a/tests/NT8.Core.Tests/Adapters/NT8StrategyBaseOrderLifecycleTests.cs b/tests/NT8.Core.Tests/Adapters/NT8StrategyBaseOrderLifecycleTests.cs new file mode 100644 index 0000000..b2c5d22 --- /dev/null +++ b/tests/NT8.Core.Tests/Adapters/NT8StrategyBaseOrderLifecycleTests.cs @@ -0,0 +1,98 @@ +using System; +using System.IO; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace NT8.Core.Tests.Adapters +{ + [TestClass] + public class NT8StrategyBaseOrderLifecycleTests + { + [TestMethod] + public void OnOrderUpdate_LogsEverySdkOrderTransitionToSessionFile() + { + string source = File.ReadAllText(FindStrategyBaseSourceFile()); + string method = ExtractMethodBlock(source, "protected override void OnOrderUpdate"); + + Assert.IsTrue(method.Contains("!order.Name.StartsWith(\"SDK_\")"), "Order lifecycle logging must stay scoped to SDK-managed signals."); + Assert.IsTrue(method.Contains("if (EnableFileLogging && State == State.Realtime)"), "Order lifecycle logging must be realtime file-log gated."); + Assert.IsTrue(method.Contains("FileLog(string.Format(\"ORDER {0} State={1} Filled={2}/{3} AvgFillPrice={4:F2}\""), "OnOrderUpdate must emit the ORDER lifecycle line."); + + int orderLogIndex = method.IndexOf("ORDER {0} State={1}", StringComparison.Ordinal); + int rejectedIndex = method.IndexOf("orderState == NinjaTrader.Cbi.OrderState.Rejected", StringComparison.Ordinal); + Assert.IsTrue(orderLogIndex >= 0 && rejectedIndex >= 0 && orderLogIndex < rejectedIndex, "ORDER logging must occur before the Rejected-only branch so non-rejected transitions are logged too."); + } + + [TestMethod] + public void OnExecutionUpdate_FillLineIncludesSignalName() + { + string source = File.ReadAllText(FindStrategyBaseSourceFile()); + string method = ExtractMethodBlock(source, "protected override void OnExecutionUpdate"); + + Assert.IsTrue(method.Contains("FileLog(string.Format(\"FILL {0} {1} @ {2:F2} | OrderId={3} Signal={4}\""), "FILL line must include the order signal name."); + Assert.IsTrue(method.Contains("execution.Order.Name));"), "FILL line must populate Signal from execution.Order.Name."); + Assert.IsTrue(method.Contains("if (State != State.Realtime)"), "FILL line must remain historical-replay gated."); + } + + [TestMethod] + public void OrderLifecycleLogging_KeepsExistingCallbacksUnchanged() + { + string source = File.ReadAllText(FindStrategyBaseSourceFile()); + string orderMethod = ExtractMethodBlock(source, "protected override void OnOrderUpdate"); + string executionMethod = ExtractMethodBlock(source, "protected override void OnExecutionUpdate"); + + Assert.IsTrue(orderMethod.Contains("_circuitBreaker.RecordOrderRejection(reason);"), "Rejected-specific circuit breaker behavior must remain present."); + Assert.IsTrue(orderMethod.Contains("_executionAdapter.ProcessOrderUpdate("), "Order adapter update callback must remain present."); + Assert.IsTrue(orderMethod.Contains("order.OrderId,"), "ProcessOrderUpdate must still receive order.OrderId."); + Assert.IsTrue(orderMethod.Contains("order.Name,"), "ProcessOrderUpdate must still receive order.Name."); + Assert.IsTrue(orderMethod.Contains("orderState.ToString(),"), "ProcessOrderUpdate must still receive orderState.ToString()."); + Assert.IsTrue(orderMethod.Contains("filled,"), "ProcessOrderUpdate must still receive filled quantity."); + Assert.IsTrue(orderMethod.Contains("averageFillPrice,"), "ProcessOrderUpdate must still receive average fill price."); + Assert.IsTrue(orderMethod.Contains("(int)errorCode,"), "ProcessOrderUpdate must still receive error code."); + Assert.IsTrue(orderMethod.Contains("nativeError);"), "ProcessOrderUpdate must still receive native error."); + + Assert.IsTrue(executionMethod.Contains("PortfolioRiskManager.Instance.ReportFill(Name, fill);"), "Portfolio risk fill reporting must remain present."); + Assert.IsTrue(executionMethod.Contains("_executionAdapter.ProcessExecution(orderId, executionId, price, quantity, time);"), "Execution adapter callback must remain unchanged."); + } + + private static string FindStrategyBaseSourceFile() + { + DirectoryInfo dir = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory); + while (dir != null) + { + string candidate = Path.Combine(dir.FullName, "src", "NT8.Adapters", "Strategies", "NT8StrategyBase.cs"); + if (File.Exists(candidate)) + return candidate; + dir = dir.Parent; + } + + throw new FileNotFoundException("Could not locate src\\NT8.Adapters\\Strategies\\NT8StrategyBase.cs from test output directory."); + } + + private static string ExtractMethodBlock(string source, string signature) + { + int start = source.IndexOf(signature, StringComparison.Ordinal); + if (start < 0) + Assert.Fail("Source does not contain method: " + signature); + + int brace = source.IndexOf("{", start, StringComparison.Ordinal); + if (brace < 0) + Assert.Fail("Method has no opening brace: " + signature); + + int depth = 0; + for (int i = brace; i < source.Length; i++) + { + if (source[i] == '{') + depth++; + else if (source[i] == '}') + { + depth--; + if (depth == 0) + return source.Substring(start, i - start + 1); + } + } + + Assert.Fail("Method has no closing brace: " + signature); + return string.Empty; + } + } +}