clm-kernel 0.0.7

Cubical Logic Model (CLM) sovereign execution engine, CPN-VM, and Cordis spatiotemporal substrate
Documentation
#![allow(unused_imports)]
use super::*;

impl CLMContext {
    /// Attach a custom `ProcessSpawner` policy to a child context (`INV-491`).
    pub fn with_process_spawner(&self, spawner: Arc<dyn ProcessSpawner>) -> Self {
        self.with_child_scope(CLMContextOverrides {
            process_spawner: Some(spawner),
            ..Default::default()
        })
    }

    /// Create a child context configured with a `RejectingProcessSpawner` and `forbid_subprocess = true`.
    pub fn with_rejecting_process_spawner(&self) -> (Self, Arc<RejectingProcessSpawner>) {
        let spawner = Arc::new(RejectingProcessSpawner::new());
        let ctx = self.with_child_scope(CLMContextOverrides {
            forbid_subprocess: Some(true),
            process_spawner: Some(spawner.clone()),
            ..Default::default()
        });
        (ctx, spawner)
    }

    /// Validate or execute a process launch against this context's process policy (`INV-491`).
    pub fn spawn_evaluation_process(
        &self,
        command: &str,
        args: &[String],
    ) -> Result<i32, SovereignSubprocessForbiddenError> {
        if let Some(ref spawner) = self.inner.process_spawner {
            return spawner.spawn_process(command, args);
        }
        if self.inner.forbid_subprocess {
            return Err(SovereignSubprocessForbiddenError::new(
                command,
                args.to_vec(),
                "Subprocess spawning is forbidden (CLM_FORBID_SUBPROCESS=true)",
            ));
        }
        Ok(0)
    }

    /// Validate a process launch against the active thread-local `CLMContext` policy.
    pub fn check_current_process_policy(
        command: &str,
        args: &[String],
    ) -> Result<(), SovereignSubprocessForbiddenError> {
        let ctx = Self::current();
        ctx.spawn_evaluation_process(command, args).map(|_| ())
    }

    pub fn runtime(&self) -> &str {
        &self.inner.runtime
    }

    pub fn agent_did(&self) -> &str {
        &self.inner.agent_did
    }

    pub fn sponsor_did(&self) -> Option<&str> {
        self.inner.sponsor_did.as_deref()
    }

    pub fn execution_locale_hash(&self) -> &str {
        &self.inner.execution_locale_hash
    }

    pub fn hash_algo(&self) -> &str {
        &self.inner.hash_algo
    }

    pub fn zero_fs(&self) -> bool {
        self.inner.zero_fs
    }

    pub fn forbid_subprocess(&self) -> bool {
        self.inner.forbid_subprocess
    }

    pub fn target_file(&self) -> Option<&Path> {
        self.inner.target_file.as_deref()
    }

    pub fn db_dir(&self) -> &Path {
        &self.inner.db_dir
    }

    pub fn get_contract_var(&self, var: ClmEnvVar) -> Option<String> {
        resolve_env_from_map(var, &self.inner.scoped_vars)
    }

    /// Export strictly allowlisted environment keys (`ENV_PASSTHROUGH_WHITELIST`).
    /// Never copies ambient host `std::env::vars()`.
    pub fn passthrough_env_map(&self) -> HashMap<String, String> {
        let mut out = HashMap::new();
        for key in ENV_PASSTHROUGH_WHITELIST {
            if let Some(val) = self.inner.scoped_vars.get(*key) {
                out.insert((*key).to_string(), val.clone());
            }
        }
        if let Some(root) = &self.inner.project_root {
            out.insert("CLM_ROOT".to_string(), root.to_string_lossy().to_string());
        }
        out.insert("CLM_RUNTIME".to_string(), self.inner.runtime.clone());
        out.insert("CLM_RUNNER".to_string(), self.inner.runtime.clone());
        out.insert("CLM_AGENT_DID".to_string(), self.inner.agent_did.clone());
        out.insert("CLM_HASH_ALGO".to_string(), self.inner.hash_algo.clone());
        out.insert(
            "CLM_ZERO_FS".to_string(),
            if self.inner.zero_fs { "true" } else { "false" }.to_string(),
        );
        out.insert(
            "CLM_FORBID_SUBPROCESS".to_string(),
            if self.inner.forbid_subprocess {
                "true"
            } else {
                "false"
            }
            .to_string(),
        );
        if let Some(tf) = &self.inner.target_file {
            out.insert(
                "CLM_TARGET_FILE".to_string(),
                tf.to_string_lossy().to_string(),
            );
        }
        out
    }

    // --- Legacy `EvaluationContext` compatibility surface (`DoD-490-06`) ---

    pub fn with_root(self, root: impl Into<PathBuf>) -> Self {
        self.with_child_scope(CLMContextOverrides {
            project_root: Some(root.into()),
            ..Default::default()
        })
    }

    pub fn with_nested_exec(self, nested: bool) -> Self {
        self.with_child_scope(CLMContextOverrides {
            nested_exec: Some(nested),
            ..Default::default()
        })
    }

    pub fn with_zero_fs(self, zero_fs: bool) -> Self {
        self.with_child_scope(CLMContextOverrides {
            zero_fs: Some(zero_fs),
            ..Default::default()
        })
    }

    pub fn root(&self) -> Option<&Path> {
        self.inner.project_root.as_deref()
    }

    pub fn project_root(&self) -> Option<&Path> {
        self.inner.project_root.as_deref()
    }


    pub fn is_nested(&self) -> bool {
        self.inner.nested_exec
    }

    pub fn is_zero_fs(&self) -> bool {
        self.inner.zero_fs || crate::utils::is_zero_fs()
    }

    pub fn set_var(&mut self, key: impl Into<String>, value: impl Into<String>) {
        let mut overrides = CLMContextOverrides::default();
        overrides.scoped_vars.insert(key.into(), value.into());
        *self = self.with_child_scope(overrides);
    }

    pub fn get_var(&self, key: &str) -> Option<String> {
        if let Some(val) = self.inner.scoped_vars.get(key) {
            return Some(val.clone());
        }
        std::env::var(key).ok()
    }

    pub fn remove_var(&mut self, key: &str) {
        let mut next = (*self.inner).clone();
        next.scoped_vars.remove(key);
        self.inner = Arc::new(next);
    }
}