cognitive-core 0.1.2 → 0.2.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 (483) hide show
  1. package/CLAUDE.md +233 -0
  2. package/README.md +370 -54
  3. package/dist/atlas.d.ts +10 -10
  4. package/dist/atlas.d.ts.map +1 -1
  5. package/dist/atlas.js +79 -48
  6. package/dist/atlas.js.map +1 -1
  7. package/dist/bin/cli-utils.d.ts +37 -0
  8. package/dist/bin/cli-utils.d.ts.map +1 -0
  9. package/dist/bin/cli-utils.js +176 -0
  10. package/dist/bin/cli-utils.js.map +1 -0
  11. package/dist/bin/cognitive-core.d.ts +2 -12
  12. package/dist/bin/cognitive-core.d.ts.map +1 -1
  13. package/dist/bin/cognitive-core.js +76 -351
  14. package/dist/bin/cognitive-core.js.map +1 -1
  15. package/dist/bin/commands/kb.d.ts +6 -0
  16. package/dist/bin/commands/kb.d.ts.map +1 -0
  17. package/dist/bin/commands/kb.js +240 -0
  18. package/dist/bin/commands/kb.js.map +1 -0
  19. package/dist/bin/commands/learn.d.ts +6 -0
  20. package/dist/bin/commands/learn.d.ts.map +1 -0
  21. package/dist/bin/commands/learn.js +91 -0
  22. package/dist/bin/commands/learn.js.map +1 -0
  23. package/dist/bin/commands/legacy.d.ts +12 -0
  24. package/dist/bin/commands/legacy.d.ts.map +1 -0
  25. package/dist/bin/commands/legacy.js +142 -0
  26. package/dist/bin/commands/legacy.js.map +1 -0
  27. package/dist/bin/commands/run.d.ts +3 -0
  28. package/dist/bin/commands/run.d.ts.map +1 -0
  29. package/dist/bin/commands/run.js +99 -0
  30. package/dist/bin/commands/run.js.map +1 -0
  31. package/dist/bin/commands/sessions.d.ts +9 -0
  32. package/dist/bin/commands/sessions.d.ts.map +1 -0
  33. package/dist/bin/commands/sessions.js +183 -0
  34. package/dist/bin/commands/sessions.js.map +1 -0
  35. package/dist/bin/commands/skills.d.ts +6 -0
  36. package/dist/bin/commands/skills.d.ts.map +1 -0
  37. package/dist/bin/commands/skills.js +135 -0
  38. package/dist/bin/commands/skills.js.map +1 -0
  39. package/dist/embeddings/index.d.ts +1 -0
  40. package/dist/embeddings/index.d.ts.map +1 -1
  41. package/dist/embeddings/index.js +2 -0
  42. package/dist/embeddings/index.js.map +1 -1
  43. package/dist/embeddings/inverted-index.d.ts +47 -0
  44. package/dist/embeddings/inverted-index.d.ts.map +1 -0
  45. package/dist/embeddings/inverted-index.js +122 -0
  46. package/dist/embeddings/inverted-index.js.map +1 -0
  47. package/dist/embeddings/manager.d.ts +3 -1
  48. package/dist/embeddings/manager.d.ts.map +1 -1
  49. package/dist/embeddings/manager.js +12 -7
  50. package/dist/embeddings/manager.js.map +1 -1
  51. package/dist/embeddings/vector-store.d.ts +7 -3
  52. package/dist/embeddings/vector-store.d.ts.map +1 -1
  53. package/dist/embeddings/vector-store.js +22 -6
  54. package/dist/embeddings/vector-store.js.map +1 -1
  55. package/dist/factory.d.ts +11 -12
  56. package/dist/factory.d.ts.map +1 -1
  57. package/dist/factory.js +20 -7
  58. package/dist/factory.js.map +1 -1
  59. package/dist/index.d.ts +7 -6
  60. package/dist/index.d.ts.map +1 -1
  61. package/dist/index.js +37 -5
  62. package/dist/index.js.map +1 -1
  63. package/dist/learning/analyzer.d.ts.map +1 -1
  64. package/dist/learning/analyzer.js +17 -35
  65. package/dist/learning/analyzer.js.map +1 -1
  66. package/dist/learning/energy-evaluator.d.ts +128 -0
  67. package/dist/learning/energy-evaluator.d.ts.map +1 -0
  68. package/dist/learning/energy-evaluator.js +175 -0
  69. package/dist/learning/energy-evaluator.js.map +1 -0
  70. package/dist/learning/healing-orchestrator.d.ts +182 -0
  71. package/dist/learning/healing-orchestrator.d.ts.map +1 -0
  72. package/dist/learning/healing-orchestrator.js +250 -0
  73. package/dist/learning/healing-orchestrator.js.map +1 -0
  74. package/dist/learning/index.d.ts +7 -2
  75. package/dist/learning/index.d.ts.map +1 -1
  76. package/dist/learning/index.js +13 -2
  77. package/dist/learning/index.js.map +1 -1
  78. package/dist/learning/instant-loop.d.ts +87 -0
  79. package/dist/learning/instant-loop.d.ts.map +1 -0
  80. package/dist/learning/instant-loop.js +264 -0
  81. package/dist/learning/instant-loop.js.map +1 -0
  82. package/dist/learning/loop-coordinator.d.ts +61 -0
  83. package/dist/learning/loop-coordinator.d.ts.map +1 -0
  84. package/dist/learning/loop-coordinator.js +96 -0
  85. package/dist/learning/loop-coordinator.js.map +1 -0
  86. package/dist/learning/maintenance-scheduler.d.ts +141 -0
  87. package/dist/learning/maintenance-scheduler.d.ts.map +1 -0
  88. package/dist/learning/maintenance-scheduler.js +186 -0
  89. package/dist/learning/maintenance-scheduler.js.map +1 -0
  90. package/dist/learning/meta-learner.d.ts.map +1 -1
  91. package/dist/learning/meta-learner.js +4 -21
  92. package/dist/learning/meta-learner.js.map +1 -1
  93. package/dist/learning/pipeline.d.ts +31 -4
  94. package/dist/learning/pipeline.d.ts.map +1 -1
  95. package/dist/learning/pipeline.js +64 -12
  96. package/dist/learning/pipeline.js.map +1 -1
  97. package/dist/learning/reflexion-generator.d.ts +64 -0
  98. package/dist/learning/reflexion-generator.d.ts.map +1 -0
  99. package/dist/learning/reflexion-generator.js +194 -0
  100. package/dist/learning/reflexion-generator.js.map +1 -0
  101. package/dist/learning/trajectory-sources/entire.d.ts +8 -5
  102. package/dist/learning/trajectory-sources/entire.d.ts.map +1 -1
  103. package/dist/learning/trajectory-sources/entire.js +13 -6
  104. package/dist/learning/trajectory-sources/entire.js.map +1 -1
  105. package/dist/learning/trajectory-sources/index.d.ts +1 -1
  106. package/dist/learning/trajectory-sources/index.d.ts.map +1 -1
  107. package/dist/learning/trajectory-sources/index.js +1 -1
  108. package/dist/learning/trajectory-sources/index.js.map +1 -1
  109. package/dist/learning/trajectory-sources/pipeline.d.ts +4 -4
  110. package/dist/learning/trajectory-sources/pipeline.d.ts.map +1 -1
  111. package/dist/learning/trajectory-sources/pipeline.js +2 -2
  112. package/dist/learning/trajectory-sources/pipeline.js.map +1 -1
  113. package/dist/learning/unified-pipeline.d.ts +281 -0
  114. package/dist/learning/unified-pipeline.d.ts.map +1 -0
  115. package/dist/learning/unified-pipeline.js +637 -0
  116. package/dist/learning/unified-pipeline.js.map +1 -0
  117. package/dist/memory/candidate-retrieval.d.ts +43 -0
  118. package/dist/memory/candidate-retrieval.d.ts.map +1 -0
  119. package/dist/memory/candidate-retrieval.js +41 -0
  120. package/dist/memory/candidate-retrieval.js.map +1 -0
  121. package/dist/memory/causal-store.d.ts +97 -0
  122. package/dist/memory/causal-store.d.ts.map +1 -0
  123. package/dist/memory/causal-store.js +209 -0
  124. package/dist/memory/causal-store.js.map +1 -0
  125. package/dist/memory/coherence.d.ts +71 -0
  126. package/dist/memory/coherence.d.ts.map +1 -0
  127. package/dist/memory/coherence.js +176 -0
  128. package/dist/memory/coherence.js.map +1 -0
  129. package/dist/memory/experience.d.ts +39 -6
  130. package/dist/memory/experience.d.ts.map +1 -1
  131. package/dist/memory/experience.js +193 -49
  132. package/dist/memory/experience.js.map +1 -1
  133. package/dist/memory/index.d.ts +7 -0
  134. package/dist/memory/index.d.ts.map +1 -1
  135. package/dist/memory/index.js +12 -0
  136. package/dist/memory/index.js.map +1 -1
  137. package/dist/memory/knowledge-bank.d.ts +14 -0
  138. package/dist/memory/knowledge-bank.d.ts.map +1 -1
  139. package/dist/memory/knowledge-bank.js +45 -0
  140. package/dist/memory/knowledge-bank.js.map +1 -1
  141. package/dist/memory/meta.d.ts +7 -8
  142. package/dist/memory/meta.d.ts.map +1 -1
  143. package/dist/memory/meta.js +73 -79
  144. package/dist/memory/meta.js.map +1 -1
  145. package/dist/memory/playbook.d.ts +26 -9
  146. package/dist/memory/playbook.d.ts.map +1 -1
  147. package/dist/memory/playbook.js +198 -74
  148. package/dist/memory/playbook.js.map +1 -1
  149. package/dist/memory/reasoning-bank.d.ts +130 -0
  150. package/dist/memory/reasoning-bank.d.ts.map +1 -0
  151. package/dist/memory/reasoning-bank.js +342 -0
  152. package/dist/memory/reasoning-bank.js.map +1 -0
  153. package/dist/memory/reflexion.d.ts +59 -0
  154. package/dist/memory/reflexion.d.ts.map +1 -0
  155. package/dist/memory/reflexion.js +96 -0
  156. package/dist/memory/reflexion.js.map +1 -0
  157. package/dist/memory/system.d.ts +7 -2
  158. package/dist/memory/system.d.ts.map +1 -1
  159. package/dist/memory/system.js +19 -7
  160. package/dist/memory/system.js.map +1 -1
  161. package/dist/memory/temporal-compressor.d.ts +126 -0
  162. package/dist/memory/temporal-compressor.d.ts.map +1 -0
  163. package/dist/memory/temporal-compressor.js +335 -0
  164. package/dist/memory/temporal-compressor.js.map +1 -0
  165. package/dist/persistence/index.d.ts +11 -0
  166. package/dist/persistence/index.d.ts.map +1 -0
  167. package/dist/persistence/index.js +11 -0
  168. package/dist/persistence/index.js.map +1 -0
  169. package/dist/persistence/migrator.d.ts +40 -0
  170. package/dist/persistence/migrator.d.ts.map +1 -0
  171. package/dist/persistence/migrator.js +238 -0
  172. package/dist/persistence/migrator.js.map +1 -0
  173. package/dist/persistence/serializers.d.ts +45 -0
  174. package/dist/persistence/serializers.d.ts.map +1 -0
  175. package/dist/persistence/serializers.js +80 -0
  176. package/dist/persistence/serializers.js.map +1 -0
  177. package/dist/persistence/sqlite-persistence.d.ts +228 -0
  178. package/dist/persistence/sqlite-persistence.d.ts.map +1 -0
  179. package/dist/persistence/sqlite-persistence.js +588 -0
  180. package/dist/persistence/sqlite-persistence.js.map +1 -0
  181. package/dist/runtime/flows/learning.d.ts +10 -12
  182. package/dist/runtime/flows/learning.d.ts.map +1 -1
  183. package/dist/runtime/flows/learning.js +10 -23
  184. package/dist/runtime/flows/learning.js.map +1 -1
  185. package/dist/search/index.d.ts +1 -0
  186. package/dist/search/index.d.ts.map +1 -1
  187. package/dist/search/index.js +2 -0
  188. package/dist/search/index.js.map +1 -1
  189. package/dist/search/moe-gate.d.ts +124 -0
  190. package/dist/search/moe-gate.d.ts.map +1 -0
  191. package/dist/search/moe-gate.js +234 -0
  192. package/dist/search/moe-gate.js.map +1 -0
  193. package/dist/search/router.d.ts +32 -2
  194. package/dist/search/router.d.ts.map +1 -1
  195. package/dist/search/router.js +87 -4
  196. package/dist/search/router.js.map +1 -1
  197. package/dist/session-bank/git-reader.d.ts +9 -4
  198. package/dist/session-bank/git-reader.d.ts.map +1 -1
  199. package/dist/session-bank/git-reader.js +22 -15
  200. package/dist/session-bank/git-reader.js.map +1 -1
  201. package/dist/session-bank/index.d.ts +2 -2
  202. package/dist/session-bank/index.d.ts.map +1 -1
  203. package/dist/session-bank/index.js +2 -2
  204. package/dist/session-bank/index.js.map +1 -1
  205. package/dist/session-bank/parser.d.ts +16 -5
  206. package/dist/session-bank/parser.d.ts.map +1 -1
  207. package/dist/session-bank/parser.js +187 -80
  208. package/dist/session-bank/parser.js.map +1 -1
  209. package/dist/session-bank/session-bank.d.ts +5 -0
  210. package/dist/session-bank/session-bank.d.ts.map +1 -1
  211. package/dist/session-bank/session-bank.js +30 -9
  212. package/dist/session-bank/session-bank.js.map +1 -1
  213. package/dist/session-bank/types.d.ts +4 -1
  214. package/dist/session-bank/types.d.ts.map +1 -1
  215. package/dist/session-bank/types.js +3 -3
  216. package/dist/session-bank/types.js.map +1 -1
  217. package/dist/surfacing/skill-publisher.d.ts.map +1 -1
  218. package/dist/surfacing/skill-publisher.js +15 -43
  219. package/dist/surfacing/skill-publisher.js.map +1 -1
  220. package/dist/surfacing/sqlite-storage-adapter.d.ts.map +1 -1
  221. package/dist/surfacing/sqlite-storage-adapter.js +13 -21
  222. package/dist/surfacing/sqlite-storage-adapter.js.map +1 -1
  223. package/dist/types/config.d.ts +100 -0
  224. package/dist/types/config.d.ts.map +1 -1
  225. package/dist/types/config.js +27 -0
  226. package/dist/types/config.js.map +1 -1
  227. package/dist/types/index.d.ts +2 -2
  228. package/dist/types/index.d.ts.map +1 -1
  229. package/dist/types/index.js +1 -1
  230. package/dist/types/index.js.map +1 -1
  231. package/dist/types/memory.d.ts +52 -0
  232. package/dist/types/memory.d.ts.map +1 -1
  233. package/dist/types/memory.js +13 -0
  234. package/dist/types/memory.js.map +1 -1
  235. package/dist/types/playbook.d.ts +4 -0
  236. package/dist/types/playbook.d.ts.map +1 -1
  237. package/dist/types/playbook.js.map +1 -1
  238. package/dist/utils/error-classifier.d.ts +30 -0
  239. package/dist/utils/error-classifier.d.ts.map +1 -0
  240. package/dist/utils/error-classifier.js +85 -0
  241. package/dist/utils/error-classifier.js.map +1 -0
  242. package/dist/utils/index.d.ts +3 -0
  243. package/dist/utils/index.d.ts.map +1 -1
  244. package/dist/utils/index.js +3 -0
  245. package/dist/utils/index.js.map +1 -1
  246. package/dist/utils/partitioned-store.d.ts +93 -0
  247. package/dist/utils/partitioned-store.d.ts.map +1 -0
  248. package/dist/utils/partitioned-store.js +251 -0
  249. package/dist/utils/partitioned-store.js.map +1 -0
  250. package/dist/utils/trajectory-helpers.d.ts +39 -0
  251. package/dist/utils/trajectory-helpers.d.ts.map +1 -0
  252. package/dist/utils/trajectory-helpers.js +57 -0
  253. package/dist/utils/trajectory-helpers.js.map +1 -0
  254. package/dist/workspace/runner.d.ts +3 -4
  255. package/dist/workspace/runner.d.ts.map +1 -1
  256. package/dist/workspace/runner.js.map +1 -1
  257. package/dist/workspace/types.d.ts +9 -2
  258. package/dist/workspace/types.d.ts.map +1 -1
  259. package/dist/workspace/types.js.map +1 -1
  260. package/package.json +6 -4
  261. package/references/sessionlog/.husky/pre-commit +1 -0
  262. package/references/sessionlog/.lintstagedrc.json +4 -0
  263. package/references/sessionlog/.prettierignore +4 -0
  264. package/references/sessionlog/.prettierrc.json +11 -0
  265. package/references/sessionlog/LICENSE +21 -0
  266. package/references/sessionlog/README.md +453 -0
  267. package/references/sessionlog/eslint.config.js +58 -0
  268. package/references/sessionlog/package-lock.json +3672 -0
  269. package/references/sessionlog/package.json +65 -0
  270. package/references/sessionlog/src/__tests__/agent-hooks.test.ts +570 -0
  271. package/references/sessionlog/src/__tests__/agent-registry.test.ts +127 -0
  272. package/references/sessionlog/src/__tests__/claude-code-hooks.test.ts +225 -0
  273. package/references/sessionlog/src/__tests__/claude-generator.test.ts +46 -0
  274. package/references/sessionlog/src/__tests__/commit-msg.test.ts +86 -0
  275. package/references/sessionlog/src/__tests__/cursor-agent.test.ts +224 -0
  276. package/references/sessionlog/src/__tests__/e2e-live.test.ts +890 -0
  277. package/references/sessionlog/src/__tests__/event-log.test.ts +183 -0
  278. package/references/sessionlog/src/__tests__/flush-sentinel.test.ts +105 -0
  279. package/references/sessionlog/src/__tests__/gemini-agent.test.ts +375 -0
  280. package/references/sessionlog/src/__tests__/git-hooks.test.ts +78 -0
  281. package/references/sessionlog/src/__tests__/hook-managers.test.ts +121 -0
  282. package/references/sessionlog/src/__tests__/lifecycle-tasks.test.ts +759 -0
  283. package/references/sessionlog/src/__tests__/opencode-agent.test.ts +338 -0
  284. package/references/sessionlog/src/__tests__/redaction.test.ts +136 -0
  285. package/references/sessionlog/src/__tests__/session-repo.test.ts +353 -0
  286. package/references/sessionlog/src/__tests__/session-store.test.ts +166 -0
  287. package/references/sessionlog/src/__tests__/setup-ccweb.test.ts +466 -0
  288. package/references/sessionlog/src/__tests__/skill-live.test.ts +461 -0
  289. package/references/sessionlog/src/__tests__/summarize.test.ts +348 -0
  290. package/references/sessionlog/src/__tests__/task-plan-e2e.test.ts +610 -0
  291. package/references/sessionlog/src/__tests__/task-plan-live.test.ts +632 -0
  292. package/references/sessionlog/src/__tests__/transcript-timestamp.test.ts +121 -0
  293. package/references/sessionlog/src/__tests__/types.test.ts +166 -0
  294. package/references/sessionlog/src/__tests__/utils.test.ts +333 -0
  295. package/references/sessionlog/src/__tests__/validation.test.ts +103 -0
  296. package/references/sessionlog/src/__tests__/worktree.test.ts +57 -0
  297. package/references/sessionlog/src/agent/agents/claude-code.ts +1089 -0
  298. package/references/sessionlog/src/agent/agents/cursor.ts +361 -0
  299. package/references/sessionlog/src/agent/agents/gemini-cli.ts +632 -0
  300. package/references/sessionlog/src/agent/agents/opencode.ts +540 -0
  301. package/references/sessionlog/src/agent/registry.ts +143 -0
  302. package/references/sessionlog/src/agent/session-types.ts +113 -0
  303. package/references/sessionlog/src/agent/types.ts +220 -0
  304. package/references/sessionlog/src/cli.ts +597 -0
  305. package/references/sessionlog/src/commands/clean.ts +133 -0
  306. package/references/sessionlog/src/commands/disable.ts +84 -0
  307. package/references/sessionlog/src/commands/doctor.ts +145 -0
  308. package/references/sessionlog/src/commands/enable.ts +202 -0
  309. package/references/sessionlog/src/commands/explain.ts +261 -0
  310. package/references/sessionlog/src/commands/reset.ts +105 -0
  311. package/references/sessionlog/src/commands/resume.ts +180 -0
  312. package/references/sessionlog/src/commands/rewind.ts +195 -0
  313. package/references/sessionlog/src/commands/setup-ccweb.ts +275 -0
  314. package/references/sessionlog/src/commands/status.ts +172 -0
  315. package/references/sessionlog/src/config.ts +165 -0
  316. package/references/sessionlog/src/events/event-log.ts +126 -0
  317. package/references/sessionlog/src/git-operations.ts +558 -0
  318. package/references/sessionlog/src/hooks/git-hooks.ts +165 -0
  319. package/references/sessionlog/src/hooks/lifecycle.ts +391 -0
  320. package/references/sessionlog/src/index.ts +650 -0
  321. package/references/sessionlog/src/security/redaction.ts +283 -0
  322. package/references/sessionlog/src/session/state-machine.ts +452 -0
  323. package/references/sessionlog/src/store/checkpoint-store.ts +509 -0
  324. package/references/sessionlog/src/store/native-store.ts +173 -0
  325. package/references/sessionlog/src/store/provider-types.ts +99 -0
  326. package/references/sessionlog/src/store/session-store.ts +266 -0
  327. package/references/sessionlog/src/strategy/attribution.ts +296 -0
  328. package/references/sessionlog/src/strategy/common.ts +207 -0
  329. package/references/sessionlog/src/strategy/content-overlap.ts +228 -0
  330. package/references/sessionlog/src/strategy/manual-commit.ts +988 -0
  331. package/references/sessionlog/src/strategy/types.ts +279 -0
  332. package/references/sessionlog/src/summarize/claude-generator.ts +115 -0
  333. package/references/sessionlog/src/summarize/summarize.ts +432 -0
  334. package/references/sessionlog/src/types.ts +508 -0
  335. package/references/sessionlog/src/utils/chunk-files.ts +49 -0
  336. package/references/sessionlog/src/utils/commit-message.ts +65 -0
  337. package/references/sessionlog/src/utils/detect-agent.ts +36 -0
  338. package/references/sessionlog/src/utils/hook-managers.ts +125 -0
  339. package/references/sessionlog/src/utils/ide-tags.ts +32 -0
  340. package/references/sessionlog/src/utils/paths.ts +79 -0
  341. package/references/sessionlog/src/utils/preview-rewind.ts +80 -0
  342. package/references/sessionlog/src/utils/rewind-conflict.ts +121 -0
  343. package/references/sessionlog/src/utils/shadow-branch.ts +109 -0
  344. package/references/sessionlog/src/utils/string-utils.ts +46 -0
  345. package/references/sessionlog/src/utils/todo-extract.ts +188 -0
  346. package/references/sessionlog/src/utils/trailers.ts +187 -0
  347. package/references/sessionlog/src/utils/transcript-parse.ts +177 -0
  348. package/references/sessionlog/src/utils/transcript-timestamp.ts +59 -0
  349. package/references/sessionlog/src/utils/tree-ops.ts +219 -0
  350. package/references/sessionlog/src/utils/tty.ts +72 -0
  351. package/references/sessionlog/src/utils/validation.ts +65 -0
  352. package/references/sessionlog/src/utils/worktree.ts +58 -0
  353. package/references/sessionlog/src/wire-types.ts +59 -0
  354. package/references/sessionlog/templates/setup-env.sh +153 -0
  355. package/references/sessionlog/tsconfig.json +18 -0
  356. package/references/sessionlog/vitest.config.ts +12 -0
  357. package/references/skill-tree/.sudocode/issues.jsonl +8 -0
  358. package/references/skill-tree/.sudocode/specs.jsonl +2 -0
  359. package/references/skill-tree/CLAUDE.md +56 -80
  360. package/references/skill-tree/README.md +188 -140
  361. package/references/skill-tree/examples/basic-usage.ts +95 -121
  362. package/references/skill-tree/package-lock.json +369 -26
  363. package/references/skill-tree/package.json +1 -1
  364. package/src/atlas.ts +97 -67
  365. package/src/bin/cli-utils.ts +220 -0
  366. package/src/bin/cognitive-core.ts +84 -392
  367. package/src/bin/commands/kb.ts +266 -0
  368. package/src/bin/commands/learn.ts +100 -0
  369. package/src/bin/commands/legacy.ts +182 -0
  370. package/src/bin/commands/run.ts +113 -0
  371. package/src/bin/commands/sessions.ts +221 -0
  372. package/src/bin/commands/skills.ts +146 -0
  373. package/src/embeddings/index.ts +3 -0
  374. package/src/embeddings/inverted-index.ts +134 -0
  375. package/src/embeddings/manager.ts +13 -8
  376. package/src/embeddings/vector-store.ts +21 -9
  377. package/src/factory.ts +33 -16
  378. package/src/index.ts +109 -9
  379. package/src/learning/analyzer.ts +21 -37
  380. package/src/learning/energy-evaluator.ts +282 -0
  381. package/src/learning/healing-orchestrator.ts +383 -0
  382. package/src/learning/index.ts +65 -9
  383. package/src/learning/instant-loop.ts +357 -0
  384. package/src/learning/maintenance-scheduler.ts +271 -0
  385. package/src/learning/meta-learner.ts +5 -23
  386. package/src/learning/reflexion-generator.ts +273 -0
  387. package/src/learning/trajectory-sources/entire.ts +24 -13
  388. package/src/learning/trajectory-sources/index.ts +2 -2
  389. package/src/learning/trajectory-sources/pipeline.ts +5 -5
  390. package/src/learning/unified-pipeline.ts +921 -0
  391. package/src/memory/candidate-retrieval.ts +71 -0
  392. package/src/memory/causal-store.ts +273 -0
  393. package/src/memory/coherence.ts +252 -0
  394. package/src/memory/experience.ts +217 -50
  395. package/src/memory/index.ts +43 -0
  396. package/src/memory/knowledge-bank.ts +57 -0
  397. package/src/memory/meta.ts +78 -96
  398. package/src/memory/playbook.ts +239 -75
  399. package/src/memory/reasoning-bank.ts +458 -0
  400. package/src/memory/reflexion.ts +122 -0
  401. package/src/memory/system.ts +21 -5
  402. package/src/memory/temporal-compressor.ts +409 -0
  403. package/src/persistence/index.ts +37 -0
  404. package/src/persistence/migrator.ts +298 -0
  405. package/src/persistence/serializers.ts +79 -0
  406. package/src/persistence/sqlite-persistence.ts +925 -0
  407. package/src/runtime/flows/learning.ts +25 -42
  408. package/src/search/index.ts +10 -0
  409. package/src/search/moe-gate.ts +304 -0
  410. package/src/search/router.ts +111 -4
  411. package/src/session-bank/git-reader.ts +29 -19
  412. package/src/session-bank/index.ts +4 -2
  413. package/src/session-bank/parser.ts +280 -98
  414. package/src/session-bank/session-bank.ts +33 -12
  415. package/src/session-bank/types.ts +8 -5
  416. package/src/surfacing/skill-publisher.ts +17 -49
  417. package/src/surfacing/sqlite-storage-adapter.ts +16 -32
  418. package/src/types/config.ts +30 -0
  419. package/src/types/index.ts +3 -0
  420. package/src/types/memory.ts +30 -0
  421. package/src/types/playbook.ts +4 -0
  422. package/src/utils/error-classifier.ts +113 -0
  423. package/src/utils/index.ts +18 -0
  424. package/src/utils/partitioned-store.ts +299 -0
  425. package/src/utils/trajectory-helpers.ts +79 -0
  426. package/src/workspace/runner.ts +3 -3
  427. package/src/workspace/types.ts +10 -2
  428. package/tests/embeddings/inverted-index.test.ts +138 -0
  429. package/tests/feature-toggles.test.ts +275 -0
  430. package/tests/gap-fixes.test.ts +17 -4
  431. package/tests/integration/cli-e2e.test.ts +621 -0
  432. package/tests/integration/e2e.test.ts +6 -5
  433. package/tests/integration/entire-e2e.test.ts +314 -125
  434. package/tests/integration/persistence-e2e.test.ts +741 -0
  435. package/tests/integration/phase-e2e.test.ts +1143 -0
  436. package/tests/integration/session-bank.test.ts +20 -14
  437. package/tests/integration/sessionlog-e2e.test.ts +329 -0
  438. package/tests/integration/unified-pipeline-e2e.test.ts +634 -0
  439. package/tests/learning/analyzer.test.ts +1 -1
  440. package/tests/learning/energy-evaluator.test.ts +180 -0
  441. package/tests/learning/entire-trajectory-source.test.ts +25 -25
  442. package/tests/learning/healing-orchestrator.test.ts +269 -0
  443. package/tests/learning/instant-loop.test.ts +243 -0
  444. package/tests/learning/maintenance-scheduler.test.ts +191 -0
  445. package/tests/learning/reflexion-generator.test.ts +411 -0
  446. package/tests/learning/trajectory-sources.test.ts +12 -4
  447. package/tests/learning/unified-pipeline.test.ts +322 -0
  448. package/tests/mcp/playbook-server.test.ts +6 -1
  449. package/tests/memory/causal-store.test.ts +276 -0
  450. package/tests/memory/coherence.test.ts +232 -0
  451. package/tests/memory/experience.test.ts +8 -3
  452. package/tests/memory/playbook.test.ts +307 -1
  453. package/tests/memory/provenance.test.ts +11 -2
  454. package/tests/memory/reasoning-bank.test.ts +239 -0
  455. package/tests/memory/reflexion.test.ts +166 -0
  456. package/tests/memory/skill-exporter.test.ts +6 -1
  457. package/tests/memory/system.test.ts +6 -1
  458. package/tests/memory/temporal-compressor.test.ts +318 -0
  459. package/tests/persistence/migrator.test.ts +1009 -0
  460. package/tests/persistence/sqlite-persistence.test.ts +635 -0
  461. package/tests/runtime/agent-manager.test.ts +6 -1
  462. package/tests/runtime/delegate.test.ts +6 -1
  463. package/tests/search/moe-gate.test.ts +250 -0
  464. package/tests/search/refinement-loop.test.ts +11 -2
  465. package/tests/search/router.test.ts +81 -2
  466. package/tests/session-bank/fixtures/sessionlog-root-metadata.json +16 -0
  467. package/tests/session-bank/fixtures/sessionlog-session/full.jsonl +6 -0
  468. package/tests/session-bank/fixtures/sessionlog-session/metadata.json +55 -0
  469. package/tests/session-bank/git-reader.test.ts +13 -13
  470. package/tests/session-bank/parser.test.ts +135 -3
  471. package/tests/session-bank/session-bank.test.ts +1 -1
  472. package/tests/surfacing/skill-library.test.ts +6 -1
  473. package/tests/surfacing/skill-publisher.test.ts +24 -58
  474. package/tests/surfacing/sqlite-storage-adapter.test.ts +11 -23
  475. package/tests/utils/partitioned-store.test.ts +230 -0
  476. package/tests/workspace/full-flow.test.ts +10 -4
  477. package/tests/workspace/runner.test.ts +10 -4
  478. package/docs/DESIGN-workspace-migration.md +0 -1079
  479. package/docs/PLAN-agentic-workspace-implementation.md +0 -717
  480. package/docs/PLAN-graph-migration.md +0 -299
  481. package/docs/PLAN-session-bank-implementation.md +0 -474
  482. package/src/learning/pipeline.ts +0 -323
  483. package/tests/learning/pipeline.test.ts +0 -176
package/src/atlas.ts CHANGED
@@ -10,11 +10,11 @@ import type { KnowledgeMatch } from './types/knowledge.js';
10
10
  import { createDefaultConfig } from './types/index.js';
11
11
  import { MemorySystem, createMemorySystem, type TeamExperience } from './memory/index.js';
12
12
  import {
13
- LearningPipeline,
14
- createLearningPipeline,
15
- type ProcessResult,
16
- type BatchResult,
17
- } from './learning/index.js';
13
+ UnifiedLearningPipeline,
14
+ createUnifiedPipeline,
15
+ type ImmediateResult,
16
+ type UnifiedBatchResult,
17
+ } from './learning/unified-pipeline.js';
18
18
  import {
19
19
  TeamLearningPipeline,
20
20
  createTeamLearningPipeline,
@@ -40,14 +40,8 @@ import {
40
40
  PlaybookUsageInference,
41
41
  createUsageInference,
42
42
  } from './learning/usage-inference.js';
43
- import {
44
- MetaLearner,
45
- createMetaLearner,
46
- } from './learning/meta-learner.js';
47
- import {
48
- LearningEffectivenessTracker,
49
- createEffectivenessTracker,
50
- } from './learning/effectiveness.js';
43
+ import type { MetaLearner } from './learning/meta-learner.js';
44
+ import type { LearningEffectivenessTracker } from './learning/effectiveness.js';
51
45
  import { TaskRouter, createRouter, type RoutingDecision } from './search/router.js';
52
46
  import { DirectSolver, createSolver } from './search/solver.js';
53
47
  import {
@@ -64,6 +58,7 @@ import {
64
58
  } from './surfacing/skill-library.js';
65
59
  import { SqliteStorageAdapter } from './surfacing/sqlite-storage-adapter.js';
66
60
  import { join } from 'node:path';
61
+ import { SqlitePersistence, createSqlitePersistence } from './persistence/index.js';
67
62
  import {
68
63
  PlaybookMCPServer,
69
64
  createPlaybookMCPServer,
@@ -160,7 +155,7 @@ export interface SolveResult {
160
155
  */
161
156
  export class Atlas {
162
157
  private memory: MemorySystem;
163
- private learning: LearningPipeline;
158
+ private unifiedPipeline: UnifiedLearningPipeline;
164
159
  private router: TaskRouter;
165
160
  private solver: DirectSolver | null = null;
166
161
  private agentManager: AgentManager | null = null;
@@ -176,10 +171,6 @@ export class Atlas {
176
171
  private usageInference: PlaybookUsageInference | null = null;
177
172
  private mcpServer: PlaybookMCPServer | null = null;
178
173
 
179
- // Meta-learning and effectiveness
180
- private metaLearner: MetaLearner | null = null;
181
- private effectivenessTracker: LearningEffectivenessTracker | null = null;
182
-
183
174
  // Agentic workspace components
184
175
  private taskRunner: AgenticTaskRunner | null = null;
185
176
  private workspaceManager: WorkspaceManager | null = null;
@@ -190,21 +181,50 @@ export class Atlas {
190
181
  private teamMetaLearner: TeamMetaLearner | null = null;
191
182
  private teamSkillLibrary: TeamSkillLibrary | null = null;
192
183
 
184
+ // Unified SQLite persistence
185
+ private persistence: SqlitePersistence;
186
+
193
187
  constructor(config?: Partial<AtlasConfig>) {
194
188
  this.config = createDefaultConfig(config);
195
189
 
190
+ // Create unified SQLite persistence
191
+ this.persistence = createSqlitePersistence({
192
+ baseDir: this.config.storage.baseDir,
193
+ dbName: this.config.storage.dbName,
194
+ });
195
+
196
196
  // Initialize memory system (with optional knowledge bank)
197
197
  this.memory = createMemorySystem(
198
+ this.persistence,
198
199
  this.config.storage.baseDir,
199
200
  this.config.memory,
200
- this.config.knowledgeBank.enabled ? this.config.knowledgeBank : undefined
201
+ this.config.knowledgeBank.enabled
202
+ ? { ...this.config.knowledgeBank, coherenceChecking: this.config.features.coherenceChecking }
203
+ : undefined
201
204
  );
202
205
 
203
- // Initialize learning pipeline
204
- this.learning = createLearningPipeline(this.memory, this.config.learning);
206
+ // Initialize unified learning pipeline (replaces LearningPipeline + InstantLoop + LoopCoordinator)
207
+ this.unifiedPipeline = createUnifiedPipeline(this.memory, {
208
+ instant: {
209
+ enableReflexion: this.config.features.reflexion,
210
+ enableCausalExtraction: this.config.features.causalExtraction,
211
+ },
212
+ batch: {
213
+ learning: this.config.learning,
214
+ energy: {
215
+ countThreshold: this.config.learning.minTrajectories ?? 10,
216
+ },
217
+ },
218
+ features: {
219
+ temporalCompression: this.config.features.temporalCompression,
220
+ reasoningBank: this.config.features.reasoningBank,
221
+ reflexion: this.config.features.reflexion,
222
+ causalExtraction: this.config.features.causalExtraction,
223
+ },
224
+ }, this.persistence);
205
225
 
206
- // Initialize router
207
- this.router = createRouter(this.memory, this.config.router);
226
+ // Initialize router (with persistence for MoEGate)
227
+ this.router = createRouter(this.memory, this.config.router, undefined, this.persistence);
208
228
 
209
229
  // Initialize team learning pipeline and router if enabled
210
230
  if (this.config.teamLearning.enabled) {
@@ -232,8 +252,23 @@ export class Atlas {
232
252
  * Initialize Atlas (load persisted data)
233
253
  */
234
254
  async init(): Promise<void> {
255
+ await this.persistence.init();
256
+
257
+ // Load persisted state for components created before persistence.init()
258
+ this.router.loadPersistedState();
259
+ this.unifiedPipeline.loadPersistedState();
260
+
235
261
  await this.memory.init();
236
262
 
263
+ // Initialize unified pipeline async components (meta-learner, effectiveness tracker, energy seed)
264
+ await this.unifiedPipeline.init();
265
+
266
+ // Connect meta-learner to router
267
+ const metaLearner = this.unifiedPipeline.getMetaLearner();
268
+ if (metaLearner) {
269
+ this.router.setMetaLearner(metaLearner);
270
+ }
271
+
237
272
  // Initialize playbook-based components if playbooks are available
238
273
  if (this.memory.playbooks) {
239
274
  this.skillLibrary = createSkillLibrary(
@@ -253,18 +288,6 @@ export class Atlas {
253
288
  // Refresh skill tiers on init
254
289
  await this.skillLibrary.refreshTiers();
255
290
  }
256
-
257
- // Initialize meta-learner and connect to router
258
- if (this.memory.meta) {
259
- this.metaLearner = createMetaLearner(this.memory.meta);
260
- this.router.setMetaLearner(this.metaLearner);
261
- }
262
-
263
- // Initialize effectiveness tracker
264
- this.effectivenessTracker = createEffectivenessTracker(
265
- this.config.storage.baseDir
266
- );
267
- await this.effectivenessTracker.init();
268
291
  }
269
292
 
270
293
  /**
@@ -303,12 +326,12 @@ export class Atlas {
303
326
  this.workspaceManager,
304
327
  this.agentManager,
305
328
  this.memory,
306
- this.learning,
329
+ this.unifiedPipeline,
307
330
  this.config.agenticTasks
308
331
  );
309
332
 
310
- // Connect task runner to learning pipeline for workspace-based analysis
311
- this.learning.setTaskRunner(this.taskRunner);
333
+ // Connect task runner to unified pipeline for workspace-based analysis
334
+ this.unifiedPipeline.setTaskRunner(this.taskRunner);
312
335
 
313
336
  // Connect task runner to team learning pipeline
314
337
  if (this.teamLearning) {
@@ -481,12 +504,14 @@ export class Atlas {
481
504
  }
482
505
 
483
506
  // Generate meta-reflection (records observation + periodically generates strategies)
484
- if (this.metaLearner) {
507
+ const metaLearner = this.unifiedPipeline.getMetaLearner();
508
+ if (metaLearner) {
485
509
  await this.reflectOnExecution(result.trajectory, routing);
486
510
  }
487
511
 
488
512
  // Annotate trajectory for effectiveness tracking
489
- if (this.effectivenessTracker) {
513
+ const effectivenessTracker = this.unifiedPipeline.getEffectivenessTracker();
514
+ if (effectivenessTracker) {
490
515
  const appliedPlaybookIds = (result.playbookUsage ?? [])
491
516
  .filter(u => u.wasUsed)
492
517
  .map(u => u.playbookId);
@@ -495,7 +520,7 @@ export class Atlas {
495
520
  e => e.experience.id
496
521
  );
497
522
 
498
- await this.effectivenessTracker.annotate(
523
+ await effectivenessTracker.annotate(
499
524
  result.trajectory,
500
525
  routing.memoryContext.playbooks,
501
526
  surfacedExperienceIds,
@@ -503,15 +528,10 @@ export class Atlas {
503
528
  );
504
529
  }
505
530
 
506
- // Process the trajectory for learning
507
- await this.learning.processTrajectory(result.trajectory);
531
+ // Process the trajectory through unified pipeline (immediate + batch if energy triggers)
532
+ await this.unifiedPipeline.processTrajectory(result.trajectory);
508
533
  this.trajectoriesProcessed++;
509
534
 
510
- // Run batch learning if ready
511
- if (this.learning.shouldRunBatch()) {
512
- await this.runBatchLearning();
513
- }
514
-
515
535
  return result;
516
536
  }
517
537
 
@@ -588,10 +608,12 @@ export class Atlas {
588
608
  }
589
609
 
590
610
  /**
591
- * Process an external trajectory (from another agent)
611
+ * Process an external trajectory (from another agent).
612
+ * Runs the instant loop first (hot path), then feeds into the batch pipeline.
592
613
  */
593
- async processTrajectory(trajectory: Trajectory): Promise<ProcessResult> {
594
- const result = await this.learning.processTrajectory(trajectory);
614
+ async processTrajectory(trajectory: Trajectory): Promise<ImmediateResult> {
615
+ // Unified pipeline handles immediate + batch (if energy triggers)
616
+ const result = await this.unifiedPipeline.processTrajectory(trajectory);
595
617
  this.trajectoriesProcessed++;
596
618
  return result;
597
619
  }
@@ -763,8 +785,8 @@ export class Atlas {
763
785
  /**
764
786
  * Run batch learning explicitly
765
787
  */
766
- async runBatchLearning(): Promise<BatchResult> {
767
- return this.learning.runBatchLearning();
788
+ async runBatchLearning(): Promise<UnifiedBatchResult> {
789
+ return this.unifiedPipeline.runBatch();
768
790
  }
769
791
 
770
792
  /**
@@ -790,7 +812,7 @@ export class Atlas {
790
812
  memory: memoryStats,
791
813
  learning: {
792
814
  trajectoriesProcessed: this.trajectoriesProcessed,
793
- pendingTrajectories: this.learning.getAccumulatedCount(),
815
+ pendingTrajectories: this.unifiedPipeline.getAccumulatedCount(),
794
816
  teamTrajectoriesProcessed: this.teamTrajectoriesProcessed,
795
817
  },
796
818
  };
@@ -845,8 +867,8 @@ export class Atlas {
845
867
  /**
846
868
  * Get the learning pipeline for direct access
847
869
  */
848
- getLearning(): LearningPipeline {
849
- return this.learning;
870
+ getLearning(): UnifiedLearningPipeline {
871
+ return this.unifiedPipeline;
850
872
  }
851
873
 
852
874
  /**
@@ -903,7 +925,7 @@ export class Atlas {
903
925
  */
904
926
  updateConfig(config: Partial<AtlasConfig>): void {
905
927
  if (config.learning) {
906
- this.learning.updateConfig(config.learning);
928
+ this.unifiedPipeline.updateConfig(config.learning);
907
929
  }
908
930
  if (config.router) {
909
931
  this.router.updateConfig(config.router);
@@ -949,7 +971,8 @@ export class Atlas {
949
971
  trajectory: Trajectory,
950
972
  routing: RoutingDecision,
951
973
  ): Promise<void> {
952
- if (!this.metaLearner) return;
974
+ const metaLearnerRef = this.unifiedPipeline.getMetaLearner();
975
+ if (!metaLearnerRef) return;
953
976
 
954
977
  const routingInfo = {
955
978
  strategy: routing.strategy,
@@ -970,7 +993,7 @@ export class Atlas {
970
993
  // Bridge template output into MetaLearner storage
971
994
  const playbookIds = playbookMatches.map(p => p.playbook.id);
972
995
  const actualEffort = trajectory.llmCalls ?? trajectory.steps.length;
973
- await this.metaLearner.recordFromReflection(
996
+ await metaLearnerRef.recordFromReflection(
974
997
  result.output,
975
998
  trajectory.id,
976
999
  routingInfo,
@@ -987,7 +1010,7 @@ export class Atlas {
987
1010
  }
988
1011
 
989
1012
  // Heuristic fallback
990
- await this.metaLearner.generateReflection(
1013
+ await metaLearnerRef.generateReflection(
991
1014
  trajectory,
992
1015
  routingInfo,
993
1016
  playbookMatches,
@@ -998,23 +1021,23 @@ export class Atlas {
998
1021
  * Get the meta learner for direct access
999
1022
  */
1000
1023
  getMetaLearner(): MetaLearner | null {
1001
- return this.metaLearner;
1024
+ return this.unifiedPipeline.getMetaLearner();
1002
1025
  }
1003
1026
 
1004
1027
  /**
1005
1028
  * Get the effectiveness tracker for direct access
1006
1029
  */
1007
1030
  getEffectivenessTracker(): LearningEffectivenessTracker | null {
1008
- return this.effectivenessTracker;
1031
+ return this.unifiedPipeline.getEffectivenessTracker();
1009
1032
  }
1010
1033
 
1011
1034
  async close(): Promise<void> {
1012
1035
  // Stop MCP server if running
1013
1036
  await this.stopMCPServer();
1014
1037
 
1015
- // Flush learning pipeline
1016
- if (this.learning.getAccumulatedCount() > 0) {
1017
- await this.learning.runBatchLearning();
1038
+ // Flush unified pipeline
1039
+ if (this.unifiedPipeline.getAccumulatedCount() > 0) {
1040
+ await this.unifiedPipeline.runBatch();
1018
1041
  }
1019
1042
 
1020
1043
  // Flush team learning pipeline
@@ -1022,9 +1045,13 @@ export class Atlas {
1022
1045
  await this.teamLearning.runBatchLearning();
1023
1046
  }
1024
1047
 
1048
+ // Flush MoE gate state before closing persistence
1049
+ this.router.persistGateState();
1050
+
1025
1051
  // Close effectiveness tracker
1026
- if (this.effectivenessTracker) {
1027
- await this.effectivenessTracker.close();
1052
+ const tracker = this.unifiedPipeline.getEffectivenessTracker();
1053
+ if (tracker) {
1054
+ await tracker.close();
1028
1055
  }
1029
1056
 
1030
1057
  // Close skill-tree storage
@@ -1034,6 +1061,9 @@ export class Atlas {
1034
1061
 
1035
1062
  // Close memory system
1036
1063
  await this.memory.close();
1064
+
1065
+ // Close SQLite persistence
1066
+ this.persistence.close();
1037
1067
  }
1038
1068
  }
1039
1069
 
@@ -0,0 +1,220 @@
1
+ /**
2
+ * CLI shared utilities — types, arg parser, Atlas lifecycle, output helpers.
3
+ */
4
+
5
+ import { existsSync } from 'node:fs';
6
+ import * as path from 'node:path';
7
+ import { Atlas, createAtlas } from '../atlas.js';
8
+ import type { KnowledgeBank } from '../memory/knowledge-bank.js';
9
+ import type { SkillLibrary } from '../surfacing/skill-library.js';
10
+ import type { Playbook, AtlasConfig } from '../types/index.js';
11
+
12
+ // ─── Types ───────────────────────────────────────────────────────────
13
+
14
+ export interface CLIOptions {
15
+ dir?: string;
16
+ domain?: string;
17
+ limit?: number;
18
+ format?: 'json' | 'text';
19
+ type?: string;
20
+ tier?: string;
21
+ agent?: string;
22
+ since?: string;
23
+ before?: string;
24
+ branch?: string;
25
+ interval?: number;
26
+ repo?: string;
27
+ once?: boolean;
28
+ }
29
+
30
+ export interface ParsedArgs {
31
+ group: string;
32
+ command: string;
33
+ positional: string[];
34
+ options: CLIOptions;
35
+ }
36
+
37
+ const COMMAND_GROUPS = new Set(['kb', 'sessions', 'learn', 'skills', 'run']);
38
+
39
+ // ─── Arg Parser ──────────────────────────────────────────────────────
40
+
41
+ export function parseArgs(args: string[]): ParsedArgs {
42
+ const options: CLIOptions = {};
43
+ const positional: string[] = [];
44
+
45
+ for (let i = 0; i < args.length; i++) {
46
+ const arg = args[i];
47
+
48
+ if (arg === '--dir' && args[i + 1]) {
49
+ options.dir = args[++i];
50
+ } else if (arg === '--domain' && args[i + 1]) {
51
+ options.domain = args[++i];
52
+ } else if (arg === '--limit' && args[i + 1]) {
53
+ options.limit = parseInt(args[++i], 10);
54
+ } else if (arg === '--format' && args[i + 1]) {
55
+ options.format = args[++i] as 'json' | 'text';
56
+ } else if (arg === '--json') {
57
+ options.format = 'json';
58
+ } else if (arg === '--type' && args[i + 1]) {
59
+ options.type = args[++i];
60
+ } else if (arg === '--tier' && args[i + 1]) {
61
+ options.tier = args[++i];
62
+ } else if (arg === '--agent' && args[i + 1]) {
63
+ options.agent = args[++i];
64
+ } else if (arg === '--since' && args[i + 1]) {
65
+ options.since = args[++i];
66
+ } else if (arg === '--before' && args[i + 1]) {
67
+ options.before = args[++i];
68
+ } else if (arg === '--branch' && args[i + 1]) {
69
+ options.branch = args[++i];
70
+ } else if (arg === '--interval' && args[i + 1]) {
71
+ options.interval = parseInt(args[++i], 10);
72
+ } else if (arg === '--repo' && args[i + 1]) {
73
+ options.repo = args[++i];
74
+ } else if (arg === '--once') {
75
+ options.once = true;
76
+ } else if (!arg.startsWith('-')) {
77
+ positional.push(arg);
78
+ }
79
+ }
80
+
81
+ // Two-level routing: first positional may be a command group
82
+ const first = positional[0] ?? '';
83
+
84
+ if (COMMAND_GROUPS.has(first)) {
85
+ return {
86
+ group: first,
87
+ command: positional[1] ?? '',
88
+ positional: positional.slice(2),
89
+ options,
90
+ };
91
+ }
92
+
93
+ // Legacy commands — group is empty, first positional is the command
94
+ return {
95
+ group: '',
96
+ command: first,
97
+ positional: positional.slice(1),
98
+ options,
99
+ };
100
+ }
101
+
102
+ // ─── Storage Dir ─────────────────────────────────────────────────────
103
+
104
+ export function resolveStorageDir(): string {
105
+ if (process.env.COGNITIVE_CORE_HOME) return process.env.COGNITIVE_CORE_HOME;
106
+ const swarmDir = path.join(process.cwd(), '.swarm', 'cognitive-core');
107
+ if (existsSync(swarmDir)) return swarmDir;
108
+ return path.join(process.cwd(), '.cognitive-core');
109
+ }
110
+
111
+ // ─── Atlas Lifecycle ─────────────────────────────────────────────────
112
+
113
+ export async function withAtlas<T>(
114
+ options: CLIOptions,
115
+ fn: (atlas: Atlas) => Promise<T>,
116
+ ): Promise<T> {
117
+ const atlas = createAtlas({
118
+ storage: { baseDir: options.dir || resolveStorageDir() },
119
+ knowledgeBank: { enabled: true },
120
+ } as Partial<AtlasConfig>);
121
+ try {
122
+ await atlas.init();
123
+ return await fn(atlas);
124
+ } finally {
125
+ await atlas.close();
126
+ }
127
+ }
128
+
129
+ // ─── Subsystem Guards ────────────────────────────────────────────────
130
+
131
+ export function requireKnowledgeBank(atlas: Atlas): KnowledgeBank {
132
+ const kb = atlas.getKnowledgeBank();
133
+ if (!kb) {
134
+ throw new Error(
135
+ 'Knowledge bank is not enabled. Ensure knowledgeBank.enabled is true in config.',
136
+ );
137
+ }
138
+ return kb;
139
+ }
140
+
141
+ export function requireSkillLibrary(atlas: Atlas): SkillLibrary {
142
+ const lib = atlas.getSkillLibrary();
143
+ if (!lib) {
144
+ throw new Error(
145
+ 'Skill library is not available. Initialize Atlas first.',
146
+ );
147
+ }
148
+ return lib;
149
+ }
150
+
151
+ // ─── Output Helpers ──────────────────────────────────────────────────
152
+
153
+ export function outputJson(data: unknown): void {
154
+ console.log(JSON.stringify(data, null, 2));
155
+ }
156
+
157
+ export function outputError(error: unknown, options: CLIOptions): void {
158
+ const message = error instanceof Error ? error.message : String(error);
159
+ if (options.format === 'json') {
160
+ console.log(JSON.stringify({ error: message }));
161
+ } else {
162
+ console.error(`Error: ${message}`);
163
+ }
164
+ }
165
+
166
+ // ─── Playbook Formatting ─────────────────────────────────────────────
167
+
168
+ export function formatPlaybook(playbook: Playbook, verbose = false): string {
169
+ const lines: string[] = [];
170
+ const { successCount, failureCount } = playbook.evolution;
171
+ const totalUses = successCount + failureCount;
172
+ const successRate = totalUses > 0 ? Math.round((successCount / totalUses) * 100) : 0;
173
+
174
+ lines.push(`## ${playbook.name}`);
175
+ lines.push(`ID: ${playbook.id}`);
176
+ lines.push(`Confidence: ${Math.round(playbook.confidence * 100)}%`);
177
+ lines.push(`Success Rate: ${successRate}% (${totalUses} uses)`);
178
+ lines.push(`Domains: ${playbook.applicability.domains.join(', ')}`);
179
+ lines.push('');
180
+ lines.push(`**Strategy**: ${playbook.guidance.strategy}`);
181
+
182
+ if (playbook.guidance.tactics.length > 0) {
183
+ lines.push('**Tactics**:');
184
+ for (const tactic of playbook.guidance.tactics) {
185
+ lines.push(` - ${tactic}`);
186
+ }
187
+ }
188
+
189
+ if (verbose) {
190
+ if (playbook.guidance.steps && playbook.guidance.steps.length > 0) {
191
+ lines.push('**Steps**:');
192
+ for (let i = 0; i < playbook.guidance.steps.length; i++) {
193
+ lines.push(` ${i + 1}. ${playbook.guidance.steps[i]}`);
194
+ }
195
+ }
196
+
197
+ if (playbook.applicability.situations.length > 0) {
198
+ lines.push('**When to use**:');
199
+ for (const sit of playbook.applicability.situations) {
200
+ lines.push(` - ${sit}`);
201
+ }
202
+ }
203
+
204
+ if (playbook.applicability.antiPatterns.length > 0) {
205
+ lines.push('**Avoid when**:');
206
+ for (const anti of playbook.applicability.antiPatterns) {
207
+ lines.push(` - ${anti}`);
208
+ }
209
+ }
210
+
211
+ if (playbook.verification.successIndicators.length > 0) {
212
+ lines.push('**Success indicators**:');
213
+ for (const ind of playbook.verification.successIndicators) {
214
+ lines.push(` - ${ind}`);
215
+ }
216
+ }
217
+ }
218
+
219
+ return lines.join('\n');
220
+ }