Files
ruflo/v2/bin/training-pipeline.js
T
rUvandClaude Opus 4.5 1b6a1971f7 feat(v3): add comprehensive v3 implementation plans and swarm architecture
- Reorganize v3/ into implementation/ subfolders:
  - planning/, architecture/, migration/, integration/
  - security/, adrs/, research/, swarm-plans/

- Add 15-agent concurrent swarm implementation plans:
  - SWARM-OVERVIEW.md: Hierarchical mesh topology, 4 phases
  - AGENT-SPECIFICATIONS.md: 15 agent configs with capabilities
  - TDD-LONDON-SCHOOL-PLAN.md: Mock-first TDD methodology
  - BENCHMARK-OPTIMIZATION.md: Performance targets (2.49x-7.47x)
  - DEPLOYMENT-PLAN.md: Release strategy, CI/CD, rollback
  - GITHUB-ISSUE-TRACKING.md: Issue workflow with agent replies

- Update .claude/settings.json with proper hooks format
- Add bash aliases for claude --dangerously-skip-permissions

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-04 15:40:51 +00:00

861 lines
26 KiB
JavaScript

#!/usr/bin/env node
/**
* Training Pipeline with Real Task Execution
* Executes actual code and learns from real test results
*/
import fs from 'fs/promises';
import path from 'path';
import { execSync, spawn } from 'child_process';
import { promisify } from 'util';
import { exec } from 'child_process';
const execAsync = promisify(exec);
/**
* Training Pipeline that executes real tasks and learns from actual results
*/
export class TrainingPipeline {
constructor() {
this.pipelineConfig = '.claude-flow/pipeline-config.json';
this.trainingLog = '.claude-flow/training/pipeline-log.jsonl';
this.improvementMetrics = '.claude-flow/metrics/improvements.json';
this.agentProfiles = '.claude-flow/agents/profiles.json';
this.realTasksDir = '.claude-flow/training/real-tasks';
this.initialized = false;
}
/**
* Initialize the training pipeline
*/
async initialize() {
// Create necessary directories
const dirs = [
'.claude-flow/pipeline',
'.claude-flow/training',
'.claude-flow/training/real-tasks',
'.claude-flow/metrics',
'.claude-flow/agents',
'.claude-flow/validation',
'.claude-flow/benchmarks'
];
for (const dir of dirs) {
await fs.mkdir(dir, { recursive: true });
}
// Load or create pipeline configuration
await this.loadPipelineConfig();
console.log('🚀 Training Pipeline initialized');
this.initialized = true;
}
/**
* STAGE 1: Generate Training Tasks
* Creates actual code files that can be tested
*/
async generateTrainingTasks(complexity = 'medium') {
const taskId = Date.now();
const taskDir = path.join(this.realTasksDir, `task-${taskId}`);
await fs.mkdir(taskDir, { recursive: true });
const tasks = {
easy: [
{
type: 'function',
name: 'validateEmail',
task: 'Create email validation function',
code: `function validateEmail(email) {
const regex = /^[^\\s@]+@[^\\s@]+\\.[^\\s@]+$/;
return regex.test(email);
}
module.exports = { validateEmail };`,
test: `
const { validateEmail } = require('./index');
describe('validateEmail', () => {
test('validates correct email', () => {
expect(validateEmail('test@example.com')).toBe(true);
});
test('rejects invalid email', () => {
expect(validateEmail('invalid')).toBe(false);
});
});
`
}
],
medium: [
{
type: 'api',
name: 'userApi',
task: 'Build user API endpoint',
code: `
const express = require('express');
const app = express();
app.use(express.json());
const users = [];
app.get('/users', (req, res) => {
res.json(users);
});
app.post('/users', (req, res) => {
const { name, email } = req.body;
if (!name || !email) {
return res.status(400).json({ error: 'Name and email required' });
}
const user = { id: users.length + 1, name, email };
users.push(user);
res.status(201).json(user);
});
module.exports = app;
`,
test: `
const request = require('supertest');
const app = require('./index');
describe('User API', () => {
test('GET /users returns empty array initially', async () => {
const res = await request(app).get('/users');
expect(res.status).toBe(200);
expect(res.body).toEqual([]);
});
test('POST /users creates a user', async () => {
const res = await request(app)
.post('/users')
.send({ name: 'Test', email: 'test@test.com' });
expect(res.status).toBe(201);
expect(res.body.name).toBe('Test');
});
});
`
}
],
hard: [
{
type: 'algorithm',
name: 'sortAlgorithm',
task: 'Implement efficient sorting',
code: `
function quickSort(arr) {
if (arr.length <= 1) return arr;
const pivot = arr[Math.floor(arr.length / 2)];
const left = arr.filter(x => x < pivot);
const middle = arr.filter(x => x === pivot);
const right = arr.filter(x => x > pivot);
return [...quickSort(left), ...middle, ...quickSort(right)];
}
function mergeSort(arr) {
if (arr.length <= 1) return arr;
const mid = Math.floor(arr.length / 2);
const left = mergeSort(arr.slice(0, mid));
const right = mergeSort(arr.slice(mid));
const result = [];
let i = 0, j = 0;
while (i < left.length && j < right.length) {
if (left[i] < right[j]) {
result.push(left[i++]);
} else {
result.push(right[j++]);
}
}
return result.concat(left.slice(i)).concat(right.slice(j));
}
module.exports = { quickSort, mergeSort };
`,
test: `
const { quickSort, mergeSort } = require('./index');
describe('Sorting Algorithms', () => {
const unsorted = [3, 1, 4, 1, 5, 9, 2, 6];
const sorted = [1, 1, 2, 3, 4, 5, 6, 9];
test('quickSort sorts correctly', () => {
expect(quickSort(unsorted)).toEqual(sorted);
});
test('mergeSort sorts correctly', () => {
expect(mergeSort(unsorted)).toEqual(sorted);
});
test('handles empty arrays', () => {
expect(quickSort([])).toEqual([]);
expect(mergeSort([])).toEqual([]);
});
});
`
}
]
};
const selectedTasks = tasks[complexity] || tasks.medium;
const realTasks = [];
// Create real task files
for (const task of selectedTasks) {
const projectDir = path.join(taskDir, task.name);
await fs.mkdir(projectDir, { recursive: true });
// Write actual code file
await fs.writeFile(path.join(projectDir, 'index.js'), task.code);
// Write test file
await fs.writeFile(path.join(projectDir, 'index.test.js'), task.test);
// Create package.json with real dependencies
const packageJson = {
name: task.name,
version: "1.0.0",
scripts: {
test: "jest --silent",
lint: "eslint index.js || true",
typecheck: "echo 'No TypeScript' || true"
},
devDependencies: {
jest: "^29.0.0",
eslint: "^8.0.0",
supertest: "^6.0.0"
},
dependencies: {
express: task.type === 'api' ? "^4.18.0" : undefined
}
};
await fs.writeFile(
path.join(projectDir, 'package.json'),
JSON.stringify(packageJson, null, 2)
);
realTasks.push({
...task,
projectDir,
taskId
});
}
console.log(`📝 Generated ${realTasks.length} ${complexity} training tasks`);
return realTasks;
}
/**
* STAGE 2: Execute Tasks with Different Strategies
* Actually runs npm install, tests, and measures real performance
*/
async executeTrainingRun(tasks, agentConfig = {}) {
const results = [];
for (const task of tasks) {
console.log(`\n🔄 Executing task: ${task.task}`);
// Install dependencies (only once per task)
try {
console.log(` 📦 Installing dependencies...`);
execSync('npm install --silent', {
cwd: task.projectDir,
stdio: 'pipe'
});
} catch (e) {
console.log(` ⚠️ Install warning: ${e.message.slice(0, 50)}`);
}
// Test different strategies with REAL variations
const strategies = agentConfig.strategies || ['conservative', 'balanced', 'aggressive'];
for (const strategy of strategies) {
const result = await this.executeTaskWithStrategy(task, strategy);
results.push({
task: task.task,
type: task.type,
strategy,
...result,
timestamp: new Date().toISOString()
});
}
}
// Save real results
const resultsFile = `.claude-flow/training/real-results-${Date.now()}.json`;
await fs.writeFile(resultsFile, JSON.stringify(results, null, 2));
return results;
}
/**
* Execute a task with a specific strategy
* Strategies affect how we modify and test the code
*/
async executeTaskWithStrategy(task, strategy) {
const startTime = Date.now();
const checks = {};
// Save original code
const originalCode = await fs.readFile(path.join(task.projectDir, 'index.js'), 'utf8');
// Modify code based on strategy (but more carefully!)
if (strategy === 'aggressive') {
// Aggressive: Skip some validation (but keep valid syntax)
const aggressiveCode = originalCode.replace(
/if \(!(\w+)\)/g,
'if (false && !$1)'
);
await fs.writeFile(path.join(task.projectDir, 'index.js'), aggressiveCode);
} else if (strategy === 'conservative') {
// Conservative: Add validation at the top of functions
const conservativeCode = originalCode.replace(
/function (\w+)\((.*?)\) {/g,
'function $1($2) {\n // Extra validation for conservative strategy\n if (arguments.length === 0) throw new Error("No arguments provided");'
);
await fs.writeFile(path.join(task.projectDir, 'index.js'), conservativeCode);
}
// Balanced: Keep original code
// Run REAL tests
try {
const testResult = execSync('npm test', {
cwd: task.projectDir,
encoding: 'utf8',
stdio: 'pipe'
});
checks.test = {
passed: true,
score: 1.0,
output: testResult.slice(0, 100)
};
} catch (e) {
checks.test = {
passed: false,
score: 0.3,
error: e.message.slice(0, 100)
};
}
// Run REAL lint
try {
const lintResult = execSync('npm run lint', {
cwd: task.projectDir,
encoding: 'utf8',
stdio: 'pipe'
});
const hasErrors = lintResult.includes('error');
checks.lint = {
passed: !hasErrors,
score: hasErrors ? 0.5 : 1.0
};
} catch (e) {
checks.lint = {
passed: false,
score: 0.3
};
}
// Restore original code after testing
await fs.writeFile(path.join(task.projectDir, 'index.js'), originalCode);
// Calculate REAL performance metrics
const executionTime = Date.now() - startTime;
const successRate = Object.values(checks).filter(c => c.passed).length / Object.values(checks).length;
// Strategy-specific scoring based on REAL results
let strategyBonus = 0;
if (strategy === 'aggressive' && executionTime < 1000) {
strategyBonus = 0.2; // Bonus for fast execution
} else if (strategy === 'conservative' && successRate === 1.0) {
strategyBonus = 0.3; // Bonus for perfect reliability
} else if (strategy === 'balanced' && successRate > 0.5 && executionTime < 2000) {
strategyBonus = 0.25; // Bonus for good balance
}
const score = (successRate * 60) + (Math.max(0, 1 - executionTime/5000) * 20) + (strategyBonus * 20);
return {
executionTime,
successRate,
checks,
strategy,
score,
real: true // Mark as real execution
};
}
/**
* STAGE 3: Learn from Results
* Updates profiles based on actual performance
*/
async learnFromResults(results) {
console.log('\n🧠 Learning from results...');
// Load current agent profiles
let profiles = {};
try {
const data = await fs.readFile(this.agentProfiles, 'utf8');
profiles = JSON.parse(data);
} catch {
profiles = this.getDefaultProfiles();
}
// Analyze REAL results by strategy
const strategyPerformance = {};
for (const result of results) {
if (!strategyPerformance[result.strategy]) {
strategyPerformance[result.strategy] = {
totalScore: 0,
count: 0,
avgExecutionTime: 0,
successRate: 0,
realExecutions: 0
};
}
const perf = strategyPerformance[result.strategy];
perf.totalScore += result.score;
perf.count++;
perf.avgExecutionTime += result.executionTime;
perf.successRate += result.successRate;
if (result.real) perf.realExecutions++;
}
// Calculate averages and update profiles with REAL data
for (const [strategy, perf] of Object.entries(strategyPerformance)) {
perf.avgScore = perf.totalScore / perf.count;
perf.avgExecutionTime = perf.avgExecutionTime / perf.count;
perf.successRate = perf.successRate / perf.count;
// Update with stronger learning for real executions
const learningRate = perf.realExecutions > 0 ? 0.4 : 0.1;
this.updateAgentProfile(profiles, strategy, perf, learningRate);
}
// Save updated profiles
await fs.writeFile(this.agentProfiles, JSON.stringify(profiles, null, 2));
// Generate recommendations based on performance
const recommendations = this.generateRecommendations(strategyPerformance);
console.log('\n📊 Learning Results:');
for (const [strategy, perf] of Object.entries(strategyPerformance)) {
console.log(` ${strategy}: Score ${perf.avgScore.toFixed(2)}, Success ${(perf.successRate * 100).toFixed(1)}%, Time ${perf.avgExecutionTime.toFixed(0)}ms`);
}
return { profiles, recommendations };
}
updateAgentProfile(profiles, strategy, performance, learningRate = 0.3) {
if (!profiles[strategy]) {
profiles[strategy] = {
successRate: 0.5,
avgScore: 50,
avgExecutionTime: 2000,
uses: 0,
realExecutions: 0
};
}
const profile = profiles[strategy];
// Update with exponential moving average
profile.successRate = profile.successRate * (1 - learningRate) + performance.successRate * learningRate;
profile.avgScore = profile.avgScore * (1 - learningRate) + performance.avgScore * learningRate;
profile.avgExecutionTime = profile.avgExecutionTime * (1 - learningRate) + performance.avgExecutionTime * learningRate;
profile.uses++;
if (performance.realExecutions) {
profile.realExecutions = (profile.realExecutions || 0) + performance.realExecutions;
}
// Add performance trend
if (!profile.trend) profile.trend = [];
profile.trend.push({
score: performance.avgScore,
timestamp: new Date().toISOString(),
real: performance.realExecutions > 0
});
if (profile.trend.length > 20) {
profile.trend = profile.trend.slice(-20);
}
// Mark improvement
if (profile.trend.length > 1) {
const recent = profile.trend.slice(-5).reduce((sum, t) => sum + t.score, 0) / Math.min(5, profile.trend.length);
const older = profile.trend.slice(0, -5).reduce((sum, t) => sum + t.score, 0) / Math.max(1, profile.trend.length - 5);
profile.improving = recent > older;
profile.improvementRate = ((recent - older) / older * 100).toFixed(1);
}
}
generateRecommendations(strategyPerformance) {
const recommendations = [];
for (const [strategy, perf] of Object.entries(strategyPerformance)) {
if (perf.successRate < 0.7) {
recommendations.push({
type: 'improve_reliability',
strategy,
action: `${strategy} needs better error handling (${(perf.successRate * 100).toFixed(1)}% success)`,
priority: 'high'
});
}
if (perf.avgExecutionTime > 3000) {
recommendations.push({
type: 'optimize_speed',
strategy,
action: `${strategy} is too slow (${perf.avgExecutionTime.toFixed(0)}ms avg)`,
priority: 'medium'
});
}
if (perf.avgScore > 75) {
recommendations.push({
type: 'good_performance',
strategy,
action: `${strategy} performing well (${perf.avgScore.toFixed(1)} score)`,
priority: 'info'
});
}
}
return recommendations;
}
/**
* Full Training Pipeline Execution
*/
async runFullPipeline(options = {}) {
const {
complexity = 'medium',
iterations = 3,
validate = true
} = options;
console.log('🎯 Starting Training Pipeline');
console.log('━'.repeat(50));
await this.initialize();
// Capture baseline metrics
const baselineMetrics = await this.captureMetrics();
let cumulativeResults = [];
for (let i = 1; i <= iterations; i++) {
console.log(`\n📍 Iteration ${i}/${iterations}`);
console.log('─'.repeat(40));
// Stage 1: Generate tasks
const tasks = await this.generateTrainingTasks(complexity);
// Stage 2: Execute with real code
const results = await this.executeTrainingRun(tasks);
cumulativeResults = [...cumulativeResults, ...results];
// Stage 3: Learn from results
const { profiles, recommendations } = await this.learnFromResults(results);
// Show recommendations
if (recommendations.length > 0) {
console.log('\n💡 Recommendations:');
for (const rec of recommendations.slice(0, 3)) {
console.log(` • ${rec.action}`);
}
}
// Stage 4: Validate if enabled
if (validate && i === iterations) {
const currentMetrics = await this.captureMetrics();
const validation = await this.validateImprovements(baselineMetrics, currentMetrics);
if (validation.summary.overallImprovement) {
console.log('✅ Improvement detected!');
} else {
console.log('⚠️ More training needed for significant improvement');
}
}
}
// Generate final report
const report = await this.generateFinalReport(cumulativeResults);
console.log('\n' + report);
return {
success: true,
totalTasks: cumulativeResults.length,
realExecutions: cumulativeResults.filter(r => r.real).length,
improvements: await this.calculateOverallImprovement(baselineMetrics)
};
}
// Reuse helper methods from original
async captureMetrics() {
try {
const data = await fs.readFile(this.agentProfiles, 'utf8');
const profiles = JSON.parse(data);
// Calculate weighted average from all strategies
let totalScore = 0;
let totalSuccess = 0;
let totalTime = 0;
let count = 0;
for (const profile of Object.values(profiles)) {
if (profile.uses > 0) {
totalScore += profile.avgScore;
totalSuccess += profile.successRate;
totalTime += profile.avgExecutionTime;
count++;
}
}
return {
successRate: count > 0 ? totalSuccess / count : 0,
executionTime: count > 0 ? totalTime / count : 0,
score: count > 0 ? totalScore / count : 0
};
} catch {
return {
successRate: 0,
executionTime: 0,
score: 0
};
}
}
async validateImprovements(beforeMetrics, afterMetrics) {
const validation = {
improved: [],
declined: [],
unchanged: [],
summary: {}
};
const metrics = ['successRate', 'executionTime', 'score'];
for (const metric of metrics) {
const before = beforeMetrics[metric] || 0.01; // Avoid division by zero
const after = afterMetrics[metric] || 0;
const change = after - before;
const percentChange = (change / before) * 100;
if (percentChange > 5) {
validation.improved.push({ metric, change: percentChange });
} else if (percentChange < -5) {
validation.declined.push({ metric, change: percentChange });
} else {
validation.unchanged.push({ metric, change: percentChange });
}
}
validation.summary = {
overallImprovement: validation.improved.length > validation.declined.length,
improvementScore: validation.improved.length - validation.declined.length,
timestamp: new Date().toISOString()
};
console.log(`\n✅ Validating improvements...`);
console.log(` Improved: ${validation.improved.length} metrics`);
console.log(` Declined: ${validation.declined.length} metrics`);
console.log(` Unchanged: ${validation.unchanged.length} metrics`);
return validation;
}
async calculateOverallImprovement(baselineMetrics) {
const currentMetrics = await this.captureMetrics();
const base = {
successRate: baselineMetrics.successRate || 0.01,
executionTime: baselineMetrics.executionTime || 1,
score: baselineMetrics.score || 0.01
};
return {
successRate: ((currentMetrics.successRate - base.successRate) / base.successRate) * 100,
executionTime: ((base.executionTime - currentMetrics.executionTime) / base.executionTime) * 100,
score: ((currentMetrics.score - base.score) / base.score) * 100
};
}
async generateFinalReport(results) {
const realResults = results.filter(r => r.real);
const successRates = {};
const scores = {};
const times = {};
for (const result of realResults) {
if (!successRates[result.strategy]) {
successRates[result.strategy] = [];
scores[result.strategy] = [];
times[result.strategy] = [];
}
successRates[result.strategy].push(result.successRate);
scores[result.strategy].push(result.score);
times[result.strategy].push(result.executionTime);
}
let report = '📊 Training Pipeline Report\n';
report += '━'.repeat(50) + '\n\n';
for (const strategy of Object.keys(successRates)) {
const avgSuccess = successRates[strategy].reduce((a, b) => a + b, 0) / successRates[strategy].length;
const avgScore = scores[strategy].reduce((a, b) => a + b, 0) / scores[strategy].length;
const avgTime = times[strategy].reduce((a, b) => a + b, 0) / times[strategy].length;
report += `Strategy: ${strategy}\n`;
report += ` Average Success Rate: ${(avgSuccess * 100).toFixed(1)}%\n`;
report += ` Average Score: ${avgScore.toFixed(2)}\n`;
report += ` Average Time: ${avgTime.toFixed(0)}ms\n`;
report += ` Real Executions: ${successRates[strategy].length}\n`;
report += '\n';
}
return report;
}
getDefaultProfiles() {
return {
conservative: {
successRate: 0.5,
avgScore: 50,
avgExecutionTime: 3000,
uses: 0,
realExecutions: 0
},
balanced: {
successRate: 0.5,
avgScore: 50,
avgExecutionTime: 2000,
uses: 0,
realExecutions: 0
},
aggressive: {
successRate: 0.5,
avgScore: 50,
avgExecutionTime: 1000,
uses: 0,
realExecutions: 0
}
};
}
async loadPipelineConfig() {
try {
const data = await fs.readFile(this.pipelineConfig, 'utf8');
return JSON.parse(data);
} catch {
const defaultConfig = {
version: '2.0.0',
strategies: ['conservative', 'balanced', 'aggressive'],
learningRate: 0.4,
minSamplesForUpdate: 3,
useRealExecution: true,
created: new Date().toISOString()
};
await fs.writeFile(this.pipelineConfig, JSON.stringify(defaultConfig, null, 2));
return defaultConfig;
}
}
}
/**
* CLI Command Handler
*/
export async function trainingPipelineCommand(args, flags) {
const pipeline = new TrainingPipeline();
const subcommand = args[0] || 'run';
switch (subcommand) {
case 'run':
// Run real pipeline
const options = {
complexity: flags.complexity || 'medium',
iterations: parseInt(flags.iterations) || 3,
validate: flags.validate !== false
};
console.log('🚀 Starting Training Pipeline');
console.log(` Complexity: ${options.complexity}`);
console.log(` Iterations: ${options.iterations}`);
console.log(` Validation: ${options.validate ? 'Enabled' : 'Disabled'}`);
const result = await pipeline.runFullPipeline(options);
if (result.success) {
console.log('\n✅ Training Pipeline completed');
console.log(` Total tasks: ${result.totalTasks}`);
console.log(` Real executions: ${result.realExecutions}`);
if (result.improvements) {
console.log('\n📈 Improvements:');
console.log(` Success Rate: ${result.improvements.successRate > 0 ? '+' : ''}${result.improvements.successRate.toFixed(1)}%`);
console.log(` Execution Time: ${result.improvements.executionTime > 0 ? '+' : ''}${result.improvements.executionTime.toFixed(1)}%`);
console.log(` Score: ${result.improvements.score > 0 ? '+' : ''}${result.improvements.score.toFixed(1)}%`);
}
}
break;
case 'status':
// Show real pipeline status
await pipeline.initialize();
let profiles = {};
try {
const data = await fs.readFile(pipeline.agentProfiles, 'utf8');
profiles = JSON.parse(data);
} catch {
profiles = pipeline.getDefaultProfiles();
}
console.log('\n📊 Training Pipeline Status');
console.log('━'.repeat(50));
console.log('\n🤖 Strategy Profiles:');
for (const [strategy, profile] of Object.entries(profiles)) {
console.log(` ${strategy}:`);
console.log(` Success Rate: ${(profile.successRate * 100).toFixed(1)}%`);
console.log(` Average Score: ${profile.avgScore.toFixed(2)}`);
console.log(` Execution Time: ${profile.avgExecutionTime.toFixed(0)}ms`);
console.log(` Total Uses: ${profile.uses}`);
console.log(` Real Executions: ${profile.realExecutions || 0}`);
if (profile.improving !== undefined) {
console.log(` Trend: ${profile.improving ? '📈 Improving' : '📉 Declining'} (${profile.improvementRate}%)`);
}
}
break;
case 'validate':
// Validate current performance
await pipeline.initialize();
const metrics = await pipeline.captureMetrics();
console.log('\n📊 Current Performance:');
console.log(` Success Rate: ${(metrics.successRate * 100).toFixed(1)}%`);
console.log(` Avg Execution Time: ${metrics.executionTime.toFixed(0)}ms`);
console.log(` Average Score: ${metrics.score.toFixed(2)}`);
break;
default:
console.log('Usage: train-pipeline <command> [options]');
console.log('\nCommands:');
console.log(' run - Run training pipeline with real code');
console.log(' status - Show pipeline status');
console.log(' validate - Check current performance');
console.log('\nOptions:');
console.log(' --complexity <level> - easy/medium/hard');
console.log(' --iterations <n> - Training cycles');
}
}
export default TrainingPipeline;