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
@@ -4,7 +4,7 @@
4
4
  * Extracted from grid_reconcile.ts for file-size management.
5
5
  * All helpers are prefixed with _ and are NOT part of the public API.
6
6
  */
7
- import type { OrderManagerLike, ManagedOrder, ChainOrder, ParsedChainOrder, ChainOrdersLike, LoggerLike, UnknownRecord, StartupCreatePlan, StartupUpdatePlan, StartupCancelPlan, OrderType } from '../types.js';
7
+ import type { OrderManagerLike, ManagedOrder, ChainOrder, ParsedChainOrder, ChainOrdersLike, AssetPair, LoggerLike, UnknownRecord, StartupCreatePlan, StartupUpdatePlan, StartupCancelPlan, OrderType } from '../types.js';
8
8
  interface PreparedStartupUpdate {
9
9
  plan: StartupUpdatePlan;
10
10
  chainOrderId: string;
@@ -149,6 +149,42 @@ declare function _executePlannedStartupCreates({ createPlans, chainOrders, accou
149
149
  manager: OrderManagerLike;
150
150
  dryRun: boolean;
151
151
  }): Promise<Set<string>>;
152
+ /**
153
+ * Partition the unmatched chain orders of one side into an UPDATE pool and a
154
+ * dust-surplus CANCEL pool.
155
+ *
156
+ * Policy (single implementation used by the startup reconcile):
157
+ * - dust = size below the per-slot single-dust threshold (same definition the
158
+ * partial-dust health check uses);
159
+ * - when the NON-dust orders alone can cover every slot this side needs, dust
160
+ * orders are surplus -> CANCEL only. They are never grown into a full slot
161
+ * and no replacement is created for them (there are already enough real
162
+ * orders to cover the target);
163
+ * - otherwise dust stays eligible to fill the remaining slots.
164
+ *
165
+ * Extracted so the policy is unit-testable and cannot drift between the
166
+ * startup reconcile and any future caller.
167
+ * @param {Object} input
168
+ * @param {Array} input.sortedUnmatched - Unmatched chain orders, pre-sorted by price
169
+ * @param {Array} input.desiredSlots - Target slots this side wants to activate
170
+ * @param {Object} input.assets - Asset metadata (precisions)
171
+ * @param {number} [input.dustThresholdPercent] - Dust threshold percentage
172
+ * @returns {Object} { updatePool, dustSurplus, nonDustCount, dustCount, referenceIdeal, dustThreshold, enoughNonDust }
173
+ */
174
+ declare function _classifyStartupOrderReuse({ sortedUnmatched, desiredSlots, assets, dustThresholdPercent, }: {
175
+ sortedUnmatched: ChainOrder[];
176
+ desiredSlots: ManagedOrder[];
177
+ assets: AssetPair;
178
+ dustThresholdPercent?: number;
179
+ }): {
180
+ updatePool: ChainOrder[];
181
+ dustSurplus: ChainOrder[];
182
+ nonDustCount: number;
183
+ dustCount: number;
184
+ referenceIdeal: number;
185
+ dustThreshold: number;
186
+ enoughNonDust: boolean;
187
+ };
152
188
  declare function _reconcileStartupSide({ orderType, targetCount, chainSideOrders, unmatchedSideOrders, manager, chainOrders, account, privateKey, dryRun, plannedCreates, plannedUpdates, plannedCancels, planOnly, }: {
153
189
  orderType: OrderType;
154
190
  targetCount: number;
@@ -166,5 +202,5 @@ declare function _reconcileStartupSide({ orderType, targetCount, chainSideOrders
166
202
  }): Promise<{
167
203
  chainCount: number;
168
204
  }>;
169
- export { _countActiveOnGrid, _pickVirtualSlotsToActivate, _createOrderFromGrid, _prepareStartupUpdatePlan, _cancelChainOrder, _recoverStartupSyncFailure, _refreshStartupUpdatePlans, _executeStartupUpdateBatch, _executeStartupSequentialUpdateFallback, _executeStartupCreateGroupBatch, _createStartupOrderWithHandling, _executePlannedStartupCreates, _reconcileStartupSide };
205
+ export { _countActiveOnGrid, _pickVirtualSlotsToActivate, _createOrderFromGrid, _prepareStartupUpdatePlan, _cancelChainOrder, _recoverStartupSyncFailure, _refreshStartupUpdatePlans, _executeStartupUpdateBatch, _executeStartupSequentialUpdateFallback, _executeStartupCreateGroupBatch, _createStartupOrderWithHandling, _executePlannedStartupCreates, _reconcileStartupSide, _classifyStartupOrderReuse };
170
206
  //# sourceMappingURL=grid_reconcile_internal.d.ts.map
@@ -6,8 +6,8 @@
6
6
  */
7
7
  import { ORDER_TYPES, ORDER_STATES, TIMING, BTS_PRECISION } from '../constants.js';
8
8
  import { readOpenOrdersGuarded } from '../chain_orders.js';
9
- import { getMinOrderSize, getAssetFees, getAssetFeesSafe, blockchainToFloat, findCrossedOrder, resolveGapBand, isSlotInRail, pricesOnSameLevel, pricesWithinHalfStep, sameGridLevel } from './utils/math.js';
10
- import { isOrderPlaced, parseChainOrder, buildCreateOrderArgs, buildOutsideInPairGroups, extractBatchOperationResults, chainOrderMatchesSlotWithTolerance, buildCrossingCheckCandidates, isCrossingCheckCandidate, getSideBudget, calculateBudgetedSizes, getCoreOrdersTotal, convertToSpreadPlaceholder, isOrderGoneErrorMessage, clearDuplicateOrphanDetection, resolveEdgeCount, resolveLiveEdgeAnchorPrice, edgeOrderIdSet, compareEdge, reportGridPriceInvariant, liveWindowIdSet } from './utils/order.js';
9
+ import { getMinOrderSize, getSingleDustThreshold, getAssetFees, getAssetFeesSafe, blockchainToFloat, findCrossedOrder, resolveGapBand, resolveGapSlots, isSlotInRail, pricesOnSameLevel, pricesWithinHalfStep, sameGridLevel } from './utils/math.js';
10
+ import { isOrderPlaced, parseChainOrder, buildCreateOrderArgs, buildOutsideInPairGroups, extractBatchOperationResults, chainOrderMatchesSlotWithTolerance, buildCrossingCheckCandidates, isCrossingCheckCandidate, getSideBudget, calculateBudgetedSizes, getCoreOrdersTotal, convertToSpreadPlaceholder, isOrderGoneErrorMessage, clearDuplicateOrphanDetection, resolveEdgeCount, resolveLiveEdgeAnchorPrice, edgeOrderIdSet, compareEdge, reportGridPriceInvariant, liveWindowIdSet, getLivePendingBroadcasts } from './utils/order.js';
11
11
  import { parseSlotIndex } from './utils/slot.js';
12
12
  import { resolveAccountRef, isLastFillPlacementBlocked, hasUnsettledFills } from './utils/system.js';
13
13
  import * as Format from './format.js';
@@ -490,6 +490,45 @@ async function _createOrderFromGrid({ chainOrders, account, privateKey, manager,
490
490
  manager.logger?.log?.(`[_createOrderFromGrid] SKIP: Slot ${gridOrder.id} already has orderId ${currentSlot.orderId}`, 'warn');
491
491
  return null;
492
492
  }
493
+ // PENDING-BROADCAST GUARD: an earlier uncertain CREATE for this slot may
494
+ // already be live on chain but not yet adopted. The COW pre-broadcast guard
495
+ // blocks the whole CREATE batch for this case; the reconcile/startup path
496
+ // must not re-create the same slot OR the same price level either (same
497
+ // duplicate class).
498
+ //
499
+ // Liveness comes from the shared authority (`getLivePendingBroadcasts`), so
500
+ // this path cannot disagree with the COW guard about what is expired. It
501
+ // checks BOTH the slot id and the price level: a rebuild re-maps a price
502
+ // onto a different slot id, and reconcile is exactly the rebuild path — so
503
+ // a slot-id-only check would miss the entry the COW guard was written to
504
+ // catch. Placement is held and the chain snapshot was just re-read, so a
505
+ // live entry here is authoritative.
506
+ {
507
+ const pendingEntries = getLivePendingBroadcasts(manager);
508
+ for (const pending of pendingEntries) {
509
+ const slotId = pending?.slotId ?? pending?.orderId ?? pending?.order?.id;
510
+ if (slotId == null || String(slotId) !== String(gridOrder.id))
511
+ continue;
512
+ manager.logger?.log?.(`[_createOrderFromGrid] SKIP (PENDING-BROADCAST): Create for ${gridOrder.id} at ` +
513
+ `${Format.formatPrice6(Number(gridOrder.price))} may already be live from an uncertain broadcast; ` +
514
+ `waiting for recovery/adoption instead of duplicating it`, 'warn');
515
+ return null;
516
+ }
517
+ for (const pending of pendingEntries) {
518
+ const pendingPrice = Number(pending?.order?.price);
519
+ if (!Number.isFinite(pendingPrice) || pendingPrice <= 0)
520
+ continue;
521
+ const pendingType = pending?.orderType ?? pending?.order?.type;
522
+ if (pendingType && gridOrder.type && pendingType !== gridOrder.type)
523
+ continue;
524
+ if (!pricesOnSameLevel(pendingPrice, Number(pending?.order?.size) || 0, pendingType, Number(gridOrder.price), Number(gridOrder.size) || 0, gridOrder.type, manager.assets, manager.config?.incrementPercent))
525
+ continue;
526
+ manager.logger?.log?.(`[_createOrderFromGrid] SKIP (PENDING-BROADCAST): Create for ${gridOrder.id} at ` +
527
+ `${Format.formatPrice6(Number(gridOrder.price))} would duplicate the price level of an uncertain ` +
528
+ `CREATE for slot ${String(pending?.slotId ?? '?')}; waiting for recovery/adoption`, 'warn');
529
+ return null;
530
+ }
531
+ }
493
532
  const createPrice = gridOrder.price;
494
533
  // CROSSING-PLACEMENT GUARD: a create must not cross an opposite-side
495
534
  // live order (master, unmatched orphan, or pending broadcast) — see computePlacementCrossing.
@@ -1462,6 +1501,53 @@ async function _executePlannedStartupCreates({ createPlans, chainOrders, account
1462
1501
  `across ${groups.length} group(s)${failedCount > 0 ? `, ${failedCount} failed/skipped (see error logs)` : ''}`, failedCount > 0 ? 'warn' : 'info');
1463
1502
  return createdOrderIds;
1464
1503
  }
1504
+ /**
1505
+ * Partition the unmatched chain orders of one side into an UPDATE pool and a
1506
+ * dust-surplus CANCEL pool.
1507
+ *
1508
+ * Policy (single implementation used by the startup reconcile):
1509
+ * - dust = size below the per-slot single-dust threshold (same definition the
1510
+ * partial-dust health check uses);
1511
+ * - when the NON-dust orders alone can cover every slot this side needs, dust
1512
+ * orders are surplus -> CANCEL only. They are never grown into a full slot
1513
+ * and no replacement is created for them (there are already enough real
1514
+ * orders to cover the target);
1515
+ * - otherwise dust stays eligible to fill the remaining slots.
1516
+ *
1517
+ * Extracted so the policy is unit-testable and cannot drift between the
1518
+ * startup reconcile and any future caller.
1519
+ * @param {Object} input
1520
+ * @param {Array} input.sortedUnmatched - Unmatched chain orders, pre-sorted by price
1521
+ * @param {Array} input.desiredSlots - Target slots this side wants to activate
1522
+ * @param {Object} input.assets - Asset metadata (precisions)
1523
+ * @param {number} [input.dustThresholdPercent] - Dust threshold percentage
1524
+ * @returns {Object} { updatePool, dustSurplus, nonDustCount, dustCount, referenceIdeal, dustThreshold, enoughNonDust }
1525
+ */
1526
+ function _classifyStartupOrderReuse({ sortedUnmatched, desiredSlots, assets, dustThresholdPercent, }) {
1527
+ const referenceIdeal = desiredSlots.length > 0
1528
+ ? desiredSlots.reduce((sum, slot) => sum + (Number(slot?.size) || 0), 0) / desiredSlots.length
1529
+ : 0;
1530
+ const dustThreshold = getSingleDustThreshold(referenceIdeal, dustThresholdPercent);
1531
+ const isDustOrder = (chainOrder) => {
1532
+ if (!(dustThreshold > 0))
1533
+ return false;
1534
+ const parsed = parseChainOrder(chainOrder, assets);
1535
+ const size = Number(parsed?.size ?? 0);
1536
+ return size > 0 && size < dustThreshold;
1537
+ };
1538
+ const nonDustUnmatched = sortedUnmatched.filter((chainOrder) => !isDustOrder(chainOrder));
1539
+ const dustUnmatched = sortedUnmatched.filter((chainOrder) => isDustOrder(chainOrder));
1540
+ const enoughNonDust = desiredSlots.length > 0 && nonDustUnmatched.length >= desiredSlots.length;
1541
+ return {
1542
+ updatePool: enoughNonDust ? nonDustUnmatched : sortedUnmatched,
1543
+ dustSurplus: enoughNonDust ? dustUnmatched : [],
1544
+ nonDustCount: nonDustUnmatched.length,
1545
+ dustCount: dustUnmatched.length,
1546
+ referenceIdeal,
1547
+ dustThreshold,
1548
+ enoughNonDust,
1549
+ };
1550
+ }
1465
1551
  async function _reconcileStartupSide({ orderType, targetCount, chainSideOrders, unmatchedSideOrders, manager, chainOrders, account, privateKey, dryRun, plannedCreates, plannedUpdates, plannedCancels, planOnly = false, }) {
1466
1552
  const logger = manager?.logger;
1467
1553
  const sideUpper = orderType === ORDER_TYPES.SELL ? 'SELL' : 'BUY';
@@ -1501,7 +1587,19 @@ async function _reconcileStartupSide({ orderType, targetCount, chainSideOrders,
1501
1587
  desiredSlots = [...desiredSlots.slice(0, keepCount), ...freshEdge];
1502
1588
  }
1503
1589
  const sortedUnmatched = unmatchedSideOrders.slice(0).sort(sortUpdateComparator);
1504
- const updateCount = Math.min(sortedUnmatched.length, desiredSlots.length);
1590
+ const classifiedReuse = _classifyStartupOrderReuse({
1591
+ sortedUnmatched,
1592
+ desiredSlots,
1593
+ assets: manager.assets,
1594
+ dustThresholdPercent: manager.config?.gridLimits?.PARTIAL_DUST_THRESHOLD_PERCENTAGE,
1595
+ });
1596
+ // Only the Phase-1 (planOnly) caller can execute cancels. A non-planOnly
1597
+ // caller must keep every order eligible for update rather than silently
1598
+ // dropping dust from both the update pool and the cancel queue.
1599
+ const updatePool = planOnly ? classifiedReuse.updatePool : sortedUnmatched;
1600
+ const dustSurplus = planOnly ? classifiedReuse.dustSurplus : [];
1601
+ const nonDustCount = classifiedReuse.nonDustCount;
1602
+ const updateCount = Math.min(updatePool.length, desiredSlots.length);
1505
1603
  let cancelledIndex = null;
1506
1604
  let projectedSideBalance = Number(manager.accountTotals?.[balanceKey] || 0);
1507
1605
  // Vacated-rail-slot tracking (Phase 4, vacate+create atomic): a re-map
@@ -1518,16 +1616,17 @@ async function _reconcileStartupSide({ orderType, targetCount, chainSideOrders,
1518
1616
  const refillCandidates = [];
1519
1617
  const planVacatedRailRefill = (chainOrder, parsedChain) => {
1520
1618
  const boundary = manager.boundaryIdx;
1521
- const gapSlots = manager._gapSlots;
1619
+ // Geometry comes from the single source of truth (resolveGapSlots:
1620
+ // `_gapSlots` when published, config-derived otherwise). Reading the raw
1621
+ // field here made an unpublished `_gapSlots` silently disable the refill
1622
+ // while every other classify site derived a valid band.
1623
+ const gapSlots = resolveGapSlots(manager);
1522
1624
  // Explicit null/undefined/'' guard BEFORE Number(): Number(null) === 0
1523
1625
  // is finite and would defeat this bail-out, running the refill scan on
1524
- // a bogus boundary 0 (and a null gapSlots would default to 0 through
1525
- // getSellStartIdx).
1626
+ // a bogus boundary 0.
1526
1627
  if (boundary === null || boundary === undefined || boundary === '')
1527
1628
  return;
1528
- if (gapSlots === null || gapSlots === undefined || gapSlots === '')
1529
- return;
1530
- if (!Number.isFinite(Number(boundary)) || !Number.isFinite(Number(gapSlots)))
1629
+ if (!Number.isFinite(Number(boundary)))
1531
1630
  return;
1532
1631
  const chainPrice = parsedChain?.price;
1533
1632
  if (!Number.isFinite(Number(chainPrice)))
@@ -1573,7 +1672,7 @@ async function _reconcileStartupSide({ orderType, targetCount, chainSideOrders,
1573
1672
  account,
1574
1673
  privateKey,
1575
1674
  manager,
1576
- unmatchedOrders: sortedUnmatched,
1675
+ unmatchedOrders: updatePool,
1577
1676
  updateCount,
1578
1677
  orderType,
1579
1678
  dryRun,
@@ -1588,7 +1687,7 @@ async function _reconcileStartupSide({ orderType, targetCount, chainSideOrders,
1588
1687
  releaseUntrackedFunds: true,
1589
1688
  orderType,
1590
1689
  boundaryIdx: manager?.boundaryIdx,
1591
- gapSlots: manager?._gapSlots,
1690
+ gapSlots: resolveGapSlots(manager),
1592
1691
  });
1593
1692
  }
1594
1693
  }
@@ -1596,7 +1695,7 @@ async function _reconcileStartupSide({ orderType, targetCount, chainSideOrders,
1596
1695
  for (let i = 0; i < updateCount; i++) {
1597
1696
  if (cancelledIndex !== null && i === cancelledIndex)
1598
1697
  continue;
1599
- const chainOrder = sortedUnmatched[i];
1698
+ const chainOrder = updatePool[i];
1600
1699
  const gridOrder = desiredSlots[i];
1601
1700
  const gridSize = Number(gridOrder.size) || 0;
1602
1701
  const parsedChain = parseChainOrder(chainOrder, manager.assets);
@@ -1652,7 +1751,29 @@ async function _reconcileStartupSide({ orderType, targetCount, chainSideOrders,
1652
1751
  },
1653
1752
  });
1654
1753
  }
1655
- const processedUnmatched = sortedUnmatched.slice(updateCount);
1754
+ const processedUnmatched = updatePool.slice(updateCount);
1755
+ // Queue the dust-surplus cancels after the update plans. Phase 2 always runs
1756
+ // cancels before creates/updates, so these can never be paired into a slot.
1757
+ // planOnly is the Phase-1 contract; the actual broadcast happens in Phase 2.
1758
+ let dustCancelledCount = 0;
1759
+ if (planOnly && dustSurplus.length > 0) {
1760
+ const alreadyPlanned = new Set(plannedCancels.map((plan) => String(plan.chainOrderId)));
1761
+ for (const dustOrder of dustSurplus) {
1762
+ if (alreadyPlanned.has(String(dustOrder.id)))
1763
+ continue;
1764
+ plannedCancels.push({
1765
+ chainOrderId: dustOrder.id,
1766
+ chainOrderObj: dustOrder,
1767
+ releaseUntrackedFunds: true,
1768
+ orderType,
1769
+ boundaryIdx: manager?.boundaryIdx,
1770
+ gapSlots: resolveGapSlots(manager),
1771
+ });
1772
+ dustCancelledCount++;
1773
+ logger?.log?.(`Startup: Cancelling ${sideUpper} dust order ${dustOrder.id} ` +
1774
+ `(${nonDustCount} non-dust order(s) already cover ${desiredSlots.length} needed slot(s); not pricing it into a full-size slot)`, 'info');
1775
+ }
1776
+ }
1656
1777
  // Slot-N gated chain count: fork-kept shelf/manual orders (non-slot-N
1657
1778
  // grid ids, e.g. deep-*) sit outside window accounting — same gate as
1658
1779
  // _countActiveOnGrid and matched-excess candidates (issue #27 follow-up).
@@ -1671,6 +1792,14 @@ async function _reconcileStartupSide({ orderType, targetCount, chainSideOrders,
1671
1792
  }
1672
1793
  }
1673
1794
  catch { /* fail-open: keep unfiltered count */ }
1795
+ // Dust-surplus cancels are already queued above, so the remaining chain
1796
+ // count must exclude them. Use the ACTUALLY queued count, not
1797
+ // dustSurplus.length: the alreadyPlanned guard can skip a dust order that
1798
+ // is already in plannedCancels, and subtracting it twice would fabricate
1799
+ // a shortfall (or over-cancel a healthy matched order). Without this
1800
+ // adjustment `createCount` would over-count (no create for the freed level)
1801
+ // and `cancelCount` would double-count the dust as excess.
1802
+ chainCount = Math.max(0, chainCount - dustCancelledCount);
1674
1803
  const createCount = Math.max(0, targetCount - chainCount);
1675
1804
  const remainingSlots = desiredSlots.slice(updateCount);
1676
1805
  // UNSETTLED-FILL GATE: while fills are queued, draining, in flight, or
@@ -1766,94 +1895,85 @@ async function _reconcileStartupSide({ orderType, targetCount, chainSideOrders,
1766
1895
  return fa - fb;
1767
1896
  });
1768
1897
  }
1898
+ // ONE SELECTION, TWO SINKS. The planOnly and inline branches used to walk
1899
+ // the same two ordered lists with duplicated (and drifting) conditions —
1900
+ // the "plan/execute parity" bug class. Selection happens once here; a
1901
+ // planOnly call records the list for Phase 2, an inline call executes it.
1902
+ // Priority is identical either way: unmatched orphans first (untracked
1903
+ // funds), then matched surplus (funds stay tracked on the grid slot), and
1904
+ // edge order slots last (static insurance, already sorted above).
1905
+ const excessGapSlots = resolveGapSlots(manager);
1906
+ const excessPlans = [];
1907
+ for (const x of parsedUnmatched) {
1908
+ if (excessPlans.length >= cancelCount)
1909
+ break;
1910
+ excessPlans.push({
1911
+ chainOrderId: x.chain.id,
1912
+ chainOrderObj: x.chain,
1913
+ releaseUntrackedFunds: true,
1914
+ orderType,
1915
+ boundaryIdx: manager?.boundaryIdx,
1916
+ gapSlots: excessGapSlots,
1917
+ });
1918
+ }
1919
+ // Matched excess must be PLANNED too, not only executed (issue #27
1920
+ // follow-up): startup reconcile always runs planOnly, and on a
1921
+ // fully-placed grid every chain order is matched — parsedUnmatched is
1922
+ // empty, so the surplus lived entirely on matched slots and was silently
1923
+ // dropped while the inline branch kept cancelling it. Nothing ever
1924
+ // created the vacancy, and a fully-placed grid could never converge to
1925
+ // its additive window+edge target (live 12 vs target 8 across restarts).
1926
+ for (const o of matchedExcess) {
1927
+ if (excessPlans.length >= cancelCount)
1928
+ break;
1929
+ if (!o.orderId)
1930
+ continue;
1931
+ excessPlans.push({
1932
+ chainOrderId: o.orderId,
1933
+ chainOrderObj: o,
1934
+ gridOrderId: o.id,
1935
+ gridOrder: { ...o },
1936
+ orderType,
1937
+ boundaryIdx: manager?.boundaryIdx,
1938
+ gapSlots: excessGapSlots,
1939
+ // Matched-slot funds are tracked on the grid slot; only unmatched
1940
+ // orphans release untracked funds.
1941
+ });
1942
+ }
1769
1943
  if (planOnly) {
1770
- // In planOnly mode, record the cancellations for Phase 2 execution.
1771
- // This prevents blockchain I/O inside _gridLock (Level 3).
1772
- if (cancelCount > 0) {
1773
- logger?.log?.(`Startup: ${sideUpper} excess ${cancelCount} order(s) queued for cancellation (Phase 2)`, 'info');
1774
- for (const x of parsedUnmatched) {
1775
- if (cancelCount <= 0)
1776
- break;
1777
- plannedCancels.push({
1778
- chainOrderId: x.chain.id,
1779
- chainOrderObj: x.chain,
1780
- releaseUntrackedFunds: true,
1781
- orderType,
1782
- boundaryIdx: manager?.boundaryIdx,
1783
- gapSlots: manager?._gapSlots,
1784
- });
1785
- cancelCount--;
1786
- }
1944
+ // Phase-1 contract: record the cancellations for Phase 2 execution so
1945
+ // no blockchain I/O runs inside _gridLock (Level 3).
1946
+ if (excessPlans.length > 0) {
1947
+ logger?.log?.(`Startup: ${sideUpper} excess ${excessPlans.length} order(s) queued for cancellation (Phase 2)`, 'info');
1787
1948
  }
1788
- // Matched excess must be PLANNED too, not only executed (issue #27
1789
- // follow-up): startup reconcile always runs planOnly, and on a
1790
- // fully-placed grid every chain order is matched — parsedUnmatched
1791
- // is empty, so the surplus lived entirely on matched slots and was
1792
- // silently dropped here while the execute branch kept cancelling
1793
- // it. Nothing ever created the vacancy, and a fully-placed grid
1794
- // could never converge to its additive window+edge target (live
1795
- // 12 vs target 8 sat static across every restart). Same priority
1796
- // as the execute branch: unmatched first (above), matched after,
1797
- // edge order last (matchedExcess ordering).
1798
- for (const o of matchedExcess) {
1799
- if (cancelCount <= 0)
1800
- break;
1801
- if (!o.orderId)
1802
- continue;
1803
- logger?.log?.(`Startup: ${sideUpper} excess matched ${o.orderId} (grid ${o.id}) queued for cancellation (Phase 2)`, 'warn');
1804
- plannedCancels.push({
1805
- chainOrderId: o.orderId,
1806
- chainOrderObj: o,
1807
- gridOrderId: o.id,
1808
- gridOrder: { ...o },
1809
- orderType,
1810
- boundaryIdx: manager?.boundaryIdx,
1811
- gapSlots: manager?._gapSlots,
1812
- // Matched-slot funds are tracked on the grid slot; only
1813
- // unmatched orphans release untracked funds (mirrors the
1814
- // execute branch, which passes no release flag here).
1815
- });
1816
- cancelCount--;
1949
+ for (const plan of excessPlans) {
1950
+ if (plan.gridOrderId) {
1951
+ logger?.log?.(`Startup: ${sideUpper} excess matched ${plan.chainOrderId} (grid ${plan.gridOrderId}) queued for cancellation (Phase 2)`, 'warn');
1952
+ }
1953
+ plannedCancels.push(plan);
1817
1954
  }
1818
1955
  }
1819
1956
  else {
1820
- for (const x of parsedUnmatched) {
1821
- if (cancelCount <= 0)
1822
- break;
1823
- logger?.log?.(`Startup: Cancelling excess ${sideUpper} chain order ${x.chain.id}`, 'info');
1957
+ for (const plan of excessPlans) {
1958
+ const label = plan.gridOrderId
1959
+ ? `excess matched ${sideUpper} ${plan.chainOrderId} (grid ${plan.gridOrderId})`
1960
+ : `excess ${sideUpper} chain order ${plan.chainOrderId}`;
1961
+ logger?.log?.(`Startup: Cancelling ${label}`, plan.gridOrderId ? 'warn' : 'info');
1824
1962
  try {
1825
1963
  await _cancelChainOrder({
1826
1964
  chainOrders,
1827
1965
  account,
1828
1966
  privateKey,
1829
1967
  manager,
1830
- chainOrderId: x.chain.id,
1831
- chainOrderObj: x.chain,
1832
- releaseUntrackedFunds: true,
1968
+ chainOrderId: plan.chainOrderId,
1969
+ chainOrderObj: plan.chainOrderObj,
1970
+ releaseUntrackedFunds: plan.releaseUntrackedFunds === true,
1833
1971
  dryRun,
1834
1972
  });
1835
- logger?.log?.(`Startup: Successfully cancelled excess ${sideUpper} order ${x.chain.id}`, 'info');
1836
- cancelCount--;
1973
+ logger?.log?.(`Startup: Successfully cancelled ${label}`, 'info');
1837
1974
  }
1838
1975
  catch (err) {
1839
- logger?.log?.(`Startup: Failed to cancel ${sideUpper} ${x.chain.id}: ${getErrorMessage(err)}`, 'error');
1840
- }
1841
- }
1842
- if (cancelCount > 0) {
1843
- for (const o of matchedExcess) {
1844
- if (cancelCount <= 0)
1845
- break;
1846
- if (!o.orderId)
1847
- continue;
1848
- logger?.log?.(`Startup: Cancelling excess matched ${sideUpper} ${o.orderId} (grid ${o.id})`, 'warn');
1849
- try {
1850
- await _cancelChainOrder({ chainOrders, account, privateKey, manager, chainOrderId: o.orderId, dryRun, chainOrderObj: null });
1851
- logger?.log?.(`Startup: Successfully cancelled excess matched ${sideUpper} order ${o.orderId} (grid ${o.id})`, 'info');
1852
- cancelCount--;
1853
- }
1854
- catch (err) {
1855
- logger?.log?.(`Startup: Failed to cancel matched ${sideUpper} ${o.orderId}: ${getErrorMessage(err)}`, 'error');
1856
- }
1976
+ logger?.log?.(`Startup: Failed to cancel ${label}: ${getErrorMessage(err)}`, 'error');
1857
1977
  }
1858
1978
  }
1859
1979
  }
@@ -1862,5 +1982,5 @@ async function _reconcileStartupSide({ orderType, targetCount, chainSideOrders,
1862
1982
  chainCount,
1863
1983
  };
1864
1984
  }
1865
- export { _countActiveOnGrid, _pickVirtualSlotsToActivate, _createOrderFromGrid, _prepareStartupUpdatePlan, _cancelChainOrder, _recoverStartupSyncFailure, _refreshStartupUpdatePlans, _executeStartupUpdateBatch, _executeStartupSequentialUpdateFallback, _executeStartupCreateGroupBatch, _createStartupOrderWithHandling, _executePlannedStartupCreates, _reconcileStartupSide };
1985
+ export { _countActiveOnGrid, _pickVirtualSlotsToActivate, _createOrderFromGrid, _prepareStartupUpdatePlan, _cancelChainOrder, _recoverStartupSyncFailure, _refreshStartupUpdatePlans, _executeStartupUpdateBatch, _executeStartupSequentialUpdateFallback, _executeStartupCreateGroupBatch, _createStartupOrderWithHandling, _executePlannedStartupCreates, _reconcileStartupSide, _classifyStartupOrderReuse };
1866
1986
  //# sourceMappingURL=grid_reconcile_internal.js.map
@@ -117,6 +117,7 @@ declare const utils: {
117
117
  unsettled: boolean;
118
118
  reason: string | null;
119
119
  };
120
+ getPipelineInFlightAgeMs(source: unknown, nowMs?: number): number;
120
121
  restoreLastMarketFill(manager: import("../types.js").OrderManagerLike, persisted: unknown, options?: {
121
122
  now?: number;
122
123
  }): boolean;
@@ -211,6 +212,7 @@ declare const utils: {
211
212
  resolveSlotIdForFillPrice: typeof order.resolveSlotIdForFillPrice;
212
213
  reviveVirtualSlotFromChain: typeof order.reviveVirtualSlotFromChain;
213
214
  attributeUnmatchedFillToGrid: typeof order.attributeUnmatchedFillToGrid;
215
+ getLivePendingBroadcasts: typeof order.getLivePendingBroadcasts;
214
216
  buildFillKey: typeof order.buildFillKey;
215
217
  correctOrderPriceOnChain: typeof order.correctOrderPriceOnChain;
216
218
  correctAllPriceMismatches: typeof order.correctAllPriceMismatches;
@@ -266,6 +268,7 @@ declare const utils: {
266
268
  calculateBudgetedSizes: typeof order.calculateBudgetedSizes;
267
269
  buildCreateOpFingerprint: typeof order.buildCreateOpFingerprint;
268
270
  isOrderGoneErrorMessage: typeof order.isOrderGoneErrorMessage;
271
+ isStructuralRebuildStaleReason: typeof order.isStructuralRebuildStaleReason;
269
272
  recordDuplicateOrphanDetection: typeof order.recordDuplicateOrphanDetection;
270
273
  clearDuplicateOrphanDetection: typeof order.clearDuplicateOrphanDetection;
271
274
  duplicateOrphanLogInfo: typeof order.duplicateOrphanLogInfo;
@@ -295,6 +298,7 @@ declare const utils: {
295
298
  isEvacuationSizeStillValid: typeof math.isEvacuationSizeStillValid;
296
299
  validateBoundaryCommit: typeof math.validateBoundaryCommit;
297
300
  validatePersistedBoundary: typeof math.validatePersistedBoundary;
301
+ boundaryMoveReducesStrands: typeof math.boundaryMoveReducesStrands;
298
302
  isTransientInBandRejection: typeof math.isTransientInBandRejection;
299
303
  BOUNDARY_REJECT_PLACED_IN_BAND: "placed_order_in_band";
300
304
  resolveGapSlots: typeof math.resolveGapSlots;