dexbot 1.6.16 → 1.6.18
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.
- package/CHANGELOG.md +58 -0
- package/analysis/README.md +10 -9
- package/analysis/ama_fitting/README.md +2 -2
- package/analysis/bot_fitting/README.md +3 -3
- package/analysis/tradingview/README.md +2 -2
- package/analysis/trend_detection/DYNAMIC_WEIGHT_RESEARCH.md +1 -1
- package/claw/package.json +1 -1
- package/claw/runtimes/openclaw-plugin/openclaw.plugin.json +1 -1
- package/claw/runtimes/openclaw-plugin/package.json +1 -1
- package/dist/analysis/ama_fitting/analyze_lambda_vs_slow.js +5 -3
- package/dist/analysis/analyze_args.d.ts +2 -1
- package/dist/analysis/fills_source.d.ts +1 -8
- package/dist/analysis/fills_source.js +1 -1
- package/dist/analysis/grid_correction_check.d.ts +9 -6
- package/dist/analysis/grid_correction_check.js +56 -64
- package/dist/analysis/math_utils.d.ts +1 -1
- package/dist/analysis/trade_profitability.d.ts +1 -1
- package/dist/analysis/tradingview/grid_reset_sim.js +1 -1
- package/dist/analysis/tradingview/tradingview_uplot_chart_generator.js +7 -7
- package/dist/analysis/trend_detection/backtest_ama_slope_huber.js +1 -1
- package/dist/analysis/trend_detection/huber_scale_variants.d.ts +4 -3
- package/dist/market_adapter/cli_args.d.ts +6 -1
- package/dist/market_adapter/cli_args.js +16 -2
- package/dist/market_adapter/core/kibana_candles.d.ts +5 -1
- package/dist/market_adapter/core/kibana_candles.js +3 -1
- package/dist/market_adapter/core/strategies/regime_gate.d.ts +7 -1
- package/dist/market_adapter/core/strategies/regime_gate.js +3 -1
- package/dist/market_adapter/inputs/cex_candle_parsing.d.ts +7 -2
- package/dist/market_adapter/inputs/cex_candle_parsing.js +3 -1
- package/dist/market_adapter/inputs/fetch_book_data.d.ts +7 -1
- package/dist/market_adapter/inputs/fetch_book_data.js +3 -1
- package/dist/market_adapter/inputs/fetch_cex_synthetic_data.js +2 -11
- package/dist/market_adapter/inputs/fetch_lp_data.d.ts +26 -1
- package/dist/market_adapter/inputs/fetch_lp_data.js +62 -52
- package/dist/market_adapter/inputs/kibana_feed_source.d.ts +14 -2
- package/dist/market_adapter/inputs/kibana_feed_source.js +14 -1
- package/dist/market_adapter/inputs/window_cache.d.ts +14 -3
- package/dist/market_adapter/inputs/window_cache.js +14 -1
- package/dist/market_adapter/lp_chart_runner.d.ts +12 -9
- package/dist/market_adapter/lp_chart_runner.js +3 -1
- package/dist/market_adapter/lp_chart_strategy_loader.d.ts +5 -1
- package/dist/market_adapter/lp_chart_strategy_loader.js +3 -1
- package/dist/market_adapter/market_adapter.d.ts +10 -2
- package/dist/market_adapter/market_adapter.js +10 -11
- package/dist/market_adapter/utils/chain.d.ts +2 -3
- package/dist/market_adapter/utils/file_json_cache.d.ts +5 -1
- package/dist/market_adapter/utils/file_json_cache.js +3 -1
- package/dist/market_adapter/utils/file_lock.js +1 -1
- package/dist/market_adapter/utils/native_history.d.ts +7 -3
- package/dist/market_adapter/utils/native_history.js +3 -1
- package/dist/market_adapter/utils/pair_orientation.d.ts +2 -2
- package/dist/modules/account_bots.d.ts +3 -0
- package/dist/modules/bots_file_lock.d.ts +2 -2
- package/dist/modules/bots_file_lock.js +2 -2
- package/dist/modules/chain_keys.d.ts +1 -1
- package/dist/modules/constants.d.ts +6 -0
- package/dist/modules/constants.js +37 -6
- package/dist/modules/credit_pricing.d.ts +20 -1
- package/dist/modules/credit_pricing.js +23 -1
- package/dist/modules/credit_runtime.js +33 -16
- package/dist/modules/dexbot_class.d.ts +11 -1
- package/dist/modules/dexbot_class.js +15 -2
- package/dist/modules/dexbot_cow_runtime.d.ts +90 -38
- package/dist/modules/dexbot_cow_runtime.js +507 -108
- package/dist/modules/dexbot_credential_client.d.ts +5 -0
- package/dist/modules/dexbot_maintenance_runtime.js +117 -12
- package/dist/modules/dexbot_startup_runtime.js +7 -1
- package/dist/modules/graceful_shutdown.d.ts +2 -2
- package/dist/modules/graceful_shutdown.js +2 -2
- package/dist/modules/launcher/credential_daemon.d.ts +14 -2
- package/dist/modules/launcher/credential_daemon.js +60 -26
- package/dist/modules/launcher/runtime_mode.d.ts +4 -3
- package/dist/modules/node_manager.d.ts +1 -1
- package/dist/modules/node_manager.js +1 -1
- package/dist/modules/order/accounting.js +2 -1
- package/dist/modules/order/export.d.ts +2 -2
- package/dist/modules/order/export.js +2 -2
- package/dist/modules/order/grid.d.ts +26 -14
- package/dist/modules/order/grid.js +416 -73
- package/dist/modules/order/grid_reconcile.js +5 -1
- package/dist/modules/order/grid_reconcile_internal.js +2 -1
- package/dist/modules/order/index.d.ts +22 -1
- package/dist/modules/order/manager.d.ts +40 -7
- package/dist/modules/order/manager.js +149 -23
- package/dist/modules/order/strategy.d.ts +5 -0
- package/dist/modules/order/strategy.js +44 -3
- package/dist/modules/order/sync_engine.d.ts +12 -1
- package/dist/modules/order/sync_engine.js +135 -20
- package/dist/modules/order/utils/math.d.ts +49 -10
- package/dist/modules/order/utils/math.js +71 -14
- package/dist/modules/order/utils/order.d.ts +65 -11
- package/dist/modules/order/utils/order.js +198 -28
- package/dist/modules/order/utils/system.d.ts +112 -2
- package/dist/modules/order/utils/system.js +338 -12
- package/dist/modules/order/utils/validate.d.ts +4 -1
- package/dist/modules/order/utils/validate.js +9 -2
- package/dist/modules/storage/index.d.ts +3 -2
- package/dist/modules/storage/index.js +3 -2
- package/dist/modules/types.d.ts +44 -2
- package/dist/scripts/analyze-credit.js +64 -23
- package/dist/scripts/bts_distribution.d.ts +66 -0
- package/dist/scripts/bts_distribution.js +2102 -0
- package/dist/scripts/token_distribution.d.ts +45 -0
- package/dist/scripts/token_distribution.js +1870 -0
- package/dist/unlock.js +42 -24
- package/docs/COPY_ON_WRITE_MASTER_PLAN.md +1 -1
- package/docs/COW_INVARIANTS.md +24 -8
- package/docs/DEXBOT_COMPARISON.md +5 -5
- package/docs/EVOLUTION.md +10 -8
- package/docs/FUND_MOVEMENT_AND_ACCOUNTING.md +13 -11
- package/docs/GRID_PRICE_INVARIANT.md +29 -3
- package/docs/GRID_RECALCULATION.md +3 -3
- package/docs/GRID_RECONCILE.md +5 -5
- package/docs/LIFECYCLE.md +11 -11
- package/docs/LOGGING.md +22 -0
- package/docs/MPA_CREDIT_USAGE.md +3 -2
- package/docs/ORDER_ENGINE_POST_1.0_RETROSPECTIVE.md +236 -32
- package/docs/README.md +1 -1
- package/docs/architecture.md +205 -6
- package/docs/developer_guide.md +12 -0
- package/market_adapter/README.md +11 -6
- package/modules/README.md +18 -14
- package/package.json +6 -3
- package/scripts/README.md +85 -148
|
@@ -79,7 +79,11 @@ export async function attemptResumePersistedGridByPriceMatch({ manager, persiste
|
|
|
79
79
|
const { loadGrid } = require('./grid');
|
|
80
80
|
// Prefer explicit genesis if caller wired it, else fall back to manager._genesis
|
|
81
81
|
const genesisArg = genesis ?? manager._genesis ?? null;
|
|
82
|
-
|
|
82
|
+
// This is a resume/adoption path, not a poison-ingestion path. Pure
|
|
83
|
+
// in-band stranding is the normal SPREAD-GUARD / partial-fill-anchor
|
|
84
|
+
// state and GAP-EVAC owns cleanup; keep the persisted boundary so a
|
|
85
|
+
// re-anchor surviving in the snapshot is not rebuilt away here.
|
|
86
|
+
await loadGrid(manager, persistedGrid, boundaryIdx, genesisArg, { tolerateTransientStranding: true });
|
|
83
87
|
await manager.synchronizeWithChain(chainOpenOrders, 'readOpenOrders');
|
|
84
88
|
const matchedOrderIds = new Set((Array.from(manager.orders.values()))
|
|
85
89
|
.filter((o) => o && isOrderOnChain(o))
|
|
@@ -7,7 +7,8 @@
|
|
|
7
7
|
import { ORDER_TYPES, ORDER_STATES, TIMING, BTS_PRECISION } from '../constants.js';
|
|
8
8
|
import { readOpenOrdersGuarded } from '../chain_orders.js';
|
|
9
9
|
import { getMinOrderSize, getAssetFees, getAssetFeesSafe, blockchainToFloat, findCrossedOrder, resolveGapBand, isSlotInRail, pricesOnSameLevel, 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,
|
|
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';
|
|
11
|
+
import { parseSlotIndex } from './utils/slot.js';
|
|
11
12
|
import { resolveAccountRef } from './utils/system.js';
|
|
12
13
|
import * as Format from './format.js';
|
|
13
14
|
import { getErrorMessage, getErrorCode, getErrorName } from '../utils/errors.js';
|
|
@@ -86,13 +86,32 @@ declare const utils: {
|
|
|
86
86
|
} | null;
|
|
87
87
|
}): system.SpreadCorrectionReadiness;
|
|
88
88
|
resolveSpreadMarketReference(manager: import("../types.js").OrderManagerLike): number | null;
|
|
89
|
+
reanchorBoundaryFromLastFill(manager: import("../types.js").OrderManagerLike, boundaryIdx: number | null | undefined, slots: import("../types.js").ManagedOrder[] | null | undefined, gapSlots: number, fills?: Array<{
|
|
90
|
+
id?: string;
|
|
91
|
+
type?: string;
|
|
92
|
+
price?: unknown;
|
|
93
|
+
}> | null | undefined): {
|
|
94
|
+
boundaryIdx: number | null;
|
|
95
|
+
reanchored: boolean;
|
|
96
|
+
from?: number | null;
|
|
97
|
+
to?: number;
|
|
98
|
+
side?: string;
|
|
99
|
+
fillSlot?: number;
|
|
100
|
+
reason?: string;
|
|
101
|
+
};
|
|
89
102
|
resolveLastFillGuardReference(manager: import("../types.js").OrderManagerLike): {
|
|
90
103
|
price: number | null;
|
|
91
104
|
type: string | null;
|
|
92
105
|
};
|
|
106
|
+
isLastFillGuardBlocked(price: number, type: unknown, lastPrice: number | null, lastType: string | null): {
|
|
107
|
+
blocked: boolean;
|
|
108
|
+
pivot: number | null;
|
|
109
|
+
threshold?: number | null;
|
|
110
|
+
};
|
|
93
111
|
restoreLastMarketFill(manager: import("../types.js").OrderManagerLike, persisted: unknown, options?: {
|
|
94
112
|
now?: number;
|
|
95
113
|
}): boolean;
|
|
114
|
+
rearmLastFillPivotFromMarketFill(manager: import("../types.js").OrderManagerLike): boolean;
|
|
96
115
|
normalizeLastFillPivot(row: unknown): {
|
|
97
116
|
price: number;
|
|
98
117
|
type: string;
|
|
@@ -177,6 +196,7 @@ declare const utils: {
|
|
|
177
196
|
reason?: string;
|
|
178
197
|
};
|
|
179
198
|
parseChainOrder: typeof order.parseChainOrder;
|
|
199
|
+
resolveLiveOrderLadderPosition: typeof order.resolveLiveOrderLadderPosition;
|
|
180
200
|
applyChainSizeToGridOrder: typeof order.applyChainSizeToGridOrder;
|
|
181
201
|
buildFillKey: typeof order.buildFillKey;
|
|
182
202
|
correctOrderPriceOnChain: typeof order.correctOrderPriceOnChain;
|
|
@@ -199,7 +219,6 @@ declare const utils: {
|
|
|
199
219
|
crossingCandidateChainId: typeof order.crossingCandidateChainId;
|
|
200
220
|
isCrossingCheckCandidate: typeof order.isCrossingCheckCandidate;
|
|
201
221
|
buildCrossingCheckCandidates: typeof order.buildCrossingCheckCandidates;
|
|
202
|
-
parseSlotIndex: typeof order.parseSlotIndex;
|
|
203
222
|
filterOrdersByType: typeof order.filterOrdersByType;
|
|
204
223
|
buildOutsideInPairGroups: typeof order.buildOutsideInPairGroups;
|
|
205
224
|
extractBatchOperationResults: typeof order.extractBatchOperationResults;
|
|
@@ -240,6 +259,7 @@ declare const utils: {
|
|
|
240
259
|
chainOrderUnchangedFromCache: typeof order.chainOrderUnchangedFromCache;
|
|
241
260
|
detectCrossedBookPlan: typeof order.detectCrossedBookPlan;
|
|
242
261
|
collectKnownOnChainOrderIds: typeof order.collectKnownOnChainOrderIds;
|
|
262
|
+
collectEdgePinnedFillIds: typeof order.collectEdgePinnedFillIds;
|
|
243
263
|
edgeOrderIdSet: typeof order.edgeOrderIdSet;
|
|
244
264
|
liveWindowIdSet: typeof order.liveWindowIdSet;
|
|
245
265
|
checkGridPriceInvariant: typeof order.checkGridPriceInvariant;
|
|
@@ -278,6 +298,7 @@ declare const utils: {
|
|
|
278
298
|
getPrecision: typeof math.getPrecision;
|
|
279
299
|
computeChainFundTotals: typeof math.computeChainFundTotals;
|
|
280
300
|
calculateAvailableFundsValue: typeof math.calculateAvailableFundsValue;
|
|
301
|
+
deriveChainTruthFundBudget: typeof math.deriveChainTruthFundBudget;
|
|
281
302
|
adjustBudgetForBtsFees: typeof math.adjustBudgetForBtsFees;
|
|
282
303
|
getGridBestPrices: typeof math.getGridBestPrices;
|
|
283
304
|
calculateSpreadFromOrders: typeof math.calculateSpreadFromOrders;
|
|
@@ -672,7 +672,30 @@ declare class OrderManager implements OrderManagerLike {
|
|
|
672
672
|
*
|
|
673
673
|
* @param {string} reason - Log/debug label for the drop
|
|
674
674
|
*/
|
|
675
|
-
|
|
675
|
+
/**
|
|
676
|
+
* Drop ALL owed fill-crawl records.
|
|
677
|
+
*
|
|
678
|
+
* This is the explicit allowed-loss path: the grid generation is being
|
|
679
|
+
* discarded (rebuild, rejected snapshot, restart replay), so the relative
|
|
680
|
+
* deltas no longer have a geometry to apply onto.
|
|
681
|
+
*/
|
|
682
|
+
_clearPendingFillCrawls(reason?: string, detail?: string): void;
|
|
683
|
+
/**
|
|
684
|
+
* Consume the owed fill-crawl records a derivation actually folded.
|
|
685
|
+
*
|
|
686
|
+
* A boundary commit must never clear a record it could not apply: a
|
|
687
|
+
* truncated cross-chunk shift, an edge-classified slot of an older
|
|
688
|
+
* generation, or a record re-written for the same slot after the
|
|
689
|
+
* derivation ran. Clearing all of them was the last way a fill's crawl
|
|
690
|
+
* could be lost silently (2026-10-25: 7 fills swept slots 147-153, the
|
|
691
|
+
* commits dropped every record and the boundary ended 7 levels behind).
|
|
692
|
+
*
|
|
693
|
+
* `consumed` is the derivation's `consumedCrawlIds`; absent/not-an-array
|
|
694
|
+
* means the plan never derived a boundary (e.g. a create-only correction),
|
|
695
|
+
* so NOTHING is consumed. Records are matched by `slotId` + `ts`, so a
|
|
696
|
+
* newer record for the same slot survives a stale consumption.
|
|
697
|
+
*/
|
|
698
|
+
_consumePendingFillCrawls(consumed: unknown, reason?: string, detail?: string): void;
|
|
676
699
|
/**
|
|
677
700
|
* Observability (fix #4, docs/ORDER_ENGINE_POST_1.0_RETROSPECTIVE.md §2): emit the live
|
|
678
701
|
* boundary whenever roles are (re)assigned, so a silent boundary crawl that
|
|
@@ -730,12 +753,22 @@ declare class OrderManager implements OrderManagerLike {
|
|
|
730
753
|
_resetLastFillPivot(reason?: string): boolean;
|
|
731
754
|
/**
|
|
732
755
|
* Record last filled price for the single-pivot guard: when armed, BOTH
|
|
733
|
-
* sides are gated against the most recent fill price
|
|
734
|
-
* pivot
|
|
735
|
-
* isLastFillGuardBlocked).
|
|
736
|
-
*
|
|
737
|
-
*
|
|
738
|
-
*
|
|
756
|
+
* sides are gated by the direction rule against the most recent fill price
|
|
757
|
+
* (BUY blocked at/above the pivot, SELL blocked at/below it; see
|
|
758
|
+
* isLastFillGuardBlocked). _lastFilledType is purely the cold/armed
|
|
759
|
+
* discriminator (null => disabled). The per-side fields are not read by the
|
|
760
|
+
* guard itself; they feed the book-seed cold-check and per-side seeding in
|
|
761
|
+
* seedLastFilledPricesFromBook.
|
|
762
|
+
*
|
|
763
|
+
* EDGE-PINNED FILLS ARE SKIPPED. An edge order is static fat-finger
|
|
764
|
+
* insurance resting at the grid floor/ceiling, not market movement: the
|
|
765
|
+
* boundary crawl already excludes its fills, and the pivot doubles as the
|
|
766
|
+
* geometry anchor for reanchorBoundaryFromLastFill. Letting a wick on an
|
|
767
|
+
* edge order arm the pivot at a rail-edge price would both veto that whole
|
|
768
|
+
* side of the book and (as an absolute anchor) teleport the window to the
|
|
769
|
+
* edge. Skipping keeps the reference on the last WINDOW trade, matching
|
|
770
|
+
* the crawl's classification (collectEdgePinnedFillIds is the shared
|
|
771
|
+
* definition).
|
|
739
772
|
* @param {Array} fills
|
|
740
773
|
*/
|
|
741
774
|
recordLastFilledPrices(fills: COWFillLike[]): void;
|
|
@@ -30,7 +30,8 @@ import { ORDER_TYPES, ORDER_STATES, REBALANCE_STATES, TIMING, LOG_LEVEL, PIPELIN
|
|
|
30
30
|
import { seedBotRuntimeConfig, createDefaultBotMetrics } from '../bot_defaults.js';
|
|
31
31
|
import { getMinOrderSize, computeChainFundTotals, hasValidAccountTotals, resolveConfigValueWithRegistry, isExplicitZeroAllocation, floatToBlockchainInt, validateBoundaryCommit, resolveGapSlots, resolveGapBand, isSlotInRail } from './utils/math.js';
|
|
32
32
|
import { validateOrder, validateGridForPersistence, validateWorkingGridFunds, checkFundDrift, reconcileGrid, optimizeRebalanceActions, projectTargetToWorkingGrid, buildStateUpdates, buildAbortedResult, buildSuccessResult, evaluateCommit } from './utils/validate.js';
|
|
33
|
-
import { resolveSpreadOrderSide,
|
|
33
|
+
import { resolveSpreadOrderSide, resolveLiveOrderLadderPosition, parseChainOrder, geometryTypeForSlotIndex, isOrderOnChain, resolveEdgeCount, resolveLiveEdgeAnchorPrice, compareEdge, collectRefillSlotIds, recordOrderPlacement, isFreshlyPlacedOrder, collectEdgePinnedFillIds } from './utils/order.js';
|
|
34
|
+
import { parseSlotIndex } from './utils/slot.js';
|
|
34
35
|
import { getErrorMessage, getErrorCode } from '../utils/errors.js';
|
|
35
36
|
const { toFiniteNumber } = Format;
|
|
36
37
|
function stalePlacementDropReason(action, planBoundary) {
|
|
@@ -111,7 +112,7 @@ class COWRebalanceEngine {
|
|
|
111
112
|
fills,
|
|
112
113
|
currentBoundaryIdx: boundaryIdx
|
|
113
114
|
};
|
|
114
|
-
const { targetGrid, boundaryIdx: targetBoundary } = this.strategy.calculateTargetGrid(strategyParams);
|
|
115
|
+
const { targetGrid, boundaryIdx: targetBoundary, consumedCrawlIds } = this.strategy.calculateTargetGrid(strategyParams);
|
|
115
116
|
// P4 / unanchored plan: the strategy returns a null boundary paired
|
|
116
117
|
// with an EMPTY target grid only when no numeric center exists to
|
|
117
118
|
// recover from (e.g. GRID-LOAD rejected the persisted boundary AND
|
|
@@ -139,7 +140,8 @@ class COWRebalanceEngine {
|
|
|
139
140
|
dustThresholdPercent,
|
|
140
141
|
gapSlots: gapSlots ?? undefined,
|
|
141
142
|
evacStreaks: evacStreaks ?? undefined,
|
|
142
|
-
assets: this.assets
|
|
143
|
+
assets: this.assets,
|
|
144
|
+
genesis: manager?._genesis ?? null
|
|
143
145
|
});
|
|
144
146
|
// Gap-evacuation telemetry: evacReady is the streak-tick output of
|
|
145
147
|
// reconcileGrid (stuck in-band candidates). It must survive both
|
|
@@ -297,7 +299,8 @@ class COWRebalanceEngine {
|
|
|
297
299
|
workingGrid,
|
|
298
300
|
workingBoundary: targetBoundary,
|
|
299
301
|
planningDuration: duration,
|
|
300
|
-
refillSlotIds
|
|
302
|
+
refillSlotIds,
|
|
303
|
+
consumedCrawlIds
|
|
301
304
|
}),
|
|
302
305
|
evacReady
|
|
303
306
|
};
|
|
@@ -1712,13 +1715,100 @@ class OrderManager {
|
|
|
1712
1715
|
*
|
|
1713
1716
|
* @param {string} reason - Log/debug label for the drop
|
|
1714
1717
|
*/
|
|
1715
|
-
|
|
1718
|
+
/**
|
|
1719
|
+
* Drop ALL owed fill-crawl records.
|
|
1720
|
+
*
|
|
1721
|
+
* This is the explicit allowed-loss path: the grid generation is being
|
|
1722
|
+
* discarded (rebuild, rejected snapshot, restart replay), so the relative
|
|
1723
|
+
* deltas no longer have a geometry to apply onto.
|
|
1724
|
+
*/
|
|
1725
|
+
_clearPendingFillCrawls(reason = 'unspecified', detail = '') {
|
|
1716
1726
|
if (!Array.isArray(this._pendingFillCrawls) || this._pendingFillCrawls.length === 0)
|
|
1717
1727
|
return;
|
|
1718
|
-
const
|
|
1728
|
+
const dropped = this._pendingFillCrawls;
|
|
1729
|
+
const count = dropped.length;
|
|
1730
|
+
// Side split + age are the two facts needed to tell "the derivation
|
|
1731
|
+
// folded and applied this crawl" from "the commit cleared a crawl the
|
|
1732
|
+
// derivation never consumed" after the fact: a same-sign, same-age run
|
|
1733
|
+
// that did NOT move the boundary accordingly is the second case (the
|
|
1734
|
+
// 2026-10-25 sweep: 7 fills, boundary 148 -> 146, 7 crawls dropped).
|
|
1735
|
+
let buys = 0;
|
|
1736
|
+
let sells = 0;
|
|
1737
|
+
let oldestTs = 0;
|
|
1738
|
+
for (const e of dropped) {
|
|
1739
|
+
if (!e)
|
|
1740
|
+
continue;
|
|
1741
|
+
if (e.side === ORDER_TYPES.BUY)
|
|
1742
|
+
buys++;
|
|
1743
|
+
else if (e.side === ORDER_TYPES.SELL)
|
|
1744
|
+
sells++;
|
|
1745
|
+
const ts = Number(e.ts);
|
|
1746
|
+
if (Number.isFinite(ts) && ts > 0 && (oldestTs === 0 || ts < oldestTs))
|
|
1747
|
+
oldestTs = ts;
|
|
1748
|
+
}
|
|
1749
|
+
const oldestAgeS = oldestTs > 0 ? Math.max(0, Math.round((Date.now() - oldestTs) / 1000)) : null;
|
|
1719
1750
|
this._pendingFillCrawls = [];
|
|
1720
1751
|
this._markGridDirty();
|
|
1721
|
-
this.logger?.log?.(`[BOUNDARY] Dropped ${count} owed fill crawl(s) (${reason})
|
|
1752
|
+
this.logger?.log?.(`[BOUNDARY] Dropped ${count} owed fill crawl(s) (${reason}): ` +
|
|
1753
|
+
`buy=${buys} sell=${sells} net=${sells - buys}` +
|
|
1754
|
+
`${oldestAgeS == null ? '' : ` oldest=${oldestAgeS}s`}` +
|
|
1755
|
+
`${detail ? ` ${detail}` : ''}`, 'debug');
|
|
1756
|
+
}
|
|
1757
|
+
/**
|
|
1758
|
+
* Consume the owed fill-crawl records a derivation actually folded.
|
|
1759
|
+
*
|
|
1760
|
+
* A boundary commit must never clear a record it could not apply: a
|
|
1761
|
+
* truncated cross-chunk shift, an edge-classified slot of an older
|
|
1762
|
+
* generation, or a record re-written for the same slot after the
|
|
1763
|
+
* derivation ran. Clearing all of them was the last way a fill's crawl
|
|
1764
|
+
* could be lost silently (2026-10-25: 7 fills swept slots 147-153, the
|
|
1765
|
+
* commits dropped every record and the boundary ended 7 levels behind).
|
|
1766
|
+
*
|
|
1767
|
+
* `consumed` is the derivation's `consumedCrawlIds`; absent/not-an-array
|
|
1768
|
+
* means the plan never derived a boundary (e.g. a create-only correction),
|
|
1769
|
+
* so NOTHING is consumed. Records are matched by `slotId` + `ts`, so a
|
|
1770
|
+
* newer record for the same slot survives a stale consumption.
|
|
1771
|
+
*/
|
|
1772
|
+
_consumePendingFillCrawls(consumed, reason = 'boundary commit', detail = '') {
|
|
1773
|
+
if (!Array.isArray(this._pendingFillCrawls) || this._pendingFillCrawls.length === 0)
|
|
1774
|
+
return;
|
|
1775
|
+
const before = this._pendingFillCrawls;
|
|
1776
|
+
if (!Array.isArray(consumed)) {
|
|
1777
|
+
this.logger?.log?.(`[BOUNDARY] Kept ${before.length} owed fill crawl(s) unconsumed (${reason})` +
|
|
1778
|
+
`${detail ? `: ${detail}` : ''} — a later derivation still owes them`, 'debug');
|
|
1779
|
+
return;
|
|
1780
|
+
}
|
|
1781
|
+
const key = (e) => {
|
|
1782
|
+
const ts = Number(e?.ts);
|
|
1783
|
+
return `${String(e?.slotId ?? '')}|${Number.isFinite(ts) ? ts : '*'}`;
|
|
1784
|
+
};
|
|
1785
|
+
const consumedKeys = new Set(consumed
|
|
1786
|
+
.filter((e) => !!e
|
|
1787
|
+
&& typeof e.slotId === 'string'
|
|
1788
|
+
&& (e.slotId).length > 0)
|
|
1789
|
+
.map(key));
|
|
1790
|
+
const dropped = before.filter((e) => consumedKeys.has(key(e)));
|
|
1791
|
+
if (dropped.length === 0) {
|
|
1792
|
+
this.logger?.log?.(`[BOUNDARY] Kept ${before.length} owed fill crawl(s) unconsumed (${reason})` +
|
|
1793
|
+
`${detail ? `: ${detail}` : ''} — a later derivation still owes them`, 'debug');
|
|
1794
|
+
return;
|
|
1795
|
+
}
|
|
1796
|
+
const kept = before.filter((e) => !consumedKeys.has(key(e)));
|
|
1797
|
+
const buys = dropped.filter((e) => e?.side === ORDER_TYPES.BUY).length;
|
|
1798
|
+
const sells = dropped.filter((e) => e?.side === ORDER_TYPES.SELL).length;
|
|
1799
|
+
let oldestTs = 0;
|
|
1800
|
+
for (const e of dropped) {
|
|
1801
|
+
const ts = Number(e?.ts);
|
|
1802
|
+
if (Number.isFinite(ts) && ts > 0 && (oldestTs === 0 || ts < oldestTs))
|
|
1803
|
+
oldestTs = ts;
|
|
1804
|
+
}
|
|
1805
|
+
const oldestAgeS = oldestTs > 0 ? Math.max(0, Math.round((Date.now() - oldestTs) / 1000)) : null;
|
|
1806
|
+
this._pendingFillCrawls = kept;
|
|
1807
|
+
this._markGridDirty();
|
|
1808
|
+
this.logger?.log?.(`[BOUNDARY] Dropped ${dropped.length} owed fill crawl(s) (${reason}): ` +
|
|
1809
|
+
`buy=${buys} sell=${sells} net=${sells - buys}` +
|
|
1810
|
+
`${oldestAgeS == null ? '' : ` oldest=${oldestAgeS}s`} kept=${kept.length}` +
|
|
1811
|
+
`${detail ? ` ${detail}` : ''}`, 'debug');
|
|
1722
1812
|
}
|
|
1723
1813
|
/**
|
|
1724
1814
|
* Observability (fix #4, docs/ORDER_ENGINE_POST_1.0_RETROSPECTIVE.md §2): emit the live
|
|
@@ -1803,23 +1893,39 @@ class OrderManager {
|
|
|
1803
1893
|
}
|
|
1804
1894
|
/**
|
|
1805
1895
|
* Record last filled price for the single-pivot guard: when armed, BOTH
|
|
1806
|
-
* sides are gated against the most recent fill price
|
|
1807
|
-
* pivot
|
|
1808
|
-
* isLastFillGuardBlocked).
|
|
1809
|
-
*
|
|
1810
|
-
*
|
|
1811
|
-
*
|
|
1896
|
+
* sides are gated by the direction rule against the most recent fill price
|
|
1897
|
+
* (BUY blocked at/above the pivot, SELL blocked at/below it; see
|
|
1898
|
+
* isLastFillGuardBlocked). _lastFilledType is purely the cold/armed
|
|
1899
|
+
* discriminator (null => disabled). The per-side fields are not read by the
|
|
1900
|
+
* guard itself; they feed the book-seed cold-check and per-side seeding in
|
|
1901
|
+
* seedLastFilledPricesFromBook.
|
|
1902
|
+
*
|
|
1903
|
+
* EDGE-PINNED FILLS ARE SKIPPED. An edge order is static fat-finger
|
|
1904
|
+
* insurance resting at the grid floor/ceiling, not market movement: the
|
|
1905
|
+
* boundary crawl already excludes its fills, and the pivot doubles as the
|
|
1906
|
+
* geometry anchor for reanchorBoundaryFromLastFill. Letting a wick on an
|
|
1907
|
+
* edge order arm the pivot at a rail-edge price would both veto that whole
|
|
1908
|
+
* side of the book and (as an absolute anchor) teleport the window to the
|
|
1909
|
+
* edge. Skipping keeps the reference on the last WINDOW trade, matching
|
|
1910
|
+
* the crawl's classification (collectEdgePinnedFillIds is the shared
|
|
1911
|
+
* definition).
|
|
1812
1912
|
* @param {Array} fills
|
|
1813
1913
|
*/
|
|
1814
1914
|
recordLastFilledPrices(fills) {
|
|
1815
1915
|
if (!Array.isArray(fills) || fills.length === 0)
|
|
1816
1916
|
return;
|
|
1917
|
+
const edgeFillIds = collectEdgePinnedFillIds(this, fills);
|
|
1817
1918
|
let recorded = 0;
|
|
1919
|
+
let skippedEdge = 0;
|
|
1818
1920
|
let lastKind = 'unknown';
|
|
1819
1921
|
let lastPriceSrc = 'direct';
|
|
1820
1922
|
for (const f of fills) {
|
|
1821
1923
|
if (!f || (f.type !== ORDER_TYPES.BUY && f.type !== ORDER_TYPES.SELL))
|
|
1822
1924
|
continue;
|
|
1925
|
+
if (f.id != null && edgeFillIds.has(String(f.id))) {
|
|
1926
|
+
skippedEdge++;
|
|
1927
|
+
continue;
|
|
1928
|
+
}
|
|
1823
1929
|
let price = Number(f.price ?? f.order?.price);
|
|
1824
1930
|
let priceSrc = 'direct';
|
|
1825
1931
|
if (!Number.isFinite(price) || price <= 0) {
|
|
@@ -1849,7 +1955,14 @@ class OrderManager {
|
|
|
1849
1955
|
const fmt = (v) => (v == null || !Number.isFinite(Number(v)) ? 'none' : Format.formatPrice6(Number(v)));
|
|
1850
1956
|
this.logger?.log?.(`[FILL-PIVOT] pivot=${fmt(this._lastFilledPrice)}(${this._lastFilledType}) ` +
|
|
1851
1957
|
`buy=${fmt(this._lastFilledBuyPrice)} sell=${fmt(this._lastFilledSellPrice)} ` +
|
|
1852
|
-
`n=${recorded} src=${lastKind}/${lastPriceSrc}
|
|
1958
|
+
`n=${recorded} src=${lastKind}/${lastPriceSrc}` +
|
|
1959
|
+
`${skippedEdge > 0 ? ` edge-skipped=${skippedEdge}` : ''}`, 'info');
|
|
1960
|
+
}
|
|
1961
|
+
catch { /* logging is best-effort */ }
|
|
1962
|
+
}
|
|
1963
|
+
else if (skippedEdge > 0) {
|
|
1964
|
+
try {
|
|
1965
|
+
this.logger?.log?.(`[FILL-PIVOT] ${skippedEdge} edge-pinned fill(s) ignored — the guard pivot and the boundary anchor stay on the last window trade`, 'info');
|
|
1853
1966
|
}
|
|
1854
1967
|
catch { /* logging is best-effort */ }
|
|
1855
1968
|
}
|
|
@@ -2055,7 +2168,8 @@ class OrderManager {
|
|
|
2055
2168
|
dustThresholdPercent: this.config?.gridLimits?.PARTIAL_DUST_THRESHOLD_PERCENTAGE,
|
|
2056
2169
|
gapSlots: this._gapSlots,
|
|
2057
2170
|
evacStreaks: this._gapEvacStreaks,
|
|
2058
|
-
assets: this.assets
|
|
2171
|
+
assets: this.assets,
|
|
2172
|
+
genesis: this._genesis ?? null
|
|
2059
2173
|
});
|
|
2060
2174
|
}
|
|
2061
2175
|
/**
|
|
@@ -2109,9 +2223,17 @@ class OrderManager {
|
|
|
2109
2223
|
const chainOrderId = candidate?.orderId;
|
|
2110
2224
|
if (!slotId || !chainOrderId)
|
|
2111
2225
|
continue;
|
|
2112
|
-
// 1.
|
|
2113
|
-
|
|
2114
|
-
|
|
2226
|
+
// 1. Live slot check + geometry re-verification against the CURRENT
|
|
2227
|
+
// committed boundary. Prefer the order's LIVE chain price: a
|
|
2228
|
+
// refused-commit adoption can leave the local slot id in-rail while
|
|
2229
|
+
// the order actually rests inside the band.
|
|
2230
|
+
const slot = this.orders.get(slotId);
|
|
2231
|
+
if (!slot || !isOrderOnChain(slot) || slot.orderId !== chainOrderId)
|
|
2232
|
+
continue;
|
|
2233
|
+
const live = resolveLiveOrderLadderPosition(slot, this.assets, this._genesis);
|
|
2234
|
+
// Unknown ladder position is not evidence of an in-band order.
|
|
2235
|
+
if (live.idx === null || !Number.isFinite(Number(live.idx))
|
|
2236
|
+
|| geometryTypeForSlotIndex(live.idx, boundary, gapSlots) !== ORDER_TYPES.SPREAD)
|
|
2115
2237
|
continue;
|
|
2116
2238
|
// 2. Act threshold (stricter than the warn threshold).
|
|
2117
2239
|
if (Number(this._gapEvacStreaks.get(slotId) || 0) < cancelThreshold)
|
|
@@ -2121,10 +2243,6 @@ class OrderManager {
|
|
|
2121
2243
|
continue;
|
|
2122
2244
|
if (this.ordersNeedingPriceCorrection.some((entry) => entry?.chainOrderId === chainOrderId))
|
|
2123
2245
|
continue;
|
|
2124
|
-
// 5. Live slot check.
|
|
2125
|
-
const slot = this.orders.get(slotId);
|
|
2126
|
-
if (!slot || !isOrderOnChain(slot) || slot.orderId !== chainOrderId)
|
|
2127
|
-
continue;
|
|
2128
2246
|
this.ordersNeedingPriceCorrection.push({
|
|
2129
2247
|
gridOrder: { ...slot },
|
|
2130
2248
|
chainOrderId,
|
|
@@ -2432,7 +2550,15 @@ class OrderManager {
|
|
|
2432
2550
|
// left for the next derivation, holes refilled same-side).
|
|
2433
2551
|
if (boundaryCheck.ok && commitBoundary !== null && commitBoundary !== undefined
|
|
2434
2552
|
&& boundaryHeld !== true) {
|
|
2435
|
-
|
|
2553
|
+
// Carry the boundary delta into the drop log: a crawl whose
|
|
2554
|
+
// net direction the commit did NOT honor is the ledger-leak
|
|
2555
|
+
// signature (see _clearPendingFillCrawls). Observability
|
|
2556
|
+
// only — the clear itself is unchanged pending that evidence.
|
|
2557
|
+
const prevBoundary = this.boundaryIdx;
|
|
2558
|
+
// Scope the drop to what the plan's derivation folded: a
|
|
2559
|
+
// record it could not apply (truncated shift, edge slot,
|
|
2560
|
+
// re-recorded slot) stays owed for the next derivation.
|
|
2561
|
+
this._consumePendingFillCrawls(options?.consumedCrawlIds, 'boundary commit', `boundary ${prevBoundary ?? 'null'} -> ${commitBoundary}`);
|
|
2436
2562
|
}
|
|
2437
2563
|
this._gridVersion++;
|
|
2438
2564
|
committed = true;
|
|
@@ -156,6 +156,11 @@ declare class StrategyEngine {
|
|
|
156
156
|
}): {
|
|
157
157
|
targetGrid: Map<any, any>;
|
|
158
158
|
boundaryIdx: number | null;
|
|
159
|
+
consumedCrawlIds?: undefined;
|
|
160
|
+
} | {
|
|
161
|
+
targetGrid: Map<string, ManagedOrder>;
|
|
162
|
+
boundaryIdx: number;
|
|
163
|
+
consumedCrawlIds: import("./utils/order.js").CrawlEntry[];
|
|
159
164
|
};
|
|
160
165
|
}
|
|
161
166
|
export default StrategyEngine;
|
|
@@ -50,8 +50,10 @@ import { ORDER_TYPES, ORDER_STATES } from '../constants.js';
|
|
|
50
50
|
import { calculateGapSlots } from './grid.js';
|
|
51
51
|
import { isSlotInRail } from './utils/math.js';
|
|
52
52
|
import { deriveTargetBoundary, getSideBudget, calculateBudgetedSizes, getCoreOrdersTotal, resolveEdgeCount, resolveLiveEdgeAnchorPrice, selectEdgeSlots, isShiftEligibleFill } from './utils/order.js';
|
|
53
|
+
import { reanchorBoundaryFromLastFill } from './utils/system.js';
|
|
53
54
|
import { assignGridRoles } from './utils/order.js';
|
|
54
|
-
import { convertToSpreadPlaceholder, toRailHolePlaceholder, geometryTypeForSlotIndex,
|
|
55
|
+
import { convertToSpreadPlaceholder, toRailHolePlaceholder, geometryTypeForSlotIndex, hasOnChainId, isOrderPlaced } from "./utils/order.js";
|
|
56
|
+
import { parseSlotIndex } from "./utils/slot.js";
|
|
55
57
|
class StrategyEngine {
|
|
56
58
|
manager;
|
|
57
59
|
/**
|
|
@@ -259,10 +261,44 @@ class StrategyEngine {
|
|
|
259
261
|
buy: resolveLiveEdgeAnchorPrice(this.manager, 'buy'),
|
|
260
262
|
sell: resolveLiveEdgeAnchorPrice(this.manager, 'sell'),
|
|
261
263
|
};
|
|
262
|
-
|
|
264
|
+
let { boundaryIdx: newBoundaryIdx, remainingBudget, consumedCrawlIds } = deriveTargetBoundary(fills, currentBoundaryIdx, allSlots, boundaryConfig, gapSlots, crossChunkBudget, this.manager._pendingFillCrawls, edgeAnchors);
|
|
263
265
|
if (crossChunkBudget != null) {
|
|
264
266
|
this.manager._boundaryShiftBudget = remainingBudget;
|
|
265
267
|
}
|
|
268
|
+
// GEOMETRY RE-ANCHOR: the crawl is +-1 slot per committed fill, so a
|
|
269
|
+
// window that ended up BEHIND the newest real fill can never catch up —
|
|
270
|
+
// every tightening slot on that side is then at/through the fill and the
|
|
271
|
+
// last-fill guard refuses all of them while the funds sit in the resting
|
|
272
|
+
// inventory. The fill is the anchor: place it outside its own window.
|
|
273
|
+
// Validated like a boundary commit (strands no live in-band order), and
|
|
274
|
+
// only for an on-ladder fill price.
|
|
275
|
+
const reanchor = reanchorBoundaryFromLastFill(this.manager, newBoundaryIdx, allSlots, gapSlots, fills);
|
|
276
|
+
if (reanchor.reanchored) {
|
|
277
|
+
newBoundaryIdx = reanchor.boundaryIdx;
|
|
278
|
+
// An absolute fill anchor already reflects every older owed crawl.
|
|
279
|
+
// The derivation may have left records owed (cap exhausted, mixed
|
|
280
|
+
// remainder), so consuming only its subset would let a later
|
|
281
|
+
// derivation apply those deltas ON TOP of the re-anchored boundary
|
|
282
|
+
// and overshoot. Snapshot at PLAN time (not commit time): a fill
|
|
283
|
+
// recorded after this plan must stay owed and crawl next cycle.
|
|
284
|
+
const pendingAtPlan = Array.isArray(this.manager._pendingFillCrawls)
|
|
285
|
+
? this.manager._pendingFillCrawls
|
|
286
|
+
: [];
|
|
287
|
+
consumedCrawlIds = pendingAtPlan
|
|
288
|
+
.filter((e) => e && typeof e.slotId === 'string' && e.slotId.length > 0)
|
|
289
|
+
.map((e) => {
|
|
290
|
+
const ts = Number(e.ts);
|
|
291
|
+
return {
|
|
292
|
+
slotId: e.slotId,
|
|
293
|
+
side: String(e.side ?? ''),
|
|
294
|
+
...(Number.isFinite(ts) ? { ts } : {})
|
|
295
|
+
};
|
|
296
|
+
});
|
|
297
|
+
try {
|
|
298
|
+
this.manager._markGridDirty?.();
|
|
299
|
+
}
|
|
300
|
+
catch { /* best-effort */ }
|
|
301
|
+
}
|
|
266
302
|
// Unanchorable (null boundary, no numeric center anywhere): refuse to
|
|
267
303
|
// plan rotations on fabricated geometry. The COW engine routes this
|
|
268
304
|
// to a structural resync instead.
|
|
@@ -417,7 +453,12 @@ class StrategyEngine {
|
|
|
417
453
|
});
|
|
418
454
|
return {
|
|
419
455
|
targetGrid: targetGrid,
|
|
420
|
-
boundaryIdx: newBoundaryIdx
|
|
456
|
+
boundaryIdx: newBoundaryIdx,
|
|
457
|
+
// Records whose shift this derivation actually folded. The commit
|
|
458
|
+
// clears ONLY these, so a record it could not apply (truncated
|
|
459
|
+
// shift, edge classification, a newer record for the same slot)
|
|
460
|
+
// stays owed instead of vanishing with the commit.
|
|
461
|
+
consumedCrawlIds
|
|
421
462
|
};
|
|
422
463
|
}
|
|
423
464
|
}
|
|
@@ -141,6 +141,16 @@ interface FillTransitionParams {
|
|
|
141
141
|
blockNum?: number;
|
|
142
142
|
historyId?: string;
|
|
143
143
|
isMaker: boolean;
|
|
144
|
+
/**
|
|
145
|
+
* Execution price (quote per base) from the chain fill / chain order, when
|
|
146
|
+
* known. The fill record must carry the REAL trade price: `matchedGridOrder`
|
|
147
|
+
* is keyed by slot, and a refused-commit adoption can leave that slot at a
|
|
148
|
+
* stale generation price (e.g. slot-118 @1267 while the chain order traded
|
|
149
|
+
* at 1345.7). The last-fill pivot / re-anchor read this field, never the
|
|
150
|
+
* slot price. The slot's own identity price is intentionally NOT rewritten
|
|
151
|
+
* (GRID-PRICE-INVARIANT).
|
|
152
|
+
*/
|
|
153
|
+
fillPrice?: number | null;
|
|
144
154
|
}
|
|
145
155
|
interface SyncChainData {
|
|
146
156
|
gridOrderId?: string;
|
|
@@ -290,7 +300,7 @@ declare class SyncEngine {
|
|
|
290
300
|
unmatchedChainOrders: UnmatchedChainOrder[];
|
|
291
301
|
}>;
|
|
292
302
|
_findMatchingGridOrder(mgr: OrderManagerLike, orderId: string | null | undefined): ManagedOrder | null;
|
|
293
|
-
_computeFillContext(mgr: OrderManagerLike, matchedGridOrder: ManagedOrder, paysAssetId: string | null | undefined, paysAmountRaw: number): {
|
|
303
|
+
_computeFillContext(mgr: OrderManagerLike, matchedGridOrder: ManagedOrder, paysAssetId: string | null | undefined, paysAmountRaw: number, receivesAssetId?: string | null | undefined, receivesAmountRaw?: number | null): {
|
|
294
304
|
orderType: OrderType;
|
|
295
305
|
currentSize: number;
|
|
296
306
|
precision: number;
|
|
@@ -298,6 +308,7 @@ declare class SyncEngine {
|
|
|
298
308
|
currentSizeIntFromGrid: number;
|
|
299
309
|
rawForSaleInt: number;
|
|
300
310
|
driftSignal: boolean;
|
|
311
|
+
fillPrice: number | null;
|
|
301
312
|
};
|
|
302
313
|
_computeFillTransitionResult(mgr: OrderManagerLike, params: FillTransitionParams): Promise<{
|
|
303
314
|
isFull: boolean;
|