// Planu — Memory directory auditor for SPEC-092
import { readFile, readdir } from 'node:fs/promises';
import { homedir } from 'node:os';
import { join } from 'node:path';
const DOCS_REF = 'https://code.claude.com/docs/en/memory#auto-memory';
const MAX_MEMORY_LINES = 200;
function deriveMemoryPath(projectPath) {
    const derived = `-${projectPath.replace(/\//g, '-')}`;
    return join(homedir(), '.claude', 'projects', derived, 'memory');
}
async function readMemoryLines(memoryDir) {
    try {
        const content = await readFile(join(memoryDir, 'MEMORY.md'), 'utf-8');
        return content.split('\n');
    }
    catch {
        return null;
    }
}
export async function auditMemory(projectPath) {
    const findings = [];
    const memoryDir = deriveMemoryPath(projectPath);
    const memoryLines = await readMemoryLines(memoryDir);
    if (!memoryLines) {
        findings.push({
            area: 'memory',
            severity: 'info',
            message: 'No MEMORY.md found — auto-memory may be disabled or no memories saved yet',
            suggestion: 'Enable auto-memory in settings or manually create MEMORY.md for cross-session context',
            docsRef: DOCS_REF,
        });
        return findings;
    }
    if (memoryLines.length > MAX_MEMORY_LINES) {
        findings.push({
            area: 'memory',
            severity: 'warn',
            message: `MEMORY.md has ${String(memoryLines.length)} lines — only first ${String(MAX_MEMORY_LINES)} are loaded per session`,
            suggestion: 'Move detailed notes to topic files (debugging.md, patterns.md) and keep MEMORY.md as an index',
            docsRef: DOCS_REF,
        });
    }
    // Check for topic files (good practice)
    try {
        const entries = await readdir(memoryDir);
        const topicFiles = entries.filter((f) => f.endsWith('.md') && f !== 'MEMORY.md');
        if (topicFiles.length === 0 && memoryLines.length > 50) {
            findings.push({
                area: 'memory',
                severity: 'info',
                message: 'No topic files found alongside MEMORY.md',
                suggestion: 'Create topic files (e.g., debugging.md, patterns.md) for detailed notes, linked from MEMORY.md',
                docsRef: DOCS_REF,
            });
        }
    }
    catch {
        // Memory dir unreadable — already reported above
    }
    return findings;
}
//# sourceMappingURL=memory-auditor.js.map