Skip to main content

kernel/
env.rs

1//! The environment variables the kernel process and its Python runtime (`runtime.py` and its modules) read, where a
2//! subagent's worktree lives, and the interpreter the kernel runs.
3
4use std::path::Path;
5
6pub const PYTHON_ENV: &str = "ULTRON_PYTHON";
7pub const RLM_ENV_ALLOW_ENV: &str = "ULTRON_RLM_ENV_ALLOW";
8pub const RLM_MAX_MEMORY_MB_ENV: &str = "ULTRON_RLM_MAX_MEMORY_MB";
9pub const RLM_MAX_CPU_SECONDS_ENV: &str = "ULTRON_RLM_MAX_CPU_SECONDS";
10pub const RLM_MAX_TREE_MEMORY_MB_ENV: &str = "ULTRON_RLM_MAX_TREE_MEMORY_MB";
11pub const RLM_TREE_MEMORY_BACKEND_ENV: &str = "ULTRON_RLM_TREE_MEMORY_BACKEND";
12pub const RLM_OUTPUT_BYTES_ENV: &str = "ULTRON_RLM_OUTPUT_BYTES";
13pub const RLM_SPILL_DIR_ENV: &str = "ULTRON_RLM_SPILL_DIR";
14pub const RLM_STREAM_OUTPUT_ENV: &str = "ULTRON_RLM_STREAM_OUTPUT";
15pub const CODE_SKILLS_DIR_ENV: &str = "ULTRON_CODE_SKILLS_DIR";
16pub const BASH_YIELD_AFTER_ENV: &str = "ULTRON_BASH_YIELD_AFTER";
17pub const CELL_YIELD_AFTER_ENV: &str = "ULTRON_CELL_YIELD_AFTER";
18pub const FILE_HOOKS_ENV: &str = "ULTRON_FILE_HOOKS";
19/// A subagent's worktree, its parent's checkout and its branch (`worktree_api.py`).
20pub const WORKTREE_ENV: &str = "ULTRON_WORKTREE";
21pub const PARENT_REPO_ENV: &str = "ULTRON_PARENT_REPO";
22pub const WORKTREE_BRANCH_ENV: &str = "ULTRON_WORKTREE_BRANCH";
23/// The directory under the Git common dir that holds subagent worktrees and their records (worktrees.ts
24/// `WORKTREES_DIR`).
25pub const WORKTREES_DIR: &str = "ultron-worktrees";
26
27/// Set (to `1`) in every kernel's environment, so a cell's command or a program it started knows it runs inside a
28/// kernel. Rust-only; the resource limits beside it may also come from the user.
29pub const IN_KERNEL_ENV: &str = "ULTRON_IN_KERNEL";
30
31/// Whether this process runs inside an Ultron kernel ([`IN_KERNEL_ENV`]).
32pub fn inside_kernel(var: impl Fn(&str) -> Option<String>) -> bool {
33    var(IN_KERNEL_ENV).as_deref() == Some("1")
34}
35
36/// kernel.ts: the interpreter the kernel runs, `ULTRON_PYTHON ?? (linux && /usr/bin/python3 exists ?
37/// "/usr/bin/python3" : "python3")`.
38pub fn python(var: impl Fn(&str) -> Option<String>) -> String {
39    var(PYTHON_ENV).unwrap_or_else(|| {
40        if cfg!(target_os = "linux") && Path::new("/usr/bin/python3").exists() {
41            "/usr/bin/python3".into()
42        } else {
43            "python3".into()
44        }
45    })
46}