Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.
Python agent-contextagent-memoryagent-pluginsagentic-ragcontext-databasedsh-pluginknowledge-basememorymemory-managementself-evolvingvector-database
fix(ragfs): sort directory listings dirs first (#2993)
Qin Haojie4b9d17db2f05
diff --git a/crates/ragfs/src/core/filesystem.rs b/crates/ragfs/src/core/filesystem.rsindex 3b2dc6da3..c8d4c3fa7 100644--- a/crates/ragfs/src/core/filesystem.rs+++ b/crates/ragfs/src/core/filesystem.rs@@ -7,6 +7,7 @@use async_trait::async_trait;use regex::Regex;use std::any::Any;+use std::cmp::Ordering;use super::errors::{Error, Result};use super::types::{FileInfo, GrepResult, TreeEntry, WriteFlag};@@ -23,6 +24,22 @@ pub(crate) fn normalize_prefix_path(path: &str) -> String {}}+/// Compare listing names case-insensitively, with the original name as tie-breaker.+pub(crate) fn compare_listing_names(a: &str, b: &str) -> Ordering {+ a.to_lowercase()+ .cmp(&b.to_lowercase())+ .then_with(|| a.cmp(b))+}++/// Sort listing entries with directories first, then case-insensitive name.+pub(crate) fn sort_directory_entries(entries: &mut [FileInfo]) {+ entries.sort_by(|a, b| {+ b.is_dir+ .cmp(&a.is_dir)+ .then_with(|| compare_listing_names(&a.name, &b.name))+ });+}+/// Check whether `path` is under `exclude_path` (including itself).pub(crate) fn is_excluded_path(path: &str, exclude_path: &str) -> bool {if exclude_path == "/" {@@ -487,7 +504,8 @@ pub trait FileSystem: Send + Sync + Any {}}- let entries = self.read_dir(current_path).await?;+ let mut entries = self.read_dir(current_path).await?;+ sort_directory_entries(&mut entries);for entry in entries {if node_limit.is_some_and(|limit| result.len() >= limit) {@@ -853,22 +871,25 @@ mod tests {#[tokio::test]async fn test_tree_nested_dfs_order() {let fs = TreeFS::default()- .with_dir_entries("/root", vec![("a.txt", false), ("sub", true)])- .with_dir_entries("/root/sub", vec![("b.txt", false)]);+ .with_dir_entries("/root", vec![("a.txt", false), ("Sub", true), ("b", true)])+ .with_dir_entries("/root/Sub", vec![("b.txt", false)])+ .with_dir_entries("/root/b", vec![]);let entries = root_tree(&fs, false, None, None).await;- assert_eq!(entries.len(), 3);- assert_tree_names(&entries, &["a.txt", "sub", "b.txt"]);- assert_tree_rel_paths(&entries, &["a.txt", "sub", "sub/b.txt"]);+ assert_eq!(entries.len(), 4);+ assert_tree_names(&entries, &["b", "Sub", "b.txt", "a.txt"]);+ assert_tree_rel_paths(&entries, &["b", "Sub", "Sub/b.txt", "a.txt"]);assert_eq!(tree_paths(&entries),- vec!["/root/a.txt", "/root/sub", "/root/sub/b.txt"]+ vec!["/root/b", "/root/Sub", "/root/Sub/b.txt", "/root/a.txt"]);- assert_eq!(entries[0].info.mode, 0o644);- assert!(!entries[0].info.is_dir);+ assert_eq!(entries[0].info.mode, 0o755);+ assert!(entries[0].info.is_dir);assert_eq!(entries[1].info.mode, 0o755);assert!(entries[1].info.is_dir);+ assert_eq!(entries[2].info.mode, 0o644);+ assert!(!entries[2].info.is_dir);assert!(entries.iter().all(|entry| entry.extra.is_empty()));}@@ -877,11 +898,11 @@ mod tests {let fs = TreeFS::default().with_dir_entries("/root",vec![- ("a.txt", false),- ("b.txt", false),+ ("e.txt", false),("c.txt", false),+ ("a.txt", false),("d.txt", false),- ("e.txt", false),+ ("b.txt", false),],);diff --git a/crates/ragfs/src/core/mountable.rs b/crates/ragfs/src/core/mountable.rsindex 08dc4e1b2..4dac8a5d1 100644--- a/crates/ragfs/src/core/mountable.rs+++ b/crates/ragfs/src/core/mountable.rs@@ -21,7 +21,7 @@ const PATH_LOCK_FILE: &str = ".path.ovlock";use super::encryption_wrapper::EncryptionWrappedFS;use super::errors::{Error, Result};-use super::filesystem::FileSystem;+use super::filesystem::{sort_directory_entries, FileSystem};use super::multibackend_wrapper::MultiWriteWrappedFS;use super::plugin::ServicePlugin;use super::stats::{FilesystemStats, StatsCollector};@@ -763,6 +763,7 @@ impl FileSystem for MountableFS {let (mount_info, rel_path) = self.find_mount(path).await?;let mut entries = mount_info.fs.read_dir(&rel_path).await?;entries.retain(|e| e.name != PATH_LOCK_FILE);+ sort_directory_entries(&mut entries);Ok(entries)}@@ -1020,7 +1021,11 @@ mod tests {}async fn read_dir(&self, _path: &str) -> Result<Vec<FileInfo>> {- Ok(vec![])+ Ok(self+ .tree_entries+ .iter()+ .map(|entry| entry.info.clone())+ .collect())}async fn stat(&self, path: &str) -> Result<FileInfo> {@@ -1717,6 +1722,32 @@ mod tests {}}+ #[tokio::test]+ async fn test_read_dir_sorts_dirs_first_then_name() {+ let mfs = MountableFS::new();+ let plugin = MockPlugin::with_tree_entries(+ "sorted",+ vec![+ make_tree_entry("/b.txt", "b.txt", "b.txt", false),+ make_tree_entry("/A.txt", "A.txt", "A.txt", false),+ make_tree_entry("/c", "c", "c", true),+ make_tree_entry("/B", "B", "B", true),+ ],+ );+ mfs.register_plugin(plugin).await;+ mfs.mount(test_config("sorted", "/sorted")).await.unwrap();++ let names: Vec<String> = mfs+ .read_dir("/sorted")+ .await+ .unwrap()+ .into_iter()+ .map(|entry| entry.name)+ .collect();++ assert_eq!(names, vec!["B", "c", "A.txt", "b.txt"]);+ }+#[tokio::test]async fn test_tree_directory_no_mount_returns_error() {let mfs = MountableFS::new();diff --git a/crates/ragfs/src/plugins/s3fs/mod.rs b/crates/ragfs/src/plugins/s3fs/mod.rsindex b91b0838e..db764aec2 100644--- a/crates/ragfs/src/plugins/s3fs/mod.rs+++ b/crates/ragfs/src/plugins/s3fs/mod.rs@@ -27,7 +27,7 @@ use futures::stream::{self, StreamExt};use regex::Regex;use std::sync::atomic::{AtomicUsize, Ordering};-use crate::core::filesystem::{relative_depth, relative_match_file};+use crate::core::filesystem::{relative_depth, relative_match_file, sort_directory_entries};use crate::core::{ConfigParameter, Error, FileInfo, FileSystem, GrepMatch, GrepResult, PluginConfig, Result,ServicePlugin, TreeEntry, WriteFlag,@@ -546,8 +546,7 @@ impl FileSystem for S3FileSystem {});}- // Sort by name- files.sort_by(|a, b| a.name.cmp(&b.name));+ sort_directory_entries(&mut files);// Cacheself.dir_cache.put(normalized.clone(), files.clone()).await;diff --git a/crates/ragfs/src/plugins/s3fs/tree.rs b/crates/ragfs/src/plugins/s3fs/tree.rsindex 9dc8bec85..aad437780 100644--- a/crates/ragfs/src/plugins/s3fs/tree.rs+++ b/crates/ragfs/src/plugins/s3fs/tree.rs@@ -9,7 +9,7 @@ use std::collections::HashSet;use std::time::SystemTime;use super::client;-use crate::core::filesystem::{relative_depth, relative_match_file};+use crate::core::filesystem::{compare_listing_names, relative_depth, relative_match_file};use crate::core::{FileInfo, Result, TreeEntry};/// Extract the last path component (file/dir name) from a path.@@ -21,8 +21,9 @@ fn s3_file_name(path: &str) -> String {////// Reconstructs synthetic directories (from directory markers and from file/// parent prefixes), filters hidden files and entries beyond `level_limit`,-/// and orders entries by path-component lexicographic order so the output is-/// stable and consistent with the default DFS implementation.+/// and orders entries by DFS order with sibling directories first, then by+/// case-insensitive name, so the output is stable and consistent with the+/// default DFS implementation.////// `query_root` is the normalized query root path; `key_to_path` maps an S3/// object key to a filesystem path (allowing prefix stripping to be injected).@@ -110,7 +111,19 @@ where.split('/').filter(|p| !p.is_empty()).collect();- a_parts.cmp(&b_parts)+ for i in 0..a_parts.len().min(b_parts.len()) {+ if a_parts[i] == b_parts[i] {+ continue;+ }+ let a_prefix = format!("/{}", a_parts[..=i].join("/"));+ let b_prefix = format!("/{}", b_parts[..=i].join("/"));+ let a_is_dir = seen_dirs.contains(&a_prefix);+ let b_is_dir = seen_dirs.contains(&b_prefix);+ return b_is_dir+ .cmp(&a_is_dir)+ .then_with(|| compare_listing_names(a_parts[i], b_parts[i]));+ }+ a_parts.len().cmp(&b_parts.len())});let mut result = Vec::new();@@ -213,7 +226,7 @@ mod tests {let paths: Vec<&str> = result.iter().map(|e| e.path.as_str()).collect();assert_eq!(paths,- vec!["/a", "/a/a.txt", "/a/b", "/a/b/c.txt", "/a/b.txt"]+ vec!["/a", "/a/b", "/a/b/c.txt", "/a/a.txt", "/a/b.txt"]);}@@ -221,8 +234,8 @@ mod tests {fn test_build_tree_entries_level_limit() {let objects = vec![make_meta("a.txt", 100, false),- make_meta("sub/b.txt", 200, false),- make_meta("sub/deep/c.txt", 300, false),+ make_meta("CONTRIBUTING_CN/file.txt", 200, false),+ make_meta("b/file.txt", 300, false),];let result = build_tree_entries_from_flat_listing("/",@@ -233,7 +246,7 @@ mod tests {).unwrap();let paths: Vec<&str> = result.iter().map(|e| e.path.as_str()).collect();- assert_eq!(paths, vec!["/a.txt", "/sub"]);+ assert_eq!(paths, vec!["/b", "/CONTRIBUTING_CN", "/a.txt"]);}#[test]