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
@@ -1,6 +1,50 @@
1
1
  interface FindLatestLpDataOptions {
2
2
  dataDir?: string;
3
3
  }
4
+ /**
5
+ * Result of auto-discovering the newest LP series. `candles` is always the
6
+ * complete series, whether it came from one whole-history export
7
+ * (`assembled: false`) or was merged across month shards (`assembled: true`).
8
+ */
9
+ interface LpSeriesResult {
10
+ path: string;
11
+ meta: Record<string, unknown> | null;
12
+ candles: unknown[];
13
+ assembled: boolean;
14
+ }
15
+ /**
16
+ * Whole-history-export discovery only: returns the newest `lp_pool_*.json` that
17
+ * is a complete series, or null. Month shards and legacy `.chunk_*` files are
18
+ * excluded — a single shard is a slice, and (because the active shard is
19
+ * rewritten every run) by mtime it would otherwise always win. Use
20
+ * `loadLatestLpSeries` when a shard-only cache should still yield a series.
21
+ */
4
22
  declare function findLatestLpData(options?: FindLatestLpDataOptions): string | null;
5
- export { findLatestLpData };
23
+ /**
24
+ * Auto-discover and load the newest LP series.
25
+ *
26
+ * Month shards are the cache and a whole-history export is a derived snapshot
27
+ * of the same shard family (both writers, `fetch_lp_data` and
28
+ * `fetch_lp_candles`, regenerate the export from the assembled shards after
29
+ * fetching). The two therefore agree in extent, and the ordinary mtime race
30
+ * decides — an export produced by an external tool can still be newer and win.
31
+ * A shard-only directory is assembled; an export-only directory is read as-is.
32
+ */
33
+ declare function loadLatestLpSeries(options?: FindLatestLpDataOptions): LpSeriesResult | null;
34
+ /**
35
+ * Assemble the whole series for a base path directly from its month shards,
36
+ * regardless of any sibling whole-history export. Writers use this to derive
37
+ * the export from the cache, so the monolithic file can never become a
38
+ * narrower or staler snapshot than the shards beside it.
39
+ */
40
+ declare function loadLpSeriesFromBasePath(basePath: string): LpSeriesResult | null;
41
+ /**
42
+ * Load one explicit LP path. A month shard is transparently upgraded to its
43
+ * whole family: pointing `--data` at `.shard_2026-10.json` charts the full
44
+ * cached history, never a silent one-month slice. A whole-history export (or
45
+ * a lone shard whose siblings are missing) is read as-is.
46
+ */
47
+ declare function loadLpSeriesFromPath(filePath: string): LpSeriesResult | null;
48
+ export { findLatestLpData, loadLatestLpSeries, loadLpSeriesFromPath, loadLpSeriesFromBasePath };
49
+ export type { LpSeriesResult, FindLatestLpDataOptions };
6
50
  //# sourceMappingURL=data_discovery.d.ts.map
@@ -2,47 +2,240 @@
2
2
  import { path } from '../../modules/path_api.js';
3
3
  import { getStorage } from '../../modules/storage/index.js';
4
4
  import { PATHS } from '../../modules/paths.js';
5
+ import { mergeCandles, higherVolumeWins } from '../candle_utils.js';
6
+ import { shardKeyFromName, shardStemFromName } from './month_shards.js';
5
7
  const storage = getStorage();
6
- function findLatestLpData(options = {}) {
7
- const dataDir = options.dataDir ? path.resolve(options.dataDir) : PATHS.MARKET_ADAPTER.LP_DATA_DIR;
8
+ function isLpJsonName(name) {
9
+ return name.startsWith('lp_pool_') && name.endsWith('.json');
10
+ }
11
+ /**
12
+ * All shard files for one base path (`<base>.shard_YYYY-MM.json`), read from
13
+ * disk. Empty when the base is not a shard-addressed series.
14
+ */
15
+ function readShardFamily(basePath) {
16
+ const resolved = path.resolve(basePath);
17
+ const parsed = path.parse(resolved);
18
+ if (!storage.exists(parsed.dir))
19
+ return [];
8
20
  const out = [];
9
- if (!storage.exists(dataDir))
21
+ for (const name of storage.readdir(parsed.dir)) {
22
+ if (!shardKeyFromName(name, parsed.name, parsed.ext))
23
+ continue;
24
+ const full = path.join(parsed.dir, name);
25
+ try {
26
+ const info = storage.stat(full);
27
+ if (info.isFile())
28
+ out.push({ path: full, mtime: info.mtimeMs });
29
+ }
30
+ catch (_) { /* unreadable sibling: skip it, keep the rest */ }
31
+ }
32
+ return out.sort((a, b) => (a.path < b.path ? -1 : 1));
33
+ }
34
+ /**
35
+ * Merge an ordered shard family into one series. `sourcePath` is the file the
36
+ * caller should report (the newest shard, or the explicitly requested one).
37
+ */
38
+ function assembleLpShards(files, sourcePath) {
39
+ let merged = [];
40
+ let meta = null;
41
+ for (const file of files) {
42
+ const raw = storage.readJSON(file.path);
43
+ if (!meta)
44
+ meta = metaOf(raw);
45
+ merged = mergeCandles(merged, rawCandlesOf(raw), { onCollision: higherVolumeWins });
46
+ }
47
+ if (merged.length === 0)
10
48
  return null;
11
- const stack = [dataDir];
12
- while (stack.length > 0) {
13
- const dir = stack.pop();
14
- let names = [];
49
+ const firstTs = Number(merged[0][0]);
50
+ const lastTs = Number(merged[merged.length - 1][0]);
51
+ // A shard's meta describes one month; publish the assembled extent instead
52
+ // and drop per-shard bookkeeping that would misrepresent a whole series.
53
+ const mergedMeta = {
54
+ ...(meta || {}),
55
+ candleCount: merged.length,
56
+ firstTs: new Date(firstTs).toISOString(),
57
+ lastTs: new Date(lastTs).toISOString(),
58
+ timeRange: { gte: new Date(firstTs).toISOString(), lte: new Date(lastTs).toISOString() },
59
+ assembledFrom: 'month-shards',
60
+ };
61
+ delete mergedMeta.shard;
62
+ delete mergedMeta.queriedRanges;
63
+ return { path: sourcePath, meta: mergedMeta, candles: merged, assembled: true };
64
+ }
65
+ /**
66
+ * Scan the LP data dir for whole-history exports and month-shard families.
67
+ *
68
+ * The on-disk layout is fixed at two levels (`<dataDir>/<pair>/<file>`), so the
69
+ * scan is deliberately NOT recursive: it stats each direct child once and only
70
+ * descends one level into pair folders. Legacy run-relative `.chunk_*` files
71
+ * are ignored — nothing reads them and they are not a series.
72
+ */
73
+ function scanLpDataDir(dataDir) {
74
+ const whole = [];
75
+ const shardFamilies = new Map();
76
+ if (!storage.exists(dataDir))
77
+ return { whole, shardFamilies };
78
+ const consider = (full, stat) => {
79
+ const name = path.basename(full);
80
+ if (!isLpJsonName(name) || name.includes('.chunk_'))
81
+ return;
82
+ let info = stat;
83
+ if (!info) {
84
+ try {
85
+ info = storage.stat(full);
86
+ }
87
+ catch (_) {
88
+ return;
89
+ }
90
+ }
91
+ if (!info.isFile())
92
+ return;
93
+ const stem = shardStemFromName(name, path.extname(name));
94
+ if (!stem) {
95
+ whole.push({ path: full, mtime: info.mtimeMs });
96
+ return;
97
+ }
98
+ const key = path.join(path.dirname(full), stem);
99
+ let family = shardFamilies.get(key);
100
+ if (!family) {
101
+ family = { stem, files: [], maxMtime: 0 };
102
+ shardFamilies.set(key, family);
103
+ }
104
+ family.files.push({ path: full, mtime: info.mtimeMs });
105
+ if (info.mtimeMs > family.maxMtime)
106
+ family.maxMtime = info.mtimeMs;
107
+ };
108
+ for (const name of storage.readdir(dataDir)) {
109
+ const full = path.join(dataDir, name);
110
+ let stat = null;
15
111
  try {
16
- names = storage.readdir(dir);
112
+ stat = storage.stat(full);
17
113
  }
18
114
  catch (_) {
19
- // Directory vanished or is unreadable — skip it instead of
20
- // aborting the whole scan.
21
115
  continue;
22
116
  }
23
- for (const name of names) {
24
- const full = path.join(dir, name);
25
- let stat = null;
117
+ if (stat.isDirectory()) {
118
+ let names = [];
26
119
  try {
27
- stat = storage.stat(full);
120
+ names = storage.readdir(full);
28
121
  }
29
122
  catch (_) {
30
- // Dangling symlink or race-deleted entry — skip the entry.
31
- continue;
32
- }
33
- if (stat.isDirectory()) {
34
- stack.push(full);
35
123
  continue;
36
124
  }
37
- if (!stat.isFile() || !name.endsWith('.json'))
38
- continue;
39
- if (!name.startsWith('lp_pool_'))
40
- continue;
41
- out.push({ path: full, mtime: stat.mtimeMs });
125
+ for (const inner of names)
126
+ consider(path.join(full, inner));
127
+ }
128
+ else if (stat.isFile()) {
129
+ consider(full, stat);
42
130
  }
43
131
  }
44
- out.sort((a, b) => b.mtime - a.mtime);
45
- return out.length > 0 ? out[0].path : null;
132
+ return { whole, shardFamilies };
133
+ }
134
+ function newestOf(files) {
135
+ let best = null;
136
+ for (const f of files)
137
+ if (!best || f.mtime > best.mtime)
138
+ best = f;
139
+ return best;
140
+ }
141
+ function newestFamily(families) {
142
+ let best = null;
143
+ for (const fam of families.values())
144
+ if (!best || fam.maxMtime > best.maxMtime)
145
+ best = fam;
146
+ return best;
147
+ }
148
+ function rawCandlesOf(raw) {
149
+ if (Array.isArray(raw))
150
+ return raw;
151
+ const r = raw;
152
+ if (r && Array.isArray(r.candles))
153
+ return r.candles;
154
+ if (r && Array.isArray(r.data))
155
+ return r.data;
156
+ return [];
157
+ }
158
+ function metaOf(raw) {
159
+ if (Array.isArray(raw))
160
+ return null;
161
+ const meta = raw?.meta;
162
+ return meta && typeof meta === 'object' ? meta : null;
163
+ }
164
+ /**
165
+ * Whole-history-export discovery only: returns the newest `lp_pool_*.json` that
166
+ * is a complete series, or null. Month shards and legacy `.chunk_*` files are
167
+ * excluded — a single shard is a slice, and (because the active shard is
168
+ * rewritten every run) by mtime it would otherwise always win. Use
169
+ * `loadLatestLpSeries` when a shard-only cache should still yield a series.
170
+ */
171
+ function findLatestLpData(options = {}) {
172
+ const dataDir = options.dataDir ? path.resolve(options.dataDir) : PATHS.MARKET_ADAPTER.LP_DATA_DIR;
173
+ const { whole } = scanLpDataDir(dataDir);
174
+ return newestOf(whole)?.path ?? null;
175
+ }
176
+ /**
177
+ * Auto-discover and load the newest LP series.
178
+ *
179
+ * Month shards are the cache and a whole-history export is a derived snapshot
180
+ * of the same shard family (both writers, `fetch_lp_data` and
181
+ * `fetch_lp_candles`, regenerate the export from the assembled shards after
182
+ * fetching). The two therefore agree in extent, and the ordinary mtime race
183
+ * decides — an export produced by an external tool can still be newer and win.
184
+ * A shard-only directory is assembled; an export-only directory is read as-is.
185
+ */
186
+ function loadLatestLpSeries(options = {}) {
187
+ const dataDir = options.dataDir ? path.resolve(options.dataDir) : PATHS.MARKET_ADAPTER.LP_DATA_DIR;
188
+ const { whole, shardFamilies } = scanLpDataDir(dataDir);
189
+ const newestWhole = newestOf(whole);
190
+ const family = newestFamily(shardFamilies);
191
+ // Whole-history export wins ties/races; only a strictly newer shard family
192
+ // forces the merge path.
193
+ if (newestWhole && (!family || newestWhole.mtime >= family.maxMtime)) {
194
+ const raw = storage.readJSON(newestWhole.path);
195
+ const candles = rawCandlesOf(raw);
196
+ if (candles.length === 0)
197
+ return null;
198
+ return { path: newestWhole.path, meta: metaOf(raw), candles, assembled: false };
199
+ }
200
+ if (!family)
201
+ return null;
202
+ const ordered = [...family.files].sort((a, b) => (a.path < b.path ? -1 : 1));
203
+ return assembleLpShards(ordered, ordered[ordered.length - 1].path);
204
+ }
205
+ /**
206
+ * Assemble the whole series for a base path directly from its month shards,
207
+ * regardless of any sibling whole-history export. Writers use this to derive
208
+ * the export from the cache, so the monolithic file can never become a
209
+ * narrower or staler snapshot than the shards beside it.
210
+ */
211
+ function loadLpSeriesFromBasePath(basePath) {
212
+ const resolved = path.resolve(basePath);
213
+ const family = readShardFamily(resolved);
214
+ if (family.length === 0)
215
+ return null;
216
+ return assembleLpShards(family, family[family.length - 1].path);
217
+ }
218
+ /**
219
+ * Load one explicit LP path. A month shard is transparently upgraded to its
220
+ * whole family: pointing `--data` at `.shard_2026-10.json` charts the full
221
+ * cached history, never a silent one-month slice. A whole-history export (or
222
+ * a lone shard whose siblings are missing) is read as-is.
223
+ */
224
+ function loadLpSeriesFromPath(filePath) {
225
+ const resolved = path.resolve(filePath);
226
+ const parsed = path.parse(resolved);
227
+ const stem = shardStemFromName(parsed.base, parsed.ext);
228
+ if (stem) {
229
+ const family = readShardFamily(path.join(parsed.dir, `${stem}${parsed.ext}`));
230
+ const assembled = assembleLpShards(family, resolved);
231
+ if (assembled)
232
+ return assembled;
233
+ }
234
+ const raw = storage.readJSON(resolved);
235
+ const candles = rawCandlesOf(raw);
236
+ if (candles.length === 0)
237
+ return null;
238
+ return { path: resolved, meta: metaOf(raw), candles, assembled: false };
46
239
  }
47
- export { findLatestLpData };
240
+ export { findLatestLpData, loadLatestLpSeries, loadLpSeriesFromPath, loadLpSeriesFromBasePath };
48
241
  //# sourceMappingURL=data_discovery.js.map
@@ -0,0 +1,84 @@
1
+ /** A span that was actually queried, with the time the query ran. */
2
+ type QueriedRange = {
3
+ gte: number;
4
+ lte: number;
5
+ at: number | null;
6
+ };
7
+ /** A plain inclusive ms range. */
8
+ type Span = {
9
+ gte: number;
10
+ lte: number;
11
+ };
12
+ declare function shardKeyForTimestamp(tsMs: unknown): string;
13
+ declare function shardBoundsForKey(key: unknown): {
14
+ start: number;
15
+ end: number;
16
+ };
17
+ declare function shardKeysForRange(gteMs: unknown, lteMs: unknown): string[];
18
+ declare function shardPathFor(basePath: string, shardKey: string): string;
19
+ /**
20
+ * If `name` is a shard file for `baseName`, return its `YYYY-MM` key;
21
+ * otherwise null. Guards the `sibling`/family scans against stray files.
22
+ */
23
+ declare function shardKeyFromName(name: string, baseName: string, ext?: string): string | null;
24
+ /**
25
+ * The base stem of a shard filename, or null when `name` is not a shard.
26
+ * `lp_pool_133_1h.shard_2026-10.json` → `lp_pool_133_1h`. Callers that must
27
+ * group or upgrade a family use this instead of re-deriving the marker regex.
28
+ */
29
+ declare function shardStemFromName(name: string, ext?: string): string | null;
30
+ /** Parse a verification timestamp: epoch ms or ISO. Anything else is unknown. */
31
+ declare function timestampMs(raw: unknown): number | null;
32
+ declare function verifiedAt(range: unknown): number | null;
33
+ /**
34
+ * Normalize recorded spans: sort, then merge overlapping / bucket-adjacent
35
+ * ones — but ONLY when they assert the same verification time. Merging spans
36
+ * with different `at` would have to pick one verdict for the whole union, and
37
+ * either pick loses (min → the merged span vouches for buckets the older
38
+ * query never saw; max → buckets verified long ago look freshly verified).
39
+ * Keeping them separate preserves exact per-bucket freshness. The same-`at`
40
+ * case (bulk fetches, and every pre-`at` shard) collapses to one span.
41
+ *
42
+ * `bucketMs` widens adjacency: `0` merges only overlaps, `1` merges adjacent
43
+ * integer-ms spans (fills), a bucket size merges spans a bucket apart (candles).
44
+ */
45
+ declare function unionQueriedRanges(ranges: {
46
+ gte: number;
47
+ lte: number;
48
+ at?: number | null;
49
+ }[], bucketMs?: number): QueriedRange[];
50
+ /**
51
+ * Bound the persisted span list. Because spans merge only when they assert the
52
+ * same `at`, a shard that is re-verified on every run gains one span per run
53
+ * and would otherwise grow without limit. Dropping the OLDEST spans never
54
+ * invents emptiness. The input is already sorted by extent, so the tail is
55
+ * newest.
56
+ */
57
+ declare function compactCoverage(spans: QueriedRange[], maxSpans: number): QueriedRange[];
58
+ declare function rangesCoveredBy(have: {
59
+ gte: number;
60
+ lte: number;
61
+ }[], want: {
62
+ gte: number;
63
+ lte: number;
64
+ }[]): boolean;
65
+ /**
66
+ * Change detection for shard writes: `have` already accounts for every span in
67
+ * `want`, in extent AND in verification time. Without the `at` half, a run
68
+ * whose only news is "these buckets were re-verified 1h later" would skip the
69
+ * write and the next run would fall back to the wide fixed refresh window.
70
+ * An unknown `at` on either side is never treated as fresh.
71
+ */
72
+ declare function coverageSatisfied(have: QueriedRange[], want: QueriedRange[]): boolean;
73
+ /** Every recorded span across the loaded files, newest verification included. */
74
+ declare function allQueriedRanges(fileCover: {
75
+ queried?: QueriedRange[] | null;
76
+ }[]): QueriedRange[];
77
+ declare function clipRangeTo(q: QueriedRange, gteMs: number, lteMs: number): QueriedRange | null;
78
+ /** Sort and merge overlapping / adjacent (integer-ms) spans. */
79
+ declare function mergeSpans(spans: Span[]): Span[];
80
+ /** `span` minus the union of `covered` (both as inclusive ms ranges). */
81
+ declare function subtractRanges(span: Span, covered: Span[]): Span[];
82
+ export { shardKeyForTimestamp, shardBoundsForKey, shardKeysForRange, shardPathFor, shardKeyFromName, shardStemFromName, timestampMs, verifiedAt, unionQueriedRanges, compactCoverage, rangesCoveredBy, coverageSatisfied, allQueriedRanges, clipRangeTo, mergeSpans, subtractRanges, };
83
+ export type { QueriedRange, Span };
84
+ //# sourceMappingURL=month_shards.d.ts.map
@@ -0,0 +1,237 @@
1
+ 'use strict';
2
+ /**
3
+ * MONTH-SHARD GEOMETRY & COVERAGE — the one place that defines how calendar-
4
+ * month cache shards are named, located, and how recorded query coverage is
5
+ * merged and compared.
6
+ *
7
+ * Two independent caches store one `{ meta, ... }` file per UTC month named
8
+ * `<base>.shard_YYYY-MM.json`:
9
+ * - the candle cache (`market_adapter/inputs/window_cache.ts`)
10
+ * - the fills cache (`analysis/fills_cache.ts`)
11
+ *
12
+ * They previously carried private, near-identical copies of every helper
13
+ * below. The geometry in particular must never drift: two caches that disagree
14
+ * on a month boundary silently double-fetch or drop a bucket. Keeping it here
15
+ * makes the layout a single contract.
16
+ *
17
+ * Pure and I/O-free (only path arithmetic) so it is safe to import from any
18
+ * runtime, including browser-safe code that merely checks a filename.
19
+ */
20
+ import { path } from '../../modules/path_api.js';
21
+ const SHARD_MARKER = '.shard_';
22
+ const SHARD_KEY_RE = /^\d{4}-\d{2}$/;
23
+ // ─── Month-shard naming ───────────────────────────────────────────────────────
24
+ // Shard key is the UTC calendar month; bounds are half-open [start, end) so
25
+ // every timestamp maps to exactly one shard (a value exactly at a month
26
+ // boundary belongs to the new month).
27
+ function shardKeyForTimestamp(tsMs) {
28
+ const d = new Date(Number(tsMs));
29
+ return `${d.getUTCFullYear()}-${String(d.getUTCMonth() + 1).padStart(2, '0')}`;
30
+ }
31
+ function shardBoundsForKey(key) {
32
+ const parts = String(key).split('-').map(Number);
33
+ const start = Date.UTC(parts[0], parts[1] - 1, 1);
34
+ const end = parts[1] === 12 ? Date.UTC(parts[0] + 1, 0, 1) : Date.UTC(parts[0], parts[1], 1);
35
+ return { start, end };
36
+ }
37
+ function shardKeysForRange(gteMs, lteMs) {
38
+ const keys = [];
39
+ const cursor = new Date(Date.UTC(new Date(Number(gteMs)).getUTCFullYear(), new Date(Number(gteMs)).getUTCMonth(), 1));
40
+ const last = new Date(Number(lteMs));
41
+ while (cursor <= last) {
42
+ keys.push(shardKeyForTimestamp(cursor.getTime()));
43
+ cursor.setUTCMonth(cursor.getUTCMonth() + 1);
44
+ }
45
+ return keys;
46
+ }
47
+ function shardPathFor(basePath, shardKey) {
48
+ const parsed = path.parse(basePath);
49
+ return path.join(parsed.dir, `${parsed.name}${SHARD_MARKER}${shardKey}${parsed.ext}`);
50
+ }
51
+ /**
52
+ * If `name` is a shard file for `baseName`, return its `YYYY-MM` key;
53
+ * otherwise null. Guards the `sibling`/family scans against stray files.
54
+ */
55
+ function shardKeyFromName(name, baseName, ext = '.json') {
56
+ const prefix = `${baseName}${SHARD_MARKER}`;
57
+ if (ext && !name.endsWith(ext))
58
+ return null;
59
+ if (!name.startsWith(prefix))
60
+ return null;
61
+ const key = name.slice(prefix.length, name.length - ext.length);
62
+ return SHARD_KEY_RE.test(key) ? key : null;
63
+ }
64
+ /**
65
+ * The base stem of a shard filename, or null when `name` is not a shard.
66
+ * `lp_pool_133_1h.shard_2026-10.json` → `lp_pool_133_1h`. Callers that must
67
+ * group or upgrade a family use this instead of re-deriving the marker regex.
68
+ */
69
+ function shardStemFromName(name, ext = '.json') {
70
+ if (ext && !name.endsWith(ext))
71
+ return null;
72
+ const body = ext ? name.slice(0, name.length - ext.length) : name;
73
+ const idx = body.lastIndexOf(SHARD_MARKER);
74
+ if (idx < 0)
75
+ return null;
76
+ const key = body.slice(idx + SHARD_MARKER.length);
77
+ return SHARD_KEY_RE.test(key) ? body.slice(0, idx) : null;
78
+ }
79
+ // ─── Coverage spans ───────────────────────────────────────────────────────────
80
+ /** Parse a verification timestamp: epoch ms or ISO. Anything else is unknown. */
81
+ function timestampMs(raw) {
82
+ // `null`/`undefined`/`''` mean "not recorded". They must NOT fall through
83
+ // to Number(): Number(null) === 0 is finite, which would read an unknown
84
+ // verification time as "verified at the epoch" — i.e. as maximally stale.
85
+ if (raw == null || raw === '')
86
+ return null;
87
+ if (typeof raw === 'string') {
88
+ const parsed = Date.parse(raw);
89
+ return Number.isFinite(parsed) ? parsed : null;
90
+ }
91
+ const at = Number(raw);
92
+ return Number.isFinite(at) ? at : null;
93
+ }
94
+ function verifiedAt(range) {
95
+ return timestampMs(range?.at);
96
+ }
97
+ /**
98
+ * Normalize recorded spans: sort, then merge overlapping / bucket-adjacent
99
+ * ones — but ONLY when they assert the same verification time. Merging spans
100
+ * with different `at` would have to pick one verdict for the whole union, and
101
+ * either pick loses (min → the merged span vouches for buckets the older
102
+ * query never saw; max → buckets verified long ago look freshly verified).
103
+ * Keeping them separate preserves exact per-bucket freshness. The same-`at`
104
+ * case (bulk fetches, and every pre-`at` shard) collapses to one span.
105
+ *
106
+ * `bucketMs` widens adjacency: `0` merges only overlaps, `1` merges adjacent
107
+ * integer-ms spans (fills), a bucket size merges spans a bucket apart (candles).
108
+ */
109
+ function unionQueriedRanges(ranges, bucketMs = 0) {
110
+ const clean = (ranges || [])
111
+ .filter((q) => q && Number.isFinite(Number(q.gte)) && Number.isFinite(Number(q.lte)) && Number(q.lte) >= Number(q.gte))
112
+ .map((q) => ({ gte: Number(q.gte), lte: Number(q.lte), at: verifiedAt(q) }))
113
+ .sort((a, b) => a.gte - b.gte || a.lte - b.lte);
114
+ const merged = [];
115
+ const gap = Number.isFinite(Number(bucketMs)) && Number(bucketMs) > 0 ? Number(bucketMs) : 0;
116
+ for (const q of clean) {
117
+ const top = merged[merged.length - 1];
118
+ if (top && q.gte <= top.lte + gap && top.at === q.at) {
119
+ if (q.lte > top.lte)
120
+ top.lte = q.lte;
121
+ }
122
+ else {
123
+ merged.push({ gte: q.gte, lte: q.lte, at: q.at });
124
+ }
125
+ }
126
+ return merged;
127
+ }
128
+ /**
129
+ * Bound the persisted span list. Because spans merge only when they assert the
130
+ * same `at`, a shard that is re-verified on every run gains one span per run
131
+ * and would otherwise grow without limit. Dropping the OLDEST spans never
132
+ * invents emptiness. The input is already sorted by extent, so the tail is
133
+ * newest.
134
+ */
135
+ function compactCoverage(spans, maxSpans) {
136
+ const list = spans || [];
137
+ const max = Number.isFinite(Number(maxSpans)) && Number(maxSpans) >= 1 ? Math.floor(Number(maxSpans)) : list.length;
138
+ return list.length <= max ? list : list.slice(list.length - max);
139
+ }
140
+ function rangesCoveredBy(have, want) {
141
+ for (const w of want || []) {
142
+ let covered = false;
143
+ for (const h of have || []) {
144
+ if (h.gte <= w.gte && h.lte >= w.lte) {
145
+ covered = true;
146
+ break;
147
+ }
148
+ }
149
+ if (!covered)
150
+ return false;
151
+ }
152
+ return true;
153
+ }
154
+ /**
155
+ * Change detection for shard writes: `have` already accounts for every span in
156
+ * `want`, in extent AND in verification time. Without the `at` half, a run
157
+ * whose only news is "these buckets were re-verified 1h later" would skip the
158
+ * write and the next run would fall back to the wide fixed refresh window.
159
+ * An unknown `at` on either side is never treated as fresh.
160
+ */
161
+ function coverageSatisfied(have, want) {
162
+ if (!rangesCoveredBy(have, want))
163
+ return false;
164
+ for (const w of want || []) {
165
+ const wAt = verifiedAt(w);
166
+ if (wAt == null)
167
+ continue;
168
+ let fresh = false;
169
+ for (const h of have || []) {
170
+ if (h.gte > w.gte || h.lte < w.lte)
171
+ continue;
172
+ const hAt = verifiedAt(h);
173
+ if (hAt != null && hAt >= wAt) {
174
+ fresh = true;
175
+ break;
176
+ }
177
+ }
178
+ if (!fresh)
179
+ return false;
180
+ }
181
+ return true;
182
+ }
183
+ /** Every recorded span across the loaded files, newest verification included. */
184
+ function allQueriedRanges(fileCover) {
185
+ const out = [];
186
+ for (const f of fileCover || []) {
187
+ for (const q of f?.queried || []) {
188
+ if (q && Number.isFinite(Number(q.gte)) && Number.isFinite(Number(q.lte)))
189
+ out.push(q);
190
+ }
191
+ }
192
+ return out;
193
+ }
194
+ function clipRangeTo(q, gteMs, lteMs) {
195
+ const gte = Math.max(q.gte, gteMs);
196
+ const lte = Math.min(q.lte, lteMs);
197
+ return lte >= gte ? { gte, lte, at: verifiedAt(q) } : null;
198
+ }
199
+ /** Sort and merge overlapping / adjacent (integer-ms) spans. */
200
+ function mergeSpans(spans) {
201
+ const sorted = [...spans].sort((a, b) => a.gte - b.gte || a.lte - b.lte);
202
+ const merged = [];
203
+ for (const s of sorted) {
204
+ const top = merged[merged.length - 1];
205
+ if (top && s.gte <= top.lte + 1) {
206
+ if (s.lte > top.lte)
207
+ top.lte = s.lte;
208
+ }
209
+ else {
210
+ merged.push({ ...s });
211
+ }
212
+ }
213
+ return merged;
214
+ }
215
+ /** `span` minus the union of `covered` (both as inclusive ms ranges). */
216
+ function subtractRanges(span, covered) {
217
+ let parts = [{ ...span }];
218
+ for (const c of mergeSpans(covered)) {
219
+ const next = [];
220
+ for (const p of parts) {
221
+ if (c.lte < p.gte || c.gte > p.lte) {
222
+ next.push(p);
223
+ continue;
224
+ }
225
+ if (c.gte > p.gte)
226
+ next.push({ gte: p.gte, lte: Math.min(p.lte, c.gte - 1) });
227
+ if (c.lte < p.lte)
228
+ next.push({ gte: Math.max(p.gte, c.lte + 1), lte: p.lte });
229
+ }
230
+ parts = next.filter((p) => p.lte >= p.gte);
231
+ if (parts.length === 0)
232
+ break;
233
+ }
234
+ return parts;
235
+ }
236
+ export { shardKeyForTimestamp, shardBoundsForKey, shardKeysForRange, shardPathFor, shardKeyFromName, shardStemFromName, timestampMs, verifiedAt, unionQueriedRanges, compactCoverage, rangesCoveredBy, coverageSatisfied, allQueriedRanges, clipRangeTo, mergeSpans, subtractRanges, };
237
+ //# sourceMappingURL=month_shards.js.map
@@ -144,6 +144,7 @@ declare function loadGeneralSettings(): {
144
144
  RETRY_BACKOFF_CAP_MS: number;
145
145
  DAEMON_PING_TIMEOUT_MS: number;
146
146
  CREDENTIAL_DAEMON_WATCHDOG_MS: number;
147
+ CREDENTIAL_DAEMON_WATCHDOG_FAILURE_THRESHOLD: number;
147
148
  RUN_LOOP_DEFAULT_MS: number;
148
149
  OPEN_ORDERS_SYNC_LOOP_ENABLED: boolean;
149
150
  CHECK_INTERVAL_MS: number;