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14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/**
 * Context Loader - Loads project context files for agent prompts
 *
 * Provides a shared utility to load context files from .automaker/context/
 * and memory files from .automaker/memory/, formatting them as system prompt
 * content. Used by both auto-mode-service and agent-service to ensure all
 * agents are aware of project context and past learnings.
 *
 * Context files contain project-specific rules, conventions, and guidelines
 * that agents must follow when working on the project.
 *
 * Memory files contain learnings from past agent work, including decisions,
 * gotchas, and patterns that should inform future work.
 */

import path from 'path';
import { secureFs } from '@automaker/platform';
import {
  getMemoryDir,
  parseFrontmatter,
  initializeMemoryFolder,
  extractTerms,
  calculateUsageScore,
  countMatches,
  incrementUsageStat,
  type MemoryFsModule,
  type MemoryMetadata,
} from './memory-loader.js';

/**
 * Metadata structure for context files
 * Stored in {projectPath}/.automaker/context/context-metadata.json
 */
export interface ContextMetadata {
  files: Record<string, { description: string }>;
}

/**
 * Individual context file with metadata
 */
export interface ContextFileInfo {
  name: string;
  path: string;
  content: string;
  description?: string;
}

/**
 * Memory file info (from .automaker/memory/)
 */
export interface MemoryFileInfo {
  name: string;
  path: string;
  content: string;
  category: string;
}

/**
 * Result of loading context files
 */
export interface ContextFilesResult {
  files: ContextFileInfo[];
  memoryFiles: MemoryFileInfo[];
  formattedPrompt: string;
}

/**
 * File system module interface for context loading
 * Compatible with secureFs from @automaker/platform
 * Includes write methods needed for memory initialization
 */
export interface ContextFsModule {
  access: (path: string) => Promise<void>;
  readdir: (path: string) => Promise<string[]>;
  readFile: (path: string, encoding?: BufferEncoding) => Promise<string | Buffer>;
  // Write methods needed for memory operations
  writeFile: (path: string, content: string) => Promise<void>;
  mkdir: (path: string, options?: { recursive?: boolean }) => Promise<string | undefined>;
  appendFile: (path: string, content: string) => Promise<void>;
}

/**
 * Task context for smart memory selection
 */
export interface TaskContext {
  /** Title or name of the current task/feature */
  title: string;
  /** Description of what the task involves */
  description?: string;
}

/**
 * Options for loading context files
 */
export interface LoadContextFilesOptions {
  /** Project path to load context from */
  projectPath: string;
  /** Optional custom secure fs module (for dependency injection) */
  fsModule?: ContextFsModule;
  /** Whether to include context files from .automaker/context/ (default: true) */
  includeContextFiles?: boolean;
  /** Whether to include memory files from .automaker/memory/ (default: true) */
  includeMemory?: boolean;
  /** Whether to initialize memory folder if it doesn't exist (default: true) */
  initializeMemory?: boolean;
  /** Task context for smart memory selection - if not provided, only loads high-importance files */
  taskContext?: TaskContext;
  /** Maximum number of memory files to load (default: 5) */
  maxMemoryFiles?: number;
}

/**
 * Get the context directory path for a project
 */
function getContextDir(projectPath: string): string {
  return path.join(projectPath, '.automaker', 'context');
}

/**
 * Load context metadata from the metadata file
 */
async function loadContextMetadata(
  contextDir: string,
  fsModule: ContextFsModule
): Promise<ContextMetadata> {
  const metadataPath = path.join(contextDir, 'context-metadata.json');
  try {
    const content = await fsModule.readFile(metadataPath, 'utf-8');
    return JSON.parse(content as string);
  } catch {
    // Metadata file doesn't exist yet - that's fine
    return { files: {} };
  }
}

/**
 * Format a single context file entry for the prompt
 */
function formatContextFileEntry(file: ContextFileInfo): string {
  const header = `## ${file.name}`;
  const pathInfo = `**Path:** \`${file.path}\``;

  let descriptionInfo = '';
  if (file.description) {
    descriptionInfo = `\n**Purpose:** ${file.description}`;
  }

  return `${header}\n${pathInfo}${descriptionInfo}\n\n${file.content}`;
}

/**
 * Build the formatted system prompt from context files
 */
function buildContextPrompt(files: ContextFileInfo[]): string {
  if (files.length === 0) {
    return '';
  }

  const formattedFiles = files.map(formatContextFileEntry);

  return `# Project Context Files

The following context files provide project-specific rules, conventions, and guidelines.
Each file serves a specific purpose - use the description to understand when to reference it.
If you need more details about a context file, you can read the full file at the path provided.

**IMPORTANT**: You MUST follow the rules and conventions specified in these files.
- Follow ALL commands exactly as shown (e.g., if the project uses \`pnpm\`, NEVER use \`npm\` or \`npx\`)
- Follow ALL coding conventions, commit message formats, and architectural patterns specified
- Reference these rules before running ANY shell commands or making commits

---

${formattedFiles.join('\n\n---\n\n')}

---

**REMINDER**: Before taking any action, verify you are following the conventions specified above.
`;
}

/**
 * Load context files from a project's .automaker/context/ directory
 * and optionally memory files from .automaker/memory/
 *
 * This function loads all .md and .txt files from the context directory,
 * along with their metadata (descriptions), and formats them into a
 * system prompt that can be prepended to agent prompts.
 *
 * By default, it also loads memory files containing learnings from past
 * agent work, which helps agents make better decisions.
 *
 * @param options - Configuration options
 * @returns Promise resolving to context files, memory files, and formatted prompt
 *
 * @example
 * ```typescript
 * const { formattedPrompt, files, memoryFiles } = await loadContextFiles({
 *   projectPath: '/path/to/project'
 * });
 *
 * // Use as system prompt
 * const executeOptions = {
 *   prompt: userPrompt,
 *   systemPrompt: formattedPrompt,
 * };
 * ```
 */
export async function loadContextFiles(
  options: LoadContextFilesOptions
): Promise<ContextFilesResult> {
  const {
    projectPath,
    fsModule = secureFs,
    includeContextFiles = true,
    includeMemory = true,
    initializeMemory = true,
    taskContext,
    maxMemoryFiles = 5,
  } = options;
  const contextDir = path.resolve(getContextDir(projectPath));

  const files: ContextFileInfo[] = [];
  const memoryFiles: MemoryFileInfo[] = [];

  // Load context files if enabled
  if (includeContextFiles) {
    try {
      // Check if directory exists
      await fsModule.access(contextDir);

      // Read directory contents
      const allFiles = await fsModule.readdir(contextDir);

      // Filter for text-based context files (case-insensitive for cross-platform)
      const textFiles = allFiles.filter((f) => {
        const lower = f.toLowerCase();
        return (lower.endsWith('.md') || lower.endsWith('.txt')) && f !== 'context-metadata.json';
      });

      if (textFiles.length > 0) {
        // Load metadata for descriptions
        const metadata = await loadContextMetadata(contextDir, fsModule);

        // Load each file with its content and metadata
        for (const fileName of textFiles) {
          const filePath = path.join(contextDir, fileName);
          try {
            const content = await fsModule.readFile(filePath, 'utf-8');
            files.push({
              name: fileName,
              path: filePath,
              content: content as string,
              description: metadata.files[fileName]?.description,
            });
          } catch (error) {
            console.warn(`[ContextLoader] Failed to read context file ${fileName}:`, error);
          }
        }
      }
    } catch {
      // Context directory doesn't exist or is inaccessible - that's fine
    }
  }

  // Load memory files if enabled (with smart selection)
  if (includeMemory) {
    const memoryDir = getMemoryDir(projectPath);

    // Initialize memory folder if needed
    if (initializeMemory) {
      try {
        await initializeMemoryFolder(projectPath, fsModule as MemoryFsModule);
      } catch {
        // Initialization failed, continue without memory
      }
    }

    try {
      await fsModule.access(memoryDir);
      const allMemoryFiles = await fsModule.readdir(memoryDir);

      // Filter for markdown memory files (except _index.md, case-insensitive)
      const memoryMdFiles = allMemoryFiles.filter((f) => {
        const lower = f.toLowerCase();
        return lower.endsWith('.md') && lower !== '_index.md';
      });

      // Extract terms from task context for matching
      const taskTerms = taskContext
        ? extractTerms(taskContext.title + ' ' + (taskContext.description || ''))
        : [];

      // Score and load memory files
      const scoredFiles: Array<{
        fileName: string;
        filePath: string;
        body: string;
        metadata: MemoryMetadata;
        score: number;
      }> = [];

      for (const fileName of memoryMdFiles) {
        const filePath = path.join(memoryDir, fileName);
        try {
          const rawContent = await fsModule.readFile(filePath, 'utf-8');
          const { metadata, body } = parseFrontmatter(rawContent as string);

          // Skip empty files
          if (!body.trim()) continue;

          // Calculate relevance score
          let score = 0;

          if (taskTerms.length > 0) {
            // Match task terms against file metadata
            const tagScore = countMatches(metadata.tags, taskTerms) * 3;
            const relevantToScore = countMatches(metadata.relevantTo, taskTerms) * 2;
            const summaryTerms = extractTerms(metadata.summary);
            const summaryScore = countMatches(summaryTerms, taskTerms);
            // Split category name on hyphens/underscores for better matching
            // e.g., "authentication-decisions" matches "authentication"
            const categoryTerms = fileName
              .replace('.md', '')
              .split(/[-_]/)
              .filter((t) => t.length > 2);
            const categoryScore = countMatches(categoryTerms, taskTerms) * 4;

            // Usage-based scoring (files that helped before rank higher)
            const usageScore = calculateUsageScore(metadata.usageStats);

            score =
              (tagScore + relevantToScore + summaryScore + categoryScore) *
              metadata.importance *
              usageScore;
          } else {
            // No task context - use importance as score
            score = metadata.importance;
          }

          scoredFiles.push({ fileName, filePath, body, metadata, score });
        } catch (error) {
          console.warn(`[ContextLoader] Failed to read memory file ${fileName}:`, error);
        }
      }

      // Sort by score (highest first)
      scoredFiles.sort((a, b) => b.score - a.score);

      // Select files to load:
      // 1. Always include gotchas.md if it exists (unless maxMemoryFiles=0)
      // 2. Include high-importance files (importance >= 0.9)
      // 3. Include top scoring files up to maxMemoryFiles
      const selectedFiles = new Set<string>();

      // Skip selection if maxMemoryFiles is 0
      if (maxMemoryFiles > 0) {
        // Always include gotchas.md
        const gotchasFile = scoredFiles.find((f) => f.fileName === 'gotchas.md');
        if (gotchasFile) {
          selectedFiles.add('gotchas.md');
        }

        // Add high-importance files
        for (const file of scoredFiles) {
          if (file.metadata.importance >= 0.9 && selectedFiles.size < maxMemoryFiles) {
            selectedFiles.add(file.fileName);
          }
        }

        // Add top scoring files (if we have task context and room)
        if (taskTerms.length > 0) {
          for (const file of scoredFiles) {
            if (file.score > 0 && selectedFiles.size < maxMemoryFiles) {
              selectedFiles.add(file.fileName);
            }
          }
        }
      }

      // Load selected files and increment loaded stat
      for (const file of scoredFiles) {
        if (selectedFiles.has(file.fileName)) {
          memoryFiles.push({
            name: file.fileName,
            path: file.filePath,
            content: file.body,
            category: file.fileName.replace('.md', ''),
          });

          // Increment the 'loaded' stat for this file (CRITICAL FIX)
          // This makes calculateUsageScore work correctly
          try {
            await incrementUsageStat(file.filePath, 'loaded', fsModule as MemoryFsModule);
          } catch {
            // Non-critical - continue even if stat update fails
          }
        }
      }

      if (memoryFiles.length > 0) {
        const selectedNames = memoryFiles.map((f) => f.category).join(', ');
        console.log(`[ContextLoader] Selected memory files: ${selectedNames}`);
      }
    } catch {
      // Memory directory doesn't exist - that's fine
    }
  }

  // Build combined prompt
  const contextPrompt = buildContextPrompt(files);
  const memoryPrompt = buildMemoryPrompt(memoryFiles);
  const formattedPrompt = [contextPrompt, memoryPrompt].filter(Boolean).join('\n\n');

  const loadedItems = [];
  if (files.length > 0) {
    loadedItems.push(`${files.length} context file(s)`);
  }
  if (memoryFiles.length > 0) {
    loadedItems.push(`${memoryFiles.length} memory file(s)`);
  }
  if (loadedItems.length > 0) {
    console.log(`[ContextLoader] Loaded ${loadedItems.join(' and ')}`);
  }

  return { files, memoryFiles, formattedPrompt };
}

/**
 * Build a formatted prompt from memory files
 */
function buildMemoryPrompt(memoryFiles: MemoryFileInfo[]): string {
  if (memoryFiles.length === 0) {
    return '';
  }

  const sections = memoryFiles.map((file) => {
    return `## ${file.category.toUpperCase()}

${file.content}`;
  });

  return `# Project Memory

The following learnings and decisions from previous work are available.
**IMPORTANT**: Review these carefully before making changes that could conflict with past decisions.

---

${sections.join('\n\n---\n\n')}

---
`;
}

/**
 * Get a summary of available context files (names and descriptions only)
 * Useful for informing the agent about what context is available without
 * loading full content.
 */
export async function getContextFilesSummary(
  options: LoadContextFilesOptions
): Promise<Array<{ name: string; path: string; description?: string }>> {
  const { projectPath, fsModule = secureFs } = options;
  const contextDir = path.resolve(getContextDir(projectPath));

  try {
    await fsModule.access(contextDir);
    const allFiles = await fsModule.readdir(contextDir);

    const textFiles = allFiles.filter((f) => {
      const lower = f.toLowerCase();
      return (lower.endsWith('.md') || lower.endsWith('.txt')) && f !== 'context-metadata.json';
    });

    if (textFiles.length === 0) {
      return [];
    }

    const metadata = await loadContextMetadata(contextDir, fsModule);

    return textFiles.map((fileName) => ({
      name: fileName,
      path: path.join(contextDir, fileName),
      description: metadata.files[fileName]?.description,
    }));
  } catch {
    return [];
  }
}