@wrongstack/core 0.313.0 → 0.316.1

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 (343) hide show
  1. package/dist/chronicle/index.js +84 -6391
  2. package/dist/chronicle/legacy-journal-import.d.ts +0 -6
  3. package/dist/chronicle/metrics-ingest.d.ts +14 -0
  4. package/dist/chronicle/metrics-schema.d.ts +10 -1
  5. package/dist/chronicle/metrics-store.d.ts +20 -2
  6. package/dist/chronicle/project-server.js +98 -4874
  7. package/dist/chronicle/sink.d.ts +13 -0
  8. package/dist/chronicle/sqlite-journal.d.ts +20 -2
  9. package/dist/chunk-25LSLC3G.js +334 -0
  10. package/dist/chunk-27QIURLN.js +1073 -0
  11. package/dist/chunk-2FK4WTFF.js +207 -0
  12. package/dist/chunk-2YU4WVBW.js +134 -0
  13. package/dist/chunk-3AGWVSS7.js +4608 -0
  14. package/dist/chunk-3BDV7RL3.js +211 -0
  15. package/dist/chunk-3ECENVHL.js +325 -0
  16. package/dist/chunk-3XPVB7BA.js +135 -0
  17. package/dist/chunk-45WI72ZB.js +16884 -0
  18. package/dist/chunk-4BMVPOHO.js +3748 -0
  19. package/dist/chunk-4I7HGVVA.js +363 -0
  20. package/dist/chunk-4LCZVJVF.js +317 -0
  21. package/dist/chunk-4SJNS7OB.js +217 -0
  22. package/dist/chunk-4VWO3ZXL.js +342 -0
  23. package/dist/chunk-57GEWK5X.js +26 -0
  24. package/dist/chunk-5BMIE7QG.js +46 -0
  25. package/dist/chunk-5S437PRC.js +50 -0
  26. package/dist/chunk-5XDUGTKN.js +511 -0
  27. package/dist/chunk-5YPSXQXM.js +1443 -0
  28. package/dist/chunk-6FHWXLPT.js +10 -0
  29. package/dist/chunk-6H5GEOET.js +2406 -0
  30. package/dist/chunk-6NWEVFEL.js +151 -0
  31. package/dist/chunk-AYW5YINX.js +58 -0
  32. package/dist/chunk-B4AMLZPB.js +9 -0
  33. package/dist/chunk-B7F4WVP4.js +65 -0
  34. package/dist/chunk-BJHVULHO.js +55 -0
  35. package/dist/chunk-BPBKKBUK.js +2085 -0
  36. package/dist/chunk-BYD345OX.js +589 -0
  37. package/dist/chunk-C27KDLOX.js +279 -0
  38. package/dist/chunk-CU546ESW.js +597 -0
  39. package/dist/chunk-DLHHXUAM.js +172 -0
  40. package/dist/chunk-DOTHQSJ2.js +429 -0
  41. package/dist/chunk-DYLPMV4Q.js +711 -0
  42. package/dist/chunk-EODDVJBM.js +82 -0
  43. package/dist/chunk-F3JWNST7.js +361 -0
  44. package/dist/chunk-FC2ZGRF3.js +140 -0
  45. package/dist/chunk-FCBTO76Z.js +5786 -0
  46. package/dist/chunk-FHZVFYSL.js +396 -0
  47. package/dist/chunk-FNXVT7HF.js +52 -0
  48. package/dist/chunk-FWNLEZYG.js +259 -0
  49. package/dist/chunk-FXQLIX3K.js +211 -0
  50. package/dist/chunk-G5S3S5ZN.js +38 -0
  51. package/dist/chunk-GDQ7CZRQ.js +116 -0
  52. package/dist/chunk-GJJGJB2R.js +1374 -0
  53. package/dist/chunk-GTCU6DZ3.js +1 -0
  54. package/dist/chunk-HA4ZYA6T.js +178 -0
  55. package/dist/chunk-HI3JGZR2.js +188 -0
  56. package/dist/chunk-HMLIGAU7.js +395 -0
  57. package/dist/chunk-HXKZ7OBQ.js +303 -0
  58. package/dist/chunk-I7DALG7J.js +292 -0
  59. package/dist/chunk-IRUTEHCE.js +960 -0
  60. package/dist/chunk-IYFBFX34.js +86 -0
  61. package/dist/chunk-J2WRWR37.js +1104 -0
  62. package/dist/chunk-JFPJ6LLQ.js +129 -0
  63. package/dist/chunk-JMULFFLN.js +9 -0
  64. package/dist/chunk-JO4MGB6Z.js +1264 -0
  65. package/dist/chunk-JVKB2TBL.js +48 -0
  66. package/dist/chunk-JX7VBZDK.js +31 -0
  67. package/dist/chunk-K6BXGITA.js +931 -0
  68. package/dist/chunk-KDQJCT2J.js +45 -0
  69. package/dist/chunk-KJ2ZTQSL.js +47 -0
  70. package/dist/chunk-KZMSZV32.js +187 -0
  71. package/dist/chunk-LA7CLDRW.js +129 -0
  72. package/dist/chunk-LHOA5DWA.js +28 -0
  73. package/dist/chunk-LSMMOS2Z.js +119 -0
  74. package/dist/chunk-LX5QWLBL.js +27 -0
  75. package/dist/chunk-LXWCMQNJ.js +789 -0
  76. package/dist/chunk-M27LIEKV.js +2829 -0
  77. package/dist/chunk-MJPHJRAL.js +663 -0
  78. package/dist/chunk-MTL4O5YR.js +228 -0
  79. package/dist/chunk-NDBIPFNY.js +102 -0
  80. package/dist/chunk-NEMIWEXC.js +1199 -0
  81. package/dist/chunk-NHJ75HXY.js +1 -0
  82. package/dist/chunk-NIOAGFPB.js +1510 -0
  83. package/dist/chunk-NS52ISWS.js +355 -0
  84. package/dist/chunk-NSOFPKGT.js +5352 -0
  85. package/dist/chunk-NVIZG63R.js +757 -0
  86. package/dist/chunk-O37BQ65S.js +21 -0
  87. package/dist/chunk-OOYZPOXM.js +308 -0
  88. package/dist/chunk-OSGLYW5J.js +28 -0
  89. package/dist/chunk-PFCMSSUF.js +1640 -0
  90. package/dist/chunk-PFZO3C6H.js +14 -0
  91. package/dist/chunk-PGRVXIMY.js +158 -0
  92. package/dist/chunk-PHH7LD45.js +113 -0
  93. package/dist/chunk-PISBFBJN.js +109 -0
  94. package/dist/chunk-POOOIWE5.js +9 -0
  95. package/dist/chunk-PQNS3U7Z.js +1240 -0
  96. package/dist/chunk-QSTSJOCK.js +402 -0
  97. package/dist/chunk-RFQMOKWK.js +282 -0
  98. package/dist/chunk-RJIZEJ42.js +30 -0
  99. package/dist/chunk-RTOTNK3P.js +484 -0
  100. package/dist/chunk-RXKSDUAU.js +1172 -0
  101. package/dist/chunk-S45JPKQN.js +3317 -0
  102. package/dist/chunk-SAFHACFA.js +152 -0
  103. package/dist/chunk-SQJVFEBV.js +41 -0
  104. package/dist/chunk-SVT4RNTR.js +49 -0
  105. package/dist/chunk-SWUFN7XM.js +43 -0
  106. package/dist/chunk-SXMVYC5H.js +174 -0
  107. package/dist/chunk-SZ6AYUIZ.js +23 -0
  108. package/dist/chunk-TBRL2RCC.js +9 -0
  109. package/dist/chunk-TCCHINEF.js +655 -0
  110. package/dist/chunk-THEGTRAG.js +316 -0
  111. package/dist/chunk-TR4LQE7L.js +2256 -0
  112. package/dist/chunk-TRT2HRWZ.js +18 -0
  113. package/dist/chunk-TWJMGXCI.js +900 -0
  114. package/dist/chunk-U45AXT5D.js +1023 -0
  115. package/dist/chunk-U4FWBSO3.js +509 -0
  116. package/dist/chunk-UBGDX6FI.js +147 -0
  117. package/dist/chunk-UJILKYES.js +75 -0
  118. package/dist/chunk-ULMAQV3T.js +234 -0
  119. package/dist/chunk-VVBP6KGT.js +9 -0
  120. package/dist/chunk-W5VITDM2.js +168 -0
  121. package/dist/chunk-WAHQVNWT.js +194 -0
  122. package/dist/chunk-WBPVKEEE.js +227 -0
  123. package/dist/chunk-WGGYRBXH.js +5434 -0
  124. package/dist/chunk-WLYKU45P.js +5770 -0
  125. package/dist/chunk-WP7BVANJ.js +53 -0
  126. package/dist/chunk-WQQON3SV.js +473 -0
  127. package/dist/chunk-WQTOVMGV.js +119 -0
  128. package/dist/chunk-XBDP4QPW.js +7 -0
  129. package/dist/chunk-XEBHSJLT.js +103 -0
  130. package/dist/chunk-XF4VUMKW.js +231 -0
  131. package/dist/chunk-XQ2ECNK3.js +1 -0
  132. package/dist/chunk-Y5YXDM7D.js +74 -0
  133. package/dist/chunk-YOPAUVU4.js +2588 -0
  134. package/dist/chunk-YPT4A46F.js +362 -0
  135. package/dist/chunk-YWXT3JNM.js +425 -0
  136. package/dist/chunk-YY6APYUM.js +49 -0
  137. package/dist/chunk-Z2IJUO45.js +691 -0
  138. package/dist/chunk-ZHZ2Y2DH.js +1643 -0
  139. package/dist/chunk-ZT7JNIGX.js +152 -0
  140. package/dist/coordination/agent-monitor.d.ts +9 -1
  141. package/dist/coordination/agent-subagent-runner.d.ts +1 -1
  142. package/dist/coordination/agents/agent-prompts.d.ts +8 -0
  143. package/dist/coordination/agents/index.d.ts +33 -1
  144. package/dist/coordination/agents/index.js +184 -6269
  145. package/dist/coordination/agents/phase3-wave1-platform.d.ts +1 -3
  146. package/dist/coordination/agents/phase3-wave2-meta.d.ts +1 -3
  147. package/dist/coordination/agents/phase8-wave3-products.d.ts +1 -3
  148. package/dist/coordination/agents/phase9-wave4-platform-meta.d.ts +1 -3
  149. package/dist/coordination/agents/project-agent-identity.d.ts +11 -11
  150. package/dist/coordination/agents/types.d.ts +13 -2
  151. package/dist/coordination/brain-monitor.d.ts +30 -7
  152. package/dist/coordination/collab-debug.d.ts +1 -1
  153. package/dist/coordination/collab-pause.d.ts +1 -1
  154. package/dist/coordination/delegate-tool.d.ts +7 -5
  155. package/dist/coordination/director-kanban-queue-helpers.d.ts +1 -0
  156. package/dist/coordination/director-options.d.ts +7 -0
  157. package/dist/coordination/director-spawn-model.d.ts +17 -0
  158. package/dist/coordination/director-tools.d.ts +2 -2
  159. package/dist/coordination/director.d.ts +12 -0
  160. package/dist/coordination/dispatcher.d.ts +8 -0
  161. package/dist/coordination/fleet-bus.d.ts +35 -4
  162. package/dist/coordination/fleet-supervisor.d.ts +10 -2
  163. package/dist/coordination/fleet.d.ts +16 -7
  164. package/dist/coordination/index.d.ts +4 -0
  165. package/dist/coordination/index.js +402 -34324
  166. package/dist/coordination/kanban-dispatch-port.d.ts +2 -2
  167. package/dist/coordination/mailbox-http-router.d.ts +4 -3
  168. package/dist/coordination/mailbox-http-validation.d.ts +1 -1
  169. package/dist/coordination/mailbox-project-server.js +48 -3276
  170. package/dist/coordination/mailbox-types.d.ts +24 -6
  171. package/dist/coordination/model-tier-leader.d.ts +122 -0
  172. package/dist/coordination/model-tier.d.ts +122 -0
  173. package/dist/coordination/multi-agent-coordinator.d.ts +24 -0
  174. package/dist/coordination/origin-session.d.ts +16 -0
  175. package/dist/coordination/provider-status-tracker.d.ts +41 -5
  176. package/dist/coordination/session-note-hub.d.ts +44 -0
  177. package/dist/coordination/session-note-tool.d.ts +10 -0
  178. package/dist/core/agent.d.ts +15 -2
  179. package/dist/core/context.d.ts +22 -3
  180. package/dist/core/index.d.ts +2 -0
  181. package/dist/core/index.js +239 -12360
  182. package/dist/core/model-ref.js +5 -23
  183. package/dist/core/provider-runner.d.ts +36 -1
  184. package/dist/core/request-conversation-binding.d.ts +37 -0
  185. package/dist/core/session-notes.d.ts +25 -0
  186. package/dist/core/system-prompt-builder.d.ts +11 -2
  187. package/dist/core/system-prompt-variants.d.ts +78 -0
  188. package/dist/design/index.js +50 -1087
  189. package/dist/execution/auto-compaction-middleware.d.ts +11 -0
  190. package/dist/execution/autonomy-brain.d.ts +2 -1
  191. package/dist/execution/autonomy-prompt-contributor.d.ts +7 -2
  192. package/dist/execution/compaction-core.d.ts +4 -4
  193. package/dist/execution/eternal-autonomy.d.ts +1 -1
  194. package/dist/execution/index.d.ts +1 -1
  195. package/dist/execution/index.js +1615 -21895
  196. package/dist/execution/model-runtime.d.ts +0 -7
  197. package/dist/execution/parallel-eternal-engine.d.ts +7 -7
  198. package/dist/execution/prompt-enhancer.js +16 -535
  199. package/dist/execution/retry-policy.d.ts +31 -1
  200. package/dist/extension/index.js +7 -311
  201. package/dist/goal/index.js +33 -3435
  202. package/dist/hooks/index.js +14 -1138
  203. package/dist/hq/index.js +114 -5139
  204. package/dist/hq/persistence/event-log.d.ts +0 -1
  205. package/dist/hq/persistence.d.ts +4 -4
  206. package/dist/hq/protocol.js +21 -668
  207. package/dist/hq/publisher.d.ts +2 -0
  208. package/dist/index.d.ts +14 -11
  209. package/dist/index.js +1502 -92655
  210. package/dist/infrastructure/index.d.ts +4 -4
  211. package/dist/infrastructure/index.js +504 -5230
  212. package/dist/infrastructure/token-counter.d.ts +9 -0
  213. package/dist/kernel/events/agent-events.d.ts +77 -3
  214. package/dist/kernel/events/brain-events.d.ts +12 -0
  215. package/dist/kernel/events/provider-events.d.ts +22 -1
  216. package/dist/kernel/events/session-events.d.ts +14 -0
  217. package/dist/kernel/events/tool-events.d.ts +13 -0
  218. package/dist/kernel/index.js +14 -992
  219. package/dist/mailbox-attach.d.ts +1 -1
  220. package/dist/models/index.js +41 -1117
  221. package/dist/notifications/index.js +3 -108
  222. package/dist/observability/event-bridge.d.ts +25 -5
  223. package/dist/observability/index.d.ts +9 -8
  224. package/dist/observability/index.js +477 -631
  225. package/dist/observability/tool-usage-source.d.ts +65 -0
  226. package/dist/plugin/index.d.ts +5 -0
  227. package/dist/plugin/index.js +600 -14835
  228. package/dist/plugins/auto-review-plugin.d.ts +3 -2
  229. package/dist/plugins/review-finding-commands.d.ts +0 -8
  230. package/dist/plugins/review-finding-store.js +22 -0
  231. package/dist/plugins/review-finding-types.js +19 -0
  232. package/dist/plugins/review-report-integration.js +21 -0
  233. package/dist/plugins/review-report-store.js +20 -0
  234. package/dist/plugins/review-report-types.js +13 -0
  235. package/dist/prompts/index.js +14 -948
  236. package/dist/registry/index.js +9 -822
  237. package/dist/registry/tool-registry.d.ts +59 -9
  238. package/dist/registry-7N4O3E7W.js +14 -0
  239. package/dist/replay/index.js +6 -84
  240. package/dist/security/capabilities.d.ts +6 -0
  241. package/dist/security/file-permissions.d.ts +1 -1
  242. package/dist/security/index.js +161 -2235
  243. package/dist/security/permission-policy.d.ts +12 -1
  244. package/dist/security/secret-scrubber.d.ts +15 -0
  245. package/dist/session-catalog/index.d.ts +1 -0
  246. package/dist/session-catalog/index.js +41 -2103
  247. package/dist/session-catalog/project-server.js +59 -1435
  248. package/dist/session-catalog/protocol.d.ts +32 -1
  249. package/dist/session-catalog/registry.d.ts +17 -1
  250. package/dist/session-catalog/session-agents.d.ts +56 -0
  251. package/dist/session-catalog/store-rebuild.d.ts +14 -0
  252. package/dist/session-catalog/store-schema.d.ts +26 -0
  253. package/dist/session-catalog/store.d.ts +27 -1
  254. package/dist/session-note-attach.d.ts +9 -0
  255. package/dist/skills/index.js +35 -1578
  256. package/dist/statusline/index.d.ts +43 -0
  257. package/dist/statusline/index.js +102 -0
  258. package/dist/storage/board-store-port.d.ts +2 -2
  259. package/dist/storage/config-loader/in-project-policy.d.ts +29 -0
  260. package/dist/storage/config-loader.d.ts +3 -2
  261. package/dist/storage/file-session-writer.d.ts +4 -2
  262. package/dist/storage/goal-coordination.d.ts +6 -1
  263. package/dist/storage/goal-kanban.d.ts +4 -1
  264. package/dist/storage/index.d.ts +12 -4
  265. package/dist/storage/index.js +198 -15601
  266. package/dist/storage/input-history-store.d.ts +2 -4
  267. package/dist/storage/memory-curator.d.ts +133 -0
  268. package/dist/storage/session-agent-attribution.d.ts +39 -0
  269. package/dist/storage/session-doctor.d.ts +109 -0
  270. package/dist/storage/session-event-bridge.d.ts +5 -7
  271. package/dist/storage/session-outcome.d.ts +42 -0
  272. package/dist/storage/session-recovery.d.ts +55 -0
  273. package/dist/storage/session-store/load-session-data.d.ts +11 -0
  274. package/dist/storage/session-store/prune-helpers.d.ts +1 -1
  275. package/dist/storage/session-store/resume-session.d.ts +0 -2
  276. package/dist/storage/session-store/types.d.ts +8 -0
  277. package/dist/storage/session-store.d.ts +3 -3
  278. package/dist/storage/session-summary-tracker.d.ts +8 -1
  279. package/dist/storage/session-write-buffer.d.ts +18 -0
  280. package/dist/storage/session-writer-truncate.d.ts +2 -0
  281. package/dist/tasking/index.js +10 -676
  282. package/dist/tools/fallback-manage-tools.d.ts +6 -4
  283. package/dist/tools/index.js +52 -9590
  284. package/dist/tools/model-tier-set-tool.d.ts +66 -0
  285. package/dist/types/agent-bridge.d.ts +2 -1
  286. package/dist/types/config/autonomy.d.ts +5 -0
  287. package/dist/types/config/mcp-features.d.ts +7 -0
  288. package/dist/types/config/model-tiers.d.ts +148 -0
  289. package/dist/types/config/root.d.ts +9 -0
  290. package/dist/types/config/tools.d.ts +73 -0
  291. package/dist/types/config/ui.d.ts +1 -1
  292. package/dist/types/config.d.ts +1 -0
  293. package/dist/types/context.d.ts +2 -0
  294. package/dist/types/default-config.d.ts +6 -0
  295. package/dist/types/errors.d.ts +20 -1
  296. package/dist/types/index.d.ts +11 -9
  297. package/dist/types/index.js +131 -1342
  298. package/dist/types/multi-agent.d.ts +43 -0
  299. package/dist/types/plugin.d.ts +1 -1
  300. package/dist/types/provider-runner.d.ts +8 -0
  301. package/dist/types/provider.d.ts +15 -0
  302. package/dist/types/runtime-capability-manifest.d.ts +1 -1
  303. package/dist/types/secret-scrubber.d.ts +15 -0
  304. package/dist/types/session-markers.d.ts +37 -10
  305. package/dist/types/session-markers.js +8 -144
  306. package/dist/types/session-timeline.d.ts +197 -0
  307. package/dist/types/session-timeline.js +12 -0
  308. package/dist/types/session.d.ts +172 -11
  309. package/dist/types/system-prompt.d.ts +27 -0
  310. package/dist/utils/config-backup.d.ts +9 -1
  311. package/dist/utils/error.js +3 -4
  312. package/dist/utils/expect-defined.js +3 -9
  313. package/dist/utils/heap-watchdog.js +6 -706
  314. package/dist/utils/index.js +271 -5685
  315. package/dist/utils/message-invariants.d.ts +4 -1
  316. package/dist/utils/regex-guard.d.ts +1 -1
  317. package/dist/utils/sage-output-block.js +5 -42
  318. package/dist/utils/socket-path.d.ts +1 -1
  319. package/dist/utils/token-estimate.d.ts +6 -2
  320. package/dist/utils/wstack-paths.d.ts +2 -0
  321. package/dist/wiring/proxy-rewrite.d.ts +46 -0
  322. package/dist/wiring/proxy-rewrite.js +24 -121
  323. package/dist/worktree/index.js +6 -887
  324. package/instructions/agents/explore-companion.md +45 -18
  325. package/instructions/agents/memory-curator.md +34 -22
  326. package/instructions/coordination/director-preamble.md +12 -7
  327. package/instructions/coordination/subagent-baseline.md +26 -5
  328. package/instructions/leader-after-task.md +4 -1
  329. package/instructions/llm/chimera-review.md +30 -9
  330. package/instructions/llm/memory-curator.md +31 -0
  331. package/instructions/sections/tool/delegation-compact.md +2 -0
  332. package/instructions/sections/tool/delegation-full.md +5 -1
  333. package/instructions/sections/tool/mailbox-compact.md +5 -1
  334. package/instructions/sections/tool/mailbox-full.md +3 -0
  335. package/instructions/sections/tool/session-note-compact.md +3 -0
  336. package/instructions/sections/tool/session-note-full.md +15 -0
  337. package/instructions/system-lite.md +9 -4
  338. package/instructions/system-pro.md +66 -143
  339. package/instructions/system.md +73 -175
  340. package/package.json +32 -4
  341. package/dist/coordination/agents.d.ts +0 -8
  342. package/dist/coordination/transport.d.ts +0 -2
  343. package/dist/storage/session-writer/session-writer-summary-tracker.d.ts +0 -28
@@ -1,710 +1,10 @@
1
- // src/utils/heap-watchdog.ts
2
- import * as fs from "node:fs";
3
- import * as fsp2 from "node:fs/promises";
4
- import * as path3 from "node:path";
5
1
  import {
6
- PerformanceObserver,
7
- constants as performanceConstants
8
- } from "node:perf_hooks";
9
- import * as v8 from "node:v8";
10
-
11
- // src/utils/memory-flight-recorder.ts
12
- import * as fsp from "node:fs/promises";
13
- import * as path2 from "node:path";
14
-
15
- // src/utils/continuous-memory-profiler.ts
16
- import { Session } from "node:inspector/promises";
17
- var DEFAULT_SAMPLE_INTERVAL_BYTES = 512 * 1024;
18
- var DEFAULT_STACK_DEPTH = 64;
19
- var MAX_SUMMARY_STACKS = 20;
20
- var MAX_FRAMES_PER_STACK = 12;
21
- function errorMessage(error) {
22
- return error instanceof Error ? error.message : String(error);
23
- }
24
- function numeric(value) {
25
- if (typeof value === "bigint") return Number(value);
26
- return typeof value === "number" && Number.isFinite(value) ? value : 0;
27
- }
28
- function summarizePprof(profile) {
29
- const strings = profile.stringTable.strings;
30
- const functions = new Map(profile.function.map((entry) => [numeric(entry.id), entry]));
31
- const locations = new Map(profile.location.map((entry) => [numeric(entry.id), entry]));
32
- const stacks = /* @__PURE__ */ new Map();
33
- let sampledBytes = 0;
34
- let sampledObjects = 0;
35
- for (const sample of profile.sample) {
36
- const objects = numeric(sample.value[0]);
37
- const bytes = numeric(sample.value[1]);
38
- sampledObjects += objects;
39
- sampledBytes += bytes;
40
- const frames = sample.locationId.slice(0, MAX_FRAMES_PER_STACK).flatMap((locationId) => {
41
- const location = locations.get(numeric(locationId));
42
- const line = location?.line[0];
43
- const fn = line ? functions.get(numeric(line.functionId)) : void 0;
44
- if (!fn) return [];
45
- const name = strings[numeric(fn.name)] || "(anonymous)";
46
- const filename = strings[numeric(fn.filename)] || "";
47
- const lineNumber = numeric(line?.line);
48
- return [filename ? `${name} (${filename}${lineNumber > 0 ? `:${lineNumber}` : ""})` : name];
49
- });
50
- if (frames.length === 0) frames.push("(unknown)");
51
- const key = frames.join("\n");
52
- const existing = stacks.get(key);
53
- if (existing) {
54
- existing.bytes += bytes;
55
- existing.objects += objects;
56
- } else {
57
- stacks.set(key, { bytes, objects, frames });
58
- }
59
- }
60
- return {
61
- sampledBytes,
62
- sampledObjects,
63
- sampleCount: profile.sample.length,
64
- topStacks: [...stacks.values()].sort((left, right) => right.bytes - left.bytes).slice(0, MAX_SUMMARY_STACKS)
65
- };
66
- }
67
- async function startDatadogProfiler() {
68
- const pprof = await import("@datadog/pprof");
69
- pprof.heap.start(DEFAULT_SAMPLE_INTERVAL_BYTES, DEFAULT_STACK_DEPTH);
70
- let stopped = false;
71
- return {
72
- backend: "datadog-pprof",
73
- capture: async () => {
74
- if (stopped) throw new Error("Continuous memory profiler is stopped");
75
- const profile = pprof.heap.profile();
76
- return {
77
- backend: "datadog-pprof",
78
- data: await pprof.encode(profile),
79
- extension: ".pb.gz",
80
- summary: summarizePprof(profile)
81
- };
82
- },
83
- stop: async () => {
84
- if (stopped) return;
85
- stopped = true;
86
- pprof.heap.stop();
87
- }
88
- };
89
- }
90
- async function startInspectorProfiler() {
91
- const session = new Session();
92
- session.connect();
93
- try {
94
- await session.post("HeapProfiler.enable");
95
- await session.post("HeapProfiler.startSampling", {
96
- samplingInterval: DEFAULT_SAMPLE_INTERVAL_BYTES,
97
- includeObjectsCollectedByMajorGC: false,
98
- includeObjectsCollectedByMinorGC: false
99
- });
100
- } catch (error) {
101
- try {
102
- session.disconnect();
103
- } catch {
104
- }
105
- throw error;
106
- }
107
- let stopped = false;
108
- return {
109
- backend: "node-inspector",
110
- capture: async () => {
111
- if (stopped) throw new Error("Continuous memory profiler is stopped");
112
- const result = await session.post("HeapProfiler.stopSampling");
113
- await session.post("HeapProfiler.startSampling", {
114
- samplingInterval: DEFAULT_SAMPLE_INTERVAL_BYTES,
115
- includeObjectsCollectedByMajorGC: false,
116
- includeObjectsCollectedByMinorGC: false
117
- });
118
- return {
119
- backend: "node-inspector",
120
- data: JSON.stringify(result.profile),
121
- extension: ".heapprofile"
122
- };
123
- },
124
- stop: async () => {
125
- if (stopped) return;
126
- stopped = true;
127
- try {
128
- await session.post("HeapProfiler.disable");
129
- } catch {
130
- }
131
- try {
132
- session.disconnect();
133
- } catch {
134
- }
135
- }
136
- };
137
- }
138
- async function startContinuousMemoryProfiler() {
139
- try {
140
- return { profiler: await startDatadogProfiler() };
141
- } catch (error) {
142
- const fallbackReason = errorMessage(error);
143
- return {
144
- profiler: await startInspectorProfiler(),
145
- fallbackReason
146
- };
147
- }
148
- }
149
-
150
- // src/utils/wstack-paths.ts
151
- import * as os from "node:os";
152
- import * as path from "node:path";
153
- function wstackGlobalRoot() {
154
- const fromEnv = process.env["WRONGSTACK_HOME"];
155
- if (fromEnv && fromEnv.trim().length > 0) return path.resolve(fromEnv);
156
- return path.join(os.homedir(), ".wrongstack");
157
- }
158
-
159
- // src/utils/memory-flight-recorder.ts
160
- var MB = 1024 * 1024;
161
- var HOUR_MS = 60 * 60 * 1e3;
162
- var DEFAULT_WINDOW_MS = 5 * 60 * 1e3;
163
- var DEFAULT_MINIMUM_WINDOW_MS = 2 * 60 * 1e3;
164
- var DEFAULT_COOLDOWN_MS = 15 * 60 * 1e3;
165
- var DEFAULT_CONTINUOUS_CAPTURE_MS = 5 * 60 * 1e3;
166
- var MAX_OBSERVATIONS = 24;
167
- var MAX_CAPTURE_SESSIONS = 12;
168
- var MAX_CAPTURES_PER_SESSION = 6;
169
- var SESSION_MAX_AGE_MS = 7 * 24 * HOUR_MS;
170
- function timestampSlug(date = /* @__PURE__ */ new Date()) {
171
- return date.toISOString().replaceAll(":", "").replaceAll(".", "-");
172
- }
173
- async function pruneCaptureFiles(artifactDir) {
174
- const entries = await fsp.readdir(artifactDir, { withFileTypes: true });
175
- const captures = entries.filter(
176
- (entry) => entry.isFile() && entry.name.startsWith("event-") && entry.name.endsWith(".json")
177
- ).map((entry) => entry.name).sort();
178
- const remove = captures.slice(0, Math.max(0, captures.length - MAX_CAPTURES_PER_SESSION));
179
- await Promise.all(
180
- remove.flatMap((eventName) => {
181
- const stem = eventName.slice("event-".length, -".json".length);
182
- return [
183
- fsp.rm(path2.join(artifactDir, eventName), { force: true }),
184
- fsp.rm(path2.join(artifactDir, `allocations-${stem}.heapprofile`), { force: true }),
185
- fsp.rm(path2.join(artifactDir, `allocations-${stem}.pb.gz`), { force: true })
186
- ];
187
- })
188
- );
189
- }
190
- async function pruneOldSessions(root, currentDir) {
191
- const entries = await fsp.readdir(root, { withFileTypes: true });
192
- const sessions = await Promise.all(
193
- entries.filter((entry) => entry.isDirectory() && entry.name !== path2.basename(currentDir)).map(async (entry) => {
194
- const fullPath = path2.join(root, entry.name);
195
- const stat2 = await fsp.stat(fullPath);
196
- return { fullPath, mtimeMs: stat2.mtimeMs };
197
- })
198
- );
199
- sessions.sort((a, b) => b.mtimeMs - a.mtimeMs);
200
- const now = Date.now();
201
- await Promise.all(
202
- sessions.filter(
203
- (session, index) => now - session.mtimeMs > SESSION_MAX_AGE_MS || index >= MAX_CAPTURE_SESSIONS && now - session.mtimeMs > 24 * HOUR_MS
204
- ).map((session) => fsp.rm(session.fullPath, { recursive: true, force: true }))
205
- );
206
- }
207
- var ContinuousMemoryCaptureWriter = class {
208
- artifactDir;
209
- root;
210
- ready;
211
- profiler;
212
- profilerBackend = "initializing";
213
- fallbackReason;
214
- startError;
215
- lastSummary;
216
- stopped = false;
217
- constructor(root) {
218
- this.root = root;
219
- this.artifactDir = path2.join(root, `${timestampSlug()}-pid-${process.pid}`);
220
- this.ready = this.start().catch((error) => {
221
- this.startError = error instanceof Error ? error.message : String(error);
222
- this.profilerBackend = "unavailable";
223
- });
224
- }
225
- async start() {
226
- const started = await startContinuousMemoryProfiler();
227
- this.profiler = started.profiler;
228
- this.profilerBackend = started.profiler.backend;
229
- this.fallbackReason = started.fallbackReason;
230
- }
231
- diagnostics() {
232
- return {
233
- memoryProfilerBackend: this.profilerBackend,
234
- ...this.fallbackReason ? { memoryProfilerFallbackReason: this.fallbackReason } : {},
235
- ...this.startError ? { memoryProfilerStartError: this.startError } : {},
236
- ...this.lastSummary ? {
237
- memoryProfileSampledBytes: this.lastSummary.sampledBytes,
238
- memoryProfileSampledObjects: this.lastSummary.sampledObjects,
239
- memoryProfileSampleCount: this.lastSummary.sampleCount,
240
- memoryProfileTopStack: this.lastSummary.topStack,
241
- memoryProfileTopStackBytes: this.lastSummary.topStackBytes
242
- } : {}
243
- };
244
- }
245
- async capture(event) {
246
- await this.ready;
247
- if (this.stopped) return;
248
- let profile;
249
- let captureError = this.startError;
250
- if (!captureError && this.profiler) {
251
- try {
252
- profile = await this.profiler.capture();
253
- const top = profile.summary?.topStacks[0];
254
- if (profile.summary) {
255
- this.lastSummary = {
256
- sampledBytes: profile.summary.sampledBytes,
257
- sampledObjects: profile.summary.sampledObjects,
258
- sampleCount: profile.summary.sampleCount,
259
- topStack: top?.frames.join(" <- "),
260
- topStackBytes: top?.bytes
261
- };
262
- }
263
- } catch (error) {
264
- captureError = error instanceof Error ? error.message : String(error);
265
- }
266
- }
267
- await fsp.mkdir(this.artifactDir, { recursive: true });
268
- const stem = `${timestampSlug(new Date(event.capturedAt))}-${event.reason}`;
269
- await fsp.writeFile(
270
- path2.join(this.artifactDir, `event-${stem}.json`),
271
- `${JSON.stringify(
272
- {
273
- ...event,
274
- profilerBackend: profile?.backend ?? this.profilerBackend,
275
- ...profile?.summary ? { allocationSummary: profile.summary } : {},
276
- ...this.fallbackReason ? { profilerFallbackReason: this.fallbackReason } : {},
277
- ...captureError ? { captureError } : {}
278
- },
279
- null,
280
- 2
281
- )}
282
- `,
283
- "utf8"
284
- );
285
- if (profile) {
286
- await fsp.writeFile(
287
- path2.join(this.artifactDir, `allocations-${stem}${profile.extension}`),
288
- profile.data
289
- );
290
- }
291
- await Promise.all([
292
- pruneCaptureFiles(this.artifactDir),
293
- fsp.mkdir(this.root, { recursive: true }).then(() => pruneOldSessions(this.root, this.artifactDir))
294
- ]);
295
- }
296
- async stop() {
297
- if (this.stopped) return;
298
- this.stopped = true;
299
- await this.ready;
300
- await this.profiler?.stop();
301
- }
302
- };
303
- function disabledWriter(artifactDir) {
304
- return {
305
- artifactDir,
306
- capture: async () => void 0,
307
- stop: async () => void 0
308
- };
309
- }
310
- function createMemoryFlightRecorder(options = {}) {
311
- const now = options.now ?? Date.now;
312
- const startedAt = now();
313
- const root = options.diagnosticsRoot ?? path2.join(wstackGlobalRoot(), "diagnostics", "memory");
314
- const isBunRuntime = typeof globalThis.Bun !== "undefined";
315
- const enabled = options.enabled ?? (!isBunRuntime && process.env["WRONGSTACK_MEMORY_FLIGHT_RECORDER"] !== "0" && process.env["NODE_ENV"] !== "test");
316
- const writer = options.captureWriter ?? (enabled ? new ContinuousMemoryCaptureWriter(root) : disabledWriter(path2.join(root, `${timestampSlug()}-pid-${process.pid}`)));
317
- const observations = [];
318
- const windowMs = options.windowMs ?? DEFAULT_WINDOW_MS;
319
- const minimumWindowMs = options.minimumWindowMs ?? DEFAULT_MINIMUM_WINDOW_MS;
320
- const cooldownMs = options.cooldownMs ?? DEFAULT_COOLDOWN_MS;
321
- const continuousCaptureMs = options.continuousCaptureMs ?? DEFAULT_CONTINUOUS_CAPTURE_MS;
322
- const warmupMs = options.warmupMs ?? 6e4;
323
- let lastSignalCaptureAt = Number.NEGATIVE_INFINITY;
324
- let lastContinuousCaptureAt = startedAt;
325
- let captureInFlight = false;
326
- let capturePromise;
327
- const observe = (sample, stats) => {
328
- const at = now();
329
- const postGcHeapUsed = typeof stats["postGcHeapUsed"] === "number" ? stats["postGcHeapUsed"] : 0;
330
- const postMajorGcHeapUsed = typeof stats["postMajorGcHeapUsed"] === "number" ? stats["postMajorGcHeapUsed"] : 0;
331
- const lastGcAgoMs = typeof stats["lastGcAgoMs"] === "number" ? stats["lastGcAgoMs"] : -1;
332
- const lastMajorGcAgoMs = typeof stats["lastMajorGcAgoMs"] === "number" ? stats["lastMajorGcAgoMs"] : -1;
333
- const retainedHeapUsed = postMajorGcHeapUsed > 0 && lastMajorGcAgoMs >= 0 && lastMajorGcAgoMs <= windowMs ? postMajorGcHeapUsed : postGcHeapUsed > 0 && lastGcAgoMs >= 0 && lastGcAgoMs <= minimumWindowMs ? postGcHeapUsed : sample.heapUsed;
334
- observations.push({
335
- at,
336
- heapUsed: retainedHeapUsed,
337
- rss: sample.rss,
338
- nativeResidual: sample.nativeResidual ?? 0
339
- });
340
- while (observations.length > MAX_OBSERVATIONS || observations.length > 1 && at - (observations[0]?.at ?? at) > windowMs) {
341
- observations.shift();
342
- }
343
- const first = observations[0] ?? observations.at(-1);
344
- const elapsedMs = first ? Math.max(0, at - first.at) : 0;
345
- const heapGrowthBytes = first ? retainedHeapUsed - first.heapUsed : 0;
346
- const rssGrowthBytes = first ? sample.rss - first.rss : 0;
347
- const nativeGrowthBytes = first ? (sample.nativeResidual ?? 0) - first.nativeResidual : 0;
348
- const heapGrowthBytesPerHour = elapsedMs > 0 ? Math.round(heapGrowthBytes / elapsedMs * HOUR_MS) : 0;
349
- const rssGrowthBytesPerHour = elapsedMs > 0 ? Math.round(rssGrowthBytes / elapsedMs * HOUR_MS) : 0;
350
- const nativeGrowthBytesPerHour = elapsedMs > 0 ? Math.round(nativeGrowthBytes / elapsedMs * HOUR_MS) : 0;
351
- let signal = "stable";
352
- if ((sample.detachedContexts ?? 0) > 0) signal = "detached-contexts";
353
- else if (heapGrowthBytes >= 32 * MB && heapGrowthBytesPerHour > 0) signal = "js-retention";
354
- else if (nativeGrowthBytes >= 64 * MB && nativeGrowthBytesPerHour > 0) signal = "native-rss";
355
- const diagnosis = {
356
- memorySignal: signal,
357
- memoryWindowMs: elapsedMs,
358
- retainedHeapUsed,
359
- transientHeapBytes: Math.max(0, sample.heapUsed - retainedHeapUsed),
360
- heapGrowthBytes,
361
- rssGrowthBytes,
362
- nativeGrowthBytes,
363
- heapGrowthBytesPerHour,
364
- rssGrowthBytesPerHour,
365
- nativeGrowthBytesPerHour,
366
- memoryArtifactDir: writer.artifactDir,
367
- ...writer.diagnostics?.() ?? {}
368
- };
369
- let reason;
370
- if (at - startedAt >= warmupMs) {
371
- if ((sample.detachedContexts ?? 0) > 0) reason = "detached-contexts";
372
- else if (sample.load >= 0.45) reason = "heap-load";
373
- else if (sample.heapUsed >= (options.absoluteHeapBytes ?? 768 * MB)) {
374
- reason = "absolute-heap";
375
- } else if (elapsedMs >= minimumWindowMs && heapGrowthBytes >= (options.heapGrowthBytes ?? 32 * MB) && heapGrowthBytesPerHour >= (options.heapGrowthBytesPerHour ?? 192 * MB)) {
376
- reason = "heap-growth";
377
- } else if (elapsedMs >= minimumWindowMs && nativeGrowthBytes >= (options.nativeGrowthBytes ?? 128 * MB) && nativeGrowthBytesPerHour >= (options.nativeGrowthBytesPerHour ?? 256 * MB)) {
378
- reason = "native-rss-growth";
379
- }
380
- }
381
- const signalCaptureDue = reason !== void 0 && at - lastSignalCaptureAt >= cooldownMs;
382
- const intervalCaptureDue = at - lastContinuousCaptureAt >= continuousCaptureMs;
383
- const captureReason = signalCaptureDue ? reason : intervalCaptureDue ? "interval" : void 0;
384
- if (enabled && captureReason && !captureInFlight) {
385
- if (captureReason !== "interval") lastSignalCaptureAt = at;
386
- lastContinuousCaptureAt = at;
387
- captureInFlight = true;
388
- diagnosis["memoryCaptureReason"] = captureReason;
389
- capturePromise = writer.capture({
390
- reason: captureReason,
391
- capturedAt: new Date(at).toISOString(),
392
- pid: process.pid,
393
- sample,
394
- stats,
395
- diagnosis: { ...diagnosis },
396
- observations: observations.map((observation) => ({ ...observation }))
397
- }).catch(() => {
398
- }).finally(() => {
399
- captureInFlight = false;
400
- capturePromise = void 0;
401
- });
402
- }
403
- return diagnosis;
404
- };
405
- return {
406
- artifactDir: writer.artifactDir,
407
- observe,
408
- stop: async () => {
409
- await capturePromise;
410
- await writer.stop();
411
- }
412
- };
413
- }
414
-
415
- // src/utils/heap-watchdog.ts
416
- var MB2 = 1024 * 1024;
417
- function defaultHeapLogPath() {
418
- const override = process.env["WRONGSTACK_HEAP_LOG_PATH"]?.trim();
419
- if (override) return path3.resolve(override);
420
- return path3.join(wstackGlobalRoot(), "logs", "heap.jsonl");
421
- }
422
- function takeHeapSample() {
423
- const m = process.memoryUsage();
424
- const heap = v8.getHeapStatistics();
425
- let spaces = [];
426
- try {
427
- spaces = v8.getHeapSpaceStatistics();
428
- } catch {
429
- }
430
- const spaceUsed = (name) => spaces.find((space) => space.space_name === name)?.space_used_size ?? 0;
431
- const limit = heap.heap_size_limit || 0;
432
- let activeResources = 0;
433
- let activeResourceTypes = "";
434
- try {
435
- const resources = process.getActiveResourcesInfo?.() ?? [];
436
- activeResources = resources.length;
437
- const counts = /* @__PURE__ */ new Map();
438
- for (const resource of resources) counts.set(resource, (counts.get(resource) ?? 0) + 1);
439
- activeResourceTypes = [...counts].sort(([left], [right]) => left.localeCompare(right)).map(([resource, count]) => `${resource}=${count}`).join(",");
440
- } catch {
441
- }
442
- return {
443
- ts: (/* @__PURE__ */ new Date()).toISOString(),
444
- rss: m.rss,
445
- heapUsed: m.heapUsed,
446
- heapTotal: m.heapTotal,
447
- external: m.external,
448
- arrayBuffers: m.arrayBuffers,
449
- nativeResidual: Math.max(0, m.rss - m.heapTotal - m.external),
450
- oldSpaceUsed: spaceUsed("old_space"),
451
- newSpaceUsed: spaceUsed("new_space"),
452
- largeObjectSpaceUsed: spaceUsed("large_object_space"),
453
- codeSpaceUsed: spaceUsed("code_space"),
454
- mallocedMemory: heap.malloced_memory,
455
- peakMallocedMemory: heap.peak_malloced_memory,
456
- nativeContexts: heap.number_of_native_contexts,
457
- detachedContexts: heap.number_of_detached_contexts,
458
- activeResources,
459
- activeResourceTypes,
460
- heapLimit: limit,
461
- load: limit > 0 ? m.heapUsed / limit : 0
462
- };
463
- }
464
- function startHeapWatchdog(opts = {}) {
465
- const profileMode = process.env["WRONGSTACK_MEMORY_PROFILE"] === "1";
466
- const sampleEveryMs = opts.sampleEveryMs ?? (profileMode ? 5e3 : 3e4);
467
- const logEveryMs = opts.logEveryMs ?? (profileMode ? 5e3 : 6e4);
468
- const logPath = opts.logPath ?? defaultHeapLogPath();
469
- const warnAt = opts.warnAt ?? 0.6;
470
- const criticalAt = opts.criticalAt ?? 0.85;
471
- const REARM_MARGIN = 0.05;
472
- let warnArmed = true;
473
- let criticalArmed = true;
474
- let lastLogAt = 0;
475
- let writeInFlight = false;
476
- let pendingLine;
477
- let drainPromise;
478
- let stopped = false;
479
- let dirReady = false;
480
- const flightRecorder = createMemoryFlightRecorder();
481
- let gcMajorCount = 0;
482
- let gcMinorCount = 0;
483
- let gcIncrementalCount = 0;
484
- let gcWeakCallbackCount = 0;
485
- let gcDurationMs = 0;
486
- let postGcHeapUsed = 0;
487
- let postMajorGcHeapUsed = 0;
488
- let lastGcAt = 0;
489
- let lastMajorGcAt = 0;
490
- const gcObserver = new PerformanceObserver((list) => {
491
- for (const entry of list.getEntries()) {
492
- const kind = entry.detail?.kind ?? 0;
493
- if ((kind & performanceConstants.NODE_PERFORMANCE_GC_MAJOR) !== 0) gcMajorCount++;
494
- if ((kind & performanceConstants.NODE_PERFORMANCE_GC_MINOR) !== 0) gcMinorCount++;
495
- if ((kind & performanceConstants.NODE_PERFORMANCE_GC_INCREMENTAL) !== 0) {
496
- gcIncrementalCount++;
497
- }
498
- if ((kind & performanceConstants.NODE_PERFORMANCE_GC_WEAKCB) !== 0) gcWeakCallbackCount++;
499
- gcDurationMs += entry.duration;
500
- postGcHeapUsed = process.memoryUsage().heapUsed;
501
- if ((kind & performanceConstants.NODE_PERFORMANCE_GC_MAJOR) !== 0) {
502
- postMajorGcHeapUsed = postGcHeapUsed;
503
- lastMajorGcAt = Date.now();
504
- }
505
- lastGcAt = Date.now();
506
- }
507
- });
508
- try {
509
- gcObserver.observe({ entryTypes: ["gc"] });
510
- } catch {
511
- }
512
- const collectRuntimeStats = () => ({
513
- gcMajorCount,
514
- gcMinorCount,
515
- gcIncrementalCount,
516
- gcWeakCallbackCount,
517
- gcDurationMs: Math.round(gcDurationMs),
518
- postGcHeapUsed,
519
- postMajorGcHeapUsed,
520
- lastGcAgoMs: lastGcAt > 0 ? Date.now() - lastGcAt : -1,
521
- lastMajorGcAgoMs: lastMajorGcAt > 0 ? Date.now() - lastMajorGcAt : -1
522
- });
523
- const writeDiagnosticLine = opts.writeDiagnosticLine ?? (async (targetPath, line) => {
524
- if (!dirReady) {
525
- await fsp2.mkdir(path3.dirname(targetPath), { recursive: true });
526
- dirReady = true;
527
- }
528
- await fsp2.appendFile(targetPath, `${line}
529
- `, { encoding: "utf8", mode: 384 });
530
- });
531
- const drainWrites = async (firstLine) => {
532
- let line = firstLine;
533
- while (line !== void 0) {
534
- try {
535
- await writeDiagnosticLine(logPath, line);
536
- } catch {
537
- }
538
- line = pendingLine;
539
- pendingLine = void 0;
540
- }
541
- writeInFlight = false;
542
- };
543
- const append = (line) => {
544
- if (stopped) return;
545
- if (writeInFlight) {
546
- pendingLine = line;
547
- return;
548
- }
549
- writeInFlight = true;
550
- drainPromise = drainWrites(line);
551
- };
552
- const appendTerminalSample = (event, error, origin) => {
553
- if (stopped) return;
554
- let extras = {};
555
- try {
556
- extras = opts.collectStats?.() ?? {};
557
- } catch {
558
- }
559
- try {
560
- const sample = takeHeapSample();
561
- const runtimeStats = collectRuntimeStats();
562
- fs.mkdirSync(path3.dirname(logPath), { recursive: true });
563
- fs.appendFileSync(
564
- logPath,
565
- `${JSON.stringify({
566
- ...extras,
567
- ...runtimeStats,
568
- event,
569
- ...origin ? { crashOrigin: origin } : {},
570
- ...error instanceof Error ? { errorName: error.name, errorMessage: error.message.slice(0, 1e3) } : {},
571
- pid: process.pid,
572
- ...sample,
573
- exitCode: process.exitCode
574
- })}
575
- `,
576
- "utf8"
577
- );
578
- } catch {
579
- }
580
- };
581
- const onUncaughtException = (error, origin) => {
582
- appendTerminalSample("memory.flight_recorder.crash", error, origin);
583
- };
584
- const onProcessExit = () => {
585
- appendTerminalSample("memory.flight_recorder.exit");
586
- };
587
- process.on("uncaughtExceptionMonitor", onUncaughtException);
588
- process.on("exit", onProcessExit);
589
- const tick = () => {
590
- let userTimings = 0;
591
- try {
592
- userTimings = performance.getEntriesByType("mark").length + performance.getEntriesByType("measure").length;
593
- if (userTimings > 0) {
594
- performance.clearMarks();
595
- performance.clearMeasures();
596
- }
597
- } catch {
598
- }
599
- const s = takeHeapSample();
600
- let extras = {};
601
- try {
602
- extras = opts.collectStats?.() ?? {};
603
- } catch {
604
- }
605
- const runtimeStats = collectRuntimeStats();
606
- const diagnosticStats = { ...extras, ...runtimeStats };
607
- const memoryDiagnosis = flightRecorder.observe(s, diagnosticStats);
608
- if (s.load >= criticalAt && criticalArmed) {
609
- criticalArmed = false;
610
- warnArmed = false;
611
- opts.onWarn?.(
612
- "critical",
613
- `Heap critical: ${Math.round(s.heapUsed / MB2)} MB of ~${Math.round(s.heapLimit / MB2)} MB V8 limit (${Math.round(s.load * 100)}%). An out-of-memory crash is likely soon \u2014 finish/checkpoint work and restart the session. Diagnostics: ${logPath}`,
614
- s
615
- );
616
- } else if (s.load >= warnAt && warnArmed) {
617
- warnArmed = false;
618
- opts.onWarn?.(
619
- "warn",
620
- `Heap high: ${Math.round(s.heapUsed / MB2)} MB of ~${Math.round(s.heapLimit / MB2)} MB V8 limit (${Math.round(s.load * 100)}%). Memory diagnostics are being recorded to ${logPath}`,
621
- s
622
- );
623
- }
624
- if (!warnArmed && s.load < warnAt - REARM_MARGIN) warnArmed = true;
625
- if (!criticalArmed && s.load < criticalAt - REARM_MARGIN) criticalArmed = true;
626
- const due = Date.now() - lastLogAt >= logEveryMs;
627
- const crossed = s.load >= warnAt;
628
- const captured = typeof memoryDiagnosis["memoryCaptureReason"] === "string";
629
- if (due || crossed || captured) {
630
- lastLogAt = Date.now();
631
- append(
632
- JSON.stringify({
633
- ...extras,
634
- ...runtimeStats,
635
- ...memoryDiagnosis,
636
- pid: process.pid,
637
- ...s,
638
- userTimings
639
- })
640
- );
641
- }
642
- };
643
- const timer = setInterval(tick, sampleEveryMs);
644
- timer.unref?.();
645
- tick();
646
- return async () => {
647
- stopped = true;
648
- clearInterval(timer);
649
- gcObserver.disconnect();
650
- process.removeListener("uncaughtExceptionMonitor", onUncaughtException);
651
- process.removeListener("exit", onProcessExit);
652
- await drainPromise;
653
- await flightRecorder.stop();
654
- };
655
- }
656
- var sharedContributors = /* @__PURE__ */ new Map();
657
- var nextSharedContributorId = 1;
658
- var sharedSamplerActive = false;
659
- var stopSharedSampler;
660
- function collectSharedStats() {
661
- const merged = {};
662
- for (const contributor of sharedContributors.values()) {
663
- try {
664
- Object.assign(merged, contributor.collectStats?.() ?? {});
665
- } catch {
666
- }
667
- }
668
- return merged;
669
- }
670
- function warnSharedContributors(level, message, sample) {
671
- for (const contributor of sharedContributors.values()) {
672
- try {
673
- contributor.onWarn?.(level, message, sample);
674
- } catch {
675
- }
676
- }
677
- }
678
- function startSharedHeapWatchdog(opts = {}) {
679
- if (process.env["NODE_ENV"] === "test" && !sharedSamplerActive && opts.logPath === void 0 && opts.writeDiagnosticLine === void 0) {
680
- return async () => void 0;
681
- }
682
- const id = nextSharedContributorId++;
683
- sharedContributors.set(id, {
684
- collectStats: opts.collectStats,
685
- onWarn: opts.onWarn
686
- });
687
- if (!sharedSamplerActive) {
688
- sharedSamplerActive = true;
689
- stopSharedSampler = startHeapWatchdog({
690
- ...opts,
691
- collectStats: collectSharedStats,
692
- onWarn: warnSharedContributors
693
- });
694
- }
695
- let stopped = false;
696
- return async () => {
697
- if (stopped) return;
698
- stopped = true;
699
- sharedContributors.delete(id);
700
- if (sharedContributors.size > 0 || !sharedSamplerActive) return;
701
- const stop = stopSharedSampler;
702
- if (!stop) return;
703
- sharedSamplerActive = false;
704
- stopSharedSampler = void 0;
705
- await stop();
706
- };
707
- }
2
+ defaultHeapLogPath,
3
+ startHeapWatchdog,
4
+ startSharedHeapWatchdog,
5
+ takeHeapSample
6
+ } from "../chunk-DYLPMV4Q.js";
7
+ import "../chunk-SAFHACFA.js";
708
8
  export {
709
9
  defaultHeapLogPath,
710
10
  startHeapWatchdog,