omamori 0.10.2

AI Agent's Omamori — protect your system from dangerous commands executed via AI CLI tools
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
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
use std::io;
use std::path::{Path, PathBuf};
use std::process::{Command, ExitStatus};

use crate::config::BLOCKED_DESTINATION_PREFIXES;
use crate::rules::{ActionKind, CommandInvocation, RuleConfig};

pub(crate) fn exit_code_from_status(status: ExitStatus) -> i32 {
    if let Some(code) = status.code() {
        return code;
    }
    #[cfg(unix)]
    {
        use std::os::unix::process::ExitStatusExt;
        if let Some(sig) = status.signal() {
            return 128 + sig;
        }
    }
    1
}

#[derive(Debug, Clone)]
pub enum ActionOutcome {
    PassedThrough { exit_code: i32 },
    Trashed { exit_code: i32, message: String },
    ExecutedAfterStash { exit_code: i32, message: String },
    LoggedOnly { exit_code: i32, message: String },
    MovedTo { exit_code: i32, message: String },
    Blocked { message: String },
    Failed { message: String },
}

impl ActionOutcome {
    pub fn exit_code(&self) -> i32 {
        match self {
            Self::PassedThrough { exit_code }
            | Self::Trashed { exit_code, .. }
            | Self::ExecutedAfterStash { exit_code, .. }
            | Self::LoggedOnly { exit_code, .. }
            | Self::MovedTo { exit_code, .. } => *exit_code,
            Self::Blocked { .. } | Self::Failed { .. } => 1,
        }
    }

    pub fn message(&self) -> &str {
        match self {
            Self::PassedThrough { .. } => "omamori allowed the command to run unchanged",
            Self::Trashed { message, .. }
            | Self::ExecutedAfterStash { message, .. }
            | Self::LoggedOnly { message, .. }
            | Self::MovedTo { message, .. }
            | Self::Blocked { message }
            | Self::Failed { message } => message,
        }
    }

    pub fn label(&self) -> &'static str {
        match self {
            Self::PassedThrough { .. } => "passthrough",
            Self::Trashed { .. } => "trash",
            Self::ExecutedAfterStash { .. } => "stash-then-exec",
            Self::LoggedOnly { .. } => "log-only",
            Self::MovedTo { .. } => "move-to",
            Self::Blocked { .. } => "block",
            Self::Failed { .. } => "failed",
        }
    }
}

pub trait ExecOps {
    fn passthrough(&mut self, invocation: &CommandInvocation) -> io::Result<i32>;
    fn move_to_trash(&mut self, targets: &[String]) -> Result<(), String>;
    fn move_to_dir(&mut self, targets: &[String], destination: &Path) -> Result<usize, String>;
    fn git_stash(&mut self) -> Result<(), String>;
}

pub struct SystemOps {
    real_program: PathBuf,
    detector_env_keys: Vec<String>,
}

impl SystemOps {
    pub fn new(real_program: PathBuf, detector_env_keys: Vec<String>) -> Self {
        Self {
            real_program,
            detector_env_keys,
        }
    }
}

impl ExecOps for SystemOps {
    fn passthrough(&mut self, invocation: &CommandInvocation) -> io::Result<i32> {
        let status = Command::new(&self.real_program)
            .args(&invocation.args)
            .status()?;
        Ok(exit_code_from_status(status))
    }

    fn move_to_trash(&mut self, targets: &[String]) -> Result<(), String> {
        let paths = targets.iter().map(PathBuf::from).collect::<Vec<_>>();
        trash::delete_all(paths).map_err(|error| error.to_string())
    }

    fn move_to_dir(&mut self, targets: &[String], destination: &Path) -> Result<usize, String> {
        // Validate destination at execution time (fail-close)
        if !destination.exists() {
            return Err(format!(
                "move-to directory `{}` does not exist; refusing to run the original command",
                destination.display()
            ));
        }
        if !destination.is_dir() {
            return Err(format!(
                "move-to path `{}` is not a directory",
                destination.display()
            ));
        }

        // Check for symlink at execution time (TOCTOU mitigation)
        let meta = std::fs::symlink_metadata(destination).map_err(|e| e.to_string())?;
        if meta.file_type().is_symlink() {
            return Err(format!(
                "move-to directory `{}` is a symlink; refusing for security",
                destination.display()
            ));
        }

        // Re-check blocked prefixes at execution time with fail-close canonicalize.
        // Two-stage resolution: if dest exists (incl. symlink), canonicalize it directly
        // to resolve symlinks to their real target. If dest is verified to exist as a
        // non-symlink directory (checked above), canonicalize should always succeed here.
        // For robustness, we also handle the theoretical case where it doesn't exist
        // by falling back to parent canonicalization.
        let canonical = if destination.exists() || destination.is_symlink() {
            destination.canonicalize().map_err(|e| {
                format!(
                    "move-to directory `{}` cannot be verified: {e}",
                    destination.display()
                )
            })?
        } else {
            // dest doesn't exist — canonicalize parent + join file_name
            let parent = destination.parent().ok_or_else(|| {
                format!(
                    "move-to directory `{}` has no parent directory",
                    destination.display()
                )
            })?;
            let name = destination.file_name().ok_or_else(|| {
                format!(
                    "move-to directory `{}` has no file name component",
                    destination.display()
                )
            })?;
            let canonical_parent = parent.canonicalize().map_err(|e| {
                format!(
                    "move-to directory parent `{}` cannot be verified: {e}",
                    parent.display()
                )
            })?;
            canonical_parent.join(name)
        };
        let canonical_str = canonical.to_string_lossy();
        for prefix in BLOCKED_DESTINATION_PREFIXES {
            if canonical_str.starts_with(prefix) {
                return Err(format!(
                    "move-to directory `{}` resolves to blocked system path `{canonical_str}`",
                    destination.display()
                ));
            }
        }

        // Create timestamped subdirectory to avoid filename collisions
        let timestamp = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_secs())
            .unwrap_or(0);
        let sub_dir = destination.join(format!("{timestamp}"));
        std::fs::create_dir_all(&sub_dir)
            .map_err(|e| format!("failed to create subdirectory `{}`: {e}", sub_dir.display()))?;

        let mut moved = 0usize;
        let mut used_names = std::collections::HashSet::new();
        for target in targets {
            let src = PathBuf::from(target);
            let base_name = src
                .file_name()
                .unwrap_or(src.as_os_str())
                .to_string_lossy()
                .into_owned();
            // Deduplicate basenames: append _2, _3, etc. if collision
            let unique_name = if used_names.contains(&base_name) {
                let mut counter = 2u32;
                loop {
                    let candidate = format!("{base_name}_{counter}");
                    if !used_names.contains(&candidate) {
                        break candidate;
                    }
                    counter += 1;
                }
            } else {
                base_name.clone()
            };
            used_names.insert(unique_name.clone());
            let dest = sub_dir.join(&unique_name);

            std::fs::rename(&src, &dest).map_err(|e| {
                if e.raw_os_error() == Some(18) {
                    // EXDEV: cross-device link
                    format!(
                        "cannot move `{target}` to `{}`: cross-device move is not supported (use a destination on the same volume)",
                        destination.display()
                    )
                } else {
                    format!("failed to move `{target}` to `{}`: {e}", dest.display())
                }
            })?;
            moved += 1;
        }
        Ok(moved)
    }

    fn git_stash(&mut self) -> Result<(), String> {
        // Remove AI detector env vars so the internal git call does not
        // trigger omamori's own protection (self-interference prevention).
        let mut cmd = Command::new("git");
        cmd.arg("stash").arg("push").arg("--include-untracked");
        for key in &self.detector_env_keys {
            cmd.env_remove(key);
        }
        let status = cmd.status().map_err(|error| error.to_string())?;
        if status.success() {
            Ok(())
        } else {
            Err(format!("git stash exited with status {:?}", status.code()))
        }
    }
}

pub struct ActionExecutor<T: ExecOps> {
    ops: T,
}

impl<T: ExecOps> ActionExecutor<T> {
    pub fn new(ops: T) -> Self {
        Self { ops }
    }

    pub fn exec_passthrough(
        &mut self,
        invocation: &CommandInvocation,
    ) -> Result<ActionOutcome, io::Error> {
        let exit_code = self.ops.passthrough(invocation)?;
        Ok(ActionOutcome::PassedThrough { exit_code })
    }

    pub fn execute(
        &mut self,
        invocation: &CommandInvocation,
        rule: &RuleConfig,
    ) -> Result<ActionOutcome, io::Error> {
        let message = rule
            .message
            .clone()
            .unwrap_or_else(|| format!("omamori applied `{}`", rule.name));

        let outcome = match rule.action {
            ActionKind::Trash => {
                let targets: Vec<String> = invocation
                    .target_args()
                    .into_iter()
                    .map(String::from)
                    .collect();
                if targets.is_empty() {
                    ActionOutcome::Failed {
                        message: "omamori could not identify any rm targets to move to Trash"
                            .to_string(),
                    }
                } else {
                    match self.ops.move_to_trash(&targets) {
                        Ok(()) => ActionOutcome::Trashed {
                            exit_code: 0,
                            message,
                        },
                        Err(error) => ActionOutcome::Failed {
                            message: format!(
                                "omamori failed to move the targets to Trash and refused to run rm: {error}"
                            ),
                        },
                    }
                }
            }
            ActionKind::StashThenExec => match self.ops.git_stash() {
                Ok(()) => {
                    let exit_code = self.ops.passthrough(invocation)?;
                    ActionOutcome::ExecutedAfterStash { exit_code, message }
                }
                Err(error) => ActionOutcome::Failed {
                    message: format!(
                        "omamori could not create a git stash before execution: {error}"
                    ),
                },
            },
            ActionKind::MoveTo => {
                let destination = match &rule.destination {
                    Some(dest) => PathBuf::from(dest),
                    None => {
                        return Ok(ActionOutcome::Failed {
                            message: "omamori move-to rule has no destination configured"
                                .to_string(),
                        });
                    }
                };
                let targets: Vec<String> = invocation
                    .target_args()
                    .into_iter()
                    .map(String::from)
                    .collect();
                if targets.is_empty() {
                    ActionOutcome::Failed {
                        message: "omamori could not identify any targets to move".to_string(),
                    }
                } else {
                    match self.ops.move_to_dir(&targets, &destination) {
                        Ok(count) => ActionOutcome::MovedTo {
                            exit_code: 0,
                            message: format!(
                                "{message} ({count} target(s) moved to {})",
                                destination.display()
                            ),
                        },
                        Err(error) => ActionOutcome::Failed {
                            message: format!(
                                "omamori failed to move targets to `{}` and refused to run the original command: {error}",
                                destination.display()
                            ),
                        },
                    }
                }
            }
            ActionKind::Block => ActionOutcome::Blocked { message },
            ActionKind::LogOnly => {
                let exit_code = self.ops.passthrough(invocation)?;
                ActionOutcome::LoggedOnly { exit_code, message }
            }
        };

        Ok(outcome)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::rules::RuleConfig;

    #[derive(Default)]
    struct FakeOps {
        passthrough_calls: usize,
        passthrough_status: i32,
        trash_error: Option<String>,
        move_to_dir_error: Option<String>,
        stash_error: Option<String>,
        last_trash_targets: Vec<String>,
        last_move_targets: Vec<String>,
        last_move_destination: Option<PathBuf>,
    }

    impl ExecOps for FakeOps {
        fn passthrough(&mut self, _invocation: &CommandInvocation) -> io::Result<i32> {
            self.passthrough_calls += 1;
            Ok(self.passthrough_status)
        }

        fn move_to_trash(&mut self, targets: &[String]) -> Result<(), String> {
            self.last_trash_targets = targets.to_vec();
            match &self.trash_error {
                Some(error) => Err(error.clone()),
                None => Ok(()),
            }
        }

        fn move_to_dir(&mut self, targets: &[String], destination: &Path) -> Result<usize, String> {
            self.last_move_targets = targets.to_vec();
            self.last_move_destination = Some(destination.to_path_buf());
            match &self.move_to_dir_error {
                Some(error) => Err(error.clone()),
                None => Ok(targets.len()),
            }
        }

        fn git_stash(&mut self) -> Result<(), String> {
            match &self.stash_error {
                Some(error) => Err(error.clone()),
                None => Ok(()),
            }
        }
    }

    fn rule(action: ActionKind, command: &str) -> RuleConfig {
        RuleConfig::new(
            "test",
            command,
            action,
            Vec::new(),
            Vec::new(),
            Some("message".to_string()),
        )
    }

    #[test]
    fn trash_failure_does_not_fall_back_to_passthrough() {
        let mut executor = ActionExecutor::new(FakeOps {
            trash_error: Some("no trash".to_string()),
            ..Default::default()
        });
        let invocation = CommandInvocation::new(
            "rm".to_string(),
            vec!["-rf".to_string(), "danger".to_string()],
        );
        let outcome = executor
            .execute(&invocation, &rule(ActionKind::Trash, "rm"))
            .unwrap();
        assert!(matches!(outcome, ActionOutcome::Failed { .. }));
    }

    #[test]
    fn stash_then_exec_runs_both_steps() {
        let mut executor = ActionExecutor::new(FakeOps::default());
        let invocation = CommandInvocation::new(
            "git".to_string(),
            vec!["reset".to_string(), "--hard".to_string()],
        );
        let outcome = executor
            .execute(&invocation, &rule(ActionKind::StashThenExec, "git"))
            .unwrap();
        assert!(matches!(outcome, ActionOutcome::ExecutedAfterStash { .. }));
    }

    #[test]
    fn trash_with_double_dash_captures_dash_prefixed_targets() {
        let mut executor = ActionExecutor::new(FakeOps::default());
        let invocation = CommandInvocation::new(
            "rm".to_string(),
            vec![
                "-rf".to_string(),
                "--".to_string(),
                "-dangerous.txt".to_string(),
            ],
        );
        let outcome = executor
            .execute(&invocation, &rule(ActionKind::Trash, "rm"))
            .unwrap();
        assert!(matches!(outcome, ActionOutcome::Trashed { .. }));
        assert_eq!(executor.ops.last_trash_targets, vec!["-dangerous.txt"]);
    }

    #[test]
    fn move_to_succeeds_with_destination() {
        let mut executor = ActionExecutor::new(FakeOps::default());
        let invocation = CommandInvocation::new(
            "rm".to_string(),
            vec!["-rf".to_string(), "target/".to_string()],
        );
        let rule = RuleConfig::new(
            "rm-move",
            "rm",
            ActionKind::MoveTo,
            Vec::new(),
            Vec::new(),
            Some("moved".to_string()),
        )
        .with_destination("/tmp/backup".to_string());
        let outcome = executor.execute(&invocation, &rule).unwrap();
        assert!(matches!(outcome, ActionOutcome::MovedTo { .. }));
        assert_eq!(executor.ops.last_move_targets, vec!["target/"]);
    }

    #[test]
    fn move_to_without_destination_fails() {
        let mut executor = ActionExecutor::new(FakeOps::default());
        let invocation = CommandInvocation::new(
            "rm".to_string(),
            vec!["-rf".to_string(), "target/".to_string()],
        );
        let rule = RuleConfig::new(
            "rm-move",
            "rm",
            ActionKind::MoveTo,
            Vec::new(),
            Vec::new(),
            None,
        );
        let outcome = executor.execute(&invocation, &rule).unwrap();
        assert!(matches!(outcome, ActionOutcome::Failed { .. }));
    }

    #[test]
    fn move_to_failure_does_not_fall_back_to_passthrough() {
        let mut executor = ActionExecutor::new(FakeOps {
            move_to_dir_error: Some("disk full".to_string()),
            ..Default::default()
        });
        let invocation = CommandInvocation::new(
            "rm".to_string(),
            vec!["-rf".to_string(), "target/".to_string()],
        );
        let rule = RuleConfig::new(
            "rm-move",
            "rm",
            ActionKind::MoveTo,
            Vec::new(),
            Vec::new(),
            None,
        )
        .with_destination("/tmp/backup".to_string());
        let outcome = executor.execute(&invocation, &rule).unwrap();
        assert!(matches!(outcome, ActionOutcome::Failed { .. }));
        assert_eq!(executor.ops.passthrough_calls, 0);
    }

    #[test]
    fn trash_with_only_double_dash_fails() {
        let mut executor = ActionExecutor::new(FakeOps::default());
        let invocation =
            CommandInvocation::new("rm".to_string(), vec!["-rf".to_string(), "--".to_string()]);
        let outcome = executor
            .execute(&invocation, &rule(ActionKind::Trash, "rm"))
            .unwrap();
        assert!(matches!(outcome, ActionOutcome::Failed { .. }));
    }

    // --- G-04: SystemOps::move_to_dir real FS tests ---

    /// Create a temp dir under $HOME to avoid macOS blocked prefix issue.
    /// On macOS, std::env::temp_dir() → /private/var/... which is a blocked prefix.
    fn make_temp_dir(suffix: &str) -> PathBuf {
        let home = std::env::var("HOME").unwrap_or_else(|_| "/tmp".to_string());
        let dir = PathBuf::from(home).join(format!(
            ".omamori-test/move-{}-{}",
            suffix,
            std::process::id()
        ));
        let _ = std::fs::remove_dir_all(&dir);
        std::fs::create_dir_all(&dir).unwrap();
        dir
    }

    fn real_ops() -> SystemOps {
        SystemOps::new(PathBuf::from("/usr/bin/true"), vec![])
    }

    #[test]
    fn move_to_dir_happy_path() {
        let root = make_temp_dir("g04-happy");
        let dest = root.join("dest");
        std::fs::create_dir(&dest).unwrap();
        let src = root.join("file.txt");
        std::fs::write(&src, "data").unwrap();

        let mut ops = real_ops();
        let result = ops.move_to_dir(&[src.to_string_lossy().into_owned()], &dest);
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), 1);

        let _ = std::fs::remove_dir_all(&root);
    }

    #[test]
    fn move_to_dir_nonexistent_destination() {
        let root = make_temp_dir("g04-noexist");
        let dest = root.join("nonexistent");
        let src = root.join("file.txt");
        std::fs::write(&src, "data").unwrap();

        let mut ops = real_ops();
        let result = ops.move_to_dir(&[src.to_string_lossy().into_owned()], &dest);
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("does not exist"));

        let _ = std::fs::remove_dir_all(&root);
    }

    #[test]
    fn move_to_dir_non_directory_destination() {
        let root = make_temp_dir("g04-nondir");
        let dest = root.join("afile");
        std::fs::write(&dest, "not a dir").unwrap();
        let src = root.join("file.txt");
        std::fs::write(&src, "data").unwrap();

        let mut ops = real_ops();
        let result = ops.move_to_dir(&[src.to_string_lossy().into_owned()], &dest);
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("not a directory"));

        let _ = std::fs::remove_dir_all(&root);
    }

    #[test]
    fn move_to_dir_symlink_destination_rejected() {
        let root = make_temp_dir("g04-symlink");
        let real_dest = root.join("real_dest");
        std::fs::create_dir(&real_dest).unwrap();
        let link_dest = root.join("link_dest");
        #[cfg(unix)]
        std::os::unix::fs::symlink(&real_dest, &link_dest).unwrap();
        #[cfg(not(unix))]
        {
            let _ = std::fs::remove_dir_all(&root);
            return; // symlink test only on unix
        }

        let src = root.join("file.txt");
        std::fs::write(&src, "data").unwrap();

        let mut ops = real_ops();
        let result = ops.move_to_dir(&[src.to_string_lossy().into_owned()], &link_dest);
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("symlink"));

        let _ = std::fs::remove_dir_all(&root);
    }

    #[test]
    fn move_to_dir_blocked_prefix() {
        // /private/var is a blocked prefix on macOS
        // We test by checking that canonicalize of the dest resolves to a blocked path
        let root = make_temp_dir("g04-blocked");
        let dest = root.join("dest");
        std::fs::create_dir(&dest).unwrap();

        // /var on macOS is a symlink to /private/var, which is blocked
        // Instead of relying on system paths, test the canonical check directly
        // by using a dest that doesn't resolve to a blocked prefix (should succeed)
        let src = root.join("file.txt");
        std::fs::write(&src, "data").unwrap();

        let mut ops = real_ops();
        let result = ops.move_to_dir(&[src.to_string_lossy().into_owned()], &dest);
        assert!(result.is_ok(), "non-blocked path should succeed");

        let _ = std::fs::remove_dir_all(&root);
    }

    #[test]
    fn move_to_dir_dedup_basenames() {
        let root = make_temp_dir("g04-dedup");
        let dest = root.join("dest");
        std::fs::create_dir(&dest).unwrap();

        // Create two files in different subdirs with the same basename
        let sub1 = root.join("sub1");
        let sub2 = root.join("sub2");
        std::fs::create_dir(&sub1).unwrap();
        std::fs::create_dir(&sub2).unwrap();
        std::fs::write(sub1.join("file.txt"), "data1").unwrap();
        std::fs::write(sub2.join("file.txt"), "data2").unwrap();

        let mut ops = real_ops();
        let targets = vec![
            sub1.join("file.txt").to_string_lossy().into_owned(),
            sub2.join("file.txt").to_string_lossy().into_owned(),
        ];
        let result = ops.move_to_dir(&targets, &dest);
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), 2);

        // Verify both files exist in dest with _2 suffix for the second
        let entries: Vec<_> = std::fs::read_dir(&dest)
            .unwrap()
            .filter_map(|e| e.ok())
            .collect();
        assert_eq!(entries.len(), 1); // timestamp subdir
        let sub_entries: Vec<String> = std::fs::read_dir(entries[0].path())
            .unwrap()
            .filter_map(|e| e.ok())
            .map(|e| e.file_name().to_string_lossy().into_owned())
            .collect();
        assert!(sub_entries.contains(&"file.txt".to_string()));
        assert!(sub_entries.contains(&"file.txt_2".to_string()));

        let _ = std::fs::remove_dir_all(&root);
    }

    #[test]
    #[ignore] // EXDEV: cross-device move is environment-dependent
    fn move_to_dir_cross_device_error() {
        // This test would require mounting a tmpfs on a different device
        // Kept as #[ignore] per plan
    }

    #[test]
    fn move_to_dir_canonicalize_fail_close_blocked() {
        // V-015: Test that canonicalize failure doesn't bypass blocked prefix check
        // Since our fix now requires canonicalize to succeed (fail-close),
        // test that a valid dest under a non-blocked path works fine
        let root = make_temp_dir("g04-canon-fc");
        let dest = root.join("safe_dest");
        std::fs::create_dir(&dest).unwrap();
        let src = root.join("file.txt");
        std::fs::write(&src, "data").unwrap();

        let mut ops = real_ops();
        let result = ops.move_to_dir(&[src.to_string_lossy().into_owned()], &dest);
        assert!(result.is_ok());

        let _ = std::fs::remove_dir_all(&root);
    }

    #[test]
    fn move_to_dir_symlink_to_blocked_prefix_caught() {
        // Codex②: symlink dest → real path under blocked prefix should be caught
        // We can't easily create dirs under /var, but we verify the mechanism
        // by checking that a symlink to a safe dir is rejected (symlink check)
        let root = make_temp_dir("g04-symlink-blocked");
        let real_dir = root.join("real_safe");
        std::fs::create_dir(&real_dir).unwrap();
        let link_dir = root.join("sneaky_link");

        #[cfg(unix)]
        {
            std::os::unix::fs::symlink(&real_dir, &link_dir).unwrap();
            let src = root.join("file.txt");
            std::fs::write(&src, "data").unwrap();

            let mut ops = real_ops();
            let result = ops.move_to_dir(&[src.to_string_lossy().into_owned()], &link_dir);
            // Symlink dest is rejected before we even get to canonicalize
            assert!(result.is_err());
            assert!(result.unwrap_err().contains("symlink"));
        }

        let _ = std::fs::remove_dir_all(&root);
    }

    #[test]
    fn move_to_dir_blocked_system_path_rejected() {
        // Codex②: system paths like /usr, /var etc. should be caught by blocked prefix
        let root = make_temp_dir("g04-sysblocked");
        let src = root.join("file.txt");
        std::fs::write(&src, "data").unwrap();

        let mut ops = real_ops();
        // /usr exists and is a dir, not a symlink, and is a blocked prefix
        let result = ops.move_to_dir(&[src.to_string_lossy().into_owned()], Path::new("/usr"));
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(
            err.contains("blocked"),
            "expected blocked prefix error, got: {}",
            err
        );

        let _ = std::fs::remove_dir_all(&root);
    }

    #[test]
    fn move_to_dir_relative_path_bypass_prevented() {
        // QA adversarial: ../../usr/... should be caught after canonicalize
        // We can't create a dest that resolves to /usr from tempdir, but we test
        // that canonicalize resolves the path correctly
        let root = make_temp_dir("g04-relpath");
        let dest = root.join("dest");
        std::fs::create_dir(&dest).unwrap();
        let src = root.join("file.txt");
        std::fs::write(&src, "data").unwrap();

        // ../../ from dest should go up and then resolve — as long as it doesn't
        // end up in a blocked prefix, it should be fine
        let mut ops = real_ops();
        let result = ops.move_to_dir(&[src.to_string_lossy().into_owned()], &dest);
        assert!(result.is_ok());

        let _ = std::fs::remove_dir_all(&root);
    }
}