dexbot 1.7.0 → 1.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (105) hide show
  1. package/CHANGELOG.md +42 -0
  2. package/README.md +3 -3
  3. package/analysis/README.md +33 -13
  4. package/claw/package.json +1 -1
  5. package/claw/runtimes/openclaw-plugin/openclaw.plugin.json +1 -1
  6. package/claw/runtimes/openclaw-plugin/package.json +1 -1
  7. package/dist/analysis/account_resolver.js +5 -6
  8. package/dist/analysis/analyze_liquidity.js +15 -4
  9. package/dist/analysis/bot_key_utils.d.ts +44 -1
  10. package/dist/analysis/bot_key_utils.js +76 -7
  11. package/dist/analysis/fills_cache.js +2 -1
  12. package/dist/analysis/grid_correction_check.d.ts +1 -19
  13. package/dist/analysis/grid_correction_check.js +12 -30
  14. package/dist/analysis/liquidity/liquidity_chart_generator.js +190 -20
  15. package/dist/analysis/liquidity/liquidity_depth_model.d.ts +41 -16
  16. package/dist/analysis/liquidity/liquidity_depth_model.js +49 -21
  17. package/dist/analysis/liquidity/liquidity_view.d.ts +15 -0
  18. package/dist/analysis/liquidity/liquidity_view.js +11 -0
  19. package/dist/analysis/resolve_bot_accounts.js +7 -6
  20. package/dist/analysis/trade_profitability.js +2 -1
  21. package/dist/credential-daemon.js +13 -2
  22. package/dist/dexbot.js +4 -4
  23. package/dist/modules/account_bots.d.ts +11 -1
  24. package/dist/modules/account_orders.d.ts +12 -0
  25. package/dist/modules/account_orders.js +30 -0
  26. package/dist/modules/bitshares-native/transport.js +24 -6
  27. package/dist/modules/bitshares_client.d.ts +24 -6
  28. package/dist/modules/bitshares_client.js +44 -7
  29. package/dist/modules/bot_settings.d.ts +2 -0
  30. package/dist/modules/constants.d.ts +26 -3
  31. package/dist/modules/constants.js +63 -8
  32. package/dist/modules/dexbot_class.d.ts +1 -6
  33. package/dist/modules/dexbot_class.js +14 -0
  34. package/dist/modules/dexbot_cow_runtime.d.ts +97 -7
  35. package/dist/modules/dexbot_cow_runtime.js +620 -132
  36. package/dist/modules/dexbot_fill_runtime.js +124 -35
  37. package/dist/modules/dexbot_maintenance_runtime.js +226 -73
  38. package/dist/modules/dexbot_startup_runtime.js +32 -20
  39. package/dist/modules/dexbot_state_recovery.d.ts +33 -1
  40. package/dist/modules/dexbot_state_recovery.js +167 -5
  41. package/dist/modules/node_connect_policy.d.ts +8 -1
  42. package/dist/modules/node_connect_policy.js +18 -2
  43. package/dist/modules/node_flap_score.d.ts +87 -0
  44. package/dist/modules/node_flap_score.js +124 -0
  45. package/dist/modules/node_health_cache.d.ts +40 -3
  46. package/dist/modules/node_health_cache.js +66 -20
  47. package/dist/modules/node_manager.d.ts +101 -19
  48. package/dist/modules/node_manager.js +255 -65
  49. package/dist/modules/order/accounting.js +56 -7
  50. package/dist/modules/order/format.d.ts +24 -10
  51. package/dist/modules/order/format.js +30 -10
  52. package/dist/modules/order/grid.js +82 -11
  53. package/dist/modules/order/grid_reconcile.d.ts +5 -1
  54. package/dist/modules/order/grid_reconcile.js +333 -210
  55. package/dist/modules/order/grid_reconcile_internal.d.ts +38 -2
  56. package/dist/modules/order/grid_reconcile_internal.js +207 -87
  57. package/dist/modules/order/index.d.ts +4 -0
  58. package/dist/modules/order/lifecycle.d.ts +353 -0
  59. package/dist/modules/order/lifecycle.js +1109 -0
  60. package/dist/modules/order/manager.d.ts +271 -5
  61. package/dist/modules/order/manager.js +651 -48
  62. package/dist/modules/order/placement_lock.d.ts +102 -0
  63. package/dist/modules/order/placement_lock.js +222 -0
  64. package/dist/modules/order/strategy.js +13 -1
  65. package/dist/modules/order/sync_engine.d.ts +13 -1
  66. package/dist/modules/order/sync_engine.js +153 -53
  67. package/dist/modules/order/utils/math.d.ts +22 -1
  68. package/dist/modules/order/utils/math.js +54 -1
  69. package/dist/modules/order/utils/order.d.ts +75 -5
  70. package/dist/modules/order/utils/order.js +158 -9
  71. package/dist/modules/order/utils/system.d.ts +18 -0
  72. package/dist/modules/order/utils/system.js +84 -8
  73. package/dist/modules/order/utils/validate.d.ts +7 -1
  74. package/dist/modules/order/utils/validate.js +48 -2
  75. package/dist/modules/outcome.d.ts +54 -0
  76. package/dist/modules/outcome.js +51 -0
  77. package/dist/modules/settings_merge.d.ts +4 -0
  78. package/dist/modules/settings_merge.js +9 -0
  79. package/dist/modules/timing_policy.d.ts +82 -0
  80. package/dist/modules/timing_policy.js +244 -0
  81. package/dist/modules/types.d.ts +132 -0
  82. package/dist/scripts/analyze-credit.js +54 -17
  83. package/dist/scripts/analyze-orders.d.ts +4 -2
  84. package/dist/scripts/analyze-orders.js +113 -34
  85. package/dist/scripts/chart_targets.js +11 -15
  86. package/dist/scripts/test-credit-renewal.js +12 -5
  87. package/docs/COW_INVARIANTS.md +67 -8
  88. package/docs/DEXBOT_COMPARISON.md +6 -6
  89. package/docs/EVOLUTION.md +9 -8
  90. package/docs/FUND_MOVEMENT_AND_ACCOUNTING.md +1 -1
  91. package/docs/GRID_PRICE_INVARIANT.md +1 -1
  92. package/docs/GRID_RECONCILE.md +17 -12
  93. package/docs/LOGGING.md +25 -1
  94. package/docs/ORDER_ENGINE_POST_1.0_RETROSPECTIVE.md +175 -26
  95. package/docs/README.md +23 -2
  96. package/docs/WORKFLOW.md +9 -3
  97. package/docs/architecture.md +42 -5
  98. package/docs/developer_guide.md +25 -8
  99. package/modules/README.md +3 -0
  100. package/package.json +1 -1
  101. package/scripts/README.md +16 -0
  102. package/scripts/pdev.sh +14 -1
  103. package/scripts/pmain.sh +23 -8
  104. package/scripts/ptest.sh +7 -0
  105. package/tests/README.md +30 -1
@@ -13,6 +13,7 @@ const { readOpenOrdersWithMetaSafe } = chainOrdersModule;
13
13
  import { BroadcastUncertainError as BroadcastUncertainErrorBinding } from './dexbot_credential_client.js';
14
14
  const BroadcastUncertainError = BroadcastUncertainErrorBinding;
15
15
  import * as orderUtils from './order/utils/order.js';
16
+ import { lazyShim } from './lazy_shim.js';
16
17
  import { sleep, setLastFillPivot, fireStructuralResync, evaluateSpreadCorrectionReadiness, resolveLastFillGuardReference, resolveSpreadCrossingFloor, isLastFillGuardBlocked } from './order/utils/system.js';
17
18
  const { buildCreateOrderArgs, buildCreateOpFingerprint, extractBatchOperationResults, formatUnmatchedChainOrder, convertToSpreadPlaceholder, toRailHolePlaceholder, buildOutsideInPairGroups, isOrderPlaced, chainOrderUnchangedFromCache, detectCrossedBookPlan, collectKnownOnChainOrderIds, } = orderUtils;
18
19
  import * as validate from './order/utils/validate.js';
@@ -59,6 +60,147 @@ export function isEvacuationStampStillValid(liveBoundary, liveGapSlots, sourceId
59
60
  const dstInRail = orderType === ORDER_TYPES.SELL ? Number(dstIdx) >= sellStartIdx : Number(dstIdx) <= b;
60
61
  return dstInRail;
61
62
  }
63
+ /**
64
+ * Detect a live-rail conflict: the resting BUY/SELL rails cannot coexist with
65
+ * a `gapSlots`-wide spread band, i.e. no boundary value satisfies
66
+ * `maxBuy <= boundary` and `minSell >= boundary + gapSlots + 1` at once.
67
+ *
68
+ * Why this matters for gap evacuation: the stale-stamp check below refuses an
69
+ * evacuation whose source is no longer in-band under the LIVE (pre-commit)
70
+ * boundary. That is correct when the live geometry is itself consistent (the
71
+ * 2026-10-30 incident: plan boundary 124 wanted buys 125-128 evacuated, live
72
+ * 128 kept them on the rail, the bid edge survived). It is WRONG when the live
73
+ * rails already overlap: then the batch's boundary retreat is FORCED — some
74
+ * order has to move — and refusing the evacuation leaves the stranded order
75
+ * inside the band permanently (observed class: a buy fill inside the band
76
+ * crawls the boundary down, the retreat strands the two buys above it, the
77
+ * guard skips their rotation as stale, the rail clamp cannot move the boundary
78
+ * back because the sell rail caps it, and the 4-slot spread band keeps two
79
+ * live buys forever → real spread 1.2% against a 2.0% target).
80
+ *
81
+ * The test is boundary-independent (a crossing is a crossing at any boundary),
82
+ * so it cannot be satisfied by simply moving the boundary: only an evacuation
83
+ * resolves it. Ladder positions use the same live-price resolution as
84
+ * deriveRailBoundary, so a stale slot id cannot fake a rail member.
85
+ *
86
+ * @param manager - Manager exposing orders/assets/_genesis
87
+ * @param gapSlots - Gap-band width (slots)
88
+ * @returns `{ crossed, maxBuyIdx, minSellIdx, upper }`; `crossed` is false when
89
+ * either rail is empty (one-sided book proves nothing) or geometry is
90
+ * unknown (fail closed: keep the stale-stamp refusal).
91
+ */
92
+ export function liveRailsConflictWithGap(manager, gapSlots) {
93
+ const result = { crossed: false, maxBuyIdx: null, minSellIdx: null, upper: null };
94
+ const rawGap = Format.toKnownNumber(gapSlots);
95
+ if (!Number.isFinite(rawGap) || rawGap < 0)
96
+ return result;
97
+ const gap = Math.floor(rawGap);
98
+ const master = manager?.orders;
99
+ if (!(master instanceof Map))
100
+ return result;
101
+ const assets = manager?.assets;
102
+ const genesis = manager?._genesis;
103
+ let maxBuyIdx = null;
104
+ let minSellIdx = null;
105
+ for (const raw of master.values()) {
106
+ const o = raw;
107
+ if (!o || !o.id || !o.orderId)
108
+ continue;
109
+ if (o.state !== ORDER_STATES.ACTIVE && o.state !== ORDER_STATES.PARTIAL)
110
+ continue;
111
+ if (o.type !== ORDER_TYPES.BUY && o.type !== ORDER_TYPES.SELL)
112
+ continue;
113
+ const live = orderUtils.resolveLiveOrderLadderPosition(o, assets, genesis);
114
+ const idx = live.idx !== null && Number.isFinite(Number(live.idx)) ? Number(live.idx) : null;
115
+ if (idx === null)
116
+ continue;
117
+ if (o.type === ORDER_TYPES.BUY)
118
+ maxBuyIdx = maxBuyIdx === null ? idx : Math.max(maxBuyIdx, idx);
119
+ else
120
+ minSellIdx = minSellIdx === null ? idx : Math.min(minSellIdx, idx);
121
+ }
122
+ result.maxBuyIdx = maxBuyIdx;
123
+ result.minSellIdx = minSellIdx;
124
+ if (maxBuyIdx === null || minSellIdx === null)
125
+ return result;
126
+ const upper = minSellIdx - gap - 1;
127
+ result.upper = upper;
128
+ result.crossed = maxBuyIdx > upper;
129
+ return result;
130
+ }
131
+ /**
132
+ * Decide whether a stale-stamp refusal of a gap-evacuation rotation must yield
133
+ * to the forced-retreat allowance. Single authority for the three call sites
134
+ * (stamped build branch, unstamped build branch, final pivot gate) so the
135
+ * conditions can never drift apart.
136
+ *
137
+ * Two conditions, both required:
138
+ * 1. The stamp could actually be EVALUATED: an unparseable source/dest id is
139
+ * not a geometry disagreement — `isEvacuationStampStillValid` refuses those
140
+ * too, and overriding that would ship a rotation whose geometry nobody
141
+ * proved. Never override an unprovable stamp.
142
+ * 2. The live rails already conflict (see liveRailsConflictWithGap): some
143
+ * order must move at ANY boundary, so the plan's retreat is the only
144
+ * consistent geometry and the pre-commit "still on the rail" reading is not
145
+ * authoritative.
146
+ *
147
+ * @param manager - Manager exposing orders/assets/_genesis
148
+ * @param gapSlots - LIVE gap-band width (slots)
149
+ * @param sourceId - Evacuation source slot id
150
+ * @param destId - Evacuation destination slot id
151
+ * @returns The conflict record when the allowance applies, otherwise null.
152
+ */
153
+ export function forcedRetreatJustifiesEvacuation(manager, gapSlots, sourceId, destId) {
154
+ if (parseSlotIndex(sourceId) == null || parseSlotIndex(destId) == null)
155
+ return null;
156
+ const conflict = liveRailsConflictWithGap(manager, gapSlots);
157
+ return conflict.crossed ? conflict : null;
158
+ }
159
+ /**
160
+ * Split pending price corrections into "drain now" and "held back because this
161
+ * batch supersedes them".
162
+ *
163
+ * A cancel-only gap-evacuation whose chain order the batch ROTATES is
164
+ * superseded by the plan: the rotation moves that order onto its rail in place,
165
+ * and cancelling it first destroys the batch's own source. Observed live on a
166
+ * stranded 4-slot band: the teeth queued cancels for the two rotation sources,
167
+ * the pre-batch drain executed them, and the UPDATE batch was rejected
168
+ * pre-broadcast ("2 target(s) missing on chain") → structural resync + full grid
169
+ * reload + two CREATEs, instead of two in-place price updates.
170
+ *
171
+ * Held entries are put back after the drain: if the rotation is dropped or the
172
+ * batch aborts they stay queued (their own staleness check drops them once the
173
+ * slot no longer owns the order).
174
+ *
175
+ * @param entries - `manager.ordersNeedingPriceCorrection`
176
+ * @param actions - The batch's planned actions
177
+ * @returns `{ kept, held }` (both empty-safe)
178
+ */
179
+ export function splitEvacuationCancelsSupersededByRotations(entries, actions) {
180
+ const list = Array.isArray(entries) ? entries : [];
181
+ const kept = [];
182
+ const held = [];
183
+ const rotatedOrderIds = new Set();
184
+ if (Array.isArray(actions)) {
185
+ for (const action of actions) {
186
+ if (action?.type !== COW_ACTIONS.UPDATE || !action?.orderId)
187
+ continue;
188
+ rotatedOrderIds.add(String(action.orderId));
189
+ }
190
+ }
191
+ if (rotatedOrderIds.size === 0)
192
+ return { kept: list, held };
193
+ for (const entry of list) {
194
+ const superseded = entry?.gapEvacuation === true
195
+ && entry?.chainOrderId != null
196
+ && rotatedOrderIds.has(String(entry.chainOrderId));
197
+ if (superseded)
198
+ held.push(entry);
199
+ else
200
+ kept.push(entry);
201
+ }
202
+ return { kept, held };
203
+ }
62
204
  function hasSlotPriceCollision(items, targetPrice, genesis, excludeId, predicate) {
63
205
  for (const it of items) {
64
206
  if (predicate && !predicate(it))
@@ -84,6 +226,16 @@ const Format = FormatModule;
84
226
  import * as workingGridModule from './order/working_grid.js';
85
227
  const { WorkingGrid } = workingGridModule;
86
228
  import { getErrorMessage, resolveSeamMs, resolveSeamMsOrNull, getErrorField } from './utils/errors.js';
229
+ import { unknownCowOutcome } from './outcome.js';
230
+ /**
231
+ * Post-commit gap-evacuation sweep (lazy: dexbot_state_recovery pulls the
232
+ * bitshares client into the graph, which this runtime deliberately avoids).
233
+ * Detects live orders left inside the committed band and schedules a fill-less
234
+ * re-plan to evacuate them — the in-batch rotation is refused as stale when the
235
+ * plan's own crawl created the stranding, and without this the spread collapse
236
+ * never heals on a quiet market.
237
+ */
238
+ const scheduleGapEvacRepair = lazyShim('./dexbot_state_recovery', 'scheduleGapEvacRepair');
87
239
  // Maximum number of times the pre-broadcast staleness guard may re-plan the
88
240
  // batch from a fresh master before proceeding anyway. Bounded so a master
89
241
  // grid that keeps mutating (fill bursts, sync loops) can never livelock the
@@ -152,16 +304,16 @@ function createOpFingerprintForSlot(bot, order, finalInts, slotId) {
152
304
  });
153
305
  }
154
306
  /**
155
- * Read the pending-broadcast entries as an array. The manager map is
156
- * created lazily by recordPendingBroadcast, so every reader must guard
157
- * on `instanceof Map` — this helper is the single spelling of that guard.
307
+ * Read the pending-broadcast entries as an array.
308
+ *
309
+ * Thin wrapper over the single liveness authority (`getLivePendingBroadcasts`
310
+ * in `order/utils/order.ts`): TTL + caught-up-sync + 2× hard cap, with expired
311
+ * entries pruned. Do not re-implement the policy here.
158
312
  * @param {import('./dexbot_class.js').DEXBot} bot
159
313
  * @returns {Array}
160
314
  */
161
315
  function getPendingBroadcasts(bot) {
162
- return (bot.manager && bot.manager._pendingBroadcasts instanceof Map)
163
- ? Array.from(bot.manager._pendingBroadcasts.values())
164
- : [];
316
+ return orderUtils.getLivePendingBroadcasts(bot?.manager);
165
317
  }
166
318
  /**
167
319
  * Probe the chain snapshot for a pending-broadcast entry's slot.
@@ -332,6 +484,8 @@ function recordPendingBroadcast(bot, entry) {
332
484
  if (!bot.manager._pendingBroadcasts || !(bot.manager._pendingBroadcasts instanceof Map)) {
333
485
  bot.manager._pendingBroadcasts = new Map();
334
486
  }
487
+ // Prune expired entries before recording a new one (bounded map).
488
+ getPendingBroadcasts(bot);
335
489
  const fingerprint = createOpFingerprintForSlot(bot, entry.order, entry.finalInts, entry.order.id);
336
490
  if (!fingerprint) {
337
491
  bot.manager.logger.log?.(`[COW] Skipped pending-broadcast record: could not build fingerprint for ${entry.order?.id || 'unknown'}`, 'warn');
@@ -472,7 +626,7 @@ async function deferUncertainBroadcastRead(bot, detail, suffix, resyncReason, re
472
626
  bot.manager.logger.log(`[COW][UNCERTAIN] Structural resync escalation suppressed (cooldown ${cooldownMs}ms) — ` +
473
627
  `pending-broadcast protection retained; next clean read adopts landed orders.`, 'debug');
474
628
  }
475
- return { executed: false, hadRotation: false, uncertain: true, ambiguousRead: true };
629
+ return unknownCowOutcome('ambiguous-read');
476
630
  }
477
631
  /**
478
632
  * Build a fingerprint for an on-chain order so it can be matched against
@@ -965,7 +1119,7 @@ async function reconcileAfterUncertainBroadcastImpl(bot, err, opContexts, _optio
965
1119
  bot.manager.logger.log('[COW][UNCERTAIN] readOpenOrdersWithMeta unavailable; falling back to structural resync only.', 'error');
966
1120
  await requestStructuralResync(bot, 'broadcast uncertain — readOpenOrders unavailable', { batchId: err?.batchId || null });
967
1121
  clearPendingBroadcasts(bot.manager?._pendingBroadcasts);
968
- return { executed: false, hadRotation: false, uncertain: true };
1122
+ return unknownCowOutcome('unavailable', { detail: 'readOpenOrdersWithMeta unavailable' });
969
1123
  }
970
1124
  // 1. Read the chain
971
1125
  const accountRef = bot.accountId || bot.account?.id || bot.account;
@@ -981,7 +1135,7 @@ async function reconcileAfterUncertainBroadcastImpl(bot, err, opContexts, _optio
981
1135
  `Falling back to structural resync.`, 'error');
982
1136
  await requestStructuralResync(bot, 'broadcast uncertain — readOpenOrders failed', { batchId: err?.batchId || null, error: getErrorMessage(readErr) || String(readErr) });
983
1137
  clearPendingBroadcasts(bot.manager?._pendingBroadcasts);
984
- return { executed: false, hadRotation: false, uncertain: true };
1138
+ return unknownCowOutcome('unavailable', { detail: 'readOpenOrders failed' });
985
1139
  }
986
1140
  // 1.5. Empty/truncated-read guard: an empty snapshot is ambiguous — the
987
1141
  // account is either genuinely empty or the node is lagging behind the
@@ -1026,7 +1180,7 @@ async function reconcileAfterUncertainBroadcastImpl(bot, err, opContexts, _optio
1026
1180
  // after the reconciliation, ensuring the next cycle re-plans from a clean
1027
1181
  // chain snapshot.
1028
1182
  await resyncIfUnreconciled(bot, err, pending, chainSnapshot, adopted, adoptedCount, discardedCount);
1029
- return { executed: false, hadRotation: false, uncertain: true, adoptedCount, discardedCount };
1183
+ return unknownCowOutcome('lost-receipt', { adoptedCount, discardedCount });
1030
1184
  }
1031
1185
  /**
1032
1186
  * Check whether to execute creates in outside-in pair mode.
@@ -2477,6 +2631,42 @@ function toRefillSlotIdSet(refillSlotIds) {
2477
2631
  }
2478
2632
  return set;
2479
2633
  }
2634
+ /**
2635
+ * Resolve the rail side of a plan's refill holes from the working grid (the
2636
+ * planned CREATE/rotation type) with the master grid as fallback. Used only to
2637
+ * scope the boundary hold to the promotion direction; an unknown side keeps the
2638
+ * legacy hold (fail safe).
2639
+ * @param refillSlotIds - Plan hole-CREATE/rotation destination ids
2640
+ * @param workingGrid - Plan working grid (has the planned slot types)
2641
+ * @param manager - Order manager (master fallback)
2642
+ * @returns ORDER_TYPES.BUY / SELL, or null when no refill slot carries a side
2643
+ */
2644
+ function resolveRefillSide(refillSlotIds, workingGrid, manager) {
2645
+ if (!Array.isArray(refillSlotIds))
2646
+ return null;
2647
+ let side = null;
2648
+ for (const raw of refillSlotIds) {
2649
+ if (typeof raw !== 'string' || raw.length === 0)
2650
+ continue;
2651
+ const planned = workingGrid?.get?.(raw);
2652
+ const master = manager?.orders instanceof Map ? manager.orders.get(raw) : null;
2653
+ // Prefer the planned type, but fall back to the master type when the
2654
+ // planned slot is side-neutral (SPREAD) or absent: a refill wire can
2655
+ // carry an id whose working-grid entry was normalized away.
2656
+ const plannedType = planned?.type;
2657
+ const type = (plannedType === ORDER_TYPES.BUY || plannedType === ORDER_TYPES.SELL)
2658
+ ? plannedType
2659
+ : master?.type;
2660
+ if (type !== ORDER_TYPES.BUY && type !== ORDER_TYPES.SELL)
2661
+ continue;
2662
+ // A one-sided plan is the norm; a mixed wire is not a usable direction
2663
+ // signal, so report unknown and let the caller keep the legacy hold.
2664
+ if (side !== null && side !== type)
2665
+ return null;
2666
+ side = type;
2667
+ }
2668
+ return side;
2669
+ }
2480
2670
  /**
2481
2671
  * Boundary-hold decision for guard-skipped refills.
2482
2672
  * When a skipped slot is one of the plan's refill slots — a hole-CREATE that
@@ -2487,11 +2677,30 @@ function toRefillSlotIdSet(refillSlotIds) {
2487
2677
  * Unrelated vetoes (skip ids outside the refill set) never pin geometry.
2488
2678
  * The grid still commits; only the boundary value is held (same discipline
2489
2679
  * as the overrun-hold in validateBoundaryCommit).
2680
+ *
2681
+ * `refillSide` (BUY/SELL) scopes the hold to the boundary's PROMOTION
2682
+ * direction; a fill-driven retreat, or a wire whose side cannot be resolved
2683
+ * (null), keeps the legacy hold.
2490
2684
  */
2491
- function resolveRefillBoundaryHold(workingBoundary, committedBoundary, skippedUpdateSlotIds, clampedUpdateSlotIds, refillSlotIds, skippedCreateSlotIds = undefined) {
2685
+ function resolveRefillBoundaryHold(workingBoundary, committedBoundary, skippedUpdateSlotIds, clampedUpdateSlotIds, refillSlotIds, skippedCreateSlotIds = undefined, refillSide = null) {
2492
2686
  const refills = toRefillSlotIdSet(refillSlotIds);
2493
2687
  const heldRefillSlotIds = [];
2494
- if (refills.size > 0) {
2688
+ // A refill hold is evidence for a boundary PROMOTION — the move advances
2689
+ // TOWARD the band holes the refills would fill. A fill-driven retreat moves
2690
+ // AWAY from them: the skipped holes are now band slots, so holding the old
2691
+ // boundary would discard the fill-derived geometry (live incident: a hold
2692
+ // kept 169 over a fill-derived 168 after the buy fills at 168/167). Judging
2693
+ // the direction keeps the hold's own premise instead of adding a second
2694
+ // geometry source. No side/geometry => legacy hold (fail safe, never
2695
+ // fail-open into a boundary shift).
2696
+ const working = Format.toKnownNumber(workingBoundary);
2697
+ const committed = Format.toKnownNumber(committedBoundary);
2698
+ const towardBand = refillSide === ORDER_TYPES.BUY
2699
+ ? (Number.isFinite(working) && Number.isFinite(committed) ? Number(working) > Number(committed) : true)
2700
+ : refillSide === ORDER_TYPES.SELL
2701
+ ? (Number.isFinite(working) && Number.isFinite(committed) ? Number(working) < Number(committed) : true)
2702
+ : true;
2703
+ if (refills.size > 0 && towardBand) {
2495
2704
  const seen = new Set();
2496
2705
  const skipCollections = skippedCreateSlotIds !== undefined
2497
2706
  ? [skippedUpdateSlotIds, clampedUpdateSlotIds, skippedCreateSlotIds]
@@ -2751,13 +2960,24 @@ export function deriveRailBoundary(manager, opContexts, desiredBoundary, gapSlot
2751
2960
  if (dense) {
2752
2961
  const check = math.validatePersistedBoundary(next, slotsForGate, gap, vacatedSlotIds.size > 0 ? { allowInBandSlotIds: vacatedSlotIds } : undefined);
2753
2962
  if (!check || check.ok !== true) {
2754
- return {
2755
- boundary: null,
2756
- maxBuyIdx,
2757
- minSellIdx,
2758
- clamped: false,
2759
- reason: `unsafe: ${check?.reason ?? 'unknown'}${check?.detail ? ` (${check.detail})` : ''}`
2760
- };
2963
+ // VIOLATION-REDUCING REPAIR (same rule as the fill re-anchor):
2964
+ // the clamp may accept a move the strict validator refuses for
2965
+ // transient stranding only when it strands STRICTLY FEWER placed
2966
+ // orders than the committed boundary already does. This is what
2967
+ // lets a fill-derived retreat land while GAP-EVAC is still
2968
+ // draining older strands; structural refusals stay closed.
2969
+ const committed = Format.toKnownNumber(manager?.boundaryIdx);
2970
+ const repairsStrands = check?.reason === math.BOUNDARY_REJECT_PLACED_IN_BAND
2971
+ && math.boundaryMoveReducesStrands(Number.isFinite(committed) ? Number(committed) : null, next, slotsForGate, gap, vacatedSlotIds.size > 0 ? { allowInBandSlotIds: vacatedSlotIds } : undefined);
2972
+ if (!repairsStrands) {
2973
+ return {
2974
+ boundary: null,
2975
+ maxBuyIdx,
2976
+ minSellIdx,
2977
+ clamped: false,
2978
+ reason: `unsafe: ${check?.reason ?? 'unknown'}${check?.detail ? ` (${check.detail})` : ''}`
2979
+ };
2980
+ }
2761
2981
  }
2762
2982
  }
2763
2983
  }
@@ -2866,12 +3086,57 @@ function restoreSkippedUpdateSlotsInWorkingGrid(bot, workingGrid, skippedSlotIds
2866
3086
  * @param {Object} cowResult - The stale plan result
2867
3087
  * @param {number} replanDepth - Recursion depth (0 = first attempt)
2868
3088
  * @param {Object} preBroadcastGuard - The failed evaluateCommit result
2869
- * @returns {Promise<{handled: boolean, result?: Object}>} handled=true when the
3089
+ * @returns {Promise<{handled: boolean, result?: Object, placementTransferred?: boolean}>} handled=true when the
2870
3090
  * batch was resolved by the re-plan (fresh plan executed, or stale plan
2871
3091
  * skipped as already-consistent); handled=false when the caller must proceed
2872
- * with the original plan.
3092
+ * with the original plan. `placementTransferred` means the recursion now owns
3093
+ * the placement lease and the caller must not release it.
2873
3094
  */
2874
- async function replanStaleBatch(bot, cowResult, replanDepth, preBroadcastGuard, seamPollIntervalMs) {
3095
+ async function replanStaleBatch(bot, cowResult, replanDepth, preBroadcastGuard, seamPollIntervalMs, placementLease = null) {
3096
+ const rebuildInProgress = typeof bot.manager?.isStructuralRebuildInProgress === 'function'
3097
+ && bot.manager.isStructuralRebuildInProgress() === true;
3098
+ // STRUCTURAL REBUILD GUARD. A `grid-init`/resync staleness means the master
3099
+ // was rebuilt while this plan was in flight. The fresh master has no live
3100
+ // chain order IDs, so a re-plan would emit a create-only batch for levels
3101
+ // that startup reconcile is concurrently re-pricing via UPDATE. Both
3102
+ // generations land and the loser becomes a duplicate-price orphan backlog
3103
+ // (2026-11-03: 25 duplicate orders). Abort instead and let the
3104
+ // reconcile (update-first) complete — fills stay recorded and are drained
3105
+ // by the bootstrap consumer / next reconcile.
3106
+ //
3107
+ // The progress flag is the authoritative signal, not the reason string: a
3108
+ // rebuild mutates the master with contexts like `sync-pass1-partial`
3109
+ // (those mutations are stamped `structural rebuild` by the manager), and
3110
+ // a plan can reach here after the rebuild ended but while the reason still
3111
+ // names a fill. Checking both closes the non-`grid-init` gap.
3112
+ const structuralRebuild = rebuildInProgress
3113
+ || (typeof bot.manager?.isStructuralRebuildStale === 'function'
3114
+ ? bot.manager.isStructuralRebuildStale(preBroadcastGuard?.reason)
3115
+ : orderUtils.isStructuralRebuildStaleReason(preBroadcastGuard?.reason));
3116
+ if (structuralRebuild) {
3117
+ bot.manager.logger.log(`[COW] Plan stale after a structural grid rebuild (${preBroadcastGuard.reason}); ` +
3118
+ `aborting this batch so the startup reconcile keeps update-first ownership (no create-only re-plan).`, 'warn');
3119
+ popPushedWorkingGrid(bot, cowResult);
3120
+ // The aborted plan's CREATE slots were recorded as pending broadcasts
3121
+ // during op-building but will never be sent. Drop only THEIR entries:
3122
+ // keeping them would make the reconcile's surplus sweep and the next
3123
+ // batch's pending-broadcast guard treat a phantom create as in flight.
3124
+ // Entries from an earlier unresolved batch are deliberately kept.
3125
+ clearPendingBroadcastsForSlots(bot, cowResult.actions);
3126
+ // A rebuild that is still running already owns the repair: requesting
3127
+ // another structural resync here would start a second rebuild on top of
3128
+ // it (churn loop). Only ask when the rebuild has already ended.
3129
+ if (!rebuildInProgress) {
3130
+ await requestStructuralResync(bot, 'plan stale after structural rebuild (deferred to reconcile)', { reason: preBroadcastGuard.reason });
3131
+ }
3132
+ else {
3133
+ bot.manager.logger.log('[COW] Structural rebuild still in progress; leaving the resync to the active rebuild (no churn request)', 'debug');
3134
+ }
3135
+ return {
3136
+ handled: true,
3137
+ result: { executed: false, aborted: true, reason: 'STRUCTURAL_REBUILD_DEFERRED', hadRotation: false }
3138
+ };
3139
+ }
2875
3140
  const canReplan = replanDepth < STALE_PLAN_REPLAN_LIMIT
2876
3141
  && Array.isArray(cowResult.fills) && cowResult.fills.length > 0;
2877
3142
  if (!canReplan) {
@@ -2892,6 +3157,10 @@ async function replanStaleBatch(bot, cowResult, replanDepth, preBroadcastGuard,
2892
3157
  const hadPushedGrid = cowResult?._workingGridPushed === true;
2893
3158
  popPushedWorkingGrid(bot, cowResult);
2894
3159
  let replanned = null;
3160
+ // Pre-plan snapshot of the per-batch absolute-anchor budget (see the restore
3161
+ // above). Kept at function scope so the fresh plan can carry it on for a
3162
+ // second re-plan.
3163
+ const reanchorBudgetBefore = Number(cowResult._reanchorBudgetBeforePlan);
2895
3164
  try {
2896
3165
  // Restore the boundary-shift budget consumed by the abandoned plan:
2897
3166
  // it was built from the same fills and never shipped, so the re-plan
@@ -2901,6 +3170,14 @@ async function replanStaleBatch(bot, cowResult, replanDepth, preBroadcastGuard,
2901
3170
  if (bot.manager?._boundaryShiftBudgetBase != null) {
2902
3171
  bot.manager._boundaryShiftBudget = bot.manager._boundaryShiftBudgetBase;
2903
3172
  }
3173
+ // The absolute fill re-anchor is a per-batch BUDGET consumed at PLAN time
3174
+ // (strategy.calculateTargetGrid). The abandoned plan's anchor was never
3175
+ // committed, so hand it back: restore the value observed by the pre-plan
3176
+ // snapshot carried on the plan. A static batch base would be wrong — an
3177
+ // earlier chunk that legitimately committed its anchor must stay spent.
3178
+ if (Number.isFinite(reanchorBudgetBefore)) {
3179
+ bot.manager._boundaryReanchorBudget = reanchorBudgetBefore;
3180
+ }
2904
3181
  if (typeof bot.manager.performSafeRebalance === 'function') {
2905
3182
  // skipBroadcastWait: this frame is itself inside the executor's
2906
3183
  // startBroadcasting() region — waiting on the flag we hold would
@@ -2924,10 +3201,17 @@ async function replanStaleBatch(bot, cowResult, replanDepth, preBroadcastGuard,
2924
3201
  // (duplicate orders). The recursion's guard will then abort +
2925
3202
  // reconcile instead.
2926
3203
  clearPendingBroadcastsForSlots(bot, cowResult.actions);
3204
+ // Hand the same pre-plan snapshot to the fresh plan so a SECOND
3205
+ // re-plan (its plan also goes stale) can restore the anchor again:
3206
+ // the budget is per-batch, and every abandoned plan must give back
3207
+ // only what IT spent.
3208
+ replanned._reanchorBudgetBeforePlan = reanchorBudgetBefore;
2927
3209
  return {
2928
3210
  handled: true,
3211
+ placementTransferred: true,
2929
3212
  result: await updateOrdersOnChainBatchCOW(bot, replanned, {
2930
3213
  replanDepth: replanDepth + 1,
3214
+ placementLease,
2931
3215
  // Carry the seam through the recursion explicitly: the inner
2932
3216
  // frame re-sets and re-restores it, so it must not depend on
2933
3217
  // the outer frame's side-channel value surviving.
@@ -3020,9 +3304,20 @@ async function waitForCowBroadcastSingleFlight(bot, label) {
3020
3304
  * @returns {number}
3021
3305
  */
3022
3306
  function plannedUpdateSize(action) {
3023
- return Number.isFinite(Number(action.newSize))
3307
+ const planned = Number.isFinite(Number(action.newSize))
3024
3308
  ? Number(action.newSize)
3025
3309
  : Number(action.order?.size || 0);
3310
+ // A PARTIAL slot's booked size is the post-fill remainder. The plan's
3311
+ // desired size is a full-side target and must not grow the order in place:
3312
+ // target the booked remainder at plan time so the broadcast op is correct
3313
+ // by construction (the clamp below stays as a defense-in-depth invariant).
3314
+ const order = action.order;
3315
+ if (order && order.state === ORDER_STATES.PARTIAL) {
3316
+ const booked = Number(order.size);
3317
+ if (Number.isFinite(booked) && booked > 0 && planned > booked)
3318
+ return booked;
3319
+ }
3320
+ return planned;
3026
3321
  }
3027
3322
  /**
3028
3323
  * Post-fill size invariant for COW UPDATE ops: a partially-filled order
@@ -3047,10 +3342,16 @@ function clampPostFillUpdateSize(bot, masterOrder, newSize, action) {
3047
3342
  if (!Number.isFinite(target) || !Number.isFinite(booked) || booked <= 0)
3048
3343
  return target;
3049
3344
  if (target > booked) {
3345
+ const slotKey = String(action?.id || masterOrder?.id || 'unknown');
3346
+ const warned = bot._clampPostFillWarnedSlots
3347
+ || (bot._clampPostFillWarnedSlots = new Set());
3348
+ const firstHit = !warned.has(slotKey);
3349
+ if (firstHit)
3350
+ warned.add(slotKey);
3050
3351
  bot.manager.logger.log(`[COW] Post-fill size clamp for ${action?.id || masterOrder?.id}: slot is PARTIAL with ` +
3051
3352
  `booked remaining ${Format.formatAmount(booked)} but the plan targets ` +
3052
3353
  `${Format.formatAmount(target)} — clamping to booked remaining. ` +
3053
- `A partially-filled order must not be grown in place by a COW update (fill accounting divergence).`, 'warn');
3354
+ `A partially-filled order must not be grown in place by a COW update (fill accounting divergence).`, firstHit ? 'warn' : 'debug');
3054
3355
  return booked;
3055
3356
  }
3056
3357
  return target;
@@ -3074,7 +3375,13 @@ async function runPreBroadcastGuards(bot, cowResult) {
3074
3375
  const chainOrderCandidates = Array.isArray(bot.manager?._lastUnmatchedChainOrders)
3075
3376
  ? bot.manager._lastUnmatchedChainOrders
3076
3377
  : [];
3077
- const createSlotValidation = validateCreateTargetSlots(actions, bot.manager?.orders, bot.manager?.assets, chainOrderCandidates, bot.manager?.config?.incrementPercent ?? null);
3378
+ const createSlotValidation = validateCreateTargetSlots(actions, bot.manager?.orders, bot.manager?.assets, chainOrderCandidates, bot.manager?.config?.incrementPercent ?? null, getPendingBroadcasts(bot).map((entry) => ({
3379
+ slotId: entry.slotId ?? entry.order?.id,
3380
+ price: entry.order?.price,
3381
+ size: entry.order?.size,
3382
+ type: entry.orderType ?? entry.order?.type,
3383
+ fingerprint: entry.fingerprint,
3384
+ })));
3078
3385
  if (!createSlotValidation.isValid) {
3079
3386
  for (const violation of createSlotValidation.violations) {
3080
3387
  let reason;
@@ -3097,6 +3404,9 @@ async function runPreBroadcastGuards(bot, cowResult) {
3097
3404
  case 'slot_create_uncertain':
3098
3405
  reason = `an earlier CREATE here has an unknown outcome (createUncertain)`;
3099
3406
  break;
3407
+ case 'pending_broadcast_collision':
3408
+ reason = `a pending uncertain CREATE already targets this slot/price`;
3409
+ break;
3100
3410
  default:
3101
3411
  reason = `existing orderId=${violation.currentOrderId}`;
3102
3412
  }
@@ -3434,7 +3744,7 @@ async function updateOrdersOnChainBatchCOW(bot, cowResult, options = {}) {
3434
3744
  // recursion, throws) funnels through the body() finally below, so no exit
3435
3745
  // can leak bot._testPollIntervalMs onto the bot.
3436
3746
  try {
3437
- return await updateOrdersOnChainBatchCOWBody(bot, cowResult, replanDepth, seamPollIntervalMs ?? undefined);
3747
+ return await updateOrdersOnChainBatchCOWBody(bot, cowResult, replanDepth, seamPollIntervalMs ?? undefined, options?.placementLease ?? null);
3438
3748
  }
3439
3749
  finally {
3440
3750
  // Restore only when this frame actually wrote a seam. The entry
@@ -3451,7 +3761,7 @@ async function updateOrdersOnChainBatchCOW(bot, cowResult, options = {}) {
3451
3761
  }
3452
3762
  }
3453
3763
  }
3454
- async function updateOrdersOnChainBatchCOWBody(bot, cowResult, replanDepth, seamPollIntervalMs) {
3764
+ async function updateOrdersOnChainBatchCOWBody(bot, cowResult, replanDepth, seamPollIntervalMs, inheritedPlacementLease = null) {
3455
3765
  bot._currentCycleId = (Number.isFinite(Number(bot._currentCycleId)) ? Number(bot._currentCycleId) : 0) + 1;
3456
3766
  const { workingGrid, workingIndexes, workingBoundary, actions } = cowResult;
3457
3767
  // Boundary-hold value: computed pre-broadcast after the skip-restore and
@@ -3484,93 +3794,49 @@ async function updateOrdersOnChainBatchCOWBody(bot, cowResult, replanDepth, seam
3484
3794
  popPushedWorkingGrid(bot, cowResult);
3485
3795
  return { executed: true, hadRotation: false };
3486
3796
  }
3487
- // Single-flight COW broadcast guard (entry check): never broadcast two
3488
- // batches concurrently. Overlapping broadcasts plan from the same base
3489
- // grid version; when the first commits it bumps _gridVersion, so the
3490
- // second's commit is refused (base-version mismatch) -> adopt-from-chain
3491
- // -> snapshot reload that can drop the adopted order and produce an
3492
- // orphan fill. This entry wait is an optimization; the authoritative
3493
- // atomic check-and-set happens right before the broadcast below.
3494
- if (await waitForCowBroadcastSingleFlight(bot, 'entry')) {
3495
- popPushedWorkingGrid(bot, cowResult);
3496
- return { executed: false, aborted: true, reason: 'SHUTDOWN_IN_PROGRESS', hadRotation: false };
3497
- }
3498
- // DRAIN PENDING CORRECTIONS before the batch is planned/broadcast.
3499
- // Cancel-only corrections (duplicate-price orphans) queued by an earlier
3500
- // sync must not sit while batches run back-to-back (startup create
3501
- // groups, fill bursts) — with the open-orders sync loop disabled they
3502
- // would otherwise linger indefinitely, keep blocking same-level CREATEs,
3503
- // and risk cancelling the wrong side of a duplicate later. Draining here
3504
- // also keeps this batch's collision checks (chain_orphan_collision)
3505
- // honest: orphaned chain orders already queued for cancellation are
3506
- // resolved before the plan validates its CREATE targets against them.
3507
- const pendingCorrectionCount = Array.isArray(bot.manager?.ordersNeedingPriceCorrection)
3508
- ? bot.manager.ordersNeedingPriceCorrection.length
3509
- : 0;
3510
- if (pendingCorrectionCount > 0 && !bot._shuttingDown) {
3511
- try {
3512
- bot.manager.logger.log(`[COW] Draining ${pendingCorrectionCount} pending correction(s) before batch`, 'info');
3513
- const drainResult = await orderUtils.correctAllPriceMismatches(bot.manager, bot.account, bot.privateKey, chainOrders);
3514
- if (drainResult?.failed > 0) {
3515
- bot.manager.logger.log(`[COW] ${drainResult.failed} correction(s) failed pre-batch` +
3516
- ((drainResult.staleDropped ?? 0) > 0 ? `, ${drainResult.staleDropped} stale dropped` : '') +
3517
- `; remaining entries retry on next sync/maintenance tick`, 'warn');
3518
- }
3519
- else if ((drainResult?.staleDropped ?? 0) > 0) {
3520
- bot.manager.logger.log(`[COW] Pre-batch drain resolved, ${drainResult.staleDropped} stale correction(s) dropped`, 'info');
3521
- }
3522
- }
3523
- catch (drainErr) {
3524
- bot.manager.logger.log(`[COW] Pre-batch correction drain failed: ${getErrorMessage(drainErr)}`, 'warn');
3797
+ // Exclusive placement region (shared with startup reconcile Phase 2,
3798
+ // correction drains, dust/residual cancels and pool swaps). Claim it BEFORE
3799
+ // the pre-batch drain and before any other mutation: planning is read-only,
3800
+ // but the drain and the broadcast are not, and a competing path that slips
3801
+ // in between the drain and the old late claim is exactly the duplicate
3802
+ // window this closes. Held through commit/uncertain-catch and released in
3803
+ // the body's finally (or transferred to the re-plan recursion).
3804
+ let heldPlacementLease = inheritedPlacementLease;
3805
+ const claimPlacement = async (stage) => {
3806
+ if (heldPlacementLease)
3807
+ return true;
3808
+ if (typeof bot.manager?.acquirePlacement !== 'function')
3809
+ return true;
3810
+ const lease = await bot.manager.acquirePlacement('cow-broadcast', {
3811
+ timeoutMs: SINGLE_FLIGHT_MAX_WAIT_MS,
3812
+ isShuttingDown: () => bot._shuttingDown === true,
3813
+ });
3814
+ if (!lease) {
3815
+ bot.manager.logger.log(`[COW] ${stage}: exclusive placement region unavailable (timeout/shutdown); aborting this batch instead of overlapping another placement path.`, 'warn');
3816
+ return false;
3525
3817
  }
3818
+ heldPlacementLease = lease;
3819
+ return true;
3820
+ };
3821
+ const releasePlacement = () => {
3822
+ const lease = heldPlacementLease;
3823
+ heldPlacementLease = null;
3824
+ lease?.release();
3825
+ };
3826
+ // Claim before the entry single-flight wait: the second batch then queues
3827
+ // on placement instead of building a plan, and there is no lock-order
3828
+ // inversion (both COW frames acquire placement before the COW slot).
3829
+ if (!(await claimPlacement('entry'))) {
3830
+ popPushedWorkingGrid(bot, cowResult);
3831
+ return { executed: false, aborted: true, reason: 'PLACEMENT_REGION_UNAVAILABLE', hadRotation: false };
3526
3832
  }
3527
- // Pre-broadcast guard chain (create-slot validation, recovery-exhausted
3528
- // block, pending/unmatched guards, crossed-book gate) — see
3529
- // runPreBroadcastGuards. Refusals already popped the working grid.
3530
- const guards = await runPreBroadcastGuards(bot, cowResult);
3531
- if (!guards.proceed)
3532
- return guards.result ?? { executed: false, hadRotation: false };
3533
- const { crossingCandidates = [], intraBatchCandidates = [] } = guards;
3534
- const { assetA, assetB } = bot.manager.assets;
3833
+ // Everything after a successful claim runs INSIDE this try/finally: the
3834
+ // placement lease is released on every exit, including a throw from the
3835
+ // pre-broadcast drain/guards. An acquire outside the try leaked the lease
3836
+ // on any throw, and a leaked lease parks the fill queue (placement-aware
3837
+ // deferral) while the broadcast watchdog refuses to clear it.
3535
3838
  const operations = [];
3536
3839
  const opContexts = [];
3537
- const skippedUpdateSlotIds = new Set();
3538
- let skippedUpdateCount = 0;
3539
- // Guard-skipped CREATE slot ids (hole-refills never placed). Fed to the
3540
- // boundary-hold intersect alongside the UPDATE sets above — a skipped
3541
- // refill CREATE strands its rail hole exactly like a restored UPDATE.
3542
- const skippedCreateSlotIds = new Set();
3543
- // Per-batch LAST-FILL-GUARD disposition counters. Per-action pass lines
3544
- // would spam big batches, so the guard emits one batch summary instead
3545
- // (see the summary after the action loop below).
3546
- const lastFillGuardStats = { checked: 0, passed: 0, skipped: 0, bypassed: 0, pivotOffGrid: 0 };
3547
- // One crossing-CREATE observation per batch (see warnCrossingCreate).
3548
- let crossingCreateWarned = false;
3549
- // Fingerprints of the pending-broadcast entries recorded by THIS batch's
3550
- // op-building (both CREATE paths). The final pivot gate's compaction
3551
- // remaps these entries' stored opIndex/ctxIndex to their post-drop
3552
- // positions; entry.batchId cannot discriminate batches (always null in
3553
- // production), so the fingerprint set is the ownership marker.
3554
- const batchPendingFps = new Set();
3555
- // Per-batch GRID-PRICE-INVARIANT counters (BLOCKING). Tracks emitted prices
3556
- // that are not the genesis level for their slot — such emissions are
3557
- // rejected, not placed. One batch summary.
3558
- const gridPriceInvariantStats = { checked: 0, violated: 0, unchecked: 0 };
3559
- // Whether any guard check in this batch refreshed the pivot from
3560
- // still-queued fills — reported in the batch summary so a pivot change
3561
- // that altered a guard decision is visible at info, not just debug.
3562
- let lastFillGuardPivotRefreshed = false;
3563
- // Slots whose size-update op was broadcast with a post-fill-clamped
3564
- // target: the working grid still holds the planned (larger) size, so the
3565
- // slots are re-synced from master before commit to keep the committed
3566
- // books equal to the broadcast chain amounts.
3567
- const clampedUpdateSlotIds = new Set();
3568
- // orderId -> operations index of its cancel op. A crossing re-pricing
3569
- // update is only safe when the crossed order's cancel was already queued
3570
- // at an earlier position: ops broadcast in gap-slot-sized chunks,
3571
- // so an earlier index means the cancel confirms on chain (same or earlier
3572
- // chunk, applied sequentially) before the crossing order lands.
3573
- const cancelOpIndexByOrderId = new Map();
3574
3840
  const idsToLock = new Set();
3575
3841
  for (const action of actions) {
3576
3842
  if (action.type === COW_ACTIONS.CANCEL && action.orderId) {
@@ -3587,17 +3853,153 @@ async function updateOrdersOnChainBatchCOWBody(bot, cowResult, replanDepth, seam
3587
3853
  idsToLock.add(action.id);
3588
3854
  }
3589
3855
  }
3590
- bot.manager.lockOrders(idsToLock);
3591
- // Ownership of the single-flight broadcast slot for THIS frame. Only the
3592
- // frame that claimed the slot (set bot._cowBroadcastInFlight = true below)
3593
- // may clear it in the finally. A frame that waits at the entry check or
3594
- // early-returns after planning (fund validation, create-slot abort, op-prep
3595
- // throw) never owns the slot and MUST NOT clear it — an unconditional clear
3596
- // would wipe a concurrent batch's in-flight flag and let a third batch
3597
- // broadcast on top of it (the exact overlap this guard prevents).
3598
3856
  let heldBroadcastSlot = false;
3857
+ let batchInFlightRaised = false;
3858
+ let ordersLocked = false;
3599
3859
  try {
3860
+ // Single-flight COW broadcast guard (entry check): never broadcast two
3861
+ // batches concurrently. Overlapping broadcasts plan from the same base
3862
+ // grid version; when the first commits it bumps _gridVersion, so the
3863
+ // second's commit is refused (base-version mismatch) -> adopt-from-chain
3864
+ // -> snapshot reload that can drop the adopted order and produce an
3865
+ // orphan fill. This entry wait is an optimization; the authoritative
3866
+ // atomic check-and-set happens right before the broadcast below.
3867
+ if (await waitForCowBroadcastSingleFlight(bot, 'entry')) {
3868
+ releasePlacement();
3869
+ popPushedWorkingGrid(bot, cowResult);
3870
+ return { executed: false, aborted: true, reason: 'SHUTDOWN_IN_PROGRESS', hadRotation: false };
3871
+ }
3872
+ // DRAIN PENDING CORRECTIONS before the batch is planned/broadcast.
3873
+ // Cancel-only corrections (duplicate-price orphans) queued by an earlier
3874
+ // sync must not sit while batches run back-to-back (startup create
3875
+ // groups, fill bursts) — with the open-orders sync loop disabled they
3876
+ // would otherwise linger indefinitely, keep blocking same-level CREATEs,
3877
+ // and risk cancelling the wrong side of a duplicate later. Draining here
3878
+ // also keeps this batch's collision checks (chain_orphan_collision)
3879
+ // honest: orphaned chain orders already queued for cancellation are
3880
+ // resolved before the plan validates its CREATE targets against them.
3881
+ //
3882
+ // EXCEPTION — a cancel-only gap-evacuation whose chain order THIS batch
3883
+ // rotates is superseded by the plan: the rotation moves the order onto its
3884
+ // rail in place, and cancelling it first destroys the batch's own source
3885
+ // (observed live: "2 target(s) missing on chain" → structural resync + full
3886
+ // grid reload instead of two in-place price updates). Hold those entries out
3887
+ // of the drain and put them back afterwards — if the rotation is dropped or
3888
+ // the batch aborts, the entry stays queued for the next tick (its own
3889
+ // staleness check drops it once the slot is evacuated).
3890
+ let heldEvacuationCorrections = [];
3891
+ if (Array.isArray(bot.manager?.ordersNeedingPriceCorrection)) {
3892
+ const split = splitEvacuationCancelsSupersededByRotations(bot.manager.ordersNeedingPriceCorrection, actions);
3893
+ if (split.held.length > 0) {
3894
+ heldEvacuationCorrections = split.held;
3895
+ bot.manager.ordersNeedingPriceCorrection = split.kept;
3896
+ bot.manager.logger.log(`[COW] Holding ${split.held.length} gap-evacuation cancel(s) back from the pre-batch drain: ` +
3897
+ `this batch rotates the same chain order (the rotation IS the evacuation)`, 'debug');
3898
+ }
3899
+ }
3900
+ const pendingCorrectionCount = Array.isArray(bot.manager?.ordersNeedingPriceCorrection)
3901
+ ? bot.manager.ordersNeedingPriceCorrection.length
3902
+ : 0;
3903
+ if (pendingCorrectionCount > 0 && !bot._shuttingDown) {
3904
+ try {
3905
+ bot.manager.logger.log(`[COW] Draining ${pendingCorrectionCount} pending correction(s) before batch`, 'info');
3906
+ const drainResult = await orderUtils.correctAllPriceMismatches(bot.manager, bot.account, bot.privateKey, chainOrders, { placementLease: heldPlacementLease });
3907
+ if (drainResult?.failed > 0) {
3908
+ bot.manager.logger.log(`[COW] ${drainResult.failed} correction(s) failed pre-batch` +
3909
+ ((drainResult.staleDropped ?? 0) > 0 ? `, ${drainResult.staleDropped} stale dropped` : '') +
3910
+ `; remaining entries retry on next sync/maintenance tick`, 'warn');
3911
+ }
3912
+ else if ((drainResult?.staleDropped ?? 0) > 0) {
3913
+ bot.manager.logger.log(`[COW] Pre-batch drain resolved, ${drainResult.staleDropped} stale correction(s) dropped`, 'info');
3914
+ }
3915
+ }
3916
+ catch (drainErr) {
3917
+ bot.manager.logger.log(`[COW] Pre-batch correction drain failed: ${getErrorMessage(drainErr)}`, 'warn');
3918
+ }
3919
+ }
3920
+ if (heldEvacuationCorrections.length > 0 && Array.isArray(bot.manager?.ordersNeedingPriceCorrection)) {
3921
+ // Restore in queue order. Entries the drain already resolved are gone;
3922
+ // these were never handed to it, so they are all still pending.
3923
+ bot.manager.ordersNeedingPriceCorrection.push(...heldEvacuationCorrections);
3924
+ }
3925
+ // Pre-broadcast guard chain (create-slot validation, recovery-exhausted
3926
+ // block, pending/unmatched guards, crossed-book gate) — see
3927
+ // runPreBroadcastGuards. Refusals already popped the working grid.
3928
+ const guards = await runPreBroadcastGuards(bot, cowResult);
3929
+ if (!guards.proceed) {
3930
+ releasePlacement();
3931
+ return guards.result ?? { executed: false, hadRotation: false };
3932
+ }
3933
+ const { crossingCandidates = [], intraBatchCandidates = [] } = guards;
3934
+ const { assetA, assetB } = bot.manager.assets;
3935
+ const skippedUpdateSlotIds = new Set();
3936
+ let skippedUpdateCount = 0;
3937
+ // Guard-skipped CREATE slot ids (hole-refills never placed). Fed to the
3938
+ // boundary-hold intersect alongside the UPDATE sets above — a skipped
3939
+ // refill CREATE strands its rail hole exactly like a restored UPDATE.
3940
+ const skippedCreateSlotIds = new Set();
3941
+ // Per-batch LAST-FILL-GUARD disposition counters. Per-action pass lines
3942
+ // would spam big batches, so the guard emits one batch summary instead
3943
+ // (see the summary after the action loop below).
3944
+ const lastFillGuardStats = { checked: 0, passed: 0, skipped: 0, bypassed: 0, pivotOffGrid: 0 };
3945
+ // One crossing-CREATE observation per batch (see warnCrossingCreate).
3946
+ let crossingCreateWarned = false;
3947
+ // Fingerprints of the pending-broadcast entries recorded by THIS batch's
3948
+ // op-building (both CREATE paths). The final pivot gate's compaction
3949
+ // remaps these entries' stored opIndex/ctxIndex to their post-drop
3950
+ // positions; entry.batchId cannot discriminate batches (always null in
3951
+ // production), so the fingerprint set is the ownership marker.
3952
+ const batchPendingFps = new Set();
3953
+ // Per-batch GRID-PRICE-INVARIANT counters (BLOCKING). Tracks emitted prices
3954
+ // that are not the genesis level for their slot — such emissions are
3955
+ // rejected, not placed. One batch summary.
3956
+ const gridPriceInvariantStats = { checked: 0, violated: 0, unchecked: 0 };
3957
+ // Whether any guard check in this batch refreshed the pivot from
3958
+ // still-queued fills — reported in the batch summary so a pivot change
3959
+ // that altered a guard decision is visible at info, not just debug.
3960
+ let lastFillGuardPivotRefreshed = false;
3961
+ // Slots whose size-update op was broadcast with a post-fill-clamped
3962
+ // target: the working grid still holds the planned (larger) size, so the
3963
+ // slots are re-synced from master before commit to keep the committed
3964
+ // books equal to the broadcast chain amounts.
3965
+ const clampedUpdateSlotIds = new Set();
3966
+ // orderId -> operations index of its cancel op. A crossing re-pricing
3967
+ // update is only safe when the crossed order's cancel was already queued
3968
+ // at an earlier position: ops broadcast in gap-slot-sized chunks,
3969
+ // so an earlier index means the cancel confirms on chain (same or earlier
3970
+ // chunk, applied sequentially) before the crossing order lands.
3971
+ const cancelOpIndexByOrderId = new Map();
3972
+ bot.manager.lockOrders(idsToLock);
3973
+ ordersLocked = true;
3974
+ // Ownership of the single-flight broadcast slot for THIS frame. Only the
3975
+ // frame that claimed the slot (set bot._cowBroadcastInFlight = true below)
3976
+ // may clear it in the finally. A frame that waits at the entry check or
3977
+ // early-returns after planning (fund validation, create-slot abort, op-prep
3978
+ // throw) never owns the slot and MUST NOT clear it — an unconditional clear
3979
+ // would wipe a concurrent batch's in-flight flag and let a third batch
3980
+ // broadcast on top of it (the exact overlap this guard prevents).
3981
+ // Exclusive placement region is claimed at the top of the body (before the
3982
+ // pre-batch drain). `heldBroadcastSlot` only serializes COW batches; the
3983
+ // placement lease serializes EVERY chain-mutating path. Both are released
3984
+ // on every exit; the re-plan recursion inherits the lease.
3985
+ // LEASE-LIVENESS RE-CHECK (the mirror of the nested drain's check): the
3986
+ // stale-region watchdog may have force-released this lease while the batch
3987
+ // was parked on a slow chain read, and another path can own the region by
3988
+ // now. The OWNER has to verify before broadcasting, exactly like the drain
3989
+ // it hands the lease to — otherwise the torn-down batch wakes up and
3990
+ // broadcasts unprotected, which is the duplicate window the watchdog was
3991
+ // allowed to open only because it believed the batch was hung.
3992
+ if (heldPlacementLease
3993
+ && typeof bot.manager.isPlacementLeaseCurrent === 'function'
3994
+ && !bot.manager.isPlacementLeaseCurrent(heldPlacementLease)) {
3995
+ bot.manager.logger.log('[COW] Placement lease was force-released while this batch was in flight (stale-region watchdog); ' +
3996
+ 'aborting this batch instead of broadcasting without the region.', 'warn');
3997
+ popPushedWorkingGrid(bot, cowResult);
3998
+ return { executed: false, aborted: true, reason: 'PLACEMENT_LEASE_LOST', hadRotation: false };
3999
+ }
3600
4000
  bot._batchInFlight++;
4001
+ batchInFlightRaised = true;
4002
+ bot._batchInFlightStartedAt = Date.now();
3601
4003
  bot._markGridActivity('batch start');
3602
4004
  bot.manager._setRebalanceState(REBALANCE_STATES.BROADCASTING);
3603
4005
  bot.manager.startBroadcasting();
@@ -3930,9 +4332,27 @@ async function updateOrdersOnChainBatchCOWBody(bot, cowResult, replanDepth, seam
3930
4332
  // live probe below instead of bypassing outright.
3931
4333
  const stampValid = isEvacuationStampStillValid(bot.manager?.boundaryIdx, bot.manager?._gapSlots, action.id, action.newGridId, orderType);
3932
4334
  if (!stampValid) {
3933
- stampUsable = false;
3934
- bot.manager.logger.log(`[LAST-FILL-GUARD] Stamped evacuation for ${action.id} -> ${action.newGridId} is stale under live ` +
3935
- `boundary ${bot.manager?.boundaryIdx}/gap ${bot.manager?._gapSlots} — refusing the stale plan`, 'warn');
4335
+ // FORCED-RETREAT ALLOWANCE: the pre-commit
4336
+ // geometry says the source is still a rail
4337
+ // member, but if the live rails cannot host
4338
+ // the gap band at ANY boundary then the
4339
+ // batch's retreat is forced and refusing
4340
+ // this evacuation is what permanently
4341
+ // strands the order inside the band (the
4342
+ // spread-collapse class this check is meant
4343
+ // to protect against, inverted).
4344
+ const conflict = forcedRetreatJustifiesEvacuation(bot.manager, bot.manager?._gapSlots, action.id, action.newGridId);
4345
+ if (conflict) {
4346
+ bot.manager.logger.log(`[LAST-FILL-GUARD] Stamped evacuation for ${action.id} -> ${action.newGridId} is stale under live ` +
4347
+ `boundary ${bot.manager?.boundaryIdx}/gap ${bot.manager?._gapSlots}, but the live rails already conflict ` +
4348
+ `(maxBuy=${conflict.maxBuyIdx} > minSell-gap-1=${conflict.upper}) — the retreat this batch commits is ` +
4349
+ `forced, so the evacuation proceeds (plan-build authority kept)`, 'warn');
4350
+ }
4351
+ else {
4352
+ stampUsable = false;
4353
+ bot.manager.logger.log(`[LAST-FILL-GUARD] Stamped evacuation for ${action.id} -> ${action.newGridId} is stale under live ` +
4354
+ `boundary ${bot.manager?.boundaryIdx}/gap ${bot.manager?._gapSlots} — refusing the stale plan`, 'warn');
4355
+ }
3936
4356
  }
3937
4357
  }
3938
4358
  if (stampUsable) {
@@ -4005,10 +4425,26 @@ async function updateOrdersOnChainBatchCOWBody(bot, cowResult, replanDepth, seam
4005
4425
  // Non-finite live geometry keeps plan-build authority
4006
4426
  // (isEvacuationStampStillValid returns true there).
4007
4427
  let staleSkipReason = null;
4008
- if (!isEvacuationStampStillValid(bot.manager?.boundaryIdx, bot.manager?._gapSlots, action.id, action.newGridId, orderType)) {
4009
- staleSkipReason = `source/dest no longer evacuation geometry under live boundary ${bot.manager?.boundaryIdx}/gap ${bot.manager?._gapSlots}`;
4428
+ const stampStillValid = isEvacuationStampStillValid(bot.manager?.boundaryIdx, bot.manager?._gapSlots, action.id, action.newGridId, orderType);
4429
+ if (!stampStillValid) {
4430
+ // Same forced-retreat allowance as the
4431
+ // stamped branch: a live rail conflict means
4432
+ // only an evacuation can resolve the
4433
+ // geometry, so the pre-commit "still on the
4434
+ // rail" reading is not authoritative. The
4435
+ // live probe below still proves direction
4436
+ // and size (this is not a blind bypass).
4437
+ const conflict = forcedRetreatJustifiesEvacuation(bot.manager, bot.manager?._gapSlots, action.id, action.newGridId);
4438
+ if (conflict) {
4439
+ bot.manager.logger.log(`[LAST-FILL-GUARD] ${action.id} -> ${action.newGridId} is stale under live boundary ` +
4440
+ `${bot.manager?.boundaryIdx}/gap ${bot.manager?._gapSlots}, but the live rails already conflict ` +
4441
+ `(maxBuy=${conflict.maxBuyIdx} > minSell-gap-1=${conflict.upper}) — re-proving the evacuation live`, 'warn');
4442
+ }
4443
+ else {
4444
+ staleSkipReason = `source/dest no longer evacuation geometry under live boundary ${bot.manager?.boundaryIdx}/gap ${bot.manager?._gapSlots}`;
4445
+ }
4010
4446
  }
4011
- else {
4447
+ if (staleSkipReason === null) {
4012
4448
  const evacCheck = isEvacuationRotationAllowed(oldPrice, oldSize, newPrice, newSize, orderType, sidePrecision);
4013
4449
  if (evacCheck.allowed) {
4014
4450
  bypassedEvacuation = true;
@@ -4390,8 +4826,11 @@ async function updateOrdersOnChainBatchCOWBody(bot, cowResult, replanDepth, seam
4390
4826
  }
4391
4827
  // BOUNDARY HOLD: skipped refill slots strand empty rail holes past the
4392
4828
  // planned boundary (commit gate skips empties, self-legalizing) — hold
4393
- // the committed boundary; intersect-only, grid still commits.
4394
- const refillHold = resolveRefillBoundaryHold(workingBoundary, bot.manager.boundaryIdx, skippedUpdateSlotIds, clampedUpdateSlotIds, cowResult?.refillSlotIds, skippedCreateSlotIds);
4829
+ // the committed boundary; intersect-only, grid still commits. The hold
4830
+ // is scoped to the boundary's PROMOTION direction: a fill-driven retreat
4831
+ // moves away from the holes and must not be pinned (see
4832
+ // resolveRefillBoundaryHold).
4833
+ const refillHold = resolveRefillBoundaryHold(workingBoundary, bot.manager.boundaryIdx, skippedUpdateSlotIds, clampedUpdateSlotIds, cowResult?.refillSlotIds, skippedCreateSlotIds, resolveRefillSide(cowResult?.refillSlotIds, workingGrid, bot.manager));
4395
4834
  effectiveBoundary = refillHold.effectiveBoundary;
4396
4835
  boundaryHeld = refillHold.heldRefillSlotIds.length > 0;
4397
4836
  if (refillHold.heldRefillSlotIds.length > 0) {
@@ -4484,8 +4923,10 @@ async function updateOrdersOnChainBatchCOWBody(bot, cowResult, replanDepth, seam
4484
4923
  });
4485
4924
  if (!preBroadcastGuard.canCommit) {
4486
4925
  // Bounded re-plan + proceed — policy documented on replanStaleBatch.
4487
- const replan = await replanStaleBatch(bot, cowResult, replanDepth, preBroadcastGuard, seamPollIntervalMs);
4926
+ const replan = await replanStaleBatch(bot, cowResult, replanDepth, preBroadcastGuard, seamPollIntervalMs, heldPlacementLease);
4488
4927
  if (replan.handled) {
4928
+ if (replan.placementTransferred)
4929
+ heldPlacementLease = null;
4489
4930
  return replan.result ?? { executed: false, hadRotation: false };
4490
4931
  }
4491
4932
  // Fall through: proceed with the current plan (bounded policy).
@@ -4507,6 +4948,8 @@ async function updateOrdersOnChainBatchCOWBody(bot, cowResult, replanDepth, seam
4507
4948
  popPushedWorkingGrid(bot, cowResult);
4508
4949
  return { executed: false, aborted: true, reason: 'SHUTDOWN_IN_PROGRESS', hadRotation: false };
4509
4950
  }
4951
+ // Placement was claimed at the top of the body, before the pre-batch
4952
+ // drain, and is held through this broadcast and the commit.
4510
4953
  bot._cowBroadcastInFlight = true;
4511
4954
  heldBroadcastSlot = true;
4512
4955
  // Re-validate the plan AFTER winning the broadcast slot, but only when
@@ -4530,8 +4973,10 @@ async function updateOrdersOnChainBatchCOWBody(bot, cowResult, replanDepth, seam
4530
4973
  `(${postWaitGuard.reason}); releasing the broadcast slot and re-planning from fresh master`, 'warn');
4531
4974
  bot._cowBroadcastInFlight = false;
4532
4975
  heldBroadcastSlot = false;
4533
- const replan = await replanStaleBatch(bot, cowResult, replanDepth, postWaitGuard, seamPollIntervalMs);
4976
+ const replan = await replanStaleBatch(bot, cowResult, replanDepth, postWaitGuard, seamPollIntervalMs, heldPlacementLease);
4534
4977
  if (replan.handled) {
4978
+ if (replan.placementTransferred)
4979
+ heldPlacementLease = null;
4535
4980
  return replan.result ?? { executed: false, hadRotation: false };
4536
4981
  }
4537
4982
  // No fill context for a re-plan (or the re-plan limit was
@@ -4541,6 +4986,10 @@ async function updateOrdersOnChainBatchCOWBody(bot, cowResult, replanDepth, seam
4541
4986
  popPushedWorkingGrid(bot, cowResult);
4542
4987
  return { executed: false, aborted: true, reason: 'SHUTDOWN_IN_PROGRESS', hadRotation: false };
4543
4988
  }
4989
+ if (!(await claimPlacement('post-stale-replan'))) {
4990
+ popPushedWorkingGrid(bot, cowResult);
4991
+ return { executed: false, aborted: true, reason: 'PLACEMENT_REGION_UNAVAILABLE', hadRotation: false };
4992
+ }
4544
4993
  bot._cowBroadcastInFlight = true;
4545
4994
  heldBroadcastSlot = true;
4546
4995
  }
@@ -4699,6 +5148,17 @@ async function updateOrdersOnChainBatchCOWBody(bot, cowResult, replanDepth, seam
4699
5148
  }
4700
5149
  bot._metrics.batchesExecuted++;
4701
5150
  clearPendingBroadcasts(bot.manager?._pendingBroadcasts);
5151
+ // Post-commit stranding sweep: a boundary move that left a live
5152
+ // order inside the band is a violation this batch created, and
5153
+ // the in-batch evacuation was refused as stale (see
5154
+ // forcedRetreatJustifiesEvacuation for the allowance).
5155
+ // Re-plan now that the committed geometry IS the live geometry:
5156
+ // the evacuation stamp validates and the rotation executes as a
5157
+ // single price update. No-op when the band is clean.
5158
+ try {
5159
+ scheduleGapEvacRepair(bot, 'post-commit');
5160
+ }
5161
+ catch { /* heal path is best-effort; never fail a committed batch */ }
4702
5162
  return { ...batchResult, executed: true, hadRotation: true };
4703
5163
  }
4704
5164
  else {
@@ -4729,7 +5189,7 @@ async function updateOrdersOnChainBatchCOWBody(bot, cowResult, replanDepth, seam
4729
5189
  // and we can commit the working grid directly, bypassing the expensive
4730
5190
  // reconciliation state machine. This handles the ~90% case where the
4731
5191
  // chain accepted the transaction but the response was lost.
4732
- if (err instanceof BroadcastUncertainError && getErrorField(err, 'partialOnChainState') !== true) {
5192
+ if (err instanceof BroadcastUncertainError && heldBroadcastSlot && getErrorField(err, 'partialOnChainState') !== true) {
4733
5193
  try {
4734
5194
  const confirmation = await pollChainForConfirmation(bot, opContexts);
4735
5195
  if (confirmation.allConfirmed) {
@@ -4827,11 +5287,19 @@ async function updateOrdersOnChainBatchCOWBody(bot, cowResult, replanDepth, seam
4827
5287
  throw err;
4828
5288
  }
4829
5289
  finally {
4830
- bot._batchInFlight--;
5290
+ if (batchInFlightRaised) {
5291
+ bot._batchInFlight = Math.max(0, bot._batchInFlight - 1);
5292
+ if (bot._batchInFlight === 0) {
5293
+ bot._batchInFlightStartedAt = 0;
5294
+ }
5295
+ }
4831
5296
  if (heldBroadcastSlot)
4832
5297
  bot._cowBroadcastInFlight = false;
4833
- bot._markGridActivity('batch end');
4834
- bot.manager.unlockOrders(idsToLock);
5298
+ releasePlacement();
5299
+ if (batchInFlightRaised)
5300
+ bot._markGridActivity('batch end');
5301
+ if (ordersLocked)
5302
+ bot.manager.unlockOrders(idsToLock);
4835
5303
  if (!bot._shuttingDown && bot._incomingFillQueue.length > 0) {
4836
5304
  bot._scheduleFillConsumerRestart(chainOrders);
4837
5305
  }
@@ -5527,6 +5995,26 @@ function runFinalPivotGate(bot, operations, opContexts, opts = {}) {
5527
5995
  stats.bypassed = (Number(stats.bypassed) || 0) + 1;
5528
5996
  continue;
5529
5997
  }
5998
+ // FORCED-RETREAT ALLOWANCE (mirrors the build loop): when the
5999
+ // live rails cannot host the gap band at ANY boundary, the
6000
+ // boundary move this batch commits is forced and only an
6001
+ // evacuation can resolve the geometry — keeping the op is the
6002
+ // violation-reducing choice, not a bypass of the guard's
6003
+ // intent. Without this the gate would drop exactly the
6004
+ // rotation the retreat needs and strand the order in the band.
6005
+ const gateConflict = forcedRetreatJustifiesEvacuation(bot.manager, bot.manager?._gapSlots, evacSrcId, evacDestId);
6006
+ if (gateConflict) {
6007
+ if (stats)
6008
+ stats.bypassed = (Number(stats.bypassed) || 0) + 1;
6009
+ try {
6010
+ bot.manager?.logger?.log?.(`[LAST-FILL-GUARD] Final gate keeping gap-evacuation ${ctx.kind} ${slotId ?? 'unknown'} ` +
6011
+ `despite live boundary ${bot.manager?.boundaryIdx}/gap ${bot.manager?._gapSlots}: the live rails ` +
6012
+ `already conflict (maxBuy=${gateConflict.maxBuyIdx} > minSell-gap-1=${gateConflict.upper}), so the ` +
6013
+ `retreat is forced`, 'warn');
6014
+ }
6015
+ catch { /* logging is best-effort */ }
6016
+ continue;
6017
+ }
5530
6018
  // Geometry went stale after op-building (e.g. a boundary
5531
6019
  // commit landed mid-batch). Dropping is the only fail-closed
5532
6020
  // option: the normal guard below only checks the fill price,