mirror of
https://github.com/ruvnet/ruflo.git
synced 2026-11-25 06:36:49 +09:00
Remove Math.random() metrics from 6 locations: - agent_health: CPU/memory/latency were random — now null with notes - system_health: all checks were hardcoded "healthy" — now real fs checks - coordination_metrics: latency/throughput were random — now null with real sync data - github-tools: all 5 tools fabricated — now return _stub:true, local CRUD only - process monitor: all metrics random — now real os.loadavg/memoryUsage - hook-handler route: random latency, hardcoded matches — removed Fix 6 bugs: - task-tools path mismatch: agents.json → agents/store.json - token-optimizer double-increment of editsOptimized - config get: now reads real configManager instead of hardcoded map - config providers: now reads/writes real config, mutation flags work - session restore: now syncs to sql.js database after legacy JSON write - claims rebalance: now executes moves when dryRun=false Mark stubs honestly (_stub:true): - neural compress/optimize - performance bottleneck/profile/optimize - hooks metrics/pretrain/intelligence-reset (reset now actually deletes files) - workflow execute/resume (steps stay pending, not auto-completed) - hooks session-end summary: reads real counts from stores - hooks explain: reads real success rate from routing-outcomes.json - hooks transfer: returns failure instead of demo data when source empty - status.ts: performance claims → 'not measured' Co-Authored-By: claude-flow <ruv@ruv.net>
307 lines
8.1 KiB
TypeScript
307 lines
8.1 KiB
TypeScript
/**
|
|
* Token Optimizer - Integrates agentic-flow Agent Booster capabilities
|
|
*
|
|
* Combines:
|
|
* - Agent Booster (352x code edit speedup)
|
|
* - ReasoningBank (32% token reduction via semantic retrieval)
|
|
* - Configuration Tuning (batch/cache/topology optimization)
|
|
*
|
|
* @module v3/integration/token-optimizer
|
|
*/
|
|
|
|
import { EventEmitter } from 'events';
|
|
|
|
// Types for agentic-flow integration
|
|
interface TokenStats {
|
|
saved: number;
|
|
baseline: number;
|
|
reduction: number;
|
|
method: string;
|
|
}
|
|
|
|
interface MemoryContext {
|
|
query: string;
|
|
memories: Array<{ content: string; score: number }>;
|
|
compactPrompt: string;
|
|
tokensSaved: number;
|
|
}
|
|
|
|
interface EditOptimization {
|
|
speedupFactor: number;
|
|
executionMs: number;
|
|
method: 'agent-booster' | 'traditional';
|
|
}
|
|
|
|
// Dynamic import helper to handle module resolution
|
|
async function safeImport<T>(modulePath: string): Promise<T | null> {
|
|
try {
|
|
return await import(modulePath);
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Token Optimizer - Reduces token usage via agentic-flow integration
|
|
*/
|
|
export class TokenOptimizer extends EventEmitter {
|
|
private stats = {
|
|
totalTokensSaved: 0,
|
|
editsOptimized: 0,
|
|
cacheHits: 0,
|
|
cacheMisses: 0,
|
|
memoriesRetrieved: 0,
|
|
};
|
|
|
|
private agenticFlowAvailable = false;
|
|
private reasoningBank: any = null;
|
|
private agentBooster: any = null;
|
|
private configTuning: any = null;
|
|
private localCache = new Map<string, { data: any; timestamp: number }>();
|
|
|
|
async initialize(): Promise<void> {
|
|
try {
|
|
// Dynamic import of agentic-flow main module
|
|
const af = await safeImport<any>('agentic-flow');
|
|
|
|
if (af) {
|
|
this.agenticFlowAvailable = true;
|
|
|
|
// Load ReasoningBank (exported path)
|
|
const rb = await safeImport<any>('agentic-flow/reasoningbank');
|
|
if (rb && rb.retrieveMemories) {
|
|
this.reasoningBank = rb;
|
|
}
|
|
|
|
// Load Agent Booster (exported path)
|
|
const ab = await safeImport<any>('agentic-flow/agent-booster');
|
|
if (ab) {
|
|
// Agent booster may export different API
|
|
this.agentBooster = ab.agentBooster || ab.AgentBooster || ab;
|
|
}
|
|
|
|
// Config tuning is part of main module or agent-booster
|
|
// Use our fallback with anti-drift defaults
|
|
if (af.configTuning) {
|
|
this.configTuning = af.configTuning;
|
|
}
|
|
}
|
|
} catch {
|
|
this.agenticFlowAvailable = false;
|
|
}
|
|
|
|
this.emit('initialized', {
|
|
agenticFlowAvailable: this.agenticFlowAvailable,
|
|
reasoningBank: !!this.reasoningBank,
|
|
agentBooster: !!this.agentBooster,
|
|
configTuning: !!this.configTuning,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Retrieve compact context instead of full file content
|
|
* Saves ~32% tokens via semantic retrieval
|
|
*/
|
|
async getCompactContext(query: string, options?: {
|
|
limit?: number;
|
|
threshold?: number;
|
|
}): Promise<MemoryContext> {
|
|
const limit = options?.limit ?? 5;
|
|
const threshold = options?.threshold ?? 0.7;
|
|
|
|
if (!this.reasoningBank) {
|
|
// Fallback: return empty context
|
|
return {
|
|
query,
|
|
memories: [],
|
|
compactPrompt: '',
|
|
tokensSaved: 0,
|
|
};
|
|
}
|
|
|
|
const memories = await this.reasoningBank.retrieveMemories(query, {
|
|
limit,
|
|
threshold,
|
|
});
|
|
|
|
const compactPrompt = this.reasoningBank.formatMemoriesForPrompt(memories);
|
|
|
|
// Estimate tokens saved based on actual content length difference
|
|
// Rough heuristic: ~4 chars per token, compare full query context vs compact
|
|
const queryTokenEstimate = Math.ceil(query.length / 4);
|
|
const compactTokenEstimate = Math.ceil(compactPrompt.length / 4);
|
|
const saved = Math.max(0, queryTokenEstimate - compactTokenEstimate);
|
|
|
|
this.stats.totalTokensSaved += saved;
|
|
this.stats.memoriesRetrieved += memories.length;
|
|
|
|
return {
|
|
query,
|
|
memories,
|
|
compactPrompt,
|
|
tokensSaved: saved,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Optimized code edit using Agent Booster (352x faster)
|
|
* Faster edits = fewer timeouts = fewer retry tokens
|
|
*/
|
|
async optimizedEdit(
|
|
filePath: string,
|
|
oldContent: string,
|
|
newContent: string,
|
|
language: string
|
|
): Promise<EditOptimization> {
|
|
if (!this.agentBooster) {
|
|
// Fallback: no optimization available
|
|
return {
|
|
speedupFactor: 1,
|
|
executionMs: 0,
|
|
method: 'traditional',
|
|
};
|
|
}
|
|
|
|
const result = await this.agentBooster.editCode({
|
|
filePath,
|
|
oldContent,
|
|
newContent,
|
|
language,
|
|
});
|
|
|
|
// Track optimized edits (no fabricated token savings — actual savings
|
|
// come from fewer retries, which we can't measure here)
|
|
if (result.method === 'agent-booster') {
|
|
this.stats.editsOptimized++;
|
|
}
|
|
|
|
return {
|
|
speedupFactor: result.speedupFactor,
|
|
executionMs: result.executionTimeMs,
|
|
method: result.method,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Get optimal swarm configuration to prevent failures
|
|
* 100% success rate = no wasted retry tokens
|
|
*/
|
|
getOptimalConfig(agentCount: number): {
|
|
batchSize: number;
|
|
cacheSizeMB: number;
|
|
topology: string;
|
|
expectedSuccessRate: number;
|
|
} {
|
|
if (!this.configTuning) {
|
|
// Scale defaults based on agent count
|
|
const batchSize = agentCount <= 4 ? 2 : agentCount <= 8 ? 4 : 6;
|
|
const cacheSizeMB = Math.min(200, 25 * Math.ceil(agentCount / 2));
|
|
const topology = agentCount <= 6 ? 'hierarchical' : agentCount <= 12 ? 'hierarchical-mesh' : 'mesh';
|
|
return {
|
|
batchSize,
|
|
cacheSizeMB,
|
|
topology,
|
|
expectedSuccessRate: agentCount <= 8 ? 0.95 : 0.90,
|
|
};
|
|
}
|
|
|
|
const batch = this.configTuning.getOptimalBatchSize();
|
|
const cache = this.configTuning.getOptimalCacheConfig();
|
|
const topo = this.configTuning.selectTopology(agentCount);
|
|
|
|
return {
|
|
batchSize: batch.size,
|
|
cacheSizeMB: cache.sizeMB,
|
|
topology: topo.topology,
|
|
expectedSuccessRate: batch.expectedSuccessRate,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Cache-aware embedding lookup
|
|
* 95% hit rate = 95% fewer embedding API calls
|
|
*/
|
|
async cachedLookup<T>(key: string, generator: () => Promise<T>): Promise<T> {
|
|
// Use local cache if configTuning not available
|
|
const cacheEntry = this.localCache.get(key);
|
|
if (cacheEntry && Date.now() - cacheEntry.timestamp < 300000) { // 5 min TTL
|
|
this.stats.cacheHits++;
|
|
return cacheEntry.data as T;
|
|
}
|
|
|
|
if (this.configTuning) {
|
|
const cached = await this.configTuning.cacheGet(key);
|
|
if (cached) {
|
|
this.stats.cacheHits++;
|
|
return cached as T;
|
|
}
|
|
}
|
|
|
|
this.stats.cacheMisses++;
|
|
const result = await generator();
|
|
|
|
// Store in local cache
|
|
this.localCache.set(key, { data: result, timestamp: Date.now() });
|
|
|
|
if (this.configTuning) {
|
|
await this.configTuning.cacheSet(key, result);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Get optimization statistics
|
|
*/
|
|
getStats(): typeof this.stats & {
|
|
agenticFlowAvailable: boolean;
|
|
cacheHitRate: string;
|
|
estimatedMonthlySavings: string;
|
|
} {
|
|
const total = this.stats.cacheHits + this.stats.cacheMisses;
|
|
const hitRate = total > 0 ? (this.stats.cacheHits / total * 100).toFixed(1) : '0';
|
|
|
|
// Estimate $0.01 per 1000 tokens
|
|
const savings = (this.stats.totalTokensSaved / 1000 * 0.01).toFixed(2);
|
|
|
|
return {
|
|
...this.stats,
|
|
agenticFlowAvailable: this.agenticFlowAvailable,
|
|
cacheHitRate: `${hitRate}%`,
|
|
estimatedMonthlySavings: `$${savings}`,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Generate token savings report
|
|
*/
|
|
generateReport(): string {
|
|
const stats = this.getStats();
|
|
return `
|
|
## Token Optimization Report
|
|
|
|
| Metric | Value |
|
|
|--------|-------|
|
|
| Tokens Saved | ${stats.totalTokensSaved.toLocaleString()} |
|
|
| Edits Optimized | ${stats.editsOptimized} |
|
|
| Cache Hit Rate | ${stats.cacheHitRate} |
|
|
| Memories Retrieved | ${stats.memoriesRetrieved} |
|
|
| Est. Monthly Savings | ${stats.estimatedMonthlySavings} |
|
|
| Agentic-Flow Active | ${stats.agenticFlowAvailable ? '✓' : '✗'} |
|
|
`.trim();
|
|
}
|
|
}
|
|
|
|
// Singleton instance
|
|
let optimizer: TokenOptimizer | null = null;
|
|
|
|
export async function getTokenOptimizer(): Promise<TokenOptimizer> {
|
|
if (!optimizer) {
|
|
optimizer = new TokenOptimizer();
|
|
await optimizer.initialize();
|
|
}
|
|
return optimizer;
|
|
}
|
|
|
|
export default TokenOptimizer;
|