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.
- package/README.md +302 -116
- package/SKILL.md +193 -0
- package/dist/agents/index.d.ts +3 -0
- package/dist/agents/index.d.ts.map +1 -0
- package/dist/agents/index.js +5 -0
- package/dist/agents/index.js.map +1 -0
- package/dist/agents/mock-provider.d.ts +23 -0
- package/dist/agents/mock-provider.d.ts.map +1 -0
- package/dist/agents/mock-provider.js +71 -0
- package/dist/agents/mock-provider.js.map +1 -0
- package/dist/agents/types.d.ts +98 -0
- package/dist/agents/types.d.ts.map +1 -0
- package/dist/agents/types.js +44 -0
- package/dist/agents/types.js.map +1 -0
- package/dist/atlas.d.ts +196 -0
- package/dist/atlas.d.ts.map +1 -0
- package/dist/atlas.js +373 -0
- package/dist/atlas.js.map +1 -0
- package/dist/bin/cognitive-core.d.ts +18 -0
- package/dist/bin/cognitive-core.d.ts.map +1 -0
- package/dist/bin/cognitive-core.js +419 -0
- package/dist/bin/cognitive-core.js.map +1 -0
- package/dist/embeddings/bm25.d.ts +104 -0
- package/dist/embeddings/bm25.d.ts.map +1 -0
- package/dist/embeddings/bm25.js +264 -0
- package/dist/embeddings/bm25.js.map +1 -0
- package/dist/embeddings/index.d.ts +12 -0
- package/dist/embeddings/index.d.ts.map +1 -0
- package/dist/embeddings/index.js +16 -0
- package/dist/embeddings/index.js.map +1 -0
- package/dist/embeddings/manager.d.ts +112 -0
- package/dist/embeddings/manager.d.ts.map +1 -0
- package/dist/embeddings/manager.js +215 -0
- package/dist/embeddings/manager.js.map +1 -0
- package/dist/embeddings/provider.d.ts +101 -0
- package/dist/embeddings/provider.d.ts.map +1 -0
- package/dist/embeddings/provider.js +232 -0
- package/dist/embeddings/provider.js.map +1 -0
- package/dist/embeddings/vector-store.d.ts +101 -0
- package/dist/embeddings/vector-store.d.ts.map +1 -0
- package/dist/embeddings/vector-store.js +256 -0
- package/dist/embeddings/vector-store.js.map +1 -0
- package/dist/factory.d.ts +193 -0
- package/dist/factory.d.ts.map +1 -0
- package/dist/factory.js +109 -0
- package/dist/factory.js.map +1 -0
- package/dist/index.d.ts +30 -453
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +84 -509
- package/dist/index.js.map +1 -0
- package/dist/learning/analyzer.d.ts +110 -0
- package/dist/learning/analyzer.d.ts.map +1 -0
- package/dist/learning/analyzer.js +213 -0
- package/dist/learning/analyzer.js.map +1 -0
- package/dist/learning/effectiveness.d.ts +158 -0
- package/dist/learning/effectiveness.d.ts.map +1 -0
- package/dist/learning/effectiveness.js +251 -0
- package/dist/learning/effectiveness.js.map +1 -0
- package/dist/learning/index.d.ts +8 -0
- package/dist/learning/index.d.ts.map +1 -0
- package/dist/learning/index.js +11 -0
- package/dist/learning/index.js.map +1 -0
- package/dist/learning/llm-extractor.d.ts +88 -0
- package/dist/learning/llm-extractor.d.ts.map +1 -0
- package/dist/learning/llm-extractor.js +372 -0
- package/dist/learning/llm-extractor.js.map +1 -0
- package/dist/learning/meta-learner.d.ts +80 -0
- package/dist/learning/meta-learner.d.ts.map +1 -0
- package/dist/learning/meta-learner.js +355 -0
- package/dist/learning/meta-learner.js.map +1 -0
- package/dist/learning/pipeline.d.ts +65 -0
- package/dist/learning/pipeline.d.ts.map +1 -0
- package/dist/learning/pipeline.js +170 -0
- package/dist/learning/pipeline.js.map +1 -0
- package/dist/learning/playbook-extractor.d.ts +113 -0
- package/dist/learning/playbook-extractor.d.ts.map +1 -0
- package/dist/learning/playbook-extractor.js +523 -0
- package/dist/learning/playbook-extractor.js.map +1 -0
- package/dist/learning/usage-inference.d.ts +82 -0
- package/dist/learning/usage-inference.d.ts.map +1 -0
- package/dist/learning/usage-inference.js +261 -0
- package/dist/learning/usage-inference.js.map +1 -0
- package/dist/mcp/index.d.ts +6 -0
- package/dist/mcp/index.d.ts.map +1 -0
- package/dist/mcp/index.js +6 -0
- package/dist/mcp/index.js.map +1 -0
- package/dist/mcp/playbook-server.d.ts +120 -0
- package/dist/mcp/playbook-server.d.ts.map +1 -0
- package/dist/mcp/playbook-server.js +427 -0
- package/dist/mcp/playbook-server.js.map +1 -0
- package/dist/memory/curated-loader.d.ts +62 -0
- package/dist/memory/curated-loader.d.ts.map +1 -0
- package/dist/memory/curated-loader.js +106 -0
- package/dist/memory/curated-loader.js.map +1 -0
- package/dist/memory/experience.d.ts +122 -0
- package/dist/memory/experience.d.ts.map +1 -0
- package/dist/memory/experience.js +392 -0
- package/dist/memory/experience.js.map +1 -0
- package/dist/memory/index.d.ts +6 -0
- package/dist/memory/index.d.ts.map +1 -0
- package/dist/memory/index.js +9 -0
- package/dist/memory/index.js.map +1 -0
- package/dist/memory/meta.d.ts +90 -0
- package/dist/memory/meta.d.ts.map +1 -0
- package/dist/memory/meta.js +362 -0
- package/dist/memory/meta.js.map +1 -0
- package/dist/memory/playbook.d.ts +133 -0
- package/dist/memory/playbook.d.ts.map +1 -0
- package/dist/memory/playbook.js +357 -0
- package/dist/memory/playbook.js.map +1 -0
- package/dist/memory/system.d.ts +167 -0
- package/dist/memory/system.d.ts.map +1 -0
- package/dist/memory/system.js +383 -0
- package/dist/memory/system.js.map +1 -0
- package/dist/runtime/backends/acp.d.ts +67 -0
- package/dist/runtime/backends/acp.d.ts.map +1 -0
- package/dist/runtime/backends/acp.js +290 -0
- package/dist/runtime/backends/acp.js.map +1 -0
- package/dist/runtime/backends/index.d.ts +5 -0
- package/dist/runtime/backends/index.d.ts.map +1 -0
- package/dist/runtime/backends/index.js +6 -0
- package/dist/runtime/backends/index.js.map +1 -0
- package/dist/runtime/backends/mock.d.ts +67 -0
- package/dist/runtime/backends/mock.d.ts.map +1 -0
- package/dist/runtime/backends/mock.js +153 -0
- package/dist/runtime/backends/mock.js.map +1 -0
- package/dist/runtime/backends/subprocess.d.ts +56 -0
- package/dist/runtime/backends/subprocess.d.ts.map +1 -0
- package/dist/runtime/backends/subprocess.js +260 -0
- package/dist/runtime/backends/subprocess.js.map +1 -0
- package/dist/runtime/flows/learning.d.ts +73 -0
- package/dist/runtime/flows/learning.d.ts.map +1 -0
- package/dist/runtime/flows/learning.js +116 -0
- package/dist/runtime/flows/learning.js.map +1 -0
- package/dist/runtime/flows/validation.d.ts +122 -0
- package/dist/runtime/flows/validation.d.ts.map +1 -0
- package/dist/runtime/flows/validation.js +223 -0
- package/dist/runtime/flows/validation.js.map +1 -0
- package/dist/runtime/index.d.ts +6 -0
- package/dist/runtime/index.d.ts.map +1 -0
- package/dist/runtime/index.js +8 -0
- package/dist/runtime/index.js.map +1 -0
- package/dist/runtime/manager.d.ts +116 -0
- package/dist/runtime/manager.d.ts.map +1 -0
- package/dist/runtime/manager.js +416 -0
- package/dist/runtime/manager.js.map +1 -0
- package/dist/runtime/types.d.ts +138 -0
- package/dist/runtime/types.d.ts.map +1 -0
- package/dist/runtime/types.js +2 -0
- package/dist/runtime/types.js.map +1 -0
- package/dist/search/evaluator.d.ts +102 -0
- package/dist/search/evaluator.d.ts.map +1 -0
- package/dist/search/evaluator.js +352 -0
- package/dist/search/evaluator.js.map +1 -0
- package/dist/search/index.d.ts +7 -0
- package/dist/search/index.d.ts.map +1 -0
- package/dist/search/index.js +11 -0
- package/dist/search/index.js.map +1 -0
- package/dist/search/refinement-loop.d.ts +73 -0
- package/dist/search/refinement-loop.d.ts.map +1 -0
- package/dist/search/refinement-loop.js +245 -0
- package/dist/search/refinement-loop.js.map +1 -0
- package/dist/search/refinement-types.d.ts +154 -0
- package/dist/search/refinement-types.d.ts.map +1 -0
- package/dist/search/refinement-types.js +99 -0
- package/dist/search/refinement-types.js.map +1 -0
- package/dist/search/router.d.ts +61 -0
- package/dist/search/router.d.ts.map +1 -0
- package/dist/search/router.js +197 -0
- package/dist/search/router.js.map +1 -0
- package/dist/search/solver.d.ts +75 -0
- package/dist/search/solver.d.ts.map +1 -0
- package/dist/search/solver.js +216 -0
- package/dist/search/solver.js.map +1 -0
- package/dist/search/verification-runner.d.ts +125 -0
- package/dist/search/verification-runner.d.ts.map +1 -0
- package/dist/search/verification-runner.js +440 -0
- package/dist/search/verification-runner.js.map +1 -0
- package/dist/surfacing/index.d.ts +2 -0
- package/dist/surfacing/index.d.ts.map +1 -0
- package/dist/surfacing/index.js +2 -0
- package/dist/surfacing/index.js.map +1 -0
- package/dist/surfacing/skill-library.d.ts +158 -0
- package/dist/surfacing/skill-library.d.ts.map +1 -0
- package/dist/surfacing/skill-library.js +429 -0
- package/dist/surfacing/skill-library.js.map +1 -0
- package/dist/types/config.d.ts +1113 -0
- package/dist/types/config.d.ts.map +1 -0
- package/dist/types/config.js +274 -0
- package/dist/types/config.js.map +1 -0
- package/dist/types/index.d.ts +9 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +14 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/memory.d.ts +339 -0
- package/dist/types/memory.d.ts.map +1 -0
- package/dist/types/memory.js +207 -0
- package/dist/types/memory.js.map +1 -0
- package/dist/types/meta.d.ts +146 -0
- package/dist/types/meta.d.ts.map +1 -0
- package/dist/types/meta.js +51 -0
- package/dist/types/meta.js.map +1 -0
- package/dist/types/outcome.d.ts +42 -0
- package/dist/types/outcome.d.ts.map +1 -0
- package/dist/types/outcome.js +50 -0
- package/dist/types/outcome.js.map +1 -0
- package/dist/types/playbook.d.ts +119 -0
- package/dist/types/playbook.d.ts.map +1 -0
- package/dist/types/playbook.js +71 -0
- package/dist/types/playbook.js.map +1 -0
- package/dist/types/step.d.ts +44 -0
- package/dist/types/step.d.ts.map +1 -0
- package/dist/types/step.js +32 -0
- package/dist/types/step.js.map +1 -0
- package/dist/types/task.d.ts +91 -0
- package/dist/types/task.d.ts.map +1 -0
- package/dist/types/task.js +39 -0
- package/dist/types/task.js.map +1 -0
- package/dist/types/trajectory.d.ts +221 -0
- package/dist/types/trajectory.d.ts.map +1 -0
- package/dist/types/trajectory.js +60 -0
- package/dist/types/trajectory.js.map +1 -0
- package/dist/utils/index.d.ts +4 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +4 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/similarity.d.ts +31 -0
- package/dist/utils/similarity.d.ts.map +1 -0
- package/dist/utils/similarity.js +107 -0
- package/dist/utils/similarity.js.map +1 -0
- package/dist/utils/storage.d.ts +106 -0
- package/dist/utils/storage.d.ts.map +1 -0
- package/dist/utils/storage.js +203 -0
- package/dist/utils/storage.js.map +1 -0
- package/dist/utils/validation.d.ts +129 -0
- package/dist/utils/validation.d.ts.map +1 -0
- package/dist/utils/validation.js +171 -0
- package/dist/utils/validation.js.map +1 -0
- package/package.json +50 -34
- package/scripts/migrate-to-playbooks.ts +307 -0
- package/src/agents/index.ts +14 -0
- package/src/agents/mock-provider.ts +93 -0
- package/src/agents/types.ts +137 -0
- package/src/atlas.ts +560 -0
- package/src/bin/cognitive-core.ts +470 -0
- package/src/embeddings/bm25.ts +337 -0
- package/src/embeddings/index.ts +39 -0
- package/src/embeddings/manager.ts +288 -0
- package/src/embeddings/provider.ts +311 -0
- package/src/embeddings/vector-store.ts +353 -0
- package/src/factory.ts +263 -0
- package/src/index.ts +246 -0
- package/src/learning/analyzer.ts +335 -0
- package/src/learning/effectiveness.ts +428 -0
- package/src/learning/index.ts +58 -0
- package/src/learning/llm-extractor.ts +542 -0
- package/src/learning/meta-learner.ts +516 -0
- package/src/learning/pipeline.ts +244 -0
- package/src/learning/playbook-extractor.ts +702 -0
- package/src/learning/usage-inference.ts +372 -0
- package/src/mcp/index.ts +12 -0
- package/src/mcp/playbook-server.ts +565 -0
- package/src/memory/curated-loader.ts +160 -0
- package/src/memory/experience.ts +515 -0
- package/src/memory/index.ts +27 -0
- package/src/memory/meta.ts +506 -0
- package/src/memory/playbook.ts +493 -0
- package/src/memory/system.ts +551 -0
- package/src/runtime/backends/acp.ts +378 -0
- package/src/runtime/backends/index.ts +24 -0
- package/src/runtime/backends/mock.ts +218 -0
- package/src/runtime/backends/subprocess.ts +356 -0
- package/src/runtime/flows/learning.ts +183 -0
- package/src/runtime/flows/validation.ts +381 -0
- package/src/runtime/index.ts +53 -0
- package/src/runtime/manager.ts +541 -0
- package/src/runtime/types.ts +157 -0
- package/src/search/evaluator.ts +474 -0
- package/src/search/index.ts +59 -0
- package/src/search/refinement-loop.ts +363 -0
- package/src/search/refinement-types.ts +159 -0
- package/src/search/router.ts +261 -0
- package/src/search/solver.ts +303 -0
- package/src/search/verification-runner.ts +570 -0
- package/src/surfacing/index.ts +6 -0
- package/src/surfacing/skill-library.ts +594 -0
- package/src/types/config.ts +333 -0
- package/src/types/index.ts +130 -0
- package/src/types/memory.ts +270 -0
- package/src/types/meta.ts +218 -0
- package/src/types/outcome.ts +66 -0
- package/src/types/playbook.ts +196 -0
- package/src/types/step.ts +40 -0
- package/src/types/task.ts +52 -0
- package/src/types/trajectory.ts +80 -0
- package/src/utils/index.ts +38 -0
- package/src/utils/similarity.ts +139 -0
- package/src/utils/storage.ts +249 -0
- package/src/utils/validation.ts +286 -0
- package/tests/embeddings/bm25.test.ts +130 -0
- package/tests/embeddings/manager.test.ts +205 -0
- package/tests/integration/atlas.test.ts +266 -0
- package/tests/integration/e2e.test.ts +929 -0
- package/tests/learning/analyzer.test.ts +426 -0
- package/tests/learning/effectiveness.test.ts +542 -0
- package/tests/learning/pipeline.test.ts +176 -0
- package/tests/learning/playbook-extractor-provenance.test.ts +114 -0
- package/tests/learning/usage-inference.test.ts +254 -0
- package/tests/mcp/playbook-server.test.ts +252 -0
- package/tests/memory/experience.test.ts +198 -0
- package/tests/memory/playbook.test.ts +338 -0
- package/tests/memory/provenance.test.ts +639 -0
- package/tests/memory/system.test.ts +325 -0
- package/tests/runtime/agent-manager.test.ts +512 -0
- package/tests/runtime/mock-backend.test.ts +248 -0
- package/tests/search/refinement-loop.test.ts +468 -0
- package/tests/search/refinement.test.ts +267 -0
- package/tests/search/router.test.ts +427 -0
- package/tests/surfacing/skill-library.test.ts +292 -0
- package/tests/types/outcome.test.ts +147 -0
- package/tests/types/step.test.ts +133 -0
- package/tests/types/task.test.ts +158 -0
- package/tests/types/trajectory.test.ts +253 -0
- package/tests/utils/similarity.test.ts +188 -0
- package/tests/utils/validation.test.ts +252 -0
- package/tsconfig.json +25 -0
- package/vitest.config.ts +22 -0
- package/dist/index.d.mts +0 -466
- package/dist/index.mjs +0 -478
|
@@ -0,0 +1,551 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
Experience,
|
|
3
|
+
CodeConcept,
|
|
4
|
+
Strategy,
|
|
5
|
+
Skill,
|
|
6
|
+
Playbook,
|
|
7
|
+
MetaObservation,
|
|
8
|
+
MetaStrategy,
|
|
9
|
+
MetaContext,
|
|
10
|
+
MemoryConfig,
|
|
11
|
+
MemoryCapacityConfig,
|
|
12
|
+
Trajectory,
|
|
13
|
+
} from '../types/index.js';
|
|
14
|
+
import { createExperience } from '../types/index.js';
|
|
15
|
+
import { ExperienceMemory, type PruneResult } from './experience.js';
|
|
16
|
+
import { PlaybookLibrary, type PlaybookMatch } from './playbook.js';
|
|
17
|
+
import { MetaMemory } from './meta.js';
|
|
18
|
+
import {
|
|
19
|
+
LearningEffectivenessTracker,
|
|
20
|
+
type EffectivenessMetrics,
|
|
21
|
+
} from '../learning/effectiveness.js';
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Default capacity configuration
|
|
25
|
+
*/
|
|
26
|
+
const DEFAULT_CAPACITY: MemoryCapacityConfig = {
|
|
27
|
+
maxExperiences: 0,
|
|
28
|
+
maxPlaybooks: 0,
|
|
29
|
+
maxExperienceAgeDays: 0,
|
|
30
|
+
maxFailedExperienceAgeDays: 30,
|
|
31
|
+
autoPrune: true,
|
|
32
|
+
preserveDomainCoverage: true,
|
|
33
|
+
deduplicationThreshold: 0,
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Result from a memory query (legacy format)
|
|
38
|
+
* @deprecated Use MemoryQueryResultV2 for new code
|
|
39
|
+
*/
|
|
40
|
+
export interface MemoryQueryResult {
|
|
41
|
+
experiences: Array<{ experience: Experience; score: number }>;
|
|
42
|
+
/** @deprecated Always returns empty array - use playbooks instead */
|
|
43
|
+
concepts: Array<{ concept: CodeConcept; score: number }>;
|
|
44
|
+
/** @deprecated Always returns empty array - use playbooks instead */
|
|
45
|
+
strategies: Array<{ strategy: Strategy; score: number }>;
|
|
46
|
+
/** @deprecated Always returns empty array - use playbooks instead */
|
|
47
|
+
skills: Array<{ skill: Skill; score: number }>;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Result from a memory query (new format with playbooks)
|
|
52
|
+
*/
|
|
53
|
+
export interface MemoryQueryResultV2 {
|
|
54
|
+
experiences: Array<{ experience: Experience; score: number }>;
|
|
55
|
+
playbooks: PlaybookMatch[];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Unified Memory System
|
|
60
|
+
* Aggregates all memory components with parallel querying
|
|
61
|
+
*/
|
|
62
|
+
export class MemorySystem {
|
|
63
|
+
public readonly experiences: ExperienceMemory;
|
|
64
|
+
public readonly playbooks: PlaybookLibrary;
|
|
65
|
+
public readonly meta: MetaMemory;
|
|
66
|
+
public readonly effectiveness: LearningEffectivenessTracker;
|
|
67
|
+
|
|
68
|
+
private config: MemoryConfig;
|
|
69
|
+
private capacityConfig: MemoryCapacityConfig;
|
|
70
|
+
private initialized = false;
|
|
71
|
+
private lastCapacityCheck = 0;
|
|
72
|
+
private capacityCheckInterval = 60000; // Check every minute
|
|
73
|
+
|
|
74
|
+
constructor(baseDir: string, config?: Partial<MemoryConfig>) {
|
|
75
|
+
this.config = {
|
|
76
|
+
maxExperiences: config?.maxExperiences ?? 4,
|
|
77
|
+
maxStrategies: config?.maxStrategies ?? 3,
|
|
78
|
+
maxConcepts: config?.maxConcepts ?? 5,
|
|
79
|
+
maxSkills: config?.maxSkills ?? 3,
|
|
80
|
+
maxContextTokens: config?.maxContextTokens ?? 4000,
|
|
81
|
+
capacity: config?.capacity ?? DEFAULT_CAPACITY,
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
this.capacityConfig = { ...DEFAULT_CAPACITY, ...config?.capacity };
|
|
85
|
+
|
|
86
|
+
this.experiences = new ExperienceMemory(baseDir);
|
|
87
|
+
this.playbooks = new PlaybookLibrary(baseDir);
|
|
88
|
+
this.meta = new MetaMemory(baseDir);
|
|
89
|
+
this.effectiveness = new LearningEffectivenessTracker(baseDir);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Initialize all memory components
|
|
94
|
+
*/
|
|
95
|
+
async init(): Promise<void> {
|
|
96
|
+
if (this.initialized) return;
|
|
97
|
+
|
|
98
|
+
await Promise.all([
|
|
99
|
+
this.experiences.init(),
|
|
100
|
+
this.playbooks.init(),
|
|
101
|
+
this.meta.init(),
|
|
102
|
+
this.effectiveness.init(),
|
|
103
|
+
]);
|
|
104
|
+
|
|
105
|
+
this.initialized = true;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Query memory (legacy format)
|
|
110
|
+
* @deprecated Use queryV2 for new code - this method returns empty arrays for deprecated fields
|
|
111
|
+
*/
|
|
112
|
+
async query(
|
|
113
|
+
input: string,
|
|
114
|
+
options?: {
|
|
115
|
+
domain?: string;
|
|
116
|
+
includeExperiences?: boolean;
|
|
117
|
+
}
|
|
118
|
+
): Promise<MemoryQueryResult> {
|
|
119
|
+
await this.init();
|
|
120
|
+
|
|
121
|
+
const experiences = await this.experiences.findSimilar(input, {
|
|
122
|
+
k: this.config.maxExperiences,
|
|
123
|
+
domain: options?.domain,
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
// Return empty arrays for deprecated fields
|
|
127
|
+
return {
|
|
128
|
+
experiences,
|
|
129
|
+
concepts: [],
|
|
130
|
+
strategies: [],
|
|
131
|
+
skills: [],
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Store a trajectory and extract experience from it
|
|
137
|
+
* Preserves trajectory structure in metadata for richer learning
|
|
138
|
+
*/
|
|
139
|
+
async storeTrajectory(trajectory: Trajectory): Promise<Experience> {
|
|
140
|
+
await this.init();
|
|
141
|
+
|
|
142
|
+
// Create experience from trajectory
|
|
143
|
+
// Handle undefined solution (e.g., from failed trajectories)
|
|
144
|
+
const solutionOutput = trajectory.outcome.solution !== undefined
|
|
145
|
+
? JSON.stringify(trajectory.outcome.solution)
|
|
146
|
+
: trajectory.outcome.errorInfo ?? 'No solution (task failed)';
|
|
147
|
+
|
|
148
|
+
// Preserve trajectory structure in metadata for learning from intermediate steps
|
|
149
|
+
const trajectoryMetadata: Record<string, unknown> = {
|
|
150
|
+
// Preserve steps with key information (limited to prevent bloat)
|
|
151
|
+
steps: trajectory.steps.map((step, index) => ({
|
|
152
|
+
index,
|
|
153
|
+
thought: step.thought?.slice(0, 500),
|
|
154
|
+
action: step.action?.slice(0, 200),
|
|
155
|
+
observation: step.observation?.slice(0, 300),
|
|
156
|
+
attributionScore: step.attributionScore,
|
|
157
|
+
})),
|
|
158
|
+
// Preserve outcome details
|
|
159
|
+
outcome: {
|
|
160
|
+
partialScore: trajectory.outcome.partialScore,
|
|
161
|
+
errorInfo: trajectory.outcome.errorInfo,
|
|
162
|
+
},
|
|
163
|
+
// Preserve agent and timing info
|
|
164
|
+
agentId: trajectory.agentId,
|
|
165
|
+
wallTimeSeconds: trajectory.wallTimeSeconds,
|
|
166
|
+
llmCalls: trajectory.llmCalls,
|
|
167
|
+
stepCount: trajectory.steps.length,
|
|
168
|
+
// Preserve task context
|
|
169
|
+
taskContext: trajectory.task.context,
|
|
170
|
+
// Original trajectory metadata
|
|
171
|
+
...trajectory.metadata,
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
const experience = createExperience({
|
|
175
|
+
taskInput: trajectory.task.description,
|
|
176
|
+
solutionOutput,
|
|
177
|
+
feedback: trajectory.outcome.success
|
|
178
|
+
? 'Success'
|
|
179
|
+
: trajectory.outcome.errorInfo ?? 'Failed',
|
|
180
|
+
success: trajectory.outcome.success,
|
|
181
|
+
domain: trajectory.task.domain,
|
|
182
|
+
trajectoryId: trajectory.id,
|
|
183
|
+
metadata: trajectoryMetadata,
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
await this.experiences.add(experience);
|
|
187
|
+
|
|
188
|
+
// Check capacity limits
|
|
189
|
+
await this.maybeEnforceCapacity();
|
|
190
|
+
|
|
191
|
+
return experience;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Record usage of an experience
|
|
196
|
+
*/
|
|
197
|
+
async recordExperienceUsage(id: string): Promise<void> {
|
|
198
|
+
await this.init();
|
|
199
|
+
await this.experiences.recordUsage(id);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Query memory with new playbook-based format
|
|
204
|
+
*/
|
|
205
|
+
async queryV2(
|
|
206
|
+
input: string,
|
|
207
|
+
options?: {
|
|
208
|
+
domains?: string[];
|
|
209
|
+
includeExperiences?: boolean;
|
|
210
|
+
includePlaybooks?: boolean;
|
|
211
|
+
minPlaybookConfidence?: number;
|
|
212
|
+
}
|
|
213
|
+
): Promise<MemoryQueryResultV2> {
|
|
214
|
+
await this.init();
|
|
215
|
+
|
|
216
|
+
const includeAll =
|
|
217
|
+
!options ||
|
|
218
|
+
(options.includeExperiences === undefined &&
|
|
219
|
+
options.includePlaybooks === undefined);
|
|
220
|
+
|
|
221
|
+
const [experiences, playbooks] = await Promise.all([
|
|
222
|
+
includeAll || options?.includeExperiences
|
|
223
|
+
? this.experiences.findSimilar(input, {
|
|
224
|
+
k: this.config.maxExperiences,
|
|
225
|
+
domain: options?.domains?.[0],
|
|
226
|
+
})
|
|
227
|
+
: Promise.resolve([]),
|
|
228
|
+
includeAll || options?.includePlaybooks
|
|
229
|
+
? this.playbooks.findMatching(input, {
|
|
230
|
+
k: this.config.maxStrategies + this.config.maxSkills, // Combined count
|
|
231
|
+
domains: options?.domains,
|
|
232
|
+
minConfidence: options?.minPlaybookConfidence,
|
|
233
|
+
excludeAntiPatterns: true,
|
|
234
|
+
})
|
|
235
|
+
: Promise.resolve([]),
|
|
236
|
+
]);
|
|
237
|
+
|
|
238
|
+
return { experiences, playbooks };
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Format memory for context injection (new playbook format)
|
|
243
|
+
*/
|
|
244
|
+
formatForPromptV2(
|
|
245
|
+
result: MemoryQueryResultV2,
|
|
246
|
+
options?: {
|
|
247
|
+
maxTokens?: number;
|
|
248
|
+
includeRefinements?: boolean;
|
|
249
|
+
}
|
|
250
|
+
): string {
|
|
251
|
+
const sections: string[] = [];
|
|
252
|
+
const includeRefinements = options?.includeRefinements ?? true;
|
|
253
|
+
|
|
254
|
+
// Format experiences
|
|
255
|
+
if (result.experiences.length > 0) {
|
|
256
|
+
sections.push('## Similar Experiences\n');
|
|
257
|
+
for (const { experience, score } of result.experiences) {
|
|
258
|
+
sections.push(
|
|
259
|
+
`### Task (${Math.round(score * 100)}% match)`
|
|
260
|
+
);
|
|
261
|
+
sections.push(`${experience.taskInput.slice(0, 200)}`);
|
|
262
|
+
sections.push(`**Approach**: ${experience.solutionOutput.slice(0, 300)}`);
|
|
263
|
+
sections.push(
|
|
264
|
+
`**Outcome**: ${experience.success ? 'Succeeded' : 'Failed'}`
|
|
265
|
+
);
|
|
266
|
+
sections.push('');
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// Format playbooks
|
|
271
|
+
if (result.playbooks.length > 0) {
|
|
272
|
+
sections.push('## Applicable Playbooks\n');
|
|
273
|
+
for (const { playbook, score: _score, matchType } of result.playbooks) {
|
|
274
|
+
sections.push(
|
|
275
|
+
`### ${playbook.name} (${Math.round(playbook.confidence * 100)}% confidence, matched on ${matchType})`
|
|
276
|
+
);
|
|
277
|
+
sections.push(`**Strategy**: ${playbook.guidance.strategy}`);
|
|
278
|
+
|
|
279
|
+
if (playbook.guidance.tactics.length > 0) {
|
|
280
|
+
sections.push('**Tactics**:');
|
|
281
|
+
for (const tactic of playbook.guidance.tactics) {
|
|
282
|
+
sections.push(` - ${tactic}`);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
if (playbook.guidance.steps && playbook.guidance.steps.length > 0) {
|
|
287
|
+
sections.push('**Steps**:');
|
|
288
|
+
for (let i = 0; i < playbook.guidance.steps.length; i++) {
|
|
289
|
+
sections.push(` ${i + 1}. ${playbook.guidance.steps[i]}`);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
if (playbook.verification.successIndicators.length > 0) {
|
|
294
|
+
sections.push(
|
|
295
|
+
`**Success indicators**: ${playbook.verification.successIndicators.join(', ')}`
|
|
296
|
+
);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// Include refinements if requested
|
|
300
|
+
if (includeRefinements && playbook.evolution.refinements.length > 0) {
|
|
301
|
+
sections.push('**Context-specific notes**:');
|
|
302
|
+
for (const refinement of playbook.evolution.refinements.slice(0, 3)) {
|
|
303
|
+
sections.push(` - In ${refinement.context}: ${refinement.addition}`);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
sections.push('');
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
return sections.join('\n');
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
// === META-LEARNING METHODS ===
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* Record a meta-observation after task completion
|
|
318
|
+
*/
|
|
319
|
+
async recordMetaObservation(observation: MetaObservation): Promise<void> {
|
|
320
|
+
await this.init();
|
|
321
|
+
await this.meta.recordObservation(observation);
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* Get meta-learning adjustments for routing
|
|
326
|
+
*/
|
|
327
|
+
async getMetaAdjustments(context: MetaContext): Promise<MetaStrategy[]> {
|
|
328
|
+
await this.init();
|
|
329
|
+
return this.meta.getRoutingAdjustments(context);
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* Add a playbook to the library
|
|
334
|
+
*/
|
|
335
|
+
async addPlaybook(playbook: Playbook): Promise<void> {
|
|
336
|
+
await this.init();
|
|
337
|
+
await this.playbooks.add(playbook);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* Record playbook usage
|
|
342
|
+
*/
|
|
343
|
+
async recordPlaybookUsage(
|
|
344
|
+
id: string,
|
|
345
|
+
trajectoryId: string,
|
|
346
|
+
success: boolean,
|
|
347
|
+
failureContext?: string,
|
|
348
|
+
failureMode?: string
|
|
349
|
+
): Promise<void> {
|
|
350
|
+
await this.init();
|
|
351
|
+
if (success) {
|
|
352
|
+
await this.playbooks.recordSuccess(id, trajectoryId);
|
|
353
|
+
} else {
|
|
354
|
+
await this.playbooks.recordFailure(
|
|
355
|
+
id,
|
|
356
|
+
trajectoryId,
|
|
357
|
+
failureContext ?? 'Unknown context',
|
|
358
|
+
failureMode ?? 'Unknown failure mode'
|
|
359
|
+
);
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* Get statistics about memory contents
|
|
365
|
+
*/
|
|
366
|
+
async getStats(): Promise<{
|
|
367
|
+
experienceCount: number;
|
|
368
|
+
playbookCount: number;
|
|
369
|
+
metaObservationCount: number;
|
|
370
|
+
annotationCount: number;
|
|
371
|
+
}> {
|
|
372
|
+
await this.init();
|
|
373
|
+
|
|
374
|
+
const [experienceCount, playbookCount, metaStats, annotationCount] = await Promise.all([
|
|
375
|
+
this.experiences.count(),
|
|
376
|
+
this.playbooks.count(),
|
|
377
|
+
this.meta.getStats(),
|
|
378
|
+
this.effectiveness.count(),
|
|
379
|
+
]);
|
|
380
|
+
|
|
381
|
+
return {
|
|
382
|
+
experienceCount,
|
|
383
|
+
playbookCount,
|
|
384
|
+
metaObservationCount: metaStats.totalObservations,
|
|
385
|
+
annotationCount,
|
|
386
|
+
};
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* Get learning effectiveness metrics
|
|
391
|
+
*/
|
|
392
|
+
async getEffectivenessMetrics(options?: {
|
|
393
|
+
since?: Date;
|
|
394
|
+
until?: Date;
|
|
395
|
+
}): Promise<EffectivenessMetrics> {
|
|
396
|
+
await this.init();
|
|
397
|
+
return this.effectiveness.computeMetrics(options);
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
// === CAPACITY MANAGEMENT ===
|
|
401
|
+
|
|
402
|
+
/**
|
|
403
|
+
* Check and enforce capacity limits (called automatically if autoPrune is enabled)
|
|
404
|
+
*/
|
|
405
|
+
async enforceCapacityLimits(): Promise<{
|
|
406
|
+
experiencesPruned: PruneResult | null;
|
|
407
|
+
playbooksPruned: number;
|
|
408
|
+
}> {
|
|
409
|
+
await this.init();
|
|
410
|
+
|
|
411
|
+
const result: {
|
|
412
|
+
experiencesPruned: PruneResult | null;
|
|
413
|
+
playbooksPruned: number;
|
|
414
|
+
} = {
|
|
415
|
+
experiencesPruned: null,
|
|
416
|
+
playbooksPruned: 0,
|
|
417
|
+
};
|
|
418
|
+
|
|
419
|
+
const capacity = this.capacityConfig;
|
|
420
|
+
|
|
421
|
+
// Check if we need to prune experiences
|
|
422
|
+
const experienceCount = await this.experiences.count();
|
|
423
|
+
const needsExperiencePrune =
|
|
424
|
+
(capacity.maxExperiences > 0 && experienceCount > capacity.maxExperiences) ||
|
|
425
|
+
capacity.maxExperienceAgeDays > 0 ||
|
|
426
|
+
capacity.maxFailedExperienceAgeDays > 0 ||
|
|
427
|
+
capacity.deduplicationThreshold > 0;
|
|
428
|
+
|
|
429
|
+
if (needsExperiencePrune) {
|
|
430
|
+
result.experiencesPruned = await this.experiences.prune({
|
|
431
|
+
maxCount: capacity.maxExperiences > 0 ? capacity.maxExperiences : undefined,
|
|
432
|
+
maxAgeDays: capacity.maxExperienceAgeDays > 0 ? capacity.maxExperienceAgeDays : undefined,
|
|
433
|
+
removeFailedOlderThanDays:
|
|
434
|
+
capacity.maxFailedExperienceAgeDays > 0
|
|
435
|
+
? capacity.maxFailedExperienceAgeDays
|
|
436
|
+
: undefined,
|
|
437
|
+
similarityThreshold:
|
|
438
|
+
capacity.deduplicationThreshold > 0
|
|
439
|
+
? capacity.deduplicationThreshold
|
|
440
|
+
: undefined,
|
|
441
|
+
preserveDomainCoverage: capacity.preserveDomainCoverage,
|
|
442
|
+
});
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
// Check if we need to prune playbooks
|
|
446
|
+
if (capacity.maxPlaybooks > 0) {
|
|
447
|
+
const playbookCount = await this.playbooks.count();
|
|
448
|
+
if (playbookCount > capacity.maxPlaybooks) {
|
|
449
|
+
// Prune lowest confidence playbooks
|
|
450
|
+
const allPlaybooks = await this.playbooks.getAll();
|
|
451
|
+
const sorted = allPlaybooks.sort((a, b) => b.confidence - a.confidence);
|
|
452
|
+
const toRemove = sorted.slice(capacity.maxPlaybooks);
|
|
453
|
+
|
|
454
|
+
for (const playbook of toRemove) {
|
|
455
|
+
await this.playbooks.delete(playbook.id);
|
|
456
|
+
result.playbooksPruned++;
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
return result;
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
/**
|
|
465
|
+
* Check capacity and prune if necessary (throttled)
|
|
466
|
+
* Called automatically after adding experiences
|
|
467
|
+
*/
|
|
468
|
+
private async maybeEnforceCapacity(): Promise<void> {
|
|
469
|
+
if (!this.capacityConfig.autoPrune) return;
|
|
470
|
+
|
|
471
|
+
const now = Date.now();
|
|
472
|
+
if (now - this.lastCapacityCheck < this.capacityCheckInterval) return;
|
|
473
|
+
|
|
474
|
+
this.lastCapacityCheck = now;
|
|
475
|
+
await this.enforceCapacityLimits();
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
/**
|
|
479
|
+
* Get capacity status
|
|
480
|
+
*/
|
|
481
|
+
async getCapacityStatus(): Promise<{
|
|
482
|
+
experiences: {
|
|
483
|
+
current: number;
|
|
484
|
+
limit: number;
|
|
485
|
+
utilizationPercent: number;
|
|
486
|
+
};
|
|
487
|
+
playbooks: {
|
|
488
|
+
current: number;
|
|
489
|
+
limit: number;
|
|
490
|
+
utilizationPercent: number;
|
|
491
|
+
};
|
|
492
|
+
pruningEnabled: boolean;
|
|
493
|
+
lastCheck: Date | null;
|
|
494
|
+
}> {
|
|
495
|
+
await this.init();
|
|
496
|
+
|
|
497
|
+
const experienceCount = await this.experiences.count();
|
|
498
|
+
const playbookCount = await this.playbooks.count();
|
|
499
|
+
const capacity = this.capacityConfig;
|
|
500
|
+
|
|
501
|
+
return {
|
|
502
|
+
experiences: {
|
|
503
|
+
current: experienceCount,
|
|
504
|
+
limit: capacity.maxExperiences,
|
|
505
|
+
utilizationPercent:
|
|
506
|
+
capacity.maxExperiences > 0
|
|
507
|
+
? Math.round((experienceCount / capacity.maxExperiences) * 100)
|
|
508
|
+
: 0,
|
|
509
|
+
},
|
|
510
|
+
playbooks: {
|
|
511
|
+
current: playbookCount,
|
|
512
|
+
limit: capacity.maxPlaybooks,
|
|
513
|
+
utilizationPercent:
|
|
514
|
+
capacity.maxPlaybooks > 0
|
|
515
|
+
? Math.round((playbookCount / capacity.maxPlaybooks) * 100)
|
|
516
|
+
: 0,
|
|
517
|
+
},
|
|
518
|
+
pruningEnabled: capacity.autoPrune,
|
|
519
|
+
lastCheck: this.lastCapacityCheck > 0 ? new Date(this.lastCapacityCheck) : null,
|
|
520
|
+
};
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
/**
|
|
524
|
+
* Update capacity configuration
|
|
525
|
+
*/
|
|
526
|
+
updateCapacityConfig(config: Partial<MemoryCapacityConfig>): void {
|
|
527
|
+
this.capacityConfig = { ...this.capacityConfig, ...config };
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
/**
|
|
531
|
+
* Close all stores
|
|
532
|
+
*/
|
|
533
|
+
async close(): Promise<void> {
|
|
534
|
+
await Promise.all([
|
|
535
|
+
this.experiences.close(),
|
|
536
|
+
this.playbooks.close(),
|
|
537
|
+
this.meta.close(),
|
|
538
|
+
this.effectiveness.close(),
|
|
539
|
+
]);
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
/**
|
|
544
|
+
* Create a memory system instance
|
|
545
|
+
*/
|
|
546
|
+
export function createMemorySystem(
|
|
547
|
+
baseDir: string,
|
|
548
|
+
config?: Partial<MemoryConfig>
|
|
549
|
+
): MemorySystem {
|
|
550
|
+
return new MemorySystem(baseDir, config);
|
|
551
|
+
}
|