aix-rs 1.0.0

Manage AI gateway profiles and launch AI coding tools with the right credentials
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
use super::LaunchEnv;
use crate::config::{self, PiToolConfig};
use crate::error::AixError;
use color_eyre::eyre::{eyre, WrapErr};
use serde_json::{Map, Value};
use std::collections::HashSet;
use std::env;
use std::fs;
use std::path::{Path, PathBuf};
use tempfile::{Builder, TempDir};

const AGENT_DIR_ENV: &str = "PI_CODING_AGENT_DIR";
const STORED_DATA_FILES: [&str; 3] = ["auth.json", "mcp-auth.json", "trust.json"];
const SHARED_PACKAGE_DIRS: [&str; 2] = ["npm", "git"];

pub(super) struct PiAgentConfigLaunch {
    profile: String,
    source: Option<PathBuf>,
    configured_source: PathBuf,
    source_id: String,
    api_format: config::ApiFormat,
    stage: Option<TempDir>,
}

impl PiAgentConfigLaunch {
    pub(super) fn resolve(
        profile: &str,
        config_path: &Path,
        config: &PiToolConfig,
        api_format: config::ApiFormat,
    ) -> color_eyre::Result<Self> {
        let configured_source = resolve_agent_dir(&config.agent_dir, config_path)?;
        let source_id = format!("profiles.{profile}.tool_configs.pi.agent_dir");
        let source = match inspect_agent_dir(&configured_source) {
            Ok(()) => Some(configured_source.clone()),
            Err(reason) => {
                warn_unavailable(profile, &source_id, &configured_source, &reason);
                None
            }
        };

        Ok(Self {
            profile: profile.to_string(),
            source,
            configured_source,
            source_id,
            api_format,
            stage: None,
        })
    }

    pub(super) fn validate_arguments(&self, args: &[String]) -> color_eyre::Result<()> {
        if self.source.is_none() {
            return Ok(());
        }

        for arg in args {
            if arg == "--agent-dir" || arg.starts_with("--agent-dir=") {
                return Err(AixError::PiAgentConfigArgumentConflict {
                    option: "--agent-dir",
                }
                .into());
            }
            if arg == "--api-key" || arg.starts_with("--api-key=") {
                return Err(AixError::PiAgentConfigArgumentConflict {
                    option: "--api-key",
                }
                .into());
            }
        }

        Ok(())
    }

    pub(super) fn print_dry_run(&self) {
        if self.source.is_some() {
            eprintln!(
                "Pi agent configuration: profile {:?}, tool \"pi\", source {} ({})",
                self.profile,
                self.source_id,
                self.configured_source.display()
            );
        } else {
            eprintln!(
                "Pi agent configuration: profile {:?}, tool \"pi\", source {} unavailable; using standard configuration",
                self.profile, self.source_id
            );
        }
    }

    pub(super) fn prepare(&mut self, env: &mut LaunchEnv) -> color_eyre::Result<()> {
        let Some(source) = self.source.as_ref() else {
            return Ok(());
        };

        let standard_agent_dir = standard_agent_dir()?;
        let temp = Builder::new()
            .prefix("aix-pi-agent-")
            .tempdir()
            .wrap_err("create temporary Pi agent configuration directory")?;
        let staged_agent_dir = temp.path().join("agent");
        fs::create_dir(&staged_agent_dir)
            .wrap_err("create staged Pi agent configuration directory")?;

        if let Err(error) = stage_agent_config(
            source,
            &staged_agent_dir,
            &standard_agent_dir,
            self.api_format,
            env,
        ) {
            match error {
                StageError::Source(error) => {
                    let reason = format!("cannot read selected directory ({error})");
                    warn_unavailable(
                        &self.profile,
                        &self.source_id,
                        &self.configured_source,
                        &reason,
                    );
                    self.source = None;
                    return Ok(());
                }
                StageError::Destination(error) => {
                    return Err(error).wrap_err("stage selected Pi agent configuration");
                }
            }
        }

        prepare_shared_store(&standard_agent_dir)
            .wrap_err("prepare shared Pi user data directories")?;
        add_environment_override(env, AGENT_DIR_ENV, &staged_agent_dir);
        self.stage = Some(temp);
        Ok(())
    }

    pub(super) fn add_runtime_arguments(
        &self,
        args: &[String],
        env: &LaunchEnv,
    ) -> color_eyre::Result<Vec<String>> {
        if self.source.is_none() {
            return Ok(args.to_vec());
        }

        if args.first().is_some_and(|arg| arg == "auth") {
            return Ok(args.to_vec());
        }

        let key_name = match self.api_format {
            config::ApiFormat::Anthropic => "ANTHROPIC_API_KEY",
            config::ApiFormat::OpenAi | config::ApiFormat::Both => "OPENAI_API_KEY",
        };
        let api_key = effective_value(key_name, &env.vars).ok_or_else(|| {
            eyre!("Pi profile configuration requires {key_name}, but the launch environment did not provide it")
        })?;

        let has_model_selector = args
            .iter()
            .take_while(|arg| arg.as_str() != "--")
            .any(|arg| matches!(arg.as_str(), "--model" | "--provider" | "--models"));
        let mut result = Vec::with_capacity(args.len() + 4);
        result.extend(["--api-key".to_string(), api_key]);
        if !has_model_selector {
            // Pi 1.0.4 requires a CLI model selection alongside --api-key.
            // A wildcard scope keeps Pi's defaultModel first-choice behavior
            // while leaving its available model set unrestricted.
            result.extend(["--models".to_string(), "*".to_string()]);
        }
        result.extend_from_slice(args);
        Ok(result)
    }
}

fn resolve_agent_dir(configured: &Path, config_path: &Path) -> color_eyre::Result<PathBuf> {
    let configured = configured.to_string_lossy();
    let expanded = shellexpand::tilde(&configured);
    let path = PathBuf::from(expanded.as_ref());
    if path.is_absolute() {
        return Ok(path);
    }

    let config_parent = config_path.parent().unwrap_or_else(|| Path::new("."));
    let config_parent = if config_parent.is_absolute() {
        config_parent.to_path_buf()
    } else {
        env::current_dir()?.join(config_parent)
    };
    Ok(normalize_path(&config_parent.join(path)))
}

fn normalize_path(path: &Path) -> PathBuf {
    let mut normalized = PathBuf::new();
    for component in path.components() {
        match component {
            std::path::Component::Prefix(_) | std::path::Component::RootDir => {
                normalized.push(component.as_os_str());
            }
            std::path::Component::CurDir => {}
            std::path::Component::ParentDir => {
                if normalized
                    .components()
                    .next_back()
                    .is_some_and(|last| matches!(last, std::path::Component::Normal(_)))
                {
                    normalized.pop();
                } else if !normalized.has_root() {
                    normalized.push("..");
                }
            }
            std::path::Component::Normal(_) => normalized.push(component.as_os_str()),
        }
    }
    normalized
}

fn inspect_agent_dir(path: &Path) -> Result<(), String> {
    let metadata = fs::metadata(path).map_err(|error| match error.kind() {
        std::io::ErrorKind::NotFound => "directory does not exist".to_string(),
        _ => format!("directory cannot be inspected ({error})"),
    })?;
    if !metadata.is_dir() {
        return Err("path is not a directory".to_string());
    }
    fs::read_dir(path).map_err(|error| format!("directory is not readable ({error})"))?;
    Ok(())
}

fn warn_unavailable(profile: &str, source_id: &str, path: &Path, reason: &str) {
    eprintln!(
        "warning: profile {profile:?}, tool \"pi\": selected agent directory from {source_id} ({}) is unavailable ({reason}); using standard Pi configuration",
        path.display()
    );
}

fn standard_agent_dir() -> color_eyre::Result<PathBuf> {
    if let Some(path) = env::var_os(AGENT_DIR_ENV) {
        let path = path.to_string_lossy();
        let expanded = shellexpand::tilde(&path);
        let path = PathBuf::from(expanded.as_ref());
        return if path.is_absolute() {
            Ok(path)
        } else {
            Ok(env::current_dir()?.join(path))
        };
    }

    let home = directories::BaseDirs::new()
        .ok_or_else(|| eyre!("could not determine the user's home directory for Pi data"))?;
    Ok(home.home_dir().join(".pi").join("agent"))
}

fn effective_value(name: &str, vars: &[(String, String)]) -> Option<String> {
    vars.iter()
        .rev()
        .find(|(key, _)| key == name)
        .map(|(_, value)| value.clone())
        .or_else(|| env::var(name).ok())
}

fn add_environment_override(env: &mut LaunchEnv, name: &str, value: &Path) {
    env.vars.retain(|(key, _)| key != name);
    env.vars
        .push((name.to_string(), value.to_string_lossy().into_owned()));
}

#[derive(Debug)]
enum StageError {
    Source(std::io::Error),
    Destination(std::io::Error),
}

fn stage_agent_config(
    source: &Path,
    destination: &Path,
    standard_agent_dir: &Path,
    api_format: config::ApiFormat,
    env: &LaunchEnv,
) -> Result<(), StageError> {
    let entries = fs::read_dir(source).map_err(StageError::Source)?;
    let endpoint_overrides = endpoint_overrides(api_format, &env.vars);
    let mut has_models_file = false;

    for entry in entries {
        let entry = entry.map_err(StageError::Source)?;
        let name = entry.file_name();
        let name_text = name.to_string_lossy();
        if STORED_DATA_FILES.contains(&name_text.as_ref()) || name_text == "sessions" {
            continue;
        }
        if SHARED_PACKAGE_DIRS.contains(&name_text.as_ref()) {
            continue;
        }

        let source_path = entry.path();
        let destination_path = destination.join(&name);
        if name_text == "models.json" {
            has_models_file = true;
            let metadata = fs::metadata(&source_path).map_err(StageError::Source)?;
            if metadata.is_file() {
                let bytes = fs::read(&source_path).map_err(StageError::Source)?;
                let rewritten = rewrite_models(&bytes, &endpoint_overrides);
                fs::write(destination_path, rewritten).map_err(StageError::Destination)?;
                continue;
            }
        } else if name_text == "settings.json" {
            let metadata = fs::metadata(&source_path).map_err(StageError::Source)?;
            if metadata.is_file() {
                let bytes = fs::read(&source_path).map_err(StageError::Source)?;
                let rewritten = rewrite_relative_resources(&bytes, source);
                fs::write(destination_path, rewritten).map_err(StageError::Destination)?;
                continue;
            }
        }

        let mut ancestors = HashSet::new();
        copy_entry(&source_path, &destination_path, &mut ancestors)?;
    }

    if !has_models_file && !endpoint_overrides.is_empty() {
        let mut providers = Map::new();
        for (provider, base_url) in endpoint_overrides {
            providers.insert(
                provider.to_string(),
                serde_json::json!({ "baseUrl": base_url }),
            );
        }
        let models = serde_json::json!({ "providers": providers });
        let mut bytes = serde_json::to_vec_pretty(&models)
            .map_err(|error| StageError::Destination(std::io::Error::other(error.to_string())))?;
        bytes.push(b'\n');
        fs::write(destination.join("models.json"), bytes).map_err(StageError::Destination)?;
    }

    for name in STORED_DATA_FILES {
        link_path(
            &standard_agent_dir.join(name),
            &destination.join(name),
            false,
        )
        .map_err(StageError::Destination)?;
    }
    for name in SHARED_PACKAGE_DIRS {
        link_path(
            &standard_agent_dir.join(name),
            &destination.join(name),
            true,
        )
        .map_err(StageError::Destination)?;
    }
    // The default session path remains in the original store through this link,
    // without overriding Pi's native settings.json or project sessionDir settings.
    link_path(
        &standard_agent_dir.join("sessions"),
        &destination.join("sessions"),
        true,
    )
    .map_err(StageError::Destination)?;

    Ok(())
}

fn endpoint_overrides(
    api_format: config::ApiFormat,
    vars: &[(String, String)],
) -> Vec<(&'static str, String)> {
    let mut providers = Vec::new();
    if api_format.supports_openai() {
        if let Some(url) = effective_value("OPENAI_BASE_URL", vars) {
            providers.push(("openai", url));
        }
    }
    if api_format.supports_anthropic() {
        if let Some(url) = effective_value("ANTHROPIC_BASE_URL", vars) {
            providers.push(("anthropic", url));
        }
    }
    providers
}

fn rewrite_models(bytes: &[u8], endpoints: &[(&str, String)]) -> Vec<u8> {
    let Ok(mut config) = serde_json::from_slice::<Value>(bytes) else {
        return bytes.to_vec();
    };
    let Some(root) = config.as_object_mut() else {
        return bytes.to_vec();
    };
    let providers = root
        .entry("providers")
        .or_insert_with(|| Value::Object(Map::new()));
    let Some(providers) = providers.as_object_mut() else {
        return bytes.to_vec();
    };

    for (provider_name, base_url) in endpoints {
        let provider = providers
            .entry((*provider_name).to_string())
            .or_insert_with(|| Value::Object(Map::new()));
        let Some(provider) = provider.as_object_mut() else {
            continue;
        };
        provider.insert("baseUrl".to_string(), Value::String(base_url.clone()));
        if let Some(models) = provider.get_mut("models").and_then(Value::as_array_mut) {
            for model in models {
                if let Some(model) = model.as_object_mut() {
                    model.insert("baseUrl".to_string(), Value::String(base_url.clone()));
                }
            }
        }
    }

    serialize_json(config, bytes)
}

fn rewrite_relative_resources(bytes: &[u8], agent_dir: &Path) -> Vec<u8> {
    let Ok(mut settings) = serde_json::from_slice::<Value>(bytes) else {
        return bytes.to_vec();
    };
    let Some(root) = settings.as_object_mut() else {
        return bytes.to_vec();
    };

    for key in ["extensions", "skills", "prompts", "themes"] {
        if let Some(resources) = root.get_mut(key).and_then(Value::as_array_mut) {
            for resource in resources {
                rewrite_resource_value(resource, agent_dir);
            }
        }
    }
    if let Some(packages) = root.get_mut("packages").and_then(Value::as_array_mut) {
        for package in packages {
            match package {
                Value::String(_) => rewrite_resource_value(package, agent_dir),
                Value::Object(package) => {
                    if let Some(source) = package.get_mut("source") {
                        rewrite_resource_value(source, agent_dir);
                    }
                }
                _ => {}
            }
        }
    }

    serialize_json(settings, bytes)
}

fn rewrite_resource_value(value: &mut Value, agent_dir: &Path) {
    let Some(source) = value.as_str() else {
        return;
    };
    let source = source.trim();
    if source.is_empty()
        || Path::new(source).is_absolute()
        || source.starts_with('~')
        || [
            "npm:", "git:", "github:", "http:", "https:", "ssh:", "builtin:", "file:",
        ]
        .iter()
        .any(|prefix| source.starts_with(prefix))
    {
        return;
    }

    *value = Value::String(agent_dir.join(source).to_string_lossy().into_owned());
}

fn serialize_json(value: Value, original: &[u8]) -> Vec<u8> {
    let Ok(mut rewritten) = serde_json::to_vec_pretty(&value) else {
        return original.to_vec();
    };
    rewritten.push(b'\n');
    rewritten
}

fn copy_entry(
    source: &Path,
    destination: &Path,
    ancestors: &mut HashSet<PathBuf>,
) -> Result<(), StageError> {
    let canonical = fs::canonicalize(source).map_err(StageError::Source)?;
    if !ancestors.insert(canonical.clone()) {
        return Ok(());
    }
    let result = copy_entry_contents(&canonical, destination, ancestors);
    ancestors.remove(&canonical);
    result
}

fn copy_entry_contents(
    source: &Path,
    destination: &Path,
    ancestors: &mut HashSet<PathBuf>,
) -> Result<(), StageError> {
    let metadata = fs::metadata(source).map_err(StageError::Source)?;
    if metadata.is_dir() {
        fs::create_dir(destination).map_err(StageError::Destination)?;
        let entries = fs::read_dir(source).map_err(StageError::Source)?;
        for entry in entries {
            let entry = entry.map_err(StageError::Source)?;
            copy_entry(
                &entry.path(),
                &destination.join(entry.file_name()),
                ancestors,
            )?;
        }
    } else if metadata.is_file() {
        let bytes = fs::read(source).map_err(StageError::Source)?;
        fs::write(destination, bytes).map_err(StageError::Destination)?;
    }
    Ok(())
}

fn prepare_shared_store(agent_dir: &Path) -> std::io::Result<()> {
    fs::create_dir_all(agent_dir)?;
    for name in SHARED_PACKAGE_DIRS {
        fs::create_dir_all(agent_dir.join(name))?;
    }
    fs::create_dir_all(agent_dir.join("sessions"))
}

#[cfg(unix)]
fn link_path(target: &Path, link: &Path, _is_dir: bool) -> std::io::Result<()> {
    std::os::unix::fs::symlink(target, link)
}

#[cfg(windows)]
fn link_path(target: &Path, link: &Path, is_dir: bool) -> std::io::Result<()> {
    if is_dir {
        std::os::windows::fs::symlink_dir(target, link)
    } else {
        std::os::windows::fs::symlink_file(target, link)
    }
}

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

    #[test]
    fn relative_resource_paths_keep_their_original_base() {
        let root = assert_fs::TempDir::new().unwrap();
        let agent_dir = root.path().join("profile").join("agent");
        let settings = br#"{"sessionDir":"../existing-sessions","extensions":["../shared/ext.js","npm:example"],"packages":["../../packages/demo",{"source":"../shared/pkg"}],"skills":["./skills"]}"#;

        let rewritten = rewrite_relative_resources(settings, &agent_dir);
        let parsed: Value = serde_json::from_slice(&rewritten).unwrap();
        assert_eq!(parsed["sessionDir"], "../existing-sessions");
        assert_eq!(
            parsed["extensions"][0],
            agent_dir
                .join("../shared/ext.js")
                .to_string_lossy()
                .as_ref()
        );
        assert_eq!(parsed["extensions"][1], "npm:example");
        assert_eq!(
            parsed["packages"][0],
            agent_dir
                .join("../../packages/demo")
                .to_string_lossy()
                .as_ref()
        );
        assert_eq!(
            parsed["packages"][1]["source"],
            agent_dir.join("../shared/pkg").to_string_lossy().as_ref()
        );
        assert_eq!(
            parsed["skills"][0],
            agent_dir.join("./skills").to_string_lossy().as_ref()
        );
    }

    #[test]
    fn malformed_native_files_are_preserved_for_pi_to_validate() {
        let malformed = b"{not valid json";
        assert_eq!(
            rewrite_models(malformed, &[("openai", "http://gateway/v1".into())]),
            malformed
        );
        assert_eq!(
            rewrite_relative_resources(malformed, Path::new("/selected/agent")),
            malformed
        );
    }

    #[test]
    fn model_endpoint_override_leaves_other_providers_untouched() {
        let models = br#"{"providers":{"openai":{"baseUrl":"https://old.invalid/v1","models":[{"id":"m","baseUrl":"https://model.invalid/v1"}]},"anthropic":{"baseUrl":"https://native.invalid"}}}"#;
        let rewritten = rewrite_models(models, &[("openai", "http://gateway/v1".into())]);
        let parsed: Value = serde_json::from_slice(&rewritten).unwrap();
        assert_eq!(
            parsed["providers"]["openai"]["baseUrl"],
            "http://gateway/v1"
        );
        assert_eq!(
            parsed["providers"]["openai"]["models"][0]["baseUrl"],
            "http://gateway/v1"
        );
        assert_eq!(
            parsed["providers"]["anthropic"]["baseUrl"],
            "https://native.invalid"
        );
    }

    #[test]
    fn source_and_standard_data_are_staged_separately_without_mutating_inputs() {
        let root = assert_fs::TempDir::new().unwrap();
        let source = root.path().join("profile-agent");
        let standard = root.path().join("normal-agent");
        fs::create_dir_all(&source).unwrap();
        fs::create_dir_all(&standard).unwrap();
        fs::create_dir_all(standard.join("sessions")).unwrap();
        fs::create_dir_all(standard.join("npm")).unwrap();
        fs::create_dir_all(standard.join("git")).unwrap();
        fs::write(source.join("settings.json"), br#"{"theme":"light"}"#).unwrap();
        fs::write(
            source.join("models.json"),
            br#"{"providers":{"openai":{"models":[]}}}"#,
        )
        .unwrap();
        fs::write(source.join("AGENTS.md"), "selected instructions").unwrap();
        fs::write(standard.join("auth.json"), "{\"openai\":{}}\n").unwrap();
        fs::write(standard.join("trust.json"), "{}\n").unwrap();

        let destination = root.path().join("stage");
        fs::create_dir(&destination).unwrap();
        let env = LaunchEnv {
            vars: vec![("OPENAI_BASE_URL".into(), "http://gateway.invalid/v1".into())],
            auth_vars: Vec::new(),
            display_only_vars: Vec::new(),
            clear_vars: Vec::new(),
            remove_vars: Vec::new(),
            profile_name: "work".into(),
        };
        stage_agent_config(
            &source,
            &destination,
            &standard,
            config::ApiFormat::OpenAi,
            &env,
        )
        .unwrap();

        assert_eq!(
            fs::read(destination.join("AGENTS.md")).unwrap(),
            b"selected instructions"
        );
        assert_eq!(
            fs::read_to_string(source.join("models.json")).unwrap(),
            r#"{"providers":{"openai":{"models":[]}}}"#
        );
        assert_eq!(
            fs::read_to_string(destination.join("auth.json")).unwrap(),
            fs::read_to_string(standard.join("auth.json")).unwrap()
        );
        assert_eq!(
            fs::read_to_string(destination.join("trust.json")).unwrap(),
            fs::read_to_string(standard.join("trust.json")).unwrap()
        );
        #[cfg(unix)]
        for name in ["auth.json", "trust.json", "sessions"] {
            assert!(
                fs::symlink_metadata(destination.join(name))
                    .unwrap()
                    .file_type()
                    .is_symlink(),
                "{name} must share existing Pi data without copying"
            );
        }
        let models: Value =
            serde_json::from_slice(&fs::read(destination.join("models.json")).unwrap()).unwrap();
        assert_eq!(
            models["providers"]["openai"]["baseUrl"],
            "http://gateway.invalid/v1"
        );
    }

    #[test]
    fn agent_path_resolves_against_config_directory_and_supports_home_shorthand() {
        let root = assert_fs::TempDir::new().unwrap();
        let config_path = root.path().join("config").join("aix.toml");
        fs::create_dir_all(config_path.parent().unwrap()).unwrap();
        let relative = resolve_agent_dir(Path::new("../pi-agent"), &config_path).unwrap();
        assert_eq!(relative, root.path().join("pi-agent"));

        let absolute = root.path().join("absolute-agent");
        assert_eq!(
            resolve_agent_dir(&absolute, &config_path).unwrap(),
            absolute
        );

        let home = directories::BaseDirs::new()
            .unwrap()
            .home_dir()
            .to_path_buf();
        let expanded = resolve_agent_dir(Path::new("~/.pi/agent"), &config_path).unwrap();
        assert_eq!(expanded, home.join(".pi/agent"));
    }

    #[test]
    fn agent_directory_inspection_rejects_missing_paths_and_files() {
        let root = assert_fs::TempDir::new().unwrap();
        let file = root.path().join("not-a-directory");
        fs::write(&file, "file").unwrap();

        assert_eq!(
            inspect_agent_dir(&file).unwrap_err(),
            "path is not a directory"
        );
        assert_eq!(
            inspect_agent_dir(&root.path().join("missing")).unwrap_err(),
            "directory does not exist"
        );
    }

    #[test]
    fn explicit_native_provider_and_model_overrides_remain_allowed() {
        let root = assert_fs::TempDir::new().unwrap();
        fs::create_dir(root.path().join("agent")).unwrap();
        let config = PiToolConfig {
            agent_dir: root.path().join("agent"),
        };
        let config_file = root.path().join("aix.toml");
        fs::write(&config_file, "").unwrap();
        let launch =
            PiAgentConfigLaunch::resolve("work", &config_file, &config, config::ApiFormat::OpenAi)
                .unwrap();
        launch
            .validate_arguments(&[
                "--provider".into(),
                "anthropic".into(),
                "--model".into(),
                "anthropic/claude".into(),
            ])
            .unwrap();
        assert!(launch
            .validate_arguments(&["--agent-dir=/tmp/other".into()])
            .is_err());
        assert!(launch
            .validate_arguments(&["--api-key".into(), "other".into()])
            .is_err());
    }
}