agentignore 0.4.3

FUSE filesystem that hides files matching .agentignore rules from processes - control what agents can see while building apps
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
use agentignore::fs::AgentFS;

use fuser::{FileType, INodeNo};

use std::ffi::OsStr;
use std::fs;
use std::path::PathBuf;

mod common;

#[test]
fn agentfs_new_stores_root() {
    let (_dir, root) = common::test_dir();
    let fs = AgentFS::new(root.clone());
    assert_eq!(fs.root, root);
}

#[test]
fn agentfs_is_hidden_delegates_to_policy() {
    let (_dir, root) = common::test_dir();
    common::make_agentignore(&root, "secret.txt\n");
    common::touch(&root.join("secret.txt"));
    common::touch(&root.join("visible.txt"));

    let fs = AgentFS::new(root.clone());
    assert!(fs.is_hidden(&root.join("secret.txt")));
    assert!(!fs.is_hidden(&root.join("visible.txt")));
}

#[test]
fn agentfs_real_path_returns_none_for_unknown_ino() {
    let (_dir, root) = common::test_dir();
    let fs = AgentFS::new(root);
    assert!(fs.real_path(INodeNo(9999)).is_none());
}

#[test]
fn agentfs_real_path_returns_root_for_root_ino() {
    let (_dir, root) = common::test_dir();
    let fs = AgentFS::new(root.clone());
    assert_eq!(fs.real_path(INodeNo::ROOT), Some(root));
}

#[test]
fn agentfs_lookup_child_nonexistent() {
    let (_dir, root) = common::test_dir();
    let fs = AgentFS::new(root.clone());
    assert!(
        fs.lookup_child(INodeNo::ROOT, OsStr::new("nonexistent"), None)
            .is_none()
    );
}

#[test]
fn agentfs_lookup_child_finds_existing_file() {
    let (_dir, root) = common::test_dir();
    common::touch(&root.join("hello.txt"));
    let fs = AgentFS::new(root.clone());
    let (path, _resolved, ino) = fs
        .lookup_child(INodeNo::ROOT, OsStr::new("hello.txt"), None)
        .unwrap();
    assert!(path.ends_with("hello.txt"));
    assert!(ino >= 2);
}

#[test]
fn agentfs_lookup_child_hidden_file() {
    let (_dir, root) = common::test_dir();
    common::make_agentignore(&root, "*.secret\n");
    common::touch(&root.join("data.secret"));
    let fs = AgentFS::new(root.clone());
    assert!(
        fs.lookup_child(INodeNo::ROOT, OsStr::new("data.secret"), None)
            .is_none()
    );
}

#[test]
fn agentfs_lookup_child_path_escape() {
    let (_dir, root) = common::test_dir();
    let escape_target = if cfg!(target_os = "linux") {
        PathBuf::from("/etc/passwd")
    } else {
        PathBuf::from("/etc/hosts")
    };
    let link_path = root.join("escape");
    std::os::unix::fs::symlink(&escape_target, &link_path).unwrap();

    let fs = AgentFS::new(root.clone());
    assert!(
        fs.lookup_child(INodeNo::ROOT, OsStr::new("escape"), None)
            .is_none()
    );
}

#[test]
fn agentfs_lookup_child_finds_directory() {
    let (_dir, root) = common::test_dir();
    common::mkdirp(&root.join("mydir"));
    let fs = AgentFS::new(root.clone());
    let (path, _resolved, _ino) = fs
        .lookup_child(INodeNo::ROOT, OsStr::new("mydir"), None)
        .unwrap();
    assert!(path.ends_with("mydir"));
    assert!(path.is_dir());
}

#[test]
fn agentfs_stat_returns_attributes_for_file() {
    let (_dir, root) = common::test_dir();
    let path = root.join("stat_test.txt");
    fs::write(&path, b"hello world").unwrap();

    let fs = AgentFS::new(root.clone());
    let attr = fs.stat(INodeNo::ROOT, &path).unwrap();
    assert_eq!(attr.size, 11);
    assert_eq!(attr.kind, FileType::RegularFile);
}

#[test]
fn agentfs_stat_returns_attributes_for_directory() {
    let (_dir, root) = common::test_dir();
    common::mkdirp(&root.join("somedir"));

    let fs = AgentFS::new(root.clone());
    let attr = fs.stat(INodeNo::ROOT, &root.join("somedir")).unwrap();
    assert_eq!(attr.kind, FileType::Directory);
}

#[test]
fn agentfs_stat_returns_none_for_nonexistent() {
    let (_dir, root) = common::test_dir();
    let fs = AgentFS::new(root);
    assert!(
        fs.stat(INodeNo::ROOT, &PathBuf::from("/nonexistent_path_12345"))
            .is_none()
    );
}

#[test]
fn agentfs_stat_for_symlink() {
    let (_dir, root) = common::test_dir();
    common::touch(&root.join("target"));
    std::os::unix::fs::symlink(root.join("target"), root.join("link")).unwrap();

    let fs = AgentFS::new(root.clone());
    let attr = fs.stat(INodeNo::ROOT, &root.join("link")).unwrap();
    assert_eq!(attr.kind, FileType::Symlink);
}

#[test]
fn agentfs_stat_inode_number() {
    let (_dir, root) = common::test_dir();
    common::touch(&root.join("foo.txt"));
    let fs = AgentFS::new(root.clone());
    let attr = fs.stat(INodeNo(42), &root.join("foo.txt")).unwrap();
    assert_eq!(attr.ino, INodeNo(42));
}

#[test]
fn agentfs_lookup_child_caches_inode() {
    let (_dir, root) = common::test_dir();
    common::touch(&root.join("same.txt"));
    let fs = AgentFS::new(root.clone());
    let (_, _, ino1) = fs
        .lookup_child(INodeNo::ROOT, OsStr::new("same.txt"), None)
        .unwrap();
    let (_, _, ino2) = fs
        .lookup_child(INodeNo::ROOT, OsStr::new("same.txt"), None)
        .unwrap();
    assert_eq!(ino1, ino2);
}

#[test]
fn agentfs_lookup_child_different_inodes() {
    let (_dir, root) = common::test_dir();
    common::touch(&root.join("one.txt"));
    common::touch(&root.join("two.txt"));
    let fs = AgentFS::new(root.clone());
    let (_, _, ino1) = fs
        .lookup_child(INodeNo::ROOT, OsStr::new("one.txt"), None)
        .unwrap();
    let (_, _, ino2) = fs
        .lookup_child(INodeNo::ROOT, OsStr::new("two.txt"), None)
        .unwrap();
    assert_ne!(ino1, ino2);
}

#[test]
fn agentfs_lookup_child_nested() {
    let (_dir, root) = common::test_dir();
    common::mkdirp(&root.join("a").join("b"));
    common::touch(&root.join("a").join("b").join("c.txt"));
    let fs = AgentFS::new(root.clone());

    let (a_path, _, a_ino) = fs
        .lookup_child(INodeNo::ROOT, OsStr::new("a"), None)
        .unwrap();
    assert!(a_path.ends_with("a"));

    let (b_path, _, b_ino) = fs
        .lookup_child(INodeNo(a_ino), OsStr::new("b"), None)
        .unwrap();
    assert!(b_path.ends_with("b"));

    let (c_path, _, _) = fs
        .lookup_child(INodeNo(b_ino), OsStr::new("c.txt"), None)
        .unwrap();
    assert!(c_path.ends_with("c.txt"));
}

#[test]
fn agentfs_lookup_child_hidden_directory() {
    let (_dir, root) = common::test_dir();
    common::make_agentignore(&root, "secret/\n");
    common::mkdirp(&root.join("secret"));
    let fs = AgentFS::new(root.clone());
    assert!(
        fs.lookup_child(INodeNo::ROOT, OsStr::new("secret"), None)
            .is_none()
    );
}

#[test]
fn agentfs_lookup_child_symlink_to_file_inside_root() {
    let (_dir, root) = common::test_dir();
    common::touch(&root.join("realfile.txt"));
    std::os::unix::fs::symlink(root.join("realfile.txt"), root.join("link.txt")).unwrap();

    let fs = AgentFS::new(root.clone());
    let (path, resolved, _ino) = fs
        .lookup_child(INodeNo::ROOT, OsStr::new("link.txt"), None)
        .unwrap();
    // After the fix, lookup_child returns the access path (the symlink),
    // not the canonicalized target. The resolved target is returned separately.
    assert!(path.ends_with("link.txt"));
    // But the resolved target should point to realfile.txt
    assert_eq!(resolved, Some(root.join("realfile.txt")));
}

#[test]
fn agentfs_lookup_child_symlink_to_hidden_target_returns_none() {
    let (_dir, root) = common::test_dir();
    common::make_agentignore(&root, ".env\n");
    common::touch(&root.join(".env"));
    std::os::unix::fs::symlink(root.join(".env"), root.join("link_to_env")).unwrap();

    let fs = AgentFS::new(root.clone());
    assert!(
        fs.lookup_child(INodeNo::ROOT, OsStr::new("link_to_env"), None)
            .is_none()
    );
}

#[test]
fn agentfs_lookup_child_dangling_symlink_returns_some() {
    // A dangling symlink still exists as an entry — it should be visible.
    let (_dir, root) = common::test_dir();
    std::os::unix::fs::symlink(root.join("nonexistent_target"), root.join("dangling")).unwrap();

    let fs = AgentFS::new(root.clone());
    let (path, resolved, _ino) = fs
        .lookup_child(INodeNo::ROOT, OsStr::new("dangling"), None)
        .unwrap();
    assert!(path.ends_with("dangling"));
    // Resolved is None because canonicalize fails (target doesn't exist)
    assert!(resolved.is_none());
}

#[test]
fn agentfs_symlink_and_target_get_distinct_inodes() {
    let (_dir, root) = common::test_dir();
    common::touch(&root.join("real_file"));
    std::os::unix::fs::symlink(root.join("real_file"), root.join("the_link")).unwrap();

    let fs = AgentFS::new(root.clone());
    let (link_path, _, link_ino) = fs
        .lookup_child(INodeNo::ROOT, OsStr::new("the_link"), None)
        .unwrap();
    let (target_path, _, target_ino) = fs
        .lookup_child(INodeNo::ROOT, OsStr::new("real_file"), None)
        .unwrap();

    // Inodes must be different — symlink != target
    assert_ne!(link_ino, target_ino);
    // The link access path is the symlink itself
    assert!(link_path.ends_with("the_link"));
    // The target access path is the real file
    assert!(target_path.ends_with("real_file"));
}

#[test]
fn agentfs_two_symlinks_to_same_target_get_distinct_inodes() {
    let (_dir, root) = common::test_dir();
    common::touch(&root.join("target.txt"));
    std::os::unix::fs::symlink(root.join("target.txt"), root.join("link_a")).unwrap();
    std::os::unix::fs::symlink(root.join("target.txt"), root.join("link_b")).unwrap();

    let fs = AgentFS::new(root.clone());
    let (_, _, ino_a) = fs
        .lookup_child(INodeNo::ROOT, OsStr::new("link_a"), None)
        .unwrap();
    let (_, _, ino_b) = fs
        .lookup_child(INodeNo::ROOT, OsStr::new("link_b"), None)
        .unwrap();

    assert_ne!(
        ino_a, ino_b,
        "two symlinks to same target must have distinct inodes"
    );
}

#[test]
fn agentfs_unlink_symlink_removes_symlink_not_target() {
    let (_dir, root) = common::test_dir();
    common::touch(&root.join("precious.txt"));
    std::os::unix::fs::symlink(root.join("precious.txt"), root.join("link_to_precious")).unwrap();

    // Verify both exist
    assert!(root.join("precious.txt").exists());
    assert!(root.join("link_to_precious").exists());

    // Use lookup_child to get the access path (the symlink node), then remove it
    let fs = AgentFS::new(root.clone());
    let (access_path, _, _ino) = fs
        .lookup_child(INodeNo::ROOT, OsStr::new("link_to_precious"), None)
        .unwrap();

    // Remove the symlink via its access path (not the resolved target)
    std::fs::remove_file(&access_path).unwrap();

    // The target must survive
    assert!(
        root.join("precious.txt").exists(),
        "target file was removed!"
    );
    // The symlink itself is gone
    assert!(
        !root.join("link_to_precious").exists(),
        "symlink was not removed"
    );
}

#[test]
fn agentfs_stat_on_symlink_returns_correct_size() {
    let (_dir, root) = common::test_dir();
    common::touch(&root.join("target_large"));
    // Write some content so target has a non-zero size
    std::fs::write(root.join("target_large"), b"some content here").unwrap();
    let link_path = root.join("mylink");
    let target_path = root.join("target_large");
    // Create a symlink. The size should be the length of the target path string.
    std::os::unix::fs::symlink(&target_path, &link_path).unwrap();

    // The symlink's size on disk is the byte length of the target path
    let symlink_target_content = std::fs::read_link(&link_path).unwrap();
    let expected_symlink_size = symlink_target_content.as_os_str().len() as u64;

    let fs = AgentFS::new(root.clone());
    let attr = fs.stat(INodeNo(2), &link_path).unwrap();

    assert_eq!(attr.kind, FileType::Symlink);
    // The symlink's size is the length of the target path string, not the target file content size
    assert_eq!(
        attr.size, expected_symlink_size,
        "symlink size must be the length of the target path string"
    );
    // Verify the symlink size differs from the target file's content size
    let target_meta = std::fs::metadata(&target_path).unwrap();
    assert_ne!(
        attr.size,
        target_meta.len(),
        "symlink stat size must differ from target file size"
    );
}

#[test]
fn agentfs_resolve_child_hidden_check_denies_symlink_to_hidden() {
    let (_dir, root) = common::test_dir();
    common::make_agentignore(&root, ".secret\n");
    common::touch(&root.join(".secret"));
    std::os::unix::fs::symlink(root.join(".secret"), root.join("sym_to_secret")).unwrap();

    let fs = AgentFS::new(root.clone());
    assert!(
        fs.lookup_child(INodeNo::ROOT, OsStr::new("sym_to_secret"), None)
            .is_none(),
        "symlink to hidden target should be rejected"
    );
}

#[test]
fn agentfs_resolve_child_accepts_symlink_to_visible_target() {
    let (_dir, root) = common::test_dir();
    common::make_agentignore(&root, ".secret\n");
    common::touch(&root.join("visible.txt"));
    common::touch(&root.join(".secret"));
    std::os::unix::fs::symlink(root.join("visible.txt"), root.join("sym_to_visible")).unwrap();

    let fs = AgentFS::new(root.clone());
    let (path, resolved, _ino) = fs
        .lookup_child(INodeNo::ROOT, OsStr::new("sym_to_visible"), None)
        .unwrap();
    assert!(path.ends_with("sym_to_visible"));
    assert_eq!(resolved, Some(root.join("visible.txt")));
}