diff options
| author | Ogulcan Celik <ogulcancelik@gmail.com> | 2026-07-16 02:39:58 +0300 |
|---|---|---|
| committer | Ogulcan Celik <ogulcancelik@gmail.com> | 2026-07-16 02:40:39 +0300 |
| commit | 947328fa79fcefd2fa8087de014168d46a641312 (patch) | |
| tree | 15d89163b821dcbd11a05460047e0ccd52e1b66c /src/platform/mod.rs | |
| parent | a22454f27ce096585e19d1787dba43f56d1505cf (diff) | |
feat(macos): support agent environment hints
refs https://github.com/ogulcancelik/herdr/discussions/679
Diffstat (limited to 'src/platform/mod.rs')
| -rw-r--r-- | src/platform/mod.rs | 33 |
1 files changed, 32 insertions, 1 deletions
diff --git a/src/platform/mod.rs b/src/platform/mod.rs index e396966b..3583df5e 100644 --- a/src/platform/mod.rs +++ b/src/platform/mod.rs @@ -148,11 +148,22 @@ mod fallback; #[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))] pub use fallback::*; -#[cfg(not(target_os = "linux"))] +#[cfg(not(any(target_os = "linux", target_os = "macos")))] pub fn process_agent_hint(_pid: u32) -> Option<crate::detect::Agent> { None } +#[cfg(any(target_os = "linux", target_os = "macos"))] +pub(crate) fn parse_agent_env_hint(environ: &[u8]) -> Option<crate::detect::Agent> { + for record in environ.split(|&byte| byte == 0) { + let Some(value) = record.strip_prefix(b"HERDR_AGENT=") else { + continue; + }; + return crate::detect::parse_agent_label(std::str::from_utf8(value).ok()?); + } + None +} + #[cfg(not(target_os = "macos"))] #[derive(Debug)] pub(crate) struct InputSourceRestore; @@ -229,6 +240,26 @@ mod tests { ); } + #[cfg(any(target_os = "linux", target_os = "macos"))] + #[test] + fn parse_agent_env_hint_accepts_known_agents() { + assert_eq!( + parse_agent_env_hint(b"PATH=/bin\0HERDR_AGENT=claude\0TERM=xterm\0"), + Some(crate::detect::Agent::Claude) + ); + assert_eq!( + parse_agent_env_hint(b"HERDR_AGENT=codex"), + Some(crate::detect::Agent::Codex) + ); + } + + #[cfg(any(target_os = "linux", target_os = "macos"))] + #[test] + fn parse_agent_env_hint_ignores_missing_or_unknown_agents() { + assert_eq!(parse_agent_env_hint(b"PATH=/bin\0TERM=xterm\0"), None); + assert_eq!(parse_agent_env_hint(b"HERDR_AGENT=not-an-agent\0"), None); + } + #[test] fn read_limited_reader_returns_complete_data_under_limit() { let input = std::io::Cursor::new(b"image".to_vec()); |
