dexbot 1.6.18 → 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 (146) hide show
  1. package/CHANGELOG.md +68 -0
  2. package/README.md +24 -18
  3. package/analysis/README.md +117 -5
  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_dynamic_weight.js +2 -1
  9. package/dist/analysis/analyze_kalman.js +3 -2
  10. package/dist/analysis/analyze_liquidity.d.ts +17 -0
  11. package/dist/analysis/analyze_liquidity.js +336 -0
  12. package/dist/analysis/analyze_regime.js +3 -2
  13. package/dist/analysis/analyze_regime_windows.js +3 -2
  14. package/dist/analysis/analyze_volatility.js +3 -2
  15. package/dist/analysis/bot_key_utils.d.ts +44 -1
  16. package/dist/analysis/bot_key_utils.js +76 -7
  17. package/dist/analysis/fills_cache.js +2 -1
  18. package/dist/analysis/grid_correction_check.d.ts +1 -19
  19. package/dist/analysis/grid_correction_check.js +12 -30
  20. package/dist/analysis/liquidity/liquidity_chain.d.ts +66 -0
  21. package/dist/analysis/liquidity/liquidity_chain.js +178 -0
  22. package/dist/analysis/liquidity/liquidity_chart_generator.d.ts +19 -0
  23. package/dist/analysis/liquidity/liquidity_chart_generator.js +693 -0
  24. package/dist/analysis/liquidity/liquidity_depth_model.d.ts +166 -0
  25. package/dist/analysis/liquidity/liquidity_depth_model.js +258 -0
  26. package/dist/analysis/liquidity/liquidity_view.d.ts +53 -0
  27. package/dist/analysis/liquidity/liquidity_view.js +66 -0
  28. package/dist/analysis/resolve_bot_accounts.js +7 -6
  29. package/dist/analysis/trade_profitability.js +4 -2
  30. package/dist/analysis/tradingview/analyze_tradingview.js +2 -1
  31. package/dist/credential-daemon.js +13 -2
  32. package/dist/dexbot.js +77 -172
  33. package/dist/modules/account_bots.d.ts +11 -1
  34. package/dist/modules/account_orders.d.ts +12 -0
  35. package/dist/modules/account_orders.js +30 -0
  36. package/dist/modules/asset_lookup.d.ts +25 -0
  37. package/dist/modules/asset_lookup.js +75 -0
  38. package/dist/modules/bitshares-native/transport.js +24 -6
  39. package/dist/modules/bitshares_client.d.ts +24 -6
  40. package/dist/modules/bitshares_client.js +44 -7
  41. package/dist/modules/bot_settings.d.ts +2 -0
  42. package/dist/modules/chain_scan.d.ts +64 -0
  43. package/dist/modules/chain_scan.js +80 -0
  44. package/dist/modules/cli_colors.d.ts +5 -0
  45. package/dist/modules/cli_colors.js +7 -0
  46. package/dist/modules/cli_progress.d.ts +51 -0
  47. package/dist/modules/cli_progress.js +113 -0
  48. package/dist/modules/constants.d.ts +26 -3
  49. package/dist/modules/constants.js +63 -8
  50. package/dist/modules/dexbot_class.d.ts +1 -6
  51. package/dist/modules/dexbot_class.js +14 -0
  52. package/dist/modules/dexbot_cow_runtime.d.ts +97 -7
  53. package/dist/modules/dexbot_cow_runtime.js +634 -136
  54. package/dist/modules/dexbot_fill_runtime.js +132 -23
  55. package/dist/modules/dexbot_maintenance_runtime.js +242 -74
  56. package/dist/modules/dexbot_startup_runtime.js +32 -20
  57. package/dist/modules/dexbot_state_recovery.d.ts +33 -1
  58. package/dist/modules/dexbot_state_recovery.js +167 -5
  59. package/dist/modules/launcher/runtime_entry.d.ts +21 -1
  60. package/dist/modules/launcher/runtime_entry.js +36 -1
  61. package/dist/modules/liquidity_pool_ref.d.ts +32 -0
  62. package/dist/modules/liquidity_pool_ref.js +116 -0
  63. package/dist/modules/node_connect_policy.d.ts +8 -1
  64. package/dist/modules/node_connect_policy.js +18 -2
  65. package/dist/modules/node_flap_score.d.ts +87 -0
  66. package/dist/modules/node_flap_score.js +124 -0
  67. package/dist/modules/node_health_cache.d.ts +40 -3
  68. package/dist/modules/node_health_cache.js +66 -20
  69. package/dist/modules/node_manager.d.ts +101 -19
  70. package/dist/modules/node_manager.js +255 -65
  71. package/dist/modules/order/accounting.js +56 -7
  72. package/dist/modules/order/format.d.ts +24 -10
  73. package/dist/modules/order/format.js +30 -10
  74. package/dist/modules/order/grid.js +91 -13
  75. package/dist/modules/order/grid_reconcile.d.ts +5 -1
  76. package/dist/modules/order/grid_reconcile.js +333 -189
  77. package/dist/modules/order/grid_reconcile_internal.d.ts +38 -2
  78. package/dist/modules/order/grid_reconcile_internal.js +298 -100
  79. package/dist/modules/order/index.d.ts +19 -1
  80. package/dist/modules/order/lifecycle.d.ts +353 -0
  81. package/dist/modules/order/lifecycle.js +1109 -0
  82. package/dist/modules/order/manager.d.ts +273 -5
  83. package/dist/modules/order/manager.js +654 -48
  84. package/dist/modules/order/placement_lock.d.ts +102 -0
  85. package/dist/modules/order/placement_lock.js +222 -0
  86. package/dist/modules/order/strategy.js +13 -1
  87. package/dist/modules/order/sync_engine.d.ts +33 -1
  88. package/dist/modules/order/sync_engine.js +241 -73
  89. package/dist/modules/order/utils/math.d.ts +39 -1
  90. package/dist/modules/order/utils/math.js +83 -1
  91. package/dist/modules/order/utils/order.d.ts +181 -5
  92. package/dist/modules/order/utils/order.js +455 -9
  93. package/dist/modules/order/utils/system.d.ts +81 -1
  94. package/dist/modules/order/utils/system.js +331 -44
  95. package/dist/modules/order/utils/validate.d.ts +7 -1
  96. package/dist/modules/order/utils/validate.js +80 -3
  97. package/dist/modules/outcome.d.ts +54 -0
  98. package/dist/modules/outcome.js +51 -0
  99. package/dist/modules/settings_merge.d.ts +4 -0
  100. package/dist/modules/settings_merge.js +9 -0
  101. package/dist/modules/timing_policy.d.ts +82 -0
  102. package/dist/modules/timing_policy.js +244 -0
  103. package/dist/modules/types.d.ts +137 -0
  104. package/dist/modules/utils/asset_symbols.d.ts +13 -2
  105. package/dist/modules/utils/asset_symbols.js +21 -2
  106. package/dist/scripts/analysis_runner.d.ts +10 -0
  107. package/dist/scripts/analysis_runner.js +33 -0
  108. package/dist/scripts/analyze-credit.js +54 -17
  109. package/dist/scripts/analyze-orders.d.ts +4 -2
  110. package/dist/scripts/analyze-orders.js +115 -35
  111. package/dist/scripts/asset.d.ts +26 -0
  112. package/dist/scripts/asset.js +165 -0
  113. package/dist/scripts/bts_distribution.d.ts +1 -0
  114. package/dist/scripts/bts_distribution.js +194 -142
  115. package/dist/scripts/chart_command.d.ts +1 -1
  116. package/dist/scripts/chart_command.js +60 -86
  117. package/dist/scripts/chart_targets.d.ts +18 -0
  118. package/dist/scripts/chart_targets.js +48 -0
  119. package/dist/scripts/dw.js +1 -1
  120. package/dist/scripts/liquid.d.ts +24 -0
  121. package/dist/scripts/liquid.js +134 -0
  122. package/dist/scripts/rich_list.js +12 -16
  123. package/dist/scripts/test-credit-renewal.js +12 -5
  124. package/dist/scripts/token_distribution.d.ts +2 -0
  125. package/dist/scripts/token_distribution.js +132 -108
  126. package/dist/scripts/tv.js +1 -1
  127. package/dist/scripts/verify-browser-bundle.js +18 -0
  128. package/docs/COW_INVARIANTS.md +69 -10
  129. package/docs/DEXBOT_COMPARISON.md +6 -6
  130. package/docs/EVOLUTION.md +10 -8
  131. package/docs/FUND_MOVEMENT_AND_ACCOUNTING.md +1 -1
  132. package/docs/GRID_PRICE_INVARIANT.md +5 -2
  133. package/docs/GRID_RECONCILE.md +36 -11
  134. package/docs/LOGGING.md +50 -4
  135. package/docs/ORDER_ENGINE_POST_1.0_RETROSPECTIVE.md +177 -27
  136. package/docs/README.md +23 -2
  137. package/docs/WORKFLOW.md +12 -4
  138. package/docs/architecture.md +42 -5
  139. package/docs/developer_guide.md +25 -8
  140. package/modules/README.md +8 -1
  141. package/package.json +3 -1
  142. package/scripts/README.md +39 -7
  143. package/scripts/pdev.sh +14 -1
  144. package/scripts/pmain.sh +23 -8
  145. package/scripts/ptest.sh +7 -0
  146. 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.
@@ -1777,15 +1931,22 @@ function recordRestampBuyAboveAnchor(bot, price, type) {
1777
1931
  try {
1778
1932
  if (type !== ORDER_TYPES.BUY)
1779
1933
  return false;
1780
- if (bot?.manager?._lastFilledType !== ORDER_TYPES.BUY)
1934
+ // ONE reference, the same one the guard judges against: the raw live
1935
+ // fields miss the market-row fallback (a rebuild) and would still count
1936
+ // against an expired pivot that no longer gates anything.
1937
+ const reference = resolveLastFillGuardReference(bot?.manager);
1938
+ if (reference.type !== ORDER_TYPES.BUY)
1781
1939
  return false;
1782
- const pivot = Number(bot?.manager?._lastFilledPrice);
1940
+ const pivot = Number(reference.price);
1783
1941
  const p = Number(price);
1784
1942
  if (!Number.isFinite(pivot) || pivot <= 0 || !Number.isFinite(p) || p <= 0)
1785
1943
  return false;
1786
1944
  // At or above the pivot is the re-stamp shape; a lower BUY is the
1787
- // legitimate rotation target (I3) and is not counted.
1788
- if (p < pivot)
1945
+ // legitimate rotation target (I3) and is not counted. SAME LEVEL counts
1946
+ // too: a slot level and its execution price differ by rounding (slot
1947
+ // 1310.046339 filled at 1310.060435), so a bare `p < pivot` misses
1948
+ // exactly the shape this counter exists to expose.
1949
+ if (p < pivot && !math.pricesWithinHalfStep(p, pivot, bot?.manager?.config?.incrementPercent))
1789
1950
  return false;
1790
1951
  if (bot._metrics)
1791
1952
  bot._metrics.restampBuyAboveAnchorCount = (bot._metrics.restampBuyAboveAnchorCount || 0) + 1;
@@ -2470,6 +2631,42 @@ function toRefillSlotIdSet(refillSlotIds) {
2470
2631
  }
2471
2632
  return set;
2472
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
+ }
2473
2670
  /**
2474
2671
  * Boundary-hold decision for guard-skipped refills.
2475
2672
  * When a skipped slot is one of the plan's refill slots — a hole-CREATE that
@@ -2480,11 +2677,30 @@ function toRefillSlotIdSet(refillSlotIds) {
2480
2677
  * Unrelated vetoes (skip ids outside the refill set) never pin geometry.
2481
2678
  * The grid still commits; only the boundary value is held (same discipline
2482
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.
2483
2684
  */
2484
- function resolveRefillBoundaryHold(workingBoundary, committedBoundary, skippedUpdateSlotIds, clampedUpdateSlotIds, refillSlotIds, skippedCreateSlotIds = undefined) {
2685
+ function resolveRefillBoundaryHold(workingBoundary, committedBoundary, skippedUpdateSlotIds, clampedUpdateSlotIds, refillSlotIds, skippedCreateSlotIds = undefined, refillSide = null) {
2485
2686
  const refills = toRefillSlotIdSet(refillSlotIds);
2486
2687
  const heldRefillSlotIds = [];
2487
- 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) {
2488
2704
  const seen = new Set();
2489
2705
  const skipCollections = skippedCreateSlotIds !== undefined
2490
2706
  ? [skippedUpdateSlotIds, clampedUpdateSlotIds, skippedCreateSlotIds]
@@ -2744,13 +2960,24 @@ export function deriveRailBoundary(manager, opContexts, desiredBoundary, gapSlot
2744
2960
  if (dense) {
2745
2961
  const check = math.validatePersistedBoundary(next, slotsForGate, gap, vacatedSlotIds.size > 0 ? { allowInBandSlotIds: vacatedSlotIds } : undefined);
2746
2962
  if (!check || check.ok !== true) {
2747
- return {
2748
- boundary: null,
2749
- maxBuyIdx,
2750
- minSellIdx,
2751
- clamped: false,
2752
- reason: `unsafe: ${check?.reason ?? 'unknown'}${check?.detail ? ` (${check.detail})` : ''}`
2753
- };
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
+ }
2754
2981
  }
2755
2982
  }
2756
2983
  }
@@ -2859,12 +3086,57 @@ function restoreSkippedUpdateSlotsInWorkingGrid(bot, workingGrid, skippedSlotIds
2859
3086
  * @param {Object} cowResult - The stale plan result
2860
3087
  * @param {number} replanDepth - Recursion depth (0 = first attempt)
2861
3088
  * @param {Object} preBroadcastGuard - The failed evaluateCommit result
2862
- * @returns {Promise<{handled: boolean, result?: Object}>} handled=true when the
3089
+ * @returns {Promise<{handled: boolean, result?: Object, placementTransferred?: boolean}>} handled=true when the
2863
3090
  * batch was resolved by the re-plan (fresh plan executed, or stale plan
2864
3091
  * skipped as already-consistent); handled=false when the caller must proceed
2865
- * 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.
2866
3094
  */
2867
- 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
+ }
2868
3140
  const canReplan = replanDepth < STALE_PLAN_REPLAN_LIMIT
2869
3141
  && Array.isArray(cowResult.fills) && cowResult.fills.length > 0;
2870
3142
  if (!canReplan) {
@@ -2885,6 +3157,10 @@ async function replanStaleBatch(bot, cowResult, replanDepth, preBroadcastGuard,
2885
3157
  const hadPushedGrid = cowResult?._workingGridPushed === true;
2886
3158
  popPushedWorkingGrid(bot, cowResult);
2887
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);
2888
3164
  try {
2889
3165
  // Restore the boundary-shift budget consumed by the abandoned plan:
2890
3166
  // it was built from the same fills and never shipped, so the re-plan
@@ -2894,6 +3170,14 @@ async function replanStaleBatch(bot, cowResult, replanDepth, preBroadcastGuard,
2894
3170
  if (bot.manager?._boundaryShiftBudgetBase != null) {
2895
3171
  bot.manager._boundaryShiftBudget = bot.manager._boundaryShiftBudgetBase;
2896
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
+ }
2897
3181
  if (typeof bot.manager.performSafeRebalance === 'function') {
2898
3182
  // skipBroadcastWait: this frame is itself inside the executor's
2899
3183
  // startBroadcasting() region — waiting on the flag we hold would
@@ -2917,10 +3201,17 @@ async function replanStaleBatch(bot, cowResult, replanDepth, preBroadcastGuard,
2917
3201
  // (duplicate orders). The recursion's guard will then abort +
2918
3202
  // reconcile instead.
2919
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;
2920
3209
  return {
2921
3210
  handled: true,
3211
+ placementTransferred: true,
2922
3212
  result: await updateOrdersOnChainBatchCOW(bot, replanned, {
2923
3213
  replanDepth: replanDepth + 1,
3214
+ placementLease,
2924
3215
  // Carry the seam through the recursion explicitly: the inner
2925
3216
  // frame re-sets and re-restores it, so it must not depend on
2926
3217
  // the outer frame's side-channel value surviving.
@@ -3013,9 +3304,20 @@ async function waitForCowBroadcastSingleFlight(bot, label) {
3013
3304
  * @returns {number}
3014
3305
  */
3015
3306
  function plannedUpdateSize(action) {
3016
- return Number.isFinite(Number(action.newSize))
3307
+ const planned = Number.isFinite(Number(action.newSize))
3017
3308
  ? Number(action.newSize)
3018
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;
3019
3321
  }
3020
3322
  /**
3021
3323
  * Post-fill size invariant for COW UPDATE ops: a partially-filled order
@@ -3040,10 +3342,16 @@ function clampPostFillUpdateSize(bot, masterOrder, newSize, action) {
3040
3342
  if (!Number.isFinite(target) || !Number.isFinite(booked) || booked <= 0)
3041
3343
  return target;
3042
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);
3043
3351
  bot.manager.logger.log(`[COW] Post-fill size clamp for ${action?.id || masterOrder?.id}: slot is PARTIAL with ` +
3044
3352
  `booked remaining ${Format.formatAmount(booked)} but the plan targets ` +
3045
3353
  `${Format.formatAmount(target)} — clamping to booked remaining. ` +
3046
- `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');
3047
3355
  return booked;
3048
3356
  }
3049
3357
  return target;
@@ -3067,7 +3375,13 @@ async function runPreBroadcastGuards(bot, cowResult) {
3067
3375
  const chainOrderCandidates = Array.isArray(bot.manager?._lastUnmatchedChainOrders)
3068
3376
  ? bot.manager._lastUnmatchedChainOrders
3069
3377
  : [];
3070
- 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
+ })));
3071
3385
  if (!createSlotValidation.isValid) {
3072
3386
  for (const violation of createSlotValidation.violations) {
3073
3387
  let reason;
@@ -3087,6 +3401,12 @@ async function runPreBroadcastGuards(bot, cowResult) {
3087
3401
  case 'create_price_invalid':
3088
3402
  reason = `broadcast price is not a finite number`;
3089
3403
  break;
3404
+ case 'slot_create_uncertain':
3405
+ reason = `an earlier CREATE here has an unknown outcome (createUncertain)`;
3406
+ break;
3407
+ case 'pending_broadcast_collision':
3408
+ reason = `a pending uncertain CREATE already targets this slot/price`;
3409
+ break;
3090
3410
  default:
3091
3411
  reason = `existing orderId=${violation.currentOrderId}`;
3092
3412
  }
@@ -3424,7 +3744,7 @@ async function updateOrdersOnChainBatchCOW(bot, cowResult, options = {}) {
3424
3744
  // recursion, throws) funnels through the body() finally below, so no exit
3425
3745
  // can leak bot._testPollIntervalMs onto the bot.
3426
3746
  try {
3427
- return await updateOrdersOnChainBatchCOWBody(bot, cowResult, replanDepth, seamPollIntervalMs ?? undefined);
3747
+ return await updateOrdersOnChainBatchCOWBody(bot, cowResult, replanDepth, seamPollIntervalMs ?? undefined, options?.placementLease ?? null);
3428
3748
  }
3429
3749
  finally {
3430
3750
  // Restore only when this frame actually wrote a seam. The entry
@@ -3441,7 +3761,7 @@ async function updateOrdersOnChainBatchCOW(bot, cowResult, options = {}) {
3441
3761
  }
3442
3762
  }
3443
3763
  }
3444
- async function updateOrdersOnChainBatchCOWBody(bot, cowResult, replanDepth, seamPollIntervalMs) {
3764
+ async function updateOrdersOnChainBatchCOWBody(bot, cowResult, replanDepth, seamPollIntervalMs, inheritedPlacementLease = null) {
3445
3765
  bot._currentCycleId = (Number.isFinite(Number(bot._currentCycleId)) ? Number(bot._currentCycleId) : 0) + 1;
3446
3766
  const { workingGrid, workingIndexes, workingBoundary, actions } = cowResult;
3447
3767
  // Boundary-hold value: computed pre-broadcast after the skip-restore and
@@ -3474,93 +3794,49 @@ async function updateOrdersOnChainBatchCOWBody(bot, cowResult, replanDepth, seam
3474
3794
  popPushedWorkingGrid(bot, cowResult);
3475
3795
  return { executed: true, hadRotation: false };
3476
3796
  }
3477
- // Single-flight COW broadcast guard (entry check): never broadcast two
3478
- // batches concurrently. Overlapping broadcasts plan from the same base
3479
- // grid version; when the first commits it bumps _gridVersion, so the
3480
- // second's commit is refused (base-version mismatch) -> adopt-from-chain
3481
- // -> snapshot reload that can drop the adopted order and produce an
3482
- // orphan fill. This entry wait is an optimization; the authoritative
3483
- // atomic check-and-set happens right before the broadcast below.
3484
- if (await waitForCowBroadcastSingleFlight(bot, 'entry')) {
3485
- popPushedWorkingGrid(bot, cowResult);
3486
- return { executed: false, aborted: true, reason: 'SHUTDOWN_IN_PROGRESS', hadRotation: false };
3487
- }
3488
- // DRAIN PENDING CORRECTIONS before the batch is planned/broadcast.
3489
- // Cancel-only corrections (duplicate-price orphans) queued by an earlier
3490
- // sync must not sit while batches run back-to-back (startup create
3491
- // groups, fill bursts) — with the open-orders sync loop disabled they
3492
- // would otherwise linger indefinitely, keep blocking same-level CREATEs,
3493
- // and risk cancelling the wrong side of a duplicate later. Draining here
3494
- // also keeps this batch's collision checks (chain_orphan_collision)
3495
- // honest: orphaned chain orders already queued for cancellation are
3496
- // resolved before the plan validates its CREATE targets against them.
3497
- const pendingCorrectionCount = Array.isArray(bot.manager?.ordersNeedingPriceCorrection)
3498
- ? bot.manager.ordersNeedingPriceCorrection.length
3499
- : 0;
3500
- if (pendingCorrectionCount > 0 && !bot._shuttingDown) {
3501
- try {
3502
- bot.manager.logger.log(`[COW] Draining ${pendingCorrectionCount} pending correction(s) before batch`, 'info');
3503
- const drainResult = await orderUtils.correctAllPriceMismatches(bot.manager, bot.account, bot.privateKey, chainOrders);
3504
- if (drainResult?.failed > 0) {
3505
- bot.manager.logger.log(`[COW] ${drainResult.failed} correction(s) failed pre-batch` +
3506
- ((drainResult.staleDropped ?? 0) > 0 ? `, ${drainResult.staleDropped} stale dropped` : '') +
3507
- `; remaining entries retry on next sync/maintenance tick`, 'warn');
3508
- }
3509
- else if ((drainResult?.staleDropped ?? 0) > 0) {
3510
- bot.manager.logger.log(`[COW] Pre-batch drain resolved, ${drainResult.staleDropped} stale correction(s) dropped`, 'info');
3511
- }
3512
- }
3513
- catch (drainErr) {
3514
- 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;
3515
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 };
3516
3832
  }
3517
- // Pre-broadcast guard chain (create-slot validation, recovery-exhausted
3518
- // block, pending/unmatched guards, crossed-book gate) — see
3519
- // runPreBroadcastGuards. Refusals already popped the working grid.
3520
- const guards = await runPreBroadcastGuards(bot, cowResult);
3521
- if (!guards.proceed)
3522
- return guards.result ?? { executed: false, hadRotation: false };
3523
- const { crossingCandidates = [], intraBatchCandidates = [] } = guards;
3524
- 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.
3525
3838
  const operations = [];
3526
3839
  const opContexts = [];
3527
- const skippedUpdateSlotIds = new Set();
3528
- let skippedUpdateCount = 0;
3529
- // Guard-skipped CREATE slot ids (hole-refills never placed). Fed to the
3530
- // boundary-hold intersect alongside the UPDATE sets above — a skipped
3531
- // refill CREATE strands its rail hole exactly like a restored UPDATE.
3532
- const skippedCreateSlotIds = new Set();
3533
- // Per-batch LAST-FILL-GUARD disposition counters. Per-action pass lines
3534
- // would spam big batches, so the guard emits one batch summary instead
3535
- // (see the summary after the action loop below).
3536
- const lastFillGuardStats = { checked: 0, passed: 0, skipped: 0, bypassed: 0, pivotOffGrid: 0 };
3537
- // One crossing-CREATE observation per batch (see warnCrossingCreate).
3538
- let crossingCreateWarned = false;
3539
- // Fingerprints of the pending-broadcast entries recorded by THIS batch's
3540
- // op-building (both CREATE paths). The final pivot gate's compaction
3541
- // remaps these entries' stored opIndex/ctxIndex to their post-drop
3542
- // positions; entry.batchId cannot discriminate batches (always null in
3543
- // production), so the fingerprint set is the ownership marker.
3544
- const batchPendingFps = new Set();
3545
- // Per-batch GRID-PRICE-INVARIANT counters (BLOCKING). Tracks emitted prices
3546
- // that are not the genesis level for their slot — such emissions are
3547
- // rejected, not placed. One batch summary.
3548
- const gridPriceInvariantStats = { checked: 0, violated: 0, unchecked: 0 };
3549
- // Whether any guard check in this batch refreshed the pivot from
3550
- // still-queued fills — reported in the batch summary so a pivot change
3551
- // that altered a guard decision is visible at info, not just debug.
3552
- let lastFillGuardPivotRefreshed = false;
3553
- // Slots whose size-update op was broadcast with a post-fill-clamped
3554
- // target: the working grid still holds the planned (larger) size, so the
3555
- // slots are re-synced from master before commit to keep the committed
3556
- // books equal to the broadcast chain amounts.
3557
- const clampedUpdateSlotIds = new Set();
3558
- // orderId -> operations index of its cancel op. A crossing re-pricing
3559
- // update is only safe when the crossed order's cancel was already queued
3560
- // at an earlier position: ops broadcast in gap-slot-sized chunks,
3561
- // so an earlier index means the cancel confirms on chain (same or earlier
3562
- // chunk, applied sequentially) before the crossing order lands.
3563
- const cancelOpIndexByOrderId = new Map();
3564
3840
  const idsToLock = new Set();
3565
3841
  for (const action of actions) {
3566
3842
  if (action.type === COW_ACTIONS.CANCEL && action.orderId) {
@@ -3577,17 +3853,153 @@ async function updateOrdersOnChainBatchCOWBody(bot, cowResult, replanDepth, seam
3577
3853
  idsToLock.add(action.id);
3578
3854
  }
3579
3855
  }
3580
- bot.manager.lockOrders(idsToLock);
3581
- // Ownership of the single-flight broadcast slot for THIS frame. Only the
3582
- // frame that claimed the slot (set bot._cowBroadcastInFlight = true below)
3583
- // may clear it in the finally. A frame that waits at the entry check or
3584
- // early-returns after planning (fund validation, create-slot abort, op-prep
3585
- // throw) never owns the slot and MUST NOT clear it — an unconditional clear
3586
- // would wipe a concurrent batch's in-flight flag and let a third batch
3587
- // broadcast on top of it (the exact overlap this guard prevents).
3588
3856
  let heldBroadcastSlot = false;
3857
+ let batchInFlightRaised = false;
3858
+ let ordersLocked = false;
3589
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
+ }
3590
4000
  bot._batchInFlight++;
4001
+ batchInFlightRaised = true;
4002
+ bot._batchInFlightStartedAt = Date.now();
3591
4003
  bot._markGridActivity('batch start');
3592
4004
  bot.manager._setRebalanceState(REBALANCE_STATES.BROADCASTING);
3593
4005
  bot.manager.startBroadcasting();
@@ -3920,9 +4332,27 @@ async function updateOrdersOnChainBatchCOWBody(bot, cowResult, replanDepth, seam
3920
4332
  // live probe below instead of bypassing outright.
3921
4333
  const stampValid = isEvacuationStampStillValid(bot.manager?.boundaryIdx, bot.manager?._gapSlots, action.id, action.newGridId, orderType);
3922
4334
  if (!stampValid) {
3923
- stampUsable = false;
3924
- bot.manager.logger.log(`[LAST-FILL-GUARD] Stamped evacuation for ${action.id} -> ${action.newGridId} is stale under live ` +
3925
- `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
+ }
3926
4356
  }
3927
4357
  }
3928
4358
  if (stampUsable) {
@@ -3995,10 +4425,26 @@ async function updateOrdersOnChainBatchCOWBody(bot, cowResult, replanDepth, seam
3995
4425
  // Non-finite live geometry keeps plan-build authority
3996
4426
  // (isEvacuationStampStillValid returns true there).
3997
4427
  let staleSkipReason = null;
3998
- if (!isEvacuationStampStillValid(bot.manager?.boundaryIdx, bot.manager?._gapSlots, action.id, action.newGridId, orderType)) {
3999
- 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
+ }
4000
4446
  }
4001
- else {
4447
+ if (staleSkipReason === null) {
4002
4448
  const evacCheck = isEvacuationRotationAllowed(oldPrice, oldSize, newPrice, newSize, orderType, sidePrecision);
4003
4449
  if (evacCheck.allowed) {
4004
4450
  bypassedEvacuation = true;
@@ -4380,8 +4826,11 @@ async function updateOrdersOnChainBatchCOWBody(bot, cowResult, replanDepth, seam
4380
4826
  }
4381
4827
  // BOUNDARY HOLD: skipped refill slots strand empty rail holes past the
4382
4828
  // planned boundary (commit gate skips empties, self-legalizing) — hold
4383
- // the committed boundary; intersect-only, grid still commits.
4384
- 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));
4385
4834
  effectiveBoundary = refillHold.effectiveBoundary;
4386
4835
  boundaryHeld = refillHold.heldRefillSlotIds.length > 0;
4387
4836
  if (refillHold.heldRefillSlotIds.length > 0) {
@@ -4474,8 +4923,10 @@ async function updateOrdersOnChainBatchCOWBody(bot, cowResult, replanDepth, seam
4474
4923
  });
4475
4924
  if (!preBroadcastGuard.canCommit) {
4476
4925
  // Bounded re-plan + proceed — policy documented on replanStaleBatch.
4477
- const replan = await replanStaleBatch(bot, cowResult, replanDepth, preBroadcastGuard, seamPollIntervalMs);
4926
+ const replan = await replanStaleBatch(bot, cowResult, replanDepth, preBroadcastGuard, seamPollIntervalMs, heldPlacementLease);
4478
4927
  if (replan.handled) {
4928
+ if (replan.placementTransferred)
4929
+ heldPlacementLease = null;
4479
4930
  return replan.result ?? { executed: false, hadRotation: false };
4480
4931
  }
4481
4932
  // Fall through: proceed with the current plan (bounded policy).
@@ -4497,6 +4948,8 @@ async function updateOrdersOnChainBatchCOWBody(bot, cowResult, replanDepth, seam
4497
4948
  popPushedWorkingGrid(bot, cowResult);
4498
4949
  return { executed: false, aborted: true, reason: 'SHUTDOWN_IN_PROGRESS', hadRotation: false };
4499
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.
4500
4953
  bot._cowBroadcastInFlight = true;
4501
4954
  heldBroadcastSlot = true;
4502
4955
  // Re-validate the plan AFTER winning the broadcast slot, but only when
@@ -4520,8 +4973,10 @@ async function updateOrdersOnChainBatchCOWBody(bot, cowResult, replanDepth, seam
4520
4973
  `(${postWaitGuard.reason}); releasing the broadcast slot and re-planning from fresh master`, 'warn');
4521
4974
  bot._cowBroadcastInFlight = false;
4522
4975
  heldBroadcastSlot = false;
4523
- const replan = await replanStaleBatch(bot, cowResult, replanDepth, postWaitGuard, seamPollIntervalMs);
4976
+ const replan = await replanStaleBatch(bot, cowResult, replanDepth, postWaitGuard, seamPollIntervalMs, heldPlacementLease);
4524
4977
  if (replan.handled) {
4978
+ if (replan.placementTransferred)
4979
+ heldPlacementLease = null;
4525
4980
  return replan.result ?? { executed: false, hadRotation: false };
4526
4981
  }
4527
4982
  // No fill context for a re-plan (or the re-plan limit was
@@ -4531,6 +4986,10 @@ async function updateOrdersOnChainBatchCOWBody(bot, cowResult, replanDepth, seam
4531
4986
  popPushedWorkingGrid(bot, cowResult);
4532
4987
  return { executed: false, aborted: true, reason: 'SHUTDOWN_IN_PROGRESS', hadRotation: false };
4533
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
+ }
4534
4993
  bot._cowBroadcastInFlight = true;
4535
4994
  heldBroadcastSlot = true;
4536
4995
  }
@@ -4689,6 +5148,17 @@ async function updateOrdersOnChainBatchCOWBody(bot, cowResult, replanDepth, seam
4689
5148
  }
4690
5149
  bot._metrics.batchesExecuted++;
4691
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 */ }
4692
5162
  return { ...batchResult, executed: true, hadRotation: true };
4693
5163
  }
4694
5164
  else {
@@ -4719,7 +5189,7 @@ async function updateOrdersOnChainBatchCOWBody(bot, cowResult, replanDepth, seam
4719
5189
  // and we can commit the working grid directly, bypassing the expensive
4720
5190
  // reconciliation state machine. This handles the ~90% case where the
4721
5191
  // chain accepted the transaction but the response was lost.
4722
- if (err instanceof BroadcastUncertainError && getErrorField(err, 'partialOnChainState') !== true) {
5192
+ if (err instanceof BroadcastUncertainError && heldBroadcastSlot && getErrorField(err, 'partialOnChainState') !== true) {
4723
5193
  try {
4724
5194
  const confirmation = await pollChainForConfirmation(bot, opContexts);
4725
5195
  if (confirmation.allConfirmed) {
@@ -4817,11 +5287,19 @@ async function updateOrdersOnChainBatchCOWBody(bot, cowResult, replanDepth, seam
4817
5287
  throw err;
4818
5288
  }
4819
5289
  finally {
4820
- 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
+ }
4821
5296
  if (heldBroadcastSlot)
4822
5297
  bot._cowBroadcastInFlight = false;
4823
- bot._markGridActivity('batch end');
4824
- bot.manager.unlockOrders(idsToLock);
5298
+ releasePlacement();
5299
+ if (batchInFlightRaised)
5300
+ bot._markGridActivity('batch end');
5301
+ if (ordersLocked)
5302
+ bot.manager.unlockOrders(idsToLock);
4825
5303
  if (!bot._shuttingDown && bot._incomingFillQueue.length > 0) {
4826
5304
  bot._scheduleFillConsumerRestart(chainOrders);
4827
5305
  }
@@ -5517,6 +5995,26 @@ function runFinalPivotGate(bot, operations, opContexts, opts = {}) {
5517
5995
  stats.bypassed = (Number(stats.bypassed) || 0) + 1;
5518
5996
  continue;
5519
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
+ }
5520
6018
  // Geometry went stale after op-building (e.g. a boundary
5521
6019
  // commit landed mid-batch). Dropping is the only fail-closed
5522
6020
  // option: the normal guard below only checks the fill price,