kaish-kernel 0.16.0

Core kernel for kaish: lexer, parser, interpreter, and runtime
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
//! glob — Expand glob patterns to matching file paths.

use async_trait::async_trait;
use clap::{CommandFactory, Parser};

use crate::ast::Value;
use crate::backend_walker_fs::BackendWalkerFs;
use crate::interpreter::{EntryType, ExecResult, OutputData, OutputNode};
use crate::tools::builtin::read_repeatable_strings;
use crate::tools::{schema_from_clap, ExecContext, ToolCtx, GlobalFlags, Tool, ToolArgs, ToolSchema};
use crate::walker::{
    build_file_types, list_file_types, EntryTypes, FileWalker, GlobPath, IncludeExclude, WalkOptions,
};

/// Glob tool: expand glob patterns to matching file paths.
pub struct Glob;

/// clap-derived argv layer for glob.
#[derive(Parser, Debug)]
#[command(name = "glob", about = "Expand glob patterns to matching file paths")]
struct GlobArgs {
    /// Maximum depth to recurse.
    #[arg(short = 'd', long = "depth")]
    depth: Option<i64>,

    /// Include ignored files like .git, node_modules.
    #[arg(long = "no-ignore", visible_alias = "no_ignore")]
    no_ignore: bool,

    /// Include hidden files starting with `.`.
    #[arg(short = 'a', long = "hidden")]
    hidden: bool,

    /// Entry type: f=files, d=dirs, a=all.
    #[arg(id = "type", short = 't', long = "type")]
    type_: Option<String>,

    /// Filter to files of the given type(s), e.g. `--ftype rust`. Repeatable.
    /// File-type (rg-style, by extension) — distinct from `-t`/`--type`, which
    /// is *entry kind* (file/dir). See `--ftype-list`.
    #[arg(long = "ftype")]
    ftype: Vec<String>,

    /// Exclude files of the given type(s). Repeatable.
    #[arg(long = "ftype-not")]
    ftype_not: Vec<String>,

    /// List known file types (TYPE → globs) and exit. No pattern needed.
    #[arg(long = "ftype-list")]
    ftype_list: bool,

    /// Null-separated output.
    #[arg(short = '0', long = "null")]
    null: bool,

    /// Include pattern: only matches also matching one of these are kept
    /// (repeatable).
    #[arg(long = "include")]
    include: Vec<String>,

    /// Exclude pattern: matches are dropped, directories pruned (repeatable).
    #[arg(long = "exclude")]
    exclude: Vec<String>,

    #[command(flatten)]
    global: GlobalFlags,

    /// Glob pattern(s) to expand.
    pattern: Vec<String>,
}

#[async_trait]
impl Tool for Glob {
    fn name(&self) -> &str {
        "glob"
    }

    fn schema(&self) -> ToolSchema {
        // glob_passthrough: the argv binder hands bare patterns through as
        // written — without it, `glob **/*.rs` (as every example here spells
        // it) would bind the first *matching path* as the pattern.
        schema_from_clap(
            &GlobArgs::command(),
            "glob",
            "Expand glob patterns to matching file paths",
            [
                ("Find all Rust files", "glob **/*.rs"),
                ("Find in specific directory", "glob src/**/*.rs"),
                ("Multiple patterns", "glob **/*.rs **/*.toml"),
                ("Multiple extensions", "glob **/*.{rs,go,py}"),
                ("With depth limit", "glob **/*.rs -d 3"),
                ("Include hidden files", "glob **/*.rs -a"),
                ("Null-separated for xargs", "glob **/*.rs -0"),
                ("Exclude test files", "glob **/*.rs --exclude='*_test.rs'"),
            ],
        )
        .with_glob_passthrough()
    }

    async fn execute(&self, mut args: ToolArgs, ctx: &mut dyn ToolCtx) -> ExecResult {
        let Some(ctx) = ctx.as_any_mut().downcast_mut::<ExecContext>() else {
            return ExecResult::failure(1, "internal error: kernel builtin requires ExecContext");
        };
        // Tests poke args.named.insert("no-ignore", Value::Bool(true)); promote
        // such bool-typed named entries to flag form so clap accepts them.
        args.flagify_bool_named(&self.schema());

        let argv = match args.to_argv() {
            Ok(v) => v,
            Err(e) => return ExecResult::failure(2, format!("glob: {e}")),
        };
        let parsed = match GlobArgs::try_parse_from(
            std::iter::once("glob".to_string()).chain(argv),
        ) {
            Ok(p) => p,
            Err(e) => return ExecResult::failure(2, format!("glob: {e}")),
        };
        parsed.global.apply(ctx);

        // `--ftype-list`: emit the TYPE→globs table and exit, no pattern needed.
        if parsed.ftype_list {
            let rows: Vec<OutputNode> = list_file_types()
                .into_iter()
                .map(|(name, globs)| OutputNode::new(&name).with_cells(vec![globs.join(", ")]))
                .collect();
            let table = OutputData::table(vec!["TYPE".to_string(), "GLOBS".to_string()], rows);
            return ExecResult::with_output(table);
        }

        // Build the rg-style file-type filter from `--ftype`/`--ftype-not`.
        // Repeatable value flags arrive as `Json(Array)`; read them off the raw
        // args (shared with grep). An unknown type name is loud (exit 2).
        let ftype_select = match read_repeatable_strings(&args, "ftype") {
            Ok(v) => v,
            Err(e) => return ExecResult::failure(2, format!("glob: {e}")),
        };
        let ftype_negate = match read_repeatable_strings(&args, "ftype-not") {
            Ok(v) => v,
            Err(e) => return ExecResult::failure(2, format!("glob: {e}")),
        };
        let file_types = match build_file_types(&ftype_select, &ftype_negate) {
            Ok(t) => t,
            Err(e) => return ExecResult::failure(2, format!("glob: {e}")),
        };

        // Every positional is a pattern. The argv binder hands bare patterns
        // through as written (ToolSchema::glob_passthrough), so this is the
        // whole pattern list; Value-typed positionals are read off
        // `args.positional`, not the clap struct (to_argv is lossy).
        let patterns: Vec<String> = args
            .positional
            .iter()
            .map(crate::interpreter::value_to_string)
            .collect();
        if patterns.is_empty() {
            return ExecResult::failure(1, "glob: missing pattern argument");
        }

        // Parse options off the clap struct. A non-numeric `--depth` was
        // already a loud clap error; a negative one is refused here rather
        // than wrapping to "effectively unlimited".
        let max_depth: Option<usize> = match parsed.depth {
            Some(d) if d < 0 => {
                return ExecResult::failure(
                    2,
                    format!("glob: invalid --depth {d}: must be >= 0"),
                )
            }
            other => other.map(|d| d as usize),
        };

        let no_ignore = parsed.no_ignore;
        let include_hidden = parsed.hidden;
        let null_sep = parsed.null;

        // Entry types: f=files (default), d=dirs, a=all. Anything else used
        // to silently mean "files"; now it's a usage error.
        let entry_types = match parsed.type_.as_deref() {
            None | Some("f") => EntryTypes::files_only(),
            Some("d") => EntryTypes::dirs_only(),
            Some("a") => EntryTypes::all(),
            Some(other) => {
                return ExecResult::failure(
                    2,
                    format!("glob: invalid --type '{other}': expected f, d, or a"),
                )
            }
        };

        // Build the include/exclude filter. Repeatable value flags arrive as
        // `Json(Array)`; read them off the raw args like `--ftype` above.
        let mut filter = IncludeExclude::new();
        let includes = match read_repeatable_strings(&args, "include") {
            Ok(v) => v,
            Err(e) => return ExecResult::failure(2, format!("glob: {e}")),
        };
        let excludes = match read_repeatable_strings(&args, "exclude") {
            Ok(v) => v,
            Err(e) => return ExecResult::failure(2, format!("glob: {e}")),
        };
        for pattern in includes {
            filter.include(&pattern);
        }
        for pattern in excludes {
            filter.exclude(&pattern);
        }

        let fs = BackendWalkerFs(ctx.backend.as_ref());
        let mut nodes: Vec<OutputNode> = Vec::new();
        let mut seen: std::collections::HashSet<String> = std::collections::HashSet::new();

        for pattern in &patterns {
            let glob = match GlobPath::new(pattern) {
                Ok(g) => g,
                Err(e) => return ExecResult::failure(1, format!("glob: invalid pattern: {}", e)),
            };

            // Names are reported relative to the conventional root — `/` for an
            // anchored pattern, the cwd otherwise — regardless of where the walk
            // actually starts. Captured before `split_static_dir` shadows `glob`
            // below with an always-unanchored remainder pattern.
            let is_anchored = glob.is_anchored();
            let report_root = if is_anchored {
                ctx.resolve_path("/")
            } else {
                ctx.resolve_path(".")
            };

            // Walk from the pattern's deepest static directory, not from `/`.
            // Walking from `/` is O(filesystem) and — fatally — the walker skips
            // hidden intermediate directories, so an anchored pattern routed
            // through a hidden dir (e.g. tempfile's `/tmp/.tmpXXXX/*.txt`) matched
            // nothing. `split_static_dir` peels the literal leading components into
            // the walk root and leaves the remainder as the match pattern.
            let (static_dir, glob) = glob.split_static_dir();
            let root = if static_dir.as_os_str().is_empty() {
                report_root.clone()
            } else {
                report_root.join(&static_dir)
            };

            let options = WalkOptions {
                max_depth,
                entry_types,
                respect_gitignore: if no_ignore { false } else { ctx.ignore_config.auto_gitignore() },
                include_hidden,
                filter: filter.clone(),
                types: file_types.clone(),
                ..WalkOptions::default()
            };

            let mut walker = FileWalker::new(&fs, &root)
                .with_pattern(glob)
                .with_options(options);

            // Inject ignore filter from config (unless --no-ignore)
            if !no_ignore {
                if let Some(ignore_filter) = ctx.build_ignore_filter(&root).await {
                    walker = walker.with_ignore(ignore_filter);
                }
            }

            let paths = match walker.collect().await {
                Ok(p) => p,
                Err(e) => return ExecResult::failure(1, format!("glob: {}", e)),
            };

            // Strict-glob guarantee, per pattern: zero matches is an error
            // naming the pattern that missed, not silent success. This is
            // consistent with how the kernel treats a bare glob in argv
            // position (no matches → error). The `glob` builtin must enforce
            // the same contract so agents can rely on a non-zero exit to
            // detect misses.
            if paths.is_empty() {
                return ExecResult::failure(1, format!("glob: no matches for pattern '{pattern}'"));
            }

            // Build OutputNodes for each matched path, deduped across patterns
            // by reported name. An anchored pattern reports the full absolute
            // path: `report_root` is `/` for that case, and stripping it as a
            // path prefix would also strip the leading `/` itself (`/a/b`
            // .strip_prefix("/") == "a/b`), silently turning an absolute
            // result relative. Only a relative pattern's cwd-rooted walk gets
            // stripped down to a bare relative name.
            for p in &paths {
                let name = if is_anchored {
                    p.to_string_lossy().to_string()
                } else {
                    let rel = p.strip_prefix(&report_root).unwrap_or(p);
                    rel.to_string_lossy().to_string()
                };
                if !seen.insert(name.clone()) {
                    continue;
                }
                let entry_type = if p.extension().is_none() && name.ends_with('/') {
                    EntryType::Directory
                } else {
                    EntryType::File
                };
                nodes.push(OutputNode::new(name).with_entry_type(entry_type));
            }
        }

        // Build JSON array for structured pipeline flow (same pattern as seq/split/find)
        let json_array: Vec<serde_json::Value> = nodes
            .iter()
            .map(|n| serde_json::Value::String(n.name.clone()))
            .collect();

        // Null-separated mode returns plain text for xargs compatibility
        if null_sep {
            let output: String = nodes.iter()
                .map(|n| n.name.as_str())
                .collect::<Vec<_>>()
                .join("\0");
            return ExecResult::success(output);
        }

        let mut result = ExecResult::with_output(OutputData::nodes(nodes));
        result.data = Some(Value::Json(serde_json::Value::Array(json_array)));
        // Text is the default here; `--json` serializes each name as its own
        // JSON string and never joins them by newline, so it stays the
        // documented lossless way past a newline-bearing name. See
        // `guard_no_newline_names`.
        if ctx.output_format.is_none()
            && let Some(output) = result.output()
            && let Err(e) = super::guard_no_newline_names("glob", output)
        {
            return ExecResult::failure(2, e);
        }
        result
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::vfs::{Filesystem, MemoryFs, VfsRouter};
    use std::path::Path;
    use std::sync::Arc;

    async fn make_ctx() -> ExecContext {
        let mut vfs = VfsRouter::new();
        let mem = MemoryFs::new();

        mem.mkdir(Path::new("src")).await.unwrap();
        mem.mkdir(Path::new("src/lib")).await.unwrap();
        mem.mkdir(Path::new("test")).await.unwrap();

        mem.write(Path::new("src/main.rs"), b"fn main() {}")
            .await
            .unwrap();
        mem.write(Path::new("src/lib.rs"), b"pub mod lib;")
            .await
            .unwrap();
        mem.write(Path::new("src/lib/utils.rs"), b"pub fn util() {}")
            .await
            .unwrap();
        mem.write(Path::new("test/main_test.rs"), b"#[test]")
            .await
            .unwrap();
        mem.write(Path::new("README.md"), b"# Test").await.unwrap();
        mem.write(Path::new(".hidden"), b"secret").await.unwrap();

        vfs.mount("/", mem);
        ExecContext::new(Arc::new(vfs))
    }

    #[tokio::test]
    async fn test_glob_basic() {
        let mut ctx = make_ctx().await;
        let mut args = ToolArgs::new();
        args.positional.push(Value::String("**/*.rs".into()));

        let result = Glob.execute(args, &mut ctx).await;
        assert!(result.ok());
        assert!(result.text_out().contains("main.rs"));
        assert!(result.text_out().contains("lib.rs"));
        assert!(result.text_out().contains("utils.rs"));
        assert!(!result.text_out().contains("README.md"));
    }

    #[tokio::test]
    async fn test_glob_scoped() {
        let mut ctx = make_ctx().await;
        let mut args = ToolArgs::new();
        args.positional.push(Value::String("src/**/*.rs".into()));

        let result = Glob.execute(args, &mut ctx).await;
        assert!(result.ok());
        assert!(result.text_out().contains("main.rs"));
        assert!(result.text_out().contains("lib.rs"));
        assert!(!result.text_out().contains("main_test.rs"));
    }

    #[tokio::test]
    async fn test_glob_json_via_global_flag() {
        use crate::interpreter::{apply_output_format, OutputFormat};

        let mut ctx = make_ctx().await;
        let mut args = ToolArgs::new();
        args.positional.push(Value::String("**/*.rs".into()));

        let result = Glob.execute(args, &mut ctx).await;
        assert!(result.ok());
        // Should have structured OutputData
        assert!(result.has_output());

        // Simulate global --json (handled by kernel)
        let result = apply_output_format(result, OutputFormat::Json);
        assert!(result.text_out().starts_with('['));
        assert!(result.text_out().ends_with(']'));
    }

    #[tokio::test]
    async fn test_glob_exclude() {
        let mut ctx = make_ctx().await;
        let mut args = ToolArgs::new();
        args.positional.push(Value::String("**/*.rs".into()));
        args.named
            .insert("exclude".to_string(), Value::String("*_test.rs".into()));

        let result = Glob.execute(args, &mut ctx).await;
        assert!(result.ok());
        assert!(result.text_out().contains("main.rs"));
        assert!(!result.text_out().contains("main_test.rs"));
    }

    #[tokio::test]
    async fn test_glob_hidden() {
        let mut ctx = make_ctx().await;
        let mut args = ToolArgs::new();
        args.positional.push(Value::String("*".into()));
        args.flags.insert("a".to_string());
        args.named
            .insert("no_ignore".to_string(), Value::Bool(true));

        let result = Glob.execute(args, &mut ctx).await;
        assert!(result.ok());
        assert!(result.text_out().contains(".hidden"));
    }

    #[tokio::test]
    async fn test_glob_result_data() {
        let mut ctx = make_ctx().await;
        let mut args = ToolArgs::new();
        args.positional.push(Value::String("**/*.rs".into()));

        let result = Glob.execute(args, &mut ctx).await;
        assert!(result.ok());
        // result.data should be a JSON array of path strings
        let data = result.data.expect("glob should set result.data");
        if let Value::Json(serde_json::Value::Array(arr)) = data {
            assert!(!arr.is_empty(), "should have matched .rs files");
            // All entries should be strings
            for entry in &arr {
                assert!(entry.is_string(), "each entry should be a string: {:?}", entry);
            }
            // Should contain our test files
            let paths: Vec<&str> = arr.iter().filter_map(|v| v.as_str()).collect();
            assert!(paths.iter().any(|p| p.contains("main.rs")));
            assert!(paths.iter().any(|p| p.contains("lib.rs")));
        } else {
            panic!("Expected JSON array, got: {:?}", data);
        }
    }

    #[tokio::test]
    async fn test_glob_no_pattern() {
        let mut ctx = make_ctx().await;
        let args = ToolArgs::new();

        let result = Glob.execute(args, &mut ctx).await;
        assert!(!result.ok());
        assert!(result.err.contains("missing pattern"));
    }

    #[tokio::test]
    async fn test_glob_anchored_through_hidden_dir() {
        // Regression: an absolute pattern routed through a HIDDEN intermediate
        // directory matched nothing. The walk started at `/` and the walker
        // skips hidden entries, so the `.work` component was never descended —
        // which is exactly how `tempfile`'s `/tmp/.tmpXXXX/*.txt` resolved to
        // zero matches. The fix walks from the pattern's static directory
        // (`/.work/data`) instead of `/`.
        let mut vfs = VfsRouter::new();
        let mem = MemoryFs::new();
        mem.mkdir(Path::new(".work")).await.unwrap();
        mem.mkdir(Path::new(".work/data")).await.unwrap();
        mem.mkdir(Path::new(".work/data/sub")).await.unwrap();
        mem.write(Path::new(".work/data/a.txt"), b"a").await.unwrap();
        mem.write(Path::new(".work/data/b.txt"), b"b").await.unwrap();
        // A deeper file must NOT be caught by a fixed-depth `*.txt`.
        mem.write(Path::new(".work/data/sub/c.txt"), b"c").await.unwrap();
        vfs.mount("/", mem);
        let mut ctx = ExecContext::new(Arc::new(vfs));

        let mut args = ToolArgs::new();
        args.positional.push(Value::String("/.work/data/*.txt".into()));
        let result = Glob.execute(args, &mut ctx).await;
        assert!(result.ok(), "glob failed: {}", result.err);

        let out = result.text_out();
        assert!(out.contains("a.txt"), "should match a.txt through hidden dir: {out:?}");
        assert!(out.contains("b.txt"), "should match b.txt through hidden dir: {out:?}");
        assert!(!out.contains("c.txt"), "*.txt must not match nested sub/c.txt: {out:?}");
    }

    #[tokio::test]
    async fn test_glob_anchored_exact_literal_file() {
        // All-literal anchored pattern: `split_static_dir` must keep the final
        // component as the match target (walk `/proj`, match `only.txt`) rather
        // than trying to descend into the file itself.
        let mut vfs = VfsRouter::new();
        let mem = MemoryFs::new();
        mem.mkdir(Path::new("proj")).await.unwrap();
        mem.write(Path::new("proj/only.txt"), b"x").await.unwrap();
        mem.write(Path::new("proj/other.txt"), b"y").await.unwrap();
        vfs.mount("/", mem);
        let mut ctx = ExecContext::new(Arc::new(vfs));

        let mut args = ToolArgs::new();
        args.positional.push(Value::String("/proj/only.txt".into()));
        let result = Glob.execute(args, &mut ctx).await;
        assert!(result.ok(), "glob failed: {}", result.err);

        let out = result.text_out();
        assert!(out.contains("only.txt"), "should match the exact file: {out:?}");
        assert!(!out.contains("other.txt"), "must not match siblings: {out:?}");
    }

    async fn make_ctx_with_artifacts() -> ExecContext {
        let mut vfs = VfsRouter::new();
        let mem = MemoryFs::new();

        mem.mkdir(Path::new("src")).await.unwrap();
        mem.mkdir(Path::new("target")).await.unwrap();
        mem.mkdir(Path::new("target/debug")).await.unwrap();
        mem.mkdir(Path::new("node_modules")).await.unwrap();
        mem.mkdir(Path::new("node_modules/foo")).await.unwrap();

        mem.write(Path::new("src/main.rs"), b"fn main() {}")
            .await
            .unwrap();
        mem.write(Path::new("target/debug/app.rs"), b"compiled")
            .await
            .unwrap();
        mem.write(Path::new("node_modules/foo/index.js"), b"module")
            .await
            .unwrap();

        vfs.mount("/", mem);
        ExecContext::new(Arc::new(vfs))
    }

    #[tokio::test]
    async fn test_glob_none_config_no_filtering() {
        // IgnoreConfig::none() — glob should return everything
        let mut ctx = make_ctx_with_artifacts().await;
        // Default is IgnoreConfig::none()
        let mut args = ToolArgs::new();
        args.positional.push(Value::String("**/*.rs".into()));

        let result = Glob.execute(args, &mut ctx).await;
        assert!(result.ok());
        assert!(result.text_out().contains("main.rs"));
        // With none config, target/ files should be included
        assert!(result.text_out().contains("app.rs"), "none config should include target/debug/app.rs");
    }

    #[tokio::test]
    async fn test_glob_agent_config_filters_defaults() {
        // IgnoreConfig::agent() — glob should skip target/ and node_modules/
        let mut ctx = make_ctx_with_artifacts().await;
        ctx.ignore_config = crate::ignore_config::IgnoreConfig::agent();

        let mut args = ToolArgs::new();
        args.positional.push(Value::String("**/*.rs".into()));

        let result = Glob.execute(args, &mut ctx).await;
        assert!(result.ok());
        assert!(result.text_out().contains("main.rs"));
        // MCP config has defaults on — target/ should be filtered
        assert!(!result.text_out().contains("app.rs"), "agent config should filter target/debug/app.rs");
    }

    #[tokio::test]
    async fn test_glob_no_ignore_overrides_config() {
        // --no-ignore should bypass config filtering
        let mut ctx = make_ctx_with_artifacts().await;
        ctx.ignore_config = crate::ignore_config::IgnoreConfig::agent();

        let mut args = ToolArgs::new();
        args.positional.push(Value::String("**/*.rs".into()));
        args.flags.insert("no-ignore".to_string());

        let result = Glob.execute(args, &mut ctx).await;
        assert!(result.ok());
        assert!(result.text_out().contains("main.rs"));
        // --no-ignore overrides: target/ files should be visible
        assert!(result.text_out().contains("app.rs"), "--no-ignore should bypass config");
    }
}