agix-platform 0.4.4

Agix — this machine's execution providers, crash hook, and process identity
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
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
use super::*;

/// A command reads none of the credential stores in the user's home, nor
/// what in the state root holds keys — the settings files, whose
/// `api_key` and MCP headers are plain text, and the MCP OAuth tokens, of
/// the root and of every scene. Everything else in the state root, which
/// skills, memory and the session's own files live in, stays readable.
#[cfg(any(target_os = "macos", target_os = "linux"))]
#[test]
#[allow(clippy::unwrap_used)]
fn a_sandboxed_command_reads_no_credentials_and_the_rest_of_the_state_root() {
    if !a_backend_runs() {
        return;
    }
    let dir = tempfile::tempdir().unwrap();
    let state = dir.path().join("state");
    let project = dir.path().join("project");
    // In the project, which the command sees on every backend: bwrap
    // gives a command a `/tmp` of its own, and the temporary directory
    // may well be there.
    let home = project.join("home");
    std::fs::create_dir_all(project.join(".agix")).unwrap();
    std::fs::create_dir_all(&state).unwrap();
    let secret = "agix-test-secret";
    let secrets = [
        home.join(".git-credentials"),
        home.join(".config/git/credentials"),
        home.join(".config/gcloud/application_default_credentials.json"),
        home.join(".cargo/credentials"),
        home.join(".cargo/credentials.toml"),
        home.join(".vault-token"),
        home.join(".docker/config.json"),
        home.join(".kube/config"),
        home.join(".npmrc"),
        home.join(".pypirc"),
        home.join(".netrc"),
        state.join("settings.json"),
        state.join("settings.local.json"),
        state.join(".settings.local.json.1234.tmp"),
        state.join("scenes/code/settings.json"),
        state.join("scenes/code/settings.local.json"),
        state.join("scenes/code/mcp/oauth/server.json"),
        state.join("scenes/triage/settings.local.json"),
        state.join("mcp/oauth/server.json"),
        state.join("daemon/auth/desktop.token"),
    ];
    for path in &secrets {
        write_file(path, secret);
    }
    let readable = [
        home.join(".gitconfig"),
        home.join(".cargo/config.toml"),
        state.join("skills/s/SKILL.md"),
        state.join("scenes/code/skills/t/run.sh"),
        state.join("scenes/code/scene.json"),
        state.join("scenes/code/scheduled_tasks.json"),
        state.join("memory/MEMORY.md"),
        state.join("scratchpad/notes.md"),
        state.join("sessions/s.jsonl"),
        state.join("daemon/instances.d/desktop.json"),
    ];
    for path in &readable {
        write_file(path, "fine");
    }
    let policy = SandboxPolicy {
        deny_read: default_deny_read_in(&home),
        state_root: Some(state.clone()),
        ..Default::default()
    };
    let cat = |command: &str| {
        let out = run_confined(&project, &policy, &[("HOME", &home)], command);
        (
            out.status.success(),
            String::from_utf8_lossy(&out.stdout).into_owned(),
        )
    };

    for command in [
        "cat ~/.git-credentials".to_string(),
        format!("cat '{}/settings.local.json'", state.display()),
    ] {
        let (ok, shown) = cat(&command);
        assert!(!ok && !shown.contains(secret), "`{command}` read {shown:?}");
    }
    for path in &secrets {
        let (ok, shown) = cat(&format!("cat '{}'", path.display()));
        assert!(
            !ok && !shown.contains(secret),
            "{} was read: {shown:?}",
            path.display()
        );
    }
    for path in &readable {
        let (ok, shown) = cat(&format!("cat '{}'", path.display()));
        assert!(ok && shown == "fine", "{} was not read", path.display());
    }
    let (ok, listed) = cat(&format!("ls '{}'", state.join("scenes/code").display()));
    assert!(ok && listed.contains("skills"), "{listed}");
}

/// 场景目录是指向别处的符号链接时(把场景放在 dotfiles 仓库里再链进来),
/// 它的 `settings.json` 仍是这个场景的凭据,沙箱里的命令照样读不到。macOS
/// 的读拒绝匹配的是解析后的路径,链接目标不在 `<状态目录>/scenes/` 下。
#[cfg(any(target_os = "macos", target_os = "linux"))]
#[test]
#[allow(clippy::unwrap_used)]
fn a_scene_linked_in_from_elsewhere_keeps_its_settings_unreadable() {
    if !a_backend_runs() {
        return;
    }
    let dir = tempfile::tempdir().unwrap();
    let state = dir.path().join("state");
    let project = dir.path().join("project");
    let elsewhere = dir.path().join("dotfiles/scene");
    std::fs::create_dir_all(state.join("scenes")).unwrap();
    std::fs::create_dir_all(&project).unwrap();
    let secret = "agix-audit3-scene-secret";
    write_file(&elsewhere.join("settings.json"), secret);
    write_file(&state.join("scenes/code/settings.json"), secret);
    std::os::unix::fs::symlink(&elsewhere, state.join("scenes/linked")).unwrap();
    let policy = SandboxPolicy {
        state_root: Some(state.clone()),
        ..Default::default()
    };
    for path in [
        state.join("scenes/code/settings.json"),
        state.join("scenes/linked/settings.json"),
    ] {
        let out = run_confined(&project, &policy, &[], &format!("cat '{}'", path.display()));
        let shown = String::from_utf8_lossy(&out.stdout);
        assert!(
            !out.status.success() && !shown.contains(secret),
            "{} was read: {shown:?}",
            path.display()
        );
    }
}

/// The project's own `.agix/settings.local.json` and `.agix/mcp.local.json`
/// are where the engine writes the project's keys; a command reads neither,
/// even with the whole project in `allow_read`. The committed
/// `settings.json` beside them stays readable.
#[cfg(any(target_os = "macos", target_os = "linux"))]
#[test]
#[allow(clippy::unwrap_used)]
fn a_sandboxed_command_reads_none_of_the_projects_keys() {
    if !a_backend_runs() {
        return;
    }
    for (own, keys_in) in [
        (".agix", ".agix"),
        (".codix", ".codix"),
        (".codix", ".agix"),
    ] {
        reads_none_of_the_keys_in(&base::paths::Layout::new(own).unwrap(), keys_in);
    }
}

/// Under `layout`, the keys in the project's `keys_in`: its own directory,
/// or a foreign `.agix`.
#[cfg(any(target_os = "macos", target_os = "linux"))]
#[allow(clippy::unwrap_used)]
fn reads_none_of_the_keys_in(layout: &base::paths::Layout, keys_in: &str) {
    let dir = tempfile::tempdir().unwrap();
    let project = dir.path().join("project");
    let agix = project.join(keys_in);
    let secret = "agix-test-secret";
    let secrets = [
        agix.join("settings.local.json"),
        agix.join("mcp.local.json"),
        agix.join(".settings.local.json.1234.tmp"),
    ];
    for path in &secrets {
        write_file(path, secret);
    }
    write_file(&agix.join("settings.json"), "fine");
    let policy = SandboxPolicy {
        deny_read: vec![dir.path().join("nowhere")],
        allow_read: vec![project.clone()],
        state_root: Some(dir.path().join("state")),
        layout: layout.clone(),
        ..Default::default()
    };
    let cat = |path: &Path| {
        let out = run_confined(&project, &policy, &[], &format!("cat '{}'", path.display()));
        (
            out.status.success(),
            String::from_utf8_lossy(&out.stdout).into_owned(),
        )
    };
    for path in &secrets {
        let (ok, shown) = cat(path);
        assert!(
            !ok && !shown.contains(secret),
            "{}: {} was read: {shown:?}",
            layout.project_dir_name(),
            path.display()
        );
    }
    assert_eq!(cat(&agix.join("settings.json")), (true, "fine".to_string()));
}

/// What a process a confined command left running in `project` reads of
/// `paths` after each of them is written — the engine's way, a
/// temporary file renamed over it — once it is running.
#[cfg(any(target_os = "macos", target_os = "linux"))]
#[allow(clippy::unwrap_used)]
fn read_after_the_fact(project: &Path, policy: &SandboxPolicy, paths: &[PathBuf]) -> String {
    let listed: Vec<String> = paths.iter().map(|p| format!("'{}'", p.display())).collect();
    let command = format!(
        "( until [ -e go ]; do sleep 0.02; done; \
              for f in {}; do cat \"$f\"; echo; done > out 2>/dev/null; \
              touch done ) & exit 0",
        listed.join(" ")
    );
    let confined = PlatformSandbox.confine(
        ProcessSpec::new("bash", project).args(["-c", &command].map(String::from)),
        policy,
    );
    assert_eq!(
        confined.enforcement,
        Enforcement::Full,
        "{:?}",
        confined.unmet
    );
    let mut child = std::process::Command::new(&confined.spec.program)
        .args(&confined.spec.args)
        .current_dir(project)
        .stdin(std::process::Stdio::null())
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::null())
        .spawn()
        .unwrap();
    let _ = child.wait();
    for path in paths {
        std::fs::create_dir_all(path.parent().unwrap()).unwrap();
        let tmp = path.with_file_name(".agix-rename.tmp");
        std::fs::write(&tmp, "agix-after-the-fact-secret").unwrap();
        std::fs::rename(&tmp, path).unwrap();
    }
    std::fs::write(project.join("go"), "").unwrap();
    let done = (0..500).any(|_| {
        if project.join("done").exists() {
            return true;
        }
        std::thread::sleep(std::time::Duration::from_millis(20));
        false
    });
    assert!(done, "the background reader never finished");
    std::fs::read_to_string(project.join("out")).unwrap_or_default()
}

/// The layouts that keep the state root from being masked as a whole —
/// the project is the state root, or a scene directory in it — and the
/// project's own `.agix`: a credential the engine writes there after a
/// command started, or rename-writes over an old one, does not reach
/// what that command left running. On Linux the key files directly in
/// a project that is the state root or a scene directory are the known
/// limit (`mask`'s header) and not asserted; macOS denies them by
/// pattern.
#[cfg(any(target_os = "macos", target_os = "linux"))]
#[test]
#[allow(clippy::unwrap_used)]
fn a_credential_written_later_stays_unreadable_where_the_project_holds_credentials() {
    if !a_backend_runs() {
        return;
    }
    let pattern_held = cfg!(target_os = "macos");
    let dir = tempfile::tempdir().unwrap();
    let base = dir.path().canonicalize().unwrap();

    let state = base.join("state-is-project");
    write_file(&state.join("scenes/code/settings.local.json"), "{}");
    write_file(&state.join("scenes/code/scene.json"), "fine");
    write_file(&state.join("mcp/servers.json"), "fine");
    write_file(&state.join("settings.local.json"), "{}");
    let policy = SandboxPolicy {
        state_root: Some(state.clone()),
        ..Default::default()
    };
    let mut later = vec![
        state.join("scenes/code/settings.local.json"),
        state.join("scenes/new/settings.json"),
        state.join("mcp/oauth/server.json"),
        state.join(".agix/settings.local.json"),
    ];
    if pattern_held {
        later.push(state.join("settings.local.json"));
    }
    let saw = read_after_the_fact(&state, &policy, &later);
    assert!(
        !saw.contains("after-the-fact"),
        "project = state root: {saw:?}"
    );

    let state = base.join("state");
    let scene = state.join("scenes/code");
    write_file(&state.join("settings.local.json"), "{}");
    write_file(&state.join("scenes/triage/settings.json"), "{}");
    write_file(&scene.join("settings.local.json"), "{}");
    write_file(&scene.join("scene.json"), "{}");
    let policy = SandboxPolicy {
        state_root: Some(state.clone()),
        ..Default::default()
    };
    let mut later = vec![
        state.join("settings.local.json"),
        state.join("settings.json"),
        state.join("scenes/triage/settings.json"),
        state.join("scenes/new/settings.json"),
        scene.join(".agix/mcp.local.json"),
    ];
    if pattern_held {
        later.push(scene.join("settings.local.json"));
    }
    let saw = read_after_the_fact(&scene, &policy, &later);
    assert!(!saw.contains("after-the-fact"), "project = scene: {saw:?}");
    assert!(
        run_confined(
            &scene,
            &policy,
            &[],
            "echo x > notes.md && ! echo x > scene.json"
        )
        .status
        .success(),
        "a scene opened as a project keeps its scene files read-only, the rest writable"
    );

    for own in [".agix", ".codix"] {
        let project = base.join("project").join(own);
        let layout = base::paths::Layout::new(own).unwrap();
        let in_own = |p: &str| layout.project_dir(&project).join(p);
        write_file(&in_own("settings.json"), "fine");
        write_file(&in_own("settings.local.json"), "{}");
        std::fs::create_dir_all(in_own("state/scratchpad")).unwrap();
        write_file(&project.join(".agix/settings.local.json"), "{}");
        let policy = SandboxPolicy {
            state_root: Some(base.join("elsewhere")),
            layout: layout.clone(),
            ..Default::default()
        };
        let later = [
            in_own("settings.local.json"),
            in_own("mcp.local.json"),
            project.join(".agix/settings.local.json"),
        ];
        let saw = read_after_the_fact(&project, &policy, &later);
        assert!(
            !saw.contains("after-the-fact"),
            "the project's {own}: {saw:?}"
        );
        assert!(
            run_confined(
                &project,
                &policy,
                &[],
                &format!("cat {own}/settings.json && echo x > {own}/state/scratchpad/n")
            )
            .status
            .success(),
            "the project's {own} keeps its other entries, the working ones writable"
        );
        if cfg!(target_os = "linux") {
            assert!(
                !run_confined(&project, &policy, &[], &format!("echo x > {own}/new"))
                    .status
                    .success(),
                "a new entry directly in {own} was created"
            );
        }
    }
}

/// Another instance's state root on this machine is guarded as the
/// session's own is: a command reads none of its keys and tokens, connects
/// to no socket in it, and writes nothing in it — here even with it inside
/// the writable project. The rest of it stays readable.
#[cfg(any(target_os = "macos", target_os = "linux"))]
#[test]
#[allow(clippy::unwrap_used)]
fn another_instances_state_root_is_guarded_as_the_own_one() {
    if !a_backend_runs() {
        return;
    }
    let dir = tempfile::tempdir().unwrap();
    let base = dir.path().canonicalize().unwrap();
    let project = base.join("p");
    let other = project.join("h/.agix");
    let secret = "agix-other-instance-secret";
    let secrets = [
        other.join("settings.json"),
        other.join("settings.local.json"),
        other.join("scenes/code/settings.json"),
        other.join("mcp/oauth/server.json"),
        other.join("daemon/auth/desktop.token"),
    ];
    for path in &secrets {
        write_file(path, secret);
    }
    write_file(&other.join("skills/s/SKILL.md"), "fine");
    std::fs::create_dir_all(other.join("memory")).unwrap();
    let socket = other.join("a.sock");
    let listener = std::os::unix::net::UnixListener::bind(&socket).unwrap();
    std::thread::spawn(move || {
        for conn in listener.incoming() {
            drop(conn);
        }
    });
    let policy = SandboxPolicy {
        state_root: Some(base.join("state")),
        foreign_state_roots: vec![other.clone()],
        ..Default::default()
    };
    let run = |command: &str| {
        let out = run_confined(&project, &policy, &[], command);
        (
            out.status.success(),
            String::from_utf8_lossy(&out.stdout).into_owned(),
        )
    };
    for path in &secrets {
        let (ok, shown) = run(&format!("cat '{}'", path.display()));
        assert!(
            !ok && !shown.contains(secret),
            "{} was read: {shown:?}",
            path.display()
        );
    }
    assert_eq!(
        run(&format!(
            "cat '{}'",
            other.join("skills/s/SKILL.md").display()
        )),
        (true, "fine".to_string())
    );
    for write in ["memory/m.md", "new.json", "skills/s/SKILL.md"] {
        let path = other.join(write);
        let (ok, _) = run(&format!("echo x > '{}'", path.display()));
        assert!(!ok, "{} was written", path.display());
    }
    let (_, connected) = run(&format!(
        "python3 -c 'import socket,sys; s=socket.socket(socket.AF_UNIX); s.connect(sys.argv[1]); print(\"in\")' '{}'",
        socket.display()
    ));
    assert!(!connected.contains("in"), "the socket was reached");
    assert!(run("echo x > p.txt").0, "the project stays writable");
}

/// A foreign configuration root is shut to a command's writes as a whole,
/// as the gate takes every entry of one for configuration — its working
/// entries and new entries directly in it too — but for a writable root the
/// policy names inside it. In the instance's own root the working entries
/// of its `state/` stay writable — `state/scratchpad/` and `state/memory/`,
/// all the gate leaves out of configuration — and nothing else in it: not
/// `commands/`, not `state/teams/`, `state/telemetry/` or
/// `state/tool-results/`, which the engine writes itself, not the
/// embedder's `state/<product id>/`, not a `scratchpad/` directly in the
/// root, nor a `worktrees/`, which the engine's worktrees are not in.
#[cfg(any(target_os = "macos", target_os = "linux"))]
#[test]
#[allow(clippy::unwrap_used)]
fn a_foreign_configuration_root_is_shut_to_writes_as_a_whole() {
    if !a_backend_runs() {
        return;
    }
    for own in [".agix", ".codix"] {
        let dir = tempfile::tempdir().unwrap();
        let project = dir.path().canonicalize().unwrap().join("project");
        let layout = base::paths::Layout::new(own).unwrap();
        for d in [
            ".agix/memory",
            ".agix/scratchpad",
            ".claude/commands",
            ".claude/memory",
        ] {
            std::fs::create_dir_all(project.join(d)).unwrap();
        }
        let host = format!("state/{}", layout.product_id());
        for entry in [
            "scratchpad",
            "memory",
            "state/scratchpad",
            "state/teams",
            "state/telemetry",
            "state/tool-results",
            host.as_str(),
            "worktrees/w",
        ] {
            std::fs::create_dir_all(project.join(own).join(entry)).unwrap();
        }
        let named = project.join(".claude/named");
        std::fs::create_dir_all(&named).unwrap();
        let policy = SandboxPolicy {
            state_root: Some(dir.path().join("state")),
            layout: layout.clone(),
            additional_writable: vec![named.clone()],
            ..Default::default()
        };
        let writes = |path: &str| {
            run_confined(
                &project,
                &policy,
                &[],
                &format!("echo x > '{}'", project.join(path).display()),
            )
            .status
            .success()
        };
        let mut shut = vec![
            ".claude/commands/c.md",
            ".claude/memory/m.md",
            ".claude/new.md",
        ];
        if own != ".agix" {
            shut.extend([".agix/memory/m.md", ".agix/scratchpad/n", ".agix/new.md"]);
        }
        let own_commands = format!("{own}/commands");
        let created = run_confined(
            &project,
            &policy,
            &[],
            &format!("mkdir -p {own_commands} && echo x > {own_commands}/c.md"),
        );
        assert!(
            !created.status.success() && !project.join(&own_commands).join("c.md").exists(),
            "{own}: {own_commands}/c.md was written"
        );
        for path in shut {
            assert!(!writes(path), "{own}: {path} was written");
        }
        let in_host = format!("{host}/x");
        for entry in [
            "state/teams/t.json",
            "state/telemetry/x.jsonl",
            "state/tool-results/r.txt",
            "state/notes.md",
            in_host.as_str(),
            "scratchpad/n",
            "memory/m.md",
            "notes.md",
            "worktrees/w/x",
        ] {
            let path = format!("{own}/{entry}");
            assert!(!writes(&path), "{own}: {path} was written");
        }
        let own_scratch = format!("{own}/state/scratchpad/n");
        for path in [own_scratch.as_str(), ".claude/named/n"] {
            assert!(writes(path), "{own}: {path} was not writable");
        }
    }
}

/// The engine's worktrees directory is ordinary project space, yet a
/// command can neither move it nor a worktree in it aside — and rewrite a
/// worktree's `.git` link where no rule names it — nor add a directory of
/// its own there or rewrite a link in place. Inside a worktree it writes as
/// anywhere in the project.
#[cfg(any(target_os = "macos", target_os = "linux"))]
#[test]
#[allow(clippy::unwrap_used)]
fn a_command_cannot_move_the_worktrees_aside_or_rewrite_their_links() {
    if !a_backend_runs() {
        return;
    }
    for layout in [
        base::paths::Layout::default(),
        base::paths::Layout::new(".codix")
            .unwrap()
            .with_worktrees_dir(".trees")
            .unwrap(),
    ] {
        let dir = tempfile::tempdir().unwrap();
        let project = dir.path().canonicalize().unwrap().join("project");
        let wt = layout.worktrees_dir(&project).join("wt");
        std::fs::create_dir_all(&wt).unwrap();
        std::fs::write(wt.join(".git"), "gitdir: /nowhere\n").unwrap();
        let policy = SandboxPolicy {
            state_root: Some(dir.path().join("state")),
            layout: layout.clone(),
            ..Default::default()
        };
        let runs = |command: &str| {
            run_confined(&project, &policy, &[], command)
                .status
                .success()
        };
        let wk = layout.worktrees_dir_name();
        for command in [
            format!("mv {wk} moved"),
            format!("mv {wk}/wt {wk}/other"),
            format!("mv {wk}/wt moved"),
            format!("mkdir {wk}/planted"),
            format!("echo 'gitdir: /tmp' > {wk}/wt/.git"),
            format!("rm -rf {wk}/wt"),
        ] {
            assert!(!runs(&command), "{wk}: `{command}` was allowed");
        }
        assert_eq!(
            std::fs::read_to_string(wt.join(".git")).unwrap(),
            "gitdir: /nowhere\n"
        );
        // Across Linux's bind mounts `mv` falls back to copying, so a copy
        // may appear; what matters is that nothing left its place.
        for gone in [format!("{wk}/other"), format!("{wk}/planted")] {
            assert!(!project.join(&gone).exists(), "{gone}");
        }
        assert!(
            runs(&format!(
                "echo x > {wk}/wt/f && mkdir -p {wk}/wt/src && echo y > {wk}/wt/src/a"
            )),
            "{wk}: a worktree's files were not writable"
        );
    }
}

/// A read-only directory of the policy cannot be replaced by moving it, or
/// a directory above it in the project, aside and making another in its
/// place; the rest of those directories stays writable.
#[cfg(any(target_os = "macos", target_os = "linux"))]
#[test]
#[allow(clippy::unwrap_used)]
fn a_command_cannot_move_a_read_only_directory_aside_and_plant_another() {
    if !a_backend_runs() {
        return;
    }
    let dir = tempfile::tempdir().unwrap();
    let project = dir.path().canonicalize().unwrap().join("project");
    let packages = ["tools/xflow", "a/b/pkg", "a/pkg"];
    for pkg in packages {
        std::fs::create_dir_all(project.join(pkg)).unwrap();
        std::fs::write(project.join(pkg).join("x.js"), "host").unwrap();
    }
    let policy = SandboxPolicy {
        state_root: Some(dir.path().join("state")),
        read_only_dirs: packages.iter().map(|pkg| project.join(pkg)).collect(),
        ..Default::default()
    };
    let runs = |command: &str| {
        run_confined(&project, &policy, &[], command)
            .status
            .success()
    };
    let plant = |pkg: &str| format!("mkdir -p {pkg} && printf HACKED > {pkg}/x.js");
    for (aside, pkg) in [
        ("mv tools moved", "tools/xflow"),
        ("mv tools/xflow tools/x2", "tools/xflow"),
        ("mv a moved", "a/b/pkg"),
        ("mv a/b a/c", "a/b/pkg"),
        ("mv a/b/pkg a/b/p2", "a/b/pkg"),
        ("rm -rf tools", "tools/xflow"),
        ("true", "tools/xflow"),
        ("true", "a/b/pkg"),
        ("true", "a/pkg"),
    ] {
        let command = format!("{aside} && {}", plant(pkg));
        assert!(!runs(&command), "`{command}` was allowed");
    }
    for pkg in packages {
        assert_eq!(
            std::fs::read_to_string(project.join(pkg).join("x.js")).unwrap(),
            "host",
            "{pkg}"
        );
    }
    for gone in ["moved", "tools/x2", "a/c", "a/b/p2"] {
        assert!(!project.join(gone).exists(), "{gone}");
    }
    assert!(
        runs("echo x > tools/other.txt && mkdir tools/new && echo y > a/b/q.txt && rm a/b/q.txt"),
        "the directories above stayed writable"
    );
}

/// What in the project directory's `state/` the engine acts on unsandboxed
/// — `history/`, `tool-results/`, the embedder's `state/<product id>/` —
/// cannot be replaced by moving it, `state/` or the project directory aside
/// and making another in its place, nor written under a writable root the
/// policy names in `state/`, which stays writable around them. The same
/// holds for the configuration under a writable root named at or in the
/// project directory, or in one of its configuration directories.
#[cfg(any(target_os = "macos", target_os = "linux"))]
#[test]
#[allow(clippy::unwrap_used)]
fn a_command_cannot_move_the_project_state_aside_and_plant_history() {
    if !a_backend_runs() {
        return;
    }
    for layout in [
        base::paths::Layout::default(),
        base::paths::Layout::new(".codix").unwrap(),
    ] {
        let dir = tempfile::tempdir().unwrap();
        let project = dir.path().canonicalize().unwrap().join("project");
        let own = layout.project_dir_name().to_string();
        let state = format!("{own}/state");
        let host = format!("{state}/{}", layout.product_id());
        let engine = [
            format!("{state}/history"),
            format!("{state}/tool-results"),
            host.clone(),
        ];
        for d in engine.iter().chain([&format!("{state}/scratchpad")]) {
            std::fs::create_dir_all(project.join(d).join("s")).unwrap();
            std::fs::write(project.join(d).join("s/f"), "engine").unwrap();
        }
        let runs = |named: &[PathBuf], command: &str| {
            let policy = SandboxPolicy {
                state_root: Some(dir.path().join("state")),
                layout: layout.clone(),
                additional_writable: named.to_vec(),
                ..Default::default()
            };
            run_confined(&project, &policy, &[], command)
                .status
                .success()
        };
        let plant = |d: &str| format!("mkdir -p {d}/s && printf forged > {d}/s/f");
        let named_state = [project.join(&state)];
        let named_own = [project.join(&own)];
        for named in [&[][..], &named_state[..], &named_own[..]] {
            for d in &engine {
                for aside in [
                    format!("mv {d} {d}.old"),
                    format!("mv {state} {state}.old"),
                    format!("mv {own} {own}.old"),
                    format!("rm -rf {d}"),
                    "true".to_string(),
                ] {
                    let command = format!("{aside} && {}", plant(d));
                    assert!(!runs(named, &command), "{named:?}: `{command}` was allowed");
                }
                assert_eq!(
                    std::fs::read_to_string(project.join(d).join("s/f")).unwrap(),
                    "engine",
                    "{d}"
                );
            }
            for gone in [format!("{state}.old"), format!("{own}.old")] {
                assert!(!project.join(&gone).exists(), "{gone}");
            }
        }
        assert!(
            runs(&[], &format!("echo x > {state}/scratchpad/s/n")),
            "{state}/scratchpad was not writable"
        );
        assert!(
            runs(&named_state, &format!("echo x > {state}/notes.md")),
            "the named {state} was not writable"
        );
        std::fs::create_dir_all(project.join(&own).join("hooks")).unwrap();
        std::fs::write(project.join(&own).join("settings.json"), "{}").unwrap();
        for command in [
            format!("echo x > {own}/hooks/h.sh"),
            format!("echo x > {own}/settings.json"),
            format!("mv {own}/hooks {own}/hooks.old && mkdir {own}/hooks"),
            format!("mkdir -p {own}/agents && echo x > {own}/agents/a.md"),
        ] {
            assert!(!runs(&named_own, &command), "`{command}` was allowed");
        }
        let in_hooks = project.join(&own).join("hooks/named");
        std::fs::create_dir_all(&in_hooks).unwrap();
        let command = format!("echo x > {own}/hooks/named/h.sh");
        assert!(!runs(&[in_hooks], &command), "`{command}` was allowed");
    }
}

/// Where a configuration root or the project's skills directory is not
/// there, a command cannot make it and put configuration in it — the
/// instance's own directory, a foreign root, `.agents/skills` — and the
/// project is left as it was: no empty directory stays behind where a
/// foreign root or `.agents` was held.
#[cfg(any(target_os = "macos", target_os = "linux"))]
#[test]
#[allow(clippy::unwrap_used)]
fn no_command_creates_a_configuration_root_that_is_not_there() {
    if !a_backend_runs() {
        return;
    }
    for own in [".agix", ".codix"] {
        let dir = tempfile::tempdir().unwrap();
        let project = dir.path().canonicalize().unwrap().join("project");
        std::fs::create_dir_all(&project).unwrap();
        let policy = SandboxPolicy {
            state_root: Some(dir.path().join("state")),
            layout: base::paths::Layout::new(own).unwrap(),
            ..Default::default()
        };
        let mut planted = vec![
            format!("{own}/settings.json"),
            format!("{own}/hooks/a.sh"),
            ".claude/settings.json".to_string(),
            ".claude/commands/c.md".to_string(),
            ".agents/skills/s/SKILL.md".to_string(),
        ];
        if own != ".agix" {
            planted.push(".agix/settings.json".to_string());
        }
        for path in &planted {
            let parent = Path::new(path).parent().unwrap().display().to_string();
            let out = run_confined(
                &project,
                &policy,
                &[],
                &format!("mkdir -p {parent} && echo x > {path}"),
            );
            assert!(
                !out.status.success() && !project.join(path).exists(),
                "{own}: {path} was created"
            );
        }
        for left in [".claude", ".agents", ".agix"]
            .into_iter()
            .filter(|d| *d != own)
        {
            assert!(
                project.join(left).symlink_metadata().is_err(),
                "{own}: {left} was left behind"
            );
        }
        assert!(
            run_confined(&project, &policy, &[], "echo x > p.txt")
                .status
                .success(),
            "{own}: the project stays writable"
        );
    }
}

/// A command that finishes while another runs in the same project leaves
/// the directory the other one's sandbox holds where it is: removing it
/// would take the other's mount away and let it make the directory.
#[cfg(any(target_os = "macos", target_os = "linux"))]
#[test]
#[allow(clippy::unwrap_used)]
fn a_command_that_ends_first_leaves_the_others_held_directory_alone() {
    if !a_backend_runs() {
        return;
    }
    let dir = tempfile::tempdir().unwrap();
    let project = dir.path().canonicalize().unwrap().join("project");
    std::fs::create_dir_all(&project).unwrap();
    let policy = SandboxPolicy {
        state_root: Some(dir.path().join("state")),
        ..Default::default()
    };
    let confined = PlatformSandbox.confine(
        ProcessSpec::new("bash", &project).args(
            [
                "-c",
                "until [ -e go ]; do sleep 0.02; done; \
                 mkdir -p .claude; echo x > .claude/settings.json; touch done",
            ]
            .map(String::from),
        ),
        &policy,
    );
    let mut first = std::process::Command::new(&confined.spec.program)
        .args(&confined.spec.args)
        .current_dir(&project)
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::null())
        .spawn()
        .unwrap();
    if cfg!(target_os = "linux") {
        let held = (0..500).any(|_| {
            std::thread::sleep(std::time::Duration::from_millis(10));
            project.join(".claude").exists()
        });
        assert!(held, "the first command's sandbox never held .claude");
    }
    assert!(run_confined(&project, &policy, &[], "true")
        .status
        .success());
    std::fs::write(project.join("go"), "").unwrap();
    let _ = first.wait();
    assert!(project.join("done").exists());
    assert!(
        !project.join(".claude/settings.json").exists(),
        "the first command made .claude once the second removed it"
    );
    assert!(project.join(".claude").symlink_metadata().is_err());
}

/// A command stopped the way the engine stops one — `SIGTERM` to its group,
/// then `SIGKILL` — keeps the directory its sandbox holds in place until
/// that sandbox is gone, though the next command in the project ends while
/// what the stopped one left in a session of its own still runs.
#[cfg(target_os = "linux")]
#[tokio::test(flavor = "multi_thread")]
#[allow(clippy::unwrap_used)]
async fn linux_a_stopped_command_leaves_nothing_to_make_a_directory_after_the_cleanup() {
    use base::interface::exec::Process;
    if !a_backend_runs() {
        return;
    }
    let dir = tempfile::tempdir().unwrap();
    let project = dir.path().canonicalize().unwrap().join("project");
    std::fs::create_dir_all(&project).unwrap();
    let policy = SandboxPolicy {
        state_root: Some(dir.path().join("state")),
        ..Default::default()
    };
    let confined = PlatformSandbox.confine(
        ProcessSpec::new("bash", &project).args(
            [
                "-c",
                "setsid sh -c 'until [ -e go ]; do sleep 0.02; done; \
                 mkdir -p .claude; echo x > .claude/settings.json; touch done' & sleep 30",
            ]
            .map(String::from),
        ),
        &policy,
    );
    let cancel = tokio_util::sync::CancellationToken::new();
    let mut stopped = crate::LocalProcess::default()
        .spawn(confined.spec, cancel.clone())
        .await
        .unwrap();
    let held = (0..500).any(|_| {
        std::thread::sleep(std::time::Duration::from_millis(10));
        project.join(".claude").exists()
    });
    assert!(held, "the command's sandbox never held .claude");
    cancel.cancel();
    let _ = stopped.wait().await;
    let cleaned = tokio::task::spawn_blocking({
        let (project, policy) = (project.clone(), policy.clone());
        move || run_confined(&project, &policy, &[], "true")
    })
    .await
    .unwrap();
    assert!(cleaned.status.success());
    std::fs::write(project.join("go"), "").unwrap();
    let _tried = (0..250).any(|_| {
        std::thread::sleep(std::time::Duration::from_millis(20));
        project.join("done").exists()
    });
    assert!(
        !project.join(".claude/settings.json").exists(),
        "what the stopped command left running made .claude after the cleanup"
    );
}

/// A command that ends leaving something in the background keeps the
/// directory its sandbox holds until that is gone too: removing it when the
/// command ends would let what still runs there make it.
#[cfg(target_os = "linux")]
#[test]
#[allow(clippy::unwrap_used)]
fn linux_what_a_command_leaves_running_cannot_make_a_directory_once_it_ends() {
    if !a_backend_runs() {
        return;
    }
    let dir = tempfile::tempdir().unwrap();
    let project = dir.path().canonicalize().unwrap().join("project");
    std::fs::create_dir_all(&project).unwrap();
    let policy = SandboxPolicy {
        state_root: Some(dir.path().join("state")),
        ..Default::default()
    };
    let ended = run_confined(
        &project,
        &policy,
        &[],
        "(until [ -e go ]; do sleep 0.02; done; \
         mkdir -p .claude; echo x > .claude/settings.json; touch done) >/dev/null 2>&1 &",
    );
    assert!(ended.status.success());
    assert!(
        project.join(".claude").exists(),
        "the directory went while held"
    );
    std::fs::write(project.join("go"), "").unwrap();
    let tried = (0..500).any(|_| {
        std::thread::sleep(std::time::Duration::from_millis(20));
        project.join("done").exists()
    });
    assert!(tried, "what the command left running never got to try");
    assert!(!project.join(".claude/settings.json").exists());
    let removed = (0..500).any(|_| {
        std::thread::sleep(std::time::Duration::from_millis(20));
        project.join(".claude").symlink_metadata().is_err()
    });
    assert!(removed, "the directory stayed once nothing held it");
}