Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

fix(agent): file_list with a pattern looks into subfolders, knows {a,b}, and never calls a full folder empty (CA)

purpleefc90e6abc48
diff --git a/src-tauri/src/commands/filesystem.rs b/src-tauri/src/commands/filesystem.rs
index f8306bcf..aea1308e 100644
--- a/src-tauri/src/commands/filesystem.rs
+++ b/src-tauri/src/commands/filesystem.rs
@@ -1208,6 +1208,32 @@ pub(crate) fn fs_write_bytes_sync(
}))
}
+/// `*.{js,mjs}` → `*.js`, `*.mjs`. One level of braces per group, groups
+/// expand left to right, at most 32 patterns so a crafted pattern stays cheap.
+fn expand_braces(pat: &str) -> Vec<String> {
+ let mut out = vec![pat.to_string()];
+ loop {
+ let mut next = Vec::new();
+ let mut changed = false;
+ for p in &out {
+ match (p.find('{'), p.find('}')) {
+ (Some(a), Some(b)) if a < b && p[a + 1..b].contains(',') => {
+ changed = true;
+ for alt in p[a + 1..b].split(',') {
+ next.push(format!("{}{}{}", &p[..a], alt, &p[b + 1..]));
+ }
+ }
+ _ => next.push(p.clone()),
+ }
+ }
+ next.truncate(32);
+ out = next;
+ if !changed {
+ return out;
+ }
+ }
+}
+
#[tauri::command]
#[allow(non_snake_case)]
pub fn fs_list(
@@ -1287,16 +1313,27 @@ pub fn fs_list(
return Err("Pattern escapes the allowed workspace".to_string());
}
}
- let glob_pattern = dir.join(pat).to_string_lossy().to_string();
- if let Ok(paths) = glob_match(&glob_pattern) {
- for entry in paths.flatten() {
- if entries.len() >= max_entries {
- break;
- }
- if contain_within(&dir, &entry).is_err() {
- continue;
+ // glob 0.3 knows no braces and file_list dropped `recursive` whenever a
+ // pattern came along: Kimi asked for `*.{js,mjs}` recursively over a
+ // tree of .js files and was told "The folder is empty." (CA bench AG9).
+ let mut seen = std::collections::HashSet::new();
+ 'outer: for one in expand_braces(pat) {
+ let one = if recursive.unwrap_or(false) && !one.contains('/') && !one.contains('\\') {
+ format!("**/{one}")
+ } else {
+ one
+ };
+ let glob_pattern = dir.join(&one).to_string_lossy().to_string();
+ if let Ok(paths) = glob_match(&glob_pattern) {
+ for entry in paths.flatten() {
+ if entries.len() >= max_entries {
+ break 'outer;
+ }
+ if contain_within(&dir, &entry).is_err() || !seen.insert(entry.clone()) {
+ continue;
+ }
+ entries.push(file_meta(&entry));
}
- entries.push(file_meta(&entry));
}
}
} else if recursive.unwrap_or(false) {
@@ -1867,6 +1904,30 @@ mod jail_adversarial_tests {
assert_eq!(ok["count"], 1);
assert!(ok.to_string().contains("inside.txt"));
}
+
+ /// CA bench AG9: `*.{js,mjs}` with recursive=true over a tree of .js files
+ /// came back empty, and the model was told the folder was empty.
+ #[test]
+ fn a_pattern_with_braces_finds_files_in_subfolders() {
+ use std::fs;
+ let base = crate::os_paths::test_dir("globbraces");
+ let ws = base.join("workspace");
+ fs::create_dir_all(ws.join("projekt/modul0")).unwrap();
+ fs::write(ws.join("projekt/modul0/a.js"), b"x").unwrap();
+ fs::write(ws.join("projekt/modul0/b.mjs"), b"x").unwrap();
+ fs::write(ws.join("projekt/README.md"), b"x").unwrap();
+ allow_root_for_test(&ws);
+ let wd = Some(ws.to_string_lossy().to_string());
+
+ let deep = fs_list("projekt".into(), Some(true), Some("*.{js,mjs}".into()), None, wd.clone()).expect("list");
+ assert_eq!(deep["count"], 2, "{deep}");
+ assert!(!deep.to_string().contains("README"));
+
+ let flat = fs_list("projekt".into(), None, Some("*.{js,mjs}".into()), None, wd).expect("list");
+ assert_eq!(flat["count"], 0, "without recursive the pattern stays in this folder");
+ assert_eq!(expand_braces("*.{a,b}/{c,d}").len(), 4);
+ assert_eq!(expand_braces("plain*.ts"), vec!["plain*.ts".to_string()]);
+ }
}
/// The root itself used to be taken on faith. `contain_within` answered
diff --git a/src/api/mcp/builtin-tools.ts b/src/api/mcp/builtin-tools.ts
index 4de75178..0db8d3fd 100644
--- a/src/api/mcp/builtin-tools.ts
+++ b/src/api/mcp/builtin-tools.ts
@@ -954,6 +954,14 @@ async function executeFileEdit(args: ToolArgs, run?: AgentRunContext): Promise<s
return JSON.stringify(w)
}
+/** What file_list says when it lists nothing. */
+export function emptyListAnswer(args: ToolArgs): string {
+ const pattern = typeof args.pattern === 'string' ? args.pattern.trim() : ''
+ if (!pattern) return 'The folder is empty.'
+ if (args.recursive || pattern.includes('/')) return `No file matches "${pattern}" here or in its subfolders.`
+ return `No file in this folder matches "${pattern}". The pattern does not look into subfolders: set recursive=true or use "**/${pattern}".`
+}
+
async function executeFileList(args: ToolArgs, run?: AgentRunContext): Promise<string> {
const bad = missingArgs('file_list', args, 'path')
if (bad) return bad
@@ -973,7 +981,10 @@ async function executeFileList(args: ToolArgs, run?: AgentRunContext): Promise<s
if (Array.isArray(data.entries)) {
// An empty answer read as nothing: the model said "I listed the files"
// and named none, the step showed no result (3.0.4 box run).
- if (!data.entries.length) return 'The folder is empty.'
+ // With a pattern an empty answer means "nothing matched", not "empty":
+ // Kimi read "The folder is empty." for `*.{js,mjs}` over a tree full of
+ // .js files (CA bench AG9).
+ if (!data.entries.length) return emptyListAnswer(args)
return data.entries
.map((e) => `${e.isDir ? '[DIR]' : ''} ${e.name} (${formatBytes(e.size)}) ${shownToolPath(e.path)}`)
.join('\n')
diff --git a/src/lib/__tests__/an-empty-pattern-match-is-not-an-empty-folder.test.ts b/src/lib/__tests__/an-empty-pattern-match-is-not-an-empty-folder.test.ts
new file mode 100644
index 00000000..90887497
--- /dev/null
+++ b/src/lib/__tests__/an-empty-pattern-match-is-not-an-empty-folder.test.ts
@@ -0,0 +1,20 @@
+import { describe, it, expect } from 'vitest'
+import { emptyListAnswer } from '../../api/mcp/builtin-tools'
+
+// CA bench AG9: Kimi listed `*.{js,mjs}` over a tree full of .js files and
+// was told "The folder is empty.", which is false and steers the model wrong.
+describe('file_list with nothing to show', () => {
+ it('calls a folder empty only when no pattern was given', () => {
+ expect(emptyListAnswer({ path: 'projekt' })).toBe('The folder is empty.')
+ })
+ it('says the pattern matched nothing and how to look deeper', () => {
+ const a = emptyListAnswer({ path: 'projekt', pattern: '*.js' })
+ expect(a).not.toMatch(/empty/)
+ expect(a).toMatch(/\*\*\/\*\.js/)
+ })
+ it('does not suggest recursion that already happened', () => {
+ const a = emptyListAnswer({ path: 'projekt', pattern: '*.{js,mjs}', recursive: true })
+ expect(a).toMatch(/subfolders/)
+ expect(a).not.toMatch(/set recursive/)
+ })
+})