mark-improving-agent 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +335 -0
- package/VERSION +1 -0
- package/bin/cli.js +12 -0
- package/dist/agent/context.js +78 -0
- package/dist/agent/index.js +6 -0
- package/dist/agent/runtime.js +195 -0
- package/dist/agent/task-graph.js +209 -0
- package/dist/agent/types.js +1 -0
- package/dist/cli/index.js +206 -0
- package/dist/core/cognition/active-inference.js +296 -0
- package/dist/core/cognition/cognitive-architecture.js +263 -0
- package/dist/core/cognition/dual-process.js +102 -0
- package/dist/core/cognition/index.js +13 -0
- package/dist/core/cognition/learning-from-failure.js +184 -0
- package/dist/core/cognition/meta-agent.js +407 -0
- package/dist/core/cognition/metacognition.js +322 -0
- package/dist/core/cognition/react.js +177 -0
- package/dist/core/cognition/retrieval-anchor.js +99 -0
- package/dist/core/cognition/self-evolution.js +294 -0
- package/dist/core/cognition/self-verification.js +190 -0
- package/dist/core/cognition/thought-graph.js +495 -0
- package/dist/core/cognition/tool-augmented-llm.js +188 -0
- package/dist/core/cognition/tool-execution-verifier.js +204 -0
- package/dist/core/collaboration/agentic-loop.js +165 -0
- package/dist/core/collaboration/index.js +3 -0
- package/dist/core/collaboration/multi-agent-system.js +186 -0
- package/dist/core/collaboration/multi-agent.js +110 -0
- package/dist/core/consciousness/emotion-engine.js +101 -0
- package/dist/core/consciousness/flow-machine.js +121 -0
- package/dist/core/consciousness/index.js +4 -0
- package/dist/core/consciousness/personality.js +103 -0
- package/dist/core/consciousness/types.js +1 -0
- package/dist/core/emotional-protocol.js +54 -0
- package/dist/core/evolution/engine.js +194 -0
- package/dist/core/evolution/goal-engine.js +153 -0
- package/dist/core/evolution/index.js +6 -0
- package/dist/core/evolution/meta-learning.js +172 -0
- package/dist/core/evolution/reflection.js +158 -0
- package/dist/core/evolution/self-healer.js +139 -0
- package/dist/core/evolution/types.js +1 -0
- package/dist/core/healing-rl.js +266 -0
- package/dist/core/heartbeat.js +408 -0
- package/dist/core/identity/index.js +3 -0
- package/dist/core/identity/reflexion.js +165 -0
- package/dist/core/identity/self-model.js +274 -0
- package/dist/core/identity/self-verifier.js +158 -0
- package/dist/core/identity/types.js +12 -0
- package/dist/core/lesson-bank.js +301 -0
- package/dist/core/memory/adaptive-rag.js +440 -0
- package/dist/core/memory/archive-store.js +187 -0
- package/dist/core/memory/dream-consolidation.js +366 -0
- package/dist/core/memory/embedder.js +130 -0
- package/dist/core/memory/hopfield-network.js +128 -0
- package/dist/core/memory/index.js +9 -0
- package/dist/core/memory/knowledge-graph.js +151 -0
- package/dist/core/memory/spaced-repetition.js +113 -0
- package/dist/core/memory/store.js +404 -0
- package/dist/core/memory/types.js +1 -0
- package/dist/core/psychology/analysis.js +456 -0
- package/dist/core/psychology/index.js +1 -0
- package/dist/core/rollback-manager.js +191 -0
- package/dist/core/security/index.js +1 -0
- package/dist/core/security/privacy.js +132 -0
- package/dist/core/truth-teller.js +253 -0
- package/dist/core/truthfulness.js +99 -0
- package/dist/core/types.js +2 -0
- package/dist/event/bus.js +47 -0
- package/dist/index.js +8 -0
- package/dist/skills/dag.js +181 -0
- package/dist/skills/index.js +5 -0
- package/dist/skills/registry.js +40 -0
- package/dist/skills/types.js +1 -0
- package/dist/storage/archive.js +77 -0
- package/dist/storage/checkpoint.js +119 -0
- package/dist/storage/types.js +1 -0
- package/dist/utils/config.js +81 -0
- package/dist/utils/logger.js +49 -0
- package/dist/version.js +1 -0
- package/package.json +37 -0
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HealingMemoryRL: Reinforcement Learning for Error Recovery
|
|
3
|
+
*
|
|
4
|
+
* Closed-loop system: record → learn → rankedPatches
|
|
5
|
+
* Based on RL principles from v0.13.163 references.
|
|
6
|
+
*
|
|
7
|
+
* Q-table learning with epsilon-greedy strategy selection.
|
|
8
|
+
*/
|
|
9
|
+
import { atomicWriteJSON, readJSON, ensureDir } from '../storage/archive.js';
|
|
10
|
+
const RL_STATE_FILE = 'healing-rl-state.json';
|
|
11
|
+
const MAX_RECENT_FAILURES = 20;
|
|
12
|
+
function getBackoffMs(strategy) {
|
|
13
|
+
switch (strategy) {
|
|
14
|
+
case 'retry': return 1000;
|
|
15
|
+
case 'fallback': return 5000;
|
|
16
|
+
case 'skip': return 0;
|
|
17
|
+
case 'abort': return 0;
|
|
18
|
+
case 'decompose': return 2000;
|
|
19
|
+
case 'simplify': return 1000;
|
|
20
|
+
default: return 1000;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
function allStrategies() {
|
|
24
|
+
return ['retry', 'fallback', 'skip', 'abort', 'decompose', 'simplify'];
|
|
25
|
+
}
|
|
26
|
+
function extractPattern(error) {
|
|
27
|
+
const msg = error.message ?? error.error ?? String(error);
|
|
28
|
+
const msgStr = typeof msg === 'string' ? msg : JSON.stringify(msg);
|
|
29
|
+
// Extract key patterns from error message
|
|
30
|
+
const patterns = [];
|
|
31
|
+
if (msgStr.toLowerCase().includes('null') || msgStr.toLowerCase().includes('undefined')) {
|
|
32
|
+
patterns.push('null-reference');
|
|
33
|
+
}
|
|
34
|
+
if (msgStr.toLowerCase().includes('timeout')) {
|
|
35
|
+
patterns.push('async-timeout');
|
|
36
|
+
}
|
|
37
|
+
if (msgStr.toLowerCase().includes('network') || msgStr.toLowerCase().includes('fetch')) {
|
|
38
|
+
patterns.push('network-error');
|
|
39
|
+
}
|
|
40
|
+
if (msgStr.toLowerCase().includes('type')) {
|
|
41
|
+
patterns.push('type-error');
|
|
42
|
+
}
|
|
43
|
+
if (msgStr.toLowerCase().includes('syntax') || msgStr.toLowerCase().includes('parse')) {
|
|
44
|
+
patterns.push('syntax-error');
|
|
45
|
+
}
|
|
46
|
+
if (msgStr.toLowerCase().includes('permission') || msgStr.toLowerCase().includes('denied')) {
|
|
47
|
+
patterns.push('permission-error');
|
|
48
|
+
}
|
|
49
|
+
if (msgStr.toLowerCase().includes('not found') || msgStr.toLowerCase().includes('404')) {
|
|
50
|
+
patterns.push('not-found');
|
|
51
|
+
}
|
|
52
|
+
if (msgStr.toLowerCase().includes('validation')) {
|
|
53
|
+
patterns.push('validation-error');
|
|
54
|
+
}
|
|
55
|
+
if (msgStr.toLowerCase().includes('logic') || msgStr.toLowerCase().includes('assertion')) {
|
|
56
|
+
patterns.push('logic-error');
|
|
57
|
+
}
|
|
58
|
+
if (msgStr.toLowerCase().includes('memory') || msgStr.toLowerCase().includes('heap')) {
|
|
59
|
+
patterns.push('memory-error');
|
|
60
|
+
}
|
|
61
|
+
return patterns.length > 0 ? patterns.join('+') : 'generic-error';
|
|
62
|
+
}
|
|
63
|
+
function generateHints(pattern, strategy) {
|
|
64
|
+
const hints = [];
|
|
65
|
+
switch (strategy) {
|
|
66
|
+
case 'retry':
|
|
67
|
+
hints.push('Retry the operation with exponential backoff');
|
|
68
|
+
hints.push('Check if state was modified during failure');
|
|
69
|
+
break;
|
|
70
|
+
case 'fallback':
|
|
71
|
+
hints.push('Use alternative approach or cached data');
|
|
72
|
+
hints.push('Fallback to degraded but functional mode');
|
|
73
|
+
break;
|
|
74
|
+
case 'skip':
|
|
75
|
+
hints.push('Skip non-critical step and continue');
|
|
76
|
+
hints.push('Mark as optional and proceed');
|
|
77
|
+
break;
|
|
78
|
+
case 'abort':
|
|
79
|
+
hints.push('Stop operation and report error');
|
|
80
|
+
hints.push('Clean up partial state before aborting');
|
|
81
|
+
break;
|
|
82
|
+
case 'decompose':
|
|
83
|
+
hints.push('Break complex operation into smaller steps');
|
|
84
|
+
hints.push('Handle each step independently with error recovery');
|
|
85
|
+
break;
|
|
86
|
+
case 'simplify':
|
|
87
|
+
hints.push('Reduce complexity of the operation');
|
|
88
|
+
hints.push('Use simpler data structures or algorithms');
|
|
89
|
+
break;
|
|
90
|
+
}
|
|
91
|
+
// Add pattern-specific hints
|
|
92
|
+
if (pattern.includes('null-reference')) {
|
|
93
|
+
hints.push('Add null/undefined guards before property access');
|
|
94
|
+
hints.push('Validate object initialization');
|
|
95
|
+
}
|
|
96
|
+
if (pattern.includes('timeout')) {
|
|
97
|
+
hints.push('Increase timeout threshold');
|
|
98
|
+
hints.push('Implement request batching for large operations');
|
|
99
|
+
}
|
|
100
|
+
if (pattern.includes('network')) {
|
|
101
|
+
hints.push('Check network connectivity and endpoints');
|
|
102
|
+
hints.push('Implement retry with exponential backoff');
|
|
103
|
+
}
|
|
104
|
+
return hints.slice(0, 3);
|
|
105
|
+
}
|
|
106
|
+
export function createHealingMemoryRL(dataDir) {
|
|
107
|
+
const state = {
|
|
108
|
+
qTable: new Map(),
|
|
109
|
+
recentFailures: [],
|
|
110
|
+
totalRepairs: 0,
|
|
111
|
+
lastUpdated: Date.now(),
|
|
112
|
+
};
|
|
113
|
+
const statePath = `${dataDir}/identity/${RL_STATE_FILE}`;
|
|
114
|
+
function getQ(pattern, strategy) {
|
|
115
|
+
const patternMap = state.qTable.get(pattern);
|
|
116
|
+
if (!patternMap)
|
|
117
|
+
return 0.5; // Default Q value
|
|
118
|
+
return patternMap.get(strategy) ?? 0.5;
|
|
119
|
+
}
|
|
120
|
+
function setQ(pattern, strategy, value) {
|
|
121
|
+
if (!state.qTable.has(pattern)) {
|
|
122
|
+
state.qTable.set(pattern, new Map());
|
|
123
|
+
}
|
|
124
|
+
// Clamp Q values to [0, 1]
|
|
125
|
+
const clamped = Math.max(0, Math.min(1, value));
|
|
126
|
+
state.qTable.get(pattern).set(strategy, clamped);
|
|
127
|
+
}
|
|
128
|
+
function updateQ(pattern, strategy, reward) {
|
|
129
|
+
const currentQ = getQ(pattern, strategy);
|
|
130
|
+
const learningRate = 0.1;
|
|
131
|
+
// Q[pattern][strategy] += alpha * (reward - Q[pattern][strategy])
|
|
132
|
+
const newQ = currentQ + learningRate * (reward - currentQ);
|
|
133
|
+
setQ(pattern, strategy, newQ);
|
|
134
|
+
}
|
|
135
|
+
function selectStrategy(pattern) {
|
|
136
|
+
const epsilon = 0.1; // 10% random exploration
|
|
137
|
+
if (Math.random() < epsilon) {
|
|
138
|
+
// Random exploration
|
|
139
|
+
const strategies = allStrategies();
|
|
140
|
+
const randomIdx = Math.floor(Math.random() * strategies.length);
|
|
141
|
+
const randomStrategy = strategies[randomIdx];
|
|
142
|
+
return { strategy: randomStrategy, qValue: getQ(pattern, randomStrategy) };
|
|
143
|
+
}
|
|
144
|
+
// Exploitation: pick best strategy
|
|
145
|
+
let bestStrategy = 'retry';
|
|
146
|
+
let bestQ = -Infinity;
|
|
147
|
+
for (const strategy of allStrategies()) {
|
|
148
|
+
const q = getQ(pattern, strategy);
|
|
149
|
+
if (q > bestQ) {
|
|
150
|
+
bestQ = q;
|
|
151
|
+
bestStrategy = strategy;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return { strategy: bestStrategy, qValue: bestQ };
|
|
155
|
+
}
|
|
156
|
+
return {
|
|
157
|
+
record(pattern, strategy, outcome) {
|
|
158
|
+
const reward = outcome === 'success' ? 1.0 : 0.0;
|
|
159
|
+
updateQ(pattern, strategy, reward);
|
|
160
|
+
state.recentFailures.push({
|
|
161
|
+
pattern,
|
|
162
|
+
strategy,
|
|
163
|
+
outcome,
|
|
164
|
+
timestamp: Date.now(),
|
|
165
|
+
});
|
|
166
|
+
if (state.recentFailures.length > MAX_RECENT_FAILURES) {
|
|
167
|
+
state.recentFailures = state.recentFailures.slice(-MAX_RECENT_FAILURES);
|
|
168
|
+
}
|
|
169
|
+
state.totalRepairs++;
|
|
170
|
+
state.lastUpdated = Date.now();
|
|
171
|
+
},
|
|
172
|
+
learn(input) {
|
|
173
|
+
// Analyze input string, extract key patterns, generate ranked hint patches
|
|
174
|
+
const patterns = [];
|
|
175
|
+
const input_lower = input.toLowerCase();
|
|
176
|
+
// Extract pattern keywords
|
|
177
|
+
const keywords = [
|
|
178
|
+
'null', 'undefined', 'timeout', 'network', 'fetch',
|
|
179
|
+
'type', 'syntax', 'parse', 'permission', 'denied',
|
|
180
|
+
'not found', '404', 'validation', 'logic', 'assertion',
|
|
181
|
+
'memory', 'heap', 'deadlock', 'race'
|
|
182
|
+
];
|
|
183
|
+
for (const kw of keywords) {
|
|
184
|
+
if (input_lower.includes(kw)) {
|
|
185
|
+
const pattern = kw === 'null' || kw === 'undefined' ? 'null-reference' :
|
|
186
|
+
kw === 'not found' ? 'not-found' :
|
|
187
|
+
kw === 'permission' || kw === 'denied' ? 'permission-error' :
|
|
188
|
+
`${kw}-error`;
|
|
189
|
+
patterns.push(pattern);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
if (patterns.length === 0) {
|
|
193
|
+
patterns.push('generic-error');
|
|
194
|
+
}
|
|
195
|
+
// Generate ranked hints based on best strategies for each pattern
|
|
196
|
+
const hints = [];
|
|
197
|
+
const seen = new Set();
|
|
198
|
+
for (const pattern of patterns) {
|
|
199
|
+
const { strategy, qValue } = selectStrategy(pattern);
|
|
200
|
+
const strategyHints = generateHints(pattern, strategy);
|
|
201
|
+
for (const hint of strategyHints) {
|
|
202
|
+
if (!seen.has(hint)) {
|
|
203
|
+
hints.push(`[${pattern}] Q=${qValue.toFixed(2)}: ${hint}`);
|
|
204
|
+
seen.add(hint);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
return hints.slice(0, 3);
|
|
209
|
+
},
|
|
210
|
+
heal(error) {
|
|
211
|
+
const pattern = extractPattern(error);
|
|
212
|
+
const { strategy, qValue } = selectStrategy(pattern);
|
|
213
|
+
const hints = generateHints(pattern, strategy);
|
|
214
|
+
const backoffMs = getBackoffMs(strategy);
|
|
215
|
+
return {
|
|
216
|
+
ok: false,
|
|
217
|
+
strategy,
|
|
218
|
+
hints,
|
|
219
|
+
backoffMs,
|
|
220
|
+
attempt: 1,
|
|
221
|
+
qValue,
|
|
222
|
+
};
|
|
223
|
+
},
|
|
224
|
+
getStats() {
|
|
225
|
+
const recent = state.recentFailures.slice(-10);
|
|
226
|
+
const successes = recent.filter(f => f.outcome === 'success').length;
|
|
227
|
+
const successRate = recent.length > 0 ? successes / recent.length : 0;
|
|
228
|
+
return {
|
|
229
|
+
totalRepairs: state.totalRepairs,
|
|
230
|
+
successRate,
|
|
231
|
+
recentPatterns: recent.slice(-10).map(f => f.pattern),
|
|
232
|
+
};
|
|
233
|
+
},
|
|
234
|
+
async persist() {
|
|
235
|
+
// Serialize Q-table as plain object for JSON
|
|
236
|
+
const qTableObj = {};
|
|
237
|
+
for (const [pattern, strategyMap] of state.qTable) {
|
|
238
|
+
qTableObj[pattern] = Object.fromEntries(strategyMap);
|
|
239
|
+
}
|
|
240
|
+
await atomicWriteJSON(statePath, {
|
|
241
|
+
qTable: qTableObj,
|
|
242
|
+
recentFailures: state.recentFailures,
|
|
243
|
+
totalRepairs: state.totalRepairs,
|
|
244
|
+
lastUpdated: state.lastUpdated,
|
|
245
|
+
});
|
|
246
|
+
},
|
|
247
|
+
async boot() {
|
|
248
|
+
await ensureDir(`${dataDir}/identity`);
|
|
249
|
+
const loaded = await readJSON(statePath, {});
|
|
250
|
+
if (loaded?.qTable) {
|
|
251
|
+
for (const [pattern, strategyMap] of Object.entries(loaded.qTable)) {
|
|
252
|
+
state.qTable.set(pattern, new Map(Object.entries(strategyMap)));
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
if (loaded?.recentFailures && Array.isArray(loaded.recentFailures)) {
|
|
256
|
+
state.recentFailures = loaded.recentFailures.slice(-MAX_RECENT_FAILURES);
|
|
257
|
+
}
|
|
258
|
+
if (typeof loaded?.totalRepairs === 'number') {
|
|
259
|
+
state.totalRepairs = loaded.totalRepairs;
|
|
260
|
+
}
|
|
261
|
+
if (loaded?.lastUpdated) {
|
|
262
|
+
state.lastUpdated = loaded.lastUpdated;
|
|
263
|
+
}
|
|
264
|
+
},
|
|
265
|
+
};
|
|
266
|
+
}
|
|
@@ -0,0 +1,408 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HeartFlow - Self-evolving AI agent identity framework
|
|
3
|
+
*
|
|
4
|
+
* The main HeartFlow class orchestrates all subsystems:
|
|
5
|
+
* - 3-tier memory (core, learned, ephemeral, archived)
|
|
6
|
+
* - Self-evolution engine with goal management
|
|
7
|
+
* - Identity self-model with drift detection
|
|
8
|
+
* - Consciousness (emotion, flow, personality)
|
|
9
|
+
* - Skill DAG execution
|
|
10
|
+
* - Task graph management
|
|
11
|
+
* - Checkpointing for persistence
|
|
12
|
+
*/
|
|
13
|
+
import { VERSION } from '../version.js';
|
|
14
|
+
import { createLogger } from '../utils/logger.js';
|
|
15
|
+
import { loadConfig } from '../utils/config.js';
|
|
16
|
+
import { ensureDir } from '../storage/archive.js';
|
|
17
|
+
import { createCheckpointManager } from '../storage/checkpoint.js';
|
|
18
|
+
import { events } from '../event/bus.js';
|
|
19
|
+
// Memory subsystem
|
|
20
|
+
import { createMemoryStore } from './memory/store.js';
|
|
21
|
+
import { DEFAULT_EMBEDDER } from './memory/embedder.js';
|
|
22
|
+
import { createArchiveStore } from './memory/archive-store.js';
|
|
23
|
+
import { createDreamEngine } from './memory/dream-consolidation.js';
|
|
24
|
+
import { createSpacedRepetition } from './memory/spaced-repetition.js';
|
|
25
|
+
// Evolution subsystem
|
|
26
|
+
import { createGoalEngine } from './evolution/goal-engine.js';
|
|
27
|
+
import { createMetaLearner } from './evolution/meta-learning.js';
|
|
28
|
+
import { createEvolutionEngine } from './evolution/engine.js';
|
|
29
|
+
// Identity subsystem
|
|
30
|
+
import { createSelfModelManager } from './identity/self-model.js';
|
|
31
|
+
import { createReflexion } from './identity/reflexion.js';
|
|
32
|
+
import { createSelfVerifier } from './identity/self-verifier.js';
|
|
33
|
+
// Healing RL subsystem
|
|
34
|
+
import { createHealingMemoryRL } from './healing-rl.js';
|
|
35
|
+
import { createRollbackManager } from './rollback-manager.js';
|
|
36
|
+
// Lesson Bank subsystem
|
|
37
|
+
import { createLessonBank } from './lesson-bank.js';
|
|
38
|
+
// Emotional Protocol subsystem
|
|
39
|
+
import { createEmotionalProtocol } from './emotional-protocol.js';
|
|
40
|
+
// Truthfulness subsystem
|
|
41
|
+
import { createTruthfulnessChecker } from './truthfulness.js';
|
|
42
|
+
// Consciousness subsystem
|
|
43
|
+
import { createEmotionEngine } from './consciousness/emotion-engine.js';
|
|
44
|
+
import { createFlowMachine } from './consciousness/flow-machine.js';
|
|
45
|
+
import { createPersonalityEngine } from './consciousness/personality.js';
|
|
46
|
+
// Skills subsystem
|
|
47
|
+
import { createSkillRegistry } from '../skills/registry.js';
|
|
48
|
+
import { createSkillDAG } from '../skills/dag.js';
|
|
49
|
+
// Agent subsystem
|
|
50
|
+
import { createTaskGraph } from '../agent/task-graph.js';
|
|
51
|
+
import { createContextManager } from '../agent/context.js';
|
|
52
|
+
function createHeartFlowInstance(deps, config, logger) {
|
|
53
|
+
const state = {
|
|
54
|
+
booted: false,
|
|
55
|
+
shutdown: false,
|
|
56
|
+
startTime: Date.now(),
|
|
57
|
+
evolutionCycles: 0,
|
|
58
|
+
};
|
|
59
|
+
// -------------------- Memory --------------------
|
|
60
|
+
function store(content, importance = 0.5, tags = []) {
|
|
61
|
+
const entry = deps.memoryStore.store(content, importance, tags, 'learned', 'system');
|
|
62
|
+
events.emit('memory:store', { id: entry.id, content: entry.content, importance: entry.importance, tier: entry.tier });
|
|
63
|
+
return entry;
|
|
64
|
+
}
|
|
65
|
+
function recall(query) {
|
|
66
|
+
const results = [];
|
|
67
|
+
// Use search with the query
|
|
68
|
+
const searchResults = deps.memoryStore.search(query.query, query.limit ?? 20);
|
|
69
|
+
for (const result of searchResults) {
|
|
70
|
+
// Apply tier filter
|
|
71
|
+
if (query.tier && result.entry.tier !== query.tier)
|
|
72
|
+
continue;
|
|
73
|
+
if (query.minImportance !== undefined && result.entry.importance < query.minImportance)
|
|
74
|
+
continue;
|
|
75
|
+
if (query.tags && query.tags.length > 0 && !query.tags.some(t => result.entry.tags.includes(t)))
|
|
76
|
+
continue;
|
|
77
|
+
results.push(result);
|
|
78
|
+
}
|
|
79
|
+
return results;
|
|
80
|
+
}
|
|
81
|
+
function recallById(id) {
|
|
82
|
+
return deps.memoryStore.recall(id);
|
|
83
|
+
}
|
|
84
|
+
function search(query, limit = 20) {
|
|
85
|
+
return deps.memoryStore.search(query, limit);
|
|
86
|
+
}
|
|
87
|
+
async function dream() {
|
|
88
|
+
return await deps.dreamEngine.dream([]);
|
|
89
|
+
}
|
|
90
|
+
function getDreamStats() {
|
|
91
|
+
return deps.dreamEngine.getDreamStats();
|
|
92
|
+
}
|
|
93
|
+
function getMemoryHealth() {
|
|
94
|
+
return deps.dreamEngine.getMemoryHealth();
|
|
95
|
+
}
|
|
96
|
+
// -------------------- Evolution --------------------
|
|
97
|
+
async function evolve(input) {
|
|
98
|
+
const cycle = await deps.evolutionEngine.evolve(input);
|
|
99
|
+
state.evolutionCycles++;
|
|
100
|
+
// After learning, call reflexion if outcome was failure
|
|
101
|
+
// The evolution engine stores outcome in cycle.outcomes
|
|
102
|
+
if (cycle.outcomes && cycle.outcomes.length > 0) {
|
|
103
|
+
const lastOutcome = cycle.outcomes[cycle.outcomes.length - 1];
|
|
104
|
+
if (lastOutcome.toLowerCase().includes('fail') || lastOutcome.toLowerCase().includes('error')) {
|
|
105
|
+
deps.reflexion.reflect({ task: input, outcome: 'failure', error: lastOutcome });
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
events.emit('evolution:cycle', cycle);
|
|
109
|
+
return cycle;
|
|
110
|
+
}
|
|
111
|
+
function getGrowthMetrics() {
|
|
112
|
+
return deps.selfModel.getSelfModel().growthMetrics;
|
|
113
|
+
}
|
|
114
|
+
// -------------------- Identity --------------------
|
|
115
|
+
function getSelfModel() {
|
|
116
|
+
return deps.selfModel.getSelfModel();
|
|
117
|
+
}
|
|
118
|
+
function detectDrift() {
|
|
119
|
+
return deps.selfModel.detectDrift();
|
|
120
|
+
}
|
|
121
|
+
function getIdentityCore() {
|
|
122
|
+
return deps.selfModel.getIdentityCore();
|
|
123
|
+
}
|
|
124
|
+
function recordInstall() {
|
|
125
|
+
deps.selfModel.recordInstall();
|
|
126
|
+
}
|
|
127
|
+
// -------------------- Reflexion & Self-Verification --------------------
|
|
128
|
+
function reflect(task, outcome, error) {
|
|
129
|
+
return deps.reflexion.reflect({ task, outcome, error });
|
|
130
|
+
}
|
|
131
|
+
function verify(reasoning, conclusion) {
|
|
132
|
+
const result = deps.selfVerifier.verify(reasoning, conclusion);
|
|
133
|
+
deps.selfVerifier.recordVerification(result);
|
|
134
|
+
return result;
|
|
135
|
+
}
|
|
136
|
+
function getReflexionStats() {
|
|
137
|
+
return deps.reflexion.getStats();
|
|
138
|
+
}
|
|
139
|
+
// -------------------- Healing RL --------------------
|
|
140
|
+
function heal(error) {
|
|
141
|
+
return deps.healingRL.heal(error);
|
|
142
|
+
}
|
|
143
|
+
// -------------------- Consciousness --------------------
|
|
144
|
+
function getConsciousnessState() {
|
|
145
|
+
const emotion = deps.emotionEngine.getCurrentEmotion();
|
|
146
|
+
const flow = deps.flowMachine.getState();
|
|
147
|
+
return {
|
|
148
|
+
emotion,
|
|
149
|
+
flow,
|
|
150
|
+
focus: deps.flowMachine.getFocusScore(),
|
|
151
|
+
energy: 0.5, // Placeholder
|
|
152
|
+
lastActivity: Date.now(),
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
function getPersonality() {
|
|
156
|
+
return deps.selfModel.getSelfModel().personality;
|
|
157
|
+
}
|
|
158
|
+
// -------------------- Lesson Bank --------------------
|
|
159
|
+
function checkLesson(input) {
|
|
160
|
+
return deps.lessonBank.checkPattern(input);
|
|
161
|
+
}
|
|
162
|
+
function recordLesson(lesson) {
|
|
163
|
+
return deps.lessonBank.addLesson(lesson);
|
|
164
|
+
}
|
|
165
|
+
function getLessonsBySkill(skill) {
|
|
166
|
+
return deps.lessonBank.getBySkill(skill);
|
|
167
|
+
}
|
|
168
|
+
function getTopLessons(limit) {
|
|
169
|
+
return deps.lessonBank.getTopLessons(limit);
|
|
170
|
+
}
|
|
171
|
+
// -------------------- Emotional Protocol --------------------
|
|
172
|
+
function processWithEmotion(input) {
|
|
173
|
+
return deps.emotionalProtocol.process(input);
|
|
174
|
+
}
|
|
175
|
+
// -------------------- Truthfulness --------------------
|
|
176
|
+
function checkTruthfulness(statement) {
|
|
177
|
+
return deps.truthfulnessChecker.checkStatement(statement);
|
|
178
|
+
}
|
|
179
|
+
function annotateStatement(statement, confidence) {
|
|
180
|
+
return deps.truthfulnessChecker.annotateConfidence(statement, confidence);
|
|
181
|
+
}
|
|
182
|
+
// -------------------- Skills --------------------
|
|
183
|
+
function registerSkill(skill) {
|
|
184
|
+
deps.skillRegistry.register(skill);
|
|
185
|
+
try {
|
|
186
|
+
deps.skillDAG.addSkill(skill);
|
|
187
|
+
}
|
|
188
|
+
catch {
|
|
189
|
+
deps.skillRegistry.unregister(skill.id);
|
|
190
|
+
throw new Error(`Failed to add skill ${skill.id} to DAG: cycle detected`);
|
|
191
|
+
}
|
|
192
|
+
events.emit('skill:registered', { id: skill.id, name: skill.name });
|
|
193
|
+
}
|
|
194
|
+
function listSkills() {
|
|
195
|
+
return deps.skillRegistry.list();
|
|
196
|
+
}
|
|
197
|
+
// -------------------- Checkpointing --------------------
|
|
198
|
+
async function checkpoint(label) {
|
|
199
|
+
const id = await deps.checkpointManager.save(label);
|
|
200
|
+
events.emit('checkpoint:created', { id, label });
|
|
201
|
+
return id;
|
|
202
|
+
}
|
|
203
|
+
async function restore(id) {
|
|
204
|
+
// Note: Full restore would reload all subsystem state
|
|
205
|
+
// This is a simplified version
|
|
206
|
+
const loaded = await deps.checkpointManager.load(id);
|
|
207
|
+
if (!loaded) {
|
|
208
|
+
throw new Error(`Checkpoint ${id} not found`);
|
|
209
|
+
}
|
|
210
|
+
events.emit('checkpoint:restored', { id });
|
|
211
|
+
}
|
|
212
|
+
function getRollbackStats() {
|
|
213
|
+
return deps.rollbackManager.getStats();
|
|
214
|
+
}
|
|
215
|
+
// -------------------- Stats --------------------
|
|
216
|
+
function getStats() {
|
|
217
|
+
const uptime = Date.now() - state.startTime;
|
|
218
|
+
const latestCheckpoint = deps.checkpointManager.getLatest();
|
|
219
|
+
return {
|
|
220
|
+
version: VERSION,
|
|
221
|
+
uptime,
|
|
222
|
+
memory: {
|
|
223
|
+
core: 0, // Placeholder - would track actual counts
|
|
224
|
+
learned: 0,
|
|
225
|
+
ephemeral: 0,
|
|
226
|
+
archived: 0,
|
|
227
|
+
},
|
|
228
|
+
evolution: {
|
|
229
|
+
cycles: state.evolutionCycles,
|
|
230
|
+
metrics: getGrowthMetrics(),
|
|
231
|
+
},
|
|
232
|
+
consciousness: getConsciousnessState(),
|
|
233
|
+
checkpoint: {
|
|
234
|
+
count: 0,
|
|
235
|
+
latest: null,
|
|
236
|
+
},
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
// -------------------- Boot / Shutdown --------------------
|
|
240
|
+
async function boot() {
|
|
241
|
+
if (state.booted) {
|
|
242
|
+
logger.warn('mark-improving-agent already booted');
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
logger.info('Booting mark-improving-agent...');
|
|
246
|
+
// Ensure data directories exist
|
|
247
|
+
await ensureDir(config.dataDir);
|
|
248
|
+
await ensureDir(`${config.dataDir}/checkpoints`);
|
|
249
|
+
await ensureDir(`${config.dataDir}/memory`);
|
|
250
|
+
await ensureDir(`${config.dataDir}/archive`);
|
|
251
|
+
// Boot memory store first
|
|
252
|
+
await deps.memoryStore.boot();
|
|
253
|
+
logger.debug('Memory store booted');
|
|
254
|
+
// Boot subsystems in dependency order
|
|
255
|
+
await deps.selfModel.boot();
|
|
256
|
+
logger.debug('Self-model booted');
|
|
257
|
+
await deps.emotionEngine.boot();
|
|
258
|
+
logger.debug('Emotion engine booted');
|
|
259
|
+
await deps.personalityEngine.boot();
|
|
260
|
+
logger.debug('Personality engine booted');
|
|
261
|
+
// Boot goal engine
|
|
262
|
+
deps.goalEngine.persist().catch(() => { }); // Ensure goals are loaded
|
|
263
|
+
logger.debug('Goal engine booted');
|
|
264
|
+
// Boot meta learner
|
|
265
|
+
deps.metaLearner.boot().catch(() => { });
|
|
266
|
+
logger.debug('Meta-learner booted');
|
|
267
|
+
// Boot evolution engine
|
|
268
|
+
await deps.evolutionEngine.boot();
|
|
269
|
+
logger.debug('Evolution engine booted');
|
|
270
|
+
// Boot new identity subsystems
|
|
271
|
+
await deps.reflexion.boot();
|
|
272
|
+
logger.debug('Reflexion booted');
|
|
273
|
+
await deps.selfVerifier.boot();
|
|
274
|
+
logger.debug('Self-verifier booted');
|
|
275
|
+
await deps.healingRL.boot();
|
|
276
|
+
logger.debug('Healing RL booted');
|
|
277
|
+
await deps.lessonBank.boot();
|
|
278
|
+
logger.debug('Lesson bank booted');
|
|
279
|
+
// truthfulnessChecker boots itself automatically
|
|
280
|
+
logger.debug('Truthfulness checker ready');
|
|
281
|
+
// Boot archive store
|
|
282
|
+
// Note: archiveStore.boot() if it exists
|
|
283
|
+
state.booted = true;
|
|
284
|
+
events.emit('system:booted', {});
|
|
285
|
+
logger.info('mark-improving-agent booted successfully');
|
|
286
|
+
}
|
|
287
|
+
async function shutdown() {
|
|
288
|
+
if (state.shutdown) {
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
logger.info('Shutting down mark-improving-agent...');
|
|
292
|
+
state.shutdown = true;
|
|
293
|
+
// Persist all subsystem state
|
|
294
|
+
await deps.selfModel.persist();
|
|
295
|
+
await deps.emotionEngine.persist();
|
|
296
|
+
await deps.personalityEngine.persist();
|
|
297
|
+
await deps.selfVerifier.persist();
|
|
298
|
+
await deps.healingRL.persist();
|
|
299
|
+
await deps.lessonBank.persist();
|
|
300
|
+
events.emit('system:shutdown', {});
|
|
301
|
+
logger.info('mark-improving-agent shutdown complete');
|
|
302
|
+
}
|
|
303
|
+
return {
|
|
304
|
+
boot,
|
|
305
|
+
shutdown,
|
|
306
|
+
store,
|
|
307
|
+
recall,
|
|
308
|
+
recallById,
|
|
309
|
+
search,
|
|
310
|
+
evolve,
|
|
311
|
+
getGrowthMetrics,
|
|
312
|
+
getSelfModel,
|
|
313
|
+
detectDrift,
|
|
314
|
+
getIdentityCore,
|
|
315
|
+
recordInstall,
|
|
316
|
+
reflect,
|
|
317
|
+
verify,
|
|
318
|
+
getReflexionStats,
|
|
319
|
+
heal,
|
|
320
|
+
getConsciousnessState,
|
|
321
|
+
getPersonality,
|
|
322
|
+
registerSkill,
|
|
323
|
+
listSkills,
|
|
324
|
+
checkpoint,
|
|
325
|
+
restore,
|
|
326
|
+
getRollbackStats,
|
|
327
|
+
getStats,
|
|
328
|
+
dream,
|
|
329
|
+
getDreamStats,
|
|
330
|
+
getMemoryHealth,
|
|
331
|
+
checkLesson,
|
|
332
|
+
recordLesson,
|
|
333
|
+
getLessonsBySkill,
|
|
334
|
+
getTopLessons,
|
|
335
|
+
processWithEmotion,
|
|
336
|
+
checkTruthfulness,
|
|
337
|
+
annotateStatement,
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
export async function createHeartFlow(options) {
|
|
341
|
+
const config = loadConfig();
|
|
342
|
+
// Override with options if provided
|
|
343
|
+
if (options?.dataDir) {
|
|
344
|
+
config.dataDir = options.dataDir;
|
|
345
|
+
}
|
|
346
|
+
if (options?.logLevel) {
|
|
347
|
+
config.logLevel = options.logLevel;
|
|
348
|
+
}
|
|
349
|
+
const logger = createLogger('mark-improving-agent', config.logLevel);
|
|
350
|
+
// Validate config
|
|
351
|
+
if (!config.dataDir) {
|
|
352
|
+
throw new Error('dataDir is required in mark-improving-agent config');
|
|
353
|
+
}
|
|
354
|
+
// Ensure data directory exists
|
|
355
|
+
await ensureDir(config.dataDir);
|
|
356
|
+
// Create all subsystem instances
|
|
357
|
+
const memoryStore = createMemoryStore({ dataDir: `${config.dataDir}/memory`, embedder: DEFAULT_EMBEDDER });
|
|
358
|
+
const archiveStore = createArchiveStore(`${config.dataDir}/archive`);
|
|
359
|
+
const dreamEngine = createDreamEngine(config.dataDir);
|
|
360
|
+
const spacedRepetition = createSpacedRepetition(config.dataDir);
|
|
361
|
+
const goalEngine = createGoalEngine(config.dataDir);
|
|
362
|
+
const metaLearner = createMetaLearner(config.dataDir);
|
|
363
|
+
const evolutionEngine = createEvolutionEngine(config.dataDir);
|
|
364
|
+
const selfModel = createSelfModelManager(config.dataDir);
|
|
365
|
+
const emotionEngine = createEmotionEngine(config.dataDir);
|
|
366
|
+
const flowMachine = createFlowMachine();
|
|
367
|
+
const personalityEngine = createPersonalityEngine(config.dataDir);
|
|
368
|
+
const skillRegistry = createSkillRegistry();
|
|
369
|
+
const skillDAG = createSkillDAG();
|
|
370
|
+
const taskGraph = createTaskGraph();
|
|
371
|
+
const contextManager = createContextManager(config.agent.maxContext);
|
|
372
|
+
const checkpointManager = createCheckpointManager(config.dataDir, config.checkpoint.maxKeep);
|
|
373
|
+
const rollbackManager = createRollbackManager(config.dataDir);
|
|
374
|
+
// New identity subsystems
|
|
375
|
+
const reflexion = createReflexion(config.dataDir);
|
|
376
|
+
const selfVerifier = createSelfVerifier(config.dataDir);
|
|
377
|
+
const healingRL = createHealingMemoryRL(config.dataDir);
|
|
378
|
+
const lessonBank = createLessonBank(config.dataDir);
|
|
379
|
+
const emotionalProtocol = createEmotionalProtocol();
|
|
380
|
+
const truthfulnessChecker = createTruthfulnessChecker(config.dataDir);
|
|
381
|
+
const deps = {
|
|
382
|
+
memoryStore,
|
|
383
|
+
archiveStore,
|
|
384
|
+
dreamEngine,
|
|
385
|
+
spacedRepetition,
|
|
386
|
+
goalEngine,
|
|
387
|
+
metaLearner,
|
|
388
|
+
evolutionEngine,
|
|
389
|
+
selfModel,
|
|
390
|
+
emotionEngine,
|
|
391
|
+
flowMachine,
|
|
392
|
+
personalityEngine,
|
|
393
|
+
skillRegistry,
|
|
394
|
+
skillDAG,
|
|
395
|
+
taskGraph,
|
|
396
|
+
contextManager,
|
|
397
|
+
checkpointManager,
|
|
398
|
+
rollbackManager,
|
|
399
|
+
reflexion,
|
|
400
|
+
selfVerifier,
|
|
401
|
+
healingRL,
|
|
402
|
+
lessonBank,
|
|
403
|
+
emotionalProtocol,
|
|
404
|
+
truthfulnessChecker,
|
|
405
|
+
};
|
|
406
|
+
return createHeartFlowInstance(deps, config, logger);
|
|
407
|
+
}
|
|
408
|
+
export default createHeartFlow;
|