agent-file-tools 0.55.1

Agent File Tools — tree-sitter powered code analysis for AI agents
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
//! Background bash task management: spawning detached tasks, the watchdog that
//! reaps them, output buffering/compression, and on-disk persistence so tasks
//! survive a bridge restart.

pub mod buffer;
pub mod output;
pub mod persistence;
pub mod process;
pub mod pty_process;
pub mod pty_runtime;
pub mod registry;
pub mod watchdog;
pub mod watches;

use crate::bash_permissions::PermissionAsk;
use crate::context::AppContext;
use crate::protocol::Response;
#[cfg(unix)]
use crate::sandbox_spawn::native_sandbox_enforced;
use crate::sandbox_spawn::{
    current_authenticated_principal, resolve_sandbox_spawn, HostEscalationAttempt,
    RequestedSandboxTier, SandboxTaskKind,
};
use persistence::BgMode;
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::collections::HashMap;
use std::path::PathBuf;
use std::time::Duration;

pub use registry::{BgCompletion, BgTaskHealthCounts, BgTaskRegistry};

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum BashShell {
    #[default]
    Bash,
    Powershell,
}

impl BashShell {
    pub(crate) fn is_powershell(self) -> bool {
        matches!(self, Self::Powershell)
    }

    pub(crate) fn command_text(self, command: &str) -> String {
        if self.is_powershell() {
            // Match Pi's optional tool: both .NET and PowerShell's pipeline use
            // UTF-8 before user code runs, so redirected native output remains
            // readable across macOS, Linux, and Windows.
            format!(
                "[Console]::OutputEncoding = [System.Text.UTF8Encoding]::new($false); $OutputEncoding = [Console]::OutputEncoding;\n{command}"
            )
        } else {
            command.to_string()
        }
    }
}

fn resolve_powershell_path_with(
    lookup: impl FnOnce(&str) -> Option<PathBuf>,
) -> Result<PathBuf, String> {
    #[cfg(windows)]
    let candidate = "pwsh.exe";
    #[cfg(not(windows))]
    let candidate = "pwsh";
    lookup(candidate).ok_or_else(|| {
        "PowerShell (pwsh) is not installed or is not on PATH. Install PowerShell 7+: https://aka.ms/powershell"
            .to_string()
    })
}

pub(crate) fn resolve_shell_path(pty: bool, shell: BashShell) -> Result<PathBuf, String> {
    if shell.is_powershell() {
        return resolve_powershell_path_with(|candidate| which::which(candidate).ok());
    }

    #[cfg(unix)]
    {
        Ok(if pty {
            pty_process::resolve_posix_shell()
        } else {
            registry::resolve_posix_shell()
        })
    }
    #[cfg(windows)]
    {
        let _ = pty;
        Ok(PathBuf::from("cmd.exe"))
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BgTaskInfo {
    pub task_id: String,
    pub status: BgTaskStatus,
    pub command: String,
    pub mode: BgMode,
    pub started_at: u64,
    pub duration_ms: Option<u64>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub status_reason: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum BgTaskStatus {
    Starting,
    Running,
    Killing,
    Completed,
    Failed,
    Killed,
    TimedOut,
    FateUnknown,
}

impl BgTaskStatus {
    pub fn is_terminal(&self) -> bool {
        matches!(
            self,
            BgTaskStatus::Completed
                | BgTaskStatus::Failed
                | BgTaskStatus::Killed
                | BgTaskStatus::TimedOut
                | BgTaskStatus::FateUnknown
        )
    }
}

/// Spawn a bash command in the background. Returns a task_id immediately.
#[allow(clippy::too_many_arguments)]
pub fn spawn(
    request_id: &str,
    session_id: &str,
    command: &str,
    shell: BashShell,
    shell_path: PathBuf,
    workdir: Option<PathBuf>,
    env: Option<HashMap<String, String>>,
    timeout_ms: Option<u64>,
    ctx: &AppContext,
    require_background_flag: bool,
    notify_on_completion: bool,
    compressed: bool,
    pty: bool,
    pty_rows: u16,
    pty_cols: u16,
    scanner_report: Vec<PermissionAsk>,
    host_escalation: Option<HostEscalationAttempt>,
) -> Response {
    if require_background_flag && !ctx.config().experimental_bash_background {
        return Response::error(
            request_id,
            "feature_disabled",
            "background bash is disabled; set `bash: { background: true }` (or `bash: true`) in aft.jsonc",
        );
    }

    let workdir = workdir.unwrap_or_else(|| {
        ctx.config().project_root.clone().unwrap_or_else(|| {
            std::env::current_dir().unwrap_or_else(|_| std::path::PathBuf::from("."))
        })
    });
    let storage_dir = task_storage_dir(ctx);
    let max_running = ctx.config().max_background_bash_tasks;
    let timeout = timeout_ms.map(Duration::from_millis);
    let project_root = ctx
        .config()
        .project_root
        .clone()
        .or_else(|| std::env::current_dir().ok())
        .and_then(|path| std::fs::canonicalize(&path).ok().or(Some(path)));

    let mut env = env.unwrap_or_default();
    let config = ctx.config();
    let child_storage_root = self::storage_dir(config.storage_dir.as_deref());
    if let Err(error) =
        crate::agent_child_env::inject(config.as_ref(), &child_storage_root, &mut env)
    {
        return Response::error(request_id, "child_environment_unavailable", error);
    }
    let task_kind = if pty {
        SandboxTaskKind::BashPty
    } else if require_background_flag {
        SandboxTaskKind::BashBackground
    } else {
        SandboxTaskKind::BashForeground
    };
    let principal = current_authenticated_principal();
    let requested_tier = if host_escalation.is_some() {
        RequestedSandboxTier::Host
    } else if ctx.config().sandbox.enabled {
        RequestedSandboxTier::Native
    } else {
        RequestedSandboxTier::Disabled
    };
    let session_dir = persistence::session_tasks_dir(&storage_dir, session_id);
    #[cfg(unix)]
    let (spawn_plan, unregistered_task) = if native_sandbox_enforced(ctx, &principal)
        && host_escalation.is_none()
    {
        let task = match persistence::allocate_task_layout(&storage_dir, session_id) {
            Ok(task) => task,
            Err(error) => {
                return Response::error(
                    request_id,
                    "sandbox_unavailable",
                    format!(
                        "native sandbox failed to create the task artifact directory: {error}; set sandbox.enabled=false to disable native sandboxing"
                    ),
                );
            }
        };
        let plan = resolve_sandbox_spawn(
            ctx,
            &principal,
            requested_tier,
            task_kind,
            &task.paths.io_dir,
            None,
        );
        if plan.refusal_code().is_some() {
            (plan, Some(task))
        } else {
            let root = project_root.as_deref().unwrap_or(&workdir);
            let environment = crate::sandbox_spawn::approved_environment_for_plan(&plan, &env);
            match crate::sandbox_spawn::prepare_task_payload(
                &task,
                command.as_bytes(),
                root,
                &workdir,
                &principal,
                &shell_path,
                &environment,
            ) {
                Ok(prepared) => (plan.with_prepared_task(prepared), Some(task)),
                Err(error) => {
                    let _ = persistence::delete_resolved_task(&task);
                    return Response::error(
                        request_id,
                        "sandbox_unavailable",
                        format!("native sandbox failed to materialize task payload: {error}"),
                    );
                }
            }
        }
    } else {
        (
            resolve_sandbox_spawn(
                ctx,
                &principal,
                requested_tier,
                task_kind,
                &session_dir,
                host_escalation.as_ref(),
            ),
            None,
        )
    };
    #[cfg(not(unix))]
    let spawn_plan = resolve_sandbox_spawn(
        ctx,
        &principal,
        requested_tier,
        task_kind,
        &session_dir,
        host_escalation.as_ref(),
    );
    if let Some(code) = spawn_plan.refusal_code() {
        #[cfg(unix)]
        if let Some(task) = unregistered_task.as_ref() {
            let _ = persistence::delete_resolved_task(task);
        }
        let message = spawn_plan
            .refusal_message()
            .unwrap_or("bash process creation refused by sandbox policy");
        return match spawn_plan.refusal_mismatch_class() {
            Some(class) => Response::error_with_data(
                request_id,
                code,
                message,
                json!({ "mismatch_class": class }),
            ),
            None => Response::error(request_id, code, message),
        };
    }

    let cleanup_plan = spawn_plan.clone();
    let spawn_result = if pty {
        ctx.bash_background().spawn_pty_with_shell(
            spawn_plan,
            command,
            shell,
            shell_path,
            session_id.to_string(),
            workdir,
            env,
            timeout,
            storage_dir,
            max_running,
            notify_on_completion,
            compressed,
            project_root,
            pty_rows,
            pty_cols,
        )
    } else {
        ctx.bash_background().spawn_with_shell(
            spawn_plan,
            command,
            shell,
            shell_path,
            session_id.to_string(),
            workdir,
            env,
            timeout,
            storage_dir,
            max_running,
            notify_on_completion,
            compressed,
            project_root,
        )
    };

    match spawn_result {
        Ok(task_id) => {
            if let Err(error) =
                ctx.bash_background()
                    .record_scanner_report(&task_id, session_id, scanner_report)
            {
                crate::slog_warn!("{error}");
            }
            Response::success(
                request_id,
                json!({
                    "task_id": task_id,
                    "status": BgTaskStatus::Running,
                    "mode": if pty { "pty" } else { "pipes" },
                }),
            )
        }
        Err(message) if message.contains("limit exceeded") => {
            cleanup_plan.cleanup_unspawned();
            #[cfg(unix)]
            if let Some(task) = unregistered_task.as_ref() {
                let _ = persistence::delete_resolved_task(task);
            }
            Response::error(request_id, "background_task_limit_exceeded", message)
        }
        Err(message) => {
            cleanup_plan.cleanup_unspawned();
            #[cfg(unix)]
            if let Some(task) = unregistered_task.as_ref() {
                let _ = persistence::delete_resolved_task(task);
            }
            if cleanup_plan.is_native_launcher() {
                Response::error(
                    request_id,
                    "sandbox_unavailable",
                    format!(
                        "native sandbox failed before command execution: {message}; set sandbox.enabled=false to disable native sandboxing"
                    ),
                )
            } else {
                Response::error(request_id, "execution_failed", message)
            }
        }
    }
}

pub(crate) fn task_storage_dir(ctx: &AppContext) -> PathBuf {
    let config = ctx.config();
    let root = storage_dir(config.storage_dir.as_deref());
    config
        .harness
        .as_ref()
        .map(|harness| root.join(harness.storage_segment()))
        .unwrap_or(root)
}

/// Resolve the process-state storage root exactly once for every Rust entry point.
/// The environment override is checked here so it wins over a stale plugin wire
/// value, while both plugin-less fallback and plugin-injected paths share one root.
pub fn storage_dir(configured: Option<&std::path::Path>) -> PathBuf {
    if let Some(dir) = non_empty_env_path("AFT_STORAGE_DIR") {
        return resolve_storage_path(&dir);
    }
    if let Some(dir) = configured {
        // Explicit process-state paths are already caller-owned. Preserve their
        // spelling so every downstream read/write uses the exact configured root.
        return dir.to_path_buf();
    }
    // AFT_CACHE_DIR outranks the computed data root: it predates
    // AFT_STORAGE_DIR as the storage sandbox lever, and the data root is
    // effectively always derivable (any HOME yields one), so ranking it
    // higher would leave the cache-dir arm permanently dead and leak
    // sandboxed fixtures onto the real machine root.
    if let Some(dir) = non_empty_env_path("AFT_CACHE_DIR") {
        return resolve_storage_path(&dir).join("aft");
    }
    if let Some(root) = cortexkit_data_root() {
        return root.join("cortexkit").join("aft");
    }
    std::env::temp_dir().join("cortexkit").join("aft")
}

fn non_empty_env_path(name: &str) -> Option<PathBuf> {
    std::env::var_os(name)
        .filter(|value| !value.is_empty())
        .map(PathBuf::from)
}

fn storage_home_dir() -> Option<PathBuf> {
    let configured = if cfg!(windows) {
        non_empty_env_path("USERPROFILE").or_else(|| non_empty_env_path("HOME"))
    } else {
        non_empty_env_path("HOME").or_else(|| non_empty_env_path("USERPROFILE"))
    };
    configured.or_else(std::env::home_dir)
}

fn cortexkit_data_root() -> Option<PathBuf> {
    if let Some(dir) = non_empty_env_path("XDG_DATA_HOME") {
        return Some(resolve_storage_path(&dir));
    }
    if cfg!(windows) {
        if let Some(dir) = std::env::var_os("LOCALAPPDATA")
            .filter(|value| !value.is_empty())
            .or_else(|| std::env::var_os("APPDATA").filter(|value| !value.is_empty()))
            .map(PathBuf::from)
        {
            return Some(resolve_storage_path(&dir));
        }
    }
    storage_home_dir().map(|home| {
        let root = if cfg!(windows) {
            home.join("AppData").join("Local")
        } else {
            home.join(".local").join("share")
        };
        resolve_storage_path(&root)
    })
}

fn resolve_storage_path(path: &std::path::Path) -> PathBuf {
    let expanded = if path == std::path::Path::new("~") {
        storage_home_dir().unwrap_or_else(std::env::temp_dir)
    } else if let Some(raw) = path.to_str() {
        if raw.starts_with("~/") || raw.starts_with("~\\") {
            storage_home_dir()
                .unwrap_or_else(std::env::temp_dir)
                .join(&raw[2..])
        } else {
            path.to_path_buf()
        }
    } else {
        path.to_path_buf()
    };
    let absolute = if expanded.is_absolute() {
        expanded
    } else {
        std::env::current_dir()
            .unwrap_or_else(|_| std::env::temp_dir())
            .join(expanded)
    };
    normalize_absolute_path(&absolute)
}

fn normalize_absolute_path(path: &std::path::Path) -> PathBuf {
    use std::path::Component;

    let mut normalized = PathBuf::new();
    for component in path.components() {
        match component {
            Component::CurDir => {}
            Component::ParentDir => {
                if !normalized.pop() {
                    normalized.push(component.as_os_str());
                }
            }
            other => normalized.push(other.as_os_str()),
        }
    }
    normalized
}

pub fn repair_legacy_root_tasks(storage_root: &std::path::Path, harness: crate::harness::Harness) {
    let root_tasks = storage_root.join("bash-tasks");
    if !dir_has_entries(&root_tasks) {
        return;
    }

    let harness_tasks = storage_root
        .join(harness.storage_segment())
        .join("bash-tasks");
    if dir_has_entries(&harness_tasks) {
        return;
    }
    if let Some(parent) = harness_tasks.parent() {
        if let Err(error) = std::fs::create_dir_all(parent) {
            crate::slog_warn!(
                "failed to create harness bash task dir {}: {}",
                parent.display(),
                error
            );
            return;
        }
    }
    if harness_tasks.exists() {
        let _ = std::fs::remove_dir(&harness_tasks);
    }

    match std::fs::rename(&root_tasks, &harness_tasks) {
        Ok(()) => crate::slog_info!(
            "moved legacy root bash tasks into harness namespace: {}",
            harness_tasks.display()
        ),
        Err(error) => {
            crate::slog_warn!(
                "failed to move legacy root bash tasks into {}: {}; trying child merge",
                harness_tasks.display(),
                error
            );
            if std::fs::create_dir_all(&harness_tasks).is_err() {
                return;
            }
            if let Ok(entries) = std::fs::read_dir(&root_tasks) {
                for entry in entries.flatten() {
                    let source = entry.path();
                    let target = harness_tasks.join(entry.file_name());
                    if !target.exists() {
                        let _ = std::fs::rename(source, target);
                    }
                }
            }
            let _ = std::fs::remove_dir(&root_tasks);
        }
    }
}

fn dir_has_entries(path: &std::path::Path) -> bool {
    std::fs::read_dir(path)
        .map(|mut entries| entries.next().is_some())
        .unwrap_or(false)
}

#[cfg(test)]
mod storage_root_tests {
    use std::ffi::{OsStr, OsString};
    use std::panic::{catch_unwind, AssertUnwindSafe};
    use std::path::Path;

    struct NonPanickingCleanup<F: FnOnce()> {
        cleanup: Option<F>,
    }

    impl<F: FnOnce()> NonPanickingCleanup<F> {
        fn new(cleanup: F) -> Self {
            Self {
                cleanup: Some(cleanup),
            }
        }
    }

    impl<F: FnOnce()> Drop for NonPanickingCleanup<F> {
        fn drop(&mut self) {
            let Some(cleanup) = self.cleanup.take() else {
                return;
            };
            // A cleanup panic while the test is already unwinding aborts the whole
            // libtest process, so cleanup failures must remain contained here.
            let _ = catch_unwind(AssertUnwindSafe(cleanup));
        }
    }

    struct StorageEnvGuard {
        previous: Vec<(&'static str, Option<OsString>)>,
    }

    impl StorageEnvGuard {
        fn capture() -> Self {
            Self {
                previous: [
                    "AFT_STORAGE_DIR",
                    "AFT_CACHE_DIR",
                    "XDG_DATA_HOME",
                    "HOME",
                    "USERPROFILE",
                    "LOCALAPPDATA",
                    "APPDATA",
                ]
                .into_iter()
                .map(|key| (key, std::env::var_os(key)))
                .collect(),
            }
        }

        fn set(&self, key: &'static str, value: Option<&OsStr>) {
            match value {
                Some(value) => std::env::set_var(key, value),
                None => std::env::remove_var(key),
            }
        }
    }

    impl Drop for StorageEnvGuard {
        fn drop(&mut self) {
            let previous: Vec<_> = self.previous.drain(..).collect();
            // Env restoration runs while a failing test may already be
            // unwinding; a cleanup panic at that point aborts the whole
            // libtest process (observed on Windows CI), so the restore loop
            // stays contained like NonPanickingCleanup above.
            let _ = catch_unwind(AssertUnwindSafe(move || {
                for (key, value) in previous {
                    match value {
                        Some(value) => std::env::set_var(key, value),
                        None => std::env::remove_var(key),
                    }
                }
            }));
        }
    }

    // The plugin-injected and plugin-less paths must use one resolver so every
    // artifact lane sees the same absolute root under every environment arm.
    #[test]
    fn fallback_and_injected_roots_agree_with_storage_override_arms() {
        let _env_lock = crate::test_env::process_env_lock();
        let env = StorageEnvGuard::capture();
        let base = tempfile::tempdir().expect("storage root test directory");
        let data_home = base.path().join("data");
        let home = base.path().join("home");
        let expected_plugin_root = data_home.join("cortexkit").join("aft");
        let cache_root = base.path().join("legacy-cache");
        env.set("XDG_DATA_HOME", Some(data_home.as_os_str()));
        env.set("HOME", Some(home.as_os_str()));
        env.set("USERPROFILE", Some(home.as_os_str()));
        // The fallback==injected agreement is proven with the cache lever
        // unset: plugin flows always pass `configured`, so AFT_CACHE_DIR
        // only ever steers plugin-less (sandbox/test) invocations and ranks
        // above the computed data root for exactly that purpose.
        env.set("AFT_CACHE_DIR", None);
        env.set("AFT_STORAGE_DIR", None);

        assert_eq!(super::storage_dir(None), expected_plugin_root);

        env.set("AFT_CACHE_DIR", Some(cache_root.as_os_str()));
        assert_eq!(super::storage_dir(None), cache_root.join("aft"));
        assert_eq!(
            super::storage_dir(Some(&expected_plugin_root)),
            expected_plugin_root,
            "configured root must outrank the cache lever"
        );
        env.set("AFT_CACHE_DIR", None);
        assert_eq!(
            super::storage_dir(Some(&expected_plugin_root)),
            expected_plugin_root
        );
        assert_eq!(
            crate::search_index::resolve_cache_dir(Path::new("/tmp/project"), None)
                .parent()
                .and_then(Path::parent),
            Some(expected_plugin_root.as_path())
        );

        let spelled_dir = base.path().join("spelled");
        std::fs::create_dir(&spelled_dir).expect("spelled path component");
        let explicit_spelling = spelled_dir.join("..");
        assert_eq!(
            super::storage_dir(Some(&explicit_spelling)),
            explicit_spelling
        );
        assert_eq!(
            crate::search_index::resolve_cache_dir(
                Path::new("/tmp/project"),
                Some(&explicit_spelling)
            ),
            explicit_spelling
                .join("index")
                .join(crate::search_index::artifact_cache_key(Path::new(
                    "/tmp/project"
                )))
        );

        env.set(
            "AFT_STORAGE_DIR",
            Some(OsStr::new("./relative/../local-aft-storage")),
        );
        let expected_relative = super::resolve_storage_path(Path::new("./local-aft-storage"));
        assert!(expected_relative.is_absolute());
        assert_eq!(super::storage_dir(None), expected_relative);
        assert_eq!(
            super::storage_dir(Some(&expected_plugin_root)),
            expected_relative
        );

        env.set("AFT_STORAGE_DIR", Some(OsStr::new("")));
        assert_eq!(super::storage_dir(None), expected_plugin_root);
        assert_eq!(
            super::storage_dir(Some(&expected_plugin_root)),
            expected_plugin_root
        );

        env.set("AFT_STORAGE_DIR", Some(OsStr::new("~/tilde-aft-storage")));
        let expected_tilde = home.join("tilde-aft-storage");
        assert_eq!(super::storage_dir(None), expected_tilde);
        assert_eq!(
            super::storage_dir(Some(&expected_plugin_root)),
            expected_tilde
        );
    }

    #[test]
    fn powershell_absence_has_an_honest_install_remedy() {
        let error = super::resolve_powershell_path_with(|_| None).expect_err("pwsh is absent");
        assert!(error.contains("PowerShell (pwsh) is not installed"));
        assert!(error.contains("https://aka.ms/powershell"));
    }

    #[test]
    fn cleanup_panic_during_unwind_does_not_abort_libtest() {
        use std::sync::atomic::{AtomicBool, Ordering};

        let cleanup_ran = AtomicBool::new(false);
        let unwind = catch_unwind(AssertUnwindSafe(|| {
            let _cleanup = NonPanickingCleanup::new(|| {
                cleanup_ran.store(true, Ordering::SeqCst);
                panic!("forced cleanup failure");
            });
            panic!("primary test failure");
        }));

        assert!(cleanup_ran.load(Ordering::SeqCst));
        assert_eq!(
            unwind
                .expect_err("primary panic must escape the inner scope")
                .downcast_ref::<&str>(),
            Some(&"primary test failure")
        );
    }
}