cognitive-core 0.0.2 → 0.1.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 (329) hide show
  1. package/README.md +302 -116
  2. package/SKILL.md +193 -0
  3. package/dist/agents/index.d.ts +3 -0
  4. package/dist/agents/index.d.ts.map +1 -0
  5. package/dist/agents/index.js +5 -0
  6. package/dist/agents/index.js.map +1 -0
  7. package/dist/agents/mock-provider.d.ts +23 -0
  8. package/dist/agents/mock-provider.d.ts.map +1 -0
  9. package/dist/agents/mock-provider.js +71 -0
  10. package/dist/agents/mock-provider.js.map +1 -0
  11. package/dist/agents/types.d.ts +98 -0
  12. package/dist/agents/types.d.ts.map +1 -0
  13. package/dist/agents/types.js +44 -0
  14. package/dist/agents/types.js.map +1 -0
  15. package/dist/atlas.d.ts +196 -0
  16. package/dist/atlas.d.ts.map +1 -0
  17. package/dist/atlas.js +373 -0
  18. package/dist/atlas.js.map +1 -0
  19. package/dist/bin/cognitive-core.d.ts +18 -0
  20. package/dist/bin/cognitive-core.d.ts.map +1 -0
  21. package/dist/bin/cognitive-core.js +419 -0
  22. package/dist/bin/cognitive-core.js.map +1 -0
  23. package/dist/embeddings/bm25.d.ts +104 -0
  24. package/dist/embeddings/bm25.d.ts.map +1 -0
  25. package/dist/embeddings/bm25.js +264 -0
  26. package/dist/embeddings/bm25.js.map +1 -0
  27. package/dist/embeddings/index.d.ts +12 -0
  28. package/dist/embeddings/index.d.ts.map +1 -0
  29. package/dist/embeddings/index.js +16 -0
  30. package/dist/embeddings/index.js.map +1 -0
  31. package/dist/embeddings/manager.d.ts +112 -0
  32. package/dist/embeddings/manager.d.ts.map +1 -0
  33. package/dist/embeddings/manager.js +215 -0
  34. package/dist/embeddings/manager.js.map +1 -0
  35. package/dist/embeddings/provider.d.ts +101 -0
  36. package/dist/embeddings/provider.d.ts.map +1 -0
  37. package/dist/embeddings/provider.js +232 -0
  38. package/dist/embeddings/provider.js.map +1 -0
  39. package/dist/embeddings/vector-store.d.ts +101 -0
  40. package/dist/embeddings/vector-store.d.ts.map +1 -0
  41. package/dist/embeddings/vector-store.js +256 -0
  42. package/dist/embeddings/vector-store.js.map +1 -0
  43. package/dist/factory.d.ts +193 -0
  44. package/dist/factory.d.ts.map +1 -0
  45. package/dist/factory.js +109 -0
  46. package/dist/factory.js.map +1 -0
  47. package/dist/index.d.ts +30 -453
  48. package/dist/index.d.ts.map +1 -0
  49. package/dist/index.js +84 -509
  50. package/dist/index.js.map +1 -0
  51. package/dist/learning/analyzer.d.ts +110 -0
  52. package/dist/learning/analyzer.d.ts.map +1 -0
  53. package/dist/learning/analyzer.js +213 -0
  54. package/dist/learning/analyzer.js.map +1 -0
  55. package/dist/learning/effectiveness.d.ts +158 -0
  56. package/dist/learning/effectiveness.d.ts.map +1 -0
  57. package/dist/learning/effectiveness.js +251 -0
  58. package/dist/learning/effectiveness.js.map +1 -0
  59. package/dist/learning/index.d.ts +8 -0
  60. package/dist/learning/index.d.ts.map +1 -0
  61. package/dist/learning/index.js +11 -0
  62. package/dist/learning/index.js.map +1 -0
  63. package/dist/learning/llm-extractor.d.ts +88 -0
  64. package/dist/learning/llm-extractor.d.ts.map +1 -0
  65. package/dist/learning/llm-extractor.js +372 -0
  66. package/dist/learning/llm-extractor.js.map +1 -0
  67. package/dist/learning/meta-learner.d.ts +80 -0
  68. package/dist/learning/meta-learner.d.ts.map +1 -0
  69. package/dist/learning/meta-learner.js +355 -0
  70. package/dist/learning/meta-learner.js.map +1 -0
  71. package/dist/learning/pipeline.d.ts +65 -0
  72. package/dist/learning/pipeline.d.ts.map +1 -0
  73. package/dist/learning/pipeline.js +170 -0
  74. package/dist/learning/pipeline.js.map +1 -0
  75. package/dist/learning/playbook-extractor.d.ts +113 -0
  76. package/dist/learning/playbook-extractor.d.ts.map +1 -0
  77. package/dist/learning/playbook-extractor.js +523 -0
  78. package/dist/learning/playbook-extractor.js.map +1 -0
  79. package/dist/learning/usage-inference.d.ts +82 -0
  80. package/dist/learning/usage-inference.d.ts.map +1 -0
  81. package/dist/learning/usage-inference.js +261 -0
  82. package/dist/learning/usage-inference.js.map +1 -0
  83. package/dist/mcp/index.d.ts +6 -0
  84. package/dist/mcp/index.d.ts.map +1 -0
  85. package/dist/mcp/index.js +6 -0
  86. package/dist/mcp/index.js.map +1 -0
  87. package/dist/mcp/playbook-server.d.ts +120 -0
  88. package/dist/mcp/playbook-server.d.ts.map +1 -0
  89. package/dist/mcp/playbook-server.js +427 -0
  90. package/dist/mcp/playbook-server.js.map +1 -0
  91. package/dist/memory/curated-loader.d.ts +62 -0
  92. package/dist/memory/curated-loader.d.ts.map +1 -0
  93. package/dist/memory/curated-loader.js +106 -0
  94. package/dist/memory/curated-loader.js.map +1 -0
  95. package/dist/memory/experience.d.ts +122 -0
  96. package/dist/memory/experience.d.ts.map +1 -0
  97. package/dist/memory/experience.js +392 -0
  98. package/dist/memory/experience.js.map +1 -0
  99. package/dist/memory/index.d.ts +6 -0
  100. package/dist/memory/index.d.ts.map +1 -0
  101. package/dist/memory/index.js +9 -0
  102. package/dist/memory/index.js.map +1 -0
  103. package/dist/memory/meta.d.ts +90 -0
  104. package/dist/memory/meta.d.ts.map +1 -0
  105. package/dist/memory/meta.js +362 -0
  106. package/dist/memory/meta.js.map +1 -0
  107. package/dist/memory/playbook.d.ts +133 -0
  108. package/dist/memory/playbook.d.ts.map +1 -0
  109. package/dist/memory/playbook.js +357 -0
  110. package/dist/memory/playbook.js.map +1 -0
  111. package/dist/memory/system.d.ts +167 -0
  112. package/dist/memory/system.d.ts.map +1 -0
  113. package/dist/memory/system.js +383 -0
  114. package/dist/memory/system.js.map +1 -0
  115. package/dist/runtime/backends/acp.d.ts +67 -0
  116. package/dist/runtime/backends/acp.d.ts.map +1 -0
  117. package/dist/runtime/backends/acp.js +290 -0
  118. package/dist/runtime/backends/acp.js.map +1 -0
  119. package/dist/runtime/backends/index.d.ts +5 -0
  120. package/dist/runtime/backends/index.d.ts.map +1 -0
  121. package/dist/runtime/backends/index.js +6 -0
  122. package/dist/runtime/backends/index.js.map +1 -0
  123. package/dist/runtime/backends/mock.d.ts +67 -0
  124. package/dist/runtime/backends/mock.d.ts.map +1 -0
  125. package/dist/runtime/backends/mock.js +153 -0
  126. package/dist/runtime/backends/mock.js.map +1 -0
  127. package/dist/runtime/backends/subprocess.d.ts +56 -0
  128. package/dist/runtime/backends/subprocess.d.ts.map +1 -0
  129. package/dist/runtime/backends/subprocess.js +260 -0
  130. package/dist/runtime/backends/subprocess.js.map +1 -0
  131. package/dist/runtime/flows/learning.d.ts +73 -0
  132. package/dist/runtime/flows/learning.d.ts.map +1 -0
  133. package/dist/runtime/flows/learning.js +116 -0
  134. package/dist/runtime/flows/learning.js.map +1 -0
  135. package/dist/runtime/flows/validation.d.ts +122 -0
  136. package/dist/runtime/flows/validation.d.ts.map +1 -0
  137. package/dist/runtime/flows/validation.js +223 -0
  138. package/dist/runtime/flows/validation.js.map +1 -0
  139. package/dist/runtime/index.d.ts +6 -0
  140. package/dist/runtime/index.d.ts.map +1 -0
  141. package/dist/runtime/index.js +8 -0
  142. package/dist/runtime/index.js.map +1 -0
  143. package/dist/runtime/manager.d.ts +116 -0
  144. package/dist/runtime/manager.d.ts.map +1 -0
  145. package/dist/runtime/manager.js +416 -0
  146. package/dist/runtime/manager.js.map +1 -0
  147. package/dist/runtime/types.d.ts +138 -0
  148. package/dist/runtime/types.d.ts.map +1 -0
  149. package/dist/runtime/types.js +2 -0
  150. package/dist/runtime/types.js.map +1 -0
  151. package/dist/search/evaluator.d.ts +102 -0
  152. package/dist/search/evaluator.d.ts.map +1 -0
  153. package/dist/search/evaluator.js +352 -0
  154. package/dist/search/evaluator.js.map +1 -0
  155. package/dist/search/index.d.ts +7 -0
  156. package/dist/search/index.d.ts.map +1 -0
  157. package/dist/search/index.js +11 -0
  158. package/dist/search/index.js.map +1 -0
  159. package/dist/search/refinement-loop.d.ts +73 -0
  160. package/dist/search/refinement-loop.d.ts.map +1 -0
  161. package/dist/search/refinement-loop.js +245 -0
  162. package/dist/search/refinement-loop.js.map +1 -0
  163. package/dist/search/refinement-types.d.ts +154 -0
  164. package/dist/search/refinement-types.d.ts.map +1 -0
  165. package/dist/search/refinement-types.js +99 -0
  166. package/dist/search/refinement-types.js.map +1 -0
  167. package/dist/search/router.d.ts +61 -0
  168. package/dist/search/router.d.ts.map +1 -0
  169. package/dist/search/router.js +197 -0
  170. package/dist/search/router.js.map +1 -0
  171. package/dist/search/solver.d.ts +75 -0
  172. package/dist/search/solver.d.ts.map +1 -0
  173. package/dist/search/solver.js +216 -0
  174. package/dist/search/solver.js.map +1 -0
  175. package/dist/search/verification-runner.d.ts +125 -0
  176. package/dist/search/verification-runner.d.ts.map +1 -0
  177. package/dist/search/verification-runner.js +440 -0
  178. package/dist/search/verification-runner.js.map +1 -0
  179. package/dist/surfacing/index.d.ts +2 -0
  180. package/dist/surfacing/index.d.ts.map +1 -0
  181. package/dist/surfacing/index.js +2 -0
  182. package/dist/surfacing/index.js.map +1 -0
  183. package/dist/surfacing/skill-library.d.ts +158 -0
  184. package/dist/surfacing/skill-library.d.ts.map +1 -0
  185. package/dist/surfacing/skill-library.js +429 -0
  186. package/dist/surfacing/skill-library.js.map +1 -0
  187. package/dist/types/config.d.ts +1113 -0
  188. package/dist/types/config.d.ts.map +1 -0
  189. package/dist/types/config.js +274 -0
  190. package/dist/types/config.js.map +1 -0
  191. package/dist/types/index.d.ts +9 -0
  192. package/dist/types/index.d.ts.map +1 -0
  193. package/dist/types/index.js +14 -0
  194. package/dist/types/index.js.map +1 -0
  195. package/dist/types/memory.d.ts +339 -0
  196. package/dist/types/memory.d.ts.map +1 -0
  197. package/dist/types/memory.js +207 -0
  198. package/dist/types/memory.js.map +1 -0
  199. package/dist/types/meta.d.ts +146 -0
  200. package/dist/types/meta.d.ts.map +1 -0
  201. package/dist/types/meta.js +51 -0
  202. package/dist/types/meta.js.map +1 -0
  203. package/dist/types/outcome.d.ts +42 -0
  204. package/dist/types/outcome.d.ts.map +1 -0
  205. package/dist/types/outcome.js +50 -0
  206. package/dist/types/outcome.js.map +1 -0
  207. package/dist/types/playbook.d.ts +119 -0
  208. package/dist/types/playbook.d.ts.map +1 -0
  209. package/dist/types/playbook.js +71 -0
  210. package/dist/types/playbook.js.map +1 -0
  211. package/dist/types/step.d.ts +44 -0
  212. package/dist/types/step.d.ts.map +1 -0
  213. package/dist/types/step.js +32 -0
  214. package/dist/types/step.js.map +1 -0
  215. package/dist/types/task.d.ts +91 -0
  216. package/dist/types/task.d.ts.map +1 -0
  217. package/dist/types/task.js +39 -0
  218. package/dist/types/task.js.map +1 -0
  219. package/dist/types/trajectory.d.ts +221 -0
  220. package/dist/types/trajectory.d.ts.map +1 -0
  221. package/dist/types/trajectory.js +60 -0
  222. package/dist/types/trajectory.js.map +1 -0
  223. package/dist/utils/index.d.ts +4 -0
  224. package/dist/utils/index.d.ts.map +1 -0
  225. package/dist/utils/index.js +4 -0
  226. package/dist/utils/index.js.map +1 -0
  227. package/dist/utils/similarity.d.ts +31 -0
  228. package/dist/utils/similarity.d.ts.map +1 -0
  229. package/dist/utils/similarity.js +107 -0
  230. package/dist/utils/similarity.js.map +1 -0
  231. package/dist/utils/storage.d.ts +106 -0
  232. package/dist/utils/storage.d.ts.map +1 -0
  233. package/dist/utils/storage.js +203 -0
  234. package/dist/utils/storage.js.map +1 -0
  235. package/dist/utils/validation.d.ts +129 -0
  236. package/dist/utils/validation.d.ts.map +1 -0
  237. package/dist/utils/validation.js +171 -0
  238. package/dist/utils/validation.js.map +1 -0
  239. package/package.json +50 -34
  240. package/scripts/migrate-to-playbooks.ts +307 -0
  241. package/src/agents/index.ts +14 -0
  242. package/src/agents/mock-provider.ts +93 -0
  243. package/src/agents/types.ts +137 -0
  244. package/src/atlas.ts +560 -0
  245. package/src/bin/cognitive-core.ts +470 -0
  246. package/src/embeddings/bm25.ts +337 -0
  247. package/src/embeddings/index.ts +39 -0
  248. package/src/embeddings/manager.ts +288 -0
  249. package/src/embeddings/provider.ts +311 -0
  250. package/src/embeddings/vector-store.ts +353 -0
  251. package/src/factory.ts +263 -0
  252. package/src/index.ts +246 -0
  253. package/src/learning/analyzer.ts +335 -0
  254. package/src/learning/effectiveness.ts +428 -0
  255. package/src/learning/index.ts +58 -0
  256. package/src/learning/llm-extractor.ts +542 -0
  257. package/src/learning/meta-learner.ts +516 -0
  258. package/src/learning/pipeline.ts +244 -0
  259. package/src/learning/playbook-extractor.ts +702 -0
  260. package/src/learning/usage-inference.ts +372 -0
  261. package/src/mcp/index.ts +12 -0
  262. package/src/mcp/playbook-server.ts +565 -0
  263. package/src/memory/curated-loader.ts +160 -0
  264. package/src/memory/experience.ts +515 -0
  265. package/src/memory/index.ts +27 -0
  266. package/src/memory/meta.ts +506 -0
  267. package/src/memory/playbook.ts +493 -0
  268. package/src/memory/system.ts +551 -0
  269. package/src/runtime/backends/acp.ts +378 -0
  270. package/src/runtime/backends/index.ts +24 -0
  271. package/src/runtime/backends/mock.ts +218 -0
  272. package/src/runtime/backends/subprocess.ts +356 -0
  273. package/src/runtime/flows/learning.ts +183 -0
  274. package/src/runtime/flows/validation.ts +381 -0
  275. package/src/runtime/index.ts +53 -0
  276. package/src/runtime/manager.ts +541 -0
  277. package/src/runtime/types.ts +157 -0
  278. package/src/search/evaluator.ts +474 -0
  279. package/src/search/index.ts +59 -0
  280. package/src/search/refinement-loop.ts +363 -0
  281. package/src/search/refinement-types.ts +159 -0
  282. package/src/search/router.ts +261 -0
  283. package/src/search/solver.ts +303 -0
  284. package/src/search/verification-runner.ts +570 -0
  285. package/src/surfacing/index.ts +6 -0
  286. package/src/surfacing/skill-library.ts +594 -0
  287. package/src/types/config.ts +333 -0
  288. package/src/types/index.ts +130 -0
  289. package/src/types/memory.ts +270 -0
  290. package/src/types/meta.ts +218 -0
  291. package/src/types/outcome.ts +66 -0
  292. package/src/types/playbook.ts +196 -0
  293. package/src/types/step.ts +40 -0
  294. package/src/types/task.ts +52 -0
  295. package/src/types/trajectory.ts +80 -0
  296. package/src/utils/index.ts +38 -0
  297. package/src/utils/similarity.ts +139 -0
  298. package/src/utils/storage.ts +249 -0
  299. package/src/utils/validation.ts +286 -0
  300. package/tests/embeddings/bm25.test.ts +130 -0
  301. package/tests/embeddings/manager.test.ts +205 -0
  302. package/tests/integration/atlas.test.ts +266 -0
  303. package/tests/integration/e2e.test.ts +929 -0
  304. package/tests/learning/analyzer.test.ts +426 -0
  305. package/tests/learning/effectiveness.test.ts +542 -0
  306. package/tests/learning/pipeline.test.ts +176 -0
  307. package/tests/learning/playbook-extractor-provenance.test.ts +114 -0
  308. package/tests/learning/usage-inference.test.ts +254 -0
  309. package/tests/mcp/playbook-server.test.ts +252 -0
  310. package/tests/memory/experience.test.ts +198 -0
  311. package/tests/memory/playbook.test.ts +338 -0
  312. package/tests/memory/provenance.test.ts +639 -0
  313. package/tests/memory/system.test.ts +325 -0
  314. package/tests/runtime/agent-manager.test.ts +512 -0
  315. package/tests/runtime/mock-backend.test.ts +248 -0
  316. package/tests/search/refinement-loop.test.ts +468 -0
  317. package/tests/search/refinement.test.ts +267 -0
  318. package/tests/search/router.test.ts +427 -0
  319. package/tests/surfacing/skill-library.test.ts +292 -0
  320. package/tests/types/outcome.test.ts +147 -0
  321. package/tests/types/step.test.ts +133 -0
  322. package/tests/types/task.test.ts +158 -0
  323. package/tests/types/trajectory.test.ts +253 -0
  324. package/tests/utils/similarity.test.ts +188 -0
  325. package/tests/utils/validation.test.ts +252 -0
  326. package/tsconfig.json +25 -0
  327. package/vitest.config.ts +22 -0
  328. package/dist/index.d.mts +0 -466
  329. package/dist/index.mjs +0 -478
@@ -0,0 +1,516 @@
1
+ /**
2
+ * Meta Learner
3
+ * Learns about learning - analyzes trajectories to improve routing and retrieval
4
+ */
5
+
6
+ import type {
7
+ MetaObservation,
8
+ MetaStrategy,
9
+ Trajectory,
10
+ MetaContext,
11
+ RoutingDecisionType,
12
+ RetrievalQuality,
13
+ OutcomeQuality,
14
+ Step,
15
+ } from '../types/index.js';
16
+ import { createMetaObservation, createMetaStrategy } from '../types/index.js';
17
+ import type { MetaMemory } from '../memory/meta.js';
18
+ import type { PlaybookMatch } from '../memory/playbook.js';
19
+
20
+ /**
21
+ * Routing decision from the router
22
+ */
23
+ export interface RoutingDecision {
24
+ strategy: RoutingDecisionType;
25
+ confidence: number;
26
+ estimatedBudget: number;
27
+ reasoning: string;
28
+ }
29
+
30
+ /**
31
+ * Configuration for meta learner
32
+ */
33
+ export interface MetaLearnerConfig {
34
+ /** Minimum observations before generating strategies */
35
+ minObservationsForStrategies: number;
36
+ /** How often to run strategy generation (in observations) */
37
+ strategyGenerationInterval: number;
38
+ /** Threshold for considering retrieval helpful */
39
+ helpfulRetrievalThreshold: number;
40
+ /** Minimum tactic word overlap to consider guidance followed (default: 2) */
41
+ tacticOverlapThreshold: number;
42
+ }
43
+
44
+ const DEFAULT_CONFIG: MetaLearnerConfig = {
45
+ minObservationsForStrategies: 10,
46
+ strategyGenerationInterval: 10,
47
+ helpfulRetrievalThreshold: 0.7,
48
+ tacticOverlapThreshold: 2,
49
+ };
50
+
51
+ /**
52
+ * Meta Learner
53
+ * Generates meta-observations and learns meta-strategies
54
+ */
55
+ export class MetaLearner {
56
+ private meta: MetaMemory;
57
+ private config: MetaLearnerConfig;
58
+ private observationCount = 0;
59
+
60
+ constructor(meta: MetaMemory, config?: Partial<MetaLearnerConfig>) {
61
+ this.meta = meta;
62
+ this.config = { ...DEFAULT_CONFIG, ...config };
63
+ }
64
+
65
+ /**
66
+ * Generate reflection from completed trajectory
67
+ */
68
+ async generateReflection(
69
+ trajectory: Trajectory,
70
+ routing: RoutingDecision,
71
+ playbooksUsed: PlaybookMatch[]
72
+ ): Promise<MetaObservation> {
73
+ // Analyze the trajectory
74
+ const wasSuccessful = trajectory.outcome.success;
75
+ const actualEffort = trajectory.llmCalls ?? trajectory.steps.length;
76
+
77
+ // Determine retrieval quality
78
+ const retrievalQuality = this.assessRetrievalQuality(
79
+ trajectory,
80
+ playbooksUsed,
81
+ wasSuccessful
82
+ );
83
+
84
+ // Determine outcome quality
85
+ const outcomeQuality = this.assessOutcomeQuality(trajectory, actualEffort, routing.estimatedBudget);
86
+
87
+ // Analyze execution path
88
+ const execution = this.analyzeExecution(trajectory);
89
+
90
+ // Generate lessons learned
91
+ const lessons = this.generateLessons(
92
+ trajectory,
93
+ routing,
94
+ playbooksUsed,
95
+ wasSuccessful
96
+ );
97
+
98
+ const observation = createMetaObservation({
99
+ trajectoryId: trajectory.id,
100
+ routing: {
101
+ decision: routing.strategy,
102
+ confidence: routing.confidence,
103
+ reasoning: routing.reasoning,
104
+ },
105
+ memoryUsage: {
106
+ experiencesRetrieved: 0, // TODO: Track from memory query
107
+ playbooksApplied: playbooksUsed.map((p) => p.playbook.id),
108
+ retrievalQuality,
109
+ },
110
+ execution,
111
+ outcome: {
112
+ success: wasSuccessful,
113
+ quality: outcomeQuality,
114
+ effortActual: actualEffort,
115
+ effortEstimate: routing.estimatedBudget,
116
+ },
117
+ lessons,
118
+ });
119
+
120
+ // Record the observation
121
+ await this.meta.recordObservation(observation);
122
+ this.observationCount++;
123
+
124
+ // Periodically generate new meta-strategies
125
+ if (this.observationCount % this.config.strategyGenerationInterval === 0) {
126
+ await this.learn();
127
+ }
128
+
129
+ return observation;
130
+ }
131
+
132
+ /**
133
+ * Analyze accumulated observations and generate meta-strategies
134
+ */
135
+ async learn(): Promise<MetaStrategy[]> {
136
+ const stats = await this.meta.getStats();
137
+
138
+ if (stats.totalObservations < this.config.minObservationsForStrategies) {
139
+ return [];
140
+ }
141
+
142
+ // Generate new meta-strategies based on patterns
143
+ const newStrategies = await this.meta.generateMetaStrategies();
144
+
145
+ // Additional strategy generation based on analysis
146
+ const routingAnalysis = await this.meta.analyzeRoutingEffectiveness();
147
+ const additionalStrategies = this.generateStrategiesFromAnalysis(routingAnalysis);
148
+
149
+ // Add and save additional strategies
150
+ for (const strategy of additionalStrategies) {
151
+ const existing = await this.meta.getStrategy(strategy.id);
152
+ if (!existing) {
153
+ await this.meta.addStrategy(strategy);
154
+ newStrategies.push(strategy);
155
+ }
156
+ }
157
+
158
+ return newStrategies;
159
+ }
160
+
161
+ /**
162
+ * Apply meta-strategies to adjust routing
163
+ */
164
+ async adjustRouting(
165
+ baseRouting: RoutingDecision,
166
+ taskCharacteristics: string[],
167
+ memoryState: string[]
168
+ ): Promise<RoutingDecision> {
169
+ const context: MetaContext = {
170
+ taskCharacteristics,
171
+ memoryState,
172
+ };
173
+
174
+ const applicableStrategies = await this.meta.getRoutingAdjustments(context);
175
+
176
+ if (applicableStrategies.length === 0) {
177
+ return baseRouting;
178
+ }
179
+
180
+ // Apply adjustments
181
+ let adjustedConfidence = baseRouting.confidence;
182
+ let adjustedStrategy = baseRouting.strategy;
183
+ const adjustmentReasons: string[] = [];
184
+
185
+ for (const strategy of applicableStrategies) {
186
+ // Apply routing bias
187
+ if (strategy.adjustment.routingBias) {
188
+ for (const [decision, bias] of Object.entries(strategy.adjustment.routingBias)) {
189
+ if (decision === baseRouting.strategy) {
190
+ adjustedConfidence += bias as number;
191
+ }
192
+ }
193
+
194
+ // Check if another strategy is strongly preferred
195
+ const biases = Object.entries(strategy.adjustment.routingBias);
196
+ for (const [decision, bias] of biases) {
197
+ if (
198
+ (bias as number) > 0.3 &&
199
+ decision !== baseRouting.strategy &&
200
+ baseRouting.confidence < 0.5
201
+ ) {
202
+ adjustedStrategy = decision as RoutingDecisionType;
203
+ adjustmentReasons.push(
204
+ `Meta-strategy '${strategy.name}' suggests ${decision}`
205
+ );
206
+ }
207
+ }
208
+ }
209
+
210
+ // Add execution hint to reasoning
211
+ if (strategy.adjustment.executionHint) {
212
+ adjustmentReasons.push(strategy.adjustment.executionHint);
213
+ }
214
+ }
215
+
216
+ // Clamp confidence
217
+ adjustedConfidence = Math.max(0.1, Math.min(0.95, adjustedConfidence));
218
+
219
+ return {
220
+ strategy: adjustedStrategy,
221
+ confidence: adjustedConfidence,
222
+ estimatedBudget: baseRouting.estimatedBudget,
223
+ reasoning:
224
+ adjustmentReasons.length > 0
225
+ ? `${baseRouting.reasoning}. Meta-adjustments: ${adjustmentReasons.join('; ')}`
226
+ : baseRouting.reasoning,
227
+ };
228
+ }
229
+
230
+ /**
231
+ * Assess retrieval quality based on trajectory outcome
232
+ */
233
+ private assessRetrievalQuality(
234
+ trajectory: Trajectory,
235
+ playbooksUsed: PlaybookMatch[],
236
+ wasSuccessful: boolean
237
+ ): RetrievalQuality {
238
+ if (playbooksUsed.length === 0) {
239
+ return 'neutral';
240
+ }
241
+
242
+ // Check if playbook guidance was followed
243
+ const guidanceFollowed = this.checkGuidanceFollowed(trajectory, playbooksUsed);
244
+
245
+ if (wasSuccessful && guidanceFollowed) {
246
+ return 'helpful';
247
+ } else if (!wasSuccessful && guidanceFollowed) {
248
+ // Followed guidance but still failed - might be misleading
249
+ return 'misleading';
250
+ } else {
251
+ return 'neutral';
252
+ }
253
+ }
254
+
255
+ /**
256
+ * Check if playbook guidance was followed in the trajectory
257
+ */
258
+ private checkGuidanceFollowed(
259
+ trajectory: Trajectory,
260
+ playbooksUsed: PlaybookMatch[]
261
+ ): boolean {
262
+ for (const { playbook } of playbooksUsed) {
263
+ const tactics = playbook.guidance.tactics;
264
+ if (tactics.length === 0) continue;
265
+
266
+ // Check if any tactics were reflected in the steps
267
+ let tacticsFollowed = 0;
268
+ for (const tactic of tactics) {
269
+ const tacticWords = tactic.toLowerCase().split(/\s+/);
270
+ for (const step of trajectory.steps) {
271
+ const actionWords = step.action.toLowerCase().split(/\s+/);
272
+ const overlap = tacticWords.filter((w) => actionWords.includes(w)).length;
273
+ if (overlap >= this.config.tacticOverlapThreshold) {
274
+ tacticsFollowed++;
275
+ break;
276
+ }
277
+ }
278
+ }
279
+
280
+ if (tacticsFollowed >= tactics.length * 0.5) {
281
+ return true;
282
+ }
283
+ }
284
+
285
+ return false;
286
+ }
287
+
288
+ /**
289
+ * Assess outcome quality
290
+ */
291
+ private assessOutcomeQuality(
292
+ trajectory: Trajectory,
293
+ actualEffort: number,
294
+ estimatedEffort: number
295
+ ): OutcomeQuality {
296
+ if (!trajectory.outcome.success) {
297
+ return 'poor';
298
+ }
299
+
300
+ // Compare actual vs estimated effort
301
+ const effortRatio = actualEffort / estimatedEffort;
302
+
303
+ if (effortRatio <= 0.5) {
304
+ return 'excellent'; // Much less effort than expected
305
+ } else if (effortRatio <= 1.0) {
306
+ return 'good'; // Within estimate
307
+ } else if (effortRatio <= 2.0) {
308
+ return 'acceptable'; // Some overrun
309
+ } else {
310
+ return 'poor'; // Significant overrun
311
+ }
312
+ }
313
+
314
+ /**
315
+ * Analyze execution path
316
+ */
317
+ private analyzeExecution(trajectory: Trajectory): {
318
+ decompositionUsed: boolean;
319
+ refinementIterations: number;
320
+ backtrackingOccurred: boolean;
321
+ toolsUsed: string[];
322
+ } {
323
+ const steps = trajectory.steps;
324
+ const toolsUsed = new Set<string>();
325
+ let backtrackingOccurred = false;
326
+
327
+ // Collect tools used (extract from action)
328
+ for (const step of steps) {
329
+ // Try to extract tool name from action (e.g., "tool_use: Bash" or just "Bash")
330
+ const toolMatch = step.action.match(/^(?:tool_use:\s*)?(\w+)/);
331
+ if (toolMatch) {
332
+ toolsUsed.add(toolMatch[1]);
333
+ }
334
+ }
335
+
336
+ // Check for backtracking (similar actions repeated)
337
+ const actionCounts = new Map<string, number>();
338
+ for (const step of steps) {
339
+ const normalized = step.action.toLowerCase().slice(0, 50);
340
+ actionCounts.set(normalized, (actionCounts.get(normalized) ?? 0) + 1);
341
+ }
342
+
343
+ for (const count of actionCounts.values()) {
344
+ if (count > 2) {
345
+ backtrackingOccurred = true;
346
+ break;
347
+ }
348
+ }
349
+
350
+ // Check for decomposition (multiple sub-tasks)
351
+ const decompositionUsed =
352
+ steps.some(
353
+ (s: Step) =>
354
+ s.thought?.includes('break') ||
355
+ s.thought?.includes('decompose') ||
356
+ s.thought?.includes('step by step') ||
357
+ s.thought?.includes('first,')
358
+ ) ?? false;
359
+
360
+ // Estimate refinement iterations (failures followed by retries)
361
+ let refinementIterations = 0;
362
+ for (let i = 0; i < steps.length - 1; i++) {
363
+ if (
364
+ steps[i].observation?.includes('error') ||
365
+ steps[i].observation?.includes('failed')
366
+ ) {
367
+ if (steps[i + 1]?.thought?.includes('try') || steps[i + 1]?.thought?.includes('fix')) {
368
+ refinementIterations++;
369
+ }
370
+ }
371
+ }
372
+
373
+ return {
374
+ decompositionUsed,
375
+ refinementIterations,
376
+ backtrackingOccurred,
377
+ toolsUsed: Array.from(toolsUsed),
378
+ };
379
+ }
380
+
381
+ /**
382
+ * Generate lessons learned
383
+ */
384
+ private generateLessons(
385
+ trajectory: Trajectory,
386
+ routing: RoutingDecision,
387
+ playbooksUsed: PlaybookMatch[],
388
+ wasSuccessful: boolean
389
+ ): {
390
+ whatWorked: string[];
391
+ whatFailed: string[];
392
+ suggestions: string[];
393
+ } {
394
+ const whatWorked: string[] = [];
395
+ const whatFailed: string[] = [];
396
+ const suggestions: string[] = [];
397
+
398
+ if (wasSuccessful) {
399
+ whatWorked.push(`${routing.strategy} routing led to success`);
400
+
401
+ if (playbooksUsed.length > 0) {
402
+ whatWorked.push(
403
+ `Playbook guidance was applicable: ${playbooksUsed.map((p) => p.playbook.name).join(', ')}`
404
+ );
405
+ }
406
+
407
+ // Check what steps seemed most effective
408
+ const keySteps = trajectory.steps.filter(
409
+ (s: Step) =>
410
+ !s.observation?.includes('error') && !s.observation?.includes('failed')
411
+ );
412
+ if (keySteps.length > 0) {
413
+ whatWorked.push(
414
+ `Effective actions: ${keySteps
415
+ .slice(0, 3)
416
+ .map((s: Step) => s.action.slice(0, 50))
417
+ .join(', ')}`
418
+ );
419
+ }
420
+ } else {
421
+ whatFailed.push(`${routing.strategy} routing did not lead to success`);
422
+
423
+ // Check what went wrong
424
+ const errorSteps = trajectory.steps.filter(
425
+ (s: Step) =>
426
+ s.observation?.includes('error') || s.observation?.includes('failed')
427
+ );
428
+ if (errorSteps.length > 0) {
429
+ whatFailed.push(
430
+ `Errors encountered: ${errorSteps
431
+ .slice(0, 2)
432
+ .map((s: Step) => s.observation?.slice(0, 100))
433
+ .join('; ')}`
434
+ );
435
+ }
436
+
437
+ // Suggestions based on failure
438
+ if (routing.confidence > 0.7) {
439
+ suggestions.push('High confidence did not guarantee success - consider more exploration');
440
+ }
441
+
442
+ if (playbooksUsed.length > 0) {
443
+ suggestions.push('Review playbook applicability - guidance may need refinement');
444
+ } else {
445
+ suggestions.push('No playbooks were applied - consider extracting patterns from similar tasks');
446
+ }
447
+ }
448
+
449
+ return { whatWorked, whatFailed, suggestions };
450
+ }
451
+
452
+ /**
453
+ * Generate strategies from routing analysis
454
+ */
455
+ private generateStrategiesFromAnalysis(
456
+ analysis: {
457
+ byDecision: Record<string, { total: number; successful: number }>;
458
+ recommendations: string[];
459
+ }
460
+ ): MetaStrategy[] {
461
+ const strategies: MetaStrategy[] = [];
462
+
463
+ // Generate strategy based on success rates
464
+ for (const [decision, stats] of Object.entries(analysis.byDecision)) {
465
+ if (stats.total < 5) continue;
466
+
467
+ const successRate = stats.successful / stats.total;
468
+
469
+ if (successRate < 0.4) {
470
+ // This decision often fails
471
+ strategies.push(
472
+ createMetaStrategy({
473
+ name: `avoid-${decision}-low-success`,
474
+ condition: {
475
+ taskCharacteristics: [],
476
+ memoryState: [`${decision} suggested`],
477
+ },
478
+ adjustment: {
479
+ routingBias: { [decision]: -0.2 } as Record<RoutingDecisionType, number>,
480
+ retrievalModification: 'expand search',
481
+ executionHint: `Consider alternatives to ${decision}`,
482
+ },
483
+ })
484
+ );
485
+ } else if (successRate > 0.8 && stats.total >= 10) {
486
+ // This decision is highly effective
487
+ strategies.push(
488
+ createMetaStrategy({
489
+ name: `prefer-${decision}-high-success`,
490
+ condition: {
491
+ taskCharacteristics: [],
492
+ memoryState: [`${decision} applicable`],
493
+ },
494
+ adjustment: {
495
+ routingBias: { [decision]: 0.1 } as Record<RoutingDecisionType, number>,
496
+ retrievalModification: '',
497
+ executionHint: `${decision} has been effective`,
498
+ },
499
+ })
500
+ );
501
+ }
502
+ }
503
+
504
+ return strategies;
505
+ }
506
+ }
507
+
508
+ /**
509
+ * Create a meta learner
510
+ */
511
+ export function createMetaLearner(
512
+ meta: MetaMemory,
513
+ config?: Partial<MetaLearnerConfig>
514
+ ): MetaLearner {
515
+ return new MetaLearner(meta, config);
516
+ }