@mxf-dev/core 2.0.5 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (466) hide show
  1. package/dist/config/ModelContextLimits.d.ts.map +1 -1
  2. package/dist/config/ModelContextLimits.js +7 -1
  3. package/dist/config/ModelContextLimits.js.map +1 -1
  4. package/dist/constants/CoreTools.d.ts +6 -5
  5. package/dist/constants/CoreTools.d.ts.map +1 -1
  6. package/dist/constants/CoreTools.js +6 -5
  7. package/dist/constants/CoreTools.js.map +1 -1
  8. package/dist/constants/DefaultPhaseProfiles.d.ts.map +1 -1
  9. package/dist/constants/DefaultPhaseProfiles.js +29 -0
  10. package/dist/constants/DefaultPhaseProfiles.js.map +1 -1
  11. package/dist/constants/ReservedIdentities.d.ts +14 -0
  12. package/dist/constants/ReservedIdentities.d.ts.map +1 -0
  13. package/dist/constants/ReservedIdentities.js +42 -0
  14. package/dist/constants/ReservedIdentities.js.map +1 -0
  15. package/dist/database/adapters/mongodb/MongoKnowledgeGraphRepository.d.ts +16 -16
  16. package/dist/database/adapters/mongodb/MongoKnowledgeGraphRepository.d.ts.map +1 -1
  17. package/dist/database/adapters/mongodb/MongoKnowledgeGraphRepository.js +98 -36
  18. package/dist/database/adapters/mongodb/MongoKnowledgeGraphRepository.js.map +1 -1
  19. package/dist/events/ClientEventBus.d.ts +28 -0
  20. package/dist/events/ClientEventBus.d.ts.map +1 -1
  21. package/dist/events/ClientEventBus.js +87 -40
  22. package/dist/events/ClientEventBus.js.map +1 -1
  23. package/dist/events/EventBus.d.ts +19 -1
  24. package/dist/events/EventBus.d.ts.map +1 -1
  25. package/dist/events/EventBus.js +23 -0
  26. package/dist/events/EventBus.js.map +1 -1
  27. package/dist/events/EventBusBase.d.ts +37 -6
  28. package/dist/events/EventBusBase.d.ts.map +1 -1
  29. package/dist/events/EventBusBase.js +75 -27
  30. package/dist/events/EventBusBase.js.map +1 -1
  31. package/dist/events/EventNames.d.ts +49 -36
  32. package/dist/events/EventNames.d.ts.map +1 -1
  33. package/dist/events/EventNames.js +38 -36
  34. package/dist/events/EventNames.js.map +1 -1
  35. package/dist/events/PublicEvents.d.ts +5 -1
  36. package/dist/events/PublicEvents.d.ts.map +1 -1
  37. package/dist/events/PublicEvents.js +7 -0
  38. package/dist/events/PublicEvents.js.map +1 -1
  39. package/dist/events/ServerEventBus.d.ts +7 -54
  40. package/dist/events/ServerEventBus.d.ts.map +1 -1
  41. package/dist/events/ServerEventBus.js +10 -96
  42. package/dist/events/ServerEventBus.js.map +1 -1
  43. package/dist/events/event-definitions/ControlLoopEvents.d.ts +35 -0
  44. package/dist/events/event-definitions/ControlLoopEvents.d.ts.map +1 -1
  45. package/dist/events/event-definitions/ControlLoopEvents.js +6 -0
  46. package/dist/events/event-definitions/ControlLoopEvents.js.map +1 -1
  47. package/dist/events/event-definitions/SdkEvents.d.ts +55 -0
  48. package/dist/events/event-definitions/SdkEvents.d.ts.map +1 -0
  49. package/dist/events/event-definitions/SdkEvents.js +36 -0
  50. package/dist/events/event-definitions/SdkEvents.js.map +1 -0
  51. package/dist/events/event-definitions/TaskEvents.d.ts +2 -7
  52. package/dist/events/event-definitions/TaskEvents.d.ts.map +1 -1
  53. package/dist/index.d.ts +4 -0
  54. package/dist/index.d.ts.map +1 -1
  55. package/dist/index.js +2 -0
  56. package/dist/index.js.map +1 -1
  57. package/dist/interfaces/AgentInterfaces.d.ts +5 -0
  58. package/dist/interfaces/AgentInterfaces.d.ts.map +1 -1
  59. package/dist/interfaces/ChannelConfig.d.ts +2 -2
  60. package/dist/interfaces/ChannelConfig.d.ts.map +1 -1
  61. package/dist/interfaces/ConversationMessage.d.ts +1 -1
  62. package/dist/interfaces/ConversationMessage.d.ts.map +1 -1
  63. package/dist/interfaces/IMemoryPersistence.d.ts +47 -1
  64. package/dist/interfaces/IMemoryPersistence.d.ts.map +1 -1
  65. package/dist/interfaces/SocketServiceInterface.d.ts +3 -2
  66. package/dist/interfaces/SocketServiceInterface.d.ts.map +1 -1
  67. package/dist/interfaces/TaskInterfaces.d.ts +14 -1
  68. package/dist/interfaces/TaskInterfaces.d.ts.map +1 -1
  69. package/dist/models/agent.d.ts +0 -1
  70. package/dist/models/agent.d.ts.map +1 -1
  71. package/dist/models/agent.js +0 -5
  72. package/dist/models/agent.js.map +1 -1
  73. package/dist/models/agentIdentityReservation.d.ts +42 -0
  74. package/dist/models/agentIdentityReservation.d.ts.map +1 -0
  75. package/dist/models/agentIdentityReservation.js +52 -0
  76. package/dist/models/agentIdentityReservation.js.map +1 -0
  77. package/dist/models/channel.d.ts +2 -0
  78. package/dist/models/channel.d.ts.map +1 -1
  79. package/dist/models/channel.js.map +1 -1
  80. package/dist/models/channelKey.d.ts +1 -0
  81. package/dist/models/channelKey.d.ts.map +1 -1
  82. package/dist/models/channelKey.js +5 -0
  83. package/dist/models/channelKey.js.map +1 -1
  84. package/dist/models/memory.d.ts +1 -1
  85. package/dist/models/memory.d.ts.map +1 -1
  86. package/dist/models/memory.js +9 -12
  87. package/dist/models/memory.js.map +1 -1
  88. package/dist/models/memoryStrata.d.ts +3 -16
  89. package/dist/models/memoryStrata.d.ts.map +1 -1
  90. package/dist/models/memoryStrata.js +3 -5
  91. package/dist/models/memoryStrata.js.map +1 -1
  92. package/dist/models/plan.d.ts +2 -2
  93. package/dist/models/plan.d.ts.map +1 -1
  94. package/dist/models/plan.js +1 -1
  95. package/dist/models/plan.js.map +1 -1
  96. package/dist/models/taskEffectiveness.d.ts +1 -0
  97. package/dist/models/taskEffectiveness.d.ts.map +1 -1
  98. package/dist/models/taskEffectiveness.js +7 -1
  99. package/dist/models/taskEffectiveness.js.map +1 -1
  100. package/dist/models/user.d.ts +4 -0
  101. package/dist/models/user.d.ts.map +1 -1
  102. package/dist/models/user.js +8 -0
  103. package/dist/models/user.js.map +1 -1
  104. package/dist/protocols/mcp/LlmTestEnvironmentGuard.d.ts +16 -0
  105. package/dist/protocols/mcp/LlmTestEnvironmentGuard.d.ts.map +1 -0
  106. package/dist/protocols/mcp/LlmTestEnvironmentGuard.js +25 -0
  107. package/dist/protocols/mcp/LlmTestEnvironmentGuard.js.map +1 -0
  108. package/dist/protocols/mcp/McpServerTypes.d.ts +9 -0
  109. package/dist/protocols/mcp/McpServerTypes.d.ts.map +1 -1
  110. package/dist/protocols/mcp/providers/AnthropicMcpClient.d.ts.map +1 -1
  111. package/dist/protocols/mcp/providers/AnthropicMcpClient.js +1 -0
  112. package/dist/protocols/mcp/providers/AnthropicMcpClient.js.map +1 -1
  113. package/dist/protocols/mcp/providers/AzureOpenAiMcpClient.d.ts.map +1 -1
  114. package/dist/protocols/mcp/providers/AzureOpenAiMcpClient.js +1 -0
  115. package/dist/protocols/mcp/providers/AzureOpenAiMcpClient.js.map +1 -1
  116. package/dist/protocols/mcp/providers/BaseMcpClient.d.ts +4 -0
  117. package/dist/protocols/mcp/providers/BaseMcpClient.d.ts.map +1 -1
  118. package/dist/protocols/mcp/providers/BaseMcpClient.js +10 -0
  119. package/dist/protocols/mcp/providers/BaseMcpClient.js.map +1 -1
  120. package/dist/protocols/mcp/providers/CustomMcpClient.d.ts.map +1 -1
  121. package/dist/protocols/mcp/providers/CustomMcpClient.js +7 -0
  122. package/dist/protocols/mcp/providers/CustomMcpClient.js.map +1 -1
  123. package/dist/protocols/mcp/providers/GeminiMcpClient.d.ts.map +1 -1
  124. package/dist/protocols/mcp/providers/GeminiMcpClient.js +1 -0
  125. package/dist/protocols/mcp/providers/GeminiMcpClient.js.map +1 -1
  126. package/dist/protocols/mcp/providers/OllamaMcpClient.d.ts +1 -0
  127. package/dist/protocols/mcp/providers/OllamaMcpClient.d.ts.map +1 -1
  128. package/dist/protocols/mcp/providers/OllamaMcpClient.js +1 -0
  129. package/dist/protocols/mcp/providers/OllamaMcpClient.js.map +1 -1
  130. package/dist/protocols/mcp/providers/OpenAiMcpClient.d.ts.map +1 -1
  131. package/dist/protocols/mcp/providers/OpenAiMcpClient.js +1 -0
  132. package/dist/protocols/mcp/providers/OpenAiMcpClient.js.map +1 -1
  133. package/dist/protocols/mcp/providers/OpenRouterMcpClient.d.ts.map +1 -1
  134. package/dist/protocols/mcp/providers/OpenRouterMcpClient.js +2 -0
  135. package/dist/protocols/mcp/providers/OpenRouterMcpClient.js.map +1 -1
  136. package/dist/protocols/mcp/providers/XaiMcpClient.d.ts.map +1 -1
  137. package/dist/protocols/mcp/providers/XaiMcpClient.js +1 -0
  138. package/dist/protocols/mcp/providers/XaiMcpClient.js.map +1 -1
  139. package/dist/protocols/mcp/security/BrowserTargetGuard.d.ts +14 -0
  140. package/dist/protocols/mcp/security/BrowserTargetGuard.d.ts.map +1 -0
  141. package/dist/protocols/mcp/security/BrowserTargetGuard.js +55 -0
  142. package/dist/protocols/mcp/security/BrowserTargetGuard.js.map +1 -0
  143. package/dist/protocols/mcp/security/ExternalMcpRegistrationPolicy.d.ts +24 -0
  144. package/dist/protocols/mcp/security/ExternalMcpRegistrationPolicy.d.ts.map +1 -0
  145. package/dist/protocols/mcp/security/ExternalMcpRegistrationPolicy.js +33 -0
  146. package/dist/protocols/mcp/security/ExternalMcpRegistrationPolicy.js.map +1 -0
  147. package/dist/protocols/mcp/security/HttpTargetGuard.d.ts.map +1 -1
  148. package/dist/protocols/mcp/security/HttpTargetGuard.js +68 -6
  149. package/dist/protocols/mcp/security/HttpTargetGuard.js.map +1 -1
  150. package/dist/protocols/mcp/security/McpSecurityGuard.d.ts.map +1 -1
  151. package/dist/protocols/mcp/security/McpSecurityGuard.js +7 -5
  152. package/dist/protocols/mcp/security/McpSecurityGuard.js.map +1 -1
  153. package/dist/protocols/mcp/security/McpToolPolicy.d.ts +18 -0
  154. package/dist/protocols/mcp/security/McpToolPolicy.d.ts.map +1 -1
  155. package/dist/protocols/mcp/security/McpToolPolicy.js +60 -0
  156. package/dist/protocols/mcp/security/McpToolPolicy.js.map +1 -1
  157. package/dist/protocols/mcp/services/ExternalMcpServerManager.d.ts +25 -0
  158. package/dist/protocols/mcp/services/ExternalMcpServerManager.d.ts.map +1 -1
  159. package/dist/protocols/mcp/services/ExternalMcpServerManager.js +93 -17
  160. package/dist/protocols/mcp/services/ExternalMcpServerManager.js.map +1 -1
  161. package/dist/protocols/mcp/services/ExternalServerConfigs.d.ts +2 -2
  162. package/dist/protocols/mcp/services/ExternalServerConfigs.d.ts.map +1 -1
  163. package/dist/protocols/mcp/services/ExternalServerConfigs.js +25 -8
  164. package/dist/protocols/mcp/services/ExternalServerConfigs.js.map +1 -1
  165. package/dist/protocols/mcp/tools/AnalyticsTools.d.ts +3 -37
  166. package/dist/protocols/mcp/tools/AnalyticsTools.d.ts.map +1 -1
  167. package/dist/protocols/mcp/tools/AnalyticsTools.js +134 -614
  168. package/dist/protocols/mcp/tools/AnalyticsTools.js.map +1 -1
  169. package/dist/protocols/mcp/tools/CodeAnalysisTools.d.ts.map +1 -1
  170. package/dist/protocols/mcp/tools/CodeAnalysisTools.js +16 -13
  171. package/dist/protocols/mcp/tools/CodeAnalysisTools.js.map +1 -1
  172. package/dist/protocols/mcp/tools/EffectivenessTools.d.ts.map +1 -1
  173. package/dist/protocols/mcp/tools/EffectivenessTools.js +94 -30
  174. package/dist/protocols/mcp/tools/EffectivenessTools.js.map +1 -1
  175. package/dist/protocols/mcp/tools/GitTools.d.ts.map +1 -1
  176. package/dist/protocols/mcp/tools/GitTools.js +9 -5
  177. package/dist/protocols/mcp/tools/GitTools.js.map +1 -1
  178. package/dist/protocols/mcp/tools/InfrastructureTools.d.ts +10 -40
  179. package/dist/protocols/mcp/tools/InfrastructureTools.d.ts.map +1 -1
  180. package/dist/protocols/mcp/tools/InfrastructureTools.js +29 -70
  181. package/dist/protocols/mcp/tools/InfrastructureTools.js.map +1 -1
  182. package/dist/protocols/mcp/tools/JsonTools.d.ts.map +1 -1
  183. package/dist/protocols/mcp/tools/JsonTools.js +4 -4
  184. package/dist/protocols/mcp/tools/JsonTools.js.map +1 -1
  185. package/dist/protocols/mcp/tools/KnowledgeGraphTools.d.ts +55 -592
  186. package/dist/protocols/mcp/tools/KnowledgeGraphTools.d.ts.map +1 -1
  187. package/dist/protocols/mcp/tools/KnowledgeGraphTools.js +169 -119
  188. package/dist/protocols/mcp/tools/KnowledgeGraphTools.js.map +1 -1
  189. package/dist/protocols/mcp/tools/MemorySearchTools.d.ts +6 -6
  190. package/dist/protocols/mcp/tools/MemorySearchTools.d.ts.map +1 -1
  191. package/dist/protocols/mcp/tools/MemorySearchTools.js +107 -39
  192. package/dist/protocols/mcp/tools/MemorySearchTools.js.map +1 -1
  193. package/dist/protocols/mcp/tools/MemoryUtilityTools.d.ts +4 -5
  194. package/dist/protocols/mcp/tools/MemoryUtilityTools.d.ts.map +1 -1
  195. package/dist/protocols/mcp/tools/MemoryUtilityTools.js +59 -208
  196. package/dist/protocols/mcp/tools/MemoryUtilityTools.js.map +1 -1
  197. package/dist/protocols/mcp/tools/PlanningTools.d.ts +2 -1
  198. package/dist/protocols/mcp/tools/PlanningTools.d.ts.map +1 -1
  199. package/dist/protocols/mcp/tools/PlanningTools.js +111 -49
  200. package/dist/protocols/mcp/tools/PlanningTools.js.map +1 -1
  201. package/dist/protocols/mcp/tools/ProjectContextTools.d.ts.map +1 -1
  202. package/dist/protocols/mcp/tools/ProjectContextTools.js +18 -12
  203. package/dist/protocols/mcp/tools/ProjectContextTools.js.map +1 -1
  204. package/dist/protocols/mcp/tools/SafetyTools.d.ts.map +1 -1
  205. package/dist/protocols/mcp/tools/SafetyTools.js +16 -13
  206. package/dist/protocols/mcp/tools/SafetyTools.js.map +1 -1
  207. package/dist/protocols/mcp/tools/SearchProjectTools.d.ts +24 -7
  208. package/dist/protocols/mcp/tools/SearchProjectTools.d.ts.map +1 -1
  209. package/dist/protocols/mcp/tools/SearchProjectTools.js +226 -126
  210. package/dist/protocols/mcp/tools/SearchProjectTools.js.map +1 -1
  211. package/dist/protocols/mcp/tools/TaskPlanningTools.d.ts +0 -32
  212. package/dist/protocols/mcp/tools/TaskPlanningTools.d.ts.map +1 -1
  213. package/dist/protocols/mcp/tools/TaskPlanningTools.js +363 -450
  214. package/dist/protocols/mcp/tools/TaskPlanningTools.js.map +1 -1
  215. package/dist/protocols/mcp/tools/TestTools.d.ts.map +1 -1
  216. package/dist/protocols/mcp/tools/TestTools.js +18 -11
  217. package/dist/protocols/mcp/tools/TestTools.js.map +1 -1
  218. package/dist/protocols/mcp/tools/TypeScriptTools.d.ts.map +1 -1
  219. package/dist/protocols/mcp/tools/TypeScriptTools.js +22 -12
  220. package/dist/protocols/mcp/tools/TypeScriptTools.js.map +1 -1
  221. package/dist/protocols/mcp/tools/UserInputTools.d.ts.map +1 -1
  222. package/dist/protocols/mcp/tools/UserInputTools.js +14 -1
  223. package/dist/protocols/mcp/tools/UserInputTools.js.map +1 -1
  224. package/dist/protocols/mcp/tools/UserMemoryTools.d.ts +3 -1
  225. package/dist/protocols/mcp/tools/UserMemoryTools.d.ts.map +1 -1
  226. package/dist/protocols/mcp/tools/UserMemoryTools.js +27 -62
  227. package/dist/protocols/mcp/tools/UserMemoryTools.js.map +1 -1
  228. package/dist/protocols/mcp/tools/shell/ShellExecuteHandler.d.ts.map +1 -1
  229. package/dist/protocols/mcp/tools/shell/ShellExecuteHandler.js +14 -60
  230. package/dist/protocols/mcp/tools/shell/ShellExecuteHandler.js.map +1 -1
  231. package/dist/protocols/mcp/tools/shell/ShellSandbox.d.ts.map +1 -1
  232. package/dist/protocols/mcp/tools/shell/ShellSandbox.js +9 -27
  233. package/dist/protocols/mcp/tools/shell/ShellSandbox.js.map +1 -1
  234. package/dist/repositories/interfaces/IKnowledgeGraphRepository.d.ts +40 -30
  235. package/dist/repositories/interfaces/IKnowledgeGraphRepository.d.ts.map +1 -1
  236. package/dist/schemas/EventPayloadSchema.d.ts +28 -0
  237. package/dist/schemas/EventPayloadSchema.d.ts.map +1 -1
  238. package/dist/schemas/EventPayloadSchema.js +30 -1
  239. package/dist/schemas/EventPayloadSchema.js.map +1 -1
  240. package/dist/schemas/MxpProtocolSchemas.d.ts +6 -6
  241. package/dist/services/AgentPerformanceService.d.ts +11 -0
  242. package/dist/services/AgentPerformanceService.d.ts.map +1 -1
  243. package/dist/services/AgentPerformanceService.js +59 -33
  244. package/dist/services/AgentPerformanceService.js.map +1 -1
  245. package/dist/services/AutoCorrectionService.d.ts +3 -0
  246. package/dist/services/AutoCorrectionService.d.ts.map +1 -1
  247. package/dist/services/AutoCorrectionService.js +19 -2
  248. package/dist/services/AutoCorrectionService.js.map +1 -1
  249. package/dist/services/BackgroundTaskManager.d.ts +23 -7
  250. package/dist/services/BackgroundTaskManager.d.ts.map +1 -1
  251. package/dist/services/BackgroundTaskManager.js +137 -57
  252. package/dist/services/BackgroundTaskManager.js.map +1 -1
  253. package/dist/services/BrowserManager.d.ts +2 -0
  254. package/dist/services/BrowserManager.d.ts.map +1 -1
  255. package/dist/services/BrowserManager.js +18 -15
  256. package/dist/services/BrowserManager.js.map +1 -1
  257. package/dist/services/ChannelContextMemoryOperations.d.ts +8 -1
  258. package/dist/services/ChannelContextMemoryOperations.d.ts.map +1 -1
  259. package/dist/services/ChannelContextMemoryOperations.js +89 -165
  260. package/dist/services/ChannelContextMemoryOperations.js.map +1 -1
  261. package/dist/services/ChannelContextMessageOperations.d.ts +4 -3
  262. package/dist/services/ChannelContextMessageOperations.d.ts.map +1 -1
  263. package/dist/services/ChannelContextMessageOperations.js +72 -212
  264. package/dist/services/ChannelContextMessageOperations.js.map +1 -1
  265. package/dist/services/MemoryService.d.ts +52 -21
  266. package/dist/services/MemoryService.d.ts.map +1 -1
  267. package/dist/services/MemoryService.js +775 -548
  268. package/dist/services/MemoryService.js.map +1 -1
  269. package/dist/services/PatternLearningService.d.ts +5 -0
  270. package/dist/services/PatternLearningService.d.ts.map +1 -1
  271. package/dist/services/PatternLearningService.js +26 -3
  272. package/dist/services/PatternLearningService.js.map +1 -1
  273. package/dist/services/PerformanceOptimizationService.d.ts +2 -1
  274. package/dist/services/PerformanceOptimizationService.d.ts.map +1 -1
  275. package/dist/services/PerformanceOptimizationService.js +21 -6
  276. package/dist/services/PerformanceOptimizationService.js.map +1 -1
  277. package/dist/services/PredictiveAnalyticsService.d.ts +1 -0
  278. package/dist/services/PredictiveAnalyticsService.d.ts.map +1 -1
  279. package/dist/services/PredictiveAnalyticsService.js +18 -6
  280. package/dist/services/PredictiveAnalyticsService.js.map +1 -1
  281. package/dist/services/ProactiveValidationService.d.ts +6 -0
  282. package/dist/services/ProactiveValidationService.d.ts.map +1 -1
  283. package/dist/services/ProactiveValidationService.js +58 -39
  284. package/dist/services/ProactiveValidationService.js.map +1 -1
  285. package/dist/services/TaskEffectivenessService.d.ts +55 -31
  286. package/dist/services/TaskEffectivenessService.d.ts.map +1 -1
  287. package/dist/services/TaskEffectivenessService.js +243 -125
  288. package/dist/services/TaskEffectivenessService.js.map +1 -1
  289. package/dist/services/ToolExecutionInterceptor.d.ts +4 -0
  290. package/dist/services/ToolExecutionInterceptor.d.ts.map +1 -1
  291. package/dist/services/ToolExecutionInterceptor.js +31 -5
  292. package/dist/services/ToolExecutionInterceptor.js.map +1 -1
  293. package/dist/services/UserInputRequestManager.d.ts +13 -3
  294. package/dist/services/UserInputRequestManager.d.ts.map +1 -1
  295. package/dist/services/UserInputRequestManager.js +42 -6
  296. package/dist/services/UserInputRequestManager.js.map +1 -1
  297. package/dist/services/UserMemoryService.d.ts +2 -0
  298. package/dist/services/UserMemoryService.d.ts.map +1 -1
  299. package/dist/services/UserMemoryService.js +9 -4
  300. package/dist/services/UserMemoryService.js.map +1 -1
  301. package/dist/services/ValidationAnalyticsService.d.ts +4 -0
  302. package/dist/services/ValidationAnalyticsService.d.ts.map +1 -1
  303. package/dist/services/ValidationAnalyticsService.js +30 -8
  304. package/dist/services/ValidationAnalyticsService.js.map +1 -1
  305. package/dist/services/ValidationCacheService.d.ts +3 -1
  306. package/dist/services/ValidationCacheService.d.ts.map +1 -1
  307. package/dist/services/ValidationCacheService.js +25 -4
  308. package/dist/services/ValidationCacheService.js.map +1 -1
  309. package/dist/services/ValidationMiddleware.d.ts +7 -0
  310. package/dist/services/ValidationMiddleware.d.ts.map +1 -1
  311. package/dist/services/ValidationMiddleware.js +53 -10
  312. package/dist/services/ValidationMiddleware.js.map +1 -1
  313. package/dist/services/ValidationPerformanceService.d.ts +3 -0
  314. package/dist/services/ValidationPerformanceService.d.ts.map +1 -1
  315. package/dist/services/ValidationPerformanceService.js +28 -11
  316. package/dist/services/ValidationPerformanceService.js.map +1 -1
  317. package/dist/services/WebSearchService.d.ts.map +1 -1
  318. package/dist/services/WebSearchService.js +9 -0
  319. package/dist/services/WebSearchService.js.map +1 -1
  320. package/dist/services/dag/TaskDagService.d.ts +3 -0
  321. package/dist/services/dag/TaskDagService.d.ts.map +1 -1
  322. package/dist/services/dag/TaskDagService.js +18 -0
  323. package/dist/services/dag/TaskDagService.js.map +1 -1
  324. package/dist/services/kg/EntityQValueManager.d.ts +3 -1
  325. package/dist/services/kg/EntityQValueManager.d.ts.map +1 -1
  326. package/dist/services/kg/EntityQValueManager.js +10 -10
  327. package/dist/services/kg/EntityQValueManager.js.map +1 -1
  328. package/dist/services/kg/GraphSurpriseCalculator.d.ts +0 -4
  329. package/dist/services/kg/GraphSurpriseCalculator.d.ts.map +1 -1
  330. package/dist/services/kg/GraphSurpriseCalculator.js +0 -9
  331. package/dist/services/kg/GraphSurpriseCalculator.js.map +1 -1
  332. package/dist/services/kg/KnowledgeGraphService.d.ts +12 -12
  333. package/dist/services/kg/KnowledgeGraphService.d.ts.map +1 -1
  334. package/dist/services/kg/KnowledgeGraphService.js +43 -51
  335. package/dist/services/kg/KnowledgeGraphService.js.map +1 -1
  336. package/dist/services/kg/OrparGraphIntegration.d.ts.map +1 -1
  337. package/dist/services/kg/OrparGraphIntegration.js +6 -5
  338. package/dist/services/kg/OrparGraphIntegration.js.map +1 -1
  339. package/dist/services/orpar-memory/CycleConsolidationTrigger.d.ts +1 -0
  340. package/dist/services/orpar-memory/CycleConsolidationTrigger.d.ts.map +1 -1
  341. package/dist/services/orpar-memory/CycleConsolidationTrigger.js +8 -1
  342. package/dist/services/orpar-memory/CycleConsolidationTrigger.js.map +1 -1
  343. package/dist/services/orpar-memory/OrparMemoryCoordinator.d.ts +8 -1
  344. package/dist/services/orpar-memory/OrparMemoryCoordinator.d.ts.map +1 -1
  345. package/dist/services/orpar-memory/OrparMemoryCoordinator.js +93 -45
  346. package/dist/services/orpar-memory/OrparMemoryCoordinator.js.map +1 -1
  347. package/dist/services/orpar-memory/SurpriseOrparAdapter.d.ts +1 -0
  348. package/dist/services/orpar-memory/SurpriseOrparAdapter.d.ts.map +1 -1
  349. package/dist/services/orpar-memory/SurpriseOrparAdapter.js +8 -1
  350. package/dist/services/orpar-memory/SurpriseOrparAdapter.js.map +1 -1
  351. package/dist/types/ChannelContext.d.ts +2 -1
  352. package/dist/types/ChannelContext.d.ts.map +1 -1
  353. package/dist/types/EffectivenessTypes.d.ts +4 -1
  354. package/dist/types/EffectivenessTypes.d.ts.map +1 -1
  355. package/dist/types/TaskCompletionTypes.d.ts +7 -5
  356. package/dist/types/TaskCompletionTypes.d.ts.map +1 -1
  357. package/dist/types/TaskTypes.d.ts +8 -1
  358. package/dist/types/TaskTypes.d.ts.map +1 -1
  359. package/dist/utils/GlobMatcher.d.ts +37 -0
  360. package/dist/utils/GlobMatcher.d.ts.map +1 -0
  361. package/dist/utils/GlobMatcher.js +131 -0
  362. package/dist/utils/GlobMatcher.js.map +1 -0
  363. package/package.json +1 -1
  364. package/src/config/ModelContextLimits.ts +7 -1
  365. package/src/constants/CoreTools.ts +6 -5
  366. package/src/constants/DefaultPhaseProfiles.ts +29 -0
  367. package/src/constants/ReservedIdentities.ts +51 -0
  368. package/src/database/adapters/mongodb/MongoKnowledgeGraphRepository.ts +115 -36
  369. package/src/events/ClientEventBus.ts +117 -48
  370. package/src/events/EventBus.ts +32 -2
  371. package/src/events/EventBusBase.ts +132 -39
  372. package/src/events/EventNames.ts +43 -36
  373. package/src/events/PublicEvents.ts +8 -0
  374. package/src/events/ServerEventBus.ts +9 -128
  375. package/src/events/event-definitions/ControlLoopEvents.ts +12 -0
  376. package/src/events/event-definitions/SdkEvents.ts +58 -0
  377. package/src/events/event-definitions/TaskEvents.ts +2 -2
  378. package/src/index.ts +15 -0
  379. package/src/interfaces/AgentInterfaces.ts +5 -0
  380. package/src/interfaces/ChannelConfig.ts +2 -2
  381. package/src/interfaces/ConversationMessage.ts +5 -0
  382. package/src/interfaces/IMemoryPersistence.ts +55 -1
  383. package/src/interfaces/SocketServiceInterface.ts +3 -2
  384. package/src/interfaces/TaskInterfaces.ts +16 -1
  385. package/src/models/agent.ts +0 -6
  386. package/src/models/agentIdentityReservation.ts +77 -0
  387. package/src/models/channel.ts +2 -0
  388. package/src/models/channelKey.ts +10 -0
  389. package/src/models/memory.ts +18 -19
  390. package/src/models/memoryStrata.ts +9 -23
  391. package/src/models/plan.ts +3 -3
  392. package/src/models/taskEffectiveness.ts +9 -2
  393. package/src/models/user.ts +12 -0
  394. package/src/protocols/mcp/LlmTestEnvironmentGuard.ts +30 -0
  395. package/src/protocols/mcp/McpServerTypes.ts +9 -0
  396. package/src/protocols/mcp/providers/AnthropicMcpClient.ts +2 -0
  397. package/src/protocols/mcp/providers/AzureOpenAiMcpClient.ts +1 -0
  398. package/src/protocols/mcp/providers/BaseMcpClient.ts +13 -0
  399. package/src/protocols/mcp/providers/CustomMcpClient.ts +7 -0
  400. package/src/protocols/mcp/providers/GeminiMcpClient.ts +2 -0
  401. package/src/protocols/mcp/providers/OllamaMcpClient.ts +3 -1
  402. package/src/protocols/mcp/providers/OpenAiMcpClient.ts +2 -0
  403. package/src/protocols/mcp/providers/OpenRouterMcpClient.ts +3 -0
  404. package/src/protocols/mcp/providers/XaiMcpClient.ts +2 -0
  405. package/src/protocols/mcp/security/BrowserTargetGuard.ts +64 -0
  406. package/src/protocols/mcp/security/ExternalMcpRegistrationPolicy.ts +38 -0
  407. package/src/protocols/mcp/security/HttpTargetGuard.ts +77 -6
  408. package/src/protocols/mcp/security/McpSecurityGuard.ts +7 -5
  409. package/src/protocols/mcp/security/McpToolPolicy.ts +74 -0
  410. package/src/protocols/mcp/services/ExternalMcpServerManager.ts +146 -17
  411. package/src/protocols/mcp/services/ExternalServerConfigs.ts +26 -8
  412. package/src/protocols/mcp/tools/AnalyticsTools.ts +167 -641
  413. package/src/protocols/mcp/tools/CodeAnalysisTools.ts +44 -14
  414. package/src/protocols/mcp/tools/EffectivenessTools.ts +185 -45
  415. package/src/protocols/mcp/tools/GitTools.ts +15 -5
  416. package/src/protocols/mcp/tools/InfrastructureTools.ts +42 -90
  417. package/src/protocols/mcp/tools/JsonTools.ts +7 -4
  418. package/src/protocols/mcp/tools/KnowledgeGraphTools.ts +313 -164
  419. package/src/protocols/mcp/tools/MemorySearchTools.ts +158 -46
  420. package/src/protocols/mcp/tools/MemoryUtilityTools.ts +85 -232
  421. package/src/protocols/mcp/tools/PlanningTools.ts +137 -59
  422. package/src/protocols/mcp/tools/ProjectContextTools.ts +33 -12
  423. package/src/protocols/mcp/tools/SafetyTools.ts +49 -13
  424. package/src/protocols/mcp/tools/SearchProjectTools.ts +294 -153
  425. package/src/protocols/mcp/tools/TaskPlanningTools.ts +437 -514
  426. package/src/protocols/mcp/tools/TestTools.ts +41 -11
  427. package/src/protocols/mcp/tools/TypeScriptTools.ts +61 -12
  428. package/src/protocols/mcp/tools/UserInputTools.ts +15 -1
  429. package/src/protocols/mcp/tools/UserMemoryTools.ts +27 -62
  430. package/src/protocols/mcp/tools/shell/ShellExecuteHandler.ts +26 -84
  431. package/src/protocols/mcp/tools/shell/ShellSandbox.ts +13 -31
  432. package/src/repositories/interfaces/IKnowledgeGraphRepository.ts +45 -27
  433. package/src/schemas/EventPayloadSchema.ts +45 -1
  434. package/src/services/AgentPerformanceService.ts +85 -36
  435. package/src/services/AutoCorrectionService.ts +25 -6
  436. package/src/services/BackgroundTaskManager.ts +183 -76
  437. package/src/services/BrowserManager.ts +20 -16
  438. package/src/services/ChannelContextMemoryOperations.ts +169 -202
  439. package/src/services/ChannelContextMessageOperations.ts +112 -274
  440. package/src/services/MemoryService.ts +1053 -760
  441. package/src/services/PatternLearningService.ts +32 -7
  442. package/src/services/PerformanceOptimizationService.ts +26 -9
  443. package/src/services/PredictiveAnalyticsService.ts +23 -10
  444. package/src/services/ProactiveValidationService.ts +66 -40
  445. package/src/services/TaskEffectivenessService.ts +421 -206
  446. package/src/services/ToolExecutionInterceptor.ts +40 -9
  447. package/src/services/UserInputRequestManager.ts +51 -6
  448. package/src/services/UserMemoryService.ts +12 -3
  449. package/src/services/ValidationAnalyticsService.ts +36 -11
  450. package/src/services/ValidationCacheService.ts +30 -7
  451. package/src/services/ValidationMiddleware.ts +62 -14
  452. package/src/services/ValidationPerformanceService.ts +47 -25
  453. package/src/services/WebSearchService.ts +16 -4
  454. package/src/services/dag/TaskDagService.ts +20 -1
  455. package/src/services/kg/EntityQValueManager.ts +21 -10
  456. package/src/services/kg/GraphSurpriseCalculator.ts +0 -14
  457. package/src/services/kg/KnowledgeGraphService.ts +56 -48
  458. package/src/services/kg/OrparGraphIntegration.ts +11 -5
  459. package/src/services/orpar-memory/CycleConsolidationTrigger.ts +10 -1
  460. package/src/services/orpar-memory/OrparMemoryCoordinator.ts +142 -71
  461. package/src/services/orpar-memory/SurpriseOrparAdapter.ts +10 -1
  462. package/src/types/ChannelContext.ts +3 -1
  463. package/src/types/EffectivenessTypes.ts +6 -2
  464. package/src/types/TaskCompletionTypes.ts +9 -9
  465. package/src/types/TaskTypes.ts +12 -1
  466. package/src/utils/GlobMatcher.ts +157 -0
@@ -44,6 +44,10 @@ const logger = new Logger('warn', 'ClientEventBus', 'client');
44
44
 
45
45
  // Create a strict validator for client-side operations
46
46
  const validator = createStrictValidator('ClientEventBus');
47
+ type SocketCallback = (event: string, ...args: unknown[]) => void;
48
+
49
+ /** Bound process-global socket replay protection without a cleanup timer. */
50
+ const MAX_INBOUND_EVENT_KEYS = 4096;
47
51
 
48
52
  /**
49
53
  * Interface for client-specific event bus functionality
@@ -56,6 +60,12 @@ export interface IClientEventBus extends BaseEventBusImplementation {
56
60
  */
57
61
  setClientSocket(socket: SocketLike): this;
58
62
 
63
+ /**
64
+ * Release the primary socket only when it is the expected socket. Named
65
+ * agent transports and listeners owned by other SDK services are retained.
66
+ */
67
+ clearClientSocket(socket?: SocketLike): this;
68
+
59
69
  /**
60
70
  * Set up socket event forwarding
61
71
  * @param socket Socket to use
@@ -90,6 +100,9 @@ export interface IClientEventBus extends BaseEventBusImplementation {
90
100
  */
91
101
  emitOn<K extends AnyEventName>(socketId: string, event: K, payload: PayloadOf<K>): void;
92
102
 
103
+ /** Whether the exact named socket used by emitOn() is connected. */
104
+ isRegisteredSocketConnected(socketId: string): boolean;
105
+
93
106
  /**
94
107
  * Disconnect the primary socket and unregister all sockets
95
108
  * @returns this (for chaining)
@@ -113,13 +126,21 @@ export class ClientEventBus extends BaseEventBusImplementation implements IClien
113
126
  // Primary socket (set by setClientSocket, used by admin/SDK emit())
114
127
  private socket: SocketLike | null = null;
115
128
  // Listeners tracked for the primary socket's onAny handler
116
- private socketListeners: Map<string, (...args: any[]) => void> = new Map();
129
+ private socketListeners: Map<string, SocketCallback> = new Map();
117
130
 
118
131
  // Socket registry: maps socketId (typically agentId) → socket instance
119
132
  // Agent sockets register here so they don't overwrite the primary admin socket
120
133
  private socketRegistry: Map<string, SocketLike> = new Map();
121
134
  // Listeners tracked for each registered socket's onAny handler
122
- private socketListenersRegistry: Map<string, Map<string, (...args: any[]) => void>> = new Map();
135
+ private socketListenersRegistry: Map<string, Map<string, SocketCallback>> = new Map();
136
+
137
+ /**
138
+ * Socket.IO broadcasts one envelope to every local agent socket. All of
139
+ * those sockets feed this process-global bus, so remember exact envelopes
140
+ * and publish each one only once. Set iteration order supplies bounded FIFO
141
+ * eviction; raw/custom socket payloads without an eventId remain distinct.
142
+ */
143
+ private readonly inboundEventKeys = new Set<string>();
123
144
 
124
145
 
125
146
  /**
@@ -143,6 +164,11 @@ export class ClientEventBus extends BaseEventBusImplementation implements IClien
143
164
  validator.assertHasFunction(socket, 'on');
144
165
  validator.assertHasFunction(socket, 'emit');
145
166
 
167
+ // Remove only the EventBus listeners from the previous primary
168
+ // socket. Other SDK lifecycle listeners and named agent sockets
169
+ // have independent owners and must survive a primary reconnect.
170
+ this.removePrimarySocketListeners();
171
+
146
172
  // Store the socket
147
173
  this.socket = socket;
148
174
 
@@ -157,6 +183,17 @@ export class ClientEventBus extends BaseEventBusImplementation implements IClien
157
183
  return this;
158
184
  }
159
185
 
186
+ /** Release only the primary transport and the exact callbacks this bus owns. */
187
+ public clearClientSocket(socket?: SocketLike): this {
188
+ if (socket && this.socket !== socket) {
189
+ return this;
190
+ }
191
+
192
+ this.removePrimarySocketListeners();
193
+ this.socket = null;
194
+ return this;
195
+ }
196
+
160
197
  /**
161
198
  * Forward an emitted event to the primary socket.
162
199
  *
@@ -187,9 +224,6 @@ export class ClientEventBus extends BaseEventBusImplementation implements IClien
187
224
  validator.assertHasFunction(socket, 'on');
188
225
  validator.assertHasFunction(socket, 'onAny');
189
226
 
190
- // Remove previous listeners if any
191
- this.removeAllSocketListeners();
192
-
193
227
  // Catch every event with onAny (Socket.IO specific)
194
228
  if (socket.onAny) {
195
229
  // Build a set of events that are handled specifically by MxfService.setupControlLoopSocketListeners()
@@ -201,7 +235,7 @@ export class ClientEventBus extends BaseEventBusImplementation implements IClien
201
235
  ...Object.values(OrparEvents)
202
236
  ]);
203
237
 
204
- const handleAnyEvent = (event: string, ...args: any[]) => {
238
+ const handleAnyEvent = (event: string, ...args: unknown[]): void => {
205
239
  // Skip processing of some internal socket.io events
206
240
  if (event.startsWith('$') || event === 'ping' || event === 'pong') {
207
241
  return;
@@ -215,11 +249,7 @@ export class ClientEventBus extends BaseEventBusImplementation implements IClien
215
249
 
216
250
  const payload = args.length > 0 ? args[0] : {};
217
251
 
218
- // Forward to event bus
219
- this.eventSubject.next({
220
- type: event,
221
- payload
222
- });
252
+ this.publishInboundEvent(event, payload);
223
253
  };
224
254
 
225
255
  socket.onAny(handleAnyEvent);
@@ -266,7 +296,7 @@ export class ClientEventBus extends BaseEventBusImplementation implements IClien
266
296
  ...Object.values(OrparEvents)
267
297
  ]);
268
298
 
269
- const handleAnyEvent = (event: string, ...args: any[]) => {
299
+ const handleAnyEvent = (event: string, ...args: unknown[]): void => {
270
300
  // Skip internal socket.io events
271
301
  if (event.startsWith('$') || event === 'ping' || event === 'pong') {
272
302
  return;
@@ -276,7 +306,7 @@ export class ClientEventBus extends BaseEventBusImplementation implements IClien
276
306
  return;
277
307
  }
278
308
  const payload = args.length > 0 ? args[0] : {};
279
- this.eventSubject.next({ type: event, payload });
309
+ this.publishInboundEvent(event, payload);
280
310
  };
281
311
 
282
312
  socket.onAny(handleAnyEvent);
@@ -310,14 +340,13 @@ export class ClientEventBus extends BaseEventBusImplementation implements IClien
310
340
  }
311
341
 
312
342
  try {
313
- // Remove onAny listener
314
- if (socket.offAny) {
315
- socket.offAny();
316
- }
317
-
318
343
  // Remove individually tracked listeners
319
344
  const listeners = this.socketListenersRegistry.get(socketId);
320
345
  if (listeners) {
346
+ const anyListener = listeners.get('any');
347
+ if (socket.offAny && anyListener) {
348
+ socket.offAny(anyListener);
349
+ }
321
350
  for (const [event, listener] of listeners.entries()) {
322
351
  if (event !== 'any') {
323
352
  socket.off(event, listener);
@@ -384,6 +413,8 @@ export class ClientEventBus extends BaseEventBusImplementation implements IClien
384
413
  * @returns this (for chaining)
385
414
  */
386
415
  public disconnect(): this {
416
+ this.inboundEventKeys.clear();
417
+
387
418
  // Unregister all sockets in the registry
388
419
  for (const socketId of Array.from(this.socketRegistry.keys())) {
389
420
  this.unregisterSocket(socketId);
@@ -414,42 +445,17 @@ export class ClientEventBus extends BaseEventBusImplementation implements IClien
414
445
  * @returns this (for chaining)
415
446
  */
416
447
  public removeAllSocketListeners(): this {
417
- // Clean up primary socket listeners
418
- if (this.socket) {
419
- try {
420
- // Remove "any" listener if registered
421
- if (this.socket.offAny && this.socketListeners.has('any')) {
422
- this.socket.offAny();
423
- this.socketListeners.delete('any');
424
- }
425
-
426
- // Remove individual event listeners
427
- for (const [event, listener] of this.socketListeners.entries()) {
428
- if (event !== 'any') {
429
- this.socket.off(event, listener);
430
- }
431
- }
432
-
433
- // Clear listener map
434
- this.socketListeners.clear();
435
-
436
- // Use Socket.IO's removeAllListeners if available
437
- if (this.socket.removeAllListeners) {
438
- this.socket.removeAllListeners();
439
- }
440
- } catch (error) {
441
- logger.error('[ClientEventBus] Error removing primary socket listeners:', error instanceof Error ? error.message : String(error));
442
- }
443
- }
448
+ this.removePrimarySocketListeners();
444
449
 
445
450
  // Clean up registered sockets' listeners
446
451
  for (const [socketId, socket] of this.socketRegistry.entries()) {
447
452
  try {
448
- if (socket.offAny) {
449
- socket.offAny();
450
- }
451
453
  const listeners = this.socketListenersRegistry.get(socketId);
452
454
  if (listeners) {
455
+ const anyListener = listeners.get('any');
456
+ if (socket.offAny && anyListener) {
457
+ socket.offAny(anyListener);
458
+ }
453
459
  for (const [event, listener] of listeners.entries()) {
454
460
  if (event !== 'any') {
455
461
  socket.off(event, listener);
@@ -465,6 +471,34 @@ export class ClientEventBus extends BaseEventBusImplementation implements IClien
465
471
 
466
472
  return this;
467
473
  }
474
+
475
+ /** Remove only listeners installed by this bus on the primary socket. */
476
+ private removePrimarySocketListeners(): void {
477
+ if (!this.socket) {
478
+ this.socketListeners.clear();
479
+ return;
480
+ }
481
+
482
+ try {
483
+ const anyListener = this.socketListeners.get('any');
484
+ if (this.socket.offAny && anyListener) {
485
+ this.socket.offAny(anyListener);
486
+ }
487
+
488
+ for (const [event, listener] of this.socketListeners.entries()) {
489
+ if (event !== 'any') {
490
+ this.socket.off(event, listener);
491
+ }
492
+ }
493
+
494
+ this.socketListeners.clear();
495
+ } catch (error) {
496
+ logger.error(
497
+ '[ClientEventBus] Error removing primary socket listeners:',
498
+ error instanceof Error ? error.message : String(error)
499
+ );
500
+ }
501
+ }
468
502
 
469
503
  /**
470
504
  * Remove all agent event listeners
@@ -482,5 +516,40 @@ export class ClientEventBus extends BaseEventBusImplementation implements IClien
482
516
  public isSocketConnected(): boolean {
483
517
  return !!this.socket && !!this.socket.connected;
484
518
  }
519
+
520
+ /**
521
+ * Check the exact named agent socket used by emitOn().
522
+ *
523
+ * Request/response services must not rely on emitOn()'s primary-socket
524
+ * fallback: when no transport is available that fallback only logs, which
525
+ * would leave an acknowledged request pending forever.
526
+ */
527
+ public isRegisteredSocketConnected(socketId: string): boolean {
528
+ validator.assertIsNonEmptyString(socketId);
529
+ return this.socketRegistry.get(socketId)?.connected === true;
530
+ }
531
+
532
+ /** Publish one exact typed socket envelope into the shared local bus. */
533
+ private publishInboundEvent(event: string, payload: unknown): void {
534
+ if (payload && typeof payload === 'object') {
535
+ const eventId = (payload as { eventId?: unknown }).eventId;
536
+ if (typeof eventId === 'string' && eventId.trim().length > 0) {
537
+ const eventKey = `${event}:${eventId}`;
538
+ if (this.inboundEventKeys.has(eventKey)) {
539
+ return;
540
+ }
541
+
542
+ this.inboundEventKeys.add(eventKey);
543
+ if (this.inboundEventKeys.size > MAX_INBOUND_EVENT_KEYS) {
544
+ const oldestKey = this.inboundEventKeys.values().next().value;
545
+ if (typeof oldestKey === 'string') {
546
+ this.inboundEventKeys.delete(oldestKey);
547
+ }
548
+ }
549
+ }
550
+ }
551
+
552
+ this.eventSubject.next({ type: event, payload });
553
+ }
485
554
 
486
555
  }
@@ -30,7 +30,7 @@
30
30
  * - All events are processed through RxJS for local subscriptions
31
31
  */
32
32
 
33
- import { Subject } from 'rxjs';
33
+ import { Subject, Subscription } from 'rxjs';
34
34
  import { EventMessage } from './EventBusBase.js';
35
35
  import { ClientEventBus, IClientEventBus } from './ClientEventBus.js';
36
36
  import { ServerEventBus, IServerEventBus } from './ServerEventBus.js';
@@ -85,6 +85,15 @@ export class EventBusImplementation {
85
85
  * This is primarily used for testing.
86
86
  */
87
87
  public reset(): void {
88
+ // A Subject completion disposes RxJS subscriptions, but it cannot
89
+ // detach Socket.IO callbacks registered outside RxJS. Release the
90
+ // exact transport listeners owned by the old buses before dropping
91
+ // their instances so a test reset/reinitialize cannot retain a dead
92
+ // bus through socket.onAny().
93
+ this.clientEventBus?.removeAllSocketListeners();
94
+ this.clientEventBus?.removeAllListeners();
95
+ this.serverEventBus?.cleanup();
96
+
88
97
  // Completing the Subject disposes every subscription both buses hold,
89
98
  // and their completion callbacks clear their registries.
90
99
  this.eventSubject.complete();
@@ -95,11 +104,32 @@ export class EventBusImplementation {
95
104
  this.serverEventBus = null;
96
105
  }
97
106
 
107
+ /**
108
+ * Drain async callbacks owned by either bus without instantiating a bus that
109
+ * was never used. Repeat across both buses because a settling server handler
110
+ * may synchronously publish work to the client bus (or vice versa).
111
+ */
112
+ public async drain(): Promise<void> {
113
+ for (;;) {
114
+ const buses = [this.serverEventBus, this.clientEventBus].filter(
115
+ (bus): bus is IServerEventBus | IClientEventBus => bus !== null
116
+ );
117
+ await Promise.all(buses.map(bus => bus.drain()));
118
+
119
+ const currentBuses = [this.serverEventBus, this.clientEventBus].filter(
120
+ (bus): bus is IServerEventBus | IClientEventBus => bus !== null
121
+ );
122
+ if (currentBuses.every(bus => bus.pendingHandlerCount() === 0)) {
123
+ return;
124
+ }
125
+ }
126
+ }
127
+
98
128
  /**
99
129
  * Log every event passing through the bus.
100
130
  * @returns A subscription that can be unsubscribed to stop debugging
101
131
  */
102
- public debug() {
132
+ public debug(): Subscription {
103
133
  return this.eventSubject.subscribe(event => {
104
134
  logger.debug(`event '${event.type}': ${JSON.stringify(event.payload)}`);
105
135
  });
@@ -41,20 +41,25 @@ import { Logger } from '../utils/Logger.js';
41
41
  * rejection can kill the bus subscription or vanish silently. Errors are
42
42
  * logged loudly with the event name; the bus stays alive for other handlers.
43
43
  */
44
- export const invokeHandlerSafely = (
44
+ export const invokeHandlerSafely = <T>(
45
45
  busLogger: Logger,
46
46
  eventName: unknown,
47
- handler: (payload: any) => unknown,
48
- payload: any
47
+ handler: (payload: T) => unknown,
48
+ payload: T,
49
+ trackAsyncWork?: (work: Promise<void>) => void
49
50
  ): void => {
50
51
  try {
51
52
  const result = handler(payload);
52
53
  if (result && typeof (result as Promise<unknown>).then === 'function') {
53
- (result as Promise<unknown>).catch((error: unknown) => {
54
- busLogger.error(
55
- `Async handler for '${String(eventName)}' rejected: ${error instanceof Error ? error.stack : String(error)}`
56
- );
57
- });
54
+ const work = Promise.resolve(result).then(
55
+ () => undefined,
56
+ (error: unknown): void => {
57
+ busLogger.error(
58
+ `Async handler for '${String(eventName)}' rejected: ${error instanceof Error ? error.stack : String(error)}`
59
+ );
60
+ }
61
+ );
62
+ trackAsyncWork?.(work);
58
63
  }
59
64
  } catch (error) {
60
65
  busLogger.error(
@@ -76,7 +81,7 @@ export interface SocketLike {
76
81
  off: (event: string, listener?: (...args: any[]) => void) => any;
77
82
  emit: (event: string, ...args: any[]) => any;
78
83
  onAny?: (listener: (event: string, ...args: any[]) => void) => any;
79
- offAny?: () => any;
84
+ offAny?: (listener?: (event: string, ...args: unknown[]) => void) => unknown;
80
85
  removeAllListeners?: () => any;
81
86
  disconnect?: () => void;
82
87
  data?: {
@@ -161,7 +166,13 @@ export interface EventBusBase {
161
166
  * @param event Event name
162
167
  * @param payload Event payload
163
168
  */
164
- emit?(event: AnyEventName, payload: any): void;
169
+ emit?<K extends AnyEventName>(event: K, payload: PayloadOf<K>): void;
170
+
171
+ /**
172
+ * Publish an event to local subscribers without forwarding it to the
173
+ * transport. Used when a socket event has already crossed the transport.
174
+ */
175
+ emitLocal<K extends AnyEventName>(event: K, payload: PayloadOf<K>): void;
165
176
 
166
177
  /**
167
178
  * Check if an event has subscribers
@@ -190,6 +201,12 @@ export interface EventBusBase {
190
201
  * @returns this (for chaining)
191
202
  */
192
203
  removeAllListeners(event?: AnyEventName): this;
204
+
205
+ /** Wait for every async handler already accepted by this bus to settle. */
206
+ drain(): Promise<void>;
207
+
208
+ /** Number of accepted async handlers that have not settled yet. */
209
+ pendingHandlerCount(): number;
193
210
  }
194
211
 
195
212
  /**
@@ -232,7 +249,7 @@ export function isReservedEvent(event: string): boolean {
232
249
  * the wrapper (as an earlier version did) meant off() could never find the entry,
233
250
  * so the RxJS subscription was never disposed and the "one-shot" listener kept
234
251
  * firing on every later event. take(1) now disposes the subscription itself and
235
- * the completion callback clears the registry entry.
252
+ * the subscription teardown clears the registry entry.
236
253
  *
237
254
  * ## Emit contract
238
255
  *
@@ -258,7 +275,7 @@ export abstract class BaseEventBusImplementation implements EventBusBase {
258
275
  * A handler may be registered more than once for the same event, so the
259
276
  * value is a list; off() disposes all of them.
260
277
  */
261
- protected handlerSubscriptions: Map<string, Map<EventHandler<any>, Subscription[]>> = new Map();
278
+ protected handlerSubscriptions: Map<string, Map<EventHandler<never>, Subscription[]>> = new Map();
262
279
 
263
280
  /** event → number of live per-event subscribers. */
264
281
  protected subscriptionCountMap: Map<string, number> = new Map();
@@ -266,6 +283,9 @@ export abstract class BaseEventBusImplementation implements EventBusBase {
266
283
  /** onAll() subscriptions, which are not tied to any single event name. */
267
284
  protected allEventSubscriptions: Set<Subscription> = new Set();
268
285
 
286
+ /** Async handler work accepted by this bus and still using server resources. */
287
+ private readonly pendingHandlerWork = new Set<Promise<void>>();
288
+
269
289
  private debugMode: boolean = false;
270
290
  private debugSubscription: Subscription | null = null;
271
291
 
@@ -319,6 +339,37 @@ export abstract class BaseEventBusImplementation implements EventBusBase {
319
339
  }
320
340
  }
321
341
 
342
+ /**
343
+ * Deliver an event to this bus's local subscribers only.
344
+ *
345
+ * Socket ingress must use this path when adapting a received event for SDK
346
+ * subscribers. Calling emit()/emitOn() there sends the same event back over
347
+ * the socket and creates a transport echo loop for bidirectional names.
348
+ */
349
+ public emitLocal<K extends AnyEventName>(event: K, payload: PayloadOf<K>): void {
350
+ try {
351
+ validateEventName(event);
352
+
353
+ if (payload === undefined || payload === null) {
354
+ throw new Error(
355
+ `Event '${String(event)}' was emitted locally with a ` +
356
+ `${payload === null ? 'null' : 'undefined'} payload. ` +
357
+ 'Build the payload with a helper from EventPayloadSchema.'
358
+ );
359
+ }
360
+
361
+ this.validateEventPayload(String(event), payload);
362
+ this.eventSubject.next({ type: event, payload });
363
+ } catch (error) {
364
+ this.busLogger.error(
365
+ `Error emitting local event '${String(event)}': ` +
366
+ `${error instanceof Error ? error.message : String(error)}`
367
+ );
368
+ this.reportEmitFailure(event, error);
369
+ throw error;
370
+ }
371
+ }
372
+
322
373
  /**
323
374
  * Publish a properly-formed error event describing a failed emit.
324
375
  *
@@ -375,11 +426,17 @@ export abstract class BaseEventBusImplementation implements EventBusBase {
375
426
  .subscribe({
376
427
  // Isolate throws/rejections so one bad handler cannot kill the
377
428
  // subscription or fail silently.
378
- next: (payload) => invokeHandlerSafely(this.busLogger, event, handler, payload),
379
- complete: () => this.forgetSubscription(event, handler, subscription)
429
+ next: (payload) => invokeHandlerSafely(
430
+ this.busLogger,
431
+ event,
432
+ handler,
433
+ payload,
434
+ this.trackAsyncHandlerWork
435
+ )
380
436
  });
381
437
 
382
438
  this.registerHandler(event, handler, subscription);
439
+ subscription.add(() => this.forgetSubscription(event, handler, subscription));
383
440
 
384
441
  return subscription;
385
442
  }
@@ -388,7 +445,7 @@ export abstract class BaseEventBusImplementation implements EventBusBase {
388
445
  * Subscribe to an event for exactly one delivery.
389
446
  *
390
447
  * take(1) disposes the RxJS subscription after the first event, and the
391
- * completion callback removes it from the registry, so nothing is left
448
+ * subscription teardown removes it from the registry, so nothing is left
392
449
  * behind. off(event, handler) still works before the event arrives.
393
450
  *
394
451
  * @param event Event name
@@ -398,13 +455,6 @@ export abstract class BaseEventBusImplementation implements EventBusBase {
398
455
  public once<K extends AnyEventName>(event: K, handler: EventHandler<PayloadOf<K>>): Subscription {
399
456
  validateEventName(event);
400
457
 
401
- // The completion callback needs the Subscription, which only exists once
402
- // subscribe() returns. With a plain Subject the source cannot emit during
403
- // subscribe(), but this flag keeps the bookkeeping correct even if the
404
- // Subject were ever swapped for a replaying one.
405
- let registered = false;
406
- let completedBeforeRegistration = false;
407
-
408
458
  const subscription: Subscription = this.eventSubject
409
459
  .pipe(
410
460
  filter((e: EventMessage) => e.type === event),
@@ -412,22 +462,17 @@ export abstract class BaseEventBusImplementation implements EventBusBase {
412
462
  map((e: EventMessage) => e.payload)
413
463
  )
414
464
  .subscribe({
415
- next: (payload) => invokeHandlerSafely(this.busLogger, event, handler, payload),
416
- complete: () => {
417
- if (registered) {
418
- this.forgetSubscription(event, handler, subscription);
419
- } else {
420
- completedBeforeRegistration = true;
421
- }
422
- }
465
+ next: (payload) => invokeHandlerSafely(
466
+ this.busLogger,
467
+ event,
468
+ handler,
469
+ payload,
470
+ this.trackAsyncHandlerWork
471
+ )
423
472
  });
424
473
 
425
474
  this.registerHandler(event, handler, subscription);
426
- registered = true;
427
-
428
- if (completedBeforeRegistration) {
429
- this.forgetSubscription(event, handler, subscription);
430
- }
475
+ subscription.add(() => this.forgetSubscription(event, handler, subscription));
431
476
 
432
477
  return subscription;
433
478
  }
@@ -441,11 +486,18 @@ export abstract class BaseEventBusImplementation implements EventBusBase {
441
486
  public onAll(listener: (eventType: string, payload: any) => void): Subscription {
442
487
  const subscription = this.eventSubject.subscribe({
443
488
  next: (e: EventMessage) =>
444
- invokeHandlerSafely(this.busLogger, e.type, (payload) => listener(e.type, payload), e.payload)
489
+ invokeHandlerSafely(
490
+ this.busLogger,
491
+ e.type,
492
+ (payload) => listener(e.type, payload),
493
+ e.payload,
494
+ this.trackAsyncHandlerWork
495
+ )
445
496
  });
446
497
 
447
498
  this.subscriptions.push(subscription);
448
499
  this.allEventSubscriptions.add(subscription);
500
+ subscription.add(() => this.forgetSubscriptionEverywhere(subscription));
449
501
 
450
502
  return subscription;
451
503
  }
@@ -472,6 +524,7 @@ export abstract class BaseEventBusImplementation implements EventBusBase {
472
524
  } else {
473
525
  this.categorySubscriptions.set(category, [subscription]);
474
526
  }
527
+ subscription.add(() => this.forgetCategorySubscription(category, subscription));
475
528
 
476
529
  return subscription;
477
530
  }
@@ -528,7 +581,7 @@ export abstract class BaseEventBusImplementation implements EventBusBase {
528
581
  return this;
529
582
  }
530
583
 
531
- for (const subscription of categorySubs) {
584
+ for (const subscription of [...categorySubs]) {
532
585
  subscription.unsubscribe();
533
586
  this.forgetSubscriptionEverywhere(subscription);
534
587
  }
@@ -614,6 +667,22 @@ export abstract class BaseEventBusImplementation implements EventBusBase {
614
667
  return this.allEventSubscriptions.size;
615
668
  }
616
669
 
670
+ /** Number of async callbacks which have not yet settled. */
671
+ public pendingHandlerCount(): number {
672
+ return this.pendingHandlerWork.size;
673
+ }
674
+
675
+ /**
676
+ * Wait for all accepted async callbacks, including callbacks accepted by a
677
+ * callback while the current batch is settling. Ingress must be stopped
678
+ * before calling this during process shutdown so the set eventually closes.
679
+ */
680
+ public async drain(): Promise<void> {
681
+ while (this.pendingHandlerWork.size > 0) {
682
+ await Promise.all([...this.pendingHandlerWork]);
683
+ }
684
+ }
685
+
617
686
  /**
618
687
  * Log every event passing through this bus at debug level.
619
688
  *
@@ -648,10 +717,18 @@ export abstract class BaseEventBusImplementation implements EventBusBase {
648
717
  // Internal bookkeeping
649
718
  // ---------------------------------------------------------------------
650
719
 
720
+ /** Keep shutdown ownership of an async handler until it has settled. */
721
+ private readonly trackAsyncHandlerWork = (work: Promise<void>): void => {
722
+ this.pendingHandlerWork.add(work);
723
+ void work.then((): void => {
724
+ this.pendingHandlerWork.delete(work);
725
+ });
726
+ };
727
+
651
728
  /** Record a new per-event subscription across all three structures. */
652
729
  private registerHandler(
653
730
  event: AnyEventName,
654
- handler: EventHandler<any>,
731
+ handler: EventHandler<never>,
655
732
  subscription: Subscription
656
733
  ): void {
657
734
  this.subscriptions.push(subscription);
@@ -679,7 +756,7 @@ export abstract class BaseEventBusImplementation implements EventBusBase {
679
756
  */
680
757
  private forgetSubscription(
681
758
  event: AnyEventName,
682
- handler: EventHandler<any>,
759
+ handler: EventHandler<never>,
683
760
  subscription: Subscription
684
761
  ): void {
685
762
  const eventHandlers = this.handlerSubscriptions.get(event);
@@ -728,6 +805,22 @@ export abstract class BaseEventBusImplementation implements EventBusBase {
728
805
  }
729
806
  }
730
807
 
808
+ /** Drop a subscription from one category without affecting other registrations. */
809
+ private forgetCategorySubscription(category: string, subscription: Subscription): void {
810
+ const categorySubs = this.categorySubscriptions.get(category);
811
+ if (!categorySubs) {
812
+ return;
813
+ }
814
+
815
+ const index = categorySubs.indexOf(subscription);
816
+ if (index !== -1) {
817
+ categorySubs.splice(index, 1);
818
+ }
819
+ if (categorySubs.length === 0) {
820
+ this.categorySubscriptions.delete(category);
821
+ }
822
+ }
823
+
731
824
  // ---------------------------------------------------------------------
732
825
  // Validation
733
826
  // ---------------------------------------------------------------------