"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.MemoryReadTool = exports.MemoryWriteTool = void 0;
const fs = __importStar(require("fs/promises"));
const path = __importStar(require("path"));
const memoryConflictDetector_1 = require("../services/memoryConflictDetector");
// Match the resolution used by web/server.ts so the model's `remember`
// writes land in the same .harness/memory/ that assembleSystemContext reads
// from. Falls back to cwd when the env var is absent.
function memoryProjectDir() {
    return process.env.HARNESS_PROJECT_DIR && process.env.HARNESS_PROJECT_DIR.trim()
        ? process.env.HARNESS_PROJECT_DIR
        : process.cwd();
}
/**
 * MemoryTool — the agent writes observations, patterns, and decisions
 * back to memory files so they persist across sessions.
 * (Paper §7.2: Auto Memory — "contextually relevant memory entries")
 * (Paper §2.1: Contextual Adaptability — "the relationship improves over time")
 */
exports.MemoryWriteTool = {
    name: 'remember',
    description: 'Save a note, decision, or learned pattern to memory so you remember it in future conversations. Memory is stored in plain text files the user can read and edit.',
    parameters: {
        type: 'object',
        properties: {
            category: {
                type: 'string',
                description: 'Where to save: "decision" (architectural choice), "pattern" (coding convention), "note" (general observation)',
            },
            title: { type: 'string', description: 'Brief title for this memory entry' },
            content: { type: 'string', description: 'The full text to remember' },
        },
        required: ['category', 'title', 'content'],
    },
    isReadOnly: false,
    async execute(input) {
        const category = input.category;
        const title = input.title;
        const content = input.content;
        const memoryDir = path.join(memoryProjectDir(), '.harness', 'memory');
        await fs.mkdir(memoryDir, { recursive: true });
        const date = new Date().toISOString().split('T')[0];
        let filePath;
        let entry;
        switch (category) {
            case 'decision':
                filePath = path.join(memoryDir, 'decisions.md');
                entry = `\n### ${date}: ${title}\n${content}\n`;
                break;
            case 'pattern':
                filePath = path.join(memoryDir, 'patterns.md');
                entry = `\n### ${title}\n${content}\n`;
                break;
            default:
                filePath = path.join(memoryDir, 'notes.md');
                entry = `\n### ${date}: ${title}\n${content}\n`;
                break;
        }
        try {
            // Check for conflicts with existing memory before writing.
            const fileName = category === 'decision' ? 'decisions.md'
                : category === 'pattern' ? 'patterns.md'
                    : 'notes.md';
            const conflictBody = `${title}\n${content}`;
            const conflicts = await (0, memoryConflictDetector_1.scanFileForConflicts)(memoryProjectDir(), fileName, conflictBody);
            const conflictWarning = conflicts.length > 0
                ? `\n⚠️  Conflict warning: This entry may contradict ${conflicts.length} existing section(s):\n` +
                    conflicts.map((c) => `  - "${c.existingSection.title}" (${c.conflictType}, confidence ${Math.round(c.confidence * 100)}%): ${c.reason}`).join('\n') + '\n'
                : '';
            // Initialize file with header if it doesn't exist
            try {
                await fs.access(filePath);
            }
            catch {
                const header = category === 'decision' ? '# Decisions\n\nArchitectural and design decisions.\n'
                    : category === 'pattern' ? '# Patterns\n\nLearned coding conventions and patterns.\n'
                        : '# Notes\n\nGeneral observations and context.\n';
                await fs.writeFile(filePath, header, 'utf-8');
            }
            // Append the entry
            await fs.appendFile(filePath, entry, 'utf-8');
            return {
                success: true,
                output: `📝 Remembered "${title}" in ${category}s. Saved to ${path.relative(process.cwd(), filePath)}${conflictWarning}`,
            };
        }
        catch (error) {
            const msg = error instanceof Error ? error.message : String(error);
            return { success: false, output: `Failed to save memory: ${msg}`, error: msg };
        }
    },
};
/**
 * MemoryReadTool — the agent reads its own memory files.
 */
exports.MemoryReadTool = {
    name: 'recall',
    description: 'Read your saved memories (decisions, patterns, notes) to remember context from past conversations.',
    parameters: {
        type: 'object',
        properties: {
            category: {
                type: 'string',
                description: 'Which memories to read: "decision", "pattern", "note", or "all"',
            },
        },
    },
    isReadOnly: true,
    async execute(input) {
        const category = input.category ?? 'all';
        const memoryDir = path.join(memoryProjectDir(), '.harness', 'memory');
        const files = [];
        if (category === 'all' || category === 'decision')
            files.push('decisions.md');
        if (category === 'all' || category === 'pattern')
            files.push('patterns.md');
        if (category === 'all' || category === 'note')
            files.push('notes.md');
        const parts = [];
        for (const file of files) {
            try {
                const content = await fs.readFile(path.join(memoryDir, file), 'utf-8');
                parts.push(content);
            }
            catch {
                // File doesn't exist yet — skip
            }
        }
        if (parts.length === 0) {
            return { success: true, output: 'No memories saved yet. Use the "remember" tool to save decisions, patterns, or notes.' };
        }
        return { success: true, output: parts.join('\n\n---\n\n') };
    },
};
//# sourceMappingURL=memoryTools.js.map