Skip to main content

session_core/
lib.rs

1//! One session's runtime, shared by every mode. TypeScript runs every session in a session worker process and so
2//! does the Rust build (crates/daemon `worker`); this crate is what that worker runs:
3//!
4//! - [`lane`]: the model, its key, the native worker's system prompt, the provider call, the clock lines and the
5//!   single `rlm` tool (the model's tool list is exactly `[rlm]`);
6//! - [`compose`]: the composition root of the host stack behind a lane's kernels;
7//! - [`journal`]: the format-4 harness operation journal of a run; [`compaction_op`] that of a compaction;
8//! - [`naming`]: automatic and model-made session names;
9//! - [`pi_view`] / [`view`]: the session as Pi entries and Pi's read views (tree, messages, stats);
10//! - [`core`]: the session actor that owns all of it and reports lane-watch events.
11
12mod agent_hub;
13mod agents;
14pub mod async_events;
15pub mod auto_compaction;
16pub mod auto_memory;
17pub mod auto_refine;
18mod budget_note;
19mod budget_pricing;
20pub mod cell_file_changes;
21pub mod commands;
22pub mod compaction_op;
23pub mod compose;
24pub mod context_reminder;
25pub mod continuation;
26mod contract_checks;
27pub mod core;
28pub mod durable_store;
29mod format_version;
30pub mod frame_models;
31pub mod frames;
32pub mod guard_log;
33pub mod inspect;
34pub mod journal;
35pub mod kept_contracts;
36pub mod lane;
37mod lane_reuse;
38pub mod local_resources;
39pub mod loki_notice;
40pub mod memory_children;
41pub mod memory_guidance;
42pub mod migration;
43pub mod model_refresh;
44pub mod naming;
45pub mod nudge;
46mod pi_view;
47mod playbook_route;
48mod prompt_budget;
49pub mod recovery;
50pub mod request_keys;
51pub mod request_layer;
52pub mod request_settings;
53pub mod request_trace;
54pub mod review_command;
55pub mod session_diagrams;
56pub mod session_flags;
57mod session_images;
58mod session_judge;
59mod session_knowledge;
60mod session_modules;
61pub mod session_resources;
62pub mod session_stats;
63mod session_tools;
64pub mod situational_skills;
65pub mod startup_trace;
66pub mod stored_auth;
67pub mod subagents;
68pub mod swarm_ops;
69pub mod turn_checkpoints;
70pub mod turns;
71pub mod view;
72pub mod view_images;
73pub mod wake;
74
75use std::path::PathBuf;
76use std::sync::Arc;
77
78/// A session switch's "off": `off`, `0` or `false`, in any case and with surrounding space (the TypeScript
79/// `["off", "0", "false"].includes(value.trim().toLowerCase())`).
80pub fn flag_off(value: Option<&str>) -> bool {
81    value.is_some_and(|v| matches!(v.trim().to_lowercase().as_str(), "off" | "0" | "false"))
82}
83
84pub type VarLookup = config::auth::Vars;
85
86/// The process environment and locations a session sees.
87#[derive(Clone)]
88pub struct RunEnv {
89    pub cwd: PathBuf,
90    pub home: PathBuf,
91    pub var: VarLookup,
92    /// The package files (`cli::assets`): the embedded files installed under `<agent dir>/runtime/<version>-<hash>/`,
93    /// or a live checkout in dev builds (`ULTRON_DEV_ASSETS`); the CLI makes them ready before a session reads them.
94    pub package: PathBuf,
95}
96
97impl RunEnv {
98    pub fn agent_dir(&self) -> PathBuf {
99        config::paths::agent_dir(|k| (self.var)(k), &self.home)
100    }
101
102    /// A file of the package (`getPackageDir()`-relative).
103    pub fn package_file(&self, relative: &str) -> PathBuf {
104        config::paths::normalize(&self.package.join(relative))
105    }
106
107    /// `getDocsPath()`.
108    pub fn docs(&self) -> PathBuf {
109        self.package_file("docs")
110    }
111
112    pub fn skills(&self) -> PathBuf {
113        self.package_file("skills")
114    }
115
116    pub fn runtime(&self) -> PathBuf {
117        self.package_file(RUNTIME_PY)
118    }
119
120    pub fn flag_off(&self, name: &str) -> bool {
121        flag_off((self.var)(name).as_deref())
122    }
123
124    pub fn process(cwd: PathBuf, package: PathBuf) -> RunEnv {
125        RunEnv {
126            cwd,
127            home: std::env::var_os("HOME").map(PathBuf::from).unwrap_or_default(),
128            var: Arc::new(|k| std::env::var(k).ok()),
129            package,
130        }
131    }
132}
133
134/// The REPL's runtime script, relative to the package directory.
135pub const RUNTIME_PY: &str = "src/ultron/rlm/runtime.py";
136
137/// The model flags a session starts with (`--provider`, `--model`, `--api-key`, `--thinking`, `--models`).
138#[derive(Clone, Debug, Default, PartialEq)]
139pub struct ModelFlags {
140    pub provider: Option<String>,
141    pub model: Option<String>,
142    pub api_key: Option<String>,
143    pub thinking: Option<String>,
144    /// `--models`: the scoped model patterns (None: the settings' `enabledModels`).
145    pub models: Option<Vec<String>>,
146}