loadAll method

Future<List<MemoryFile>> loadAll()

Scan and load all memory files in the project.

Implementation

Future<List<MemoryFile>> loadAll() async {
  final paths = <String, MemoryFileType>{};

  // NEOMAGE.md at project root.
  paths['$_projectRoot/NEOMAGE.md'] = MemoryFileType.neomageMd;

  // NEOMAGE.local.md.
  paths['$_projectRoot/NEOMAGE.local.md'] = MemoryFileType.neomageLocalMd;

  // TEAM.md.
  paths['$_projectRoot/TEAM.md'] = MemoryFileType.teamMd;

  // Walk parent directories for NEOMAGE.md files.
  var dir = Directory(_projectRoot).parent;
  for (int i = 0; i < 5; i++) {
    // Max 5 levels up.
    final claudeMd = File('${dir.path}/NEOMAGE.md');
    if (await claudeMd.exists()) {
      paths[claudeMd.path] = MemoryFileType.neomageMd;
    }
    if (dir.path == dir.parent.path) break; // Reached root.
    dir = dir.parent;
  }

  // .neomage/rules/*.md files.
  final rulesDir = Directory('$_projectRoot/.neomage/rules');
  if (await rulesDir.exists()) {
    await for (final entity in rulesDir.list()) {
      if (entity is File && entity.path.endsWith('.md')) {
        paths[entity.path] = MemoryFileType.ruleset;
      }
    }
  }

  // Home directory NEOMAGE.md.
  final home = Platform.environment['HOME'] ?? '';
  if (home.isNotEmpty) {
    final homeNeomage = '$home/.neomage/NEOMAGE.md';
    paths[homeNeomage] = MemoryFileType.neomageMd;
  }

  // Load each file.
  for (final entry in paths.entries) {
    final file = File(entry.key);
    if (await file.exists()) {
      final content = await file.readAsString();
      final stat = await file.stat();
      final hash = _hashContent(content);

      _files[entry.key] = MemoryFile(
        path: entry.key,
        type: entry.value,
        content: content,
        lastModified: stat.modified,
        hash: hash,
      );

      _lastKnownHashes[entry.key] = hash;
    }
  }

  return _files.values.toList();
}