@orangecharts/wrun-chart-host 0.4.0 → 0.5.0

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 (322) hide show
  1. package/README.md +15 -4
  2. package/dist/constants/exchange-names.leaf.d.ts +8 -0
  3. package/dist/constants/exchange-names.leaf.js +101 -0
  4. package/dist/constants/kscript-data-interval-restrictions.constants.d.ts +15 -0
  5. package/dist/constants/kscript-data-interval-restrictions.constants.js +40 -0
  6. package/dist/constants/opra-compliance.constants.d.ts +19 -0
  7. package/dist/constants/opra-compliance.constants.js +19 -0
  8. package/dist/constants/replay-ladder.leaf.d.ts +30 -0
  9. package/dist/constants/replay-ladder.leaf.js +99 -0
  10. package/dist/constants/scriptEditor/cme-official-scripts.constants.d.ts +168 -0
  11. package/dist/constants/scriptEditor/cme-official-scripts.constants.js +398 -0
  12. package/dist/constants/scriptEditor/converted-script-id.leaf.d.ts +8 -0
  13. package/dist/constants/scriptEditor/converted-script-id.leaf.js +10 -0
  14. package/dist/constants/scriptEditor/execution.constants.d.ts +4 -0
  15. package/dist/constants/scriptEditor/execution.constants.js +14 -0
  16. package/dist/constants/wrun-prefix-safe.constants.d.ts +23 -0
  17. package/dist/constants/wrun-prefix-safe.constants.js +26 -0
  18. package/dist/host-env.d.ts +313 -18
  19. package/dist/host-env.js +15 -0
  20. package/dist/host-ports.types.d.ts +853 -16
  21. package/dist/index.d.ts +139 -2
  22. package/dist/index.js +139 -2
  23. package/dist/instances/script-engine-worker-ohlcv-transfer.d.ts +31 -0
  24. package/dist/instances/script-engine-worker-ohlcv-transfer.js +164 -0
  25. package/dist/interfaces/market-event.interface.d.ts +231 -0
  26. package/dist/interfaces/market-event.interface.js +15 -0
  27. package/dist/interfaces/opra-realtime.interface.d.ts +70 -134
  28. package/dist/interfaces/opra-realtime.interface.js +34 -59
  29. package/dist/interfaces/replay.interface.d.ts +285 -0
  30. package/dist/interfaces/replay.interface.js +1 -0
  31. package/dist/kscript-data/controls/live-ohlcv-merge.util.d.ts +1 -0
  32. package/dist/kscript-data/controls/live-ohlcv-merge.util.js +93 -0
  33. package/dist/kscript-data/controls/live-trades-merge.util.d.ts +5 -1
  34. package/dist/kscript-data/controls/live-trades-merge.util.js +2 -2
  35. package/dist/market-data/billing.interface.d.ts +219 -0
  36. package/dist/market-data/billing.interface.js +58 -0
  37. package/dist/market-data/venue-governed.util.d.ts +2 -0
  38. package/dist/market-data/venue-governed.util.js +10 -0
  39. package/dist/market-data/venue.d.ts +122 -0
  40. package/dist/market-data/venue.js +1 -0
  41. package/dist/market-data/venues/index.d.ts +97 -0
  42. package/dist/market-data/venues/index.js +33 -0
  43. package/dist/market-data/venues/opra.contract.copy.json +35 -0
  44. package/dist/market-data/venues/opra.d.ts +70 -0
  45. package/dist/market-data/venues/opra.js +74 -0
  46. package/dist/replay/replayIndicatorRows.d.ts +31 -0
  47. package/dist/replay/replayIndicatorRows.js +117 -0
  48. package/dist/replay/replayLadder.d.ts +85 -0
  49. package/dist/replay/replayLadder.js +388 -0
  50. package/dist/utility/auth/server-clock.util.d.ts +81 -0
  51. package/dist/utility/auth/server-clock.util.js +238 -0
  52. package/dist/utility/chart/builtin-identity.util.d.ts +18 -0
  53. package/dist/utility/chart/builtin-identity.util.js +51 -0
  54. package/dist/utility/chart/cache-alignment.util.d.ts +454 -0
  55. package/dist/utility/chart/cache-alignment.util.js +926 -0
  56. package/dist/utility/chart/chart-data-fetch-generation.util.d.ts +15 -0
  57. package/dist/utility/chart/chart-data-fetch-generation.util.js +22 -0
  58. package/dist/utility/chart/chart-mount-epoch.util.d.ts +5 -0
  59. package/dist/utility/chart/chart-mount-epoch.util.js +29 -0
  60. package/dist/utility/chart/chart-time-domain.util.d.ts +12 -0
  61. package/dist/utility/chart/chart-time-domain.util.js +23 -0
  62. package/dist/utility/chart/color-customization.leaf.d.ts +10 -0
  63. package/dist/utility/chart/color-customization.leaf.js +16 -0
  64. package/dist/utility/chart/footprint-tick-grid.util.d.ts +106 -0
  65. package/dist/utility/chart/footprint-tick-grid.util.js +167 -0
  66. package/dist/utility/chart/gesture-timing.leaf.d.ts +29 -0
  67. package/dist/utility/chart/gesture-timing.leaf.js +29 -0
  68. package/dist/utility/chart/governed-contract-coin.util.d.ts +22 -0
  69. package/dist/utility/chart/governed-contract-coin.util.js +39 -0
  70. package/dist/utility/chart/host-bar-placement.util.d.ts +107 -0
  71. package/dist/utility/chart/host-bar-placement.util.js +447 -0
  72. package/dist/utility/chart/index-host-tier.util.d.ts +31 -0
  73. package/dist/utility/chart/index-host-tier.util.js +118 -0
  74. package/dist/utility/chart/index-mode-admission.util.d.ts +34 -0
  75. package/dist/utility/chart/index-mode-admission.util.js +67 -0
  76. package/dist/utility/chart/indicator-error-message.util.d.ts +35 -0
  77. package/dist/utility/chart/indicator-error-message.util.js +44 -0
  78. package/dist/utility/chart/kscript-apply-fingerprint.util.d.ts +65 -0
  79. package/dist/utility/chart/kscript-apply-fingerprint.util.js +357 -0
  80. package/dist/utility/chart/kscript-chart-flush.leaf.d.ts +22 -0
  81. package/dist/utility/chart/kscript-chart-flush.leaf.js +13 -0
  82. package/dist/utility/chart/kscript-data-interval-restriction.leaf.d.ts +6 -0
  83. package/dist/utility/chart/kscript-data-interval-restriction.leaf.js +27 -0
  84. package/dist/utility/chart/kscript-integration-decision.leaf.d.ts +89 -0
  85. package/dist/utility/chart/kscript-integration-decision.leaf.js +80 -0
  86. package/dist/utility/chart/kscript-live-coalesce.util.d.ts +76 -0
  87. package/dist/utility/chart/kscript-live-coalesce.util.js +156 -0
  88. package/dist/utility/chart/kscript-live-rest-pace.util.d.ts +34 -0
  89. package/dist/utility/chart/kscript-live-rest-pace.util.js +134 -0
  90. package/dist/utility/chart/kscript-live-source-identity.util.d.ts +98 -0
  91. package/dist/utility/chart/kscript-live-source-identity.util.js +121 -0
  92. package/dist/utility/chart/kscript-mounted-plot-set.leaf.d.ts +48 -0
  93. package/dist/utility/chart/kscript-mounted-plot-set.leaf.js +145 -0
  94. package/dist/utility/chart/kscript-perf-tracker.util.d.ts +243 -0
  95. package/dist/utility/chart/kscript-perf-tracker.util.js +522 -0
  96. package/dist/utility/chart/kscript-run-interval-projection.util.d.ts +43 -0
  97. package/dist/utility/chart/kscript-run-interval-projection.util.js +471 -0
  98. package/dist/utility/chart/kscript-run-result-equality.util.d.ts +38 -0
  99. package/dist/utility/chart/kscript-run-result-equality.util.js +89 -0
  100. package/dist/utility/chart/kscript-settings-revision.leaf.d.ts +2 -0
  101. package/dist/utility/chart/kscript-settings-revision.leaf.js +12 -0
  102. package/dist/utility/chart/kscript-source-degrade.leaf.d.ts +63 -0
  103. package/dist/utility/chart/kscript-source-degrade.leaf.js +68 -0
  104. package/dist/utility/chart/kscript-source-retention.util.d.ts +134 -0
  105. package/dist/utility/chart/kscript-source-retention.util.js +218 -0
  106. package/dist/utility/chart/kscript-standin-identity.leaf.d.ts +57 -0
  107. package/dist/utility/chart/kscript-standin-identity.leaf.js +61 -0
  108. package/dist/utility/chart/kscript-titancharts-preparation.util.d.ts +15 -0
  109. package/dist/utility/chart/kscript-titancharts-preparation.util.js +376 -0
  110. package/dist/utility/chart/kscript-titancharts-render-keys.constants.d.ts +9 -0
  111. package/dist/utility/chart/kscript-titancharts-render-keys.constants.js +130 -0
  112. package/dist/utility/chart/kscript-venue-policy.util.d.ts +217 -0
  113. package/dist/utility/chart/kscript-venue-policy.util.js +324 -0
  114. package/dist/utility/chart/live-cadence.util.d.ts +38 -0
  115. package/dist/utility/chart/live-cadence.util.js +37 -0
  116. package/dist/utility/chart/live-feed-series-match.leaf.d.ts +13 -0
  117. package/dist/utility/chart/live-feed-series-match.leaf.js +21 -0
  118. package/dist/utility/chart/local-engine-self-heal-backoff.util.d.ts +46 -0
  119. package/dist/utility/chart/local-engine-self-heal-backoff.util.js +46 -0
  120. package/dist/utility/chart/local-engine-uninit.leaf.d.ts +21 -0
  121. package/dist/utility/chart/local-engine-uninit.leaf.js +22 -0
  122. package/dist/utility/chart/local-kscript-source-revalidation.util.d.ts +62 -0
  123. package/dist/utility/chart/local-kscript-source-revalidation.util.js +123 -0
  124. package/dist/utility/chart/local-kscript-warmup-depth.util.d.ts +214 -0
  125. package/dist/utility/chart/local-kscript-warmup-depth.util.js +376 -0
  126. package/dist/utility/chart/local-technical-script-live-apply-coalescer.util.d.ts +85 -0
  127. package/dist/utility/chart/local-technical-script-live-apply-coalescer.util.js +197 -0
  128. package/dist/utility/chart/local-technical-script-range.leaf.d.ts +72 -0
  129. package/dist/utility/chart/local-technical-script-range.leaf.js +126 -0
  130. package/dist/utility/chart/mixed-mode.util.d.ts +167 -0
  131. package/dist/utility/chart/mixed-mode.util.js +139 -0
  132. package/dist/utility/chart/ohlcv-row-readiness.util.d.ts +36 -0
  133. package/dist/utility/chart/ohlcv-row-readiness.util.js +90 -0
  134. package/dist/utility/chart/opra-user-scripts-gate.util.d.ts +19 -0
  135. package/dist/utility/chart/opra-user-scripts-gate.util.js +32 -0
  136. package/dist/utility/chart/overlay-execution-guard-key.leaf.d.ts +8 -0
  137. package/dist/utility/chart/overlay-execution-guard-key.leaf.js +10 -0
  138. package/dist/utility/chart/price-precision.util.d.ts +40 -0
  139. package/dist/utility/chart/price-precision.util.js +70 -0
  140. package/dist/utility/chart/profile-block-size.util.d.ts +64 -0
  141. package/dist/utility/chart/profile-block-size.util.js +141 -0
  142. package/dist/utility/chart/protected-kscript-degenerate-rows.leaf.d.ts +11 -0
  143. package/dist/utility/chart/protected-kscript-degenerate-rows.leaf.js +29 -0
  144. package/dist/utility/chart/protected-kscript-empty-output.util.d.ts +28 -0
  145. package/dist/utility/chart/protected-kscript-empty-output.util.js +84 -0
  146. package/dist/utility/chart/run-interval-unservable.leaf.d.ts +9 -0
  147. package/dist/utility/chart/run-interval-unservable.leaf.js +20 -0
  148. package/dist/utility/chart/run-interval-warmup.leaf.d.ts +19 -0
  149. package/dist/utility/chart/run-interval-warmup.leaf.js +35 -0
  150. package/dist/utility/chart/titan-constants.pin.d.ts +9 -5
  151. package/dist/utility/chart/titan-constants.pin.js +18 -5
  152. package/dist/utility/chart/warmup-backfill-retry.leaf.d.ts +9 -0
  153. package/dist/utility/chart/warmup-backfill-retry.leaf.js +9 -0
  154. package/dist/utility/chart/wrun-bar-session-facts.util.d.ts +11 -2
  155. package/dist/utility/chart/wrun-bar-session-facts.util.js +10 -3
  156. package/dist/utility/chart/wrun-run-result.util.d.ts +5 -1
  157. package/dist/utility/chart/wrun-run-result.util.js +20 -14
  158. package/dist/utility/common/data-processing.util.d.ts +138 -0
  159. package/dist/utility/common/data-processing.util.js +346 -0
  160. package/dist/utility/common/visible-timeout.util.d.ts +13 -4
  161. package/dist/utility/common/visible-timeout.util.js +23 -8
  162. package/dist/utility/exchange-format.leaf.d.ts +1 -0
  163. package/dist/utility/exchange-format.leaf.js +47 -0
  164. package/dist/utility/market-events/earnings-scope.util.d.ts +60 -0
  165. package/dist/utility/market-events/earnings-scope.util.js +92 -0
  166. package/dist/utility/market-events/market-events.util.d.ts +147 -0
  167. package/dist/utility/market-events/market-events.util.js +392 -0
  168. package/dist/utility/market-events/zoned-clock.util.d.ts +1 -0
  169. package/dist/utility/market-events/zoned-clock.util.js +11 -0
  170. package/dist/utility/options/option-venue-disclosure.util.d.ts +43 -0
  171. package/dist/utility/options/option-venue-disclosure.util.js +56 -0
  172. package/dist/utility/scriptEditor/package-semver.util.d.ts +51 -0
  173. package/dist/utility/scriptEditor/package-semver.util.js +120 -0
  174. package/dist/utility/scriptEditor/temp-script-id.util.d.ts +18 -0
  175. package/dist/utility/scriptEditor/temp-script-id.util.js +25 -0
  176. package/dist/utility/symbol-selection/markets-request-from-symbols.leaf.d.ts +20 -0
  177. package/dist/utility/symbol-selection/markets-request-from-symbols.leaf.js +81 -0
  178. package/dist/utility/v2/governed-tier.leaf.d.ts +103 -0
  179. package/dist/utility/v2/governed-tier.leaf.js +113 -0
  180. package/dist/utility/watchlist/import/import-request.leaf.d.ts +10 -0
  181. package/dist/utility/watchlist/import/import-request.leaf.js +10 -0
  182. package/dist/utility/wrun/sources/lanes/series.js +2 -2
  183. package/dist/utility/wrun/sources/row-lanes/option_contract_state.d.ts +4 -4
  184. package/dist/utility/wrun/sources/row-lanes/option_contract_state.js +5 -6
  185. package/dist/utility/wrun/wrun-alert-outputs.registry.d.ts +166 -0
  186. package/dist/utility/wrun/wrun-alert-outputs.registry.js +219 -0
  187. package/dist/utility/wrun/wrun-bar-count.util.d.ts +83 -0
  188. package/dist/utility/wrun/wrun-bar-count.util.js +259 -0
  189. package/dist/utility/wrun/wrun-chart-context-names.util.d.ts +84 -0
  190. package/dist/utility/wrun/wrun-chart-context-names.util.js +186 -0
  191. package/dist/utility/wrun/wrun-chart-market-facts.util.d.ts +253 -0
  192. package/dist/utility/wrun/wrun-chart-market-facts.util.js +731 -0
  193. package/dist/utility/wrun/wrun-cloud-opra-stamp.util.d.ts +28 -0
  194. package/dist/utility/wrun/wrun-cloud-opra-stamp.util.js +45 -0
  195. package/dist/utility/wrun/wrun-coin-lane-live.util.d.ts +18 -0
  196. package/dist/utility/wrun/wrun-coin-lane-live.util.js +47 -0
  197. package/dist/utility/wrun/wrun-deadline.util.js +2 -1
  198. package/dist/utility/wrun/wrun-debug-output.util.d.ts +39 -0
  199. package/dist/utility/wrun/wrun-debug-output.util.js +63 -0
  200. package/dist/utility/wrun/wrun-declared-warmup.util.d.ts +113 -0
  201. package/dist/utility/wrun/wrun-declared-warmup.util.js +181 -0
  202. package/dist/utility/wrun/wrun-draft-legend-note.util.d.ts +43 -0
  203. package/dist/utility/wrun/wrun-draft-legend-note.util.js +158 -0
  204. package/dist/utility/wrun/wrun-draft-overlay.util.d.ts +181 -0
  205. package/dist/utility/wrun/wrun-draft-overlay.util.js +357 -0
  206. package/dist/utility/wrun/wrun-draw-skipped.leaf.d.ts +16 -0
  207. package/dist/utility/wrun/wrun-draw-skipped.leaf.js +16 -0
  208. package/dist/utility/wrun/wrun-earnings-facts.util.d.ts +42 -0
  209. package/dist/utility/wrun/wrun-earnings-facts.util.js +131 -0
  210. package/dist/utility/wrun/wrun-first-run-depth.util.d.ts +32 -0
  211. package/dist/utility/wrun/wrun-first-run-depth.util.js +118 -0
  212. package/dist/utility/wrun/wrun-gate-plane-readers.util.d.ts +24 -0
  213. package/dist/utility/wrun/wrun-gate-plane-readers.util.js +31 -0
  214. package/dist/utility/wrun/wrun-held-lanes.util.d.ts +32 -0
  215. package/dist/utility/wrun/wrun-held-lanes.util.js +94 -0
  216. package/dist/utility/wrun/wrun-host-visible-clock.util.d.ts +2 -0
  217. package/dist/utility/wrun/wrun-host-visible-clock.util.js +17 -0
  218. package/dist/utility/wrun/wrun-host.service.d.ts +266 -0
  219. package/dist/utility/wrun/wrun-host.service.js +655 -0
  220. package/dist/utility/wrun/wrun-inert-legend.util.d.ts +33 -0
  221. package/dist/utility/wrun/wrun-inert-legend.util.js +125 -0
  222. package/dist/utility/wrun/wrun-input-requirements.leaf.d.ts +155 -0
  223. package/dist/utility/wrun/wrun-input-requirements.leaf.js +450 -0
  224. package/dist/utility/wrun/wrun-integration-route.util.d.ts +51 -0
  225. package/dist/utility/wrun/wrun-integration-route.util.js +59 -0
  226. package/dist/utility/wrun/wrun-latest-history.leaf.d.ts +8 -0
  227. package/dist/utility/wrun/wrun-latest-history.leaf.js +8 -0
  228. package/dist/utility/wrun/wrun-legend-hover.lazy.d.ts +12 -0
  229. package/dist/utility/wrun/wrun-legend-hover.lazy.js +96 -0
  230. package/dist/utility/wrun/wrun-lifecycle-runtime.util.d.ts +21 -0
  231. package/dist/utility/wrun/wrun-lifecycle-runtime.util.js +21 -0
  232. package/dist/utility/wrun/wrun-live-hidden-hold.util.d.ts +23 -0
  233. package/dist/utility/wrun/wrun-live-hidden-hold.util.js +13 -0
  234. package/dist/utility/wrun/wrun-live-intake.util.d.ts +374 -0
  235. package/dist/utility/wrun/wrun-live-intake.util.js +2027 -0
  236. package/dist/utility/wrun/wrun-live-patch.util.d.ts +116 -0
  237. package/dist/utility/wrun/wrun-live-patch.util.js +523 -0
  238. package/dist/utility/wrun/wrun-live-tick.util.d.ts +619 -0
  239. package/dist/utility/wrun/wrun-live-tick.util.js +4974 -0
  240. package/dist/utility/wrun/wrun-loading-owner.util.d.ts +58 -0
  241. package/dist/utility/wrun/wrun-loading-owner.util.js +89 -0
  242. package/dist/utility/wrun/wrun-official-scope.leaf.d.ts +29 -0
  243. package/dist/utility/wrun/wrun-official-scope.leaf.js +36 -0
  244. package/dist/utility/wrun/wrun-oi-lane.util.d.ts +27 -0
  245. package/dist/utility/wrun/wrun-oi-lane.util.js +42 -0
  246. package/dist/utility/wrun/wrun-options-stop.util.d.ts +52 -0
  247. package/dist/utility/wrun/wrun-options-stop.util.js +123 -0
  248. package/dist/utility/wrun/wrun-overlay-drivers.util.d.ts +719 -0
  249. package/dist/utility/wrun/wrun-overlay-drivers.util.js +1180 -0
  250. package/dist/utility/wrun/wrun-overlay-lifecycle.lazy.d.ts +50 -0
  251. package/dist/utility/wrun/wrun-overlay-lifecycle.lazy.js +131 -0
  252. package/dist/utility/wrun/wrun-overlay-lifecycle.util.d.ts +118 -0
  253. package/dist/utility/wrun/wrun-overlay-lifecycle.util.js +466 -0
  254. package/dist/utility/wrun/wrun-overlay-metadata.util.d.ts +45 -0
  255. package/dist/utility/wrun/wrun-overlay-metadata.util.js +112 -0
  256. package/dist/utility/wrun/wrun-overlay-recovery.util.d.ts +114 -0
  257. package/dist/utility/wrun/wrun-overlay-recovery.util.js +971 -0
  258. package/dist/utility/wrun/wrun-overlay-run.util.d.ts +858 -0
  259. package/dist/utility/wrun/wrun-overlay-run.util.js +11013 -0
  260. package/dist/utility/wrun/wrun-panel-mount.util.d.ts +24 -9
  261. package/dist/utility/wrun/wrun-panel-mount.util.js +138 -43
  262. package/dist/utility/wrun/wrun-param-map.util.d.ts +19 -4
  263. package/dist/utility/wrun/wrun-param-map.util.js +40 -47
  264. package/dist/utility/wrun/wrun-param-values.util.d.ts +9 -3
  265. package/dist/utility/wrun/wrun-param-values.util.js +5 -5
  266. package/dist/utility/wrun/wrun-prefix-cohort.util.d.ts +93 -0
  267. package/dist/utility/wrun/wrun-prefix-cohort.util.js +484 -0
  268. package/dist/utility/wrun/wrun-prefix-lanes.util.d.ts +114 -0
  269. package/dist/utility/wrun/wrun-prefix-lanes.util.js +486 -0
  270. package/dist/utility/wrun/wrun-prefix-proof.util.d.ts +317 -0
  271. package/dist/utility/wrun/wrun-prefix-proof.util.js +630 -0
  272. package/dist/utility/wrun/wrun-refusal-surface.util.d.ts +73 -0
  273. package/dist/utility/wrun/wrun-refusal-surface.util.js +380 -0
  274. package/dist/utility/wrun/wrun-refusal-text.util.d.ts +15 -0
  275. package/dist/utility/wrun/wrun-refusal-text.util.js +40 -0
  276. package/dist/utility/wrun/wrun-replay-lane.util.d.ts +139 -0
  277. package/dist/utility/wrun/wrun-replay-lane.util.js +520 -0
  278. package/dist/utility/wrun/wrun-replay-rows.util.d.ts +21 -0
  279. package/dist/utility/wrun/wrun-replay-rows.util.js +172 -0
  280. package/dist/utility/wrun/wrun-row-source-factory.d.ts +53 -0
  281. package/dist/utility/wrun/wrun-row-source-factory.js +435 -0
  282. package/dist/utility/wrun/wrun-run-cells.util.d.ts +36 -0
  283. package/dist/utility/wrun/wrun-run-cells.util.js +46 -0
  284. package/dist/utility/wrun/wrun-run-interval-session.util.d.ts +133 -0
  285. package/dist/utility/wrun/wrun-run-interval-session.util.js +121 -0
  286. package/dist/utility/wrun/wrun-run-interval.util.d.ts +98 -0
  287. package/dist/utility/wrun/wrun-run-interval.util.js +207 -0
  288. package/dist/utility/wrun/wrun-run-retained.util.d.ts +122 -0
  289. package/dist/utility/wrun/wrun-run-retained.util.js +255 -0
  290. package/dist/utility/wrun/wrun-sandbox-row-source.d.ts +184 -0
  291. package/dist/utility/wrun/wrun-sandbox-row-source.js +444 -0
  292. package/dist/utility/wrun/wrun-script-inputs.util.js +4 -4
  293. package/dist/utility/wrun/wrun-series-stream.util.d.ts +86 -0
  294. package/dist/utility/wrun/wrun-series-stream.util.js +537 -0
  295. package/dist/utility/wrun/wrun-sheet-remembered.util.d.ts +21 -0
  296. package/dist/utility/wrun/wrun-sheet-remembered.util.js +36 -0
  297. package/dist/utility/wrun/wrun-signal-sound-reasons.leaf.d.ts +7 -0
  298. package/dist/utility/wrun/wrun-signal-sound-reasons.leaf.js +12 -0
  299. package/dist/utility/wrun/wrun-socket-row-source.js +4 -2
  300. package/dist/utility/wrun/wrun-source-rows.util.d.ts +175 -3
  301. package/dist/utility/wrun/wrun-source-rows.util.js +294 -8
  302. package/dist/utility/wrun/wrun-stats-strip.util.d.ts +46 -0
  303. package/dist/utility/wrun/wrun-stats-strip.util.js +87 -0
  304. package/dist/utility/wrun/wrun-table-chrome.util.d.ts +163 -0
  305. package/dist/utility/wrun/wrun-table-chrome.util.js +557 -0
  306. package/dist/utility/wrun/wrun-text-params.util.d.ts +11 -6
  307. package/dist/utility/wrun/wrun-text-params.util.js +19 -30
  308. package/dist/utility/wrun/wrun-transient-rerun.util.d.ts +64 -0
  309. package/dist/utility/wrun/wrun-transient-rerun.util.js +317 -0
  310. package/dist/utility/wrun/wrun-twin-lookback.util.d.ts +10 -0
  311. package/dist/utility/wrun/wrun-twin-lookback.util.js +43 -0
  312. package/dist/utility/wrun/wrun-warmup-head-store.util.d.ts +41 -0
  313. package/dist/utility/wrun/wrun-warmup-head-store.util.js +120 -0
  314. package/dist/utility/wrun-alert-params.leaf.d.ts +24 -0
  315. package/dist/utility/wrun-alert-params.leaf.js +24 -0
  316. package/dist/utils/replay/replay-buffer-math.d.ts +148 -0
  317. package/dist/utils/replay/replay-buffer-math.js +684 -0
  318. package/dist/workers/wrun/wrun-abi-host.d.ts +190 -0
  319. package/dist/workers/wrun/wrun-abi-host.js +1201 -0
  320. package/dist/workers/wrun/wrun-engine-handler.d.ts +37 -0
  321. package/dist/workers/wrun/wrun-engine-handler.js +211 -0
  322. package/package.json +3 -3
@@ -0,0 +1,2027 @@
1
+ /**
2
+ * wrun live push intake: per-overlay queues of pushed live frames, per-lane
3
+ * folds (the kScript controls' fold rules, never mutating), gap marks and
4
+ * the rig counters. Contract: ~/WebstormProjects/WRUN-PUSH-INTAKE-PLAN.md
5
+ * sections 1 and 8.
6
+ *
7
+ * Ownership seams:
8
+ * - the live-feed fan-out hands one frame per matched source to the tick
9
+ * lane, which enqueues it here with the context's generation (O(1)) and
10
+ * arms a drain;
11
+ * - the tick lane drains (`drainWrunLiveIntake`) at its cadence, folding
12
+ * everything queued into the context's lane buffers with the SAME units
13
+ * the history fetch used (`context.acquisition[lane]`);
14
+ * - a gap mark (`markWrunLiveGap`) records a reconcile reason and wakes the
15
+ * tick through the drain requester the tick registers at import
16
+ * (`setWrunLiveDrainRequester`), so this module never imports the tick;
17
+ * - the drivers' teardown hook disposes an overlay's queues, gaps and stats.
18
+ *
19
+ * Fold rules per lane (units equal the history lane's units):
20
+ * - trades: K_BUY_SELL_VOLUME.mergeLiveDataPoint (additive per side; the
21
+ * increment is base-asset coins, or volume x close when the acquisition
22
+ * asked for USD; the history worker defaults to coins);
23
+ * - liquidations: K_LIQUIDATIONS (additive per side; the increment is amount x price, USD like
24
+ * the lane's history ask, or the amount in coins when the acquisition asked for coins);
25
+ * - tape: K_TRADES's live fold (`mergeLiveTradesDataPoint`, the leaf the control and the history
26
+ * worker share): one `[offsetMs, price, size, side]` cell per raw print appended to its bar's
27
+ * row, the side from the series envelope, prints below the acquisition's `minSize` rejected,
28
+ * the bar the worker keys (a week on its Monday, the CME lattice on its venues), a delayed
29
+ * print for a bar older than the newest row never appending an out-of-order row, a live bar
30
+ * capped at 20k prints (the bucket reported as capped, which the tick refuses by name); prints
31
+ * below the floor are dropped at the enqueue too, so a busy tape never crowds a qualifying print
32
+ * out of the queue (the kScript fan-out's rule). The floor includes the large-print table's own
33
+ * (`tradesBelowLargeFloor`: a dollar-quoted print under 10,000 notional, a CME Group print under
34
+ * its root's contracts floor), so no print appears live that the next reconcile cannot answer. A
35
+ * print the queue cap drops leaves its bar short: the
36
+ * drain marks the bucket for the reconcile (`tape-lost`) when the table can restore it, else the
37
+ * tick refuses the bucket by name (`tapeLostBuckets`);
38
+ * - a row lane fed live by COIN (`WrunRowLaneDescriptor.liveByCoin`, sources/row-lane-registry.ts): a
39
+ * point of the frame's script source is admitted when its coin (the lane's `coinOf`, else the series'
40
+ * coin) is the coin the instance was asked for (its acquisition's `coin`: the chart's underlying),
41
+ * whatever contract it names and whether or not it is the chart's own market (a coin-scoped OPRA
42
+ * channel carries every contract of the root); it folds by the lane's own `fold` into the instance's
43
+ * host-bar rows (`context.rowLaneRows[id]`), bucketed on the chart's bar grid;
44
+ * - funding: K_FUNDING_RATE (last-wins, normalized to 1h, percent);
45
+ * - volume_profile: K_VOLUME_PROFILE_AGG (delta fold on the lattice, needs a
46
+ * fetched base row, quote guard);
47
+ * - book: K_ORDERBOOK.buildLiveDataRows for raw frames (an exact bar
48
+ * boundary belongs to the PREVIOUS bar, `roundObTimestamp`); rows marked
49
+ * `bucketed: true` (the heatmap rider's projection) are taken as-is;
50
+ * - a foreign GRID leg (a pinned market's rows at the chart interval, the
51
+ * G4 round-3 contract): K_OHLCV.mergeLiveDataPoint over that market's
52
+ * candle frames (`mergeLiveOhlcvDataPoint`: high/low widen, close
53
+ * replaces, volume adds; a TICKER quote folds as a zero-volume print),
54
+ * into `context.pinnedCandleRows[<market>|<chartInterval>]`. The frame
55
+ * is admitted by the leg's market (`wrunLiveFrameMarketAdmitted`), never
56
+ * by the chart's; every other foreign frame stays `ignored:foreign`.
57
+ * - an ODDS leg (`leg.odds`, the odds lane: an `odds` input's leg on
58
+ * POLYMARKET|<condition id>, grid or period) is POLL-ONLY, the hosted
59
+ * lane mirrored: no candle feed is subscribed for it (the technical-script
60
+ * control skips it), its market's frames are never admitted (a candle
61
+ * frame of that market stays `ignored:foreign`), and `wrunPushEligible`
62
+ * leaves an odds input out of the push set, so the package's other legs
63
+ * keep their push eligibility; the tick's reconcile re-asks the leg.
64
+ * - a session at another interval than the chart (a built-in twin's
65
+ * Timeframe, wrun-run-interval-session.util): the chart market's own
66
+ * candle frames, which only trigger the drain of a chart session, queue
67
+ * and fold into the session's run bars with the grid legs' candle fold on
68
+ * the run grid (every frame carries its own trades, so each folds once;
69
+ * a replayed frame the fold already took is skipped, the chart's rule).
70
+ * No store, no chart instance, no tick-lane import in this module.
71
+ */
72
+ import { findMonday, getDecimalPlaces } from '../chart/titan-constants.pin.js';
73
+ import { Intervals } from '../../constants/interval.constants.js';
74
+ import { FUNDING_INTERVAL_MAP } from '../../constants/overlay-data.constants.js';
75
+ import { mergeLiveOhlcvDataPoint } from '../../kscript-data/controls/live-ohlcv-merge.util.js';
76
+ import { mergeLiveTradesDataPoint, readLiveTradePrint, tradesBarStartMs, tradesBelowLargeFloor, tradesLargeHistoryAnswers, } from '../../kscript-data/controls/live-trades-merge.util.js';
77
+ import { liveAmountUsd, peekOptionTradeUnits } from '../options/option-trade-units.util.js';
78
+ import { parseWrunTapeHistory, wrunTapeHistoryOf } from './wrun-tape-source.util.js';
79
+ import { v2LiveTradePrintBelowFloor } from '../chart/kscript-live-coalesce.util.js';
80
+ import { roundObTimestamp } from '../common/data-processing.util.js';
81
+ import { liveFeedSeriesMatchesChartStream } from '../chart/live-feed-series-match.leaf.js';
82
+ import { liveBucketStartMs, liveBucketVenueOf } from '../chart/live-bucket.util.js';
83
+ import { hostBucketResolverFor } from '../chart/host-bar-placement.util.js';
84
+ import { wrunRowLaneByKey } from './sources/row-lane-registry.js';
85
+ import { wrunHostBarOf } from './wrun-tier-grid.util.js';
86
+ import { barStartMs } from '../chart/calendar-interval.util.js';
87
+ import { getWrunOverlayLiveContext, onWrunOverlayLiveTeardown, } from './wrun-overlay-drivers.util.js';
88
+ import { WRUN_TYPED_PINNED_SOURCES, wrunDeclaredSourceLanes, wrunLegRowHostBar, wrunLaneAcquisitionIntervalMs, wrunPinnedIntervalOf, } from './wrun-source-rows.util.js';
89
+ import { getProfileBucketHeight, normalizeTicksPerBar } from '../../workers/utils/volume-profile.worker.util.js';
90
+ import { WRUN_RUN_INTERVAL_ROWS_KEY, wrunRunIntervalSessionOf, } from './wrun-run-interval-session.util.js';
91
+ import { isWrunLegStreamClass } from './wrun-leg-stream-classes.js';
92
+ import { isWrunTradesLegLane, wrunTradesLegOf, } from './wrun-trades-lane.util.js';
93
+ /** The fan-out's script source name of a frame -> the lane it feeds. The `trades` SOURCE (K_TRADES,
94
+ * the raw TRADE channel) feeds the `tape` lane; the `trades` LANE (the package's per-bar volume
95
+ * by side) is fed by the `buy_sell_volume` source (K_BUY_SELL_VOLUME, TRADE_AGG). */
96
+ export const WRUN_LIVE_LANE_OF_SCRIPT_SOURCE = {
97
+ ohlcv: 'ohlcv',
98
+ buy_sell_volume: 'trades',
99
+ orderbook: 'book',
100
+ funding_rate: 'funding',
101
+ liquidations: 'liquidations',
102
+ volume_profile: 'volume_profile',
103
+ open_interest: 'oi',
104
+ trades: 'tape',
105
+ };
106
+ export const WRUN_PUSH_LANES = new Set([
107
+ 'book',
108
+ 'trades',
109
+ 'trades_usd',
110
+ 'funding',
111
+ 'liquidations',
112
+ 'volume_profile',
113
+ 'tape',
114
+ ]);
115
+ /** Lanes with no push channel: the poll-due reconcile re-asks them (`trade_volume_by_size` is
116
+ * the chart market's USD size-bucketed flow, K_TRADE_VOLUME_BY_SIZE has no live channel and the
117
+ * legacy poll re-asks its tail too; `options_chain` is the chart market's chain snapshot,
118
+ * re-asked through K_OPTIONS_CHAIN with the identity the full run resolved; `etf_flow` is the
119
+ * daily ETF lane, wrun-etf-flow-lane.util: K_ETF_FLOW has no live feed and a day's value lands
120
+ * after the US session). The row builder's own poll lanes are named here; a registry poll lane
121
+ * instance (a `poll:*` id) is a poll lane by construction and is never listed. */
122
+ export const WRUN_POLL_LANES = new Set([
123
+ 'oi',
124
+ 'trade_volume_by_size',
125
+ 'implied_volatility',
126
+ 'skew',
127
+ 'options_chain',
128
+ 'etf_flow',
129
+ ]);
130
+ /** Per additive lane, cap 1024 queued points (the kScript coalescer's cap:
131
+ * about 50 s of the busiest tape); the OLDEST are dropped and counted. */
132
+ export const WRUN_LIVE_ADDITIVE_QUEUE_CAP = 1024;
133
+ /** The live tick's hidden hold (rest-cost H8) keeps the queues filling for as long as the page stays
134
+ * hidden: the drain arms no timer. So that the cap above never drops a held point, a held overlay
135
+ * whose longest capped queue reaches this many points runs ONE drain while hidden
136
+ * (`wrunLiveIntakeBacklogDue`), then holds again. Half the cap: the run leaves the other half as
137
+ * headroom for the frames that land while it is in flight (the local kScript hold's bound, same cap). */
138
+ export const WRUN_LIVE_HIDDEN_BACKLOG_RUN_POINTS = WRUN_LIVE_ADDITIVE_QUEUE_CAP / 2;
139
+ const BLOCK_ROWS_KEPT = 64;
140
+ const RECONCILE_WINDOWS_KEPT = 8;
141
+ const intakes = new Map();
142
+ const declaredLanesByContext = new WeakMap();
143
+ let drainRequester = null;
144
+ /** The tick util registers its drain scheduler here at import time so a gap
145
+ * mark wakes the tick without a frame (the intake never imports the tick). */
146
+ export function setWrunLiveDrainRequester(fn) {
147
+ drainRequester = fn;
148
+ }
149
+ function emptyStats() {
150
+ return {
151
+ frames: {},
152
+ legFrames: {},
153
+ dropped: 0,
154
+ droppedGeneration: 0,
155
+ drains: 0,
156
+ computes: 0,
157
+ blockRows: [],
158
+ repairs: {},
159
+ reseeds: 0,
160
+ rebuilds: 0,
161
+ restores: 0,
162
+ snapshots: 0,
163
+ lastEvalMs: 0,
164
+ lastMainMs: 0,
165
+ fetches: 0,
166
+ reconcileWindows: [],
167
+ bypassTokensHeld: 0,
168
+ ignored: {},
169
+ };
170
+ }
171
+ function intakeFor(indicatorId, chartIndex, intervalMs, generation) {
172
+ let intake = intakes.get(indicatorId);
173
+ if (!intake) {
174
+ intake = {
175
+ chartIndex,
176
+ intervalMs,
177
+ generation,
178
+ additive: {},
179
+ bookLive: new Map(),
180
+ bookReplay: new Map(),
181
+ funding: new Map(),
182
+ legs: new Map(),
183
+ rowLanes: new Map(),
184
+ droppedSinceDrain: 0,
185
+ tapeLost: new Set(),
186
+ gaps: [],
187
+ stats: emptyStats(),
188
+ };
189
+ intakes.set(indicatorId, intake);
190
+ }
191
+ else {
192
+ intake.chartIndex = chartIndex;
193
+ intake.intervalMs = intervalMs;
194
+ }
195
+ return intake;
196
+ }
197
+ /** Discard every queued point (they were accepted under a generation whose
198
+ * context is gone) and count them; the queues then belong to `generation`. */
199
+ function discardQueuesForGeneration(intake, generation) {
200
+ const pending = pendingCount(intake);
201
+ intake.additive = {};
202
+ // The fresh full run's fetch holds the prints the cap dropped: nothing is owed for them.
203
+ intake.tapeLost = new Set();
204
+ intake.bookLive = new Map();
205
+ intake.bookReplay = new Map();
206
+ intake.funding = new Map();
207
+ intake.legs = new Map();
208
+ intake.rowLanes = new Map();
209
+ intake.droppedSinceDrain = 0;
210
+ intake.stats.droppedGeneration += pending;
211
+ intake.generation = generation;
212
+ }
213
+ /** The queues of an overlay follow the generation of the context they
214
+ * were accepted under; a mismatch means a full run replaced the context
215
+ * (its fresh fetch already holds the queued increments). */
216
+ function bindQueuesToGeneration(intake, generation) {
217
+ if (intake.generation !== generation)
218
+ discardQueuesForGeneration(intake, generation);
219
+ }
220
+ /** A reseed reopens the SESSION over the retained buffers under a fresh
221
+ * generation: the points queued under the previous generation are still
222
+ * owed to those buffers, so the reseed adopts them instead of dropping
223
+ * them (the run util calls this right after it stores the generation). */
224
+ export function adoptWrunLiveIntakeGeneration(indicatorId, generation) {
225
+ const intake = intakes.get(indicatorId);
226
+ if (intake)
227
+ intake.generation = generation;
228
+ }
229
+ /** The source classes the package declares (cached per context object). */
230
+ function declaredLanesOf(context) {
231
+ let declared = declaredLanesByContext.get(context);
232
+ if (!declared) {
233
+ // The lanes the inputs read (wrunSourceLane): a trades input asking dollars reads trades_usd.
234
+ declared = wrunDeclaredSourceLanes(context.metadata);
235
+ declaredLanesByContext.set(context, declared);
236
+ }
237
+ return declared;
238
+ }
239
+ /** The trades leg lanes (kit 0.77.0) the run asked on the frame's market. */
240
+ function tradesLegLanesForFrame(context, frame) {
241
+ if (!frame.incomingExchange || !frame.incomingSymbol || context.tradeLegRows === undefined)
242
+ return [];
243
+ const series = { exchange: frame.incomingExchange, symbol: frame.incomingSymbol };
244
+ const lanes = [];
245
+ for (const lane of declaredLanesOf(context)) {
246
+ if (!isWrunTradesLegLane(lane) || context.tradeLegRows[lane] === undefined)
247
+ continue;
248
+ const leg = wrunTradesLegOf(lane);
249
+ if (liveFeedSeriesMatchesChartStream(series, { exchange: leg.exchange, symbol: leg.symbol }))
250
+ lanes.push(lane);
251
+ }
252
+ return lanes;
253
+ }
254
+ /** The interval a lane's live points fold at: a trades leg's own tier (the interval its acquisition was asked at,
255
+ * `wrunLaneAcquisitionIntervalMs`), else `fallbackMs` (the chart's, or a session's run interval). */
256
+ function laneFoldIntervalMs(context, lane, fallbackMs) {
257
+ if (!isWrunTradesLegLane(lane))
258
+ return fallbackMs;
259
+ return wrunLaneAcquisitionIntervalMs(context.acquisition[lane]) ?? fallbackMs;
260
+ }
261
+ /** A trades leg's own market (the chart's session), its live stamps' buckets. */
262
+ function tradesLegBucketMarket(context, lane) {
263
+ const leg = wrunTradesLegOf(lane);
264
+ const sessionType = context.market?.sessionType;
265
+ return typeof sessionType === 'string'
266
+ ? { exchange: leg.exchange, symbol: leg.symbol, sessionType }
267
+ : { exchange: leg.exchange, symbol: leg.symbol };
268
+ }
269
+ /** The push lanes one frame feeds: its script source's lane, and for a buy/sell frame each
270
+ * trades lane the package reads (the coin lane, the dollar lane, or both: one TRADE_AGG
271
+ * stream, folded by each lane's own quote). */
272
+ function frameLanesOf(context, lane) {
273
+ const declared = declaredLanesOf(context);
274
+ if (lane !== 'trades')
275
+ return declared.has(lane) ? [lane] : [];
276
+ return ['trades', 'trades_usd'].filter((tradesLane) => declared.has(tradesLane));
277
+ }
278
+ /** Count a refusal on the overlay's stats: a live overlay gets its record on
279
+ * the first verdict; an id without a context is counted only when a record
280
+ * already exists (no records for unknown ids). */
281
+ function noteVerdict(indicatorId, verdict, context) {
282
+ const stats = context
283
+ ? intakeFor(indicatorId, context.chartIndex, context.intervalMs, context.generation).stats
284
+ : intakes.get(indicatorId)?.stats;
285
+ if (stats)
286
+ stats.ignored[verdict] = (stats.ignored[verdict] ?? 0) + 1;
287
+ return verdict;
288
+ }
289
+ /** The lanes whose history the gateway rolls onto a session calendar: the
290
+ * CME session lattice on a CME Group market (TRADE_AGG,
291
+ * TRADE_SIDE_AGNOSTIC_AGG and the per-symbol VOLUME_PROFILE_AGG) and the
292
+ * 09:30 New York anchored bars of a regular-session US equities market;
293
+ * liquidations and funding stay on UTC buckets. */
294
+ const WRUN_SESSION_LATTICE_LANES = new Set([
295
+ 'trades',
296
+ 'trades_usd',
297
+ 'volume_profile',
298
+ 'ohlcv',
299
+ ]);
300
+ /** The chart bucket a live stamp belongs to: every lane floors to the bar
301
+ * open (a week bucket is its Monday, the main path's `findMonday`; a month,
302
+ * quarter, half or year bucket its UTC calendar boundary), except
303
+ * the book, whose snapshots are stamped at the observation and an exact
304
+ * boundary belongs to the PREVIOUS bar (`roundObTimestamp`, the rule the
305
+ * K_ORDERBOOK lane and the rider projection apply), and a session-lattice
306
+ * lane on a `market`, which keys the bar its fetched rows have through the
307
+ * chart's one live boundary rule (`liveBucketStartMs`: the Chicago session
308
+ * lattice on a CME Group market, the 09:30 New York anchored bars and the
309
+ * three-trade-date 3d group on a regular-session US equities market under
310
+ * the host chart's `sessionType`, the UTC floor everywhere else). Null is a
311
+ * stamp with no bar on that calendar (a stock print outside the cash
312
+ * session on a regular-session chart): the caller drops it. Any other
313
+ * market passes no venue, so nothing else starts anchoring. */
314
+ export function wrunLiveBucketTs(lane, tsMs, intervalMs, market) {
315
+ if (lane === 'book')
316
+ return roundObTimestamp(tsMs, intervalMs);
317
+ if (market && WRUN_SESSION_LATTICE_LANES.has(lane)) {
318
+ return liveBucketStartMs(tsMs, intervalMs, {
319
+ exchange: market.exchange,
320
+ symbol: market.symbol,
321
+ sessionType: market.sessionType,
322
+ });
323
+ }
324
+ if (intervalMs === Intervals.WEEK)
325
+ return findMonday(tsMs);
326
+ return barStartMs(tsMs, intervalMs);
327
+ }
328
+ // ---------------------------------------------------------------------------
329
+ // Folds (never mutate their inputs: the base rows array and every row object
330
+ // and point handed in are read only; touched rows are NEW arrays in a NEW
331
+ // container, untouched rows are shared by reference).
332
+ // ---------------------------------------------------------------------------
333
+ function finiteOrZero(value) {
334
+ const numeric = Number(value);
335
+ return Number.isFinite(numeric) ? numeric : 0;
336
+ }
337
+ function isRecord(value) {
338
+ return typeof value === 'object' && value !== null;
339
+ }
340
+ /** A wire timestamp `{ seconds }` in ms, or null when absent (the controls
341
+ * skip such points). */
342
+ function wireTsMs(timestamp) {
343
+ if (!isRecord(timestamp))
344
+ return null;
345
+ const seconds = Number(timestamp.seconds);
346
+ if (!Number.isFinite(seconds) || seconds <= 0)
347
+ return null;
348
+ return seconds * 1000;
349
+ }
350
+ function sideOf(point) {
351
+ const side = isRecord(point) && isRecord(point.series) ? point.series.side : undefined;
352
+ return side === 'BUY' || side === 'SELL' ? side : null;
353
+ }
354
+ /** Binary search: the index of the first row whose ts is >= `ts`. */
355
+ function lowerBound(rows, ts) {
356
+ let low = 0;
357
+ let high = rows.length;
358
+ while (low < high) {
359
+ const mid = (low + high) >>> 1;
360
+ if (Number(rows[mid][0]) < ts)
361
+ low = mid + 1;
362
+ else
363
+ high = mid;
364
+ }
365
+ return low;
366
+ }
367
+ /** The merged lane: the base rows (sorted ascending) with the touched
368
+ * buckets replaced in place and the new buckets inserted in order. */
369
+ function mergeLaneRows(base, replaced, added) {
370
+ const out = new Array(base.length);
371
+ for (let at = 0; at < base.length; at += 1) {
372
+ const row = base[at];
373
+ const replacement = replaced.get(Number(row[0]));
374
+ out[at] = replacement === undefined ? row : replacement;
375
+ }
376
+ if (added.size === 0)
377
+ return out;
378
+ const additions = [...added.entries()].sort((a, b) => a[0] - b[0]);
379
+ const lastTs = out.length > 0 ? Number(out[out.length - 1][0]) : Number.NEGATIVE_INFINITY;
380
+ for (const [ts, row] of additions) {
381
+ if (ts > lastTs || out.length === 0) {
382
+ out.push(row);
383
+ continue;
384
+ }
385
+ out.splice(lowerBound(out, ts), 0, row);
386
+ }
387
+ return out;
388
+ }
389
+ function foldResult(rows, touched) {
390
+ if (touched.size === 0)
391
+ return null;
392
+ const list = [...touched].sort((a, b) => a - b);
393
+ return { rows, touched: list, earliestTouchedTs: list[0] };
394
+ }
395
+ /** The shared additive fold of K_BUY_SELL_VOLUME and K_LIQUIDATIONS: per
396
+ * point, the bucket's [ts, buy, sell] row gains the amount on its side
397
+ * (finite-or-zero on the untouched side, as the controls normalize). */
398
+ function foldSidedAdditive(base, points, intervalMs, lane, read, market) {
399
+ const indexByTs = new Map();
400
+ for (let at = 0; at < base.length; at += 1)
401
+ indexByTs.set(Number(base[at][0]), at);
402
+ const replaced = new Map();
403
+ const added = new Map();
404
+ const touched = new Set();
405
+ for (const point of points) {
406
+ const parsed = read(point);
407
+ if (!parsed)
408
+ continue;
409
+ const bucket = wrunLiveBucketTs(lane, parsed.tsMs, intervalMs, market);
410
+ if (bucket === null)
411
+ continue;
412
+ const existing = replaced.get(bucket) ??
413
+ added.get(bucket) ??
414
+ (indexByTs.has(bucket) ? base[indexByTs.get(bucket)] : undefined);
415
+ let next;
416
+ if (existing === undefined) {
417
+ next = [bucket, 0, 0];
418
+ }
419
+ else {
420
+ next = [bucket, finiteOrZero(existing[1]), finiteOrZero(existing[2])];
421
+ }
422
+ if (parsed.side === 'BUY')
423
+ next[1] += parsed.amount;
424
+ else
425
+ next[2] += parsed.amount;
426
+ if (indexByTs.has(bucket))
427
+ replaced.set(bucket, next);
428
+ else
429
+ added.set(bucket, next);
430
+ touched.add(bucket);
431
+ }
432
+ if (touched.size === 0)
433
+ return null;
434
+ return foldResult(mergeLaneRows(base, replaced, added), touched);
435
+ }
436
+ /** The USD of a coin-arm amount at `price` (`liveAmountUsd`): the frames are live-feed's COIN arm (the channels ask
437
+ * no quote), so an option contract's amount arrives in coin and its USD is the premium by the resolver's live-print
438
+ * rule, never amount x price; null while its units are unknown and on a coin-premium book. Every other market is
439
+ * amount x price. The point's own series names the market (its units are the ones the K_* control's read answered:
440
+ * `peekOptionTradeUnits`). */
441
+ function coinArmUsd(point, price, amount) {
442
+ const series = isRecord(point.series) ? point.series : undefined;
443
+ const exchange = typeof series?.exchange === 'string' ? series.exchange : '';
444
+ const symbol = typeof series?.symbol === 'string' ? series.symbol : '';
445
+ return liveAmountUsd(price, amount, peekOptionTradeUnits({ exchange, symbol }));
446
+ }
447
+ /** K_BUY_SELL_VOLUME.mergeLiveDataPoint's reading of a TRADE_AGG point: the
448
+ * volume in coins, or its USD when the acquisition asked for USD (`coinArmUsd`:
449
+ * volume x close, an option's premium; skipped without a usable close or a USD,
450
+ * never scaled by a bad price). */
451
+ function tradeReader(acquisition) {
452
+ const usdQuoted = String(acquisition.currency ?? '').toUpperCase() === 'USD';
453
+ return (point) => {
454
+ if (!isRecord(point))
455
+ return null;
456
+ const trade = point.tradeAggregation;
457
+ if (!isRecord(trade) || trade.volume === undefined || trade.volume === null)
458
+ return null;
459
+ const tsMs = wireTsMs(trade.timestamp);
460
+ if (tsMs === null)
461
+ return null;
462
+ const close = finiteOrZero(trade.close);
463
+ if (usdQuoted && close <= 0)
464
+ return null;
465
+ const side = sideOf(point);
466
+ if (!side)
467
+ return null;
468
+ const amount = usdQuoted ? coinArmUsd(point, close, finiteOrZero(trade.volume)) : finiteOrZero(trade.volume);
469
+ if (amount === null)
470
+ return null;
471
+ return { tsMs, side, amount };
472
+ };
473
+ }
474
+ /** K_LIQUIDATIONS.mergeLiveDataPoint's reading of a LIQUIDATION point, in the lane's units: the
475
+ * K_LIQUIDATIONS history asks USD (its request payload), so a lane whose acquisition names no
476
+ * other currency folds the USD of the amount (base coins on the wire, `coinArmUsd`: amount x
477
+ * price, an option's premium); skipped without a usable price or a USD, never scaled by a bad one.
478
+ * A lane acquired in coins folds the amount as it is. */
479
+ function liquidationReader(acquisition) {
480
+ const usdQuoted = String(acquisition.currency ?? 'USD').toUpperCase() === 'USD';
481
+ return (point) => {
482
+ if (!isRecord(point))
483
+ return null;
484
+ const liquidation = point.liquidation;
485
+ if (!isRecord(liquidation) || liquidation.amount === undefined || liquidation.amount === null)
486
+ return null;
487
+ const tsMs = wireTsMs(liquidation.timestamp);
488
+ if (tsMs === null)
489
+ return null;
490
+ const side = sideOf(point);
491
+ if (!side)
492
+ return null;
493
+ const amount = finiteOrZero(liquidation.amount);
494
+ if (!usdQuoted)
495
+ return { tsMs, side, amount };
496
+ const price = finiteOrZero(liquidation.price);
497
+ if (price <= 0)
498
+ return null;
499
+ const usd = coinArmUsd(point, price, amount);
500
+ return usd === null ? null : { tsMs, side, amount: usd };
501
+ };
502
+ }
503
+ /** K_FUNDING_RATE.mergeLiveDataPoint's value: the rate normalized to one
504
+ * hour (the series' funding interval, 8h by default) in percent, the same
505
+ * unit the history lane fetches (`fundingInterval: 1h`, `rateClose * 100`). */
506
+ export function wrunLiveFundingValue(point) {
507
+ if (!isRecord(point))
508
+ return null;
509
+ const funding = point.fundingRate;
510
+ if (!isRecord(funding) || funding.fundingRate === undefined || funding.fundingRate === null)
511
+ return null;
512
+ const tsMs = wireTsMs(funding.timestamp);
513
+ if (tsMs === null)
514
+ return null;
515
+ const series = isRecord(point.series) ? point.series : undefined;
516
+ const declaredInterval = series?.fundingInterval;
517
+ const fundingIntervalMs = declaredInterval !== undefined && declaredInterval !== null && String(declaredInterval) !== ''
518
+ ? parseInt(String(declaredInterval), 10)
519
+ : FUNDING_INTERVAL_MAP['8h'];
520
+ const value = ((Number(funding.fundingRate) * FUNDING_INTERVAL_MAP['1h']) / fundingIntervalMs) * 100;
521
+ return { tsMs, value };
522
+ }
523
+ function foldFunding(base, points, intervalMs) {
524
+ const indexByTs = new Map();
525
+ for (let at = 0; at < base.length; at += 1)
526
+ indexByTs.set(Number(base[at][0]), at);
527
+ const replaced = new Map();
528
+ const added = new Map();
529
+ const touched = new Set();
530
+ for (const point of points) {
531
+ const parsed = wrunLiveFundingValue(point);
532
+ if (!parsed)
533
+ continue;
534
+ const bucket = wrunLiveBucketTs('funding', parsed.tsMs, intervalMs);
535
+ if (bucket === null)
536
+ continue;
537
+ const row = [bucket, parsed.value];
538
+ if (indexByTs.has(bucket))
539
+ replaced.set(bucket, row);
540
+ else
541
+ added.set(bucket, row);
542
+ touched.add(bucket);
543
+ }
544
+ if (touched.size === 0)
545
+ return null;
546
+ return foldResult(mergeLaneRows(base, replaced, added), touched);
547
+ }
548
+ /** K_OHLCV.mergeLiveDataPoint's reading of a candle frame: the TRADE_AGG
549
+ * aggregation, or a TICKER quote as a zero-volume print (forex and
550
+ * commodity venues), stamped by its wire timestamp. */
551
+ export function wrunLiveOhlcvPoint(point) {
552
+ if (!isRecord(point))
553
+ return null;
554
+ let data;
555
+ if (isRecord(point.tradeAggregation))
556
+ data = point.tradeAggregation;
557
+ else if (isRecord(point.ticker)) {
558
+ const price = point.ticker.price;
559
+ data = { timestamp: point.ticker.timestamp, open: price, high: price, low: price, close: price, volume: 0 };
560
+ }
561
+ if (!data)
562
+ return null;
563
+ const tsMs = wireTsMs(data.timestamp);
564
+ return tsMs === null ? null : { tsMs, data };
565
+ }
566
+ /**
567
+ * The candle fold of a foreign grid leg: K_OHLCV.mergeLiveDataPoint per
568
+ * point (`mergeLiveOhlcvDataPoint`, run over the bucket's own row and its
569
+ * predecessor so the plausibility band reads the same reference price the
570
+ * control reads: the first frame of a bucket opens the row, later frames
571
+ * widen high/low, replace the close and ADD volume), bucketed by the chart
572
+ * rule on the leg's own `market` (a WEEK bucket on its Monday, a CME Group
573
+ * leg on the session lattice). Never mutates its inputs.
574
+ */
575
+ export function foldWrunLegCandleRows(base, points, intervalMs, market) {
576
+ if (!Number.isFinite(intervalMs) || intervalMs <= 0)
577
+ return null;
578
+ const indexByTs = new Map();
579
+ for (let at = 0; at < base.length; at += 1)
580
+ indexByTs.set(Number(base[at][0]), at);
581
+ const replaced = new Map();
582
+ const added = new Map();
583
+ const touched = new Set();
584
+ let newestTs = base.length > 0 ? Number(base[base.length - 1][0]) : Number.NEGATIVE_INFINITY;
585
+ const rowAt = (ts) => replaced.get(ts) ?? added.get(ts) ?? (indexByTs.has(ts) ? base[indexByTs.get(ts)] : undefined);
586
+ for (const point of points) {
587
+ const parsed = wrunLiveOhlcvPoint(point);
588
+ if (!parsed)
589
+ continue;
590
+ const bucket = wrunLiveBucketTs('ohlcv', parsed.tsMs, intervalMs, market);
591
+ // No bar on the leg's calendar (a stock print outside the cash session): nothing to fold.
592
+ if (bucket === null)
593
+ continue;
594
+ const existing = rowAt(bucket);
595
+ // The control's reference price: the bucket's own row, else the row
596
+ // before it (an existing bucket's predecessor, a new bucket's newest).
597
+ let predecessor;
598
+ if (existing !== undefined) {
599
+ const at = indexByTs.get(bucket);
600
+ predecessor = at !== undefined && at > 0 ? rowAt(Number(base[at - 1][0])) : undefined;
601
+ }
602
+ else if (Number.isFinite(newestTs)) {
603
+ predecessor = rowAt(newestTs);
604
+ }
605
+ const slice = [];
606
+ if (predecessor !== undefined)
607
+ slice.push(Array.from(predecessor));
608
+ if (existing !== undefined)
609
+ slice.push(Array.from(existing));
610
+ const merged = mergeLiveOhlcvDataPoint(slice, parsed.data, bucket);
611
+ const row = merged[merged.length - 1];
612
+ if (row === undefined || Number(row[0]) !== bucket)
613
+ continue;
614
+ if (indexByTs.has(bucket))
615
+ replaced.set(bucket, row);
616
+ else
617
+ added.set(bucket, row);
618
+ if (bucket > newestTs)
619
+ newestTs = bucket;
620
+ touched.add(bucket);
621
+ }
622
+ if (touched.size === 0)
623
+ return null;
624
+ return foldResult(mergeLaneRows(base, replaced, added), touched);
625
+ }
626
+ /** K_ORDERBOOK.buildLiveDataRows over a raw frame: replay bursts floor
627
+ * every row (copied), a live snapshot floors its one row (copied); a
628
+ * bucketed row (`ArrayLike<number>`) is taken as-is. Frames without a
629
+ * book payload yield nothing. */
630
+ export function wrunLiveBookRows(point, intervalMs) {
631
+ if (typeof point.length === 'number' && !isRecord(point.series)) {
632
+ const row = point;
633
+ return Number.isFinite(Number(row[0])) ? [row] : [];
634
+ }
635
+ const frame = point;
636
+ if (frame.series?.isReplay) {
637
+ const rows = (frame.float64Arrays ?? []).map((source) => {
638
+ const copy = Float64Array.from(source);
639
+ if (copy.length > 0)
640
+ copy[0] = roundObTimestamp(copy[0], intervalMs);
641
+ return copy;
642
+ });
643
+ return rows.filter((row) => row.length > 0).sort((a, b) => a[0] - b[0]);
644
+ }
645
+ if (!frame.blockBookSnapshot)
646
+ return [];
647
+ const copy = Float64Array.from(frame.blockBookSnapshot);
648
+ if (copy.length === 0)
649
+ return [];
650
+ copy[0] = roundObTimestamp(copy[0], intervalMs);
651
+ return [copy];
652
+ }
653
+ function foldBook(base, points, intervalMs) {
654
+ const indexByTs = new Map();
655
+ for (let at = 0; at < base.length; at += 1)
656
+ indexByTs.set(Number(base[at][0]), at);
657
+ const replaced = new Map();
658
+ const added = new Map();
659
+ const touched = new Set();
660
+ for (const point of points) {
661
+ for (const row of wrunLiveBookRows(point, intervalMs)) {
662
+ const bucket = Number(row[0]);
663
+ if (indexByTs.has(bucket))
664
+ replaced.set(bucket, row);
665
+ else
666
+ added.set(bucket, row);
667
+ touched.add(bucket);
668
+ }
669
+ }
670
+ if (touched.size === 0)
671
+ return null;
672
+ return foldResult(mergeLaneRows(base, replaced, added), touched);
673
+ }
674
+ /** Absolute zero-based lattice cell a price falls into (the control's
675
+ * `latticeCellLow`): ratios within epsilon of an integer snap to it. */
676
+ function latticeCellLow(price, gridHeight, precision) {
677
+ const ratio = price / gridHeight;
678
+ const nearest = Math.round(ratio);
679
+ const cellIndex = Math.abs(ratio - nearest) < 1e-6 ? nearest : Math.floor(ratio);
680
+ return Number((cellIndex * gridHeight).toFixed(precision));
681
+ }
682
+ /** The control's `deriveRowGridHeight`: the median interior bucket width of
683
+ * the most recent row with enough buckets, else the first usable width. */
684
+ function deriveRowGridHeight(rows) {
685
+ for (let at = rows.length - 1; at >= 0; at -= 1) {
686
+ const row = rows[at];
687
+ if (!Array.isArray(row) || row.length < 6)
688
+ continue;
689
+ const widths = [];
690
+ for (let cell = 1; cell < row.length; cell += 1) {
691
+ const bucket = row[cell];
692
+ if (!Array.isArray(bucket) || bucket.length < 4 || !(bucket[1] > bucket[0]))
693
+ continue;
694
+ const precision = Math.max(getDecimalPlaces(bucket[0]), getDecimalPlaces(bucket[1]));
695
+ widths.push(Number((bucket[1] - bucket[0]).toFixed(precision)));
696
+ }
697
+ const interior = widths.slice(1, -1);
698
+ if (interior.length >= 3) {
699
+ interior.sort((a, b) => a - b);
700
+ return interior[Math.floor(interior.length / 2)];
701
+ }
702
+ }
703
+ return getProfileBucketHeight(rows);
704
+ }
705
+ /** The control's `resolveFoldGridHeight`: the authoritative metadata block
706
+ * size when the acquisition carries it (x ticksPerBar, rounded to the
707
+ * step's precision), else the row-geometry derivation. */
708
+ function resolveVpGridHeight(rows, acquisition) {
709
+ const blockSize = Number(acquisition.minimumBarHeight);
710
+ if (Number.isFinite(blockSize) && blockSize > 0) {
711
+ return Number((blockSize * normalizeTicksPerBar(acquisition.ticksPerBar)).toFixed(getDecimalPlaces(blockSize)));
712
+ }
713
+ return deriveRowGridHeight(rows);
714
+ }
715
+ /** One VP delta point folded onto a clone of the lane: the new row for its
716
+ * bucket, or null when the control would reject the point (no base, wrong
717
+ * quote, no grid, nothing to fold). */
718
+ function foldVpPoint(rows, rowIndexOf, point, intervalMs, acquisition, market) {
719
+ if (!isRecord(point))
720
+ return null;
721
+ const vpa = point.volumeProfileAggregation;
722
+ if (!isRecord(vpa) || !Array.isArray(vpa.profile) || vpa.profile.length === 0)
723
+ return null;
724
+ const tsMs = wireTsMs(vpa.timestamp);
725
+ if (tsMs === null)
726
+ return null;
727
+ const series = isRecord(point.series) ? point.series : undefined;
728
+ const pointCurrency = series?.inverse === true ? 'USD' : 'Coin';
729
+ const sourceCurrency = acquisition.currency === 'USD' ? 'USD' : 'Coin';
730
+ if (pointCurrency !== sourceCurrency)
731
+ return null;
732
+ if (rows.length === 0)
733
+ return null;
734
+ const gridHeight = resolveVpGridHeight(rows, acquisition);
735
+ if (!(gridHeight > 0))
736
+ return null;
737
+ const precision = getDecimalPlaces(gridHeight);
738
+ const bucket = wrunLiveBucketTs('volume_profile', tsMs, intervalMs, market);
739
+ if (bucket === null)
740
+ return null;
741
+ const byCell = new Map();
742
+ const target = rowIndexOf(bucket);
743
+ if (target !== undefined && Array.isArray(target)) {
744
+ for (let at = 1; at < target.length; at += 1) {
745
+ const cell = target[at];
746
+ if (!Array.isArray(cell) || cell.length < 4)
747
+ continue;
748
+ const key = latticeCellLow(cell[0], gridHeight, precision);
749
+ const existing = byCell.get(key);
750
+ if (existing) {
751
+ existing[0] = Math.min(existing[0], cell[0]);
752
+ existing[1] = Math.max(existing[1], cell[1]);
753
+ existing[2] += cell[2];
754
+ existing[3] += cell[3];
755
+ }
756
+ else {
757
+ byCell.set(key, [cell[0], cell[1], cell[2], cell[3]]);
758
+ }
759
+ }
760
+ }
761
+ let folded = false;
762
+ const profile = vpa.profile;
763
+ for (let at = 0; at + 2 < profile.length; at += 3) {
764
+ const price = Number(profile[at]);
765
+ const buyDelta = Number(profile[at + 1]) || 0;
766
+ const sellDelta = Number(profile[at + 2]) || 0;
767
+ if (!Number.isFinite(price) || (buyDelta === 0 && sellDelta === 0))
768
+ continue;
769
+ const key = latticeCellLow(price, gridHeight, precision);
770
+ let cell = byCell.get(key);
771
+ if (!cell) {
772
+ cell = [key, Number((key + gridHeight).toFixed(precision)), 0, 0];
773
+ byCell.set(key, cell);
774
+ }
775
+ cell[2] = Math.max(0, cell[2] + buyDelta);
776
+ cell[3] = Math.max(0, cell[3] + sellDelta);
777
+ folded = true;
778
+ }
779
+ if (!folded)
780
+ return null;
781
+ const buckets = Array.from(byCell.values())
782
+ .filter((cell) => cell[2] > 0 || cell[3] > 0)
783
+ .sort((a, b) => a[0] - b[0]);
784
+ return { bucket, row: [bucket, ...buckets] };
785
+ }
786
+ function foldVolumeProfile(base, points, intervalMs, acquisition, market) {
787
+ const indexByTs = new Map();
788
+ for (let at = 0; at < base.length; at += 1)
789
+ indexByTs.set(Number(base[at][0]), at);
790
+ const replaced = new Map();
791
+ const added = new Map();
792
+ const touched = new Set();
793
+ // Each point folds onto the lane AS FOLDED SO FAR (a bucket several deltas
794
+ // touch accumulates them), never onto the caller's rows.
795
+ const rowOf = (bucket) => replaced.get(bucket) ??
796
+ added.get(bucket) ??
797
+ (indexByTs.has(bucket) ? base[indexByTs.get(bucket)] : undefined);
798
+ for (const point of points) {
799
+ const folded = foldVpPoint(base, rowOf, point, intervalMs, acquisition, market);
800
+ if (!folded)
801
+ continue;
802
+ if (indexByTs.has(folded.bucket))
803
+ replaced.set(folded.bucket, folded.row);
804
+ else
805
+ added.set(folded.bucket, folded.row);
806
+ touched.add(folded.bucket);
807
+ }
808
+ if (touched.size === 0)
809
+ return null;
810
+ return foldResult(mergeLaneRows(base, replaced, added), touched);
811
+ }
812
+ /** The tape fold: K_TRADES's live fold per raw print (`mergeLiveTradesDataPoint`), each point onto
813
+ * the rows as folded so far, under the acquisition's `minSize`. The leaf answers a NEW container
814
+ * with the touched row new and every other row shared, or null for a rejected print (the rows
815
+ * stand); the touched bucket is the newest row (a fold never writes behind it). A bucket the fold
816
+ * capped (its newest 20k prints kept, the oldest dropped) is reported: its row is truncated, and
817
+ * a block is never truncated, so the tick refuses it by name where the kScript lane only warns. On
818
+ * the large lane the large-print table's floor applies too (`largeFloor`, the chart's market for a
819
+ * frame that names none). */
820
+ function foldTape(base, points, intervalMs, acquisition, market) {
821
+ let rows = base;
822
+ const touched = new Set();
823
+ const capped = new Set();
824
+ const largeFloor = acquisition.large === true ? largeFloorMarketOf(market ?? {}) : undefined;
825
+ for (const point of points) {
826
+ let cappedNow = false;
827
+ const next = mergeLiveTradesDataPoint(rows, point, intervalMs, {
828
+ minSize: acquisition.minSize,
829
+ largeFloor,
830
+ onCapped: () => {
831
+ cappedNow = true;
832
+ },
833
+ });
834
+ if (next === null)
835
+ continue;
836
+ const bucket = Number(next[next.length - 1][0]);
837
+ touched.add(bucket);
838
+ if (cappedNow)
839
+ capped.add(bucket);
840
+ rows = next;
841
+ }
842
+ const folded = foldResult(rows, touched);
843
+ return folded !== null && capped.size > 0 ? { ...folded, capped: [...capped].sort((a, b) => a - b) } : folded;
844
+ }
845
+ /**
846
+ * Fold queued points into a lane's rows under the lane's kScript fold rule.
847
+ * Returns the NEW rows array (untouched rows shared, touched rows new), the
848
+ * touched buckets and the earliest of them, or null when nothing folded.
849
+ * Never mutates `rows` or any point. `acquisition` is the SAME payload the
850
+ * history fetch used for the lane, so the fold's units equal the lane's;
851
+ * `market` is the market that fetch resolved to (the chart's own), so the
852
+ * fold keys the bars its rows have (wrunLiveBucketTs).
853
+ */
854
+ export function foldWrunLaneRows(lane, rows, points, intervalMs, acquisition, market) {
855
+ if (points.length === 0)
856
+ return null;
857
+ if (!Number.isFinite(intervalMs) || intervalMs <= 0)
858
+ return null;
859
+ const base = rows ?? [];
860
+ switch (lane) {
861
+ // Each trades lane folds by its own acquisition's quote (wrun-trades-lane.util): the dollar
862
+ // lane's payload carries `currency: "USD"`, so its trades fold as volume x close.
863
+ case 'trades':
864
+ case 'trades_usd':
865
+ return foldSidedAdditive(base, points, intervalMs, lane, tradeReader(acquisition), market);
866
+ case 'liquidations':
867
+ return foldSidedAdditive(base, points, intervalMs, lane, liquidationReader(acquisition), market);
868
+ case 'funding':
869
+ return foldFunding(base, points, intervalMs);
870
+ case 'book':
871
+ return foldBook(base, points, intervalMs);
872
+ case 'volume_profile':
873
+ return foldVolumeProfile(base, points, intervalMs, acquisition, market);
874
+ case 'tape':
875
+ // The print's own envelope names its venue (the CME lattice reads it), never the chart's.
876
+ return foldTape(base, points, intervalMs, acquisition, market);
877
+ default: {
878
+ // A trades leg folds as its quote's own lane, on its market; the poll lanes have no live fold.
879
+ const leg = wrunTradesLegOf(lane);
880
+ return leg === undefined
881
+ ? null
882
+ : foldSidedAdditive(base, points, intervalMs, leg.lane, tradeReader(acquisition), market);
883
+ }
884
+ }
885
+ }
886
+ // ---------------------------------------------------------------------------
887
+ // Intake queues
888
+ // ---------------------------------------------------------------------------
889
+ /** The chart stream identity a context binds its frames to: the stored
890
+ * market (with its aliases), else the exchange and symbol of the market
891
+ * identity string (contexts stored before the market field existed). */
892
+ function chartMarketOf(context) {
893
+ const market = context.market;
894
+ if (market && typeof market.exchange === 'string' && typeof market.symbol === 'string') {
895
+ return market;
896
+ }
897
+ const [exchange = '', symbol = ''] = (context.marketIdentity ?? '').split('|');
898
+ return exchange && symbol ? { exchange, symbol } : null;
899
+ }
900
+ /** True when the frame carries a full series identity that is not the
901
+ * chart's own market under the SAME alias-aware matcher the live fan-out
902
+ * admits frames with (`liveFeedSeriesMatchesChartStream`: a continuous
903
+ * chart's frames are labelled with the resolved lead contract, a delivery
904
+ * symbol's with its wire exchange, a normalized symbol's with its
905
+ * normalized form). A frame without a full identity, or a context without
906
+ * a market to judge by, is never foreign (the fan-out admitted it). */
907
+ function isForeignFrame(context, frame) {
908
+ if (!frame.incomingExchange || !frame.incomingSymbol)
909
+ return false;
910
+ const market = chartMarketOf(context);
911
+ if (!market)
912
+ return false;
913
+ return !liveFeedSeriesMatchesChartStream({ exchange: frame.incomingExchange, symbol: frame.incomingSymbol }, { ...market, ...context.market });
914
+ }
915
+ /** The market the large-print floor judges a chart's tape prints on: its identity and, on an option
916
+ * contract of a plain option venue, its registry trade units as answered (`peekOptionTradeUnits`;
917
+ * the K_TRADES control starts the read with the acquisition), so the floor reads the premium the
918
+ * resolver prices. Until the read answers, the units price nothing and no print passes the floor. */
919
+ function largeFloorMarketOf(market) {
920
+ const { exchange, symbol } = market;
921
+ if (typeof exchange !== 'string' || typeof symbol !== 'string')
922
+ return market;
923
+ const optionUnits = peekOptionTradeUnits({ exchange, symbol });
924
+ return optionUnits ? { ...market, optionUnits } : market;
925
+ }
926
+ /** The large-print table's floor at the tape enqueue (the fold applies it again): on the large
927
+ * lane, a print under the table's floor (`tradesBelowLargeFloor`: a dollar-quoted market's notional,
928
+ * a CME Group root's contracts, an option book's premium; the frame's envelope names the market,
929
+ * else the chart's). */
930
+ function wrunTapePrintBelowLargeFloor(context, data) {
931
+ if (context.acquisition.tape?.large !== true)
932
+ return false;
933
+ const print = readLiveTradePrint(data);
934
+ return (print !== null && tradesBelowLargeFloor(print, context.market ? largeFloorMarketOf(context.market) : undefined));
935
+ }
936
+ /** Whether a reconcile can restore the tape prints the queue cap dropped: the large lane with a
937
+ * history to re-ask (not the live edge, history "0") on a market the re-ask answers
938
+ * (`tradesLargeHistoryAnswers`: the table on a dollar quote, the raw tape on a CME Group root the
939
+ * table skips; a context that names no normalized symbol is assumed held). */
940
+ function wrunTapeLostRestorable(context) {
941
+ const acquisition = context.acquisition.tape;
942
+ if (acquisition?.large !== true)
943
+ return false;
944
+ let historySec = 0;
945
+ try {
946
+ const declared = typeof acquisition.history === 'string' ? acquisition.history : undefined;
947
+ historySec = parseWrunTapeHistory(context.metadata, 'tape', wrunTapeHistoryOf({ history: declared }));
948
+ }
949
+ catch {
950
+ // The full run validated the history; a malformed record reads as the live edge.
951
+ }
952
+ return historySec > 0 && tradesLargeHistoryAnswers(context.market ?? {});
953
+ }
954
+ function pushAdditive(intake, lane, point) {
955
+ let queue = intake.additive[lane];
956
+ if (!queue) {
957
+ queue = { points: [], head: 0 };
958
+ intake.additive[lane] = queue;
959
+ }
960
+ const dropped = pushBoundedQueue(intake, queue, point);
961
+ // A tape print the cap dropped leaves its bar's block a print short: its bucket is owed a repair
962
+ // (the drain marks it for the reconcile, or the tick refuses it by name).
963
+ if (lane === 'tape' && dropped !== undefined && intake.intervalMs > 0) {
964
+ const print = readLiveTradePrint(dropped);
965
+ if (print !== null)
966
+ intake.tapeLost.add(tradesBarStartMs(print.tsMs, intake.intervalMs, print.exchange));
967
+ }
968
+ }
969
+ function pushLegPoint(intake, fetchKey, point) {
970
+ let queue = intake.legs.get(fetchKey);
971
+ if (!queue) {
972
+ queue = { points: [], head: 0 };
973
+ intake.legs.set(fetchKey, queue);
974
+ }
975
+ pushBoundedQueue(intake, queue, point);
976
+ }
977
+ function takeLegPoints(intake, fetchKey) {
978
+ const queue = intake.legs.get(fetchKey);
979
+ if (!queue)
980
+ return [];
981
+ const points = queue.head === 0 ? queue.points : queue.points.slice(queue.head);
982
+ intake.legs.delete(fetchKey);
983
+ return points;
984
+ }
985
+ function takeAdditive(intake, lane) {
986
+ const queue = intake.additive[lane];
987
+ if (!queue)
988
+ return [];
989
+ const points = queue.head === 0 ? queue.points : queue.points.slice(queue.head);
990
+ delete intake.additive[lane];
991
+ return points;
992
+ }
993
+ function pendingCount(intake) {
994
+ let count = intake.bookLive.size + intake.bookReplay.size + intake.funding.size;
995
+ for (const queue of Object.values(intake.additive)) {
996
+ if (queue)
997
+ count += queue.points.length - queue.head;
998
+ }
999
+ for (const queue of intake.legs.values())
1000
+ count += queue.points.length - queue.head;
1001
+ for (const queue of intake.rowLanes.values())
1002
+ count += queue.points.length - queue.head;
1003
+ return count;
1004
+ }
1005
+ /** Push one point onto a bounded, oldest-first queue (the additive cap and
1006
+ * its drop accounting), shared by the additive lanes and the leg queues.
1007
+ * Returns the point the cap dropped, if any. */
1008
+ function pushBoundedQueue(intake, queue, point) {
1009
+ queue.points.push(point);
1010
+ if (queue.points.length - queue.head <= WRUN_LIVE_ADDITIVE_QUEUE_CAP)
1011
+ return undefined;
1012
+ const dropped = queue.points[queue.head];
1013
+ queue.head += 1;
1014
+ intake.droppedSinceDrain += 1;
1015
+ intake.stats.dropped += 1;
1016
+ if (queue.head >= WRUN_LIVE_ADDITIVE_QUEUE_CAP) {
1017
+ queue.points = queue.points.slice(queue.head);
1018
+ queue.head = 0;
1019
+ }
1020
+ return dropped;
1021
+ }
1022
+ /** Whether an overlay's longest capped queue (an additive lane, a leg's candle frames) reached the
1023
+ * hidden hold's backlog bound: the live tick then runs one drain while hidden. */
1024
+ export function wrunLiveIntakeBacklogDue(indicatorId) {
1025
+ const intake = intakes.get(indicatorId);
1026
+ if (!intake)
1027
+ return false;
1028
+ for (const queue of Object.values(intake.additive)) {
1029
+ if (queue && queue.points.length - queue.head >= WRUN_LIVE_HIDDEN_BACKLOG_RUN_POINTS)
1030
+ return true;
1031
+ }
1032
+ for (const queue of [...intake.legs.values(), ...intake.rowLanes.values()]) {
1033
+ if (queue.points.length - queue.head >= WRUN_LIVE_HIDDEN_BACKLOG_RUN_POINTS)
1034
+ return true;
1035
+ }
1036
+ return false;
1037
+ }
1038
+ /** The market a pinned leg resolves to: the pinned parts over the chart's
1039
+ * own (the fetch placeholders resolve the same way), under the host chart's
1040
+ * session (the session its history was fetched under, the chart's rule for
1041
+ * a foreign equities source), so the live bucket keys the history's bars. */
1042
+ function legMarketOf(context, market) {
1043
+ const chart = chartMarketOf(context);
1044
+ const exchange = market?.exchange ?? chart?.exchange;
1045
+ const symbol = market?.symbol ?? chart?.symbol;
1046
+ if (typeof exchange !== 'string' || typeof symbol !== 'string')
1047
+ return null;
1048
+ const sessionType = context.market?.sessionType;
1049
+ return typeof sessionType === 'string' ? { exchange, symbol, sessionType } : { exchange, symbol };
1050
+ }
1051
+ /** The market of the pinned leg a fetch key names (the venue its candle
1052
+ * frames are bucketed by), or null for an unknown key. */
1053
+ function legMarketOfKey(context, fetchKey) {
1054
+ // A run-interval session's run bars are the chart market's own.
1055
+ if (fetchKey === WRUN_RUN_INTERVAL_ROWS_KEY)
1056
+ return chartMarketOf(context);
1057
+ const leg = context.pinnedLegs?.find((candidate) => candidate.fetchKey === fetchKey);
1058
+ return leg ? legMarketOf(context, leg.market) : null;
1059
+ }
1060
+ /** The grid legs whose market the frame's series identity matches (the
1061
+ * same alias-aware matcher the chart uses), by fetch key; empty for a
1062
+ * frame without a full identity or a context without grid legs. An odds
1063
+ * grid leg (`leg.odds`, the planner's contract for an `odds` input's leg)
1064
+ * is never a taker (poll-only): its market's frames stay foreign. */
1065
+ function gridLegKeysForFrame(context, frame) {
1066
+ if (!frame.incomingExchange || !frame.incomingSymbol)
1067
+ return [];
1068
+ const keys = [];
1069
+ for (const leg of context.pinnedLegs ?? []) {
1070
+ if (leg.kind !== 'grid' || leg.odds === true)
1071
+ continue;
1072
+ const market = legMarketOf(context, leg.market);
1073
+ if (market === null)
1074
+ continue;
1075
+ if (liveFeedSeriesMatchesChartStream({ exchange: frame.incomingExchange, symbol: frame.incomingSymbol }, market)) {
1076
+ keys.push(leg.fetchKey);
1077
+ }
1078
+ }
1079
+ return keys;
1080
+ }
1081
+ /**
1082
+ * Whether a frame of the given series identity has a taker on the overlay:
1083
+ * the chart's own market (the fan-out's rule), the market of a declared
1084
+ * grid leg (a pinned market's candle feed, subscribed for that leg) or a
1085
+ * trades leg's. The
1086
+ * live fan-out consults this before dropping a foreign-market frame; the
1087
+ * intake still decides the verdict per frame. A frame without a full
1088
+ * identity, or an overlay without a live context, answers true (the
1089
+ * fan-out already admitted it, the intake judges it).
1090
+ */
1091
+ export function wrunLiveFrameMarketAdmitted(indicatorId, series) {
1092
+ const context = getWrunOverlayLiveContext(indicatorId);
1093
+ if (!context || !series || typeof series.exchange !== 'string' || typeof series.symbol !== 'string')
1094
+ return true;
1095
+ const frame = {
1096
+ scriptSource: 'ohlcv',
1097
+ data: undefined,
1098
+ incomingExchange: series.exchange,
1099
+ incomingSymbol: series.symbol,
1100
+ };
1101
+ return (!isForeignFrame(context, frame) ||
1102
+ gridLegKeysForFrame(context, frame).length > 0 ||
1103
+ tradesLegLanesForFrame(context, frame).length > 0 ||
1104
+ // A row lane fed by coin takes every contract of its coin (a coin-scoped channel), foreign or not.
1105
+ rowLaneCoinTakers(context, typeof series.coin === 'string' ? series.coin : undefined).length > 0);
1106
+ }
1107
+ /** A point's coin as a row lane fed by coin reads it: the lane's own reader, else the frame's named coin, else
1108
+ * the point's `series.coin`; upper case, '' when none. */
1109
+ function wrunRowLanePointCoin(reader, frame) {
1110
+ const series = isRecord(frame.data) && isRecord(frame.data.series) ? frame.data.series : undefined;
1111
+ const coin = reader?.(frame.data) ?? frame.incomingCoin ?? (typeof series?.coin === 'string' ? series.coin : '');
1112
+ return typeof coin === 'string' ? coin.trim().toUpperCase() : '';
1113
+ }
1114
+ /** The coin a row-lane instance fed by coin admits: its acquisition's `coin` (the identity's underlying, the
1115
+ * chart's), upper case; '' when it names none (such an instance admits nothing: fail closed). */
1116
+ function wrunRowLaneInstanceCoin(context, instanceKey, extras) {
1117
+ const acquisition = context.acquisition[instanceKey] ?? extras;
1118
+ const coin = isRecord(acquisition) ? acquisition.coin : undefined;
1119
+ return typeof coin === 'string' ? coin.trim().toUpperCase() : '';
1120
+ }
1121
+ /** The row-lane instances fed by coin that take a point of `coin` (and of `scriptSource`, when named). */
1122
+ function rowLaneCoinTakers(context, coin, scriptSource) {
1123
+ const word = typeof coin === 'string' ? coin.trim().toUpperCase() : '';
1124
+ if (word === '')
1125
+ return [];
1126
+ const takers = [];
1127
+ for (const instance of context.rowLanes ?? []) {
1128
+ const live = wrunRowLaneByKey(instance.laneKey)?.liveByCoin;
1129
+ if (live === undefined || (scriptSource !== undefined && live.scriptSource !== scriptSource))
1130
+ continue;
1131
+ if (wrunRowLaneInstanceCoin(context, instance.key, instance.extras) === word)
1132
+ takers.push(instance.key);
1133
+ }
1134
+ return takers;
1135
+ }
1136
+ /** The row-lane instances fed by coin a frame feeds: its script source is the lane's, and its point's coin is the
1137
+ * coin the instance was asked for (the chart's underlying), whatever contract the point names. */
1138
+ function rowLaneIdsForFrame(context, frame) {
1139
+ const ids = [];
1140
+ for (const instance of context.rowLanes ?? []) {
1141
+ const live = wrunRowLaneByKey(instance.laneKey)?.liveByCoin;
1142
+ if (live === undefined || live.scriptSource !== frame.scriptSource)
1143
+ continue;
1144
+ const coin = wrunRowLanePointCoin(live.coinOf, frame);
1145
+ if (coin !== '' && coin === wrunRowLaneInstanceCoin(context, instance.key, instance.extras))
1146
+ ids.push(instance.key);
1147
+ }
1148
+ return ids;
1149
+ }
1150
+ /**
1151
+ * One pushed frame, O(1): after the fan-out's own gates (protected, replay,
1152
+ * budget-hidden) the intake checks the context, its generation, the frame's
1153
+ * market, the package's eligibility and the declared lanes, then queues the
1154
+ * point by lane rule (additive lanes keep every point up to the cap; book
1155
+ * and funding keep the newest per bar; a replay burst in its own slot). An
1156
+ * `ohlcv` frame carries no lane data (the main rows live on the chart) but
1157
+ * is 'queued' as the drain trigger it is, eligibility aside; for a session
1158
+ * at another interval the chart market's frame also queues for its run bars.
1159
+ */
1160
+ export function enqueueWrunLiveFrame(a) {
1161
+ const context = getWrunOverlayLiveContext(a.indicatorId);
1162
+ if (!context || context.chartIndex !== a.chartIndex)
1163
+ return noteVerdict(a.indicatorId, 'ignored:no-context');
1164
+ if (a.generation !== context.generation)
1165
+ return noteVerdict(a.indicatorId, 'ignored:generation', context);
1166
+ // A row lane fed by coin: a point of the instance's coin (the chart's underlying) feeds it, whatever contract it
1167
+ // names and whatever market the chart is (the coin-scoped channel's points are never the chart's own market).
1168
+ const rowLaneIds = rowLaneIdsForFrame(context, a.frame);
1169
+ if (rowLaneIds.length > 0) {
1170
+ if (!context.pushEligible)
1171
+ return noteVerdict(a.indicatorId, 'ignored:ineligible', context);
1172
+ const intake = intakeFor(a.indicatorId, context.chartIndex, context.intervalMs, context.generation);
1173
+ bindQueuesToGeneration(intake, context.generation);
1174
+ for (const id of rowLaneIds) {
1175
+ let queue = intake.rowLanes.get(id);
1176
+ if (queue === undefined) {
1177
+ queue = { points: [], head: 0 };
1178
+ intake.rowLanes.set(id, queue);
1179
+ }
1180
+ pushBoundedQueue(intake, queue, a.frame.data);
1181
+ intake.stats.frames[id] = (intake.stats.frames[id] ?? 0) + 1;
1182
+ }
1183
+ return 'queued';
1184
+ }
1185
+ const lane = WRUN_LIVE_LANE_OF_SCRIPT_SOURCE[a.frame.scriptSource];
1186
+ const foreign = isForeignFrame(context, a.frame);
1187
+ // A candle frame of a declared grid leg's market is that leg's (a pinned
1188
+ // market's feed, subscribed for it): admitted by the leg, never by the
1189
+ // chart. Every other foreign frame is refused.
1190
+ const legKeys = lane === 'ohlcv' ? gridLegKeysForFrame(context, a.frame) : [];
1191
+ // A buy/sell frame of a trades leg's market feeds that leg, the chart's own market or not.
1192
+ const tradeLegs = lane === 'trades' ? tradesLegLanesForFrame(context, a.frame) : [];
1193
+ if (foreign && legKeys.length === 0 && tradeLegs.length === 0) {
1194
+ return noteVerdict(a.indicatorId, 'ignored:foreign', context);
1195
+ }
1196
+ if (lane === undefined)
1197
+ return noteVerdict(a.indicatorId, 'ignored:undeclared', context);
1198
+ const intake = intakeFor(a.indicatorId, context.chartIndex, context.intervalMs, context.generation);
1199
+ bindQueuesToGeneration(intake, context.generation);
1200
+ if (lane === 'ohlcv') {
1201
+ if (legKeys.length > 0) {
1202
+ if (!context.pushEligible)
1203
+ return noteVerdict(a.indicatorId, 'ignored:ineligible', context);
1204
+ for (const key of legKeys) {
1205
+ pushLegPoint(intake, key, a.frame.data);
1206
+ intake.stats.legFrames[key] = (intake.stats.legFrames[key] ?? 0) + 1;
1207
+ }
1208
+ }
1209
+ if (!foreign) {
1210
+ intake.stats.frames.ohlcv = (intake.stats.frames.ohlcv ?? 0) + 1;
1211
+ // A session at another interval: the chart's candle frame extends its forming run bar.
1212
+ if (context.pushEligible && wrunRunIntervalSessionOf(context) !== undefined) {
1213
+ pushLegPoint(intake, WRUN_RUN_INTERVAL_ROWS_KEY, a.frame.data);
1214
+ }
1215
+ }
1216
+ return 'queued';
1217
+ }
1218
+ if (!context.pushEligible)
1219
+ return noteVerdict(a.indicatorId, 'ignored:ineligible', context);
1220
+ const fed = WRUN_PUSH_LANES.has(lane)
1221
+ ? [...(foreign ? [] : frameLanesOf(context, lane)), ...tradeLegs]
1222
+ : [];
1223
+ if (fed.length === 0)
1224
+ return noteVerdict(a.indicatorId, 'ignored:undeclared', context);
1225
+ const intervalMs = context.intervalMs;
1226
+ if (lane === 'book') {
1227
+ const data = a.frame.data;
1228
+ if (a.frame.bucketed) {
1229
+ const rows = Array.isArray(data) ? data : [data];
1230
+ for (const row of rows) {
1231
+ const bucket = Number(row?.[0]);
1232
+ if (Number.isFinite(bucket))
1233
+ intake.bookLive.set(bucket, row);
1234
+ }
1235
+ }
1236
+ else {
1237
+ const replay = isRecord(data) && isRecord(data.series) && data.series.isReplay === true;
1238
+ const slot = replay ? intake.bookReplay : intake.bookLive;
1239
+ for (const row of wrunLiveBookRows(data, intervalMs))
1240
+ slot.set(Number(row[0]), row);
1241
+ }
1242
+ }
1243
+ else if (lane === 'funding') {
1244
+ const parsed = wrunLiveFundingValue(a.frame.data);
1245
+ const bucket = parsed ? wrunLiveBucketTs('funding', parsed.tsMs, intervalMs) : null;
1246
+ if (bucket !== null)
1247
+ intake.funding.set(bucket, a.frame.data);
1248
+ }
1249
+ else if (lane === 'tape') {
1250
+ // Every raw print that reaches the lane is a tape frame, the ones the floors turn away included:
1251
+ // the count is the live leg's own signal, and a floor that turns most of a busy tape away (a 1
1252
+ // BTC floor on a perp) must never read as a dead feed. `ignored:below-floor` counts the turned
1253
+ // away ones, `queued` what remains.
1254
+ intake.stats.frames.tape = (intake.stats.frames.tape ?? 0) + 1;
1255
+ // The kScript fan-out's floor: a raw print below the acquisition's minSize can never fold, so it
1256
+ // never takes a slot a qualifying print needs in the capped queue (the fold applies it again).
1257
+ // The large-print table's own floor too: a print under it (a dollar-quoted market's notional, a
1258
+ // CME Group root's contracts) would fold live and vanish when the next reconcile answers the
1259
+ // bucket from the table.
1260
+ const floor = Number(context.acquisition.tape?.minSize);
1261
+ if (v2LiveTradePrintBelowFloor(a.frame.data, Number.isFinite(floor) ? floor : 0) ||
1262
+ wrunTapePrintBelowLargeFloor(context, a.frame.data)) {
1263
+ return noteVerdict(a.indicatorId, 'ignored:below-floor', context);
1264
+ }
1265
+ pushAdditive(intake, 'tape', a.frame.data);
1266
+ return 'queued';
1267
+ }
1268
+ else {
1269
+ for (const additive of fed)
1270
+ pushAdditive(intake, additive, a.frame.data);
1271
+ }
1272
+ for (const counted of fed)
1273
+ intake.stats.frames[counted] = (intake.stats.frames[counted] ?? 0) + 1;
1274
+ return 'queued';
1275
+ }
1276
+ function laneRowsOf(context, lane) {
1277
+ switch (lane) {
1278
+ case 'book':
1279
+ return context.bookRows;
1280
+ case 'trades':
1281
+ return context.buySellVolumeRows;
1282
+ case 'trades_usd':
1283
+ return context.buySellVolumeUsdRows;
1284
+ case 'funding':
1285
+ return context.fundingRateRows;
1286
+ case 'liquidations':
1287
+ return context.liquidationRows;
1288
+ case 'volume_profile':
1289
+ return context.volumeProfileRows;
1290
+ case 'trade_volume_by_size':
1291
+ return context.tradeVolumeBySizeRows;
1292
+ case 'oi':
1293
+ return context.openInterestRows;
1294
+ case 'implied_volatility':
1295
+ return context.impliedVolatilityRows;
1296
+ case 'skew':
1297
+ return context.skewRows;
1298
+ case 'etf_flow':
1299
+ return context.etfFlowRows;
1300
+ case 'tape':
1301
+ return context.tapeRows;
1302
+ case 'options_chain':
1303
+ return undefined;
1304
+ default:
1305
+ // A trades leg lane or a registry poll lane instance: its one record.
1306
+ return isWrunTradesLegLane(lane) ? context.tradeLegRows?.[lane] : context.pollLaneRows?.[lane];
1307
+ }
1308
+ }
1309
+ function setLaneRows(context, lane, rows) {
1310
+ switch (lane) {
1311
+ case 'book':
1312
+ context.bookRows = rows;
1313
+ return;
1314
+ case 'trades':
1315
+ context.buySellVolumeRows = rows;
1316
+ return;
1317
+ case 'trades_usd':
1318
+ context.buySellVolumeUsdRows = rows;
1319
+ return;
1320
+ case 'funding':
1321
+ context.fundingRateRows = rows;
1322
+ return;
1323
+ case 'liquidations':
1324
+ context.liquidationRows = rows;
1325
+ return;
1326
+ case 'volume_profile':
1327
+ context.volumeProfileRows = rows;
1328
+ return;
1329
+ case 'trade_volume_by_size':
1330
+ context.tradeVolumeBySizeRows = rows;
1331
+ return;
1332
+ case 'oi':
1333
+ context.openInterestRows = rows;
1334
+ return;
1335
+ case 'implied_volatility':
1336
+ context.impliedVolatilityRows = rows;
1337
+ return;
1338
+ case 'skew':
1339
+ context.skewRows = rows;
1340
+ return;
1341
+ case 'etf_flow':
1342
+ context.etfFlowRows = rows;
1343
+ return;
1344
+ case 'tape':
1345
+ context.tapeRows = rows;
1346
+ return;
1347
+ default:
1348
+ if (isWrunTradesLegLane(lane))
1349
+ context.tradeLegRows = { ...context.tradeLegRows, [lane]: rows };
1350
+ return;
1351
+ }
1352
+ }
1353
+ /**
1354
+ * Fold everything queued for the context's overlay into its lane buffers
1355
+ * (NEW arrays, the context's lane fields replaced), mark the fed buckets,
1356
+ * and report what moved. `dirtyFromTs` is the earliest touched bucket older
1357
+ * than `context.lastRowTs` (a late point for a committed bar; also stamped
1358
+ * on the context so the poll cadence reseeds), else null.
1359
+ */
1360
+ export function drainWrunLiveIntake(ctx) {
1361
+ const result = {
1362
+ touched: {},
1363
+ points: {},
1364
+ legs: {},
1365
+ dropped: 0,
1366
+ dirtyFromTs: null,
1367
+ };
1368
+ const intake = intakes.get(ctx.indicatorId);
1369
+ if (!intake)
1370
+ return result;
1371
+ intake.chartIndex = ctx.chartIndex;
1372
+ intake.intervalMs = ctx.intervalMs;
1373
+ // Points queued under a superseded context never fold into the fresh
1374
+ // buffers (their increments are in the new full run's fetch already).
1375
+ bindQueuesToGeneration(intake, ctx.generation);
1376
+ result.dropped = intake.droppedSinceDrain;
1377
+ intake.droppedSinceDrain = 0;
1378
+ intake.stats.drains += 1;
1379
+ const batches = [];
1380
+ for (const lane of ['trades', 'trades_usd', 'liquidations', 'volume_profile', 'tape']) {
1381
+ const points = takeAdditive(intake, lane);
1382
+ if (points.length > 0)
1383
+ batches.push([lane, points]);
1384
+ }
1385
+ for (const lane of Object.keys(intake.additive).filter(isWrunTradesLegLane)) {
1386
+ const points = takeAdditive(intake, lane);
1387
+ if (points.length > 0)
1388
+ batches.push([lane, points]);
1389
+ }
1390
+ if (intake.bookReplay.size > 0 || intake.bookLive.size > 0) {
1391
+ // Replay rows first, so a live snapshot of the same bucket wins.
1392
+ batches.push(['book', [...intake.bookReplay.values(), ...intake.bookLive.values()]]);
1393
+ intake.bookReplay = new Map();
1394
+ intake.bookLive = new Map();
1395
+ }
1396
+ if (intake.funding.size > 0) {
1397
+ batches.push(['funding', [...intake.funding.values()]]);
1398
+ intake.funding = new Map();
1399
+ }
1400
+ let dirtyFromTs = null;
1401
+ // Every lane fetched the chart's own market; a grid leg its own.
1402
+ const chartMarket = chartMarketOf(ctx);
1403
+ for (const [lane, points] of batches) {
1404
+ // A trades leg folds on its own grid at its own tier (the interval its history was asked at), as its history
1405
+ // rows sit; every other lane at the chart's interval.
1406
+ const foldIntervalMs = laneFoldIntervalMs(ctx, lane, ctx.intervalMs);
1407
+ const folded = foldWrunLaneRows(lane, laneRowsOf(ctx, lane), points, foldIntervalMs, ctx.acquisition[lane] ?? {}, isWrunTradesLegLane(lane) ? tradesLegBucketMarket(ctx, lane) : chartMarket);
1408
+ result.points[lane] = points.length;
1409
+ if (!folded)
1410
+ continue;
1411
+ setLaneRows(ctx, lane, folded.rows);
1412
+ result.touched[lane] = folded.touched;
1413
+ const fed = (ctx.fedBuckets[lane] ??= new Set());
1414
+ // A trades leg's bucket revises the chart bar its row lands in (wrunLegRowHostBar), as a grid leg's does.
1415
+ const barOf = isWrunTradesLegLane(lane)
1416
+ ? hostBucketResolverFor(ctx.intervalMs, liveBucketVenueOf(ctx.market))
1417
+ : undefined;
1418
+ for (const bucket of folded.touched) {
1419
+ fed.add(bucket);
1420
+ const bar = barOf === undefined ? bucket : wrunLegRowHostBar(bucket, foldIntervalMs, barOf);
1421
+ if (bar !== null && bar < ctx.lastRowTs && (dirtyFromTs === null || bar < dirtyFromTs))
1422
+ dirtyFromTs = bar;
1423
+ }
1424
+ if (folded.capped !== undefined) {
1425
+ // A truncated tape row is durable on the context: the next tick refuses the bucket by name
1426
+ // (and suspends the session) instead of computing on it.
1427
+ const cappedBuckets = (ctx.tapeCappedBuckets ??= new Set());
1428
+ for (const bucket of folded.capped)
1429
+ cappedBuckets.add(bucket);
1430
+ }
1431
+ }
1432
+ if (intake.tapeLost.size > 0) {
1433
+ // Tape prints the queue cap dropped: their bars' blocks are short. When the large-print table
1434
+ // holds what the live floors keep, the buckets stay fed (the reconcile window reaches back to
1435
+ // them) and `tape-lost` re-asks them before this drain computes: the answer's union restores
1436
+ // the prints. A live-edge tape (history "0") or a market the table never holds has nothing to
1437
+ // restore them from: the tick refuses the bucket by name instead (`tapeLostBuckets`).
1438
+ const lost = [...intake.tapeLost].sort((x, y) => x - y);
1439
+ intake.tapeLost = new Set();
1440
+ result.tapeLost = lost;
1441
+ if (wrunTapeLostRestorable(ctx)) {
1442
+ const fed = (ctx.fedBuckets.tape ??= new Set());
1443
+ for (const bucket of lost)
1444
+ fed.add(bucket);
1445
+ if (!intake.gaps.includes('tape-lost'))
1446
+ intake.gaps.push('tape-lost');
1447
+ }
1448
+ else {
1449
+ const short = (ctx.tapeLostBuckets ??= new Set());
1450
+ for (const bucket of lost)
1451
+ short.add(bucket);
1452
+ }
1453
+ }
1454
+ // The grid legs: each leg's candle frames fold into its buffer (a NEW
1455
+ // array under the same key, the context's record replaced), a late bucket
1456
+ // owes the same reseed a late lane point does.
1457
+ const legKeys = [...intake.legs.keys()];
1458
+ for (const key of legKeys) {
1459
+ const points = takeLegPoints(intake, key);
1460
+ if (points.length === 0)
1461
+ continue;
1462
+ if (key === WRUN_RUN_INTERVAL_ROWS_KEY) {
1463
+ // The chart's candle frames into a session's run bars (the forming one,
1464
+ // or a closed one a late frame reaches: a reseed, like a late point).
1465
+ const touched = foldWrunRunIntervalFrames(ctx, points);
1466
+ if (touched === null)
1467
+ continue;
1468
+ result.runRows = touched;
1469
+ for (const bucket of touched) {
1470
+ if (bucket < ctx.lastRowTs && (dirtyFromTs === null || bucket < dirtyFromTs))
1471
+ dirtyFromTs = bucket;
1472
+ }
1473
+ continue;
1474
+ }
1475
+ // A grid leg asked at a tier (`tierMs`) folds its frames at it, as its history rows sit.
1476
+ const legRecord = ctx.pinnedLegs?.find((leg) => leg.fetchKey === key);
1477
+ const foldMs = legRecord?.tierMs ?? ctx.intervalMs;
1478
+ const folded = foldWrunLegCandleRows(ctx.pinnedCandleRows?.[key] ?? [], points, foldMs, legMarketOfKey(ctx, key));
1479
+ if (!folded)
1480
+ continue;
1481
+ ctx.pinnedCandleRows = { ...(ctx.pinnedCandleRows ?? {}), [key]: folded.rows };
1482
+ result.legs[key] = folded.touched;
1483
+ // The buffer keys the leg's own bars; a touched row revises the chart bar it lands in (the bar holding its
1484
+ // last instant, for a leg off the chart's grid: wrunLegRowHostBar), so a late bucket owes a reseed only
1485
+ // when that bar is committed, from that bar. A row no chart bar holds (off the chart's session) revises none.
1486
+ const legMs = legRecord?.tierMs ?? legRecord?.legMs ?? ctx.intervalMs;
1487
+ const barOf = hostBucketResolverFor(ctx.intervalMs, liveBucketVenueOf(ctx.market));
1488
+ for (const bucket of folded.touched) {
1489
+ const bar = wrunLegRowHostBar(bucket, legMs, barOf);
1490
+ if (bar !== null && bar < ctx.lastRowTs && (dirtyFromTs === null || bar < dirtyFromTs))
1491
+ dirtyFromTs = bar;
1492
+ }
1493
+ }
1494
+ // The row lanes fed by coin: each instance's points fold by its lane's own rule into its host-bar rows (a NEW
1495
+ // array under the same id), every touched bar fed (the reconcile settles it) and a late one owing a reseed.
1496
+ for (const [id, queue] of [...intake.rowLanes.entries()]) {
1497
+ intake.rowLanes.delete(id);
1498
+ const points = queue.head === 0 ? queue.points : queue.points.slice(queue.head);
1499
+ const instance = ctx.rowLanes?.find((candidate) => candidate.key === id);
1500
+ const live = instance === undefined ? undefined : wrunRowLaneByKey(instance.laneKey)?.liveByCoin;
1501
+ if (instance === undefined || live === undefined || points.length === 0)
1502
+ continue;
1503
+ const barGrid = liveBucketVenueOf(ctx.market);
1504
+ const lane = id;
1505
+ ctx.rowLaneRows = {
1506
+ ...ctx.rowLaneRows,
1507
+ [id]: live.fold(ctx.rowLaneRows?.[id] ?? [], points, {
1508
+ intervalMs: ctx.intervalMs,
1509
+ barGrid,
1510
+ acquisition: ctx.acquisition[lane] ?? instance.extras,
1511
+ }),
1512
+ };
1513
+ result.points[lane] = points.length;
1514
+ const touched = new Set();
1515
+ for (const point of points) {
1516
+ const stamp = live.stampOf(point);
1517
+ const bar = stamp === null ? null : wrunHostBarOf(stamp, ctx.intervalMs, barGrid);
1518
+ if (bar !== null)
1519
+ touched.add(bar);
1520
+ }
1521
+ result.touched[lane] = [...touched].sort((x, y) => x - y);
1522
+ const fed = (ctx.fedBuckets[lane] ??= new Set());
1523
+ for (const bucket of touched) {
1524
+ fed.add(bucket);
1525
+ if (bucket < ctx.lastRowTs && (dirtyFromTs === null || bucket < dirtyFromTs))
1526
+ dirtyFromTs = bucket;
1527
+ }
1528
+ }
1529
+ result.dirtyFromTs = dirtyFromTs;
1530
+ if (dirtyFromTs !== null && (ctx.dirtyFromTs === null || dirtyFromTs < ctx.dirtyFromTs)) {
1531
+ ctx.dirtyFromTs = dirtyFromTs;
1532
+ }
1533
+ return result;
1534
+ }
1535
+ /** A candle frame's newest trade (ms): the aggregation's last trade, else its
1536
+ * window stamp (a TICKER quote's own); null when it names none. */
1537
+ function wrunLiveTradeStampMs(point) {
1538
+ if (!isRecord(point))
1539
+ return null;
1540
+ const aggregation = isRecord(point.tradeAggregation)
1541
+ ? point.tradeAggregation
1542
+ : isRecord(point.ticker)
1543
+ ? point.ticker
1544
+ : undefined;
1545
+ if (aggregation === undefined)
1546
+ return null;
1547
+ const stamp = isRecord(aggregation.lastTimestamp) ? aggregation.lastTimestamp : aggregation.timestamp;
1548
+ if (!isRecord(stamp))
1549
+ return null;
1550
+ const seconds = Number(stamp.seconds);
1551
+ if (!Number.isFinite(seconds))
1552
+ return null;
1553
+ const nanos = Number(stamp.nanoseconds ?? 0);
1554
+ return seconds * 1000 + (Number.isFinite(nanos) ? nanos / 1e6 : 0);
1555
+ }
1556
+ function isReplayedFrame(point) {
1557
+ return isRecord(point) && (point.isReplay === true || (isRecord(point.series) && point.series.isReplay === true));
1558
+ }
1559
+ /** Whether a candle frame still owes its trades to a session's run bars: a
1560
+ * live frame always does (each carries the trades since the previous one);
1561
+ * a replayed frame (a resubscribe re-sends recent aggregations) only past
1562
+ * the newest trade the fold already took, the chart's own replay rule. */
1563
+ function admitWrunRunIntervalFrame(session, point) {
1564
+ const stamp = wrunLiveTradeStampMs(point);
1565
+ if (stamp === null)
1566
+ return true;
1567
+ const newest = session.newestTradeStampMs;
1568
+ if (isReplayedFrame(point) && newest !== undefined && stamp <= newest)
1569
+ return false;
1570
+ if (newest === undefined || stamp > newest)
1571
+ session.newestTradeStampMs = stamp;
1572
+ return true;
1573
+ }
1574
+ /**
1575
+ * Fold the chart market's candle frames into a session's run bars (the grid
1576
+ * legs' candle fold, K_OHLCV.mergeLiveDataPoint per frame, bucketed on the
1577
+ * run grid of the chart's market): the first frame of a bucket opens the run
1578
+ * bar, later ones widen it, replace its close and add their volume. The
1579
+ * session's buffer is replaced (never mutated). Returns the touched buckets,
1580
+ * null when nothing folded or the context no longer runs at another interval.
1581
+ */
1582
+ function foldWrunRunIntervalFrames(ctx, points) {
1583
+ const session = wrunRunIntervalSessionOf(ctx);
1584
+ if (session === undefined)
1585
+ return null;
1586
+ const admitted = points.filter((point) => admitWrunRunIntervalFrame(session, point));
1587
+ const folded = foldWrunLegCandleRows(session.rows, admitted, ctx.intervalMs, chartMarketOf(ctx));
1588
+ if (!folded)
1589
+ return null;
1590
+ session.rows = folded.rows;
1591
+ return folded.touched;
1592
+ }
1593
+ /** Drop a grid leg's queued candle frames of the given buckets (a reconcile
1594
+ * answer covers them: fetched rows win, a re-fold would add their volume
1595
+ * twice). Returns the number of points dropped. */
1596
+ export function dropWrunLiveQueuedLegBuckets(indicatorId, fetchKey, buckets) {
1597
+ const intake = intakes.get(indicatorId);
1598
+ const queue = intake?.legs.get(fetchKey);
1599
+ if (!intake || !queue || buckets.length === 0)
1600
+ return 0;
1601
+ const drop = new Set(buckets);
1602
+ // Bucketed exactly as the drain folds them (the leg's own market, at its tier when it rides one).
1603
+ const context = getWrunOverlayLiveContext(indicatorId);
1604
+ const market = context ? legMarketOfKey(context, fetchKey) : null;
1605
+ const foldMs = context?.pinnedLegs?.find((leg) => leg.fetchKey === fetchKey)?.tierMs ?? intake.intervalMs;
1606
+ let dropped = 0;
1607
+ const kept = [];
1608
+ for (let at = queue.head; at < queue.points.length; at += 1) {
1609
+ const point = queue.points[at];
1610
+ const parsed = wrunLiveOhlcvPoint(point);
1611
+ const bucket = parsed === null ? null : wrunLiveBucketTs('ohlcv', parsed.tsMs, foldMs, market);
1612
+ if (bucket !== null && drop.has(bucket))
1613
+ dropped += 1;
1614
+ else
1615
+ kept.push(point);
1616
+ }
1617
+ if (kept.length === 0)
1618
+ intake.legs.delete(fetchKey);
1619
+ else
1620
+ intake.legs.set(fetchKey, { points: kept, head: 0 });
1621
+ return dropped;
1622
+ }
1623
+ /** Drop the queued points of the given buckets (a reconcile answer covers
1624
+ * them: fetched rows win). Returns the number of points dropped. Book and
1625
+ * funding drop by bucket key; additive points are bucketed on the fly. */
1626
+ export function dropWrunLiveQueuedBuckets(indicatorId, lane, buckets) {
1627
+ const intake = intakes.get(indicatorId);
1628
+ if (!intake || buckets.length === 0)
1629
+ return 0;
1630
+ const drop = new Set(buckets);
1631
+ let dropped = 0;
1632
+ if (lane === 'book') {
1633
+ for (const bucket of drop) {
1634
+ if (intake.bookLive.delete(bucket))
1635
+ dropped += 1;
1636
+ if (intake.bookReplay.delete(bucket))
1637
+ dropped += 1;
1638
+ }
1639
+ return dropped;
1640
+ }
1641
+ if (lane === 'funding') {
1642
+ for (const bucket of drop)
1643
+ if (intake.funding.delete(bucket))
1644
+ dropped += 1;
1645
+ return dropped;
1646
+ }
1647
+ const rowQueue = intake.rowLanes.get(lane);
1648
+ if (rowQueue !== undefined) {
1649
+ // A row lane fed by coin: its points bucketed on the chart's bar grid by the lane's own stamp, as the drain
1650
+ // folds them.
1651
+ const context = getWrunOverlayLiveContext(indicatorId);
1652
+ const instance = context?.rowLanes?.find((candidate) => candidate.key === lane);
1653
+ const live = instance === undefined ? undefined : wrunRowLaneByKey(instance.laneKey)?.liveByCoin;
1654
+ if (context === undefined || live === undefined)
1655
+ return 0;
1656
+ const barGrid = liveBucketVenueOf(context.market);
1657
+ const kept = [];
1658
+ for (let at = rowQueue.head; at < rowQueue.points.length; at += 1) {
1659
+ const point = rowQueue.points[at];
1660
+ const stamp = live.stampOf(point);
1661
+ const bucket = stamp === null ? null : wrunHostBarOf(stamp, intake.intervalMs, barGrid);
1662
+ if (bucket !== null && drop.has(bucket))
1663
+ dropped += 1;
1664
+ else
1665
+ kept.push(point);
1666
+ }
1667
+ if (kept.length === 0)
1668
+ intake.rowLanes.delete(lane);
1669
+ else
1670
+ intake.rowLanes.set(lane, { points: kept, head: 0 });
1671
+ return dropped;
1672
+ }
1673
+ const leg = isWrunTradesLegLane(lane);
1674
+ if (!leg &&
1675
+ lane !== 'trades' &&
1676
+ lane !== 'trades_usd' &&
1677
+ lane !== 'liquidations' &&
1678
+ lane !== 'volume_profile' &&
1679
+ lane !== 'tape') {
1680
+ return 0;
1681
+ }
1682
+ const queue = intake.additive[lane];
1683
+ if (!queue)
1684
+ return 0;
1685
+ // Bucketed exactly as the drain folds them (the chart's own market, a trades leg its own at its own tier).
1686
+ const context = getWrunOverlayLiveContext(indicatorId);
1687
+ const market = context ? (leg ? tradesLegBucketMarket(context, lane) : chartMarketOf(context)) : null;
1688
+ const bucketLane = leg ? wrunTradesLegOf(lane).lane : lane;
1689
+ const bucketIntervalMs = context ? laneFoldIntervalMs(context, lane, intake.intervalMs) : intake.intervalMs;
1690
+ const bucketOf = (point) => {
1691
+ if (lane === 'tape') {
1692
+ // A raw print's stamp and venue as the tape fold reads them (seconds plus nanoseconds).
1693
+ const print = readLiveTradePrint(point);
1694
+ return print === null ? null : tradesBarStartMs(print.tsMs, intake.intervalMs, print.exchange);
1695
+ }
1696
+ if (!isRecord(point))
1697
+ return null;
1698
+ const holder = bucketLane === 'trades' || bucketLane === 'trades_usd'
1699
+ ? point.tradeAggregation
1700
+ : bucketLane === 'liquidations'
1701
+ ? point.liquidation
1702
+ : point.volumeProfileAggregation;
1703
+ const tsMs = isRecord(holder) ? wireTsMs(holder.timestamp) : null;
1704
+ return tsMs === null ? null : wrunLiveBucketTs(bucketLane, tsMs, bucketIntervalMs, market);
1705
+ };
1706
+ const kept = [];
1707
+ for (let at = queue.head; at < queue.points.length; at += 1) {
1708
+ const point = queue.points[at];
1709
+ const bucket = bucketOf(point);
1710
+ if (bucket !== null && drop.has(bucket))
1711
+ dropped += 1;
1712
+ else
1713
+ kept.push(point);
1714
+ }
1715
+ if (kept.length === 0)
1716
+ delete intake.additive[lane];
1717
+ else
1718
+ intake.additive[lane] = { points: kept, head: 0 };
1719
+ return dropped;
1720
+ }
1721
+ /**
1722
+ * Authority of a reconcile answer over one bucket inside the answered
1723
+ * window (plan section 8 fold 5): 'served' when the answer carries the
1724
+ * bucket; 'absent' (authoritative absence, the fed row is dropped) only when
1725
+ * the bucket is inside the window, the answer is non-empty and its last row
1726
+ * is at or past the bucket; otherwise 'unserved' (the fed row is kept and
1727
+ * the bucket stays outstanding).
1728
+ */
1729
+ export function wrunLaneAnswerCoversBucket(answerRows, windowFromTs, windowToTs, bucketTs) {
1730
+ let lastTs = Number.NEGATIVE_INFINITY;
1731
+ for (const row of answerRows) {
1732
+ const ts = Number(row[0]);
1733
+ if (!Number.isFinite(ts))
1734
+ continue;
1735
+ if (ts === bucketTs)
1736
+ return 'served';
1737
+ if (ts > lastTs)
1738
+ lastTs = ts;
1739
+ }
1740
+ if (bucketTs < windowFromTs || bucketTs >= windowToTs)
1741
+ return 'unserved';
1742
+ if (answerRows.length === 0 || lastTs < bucketTs)
1743
+ return 'unserved';
1744
+ return 'absent';
1745
+ }
1746
+ // ---------------------------------------------------------------------------
1747
+ // Gap marks
1748
+ // ---------------------------------------------------------------------------
1749
+ /** Record a reconcile reason for the overlay AND request a drain through
1750
+ * the registered requester (without one, the mark is only recorded). A
1751
+ * reason already pending is not recorded twice. An id without a live
1752
+ * context has no session to reconcile: no record, no drain. */
1753
+ export function markWrunLiveGap(indicatorId, reason) {
1754
+ const context = getWrunOverlayLiveContext(indicatorId);
1755
+ if (!context)
1756
+ return;
1757
+ const intake = intakeFor(indicatorId, context.chartIndex, context.intervalMs, context.generation);
1758
+ if (!intake.gaps.includes(reason))
1759
+ intake.gaps.push(reason);
1760
+ drainRequester?.(indicatorId);
1761
+ }
1762
+ /** The pending reasons, in mark order, cleared on take. */
1763
+ export function takeWrunLiveGaps(indicatorId) {
1764
+ const intake = intakes.get(indicatorId);
1765
+ if (!intake || intake.gaps.length === 0)
1766
+ return [];
1767
+ const gaps = intake.gaps;
1768
+ intake.gaps = [];
1769
+ return gaps;
1770
+ }
1771
+ /** Queues, gaps and stats of one overlay: registered on the drivers'
1772
+ * teardown hook at import time. */
1773
+ export function disposeWrunLiveIntake(indicatorId) {
1774
+ intakes.delete(indicatorId);
1775
+ }
1776
+ /**
1777
+ * The tick lane's accounting into the overlay's stats: computes carry the
1778
+ * ABI host's live field (restores and snapshots are read from it, never
1779
+ * inferred), reconciles carry their fetch count and window, and the
1780
+ * verdicts the tick decides itself ('ignored:replaying') land here too.
1781
+ */
1782
+ export function noteWrunLiveIntakeEvent(indicatorId, event) {
1783
+ const context = getWrunOverlayLiveContext(indicatorId);
1784
+ const intake = intakeFor(indicatorId, context?.chartIndex ?? -1, context?.intervalMs ?? 0, context?.generation ?? 0);
1785
+ const stats = intake.stats;
1786
+ switch (event.kind) {
1787
+ case 'compute':
1788
+ stats.computes += 1;
1789
+ stats.blockRows.push(event.blockRows);
1790
+ if (stats.blockRows.length > BLOCK_ROWS_KEPT)
1791
+ stats.blockRows.splice(0, stats.blockRows.length - BLOCK_ROWS_KEPT);
1792
+ if (event.live) {
1793
+ stats.restores += event.live.restored;
1794
+ stats.snapshots += event.live.snapshotted;
1795
+ stats.lastEvalMs = event.live.evalMs;
1796
+ }
1797
+ if (event.mainMs !== undefined)
1798
+ stats.lastMainMs = event.mainMs;
1799
+ return;
1800
+ case 'reconcile':
1801
+ for (const reason of event.reasons)
1802
+ stats.repairs[reason] = (stats.repairs[reason] ?? 0) + 1;
1803
+ stats.fetches += event.fetches;
1804
+ stats.reconcileWindows.push({ fromTs: event.fromTs, toTs: event.toTs, lanes: [...event.lanes] });
1805
+ if (stats.reconcileWindows.length > RECONCILE_WINDOWS_KEPT) {
1806
+ stats.reconcileWindows.splice(0, stats.reconcileWindows.length - RECONCILE_WINDOWS_KEPT);
1807
+ }
1808
+ return;
1809
+ case 'reseed':
1810
+ stats.reseeds += 1;
1811
+ return;
1812
+ case 'session-start':
1813
+ if (event.live) {
1814
+ stats.restores += event.live.restored;
1815
+ stats.snapshots += event.live.snapshotted;
1816
+ stats.lastEvalMs = event.live.evalMs;
1817
+ }
1818
+ return;
1819
+ case 'rebuild':
1820
+ stats.rebuilds += 1;
1821
+ return;
1822
+ case 'bypass-token':
1823
+ stats.bypassTokensHeld = Math.max(0, stats.bypassTokensHeld + event.delta);
1824
+ return;
1825
+ case 'ignored':
1826
+ stats.ignored[event.verdict] = (stats.ignored[event.verdict] ?? 0) + 1;
1827
+ return;
1828
+ default:
1829
+ return;
1830
+ }
1831
+ }
1832
+ // ---------------------------------------------------------------------------
1833
+ // Eligibility and stats
1834
+ // ---------------------------------------------------------------------------
1835
+ /** An interval word's span: the pin vocabulary (the schema enum or the
1836
+ * short spelling), else the chart's own spelling. */
1837
+ function intervalMsOf(word) {
1838
+ const pinned = wrunPinnedIntervalOf(word);
1839
+ if (pinned !== undefined)
1840
+ return pinned.ms;
1841
+ const ms = Intervals.map_unit[word];
1842
+ return typeof ms === 'number' && Number.isFinite(ms) && ms > 0 ? ms : undefined;
1843
+ }
1844
+ /**
1845
+ * Whether the push intake may feed this package on a chart of
1846
+ * `chartInterval`. False when any input pins something the lane cannot
1847
+ * ride: a market pin on anything but a secondary `ohlcv` or `trades` input, an interval
1848
+ * pin on a celled input or on a class other than `ohlcv`, `funding` or
1849
+ * `oi`, a finer interval, one that is not a whole multiple of the chart's,
1850
+ * or a word outside the vocabulary. An `ohlcv` pin COARSER than the chart
1851
+ * is eligible on the chart's own market or on a pinned one: the chart's own
1852
+ * push lanes stay valid, the period leg is a poll lane (fetched at its own
1853
+ * interval, never pushed) and a foreign market's grid leg is tick-merged
1854
+ * from that market's candle feed; a `funding` or `oi` pin coarser than the
1855
+ * chart buckets its own lane (funding pushed, oi polled); an `intrabar`
1856
+ * input (finer candles as cells) is a fetch-only leg and never a push lane.
1857
+ * An interval equal to the chart's is the chart's own grid on every class.
1858
+ * An `odds` input (the odds lane) is a poll-only leg outside the push set:
1859
+ * its market (POLYMARKET|<condition id>) or interval pin never makes the
1860
+ * package ineligible, even on the primary input, so a package pairing an
1861
+ * odds input with a pinned ohlcv leg keeps that leg's push feed.
1862
+ * The full run refuses the ineligible pins by name before any fetch
1863
+ * (`planWrunPinnedLegs`), so this only classifies.
1864
+ */
1865
+ export function wrunPushEligible(m, chartInterval) {
1866
+ const chartMs = intervalMsOf(chartInterval);
1867
+ for (const input of m.inputs) {
1868
+ const source = m.inputSources[input.name];
1869
+ // An input this market does not need (its needed_by leaves it out) asks no lane
1870
+ // (wrunSheetWithLaneDemand).
1871
+ if (!source || source.undemanded === true)
1872
+ continue;
1873
+ // An intrabar leg and every leg stream (candles, oi_candles, volume_profile_candles) are fetch-only (never a
1874
+ // push lane, re-asked at every leg close): the run pass refuses a bad one by name, the chart's own push lanes
1875
+ // stay valid.
1876
+ if (source.source === 'intrabar' || isWrunLegStreamClass(source.source))
1877
+ continue;
1878
+ // An odds leg is poll-only and outside the push set (the hosted lane's
1879
+ // rule); the run pass refuses a bad odds pin by name (`wrun_odds_*`).
1880
+ if (source.source === 'odds')
1881
+ continue;
1882
+ const marketPinned = !!(source.symbol || source.exchange);
1883
+ // A secondary trades pin (kit 0.77.0) is a push lane of its own, its leg lane.
1884
+ if (marketPinned && source.source === 'trades' && input.cellType !== 'array' && input.index > 0) {
1885
+ if (source.interval !== undefined)
1886
+ return false;
1887
+ continue;
1888
+ }
1889
+ if (marketPinned && (source.source !== 'ohlcv' || input.cellType === 'array' || input.index === 0))
1890
+ return false;
1891
+ if (!source.interval)
1892
+ continue;
1893
+ if (source.interval === chartInterval)
1894
+ continue;
1895
+ const pinnedMs = intervalMsOf(source.interval);
1896
+ if (pinnedMs === undefined || chartMs === undefined)
1897
+ return false;
1898
+ if (pinnedMs === chartMs)
1899
+ continue;
1900
+ if (input.cellType === 'array' || pinnedMs < chartMs || pinnedMs % chartMs !== 0)
1901
+ return false;
1902
+ if (source.source !== 'ohlcv' && !WRUN_TYPED_PINNED_SOURCES.has(source.source))
1903
+ return false;
1904
+ }
1905
+ return true;
1906
+ }
1907
+ export function wrunLiveIntakeStats(indicatorId) {
1908
+ const intake = intakes.get(indicatorId);
1909
+ const context = getWrunOverlayLiveContext(indicatorId);
1910
+ const stats = intake?.stats ?? emptyStats();
1911
+ return {
1912
+ frames: { ...stats.frames },
1913
+ legFrames: { ...stats.legFrames },
1914
+ queued: intake ? pendingCount(intake) : 0,
1915
+ dropped: stats.dropped,
1916
+ droppedGeneration: stats.droppedGeneration,
1917
+ drains: stats.drains,
1918
+ computes: stats.computes,
1919
+ blockRows: [...stats.blockRows],
1920
+ repairs: { ...stats.repairs },
1921
+ reseeds: stats.reseeds,
1922
+ rebuilds: stats.rebuilds,
1923
+ restores: stats.restores,
1924
+ snapshots: stats.snapshots,
1925
+ lastEvalMs: stats.lastEvalMs,
1926
+ lastMainMs: stats.lastMainMs,
1927
+ fetches: stats.fetches,
1928
+ reconcileWindows: stats.reconcileWindows.map((window) => ({ ...window, lanes: [...window.lanes] })),
1929
+ coveredThroughTs: { ...(context?.coveredThroughTs ?? {}) },
1930
+ repaintOwed: context?.repaintOwed ?? false,
1931
+ liveSuspended: context?.liveSuspended ?? false,
1932
+ bypassTokensHeld: stats.bypassTokensHeld,
1933
+ ignored: { ...stats.ignored },
1934
+ };
1935
+ }
1936
+ /** A deep copy of a lane's rows (typed rows become plain arrays, nested
1937
+ * VP buckets copied), so a probe can never alias the live buffers. */
1938
+ function copyLaneRows(rows) {
1939
+ const copyValue = (value) => {
1940
+ if (ArrayBuffer.isView(value))
1941
+ return Array.from(value);
1942
+ if (Array.isArray(value))
1943
+ return value.map(copyValue);
1944
+ return value;
1945
+ };
1946
+ return rows.map((row) => Array.from(row, copyValue));
1947
+ }
1948
+ /** Rig probe: a deep copy of the context's rows for one lane; null without
1949
+ * a context, [] for a lane the context does not hold. */
1950
+ export function wrunLiveLaneRows(indicatorId, lane) {
1951
+ const context = getWrunOverlayLiveContext(indicatorId);
1952
+ if (!context)
1953
+ return null;
1954
+ return copyLaneRows(laneRowsOf(context, lane) ?? []);
1955
+ }
1956
+ const WRUN_LIVE_LANES = [
1957
+ 'book',
1958
+ 'trades',
1959
+ 'trades_usd',
1960
+ 'oi',
1961
+ 'funding',
1962
+ 'liquidations',
1963
+ 'volume_profile',
1964
+ 'trade_volume_by_size',
1965
+ 'implied_volatility',
1966
+ 'skew',
1967
+ 'etf_flow',
1968
+ 'tape',
1969
+ ];
1970
+ /** Rig probe: a shallow snapshot of the context's watermarks and flags
1971
+ * (copied scalars, never the context itself); null without a context. */
1972
+ export function wrunLiveContextSnapshot(indicatorId) {
1973
+ const context = getWrunOverlayLiveContext(indicatorId);
1974
+ if (!context)
1975
+ return null;
1976
+ const laneRowCounts = {};
1977
+ for (const lane of [
1978
+ ...WRUN_LIVE_LANES,
1979
+ ...Object.keys(context.tradeLegRows ?? {}).filter(isWrunTradesLegLane),
1980
+ ...(context.pollLanes ?? []).map((instance) => instance.key),
1981
+ ]) {
1982
+ const rows = laneRowsOf(context, lane);
1983
+ if (rows !== undefined)
1984
+ laneRowCounts[lane] = rows.length;
1985
+ }
1986
+ return {
1987
+ generation: context.generation,
1988
+ intervalMs: context.intervalMs,
1989
+ lastRowTs: context.lastRowTs,
1990
+ paintedThroughTs: context.paintedThroughTs,
1991
+ dirtyFromTs: context.dirtyFromTs,
1992
+ coveredThroughTs: { ...context.coveredThroughTs },
1993
+ pushEligible: context.pushEligible,
1994
+ liveSuspended: context.liveSuspended,
1995
+ repaintOwed: context.repaintOwed,
1996
+ laneRowCounts,
1997
+ ...(context.pinnedLegs !== undefined && context.pinnedLegs.length > 0
1998
+ ? {
1999
+ pinnedLegs: context.pinnedLegs.map((leg) => ({
2000
+ fetchKey: leg.fetchKey,
2001
+ intervalKey: leg.intervalKey,
2002
+ legMs: leg.legMs,
2003
+ inputIndexes: [...leg.inputIndexes],
2004
+ ...(leg.kind !== undefined ? { kind: leg.kind } : {}),
2005
+ ...(leg.market !== undefined ? { market: { ...leg.market } } : {}),
2006
+ rows: (leg.kind === 'intrabar' ? context.intrabarRows : context.pinnedCandleRows)?.[leg.fetchKey]?.length ?? 0,
2007
+ coveredThroughTs: leg.coveredThroughTs,
2008
+ })),
2009
+ }
2010
+ : {}),
2011
+ };
2012
+ }
2013
+ export function __resetWrunLiveIntakeForTests() {
2014
+ intakes.clear();
2015
+ drainRequester = null;
2016
+ }
2017
+ // Registered at import: a torn-down overlay's queues, gaps and stats go
2018
+ // with it (the hook runs before the registries are deleted and the worker
2019
+ // session ended). Specs that mock the drivers util partially have no hook
2020
+ // registry; they never exercise the intake, so the registration is skipped
2021
+ // rather than failing the import.
2022
+ try {
2023
+ onWrunOverlayLiveTeardown(disposeWrunLiveIntake);
2024
+ }
2025
+ catch {
2026
+ // partial module mock (specs); nothing to dispose there
2027
+ }