1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//! Functions for environment detection that need to be shared across crates.
/// Serialized provider context passed from a parent LHA session to a delegated
/// `lha exec` job.
pub const LHA_AGENT_JOB_PROVIDER_CONTEXT_ENV_VAR: &str = "LHA_AGENT_JOB_PROVIDER_CONTEXT";
/// Ephemeral auth token passed from a parent LHA session to a delegated
/// `lha exec` job. The child consumes and removes this during startup.
pub const LHA_AGENT_JOB_AUTH_TOKEN_ENV_VAR: &str = "LHA_AGENT_JOB_AUTH_TOKEN";
/// Serialized sandbox policy passed from a parent LHA session to a delegated
/// `lha exec` job. The child consumes and removes this during startup.
pub const LHA_AGENT_JOB_SANDBOX_POLICY_ENV_VAR: &str = "LHA_AGENT_JOB_SANDBOX_POLICY";
/// Serialized Windows sandbox level passed from a parent LHA session to a
/// delegated `lha exec` job. The child consumes and removes this during
/// startup.
pub const LHA_AGENT_JOB_WINDOWS_SANDBOX_LEVEL_ENV_VAR: &str = "LHA_AGENT_JOB_WINDOWS_SANDBOX_LEVEL";
/// Serialized reasoning effort passed from a parent LHA session to a delegated
/// `lha exec` job. The child consumes and removes this during startup.
pub const LHA_AGENT_JOB_REASONING_EFFORT_ENV_VAR: &str = "LHA_AGENT_JOB_REASONING_EFFORT";
fn env_var_set(key: &str) -> bool {
std::env::var(key).is_ok_and(|v| !v.trim().is_empty())
}
/// Returns true if the current process is running under Windows Subsystem for Linux.
pub fn is_wsl() -> bool {
#[cfg(target_os = "linux")]
{
if std::env::var_os("WSL_DISTRO_NAME").is_some() {
return true;
}
match std::fs::read_to_string("/proc/version") {
Ok(version) => version.to_lowercase().contains("microsoft"),
Err(_) => false,
}
}
#[cfg(not(target_os = "linux"))]
{
false
}
}
/// Returns true when LHA is likely running in an environment without a usable GUI.
///
/// This is intentionally conservative and is used by frontends to avoid flows that would try to
/// open a browser (e.g. device-code auth fallback).
pub fn is_headless_environment() -> bool {
if env_var_set("CI")
|| env_var_set("SSH_CONNECTION")
|| env_var_set("SSH_CLIENT")
|| env_var_set("SSH_TTY")
{
return true;
}
#[cfg(target_os = "linux")]
{
if !env_var_set("DISPLAY") && !env_var_set("WAYLAND_DISPLAY") {
return true;
}
}
false
}