/**
* Simple Cleanup Manager
*
* Provides a simplified interface for basic memory cleanup operations
* with safe defaults and user-friendly controls.
*/
import type { Memory } from "../../types/MemoryTypes.js";
import { RiskAssessmentEngine } from "./RiskAssessment.js";
export interface SimpleCleanupOptions {
max_memories_to_remove?: number;
min_age_days?: number;
importance_threshold?: number;
require_confirmation?: boolean;
dry_run?: boolean;
}
export interface CleanupResult {
memories_analyzed: number;
memories_removed: number;
memories_archived: number;
space_freed_mb: number;
warnings: string[];
errors: string[];
}
export class SimpleCleanupManager {
private risk_assessor: RiskAssessmentEngine;
constructor() {
this.risk_assessor = new RiskAssessmentEngine();
}
/**
* Perform a simple cleanup operation with safe defaults
*/
async performCleanup(
memories: Memory[],
options: SimpleCleanupOptions = {}
): Promise<CleanupResult> {
const {
max_memories_to_remove = 100,
min_age_days = 30,
importance_threshold = 0.3,
dry_run = false,
} = options;
const result: CleanupResult = {
memories_analyzed: memories.length,
memories_removed: 0,
memories_archived: 0,
space_freed_mb: 0,
warnings: [],
errors: [],
};
try {
// Filter memories by age
const old_memories = memories.filter((memory) => {
const age_days =
(Date.now() - memory.created_at) / (1000 * 60 * 60 * 24);
return age_days >= min_age_days;
});
if (old_memories.length === 0) {
result.warnings.push("No memories old enough for cleanup");
return result;
}
// Assess risk for all candidate memories
const risk_assessments = this.risk_assessor.assessBatchRisk(old_memories);
// Identify safe candidates for removal
const safe_candidates = old_memories.filter((memory) => {
const assessment = risk_assessments.get(memory.id);
return (
assessment?.safe_to_forget && memory.importance < importance_threshold
);
});
// Limit the number of memories to process
const candidates_to_process = safe_candidates.slice(
0,
max_memories_to_remove
);
if (candidates_to_process.length === 0) {
result.warnings.push("No safe candidates found for cleanup");
return result;
}
// Process each candidate
for (const memory of candidates_to_process) {
if (!dry_run) {
// In a real implementation, this would actually remove the memory
result.memories_removed++;
result.space_freed_mb += this.estimateMemorySize(memory);
}
}
if (dry_run) {
result.warnings.push(
`Dry run: Would remove ${candidates_to_process.length} memories`
);
}
} catch (error) {
result.errors.push(
`Cleanup failed: ${
error instanceof Error ? error.message : String(error)
}`
);
}
return result;
}
/**
* Get cleanup recommendations without performing any operations
*/
async getCleanupRecommendations(memories: Memory[]): Promise<{
total_memories: number;
cleanup_candidates: number;
potential_space_savings_mb: number;
recommendations: string[];
}> {
const risk_assessments = this.risk_assessor.assessBatchRisk(memories);
const safe_candidates = memories.filter((memory) => {
const assessment = risk_assessments.get(memory.id);
return assessment?.safe_to_forget;
});
const potential_savings = safe_candidates.reduce(
(total, memory) => total + this.estimateMemorySize(memory),
0
);
const recommendations: string[] = [];
if (safe_candidates.length === 0) {
recommendations.push("No memories are safe to remove at this time");
} else {
recommendations.push(
`${safe_candidates.length} memories can be safely removed`
);
recommendations.push(
`Potential space savings: ${potential_savings.toFixed(2)} MB`
);
}
if (safe_candidates.length > 1000) {
recommendations.push(
"Consider batch processing for large cleanup operations"
);
}
return {
total_memories: memories.length,
cleanup_candidates: safe_candidates.length,
potential_space_savings_mb: potential_savings,
recommendations,
};
}
private estimateMemorySize(memory: Memory): number {
// Rough estimate: content size + metadata overhead
const content_size = new TextEncoder().encode(memory.content).length;
const metadata_overhead = 1024; // 1KB overhead estimate
return (content_size + metadata_overhead) / (1024 * 1024); // Convert to MB
}
}