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,270 @@
1
+ import { z } from 'zod';
2
+
3
+ /**
4
+ * Experience - Task-level retrieval unit (ReMem-style)
5
+ * Stores complete (task, solution, feedback) tuples
6
+ */
7
+ export const ExperienceSchema = z.object({
8
+ id: z.string(),
9
+ /** Original task input/description */
10
+ taskInput: z.string(),
11
+ /** The solution that was produced */
12
+ solutionOutput: z.string(),
13
+ /** Feedback/verification result */
14
+ feedback: z.string(),
15
+ /** Whether the experience was successful */
16
+ success: z.boolean(),
17
+ /** Domain this experience belongs to */
18
+ domain: z.string(),
19
+ /** Embedding for similarity search */
20
+ embedding: z.array(z.number()).optional(),
21
+ /** Link back to original trajectory */
22
+ trajectoryId: z.string(),
23
+ /** Usage statistics */
24
+ usageCount: z.number().default(0),
25
+ /** Creation timestamp */
26
+ createdAt: z.date().default(() => new Date()),
27
+ /** Last accessed timestamp */
28
+ lastAccessedAt: z.date().optional(),
29
+ /** Additional metadata */
30
+ metadata: z.record(z.unknown()).default({}),
31
+ });
32
+
33
+ export type Experience = z.infer<typeof ExperienceSchema>;
34
+
35
+ /**
36
+ * CodeConcept - Reusable code patterns (Stitch/LILO-style)
37
+ * Three types: primitive (base), learned (extracted), composed (combined)
38
+ */
39
+ export const CodeConceptSchema = z.object({
40
+ id: z.string(),
41
+ /** Human-readable name */
42
+ name: z.string(),
43
+ /** Description of what this concept does */
44
+ description: z.string(),
45
+ /** The code/pattern itself */
46
+ code: z.string(),
47
+ /** Function signature if applicable */
48
+ signature: z.string().optional(),
49
+ /** Usage examples: [input, output] pairs */
50
+ examples: z.array(z.tuple([z.string(), z.string()])).default([]),
51
+ /** How the concept was created */
52
+ source: z.enum(['primitive', 'learned', 'composed']),
53
+ /** Usage statistics */
54
+ usageCount: z.number().default(0),
55
+ /** Success rate when used */
56
+ successRate: z.number().min(0).max(1).default(0),
57
+ /** Domain this concept belongs to */
58
+ domain: z.string().optional(),
59
+ /** Embedding for similarity search */
60
+ embedding: z.array(z.number()).optional(),
61
+ /** Parent concepts if composed */
62
+ parentIds: z.array(z.string()).default([]),
63
+ /** Creation timestamp */
64
+ createdAt: z.date().default(() => new Date()),
65
+ /** Additional metadata */
66
+ metadata: z.record(z.unknown()).default({}),
67
+ });
68
+
69
+ export type CodeConcept = z.infer<typeof CodeConceptSchema>;
70
+
71
+ /**
72
+ * Strategy - High-level reasoning patterns (ArcMemo-style)
73
+ * Abstract "when/what" rules for problem-solving
74
+ */
75
+ export const StrategySchema = z.object({
76
+ id: z.string(),
77
+ /** Situation description: when to apply this strategy */
78
+ situation: z.string(),
79
+ /** Suggestion: what to do in this situation */
80
+ suggestion: z.string(),
81
+ /** Parameters that can vary in application */
82
+ parameters: z.array(z.object({
83
+ name: z.string(),
84
+ description: z.string(),
85
+ type: z.string().optional(),
86
+ defaultValue: z.unknown().optional(),
87
+ })).default([]),
88
+ /** Usage statistics */
89
+ usageCount: z.number().default(0),
90
+ /** Success rate when applied */
91
+ successRate: z.number().min(0).max(1).default(0),
92
+ /** Domain this strategy belongs to */
93
+ domain: z.string().optional(),
94
+ /** Embedding for similarity search */
95
+ embedding: z.array(z.number()).optional(),
96
+ /** Source trajectories this was extracted from */
97
+ sourceTrajectoryIds: z.array(z.string()).default([]),
98
+ /** Creation timestamp */
99
+ createdAt: z.date().default(() => new Date()),
100
+ /** Confidence in this strategy */
101
+ confidence: z.number().min(0).max(1).default(0.5),
102
+ /** Additional metadata */
103
+ metadata: z.record(z.unknown()).default({}),
104
+ });
105
+
106
+ export type Strategy = z.infer<typeof StrategySchema>;
107
+
108
+ /**
109
+ * Skill - A more concrete, actionable form of strategy
110
+ * Combines situation awareness with specific steps (Claudeception-inspired)
111
+ */
112
+ export const SkillSchema = z.object({
113
+ id: z.string(),
114
+ /** Unique name (kebab-case) */
115
+ name: z.string(),
116
+ /** Rich description for semantic matching */
117
+ description: z.string(),
118
+ /** The problem this skill solves */
119
+ problem: z.string(),
120
+ /** Trigger conditions (specific symptoms, error messages, etc.) */
121
+ triggers: z.array(z.string()).default([]),
122
+ /** Step-by-step solution */
123
+ solution: z.array(z.string()),
124
+ /** How to verify the skill worked */
125
+ verification: z.string().optional(),
126
+ /** Concrete example */
127
+ example: z.string().optional(),
128
+ /** Version for tracking updates */
129
+ version: z.string().default('1.0.0'),
130
+ /** Domain this skill belongs to */
131
+ domain: z.string().optional(),
132
+ /** Embedding for similarity search */
133
+ embedding: z.array(z.number()).optional(),
134
+ /** Usage statistics */
135
+ usageCount: z.number().default(0),
136
+ /** Success rate */
137
+ successRate: z.number().min(0).max(1).default(0),
138
+ /** Creation timestamp */
139
+ createdAt: z.date().default(() => new Date()),
140
+ /** Last updated */
141
+ updatedAt: z.date().default(() => new Date()),
142
+ /** Additional metadata */
143
+ metadata: z.record(z.unknown()).default({}),
144
+ });
145
+
146
+ export type Skill = z.infer<typeof SkillSchema>;
147
+
148
+ /**
149
+ * Create a new experience
150
+ */
151
+ export function createExperience(params: {
152
+ id?: string;
153
+ taskInput: string;
154
+ solutionOutput: string;
155
+ feedback: string;
156
+ success: boolean;
157
+ domain: string;
158
+ trajectoryId: string;
159
+ embedding?: number[];
160
+ metadata?: Record<string, unknown>;
161
+ }): Experience {
162
+ return ExperienceSchema.parse({
163
+ id: params.id ?? crypto.randomUUID(),
164
+ taskInput: params.taskInput,
165
+ solutionOutput: params.solutionOutput,
166
+ feedback: params.feedback,
167
+ success: params.success,
168
+ domain: params.domain,
169
+ trajectoryId: params.trajectoryId,
170
+ embedding: params.embedding,
171
+ metadata: params.metadata ?? {},
172
+ createdAt: new Date(),
173
+ });
174
+ }
175
+
176
+ /**
177
+ * Create a new code concept
178
+ */
179
+ export function createConcept(params: {
180
+ id?: string;
181
+ name: string;
182
+ description: string;
183
+ code: string;
184
+ signature?: string;
185
+ source: 'primitive' | 'learned' | 'composed';
186
+ domain?: string;
187
+ examples?: [string, string][];
188
+ parentIds?: string[];
189
+ metadata?: Record<string, unknown>;
190
+ }): CodeConcept {
191
+ return CodeConceptSchema.parse({
192
+ id: params.id ?? crypto.randomUUID(),
193
+ name: params.name,
194
+ description: params.description,
195
+ code: params.code,
196
+ signature: params.signature,
197
+ source: params.source,
198
+ domain: params.domain,
199
+ examples: params.examples ?? [],
200
+ parentIds: params.parentIds ?? [],
201
+ metadata: params.metadata ?? {},
202
+ createdAt: new Date(),
203
+ });
204
+ }
205
+
206
+ /**
207
+ * Create a new strategy
208
+ */
209
+ export function createStrategy(params: {
210
+ id?: string;
211
+ situation: string;
212
+ suggestion: string;
213
+ parameters?: Array<{
214
+ name: string;
215
+ description: string;
216
+ type?: string;
217
+ defaultValue?: unknown;
218
+ }>;
219
+ domain?: string;
220
+ sourceTrajectoryIds?: string[];
221
+ confidence?: number;
222
+ metadata?: Record<string, unknown>;
223
+ }): Strategy {
224
+ return StrategySchema.parse({
225
+ id: params.id ?? crypto.randomUUID(),
226
+ situation: params.situation,
227
+ suggestion: params.suggestion,
228
+ parameters: params.parameters ?? [],
229
+ domain: params.domain,
230
+ sourceTrajectoryIds: params.sourceTrajectoryIds ?? [],
231
+ confidence: params.confidence ?? 0.5,
232
+ metadata: params.metadata ?? {},
233
+ createdAt: new Date(),
234
+ });
235
+ }
236
+
237
+ /**
238
+ * Create a new skill
239
+ */
240
+ export function createSkill(params: {
241
+ id?: string;
242
+ name: string;
243
+ description: string;
244
+ problem: string;
245
+ triggers?: string[];
246
+ solution: string[];
247
+ verification?: string;
248
+ example?: string;
249
+ domain?: string;
250
+ embedding?: number[];
251
+ version?: string;
252
+ metadata?: Record<string, unknown>;
253
+ }): Skill {
254
+ return SkillSchema.parse({
255
+ id: params.id ?? crypto.randomUUID(),
256
+ name: params.name,
257
+ description: params.description,
258
+ problem: params.problem,
259
+ triggers: params.triggers ?? [],
260
+ solution: params.solution,
261
+ verification: params.verification,
262
+ example: params.example,
263
+ domain: params.domain,
264
+ embedding: params.embedding,
265
+ version: params.version ?? '1.0.0',
266
+ metadata: params.metadata ?? {},
267
+ createdAt: new Date(),
268
+ updatedAt: new Date(),
269
+ });
270
+ }
@@ -0,0 +1,218 @@
1
+ /**
2
+ * Meta Memory Types
3
+ *
4
+ * Types for meta-learning - learning about learning itself
5
+ * Tracks routing decisions, memory effectiveness, and execution patterns
6
+ */
7
+
8
+ /**
9
+ * MetaObservation - Records for learning about learning
10
+ * Captures what happened during task execution for later analysis
11
+ */
12
+ export interface MetaObservation {
13
+ id: string;
14
+ trajectoryId: string;
15
+
16
+ // === ROUTING DECISION ===
17
+ routing: RoutingObservation;
18
+
19
+ // === MEMORY EFFECTIVENESS ===
20
+ memoryUsage: MemoryUsageObservation;
21
+
22
+ // === EXECUTION PATH ===
23
+ execution: ExecutionObservation;
24
+
25
+ // === OUTCOME ===
26
+ outcome: OutcomeObservation;
27
+
28
+ // === LESSONS (LLM-generated reflection) ===
29
+ lessons: LessonsLearned;
30
+
31
+ timestamp: Date;
32
+ }
33
+
34
+ /**
35
+ * Observation about the routing decision
36
+ */
37
+ export interface RoutingObservation {
38
+ decision: RoutingDecisionType;
39
+ confidence: number;
40
+ reasoning: string;
41
+ }
42
+
43
+ export type RoutingDecisionType = 'adapt' | 'direct' | 'explore' | 'fallback';
44
+
45
+ /**
46
+ * Observation about memory retrieval effectiveness
47
+ */
48
+ export interface MemoryUsageObservation {
49
+ experiencesRetrieved: number;
50
+ playbooksApplied: string[]; // IDs of playbooks used
51
+ retrievalQuality: RetrievalQuality;
52
+ }
53
+
54
+ export type RetrievalQuality = 'helpful' | 'neutral' | 'misleading';
55
+
56
+ /**
57
+ * Observation about the execution path
58
+ */
59
+ export interface ExecutionObservation {
60
+ decompositionUsed: boolean;
61
+ refinementIterations: number;
62
+ backtrackingOccurred: boolean;
63
+ toolsUsed: string[];
64
+ }
65
+
66
+ /**
67
+ * Observation about the outcome
68
+ */
69
+ export interface OutcomeObservation {
70
+ success: boolean;
71
+ quality: OutcomeQuality;
72
+ effortActual: number; // Actual LLM calls
73
+ effortEstimate: number; // What router predicted
74
+ }
75
+
76
+ export type OutcomeQuality = 'excellent' | 'good' | 'acceptable' | 'poor';
77
+
78
+ /**
79
+ * Lessons learned from the execution
80
+ */
81
+ export interface LessonsLearned {
82
+ whatWorked: string[];
83
+ whatFailed: string[];
84
+ suggestions: string[]; // How to do better next time
85
+ }
86
+
87
+ /**
88
+ * MetaStrategy - Learned patterns about learning itself
89
+ * Used to adjust routing and retrieval based on context
90
+ */
91
+ export interface MetaStrategy {
92
+ id: string;
93
+ name: string;
94
+
95
+ // When to apply this meta-strategy
96
+ condition: MetaStrategyCondition;
97
+
98
+ // What to do differently
99
+ adjustment: MetaStrategyAdjustment;
100
+
101
+ // Track effectiveness
102
+ applicationCount: number;
103
+ successRate: number;
104
+
105
+ createdAt: Date;
106
+ updatedAt: Date;
107
+ }
108
+
109
+ /**
110
+ * Conditions for applying a meta-strategy
111
+ */
112
+ export interface MetaStrategyCondition {
113
+ taskCharacteristics: string[]; // "multi-file change", "unfamiliar domain"
114
+ memoryState: string[]; // "no similar experiences", "low confidence"
115
+ }
116
+
117
+ /**
118
+ * Adjustments to make when meta-strategy applies
119
+ */
120
+ export interface MetaStrategyAdjustment {
121
+ routingBias: Partial<Record<RoutingDecisionType, number>>;
122
+ retrievalModification: string; // "expand query", "include adjacent domains"
123
+ executionHint: string; // "prefer decomposition", "verify incrementally"
124
+ }
125
+
126
+ /**
127
+ * Analysis of routing effectiveness
128
+ */
129
+ export interface RoutingAnalysis {
130
+ byDecision: Record<string, { total: number; successful: number }>;
131
+ recommendations: string[];
132
+ }
133
+
134
+ /**
135
+ * Analysis of retrieval quality
136
+ */
137
+ export interface RetrievalAnalysis {
138
+ helpfulPatterns: string[];
139
+ misleadingPatterns: string[];
140
+ recommendations: string[];
141
+ }
142
+
143
+ /**
144
+ * Statistics about meta-learning
145
+ */
146
+ export interface MetaStats {
147
+ totalObservations: number;
148
+ totalStrategies: number;
149
+ observationsByRouting: Record<RoutingDecisionType, number>;
150
+ averageSuccessRate: number;
151
+ recentTrend: 'improving' | 'stable' | 'declining';
152
+ }
153
+
154
+ /**
155
+ * Context for meta-learning queries
156
+ */
157
+ export interface MetaContext {
158
+ taskCharacteristics: string[];
159
+ memoryState: string[];
160
+ }
161
+
162
+ /**
163
+ * Create a new meta-observation with defaults
164
+ */
165
+ export function createMetaObservation(
166
+ input: Partial<MetaObservation> & {
167
+ trajectoryId: string;
168
+ routing: RoutingObservation;
169
+ outcome: OutcomeObservation;
170
+ }
171
+ ): MetaObservation {
172
+ return {
173
+ id: input.id ?? `meta-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`,
174
+ trajectoryId: input.trajectoryId,
175
+ routing: input.routing,
176
+ memoryUsage: input.memoryUsage ?? {
177
+ experiencesRetrieved: 0,
178
+ playbooksApplied: [],
179
+ retrievalQuality: 'neutral',
180
+ },
181
+ execution: input.execution ?? {
182
+ decompositionUsed: false,
183
+ refinementIterations: 0,
184
+ backtrackingOccurred: false,
185
+ toolsUsed: [],
186
+ },
187
+ outcome: input.outcome,
188
+ lessons: input.lessons ?? {
189
+ whatWorked: [],
190
+ whatFailed: [],
191
+ suggestions: [],
192
+ },
193
+ timestamp: input.timestamp ?? new Date(),
194
+ };
195
+ }
196
+
197
+ /**
198
+ * Create a new meta-strategy with defaults
199
+ */
200
+ export function createMetaStrategy(
201
+ input: Partial<MetaStrategy> & {
202
+ name: string;
203
+ condition: MetaStrategyCondition;
204
+ adjustment: MetaStrategyAdjustment;
205
+ }
206
+ ): MetaStrategy {
207
+ const now = new Date();
208
+ return {
209
+ id: input.id ?? `meta-strategy-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`,
210
+ name: input.name,
211
+ condition: input.condition,
212
+ adjustment: input.adjustment,
213
+ applicationCount: input.applicationCount ?? 0,
214
+ successRate: input.successRate ?? 0,
215
+ createdAt: input.createdAt ?? now,
216
+ updatedAt: input.updatedAt ?? now,
217
+ };
218
+ }
@@ -0,0 +1,66 @@
1
+ import { z } from 'zod';
2
+
3
+ /**
4
+ * Outcome - The result of a task attempt
5
+ */
6
+ export const OutcomeSchema = z.object({
7
+ /** Whether the task was completed successfully */
8
+ success: z.boolean(),
9
+ /** Partial progress score (0.0-1.0) */
10
+ partialScore: z.number().min(0).max(1).default(0),
11
+ /** Error information if failed */
12
+ errorInfo: z.string().optional(),
13
+ /** Detailed verification results */
14
+ verificationDetails: z.record(z.unknown()).default({}),
15
+ /** The final solution/output */
16
+ solution: z.unknown().optional(),
17
+ });
18
+
19
+ export type Outcome = z.infer<typeof OutcomeSchema>;
20
+
21
+ /**
22
+ * Create a successful outcome
23
+ */
24
+ export function successOutcome(
25
+ solution: unknown,
26
+ details?: Record<string, unknown>
27
+ ): Outcome {
28
+ return OutcomeSchema.parse({
29
+ success: true,
30
+ partialScore: 1.0,
31
+ solution,
32
+ verificationDetails: details ?? {},
33
+ });
34
+ }
35
+
36
+ /**
37
+ * Create a failed outcome
38
+ */
39
+ export function failureOutcome(
40
+ errorInfo: string,
41
+ partialScore = 0,
42
+ details?: Record<string, unknown>
43
+ ): Outcome {
44
+ return OutcomeSchema.parse({
45
+ success: false,
46
+ partialScore,
47
+ errorInfo,
48
+ verificationDetails: details ?? {},
49
+ });
50
+ }
51
+
52
+ /**
53
+ * Create a partial outcome
54
+ */
55
+ export function partialOutcome(
56
+ partialScore: number,
57
+ solution: unknown,
58
+ details?: Record<string, unknown>
59
+ ): Outcome {
60
+ return OutcomeSchema.parse({
61
+ success: false,
62
+ partialScore,
63
+ solution,
64
+ verificationDetails: details ?? {},
65
+ });
66
+ }