@monoes/monomindcli 1.19.0 → 2.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 (369) hide show
  1. package/.claude/agents/github/issue-tracker.md +0 -2
  2. package/.claude/agents/github/pr-manager.md +0 -2
  3. package/.claude/agents/github/repo-architect.md +3 -11
  4. package/.claude/agents/goal/goal-planner.md +0 -1
  5. package/.claude/commands/github/issue-tracker.md +0 -2
  6. package/.claude/commands/github/pr-manager.md +0 -2
  7. package/.claude/commands/github/repo-architect.md +3 -10
  8. package/.claude/commands/mastermind/help.md +3 -3
  9. package/.claude/commands/workflows/README.md +0 -1
  10. package/.claude/commands/workflows/workflow-create.md +0 -1
  11. package/.claude/commands/workflows/workflow-execute.md +1 -3
  12. package/.claude/commands/workflows/workflow-export.md +0 -2
  13. package/.claude/helpers/handlers/agent-start-handler.cjs +18 -1
  14. package/.claude/helpers/handlers/edit-handler.cjs +15 -0
  15. package/.claude/helpers/handlers/gates-handler.cjs +2 -8
  16. package/.claude/helpers/handlers/route-handler.cjs +149 -7
  17. package/.claude/helpers/handlers/session-handler.cjs +47 -25
  18. package/.claude/helpers/handlers/session-restore-handler.cjs +80 -43
  19. package/.claude/helpers/handlers/task-handler.cjs +8 -4
  20. package/.claude/helpers/hook-handler.cjs +69 -16
  21. package/.claude/helpers/intelligence.cjs +100 -9
  22. package/.claude/helpers/router.cjs +78 -5
  23. package/.claude/helpers/skill-registry.json +0 -189
  24. package/.claude/skills/github-toolkit/SKILL.md +60 -0
  25. package/.claude/skills/hive-mind-advanced/SKILL.md +0 -15
  26. package/.claude/skills/hooks-automation/SKILL.md +0 -1
  27. package/.claude/skills/mastermind-skills/_protocol.md +1 -1
  28. package/.claude/skills/mastermind-skills/build.md +1 -2
  29. package/.claude/skills/memory-toolkit/SKILL.md +75 -0
  30. package/.claude/skills/specialagent/SKILL.md +2 -4
  31. package/.claude/skills/swarm-advanced/SKILL.md +0 -1
  32. package/README.md +2 -5
  33. package/dist/src/autopilot-state.d.ts.map +1 -1
  34. package/dist/src/autopilot-state.js +4 -5
  35. package/dist/src/autopilot-state.js.map +1 -1
  36. package/dist/src/browser/dashboard/server.d.ts.map +1 -1
  37. package/dist/src/browser/dashboard/server.js +69 -1
  38. package/dist/src/browser/dashboard/server.js.map +1 -1
  39. package/dist/src/browser/dashboard/ui.html +214 -1764
  40. package/dist/src/capabilities/index.d.ts +0 -2
  41. package/dist/src/capabilities/index.d.ts.map +1 -1
  42. package/dist/src/capabilities/index.js +0 -1
  43. package/dist/src/capabilities/index.js.map +1 -1
  44. package/dist/src/commands/agent-lifecycle.d.ts.map +1 -1
  45. package/dist/src/commands/agent-lifecycle.js +2 -6
  46. package/dist/src/commands/agent-lifecycle.js.map +1 -1
  47. package/dist/src/commands/analyze.d.ts +0 -4
  48. package/dist/src/commands/analyze.d.ts.map +1 -1
  49. package/dist/src/commands/analyze.js +1 -29
  50. package/dist/src/commands/analyze.js.map +1 -1
  51. package/dist/src/commands/completions.d.ts.map +1 -1
  52. package/dist/src/commands/completions.js +3 -22
  53. package/dist/src/commands/completions.js.map +1 -1
  54. package/dist/src/commands/config.d.ts.map +1 -1
  55. package/dist/src/commands/config.js +0 -6
  56. package/dist/src/commands/config.js.map +1 -1
  57. package/dist/src/commands/doctor-project-checks.d.ts +12 -2
  58. package/dist/src/commands/doctor-project-checks.d.ts.map +1 -1
  59. package/dist/src/commands/doctor-project-checks.js +130 -22
  60. package/dist/src/commands/doctor-project-checks.js.map +1 -1
  61. package/dist/src/commands/doctor.d.ts.map +1 -1
  62. package/dist/src/commands/doctor.js +6 -5
  63. package/dist/src/commands/doctor.js.map +1 -1
  64. package/dist/src/commands/guidance.d.ts +5 -1
  65. package/dist/src/commands/guidance.d.ts.map +1 -1
  66. package/dist/src/commands/guidance.js +11 -556
  67. package/dist/src/commands/guidance.js.map +1 -1
  68. package/dist/src/commands/hive-mind-comms.d.ts.map +1 -1
  69. package/dist/src/commands/hive-mind-comms.js +11 -4
  70. package/dist/src/commands/hive-mind-comms.js.map +1 -1
  71. package/dist/src/commands/hive-mind.d.ts +4 -4
  72. package/dist/src/commands/hive-mind.d.ts.map +1 -1
  73. package/dist/src/commands/hive-mind.js +9 -6
  74. package/dist/src/commands/hive-mind.js.map +1 -1
  75. package/dist/src/commands/hooks-formatting.d.ts +1 -5
  76. package/dist/src/commands/hooks-formatting.d.ts.map +1 -1
  77. package/dist/src/commands/hooks-formatting.js +1 -18
  78. package/dist/src/commands/hooks-formatting.js.map +1 -1
  79. package/dist/src/commands/hooks-workers.d.ts +1 -1
  80. package/dist/src/commands/hooks-workers.d.ts.map +1 -1
  81. package/dist/src/commands/hooks-workers.js +82 -330
  82. package/dist/src/commands/hooks-workers.js.map +1 -1
  83. package/dist/src/commands/index.d.ts +0 -5
  84. package/dist/src/commands/index.d.ts.map +1 -1
  85. package/dist/src/commands/index.js +0 -45
  86. package/dist/src/commands/index.js.map +1 -1
  87. package/dist/src/commands/init-wizard.js +1 -1
  88. package/dist/src/commands/init-wizard.js.map +1 -1
  89. package/dist/src/commands/init.d.ts.map +1 -1
  90. package/dist/src/commands/init.js +4 -24
  91. package/dist/src/commands/init.js.map +1 -1
  92. package/dist/src/commands/neural-core.js +11 -11
  93. package/dist/src/commands/neural-core.js.map +1 -1
  94. package/dist/src/commands/neural-optimize.d.ts.map +1 -1
  95. package/dist/src/commands/neural-optimize.js +9 -11
  96. package/dist/src/commands/neural-optimize.js.map +1 -1
  97. package/dist/src/commands/neural-registry.js +10 -10
  98. package/dist/src/commands/neural-registry.js.map +1 -1
  99. package/dist/src/commands/providers.d.ts.map +1 -1
  100. package/dist/src/commands/providers.js +90 -1
  101. package/dist/src/commands/providers.js.map +1 -1
  102. package/dist/src/commands/search-universal.d.ts +7 -1
  103. package/dist/src/commands/search-universal.d.ts.map +1 -1
  104. package/dist/src/commands/search-universal.js +59 -5
  105. package/dist/src/commands/search-universal.js.map +1 -1
  106. package/dist/src/commands/security-cve.d.ts.map +1 -1
  107. package/dist/src/commands/security-cve.js +3 -5
  108. package/dist/src/commands/security-cve.js.map +1 -1
  109. package/dist/src/commands/session.d.ts.map +1 -1
  110. package/dist/src/commands/session.js +143 -3
  111. package/dist/src/commands/session.js.map +1 -1
  112. package/dist/src/commands/start.d.ts.map +1 -1
  113. package/dist/src/commands/start.js +0 -18
  114. package/dist/src/commands/start.js.map +1 -1
  115. package/dist/src/commands/swarm.d.ts.map +1 -1
  116. package/dist/src/commands/swarm.js +112 -65
  117. package/dist/src/commands/swarm.js.map +1 -1
  118. package/dist/src/consensus/audit-writer.d.ts.map +1 -1
  119. package/dist/src/consensus/audit-writer.js +43 -1
  120. package/dist/src/consensus/audit-writer.js.map +1 -1
  121. package/dist/src/consensus/tally.d.ts +19 -0
  122. package/dist/src/consensus/tally.d.ts.map +1 -0
  123. package/dist/src/consensus/tally.js +34 -0
  124. package/dist/src/consensus/tally.js.map +1 -0
  125. package/dist/src/index.d.ts +3 -2
  126. package/dist/src/index.d.ts.map +1 -1
  127. package/dist/src/index.js +9 -12
  128. package/dist/src/index.js.map +1 -1
  129. package/dist/src/init/claudemd-generator.d.ts.map +1 -1
  130. package/dist/src/init/claudemd-generator.js +10 -14
  131. package/dist/src/init/claudemd-generator.js.map +1 -1
  132. package/dist/src/init/executor.d.ts.map +1 -1
  133. package/dist/src/init/executor.js +39 -86
  134. package/dist/src/init/executor.js.map +1 -1
  135. package/dist/src/init/settings-generator.d.ts.map +1 -1
  136. package/dist/src/init/settings-generator.js +0 -12
  137. package/dist/src/init/settings-generator.js.map +1 -1
  138. package/dist/src/init/statusline-generator.d.ts.map +1 -1
  139. package/dist/src/init/statusline-generator.js +62 -0
  140. package/dist/src/init/statusline-generator.js.map +1 -1
  141. package/dist/src/init/types.d.ts +2 -6
  142. package/dist/src/init/types.d.ts.map +1 -1
  143. package/dist/src/init/types.js +0 -3
  144. package/dist/src/init/types.js.map +1 -1
  145. package/dist/src/knowledge/document-pipeline.d.ts.map +1 -1
  146. package/dist/src/knowledge/document-pipeline.js +18 -8
  147. package/dist/src/knowledge/document-pipeline.js.map +1 -1
  148. package/dist/src/mcp-client.d.ts.map +1 -1
  149. package/dist/src/mcp-client.js +0 -2
  150. package/dist/src/mcp-client.js.map +1 -1
  151. package/dist/src/mcp-server.d.ts.map +1 -1
  152. package/dist/src/mcp-server.js +12 -14
  153. package/dist/src/mcp-server.js.map +1 -1
  154. package/dist/src/mcp-tools/coherence/coherence-check.d.ts.map +1 -1
  155. package/dist/src/mcp-tools/coherence/coherence-check.js +1 -19
  156. package/dist/src/mcp-tools/coherence/coherence-check.js.map +1 -1
  157. package/dist/src/mcp-tools/coherence/types.d.ts +3 -2
  158. package/dist/src/mcp-tools/coherence/types.d.ts.map +1 -1
  159. package/dist/src/mcp-tools/coherence/types.js +3 -18
  160. package/dist/src/mcp-tools/coherence/types.js.map +1 -1
  161. package/dist/src/mcp-tools/embeddings-tools.d.ts.map +1 -1
  162. package/dist/src/mcp-tools/embeddings-tools.js +1 -7
  163. package/dist/src/mcp-tools/embeddings-tools.js.map +1 -1
  164. package/dist/src/mcp-tools/graphify-tools.js +2 -2
  165. package/dist/src/mcp-tools/graphify-tools.js.map +1 -1
  166. package/dist/src/mcp-tools/guidance-tools.d.ts.map +1 -1
  167. package/dist/src/mcp-tools/guidance-tools.js +7 -17
  168. package/dist/src/mcp-tools/guidance-tools.js.map +1 -1
  169. package/dist/src/mcp-tools/hive-mind-tools.d.ts +8 -1
  170. package/dist/src/mcp-tools/hive-mind-tools.d.ts.map +1 -1
  171. package/dist/src/mcp-tools/hive-mind-tools.js +174 -38
  172. package/dist/src/mcp-tools/hive-mind-tools.js.map +1 -1
  173. package/dist/src/mcp-tools/hooks-advanced.d.ts +18 -0
  174. package/dist/src/mcp-tools/hooks-advanced.d.ts.map +1 -0
  175. package/dist/src/mcp-tools/hooks-advanced.js +25 -0
  176. package/dist/src/mcp-tools/hooks-advanced.js.map +1 -0
  177. package/dist/src/mcp-tools/hooks-intelligence.d.ts +4 -8
  178. package/dist/src/mcp-tools/hooks-intelligence.d.ts.map +1 -1
  179. package/dist/src/mcp-tools/hooks-intelligence.js +33 -482
  180. package/dist/src/mcp-tools/hooks-intelligence.js.map +1 -1
  181. package/dist/src/mcp-tools/hooks-routing.d.ts +1 -0
  182. package/dist/src/mcp-tools/hooks-routing.d.ts.map +1 -1
  183. package/dist/src/mcp-tools/hooks-routing.js +51 -43
  184. package/dist/src/mcp-tools/hooks-routing.js.map +1 -1
  185. package/dist/src/mcp-tools/hooks-synthesis.d.ts +31 -0
  186. package/dist/src/mcp-tools/hooks-synthesis.d.ts.map +1 -0
  187. package/dist/src/mcp-tools/hooks-synthesis.js +196 -0
  188. package/dist/src/mcp-tools/hooks-synthesis.js.map +1 -0
  189. package/dist/src/mcp-tools/hooks-tools.d.ts.map +1 -1
  190. package/dist/src/mcp-tools/hooks-tools.js +10 -8
  191. package/dist/src/mcp-tools/hooks-tools.js.map +1 -1
  192. package/dist/src/mcp-tools/index.d.ts +0 -1
  193. package/dist/src/mcp-tools/index.d.ts.map +1 -1
  194. package/dist/src/mcp-tools/index.js +0 -1
  195. package/dist/src/mcp-tools/index.js.map +1 -1
  196. package/dist/src/mcp-tools/memory-tools.d.ts.map +1 -1
  197. package/dist/src/mcp-tools/memory-tools.js +20 -29
  198. package/dist/src/mcp-tools/memory-tools.js.map +1 -1
  199. package/dist/src/mcp-tools/monograph-tools.d.ts +6 -0
  200. package/dist/src/mcp-tools/monograph-tools.d.ts.map +1 -1
  201. package/dist/src/mcp-tools/monograph-tools.js +78 -220
  202. package/dist/src/mcp-tools/monograph-tools.js.map +1 -1
  203. package/dist/src/mcp-tools/neural-tools.d.ts +9 -4
  204. package/dist/src/mcp-tools/neural-tools.d.ts.map +1 -1
  205. package/dist/src/mcp-tools/neural-tools.js +242 -506
  206. package/dist/src/mcp-tools/neural-tools.js.map +1 -1
  207. package/dist/src/mcp-tools/security-tools.d.ts.map +1 -1
  208. package/dist/src/mcp-tools/security-tools.js +103 -0
  209. package/dist/src/mcp-tools/security-tools.js.map +1 -1
  210. package/dist/src/memory/intelligence.d.ts +2 -1
  211. package/dist/src/memory/intelligence.d.ts.map +1 -1
  212. package/dist/src/memory/intelligence.js +56 -43
  213. package/dist/src/memory/intelligence.js.map +1 -1
  214. package/dist/src/memory/memory-bridge.d.ts.map +1 -1
  215. package/dist/src/memory/memory-bridge.js +14 -9
  216. package/dist/src/memory/memory-bridge.js.map +1 -1
  217. package/dist/src/memory/memory-read.d.ts.map +1 -1
  218. package/dist/src/memory/memory-read.js +1 -19
  219. package/dist/src/memory/memory-read.js.map +1 -1
  220. package/dist/src/suggest.d.ts.map +1 -1
  221. package/dist/src/suggest.js +1 -7
  222. package/dist/src/suggest.js.map +1 -1
  223. package/dist/src/transfer/store/discovery.d.ts +3 -2
  224. package/dist/src/transfer/store/discovery.d.ts.map +1 -1
  225. package/dist/src/transfer/store/discovery.js +22 -70
  226. package/dist/src/transfer/store/discovery.js.map +1 -1
  227. package/dist/src/update/checker.d.ts.map +1 -1
  228. package/dist/src/update/checker.js +0 -1
  229. package/dist/src/update/checker.js.map +1 -1
  230. package/dist/src/utils/cosine-similarity.d.ts +22 -0
  231. package/dist/src/utils/cosine-similarity.d.ts.map +1 -0
  232. package/dist/src/utils/cosine-similarity.js +38 -0
  233. package/dist/src/utils/cosine-similarity.js.map +1 -0
  234. package/dist/src/utils/input-guards.d.ts +51 -0
  235. package/dist/src/utils/input-guards.d.ts.map +1 -0
  236. package/dist/src/utils/input-guards.js +223 -0
  237. package/dist/src/utils/input-guards.js.map +1 -0
  238. package/dist/src/utils/json-file.d.ts +33 -0
  239. package/dist/src/utils/json-file.d.ts.map +1 -0
  240. package/dist/src/utils/json-file.js +57 -0
  241. package/dist/src/utils/json-file.js.map +1 -0
  242. package/dist/tsconfig.tsbuildinfo +1 -1
  243. package/package.json +9 -6
  244. package/scripts/sync-claude-assets.sh +16 -20
  245. package/.claude/agents/generated/case-analyst.md +0 -50
  246. package/.claude/agents/generated/channel-intelligence-director.md +0 -87
  247. package/.claude/agents/generated/chief-growth-officer.md +0 -88
  248. package/.claude/agents/generated/churn-analyst.md +0 -53
  249. package/.claude/agents/generated/code-reviewer.md +0 -55
  250. package/.claude/agents/generated/code-validator.md +0 -57
  251. package/.claude/agents/generated/complexity-scanner.md +0 -56
  252. package/.claude/agents/generated/content-seo-strategist.md +0 -90
  253. package/.claude/agents/generated/copy-editor.md +0 -45
  254. package/.claude/agents/generated/court-reporter.md +0 -50
  255. package/.claude/agents/generated/defender.md +0 -51
  256. package/.claude/agents/generated/devbot-orchestrator.md +0 -58
  257. package/.claude/agents/generated/devbot-planner.md +0 -63
  258. package/.claude/agents/generated/developer-community-strategist.md +0 -91
  259. package/.claude/agents/generated/editor-in-chief.md +0 -45
  260. package/.claude/agents/generated/fact-checker.md +0 -45
  261. package/.claude/agents/generated/impact-assessor.md +0 -54
  262. package/.claude/agents/generated/judge.md +0 -51
  263. package/.claude/agents/generated/outreach-partnership-strategist.md +0 -90
  264. package/.claude/agents/generated/prosecutor.md +0 -51
  265. package/.claude/agents/generated/reporter.md +0 -45
  266. package/.claude/agents/generated/social-media-strategist.md +0 -91
  267. package/.claude/agents/generated/video-visual-strategist.md +0 -90
  268. package/.claude/agents/reengineer-squad/boss.md +0 -113
  269. package/.claude/agents/reengineer-squad/critic-architect.md +0 -132
  270. package/.claude/agents/reengineer-squad/git-manager.md +0 -145
  271. package/.claude/agents/reengineer-squad/idea-generator.md +0 -95
  272. package/.claude/agents/reengineer-squad/implementer.md +0 -112
  273. package/.claude/agents/reengineer-squad/integration-planner.md +0 -112
  274. package/.claude/agents/reengineer-squad/source-analyst.md +0 -103
  275. package/.claude/agents/reengineer-squad/target-analyst.md +0 -118
  276. package/.claude/agents/reengineer-squad/tester.md +0 -105
  277. package/.claude/agents/sparc/architecture.md +0 -453
  278. package/.claude/agents/sparc/pseudocode.md +0 -299
  279. package/.claude/agents/sparc/refinement.md +0 -504
  280. package/.claude/agents/sparc/specification.md +0 -258
  281. package/.claude/agents/templates/implementer-sparc-coder.md +0 -231
  282. package/.claude/commands/sparc/analyzer.md +0 -35
  283. package/.claude/commands/sparc/architect.md +0 -36
  284. package/.claude/commands/sparc/ask.md +0 -56
  285. package/.claude/commands/sparc/batch-executor.md +0 -37
  286. package/.claude/commands/sparc/code.md +0 -48
  287. package/.claude/commands/sparc/coder.md +0 -37
  288. package/.claude/commands/sparc/debug.md +0 -42
  289. package/.claude/commands/sparc/debugger.md +0 -37
  290. package/.claude/commands/sparc/designer.md +0 -36
  291. package/.claude/commands/sparc/devops.md +0 -68
  292. package/.claude/commands/sparc/docs-writer.md +0 -39
  293. package/.claude/commands/sparc/documenter.md +0 -37
  294. package/.claude/commands/sparc/innovator.md +0 -37
  295. package/.claude/commands/sparc/integration.md +0 -42
  296. package/.claude/commands/sparc/mcp.md +0 -76
  297. package/.claude/commands/sparc/memory-manager.md +0 -57
  298. package/.claude/commands/sparc/optimizer.md +0 -37
  299. package/.claude/commands/sparc/orchestrator.md +0 -70
  300. package/.claude/commands/sparc/post-deployment-monitoring-mode.md +0 -42
  301. package/.claude/commands/sparc/refinement-optimization-mode.md +0 -42
  302. package/.claude/commands/sparc/researcher.md +0 -37
  303. package/.claude/commands/sparc/reviewer.md +0 -37
  304. package/.claude/commands/sparc/security-review.md +0 -39
  305. package/.claude/commands/sparc/sparc-modes.md +0 -120
  306. package/.claude/commands/sparc/sparc.md +0 -71
  307. package/.claude/commands/sparc/spec-pseudocode.md +0 -39
  308. package/.claude/commands/sparc/supabase-admin.md +0 -301
  309. package/.claude/commands/sparc/swarm-coordinator.md +0 -50
  310. package/.claude/commands/sparc/tdd.md +0 -37
  311. package/.claude/commands/sparc/tester.md +0 -37
  312. package/.claude/commands/sparc/tutorial.md +0 -38
  313. package/.claude/commands/sparc/workflow-manager.md +0 -37
  314. package/.claude/commands/sparc.md +0 -112
  315. package/.claude/skills/sparc-methodology/SKILL.md +0 -950
  316. package/dist/src/capabilities/enrichment.d.ts +0 -44
  317. package/dist/src/capabilities/enrichment.d.ts.map +0 -1
  318. package/dist/src/capabilities/enrichment.js +0 -141
  319. package/dist/src/capabilities/enrichment.js.map +0 -1
  320. package/dist/src/commands/analyze-boundaries.d.ts +0 -14
  321. package/dist/src/commands/analyze-boundaries.d.ts.map +0 -1
  322. package/dist/src/commands/analyze-boundaries.js +0 -295
  323. package/dist/src/commands/analyze-boundaries.js.map +0 -1
  324. package/dist/src/commands/analyze-graph.d.ts +0 -14
  325. package/dist/src/commands/analyze-graph.d.ts.map +0 -1
  326. package/dist/src/commands/analyze-graph.js +0 -304
  327. package/dist/src/commands/analyze-graph.js.map +0 -1
  328. package/dist/src/commands/claims.d.ts +0 -10
  329. package/dist/src/commands/claims.d.ts.map +0 -1
  330. package/dist/src/commands/claims.js +0 -650
  331. package/dist/src/commands/claims.js.map +0 -1
  332. package/dist/src/commands/daemon.d.ts +0 -8
  333. package/dist/src/commands/daemon.d.ts.map +0 -1
  334. package/dist/src/commands/daemon.js +0 -681
  335. package/dist/src/commands/daemon.js.map +0 -1
  336. package/dist/src/commands/enrich.d.ts +0 -10
  337. package/dist/src/commands/enrich.d.ts.map +0 -1
  338. package/dist/src/commands/enrich.js +0 -58
  339. package/dist/src/commands/enrich.js.map +0 -1
  340. package/dist/src/commands/issues.d.ts +0 -21
  341. package/dist/src/commands/issues.d.ts.map +0 -1
  342. package/dist/src/commands/issues.js +0 -575
  343. package/dist/src/commands/issues.js.map +0 -1
  344. package/dist/src/commands/workflow.d.ts +0 -8
  345. package/dist/src/commands/workflow.d.ts.map +0 -1
  346. package/dist/src/commands/workflow.js +0 -575
  347. package/dist/src/commands/workflow.js.map +0 -1
  348. package/dist/src/mcp-tools/workflow-tools.d.ts +0 -8
  349. package/dist/src/mcp-tools/workflow-tools.d.ts.map +0 -1
  350. package/dist/src/mcp-tools/workflow-tools.js +0 -717
  351. package/dist/src/mcp-tools/workflow-tools.js.map +0 -1
  352. package/dist/src/services/claim-service.d.ts +0 -198
  353. package/dist/src/services/claim-service.d.ts.map +0 -1
  354. package/dist/src/services/claim-service.js +0 -890
  355. package/dist/src/services/claim-service.js.map +0 -1
  356. package/dist/src/services/headless-worker-executor.d.ts +0 -311
  357. package/dist/src/services/headless-worker-executor.d.ts.map +0 -1
  358. package/dist/src/services/headless-worker-executor.js +0 -1183
  359. package/dist/src/services/headless-worker-executor.js.map +0 -1
  360. package/dist/src/services/worker-daemon.d.ts +0 -230
  361. package/dist/src/services/worker-daemon.d.ts.map +0 -1
  362. package/dist/src/services/worker-daemon.js +0 -1350
  363. package/dist/src/services/worker-daemon.js.map +0 -1
  364. package/dist/src/ui/.monomind/capture/active-run.json +0 -1
  365. package/dist/src/ui/.monomind/orgs/system-trial-qa/runs/real-events-1782290897.convs.jsonl +0 -3
  366. package/dist/src/ui/.monomind/orgs/system-trial-qa/runs/real-events-1782290897.jsonl +0 -11
  367. package/dist/src/ui/.monomind/orgs/system-trial-qa/runs/rigid-qa-restart-1782288201.jsonl +0 -540
  368. package/dist/src/ui/.monomind/orgs/system-trial-qa-threads.jsonl +0 -3
  369. package/dist/src/ui/.monomind/orgs/test-event-fix/runs/rigid-qa-restart-1782288201.jsonl +0 -2
@@ -1,1350 +0,0 @@
1
- /**
2
- * Worker Daemon Service
3
- * Node.js-based background worker system that auto-runs like shell daemons
4
- *
5
- * Workers:
6
- * - map: Codebase mapping (5 min interval)
7
- * - audit: Security analysis (10 min interval)
8
- * - optimize: Performance optimization (15 min interval)
9
- * - consolidate: Memory consolidation (30 min interval)
10
- * - testgaps, predict, document, refactor, preload: no local-mode
11
- * implementation — real work only happens in headless mode via
12
- * HeadlessWorkerExecutor (Claude Code CLI required)
13
- */
14
- import { EventEmitter } from 'events';
15
- import { existsSync, mkdirSync, writeFileSync, renameSync, readFileSync, appendFileSync, unlinkSync, statSync } from 'fs';
16
- import { cpus } from 'os';
17
- import { join, resolve } from 'path';
18
- import { HeadlessWorkerExecutor, isHeadlessWorker, } from './headless-worker-executor.js';
19
- // Default worker configurations with improved intervals (P0 fix: map 5min -> 15min)
20
- const DEFAULT_WORKERS = [
21
- { type: 'map', intervalMs: 15 * 60 * 1000, offsetMs: 0, priority: 'normal', description: 'Codebase mapping', enabled: true },
22
- { type: 'audit', intervalMs: 10 * 60 * 1000, offsetMs: 2 * 60 * 1000, priority: 'critical', description: 'Security analysis', enabled: true },
23
- { type: 'optimize', intervalMs: 15 * 60 * 1000, offsetMs: 4 * 60 * 1000, priority: 'high', description: 'Performance optimization', enabled: true },
24
- { type: 'consolidate', intervalMs: 30 * 60 * 1000, offsetMs: 6 * 60 * 1000, priority: 'low', description: 'Memory consolidation', enabled: true },
25
- { type: 'testgaps', intervalMs: 20 * 60 * 1000, offsetMs: 8 * 60 * 1000, priority: 'normal', description: 'Test coverage analysis', enabled: true },
26
- { type: 'predict', intervalMs: 10 * 60 * 1000, offsetMs: 0, priority: 'low', description: 'Predictive preloading', enabled: false },
27
- { type: 'document', intervalMs: 60 * 60 * 1000, offsetMs: 0, priority: 'low', description: 'Auto-documentation', enabled: false },
28
- ];
29
- // Worker timeout — must exceed the longest per-worker headless timeout (15 min for audit/refactor).
30
- // Previously 5 min, which caused orphan processes when daemon timeout fired before executor timeout (#1117).
31
- const DEFAULT_WORKER_TIMEOUT_MS = 16 * 60 * 1000;
32
- /**
33
- * Worker Daemon - Manages background workers with Node.js
34
- */
35
- export class WorkerDaemon extends EventEmitter {
36
- config;
37
- workers = new Map();
38
- timers = new Map();
39
- running = false;
40
- startedAt;
41
- projectRoot;
42
- runningWorkers = new Set(); // Track concurrent workers
43
- pendingWorkers = []; // Queue for deferred workers
44
- // Headless execution support
45
- headlessExecutor = null;
46
- headlessAvailable = false;
47
- // Preserve the original constructor config so we can detect explicit overrides
48
- // during state restoration (R1: constructor config takes priority over stale state)
49
- originalConfig;
50
- constructor(projectRoot, config) {
51
- super();
52
- // Resolve and validate projectRoot. Without this an attacker who can pass
53
- // a crafted projectRoot (env var, untrusted CLI flag, MCP tool argument)
54
- // gets arbitrary file write via the derived pidFile, logFile, stateFile,
55
- // and metrics paths.
56
- const resolved = resolve(projectRoot);
57
- this.projectRoot = resolved;
58
- this.originalConfig = config;
59
- const monomindDir = join(resolved, '.monomind');
60
- // Read daemon config from .monomind/config.json (Layer B)
61
- const fileConfig = this.readDaemonConfigFromFile(monomindDir);
62
- // CPU-proportional smart default instead of hardcoded 2.0
63
- const cpuCount = WorkerDaemon.getEffectiveCpuCount();
64
- const smartMaxCpuLoad = Math.max(cpuCount * 0.8, 2.0); // Floor of 2.0 for single-CPU machines
65
- // Platform-aware default: macOS os.freemem() excludes reclaimable file cache,
66
- // so reported "free" is much lower than actually available memory.
67
- // Linux reports available memory (including reclaimable cache) more accurately.
68
- const defaultMinFreeMemory = process.platform === 'darwin' ? 5 : 10;
69
- // Priority: constructor arg > config.json > smart default
70
- // For resourceThresholds, merge field-by-field so partial overrides
71
- // (e.g. only --max-cpu-load) still pick up defaults for other fields.
72
- this.config = {
73
- autoStart: config?.autoStart ?? fileConfig.autoStart ?? false,
74
- logDir: config?.logDir ?? join(monomindDir, 'logs'),
75
- stateFile: config?.stateFile ?? join(monomindDir, 'daemon-state.json'),
76
- maxConcurrent: config?.maxConcurrent ?? fileConfig.maxConcurrent ?? 2,
77
- workerTimeoutMs: config?.workerTimeoutMs ?? fileConfig.workerTimeoutMs ?? DEFAULT_WORKER_TIMEOUT_MS,
78
- resourceThresholds: {
79
- maxCpuLoad: config?.resourceThresholds?.maxCpuLoad ?? fileConfig.maxCpuLoad ?? smartMaxCpuLoad,
80
- minFreeMemoryPercent: config?.resourceThresholds?.minFreeMemoryPercent ?? fileConfig.minFreeMemoryPercent ?? defaultMinFreeMemory,
81
- },
82
- workers: config?.workers ?? DEFAULT_WORKERS,
83
- };
84
- // Setup graceful shutdown handlers
85
- this.setupShutdownHandlers();
86
- // Ensure directories exist
87
- if (!existsSync(monomindDir)) {
88
- mkdirSync(monomindDir, { recursive: true });
89
- }
90
- if (!existsSync(this.config.logDir)) {
91
- mkdirSync(this.config.logDir, { recursive: true });
92
- }
93
- // Initialize worker states
94
- this.initializeWorkerStates();
95
- // Initialize headless executor (async, non-blocking)
96
- this.initHeadlessExecutor().catch((err) => {
97
- this.log('warn', `Headless executor init failed: ${err}`);
98
- });
99
- }
100
- /**
101
- * Initialize headless executor if Claude Code is available
102
- */
103
- async initHeadlessExecutor() {
104
- try {
105
- this.headlessExecutor = new HeadlessWorkerExecutor(this.projectRoot, {
106
- maxConcurrent: this.config.maxConcurrent,
107
- });
108
- this.headlessAvailable = await this.headlessExecutor.isAvailable();
109
- if (this.headlessAvailable) {
110
- this.log('info', 'Claude Code headless mode available - AI workers enabled');
111
- // Forward headless executor events.
112
- // The executor emits 'start', 'complete', 'error' (not 'execution:*').
113
- this.headlessExecutor.on('start', (data) => {
114
- this.emit('headless:start', data);
115
- });
116
- this.headlessExecutor.on('complete', (data) => {
117
- this.emit('headless:complete', data);
118
- });
119
- this.headlessExecutor.on('error', (data) => {
120
- this.emit('headless:error', data);
121
- });
122
- this.headlessExecutor.on('output', (data) => {
123
- this.emit('headless:output', data);
124
- });
125
- }
126
- else {
127
- this.log('info', 'Claude Code not found - AI workers will run in local fallback mode');
128
- }
129
- }
130
- catch (error) {
131
- this.log('warn', `Failed to initialize headless executor: ${error}`);
132
- this.headlessAvailable = false;
133
- }
134
- }
135
- /**
136
- * Check if headless execution is available
137
- */
138
- isHeadlessAvailable() {
139
- return this.headlessAvailable;
140
- }
141
- /**
142
- * Get headless executor instance
143
- */
144
- getHeadlessExecutor() {
145
- return this.headlessExecutor;
146
- }
147
- /**
148
- * Detect effective CPU count for the current environment.
149
- *
150
- * Inside Docker / K8s containers, os.cpus().length reports the HOST cpu
151
- * count, not the container limit (Node.js #28762 — wontfix). We read
152
- * cgroup v2 / v1 quota files first so the maxCpuLoad threshold stays
153
- * meaningful under resource-limited containers.
154
- */
155
- static getEffectiveCpuCount() {
156
- // 1. Try cgroup v2: /sys/fs/cgroup/cpu.max
157
- try {
158
- const cpuMax = readFileSync('/sys/fs/cgroup/cpu.max', 'utf8').trim();
159
- const [quotaStr, periodStr] = cpuMax.split(' ');
160
- if (quotaStr !== 'max') {
161
- const quota = parseInt(quotaStr, 10);
162
- const period = parseInt(periodStr, 10);
163
- if (quota > 0 && period > 0)
164
- return Math.ceil(quota / period);
165
- }
166
- }
167
- catch { /* not in cgroup v2 */ }
168
- // 2. Try cgroup v1: /sys/fs/cgroup/cpu/cpu.cfs_quota_us
169
- try {
170
- const quota = parseInt(readFileSync('/sys/fs/cgroup/cpu/cpu.cfs_quota_us', 'utf8').trim(), 10);
171
- const period = parseInt(readFileSync('/sys/fs/cgroup/cpu/cpu.cfs_period_us', 'utf8').trim(), 10);
172
- if (quota > 0 && period > 0)
173
- return Math.ceil(quota / period);
174
- }
175
- catch { /* not in cgroup v1 */ }
176
- // 3. Fallback to os.cpus().length
177
- return cpus().length || 1;
178
- }
179
- /**
180
- * Read daemon-specific config from .monomind/config.json
181
- * Supports dot-notation keys like 'daemon.resourceThresholds.maxCpuLoad'
182
- */
183
- readDaemonConfigFromFile(monomindDir) {
184
- const configPath = join(monomindDir, 'config.json');
185
- if (!existsSync(configPath)) {
186
- // Warn if config.yaml exists but config.json does not (#1395 Bug 4)
187
- const yamlPath = join(monomindDir, 'config.yaml');
188
- const ymlPath = join(monomindDir, 'config.yml');
189
- if (existsSync(yamlPath) || existsSync(ymlPath)) {
190
- this.log('warn', `Found ${existsSync(yamlPath) ? 'config.yaml' : 'config.yml'} but daemon reads only config.json — YAML config is being ignored. Convert to JSON or create config.json.`);
191
- }
192
- return {};
193
- }
194
- try {
195
- // Guard against OOM from an oversized config file (tampered or corrupted).
196
- const configSize = statSync(configPath).size;
197
- if (configSize > 1_048_576 /* 1 MB */) {
198
- this.log('warn', `config.json is unusually large (${configSize} bytes) — ignoring daemon config`);
199
- return {};
200
- }
201
- const raw = JSON.parse(readFileSync(configPath, 'utf-8'));
202
- // Support both flat keys at root and nested under scopes.project
203
- const cfg = raw?.scopes?.project ?? raw;
204
- const rawCpuLoad = cfg['daemon.resourceThresholds.maxCpuLoad'] ?? raw['daemon.resourceThresholds.maxCpuLoad'];
205
- const rawMinMem = cfg['daemon.resourceThresholds.minFreeMemoryPercent'] ?? raw['daemon.resourceThresholds.minFreeMemoryPercent'];
206
- const rawMaxConcurrent = cfg['daemon.maxConcurrent'] ?? raw['daemon.maxConcurrent'];
207
- const rawTimeout = cfg['daemon.workerTimeoutMs'] ?? raw['daemon.workerTimeoutMs'];
208
- return {
209
- autoStart: typeof raw['daemon.autoStart'] === 'boolean' ? raw['daemon.autoStart'] : undefined,
210
- maxConcurrent: (typeof rawMaxConcurrent === 'number' && rawMaxConcurrent > 0) ? rawMaxConcurrent : undefined,
211
- workerTimeoutMs: (typeof rawTimeout === 'number' && rawTimeout > 0) ? rawTimeout : undefined,
212
- maxCpuLoad: (typeof rawCpuLoad === 'number' && rawCpuLoad > 0 && rawCpuLoad < 1000) ? rawCpuLoad : undefined,
213
- minFreeMemoryPercent: (typeof rawMinMem === 'number' && rawMinMem >= 0 && rawMinMem <= 100) ? rawMinMem : undefined,
214
- };
215
- }
216
- catch {
217
- return {};
218
- }
219
- }
220
- /**
221
- * Setup graceful shutdown handlers
222
- */
223
- setupShutdownHandlers() {
224
- const shutdown = async () => {
225
- this.log('info', 'Received shutdown signal, stopping daemon...');
226
- await this.stop();
227
- process.exit(0);
228
- };
229
- process.once('SIGTERM', shutdown);
230
- process.once('SIGINT', shutdown);
231
- process.once('SIGHUP', shutdown);
232
- }
233
- /**
234
- * Check if system resources allow worker execution
235
- */
236
- async canRunWorker() {
237
- const os = await import('os');
238
- const cpuLoad = os.loadavg()[0];
239
- const totalMem = os.totalmem();
240
- const freeMem = os.freemem();
241
- const freePercent = (freeMem / totalMem) * 100;
242
- if (cpuLoad > this.config.resourceThresholds.maxCpuLoad) {
243
- return { allowed: false, reason: `CPU load too high: ${cpuLoad.toFixed(2)}` };
244
- }
245
- if (freePercent < this.config.resourceThresholds.minFreeMemoryPercent) {
246
- return { allowed: false, reason: `Memory too low: ${freePercent.toFixed(1)}% free` };
247
- }
248
- return { allowed: true };
249
- }
250
- /**
251
- * Process pending workers queue
252
- *
253
- * When executeWorkerWithConcurrencyControl defers a worker (returns null),
254
- * we break immediately to avoid a busy-wait loop — the deferred worker is
255
- * already back on the pendingWorkers queue by that point. If no workers are
256
- * currently running when we break, we schedule a backoff retry so the queue
257
- * does not get permanently stuck.
258
- */
259
- async processPendingWorkers() {
260
- while (this.pendingWorkers.length > 0 && this.runningWorkers.size < this.config.maxConcurrent) {
261
- const workerType = this.pendingWorkers.shift();
262
- const workerConfig = this.config.workers.find(w => w.type === workerType);
263
- if (workerConfig) {
264
- const result = await this.executeWorkerWithConcurrencyControl(workerConfig);
265
- if (result === null) {
266
- // Worker was deferred (resource pressure or concurrency limit).
267
- // Break to avoid tight-looping — the next executeWorker() completion
268
- // will call processPendingWorkers() again via the finally block.
269
- if (this.runningWorkers.size === 0) {
270
- // No workers running means nobody will trigger the finally-block
271
- // callback, so schedule a backoff retry to avoid a stuck queue.
272
- setTimeout(() => this.processPendingWorkers(), 30_000).unref();
273
- }
274
- break;
275
- }
276
- }
277
- }
278
- }
279
- initializeWorkerStates() {
280
- // Try to restore state from file
281
- if (existsSync(this.config.stateFile)) {
282
- try {
283
- // Guard against OOM from an oversized state file (tampered or corrupted).
284
- const stateSize = statSync(this.config.stateFile).size;
285
- if (stateSize > 5_242_880 /* 5 MB */) {
286
- this.log('warn', `daemon-state.json is unusually large (${stateSize} bytes) — starting with fresh state`);
287
- return;
288
- }
289
- const saved = JSON.parse(readFileSync(this.config.stateFile, 'utf-8'));
290
- // CRITICAL: Restore worker config (including enabled flag) from saved state
291
- // This fixes #950: daemon enable command not persisting worker state
292
- if (saved.config?.workers && Array.isArray(saved.config.workers)) {
293
- for (const savedWorker of saved.config.workers) {
294
- const workerConfig = this.config.workers.find(w => w.type === savedWorker.type);
295
- if (workerConfig && typeof savedWorker.enabled === 'boolean') {
296
- workerConfig.enabled = savedWorker.enabled;
297
- }
298
- }
299
- }
300
- // Restore resourceThresholds, maxConcurrent, workerTimeoutMs from saved state
301
- // Only restore if valid numeric values within sane ranges
302
- if (saved.config?.resourceThresholds && !this.originalConfig?.resourceThresholds) {
303
- const rt = saved.config.resourceThresholds;
304
- if (typeof rt.maxCpuLoad === 'number' && rt.maxCpuLoad > 0 && rt.maxCpuLoad < 1000) {
305
- this.config.resourceThresholds.maxCpuLoad = rt.maxCpuLoad;
306
- }
307
- if (typeof rt.minFreeMemoryPercent === 'number' && rt.minFreeMemoryPercent >= 0 && rt.minFreeMemoryPercent <= 100) {
308
- this.config.resourceThresholds.minFreeMemoryPercent = rt.minFreeMemoryPercent;
309
- }
310
- }
311
- if (typeof saved.config?.maxConcurrent === 'number' && saved.config.maxConcurrent > 0) {
312
- this.config.maxConcurrent = saved.config.maxConcurrent;
313
- }
314
- if (typeof saved.config?.workerTimeoutMs === 'number' && saved.config.workerTimeoutMs > 0) {
315
- this.config.workerTimeoutMs = saved.config.workerTimeoutMs;
316
- }
317
- // Restore worker runtime states (runCount, successCount, etc.)
318
- // Validate keys against an allowlist to defeat prototype-pollution
319
- // attempts and reject unknown worker types injected into the file.
320
- if (saved.workers && typeof saved.workers === 'object') {
321
- const VALID_TYPES = new Set(this.config.workers.map(w => w.type));
322
- const FORBIDDEN = new Set(['__proto__', 'constructor', 'prototype']);
323
- const finiteNonNeg = (v) => {
324
- if (typeof v !== 'number' || !Number.isFinite(v) || v < 0)
325
- return 0;
326
- return Math.floor(v);
327
- };
328
- for (const [type, state] of Object.entries(saved.workers)) {
329
- if (FORBIDDEN.has(type) || !VALID_TYPES.has(type))
330
- continue;
331
- const savedState = state;
332
- const lastRunValue = savedState.lastRun;
333
- const lastRunDate = typeof lastRunValue === 'string' ? new Date(lastRunValue) : undefined;
334
- this.workers.set(type, {
335
- runCount: finiteNonNeg(savedState.runCount),
336
- successCount: finiteNonNeg(savedState.successCount),
337
- failureCount: finiteNonNeg(savedState.failureCount),
338
- averageDurationMs: finiteNonNeg(savedState.averageDurationMs),
339
- lastRun: lastRunDate && !isNaN(lastRunDate.getTime()) ? lastRunDate : undefined,
340
- nextRun: undefined,
341
- isRunning: false,
342
- });
343
- }
344
- }
345
- }
346
- catch {
347
- // Ignore parse errors, start fresh
348
- }
349
- }
350
- // Initialize any missing workers
351
- for (const workerConfig of this.config.workers) {
352
- if (!this.workers.has(workerConfig.type)) {
353
- this.workers.set(workerConfig.type, {
354
- runCount: 0,
355
- successCount: 0,
356
- failureCount: 0,
357
- averageDurationMs: 0,
358
- isRunning: false,
359
- });
360
- }
361
- }
362
- }
363
- /**
364
- * Get the PID file path for singleton enforcement (#1395 Bug 3).
365
- */
366
- get pidFile() {
367
- return join(this.projectRoot, '.monomind', 'daemon.pid');
368
- }
369
- /**
370
- * Check if another daemon instance is already running.
371
- * Returns the existing PID if alive, or null if no daemon is running.
372
- */
373
- checkExistingDaemon() {
374
- if (!existsSync(this.pidFile))
375
- return null;
376
- try {
377
- const pid = parseInt(readFileSync(this.pidFile, 'utf-8').trim(), 10);
378
- if (isNaN(pid))
379
- return null;
380
- // Check if process is alive (signal 0 = existence check)
381
- process.kill(pid, 0);
382
- return pid; // Process is alive
383
- }
384
- catch {
385
- // Process is dead — clean up stale PID file
386
- try {
387
- unlinkSync(this.pidFile);
388
- }
389
- catch { /* ignore */ }
390
- return null;
391
- }
392
- }
393
- /**
394
- * Write PID file with O_EXCL for atomic singleton enforcement (closes the
395
- * TOCTOU window between checkExistingDaemon and writePidFile that previously
396
- * allowed two concurrent `daemon start` calls to both succeed).
397
- * Returns true on success, false if another daemon raced us.
398
- */
399
- writePidFile() {
400
- const dir = join(this.projectRoot, '.monomind');
401
- if (!existsSync(dir))
402
- mkdirSync(dir, { recursive: true });
403
- try {
404
- // wx flag = O_CREAT | O_EXCL — atomic create-or-fail
405
- writeFileSync(this.pidFile, String(process.pid), { flag: 'wx' });
406
- return true;
407
- }
408
- catch (e) {
409
- const code = e.code;
410
- if (code === 'EEXIST') {
411
- // Recheck liveness — file may belong to a dead daemon we missed
412
- const racePid = this.checkExistingDaemon();
413
- if (racePid === null) {
414
- // Stale file was just removed by checkExistingDaemon, retry once
415
- try {
416
- writeFileSync(this.pidFile, String(process.pid), { flag: 'wx' });
417
- return true;
418
- }
419
- catch {
420
- return false;
421
- }
422
- }
423
- return false;
424
- }
425
- throw e;
426
- }
427
- }
428
- /**
429
- * Remove PID file on shutdown.
430
- */
431
- removePidFile() {
432
- try {
433
- unlinkSync(this.pidFile);
434
- }
435
- catch { /* ignore */ }
436
- }
437
- /**
438
- * Start the daemon and all enabled workers
439
- */
440
- async start() {
441
- if (this.running) {
442
- this.emit('warning', 'Daemon already running');
443
- return;
444
- }
445
- // PID singleton enforcement (#1395 Bug 3): prevent daemon accumulation
446
- const existingPid = this.checkExistingDaemon();
447
- if (existingPid !== null) {
448
- this.log('info', `Daemon already running (PID: ${existingPid}), skipping start`);
449
- this.emit('warning', `Daemon already running (PID: ${existingPid})`);
450
- return;
451
- }
452
- // Atomic PID file write — fails if another daemon raced us past
453
- // checkExistingDaemon. Without this two `daemon start` calls could both
454
- // pass the existence check then both clobber the PID file.
455
- if (!this.writePidFile()) {
456
- this.log('info', 'Another daemon won the PID race — aborting start');
457
- this.emit('warning', 'Another daemon instance is starting/running');
458
- return;
459
- }
460
- this.running = true;
461
- this.startedAt = new Date();
462
- this.emit('started', { pid: process.pid, startedAt: this.startedAt });
463
- // Schedule all enabled workers
464
- for (const workerConfig of this.config.workers) {
465
- if (workerConfig.enabled) {
466
- this.scheduleWorker(workerConfig);
467
- }
468
- }
469
- // Save state
470
- this.saveState();
471
- this.log('info', `Daemon started (PID: ${process.pid}, CPUs: ${cpus().length}, workers: ${this.config.workers.filter(w => w.enabled).length}, maxCpuLoad: ${this.config.resourceThresholds.maxCpuLoad}, minFreeMemoryPercent: ${this.config.resourceThresholds.minFreeMemoryPercent}%)`);
472
- }
473
- /**
474
- * Stop the daemon and all workers
475
- */
476
- async stop() {
477
- if (!this.running) {
478
- this.emit('warning', 'Daemon not running');
479
- return;
480
- }
481
- // Clear all timers (convert to array to avoid iterator issues)
482
- const timerEntries = Array.from(this.timers.entries());
483
- for (const [type, timer] of timerEntries) {
484
- clearTimeout(timer);
485
- this.log('info', `Stopped worker: ${type}`);
486
- }
487
- this.timers.clear();
488
- this.running = false;
489
- this.removePidFile();
490
- this.saveState();
491
- this.emit('stopped', { stoppedAt: new Date() });
492
- this.log('info', 'Daemon stopped');
493
- }
494
- /**
495
- * Get daemon status
496
- */
497
- getStatus() {
498
- return {
499
- running: this.running,
500
- pid: process.pid,
501
- startedAt: this.startedAt,
502
- workers: new Map(this.workers),
503
- config: this.config,
504
- };
505
- }
506
- /**
507
- * Schedule a worker to run at intervals with staggered start
508
- */
509
- scheduleWorker(workerConfig) {
510
- const state = this.workers.get(workerConfig.type);
511
- const internalConfig = workerConfig;
512
- const staggerOffset = internalConfig.offsetMs || 0;
513
- // Calculate initial delay with stagger offset
514
- let initialDelay = staggerOffset;
515
- if (state.lastRun) {
516
- const timeSinceLastRun = Date.now() - state.lastRun.getTime();
517
- initialDelay = Math.max(staggerOffset, workerConfig.intervalMs - timeSinceLastRun);
518
- }
519
- state.nextRun = new Date(Date.now() + initialDelay);
520
- const runAndReschedule = async () => {
521
- if (!this.running)
522
- return;
523
- // Use concurrency-controlled execution (P0 fix)
524
- await this.executeWorkerWithConcurrencyControl(workerConfig);
525
- // Reschedule
526
- if (this.running) {
527
- const timer = setTimeout(runAndReschedule, workerConfig.intervalMs);
528
- this.timers.set(workerConfig.type, timer);
529
- state.nextRun = new Date(Date.now() + workerConfig.intervalMs);
530
- }
531
- };
532
- // Schedule first run with stagger offset
533
- const timer = setTimeout(runAndReschedule, initialDelay);
534
- this.timers.set(workerConfig.type, timer);
535
- this.log('info', `Scheduled ${workerConfig.type} (interval: ${workerConfig.intervalMs / 1000}s, first run in ${initialDelay / 1000}s)`);
536
- }
537
- /**
538
- * Execute a worker with concurrency control (P0 fix)
539
- */
540
- async executeWorkerWithConcurrencyControl(workerConfig) {
541
- // Dedup: under sustained resource pressure each scheduler tick re-pushes
542
- // the same WorkerType. Without dedup, high-priority workers (audit) crowd
543
- // out low-priority ones (consolidate) — starvation. Skip the push if the
544
- // type is already pending.
545
- const pushPending = (type) => {
546
- if (!this.pendingWorkers.includes(type))
547
- this.pendingWorkers.push(type);
548
- };
549
- // Check concurrency limit
550
- if (this.runningWorkers.size >= this.config.maxConcurrent) {
551
- this.log('info', `Worker ${workerConfig.type} deferred: max concurrent (${this.config.maxConcurrent}) reached`);
552
- pushPending(workerConfig.type);
553
- this.emit('worker:deferred', { type: workerConfig.type, reason: 'max_concurrent' });
554
- return null;
555
- }
556
- // Check resource availability
557
- const resourceCheck = await this.canRunWorker();
558
- if (!resourceCheck.allowed) {
559
- this.log('info', `Worker ${workerConfig.type} deferred: ${resourceCheck.reason}`);
560
- pushPending(workerConfig.type);
561
- this.emit('worker:deferred', { type: workerConfig.type, reason: resourceCheck.reason });
562
- return null;
563
- }
564
- return this.executeWorker(workerConfig);
565
- }
566
- /**
567
- * Execute a worker with timeout protection
568
- */
569
- async executeWorker(workerConfig) {
570
- const state = this.workers.get(workerConfig.type);
571
- const workerId = `${workerConfig.type}_${Date.now()}`;
572
- const startTime = Date.now();
573
- // Track the headless executionId assigned by HeadlessWorkerExecutor so we
574
- // can cancel it precisely on timeout. The executor generates its own ID
575
- // (format: `${type}_${ts}_${random}`) which never matches workerId, so we
576
- // capture it from the 'start' event instead.
577
- let headlessExecutionId = null;
578
- const onHeadlessStart = (data) => {
579
- headlessExecutionId = data.executionId ?? null;
580
- };
581
- if (this.headlessExecutor) {
582
- this.headlessExecutor.on('start', onHeadlessStart);
583
- }
584
- // Track running worker
585
- this.runningWorkers.add(workerConfig.type);
586
- state.isRunning = true;
587
- this.emit('worker:start', { workerId, type: workerConfig.type });
588
- this.log('info', `Starting worker: ${workerConfig.type} (${this.runningWorkers.size}/${this.config.maxConcurrent} concurrent)`);
589
- try {
590
- // Execute worker logic with timeout (P1 fix)
591
- // Pass cleanup callback to kill only this worker's child process on timeout.
592
- // cancelAll() was too broad — it would kill concurrent healthy workers.
593
- const output = await this.runWithTimeout(() => this.runWorkerLogic(workerConfig), this.config.workerTimeoutMs, `Worker ${workerConfig.type} timed out after ${this.config.workerTimeoutMs / 1000}s`, () => {
594
- if (this.headlessExecutor) {
595
- // Use the exact executionId captured from the 'start' event.
596
- // Fall back to cancelByType only if we didn't capture an ID yet
597
- // (e.g. timeout fired before the executor emitted 'start').
598
- if (headlessExecutionId && this.headlessExecutor.cancel(headlessExecutionId)) {
599
- return;
600
- }
601
- this.headlessExecutor.cancelByType(workerConfig.type);
602
- }
603
- });
604
- const durationMs = Date.now() - startTime;
605
- // Update state
606
- state.runCount++;
607
- state.successCount++;
608
- state.lastRun = new Date();
609
- state.averageDurationMs = (state.averageDurationMs * (state.runCount - 1) + durationMs) / state.runCount;
610
- state.isRunning = false;
611
- const result = {
612
- workerId,
613
- type: workerConfig.type,
614
- success: true,
615
- durationMs,
616
- output,
617
- timestamp: new Date(),
618
- };
619
- this.emit('worker:complete', result);
620
- this.log('info', `Worker ${workerConfig.type} completed in ${durationMs}ms`);
621
- this.saveState();
622
- return result;
623
- }
624
- catch (error) {
625
- const durationMs = Date.now() - startTime;
626
- state.runCount++;
627
- state.failureCount++;
628
- state.lastRun = new Date();
629
- state.isRunning = false;
630
- const result = {
631
- workerId,
632
- type: workerConfig.type,
633
- success: false,
634
- durationMs,
635
- error: error instanceof Error ? error.message : String(error),
636
- timestamp: new Date(),
637
- };
638
- this.emit('worker:error', result);
639
- this.log('error', `Worker ${workerConfig.type} failed: ${result.error}`);
640
- this.saveState();
641
- return result;
642
- }
643
- finally {
644
- // Remove from running set and process queue
645
- this.runningWorkers.delete(workerConfig.type);
646
- // Unsubscribe the executionId capture listener regardless of outcome
647
- if (this.headlessExecutor) {
648
- this.headlessExecutor.off('start', onHeadlessStart);
649
- }
650
- this.processPendingWorkers();
651
- }
652
- }
653
- /**
654
- * Run a function with timeout (P1 fix)
655
- * @param fn - The async function to execute
656
- * @param timeoutMs - Timeout in milliseconds
657
- * @param timeoutMessage - Error message on timeout
658
- * @param onTimeout - Optional cleanup callback invoked when timeout fires (#1117: kills orphan processes)
659
- */
660
- async runWithTimeout(fn, timeoutMs, timeoutMessage, onTimeout) {
661
- return new Promise((resolve, reject) => {
662
- let settled = false;
663
- const timer = setTimeout(() => {
664
- if (settled)
665
- return;
666
- settled = true;
667
- // Kill orphan child processes before rejecting (#1117)
668
- if (onTimeout) {
669
- try {
670
- onTimeout();
671
- }
672
- catch {
673
- // Ignore cleanup errors
674
- }
675
- }
676
- reject(new Error(timeoutMessage));
677
- }, timeoutMs);
678
- fn()
679
- .then((result) => {
680
- if (settled)
681
- return;
682
- settled = true;
683
- clearTimeout(timer);
684
- resolve(result);
685
- })
686
- .catch((error) => {
687
- if (settled)
688
- return;
689
- settled = true;
690
- clearTimeout(timer);
691
- reject(error);
692
- });
693
- });
694
- }
695
- /**
696
- * Run the actual worker logic
697
- */
698
- async runWorkerLogic(workerConfig) {
699
- // Check if this is a headless worker type and headless execution is available
700
- if (isHeadlessWorker(workerConfig.type) && this.headlessAvailable && this.headlessExecutor) {
701
- try {
702
- this.log('info', `Running ${workerConfig.type} in headless mode (Claude Code AI)`);
703
- const result = await this.headlessExecutor.execute(workerConfig.type);
704
- return {
705
- mode: 'headless',
706
- ...result,
707
- };
708
- }
709
- catch (error) {
710
- this.log('warn', `Headless execution failed for ${workerConfig.type}, falling back to local mode`);
711
- this.emit('headless:fallback', {
712
- type: workerConfig.type,
713
- error: error instanceof Error ? error.message : String(error),
714
- });
715
- // Fall through to local execution
716
- }
717
- }
718
- // Local execution (fallback or for non-headless workers)
719
- switch (workerConfig.type) {
720
- case 'map':
721
- return this.runMapWorker();
722
- case 'audit':
723
- return this.runAuditWorkerLocal();
724
- case 'optimize':
725
- return this.runOptimizeWorkerLocal();
726
- case 'consolidate':
727
- return this.runConsolidateWorker();
728
- case 'ultralearn':
729
- return this.runUltralearnWorkerLocal();
730
- case 'deepdive':
731
- return this.runDeepdiveWorkerLocal();
732
- case 'benchmark':
733
- return this.runBenchmarkWorkerLocal();
734
- // testgaps/predict/document/refactor/preload have no real local-mode
735
- // implementation — they only do real work in headless mode (see
736
- // HeadlessWorkerExecutor). Without Claude Code CLI available there is
737
- // nothing meaningful to run locally, so report that plainly instead of
738
- // faking a result with hardcoded empty JSON (monolean).
739
- case 'testgaps':
740
- case 'predict':
741
- case 'document':
742
- case 'refactor':
743
- case 'preload':
744
- return { mode: 'local', note: 'Install Claude Code CLI for AI-powered analysis — no local implementation exists for this worker' };
745
- default:
746
- return { status: 'unknown worker type', mode: 'local' };
747
- }
748
- }
749
- // Worker implementations
750
- async runMapWorker() {
751
- // Scan project structure and update metrics
752
- const metricsFile = join(this.projectRoot, '.monomind', 'metrics', 'codebase-map.json');
753
- const metricsDir = join(this.projectRoot, '.monomind', 'metrics');
754
- if (!existsSync(metricsDir)) {
755
- mkdirSync(metricsDir, { recursive: true });
756
- }
757
- const map = {
758
- timestamp: new Date().toISOString(),
759
- structure: {
760
- hasPackageJson: existsSync(join(this.projectRoot, 'package.json')),
761
- hasTsConfig: existsSync(join(this.projectRoot, 'tsconfig.json')),
762
- hasClaudeConfig: existsSync(join(this.projectRoot, '.claude')),
763
- hasMonomind: existsSync(join(this.projectRoot, '.monomind')),
764
- },
765
- scannedAt: Date.now(),
766
- };
767
- // Enrich with monograph graph stats for LLM context injection.
768
- // Lazy-import to avoid hard dependency; silently skip on any error.
769
- try {
770
- const { openDb, closeDb, countNodes, countEdges } = await import('@monoes/monograph');
771
- const dbPath = join(this.projectRoot, '.monomind', 'monograph.db');
772
- if (existsSync(dbPath)) {
773
- const db = openDb(dbPath);
774
- try {
775
- // Node/edge counts
776
- map['graph'] = {
777
- nodes: countNodes(db),
778
- edges: countEdges(db),
779
- };
780
- // Top 3 god nodes (high degree internal files) — same SQL as monograph_god_nodes tool
781
- const excluded = ['File', 'Folder', 'Community', 'Concept'];
782
- const rows = db.prepare(`
783
- SELECT n.name, n.file_path, n.start_line,
784
- COUNT(DISTINCT e1.id) + COUNT(DISTINCT e2.id) AS degree
785
- FROM nodes n
786
- LEFT JOIN edges e1 ON e1.source_id = n.id
787
- LEFT JOIN edges e2 ON e2.target_id = n.id
788
- WHERE n.label NOT IN (${excluded.map(() => '?').join(',')})
789
- GROUP BY n.id HAVING degree > 0
790
- ORDER BY degree DESC LIMIT 3
791
- `).all(...excluded);
792
- if (rows.length > 0) {
793
- map['topFiles'] = rows.map(r => ({
794
- ref: r.file_path
795
- ? (r.start_line != null ? `${r.file_path}:${r.start_line}` : r.file_path)
796
- : r.name,
797
- degree: r.degree,
798
- }));
799
- }
800
- // Index staleness via git — same approach as monograph_health tool
801
- try {
802
- const { execSync } = await import('child_process');
803
- const lastHash = db.prepare("SELECT value FROM meta WHERE key = 'last_commit_hash' LIMIT 1").get()?.value;
804
- if (lastHash) {
805
- const countOut = execSync(`git -C ${JSON.stringify(this.projectRoot)} rev-list --count ${lastHash}..HEAD`, { timeout: 5000 }).toString().trim();
806
- const commitsBehind = parseInt(countOut, 10);
807
- if (!isNaN(commitsBehind)) {
808
- map['graphStaleness'] = { commitsBehind };
809
- }
810
- }
811
- }
812
- catch { /* git unavailable — skip staleness */ }
813
- }
814
- finally {
815
- closeDb(db);
816
- }
817
- }
818
- }
819
- catch { /* monograph unavailable — skip graph enrichment */ }
820
- const metricsFileTmp1 = metricsFile + '.tmp';
821
- writeFileSync(metricsFileTmp1, JSON.stringify(map, null, 2));
822
- renameSync(metricsFileTmp1, metricsFile);
823
- return map;
824
- }
825
- /**
826
- * Local audit worker (fallback when headless unavailable)
827
- */
828
- async runAuditWorkerLocal() {
829
- // Basic security checks
830
- const auditFile = join(this.projectRoot, '.monomind', 'metrics', 'security-audit.json');
831
- const metricsDir = join(this.projectRoot, '.monomind', 'metrics');
832
- if (!existsSync(metricsDir)) {
833
- mkdirSync(metricsDir, { recursive: true });
834
- }
835
- const audit = {
836
- timestamp: new Date().toISOString(),
837
- mode: 'local',
838
- checks: {
839
- envFilesProtected: !existsSync(join(this.projectRoot, '.env.local')),
840
- gitIgnoreExists: existsSync(join(this.projectRoot, '.gitignore')),
841
- noHardcodedSecrets: null, // Not checked in local mode — requires AI-powered scan
842
- },
843
- riskLevel: 'low',
844
- recommendations: [],
845
- note: 'Install Claude Code CLI for AI-powered security analysis',
846
- };
847
- // Enrich with monograph high-centrality files and surprising cross-community edges.
848
- // God-node files are high-value targets for security review: they are imported by
849
- // many consumers, so a vulnerability there has the largest blast radius.
850
- // Cross-community edges reveal unexpected coupling that may indicate hidden attack surfaces.
851
- try {
852
- const { openDb, closeDb } = await import('@monoes/monograph');
853
- const dbPath = join(this.projectRoot, '.monomind', 'monograph.db');
854
- if (existsSync(dbPath)) {
855
- const db = openDb(dbPath);
856
- try {
857
- const godFileRows = db.prepare(`
858
- SELECT n.file_path,
859
- COUNT(DISTINCT e1.id) + COUNT(DISTINCT e2.id) AS degree
860
- FROM nodes n
861
- LEFT JOIN edges e1 ON e1.source_id = n.id
862
- LEFT JOIN edges e2 ON e2.target_id = n.id
863
- WHERE n.label NOT IN ('File','Folder','Community','Concept')
864
- AND n.file_path IS NOT NULL
865
- AND n.file_path NOT LIKE '%node_modules%'
866
- AND n.file_path NOT LIKE '%/dist/%'
867
- AND n.file_path NOT LIKE '%.test.%'
868
- AND n.file_path NOT LIKE '%.spec.%'
869
- GROUP BY n.file_path
870
- ORDER BY degree DESC
871
- LIMIT 5
872
- `).all();
873
- const surpriseRows = db.prepare(`
874
- SELECT n1.name as src_name, n2.name as tgt_name, e.relation, e.confidence_score,
875
- n1.file_path as src_file, n2.file_path as tgt_file
876
- FROM edges e
877
- JOIN nodes n1 ON n1.id = e.source_id
878
- JOIN nodes n2 ON n2.id = e.target_id
879
- WHERE e.confidence != 'EXTRACTED'
880
- AND n1.community_id IS NOT NULL
881
- AND n2.community_id IS NOT NULL
882
- AND n1.community_id != n2.community_id
883
- ORDER BY e.confidence_score ASC
884
- LIMIT 5
885
- `).all();
886
- if (godFileRows.length > 0) {
887
- audit['priorityScanTargets'] = godFileRows.map(r => ({
888
- file: r.file_path.replace(this.projectRoot + '/', '').replace(this.projectRoot + '\\', ''),
889
- degree: r.degree,
890
- reason: 'high-centrality: vulnerability here affects the most consumers',
891
- }));
892
- }
893
- if (surpriseRows.length > 0) {
894
- audit['unexpectedCoupling'] = surpriseRows.map(r => ({
895
- edge: `${r.src_name} --${r.relation}--> ${r.tgt_name}`,
896
- srcFile: r.src_file ?? '(unknown)',
897
- tgtFile: r.tgt_file ?? '(unknown)',
898
- confidenceScore: r.confidence_score,
899
- reason: 'cross-community edge: may indicate hidden dependency or attack surface',
900
- }));
901
- }
902
- }
903
- finally {
904
- closeDb(db);
905
- }
906
- }
907
- }
908
- catch { /* monograph unavailable — skip graph enrichment */ }
909
- const auditFileTmp = auditFile + '.tmp';
910
- writeFileSync(auditFileTmp, JSON.stringify(audit, null, 2));
911
- renameSync(auditFileTmp, auditFile);
912
- return audit;
913
- }
914
- /**
915
- * Local optimize worker (fallback when headless unavailable)
916
- */
917
- async runOptimizeWorkerLocal() {
918
- // Update performance metrics
919
- const optimizeFile = join(this.projectRoot, '.monomind', 'metrics', 'performance.json');
920
- const metricsDir = join(this.projectRoot, '.monomind', 'metrics');
921
- if (!existsSync(metricsDir)) {
922
- mkdirSync(metricsDir, { recursive: true });
923
- }
924
- const perf = {
925
- timestamp: new Date().toISOString(),
926
- mode: 'local',
927
- memoryUsage: process.memoryUsage(),
928
- uptime: process.uptime(),
929
- optimizations: {
930
- cacheHitRate: null, // Not measured in local mode — requires AI-powered analysis
931
- avgResponseTime: null, // Not measured in local mode — requires AI-powered analysis
932
- },
933
- note: 'Install Claude Code CLI for AI-powered optimization suggestions',
934
- };
935
- const optimizeFileTmp = optimizeFile + '.tmp';
936
- writeFileSync(optimizeFileTmp, JSON.stringify(perf, null, 2));
937
- renameSync(optimizeFileTmp, optimizeFile);
938
- return perf;
939
- }
940
- async runConsolidateWorker() {
941
- // RAPTOR-style memory consolidation: cluster episodic entries by namespace,
942
- // generate a summary entry as 'contextual' type referencing source cluster.
943
- // Source: https://arxiv.org/abs/2401.18059 (RAPTOR — ICLR 2024)
944
- const consolidateFile = join(this.projectRoot, '.monomind', 'metrics', 'consolidation.json');
945
- const metricsDir = join(this.projectRoot, '.monomind', 'metrics');
946
- if (!existsSync(metricsDir)) {
947
- mkdirSync(metricsDir, { recursive: true });
948
- }
949
- let patternsConsolidated = 0;
950
- let clustersCreated = 0;
951
- try {
952
- // Lazy-import memory bridge to avoid hard dependency in worker
953
- const { bridgeSearchEntries, bridgeStoreEntry } = await import('../memory/memory-bridge.js');
954
- // Retrieve recent episodic entries (short-term tier) for RAPTOR clustering
955
- const episodic = await bridgeSearchEntries({
956
- query: 'task outcome agent pattern',
957
- namespace: 'patterns',
958
- limit: 50,
959
- threshold: 0.0,
960
- });
961
- if (episodic?.results && episodic.results.length >= 3) {
962
- // Group into clusters of ~5 by simple sequential chunking
963
- const CLUSTER_SIZE = 5;
964
- for (let i = 0; i < episodic.results.length; i += CLUSTER_SIZE) {
965
- const cluster = episodic.results.slice(i, i + CLUSTER_SIZE);
966
- if (cluster.length < 2)
967
- continue;
968
- // Build cluster summary (lightweight abstraction without LLM)
969
- const keys = cluster.map(r => r.key).join(', ');
970
- const summary = `RAPTOR cluster [${Math.floor(i / CLUSTER_SIZE)}]: ` +
971
- `${cluster.length} patterns consolidated. Topics: ${keys.slice(0, 120)}`;
972
- await bridgeStoreEntry({
973
- key: `raptor_cluster:${Date.now()}_${i}`,
974
- value: summary,
975
- namespace: 'contextual',
976
- tags: ['raptor', 'cluster_summary'],
977
- });
978
- patternsConsolidated += cluster.length;
979
- clustersCreated++;
980
- }
981
- }
982
- }
983
- catch { /* non-critical — bridge may be unavailable */ }
984
- const result = {
985
- timestamp: new Date().toISOString(),
986
- patternsConsolidated,
987
- clustersCreated,
988
- memoryCleaned: 0,
989
- duplicatesRemoved: 0,
990
- mode: 'raptor',
991
- };
992
- const consolidateFileTmp = consolidateFile + '.tmp';
993
- writeFileSync(consolidateFileTmp, JSON.stringify(result, null, 2));
994
- renameSync(consolidateFileTmp, consolidateFile);
995
- return result;
996
- }
997
- /**
998
- * Local ultralearn worker (fallback when headless unavailable)
999
- */
1000
- async runUltralearnWorkerLocal() {
1001
- const result = {
1002
- timestamp: new Date().toISOString(),
1003
- mode: 'local',
1004
- patternsLearned: 0,
1005
- insightsGained: [],
1006
- };
1007
- // Enrich with monograph community clusters and bridge-node patterns for LLM context injection.
1008
- // Bridge nodes (symbols that cross community boundaries) are architecturally significant —
1009
- // they represent coupling points an LLM should be aware of when reasoning about change impact.
1010
- try {
1011
- const { openDb, closeDb, countNodes, countEdges } = await import('@monoes/monograph');
1012
- const dbPath = join(this.projectRoot, '.monomind', 'monograph.db');
1013
- if (existsSync(dbPath)) {
1014
- const db = openDb(dbPath);
1015
- try {
1016
- const nodeCount = countNodes(db);
1017
- const edgeCount = countEdges(db);
1018
- const communityRows = db.prepare(`
1019
- SELECT community_id, COUNT(*) AS member_count
1020
- FROM nodes
1021
- WHERE community_id IS NOT NULL
1022
- AND label NOT IN ('File','Folder','Community','Concept')
1023
- GROUP BY community_id
1024
- ORDER BY member_count DESC
1025
- LIMIT 5
1026
- `).all();
1027
- const bridgeRows = db.prepare(`
1028
- SELECT n.name, n.label, n.file_path, n.start_line,
1029
- COUNT(DISTINCT e.id) AS cross_edges
1030
- FROM nodes n
1031
- JOIN edges e ON (e.source_id = n.id OR e.target_id = n.id)
1032
- JOIN nodes n2 ON (
1033
- CASE WHEN e.source_id = n.id THEN e.target_id ELSE e.source_id END = n2.id
1034
- )
1035
- WHERE n.community_id IS NOT NULL
1036
- AND n2.community_id IS NOT NULL
1037
- AND n.community_id != n2.community_id
1038
- AND n.label NOT IN ('File','Folder','Community','Concept')
1039
- AND (n.file_path IS NULL OR (
1040
- n.file_path NOT LIKE '%node_modules%'
1041
- AND n.file_path NOT LIKE '%/dist/%'
1042
- AND n.file_path NOT LIKE '%.test.%'
1043
- AND n.file_path NOT LIKE '%.spec.%'
1044
- ))
1045
- GROUP BY n.id
1046
- ORDER BY cross_edges DESC
1047
- LIMIT 5
1048
- `).all();
1049
- const insights = result['insightsGained'];
1050
- if (communityRows.length > 0) {
1051
- insights.push({
1052
- category: 'community_clusters',
1053
- description: `Top ${communityRows.length} community clusters by size`,
1054
- items: communityRows.map(r => ({
1055
- communityId: r.community_id,
1056
- memberCount: r.member_count,
1057
- })),
1058
- });
1059
- }
1060
- if (bridgeRows.length > 0) {
1061
- insights.push({
1062
- category: 'bridge_nodes',
1063
- description: `Top ${bridgeRows.length} bridge nodes crossing community boundaries (high coupling risk)`,
1064
- items: bridgeRows.map(r => {
1065
- const loc = r.file_path
1066
- ? (r.start_line != null ? `${r.file_path}:${r.start_line}` : r.file_path)
1067
- : '(unknown)';
1068
- return {
1069
- name: r.name,
1070
- label: r.label,
1071
- location: loc,
1072
- crossCommunityEdges: r.cross_edges,
1073
- };
1074
- }),
1075
- });
1076
- result['patternsLearned'] = bridgeRows.length + communityRows.length;
1077
- }
1078
- result['graph'] = { nodes: nodeCount, edges: edgeCount };
1079
- }
1080
- finally {
1081
- closeDb(db);
1082
- }
1083
- }
1084
- else {
1085
- result['note'] = 'Monograph index not built — run `monomind monograph build` for deep learning';
1086
- }
1087
- }
1088
- catch { /* monograph unavailable — return minimal result */ }
1089
- return result;
1090
- }
1091
- /**
1092
- * Local deepdive worker (fallback when headless unavailable)
1093
- */
1094
- async runDeepdiveWorkerLocal() {
1095
- const deepdiveFile = join(this.projectRoot, '.monomind', 'metrics', 'deepdive.json');
1096
- const metricsDir = join(this.projectRoot, '.monomind', 'metrics');
1097
- if (!existsSync(metricsDir)) {
1098
- mkdirSync(metricsDir, { recursive: true });
1099
- }
1100
- const result = {
1101
- timestamp: new Date().toISOString(),
1102
- mode: 'local',
1103
- analysisDepth: 'graph',
1104
- findings: [],
1105
- };
1106
- // Enrich with monograph god nodes and high-degree file analysis for LLM context injection.
1107
- // Lazy-import to avoid hard dependency; silently skip on any error.
1108
- try {
1109
- const { openDb, closeDb, countNodes, countEdges } = await import('@monoes/monograph');
1110
- const dbPath = join(this.projectRoot, '.monomind', 'monograph.db');
1111
- if (existsSync(dbPath)) {
1112
- const db = openDb(dbPath);
1113
- try {
1114
- const nodeCount = countNodes(db);
1115
- const edgeCount = countEdges(db);
1116
- const godNodeRows = db.prepare(`
1117
- SELECT n.name, n.file_path, n.label,
1118
- COUNT(DISTINCT e1.id) + COUNT(DISTINCT e2.id) AS degree
1119
- FROM nodes n
1120
- LEFT JOIN edges e1 ON e1.source_id = n.id
1121
- LEFT JOIN edges e2 ON e2.target_id = n.id
1122
- WHERE n.label NOT IN ('File','Folder','Community','Concept')
1123
- AND (n.file_path IS NULL OR (
1124
- n.file_path NOT LIKE '%node_modules%'
1125
- AND n.file_path NOT LIKE '%/dist/%'
1126
- AND n.file_path NOT LIKE '%.test.%'
1127
- AND n.file_path NOT LIKE '%.spec.%'
1128
- ))
1129
- GROUP BY n.id
1130
- ORDER BY degree DESC
1131
- LIMIT 5
1132
- `).all();
1133
- const godNodes = godNodeRows.map(r => ({
1134
- name: r.name,
1135
- label: r.label,
1136
- file: r.file_path ?? '(unknown)',
1137
- degree: r.degree,
1138
- }));
1139
- const fileRows = db.prepare(`
1140
- SELECT n.file_path,
1141
- COUNT(DISTINCT e2.id) AS in_deg,
1142
- COUNT(DISTINCT e1.id) AS out_deg
1143
- FROM nodes n
1144
- LEFT JOIN edges e1 ON e1.source_id = n.id
1145
- LEFT JOIN edges e2 ON e2.target_id = n.id
1146
- WHERE n.label = 'File'
1147
- AND n.file_path NOT LIKE '%node_modules%'
1148
- AND n.file_path NOT LIKE '%/dist/%'
1149
- AND n.file_path NOT LIKE '%.test.%'
1150
- AND n.file_path NOT LIKE '%.spec.%'
1151
- GROUP BY n.id
1152
- ORDER BY (in_deg + out_deg) DESC
1153
- LIMIT 5
1154
- `).all();
1155
- const highDegFiles = fileRows.map(r => ({
1156
- file: r.file_path
1157
- .replace(this.projectRoot + '/', '')
1158
- .replace(this.projectRoot + '\\', ''),
1159
- inDegree: r.in_deg,
1160
- outDegree: r.out_deg,
1161
- totalDegree: r.in_deg + r.out_deg,
1162
- }));
1163
- const findings = [];
1164
- if (godNodes.length > 0) {
1165
- findings.push({
1166
- category: 'god_nodes',
1167
- description: `Top ${godNodes.length} high-centrality symbols (most imported/referenced)`,
1168
- items: godNodes,
1169
- });
1170
- }
1171
- if (highDegFiles.length > 0) {
1172
- findings.push({
1173
- category: 'high_degree_files',
1174
- description: `Top ${highDegFiles.length} files by total edge degree (import + export connections)`,
1175
- items: highDegFiles,
1176
- });
1177
- }
1178
- result['graph'] = { nodes: nodeCount, edges: edgeCount };
1179
- result['findings'] = findings;
1180
- result['analysisDepth'] = 'graph';
1181
- }
1182
- finally {
1183
- closeDb(db);
1184
- }
1185
- }
1186
- else {
1187
- result['note'] = 'Monograph index not built — run `monomind monograph build` for deep analysis';
1188
- }
1189
- }
1190
- catch { /* monograph unavailable — return minimal result */ }
1191
- const deepdiveFileTmp = deepdiveFile + '.tmp';
1192
- writeFileSync(deepdiveFileTmp, JSON.stringify(result, null, 2));
1193
- renameSync(deepdiveFileTmp, deepdiveFile);
1194
- return result;
1195
- }
1196
- /**
1197
- * Local benchmark worker
1198
- */
1199
- async runBenchmarkWorkerLocal() {
1200
- const benchmarkFile = join(this.projectRoot, '.monomind', 'metrics', 'benchmark.json');
1201
- const metricsDir = join(this.projectRoot, '.monomind', 'metrics');
1202
- if (!existsSync(metricsDir)) {
1203
- mkdirSync(metricsDir, { recursive: true });
1204
- }
1205
- const result = {
1206
- timestamp: new Date().toISOString(),
1207
- mode: 'local',
1208
- benchmarks: {
1209
- memoryUsage: process.memoryUsage(),
1210
- cpuUsage: process.cpuUsage(),
1211
- uptime: process.uptime(),
1212
- },
1213
- };
1214
- const benchmarkFileTmp = benchmarkFile + '.tmp';
1215
- writeFileSync(benchmarkFileTmp, JSON.stringify(result, null, 2));
1216
- renameSync(benchmarkFileTmp, benchmarkFile);
1217
- return result;
1218
- }
1219
- /**
1220
- * Manually trigger a worker
1221
- */
1222
- async triggerWorker(type) {
1223
- const workerConfig = this.config.workers.find(w => w.type === type);
1224
- if (!workerConfig) {
1225
- throw new Error(`Unknown worker type: ${type}`);
1226
- }
1227
- const result = await this.executeWorkerWithConcurrencyControl(workerConfig);
1228
- if (result !== null)
1229
- return result;
1230
- // Concurrency limit reached — execute directly for explicit manual trigger.
1231
- // Remove from pendingWorkers to avoid double execution when finally fires.
1232
- const pendingIdx = this.pendingWorkers.indexOf(workerConfig.type);
1233
- if (pendingIdx !== -1)
1234
- this.pendingWorkers.splice(pendingIdx, 1);
1235
- return this.executeWorker(workerConfig);
1236
- }
1237
- /**
1238
- * Enable/disable a worker
1239
- */
1240
- setWorkerEnabled(type, enabled) {
1241
- const workerConfig = this.config.workers.find(w => w.type === type);
1242
- if (workerConfig) {
1243
- workerConfig.enabled = enabled;
1244
- if (enabled && this.running) {
1245
- this.scheduleWorker(workerConfig);
1246
- }
1247
- else if (!enabled) {
1248
- const timer = this.timers.get(type);
1249
- if (timer) {
1250
- clearTimeout(timer);
1251
- this.timers.delete(type);
1252
- }
1253
- }
1254
- this.saveState();
1255
- }
1256
- }
1257
- /**
1258
- * Save daemon state to file
1259
- */
1260
- saveState() {
1261
- const state = {
1262
- running: this.running,
1263
- startedAt: this.startedAt?.toISOString(),
1264
- workers: Object.fromEntries(Array.from(this.workers.entries()).map(([type, state]) => [
1265
- type,
1266
- {
1267
- ...state,
1268
- lastRun: state.lastRun?.toISOString(),
1269
- nextRun: state.nextRun?.toISOString(),
1270
- }
1271
- ])),
1272
- config: {
1273
- ...this.config,
1274
- workers: this.config.workers.map(w => ({ ...w })),
1275
- },
1276
- savedAt: new Date().toISOString(),
1277
- };
1278
- try {
1279
- const tmp = this.config.stateFile + '.tmp';
1280
- writeFileSync(tmp, JSON.stringify(state, null, 2));
1281
- renameSync(tmp, this.config.stateFile);
1282
- }
1283
- catch (error) {
1284
- this.log('error', `Failed to save state: ${error}`);
1285
- }
1286
- }
1287
- /**
1288
- * Log message
1289
- */
1290
- log(level, message) {
1291
- const timestamp = new Date().toISOString();
1292
- const logMessage = `[${timestamp}] [${level.toUpperCase()}] ${message}`;
1293
- this.emit('log', { level, message, timestamp });
1294
- // Also write to log file
1295
- try {
1296
- const logFile = join(this.config.logDir, 'daemon.log');
1297
- appendFileSync(logFile, logMessage + '\n');
1298
- // Opportunistic rotation: keep the log under 10 MB. When exceeded,
1299
- // discard the oldest half so recent entries are always retained.
1300
- const MAX_LOG_BYTES = 10 * 1024 * 1024;
1301
- if (statSync(logFile).size > MAX_LOG_BYTES) {
1302
- const content = readFileSync(logFile, 'utf-8');
1303
- const lines = content.split('\n').filter(Boolean);
1304
- const trimmed = lines.slice(Math.floor(lines.length / 2)).join('\n') + '\n';
1305
- const tmp = `${logFile}.${process.pid}.${Date.now()}.tmp`;
1306
- writeFileSync(tmp, trimmed);
1307
- renameSync(tmp, logFile);
1308
- }
1309
- }
1310
- catch {
1311
- // Ignore log write errors
1312
- }
1313
- }
1314
- }
1315
- // Singleton instance for global access
1316
- let daemonInstance = null;
1317
- /**
1318
- * Get or create daemon instance
1319
- */
1320
- export function getDaemon(projectRoot, config) {
1321
- if (!daemonInstance && projectRoot) {
1322
- daemonInstance = new WorkerDaemon(projectRoot, config);
1323
- }
1324
- if (!daemonInstance) {
1325
- throw new Error('Daemon not initialized. Provide projectRoot on first call.');
1326
- }
1327
- return daemonInstance;
1328
- }
1329
- /**
1330
- * Start daemon (for use in session-start hook)
1331
- */
1332
- export async function startDaemon(projectRoot, config) {
1333
- // A2: the daemon is a long-lived host — keep the SONA write-path enabled so
1334
- // trajectories accumulate across worker runs and reach the consolidation
1335
- // threshold (see startMCPServer for the one-shot-CLI rationale).
1336
- process.env.MONOMIND_PERSISTENT_HOST = '1';
1337
- const daemon = getDaemon(projectRoot, config);
1338
- await daemon.start();
1339
- return daemon;
1340
- }
1341
- /**
1342
- * Stop daemon
1343
- */
1344
- export async function stopDaemon() {
1345
- if (daemonInstance) {
1346
- await daemonInstance.stop();
1347
- }
1348
- }
1349
- export default WorkerDaemon;
1350
- //# sourceMappingURL=worker-daemon.js.map