From 947328fa79fcefd2fa8087de014168d46a641312 Mon Sep 17 00:00:00 2001 From: Ogulcan Celik Date: Thu, 16 Jul 2026 02:39:58 +0300 Subject: feat(macos): support agent environment hints refs https://github.com/ogulcancelik/herdr/discussions/679 --- src/platform/mod.rs | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) (limited to 'src/platform/mod.rs') 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 { None } +#[cfg(any(target_os = "linux", target_os = "macos"))] +pub(crate) fn parse_agent_env_hint(environ: &[u8]) -> Option { + 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()); -- cgit v1.2.3