qualia_cli/handlers/inference_policy/
profile.rs1use std::{
3 path::{Path, PathBuf},
4 process::Command,
5};
6const CHILD: &str = "QUALIA_INFERENCE_POLICY_PROFILE_CHILD";
7pub(super) fn canonical(path: &Path, create: bool) -> Result<PathBuf, String> {
8 if !path.is_absolute() {
9 return Err("--profile must be an absolute native profile directory".into());
10 }
11 if path.exists() {
12 return path.canonicalize().map_err(|e| e.to_string());
13 }
14 if !create {
15 return Err(
16 "Inference profile does not exist; creation requires explicit init-vault".into(),
17 );
18 }
19 let parent = path
20 .parent()
21 .ok_or("Profile parent required")?
22 .canonicalize()
23 .map_err(|e| e.to_string())?;
24 let name = path.file_name().ok_or("Profile directory name required")?;
25 Ok(parent.join(name))
26}
27pub(super) fn bind_child(profile: &Path, create: bool) -> Result<PathBuf, String> {
28 let canonical = canonical(profile, create)?;
29 match std::env::var(CHILD).ok().as_deref() {
30 None => {
31 let status = Command::new(std::env::current_exe().map_err(|e| e.to_string())?)
32 .args(std::env::args_os().skip(1))
33 .env("QUALIA_APP_META_DIR", &canonical)
34 .env(CHILD, "1")
35 .status()
36 .map_err(|e| format!("Cannot launch bound native profile: {e}"))?;
37 std::process::exit(status.code().unwrap_or(1));
38 }
39 Some("1") => {
40 let inherited =
41 std::env::var_os("QUALIA_APP_META_DIR").ok_or("Bound metadata profile missing")?;
42 if self::canonical(Path::new(&inherited), create)? != canonical {
43 return Err("Bound metadata profile differs from explicit --profile".into());
44 }
45 }
46 Some(_) => {
47 return Err("Invalid profile child marker; no recursive launch permitted".into())
48 }
49 }
50 Ok(canonical)
51}