claude-flow-novice 1.6.1 → 1.6.2

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 (244) hide show
  1. package/.claude/agents/cfn-loop/product-owner.md +54 -4
  2. package/.claude/commands/cfn-claude-sync.md +303 -0
  3. package/.claude/commands/cfn-loop-epic.md +290 -0
  4. package/.claude/commands/cfn-loop-single.md +168 -0
  5. package/.claude/commands/cfn-loop-sprints.md +384 -0
  6. package/.claude/commands/cfn-loop.md +180 -0
  7. package/.claude/commands/metrics-summary.md +58 -0
  8. package/.claude/commands/parse-epic.md +357 -0
  9. package/.claude/settings.json +4 -4
  10. package/.claude/settings.local.json +9 -2
  11. package/.claude-flow-novice/.claude/agents/cfn-loop/product-owner.md +792 -0
  12. package/.claude-flow-novice/dist/mcp/server.js +21 -2
  13. package/.claude-flow-novice/dist/src/api/claude-client.js +138 -3
  14. package/.claude-flow-novice/dist/src/api/claude-client.js.map +1 -1
  15. package/.claude-flow-novice/dist/src/cfn-loop/phase-orchestrator-example.js +1 -1
  16. package/.claude-flow-novice/dist/src/cfn-loop/scope-control.js +247 -0
  17. package/.claude-flow-novice/dist/src/cfn-loop/scope-control.js.map +1 -0
  18. package/.claude-flow-novice/dist/src/cli/commands/swarm.js +32 -15
  19. package/.claude-flow-novice/dist/src/cli/commands/swarm.js.map +1 -1
  20. package/.claude-flow-novice/dist/src/cli/commands/transparency.js +455 -0
  21. package/.claude-flow-novice/dist/src/cli/commands/transparency.js.map +1 -0
  22. package/.claude-flow-novice/dist/src/cli/simple-commands/init/templates/CLAUDE.md +129 -13
  23. package/.claude-flow-novice/dist/src/components/visualizations/index.js +9 -0
  24. package/.claude-flow-novice/dist/src/components/visualizations/index.js.map +1 -0
  25. package/.claude-flow-novice/dist/src/coordination/adapters/v1-coordinator-adapter.js +462 -0
  26. package/.claude-flow-novice/dist/src/coordination/adapters/v1-coordinator-adapter.js.map +1 -0
  27. package/.claude-flow-novice/dist/src/coordination/config-translator.js +248 -0
  28. package/.claude-flow-novice/dist/src/coordination/config-translator.js.map +1 -0
  29. package/.claude-flow-novice/dist/src/coordination/coordination-toggle.js +287 -0
  30. package/.claude-flow-novice/dist/src/coordination/coordination-toggle.js.map +1 -0
  31. package/.claude-flow-novice/dist/src/coordination/distributed-consensus.js +68 -9
  32. package/.claude-flow-novice/dist/src/coordination/distributed-consensus.js.map +1 -1
  33. package/.claude-flow-novice/dist/src/coordination/feature-flags.js +166 -0
  34. package/.claude-flow-novice/dist/src/coordination/feature-flags.js.map +1 -0
  35. package/.claude-flow-novice/dist/src/coordination/queen-agent.js +18 -4
  36. package/.claude-flow-novice/dist/src/coordination/queen-agent.js.map +1 -1
  37. package/.claude-flow-novice/dist/src/coordination/role-assignment.js +6 -110
  38. package/.claude-flow-novice/dist/src/coordination/role-assignment.js.map +1 -1
  39. package/.claude-flow-novice/dist/src/coordination/v2/cache/artifact-cache-optimizer.js +632 -0
  40. package/.claude-flow-novice/dist/src/coordination/v2/cache/artifact-cache-optimizer.js.map +1 -0
  41. package/.claude-flow-novice/dist/src/coordination/v2/cache/index.js +11 -0
  42. package/.claude-flow-novice/dist/src/coordination/v2/cache/index.js.map +1 -0
  43. package/.claude-flow-novice/dist/src/coordination/v2/checkpoints/checkpoint-compressor.js +318 -0
  44. package/.claude-flow-novice/dist/src/coordination/v2/checkpoints/checkpoint-compressor.js.map +1 -0
  45. package/.claude-flow-novice/dist/src/coordination/v2/coordinators/cascading-shutdown.example.js +364 -0
  46. package/.claude-flow-novice/dist/src/coordination/v2/coordinators/cascading-shutdown.example.js.map +1 -0
  47. package/.claude-flow-novice/dist/src/coordination/v2/coordinators/cascading-shutdown.js +492 -0
  48. package/.claude-flow-novice/dist/src/coordination/v2/coordinators/cascading-shutdown.js.map +1 -0
  49. package/.claude-flow-novice/dist/src/coordination/v2/coordinators/hierarchical-coordinator.js +786 -0
  50. package/.claude-flow-novice/dist/src/coordination/v2/coordinators/hierarchical-coordinator.js.map +1 -0
  51. package/.claude-flow-novice/dist/src/coordination/v2/coordinators/index.js +16 -0
  52. package/.claude-flow-novice/dist/src/coordination/v2/coordinators/index.js.map +1 -0
  53. package/.claude-flow-novice/dist/src/coordination/v2/coordinators/parent-child-manager.js +342 -0
  54. package/.claude-flow-novice/dist/src/coordination/v2/coordinators/parent-child-manager.js.map +1 -0
  55. package/.claude-flow-novice/dist/src/coordination/v2/coordinators/swarm-coordinator-v2.js +601 -0
  56. package/.claude-flow-novice/dist/src/coordination/v2/coordinators/swarm-coordinator-v2.js.map +1 -0
  57. package/.claude-flow-novice/dist/src/coordination/v2/core/help-request-metrics.js +211 -0
  58. package/.claude-flow-novice/dist/src/coordination/v2/core/help-request-metrics.js.map +1 -0
  59. package/.claude-flow-novice/dist/src/coordination/v2/core/index.js +1 -0
  60. package/.claude-flow-novice/dist/src/coordination/v2/core/index.js.map +1 -1
  61. package/.claude-flow-novice/dist/src/coordination/v2/core/message-broker.js +365 -6
  62. package/.claude-flow-novice/dist/src/coordination/v2/core/message-broker.js.map +1 -1
  63. package/.claude-flow-novice/dist/src/coordination/v2/core/resource-manager-safe.js +478 -0
  64. package/.claude-flow-novice/dist/src/coordination/v2/core/resource-manager-safe.js.map +1 -0
  65. package/.claude-flow-novice/dist/src/coordination/v2/core/state-machine-config.js +5 -2
  66. package/.claude-flow-novice/dist/src/coordination/v2/core/state-machine-config.js.map +1 -1
  67. package/.claude-flow-novice/dist/src/coordination/v2/core/state-machine.js +189 -0
  68. package/.claude-flow-novice/dist/src/coordination/v2/core/state-machine.js.map +1 -1
  69. package/.claude-flow-novice/dist/src/coordination/v2/deadlock/deadlock-detector.js +424 -0
  70. package/.claude-flow-novice/dist/src/coordination/v2/deadlock/deadlock-detector.js.map +1 -0
  71. package/.claude-flow-novice/dist/src/coordination/v2/deadlock/index.js +9 -0
  72. package/.claude-flow-novice/dist/src/coordination/v2/deadlock/index.js.map +1 -0
  73. package/.claude-flow-novice/dist/src/coordination/v2/deadlock/resource-manager.js +669 -0
  74. package/.claude-flow-novice/dist/src/coordination/v2/deadlock/resource-manager.js.map +1 -0
  75. package/.claude-flow-novice/dist/src/coordination/v2/dependency/artifact-storage.js +451 -0
  76. package/.claude-flow-novice/dist/src/coordination/v2/dependency/artifact-storage.js.map +1 -0
  77. package/.claude-flow-novice/dist/src/coordination/v2/dependency/cycle-detector.js +271 -0
  78. package/.claude-flow-novice/dist/src/coordination/v2/dependency/cycle-detector.js.map +1 -0
  79. package/.claude-flow-novice/dist/src/coordination/v2/dependency/dependency-graph.js +335 -0
  80. package/.claude-flow-novice/dist/src/coordination/v2/dependency/dependency-graph.js.map +1 -0
  81. package/.claude-flow-novice/dist/src/coordination/v2/dependency/dependency-manager.js +439 -0
  82. package/.claude-flow-novice/dist/src/coordination/v2/dependency/dependency-manager.js.map +1 -0
  83. package/.claude-flow-novice/dist/src/coordination/v2/dependency/dependency-request.js +92 -0
  84. package/.claude-flow-novice/dist/src/coordination/v2/dependency/dependency-request.js.map +1 -0
  85. package/.claude-flow-novice/dist/src/coordination/v2/dependency/index.js +21 -0
  86. package/.claude-flow-novice/dist/src/coordination/v2/dependency/index.js.map +1 -0
  87. package/.claude-flow-novice/dist/src/coordination/v2/dependency/topological-sort.js +223 -0
  88. package/.claude-flow-novice/dist/src/coordination/v2/dependency/topological-sort.js.map +1 -0
  89. package/.claude-flow-novice/dist/src/coordination/v2/help-system/help-coordinator.js +436 -0
  90. package/.claude-flow-novice/dist/src/coordination/v2/help-system/help-coordinator.js.map +1 -0
  91. package/.claude-flow-novice/dist/src/coordination/v2/help-system/help-matcher.js +278 -0
  92. package/.claude-flow-novice/dist/src/coordination/v2/help-system/help-matcher.js.map +1 -0
  93. package/.claude-flow-novice/dist/src/coordination/v2/help-system/help-request-handler.js +317 -0
  94. package/.claude-flow-novice/dist/src/coordination/v2/help-system/help-request-handler.js.map +1 -0
  95. package/.claude-flow-novice/dist/src/coordination/v2/help-system/help-request.js +273 -0
  96. package/.claude-flow-novice/dist/src/coordination/v2/help-system/help-request.js.map +1 -0
  97. package/.claude-flow-novice/dist/src/coordination/v2/help-system/index.js +15 -0
  98. package/.claude-flow-novice/dist/src/coordination/v2/help-system/index.js.map +1 -0
  99. package/.claude-flow-novice/dist/src/coordination/v2/help-system/waiting-agent-pool.js +512 -0
  100. package/.claude-flow-novice/dist/src/coordination/v2/help-system/waiting-agent-pool.js.map +1 -0
  101. package/.claude-flow-novice/dist/src/coordination/v2/index.js +6 -0
  102. package/.claude-flow-novice/dist/src/coordination/v2/index.js.map +1 -1
  103. package/.claude-flow-novice/dist/src/coordination/v2/integration/help-deadlock-integration.js +557 -0
  104. package/.claude-flow-novice/dist/src/coordination/v2/integration/help-deadlock-integration.js.map +1 -0
  105. package/.claude-flow-novice/dist/src/coordination/v2/integration/index.js +14 -0
  106. package/.claude-flow-novice/dist/src/coordination/v2/integration/index.js.map +1 -0
  107. package/.claude-flow-novice/dist/src/coordination/v2/integration/message-bus-completion-integration.example.js +212 -0
  108. package/.claude-flow-novice/dist/src/coordination/v2/integration/message-bus-completion-integration.example.js.map +1 -0
  109. package/.claude-flow-novice/dist/src/coordination/v2/integration/message-bus-completion-integration.js +552 -0
  110. package/.claude-flow-novice/dist/src/coordination/v2/integration/message-bus-completion-integration.js.map +1 -0
  111. package/.claude-flow-novice/dist/src/coordination/v2/memory/dependency-storage.js +367 -0
  112. package/.claude-flow-novice/dist/src/coordination/v2/memory/dependency-storage.js.map +1 -0
  113. package/.claude-flow-novice/dist/src/coordination/v2/memory/index.js +1 -0
  114. package/.claude-flow-novice/dist/src/coordination/v2/memory/index.js.map +1 -1
  115. package/.claude-flow-novice/dist/src/coordination/v2/messaging/channel.js +371 -0
  116. package/.claude-flow-novice/dist/src/coordination/v2/messaging/channel.js.map +1 -0
  117. package/.claude-flow-novice/dist/src/coordination/v2/messaging/channels/dependency-channel.js +355 -0
  118. package/.claude-flow-novice/dist/src/coordination/v2/messaging/channels/dependency-channel.js.map +1 -0
  119. package/.claude-flow-novice/dist/src/coordination/v2/messaging/channels/help-channel.js +424 -0
  120. package/.claude-flow-novice/dist/src/coordination/v2/messaging/channels/help-channel.js.map +1 -0
  121. package/.claude-flow-novice/dist/src/coordination/v2/messaging/channels/index.js +16 -0
  122. package/.claude-flow-novice/dist/src/coordination/v2/messaging/channels/index.js.map +1 -0
  123. package/.claude-flow-novice/dist/src/coordination/v2/messaging/channels/state-channel.js +295 -0
  124. package/.claude-flow-novice/dist/src/coordination/v2/messaging/channels/state-channel.js.map +1 -0
  125. package/.claude-flow-novice/dist/src/coordination/v2/messaging/channels/task-channel.js +411 -0
  126. package/.claude-flow-novice/dist/src/coordination/v2/messaging/channels/task-channel.js.map +1 -0
  127. package/.claude-flow-novice/dist/src/coordination/v2/messaging/index.js +14 -0
  128. package/.claude-flow-novice/dist/src/coordination/v2/messaging/index.js.map +1 -0
  129. package/.claude-flow-novice/dist/src/coordination/v2/messaging/message-bus.js +387 -0
  130. package/.claude-flow-novice/dist/src/coordination/v2/messaging/message-bus.js.map +1 -0
  131. package/.claude-flow-novice/dist/src/coordination/v2/messaging/message-persistence.js +589 -0
  132. package/.claude-flow-novice/dist/src/coordination/v2/messaging/message-persistence.js.map +1 -0
  133. package/.claude-flow-novice/dist/src/coordination/v2/messaging/message-router.js +444 -0
  134. package/.claude-flow-novice/dist/src/coordination/v2/messaging/message-router.js.map +1 -0
  135. package/.claude-flow-novice/dist/src/coordination/v2/sdk/checkpoint-manager.js +29 -8
  136. package/.claude-flow-novice/dist/src/coordination/v2/sdk/checkpoint-manager.js.map +1 -1
  137. package/.claude-flow-novice/dist/src/coordination/v2/sdk/help-coordinator.js +470 -0
  138. package/.claude-flow-novice/dist/src/coordination/v2/sdk/help-coordinator.js.map +1 -0
  139. package/.claude-flow-novice/dist/src/coordination/v2/sdk/hierarchical-background-integration.js +450 -0
  140. package/.claude-flow-novice/dist/src/coordination/v2/sdk/hierarchical-background-integration.js.map +1 -0
  141. package/.claude-flow-novice/dist/src/coordination/v2/sdk/index.js +5 -0
  142. package/.claude-flow-novice/dist/src/coordination/v2/sdk/index.js.map +1 -1
  143. package/.claude-flow-novice/dist/src/coordination/v2/sdk/multi-level-control.js +545 -0
  144. package/.claude-flow-novice/dist/src/coordination/v2/sdk/multi-level-control.js.map +1 -0
  145. package/.claude-flow-novice/dist/src/coordination/v2/sdk/query-controller.js +44 -0
  146. package/.claude-flow-novice/dist/src/coordination/v2/sdk/query-controller.js.map +1 -1
  147. package/.claude-flow-novice/dist/src/coordination/v2/sdk/query-message-integration.js +415 -0
  148. package/.claude-flow-novice/dist/src/coordination/v2/sdk/query-message-integration.js.map +1 -0
  149. package/.claude-flow-novice/dist/src/coordination/v2/sdk/session-pool-optimizer.js +615 -0
  150. package/.claude-flow-novice/dist/src/coordination/v2/sdk/session-pool-optimizer.js.map +1 -0
  151. package/.claude-flow-novice/dist/src/coordination/v2/security/payload-validator.js +259 -0
  152. package/.claude-flow-novice/dist/src/coordination/v2/security/payload-validator.js.map +1 -0
  153. package/.claude-flow-novice/dist/src/coordination/v2/transparency/index.js +17 -0
  154. package/.claude-flow-novice/dist/src/coordination/v2/transparency/index.js.map +1 -0
  155. package/.claude-flow-novice/dist/src/coordination/v2/transparency/transparency-integration.js +357 -0
  156. package/.claude-flow-novice/dist/src/coordination/v2/transparency/transparency-integration.js.map +1 -0
  157. package/.claude-flow-novice/dist/src/coordination/v2/transparency/transparency-system.js +679 -0
  158. package/.claude-flow-novice/dist/src/coordination/v2/transparency/transparency-system.js.map +1 -0
  159. package/.claude-flow-novice/dist/src/core/agent-manager.js +30 -0
  160. package/.claude-flow-novice/dist/src/core/agent-manager.js.map +1 -1
  161. package/.claude-flow-novice/dist/src/mcp/server.js +21 -2
  162. package/.claude-flow-novice/dist/src/mcp/server.js.map +1 -1
  163. package/.claude-flow-novice/dist/src/observability/metrics-counter.js +268 -0
  164. package/.claude-flow-novice/dist/src/observability/metrics-counter.js.map +1 -0
  165. package/.claude-flow-novice/dist/src/observability/metrics-storage.js +265 -0
  166. package/.claude-flow-novice/dist/src/observability/metrics-storage.js.map +1 -0
  167. package/.claude-flow-novice/dist/src/observability/telemetry.js +26 -0
  168. package/.claude-flow-novice/dist/src/observability/telemetry.js.map +1 -1
  169. package/.claude-flow-novice/dist/src/providers/tiered-router.js +64 -10
  170. package/.claude-flow-novice/dist/src/providers/tiered-router.js.map +1 -1
  171. package/.claude-flow-novice/dist/src/providers/zai-provider.js +196 -97
  172. package/.claude-flow-novice/dist/src/providers/zai-provider.js.map +1 -1
  173. package/.claude-flow-novice/dist/src/slash-commands/cfn-claude-sync.js +533 -0
  174. package/.claude-flow-novice/dist/src/slash-commands/index.js +5 -0
  175. package/.claude-flow-novice/dist/src/slash-commands/metrics-summary-class.js +74 -0
  176. package/.claude-flow-novice/dist/src/slash-commands/metrics-summary.js +335 -0
  177. package/.claude-flow-novice/dist/src/slash-commands/register-all-commands.js +12 -0
  178. package/.claude-flow-novice/dist/src/verification/checkpoint-compression-demo.js +96 -0
  179. package/.claude-flow-novice/dist/src/verification/checkpoint-compression-demo.js.map +1 -0
  180. package/.claude-flow-novice/dist/src/verification/checkpoint-compression.js +406 -0
  181. package/.claude-flow-novice/dist/src/verification/checkpoint-compression.js.map +1 -0
  182. package/.claude-flow-novice/dist/src/verification/checkpoint-manager.js +35 -5
  183. package/.claude-flow-novice/dist/src/verification/checkpoint-manager.js.map +1 -1
  184. package/.claude-flow-novice/dist/src/web/api/config/api-config.js +186 -0
  185. package/.claude-flow-novice/dist/src/web/api/config/api-config.js.map +1 -0
  186. package/.claude-flow-novice/dist/src/web/api/middleware/auth.js +205 -0
  187. package/.claude-flow-novice/dist/src/web/api/middleware/auth.js.map +1 -0
  188. package/.claude-flow-novice/dist/src/web/api/middleware/cache.js +262 -0
  189. package/.claude-flow-novice/dist/src/web/api/middleware/cache.js.map +1 -0
  190. package/.claude-flow-novice/dist/src/web/api/middleware/error-handler.js +250 -0
  191. package/.claude-flow-novice/dist/src/web/api/middleware/error-handler.js.map +1 -0
  192. package/.claude-flow-novice/dist/src/web/api/middleware/request-logger.js +217 -0
  193. package/.claude-flow-novice/dist/src/web/api/middleware/request-logger.js.map +1 -0
  194. package/.claude-flow-novice/dist/src/web/api/middleware/validation.js +325 -0
  195. package/.claude-flow-novice/dist/src/web/api/middleware/validation.js.map +1 -0
  196. package/.claude-flow-novice/dist/src/web/api/routes/events.js +465 -0
  197. package/.claude-flow-novice/dist/src/web/api/routes/events.js.map +1 -0
  198. package/.claude-flow-novice/dist/src/web/api/routes/hierarchy.js +302 -0
  199. package/.claude-flow-novice/dist/src/web/api/routes/hierarchy.js.map +1 -0
  200. package/.claude-flow-novice/dist/src/web/api/routes/index.js +14 -0
  201. package/.claude-flow-novice/dist/src/web/api/routes/index.js.map +1 -0
  202. package/.claude-flow-novice/dist/src/web/api/routes/metrics.js +561 -0
  203. package/.claude-flow-novice/dist/src/web/api/routes/metrics.js.map +1 -0
  204. package/.claude-flow-novice/dist/src/web/api/routes/status.js +450 -0
  205. package/.claude-flow-novice/dist/src/web/api/routes/status.js.map +1 -0
  206. package/.claude-flow-novice/dist/src/web/api/server.js +451 -0
  207. package/.claude-flow-novice/dist/src/web/api/server.js.map +1 -0
  208. package/.claude-flow-novice/dist/src/web/dashboard/hooks/useWebSocket.js +385 -0
  209. package/.claude-flow-novice/dist/src/web/dashboard/hooks/useWebSocket.js.map +1 -0
  210. package/.claude-flow-novice/dist/src/web/dashboard/index.js +87 -0
  211. package/.claude-flow-novice/dist/src/web/dashboard/index.js.map +1 -0
  212. package/.claude-flow-novice/dist/src/web/dashboard/types.js +6 -0
  213. package/.claude-flow-novice/dist/src/web/dashboard/types.js.map +1 -0
  214. package/.claude-flow-novice/metrics.db +0 -0
  215. package/.claude-flow-novice/metrics.db-shm +0 -0
  216. package/.claude-flow-novice/metrics.db-wal +0 -0
  217. package/CLAUDE.md +29 -0
  218. package/README.md +27 -0
  219. package/config/hooks/post-edit-pipeline.js +36 -2
  220. package/examples/metrics-counter-demo.ts +106 -0
  221. package/examples/persistent-metrics-demo.ts +83 -0
  222. package/examples/phase-5-multi-level-control.ts +282 -0
  223. package/examples/session-pool-optimizer-example.ts +311 -0
  224. package/package.json +15 -3
  225. package/scripts/check-routing-stats.cjs +122 -0
  226. package/scripts/pre-publish-validation.cjs +212 -0
  227. package/scripts/test-provider-routing.cjs +228 -0
  228. package/scripts/test-routing-telemetry.cjs +147 -0
  229. package/scripts/test-zai-10k.cjs +81 -0
  230. package/scripts/test-zai-api.cjs +191 -0
  231. package/scripts/test-zai-diagnostic.cjs +151 -0
  232. package/scripts/test-zai-final.cjs +128 -0
  233. package/scripts/test-zai-with-env.cjs +85 -0
  234. package/scripts/validate-coordination-cli.js +69 -0
  235. package/scripts/validate-coordination-toggle-integration.cjs +501 -0
  236. package/src/cli/simple-commands/init/templates/CLAUDE.md +29 -0
  237. package/src/observability/metrics-counter.ts +347 -0
  238. package/src/observability/metrics-storage.ts +356 -0
  239. package/src/observability/telemetry.ts +658 -0
  240. package/src/slash-commands/cfn-claude-sync.js +533 -0
  241. package/src/slash-commands/index.js +5 -0
  242. package/src/slash-commands/metrics-summary-class.js +74 -0
  243. package/src/slash-commands/metrics-summary.js +335 -0
  244. package/src/slash-commands/register-all-commands.js +12 -0
@@ -0,0 +1,444 @@
1
+ /**
2
+ * Agent Coordination V2 - Message Router (Channel-Based)
3
+ *
4
+ * Priority queue-based message router for channel abstraction.
5
+ * Routes messages to channels with fairness guarantees and ordering preservation.
6
+ *
7
+ * Features:
8
+ * - Priority queue routing (high priority messages delivered first)
9
+ * - Channel-based isolation (4 specialized channels)
10
+ * - Fairness guarantees (prevent channel starvation)
11
+ * - Message ordering per channel (FIFO within channel)
12
+ * - Backpressure coordination across channels
13
+ *
14
+ * @module coordination/v2/messaging/message-router
15
+ */ import { PriorityQueue } from '../utils/priority-queue.js';
16
+ import { Channel, BackpressurePolicy } from './channel.js';
17
+ import { EventEmitter } from 'events';
18
+ /**
19
+ * Channel type enumeration.
20
+ * Defines the 4 specialized channels for agent coordination.
21
+ */ export var ChannelType = /*#__PURE__*/ function(ChannelType) {
22
+ /** Coordination messages (agent status, lifecycle) */ ChannelType["COORDINATION"] = "coordination";
23
+ /** Task execution messages (task assignment, completion) */ ChannelType["TASK"] = "task";
24
+ /** Help system messages (help requests, matches) */ ChannelType["HELP"] = "help";
25
+ /** Event notifications (system events, alerts) */ ChannelType["EVENT"] = "event";
26
+ return ChannelType;
27
+ }({});
28
+ /**
29
+ * Router events.
30
+ */ export var RouterEvent = /*#__PURE__*/ function(RouterEvent) {
31
+ /** Message routed to channel */ RouterEvent["MESSAGE_ROUTED"] = "message-routed";
32
+ /** Message dropped (no matching route) */ RouterEvent["MESSAGE_DROPPED"] = "message-dropped";
33
+ /** Backpressure threshold exceeded */ RouterEvent["BACKPRESSURE"] = "backpressure";
34
+ /** Router error */ RouterEvent["ERROR"] = "error";
35
+ return RouterEvent;
36
+ }({});
37
+ /**
38
+ * MessageRouter - Priority queue routing with channel isolation.
39
+ *
40
+ * Routes messages to specialized channels based on topic patterns,
41
+ * with priority queue ordering and fairness guarantees to prevent
42
+ * channel starvation.
43
+ *
44
+ * Architecture:
45
+ * - Global priority queue (all incoming messages)
46
+ * - 4 specialized channels (coordination, task, help, event)
47
+ * - Round-robin fairness scheduler
48
+ * - Per-channel FIFO ordering
49
+ *
50
+ * Performance:
51
+ * - route(): O(log n) for priority queue + O(1) channel lookup
52
+ * - processQueue(): O(k * q) where k = channel count, q = fairness quantum
53
+ *
54
+ * @example
55
+ * ```typescript
56
+ * const router = new MessageRouter({
57
+ * enableFairness: true,
58
+ * fairnessQuantum: 10
59
+ * });
60
+ *
61
+ * // Route messages
62
+ * router.route({
63
+ * id: 'msg-1',
64
+ * topic: 'agent.status',
65
+ * payload: { agentId: 'agent-1', status: 'online' },
66
+ * priority: 5,
67
+ * timestamp: Date.now()
68
+ * });
69
+ *
70
+ * // Process router queue
71
+ * await router.processQueue();
72
+ * ```
73
+ */ export class MessageRouter extends EventEmitter {
74
+ /** Configuration */ config;
75
+ /** Priority queue for incoming messages */ incomingQueue;
76
+ /** Channel instances by type */ channels;
77
+ /** Routing rules (pattern -> channel type) */ routingRules;
78
+ /** Statistics tracking */ stats;
79
+ /** Fairness scheduler state (last channel processed) */ fairnessState;
80
+ /** Processing lock */ isProcessing;
81
+ constructor(config = {}){
82
+ super();
83
+ this.config = {
84
+ channels: config.channels ?? {},
85
+ customRules: config.customRules ?? [],
86
+ enableFairness: config.enableFairness ?? true,
87
+ fairnessQuantum: config.fairnessQuantum ?? 10,
88
+ maxPendingMessages: config.maxPendingMessages ?? 5000
89
+ };
90
+ this.incomingQueue = new PriorityQueue();
91
+ this.channels = new Map();
92
+ this.routingRules = [];
93
+ this.isProcessing = false;
94
+ this.stats = {
95
+ totalRouted: 0,
96
+ totalDropped: 0,
97
+ messagesByChannel: new Map(),
98
+ routingLatencies: []
99
+ };
100
+ this.fairnessState = {
101
+ lastChannelIndex: -1,
102
+ channelOrder: [
103
+ "coordination",
104
+ "task",
105
+ "help",
106
+ "event"
107
+ ]
108
+ };
109
+ this.initializeChannels();
110
+ this.initializeRoutingRules();
111
+ }
112
+ /**
113
+ * Initialize the 4 specialized channels.
114
+ */ initializeChannels() {
115
+ // Coordination channel (agent status, lifecycle)
116
+ this.channels.set("coordination", new Channel({
117
+ channelId: 'coordination',
118
+ name: 'Coordination Channel',
119
+ capacity: 1000,
120
+ backpressurePolicy: BackpressurePolicy.DROP_OLDEST,
121
+ priority: 8,
122
+ ...this.config.channels["coordination"]
123
+ }));
124
+ // Task channel (task execution, completion)
125
+ this.channels.set("task", new Channel({
126
+ channelId: 'task',
127
+ name: 'Task Channel',
128
+ capacity: 2000,
129
+ backpressurePolicy: BackpressurePolicy.DROP_OLDEST,
130
+ priority: 7,
131
+ ...this.config.channels["task"]
132
+ }));
133
+ // Help channel (help requests, matching)
134
+ this.channels.set("help", new Channel({
135
+ channelId: 'help',
136
+ name: 'Help Channel',
137
+ capacity: 500,
138
+ backpressurePolicy: BackpressurePolicy.DROP_NEWEST,
139
+ priority: 9,
140
+ ...this.config.channels["help"]
141
+ }));
142
+ // Event channel (notifications, alerts)
143
+ this.channels.set("event", new Channel({
144
+ channelId: 'event',
145
+ name: 'Event Channel',
146
+ capacity: 1500,
147
+ backpressurePolicy: BackpressurePolicy.DROP_OLDEST,
148
+ priority: 5,
149
+ ...this.config.channels["event"]
150
+ }));
151
+ }
152
+ /**
153
+ * Initialize default routing rules.
154
+ */ initializeRoutingRules() {
155
+ // Add custom rules first (higher priority)
156
+ this.routingRules.push(...this.config.customRules);
157
+ // Default routing rules (pattern matching)
158
+ const defaultRules = [
159
+ // Coordination patterns
160
+ {
161
+ pattern: 'agent.*',
162
+ channelType: "coordination",
163
+ priority: 100
164
+ },
165
+ {
166
+ pattern: 'coordinator.*',
167
+ channelType: "coordination",
168
+ priority: 100
169
+ },
170
+ {
171
+ pattern: 'session.*',
172
+ channelType: "coordination",
173
+ priority: 100
174
+ },
175
+ // Task patterns
176
+ {
177
+ pattern: 'task.*',
178
+ channelType: "task",
179
+ priority: 90
180
+ },
181
+ {
182
+ pattern: 'job.*',
183
+ channelType: "task",
184
+ priority: 90
185
+ },
186
+ {
187
+ pattern: 'execution.*',
188
+ channelType: "task",
189
+ priority: 90
190
+ },
191
+ // Help patterns
192
+ {
193
+ pattern: 'help.*',
194
+ channelType: "help",
195
+ priority: 95
196
+ },
197
+ {
198
+ pattern: 'assistance.*',
199
+ channelType: "help",
200
+ priority: 95
201
+ },
202
+ // Event patterns (catch-all)
203
+ {
204
+ pattern: '*',
205
+ channelType: "event",
206
+ priority: 0
207
+ }
208
+ ];
209
+ this.routingRules.push(...defaultRules);
210
+ // Sort by priority (highest first)
211
+ this.routingRules.sort((a, b)=>b.priority - a.priority);
212
+ }
213
+ /**
214
+ * Route a message to appropriate channel.
215
+ *
216
+ * @param message - Message to route
217
+ *
218
+ * @example
219
+ * ```typescript
220
+ * router.route({
221
+ * id: 'msg-1',
222
+ * topic: 'task.completed',
223
+ * payload: { taskId: 'task-1', result: 'success' },
224
+ * priority: 5,
225
+ * timestamp: Date.now()
226
+ * });
227
+ * ```
228
+ */ route(message) {
229
+ // Check backpressure threshold
230
+ if (this.incomingQueue.size() >= this.config.maxPendingMessages) {
231
+ this.stats.totalDropped++;
232
+ this.emit("backpressure", {
233
+ pendingMessages: this.incomingQueue.size(),
234
+ maxPending: this.config.maxPendingMessages,
235
+ droppedMessageId: message.id
236
+ });
237
+ return;
238
+ }
239
+ // Add to priority queue
240
+ this.incomingQueue.enqueue(message, message.priority);
241
+ // Auto-process if not already processing
242
+ if (!this.isProcessing) {
243
+ setImmediate(()=>this.processQueue());
244
+ }
245
+ }
246
+ /**
247
+ * Process incoming message queue and route to channels.
248
+ *
249
+ * @returns Number of messages routed
250
+ */ async processQueue() {
251
+ if (this.isProcessing) {
252
+ return 0;
253
+ }
254
+ this.isProcessing = true;
255
+ let routedCount = 0;
256
+ try {
257
+ // Route all pending messages
258
+ while(!this.incomingQueue.isEmpty()){
259
+ const message = this.incomingQueue.dequeue();
260
+ if (!message) break;
261
+ const startTime = performance.now();
262
+ // Find matching channel
263
+ const channelType = this.findChannelForMessage(message);
264
+ if (!channelType) {
265
+ // No matching rule - drop message
266
+ this.stats.totalDropped++;
267
+ this.emit("message-dropped", {
268
+ messageId: message.id,
269
+ topic: message.topic,
270
+ reason: 'no-matching-route'
271
+ });
272
+ continue;
273
+ }
274
+ // Route to channel
275
+ const channel = this.channels.get(channelType);
276
+ if (channel) {
277
+ channel.publish(message);
278
+ routedCount++;
279
+ // Update statistics
280
+ this.stats.totalRouted++;
281
+ this.stats.messagesByChannel.set(channelType, (this.stats.messagesByChannel.get(channelType) ?? 0) + 1);
282
+ const latency = performance.now() - startTime;
283
+ this.stats.routingLatencies.push(latency);
284
+ // Keep only last 1000 latencies
285
+ if (this.stats.routingLatencies.length > 1000) {
286
+ this.stats.routingLatencies.shift();
287
+ }
288
+ this.emit("message-routed", {
289
+ messageId: message.id,
290
+ topic: message.topic,
291
+ channelType,
292
+ latencyMs: latency
293
+ });
294
+ }
295
+ }
296
+ // Process channels with fairness guarantees
297
+ if (this.config.enableFairness) {
298
+ await this.processFairScheduler();
299
+ } else {
300
+ // Process all channels in priority order
301
+ await this.processAllChannels();
302
+ }
303
+ } finally{
304
+ this.isProcessing = false;
305
+ }
306
+ return routedCount;
307
+ }
308
+ /**
309
+ * Find appropriate channel for message based on routing rules.
310
+ *
311
+ * @param message - Message to route
312
+ * @returns Channel type or null if no match
313
+ */ findChannelForMessage(message) {
314
+ for (const rule of this.routingRules){
315
+ if (this.matchesPattern(message.topic, rule.pattern)) {
316
+ return rule.channelType;
317
+ }
318
+ }
319
+ return null;
320
+ }
321
+ /**
322
+ * Match topic against pattern (supports * wildcard).
323
+ *
324
+ * @param topic - Message topic
325
+ * @param pattern - Pattern with wildcards
326
+ * @returns True if topic matches pattern
327
+ */ matchesPattern(topic, pattern) {
328
+ if (pattern === '*') {
329
+ return true;
330
+ }
331
+ const regexPattern = pattern.replace(/\./g, '\\.').replace(/\*/g, '.*');
332
+ const regex = new RegExp(`^${regexPattern}$`);
333
+ return regex.test(topic);
334
+ }
335
+ /**
336
+ * Process channels with fairness scheduler.
337
+ * Round-robin with quantum-based processing.
338
+ */ async processFairScheduler() {
339
+ const quantum = this.config.fairnessQuantum;
340
+ // Process each channel in round-robin order
341
+ for(let i = 0; i < this.fairnessState.channelOrder.length; i++){
342
+ const channelType = this.fairnessState.channelOrder[i];
343
+ const channel = this.channels.get(channelType);
344
+ if (channel && channel.getBufferSize() > 0) {
345
+ // Process up to quantum messages from this channel
346
+ let processed = 0;
347
+ while(processed < quantum && channel.getBufferSize() > 0){
348
+ await channel.processBuffer();
349
+ processed++;
350
+ }
351
+ }
352
+ }
353
+ }
354
+ /**
355
+ * Process all channels in priority order (no fairness).
356
+ */ async processAllChannels() {
357
+ // Get channels sorted by priority (highest first)
358
+ const sortedChannels = Array.from(this.channels.entries()).sort(([, a], [, b])=>b.priority - a.priority);
359
+ for (const [, channel] of sortedChannels){
360
+ await channel.processBuffer();
361
+ }
362
+ }
363
+ /**
364
+ * Get channel by type.
365
+ *
366
+ * @param channelType - Channel type
367
+ * @returns Channel instance or undefined
368
+ */ getChannel(channelType) {
369
+ return this.channels.get(channelType);
370
+ }
371
+ /**
372
+ * Get all channels.
373
+ *
374
+ * @returns Map of channel type to channel instance
375
+ */ getAllChannels() {
376
+ return new Map(this.channels);
377
+ }
378
+ /**
379
+ * Get router statistics.
380
+ *
381
+ * @returns Router statistics
382
+ */ getStats() {
383
+ const avgRoutingLatencyMs = this.stats.routingLatencies.length > 0 ? this.stats.routingLatencies.reduce((sum, l)=>sum + l, 0) / this.stats.routingLatencies.length : 0;
384
+ const messagesByChannel = {
385
+ ["coordination"]: this.stats.messagesByChannel.get("coordination") ?? 0,
386
+ ["task"]: this.stats.messagesByChannel.get("task") ?? 0,
387
+ ["help"]: this.stats.messagesByChannel.get("help") ?? 0,
388
+ ["event"]: this.stats.messagesByChannel.get("event") ?? 0
389
+ };
390
+ const channelStats = {};
391
+ for (const type of Object.values(ChannelType)){
392
+ const channel = this.channels.get(type);
393
+ if (channel) {
394
+ const stats = channel.getStats();
395
+ channelStats[type] = {
396
+ buffered: stats.currentBufferSize,
397
+ delivered: stats.totalDelivered,
398
+ dropped: stats.totalDropped,
399
+ subscribers: stats.totalSubscriptions
400
+ };
401
+ }
402
+ }
403
+ return {
404
+ totalRouted: this.stats.totalRouted,
405
+ totalDropped: this.stats.totalDropped,
406
+ messagesByChannel,
407
+ pendingMessages: this.incomingQueue.size(),
408
+ avgRoutingLatencyMs,
409
+ channelStats
410
+ };
411
+ }
412
+ /**
413
+ * Clear all channels and reset router.
414
+ */ clear() {
415
+ this.incomingQueue.clear();
416
+ for (const type of Object.values(ChannelType)){
417
+ const channel = this.channels.get(type);
418
+ if (channel) {
419
+ channel.clearBuffer();
420
+ channel.resetStats();
421
+ }
422
+ }
423
+ this.stats = {
424
+ totalRouted: 0,
425
+ totalDropped: 0,
426
+ messagesByChannel: new Map(),
427
+ routingLatencies: []
428
+ };
429
+ }
430
+ /**
431
+ * Shutdown router and close all channels.
432
+ */ shutdown() {
433
+ this.clear();
434
+ for (const type of Object.values(ChannelType)){
435
+ const channel = this.channels.get(type);
436
+ if (channel) {
437
+ channel.close();
438
+ }
439
+ }
440
+ this.removeAllListeners();
441
+ }
442
+ }
443
+
444
+ //# sourceMappingURL=message-router.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../../../../src/coordination/v2/messaging/message-router.ts"],"names":["PriorityQueue","Channel","BackpressurePolicy","EventEmitter","ChannelType","RouterEvent","MessageRouter","config","incomingQueue","channels","routingRules","stats","fairnessState","isProcessing","customRules","enableFairness","fairnessQuantum","maxPendingMessages","Map","totalRouted","totalDropped","messagesByChannel","routingLatencies","lastChannelIndex","channelOrder","initializeChannels","initializeRoutingRules","set","channelId","name","capacity","backpressurePolicy","DROP_OLDEST","priority","DROP_NEWEST","push","defaultRules","pattern","channelType","sort","a","b","route","message","size","emit","pendingMessages","maxPending","droppedMessageId","id","enqueue","setImmediate","processQueue","routedCount","isEmpty","dequeue","startTime","performance","now","findChannelForMessage","messageId","topic","reason","channel","get","publish","latency","length","shift","latencyMs","processFairScheduler","processAllChannels","rule","matchesPattern","regexPattern","replace","regex","RegExp","test","quantum","i","getBufferSize","processed","processBuffer","sortedChannels","Array","from","entries","getChannel","getAllChannels","getStats","avgRoutingLatencyMs","reduce","sum","l","channelStats","type","Object","values","buffered","currentBufferSize","delivered","totalDelivered","dropped","subscribers","totalSubscriptions","clear","clearBuffer","resetStats","shutdown","close","removeAllListeners"],"mappings":"AAAA;;;;;;;;;;;;;;CAcC,GAGD,SAASA,aAAa,QAAQ,6BAA6B;AAC3D,SAASC,OAAO,EAAsBC,kBAAkB,QAAQ,eAAe;AAC/E,SAASC,YAAY,QAAQ,SAAS;AAEtC;;;CAGC,GACD,OAAO,IAAA,AAAKC,qCAAAA;IACV,oDAAoD;IAGpD,0DAA0D;IAG1D,kDAAkD;IAGlD,gDAAgD;WAVtCA;MAYX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,CAAC9F,aAAc;YAC7C,MAAM2D,UAAU,IAAI,CAACtD,QAAQ,CAACuD,GAAG,CAACgC;YAClC,IAAIjC,SAAS;gBACX,MAAMpD,QAAQoD,QAAQ2B,QAAQ;gBAC9BK,YAAY,CAACC,KAAK,GAAG;oBACnBG,UAAUxF,MAAMyF,iBAAiB;oBACjCC,WAAW1F,MAAM2F,cAAc;oBAC/BC,SAAS5F,MAAMS,YAAY;oBAC3BoF,aAAa7F,MAAM8F,kBAAkB;gBACvC;YACF;QACF;QAEA,OAAO;YACLtF,aAAa,IAAI,CAACR,KAAK,CAACQ,WAAW;YACnCC,cAAc,IAAI,CAACT,KAAK,CAACS,YAAY;YACrCC;YACAyB,iBAAiB,IAAI,CAACtC,aAAa,CAACoC,IAAI;YACxC+C;YACAI;QACF;IACF;IAEA;;GAEC,GACDW,QAAc;QACZ,IAAI,CAAClG,aAAa,CAACkG,KAAK;QACxB,KAAK,MAAMV,QAAQC,OAAOC,MAAM,CAAC9F,aAAc;YAC7C,MAAM2D,UAAU,IAAI,CAACtD,QAAQ,CAACuD,GAAG,CAACgC;YAClC,IAAIjC,SAAS;gBACXA,QAAQ4C,WAAW;gBACnB5C,QAAQ6C,UAAU;YACpB;QACF;QACA,IAAI,CAACjG,KAAK,GAAG;YACXQ,aAAa;YACbC,cAAc;YACdC,mBAAmB,IAAIH;YACvBI,kBAAkB,EAAE;QACtB;IACF;IAEA;;GAEC,GACDuF,WAAiB;QACf,IAAI,CAACH,KAAK;QACV,KAAK,MAAMV,QAAQC,OAAOC,MAAM,CAAC9F,aAAc;YAC7C,MAAM2D,UAAU,IAAI,CAACtD,QAAQ,CAACuD,GAAG,CAACgC;YAClC,IAAIjC,SAAS;gBACXA,QAAQ+C,KAAK;YACf;QACF;QACA,IAAI,CAACC,kBAAkB;IACzB;AACF"}
@@ -13,6 +13,7 @@ import { promises as fs } from 'fs';
13
13
  import * as path from 'path';
14
14
  import * as msgpack from 'msgpack-lite';
15
15
  import { AgentState } from '../types/sdk.js';
16
+ import { CheckpointCompressor } from '../checkpoints/checkpoint-compressor.js';
16
17
  export class CheckpointManager {
17
18
  MAX_IN_MEMORY_CHECKPOINTS = 100;
18
19
  checkpoints = new Map();
@@ -31,9 +32,11 @@ export class CheckpointManager {
31
32
  retentionHours = 24;
32
33
  restoredCheckpoints = new Set();
33
34
  maxCheckpointAge = 3600000;
35
+ compressor;
34
36
  constructor(storagePath = '.claude-flow/checkpoints/v2', retentionHours = 24){
35
37
  this.storagePath = storagePath;
36
38
  this.retentionHours = retentionHours;
39
+ this.compressor = new CheckpointCompressor();
37
40
  this.metrics = {
38
41
  totalCheckpoints: 0,
39
42
  totalRestores: 0,
@@ -76,12 +79,15 @@ export class CheckpointManager {
76
79
  const startTime = performance.now();
77
80
  const checkpointId = this.generateCheckpointId(sessionId, messageUUID);
78
81
  const timestamp = Date.now();
79
- // Serialize and compress state snapshot using MessagePack
80
- const uncompressedData = JSON.stringify(stateSnapshot);
81
- const uncompressedSize = Buffer.byteLength(uncompressedData, 'utf8');
82
- const compressedBuffer = msgpack.encode(stateSnapshot);
83
- const compressedSize = compressedBuffer.length;
84
- const compressionRatio = 1 - compressedSize / uncompressedSize;
82
+ // Get previous checkpoint for delta compression
83
+ const previousCheckpointData = this.getLatestCheckpointData(sessionId);
84
+ const previousCheckpointId = previousCheckpointData?.id;
85
+ // Use multi-layer compression: MessagePack + Delta + Gzip
86
+ const compressionResult = await this.compressor.compress(stateSnapshot, sessionId, previousCheckpointId);
87
+ const compressedBuffer = compressionResult.compressedBuffer;
88
+ const compressedSize = compressionResult.compressedSize;
89
+ const uncompressedSize = compressionResult.uncompressedSize;
90
+ const compressionRatio = compressionResult.compressionRatio;
85
91
  // Calculate checksum for integrity verification
86
92
  const checksum = this.calculateChecksum(stateSnapshot);
87
93
  const checkpoint = {
@@ -299,7 +305,8 @@ export class CheckpointManager {
299
305
  const now = Date.now();
300
306
  const retentionMs = this.retentionHours * 60 * 60 * 1000;
301
307
  let deletedCount = 0;
302
- for (const [checkpointId, checkpoint] of this.checkpoints.entries()){
308
+ const entries = Array.from(this.checkpoints.entries());
309
+ for (const [checkpointId, checkpoint] of entries){
303
310
  const age = now - checkpoint.timestamp;
304
311
  if (age > retentionMs) {
305
312
  await this.deleteCheckpoint(checkpointId);
@@ -318,6 +325,19 @@ export class CheckpointManager {
318
325
  };
319
326
  }
320
327
  /**
328
+ * Get compression details for a specific checkpoint (for testing)
329
+ */ getCheckpointCompressionDetails(checkpointId) {
330
+ const checkpoint = this.checkpoints.get(checkpointId);
331
+ if (!checkpoint) {
332
+ return null;
333
+ }
334
+ return {
335
+ compressedSize: checkpoint.compressedSize,
336
+ uncompressedSize: checkpoint.uncompressedSize,
337
+ compressionRatio: 1 - checkpoint.compressedSize / checkpoint.uncompressedSize
338
+ };
339
+ }
340
+ /**
321
341
  * Enable/disable auto-checkpointing
322
342
  */ setAutoCheckpointEnabled(enabled) {
323
343
  this.autoCheckpointEnabled = enabled;
@@ -806,7 +826,8 @@ export class CheckpointManager {
806
826
  */ getMemoryStats() {
807
827
  const now = Date.now();
808
828
  let oldestAge = 0;
809
- for (const checkpoint of this.checkpoints.values()){
829
+ const values = Array.from(this.checkpoints.values());
830
+ for (const checkpoint of values){
810
831
  const age = now - checkpoint.timestamp;
811
832
  oldestAge = Math.max(oldestAge, age);
812
833
  }