From 4d15edcc54dda167f23c35f60abdac12c81d950e Mon Sep 17 00:00:00 2001 From: Codex Date: Sun, 31 May 2026 18:46:06 +0200 Subject: [PATCH] =?UTF-8?q?PINK:=20fix=20multi-leg=20exit=20residual=20?= =?UTF-8?q?=E2=80=94=20carry=20kernel=20leg-index=20into=20legacy=20positi?= =?UTF-8?q?on?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Root cause (harness multi_leg, ~14-TRX residual): pink_direct rebuilds the legacy TradePosition from the kernel slot every step, but left exit_leg_index=0, so IntentEngine.next_exit_ratio() consumed ratio[0] (0.5) on EVERY leg and never advanced to the final leg's 1.0: leg1: 0.5×53 ≈ 26 closed -> 27 remain leg2: 0.5×27 ≈ 13 closed -> 14 RESIDUAL (kernel believes flat, exchange isn't) Fix: propagate the kernel slot's authoritative active_leg_index into the rebuilt legacy position's exit_leg_index, so the intent engine consumes the correct leg ratio. The final leg now closes the full remaining -> fully flattens. Verified: offline 18 green (no regression); live VST harness multi_leg now closes fully (XPASS) — residual gone, all 6 capital invariants hold. xfail mark removed; capital-accounting battery is now fully green (7/7) on testnet. Co-Authored-By: Claude Opus 4.8 --- prod/clean_arch/runtime/pink_direct.py | 6 ++++++ prod/tests/test_pink_capital_harness.py | 11 +---------- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/prod/clean_arch/runtime/pink_direct.py b/prod/clean_arch/runtime/pink_direct.py index 2b97e7a..43e17e0 100644 --- a/prod/clean_arch/runtime/pink_direct.py +++ b/prod/clean_arch/runtime/pink_direct.py @@ -458,6 +458,12 @@ class PinkDirectRuntime: current_price=float(slot_dict.get("entry_price", 0.0)), initial_size=float(slot_dict.get("initial_size", 0.0)), exit_leg_ratios=tuple(slot_dict.get("exit_leg_ratios", [1.0])), + # Carry the kernel's authoritative leg progression so the intent + # engine consumes the CORRECT exit-leg ratio. The legacy position + # is rebuilt every step; without this exit_leg_index resets to 0 + # and every leg uses ratio[0] — under-closing each leg and leaving + # a residual (kernel believes flat, exchange does not). + exit_leg_index=int(slot_dict.get("active_leg_index", 0) or 0), closed=False, ) diff --git a/prod/tests/test_pink_capital_harness.py b/prod/tests/test_pink_capital_harness.py index 44d9ad2..f9e9522 100644 --- a/prod/tests/test_pink_capital_harness.py +++ b/prod/tests/test_pink_capital_harness.py @@ -319,16 +319,7 @@ _TRADING_SCENARIOS = { } -@pytest.mark.parametrize("name", [ - "round_trip", - pytest.param("multi_leg", marks=pytest.mark.xfail( - reason="KNOWN capital/sizing finding: multi-leg partial reduce-only exits leave a " - "lot-step rounding residual on the venue (kernel believes flat, exchange has a " - "remainder). To be fixed in the capital/sizing-path rework.", - strict=False)), - "sequential", - "exit_then_reentry", -]) +@pytest.mark.parametrize("name", list(_TRADING_SCENARIOS)) def test_pink_capital(name): ratios, scenario = _TRADING_SCENARIOS[name]