import { loadEnvironmentFromNodeEnv } from "../src/common/env-loader.js"; loadEnvironmentFromNodeEnv(); const { getRawSecret } = await import("../src/common/secret-manager.js"); const { initMongoDB, closeMongoDB } = await import("../src/common/mongodb.js"); const { initLogger, getLogger } = await import("../src/observability/logger.js"); const { getMemoryEvictionEnabled } = await import("../src/config.js"); const { KNOWLEDGE_COLLECTION } = await import("../src/memory/mongo-long-term-memory.js"); const { detectContradictions } = await import("../src/memory/contradiction-detector.js"); const { evaluateDocument } = await import("../src/memory/forgetting-policy.js"); initLogger(); const logger = getLogger(); interface KnowledgeRecord { _id: string; category: string; text: string; metadata?: Record | undefined; lastReferencedAt?: Date | undefined; references?: Date[] | undefined; updatedAt?: Date | undefined; } function firstNonEmpty( ...values: Array ): string | undefined { for (const value of values) { if (value?.trim()) { return value.trim(); } } return undefined; } function parseSinceDays(): number { const raw = process.argv.find((arg) => arg.startsWith("--since="))?.split("=")[1]; if (!raw) { return 30; } const match = raw.match(/^(\d+)d$/u); return match ? Number(match[1]) : 30; } const dryRun = process.argv.includes("--dry-run"); const target = process.argv.find((arg) => arg.startsWith("--target="))?.split("=")[1] ?? "all"; const sinceDays = parseSinceDays(); if (!dryRun && !getMemoryEvictionEnabled()) { throw new Error( "AGENT_MEMORY_EVICTION_ENABLED must be true for non-dry-run eviction", ); } if (target !== "knowledge" && target !== "all") { logger.info({ dryRun, target }, "memory eviction skipped: target has no Mongo knowledge work"); process.exit(0); } const connectionString = firstNonEmpty( getRawSecret("MONGODB_CONNECTION_STRING"), process.env["MONGODB_CONNECTION_STRING"], ); if (!connectionString) { if (dryRun) { logger.warn( { dryRun }, "MONGODB_CONNECTION_STRING is not configured; memory eviction dry-run skipped", ); process.exit(0); } throw new Error("MONGODB_CONNECTION_STRING is required for memory eviction"); } const connection = await initMongoDB(connectionString); try { const collection = connection.collection(KNOWLEDGE_COLLECTION); const docs = await collection .find({ "metadata.archived": { $ne: true } }) .sort({ updatedAt: 1 }) .toArray(); const contradictions = detectContradictions( docs.map((doc) => ({ id: doc._id, metadata: doc.metadata, text: doc.text, })), ); const contradictionByExistingId = new Map( contradictions.map((candidate) => [candidate.existingId, candidate.newId]), ); const now = new Date(); const sinceMs = now.getTime() - sinceDays * 24 * 60 * 60 * 1000; let archived = 0; let kept = 0; for (const doc of docs) { const references = (doc.references ?? []).filter((date) => { const time = new Date(date).getTime(); return Number.isFinite(time) && time >= sinceMs; }); const decision = evaluateDocument( { id: doc._id, category: doc.category, metadata: doc.metadata, text: doc.text, }, { contradictsId: contradictionByExistingId.get(doc._id), lastReferencedAt: doc.lastReferencedAt ? new Date(doc.lastReferencedAt) : undefined, now, referenceCount30d: references.length, updatedAt: doc.updatedAt ? new Date(doc.updatedAt) : undefined, }, ); if (decision.action === "keep") { kept += 1; continue; } archived += 1; if (!dryRun) { await collection.updateOne( { _id: doc._id }, { $set: { "metadata.archived": true, "metadata.archivedAt": now.toISOString(), "metadata.archiveReason": decision.reason, "metadata.archiveEvidence": decision.evidence, }, }, ); } } logger.info( { archived, contradictions: contradictions.length, dryRun, kept, scanned: docs.length, sinceDays, }, "memory eviction completed", ); } finally { await closeMongoDB(); }