Files
proffesor-for-testing__agen…/tests/integration/learning-persistence.test.ts
Dragan Spiridonov 346438f0de Release v2.0.0: Claude 4.5 Optimization, QUIC Transport, 29 Agent Compaction (#100)
* SwarmMemoryManager Refactoring Plan

* fix: add missing directories to npm package (v1.9.3)

Added templates/, .claude/helpers/, and docs/reference/ to package.json
files array. These were missing from the npm distribution, causing
`aqe init` to fail with "Template file not found" errors.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* checkpoint improvements

* fix: critical memory/learning system fixes for v1.9.4

## Critical Fixes
- Vector embeddings now stored correctly (was storing NULL)
- SQL parameter style fixed for agentdb's SqlJsDatabase wrapper
- HNSW index schema mismatch resolved with pattern_id column
- Hooks now save to correct database (.agentic-qe/agentdb.db)

## New Features
- Added 5 SwarmMemoryManager methods for learning data retrieval:
  - getBestAction() - Q-learning best action selection
  - getRecentLearningExperiences() - Recent experience retrieval
  - getLearningExperiencesByTaskType() - Task-filtered experiences
  - getHighRewardExperiences() - Successful experience extraction
  - getLearningStats() - Aggregate learning statistics
- Added comprehensive integration test for memory/learning loop
- Added RuVector pattern store integration (experimental)

## Technical Details
- Vector embeddings: 384 dimensions × 4 bytes = 1,536 bytes per pattern
- All 12 integration tests pass
- HNSW index: 150x faster vector search enabled

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* fix: move ARM64-only ruvector packages to optionalDependencies

Fixes CI failures caused by platform-specific dependencies:
- @ruvector/node-linux-arm64-gnu (ARM64 only)
- ruvector-core-linux-arm64-gnu (ARM64 only)

These packages caused npm ci to fail on x64 CI runners with:
"EBADPLATFORM - wanted linux/arm64, current linux/x64"

Solution: Move ARM64 packages to optionalDependencies and add
x64 variants for cross-platform compatibility.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* chore: regenerate package-lock.json with x64 ruvector packages

Updates lockfile to include optional x64 platform packages for
CI compatibility:
- @ruvector/node-linux-x64-gnu@0.1.17
- ruvector-core-linux-x64-gnu@0.1.15

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* fix: replace fake QUIC with proper implementation and clean up skills manifest

QUIC Transport:
- Remove fake HTTP/2-based QUICTransport.ts that pretended to be QUIC
- Add proper QUIC implementation via Rust/WASM (@agentic-flow/quic-wasm)
- Add WebSocket fallback transport for when WASM unavailable
- Add dedicated type declarations for optional WASM module
- Features: 0-RTT connection, stream multiplexing, TLS 1.3 built-in

Skills Manifest:
- Rewrite skills-manifest.json with only QE skills (41 skills)
- Remove Claude Flow platform skills (agentdb-*, flow-nexus-*, github-*, etc.)
- Organize into proper categories: qe-core, testing-methodologies, etc.

Bug Fixes:
- Fix UnifiedMemoryCoordinator.ts TypeScript errors
- Use Logger.getInstance() instead of private constructor
- Use swarmMemory.query() instead of non-existent list() method
- Use correct close() method signatures for backends

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* feat: add QX Partner agent, compact subagents, fix CLAUDE.md dynamic counts

- Add QX Partner agent for Quality Experience analysis combining QA + UX perspectives
- Compact 9 subagent definitions to ~80 lines each (88% reduction)
- Fix CLAUDE.md template to dynamically count agents (including subagents) and skills
- Move CLAUDE.md creation after parallel phases for accurate counts
- Remove 'Production-Ready' claim from README and CHANGELOG
- Clean up backup files and move docs to proper directory

Initialization now correctly reports:
- 31 QE Agents (20 main + 11 subagents)
- 53 QE Skills
- 8 Slash Commands

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

---------

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-02 11:31:30 +01:00

485 lines
15 KiB
TypeScript

/**
* Integration Test: Learning System Database Persistence
*
* Verifies that Q-learning experiences and Q-values are correctly
* persisted to the database and restored across agent restarts.
*
* This test was created to prevent regression of the issue where
* LearningEngine was not receiving a Database instance, causing
* all persistence operations to be silently skipped.
*/
// CRITICAL: Unmock Database to use real implementation for persistence tests
jest.unmock('../../src/utils/Database');
import { describe, it, expect, beforeEach, afterEach } from '@jest/globals';
import { LearningEngine } from '../../src/learning/LearningEngine';
import { SwarmMemoryManager } from '../../src/core/memory/SwarmMemoryManager';
import { Database } from '../../src/utils/Database';
import { TaskResult } from '../../src/learning/RewardCalculator';
import { LearningFeedback } from '../../src/learning/types';
import fs from 'fs';
import path from 'path';
describe('Learning System Database Persistence', () => {
// Use tests/.tmp directory for test databases (not project root)
const tmpDir = path.join(__dirname, '../.tmp');
const testDbPath = path.join(tmpDir, '.test-learning.db');
const memoryDbPath = path.join(tmpDir, '.test-memory.db');
let database: Database;
let memoryManager: SwarmMemoryManager;
let learningEngine: LearningEngine | null = null;
beforeEach(async () => {
// Ensure tmp directory exists
if (!fs.existsSync(tmpDir)) {
fs.mkdirSync(tmpDir, { recursive: true });
}
// Clean up test databases if they exist
if (fs.existsSync(testDbPath)) {
fs.unlinkSync(testDbPath);
}
if (fs.existsSync(memoryDbPath)) {
fs.unlinkSync(memoryDbPath);
}
// Create fresh database (for Q-learning) and memory manager (for coordination)
database = new Database(testDbPath);
await database.initialize();
memoryManager = new SwarmMemoryManager(memoryDbPath);
await memoryManager.initialize();
});
afterEach(async () => {
// CRITICAL: Dispose LearningEngine instances to prevent open handles
if (learningEngine) {
learningEngine.dispose();
learningEngine = null;
}
// CRITICAL: Close database connections before cleanup to prevent crashes
try {
if (database && typeof database.close === 'function') {
await database.close();
}
} catch (error) {
// Ignore close errors
}
try {
if (memoryManager && (memoryManager as any).db) {
(memoryManager as any).db.close();
}
} catch (error) {
// Ignore close errors
}
// Clean up test database files
if (fs.existsSync(testDbPath)) {
fs.unlinkSync(testDbPath);
}
if (fs.existsSync(memoryDbPath)) {
fs.unlinkSync(memoryDbPath);
}
});
describe('Q-Value Persistence', () => {
it('should persist Q-values to database when explicitly provided', async () => {
const agentId = 'test-agent-explicit-db';
// Create LearningEngine WITH database parameter (old way)
learningEngine = new LearningEngine(
agentId,
memoryManager,
{ enabled: true },
database // ← Explicitly provided
);
await learningEngine.initialize();
// Create test task and result (NO task_id to avoid FK constraint complexity)
const task = {
// id: undefined, // Don't provide ID to avoid FK constraint to tasks table
type: 'unit-test-generation',
context: {},
requirements: {
capabilities: ['code-analysis', 'test-generation']
}
};
const result: TaskResult = {
success: true,
executionTime: 1500,
errors: [],
coverage: 0.85,
metadata: {
strategy: 'template-based',
toolsUsed: ['ast-parser', 'jest-generator'],
parallelization: 0.5,
retryPolicy: 'exponential',
resourceAllocation: 0.6
}
};
// Record experience (should persist to database)
await learningEngine.recordExperience(task, result);
// Verify Q-values were persisted
const qValues = await database.getAllQValues(agentId);
expect(qValues.length).toBeGreaterThan(0);
expect(qValues[0].q_value).toBeDefined();
expect(qValues[0].state_key).toBeDefined();
expect(qValues[0].action_key).toBeDefined();
});
it('should auto-initialize database if not provided (NEW FIX)', async () => {
const agentId = 'test-agent-auto-init';
// Temporarily set AQE_DB_PATH to test database
const originalPath = process.env.AQE_DB_PATH;
process.env.AQE_DB_PATH = testDbPath;
try {
// Create LearningEngine WITHOUT database parameter (new way - should auto-init)
learningEngine = new LearningEngine(
agentId,
memoryManager,
{ enabled: true }
// ← No 4th parameter - should auto-initialize
);
await learningEngine.initialize();
// Create test task and result
const task = {
id: 'task-002',
type: 'integration-test-generation',
context: {},
requirements: {
capabilities: ['api-testing']
}
};
const result: TaskResult = {
success: true,
executionTime: 2000,
errors: [],
coverage: 0.90,
metadata: {
strategy: 'property-based',
toolsUsed: ['fast-check'],
parallelization: 0.7,
retryPolicy: 'linear',
resourceAllocation: 0.8
}
};
// Record experience (should persist to auto-initialized database)
await learningEngine.recordExperience(task, result);
// Verify Q-values were persisted to auto-initialized database
const qValues = await database.getAllQValues(agentId);
expect(qValues.length).toBeGreaterThan(0);
expect(qValues[0].q_value).toBeGreaterThan(-2); // Valid Q-value range [-2, 2]
expect(qValues[0].q_value).toBeLessThan(2);
} finally {
// Restore environment
if (originalPath) {
process.env.AQE_DB_PATH = originalPath;
} else {
delete process.env.AQE_DB_PATH;
}
}
});
it('should restore Q-values across agent restarts', async () => {
const agentId = 'test-agent-restart';
// Agent 1: Learn from multiple tasks
learningEngine = new LearningEngine(
agentId,
memoryManager,
{ enabled: true },
database
);
await learningEngine.initialize();
// Execute 5 tasks to build Q-table
for (let i = 0; i < 5; i++) {
const task = {
id: `task-00${i}`,
type: 'unit-test-generation',
context: {},
requirements: { capabilities: ['testing'] }
};
const result: TaskResult = {
success: i % 2 === 0, // Alternate success/failure
executionTime: 1000 + i * 200,
errors: [],
coverage: 0.7 + i * 0.05,
metadata: {
strategy: i % 2 === 0 ? 'template-based' : 'property-based',
toolsUsed: ['jest'],
parallelization: 0.5,
retryPolicy: 'exponential',
resourceAllocation: 0.5
}
};
await learningEngine.recordExperience(task, result);
}
// Get Q-table size from first agent
const qValues1 = await database.getAllQValues(agentId);
const firstAgentQTableSize = qValues1.length;
expect(firstAgentQTableSize).toBeGreaterThan(0);
// Dispose first engine before creating second one
learningEngine.dispose();
// Agent 2: Same ID, should restore previous learning
learningEngine = new LearningEngine(
agentId,
memoryManager,
{ enabled: true },
database
);
await learningEngine.initialize();
// Verify Q-table was restored
const qValues2 = await database.getAllQValues(agentId);
expect(qValues2.length).toBe(firstAgentQTableSize);
// Get strategy recommendation (should use learned Q-values)
const recommendation = await learningEngine.recommendStrategy({
taskComplexity: 0.5,
requiredCapabilities: ['testing'],
contextFeatures: {},
previousAttempts: 0,
availableResources: 0.8,
timeConstraint: 30000
});
expect(recommendation.confidence).toBeGreaterThan(0);
expect(recommendation.strategy).toBeDefined();
});
});
describe('Experience Persistence', () => {
it('should store learning experiences in database', async () => {
const agentId = 'test-agent-experiences';
learningEngine = new LearningEngine(
agentId,
memoryManager,
{ enabled: true },
database
);
await learningEngine.initialize();
// Create task with feedback
const task = {
id: 'task-feedback-001',
type: 'security-test-generation',
context: {},
requirements: {
capabilities: ['security-testing', 'owasp']
}
};
const result: TaskResult = {
success: true,
executionTime: 3000,
errors: [],
coverage: 0.95,
metadata: {
strategy: 'threat-modeling',
toolsUsed: ['semgrep', 'bandit'],
parallelization: 0.8,
retryPolicy: 'exponential',
resourceAllocation: 0.9
}
};
const feedback: LearningFeedback = {
rating: 0.9,
issues: [],
suggestions: ['Excellent coverage of OWASP Top 10']
};
// Record experience with feedback
await learningEngine.recordExperience(task, result, feedback);
// Verify experience was stored
const stats = await database.getLearningStatistics(agentId);
expect(stats.totalExperiences).toBeGreaterThan(0);
expect(stats.avgReward).toBeGreaterThan(0);
});
it('should handle high volume of experiences efficiently', async () => {
const agentId = 'test-agent-volume';
learningEngine = new LearningEngine(
agentId,
memoryManager,
{
enabled: true,
batchSize: 10,
updateFrequency: 5
},
database
);
await learningEngine.initialize();
const startTime = Date.now();
// REDUCED: Record only 10 experiences to prevent memory overload in constrained environments
for (let i = 0; i < 10; i++) {
const task = {
id: `volume-task-${i}`,
type: 'test-generation',
context: {},
requirements: { capabilities: ['testing'] }
};
const result: TaskResult = {
success: Math.random() > 0.3,
executionTime: 1000 + Math.random() * 2000,
errors: [],
coverage: 0.6 + Math.random() * 0.4,
metadata: {
strategy: i % 3 === 0 ? 'template' : i % 3 === 1 ? 'property' : 'mutation',
toolsUsed: ['jest'],
parallelization: 0.5,
retryPolicy: 'exponential',
resourceAllocation: 0.5
}
};
await learningEngine.recordExperience(task, result);
}
const duration = Date.now() - startTime;
// Verify all experiences were stored
const stats = await database.getLearningStatistics(agentId);
expect(stats.totalExperiences).toBe(10);
// Performance check: Should complete in under 3 seconds
expect(duration).toBeLessThan(3000);
// Verify Q-table was built
expect(stats.qTableSize).toBeGreaterThan(0);
});
});
describe('Pattern Discovery Persistence', () => {
it('should discover and persist patterns across tasks', async () => {
const agentId = 'test-agent-patterns';
learningEngine = new LearningEngine(
agentId,
memoryManager,
{ enabled: true },
database
);
await learningEngine.initialize();
// Execute similar tasks to discover pattern
for (let i = 0; i < 10; i++) {
const task = {
id: `pattern-task-${i}`,
type: 'api-test-generation', // Same type
context: {},
requirements: { capabilities: ['api-testing'] }
};
const result: TaskResult = {
success: true, // Always successful
executionTime: 1500,
errors: [],
coverage: 0.88,
metadata: {
strategy: 'contract-based', // Same strategy
toolsUsed: ['pact'],
parallelization: 0.6,
retryPolicy: 'exponential',
resourceAllocation: 0.7
}
};
await learningEngine.recordExperience(task, result);
}
// Get discovered patterns
const patterns = learningEngine.getPatterns();
expect(patterns.length).toBeGreaterThan(0);
// Verify pattern has increasing confidence and high success rate
const topPattern = patterns[0];
// Confidence starts at 0.5 and increases by 0.01 per use: 0.5 + (10 * 0.01) = 0.6
expect(topPattern.confidence).toBeGreaterThanOrEqual(0.59); // Allow floating point margin
expect(topPattern.confidence).toBeLessThanOrEqual(0.61);
expect(topPattern.successRate).toBe(1.0); // All 10 tasks succeeded
expect(topPattern.usageCount).toBe(10);
});
});
describe('Learning Statistics', () => {
it('should provide accurate statistics from database', async () => {
const agentId = 'test-agent-stats';
learningEngine = new LearningEngine(
agentId,
memoryManager,
{ enabled: true },
database
);
await learningEngine.initialize();
// Record mix of successful and failed experiences
const experiences = [
{ success: true, coverage: 0.85 },
{ success: true, coverage: 0.90 },
{ success: false, coverage: 0.50 },
{ success: true, coverage: 0.88 },
{ success: false, coverage: 0.45 }
];
for (let i = 0; i < experiences.length; i++) {
const exp = experiences[i];
const task = {
id: `stats-task-${i}`,
type: 'test-generation',
context: {},
requirements: { capabilities: ['testing'] }
};
const result: TaskResult = {
success: exp.success,
executionTime: 1500,
errors: exp.success ? [] : [{ message: 'Test failed' }],
coverage: exp.coverage,
metadata: {
strategy: 'template-based',
toolsUsed: ['jest'],
parallelization: 0.5,
retryPolicy: 'exponential',
resourceAllocation: 0.5
}
};
await learningEngine.recordExperience(task, result);
}
// Get statistics from database
const stats = await database.getLearningStatistics(agentId);
expect(stats.totalExperiences).toBe(5);
// Verify average reward is positive (more successes than failures: 3 vs 2)
expect(stats.avgReward).toBeGreaterThan(0);
expect(stats.avgReward).toBeLessThan(2); // Within valid reward range
expect(stats.qTableSize).toBeGreaterThan(0);
});
});
});