agentic-qe 2.1.2 → 2.2.1
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/.claude/skills/agentic-quality-engineering/SKILL.md +4 -4
- package/.claude/skills/cicd-pipeline-qe-orchestrator/README.md +14 -11
- package/.claude/skills/skills-manifest.json +2 -2
- package/CHANGELOG.md +138 -0
- package/README.md +92 -214
- package/dist/agents/BaseAgent.d.ts +5 -1
- package/dist/agents/BaseAgent.d.ts.map +1 -1
- package/dist/agents/BaseAgent.js +32 -17
- package/dist/agents/BaseAgent.js.map +1 -1
- package/dist/agents/index.d.ts.map +1 -1
- package/dist/agents/index.js +5 -1
- package/dist/agents/index.js.map +1 -1
- package/dist/cli/commands/improve/index.d.ts +8 -1
- package/dist/cli/commands/improve/index.d.ts.map +1 -1
- package/dist/cli/commands/improve/index.js +18 -16
- package/dist/cli/commands/improve/index.js.map +1 -1
- package/dist/cli/commands/learn/index.d.ts +10 -2
- package/dist/cli/commands/learn/index.d.ts.map +1 -1
- package/dist/cli/commands/learn/index.js +99 -63
- package/dist/cli/commands/learn/index.js.map +1 -1
- package/dist/cli/commands/patterns/index.d.ts +8 -1
- package/dist/cli/commands/patterns/index.d.ts.map +1 -1
- package/dist/cli/commands/patterns/index.js +79 -45
- package/dist/cli/commands/patterns/index.js.map +1 -1
- package/dist/cli/commands/routing/index.d.ts +5 -0
- package/dist/cli/commands/routing/index.d.ts.map +1 -1
- package/dist/cli/commands/routing/index.js +11 -10
- package/dist/cli/commands/routing/index.js.map +1 -1
- package/dist/cli/init/agents.d.ts +1 -1
- package/dist/cli/init/agents.js +2 -2
- package/dist/cli/init/database-init.d.ts +7 -0
- package/dist/cli/init/database-init.d.ts.map +1 -1
- package/dist/cli/init/database-init.js +29 -48
- package/dist/cli/init/database-init.js.map +1 -1
- package/dist/core/di/AgentDependencies.d.ts +127 -0
- package/dist/core/di/AgentDependencies.d.ts.map +1 -0
- package/dist/core/di/AgentDependencies.js +251 -0
- package/dist/core/di/AgentDependencies.js.map +1 -0
- package/dist/core/di/DIContainer.d.ts +149 -0
- package/dist/core/di/DIContainer.d.ts.map +1 -0
- package/dist/core/di/DIContainer.js +333 -0
- package/dist/core/di/DIContainer.js.map +1 -0
- package/dist/core/di/index.d.ts +11 -0
- package/dist/core/di/index.d.ts.map +1 -0
- package/dist/core/di/index.js +22 -0
- package/dist/core/di/index.js.map +1 -0
- package/dist/core/index.d.ts +1 -0
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +11 -1
- package/dist/core/index.js.map +1 -1
- package/dist/core/memory/HNSWVectorMemory.d.ts +261 -0
- package/dist/core/memory/HNSWVectorMemory.d.ts.map +1 -0
- package/dist/core/memory/HNSWVectorMemory.js +647 -0
- package/dist/core/memory/HNSWVectorMemory.js.map +1 -0
- package/dist/core/memory/SwarmMemoryManager.d.ts +7 -0
- package/dist/core/memory/SwarmMemoryManager.d.ts.map +1 -1
- package/dist/core/memory/SwarmMemoryManager.js +9 -0
- package/dist/core/memory/SwarmMemoryManager.js.map +1 -1
- package/dist/core/memory/index.d.ts +2 -0
- package/dist/core/memory/index.d.ts.map +1 -1
- package/dist/core/memory/index.js +11 -1
- package/dist/core/memory/index.js.map +1 -1
- package/dist/learning/ExperienceSharingProtocol.d.ts +243 -0
- package/dist/learning/ExperienceSharingProtocol.d.ts.map +1 -0
- package/dist/learning/ExperienceSharingProtocol.js +538 -0
- package/dist/learning/ExperienceSharingProtocol.js.map +1 -0
- package/dist/learning/ExplainableLearning.d.ts +191 -0
- package/dist/learning/ExplainableLearning.d.ts.map +1 -0
- package/dist/learning/ExplainableLearning.js +441 -0
- package/dist/learning/ExplainableLearning.js.map +1 -0
- package/dist/learning/GossipPatternSharingProtocol.d.ts +228 -0
- package/dist/learning/GossipPatternSharingProtocol.d.ts.map +1 -0
- package/dist/learning/GossipPatternSharingProtocol.js +590 -0
- package/dist/learning/GossipPatternSharingProtocol.js.map +1 -0
- package/dist/learning/LearningEngine.d.ts +104 -4
- package/dist/learning/LearningEngine.d.ts.map +1 -1
- package/dist/learning/LearningEngine.js +350 -16
- package/dist/learning/LearningEngine.js.map +1 -1
- package/dist/learning/PerformanceOptimizer.d.ts +268 -0
- package/dist/learning/PerformanceOptimizer.d.ts.map +1 -0
- package/dist/learning/PerformanceOptimizer.js +552 -0
- package/dist/learning/PerformanceOptimizer.js.map +1 -0
- package/dist/learning/PrivacyManager.d.ts +197 -0
- package/dist/learning/PrivacyManager.d.ts.map +1 -0
- package/dist/learning/PrivacyManager.js +551 -0
- package/dist/learning/PrivacyManager.js.map +1 -0
- package/dist/learning/QLearning.d.ts +38 -125
- package/dist/learning/QLearning.d.ts.map +1 -1
- package/dist/learning/QLearning.js +46 -267
- package/dist/learning/QLearning.js.map +1 -1
- package/dist/learning/QLearningLegacy.d.ts +154 -0
- package/dist/learning/QLearningLegacy.d.ts.map +1 -0
- package/dist/learning/QLearningLegacy.js +337 -0
- package/dist/learning/QLearningLegacy.js.map +1 -0
- package/dist/learning/TransferLearningManager.d.ts +212 -0
- package/dist/learning/TransferLearningManager.d.ts.map +1 -0
- package/dist/learning/TransferLearningManager.js +497 -0
- package/dist/learning/TransferLearningManager.js.map +1 -0
- package/dist/learning/algorithms/AbstractRLLearner.d.ts +162 -0
- package/dist/learning/algorithms/AbstractRLLearner.d.ts.map +1 -0
- package/dist/learning/algorithms/AbstractRLLearner.js +300 -0
- package/dist/learning/algorithms/AbstractRLLearner.js.map +1 -0
- package/dist/learning/algorithms/ActorCriticLearner.d.ts +201 -0
- package/dist/learning/algorithms/ActorCriticLearner.d.ts.map +1 -0
- package/dist/learning/algorithms/ActorCriticLearner.js +447 -0
- package/dist/learning/algorithms/ActorCriticLearner.js.map +1 -0
- package/dist/learning/algorithms/MAMLMetaLearner.d.ts +218 -0
- package/dist/learning/algorithms/MAMLMetaLearner.d.ts.map +1 -0
- package/dist/learning/algorithms/MAMLMetaLearner.js +532 -0
- package/dist/learning/algorithms/MAMLMetaLearner.js.map +1 -0
- package/dist/learning/algorithms/PPOLearner.d.ts +207 -0
- package/dist/learning/algorithms/PPOLearner.d.ts.map +1 -0
- package/dist/learning/algorithms/PPOLearner.js +490 -0
- package/dist/learning/algorithms/PPOLearner.js.map +1 -0
- package/dist/learning/algorithms/QLearning.d.ts +68 -0
- package/dist/learning/algorithms/QLearning.d.ts.map +1 -0
- package/dist/learning/algorithms/QLearning.js +116 -0
- package/dist/learning/algorithms/QLearning.js.map +1 -0
- package/dist/learning/algorithms/SARSALearner.d.ts +107 -0
- package/dist/learning/algorithms/SARSALearner.d.ts.map +1 -0
- package/dist/learning/algorithms/SARSALearner.js +252 -0
- package/dist/learning/algorithms/SARSALearner.js.map +1 -0
- package/dist/learning/algorithms/index.d.ts +32 -0
- package/dist/learning/algorithms/index.d.ts.map +1 -0
- package/dist/learning/algorithms/index.js +50 -0
- package/dist/learning/algorithms/index.js.map +1 -0
- package/dist/learning/index.d.ts +11 -0
- package/dist/learning/index.d.ts.map +1 -1
- package/dist/learning/index.js +31 -1
- package/dist/learning/index.js.map +1 -1
- package/dist/learning/types.d.ts +2 -0
- package/dist/learning/types.d.ts.map +1 -1
- package/dist/mcp/server-instructions.d.ts +1 -1
- package/dist/mcp/server-instructions.js +1 -1
- package/dist/memory/DistributedPatternLibrary.d.ts +159 -0
- package/dist/memory/DistributedPatternLibrary.d.ts.map +1 -0
- package/dist/memory/DistributedPatternLibrary.js +370 -0
- package/dist/memory/DistributedPatternLibrary.js.map +1 -0
- package/dist/memory/PatternQualityScorer.d.ts +169 -0
- package/dist/memory/PatternQualityScorer.d.ts.map +1 -0
- package/dist/memory/PatternQualityScorer.js +327 -0
- package/dist/memory/PatternQualityScorer.js.map +1 -0
- package/dist/memory/PatternReplicationService.d.ts +187 -0
- package/dist/memory/PatternReplicationService.d.ts.map +1 -0
- package/dist/memory/PatternReplicationService.js +392 -0
- package/dist/memory/PatternReplicationService.js.map +1 -0
- package/dist/providers/ClaudeProvider.d.ts +98 -0
- package/dist/providers/ClaudeProvider.d.ts.map +1 -0
- package/dist/providers/ClaudeProvider.js +418 -0
- package/dist/providers/ClaudeProvider.js.map +1 -0
- package/dist/providers/HybridRouter.d.ts +217 -0
- package/dist/providers/HybridRouter.d.ts.map +1 -0
- package/dist/providers/HybridRouter.js +679 -0
- package/dist/providers/HybridRouter.js.map +1 -0
- package/dist/providers/ILLMProvider.d.ts +287 -0
- package/dist/providers/ILLMProvider.d.ts.map +1 -0
- package/dist/providers/ILLMProvider.js +33 -0
- package/dist/providers/ILLMProvider.js.map +1 -0
- package/dist/providers/LLMProviderFactory.d.ts +154 -0
- package/dist/providers/LLMProviderFactory.d.ts.map +1 -0
- package/dist/providers/LLMProviderFactory.js +426 -0
- package/dist/providers/LLMProviderFactory.js.map +1 -0
- package/dist/providers/RuvllmProvider.d.ts +107 -0
- package/dist/providers/RuvllmProvider.d.ts.map +1 -0
- package/dist/providers/RuvllmProvider.js +417 -0
- package/dist/providers/RuvllmProvider.js.map +1 -0
- package/dist/providers/index.d.ts +32 -0
- package/dist/providers/index.d.ts.map +1 -0
- package/dist/providers/index.js +75 -0
- package/dist/providers/index.js.map +1 -0
- package/dist/telemetry/LearningTelemetry.d.ts +190 -0
- package/dist/telemetry/LearningTelemetry.d.ts.map +1 -0
- package/dist/telemetry/LearningTelemetry.js +403 -0
- package/dist/telemetry/LearningTelemetry.js.map +1 -0
- package/dist/telemetry/index.d.ts +1 -0
- package/dist/telemetry/index.d.ts.map +1 -1
- package/dist/telemetry/index.js +20 -2
- package/dist/telemetry/index.js.map +1 -1
- package/dist/telemetry/instrumentation/agent.d.ts +1 -1
- package/dist/telemetry/instrumentation/agent.js +1 -1
- package/dist/telemetry/instrumentation/index.d.ts +1 -1
- package/dist/telemetry/instrumentation/index.js +1 -1
- package/dist/utils/math.d.ts +11 -0
- package/dist/utils/math.d.ts.map +1 -0
- package/dist/utils/math.js +16 -0
- package/dist/utils/math.js.map +1 -0
- package/docs/reference/agents.md +1 -1
- package/docs/reference/skills.md +3 -3
- package/docs/reference/usage.md +4 -4
- package/package.json +1 -1
|
@@ -6,8 +6,22 @@
|
|
|
6
6
|
*/
|
|
7
7
|
import { SwarmMemoryManager } from '../core/memory/SwarmMemoryManager';
|
|
8
8
|
import { QLearningConfig } from './QLearning';
|
|
9
|
+
import { AbstractRLLearner } from './algorithms/AbstractRLLearner';
|
|
9
10
|
import { TaskResult } from './RewardCalculator';
|
|
10
11
|
import { LearningConfig, TaskExperience, TaskState, AgentAction, LearningFeedback, LearningOutcome, LearnedPattern, FailurePattern, StrategyRecommendation } from './types';
|
|
12
|
+
import { ExperienceSharingProtocol } from './ExperienceSharingProtocol';
|
|
13
|
+
/**
|
|
14
|
+
* RL Algorithm type selection
|
|
15
|
+
*/
|
|
16
|
+
export type RLAlgorithmType = 'q-learning' | 'sarsa' | 'actor-critic' | 'ppo' | 'maml' | 'legacy';
|
|
17
|
+
/**
|
|
18
|
+
* Extended learning configuration with algorithm selection
|
|
19
|
+
*/
|
|
20
|
+
export interface ExtendedLearningConfig extends LearningConfig {
|
|
21
|
+
algorithm?: RLAlgorithmType;
|
|
22
|
+
enableExperienceSharing?: boolean;
|
|
23
|
+
experienceSharingPriority?: number;
|
|
24
|
+
}
|
|
11
25
|
/**
|
|
12
26
|
* LearningEngine - Reinforcement learning for agents
|
|
13
27
|
*/
|
|
@@ -17,6 +31,7 @@ export declare class LearningEngine {
|
|
|
17
31
|
private readonly agentId;
|
|
18
32
|
private config;
|
|
19
33
|
private qTable;
|
|
34
|
+
private rlAlgorithm?;
|
|
20
35
|
private qLearning?;
|
|
21
36
|
private useQLearning;
|
|
22
37
|
private experiences;
|
|
@@ -24,7 +39,8 @@ export declare class LearningEngine {
|
|
|
24
39
|
private taskCount;
|
|
25
40
|
private readonly stateExtractor;
|
|
26
41
|
private readonly rewardCalculator;
|
|
27
|
-
|
|
42
|
+
private experienceSharing?;
|
|
43
|
+
constructor(agentId: string, memoryStore: SwarmMemoryManager, config?: Partial<ExtendedLearningConfig>);
|
|
28
44
|
/**
|
|
29
45
|
* Initialize the learning engine
|
|
30
46
|
*
|
|
@@ -41,10 +57,10 @@ export declare class LearningEngine {
|
|
|
41
57
|
*/
|
|
42
58
|
recordExperience(task: any, result: TaskResult, feedback?: LearningFeedback): Promise<void>;
|
|
43
59
|
/**
|
|
44
|
-
* Load patterns from memoryStore (
|
|
60
|
+
* Load patterns from memoryStore (SwarmMemoryManager -> memory.db)
|
|
45
61
|
*
|
|
46
|
-
* Architecture: Retrieves patterns from
|
|
47
|
-
*
|
|
62
|
+
* Architecture: Retrieves patterns from the unified memory.db database
|
|
63
|
+
* via SwarmMemoryManager.
|
|
48
64
|
*
|
|
49
65
|
* Note: This method is legacy and not currently used (patterns loaded on-demand).
|
|
50
66
|
*/
|
|
@@ -227,5 +243,89 @@ export declare class LearningEngine {
|
|
|
227
243
|
* The shared SwarmMemoryManager handles database lifecycle management.
|
|
228
244
|
*/
|
|
229
245
|
dispose(): void;
|
|
246
|
+
/**
|
|
247
|
+
* Set the RL algorithm to use (Q-Learning, SARSA, etc.)
|
|
248
|
+
* Supports dynamic algorithm switching with state transfer
|
|
249
|
+
*/
|
|
250
|
+
setAlgorithm(algorithm: RLAlgorithmType): void;
|
|
251
|
+
/**
|
|
252
|
+
* Disable RL algorithm and revert to legacy Q-table
|
|
253
|
+
*/
|
|
254
|
+
private disableRLAlgorithm;
|
|
255
|
+
/**
|
|
256
|
+
* Import legacy Q-table into current RL algorithm
|
|
257
|
+
*/
|
|
258
|
+
private importLegacyQTable;
|
|
259
|
+
/**
|
|
260
|
+
* Get current RL algorithm type
|
|
261
|
+
*/
|
|
262
|
+
getAlgorithm(): RLAlgorithmType;
|
|
263
|
+
/**
|
|
264
|
+
* Get RL algorithm instance (for advanced use)
|
|
265
|
+
*/
|
|
266
|
+
getRLAlgorithm(): AbstractRLLearner | undefined;
|
|
267
|
+
/**
|
|
268
|
+
* Get algorithm-specific statistics
|
|
269
|
+
*/
|
|
270
|
+
getAlgorithmStats(): {
|
|
271
|
+
algorithm: RLAlgorithmType;
|
|
272
|
+
stats?: ReturnType<AbstractRLLearner['getStatistics']>;
|
|
273
|
+
};
|
|
274
|
+
/**
|
|
275
|
+
* Enable experience sharing with other agents
|
|
276
|
+
* Connects this LearningEngine to an ExperienceSharingProtocol
|
|
277
|
+
*
|
|
278
|
+
* @param protocol - The ExperienceSharingProtocol instance to use
|
|
279
|
+
*/
|
|
280
|
+
enableExperienceSharing(protocol: ExperienceSharingProtocol): void;
|
|
281
|
+
/**
|
|
282
|
+
* Disable experience sharing
|
|
283
|
+
*/
|
|
284
|
+
disableExperienceSharing(): void;
|
|
285
|
+
/**
|
|
286
|
+
* Check if experience sharing is enabled
|
|
287
|
+
*/
|
|
288
|
+
isExperienceSharingEnabled(): boolean;
|
|
289
|
+
/**
|
|
290
|
+
* Get the ExperienceSharingProtocol instance (if enabled)
|
|
291
|
+
*/
|
|
292
|
+
getExperienceSharingProtocol(): ExperienceSharingProtocol | undefined;
|
|
293
|
+
/**
|
|
294
|
+
* Share an experience with other agents
|
|
295
|
+
* Called internally after learning from execution when sharing is enabled
|
|
296
|
+
*
|
|
297
|
+
* @param experience - The experience to share
|
|
298
|
+
* @param priority - Optional priority override (0-1, higher = more valuable)
|
|
299
|
+
*/
|
|
300
|
+
shareExperienceWithPeers(experience: TaskExperience, priority?: number): Promise<string | undefined>;
|
|
301
|
+
/**
|
|
302
|
+
* Calculate the priority/value of an experience for sharing
|
|
303
|
+
* Higher rewards and successful experiences get higher priority
|
|
304
|
+
*/
|
|
305
|
+
private calculateExperiencePriority;
|
|
306
|
+
/**
|
|
307
|
+
* Handle an experience received from another agent
|
|
308
|
+
* Integrates the shared experience into local learning
|
|
309
|
+
*/
|
|
310
|
+
private handleReceivedExperience;
|
|
311
|
+
/**
|
|
312
|
+
* Query relevant experiences from peers for a given state
|
|
313
|
+
* Useful for transfer learning before executing a task
|
|
314
|
+
*
|
|
315
|
+
* @param state - The current task state
|
|
316
|
+
* @param limit - Maximum number of experiences to retrieve
|
|
317
|
+
*/
|
|
318
|
+
queryPeerExperiences(state: TaskState, limit?: number): Promise<TaskExperience[]>;
|
|
319
|
+
/**
|
|
320
|
+
* Get experience sharing statistics
|
|
321
|
+
*/
|
|
322
|
+
getExperienceSharingStats(): {
|
|
323
|
+
enabled: boolean;
|
|
324
|
+
stats?: {
|
|
325
|
+
experiencesShared: number;
|
|
326
|
+
experiencesReceived: number;
|
|
327
|
+
activeConnections: number;
|
|
328
|
+
};
|
|
329
|
+
};
|
|
230
330
|
}
|
|
231
331
|
//# sourceMappingURL=LearningEngine.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"LearningEngine.d.ts","sourceRoot":"","sources":["../../src/learning/LearningEngine.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAIH,OAAO,EAAE,kBAAkB,EAAE,MAAM,mCAAmC,CAAC;AACvE,OAAO,EAAa,eAAe,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"LearningEngine.d.ts","sourceRoot":"","sources":["../../src/learning/LearningEngine.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAIH,OAAO,EAAE,kBAAkB,EAAE,MAAM,mCAAmC,CAAC;AACvE,OAAO,EAAa,eAAe,EAAE,MAAM,aAAa,CAAC;AAIzD,OAAO,EAAE,iBAAiB,EAAE,MAAM,gCAAgC,CAAC;AAEnE,OAAO,EAAoB,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAelE,OAAO,EACL,cAAc,EACd,cAAc,EACd,SAAS,EACT,WAAW,EACX,gBAAgB,EAChB,eAAe,EACf,cAAc,EAEd,cAAc,EACd,sBAAsB,EAEvB,MAAM,SAAS,CAAC;AACjB,OAAO,EAAE,yBAAyB,EAAoB,MAAM,6BAA6B,CAAC;AAE1F;;GAEG;AACH,MAAM,MAAM,eAAe,GAAG,YAAY,GAAG,OAAO,GAAG,cAAc,GAAG,KAAK,GAAG,MAAM,GAAG,QAAQ,CAAC;AAElG;;GAEG;AAC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,EAAE,cAAc,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC;IAwB1G;;;OAGG;IACH,OAAO,CAAC,2BAA2B;IAanC;;;OAGG;YACW,wBAAwB;IAyCtC;;;;;;OAMG;IACG,oBAAoB,CAAC,KAAK,EAAE,SAAS,EAAE,KAAK,GAAE,MAAW,GAAG,OAAO,CAAC,cAAc,EAAE,CAAC;IAc3F;;OAEG;IACH,yBAAyB,IAAI;QAC3B,OAAO,EAAE,OAAO,CAAC;QACjB,KAAK,CAAC,EAAE;YACN,iBAAiB,EAAE,MAAM,CAAC;YAC1B,mBAAmB,EAAE,MAAM,CAAC;YAC5B,iBAAiB,EAAE,MAAM,CAAC;SAC3B,CAAC;KACH;CAeF"}
|
|
@@ -13,9 +13,13 @@ exports.LearningEngine = void 0;
|
|
|
13
13
|
const uuid_1 = require("uuid");
|
|
14
14
|
const Logger_1 = require("../utils/Logger");
|
|
15
15
|
const QLearning_1 = require("./QLearning");
|
|
16
|
+
const SARSALearner_1 = require("./algorithms/SARSALearner");
|
|
17
|
+
const ActorCriticLearner_1 = require("./algorithms/ActorCriticLearner");
|
|
18
|
+
const PPOLearner_1 = require("./algorithms/PPOLearner");
|
|
16
19
|
const StateExtractor_1 = require("./StateExtractor");
|
|
17
20
|
const RewardCalculator_1 = require("./RewardCalculator");
|
|
18
21
|
const package_json_1 = __importDefault(require("../../package.json"));
|
|
22
|
+
const LearningTelemetry_1 = require("../telemetry/LearningTelemetry");
|
|
19
23
|
const PACKAGE_VERSION = package_json_1.default.version;
|
|
20
24
|
/**
|
|
21
25
|
* Default learning configuration
|
|
@@ -29,7 +33,10 @@ const DEFAULT_CONFIG = {
|
|
|
29
33
|
minExplorationRate: 0.01,
|
|
30
34
|
maxMemorySize: 100 * 1024 * 1024, // 100MB
|
|
31
35
|
batchSize: 32,
|
|
32
|
-
updateFrequency: 10
|
|
36
|
+
updateFrequency: 10,
|
|
37
|
+
algorithm: 'q-learning', // Default to Q-Learning
|
|
38
|
+
enableExperienceSharing: false, // Disabled by default for backward compatibility
|
|
39
|
+
experienceSharingPriority: 0.5 // Only share moderately valuable experiences
|
|
33
40
|
};
|
|
34
41
|
/**
|
|
35
42
|
* LearningEngine - Reinforcement learning for agents
|
|
@@ -48,13 +55,20 @@ class LearningEngine {
|
|
|
48
55
|
this.taskCount = 0;
|
|
49
56
|
this.stateExtractor = new StateExtractor_1.StateExtractor();
|
|
50
57
|
this.rewardCalculator = new RewardCalculator_1.RewardCalculator();
|
|
51
|
-
//
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
//
|
|
56
|
-
//
|
|
57
|
-
|
|
58
|
+
// Initialize RL algorithm if specified
|
|
59
|
+
if (this.config.algorithm && this.config.algorithm !== 'legacy') {
|
|
60
|
+
this.setAlgorithm(this.config.algorithm);
|
|
61
|
+
}
|
|
62
|
+
// UNIFIED PERSISTENCE ARCHITECTURE (v2.2.0):
|
|
63
|
+
// LearningEngine uses SwarmMemoryManager for ALL persistence.
|
|
64
|
+
// All data goes to the SINGLE database: .agentic-qe/memory.db
|
|
65
|
+
// This ensures CLI, MCP, and agents all share the same data.
|
|
66
|
+
//
|
|
67
|
+
// Key methods that persist to memory.db:
|
|
68
|
+
// - storeLearningExperience() -> learning_experiences table
|
|
69
|
+
// - storePattern() -> patterns table
|
|
70
|
+
// - upsertQValue() -> q_values table
|
|
71
|
+
this.logger.info(`LearningEngine initialized for agent ${agentId} with algorithm ${this.config.algorithm} - using ${memoryStore.constructor.name} for persistent storage`);
|
|
58
72
|
}
|
|
59
73
|
/**
|
|
60
74
|
* Initialize the learning engine
|
|
@@ -87,25 +101,25 @@ class LearningEngine {
|
|
|
87
101
|
await this.learnFromExecution(task, result, feedback);
|
|
88
102
|
}
|
|
89
103
|
/**
|
|
90
|
-
* Load patterns from memoryStore (
|
|
104
|
+
* Load patterns from memoryStore (SwarmMemoryManager -> memory.db)
|
|
91
105
|
*
|
|
92
|
-
* Architecture: Retrieves patterns from
|
|
93
|
-
*
|
|
106
|
+
* Architecture: Retrieves patterns from the unified memory.db database
|
|
107
|
+
* via SwarmMemoryManager.
|
|
94
108
|
*
|
|
95
109
|
* Note: This method is legacy and not currently used (patterns loaded on-demand).
|
|
96
110
|
*/
|
|
97
111
|
async loadPatternsFromMemoryStore() {
|
|
98
112
|
try {
|
|
99
|
-
// Query all patterns from
|
|
113
|
+
// Query all patterns from memory.db (confidence >= 0 returns all)
|
|
100
114
|
const allPatterns = await this.memoryStore.queryPatternsByConfidence(0);
|
|
101
115
|
if (allPatterns.length === 0) {
|
|
102
|
-
this.logger.info('No existing patterns found in
|
|
116
|
+
this.logger.info('No existing patterns found in memory.db');
|
|
103
117
|
return;
|
|
104
118
|
}
|
|
105
|
-
this.logger.info(`Found ${allPatterns.length} patterns in
|
|
119
|
+
this.logger.info(`Found ${allPatterns.length} patterns in memory.db for agent ${this.agentId}`);
|
|
106
120
|
}
|
|
107
121
|
catch (error) {
|
|
108
|
-
this.logger.warn(`Failed to load patterns from
|
|
122
|
+
this.logger.warn(`Failed to load patterns from memory.db:`, error);
|
|
109
123
|
}
|
|
110
124
|
}
|
|
111
125
|
/**
|
|
@@ -120,6 +134,7 @@ class LearningEngine {
|
|
|
120
134
|
if (!this.config.enabled) {
|
|
121
135
|
return this.createOutcome(false, 0, 0);
|
|
122
136
|
}
|
|
137
|
+
const episodeStartTime = Date.now();
|
|
123
138
|
try {
|
|
124
139
|
// Extract experience from task execution
|
|
125
140
|
const experience = this.extractExperience(task, result, feedback);
|
|
@@ -129,6 +144,8 @@ class LearningEngine {
|
|
|
129
144
|
this.experiences.push(experience);
|
|
130
145
|
// Update Q-table (in-memory)
|
|
131
146
|
await this.updateQTable(experience);
|
|
147
|
+
// Record telemetry: Q-value update occurred
|
|
148
|
+
(0, LearningTelemetry_1.recordQValueUpdate)(this.agentId, 1, undefined, this.getAlgorithm());
|
|
132
149
|
// Persist to database via memoryStore (replaces persistence adapter)
|
|
133
150
|
// Only if memoryStore is SwarmMemoryManager
|
|
134
151
|
if (this.memoryStore && typeof this.memoryStore.storeLearningExperience === "function") {
|
|
@@ -185,6 +202,10 @@ class LearningEngine {
|
|
|
185
202
|
this.decayExploration();
|
|
186
203
|
// Calculate improvement
|
|
187
204
|
const improvement = await this.calculateImprovement();
|
|
205
|
+
// Share experience with peers if enabled and valuable
|
|
206
|
+
if (this.config.enableExperienceSharing && this.experienceSharing) {
|
|
207
|
+
await this.shareExperienceWithPeers(experience);
|
|
208
|
+
}
|
|
188
209
|
// Emit learning event
|
|
189
210
|
await this.emitLearningEvent('training', {
|
|
190
211
|
experience,
|
|
@@ -867,9 +888,322 @@ class LearningEngine {
|
|
|
867
888
|
* The shared SwarmMemoryManager handles database lifecycle management.
|
|
868
889
|
*/
|
|
869
890
|
dispose() {
|
|
870
|
-
//
|
|
891
|
+
// Clean up experience sharing listeners
|
|
892
|
+
if (this.experienceSharing) {
|
|
893
|
+
this.experienceSharing.removeAllListeners('experience_received');
|
|
894
|
+
this.experienceSharing = undefined;
|
|
895
|
+
}
|
|
871
896
|
this.logger.debug(`LearningEngine disposed for agent ${this.agentId}`);
|
|
872
897
|
}
|
|
898
|
+
/**
|
|
899
|
+
* Set the RL algorithm to use (Q-Learning, SARSA, etc.)
|
|
900
|
+
* Supports dynamic algorithm switching with state transfer
|
|
901
|
+
*/
|
|
902
|
+
setAlgorithm(algorithm) {
|
|
903
|
+
if (algorithm === 'legacy') {
|
|
904
|
+
// Revert to legacy Q-table implementation
|
|
905
|
+
this.disableRLAlgorithm();
|
|
906
|
+
return;
|
|
907
|
+
}
|
|
908
|
+
const rlConfig = {
|
|
909
|
+
learningRate: this.config.learningRate,
|
|
910
|
+
discountFactor: this.config.discountFactor,
|
|
911
|
+
explorationRate: this.config.explorationRate,
|
|
912
|
+
explorationDecay: this.config.explorationDecay,
|
|
913
|
+
minExplorationRate: this.config.minExplorationRate,
|
|
914
|
+
useExperienceReplay: true,
|
|
915
|
+
replayBufferSize: 10000,
|
|
916
|
+
batchSize: this.config.batchSize
|
|
917
|
+
};
|
|
918
|
+
// Export current algorithm state if exists
|
|
919
|
+
let existingState;
|
|
920
|
+
if (this.rlAlgorithm) {
|
|
921
|
+
existingState = this.rlAlgorithm.export();
|
|
922
|
+
}
|
|
923
|
+
else if (this.qLearning) {
|
|
924
|
+
// Migrate from legacy qLearning
|
|
925
|
+
existingState = this.qLearning.export();
|
|
926
|
+
}
|
|
927
|
+
// Create new algorithm instance
|
|
928
|
+
switch (algorithm) {
|
|
929
|
+
case 'q-learning':
|
|
930
|
+
this.rlAlgorithm = new QLearning_1.QLearning(rlConfig);
|
|
931
|
+
this.qLearning = this.rlAlgorithm; // Backward compatibility
|
|
932
|
+
this.useQLearning = true;
|
|
933
|
+
break;
|
|
934
|
+
case 'sarsa':
|
|
935
|
+
this.rlAlgorithm = new SARSALearner_1.SARSALearner(rlConfig);
|
|
936
|
+
this.useQLearning = false; // SARSA is different from Q-Learning
|
|
937
|
+
break;
|
|
938
|
+
case 'actor-critic':
|
|
939
|
+
this.rlAlgorithm = new ActorCriticLearner_1.ActorCriticLearner({
|
|
940
|
+
...rlConfig,
|
|
941
|
+
actorLearningRate: rlConfig.learningRate * 0.1,
|
|
942
|
+
criticLearningRate: rlConfig.learningRate,
|
|
943
|
+
entropyCoefficient: 0.01,
|
|
944
|
+
temperature: 1.0,
|
|
945
|
+
normalizeAdvantage: true,
|
|
946
|
+
targetUpdateFrequency: 100
|
|
947
|
+
});
|
|
948
|
+
this.useQLearning = false;
|
|
949
|
+
break;
|
|
950
|
+
case 'ppo':
|
|
951
|
+
this.rlAlgorithm = new PPOLearner_1.PPOLearner({
|
|
952
|
+
...rlConfig,
|
|
953
|
+
clipEpsilon: 0.2,
|
|
954
|
+
ppoEpochs: 4,
|
|
955
|
+
miniBatchSize: 32,
|
|
956
|
+
valueLossCoefficient: 0.5,
|
|
957
|
+
entropyCoefficient: 0.01,
|
|
958
|
+
gaeLambda: 0.95,
|
|
959
|
+
maxGradNorm: 0.5,
|
|
960
|
+
clipValueLoss: true,
|
|
961
|
+
policyLearningRate: rlConfig.learningRate,
|
|
962
|
+
valueLearningRate: rlConfig.learningRate * 3
|
|
963
|
+
});
|
|
964
|
+
this.useQLearning = false;
|
|
965
|
+
break;
|
|
966
|
+
default:
|
|
967
|
+
throw new Error(`Unknown RL algorithm: ${algorithm}`);
|
|
968
|
+
}
|
|
969
|
+
// Import existing state if available
|
|
970
|
+
if (existingState) {
|
|
971
|
+
this.rlAlgorithm.import(existingState);
|
|
972
|
+
this.logger.info(`Migrated existing Q-table to ${algorithm} (${this.rlAlgorithm.getTableSize()} state-action pairs)`);
|
|
973
|
+
}
|
|
974
|
+
else if (this.qTable.size > 0) {
|
|
975
|
+
// Import from legacy Q-table
|
|
976
|
+
this.importLegacyQTable();
|
|
977
|
+
}
|
|
978
|
+
this.config.algorithm = algorithm;
|
|
979
|
+
this.logger.info(`Switched to ${algorithm} algorithm for agent ${this.agentId}`, { config: rlConfig });
|
|
980
|
+
}
|
|
981
|
+
/**
|
|
982
|
+
* Disable RL algorithm and revert to legacy Q-table
|
|
983
|
+
*/
|
|
984
|
+
disableRLAlgorithm() {
|
|
985
|
+
if (this.rlAlgorithm) {
|
|
986
|
+
// Export RL algorithm state to legacy Q-table
|
|
987
|
+
const exported = this.rlAlgorithm.export();
|
|
988
|
+
this.deserializeQTableFromQLearning(exported.qTable);
|
|
989
|
+
}
|
|
990
|
+
else if (this.qLearning && this.useQLearning) {
|
|
991
|
+
// Export from legacy qLearning
|
|
992
|
+
const exported = this.qLearning.export();
|
|
993
|
+
this.deserializeQTableFromQLearning(exported.qTable);
|
|
994
|
+
}
|
|
995
|
+
this.rlAlgorithm = undefined;
|
|
996
|
+
this.qLearning = undefined;
|
|
997
|
+
this.useQLearning = false;
|
|
998
|
+
this.config.algorithm = 'legacy';
|
|
999
|
+
this.logger.info(`Disabled RL algorithm for agent ${this.agentId}, reverted to legacy Q-table`);
|
|
1000
|
+
}
|
|
1001
|
+
/**
|
|
1002
|
+
* Import legacy Q-table into current RL algorithm
|
|
1003
|
+
*/
|
|
1004
|
+
importLegacyQTable() {
|
|
1005
|
+
if (!this.rlAlgorithm) {
|
|
1006
|
+
return;
|
|
1007
|
+
}
|
|
1008
|
+
const serialized = this.serializeQTableForQLearning();
|
|
1009
|
+
this.rlAlgorithm.import({
|
|
1010
|
+
qTable: serialized,
|
|
1011
|
+
config: {
|
|
1012
|
+
learningRate: this.config.learningRate,
|
|
1013
|
+
discountFactor: this.config.discountFactor,
|
|
1014
|
+
explorationRate: this.config.explorationRate,
|
|
1015
|
+
explorationDecay: this.config.explorationDecay,
|
|
1016
|
+
minExplorationRate: this.config.minExplorationRate,
|
|
1017
|
+
useExperienceReplay: true,
|
|
1018
|
+
replayBufferSize: 10000,
|
|
1019
|
+
batchSize: this.config.batchSize
|
|
1020
|
+
},
|
|
1021
|
+
stepCount: this.taskCount,
|
|
1022
|
+
episodeCount: Math.floor(this.taskCount / 10)
|
|
1023
|
+
});
|
|
1024
|
+
this.logger.info(`Imported legacy Q-table to ${this.config.algorithm} (${this.rlAlgorithm.getTableSize()} state-action pairs)`);
|
|
1025
|
+
}
|
|
1026
|
+
/**
|
|
1027
|
+
* Get current RL algorithm type
|
|
1028
|
+
*/
|
|
1029
|
+
getAlgorithm() {
|
|
1030
|
+
return this.config.algorithm ?? 'legacy';
|
|
1031
|
+
}
|
|
1032
|
+
/**
|
|
1033
|
+
* Get RL algorithm instance (for advanced use)
|
|
1034
|
+
*/
|
|
1035
|
+
getRLAlgorithm() {
|
|
1036
|
+
return this.rlAlgorithm;
|
|
1037
|
+
}
|
|
1038
|
+
/**
|
|
1039
|
+
* Get algorithm-specific statistics
|
|
1040
|
+
*/
|
|
1041
|
+
getAlgorithmStats() {
|
|
1042
|
+
if (this.rlAlgorithm) {
|
|
1043
|
+
return {
|
|
1044
|
+
algorithm: this.config.algorithm ?? 'legacy',
|
|
1045
|
+
stats: this.rlAlgorithm.getStatistics()
|
|
1046
|
+
};
|
|
1047
|
+
}
|
|
1048
|
+
return {
|
|
1049
|
+
algorithm: 'legacy'
|
|
1050
|
+
};
|
|
1051
|
+
}
|
|
1052
|
+
// ============================================================================
|
|
1053
|
+
// Experience Sharing Integration
|
|
1054
|
+
// ============================================================================
|
|
1055
|
+
/**
|
|
1056
|
+
* Enable experience sharing with other agents
|
|
1057
|
+
* Connects this LearningEngine to an ExperienceSharingProtocol
|
|
1058
|
+
*
|
|
1059
|
+
* @param protocol - The ExperienceSharingProtocol instance to use
|
|
1060
|
+
*/
|
|
1061
|
+
enableExperienceSharing(protocol) {
|
|
1062
|
+
this.experienceSharing = protocol;
|
|
1063
|
+
this.config.enableExperienceSharing = true;
|
|
1064
|
+
// Subscribe to incoming experiences from other agents
|
|
1065
|
+
protocol.on('experience_received', async (event) => {
|
|
1066
|
+
await this.handleReceivedExperience(event.experienceId);
|
|
1067
|
+
});
|
|
1068
|
+
this.logger.info(`Experience sharing enabled for agent ${this.agentId}`);
|
|
1069
|
+
}
|
|
1070
|
+
/**
|
|
1071
|
+
* Disable experience sharing
|
|
1072
|
+
*/
|
|
1073
|
+
disableExperienceSharing() {
|
|
1074
|
+
if (this.experienceSharing) {
|
|
1075
|
+
this.experienceSharing.removeAllListeners('experience_received');
|
|
1076
|
+
this.experienceSharing = undefined;
|
|
1077
|
+
}
|
|
1078
|
+
this.config.enableExperienceSharing = false;
|
|
1079
|
+
this.logger.info(`Experience sharing disabled for agent ${this.agentId}`);
|
|
1080
|
+
}
|
|
1081
|
+
/**
|
|
1082
|
+
* Check if experience sharing is enabled
|
|
1083
|
+
*/
|
|
1084
|
+
isExperienceSharingEnabled() {
|
|
1085
|
+
return this.config.enableExperienceSharing === true && this.experienceSharing !== undefined;
|
|
1086
|
+
}
|
|
1087
|
+
/**
|
|
1088
|
+
* Get the ExperienceSharingProtocol instance (if enabled)
|
|
1089
|
+
*/
|
|
1090
|
+
getExperienceSharingProtocol() {
|
|
1091
|
+
return this.experienceSharing;
|
|
1092
|
+
}
|
|
1093
|
+
/**
|
|
1094
|
+
* Share an experience with other agents
|
|
1095
|
+
* Called internally after learning from execution when sharing is enabled
|
|
1096
|
+
*
|
|
1097
|
+
* @param experience - The experience to share
|
|
1098
|
+
* @param priority - Optional priority override (0-1, higher = more valuable)
|
|
1099
|
+
*/
|
|
1100
|
+
async shareExperienceWithPeers(experience, priority) {
|
|
1101
|
+
if (!this.experienceSharing) {
|
|
1102
|
+
return undefined;
|
|
1103
|
+
}
|
|
1104
|
+
// Calculate priority from reward if not provided
|
|
1105
|
+
const calculatedPriority = priority ?? this.calculateExperiencePriority(experience);
|
|
1106
|
+
// Only share if priority meets threshold
|
|
1107
|
+
if (calculatedPriority < (this.config.experienceSharingPriority ?? 0.5)) {
|
|
1108
|
+
this.logger.debug(`Experience below sharing threshold: ${calculatedPriority} < ${this.config.experienceSharingPriority}`);
|
|
1109
|
+
return undefined;
|
|
1110
|
+
}
|
|
1111
|
+
try {
|
|
1112
|
+
const experienceId = await this.experienceSharing.shareExperience(experience, calculatedPriority);
|
|
1113
|
+
this.logger.debug(`Shared experience with peers: ${experienceId}`, { priority: calculatedPriority });
|
|
1114
|
+
return experienceId;
|
|
1115
|
+
}
|
|
1116
|
+
catch (error) {
|
|
1117
|
+
this.logger.warn('Failed to share experience with peers:', error);
|
|
1118
|
+
return undefined;
|
|
1119
|
+
}
|
|
1120
|
+
}
|
|
1121
|
+
/**
|
|
1122
|
+
* Calculate the priority/value of an experience for sharing
|
|
1123
|
+
* Higher rewards and successful experiences get higher priority
|
|
1124
|
+
*/
|
|
1125
|
+
calculateExperiencePriority(experience) {
|
|
1126
|
+
// Base priority from reward (normalized from [-2, 2] to [0, 1])
|
|
1127
|
+
const rewardPriority = (experience.reward + 2) / 4;
|
|
1128
|
+
// Boost for successful experiences (positive reward)
|
|
1129
|
+
const successBoost = experience.reward > 0 ? 0.2 : 0;
|
|
1130
|
+
// Boost for unusual/interesting states (high complexity)
|
|
1131
|
+
const complexityBoost = experience.state.taskComplexity > 0.7 ? 0.1 : 0;
|
|
1132
|
+
return Math.min(1.0, Math.max(0, rewardPriority + successBoost + complexityBoost));
|
|
1133
|
+
}
|
|
1134
|
+
/**
|
|
1135
|
+
* Handle an experience received from another agent
|
|
1136
|
+
* Integrates the shared experience into local learning
|
|
1137
|
+
*/
|
|
1138
|
+
async handleReceivedExperience(experienceId) {
|
|
1139
|
+
if (!this.experienceSharing) {
|
|
1140
|
+
return;
|
|
1141
|
+
}
|
|
1142
|
+
try {
|
|
1143
|
+
// Get the shared experience from the protocol
|
|
1144
|
+
const sharedExperiences = await this.experienceSharing.getRelevantExperiences({ taskComplexity: 0, requiredCapabilities: [], contextFeatures: {}, previousAttempts: 0, availableResources: 1 }, 100);
|
|
1145
|
+
const sharedExp = sharedExperiences.find(e => e.id === experienceId);
|
|
1146
|
+
if (!sharedExp) {
|
|
1147
|
+
return; // Experience not found or already processed
|
|
1148
|
+
}
|
|
1149
|
+
// Learn from the shared experience (with reduced weight)
|
|
1150
|
+
// Apply a transfer learning discount factor
|
|
1151
|
+
const transferDiscount = 0.5; // Shared experiences weighted 50%
|
|
1152
|
+
const adjustedExperience = {
|
|
1153
|
+
...sharedExp.experience,
|
|
1154
|
+
reward: sharedExp.experience.reward * transferDiscount
|
|
1155
|
+
};
|
|
1156
|
+
// Update Q-table with shared experience
|
|
1157
|
+
await this.updateQTable(adjustedExperience);
|
|
1158
|
+
// Store in local experiences buffer
|
|
1159
|
+
this.experiences.push(adjustedExperience);
|
|
1160
|
+
this.logger.debug(`Integrated shared experience: ${experienceId}`, {
|
|
1161
|
+
from: sharedExp.sourceAgentId,
|
|
1162
|
+
originalReward: sharedExp.experience.reward,
|
|
1163
|
+
adjustedReward: adjustedExperience.reward
|
|
1164
|
+
});
|
|
1165
|
+
}
|
|
1166
|
+
catch (error) {
|
|
1167
|
+
this.logger.warn(`Failed to handle received experience ${experienceId}:`, error);
|
|
1168
|
+
}
|
|
1169
|
+
}
|
|
1170
|
+
/**
|
|
1171
|
+
* Query relevant experiences from peers for a given state
|
|
1172
|
+
* Useful for transfer learning before executing a task
|
|
1173
|
+
*
|
|
1174
|
+
* @param state - The current task state
|
|
1175
|
+
* @param limit - Maximum number of experiences to retrieve
|
|
1176
|
+
*/
|
|
1177
|
+
async queryPeerExperiences(state, limit = 10) {
|
|
1178
|
+
if (!this.experienceSharing) {
|
|
1179
|
+
return [];
|
|
1180
|
+
}
|
|
1181
|
+
try {
|
|
1182
|
+
const sharedExperiences = await this.experienceSharing.getRelevantExperiences(state, limit);
|
|
1183
|
+
return sharedExperiences.map(se => se.experience);
|
|
1184
|
+
}
|
|
1185
|
+
catch (error) {
|
|
1186
|
+
this.logger.warn('Failed to query peer experiences:', error);
|
|
1187
|
+
return [];
|
|
1188
|
+
}
|
|
1189
|
+
}
|
|
1190
|
+
/**
|
|
1191
|
+
* Get experience sharing statistics
|
|
1192
|
+
*/
|
|
1193
|
+
getExperienceSharingStats() {
|
|
1194
|
+
if (!this.experienceSharing) {
|
|
1195
|
+
return { enabled: false };
|
|
1196
|
+
}
|
|
1197
|
+
const stats = this.experienceSharing.getStats();
|
|
1198
|
+
return {
|
|
1199
|
+
enabled: true,
|
|
1200
|
+
stats: {
|
|
1201
|
+
experiencesShared: stats.experiencesShared,
|
|
1202
|
+
experiencesReceived: stats.experiencesReceived,
|
|
1203
|
+
activeConnections: stats.activeConnections
|
|
1204
|
+
}
|
|
1205
|
+
};
|
|
1206
|
+
}
|
|
873
1207
|
}
|
|
874
1208
|
exports.LearningEngine = LearningEngine;
|
|
875
1209
|
//# sourceMappingURL=LearningEngine.js.map
|