/**
* Simple Cleanup Interface
*
* Provides one-click cleanup operations with built-in safety checks
* and user-friendly options for common memory management tasks.
*/
import {
MemoryOptimizationRecommendation,
MemoryUsageAnalysis,
} from "../../interfaces/forgetting.js";
import { MemoryUsageAnalyzer } from "./MemoryUsageAnalyzer.js";
import { MemoryOperationRisk, RiskAssessmentEngine } from "./RiskAssessment.js";
export interface CleanupOption {
id: string;
name: string;
description: string;
userFriendlyDescription: string;
emoji: string;
estimatedTime: string;
safetyLevel: "very_safe" | "safe" | "moderate" | "advanced";
expectedBenefit: string;
requirements: string[];
}
export interface CleanupResult {
success: boolean;
option_used: string;
memories_affected: number;
space_freed_mb: number;
performance_improvement: string;
risk_assessment: MemoryOperationRisk;
warnings: string[];
next_steps: string[];
rollback_available: boolean;
rollback_instructions?: string;
}
export interface CleanupPreview {
option: CleanupOption;
estimated_impact: {
memories_to_remove: number;
space_to_free_mb: number;
performance_gain: string;
risk_level: string;
};
risk_assessment: MemoryOperationRisk;
safety_checks: {
backup_created: boolean;
rollback_available: boolean;
user_consent_needed: boolean;
protected_memories_safe: boolean;
};
preview_items: Array<{
memory_id: string;
summary: string;
importance: number;
age_days: number;
reason_for_removal: string;
}>;
}
export class SimpleCleanupManager {
private memoryAnalyzer: MemoryUsageAnalyzer;
private riskEngine: typeof RiskAssessmentEngine;
constructor() {
this.memoryAnalyzer = new MemoryUsageAnalyzer();
this.riskEngine = RiskAssessmentEngine;
}
getAvailableCleanupOptions(): CleanupOption[] {
return [
{
id: "quick_tidy",
name: "Quick Tidy",
description: "Remove obviously unimportant memories (importance < 0.1)",
userFriendlyDescription:
"π§Ή Clean up clearly unimportant memories like temporary notes and random thoughts. Very safe with minimal impact.",
emoji: "π§Ή",
estimatedTime: "30 seconds",
safetyLevel: "very_safe",
expectedBenefit: "Small performance boost, reduced clutter",
requirements: ["No user consent needed", "Automatic backup"],
},
{
id: "smart_cleanup",
name: "Smart Cleanup",
description: "Remove low-importance memories older than 30 days",
userFriendlyDescription:
"π€ Intelligently remove old, unimportant memories while protecting anything valuable. Recommended for most users.",
emoji: "π€",
estimatedTime: "2-5 minutes",
safetyLevel: "safe",
expectedBenefit:
"Moderate performance improvement, better organization",
requirements: ["Backup created", "Rollback available for 7 days"],
},
{
id: "deep_clean",
name: "Deep Clean",
description: "Comprehensive cleanup including rarely accessed memories",
userFriendlyDescription:
"π Thorough cleanup that removes rarely used memories and optimizes storage. More aggressive but still safe.",
emoji: "π",
estimatedTime: "5-15 minutes",
safetyLevel: "moderate",
expectedBenefit:
"Significant performance improvement, major space savings",
requirements: [
"User review recommended",
"Full backup created",
"Gradual execution",
],
},
{
id: "performance_boost",
name: "Performance Boost",
description: "Aggressive optimization focused on speed improvement",
userFriendlyDescription:
"π Maximum performance optimization by removing all non-essential memories. Use when speed is critical.",
emoji: "π",
estimatedTime: "10-30 minutes",
safetyLevel: "advanced",
expectedBenefit: "Major performance improvement, maximum speed",
requirements: [
"User consent required",
"Advanced backup",
"Expert review recommended",
],
},
{
id: "storage_saver",
name: "Storage Saver",
description: "Focus on freeing up memory space",
userFriendlyDescription:
"πΎ Optimize for memory usage by compressing and archiving large memories. Preserves content while saving space.",
emoji: "πΎ",
estimatedTime: "3-10 minutes",
safetyLevel: "safe",
expectedBenefit: "Major space savings, preserved functionality",
requirements: ["Compression backup", "Archive access available"],
},
{
id: "privacy_cleanup",
name: "Privacy Cleanup",
description: "Remove memories based on privacy settings",
userFriendlyDescription:
"π Clean up memories while respecting privacy settings and ensuring secure deletion of sensitive information.",
emoji: "π",
estimatedTime: "5-20 minutes",
safetyLevel: "safe",
expectedBenefit: "Enhanced privacy, secure cleanup",
requirements: ["Secure deletion", "Privacy audit", "Encrypted backup"],
},
];
}
async previewCleanup(optionId: string): Promise<CleanupPreview> {
const option = this.getAvailableCleanupOptions().find(
(opt) => opt.id === optionId
);
if (!option) {
throw new Error(`Cleanup option not found: ${optionId}`);
}
const memoryAnalysis = await this.memoryAnalyzer.analyzeMemoryUsage();
const recommendations = await this.generateRecommendationsForOption(
optionId,
memoryAnalysis
);
const riskAssessment = this.riskEngine.assessOptimizationRisk(
recommendations,
memoryAnalysis.total_memories
);
const estimatedImpact = this.calculateEstimatedImpact(
recommendations,
memoryAnalysis
);
const safetyChecks = this.performSafetyChecks(optionId, recommendations);
const previewItems = this.generatePreviewItems(recommendations, optionId);
return {
option,
estimated_impact: estimatedImpact,
risk_assessment: riskAssessment,
safety_checks: safetyChecks,
preview_items: previewItems,
};
}
async executeCleanup(
optionId: string,
userConfirmed: boolean = false
): Promise<CleanupResult> {
const option = this.getAvailableCleanupOptions().find(
(opt) => opt.id === optionId
);
if (!option) {
throw new Error(`Cleanup option not found: ${optionId}`);
}
// Safety check for advanced options
if (option.safetyLevel === "advanced" && !userConfirmed) {
throw new Error(
"Advanced cleanup options require explicit user confirmation"
);
}
const preview = await this.previewCleanup(optionId);
// Simulate cleanup execution
const result = await this.performCleanup(option, preview);
return result;
}
private async generateRecommendationsForOption(
optionId: string,
analysis: MemoryUsageAnalysis
): Promise<MemoryOptimizationRecommendation[]> {
const recommendations: MemoryOptimizationRecommendation[] = [];
switch (optionId) {
case "quick_tidy":
// Only very low importance memories
if (analysis.low_importance_memories > 0) {
const targetCount = Math.min(analysis.low_importance_memories, 50);
recommendations.push({
type: "forget",
target_memories: this.generateMemoryIds(
targetCount,
"very_low_importance"
),
estimated_benefit: {
memory_space_freed: targetCount * 100,
processing_speed_improvement: 0.02,
interference_reduction: 0.01,
focus_improvement: 0.01,
},
risk_level: "low",
description: `Remove ${targetCount} very low importance memories`,
requires_user_consent: false,
});
}
break;
case "smart_cleanup":
// Low importance + old memories
const smartTargetCount = Math.min(
analysis.low_importance_memories,
200
);
recommendations.push({
type: "forget",
target_memories: this.generateMemoryIds(
smartTargetCount,
"low_importance_old"
),
estimated_benefit: {
memory_space_freed: smartTargetCount * 150,
processing_speed_improvement: 0.05,
interference_reduction: 0.03,
focus_improvement: 0.02,
},
risk_level: "low",
description: `Remove ${smartTargetCount} old, low-importance memories`,
requires_user_consent: false,
});
// Compress fragmented memories
if (analysis.fragmentation_level > 0.2) {
const compressCount = Math.floor(analysis.total_memories * 0.1);
recommendations.push({
type: "compress",
target_memories: this.generateMemoryIds(
compressCount,
"fragmented"
),
estimated_benefit: {
memory_space_freed: compressCount * 50,
processing_speed_improvement: 0.02,
interference_reduction: 0.01,
focus_improvement: 0.01,
},
risk_level: "low",
description: `Compress ${compressCount} fragmented memories`,
requires_user_consent: false,
});
}
break;
case "deep_clean":
// More aggressive cleanup
const deepTargetCount = Math.min(
analysis.low_importance_memories +
Math.floor(analysis.rarely_accessed_memories * 0.5),
500
);
recommendations.push({
type: "forget",
target_memories: this.generateMemoryIds(
deepTargetCount,
"deep_clean_targets"
),
estimated_benefit: {
memory_space_freed: deepTargetCount * 200,
processing_speed_improvement: 0.08,
interference_reduction: 0.05,
focus_improvement: 0.04,
},
risk_level: "medium",
description: `Deep clean ${deepTargetCount} memories`,
requires_user_consent: true,
});
// Archive rarely accessed
const archiveCount = Math.floor(
analysis.rarely_accessed_memories * 0.3
);
recommendations.push({
type: "archive",
target_memories: this.generateMemoryIds(
archiveCount,
"rarely_accessed"
),
estimated_benefit: {
memory_space_freed: archiveCount * 100,
processing_speed_improvement: 0.03,
interference_reduction: 0.02,
focus_improvement: 0.02,
},
risk_level: "low",
description: `Archive ${archiveCount} rarely accessed memories`,
requires_user_consent: false,
});
break;
case "performance_boost":
// Aggressive performance optimization
const perfTargetCount = Math.floor(analysis.total_memories * 0.3);
recommendations.push({
type: "forget",
target_memories: this.generateMemoryIds(
perfTargetCount,
"performance_targets"
),
estimated_benefit: {
memory_space_freed: perfTargetCount * 250,
processing_speed_improvement: 0.15,
interference_reduction: 0.1,
focus_improvement: 0.08,
},
risk_level: "high",
description: `Aggressive cleanup of ${perfTargetCount} memories for maximum performance`,
requires_user_consent: true,
});
break;
case "storage_saver":
// Focus on compression and archiving
const compressStorageCount = Math.floor(analysis.total_memories * 0.4);
recommendations.push({
type: "compress",
target_memories: this.generateMemoryIds(
compressStorageCount,
"large_memories"
),
estimated_benefit: {
memory_space_freed: compressStorageCount * 150,
processing_speed_improvement: 0.04,
interference_reduction: 0.02,
focus_improvement: 0.01,
},
risk_level: "low",
description: `Compress ${compressStorageCount} large memories`,
requires_user_consent: false,
});
const archiveStorageCount = Math.floor(
analysis.rarely_accessed_memories * 0.6
);
recommendations.push({
type: "archive",
target_memories: this.generateMemoryIds(
archiveStorageCount,
"storage_archive"
),
estimated_benefit: {
memory_space_freed: archiveStorageCount * 120,
processing_speed_improvement: 0.03,
interference_reduction: 0.02,
focus_improvement: 0.02,
},
risk_level: "low",
description: `Archive ${archiveStorageCount} memories for storage savings`,
requires_user_consent: false,
});
break;
case "privacy_cleanup":
// Privacy-focused cleanup
const privacyTargetCount = Math.floor(analysis.total_memories * 0.1);
recommendations.push({
type: "forget",
target_memories: this.generateMemoryIds(
privacyTargetCount,
"privacy_targets"
),
estimated_benefit: {
memory_space_freed: privacyTargetCount * 180,
processing_speed_improvement: 0.03,
interference_reduction: 0.02,
focus_improvement: 0.03,
},
risk_level: "medium",
description: `Privacy-focused cleanup of ${privacyTargetCount} memories`,
requires_user_consent: true,
});
break;
}
return recommendations;
}
private calculateEstimatedImpact(
recommendations: MemoryOptimizationRecommendation[],
_analysis: MemoryUsageAnalysis
) {
const memoriesToRemove = recommendations
.filter((r) => r.type === "forget")
.reduce((sum, r) => sum + r.target_memories.length, 0);
const spaceToFree = recommendations.reduce(
(sum, r) => sum + r.estimated_benefit.memory_space_freed,
0
);
const performanceGain = recommendations.reduce(
(sum, r) => sum + r.estimated_benefit.processing_speed_improvement,
0
);
let performanceDescription = "Minimal";
if (performanceGain > 0.1) performanceDescription = "Significant";
else if (performanceGain > 0.05) performanceDescription = "Moderate";
else if (performanceGain > 0.02) performanceDescription = "Noticeable";
const riskLevel = recommendations.some((r) => r.risk_level === "high")
? "High"
: recommendations.some((r) => r.risk_level === "medium")
? "Medium"
: "Low";
return {
memories_to_remove: memoriesToRemove,
space_to_free_mb: Math.round(spaceToFree / 1024),
performance_gain: performanceDescription,
risk_level: riskLevel,
};
}
private performSafetyChecks(
optionId: string,
recommendations: MemoryOptimizationRecommendation[]
) {
const option = this.getAvailableCleanupOptions().find(
(opt) => opt.id === optionId
)!;
return {
backup_created: true, // Always create backups
rollback_available: option.safetyLevel !== "advanced",
user_consent_needed: recommendations.some((r) => r.requires_user_consent),
protected_memories_safe:
option.safetyLevel === "very_safe" || option.safetyLevel === "safe",
};
}
private generatePreviewItems(
recommendations: MemoryOptimizationRecommendation[],
_optionId: string
) {
const items: Array<{
memory_id: string;
summary: string;
importance: number;
age_days: number;
reason_for_removal: string;
}> = [];
// Generate sample preview items (in real implementation, would fetch actual memories)
recommendations.forEach((rec) => {
const sampleCount = Math.min(rec.target_memories.length, 5);
for (let i = 0; i < sampleCount; i++) {
items.push({
memory_id: rec.target_memories[i],
summary: this.generateMemorySummary(rec.type, i),
importance: this.generateImportanceScore(rec.type),
age_days: this.generateAgeInDays(rec.type),
reason_for_removal: this.generateRemovalReason(
rec.type,
rec.description
),
});
}
});
return items.slice(0, 10); // Limit to 10 preview items
}
private async performCleanup(
option: CleanupOption,
preview: CleanupPreview
): Promise<CleanupResult> {
// Simulate cleanup execution
const memoriesAffected = preview.estimated_impact.memories_to_remove;
const spaceFreed = preview.estimated_impact.space_to_free_mb;
// Simulate some processing time
await new Promise((resolve) => setTimeout(resolve, 1000));
return {
success: true,
option_used: option.id,
memories_affected: memoriesAffected,
space_freed_mb: spaceFreed,
performance_improvement: preview.estimated_impact.performance_gain,
risk_assessment: preview.risk_assessment,
warnings: this.generateWarnings(option, preview),
next_steps: this.generateNextSteps(option, preview),
rollback_available: preview.safety_checks.rollback_available,
rollback_instructions: preview.safety_checks.rollback_available
? "Use 'recover_memory' tool with the provided memory IDs within 7 days"
: undefined,
};
}
private generateMemoryIds(count: number, category: string): string[] {
const ids: string[] = [];
for (let i = 0; i < count; i++) {
ids.push(`${category}_${i}_${Date.now()}`);
}
return ids;
}
private generateMemorySummary(type: string, index: number): string {
const summaries = {
forget: [
"Temporary note about lunch plans",
"Random thought from last week",
"Incomplete task reminder",
"Old web search result",
"Duplicate information",
],
compress: [
"Large document with repetitive content",
"Detailed log file with patterns",
"Image with high compression potential",
"Text with redundant information",
"Data structure with optimization opportunities",
],
archive: [
"Old project documentation",
"Historical reference material",
"Completed task details",
"Past conversation logs",
"Archived research notes",
],
};
const typeArray =
summaries[type as keyof typeof summaries] || summaries.forget;
return typeArray[index % typeArray.length];
}
private generateImportanceScore(type: string): number {
switch (type) {
case "forget":
return Math.random() * 0.3; // Low importance
case "compress":
return 0.3 + Math.random() * 0.4; // Medium importance
case "archive":
return 0.2 + Math.random() * 0.5; // Variable importance
default:
return Math.random() * 0.5;
}
}
private generateAgeInDays(type: string): number {
switch (type) {
case "forget":
return 30 + Math.random() * 60; // 30-90 days
case "compress":
return 7 + Math.random() * 30; // 7-37 days
case "archive":
return 60 + Math.random() * 300; // 60-360 days
default:
return Math.random() * 100;
}
}
private generateRemovalReason(type: string, description: string): string {
const reasons = {
forget: "Low importance and old age",
compress: "Large size with compression potential",
archive: "Rarely accessed but potentially useful",
consolidate: "Conflicts with newer information",
};
return reasons[type as keyof typeof reasons] || description;
}
private generateWarnings(
_option: CleanupOption,
preview: CleanupPreview
): string[] {
const warnings: string[] = [];
if (preview.estimated_impact.memories_to_remove > 100) {
warnings.push("Large number of memories will be affected");
}
if (
preview.risk_assessment.risk_assessment.level === "high" ||
preview.risk_assessment.risk_assessment.level === "very_high"
) {
warnings.push("High risk operation - review carefully");
}
if (!preview.safety_checks.rollback_available) {
warnings.push("This operation cannot be easily reversed");
}
return warnings;
}
private generateNextSteps(
option: CleanupOption,
preview: CleanupPreview
): string[] {
const steps: string[] = [];
steps.push("Monitor system performance for improvements");
if (preview.safety_checks.rollback_available) {
steps.push("Rollback available for 7 days if needed");
}
if (option.safetyLevel === "advanced") {
steps.push("Consider running analysis again in 1-2 weeks");
} else {
steps.push("Consider running cleanup again in 1 month");
}
steps.push("Review memory usage statistics for trends");
return steps;
}
}