Skip to content

fix/flag-052-cancel-race-timer — Apply Instructions

Branch: fix/flag-052-cancel-race-timer Patch directory: C:\Users\Katja\Documents\Claude Homebase Neo\02 Projects\NEO Trading Engine\08 Patches\fix-flag-052-cancel-race-timer\

What this fixes

S54 root cause (revised): When _cancel_all_live_orders returned tecNO_TARGET (FLAG-047 already handled this correctly by calling mark_cancel_race_unknown), the periodic truth check still fired BEFORE the reconciler could resolve the race.

Tick order: _maybe_run_periodic_truth_check runs at the TOP of each tick, BEFORE reconciliation (Step 5). The CANCEL_RACE_UNKNOWN was created in tick N. Tick N+1 started with the 60-second truth check interval elapsed → truth check ran first → saw +13.65 XRP / -19.5 RLUSD delta → inventory_truth_halt. Reconciler never ran in tick N+1.

Fix: one line in the else: branch of _cancel_all_live_orders — after mark_cancel_race_unknown succeeds, self._last_truth_check_ts = time.time(). This gives the reconciler one full check_interval_s (default 60s) before the next truth check.

Apply

Run from C:\Users\Katja\Documents\NEO GitHub\neo-2026\:

git branch -D fix/flag-052-cancel-race-timer
git checkout -b fix/flag-052-cancel-race-timer

Get-ChildItem "C:\Users\Katja\Documents\Claude Homebase Neo\02 Projects\NEO Trading Engine\08 Patches\fix-flag-052-cancel-race-timer" -Filter "*.patch" | Sort-Object Name | ForEach-Object { git am $_.FullName }

Status (updated 2026-04-22)

Patches already applied. The branch fix/flag-052-cancel-race-timer has both patches applied. The test file had a bug (session_id / limit_price wrong field names) and was missing the Atlas-required integration test (4th test).

Both issues have been fixed in-place via the workspace mount. Run the following to verify, then amend the test commit:

Step 1 — verify tests pass

python -m pytest tests/test_flag_052_cancel_race_timer.py -v

Expected: 4 tests pass. - test_timer_reset_on_confirmed_race - test_timer_not_reset_on_db_failure - test_timer_not_reset_on_normal_success_cancel - test_integration_grace_window_defers_truth_check

Step 2 — amend the test commit

git add tests/test_flag_052_cancel_race_timer.py
git commit --amend -m "test(main_loop): FLAG-052 4 tests — timer reset + integration grace window"

Step 3 — FLAG-047 adjacency check

python -m pytest tests/test_flag_047_cancel_fill_race.py -v

Step 4 — full regression

python -m pytest tests/ -q

Step 5 — merge to main

Once all green:

git checkout main
git merge fix/flag-052-cancel-race-timer --no-ff -m "fix(main_loop): FLAG-052 reset truth-check timer on confirmed CANCEL_RACE_UNKNOWN"