dexbot 1.6.12 → 1.6.13

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 (170) hide show
  1. package/CHANGELOG.md +33 -1
  2. package/analysis/README.md +9 -11
  3. package/analysis/ama_fitting/README.md +5 -3
  4. package/claw/README.md +1 -0
  5. package/claw/docs/AI_BOT_LIBRARY_API.md +2 -0
  6. package/claw/modules/claw_bridge.ts +0 -3
  7. package/claw/modules/claw_skill_md.ts +1 -0
  8. package/claw/package.json +3 -3
  9. package/claw/runtimes/openclaw-plugin/openclaw.plugin.json +1 -1
  10. package/claw/runtimes/openclaw-plugin/package.json +1 -1
  11. package/claw/tsconfig.json +2 -2
  12. package/dist/analysis/ama_fitting/analyze_lambda_vs_slow.js +1 -14
  13. package/dist/analysis/ama_fitting/fetch_lp_candles.js +51 -41
  14. package/dist/analysis/ama_fitting/generate_unified_comparison_chart.d.ts +1 -24
  15. package/dist/analysis/ama_fitting/generate_unified_comparison_chart.js +36 -18
  16. package/dist/analysis/ama_fitting/optimizer_high_resolution.js +18 -66
  17. package/dist/analysis/analyze_args.d.ts +12 -0
  18. package/dist/analysis/analyze_args.js +20 -0
  19. package/dist/analysis/bot_fitting/backtest_ama_sweep.d.ts +1 -16
  20. package/dist/analysis/bot_fitting/backtest_ama_sweep.js +20 -101
  21. package/dist/analysis/bot_fitting/backtest_bot_fitting.js +26 -92
  22. package/dist/analysis/bot_fitting/backtest_lookback_drawdown.js +19 -34
  23. package/dist/analysis/bot_fitting/shared_utils.d.ts +71 -3
  24. package/dist/analysis/bot_fitting/shared_utils.js +124 -8
  25. package/dist/analysis/chart_ui.d.ts +30 -1
  26. package/dist/analysis/chart_ui.js +37 -1
  27. package/dist/analysis/fills_cache.d.ts +3 -23
  28. package/dist/analysis/fills_cache.js +3 -84
  29. package/dist/analysis/fills_source.d.ts +31 -1
  30. package/dist/analysis/fills_source.js +69 -1
  31. package/dist/analysis/grid_correction_check.d.ts +1 -23
  32. package/dist/analysis/grid_correction_check.js +15 -58
  33. package/dist/analysis/math_utils.d.ts +85 -1
  34. package/dist/analysis/math_utils.js +116 -1
  35. package/dist/analysis/price_sources.d.ts +22 -1
  36. package/dist/analysis/price_sources.js +23 -0
  37. package/dist/analysis/resolve_source.js +31 -10
  38. package/dist/analysis/trade_profitability.d.ts +6 -0
  39. package/dist/analysis/trade_profitability.js +23 -97
  40. package/dist/analysis/tradingview/analyze_tradingview.d.ts +1 -17
  41. package/dist/analysis/tradingview/analyze_tradingview.js +1 -1
  42. package/dist/analysis/tradingview/tradingview_uplot_chart_generator.js +2 -15
  43. package/dist/analysis/trend_detection/backtest_ama_slope_huber.d.ts +1 -101
  44. package/dist/analysis/trend_detection/backtest_ama_slope_huber.js +22 -80
  45. package/dist/analysis/trend_detection/dynamic_weight_chart_generator.js +11 -37
  46. package/dist/analysis/trend_detection/volatility_chart_generator.js +9 -10
  47. package/dist/analysis/worker_pool.d.ts +38 -0
  48. package/dist/analysis/worker_pool.js +67 -0
  49. package/dist/dexbot.js +67 -48
  50. package/dist/market_adapter/ama_signal_runner.js +21 -54
  51. package/dist/market_adapter/candle_utils.d.ts +7 -1
  52. package/dist/market_adapter/candle_utils.js +11 -1
  53. package/dist/market_adapter/cli_args.d.ts +22 -0
  54. package/dist/market_adapter/cli_args.js +30 -0
  55. package/dist/market_adapter/core/config_normalizers.js +11 -6
  56. package/dist/market_adapter/core/kibana_candles.d.ts +3 -1
  57. package/dist/market_adapter/core/kibana_candles.js +3 -2
  58. package/dist/market_adapter/core/market_adapter_service.d.ts +85 -85
  59. package/dist/market_adapter/core/market_adapter_service.js +93 -158
  60. package/dist/market_adapter/core/signals/hurst_analyzer.d.ts +1 -6
  61. package/dist/market_adapter/core/signals/hurst_analyzer.js +1 -1
  62. package/dist/market_adapter/core/signals/permutation_entropy_analyzer.d.ts +1 -11
  63. package/dist/market_adapter/core/signals/permutation_entropy_analyzer.js +1 -1
  64. package/dist/market_adapter/inputs/fetch_book_data.js +0 -1
  65. package/dist/market_adapter/inputs/fetch_cex_synthetic_data.js +63 -107
  66. package/dist/market_adapter/inputs/fetch_lp_data.d.ts +8 -1
  67. package/dist/market_adapter/inputs/fetch_lp_data.js +25 -7
  68. package/dist/market_adapter/inputs/kibana_feed_source.js +4 -26
  69. package/dist/market_adapter/inputs/window_cache.d.ts +4 -72
  70. package/dist/market_adapter/inputs/window_cache.js +52 -177
  71. package/dist/market_adapter/interval_utils.d.ts +11 -1
  72. package/dist/market_adapter/interval_utils.js +16 -1
  73. package/dist/market_adapter/lp_chart_runner.d.ts +3 -0
  74. package/dist/market_adapter/lp_chart_runner.js +37 -19
  75. package/dist/market_adapter/market_adapter.js +27 -72
  76. package/dist/market_adapter/utils/data_discovery.d.ts +45 -1
  77. package/dist/market_adapter/utils/data_discovery.js +220 -27
  78. package/dist/market_adapter/utils/month_shards.d.ts +84 -0
  79. package/dist/market_adapter/utils/month_shards.js +237 -0
  80. package/dist/modules/account_bots.d.ts +1 -0
  81. package/dist/modules/account_bots.js +102 -121
  82. package/dist/modules/account_orders.d.ts +13 -0
  83. package/dist/modules/account_orders.js +38 -44
  84. package/dist/modules/bitshares-native/chain_client.d.ts +1 -1
  85. package/dist/modules/bitshares-native/chain_client.js +14 -13
  86. package/dist/modules/bitshares-native/resolvers.js +22 -25
  87. package/dist/modules/bitshares-native/serial/types.js +22 -50
  88. package/dist/modules/bitshares-native/signing_client.d.ts +1 -1
  89. package/dist/modules/bot_defaults.d.ts +2 -1
  90. package/dist/modules/bot_settings.d.ts +1 -1
  91. package/dist/modules/broadcast_failure.d.ts +2 -1
  92. package/dist/modules/chain_orders.d.ts +1 -1
  93. package/dist/modules/chain_orders.js +51 -96
  94. package/dist/modules/cli_start_onboarding.d.ts +2 -1
  95. package/dist/modules/constants.d.ts +7 -1
  96. package/dist/modules/constants.js +20 -1
  97. package/dist/modules/cr_planner.d.ts +1 -1
  98. package/dist/modules/cr_planner.js +1 -5
  99. package/dist/modules/credit_pricing.d.ts +2 -1
  100. package/dist/modules/credit_pricing.js +36 -40
  101. package/dist/modules/credit_runtime.d.ts +7 -0
  102. package/dist/modules/credit_runtime.js +45 -73
  103. package/dist/modules/daemon_node_health.d.ts +4 -3
  104. package/dist/modules/dexbot_class.d.ts +28 -17
  105. package/dist/modules/dexbot_class.js +105 -58
  106. package/dist/modules/dexbot_cow_runtime.d.ts +0 -7
  107. package/dist/modules/dexbot_cow_runtime.js +54 -28
  108. package/dist/modules/dexbot_credential_client.d.ts +0 -7
  109. package/dist/modules/dexbot_credential_client.js +1 -2
  110. package/dist/modules/dexbot_fill_runtime.d.ts +1 -2
  111. package/dist/modules/dexbot_fill_runtime.js +5 -6
  112. package/dist/modules/dexbot_maintenance_runtime.d.ts +20 -3
  113. package/dist/modules/dexbot_maintenance_runtime.js +84 -69
  114. package/dist/modules/dexbot_startup_runtime.d.ts +0 -1
  115. package/dist/modules/dexbot_startup_runtime.js +13 -14
  116. package/dist/modules/general_settings.d.ts +1 -1
  117. package/dist/modules/graceful_shutdown.js +17 -11
  118. package/dist/modules/key_store.js +0 -1
  119. package/dist/modules/lazy_shim.d.ts +11 -0
  120. package/dist/modules/lazy_shim.js +16 -0
  121. package/dist/modules/node_connect_policy.d.ts +3 -2
  122. package/dist/modules/node_failure_ledger.d.ts +6 -5
  123. package/dist/modules/order/format.d.ts +12 -3
  124. package/dist/modules/order/format.js +15 -3
  125. package/dist/modules/order/genesis_policy.d.ts +3 -3
  126. package/dist/modules/order/genesis_policy.js +5 -2
  127. package/dist/modules/order/grid.d.ts +4 -2
  128. package/dist/modules/order/grid.js +91 -35
  129. package/dist/modules/order/index.d.ts +1 -0
  130. package/dist/modules/order/logger.d.ts +1 -8
  131. package/dist/modules/order/logger.js +0 -9
  132. package/dist/modules/order/manager.d.ts +9 -13
  133. package/dist/modules/order/manager.js +33 -50
  134. package/dist/modules/order/utils/order.js +39 -35
  135. package/dist/modules/order/utils/system.d.ts +9 -29
  136. package/dist/modules/order/utils/system.js +17 -2
  137. package/dist/modules/order/utils/validate.js +2 -2
  138. package/dist/modules/order/utils/withPoolRef.d.ts +2 -1
  139. package/dist/modules/paths.js +32 -34
  140. package/dist/modules/runtime_settings.d.ts +2 -4
  141. package/dist/modules/settings_merge.d.ts +2 -2
  142. package/dist/modules/socket_json_client.d.ts +2 -1
  143. package/dist/modules/types.d.ts +42 -27
  144. package/dist/modules/utils/errors.js +1 -3
  145. package/dist/modules/version_notice.d.ts +11 -10
  146. package/dist/pm2.d.ts +0 -1
  147. package/dist/pm2.js +56 -35
  148. package/dist/scripts/analyze-git.js +2 -15
  149. package/dist/scripts/chart_command.js +2 -2
  150. package/dist/scripts/check_any.d.ts +4 -0
  151. package/dist/scripts/check_any.js +53 -21
  152. package/dist/scripts/diagnose-pool-history.js +1 -6
  153. package/dist/scripts/run-tests.js +0 -1
  154. package/dist/scripts/sync-version.js +0 -10
  155. package/docs/BITSHARES_ONBOARDING.md +8 -12
  156. package/docs/COPY_ON_WRITE_MASTER_PLAN.md +1 -1
  157. package/docs/DEXBOT_COMPARISON.md +6 -6
  158. package/docs/EVOLUTION.md +9 -8
  159. package/docs/FUND_MOVEMENT_AND_ACCOUNTING.md +1 -1
  160. package/docs/README.md +1 -1
  161. package/docs/WORKFLOW.md +1 -0
  162. package/docs/architecture.md +19 -10
  163. package/market_adapter/README.md +6 -4
  164. package/package.json +6 -9
  165. package/scripts/README.md +43 -38
  166. package/tests/README.md +25 -10
  167. package/dist/scripts/diagnose-kibana-candles.d.ts +0 -3
  168. package/dist/scripts/diagnose-kibana-candles.js +0 -82
  169. package/scripts/git-viewer.sh +0 -489
  170. package/scripts/postinstall.js +0 -16
package/dist/pm2.js CHANGED
@@ -79,6 +79,7 @@ const require = createRequire(import.meta.url);
79
79
  import { setUmask } from './modules/config.js';
80
80
  import { path } from './modules/path_api.js';
81
81
  import { spawn, execSync } from 'node:child_process';
82
+ import { existsSync } from 'node:fs';
82
83
  import { parseJsonWithComments } from './modules/order/utils/system.js';
83
84
  import { readBotsFileWithLock } from './modules/bots_file_lock.js';
84
85
  import { loadSettingsFile, selectActiveBotEntries } from './modules/bot_settings.js';
@@ -208,8 +209,7 @@ function buildEcosystemApps(bots, { includeUpdater = true } = {}) {
208
209
  error_file: path.join(LOGS_DIR, `${botName}-error.log`),
209
210
  out_file: path.join(LOGS_DIR, `${botName}.log`),
210
211
  log_date_format: 'YY-MM-DD HH:mm:ss.SSS',
211
- merge_logs: false,
212
- combine_logs: true,
212
+ merge_logs: true,
213
213
  max_size: '100M',
214
214
  max_restarts: 13,
215
215
  min_uptime: 86400000,
@@ -227,8 +227,7 @@ function buildEcosystemApps(bots, { includeUpdater = true } = {}) {
227
227
  error_file: path.join(LOGS_DIR, 'dexbot-adapter-error.log'),
228
228
  out_file: path.join(LOGS_DIR, 'dexbot-adapter.log'),
229
229
  log_date_format: 'YY-MM-DD HH:mm:ss.SSS',
230
- merge_logs: false,
231
- combine_logs: true,
230
+ merge_logs: true,
232
231
  max_size: '100M',
233
232
  max_restarts: 13,
234
233
  min_uptime: 60000,
@@ -259,8 +258,7 @@ function buildCredentialDaemonApp({ credentialEnv = {} } = {}) {
259
258
  error_file: path.join(LOGS_DIR, 'dexbot-cred-error.log'),
260
259
  out_file: path.join(LOGS_DIR, 'dexbot-cred.log'),
261
260
  log_date_format: 'YY-MM-DD HH:mm:ss.SSS',
262
- merge_logs: false,
263
- combine_logs: true,
261
+ merge_logs: true,
264
262
  max_size: '100M',
265
263
  env: {
266
264
  DEXBOT_CRED_DAEMON_SOCKET: CREDENTIAL_SOCKET_PATH,
@@ -657,23 +655,35 @@ async function ensurePm2Logrotate() {
657
655
  }
658
656
  }
659
657
  /**
660
- * Check if PM2 is installed locally or globally.
661
- * @returns {boolean} True if PM2 is found.
658
+ * Resolve the npm CLI that belongs to the running Node.
659
+ *
660
+ * The npm beside `process.execPath` shares this Node's global root; a version
661
+ * manager (nvm/fnm/volta) can put a different Node's npm first on PATH, which
662
+ * would install pm2 into a prefix this Node cannot see. Falls back to the bare
663
+ * name (PATH lookup) for unusual launcher layouts such as npx or shims.
664
+ *
665
+ * On Windows the shim is spawned through the shell, where an absolute path
666
+ * such as `C:\Program Files\nodejs\npm.cmd` would need quoting; the PATH
667
+ * lookup already resolves the right shim, so keep the bare name there.
668
+ * @returns {string} Absolute npm path, or 'npm' when none is found.
669
+ */
670
+ function resolveNpmBinary() {
671
+ if (process.platform === 'win32')
672
+ return 'npm';
673
+ const candidate = path.join(path.dirname(process.execPath), 'npm');
674
+ return existsSync(candidate) ? candidate : 'npm';
675
+ }
676
+ /**
677
+ * Check whether the pm2 CLI is reachable.
678
+ * @returns {boolean} True if `pm2 --version` succeeds.
662
679
  */
663
680
  function checkPM2Installed() {
664
681
  try {
665
- require.resolve('pm2');
682
+ execSync('pm2 --version', { stdio: 'ignore' });
666
683
  return true;
667
684
  }
668
- catch (e) {
669
- // Check if pm2 is available in PATH
670
- try {
671
- execSync('pm2 --version', { stdio: 'ignore' });
672
- return true;
673
- }
674
- catch (err) {
675
- return false;
676
- }
685
+ catch (err) {
686
+ return false;
677
687
  }
678
688
  }
679
689
  /**
@@ -688,11 +698,13 @@ async function installPM2() {
688
698
  return new Promise((resolve, reject) => {
689
699
  rl.question('PM2 is not installed. Install now? (Y/n): ', (answer) => {
690
700
  rl.close();
691
- if (answer.toLowerCase() === 'n') {
701
+ const normalized = answer.trim().toLowerCase();
702
+ if (normalized === 'n' || normalized === 'no') {
692
703
  console.log('PM2 installation cancelled. Run: npm install -g pm2');
693
704
  process.exit(1);
694
705
  }
695
706
  console.log('Installing PM2...');
707
+ const npmBin = resolveNpmBinary();
696
708
  // Helper to run installation command
697
709
  const runInstall = (command, args) => {
698
710
  return new Promise((res, rej) => {
@@ -709,22 +721,29 @@ async function installPM2() {
709
721
  proc.on('error', (err) => rej(err));
710
722
  });
711
723
  };
724
+ // npm can report success while writing into a global bin dir that is
725
+ // not on this PATH, so confirm `pm2` is actually reachable before
726
+ // declaring victory.
727
+ const confirmInstalled = (message) => {
728
+ if (checkPM2Installed()) {
729
+ console.log(pm2Success(message));
730
+ resolve();
731
+ }
732
+ else {
733
+ console.error(pm2Error('PM2 was installed, but `pm2` is not on PATH. Add the npm global bin directory to PATH and rerun.'));
734
+ reject(new Error('PM2 installed but not found on PATH'));
735
+ }
736
+ };
712
737
  // Try standard install first
713
- runInstall('npm', ['install', '-g', 'pm2'])
714
- .then(() => {
715
- console.log(pm2Success('PM2 installed successfully!'));
716
- resolve();
717
- })
738
+ runInstall(npmBin, ['install', '-g', 'pm2'])
739
+ .then(() => confirmInstalled('PM2 installed successfully!'))
718
740
  .catch((_err) => {
719
741
  // If failed and not on Windows, try sudo
720
742
  if (process.platform !== 'win32') {
721
743
  console.log('\nStandard installation failed (likely permissions). Trying with sudo...');
722
744
  console.log('This covers Linux and macOS. Please enter your password if prompted:');
723
- runInstall('sudo', ['npm', 'install', '-g', 'pm2'])
724
- .then(() => {
725
- console.log(pm2Success('PM2 installed successfully with sudo!'));
726
- resolve();
727
- })
745
+ runInstall('sudo', [npmBin, 'install', '-g', 'pm2'])
746
+ .then(() => confirmInstalled('PM2 installed successfully with sudo!'))
728
747
  .catch((_finalErr) => {
729
748
  reject(new Error('PM2 installation failed even with sudo'));
730
749
  });
@@ -782,6 +801,8 @@ async function execPM2Command(action, target, { suppressStderrOnError = false, s
782
801
  args.push(target);
783
802
  }
784
803
  const pm2 = spawn('pm2', args, {
804
+ cwd: PATHS.PROJECT_ROOT,
805
+ env: buildScopedChildEnv(),
785
806
  stdio: 'pipe',
786
807
  shell: process.platform === 'win32'
787
808
  });
@@ -837,9 +858,9 @@ async function stopPM2Processes(target) {
837
858
  console.log(`Stopping PM2 processes: ${target}`);
838
859
  if (target === 'all') {
839
860
  console.log('');
840
- // Stop credential daemon first — it holds the only handle to keys.
841
- // If managed apps were stopped first they'd lose key access; this order
842
- // ensures the daemon is the last process released.
861
+ // Stop the credential daemon first: it holds the only handle to keys,
862
+ // so releasing it up front is the safety stop. Managed apps are stopped
863
+ // afterwards; they can no longer sign, but they are already shutting down.
843
864
  await execPM2CommandIgnoreMissing('stop', CREDENTIAL_DAEMON_APP_NAME);
844
865
  if (storage.exists(ECOSYSTEM_FILE)) {
845
866
  await runManagedAppsPm2Action('stop');
@@ -932,18 +953,18 @@ async function restartPM2Processes(target, { headless = false, passwordFile = nu
932
953
  console.log(`Restarting PM2 processes: ${target}`);
933
954
  if (target === 'all') {
934
955
  generateEcosystemConfig({ clawOnly: false, exitOnError: false });
935
- await ensureCredentialDaemonPM2({ logReuse: false, headless, passwordFile });
956
+ await ensureCredentialDaemonPM2({ headless, passwordFile });
936
957
  await runManagedAppsPm2Action('restart');
937
958
  console.log('Managed dexbot PM2 apps restarted. dexbot-cred was left on the safe wrapper path.');
938
959
  return;
939
960
  }
940
961
  if (target === CREDENTIAL_DAEMON_APP_NAME) {
941
- await ensureCredentialDaemonPM2({ forceRefresh: true, logReuse: false, headless, passwordFile });
962
+ await ensureCredentialDaemonPM2({ forceRefresh: true, headless, passwordFile });
942
963
  console.log(`Credential daemon '${target}' restarted with a fresh unlock.`);
943
964
  return;
944
965
  }
945
966
  target = await assertActiveBotTarget(target);
946
- await ensureCredentialDaemonPM2({ logReuse: false, headless, passwordFile });
967
+ await ensureCredentialDaemonPM2({ headless, passwordFile });
947
968
  await execPM2Command('restart', target);
948
969
  console.log(`PM2 process '${target}' restarted.`);
949
970
  }
@@ -217,9 +217,8 @@ class RepoAnalyzer {
217
217
  *
218
218
  * Displays:
219
219
  * 1. Summary stats: Total commits, lines added/deleted, net change
220
- * 2. File listing: All tracked files with their change counts
221
- * 3. Add/Delete ratio: Metric for code quality (more adds vs deletes)
222
- * 4. Daily activity: Last 10 days of changes (trend analysis)
220
+ * 2. Add/Delete ratio: Metric for code quality (more adds vs deletes)
221
+ * 3. Daily activity: Last 10 days of changes (trend analysis)
223
222
  *
224
223
  * Output format uses Unicode box drawing and padding for readability
225
224
  * Suitable for terminal display with fixed-width fonts
@@ -238,18 +237,6 @@ class RepoAnalyzer {
238
237
  console.log(` Total Lines Changed: ${this.stats.totalEdits.toLocaleString()}`);
239
238
  console.log(` Net Change: ${(this.stats.totalAdded - this.stats.totalDeleted).toLocaleString()}`);
240
239
  console.log();
241
- // All tracked files
242
- console.log('📋 All Tracked Files:');
243
- console.log('─'.repeat(80));
244
- console.log(`${'File'.padEnd(45)} ${'Added'.padStart(10)} ${'Deleted'.padStart(10)} ${'Total'.padStart(10)}`);
245
- console.log('─'.repeat(80));
246
- const allFiles = Object.entries(this.stats.files)
247
- .sort((a, b) => b[1].edits - a[1].edits);
248
- for (const [file, stats] of allFiles) {
249
- const fileName = file.length > 43 ? '...' + file.slice(-40) : file;
250
- console.log(`${fileName.padEnd(45)} ${stats.added.toString().padStart(10)} ${stats.deleted.toString().padStart(10)} ${stats.edits.toString().padStart(10)}`);
251
- }
252
- console.log();
253
240
  // Add/Delete ratio
254
241
  console.log('🎯 Add vs Delete Ratio:');
255
242
  console.log('─'.repeat(80));
@@ -38,7 +38,7 @@ import { fetchMarketCandlesSequentially } from '../market_adapter/inputs/fetch_b
38
38
  import { fetchFeedCandlesSequentially } from '../market_adapter/inputs/kibana_feed_source.js';
39
39
  import { muteChainLogs } from '../modules/utils/chain_logs.js';
40
40
  import { isSameBotName, sanitizeKey } from '../modules/utils/sanitize_key.js';
41
- import { slugPart } from '../market_adapter/interval_utils.js';
41
+ import { slugPart, parseChainTimeToMs } from '../market_adapter/interval_utils.js';
42
42
  import { monthsToHours } from '../modules/utils/time_range.js';
43
43
  const INTERVAL_SECONDS = 3600;
44
44
  const DEFAULT_MONTHS = 3;
@@ -152,7 +152,7 @@ async function pickFeedContext(symA, symB, source, bitsharesClient) {
152
152
  function feedAgeMs(ctx, nowMs = Date.now()) {
153
153
  if (!ctx?.feedPublicationTime)
154
154
  return null;
155
- const ts = Date.parse(ctx.feedPublicationTime.endsWith('Z') ? ctx.feedPublicationTime : `${ctx.feedPublicationTime}Z`);
155
+ const ts = parseChainTimeToMs(ctx.feedPublicationTime);
156
156
  if (!Number.isFinite(ts))
157
157
  return null;
158
158
  return nowMs - ts;
@@ -6,6 +6,10 @@
6
6
  * this gate makes progress monotonic by failing when the code-line count rises
7
7
  * above the committed budget in `any-budget.json`.
8
8
  *
9
+ * The root scopes (`modules/`, `market_adapter/`, `analysis/`, `scripts/`) and
10
+ * the experimental `claw/` subtree are tracked with independent budgets so the
11
+ * now-clean root count is not inflated by claw, which is still being paid down.
12
+ *
9
13
  * Counts `any` tokens on code lines only (comment / JSDoc lines are ignored so
10
14
  * documentation cleanup is not double-counted).
11
15
  *
@@ -6,6 +6,10 @@
6
6
  * this gate makes progress monotonic by failing when the code-line count rises
7
7
  * above the committed budget in `any-budget.json`.
8
8
  *
9
+ * The root scopes (`modules/`, `market_adapter/`, `analysis/`, `scripts/`) and
10
+ * the experimental `claw/` subtree are tracked with independent budgets so the
11
+ * now-clean root count is not inflated by claw, which is still being paid down.
12
+ *
9
13
  * Counts `any` tokens on code lines only (comment / JSDoc lines are ignored so
10
14
  * documentation cleanup is not double-counted).
11
15
  *
@@ -23,6 +27,9 @@ const ROOT = PATHS.PROJECT_ROOT;
23
27
  const BUDGET_FILE = path.join(ROOT, 'any-budget.json');
24
28
  const SCAN_DIRS = ['modules', 'market_adapter', 'analysis', 'scripts'];
25
29
  const SCAN_ROOT_FILES = true;
30
+ const ROOT_SCOPE = 'root';
31
+ const CLAW_SCOPE = 'claw';
32
+ const CLAW_DIR = 'claw';
26
33
  // `any` as an explicit type token only: not preceded by an identifier char or
27
34
  // `.` (so the `Promise.any` built-in / member named `any` is not counted) and
28
35
  // not inside a string or template literal (log/help prose is not counted).
@@ -78,6 +85,9 @@ function sourceFiles() {
78
85
  }
79
86
  return files;
80
87
  }
88
+ function clawFiles() {
89
+ return [...walk(path.join(ROOT, CLAW_DIR))];
90
+ }
81
91
  function countAny(file) {
82
92
  const lines = fs.readFileSync(file, 'utf8').split(/\r?\n/);
83
93
  let count = 0;
@@ -90,37 +100,59 @@ function countAny(file) {
90
100
  }
91
101
  return count;
92
102
  }
93
- const counts = sourceFiles()
94
- .map((file) => ({ file: path.relative(ROOT, file), count: countAny(file) }))
95
- .filter((entry) => entry.count > 0)
96
- .sort((a, b) => b.count - a.count);
97
- const total = counts.reduce((sum, entry) => sum + entry.count, 0);
103
+ function countScope(scope, files) {
104
+ const fileCounts = files
105
+ .map((file) => ({ file: path.relative(ROOT, file), count: countAny(file) }))
106
+ .filter((entry) => entry.count > 0)
107
+ .sort((a, b) => b.count - a.count);
108
+ return { scope, files: fileCounts, total: fileCounts.reduce((sum, entry) => sum + entry.count, 0) };
109
+ }
110
+ const scopes = [
111
+ countScope(ROOT_SCOPE, sourceFiles()),
112
+ countScope(CLAW_SCOPE, clawFiles())
113
+ ];
98
114
  const listMode = process.argv.includes('--list');
99
115
  const updateMode = process.argv.includes('--update');
100
116
  if (listMode) {
101
- for (const entry of counts)
102
- console.log(`${String(entry.count).padStart(5)} ${entry.file}`);
103
- console.log(`${String(total).padStart(5)} TOTAL (${counts.length} files)`);
117
+ for (const scope of scopes) {
118
+ for (const entry of scope.files)
119
+ console.log(`${String(entry.count).padStart(5)} ${entry.file}`);
120
+ console.log(`${String(scope.total).padStart(5)} ${scope.scope.toUpperCase()} SUBTOTAL (${scope.files.length} files)`);
121
+ }
104
122
  process.exit(0);
105
123
  }
106
124
  if (updateMode) {
107
- fs.writeFileSync(BUDGET_FILE, `${JSON.stringify({ total, updated: new Date().toISOString().slice(0, 10) }, null, 2)}\n`);
108
- console.log(`budget file updated: total=${total}`);
125
+ const next = {
126
+ root: scopes.find((scope) => scope.scope === ROOT_SCOPE).total,
127
+ claw: scopes.find((scope) => scope.scope === CLAW_SCOPE).total,
128
+ updated: new Date().toISOString().slice(0, 10)
129
+ };
130
+ fs.writeFileSync(BUDGET_FILE, `${JSON.stringify(next, null, 2)}\n`);
131
+ console.log(`budget file updated: root=${next.root} claw=${next.claw}`);
109
132
  process.exit(0);
110
133
  }
111
- let budget = Infinity;
112
- if (fs.existsSync(BUDGET_FILE)) {
113
- budget = JSON.parse(fs.readFileSync(BUDGET_FILE, 'utf8')).total;
134
+ const fileBudget = fs.existsSync(BUDGET_FILE)
135
+ ? JSON.parse(fs.readFileSync(BUDGET_FILE, 'utf8'))
136
+ : {};
137
+ let exceeded = false;
138
+ for (const scope of scopes) {
139
+ const committed = typeof fileBudget[scope.scope] === 'number'
140
+ ? fileBudget[scope.scope]
141
+ // Backward compatibility with the original single-scope `{ total }` file.
142
+ : (scope.scope === ROOT_SCOPE && typeof fileBudget.total === 'number' ? fileBudget.total : Infinity);
143
+ if (scope.total > committed) {
144
+ console.error(`\u2716 explicit type budget exceeded [${scope.scope}]: ${scope.total} > ${committed}`);
145
+ exceeded = true;
146
+ }
147
+ else if (scope.total < committed) {
148
+ console.log(`\u2713 [${scope.scope}] explicit type count ${scope.total} is below budget ${committed}. Lower the budget file to ${scope.total} to lock in the gain.`);
149
+ }
150
+ else {
151
+ console.log(`\u2713 [${scope.scope}] explicit type count ${scope.total} is at budget ${committed}.`);
152
+ }
114
153
  }
115
- if (total > budget) {
116
- console.error(`\u2716 explicit type budget exceeded: ${total} > ${budget}`);
154
+ if (exceeded) {
117
155
  console.error(' Reduce usages, or run `node dist/scripts/check_any.js --update` only when lowering the budget.');
118
156
  process.exit(1);
119
157
  }
120
- if (total < budget) {
121
- console.log(`\u2713 explicit type count ${total} is below budget ${budget}. Lower the budget file to ${total} to lock in the gain.`);
122
- }
123
- else {
124
- console.log(`\u2713 explicit type count ${total} is at budget ${budget}.`);
125
- }
126
158
  //# sourceMappingURL=check_any.js.map
@@ -9,6 +9,7 @@ const require = createRequire(import.meta.url);
9
9
  */
10
10
  const { BitShares, waitForConnected } = require('../modules/bitshares_client');
11
11
  const { getErrorMessage } = require('../modules/utils/errors');
12
+ const { parseChainTimeToMs } = require('../market_adapter/interval_utils');
12
13
  const POOL_ID = process.argv.includes('--pool')
13
14
  ? process.argv[process.argv.indexOf('--pool') + 1]
14
15
  : '1.19.133';
@@ -30,12 +31,6 @@ function fmt(obj) {
30
31
  return String(obj);
31
32
  }
32
33
  }
33
- function parseChainTimeToMs(timeStr) {
34
- if (!timeStr)
35
- return Number.NaN;
36
- const s = String(timeStr);
37
- return Date.parse(s.endsWith('Z') ? s : `${s}Z`);
38
- }
39
34
  function extractReceived(row) {
40
35
  const r = row;
41
36
  const resultPayload = Array.isArray(r?.op?.result) ? r.op.result[1] : null;
@@ -32,7 +32,6 @@ const liveTestFiles = new Set([
32
32
  'dist/tests/test_debug_orderbook.js',
33
33
  'dist/tests/test_twentix_only.js',
34
34
  'dist/tests/test_fee_cache.js',
35
- 'dist/tests/test_fee_cache_twentix.js',
36
35
  'dist/tests/test_subscriptions.js',
37
36
  'dist/tests/test_fills.js',
38
37
  'dist/tests/test_funds.js',
@@ -66,16 +66,6 @@ const targets = [
66
66
  return JSON.stringify(json, null, 2) + '\n';
67
67
  },
68
68
  },
69
- {
70
- file: 'analysis/trend_detection/package.json',
71
- update(content, version) {
72
- const json = JSON.parse(content);
73
- if (json.version === version)
74
- return null;
75
- json.version = version;
76
- return JSON.stringify(json, null, 2) + '\n';
77
- },
78
- },
79
69
  // ── Source files (regex replacements) ──────────────────────
80
70
  {
81
71
  file: 'claw/tests/test_claw_mcp_transport.ts',
@@ -415,18 +415,17 @@ npm prefix -g # must be a directory you own
415
415
 
416
416
  Check with `dexbot --help`.
417
417
 
418
- ### "npm says it blocked dexbot's install scripts (`prepare` / `postinstall`)"
418
+ ### "npm says it blocked dexbot's install scripts (`prepare`)"
419
419
 
420
420
  npm 12+ no longer runs lifecycle scripts of installed packages unless they
421
421
  are allowlisted (supply-chain hardening,
422
422
  [npm/rfcs#868](https://github.com/npm/rfcs/blob/master/text/0868-allow-scripts.md)).
423
- You'll see `npm warn install-scripts ... blocked` listing dexbot's
424
- `postinstall` and `prepare`. It shows up with `npm link` because link adds
425
- your checkout to the **global** install tree as a `file:` dependency — npm
426
- treats it like any third-party package and blocks its scripts. The warning's
427
- own advice (`npm install-scripts ls` / `approve`) is a dead end here: in your
428
- project the checkout is the project root, which npm deliberately never lists
429
- or approves. Don't chase it.
423
+ With `npm link` you'll see `npm warn install-scripts ... blocked` listing
424
+ dexbot's `prepare`: link adds your checkout to the **global** install tree as
425
+ a `file:` dependency — npm treats it like any third-party package and blocks
426
+ its scripts. The warning's own advice (`npm install-scripts ls` / `approve`)
427
+ is a dead end here: in your project the checkout is the project root, which
428
+ npm deliberately never lists or approves. Don't chase it.
430
429
 
431
430
  A plain `npm install` is **not** gated — it runs the project's own `prepare`
432
431
  directly, so the correct from-source order is:
@@ -438,10 +437,7 @@ npm link # a blocked prepare here is harmless
438
437
  ```
439
438
 
440
439
  If you skipped the install (or want to be sure), run `npm run build`. Bins
441
- are never gated, so once `dist/` exists the linked `dexbot` works. The
442
- blocked `postinstall` is only the "DEXBot2 installed!" banner — cosmetic;
443
- `npm i -g dexbot` may warn about the same banner and that's harmless too
444
- (the published package ships prebuilt).
440
+ are never gated, so once `dist/` exists the linked `dexbot` works.
445
441
 
446
442
  ### "`Unknown command: dexbot` right after installing/linking"
447
443
 
@@ -233,7 +233,7 @@ syncFromMaster(masterGrid, orderId, masterVersion?) {
233
233
  | `WorkingGrid.buildDelta(masterGrid)` | Delta between master and working copy (`modules/order/working_grid.ts:205`, delegating to `utils/order.ts`) |
234
234
  | `_commitWorkingGrid(workingGrid, indexes, boundary, options = {})` | Atomic swap to master |
235
235
  | `_setRebalanceState(state)` | Track rebalance state |
236
- | `_currentWorkingGrid` | Reference to working grid during rebalance for fill sync |
236
+ | `_currentWorkingGridStack` | Stack of active working-grid references during rebalance for fill sync; peek the innermost via `_peekWorkingGrid()`, managed with `_pushWorkingGridRef`/`_popWorkingGridRef`/`_releaseWorkingGridRef`/`_clearWorkingGridRef` |
237
237
  | `_applyOrderUpdate(order, context, options = {})` | Lock-free order update (immutable swap); routes fill sync to `_syncWorkingGridFromMasterMutation` |
238
238
  | `_syncWorkingGridFromMasterMutation(orderId, context)` | Adapter: planning-state gate → `markStale()` + `syncFromMaster()` → sync error handling |
239
239
 
@@ -1,7 +1,7 @@
1
1
  # DEXBot vs DEXBot2 — Detailed Comparison Report
2
2
 
3
3
  > **Date:** 2026-09-02 *(metrics refreshed against local source trees)*
4
- > **Scope:** Full architectural, functional, and operational comparison between the original [DEXBot](https://github.com/Codaone/DEXBot) (Python, v1.0.0) and DEXBot2 (TypeScript, v1.6.12).
4
+ > **Scope:** Full architectural, functional, and operational comparison between the original [DEXBot](https://github.com/Codaone/DEXBot) (Python, v1.0.0) and DEXBot2 (TypeScript, v1.6.13).
5
5
  > **Audience:** Developers, contributors, and operators evaluating or migrating between the two projects.
6
6
 
7
7
  ---
@@ -36,7 +36,7 @@
36
36
 
37
37
  | Attribute | DEXBot (original) | DEXBot2 |
38
38
  |---|---|---|
39
- | **Release Track** | 1.0.0 | v1.6.12 |
39
+ | **Release Track** | 1.0.0 | v1.6.13 |
40
40
  | **Language** | Python 3.6+ | TypeScript 5.x |
41
41
  | **Status** | Released 1.0.0, unmaintained | Active development |
42
42
  | **Last Repo Activity** | May 23, 2020 | 2026-09-02 |
@@ -776,13 +776,13 @@ Where:
776
776
 
777
777
  | Metric | DEXBot | DEXBot2 |
778
778
  |---|---|---|
779
- | **Release Track** | 1.0.0 | v1.6.12 |
779
+ | **Release Track** | 1.0.0 | v1.6.13 |
780
780
  | **Active Since** | ~2018 | December 2025 |
781
- | **Last Commit** | May 23, 2020 | 2026-10-08 |
782
- | **Total Commits** | 2281 | 2,350 (current HEAD, v1.6.12 line) |
781
+ | **Last Commit** | May 23, 2020 | 2026-10-10 |
782
+ | **Total Commits** | 2281 | 2,372 (current HEAD, v1.6.13 line) |
783
783
  | **Lines of Code** | ~10,846 Python LOC in `dexbot/` | Large TypeScript runtime + adapter + Claw + analysis + tests |
784
784
  | **Source Files** | 72 Python files in `dexbot/` | ~656 TS files across the repo |
785
- | **Test Files** | 16 Python test files | 330 `test_*.ts` files (313 `tests/` + 17 `claw/tests/`, auto-discovered via `globSync`) |
785
+ | **Test Files** | 16 Python test files | 324 `test_*.ts` files (307 `tests/` + 17 `claw/tests/`, auto-discovered via `globSync`) |
786
786
  | **Documentation** | Sphinx docs + README | 60+ Markdown docs plus Claw skills/references |
787
787
  | **Strategies** | 3 + plugins | 1 |
788
788
  | **Max Concurrent Bots** | Many (one process) | Many (one monolithic process by default; per-bot via `--isolated`/PM2) |
package/docs/EVOLUTION.md CHANGED
@@ -2,14 +2,14 @@
2
2
 
3
3
  ## Executive Summary
4
4
 
5
- DEXBot2 is a sophisticated decentralized exchange trading bot for the BitShares blockchain. This report documents the complete evolution of the project from its inception in December 2025 through the current 1.6.12 release.
5
+ DEXBot2 is a sophisticated decentralized exchange trading bot for the BitShares blockchain. This report documents the complete evolution of the project from its inception in December 2025 through the current 1.6.13 release.
6
6
 
7
7
  ### Key Milestones
8
8
  - **Project Inception**: December 2, 2025
9
- - **Growth Phase**: 2,350 commits over ~9 active months
9
+ - **Growth Phase**: 2,372 commits over ~9 active months
10
10
  - **Code Maturity**: Evolution from basic utilities to a ~100,000+ LoC intelligent TypeScript system
11
- - **Stability**: Progression from manual testing to a suite of 313 automated test files
12
- - **Releases**: 144 version entries in the changelog (v0.1.0 to v1.6.12)
11
+ - **Stability**: Progression from manual testing to a suite of 307 automated test files
12
+ - **Releases**: 145 version entries in the changelog (v0.1.0 to v1.6.13)
13
13
 
14
14
  > **Post-1.0.0 "why":** the thematic story behind the hardening releases — root cause, recurring
15
15
  > bug families, and lessons — lives in
@@ -149,7 +149,7 @@ Evolved from a basic README to a comprehensive framework (50+ docs entries, 80%+
149
149
 
150
150
  ## Version History
151
151
 
152
- Compact, era-level view; commit counts are `git rev-list --count <tag>..<tag>` over the current history (2026-10-08), so the column sums to 2,243 — the 107 commits up to and including the `v0.1.0` tag are not attributed to an era (2,243 + 107 = 2,350). Per-release detail lives in [CHANGELOG.md](../CHANGELOG.md); the thematic post-1.0.0 story in [ORDER_ENGINE_POST_1.0_RETROSPECTIVE.md](ORDER_ENGINE_POST_1.0_RETROSPECTIVE.md).
152
+ Compact, era-level view; commit counts are `git rev-list --count <tag>..<tag>` over the current history (2026-10-10), so the column sums to 2,265 — the 107 commits up to and including the `v0.1.0` tag are not attributed to an era (2,265 + 107 = 2,372). Per-release detail lives in [CHANGELOG.md](../CHANGELOG.md); the thematic post-1.0.0 story in [ORDER_ENGINE_POST_1.0_RETROSPECTIVE.md](ORDER_ENGINE_POST_1.0_RETROSPECTIVE.md).
153
153
 
154
154
  | Era | Commits | Theme |
155
155
  |-----|--------:|-------|
@@ -170,11 +170,12 @@ Compact, era-level view; commit counts are `git rev-list --count <tag>..<tag>` o
170
170
  | v1.6.9 → v1.6.10 | 7 | Huber-robust AMA slope, asset-pair uppercase canonicalization, TradingView indicator ownership (auto opt-in, span bounds) and chart-view preservation |
171
171
  | v1.6.10 → v1.6.11 | 6 | AMA-slope 3-bar persistence gate + 16h Huber lookback, EMA-smoothing removal, PnL HTML report + month-shard fill cache, five order-engine/lifecycle safety fixes |
172
172
  | v1.6.11 → v1.6.12 | 27 | Explicit-`any` elimination + ratchet, shared ECC core & Firefox-safe pure-JS scrypt, worker-drain and fresh-placement cancel-grace safety fixes, funded spread correction, partial credit renewal, update install-layout classifier, AMA persistence-override fixes, test-suite speedup, dynamic-weight knob retune |
173
+ | v1.6.12 → v1.6.13 | 21 | Spread-stall cause reporting + chain sync on every stall, resync-cascade guards (uncertain broadcast / suspended persistence), month-shard candle cache, dedup & dead-code purge, npm 12 install & PM2 fixes |
173
174
 
174
175
  ---
175
176
 
176
177
  **Report Originally Generated**: February 19, 2026
177
- **Last Updated**: October 8, 2026
178
- **Total Commits**: 2,350
179
- **Date Range**: December 2, 2025 – October 8, 2026
178
+ **Last Updated**: October 10, 2026
179
+ **Total Commits**: 2,372
180
+ **Date Range**: December 2, 2025 – October 10, 2026
180
181
  **Repository**: DEXBot2 (BitShares DEX Trading Bot)
@@ -801,4 +801,4 @@ To prevent "Time-of-Check to Time-of-Use" errors:
801
801
  **TOCTOU protection in `processFillAccounting`.** `_buildBtsDeferredRefundAdjustment` reads `btsFeeState` from `mgr.orders` while the order lock is held — the lock is acquired before accounting runs, and the POST-RESET and BOOTSTRAP tracked-fill accounting paths follow the same locking pattern.
802
802
 
803
803
  ---
804
- *Technical Reference for DEXBot2 v1.6.12 release*
804
+ *Technical Reference for DEXBot2 v1.6.13 release*
package/docs/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  This directory contains the comprehensive technical documentation for the DEXBot2 trading bot. It is designed to guide developers from high-level architecture down to the nuances of fund accounting and state management.
4
4
 
5
- **Version context:** v1.6.12 (released).
5
+ **Version context:** v1.6.13 (released).
6
6
 
7
7
  ---
8
8
 
package/docs/WORKFLOW.md CHANGED
@@ -255,6 +255,7 @@ the canonical name is preferred in scripts and docs.
255
255
  | `dexbot tv <target>` | — | TradingView 1h chart for `<bot|pool-id|AssetA/AssetB>` over `--month N` (default 3) |
256
256
  | `dexbot dw <target>` | — | Dynamic-weight research chart: same targets/flags as `tv` |
257
257
  | `dexbot pnl <account>` | — | HTML PnL report for a bot/account over `--month N`, optional `--pair BASE/QUOTE` filter |
258
+ | `dexbot git` | — | Analyze repository git history and write an added/deleted-lines HTML chart (`repo-stats.html`; source checkouts only) |
258
259
  | `dexbot status` | `stat`, `stats` | Unified runtime health — daemon, adapter, bots |
259
260
  | `dexbot start` | `unlock` | Run credential daemon + bot (equivalent to running the `unlock` runtime, `dist/unlock.js`) |
260
261
  | `dexbot stop` | `stp`, `stopall` | Stop the monolithic runtime (unlock mode) |
@@ -476,16 +476,25 @@ The correction path is fund-only and never manufactures budget by shrinking a re
476
476
 
477
477
  ```
478
478
  1. Detect that the live window is wider than targetSpreadPercent.
479
- 2. Select up to `outOfSpread` eligible holes in safe window/gap order.
480
- 3. If the funded edge holds a non-dust PARTIAL, optionally top it up, but only
481
- when `ideal - current` is positive and free funds can cover the increase.
482
- 4. Create replacements from free available/chainFree funds; contiguous gap-slot
483
- promotion may extend the window while preserving MIN_SPREAD_ORDERS and the
484
- opposite placed rail.
485
- 5. If free funds cannot cover every target, place/top up the affordable prefix,
486
- stop without shrinking inventory, and log the fund-constrained remainder.
487
- 6. If neither side has free funds, skip correction and let the maintenance
488
- runtime refresh balances and open orders rather than recycle stale inventory.
479
+ 2. Select up to `outOfSpread` spread-tightening empty slots (below the lowest
480
+ live sell / above the highest live buy), ordered window-contiguous first, with
481
+ a market-nearest fallback when the rail has no live order.
482
+ 3. Create those slots from free available/chainFree funds only. A resting PARTIAL
483
+ is never a correction target: a top-up grows an order at its current price, so
484
+ it cannot move bestBuy/bestSell and cannot narrow the spread.
485
+ 4. Contiguous gap-slot promotion may extend the window while preserving
486
+ MIN_SPREAD_ORDERS and the opposite placed rail.
487
+ 5. If free funds cannot cover every target, place the affordable prefix, stop
488
+ without shrinking inventory, and log the fund-constrained remainder.
489
+ 6. If neither side places an effective order, `checkSpreadCondition` reports a
490
+ `stall` cause (`no-free-funds` / `unfunded` / `no-candidates` /
491
+ `batch-not-executed` / `apply-error`). Every cause arms the targeted
492
+ balances + open-orders refresh: the local snapshot the check reasoned over
493
+ may be stale (`no-candidates` can mean a missed fill left an ACTIVE slot
494
+ occupying the window edge, so no tightening slot appears to exist), and only
495
+ a chain sync can tell a stale snapshot from structural geometry. The
496
+ out-of-spread staleness watchdog remains the backstop if the sync heals
497
+ nothing. No resting order is ever recycled.
489
498
  ```
490
499
 
491
500
  Detected dust partials never enter this path: `cancelDustOrders()` cancels them