@senpi-ai/runtime 3.0.133 → 3.0.136

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 (282) hide show
  1. package/README.md +8 -0
  2. package/dist/actions/action-events.d.ts +21 -7
  3. package/dist/actions/action-events.d.ts.map +1 -1
  4. package/dist/actions/action-events.js +54 -26
  5. package/dist/actions/action-events.js.map +1 -1
  6. package/dist/actions/action-runtime-module.d.ts.map +1 -1
  7. package/dist/actions/action-runtime-module.js +38 -7
  8. package/dist/actions/action-runtime-module.js.map +1 -1
  9. package/dist/actions/close-position/close-position.action.d.ts.map +1 -1
  10. package/dist/actions/close-position/close-position.action.js +122 -15
  11. package/dist/actions/close-position/close-position.action.js.map +1 -1
  12. package/dist/actions/decision-engine.d.ts.map +1 -1
  13. package/dist/actions/decision-engine.js +15 -2
  14. package/dist/actions/decision-engine.js.map +1 -1
  15. package/dist/actions/event-derivation.d.ts +71 -2
  16. package/dist/actions/event-derivation.d.ts.map +1 -1
  17. package/dist/actions/event-derivation.js +251 -8
  18. package/dist/actions/event-derivation.js.map +1 -1
  19. package/dist/actions/open-position/open-position.action.d.ts +12 -2
  20. package/dist/actions/open-position/open-position.action.d.ts.map +1 -1
  21. package/dist/actions/open-position/open-position.action.js +141 -41
  22. package/dist/actions/open-position/open-position.action.js.map +1 -1
  23. package/dist/actions/position-tracker/position-tracker.action.d.ts +2 -1
  24. package/dist/actions/position-tracker/position-tracker.action.d.ts.map +1 -1
  25. package/dist/actions/position-tracker/position-tracker.action.js +62 -17
  26. package/dist/actions/position-tracker/position-tracker.action.js.map +1 -1
  27. package/dist/actions/skipped-signal-results.d.ts +25 -0
  28. package/dist/actions/skipped-signal-results.d.ts.map +1 -0
  29. package/dist/actions/skipped-signal-results.js +71 -0
  30. package/dist/actions/skipped-signal-results.js.map +1 -0
  31. package/dist/actions/store/types.d.ts +9 -2
  32. package/dist/actions/store/types.d.ts.map +1 -1
  33. package/dist/actions/store/types.js.map +1 -1
  34. package/dist/actions/types.d.ts +13 -0
  35. package/dist/actions/types.d.ts.map +1 -1
  36. package/dist/actions/types.js +13 -0
  37. package/dist/actions/types.js.map +1 -1
  38. package/dist/cli/guide-commands.d.ts.map +1 -1
  39. package/dist/cli/guide-commands.js +16 -0
  40. package/dist/cli/guide-commands.js.map +1 -1
  41. package/dist/cli/senpi-commands.d.ts.map +1 -1
  42. package/dist/cli/senpi-commands.js +88 -0
  43. package/dist/cli/senpi-commands.js.map +1 -1
  44. package/dist/constants/dsl/index.d.ts +6 -0
  45. package/dist/constants/dsl/index.d.ts.map +1 -1
  46. package/dist/constants/dsl/index.js +6 -0
  47. package/dist/constants/dsl/index.js.map +1 -1
  48. package/dist/dsl/engine-client/event-mapping.d.ts.map +1 -1
  49. package/dist/dsl/engine-client/event-mapping.js +14 -0
  50. package/dist/dsl/engine-client/event-mapping.js.map +1 -1
  51. package/dist/dsl/events/build-closed-event.d.ts +16 -1
  52. package/dist/dsl/events/build-closed-event.d.ts.map +1 -1
  53. package/dist/dsl/events/build-closed-event.js +6 -0
  54. package/dist/dsl/events/build-closed-event.js.map +1 -1
  55. package/dist/dsl/events/handlers.d.ts +7 -0
  56. package/dist/dsl/events/handlers.d.ts.map +1 -1
  57. package/dist/dsl/events/handlers.js +37 -7
  58. package/dist/dsl/events/handlers.js.map +1 -1
  59. package/dist/dsl/monitor/create-monitor.d.ts +6 -2
  60. package/dist/dsl/monitor/create-monitor.d.ts.map +1 -1
  61. package/dist/dsl/monitor/create-monitor.js +82 -9
  62. package/dist/dsl/monitor/create-monitor.js.map +1 -1
  63. package/dist/dsl/monitor/dsl-failure-events.d.ts +45 -2
  64. package/dist/dsl/monitor/dsl-failure-events.d.ts.map +1 -1
  65. package/dist/dsl/monitor/dsl-failure-events.js +52 -3
  66. package/dist/dsl/monitor/dsl-failure-events.js.map +1 -1
  67. package/dist/dsl/monitor/process-one-position.d.ts +1 -0
  68. package/dist/dsl/monitor/process-one-position.d.ts.map +1 -1
  69. package/dist/dsl/monitor/process-one-position.js +159 -36
  70. package/dist/dsl/monitor/process-one-position.js.map +1 -1
  71. package/dist/dsl/plugin/index.d.ts +8 -2
  72. package/dist/dsl/plugin/index.d.ts.map +1 -1
  73. package/dist/dsl/plugin/index.js +8 -6
  74. package/dist/dsl/plugin/index.js.map +1 -1
  75. package/dist/health/types.d.ts +13 -0
  76. package/dist/health/types.d.ts.map +1 -1
  77. package/dist/health/types.js +12 -0
  78. package/dist/health/types.js.map +1 -1
  79. package/dist/index.d.ts +27 -4
  80. package/dist/index.d.ts.map +1 -1
  81. package/dist/index.js +310 -41
  82. package/dist/index.js.map +1 -1
  83. package/dist/runtime/action-registry.d.ts.map +1 -1
  84. package/dist/runtime/action-registry.js +4 -3
  85. package/dist/runtime/action-registry.js.map +1 -1
  86. package/dist/runtime/auto-update/coordinator.d.ts +24 -1
  87. package/dist/runtime/auto-update/coordinator.d.ts.map +1 -1
  88. package/dist/runtime/auto-update/coordinator.js +123 -13
  89. package/dist/runtime/auto-update/coordinator.js.map +1 -1
  90. package/dist/runtime/auto-update/resolve-package-name.d.ts +15 -0
  91. package/dist/runtime/auto-update/resolve-package-name.d.ts.map +1 -1
  92. package/dist/runtime/auto-update/resolve-package-name.js +35 -0
  93. package/dist/runtime/auto-update/resolve-package-name.js.map +1 -1
  94. package/dist/runtime/auto-update/run-update-command.d.ts.map +1 -1
  95. package/dist/runtime/auto-update/run-update-command.js +17 -14
  96. package/dist/runtime/auto-update/run-update-command.js.map +1 -1
  97. package/dist/runtime/auto-update/types.d.ts +24 -0
  98. package/dist/runtime/auto-update/types.d.ts.map +1 -1
  99. package/dist/runtime/auto-update/types.js.map +1 -1
  100. package/dist/runtime/domain-events.d.ts.map +1 -1
  101. package/dist/runtime/domain-events.js +7 -0
  102. package/dist/runtime/domain-events.js.map +1 -1
  103. package/dist/runtime/lifecycle-events.d.ts +15 -3
  104. package/dist/runtime/lifecycle-events.d.ts.map +1 -1
  105. package/dist/runtime/lifecycle-events.js +17 -3
  106. package/dist/runtime/lifecycle-events.js.map +1 -1
  107. package/dist/runtime/lifecycle-reaper.d.ts +4 -6
  108. package/dist/runtime/lifecycle-reaper.d.ts.map +1 -1
  109. package/dist/runtime/lifecycle-reaper.js +4 -6
  110. package/dist/runtime/lifecycle-reaper.js.map +1 -1
  111. package/dist/runtime/reconcile-run.d.ts +13 -3
  112. package/dist/runtime/reconcile-run.d.ts.map +1 -1
  113. package/dist/runtime/reconcile-run.js +4 -3
  114. package/dist/runtime/reconcile-run.js.map +1 -1
  115. package/dist/runtime/reconcile-state.d.ts +8 -0
  116. package/dist/runtime/reconcile-state.d.ts.map +1 -1
  117. package/dist/runtime/reconcile-state.js +4 -1
  118. package/dist/runtime/reconcile-state.js.map +1 -1
  119. package/dist/runtime/run.d.ts +1 -0
  120. package/dist/runtime/run.d.ts.map +1 -1
  121. package/dist/runtime/run.js.map +1 -1
  122. package/dist/runtime/runtime-schema.js +3 -3
  123. package/dist/runtime/runtime-schema.js.map +1 -1
  124. package/dist/runtime/runtime.d.ts +1 -0
  125. package/dist/runtime/runtime.d.ts.map +1 -1
  126. package/dist/runtime/runtime.js +28 -4
  127. package/dist/runtime/runtime.js.map +1 -1
  128. package/dist/runtime/supervisor/long-running-supervisor.d.ts +13 -0
  129. package/dist/runtime/supervisor/long-running-supervisor.d.ts.map +1 -1
  130. package/dist/runtime/supervisor/long-running-supervisor.js +1 -0
  131. package/dist/runtime/supervisor/long-running-supervisor.js.map +1 -1
  132. package/dist/runtime/validate-dsl-position-tracking.d.ts.map +1 -1
  133. package/dist/runtime/validate-dsl-position-tracking.js +2 -1
  134. package/dist/runtime/validate-dsl-position-tracking.js.map +1 -1
  135. package/dist/runtime-api/handlers/ingest-signals.d.ts.map +1 -1
  136. package/dist/runtime-api/handlers/ingest-signals.js +1 -0
  137. package/dist/runtime-api/handlers/ingest-signals.js.map +1 -1
  138. package/dist/runtime-api/handlers/types.d.ts +5 -0
  139. package/dist/runtime-api/handlers/types.d.ts.map +1 -1
  140. package/dist/runtime-api/intake/ingest-request.d.ts +17 -0
  141. package/dist/runtime-api/intake/ingest-request.d.ts.map +1 -0
  142. package/dist/runtime-api/intake/ingest-request.js +34 -0
  143. package/dist/runtime-api/intake/ingest-request.js.map +1 -0
  144. package/dist/runtime-api/intake/intake-events.d.ts +1 -1
  145. package/dist/runtime-api/intake/intake-events.d.ts.map +1 -1
  146. package/dist/runtime-api/intake/intake-events.js +9 -3
  147. package/dist/runtime-api/intake/intake-events.js.map +1 -1
  148. package/dist/runtime-api/intake/intake-mount.d.ts.map +1 -1
  149. package/dist/runtime-api/intake/intake-mount.js +4 -19
  150. package/dist/runtime-api/intake/intake-mount.js.map +1 -1
  151. package/dist/runtime-api/intake/process-error-post.d.ts +6 -1
  152. package/dist/runtime-api/intake/process-error-post.d.ts.map +1 -1
  153. package/dist/runtime-api/intake/process-error-post.js +5 -1
  154. package/dist/runtime-api/intake/process-error-post.js.map +1 -1
  155. package/dist/runtime-api/intake/process-signal-post.d.ts +7 -1
  156. package/dist/runtime-api/intake/process-signal-post.d.ts.map +1 -1
  157. package/dist/runtime-api/intake/process-signal-post.js +5 -0
  158. package/dist/runtime-api/intake/process-signal-post.js.map +1 -1
  159. package/dist/runtime-api/intake/scaffold-error-router.js.map +1 -1
  160. package/dist/runtime-api/intake/scaffold-tick-router.d.ts +39 -0
  161. package/dist/runtime-api/intake/scaffold-tick-router.d.ts.map +1 -0
  162. package/dist/runtime-api/intake/scaffold-tick-router.js +122 -0
  163. package/dist/runtime-api/intake/scaffold-tick-router.js.map +1 -0
  164. package/dist/runtime-api/intake/scanner-launcher.d.ts.map +1 -1
  165. package/dist/runtime-api/intake/scanner-launcher.js +15 -0
  166. package/dist/runtime-api/intake/scanner-launcher.js.map +1 -1
  167. package/dist/runtime-api/intake/types.d.ts +55 -0
  168. package/dist/runtime-api/intake/types.d.ts.map +1 -1
  169. package/dist/runtime-api/routes/signals.d.ts +2 -0
  170. package/dist/runtime-api/routes/signals.d.ts.map +1 -1
  171. package/dist/runtime-api/routes/signals.js +5 -1
  172. package/dist/runtime-api/routes/signals.js.map +1 -1
  173. package/dist/runtime-api/routes/signals.schema.d.ts +43 -0
  174. package/dist/runtime-api/routes/signals.schema.d.ts.map +1 -1
  175. package/dist/runtime-api/routes/signals.schema.js +43 -0
  176. package/dist/runtime-api/routes/signals.schema.js.map +1 -1
  177. package/dist/runtime-api/server.d.ts +7 -1
  178. package/dist/runtime-api/server.d.ts.map +1 -1
  179. package/dist/runtime-api/server.js +1 -0
  180. package/dist/runtime-api/server.js.map +1 -1
  181. package/dist/scanners/engine/engine.d.ts +22 -1
  182. package/dist/scanners/engine/engine.d.ts.map +1 -1
  183. package/dist/scanners/engine/engine.js +78 -11
  184. package/dist/scanners/engine/engine.js.map +1 -1
  185. package/dist/scanners/engine/lifecycle.d.ts +7 -1
  186. package/dist/scanners/engine/lifecycle.d.ts.map +1 -1
  187. package/dist/scanners/events.d.ts +4 -0
  188. package/dist/scanners/events.d.ts.map +1 -1
  189. package/dist/scanners/events.js +2 -0
  190. package/dist/scanners/events.js.map +1 -1
  191. package/dist/scanners/external/scaffold/delivery.py +194 -25
  192. package/dist/scanners/external/scaffold/scaffold.py +172 -31
  193. package/dist/scanners/external/scaffold/senpi_mcp.py +150 -8
  194. package/dist/scanners/external/scaffold/tick_facts.py +130 -0
  195. package/dist/scanners/external/scaffold/ulid.py +58 -0
  196. package/dist/scanners/external-scanner-receiver.d.ts +17 -0
  197. package/dist/scanners/external-scanner-receiver.d.ts.map +1 -1
  198. package/dist/scanners/external-scanner-receiver.js +8 -0
  199. package/dist/scanners/external-scanner-receiver.js.map +1 -1
  200. package/dist/scanners/runtime-module.d.ts +1 -0
  201. package/dist/scanners/runtime-module.d.ts.map +1 -1
  202. package/dist/scanners/runtime-module.js +66 -9
  203. package/dist/scanners/runtime-module.js.map +1 -1
  204. package/dist/telemetry/attributes.d.ts +20 -0
  205. package/dist/telemetry/attributes.d.ts.map +1 -1
  206. package/dist/telemetry/attributes.js +24 -0
  207. package/dist/telemetry/attributes.js.map +1 -1
  208. package/dist/telemetry/dsl-positions.d.ts +111 -0
  209. package/dist/telemetry/dsl-positions.d.ts.map +1 -0
  210. package/dist/telemetry/dsl-positions.js +118 -0
  211. package/dist/telemetry/dsl-positions.js.map +1 -0
  212. package/dist/telemetry/event-builders.d.ts +81 -6
  213. package/dist/telemetry/event-builders.d.ts.map +1 -1
  214. package/dist/telemetry/event-builders.js +141 -17
  215. package/dist/telemetry/event-builders.js.map +1 -1
  216. package/dist/telemetry/event-store.d.ts +6 -0
  217. package/dist/telemetry/event-store.d.ts.map +1 -1
  218. package/dist/telemetry/event-store.js +12 -8
  219. package/dist/telemetry/event-store.js.map +1 -1
  220. package/dist/telemetry/live-telemetry.d.ts +257 -0
  221. package/dist/telemetry/live-telemetry.d.ts.map +1 -0
  222. package/dist/telemetry/live-telemetry.js +959 -0
  223. package/dist/telemetry/live-telemetry.js.map +1 -0
  224. package/dist/telemetry/metrics.d.ts +14 -2
  225. package/dist/telemetry/metrics.d.ts.map +1 -1
  226. package/dist/telemetry/metrics.js +19 -4
  227. package/dist/telemetry/metrics.js.map +1 -1
  228. package/dist/telemetry/scanner-stats.d.ts +143 -0
  229. package/dist/telemetry/scanner-stats.d.ts.map +1 -0
  230. package/dist/telemetry/scanner-stats.js +426 -0
  231. package/dist/telemetry/scanner-stats.js.map +1 -0
  232. package/dist/telemetry/scanner-tick-feed.d.ts +87 -0
  233. package/dist/telemetry/scanner-tick-feed.d.ts.map +1 -0
  234. package/dist/telemetry/scanner-tick-feed.js +48 -0
  235. package/dist/telemetry/scanner-tick-feed.js.map +1 -0
  236. package/dist/telemetry/spans/redact-pii.d.ts.map +1 -1
  237. package/dist/telemetry/spans/redact-pii.js +18 -2
  238. package/dist/telemetry/spans/redact-pii.js.map +1 -1
  239. package/dist/telemetry/strategy-topology.d.ts +99 -0
  240. package/dist/telemetry/strategy-topology.d.ts.map +1 -0
  241. package/dist/telemetry/strategy-topology.js +93 -0
  242. package/dist/telemetry/strategy-topology.js.map +1 -0
  243. package/dist/telemetry/telemetry-publisher.d.ts +83 -0
  244. package/dist/telemetry/telemetry-publisher.d.ts.map +1 -0
  245. package/dist/telemetry/telemetry-publisher.js +256 -0
  246. package/dist/telemetry/telemetry-publisher.js.map +1 -0
  247. package/dist/telemetry/tick-frame.d.ts +212 -0
  248. package/dist/telemetry/tick-frame.d.ts.map +1 -0
  249. package/dist/telemetry/tick-frame.js +421 -0
  250. package/dist/telemetry/tick-frame.js.map +1 -0
  251. package/dist/telemetry/timeline-buffer.d.ts +95 -0
  252. package/dist/telemetry/timeline-buffer.d.ts.map +1 -0
  253. package/dist/telemetry/timeline-buffer.js +237 -0
  254. package/dist/telemetry/timeline-buffer.js.map +1 -0
  255. package/dist/telemetry/tracing.d.ts +9 -0
  256. package/dist/telemetry/tracing.d.ts.map +1 -1
  257. package/dist/telemetry/tracing.js +36 -0
  258. package/dist/telemetry/tracing.js.map +1 -1
  259. package/dist/types/dsl/index.d.ts +48 -0
  260. package/dist/types/dsl/index.d.ts.map +1 -1
  261. package/dist/types/scanner.d.ts +15 -0
  262. package/dist/types/scanner.d.ts.map +1 -1
  263. package/dist/utils/event-catalog.d.ts +202 -0
  264. package/dist/utils/event-catalog.d.ts.map +1 -1
  265. package/dist/utils/event-catalog.js +26 -0
  266. package/dist/utils/event-catalog.js.map +1 -1
  267. package/dist/utils/logger.d.ts +4 -1
  268. package/dist/utils/logger.d.ts.map +1 -1
  269. package/dist/utils/logger.js.map +1 -1
  270. package/dist/utils/telemetry-required-log.d.ts +60 -0
  271. package/dist/utils/telemetry-required-log.d.ts.map +1 -0
  272. package/dist/utils/telemetry-required-log.js +105 -0
  273. package/dist/utils/telemetry-required-log.js.map +1 -0
  274. package/dist/utils/ulid.d.ts +9 -0
  275. package/dist/utils/ulid.d.ts.map +1 -0
  276. package/dist/utils/ulid.js +43 -0
  277. package/dist/utils/ulid.js.map +1 -0
  278. package/dist/validate/recipe-checks.d.ts.map +1 -1
  279. package/dist/validate/recipe-checks.js +2 -2
  280. package/dist/validate/recipe-checks.js.map +1 -1
  281. package/openclaw.plugin.json +4 -4
  282. package/package.json +1 -1
@@ -2,8 +2,10 @@
2
2
  * Pure builders for the DSL exit-engine's failure-state events — degradation the monitor tracks but
3
3
  * that previously surfaced only as a tick-span error or a `warn` log: an exchange SL push that threw
4
4
  * ({@link buildSlSyncFailedEvent}), a position with no price past the stale threshold
5
- * ({@link buildPriceFetchStaleEvent}), and a Phase-2 backend handoff that degraded
6
- * ({@link buildHandoffFailedEvent}).
5
+ * ({@link buildPriceFetchStaleEvent}), a Phase-2 backend handoff that degraded
6
+ * ({@link buildHandoffFailedEvent}), and the widest of them — a monitor tick that aborted before it
7
+ * reached a single position ({@link buildTickAbortedEvent}) and the tick that ends the outage
8
+ * ({@link buildTickRecoveredEvent}).
7
9
  *
8
10
  * Pure `(input) => EmittableEvent`; the monitor emits the result through `emitEvent`'s guard, so a
9
11
  * throwing build or a telemetry failure can never disturb a tick. A thrown venue/backend error rides
@@ -13,12 +15,13 @@
13
15
  * events, so it is not passed here.
14
16
  */
15
17
  import { EVENT_NAME, eventAttrs } from "../../utils/event-catalog.js";
16
- /** The id/asset/dex attributes every DSL failure event carries. */
18
+ /** The id/asset/dex and tick attributes every DSL failure event carries. */
17
19
  function baseAttrs(ctx) {
18
20
  return {
19
21
  "senpi.asset": ctx.asset,
20
22
  "senpi.dex": ctx.dex || undefined,
21
23
  "senpi.position.id": ctx.positionId,
24
+ "senpi.tick.id": ctx.tickId,
22
25
  };
23
26
  }
24
27
  /** An exchange SL create/update (`editPosition`) threw — the stop the venue never received. */
@@ -70,4 +73,50 @@ export function buildHandoffFailedEvent(ctx, opts) {
70
73
  ...(opts.error !== undefined ? { error: opts.error } : {}),
71
74
  };
72
75
  }
76
+ /** How the body phrases the age, including the case where there is no age to phrase. */
77
+ function ageText(ageMs) {
78
+ return ageMs == null ? "no tick has completed yet" : `last completed tick ${ageMs}ms ago`;
79
+ }
80
+ /**
81
+ * A monitor tick that aborted at the clearinghouse gate — no position under this monitor was
82
+ * evaluated, so no breach, no time cut and no stop sync ran for any of them. The per-position
83
+ * staleness counters live past this gate and never advance, which is why this event has to exist:
84
+ * `dsl.price_fetch_stale` cannot fire for this failure mode.
85
+ *
86
+ * The caller latches it, so a sustained outage costs one record rather than one per tick.
87
+ */
88
+ export function buildTickAbortedEvent(input) {
89
+ return {
90
+ name: EVENT_NAME.dslTickAborted,
91
+ level: "warn",
92
+ body: `DSL monitor tick aborted (${input.reason}); ` +
93
+ `${input.unevaluatedPositionCount} position(s) unevaluated, ${ageText(input.lastCompletedTickAgeMs)}`,
94
+ attributes: eventAttrs({
95
+ "senpi.tick.id": input.tickId,
96
+ "senpi.dsl.tick_abort_reason": input.reason,
97
+ "senpi.dsl.last_completed_tick_age_ms": input.lastCompletedTickAgeMs,
98
+ "senpi.dsl.unavailable_address_count": input.unavailableAddressCount,
99
+ "senpi.dsl.unevaluated_position_count": input.unevaluatedPositionCount,
100
+ }),
101
+ ...(input.error !== undefined ? { error: input.error } : {}),
102
+ };
103
+ }
104
+ /**
105
+ * The tick that ends an abort outage. Emitted once, on the first tick to complete after one or more
106
+ * aborted ones, so a reader told "not protected" is told when that stops being true. Its age field
107
+ * is measured before the recovering tick stamps its own completion, so it is the length of the gap.
108
+ */
109
+ export function buildTickRecoveredEvent(input) {
110
+ return {
111
+ name: EVENT_NAME.dslTickRecovered,
112
+ level: "info",
113
+ body: `DSL monitor tick completed after ${input.abortedTickCount} aborted tick(s); ` +
114
+ ageText(input.lastCompletedTickAgeMs),
115
+ attributes: eventAttrs({
116
+ "senpi.tick.id": input.tickId,
117
+ "senpi.dsl.last_completed_tick_age_ms": input.lastCompletedTickAgeMs,
118
+ "senpi.dsl.aborted_tick_count": input.abortedTickCount,
119
+ }),
120
+ };
121
+ }
73
122
  //# sourceMappingURL=dsl-failure-events.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"dsl-failure-events.js","sourceRoot":"","sources":["../../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,UAAU,CAA+C;YACnE,eAAe,EAAE,KAAK,CAAC,MAAM;YAC7B,sCAAsC,EAAE,KAAK,CAAC,sBAAsB;YACpE,8BAA8B,EAAE,KAAK,CAAC,gBAAgB;SACvD,CAAC;KACH,CAAC;AACJ,CAAC"}
@@ -17,6 +17,7 @@ export declare function closeReasonContext(closeReason: string, s: DslState): {
17
17
  closePeakRoe?: number;
18
18
  };
19
19
  export interface MonitorProcessContext {
20
+ /** The id for this tick — a ULID, one per `runTick`, on its log lines, events and spans. */
20
21
  tickId: string;
21
22
  ts: string;
22
23
  now: () => string;
@@ -1 +1 @@
1
- {"version":3,"file":"process-one-position.d.ts","sourceRoot":"","sources":["../../../src/dsl/monitor/process-one-position.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACxD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AAC7D,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,0BAA0B,CAAC;AACzD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAqBzD,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,oBAAoB,CAAC;AAgB/C;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAChC,WAAW,EAAE,MAAM,EACnB,CAAC,EAAE,QAAQ,GACV;IAAE,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAAC,YAAY,CAAC,EAAE,MAAM,CAAA;CAAE,CAYvD;AAkFD,MAAM,WAAW,qBAAqB;IACpC,MAAM,EAAE,MAAM,CAAC;IACf,EAAE,EAAE,MAAM,CAAC;IACX,GAAG,EAAE,MAAM,MAAM,CAAC;IAClB,YAAY,EAAE,YAAY,CAAC;IAC3B,cAAc,EAAE,GAAG,CAAC,MAAM,EAAE,aAAa,CAAC,CAAC;IAC3C,GAAG,EAAE,aAAa,CAAC;IACnB,uBAAuB,CAAC,EAAE,CAAC,GAAG,EA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EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,OAAO,CAAC;IACnD,6DAA6D;IAC7D,cAAc,CAAC,EAAE,OAAO,sBAAsB,EAAE,cAAc,CAAC;IAC/D,oDAAoD;IACpD,wBAAwB,CAAC,EAAE,OAAO,sBAAsB,EAAE,wBAAwB,CAAC;CACpF;AAgFD,wBAAsB,kBAAkB,CACtC,GAAG,EAAE,qBAAqB,EAC1B,GAAG,EAAE,MAAM,EACX,KAAK,EAAE,QAAQ,EACf,KAAK,EAAE,MAAM,GAAG,SAAS,EACzB,IAAI,EAAE,IAAI,GACT,OAAO,CAAC,IAAI,CAAC,CAs1Bf"}
@@ -2,7 +2,7 @@
2
2
  * Per-position work for one monitor tick: SL fill check, pending close, dslTick, exchange sync.
3
3
  */
4
4
  import { dslTick, exchangeStopLossTriggerPx } from "../engine/index.js";
5
- import { CLOSE_REASON, MAX_CONSECUTIVE_CLOSE_FAILURES, DSL_EXTERNAL_RETRIES, DSL_EXTERNAL_RETRY_DELAY_MS, DSL_HANDOFF_SL_STATUS_TIMEOUT_MS, DSL_EVENTS, EVENTS, DSL_PRICE_FETCH_FAILURE_WARN_THRESHOLD, DSL_HEARTBEAT_FIRST_HOUR_MS, DSL_HEARTBEAT_INTERVAL_MS, DSL_HEARTBEAT_TIER_SUPPRESS_MS, } from "../../constants/dsl/index.js";
5
+ import { CLOSE_REASON, MAX_CONSECUTIVE_CLOSE_FAILURES, DSL_EXTERNAL_RETRIES, DSL_EXTERNAL_RETRY_DELAY_MS, DSL_HANDOFF_SL_STATUS_TIMEOUT_MS, DSL_EVENTS, EVENTS, DSL_PRICE_FETCH_FAILURE_WARN_THRESHOLD, DSL_HEARTBEAT_FIRST_HOUR_MS, DSL_HEARTBEAT_INTERVAL_MS, DSL_HEARTBEAT_TIER_SUPPRESS_MS, DSL_VENUE_STATUS_MAX_LENGTH, } from "../../constants/dsl/index.js";
6
6
  import { logger } from "../../utils/logger/dsl.js";
7
7
  import { withRetry } from "../../utils/retry.js";
8
8
  import { isRetriableMcpError } from "../../senpi/client.js";
@@ -16,6 +16,7 @@ import { mapBackendEventToBusEvent } from "../engine-client/event-mapping.js";
16
16
  import { buildDslClosedEventFromState } from "../events/build-closed-event.js";
17
17
  import { parseCloseFillDetail } from "../../senpi/client.js";
18
18
  import { emitEvent } from "../../actions/event-derivation.js";
19
+ import { PAYLOAD_KEY } from "../../utils/event-catalog.js";
19
20
  import { buildSlSyncFailedEvent, buildPriceFetchStaleEvent, buildHandoffFailedEvent, } from "./dsl-failure-events.js";
20
21
  /**
21
22
  * Structured close-reason context forwarded with a DSL-cut close (notifications 2.3):
@@ -77,23 +78,35 @@ async function setIfStillActive(stateManager, key, address, asset, buildNext, ct
77
78
  });
78
79
  }
79
80
  /**
80
- * Telemetry stamps for a flat POSITION_CLOSED emit: the cross-flow position.id plus the
81
- * closing-order fill detail when a close response (or SL order id) is available. Data-only —
82
- * no consumer branches on these; the venue `orderId` is the fills-ledger join key for
83
- * post-hoc lifecycle correlation.
81
+ * Telemetry stamps every emit out of the monitor carries: the cross-flow position.id, the tick that
82
+ * produced the emit, and — on the `dsl.*` events, whose consumers read it — who moved the stop.
83
+ * Data-only; no consumer branches on these.
84
84
  */
85
- function closedEmitStamps(state, fill = {}) {
85
+ function emitStamps(state, tickId, source) {
86
86
  return {
87
87
  __positionId: state.positionId ?? state.correlationId,
88
+ [PAYLOAD_KEY.tickId]: tickId,
89
+ ...(source !== undefined ? { __dslSource: source } : {}),
90
+ };
91
+ }
92
+ /**
93
+ * Stamps for a flat POSITION_CLOSED emit: {@link emitStamps} plus the closing-order fill detail when
94
+ * a close response (or SL order id) is available. The venue `orderId` is the fills-ledger join key
95
+ * for post-hoc lifecycle correlation. No source: the lifecycle record has no field for it.
96
+ */
97
+ function closedEmitStamps(state, tickId, fill = {}) {
98
+ return {
99
+ ...emitStamps(state, tickId),
88
100
  ...fill,
89
101
  };
90
102
  }
91
103
  /** The position identity a DSL failure event pins onto its record. */
92
- function failureContext(state) {
104
+ function failureContext(state, tickId) {
93
105
  return {
94
106
  asset: state.asset,
95
107
  dex: state.dex || undefined,
96
108
  positionId: state.positionId ?? state.correlationId,
109
+ tickId,
97
110
  };
98
111
  }
99
112
  /**
@@ -126,6 +139,40 @@ function isGoneVenueSlStatus(status) {
126
139
  const s = status.trim().toLowerCase();
127
140
  return GONE_VENUE_SL_STATUSES.has(s) || s.endsWith("rejected");
128
141
  }
142
+ /**
143
+ * The venue's answer about the stop order, in the shape the state persists it.
144
+ *
145
+ * `undefined` whenever the venue named no status — a failed lookup, an unreadable response, or a
146
+ * response with no status field. Those are not answers, so they write nothing: an absent
147
+ * `lastVenueStatus` has to keep meaning "the venue has not told us", which is precisely what a
148
+ * reader needs to tell a confirmed stop from a believed one. Nothing is defaulted or synthesised
149
+ * for them.
150
+ *
151
+ * The status is stored verbatim (only length-capped), because the branching above already ignores
152
+ * everything outside the known-dead set — the unrecognised answer is the one worth keeping.
153
+ */
154
+ function venueStatusStamp(status) {
155
+ const raw = status?.status;
156
+ if (raw == null)
157
+ return undefined;
158
+ return { lastVenueStatus: raw.slice(0, DSL_VENUE_STATUS_MAX_LENGTH), lastVenueStatusAt: Date.now() };
159
+ }
160
+ /**
161
+ * The venue's answer about the BACKEND ratchet's `activeSLOrderId`, in the shape the state persists.
162
+ *
163
+ * A different order from the position's own `slOrderId`, so a different pair of fields: the handoff
164
+ * asks about the stop the backend placed, and recording that answer as the position's own would
165
+ * claim a confirmation the venue never gave for it.
166
+ */
167
+ function backendSlStatusStamp(status) {
168
+ const raw = status?.status;
169
+ if (raw == null)
170
+ return undefined;
171
+ return {
172
+ backendSlVenueStatus: raw.slice(0, DSL_VENUE_STATUS_MAX_LENGTH),
173
+ backendSlVenueStatusAt: Date.now(),
174
+ };
175
+ }
129
176
  export async function processOnePosition(ctx, key, state, price, span) {
130
177
  const { tickId, ts, now: isoNow, stateManager, strategyStates, bus, isSlPlacementInProgress = () => false } = ctx;
131
178
  const strategy = strategyStates.get(state.address);
@@ -161,6 +208,9 @@ export async function processOnePosition(ctx, key, state, price, span) {
161
208
  label: "getOrderStatus (SL-hit check)",
162
209
  onRetry: (attempt, remaining, err) => logger.warn("[dsl] getOrderStatus retry", { asset: state.asset, attempt, remaining, err }),
163
210
  }));
211
+ // The venue has answered. Every branch below keeps that answer; only a throw or a
212
+ // status-less response leaves the fields absent.
213
+ const venueStamp = venueStatusStamp(status);
164
214
  if (status?.filled) {
165
215
  span.addEvent("external_close_detected", { source: "sl_order" });
166
216
  span.setAttribute("senpi.dsl.close_reason", CLOSE_REASON.EXCHANGE_SL_HIT);
@@ -170,7 +220,13 @@ export async function processOnePosition(ctx, key, state, price, span) {
170
220
  slOrderId: state.slOrderId,
171
221
  address: state.address,
172
222
  });
173
- const closedState = { ...state, active: false, closedAt: ts, closeReason: CLOSE_REASON.EXCHANGE_SL_HIT };
223
+ const closedState = {
224
+ ...state,
225
+ ...venueStamp,
226
+ active: false,
227
+ closedAt: ts,
228
+ closeReason: CLOSE_REASON.EXCHANGE_SL_HIT,
229
+ };
174
230
  const archived = await archiveIfStillActive(stateManager, key, state.address, state.asset, effectiveDex(state), () => closedState, { tickId, skipMessage: "[dsl] skip SL-hit archive: state already cleared (concurrent close)" });
175
231
  if (!archived)
176
232
  return;
@@ -181,13 +237,15 @@ export async function processOnePosition(ctx, key, state, price, span) {
181
237
  reason: CLOSE_REASON.EXCHANGE_SL_HIT,
182
238
  closeReason: CLOSE_REASON.EXCHANGE_SL_HIT,
183
239
  // The closing order IS the exchange SL order — its id joins the closing fill.
184
- ...closedEmitStamps(state, state.slOrderId != null ? { orderId: String(state.slOrderId) } : {}),
240
+ ...closedEmitStamps(state, tickId, state.slOrderId != null ? { orderId: String(state.slOrderId) } : {}),
185
241
  timestamp: ts,
186
242
  });
187
243
  bus.emit(DSL_EVENTS.CLOSED, buildDslClosedEventFromState(closedState, {
188
244
  reason: CLOSE_REASON.EXCHANGE_SL_HIT,
189
245
  closeReason: CLOSE_REASON.EXCHANGE_SL_HIT,
190
246
  timestamp: ts,
247
+ tickId,
248
+ source: "runtime",
191
249
  }));
192
250
  return;
193
251
  }
@@ -208,8 +266,17 @@ export async function processOnePosition(ctx, key, state, price, span) {
208
266
  phase: state.phase,
209
267
  });
210
268
  recordDslDegradation({ failure: "sl_gone" });
211
- await setIfStillActive(stateManager, key, state.address, state.asset, (current) => ({ ...current, slOrderId: null, lastSyncedFloorPrice: null }), { tickId, skipMessage: "[dsl] skip gone-SL clear: state already cleared (concurrent close)" });
212
- state = { ...state, slOrderId: null, lastSyncedFloorPrice: null };
269
+ await setIfStillActive(stateManager, key, state.address, state.asset, (current) => ({ ...current, ...venueStamp, slOrderId: null, lastSyncedFloorPrice: null }), { tickId, skipMessage: "[dsl] skip gone-SL clear: state already cleared (concurrent close)" });
270
+ state = { ...state, ...venueStamp, slOrderId: null, lastSyncedFloorPrice: null };
271
+ }
272
+ else if (venueStamp) {
273
+ // The stop is neither filled nor in the known-dead set, so the branching above changes
274
+ // nothing — but the answer itself is the evidence, and it is the only thing that separates
275
+ // "a stop IS resting" from "we believe one is". Persisted here rather than left to the
276
+ // tick's own write, because the paths that return before it (no price, pendingClose error)
277
+ // merge onto the re-read state and would drop a stamp held only in this local copy.
278
+ await setIfStillActive(stateManager, key, state.address, state.asset, (current) => ({ ...current, ...venueStamp }), { tickId, skipMessage: "[dsl] skip venue-status stamp: state already cleared (concurrent close)" });
279
+ state = { ...state, ...venueStamp };
213
280
  }
214
281
  }
215
282
  catch (e) {
@@ -262,13 +329,15 @@ export async function processOnePosition(ctx, key, state, price, span) {
262
329
  wallet: state.wallet,
263
330
  reason: closeReasonToUse,
264
331
  closeReason: closeReasonToUse,
265
- ...closedEmitStamps(state, closeFill),
332
+ ...closedEmitStamps(state, tickId, closeFill),
266
333
  timestamp: ts,
267
334
  });
268
335
  bus.emit(DSL_EVENTS.CLOSED, buildDslClosedEventFromState(closedState, {
269
336
  reason: closeReasonToUse,
270
337
  closeReason: closeReasonToUse,
271
338
  timestamp: ts,
339
+ tickId,
340
+ source: "runtime",
272
341
  }));
273
342
  }
274
343
  catch (e) {
@@ -287,13 +356,15 @@ export async function processOnePosition(ctx, key, state, price, span) {
287
356
  wallet: state.wallet,
288
357
  reason: "error",
289
358
  closeReason: CLOSE_REASON.CLOSE_POSITION_FAILED,
290
- ...closedEmitStamps(state),
359
+ ...closedEmitStamps(state, tickId),
291
360
  timestamp: isoNow(),
292
361
  });
293
362
  bus.emit(DSL_EVENTS.CLOSED, buildDslClosedEventFromState(failedCloseState, {
294
363
  reason: "error",
295
364
  closeReason: CLOSE_REASON.CLOSE_POSITION_FAILED,
296
365
  timestamp: isoNow(),
366
+ tickId,
367
+ source: "runtime",
297
368
  }));
298
369
  }
299
370
  else {
@@ -303,7 +374,7 @@ export async function processOnePosition(ctx, key, state, price, span) {
303
374
  asset: state.asset,
304
375
  attempt: next.consecutiveCloseFailures,
305
376
  triggerReason: state.triggerReason,
306
- __positionId: state.positionId ?? state.correlationId,
377
+ ...emitStamps(state, tickId, "runtime"),
307
378
  timestamp: isoNow(),
308
379
  });
309
380
  }
@@ -328,7 +399,7 @@ export async function processOnePosition(ctx, key, state, price, span) {
328
399
  ...(shouldEmitStale ? { priceFetchStaleEmitted: true } : {}),
329
400
  }), { tickId, skipMessage: "[dsl] skip no-price counter write: state already cleared (concurrent close)" });
330
401
  if (shouldEmitStale) {
331
- emitEvent(logger, () => buildPriceFetchStaleEvent(failureContext(state), next.consecutiveFetchFailures));
402
+ emitEvent(logger, () => buildPriceFetchStaleEvent(failureContext(state, tickId), next.consecutiveFetchFailures));
332
403
  recordDslDegradation({ failure: "price_stale" });
333
404
  }
334
405
  logger.info("[dsl] monitor no price for position, skipping dslTick", {
@@ -498,13 +569,15 @@ export async function processOnePosition(ctx, key, state, price, span) {
498
569
  wallet: state.wallet,
499
570
  reason: "error",
500
571
  closeReason: CLOSE_REASON.CLOSE_POSITION_FAILED,
501
- ...closedEmitStamps(state),
572
+ ...closedEmitStamps(state, tickId),
502
573
  timestamp: isoNow(),
503
574
  });
504
575
  bus.emit(DSL_EVENTS.CLOSED, buildDslClosedEventFromState(failedCloseState, {
505
576
  reason: "error",
506
577
  closeReason: CLOSE_REASON.CLOSE_POSITION_FAILED,
507
578
  timestamp: isoNow(),
579
+ tickId,
580
+ source: "runtime",
508
581
  }));
509
582
  }
510
583
  else {
@@ -514,7 +587,7 @@ export async function processOnePosition(ctx, key, state, price, span) {
514
587
  asset: state.asset,
515
588
  attempt: pending.consecutiveCloseFailures,
516
589
  triggerReason: state.triggerReason,
517
- __positionId: state.positionId ?? state.correlationId,
590
+ ...emitStamps(state, tickId, "runtime"),
518
591
  timestamp: isoNow(),
519
592
  });
520
593
  }
@@ -536,13 +609,15 @@ export async function processOnePosition(ctx, key, state, price, span) {
536
609
  wallet: state.wallet,
537
610
  reason: closeReasonToUse,
538
611
  closeReason: closeReasonToUse,
539
- ...closedEmitStamps(state, closeFill),
612
+ ...closedEmitStamps(state, tickId, closeFill),
540
613
  timestamp: ts,
541
614
  });
542
615
  bus.emit(DSL_EVENTS.CLOSED, buildDslClosedEventFromState(closedState, {
543
616
  reason: closeReasonToUse,
544
617
  closeReason: closeReasonToUse,
545
618
  timestamp: ts,
619
+ tickId,
620
+ source: "runtime",
546
621
  }));
547
622
  return;
548
623
  }
@@ -562,7 +637,7 @@ export async function processOnePosition(ctx, key, state, price, span) {
562
637
  triggerPct: tier.triggerPct,
563
638
  newFloorPrice: dslEffectiveFloor,
564
639
  triggerReason: next.triggerReason,
565
- __positionId: next.positionId ?? next.correlationId,
640
+ ...emitStamps(next, tickId, "runtime"),
566
641
  timestamp: ts,
567
642
  direction: next.direction,
568
643
  entryPrice: next.entryPrice,
@@ -592,7 +667,7 @@ export async function processOnePosition(ctx, key, state, price, span) {
592
667
  phase: result.newPhase ?? 2,
593
668
  tierIndex: result.newTierIndex ?? 0,
594
669
  triggerReason: next.triggerReason,
595
- __positionId: next.positionId ?? next.correlationId,
670
+ ...emitStamps(next, tickId, "runtime"),
596
671
  timestamp: ts,
597
672
  });
598
673
  }
@@ -706,7 +781,7 @@ export async function processOnePosition(ctx, key, state, price, span) {
706
781
  newSLPrice: exchangeTriggerPx,
707
782
  slOrderId: synced.slOrderId ?? 0,
708
783
  triggerReason: synced.triggerReason,
709
- __positionId: synced.positionId ?? synced.correlationId,
784
+ ...emitStamps(synced, tickId, "runtime"),
710
785
  direction: synced.direction,
711
786
  // Stop-raise copy inputs: position leverage, the high-water price behind
712
787
  // the raise ("new high …"), and entry price (leverage + entry → the
@@ -732,7 +807,7 @@ export async function processOnePosition(ctx, key, state, price, span) {
732
807
  }, { tickId, skipMessage: "[dsl] skip SL sync-failure write: state no longer active (concurrent close)" });
733
808
  if (syncFailureCount == null)
734
809
  return;
735
- emitEvent(logger, () => buildSlSyncFailedEvent(failureContext(state), syncFailureCount ?? 0, e));
810
+ emitEvent(logger, () => buildSlSyncFailedEvent(failureContext(state, tickId), syncFailureCount ?? 0, e));
736
811
  recordDslDegradation({ failure: "sl_sync", errorType: errorTypeOf(e) });
737
812
  }
738
813
  }
@@ -765,7 +840,7 @@ export async function processOnePosition(ctx, key, state, price, span) {
765
840
  currentTierIndex: hbState.currentTierIndex,
766
841
  elapsedMinutes: hbState.elapsedMinutes,
767
842
  triggerReason: hbState.triggerReason,
768
- __positionId: hbState.positionId ?? hbState.correlationId,
843
+ ...emitStamps(hbState, tickId, "runtime"),
769
844
  margin: hbState.leverage > 0
770
845
  ? (hbState.size * hbState.entryPrice) / hbState.leverage
771
846
  : 0,
@@ -808,15 +883,17 @@ const LIVE_VENUE_SL_STATUSES = new Set(["open", "triggered"]);
808
883
  async function backendHasLiveSl(pos, strategy, tickId, asset) {
809
884
  const slOrderId = parseSlOrderId(pos.activeSLOrderId);
810
885
  if (slOrderId == null)
811
- return false;
886
+ return { live: false };
812
887
  try {
813
888
  const status = await withRetry(() => strategy.getOrderStatus(slOrderId, { timeoutMs: DSL_HANDOFF_SL_STATUS_TIMEOUT_MS }), {
814
889
  retries: DSL_EXTERNAL_RETRIES,
815
890
  delayMs: DSL_EXTERNAL_RETRY_DELAY_MS,
816
891
  label: "getOrderStatus (handoff live-SL check)",
817
892
  });
818
- if (status?.status != null && LIVE_VENUE_SL_STATUSES.has(status.status))
819
- return true;
893
+ const stamp = backendSlStatusStamp(status);
894
+ if (status?.status != null && LIVE_VENUE_SL_STATUSES.has(status.status)) {
895
+ return { live: true, ...(stamp === undefined ? {} : { stamp }) };
896
+ }
820
897
  logger.warn("[dsl] backend ratchet names an exchange SL that is not resting on the venue", {
821
898
  tickId,
822
899
  asset,
@@ -824,7 +901,7 @@ async function backendHasLiveSl(pos, strategy, tickId, asset) {
824
901
  slOrderId,
825
902
  venueStatus: status?.status ?? null,
826
903
  });
827
- return false;
904
+ return { live: false, ...(stamp === undefined ? {} : { stamp }) };
828
905
  }
829
906
  catch (e) {
830
907
  logger.warn("[dsl] could not read the backend ratchet's exchange SL status; treating it as not live", {
@@ -834,7 +911,36 @@ async function backendHasLiveSl(pos, strategy, tickId, asset) {
834
911
  slOrderId,
835
912
  err: e,
836
913
  });
837
- return false;
914
+ return { live: false };
915
+ }
916
+ }
917
+ /**
918
+ * Persist the venue's answer about the backend's stop on a path that writes nothing else.
919
+ *
920
+ * That path is the one worth recording: the checks above leave the handoff at `"pending"` and
921
+ * return, so without this the reason it is stuck exists only in a warn line. A stamp held in a local
922
+ * copy would be dropped by the next write, which merges onto the re-read state, so it is written
923
+ * under the key lock like every other field.
924
+ *
925
+ * No stamp means the venue named no status — a failed lookup, or a ratchet with no order id to ask
926
+ * about. Nothing is written for those: an absent field has to keep meaning "the venue has not told
927
+ * us".
928
+ */
929
+ async function recordBackendSlStatus(ctx, key, state, stamp) {
930
+ if (stamp === undefined)
931
+ return;
932
+ try {
933
+ await setIfStillActive(ctx.stateManager, key, state.address, state.asset, (current) => ({ ...current, ...stamp }), { tickId: ctx.tickId, skipMessage: "[dsl] skip backend SL status stamp: state already cleared (concurrent close)" });
934
+ Object.assign(state, stamp);
935
+ }
936
+ catch (e) {
937
+ // One caller sits inside the pre-check's try, whose catch falls through to sending an add. A
938
+ // failed write of an observability field must not be the reason a second ratchet is registered.
939
+ logger.warn("[dsl] could not persist the backend SL venue status", {
940
+ tickId: ctx.tickId,
941
+ asset: state.asset,
942
+ err: e,
943
+ });
838
944
  }
839
945
  }
840
946
  async function attemptHandoffRegistration(ctx, key, next, _original, strategy) {
@@ -851,8 +957,10 @@ async function attemptHandoffRegistration(ctx, key, next, _original, strategy) {
851
957
  const active = existing.filter((p) => !BACKEND_TERMINAL_STATUSES.has(p.status));
852
958
  if (active.length > 0) {
853
959
  const adopted = active[0];
854
- if (!(await backendHasLiveSl(adopted, strategy, tickId, next.asset))) {
960
+ const adoptedSlCheck = await backendHasLiveSl(adopted, strategy, tickId, next.asset);
961
+ if (!adoptedSlCheck.live) {
855
962
  logger.warn("[dsl] pre-check found backend ratchet without an exchange SL; staying pending so the runtime keeps managing the SL", { tickId, asset: next.asset, adoptedId: adopted.id, status: adopted.status });
963
+ await recordBackendSlStatus(ctx, key, next, adoptedSlCheck.stamp);
856
964
  return;
857
965
  }
858
966
  logger.info("[dsl] pre-check found existing backend ratchet, adopting instead of re-adding", {
@@ -871,6 +979,7 @@ async function attemptHandoffRegistration(ctx, key, next, _original, strategy) {
871
979
  const adoptedSl = mapEnginePositionToStateFields(adopted);
872
980
  const updated = {
873
981
  ...next,
982
+ ...adoptedSlCheck.stamp,
874
983
  slOrderId: adoptedSl.slOrderId,
875
984
  lastSyncedFloorPrice: adoptedSl.lastSyncedFloorPrice,
876
985
  handoffStatus: "ok",
@@ -922,7 +1031,8 @@ async function attemptHandoffRegistration(ctx, key, next, _original, strategy) {
922
1031
  Object.assign(next, aborted);
923
1032
  return;
924
1033
  }
925
- if (!(await backendHasLiveSl(result.position, strategy, tickId, next.asset))) {
1034
+ const registeredSlCheck = await backendHasLiveSl(result.position, strategy, tickId, next.asset);
1035
+ if (!registeredSlCheck.live) {
926
1036
  logger.warn("[dsl] backend created the position but has not placed an exchange SL; staying pending so the runtime keeps managing the SL", {
927
1037
  tickId,
928
1038
  asset: next.asset,
@@ -930,12 +1040,14 @@ async function attemptHandoffRegistration(ctx, key, next, _original, strategy) {
930
1040
  status: result.position.status,
931
1041
  rawResult: JSON.stringify(result).slice(0, 1000),
932
1042
  });
1043
+ await recordBackendSlStatus(ctx, key, next, registeredSlCheck.stamp);
933
1044
  return;
934
1045
  }
935
1046
  // Mirror the backend's stop on the registration tick itself — see the adopt site above.
936
1047
  const registeredSl = mapEnginePositionToStateFields(result.position);
937
1048
  const updated = {
938
1049
  ...next,
1050
+ ...registeredSlCheck.stamp,
939
1051
  slOrderId: registeredSl.slOrderId,
940
1052
  lastSyncedFloorPrice: registeredSl.lastSyncedFloorPrice,
941
1053
  handoffStatus: "ok",
@@ -952,7 +1064,7 @@ async function attemptHandoffRegistration(ctx, key, next, _original, strategy) {
952
1064
  });
953
1065
  }
954
1066
  else {
955
- emitEvent(logger, () => buildHandoffFailedEvent(failureContext(next), {
1067
+ emitEvent(logger, () => buildHandoffFailedEvent(failureContext(next, tickId), {
956
1068
  failure: "register",
957
1069
  redact: { result: JSON.stringify(result).slice(0, 800) },
958
1070
  }));
@@ -960,7 +1072,7 @@ async function attemptHandoffRegistration(ctx, key, next, _original, strategy) {
960
1072
  }
961
1073
  }
962
1074
  catch (e) {
963
- emitEvent(logger, () => buildHandoffFailedEvent(failureContext(next), { failure: "register", error: e }));
1075
+ emitEvent(logger, () => buildHandoffFailedEvent(failureContext(next, tickId), { failure: "register", error: e }));
964
1076
  recordDslDegradation({ failure: "handoff", handoffFailure: "register", errorType: errorTypeOf(e) });
965
1077
  }
966
1078
  }
@@ -983,7 +1095,7 @@ async function handleHandoffOk(ctx, key, next, original, strategy, span) {
983
1095
  ? positions.filter((p) => !BACKEND_TERMINAL_STATUSES.has(p.status))[0]
984
1096
  : null;
985
1097
  if (!backendPos) {
986
- emitEvent(logger, () => buildHandoffFailedEvent(failureContext(original), {
1098
+ emitEvent(logger, () => buildHandoffFailedEvent(failureContext(original, tickId), {
987
1099
  failure: "rollback",
988
1100
  backendPositionId: next.backendDslPositionId ?? undefined,
989
1101
  }));
@@ -1023,13 +1135,15 @@ async function handleHandoffOk(ctx, key, next, original, strategy, span) {
1023
1135
  reason: closeReason,
1024
1136
  closeReason,
1025
1137
  // Backend-driven close: the closing order id is not locally verifiable — stamp ids only.
1026
- ...closedEmitStamps(updated),
1138
+ ...closedEmitStamps(updated, tickId),
1027
1139
  timestamp: ts,
1028
1140
  });
1029
1141
  bus.emit(DSL_EVENTS.CLOSED, buildDslClosedEventFromState(closedState, {
1030
1142
  reason: closeReason,
1031
1143
  closeReason,
1032
1144
  timestamp: ts,
1145
+ tickId,
1146
+ source: "backend",
1033
1147
  }));
1034
1148
  return;
1035
1149
  }
@@ -1038,7 +1152,7 @@ async function handleHandoffOk(ctx, key, next, original, strategy, span) {
1038
1152
  }
1039
1153
  }
1040
1154
  catch (e) {
1041
- emitEvent(logger, () => buildHandoffFailedEvent(failureContext(original), {
1155
+ emitEvent(logger, () => buildHandoffFailedEvent(failureContext(original, tickId), {
1042
1156
  failure: "poll",
1043
1157
  backendPositionId: next.backendDslPositionId ?? undefined,
1044
1158
  error: e,
@@ -1108,11 +1222,20 @@ async function handleHandoffOk(ctx, key, next, original, strategy, span) {
1108
1222
  const minClosed = mapped.eventName === DSL_EVENTS.CLOSED
1109
1223
  ? mapped.payload
1110
1224
  : null;
1225
+ if (minClosed == null) {
1226
+ // Single-owner payload: mapBackendEventToBusEvent built this object for this emit and
1227
+ // nothing else holds a reference to it, so the tick stamp goes on in place — cloning
1228
+ // it once per backend event buys nothing.
1229
+ mapped.payload[PAYLOAD_KEY.tickId] = tickId;
1230
+ }
1111
1231
  const emitPayload = minClosed != null
1112
1232
  ? buildDslClosedEventFromState(updated, {
1113
1233
  reason: minClosed.reason,
1114
1234
  closeReason: minClosed.closeReason,
1115
1235
  timestamp: minClosed.timestamp,
1236
+ tickId,
1237
+ source: "backend",
1238
+ backendEventAt: minClosed.__backendEventAt,
1116
1239
  })
1117
1240
  : mapped.payload;
1118
1241
  bus.emit(mapped.eventName, emitPayload);
@@ -1159,7 +1282,7 @@ async function handleHandoffOk(ctx, key, next, original, strategy, span) {
1159
1282
  reason: closeReason,
1160
1283
  closeReason,
1161
1284
  // Backend-driven close: the closing order id is not locally verifiable — stamp ids only.
1162
- ...closedEmitStamps(updated),
1285
+ ...closedEmitStamps(updated, tickId),
1163
1286
  timestamp: ts,
1164
1287
  });
1165
1288
  return;