attini 0.0.1

CLI coding agent that aims to be as autonomous as it can be, without ever leaving your control
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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
//! Integration tests for `attini::tools::ToolExecutor` patch flow.
//!
//! Each test builds a scratch workspace under the system temp dir,
//! then exercises preview_patch + apply_patch against real files.
//! Filesystem mocks are not used.

use std::fs;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicUsize, Ordering};

use attini::sansio::agent::{PatchError, PatchInvocation, PatchTool};
use attini::sansio::permissions::Rule;
use attini::tools::ToolExecutor;

static COUNTER: AtomicUsize = AtomicUsize::new(0);

struct TempRoot {
    path: PathBuf,
}

impl TempRoot {
    fn new(label: &str) -> Self {
        let n = COUNTER.fetch_add(1, Ordering::SeqCst);
        let path =
            std::env::temp_dir().join(format!("attini-patch-{}-{}-{label}", std::process::id(), n));
        fs::create_dir_all(&path).expect("create temp root");
        Self { path }
    }

    fn path(&self) -> &Path {
        &self.path
    }

    fn write(&self, rel: &str, contents: &[u8]) {
        let full = self.path.join(rel);
        if let Some(parent) = full.parent() {
            fs::create_dir_all(parent).expect("create parent");
        }
        fs::write(&full, contents).expect("write file");
    }

    fn read(&self, rel: &str) -> Vec<u8> {
        fs::read(self.path.join(rel)).expect("read file")
    }
}

impl Drop for TempRoot {
    fn drop(&mut self) {
        let _ = fs::remove_dir_all(&self.path);
    }
}

fn exec(root: &TempRoot) -> ToolExecutor {
    // Patch tool is now gated by git-tracking checks. Initialise the
    // TempRoot as a git repo and `git add -A` any pre-existing files
    // so Layer 3 sees them as tracked; anything the test writes after
    // this call is Add-time under a non-ignored parent, which Layer 3
    // also allows.
    git_init_and_add_all(root.path());
    ToolExecutor::new(root.path(), Vec::new(), "test".to_string()).expect("executor")
}

fn git_init_and_add_all(root: &Path) {
    let _ = std::process::Command::new("git")
        .arg("-C")
        .arg(root)
        .args(["init", "-q"])
        .status();
    // Set a local identity so `git add` does not fail even if the
    // environment lacks a global user.email / user.name.
    let _ = std::process::Command::new("git")
        .arg("-C")
        .arg(root)
        .args(["config", "user.email", "attini-test@example.com"])
        .status();
    let _ = std::process::Command::new("git")
        .arg("-C")
        .arg(root)
        .args(["config", "user.name", "Attini Test"])
        .status();
    let _ = std::process::Command::new("git")
        .arg("-C")
        .arg(root)
        .args(["add", "-A"])
        .status();
}

fn add(path: &str, content: &str) -> PatchTool {
    PatchTool::Add {
        path: path.to_string(),
        content: content.to_string(),
    }
}

fn update(path: &str, before: &str, after: &str) -> PatchTool {
    PatchTool::Update {
        path: path.to_string(),
        before: before.to_string(),
        after: after.to_string(),
    }
}

fn inv(edits: Vec<PatchTool>) -> PatchInvocation {
    PatchInvocation { edits }
}

// -----------------------------------------------------------------
// preview_patch
// -----------------------------------------------------------------

#[test]
fn preview_add_produces_content_none_and_line_stats() {
    let root = TempRoot::new("preview-add");
    let (snapshots, preview) = exec(&root)
        .preview_patch(&inv(vec![add("new.txt", "one\ntwo\n")]))
        .expect("preview ok");
    assert_eq!(snapshots.len(), 1);
    assert_eq!(snapshots[0].path, "new.txt");
    assert_eq!(snapshots[0].content, None);
    assert_eq!(preview.added_lines, 2);
    assert_eq!(preview.removed_lines, 0);
    assert_eq!(preview.edit_count, 1);
    assert_eq!(preview.target_paths, vec!["new.txt".to_string()]);
}

#[test]
fn preview_update_returns_content_of_existing_file() {
    let root = TempRoot::new("preview-update");
    root.write("a.txt", b"hello\nworld\n");
    let (snapshots, preview) = exec(&root)
        .preview_patch(&inv(vec![update("a.txt", "world", "rust")]))
        .expect("preview ok");
    assert_eq!(
        snapshots[0].content.as_deref(),
        Some(b"hello\nworld\n".as_slice())
    );
    assert_eq!(preview.removed_lines, 1);
    assert_eq!(preview.added_lines, 1);
}

#[test]
fn preview_update_on_tracked_file_marks_auto_approve() {
    let root = TempRoot::new("preview-auto-approve-tracked");
    root.write("a.txt", b"hello\nworld\n");
    let (_, preview) = exec(&root)
        .preview_patch(&inv(vec![update("a.txt", "world", "rust")]))
        .expect("preview ok");
    assert!(preview.auto_approve, "tracked update should auto-approve");
}

#[test]
fn preview_add_marks_not_auto_approve() {
    let root = TempRoot::new("preview-auto-approve-add");
    let (_, preview) = exec(&root)
        .preview_patch(&inv(vec![add("new.txt", "one\ntwo\n")]))
        .expect("preview ok");
    assert!(!preview.auto_approve, "add must require approval");
}

#[test]
fn preview_mixed_tracked_update_and_add_marks_not_auto_approve() {
    let root = TempRoot::new("preview-auto-approve-mixed");
    root.write("a.txt", b"hello\n");
    let (_, preview) = exec(&root)
        .preview_patch(&inv(vec![
            update("a.txt", "hello", "hi"),
            add("new.txt", "x\n"),
        ]))
        .expect("preview ok");
    assert!(
        !preview.auto_approve,
        "a patch containing an add must require approval"
    );
}

#[test]
fn preview_update_rejects_no_match() {
    let root = TempRoot::new("preview-nomatch");
    root.write("a.txt", b"hello");
    let err = exec(&root)
        .preview_patch(&inv(vec![update("a.txt", "missing", "x")]))
        .expect_err("no match rejected");
    assert!(matches!(err, PatchError::NoMatch { .. }));
}

#[test]
fn preview_update_rejects_ambiguous_match() {
    let root = TempRoot::new("preview-ambig");
    root.write("a.txt", b"foofoo");
    let err = exec(&root)
        .preview_patch(&inv(vec![update("a.txt", "foo", "bar")]))
        .expect_err("ambiguous rejected");
    match err {
        PatchError::AmbiguousMatch { match_count, .. } => assert_eq!(match_count, 2),
        other => panic!("unexpected: {other:?}"),
    }
}

#[test]
fn preview_add_on_existing_rejects() {
    let root = TempRoot::new("preview-add-exists");
    root.write("a.txt", b"existing");
    let err = exec(&root)
        .preview_patch(&inv(vec![add("a.txt", "new")]))
        .expect_err("already exists rejected");
    assert!(matches!(err, PatchError::AddOnExistingFile { .. }));
}

#[test]
fn preview_update_on_missing_rejects() {
    let root = TempRoot::new("preview-update-missing");
    let err = exec(&root)
        .preview_patch(&inv(vec![update("nope.txt", "a", "b")]))
        .expect_err("missing rejected");
    assert!(matches!(err, PatchError::UpdateOnMissingFile { .. }));
}

#[test]
fn preview_outside_workspace_parks_for_approval() {
    // An `Add` outside the workspace no longer errors; it previews
    // successfully with `auto_approve=false` and a `not_revertible`
    // reason so the shell can park it for human approval.
    let root = TempRoot::new("preview-outside");
    let (_, preview) = exec(&root)
        .preview_patch(&inv(vec![add("../escape.txt", "x")]))
        .expect("outside preview succeeds");
    assert!(!preview.auto_approve);
    assert!(preview.not_revertible.is_some());
}

// -----------------------------------------------------------------
// apply_patch (happy paths)
// -----------------------------------------------------------------

#[test]
fn apply_add_writes_new_file() {
    let root = TempRoot::new("apply-add");
    let invocation = inv(vec![add("greet.txt", "hi\n")]);
    let (hashes, _) = exec(&root).preview_patch(&invocation).unwrap();
    let applied = exec(&root)
        .apply_patch(&invocation, &hashes)
        .expect("apply ok");
    assert_eq!(applied.len(), 1);
    assert_eq!(root.read("greet.txt"), b"hi\n");
}

#[test]
fn apply_update_replaces_unique_substring() {
    let root = TempRoot::new("apply-update");
    root.write("a.txt", b"prefix TODO suffix\n");
    let invocation = inv(vec![update("a.txt", "TODO", "DONE")]);
    let (hashes, _) = exec(&root).preview_patch(&invocation).unwrap();
    exec(&root)
        .apply_patch(&invocation, &hashes)
        .expect("apply");
    assert_eq!(root.read("a.txt"), b"prefix DONE suffix\n");
}

#[test]
fn apply_multiple_edits_across_files() {
    let root = TempRoot::new("apply-multi");
    root.write("a.txt", b"aa");
    let invocation = inv(vec![add("b.txt", "bb"), update("a.txt", "aa", "AA")]);
    let (hashes, _) = exec(&root).preview_patch(&invocation).unwrap();
    exec(&root)
        .apply_patch(&invocation, &hashes)
        .expect("apply");
    assert_eq!(root.read("a.txt"), b"AA");
    assert_eq!(root.read("b.txt"), b"bb");
}

// -----------------------------------------------------------------
// apply_patch (failure paths)
// -----------------------------------------------------------------

#[test]
fn apply_detects_conflict_when_file_changed_after_preview() {
    let root = TempRoot::new("apply-conflict");
    root.write("a.txt", b"orig\n");
    let invocation = inv(vec![update("a.txt", "orig", "new")]);
    let (hashes, _) = exec(&root).preview_patch(&invocation).unwrap();
    // Simulate a concurrent write between preview and apply.
    root.write("a.txt", b"tampered\n");
    let err = exec(&root)
        .apply_patch(&invocation, &hashes)
        .expect_err("conflict");
    assert!(matches!(err, PatchError::Conflict { .. }));
    // Original tampered file is untouched.
    assert_eq!(root.read("a.txt"), b"tampered\n");
}

#[test]
fn apply_atomically_rejects_all_when_one_edit_fails() {
    let root = TempRoot::new("apply-atomic");
    root.write("a.txt", b"orig");
    // First edit is fine; second update targets a file that vanished.
    let invocation = inv(vec![add("new.txt", "hi"), update("gone.txt", "x", "y")]);
    // preview_patch itself fails because gone.txt doesn't exist;
    // to test the phase-1 rollback of tmp files, arrange the failure
    // to happen inside apply_patch after preview succeeded:
    // remove the target between preview and apply.
    root.write("gone.txt", b"x");
    let (hashes, _) = exec(&root).preview_patch(&invocation).unwrap();
    // Delete it so apply's re-read fails.
    fs::remove_file(root.path().join("gone.txt")).unwrap();
    let err = exec(&root)
        .apply_patch(&invocation, &hashes)
        .expect_err("second edit fails");
    assert!(matches!(err, PatchError::UpdateOnMissingFile { .. }));
    // Even though the first (add) edit's tmp file was written, the
    // target new.txt must not exist because phase 1 aborted before
    // any rename.
    assert!(!root.path().join("new.txt").exists());
    // The tmp file must also be cleaned up.
    let leftovers: Vec<_> = fs::read_dir(root.path())
        .unwrap()
        .filter_map(|e| e.ok())
        .filter(|e| e.file_name().to_string_lossy().contains("attini-tmp"))
        .collect();
    assert!(leftovers.is_empty(), "tmp files leaked: {leftovers:?}");
}

#[test]
fn apply_add_on_existing_rejects_at_phase1() {
    let root = TempRoot::new("apply-add-race");
    let invocation = inv(vec![add("racy.txt", "hi")]);
    let (hashes, _) = exec(&root).preview_patch(&invocation).unwrap();
    // Someone else created the file between preview and apply.
    root.write("racy.txt", b"other");
    let err = exec(&root)
        .apply_patch(&invocation, &hashes)
        .expect_err("add on existing");
    assert!(matches!(err, PatchError::AddOnExistingFile { .. }));
    assert_eq!(root.read("racy.txt"), b"other");
}

// -----------------------------------------------------------------
// patch tool must ignore extra_read_roots
// -----------------------------------------------------------------

#[test]
fn patch_add_absolute_path_inside_extra_read_root_parks_for_approval() {
    // extra_read_roots grant read-only access; they do not open a
    // write path. An absolute write target outside the workspace is
    // not refused outright any more — it parks for one-shot human
    // approval — but the preview must not create anything. The point
    // of this test is that extra_read_roots confer no write privilege.
    let root = TempRoot::new("patch-abs-extra-root");
    let extra = TempRoot::new("patch-abs-extra-source");
    let extra_canon = extra.path().canonicalize().expect("canon");
    let executor = ToolExecutor::new(root.path(), vec![extra_canon.clone()], "test".to_string())
        .expect("executor with extras");
    let target = extra
        .path()
        .join("hijack.md")
        .to_string_lossy()
        .into_owned();
    let invocation = inv(vec![add(&target, "gotcha")]);
    let (_, preview) = executor
        .preview_patch(&invocation)
        .expect("absolute add previews (parks for approval)");
    assert!(!preview.auto_approve, "outside write must not auto-approve");
    assert!(
        !extra.path().join("hijack.md").exists(),
        "preview must not have created the file"
    );
}

#[test]
fn patch_update_relative_traversal_outside_workspace_parks_for_approval() {
    // A relative path with `..` that resolves outside the workspace
    // now previews and parks for approval (mirroring `read`), rather
    // than being refused. A matching `write` rule would need an
    // absolute path; with no rule the write waits for the human.
    let root = TempRoot::new("patch-traversal-workspace");
    let extra_parent = TempRoot::new("patch-traversal-parent");
    extra_parent.write("victim.md", b"original\n");
    let extra_canon = extra_parent.path().canonicalize().expect("canon");
    let executor =
        ToolExecutor::new(root.path(), vec![extra_canon], "test".to_string()).expect("executor");
    // Construct a workspace-relative path that resolves outside root.
    let workspace_canon = root.path().canonicalize().expect("workspace canon");
    let rel_to_victim = pathdiff_naive(&workspace_canon, &extra_parent.path().join("victim.md"));
    let invocation = inv(vec![update(&rel_to_victim, "original\n", "hijacked\n")]);
    let (_, preview) = executor
        .preview_patch(&invocation)
        .expect("traversal update previews (parks for approval)");
    assert!(!preview.auto_approve, "outside write must not auto-approve");
    assert_eq!(extra_parent.read("victim.md"), b"original\n");
}

// -----------------------------------------------------------------
// patch write guards (Layer 1-4)
// -----------------------------------------------------------------

/// Build an executor whose workspace is initialised as a git repo,
/// with pre-existing files at `initial_tracked` staged so they enter
/// the tracked set. Both a session-local scratchpad (`test` session,
/// mirroring the test helper above) and any `.attini/{other}/` dirs
/// requested by the caller are created after `git add -A` so they
/// stay untracked (matches the runtime shape where `.attini/` is
/// gitignored).
fn exec_with_git(root: &TempRoot, other_sessions: &[&str]) -> ToolExecutor {
    git_init_and_add_all(root.path());
    // Create scratchpad (SessionPaths equivalent) after git init.
    fs::create_dir_all(root.path().join(".attini/test/scratchpad")).expect("mkdir scratchpad");
    for other in other_sessions {
        fs::create_dir_all(root.path().join(format!(".attini/{other}"))).expect("mkdir other");
    }
    ToolExecutor::new(root.path(), Vec::new(), "test".to_string()).expect("executor")
}

#[test]
fn layer1_rejects_git_metadata_add_bypasses_gitignore_check() {
    // Regression for F1: `.git/hooks/new-hook` is not gitignored
    // (git manages the .git dir specially), so Layer 3's
    // check-ignore says "not ignored" and would allow Add. Layer 1
    // must catch it first.
    let root = TempRoot::new("layer1-git-add");
    let executor = exec_with_git(&root, &[]);
    fs::create_dir_all(root.path().join(".git/hooks")).expect("mkdir hooks");
    let err = executor
        .preview_patch(&inv(vec![add(".git/hooks/pre-commit", "#!/bin/sh")]))
        .expect_err("reject");
    assert!(
        matches!(err, PatchError::ExcludedPath { .. }),
        "got {err:?}"
    );
}

#[test]
fn layer1_rejects_syntactic_bypass_variants_of_git_metadata() {
    // `./.git/HEAD` and `x/../.git/HEAD` canonicalise to the same
    // workspace-relative path as `.git/HEAD`. Layer 1 matches on
    // canonical form and must reject all three.
    let root = TempRoot::new("layer1-syntactic");
    // `.git/HEAD` is created by `git init` inside `exec_with_git`,
    // so do NOT write it here: the Update's `before` is irrelevant
    // because Layer 1 rejects before any content match, and writing
    // git's HEAD corrupts the repo (spurious "not a git
    // repository").
    // `noise-dir/` is a real directory so `..` can traverse through
    // the intermediate component (a file would make canonicalise
    // fail with ENOTDIR → UpdateOnMissingFile).
    fs::create_dir_all(root.path().join("noise-dir")).expect("mkdir noise-dir");
    let executor = exec_with_git(&root, &[]);
    for variant in [".git/HEAD", "./.git/HEAD", "noise-dir/../.git/HEAD"] {
        let err = executor
            .preview_patch(&inv(vec![update(variant, "ref\n", "hijack\n")]))
            .expect_err("reject");
        assert!(
            matches!(err, PatchError::ExcludedPath { .. }),
            "variant {variant:?} did not hit Layer 1: {err:?}"
        );
    }
}

#[test]
fn layer1_rejects_other_session_runtime_state() {
    // I1: `main` session's agent must not touch `work` session's
    // LOCK / conversation.jsonl even if the target somehow becomes
    // git-tracked.
    let root = TempRoot::new("layer1-other-session");
    root.write(".attini/work/LOCK", b"{\"pid\":123}");
    let executor = exec_with_git(&root, &[]);
    let err = executor
        .preview_patch(&inv(vec![update(
            ".attini/work/LOCK",
            "{\"pid\":123}",
            "x",
        )]))
        .expect_err("reject");
    assert!(
        matches!(err, PatchError::ExcludedPath { .. }),
        "got {err:?}"
    );
}

#[test]
fn layer1_rejects_workspace_permissions() {
    let root = TempRoot::new("layer1-workspace-scoped");
    root.write(".attini/permissions.jsonl", b"{\"command_prefixes\":[]}");
    let executor = exec_with_git(&root, &[]);
    let err = executor
        .preview_patch(&inv(vec![update(
            ".attini/permissions.jsonl",
            "{\"command_prefixes\":[]}",
            "hijack",
        )]))
        .expect_err(".attini/permissions.jsonl");
    assert!(matches!(err, PatchError::ExcludedPath { .. }), "{err:?}");
}

#[test]
fn layer2_allows_scratchpad_add_with_subdir_auto_create() {
    let root = TempRoot::new("layer2-scratchpad-subdir");
    let executor = exec_with_git(&root, &[]);
    executor
        .preview_patch(&inv(vec![add(
            ".attini/test/scratchpad/plans/2026-08-05.md",
            "# plan\n",
        )]))
        .expect("scratchpad subdir Add ok");
}

#[test]
fn layer2_syntactic_bypass_does_not_leak_side_effect_outside_scratchpad() {
    // Regression for N2-I1: `.attini/test/scratchpad/../../../malicious/foo.md`
    // syntactically normalises to `malicious/foo.md`, so the Layer 2
    // pre-create MUST NOT run (no `malicious/` directory created)
    // and the Add falls through to Layer 3.
    let root = TempRoot::new("layer2-syntactic-bypass");
    let executor = exec_with_git(&root, &[]);
    // The path normalises to `malicious/foo.md` (inside the
    // workspace), so it falls through to Layer 3 and previews as a
    // normal Add. Whatever the outcome, Layer 2's pre-create must
    // not have run for the scratchpad-looking prefix.
    let _ = executor.preview_patch(&inv(vec![add(
        ".attini/test/scratchpad/../../../malicious/foo.md",
        "gotcha",
    )]));
    assert!(
        !root.path().join("malicious").exists(),
        "Layer 2 pre-create leaked outside scratchpad"
    );
}

#[test]
fn layer2_other_session_scratchpad_is_not_layer2() {
    // Layer 2 covers only the current session's scratchpad. Other
    // session's scratchpad falls through to Layer 3, which sees the
    // path as untracked and therefore requires approval (`NeedsApproval`)
    // rather than a hard rejection.
    let root = TempRoot::new("layer2-other-session-scratchpad");
    fs::create_dir_all(root.path().join(".attini/other/scratchpad"))
        .expect("mkdir other scratchpad");
    let executor = exec_with_git(&root, &["other"]);
    root.write(".attini/other/scratchpad/notes.md", b"prev\n");
    let (_, preview) = executor
        .preview_patch(&inv(vec![update(
            ".attini/other/scratchpad/notes.md",
            "prev\n",
            "hijack",
        )]))
        .expect("other session scratchpad previews but needs approval");
    // File was created after git add, so tracked set does not contain
    // it → not auto-approved, and flagged as not revertible.
    assert!(!preview.auto_approve, "must not be Layer 2");
    assert!(
        preview.not_revertible.is_some(),
        "untracked → not revertible"
    );
}

#[test]
fn layer3_update_on_untracked_file_needs_approval() {
    let root = TempRoot::new("layer3-untracked");
    let executor = exec_with_git(&root, &[]);
    // Create AFTER git init → untracked.
    root.write("untracked.md", b"before\n");
    let (_, preview) = executor
        .preview_patch(&inv(vec![update("untracked.md", "before\n", "after\n")]))
        .expect("untracked Update previews but needs approval");
    assert!(!preview.auto_approve);
    assert!(preview.not_revertible.is_some());
}

#[test]
fn layer3_update_allows_tracked_file() {
    let root = TempRoot::new("layer3-tracked");
    root.write("tracked.md", b"before\n");
    let executor = exec_with_git(&root, &[]); // git add -A picks up tracked.md
    executor
        .preview_patch(&inv(vec![update("tracked.md", "before\n", "after\n")]))
        .expect("tracked Update ok");
}

#[test]
fn layer3_add_rejects_when_parent_is_gitignored() {
    let root = TempRoot::new("layer3-ignored-parent");
    root.write(".gitignore", b"ignored/\n");
    root.write("ignored/keep", b"placeholder"); // ensure dir exists
    let executor = exec_with_git(&root, &[]);
    let err = executor
        .preview_patch(&inv(vec![add("ignored/new.md", "x")]))
        .expect_err("reject");
    assert!(
        matches!(err, PatchError::IgnoredParent { .. }),
        "got {err:?}"
    );
}

#[test]
fn layer3_in_invocation_add_then_update_is_allowed() {
    // After a successful Add, the target enters the tracked set for
    // the rest of the invocation so a follow-up Update on the same
    // path is not rejected as untracked. Verified by doing an Add
    // via apply_patch (Session::open would not be called in this
    // test but the tracked set mutation is done in apply_patch).
    let root = TempRoot::new("layer3-add-then-update");
    let executor = exec_with_git(&root, &[]);
    let add_inv = inv(vec![add("new.md", "hello\n")]);
    let (hashes, _) = executor.preview_patch(&add_inv).expect("preview add");
    executor.apply_patch(&add_inv, &hashes).expect("apply add");
    // Now Update the just-added file. Would need approval (untracked)
    // if the tracked set were only initialised at startup.
    let update_inv = inv(vec![update("new.md", "hello\n", "world\n")]);
    let (hashes, _) = executor.preview_patch(&update_inv).expect("preview update");
    executor
        .apply_patch(&update_inv, &hashes)
        .expect("apply update");
    assert_eq!(root.read("new.md"), b"world\n");
}

#[test]
fn layer4_not_in_git_repo_needs_approval_but_allows_scratchpad() {
    // No git init here — Layer 4 fallback kicks in.
    let root = TempRoot::new("layer4-not-a-repo");
    fs::create_dir_all(root.path().join(".attini/test/scratchpad")).expect("mkdir scratchpad");
    let executor =
        ToolExecutor::new(root.path(), Vec::new(), "test".to_string()).expect("executor");
    // Any non-scratchpad Update / Add requires approval outside a repo.
    root.write("outside.md", b"pre\n");
    let (_, preview) = executor
        .preview_patch(&inv(vec![update("outside.md", "pre\n", "post\n")]))
        .expect("non-scratchpad write previews but needs approval");
    assert!(!preview.auto_approve);
    assert!(preview.not_revertible.is_some());
    // Scratchpad is Layer 2 (git-independent): no git-tracking concern,
    // so it is not flagged as unrevertible.
    let (_, sp_preview) = executor
        .preview_patch(&inv(vec![add(".attini/test/scratchpad/note.md", "hi")]))
        .expect("scratchpad ok even without git");
    assert!(sp_preview.not_revertible.is_none());
}

// ---------------------------------------------------------------
// write permission rules (Layer 3 precedence override)
// ---------------------------------------------------------------

#[test]
fn write_allow_rule_permits_untracked_update() {
    // A non-tracked Update needs approval under Layer 3 unless an
    // explicit `write` `allow:true` rule covers it.
    let root = TempRoot::new("write-allow-untracked");
    let mut executor = exec_with_git(&root, &[]);
    root.write("notes/draft.md", b"before\n");
    executor.set_write_rules(vec![Rule::write(true, "notes".to_string())]);
    let (hashes, _) = executor
        .preview_patch(&inv(vec![update("notes/draft.md", "before\n", "after\n")]))
        .expect("preview under write allow");
    executor
        .apply_patch(
            &inv(vec![update("notes/draft.md", "before\n", "after\n")]),
            &hashes,
        )
        .expect("apply under write allow");
    assert_eq!(root.read("notes/draft.md"), b"after\n");
}

#[test]
fn write_deny_rule_rejects_tracked_update() {
    // A tracked Update would be allowed by Layer 3, but an explicit
    // `write` `allow:false` rule overrides it and refuses.
    let root = TempRoot::new("write-deny-tracked");
    root.write("tracked.md", b"before\n");
    let mut executor = exec_with_git(&root, &[]);
    executor.set_write_rules(vec![Rule::write(false, "tracked.md".to_string())]);
    let err = executor
        .preview_patch(&inv(vec![update("tracked.md", "before\n", "after\n")]))
        .expect_err("reject under write deny");
    assert!(
        matches!(err, PatchError::ExcludedPath { .. }),
        "got {err:?}"
    );
    assert_eq!(root.read("tracked.md"), b"before\n", "file untouched");
}

#[test]
fn write_rules_absent_falls_back_to_git_heuristic() {
    // No write rules: a non-tracked Update still needs approval (not
    // auto-approved), exactly as before the write-rule feature.
    let root = TempRoot::new("write-fallback");
    let executor = exec_with_git(&root, &[]);
    root.write("untracked.md", b"before\n");
    let (_, preview) = executor
        .preview_patch(&inv(vec![update("untracked.md", "before\n", "after\n")]))
        .expect("previews without rules but needs approval");
    assert!(!preview.auto_approve);
    assert!(preview.not_revertible.is_some());
}

/// Best-effort relative path constructor for the traversal test.
/// Not for production use — assumes both inputs live under the same
/// system temp dir so a simple `..` chain suffices.
fn pathdiff_naive(from: &Path, to: &Path) -> String {
    let from_components: Vec<_> = from.components().collect();
    let to_components: Vec<_> = to.components().collect();
    let common = from_components
        .iter()
        .zip(to_components.iter())
        .take_while(|(a, b)| a == b)
        .count();
    let mut out = String::new();
    for _ in common..from_components.len() {
        if !out.is_empty() {
            out.push('/');
        }
        out.push_str("..");
    }
    for c in &to_components[common..] {
        if !out.is_empty() {
            out.push('/');
        }
        out.push_str(&c.as_os_str().to_string_lossy());
    }
    out
}