session_file/env.rs
1//! Clock, randomness, home, cwd and agent directory: the process inputs the session manager reads.
2
3/// Inputs the TypeScript implementation takes from the process (`Date`, `crypto`, the agent dir).
4pub trait Env {
5 /// Milliseconds since the Unix epoch (`Date.now()`).
6 fn now_ms(&mut self) -> i64;
7 /// A random UUID string (`crypto.randomUUID()`); entry ids use its first 8 characters.
8 fn random_uuid(&mut self) -> String;
9 /// A new session id (`uuidv7()`, whose generator state is process-wide upstream).
10 fn session_id(&mut self) -> String;
11 /// The agent directory (`getAgentDir()`): default session dirs live under `<agent>/sessions`.
12 fn agent_dir(&self) -> String;
13 /// `os.homedir()`: what a leading `~` names.
14 fn home(&self) -> String;
15 /// `process.cwd()`: what relative paths resolve against.
16 fn cwd(&self) -> String;
17}
18
19/// The real process environment: system clock, OS randomness, `HOME` (else `/`), the process cwd (else `/`), and
20/// `ULTRON_CODING_AGENT_DIR` or `~/.ultron/agent`.
21#[derive(Debug, Default)]
22pub struct SystemEnv;
23
24impl Env for SystemEnv {
25 fn now_ms(&mut self) -> i64 {
26 jsrt::date::now_ms()
27 }
28
29 fn random_uuid(&mut self) -> String {
30 jsrt::random::uuid_v4()
31 }
32
33 fn session_id(&mut self) -> String {
34 jsrt::random::uuid_v7(self.now_ms())
35 }
36
37 fn agent_dir(&self) -> String {
38 config::paths::agent_dir(|name| std::env::var(name).ok(), std::path::Path::new(&self.home()))
39 .display()
40 .to_string()
41 }
42
43 fn home(&self) -> String {
44 std::env::var("HOME").unwrap_or_else(|_| "/".to_string())
45 }
46
47 fn cwd(&self) -> String {
48 std::env::current_dir()
49 .map(|p| p.to_string_lossy().into_owned())
50 .unwrap_or_else(|_| "/".to_string())
51 }
52}