pub const POOL_SUBSCRIPTION: &str = "subscription";
pub const POOL_HEADLESS: &str = "headless";
pub fn detect_pool(command: &str) -> &'static str {
let tokens: Vec<&str> = command.split_whitespace().collect();
let claude_headless = tokens
.iter()
.any(|tok| *tok == "-p" || *tok == "--print" || tok.starts_with("--print="));
if claude_headless || is_codex_exec(&tokens) {
POOL_HEADLESS
} else {
POOL_SUBSCRIPTION
}
}
fn is_codex_exec(tokens: &[&str]) -> bool {
for (i, tok) in tokens.iter().enumerate() {
let base = tok.rsplit('/').next().unwrap_or(tok);
if base == "codex" {
return tokens[i + 1..]
.iter()
.find(|t| !t.starts_with('-'))
.is_some_and(|t| *t == "exec");
}
}
false
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_interactive_is_subscription() {
assert_eq!(detect_pool("claude"), POOL_SUBSCRIPTION);
assert_eq!(detect_pool("claude 'fix the bug'"), POOL_SUBSCRIPTION);
assert_eq!(detect_pool("codex"), POOL_SUBSCRIPTION);
}
#[test]
fn test_print_flag_is_headless() {
assert_eq!(detect_pool("claude -p 'review'"), POOL_HEADLESS);
assert_eq!(detect_pool("claude --print 'review'"), POOL_HEADLESS);
assert_eq!(detect_pool("claude --print='review'"), POOL_HEADLESS);
}
#[test]
fn test_print_flag_with_leading_env_and_path() {
assert_eq!(
detect_pool("cd /repo && /usr/local/bin/claude -p 'go'"),
POOL_HEADLESS
);
}
#[test]
fn test_no_false_positive_on_substring() {
assert_eq!(detect_pool("claude 'add a -ping flag'"), POOL_SUBSCRIPTION);
assert_eq!(detect_pool("claude 'use --printer'"), POOL_SUBSCRIPTION);
}
#[test]
fn test_empty_command() {
assert_eq!(detect_pool(""), POOL_SUBSCRIPTION);
}
#[test]
fn test_codex_exec_is_headless() {
assert_eq!(detect_pool("codex exec 'fix it'"), POOL_HEADLESS);
assert_eq!(detect_pool("/opt/bin/codex exec 'fix'"), POOL_HEADLESS);
assert_eq!(detect_pool("codex --json exec 'fix'"), POOL_HEADLESS);
}
#[test]
fn test_codex_interactive_and_non_exec_subcommands_are_subscription() {
assert_eq!(detect_pool("codex"), POOL_SUBSCRIPTION);
assert_eq!(detect_pool("codex 'just chat'"), POOL_SUBSCRIPTION);
assert_eq!(detect_pool("codex login"), POOL_SUBSCRIPTION);
assert_eq!(detect_pool("claude 'run exec later'"), POOL_SUBSCRIPTION);
}
}