use std::os::unix::process::CommandExt;
use std::path::Path;
use anyhow::{Context, Result};
use serde::Deserialize;
#[derive(Debug, Deserialize, Default)]
pub struct ConfigFile {
pub spawn: Option<SpawnConfig>,
pub session: Option<SessionConfig>,
pub tui: Option<TuiConfig>,
}
#[derive(Debug, Deserialize)]
pub struct SpawnConfig {
pub wrapper: Option<Vec<String>>,
}
#[derive(Debug, Deserialize)]
pub struct SessionConfig {
pub finder: Option<String>,
}
#[derive(Debug, Deserialize)]
pub struct TuiConfig {
#[serde(default)]
pub commands: Vec<CustomCommand>,
pub theme: Option<String>,
pub animate: Option<bool>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct CustomCommand {
pub name: String,
pub key: String,
pub command: String,
}
impl CustomCommand {
pub fn key_char(&self) -> char {
self.key.chars().next().unwrap_or('\0')
}
}
#[derive(Debug)]
pub struct Config {
pub finder: Option<String>,
pub commands: Vec<CustomCommand>,
pub theme: Option<String>,
pub animate: bool,
pub wrapper: Vec<String>,
}
impl Default for Config {
fn default() -> Self {
Self {
finder: None,
commands: Vec::new(),
theme: None,
animate: true,
wrapper: Vec::new(),
}
}
}
pub fn parse_config(toml_str: &str) -> Result<Config> {
let file: ConfigFile = toml::from_str(toml_str)?;
let finder = file.session.as_ref().and_then(|s| s.finder.clone());
let commands = file
.tui
.as_ref()
.map(|t| t.commands.clone())
.unwrap_or_default();
validate_custom_commands(&commands)?;
let theme = file.tui.as_ref().and_then(|t| t.theme.clone());
if let Some(theme) = &theme {
anyhow::ensure!(
matches!(theme.as_str(), "dark" | "unicorn" | "ansi"),
"tui.theme is 'dark', 'unicorn' or 'ansi', not '{theme}'"
);
}
let wrapper = file
.spawn
.as_ref()
.and_then(|s| s.wrapper.clone())
.unwrap_or_default();
if wrapper
.first()
.is_some_and(|program| program.trim().is_empty())
{
anyhow::bail!("spawn.wrapper: the first word must be the command to run");
}
Ok(Config {
finder,
commands,
theme,
animate: file.tui.as_ref().and_then(|t| t.animate).unwrap_or(true),
wrapper,
})
}
pub fn load_config() -> Result<Config> {
match tam_worktree::config::config_path().filter(|path| path.exists()) {
Some(path) => parse_config(&std::fs::read_to_string(&path)?),
None => Ok(Config::default()),
}
}
pub(crate) fn shell_quote(s: &str) -> String {
if s.is_empty() {
return "''".to_string();
}
if s.bytes().all(|b| {
matches!(
b,
b'a'..=b'z'
| b'A'..=b'Z'
| b'0'..=b'9'
| b'-'
| b'_'
| b'/'
| b'.'
| b':'
| b'@'
| b'='
| b'+'
| b','
)
}) {
return s.to_string();
}
format!("'{}'", s.replace('\'', "'\\''"))
}
pub struct CommandValues<'a> {
pub task: &'a str,
pub id: &'a str,
pub dir: &'a Path,
pub provider: &'a str,
}
fn expand_command(template: &str, values: &CommandValues) -> String {
let dir = values.dir.to_string_lossy();
template
.replace("{task}", &shell_quote(values.task))
.replace("{id}", &shell_quote(values.id))
.replace("{dir}", &shell_quote(&dir))
.replace("{provider}", &shell_quote(values.provider))
}
pub fn run_custom_command(template: &str, values: &CommandValues) -> Result<()> {
let cmd = expand_command(template, values);
unsafe {
std::process::Command::new("sh")
.arg("-c")
.arg(&cmd)
.stdin(std::process::Stdio::null())
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.pre_exec(|| {
nix::unistd::setsid().map_err(|e| std::io::Error::from_raw_os_error(e as i32))?;
Ok(())
})
.spawn()
.with_context(|| format!("failed to run command: {cmd}"))?;
}
Ok(())
}
const RESERVED_KEYS: &[char] = &[
'q', 'j', 'k', 'h', 'l', 'n', 'r', 'R', 's', 'd', 'f', '/', 'g', 'G', 't', 'T', '?', 'a',
];
pub const YIELDING_KEYS: &[char] = &['c', 'p', 'N', 'v'];
fn validate_custom_commands(commands: &[CustomCommand]) -> Result<()> {
let mut seen = std::collections::HashSet::new();
for cmd in commands {
if cmd.key.chars().count() != 1 {
anyhow::bail!(
"custom command '{}': key must be a single character, got '{}'",
cmd.name,
cmd.key
);
}
let c = cmd.key_char();
if RESERVED_KEYS.contains(&c) {
anyhow::bail!(
"custom command '{}': key '{}' is reserved by a built-in action",
cmd.name,
cmd.key
);
}
if !seen.insert(c) {
anyhow::bail!("custom command '{}': duplicate key '{}'", cmd.name, cmd.key);
}
}
Ok(())
}
pub fn remove_legacy_hooks(agent: &str) -> Result<()> {
match agent {
"claude" => {
let settings_path = dirs::home_dir()
.context("cannot determine home directory")?
.join(".claude")
.join("settings.json");
remove_legacy_claude_hooks(&settings_path)
}
_ => anyhow::bail!("init not supported for agent '{agent}'"),
}
}
fn remove_legacy_claude_hooks(settings_path: &Path) -> Result<()> {
println!("Claude hooks are now injected into each agent tam spawns; nothing to install.");
if !settings_path.exists() {
return Ok(());
}
let content = std::fs::read_to_string(settings_path)
.with_context(|| format!("failed to read {}", settings_path.display()))?;
let mut settings: serde_json::Value = serde_json::from_str(&content)
.with_context(|| format!("failed to parse {}", settings_path.display()))?;
let removed = strip_legacy_hooks(&mut settings);
if removed.is_empty() {
println!("No old tam hooks in {}.", settings_path.display());
return Ok(());
}
let formatted = serde_json::to_string_pretty(&settings)?;
std::fs::write(settings_path, formatted.as_bytes())
.with_context(|| format!("failed to write {}", settings_path.display()))?;
println!(
"Removed old tam hooks from {}: {}",
settings_path.display(),
removed.join(", ")
);
Ok(())
}
fn is_legacy_hook_command(command: &str) -> bool {
let mut words = command.split_whitespace();
let program = words.next().unwrap_or("");
let program = program.rsplit('/').next().unwrap_or(program);
matches!(program, "tam" | "zinc")
&& words.next() == Some("hook-notify")
&& words.next() == Some("--event")
}
fn strip_legacy_hooks(settings: &mut serde_json::Value) -> Vec<String> {
let mut removed = Vec::new();
let Some(hooks) = settings.get_mut("hooks").and_then(|h| h.as_object_mut()) else {
return removed;
};
for (event, groups) in hooks.iter_mut() {
let Some(groups) = groups.as_array_mut() else {
continue;
};
groups.retain_mut(|group| {
let Some(handlers) = group.get_mut("hooks").and_then(|h| h.as_array_mut()) else {
return true;
};
let before = handlers.len();
handlers.retain(|h| !h["command"].as_str().is_some_and(is_legacy_hook_command));
if handlers.len() == before {
return true;
}
let label = match group.get("matcher").and_then(|m| m.as_str()) {
Some(m) if !m.is_empty() => format!("{event}({m})"),
_ => event.clone(),
};
removed.push(label);
!group["hooks"].as_array().is_some_and(|h| h.is_empty())
});
}
hooks.retain(|_, groups| !groups.as_array().is_some_and(|g| g.is_empty()));
if hooks.is_empty() {
if let Some(obj) = settings.as_object_mut() {
obj.remove("hooks");
}
}
removed
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_theme_is_one_of_three_and_rainbows_can_stand_still() {
let config = parse_config("[tui]\ntheme = \"unicorn\"\nanimate = false\n").unwrap();
assert_eq!(config.theme.as_deref(), Some("unicorn"));
assert!(!config.animate);
assert!(parse_config("").unwrap().animate);
assert!(parse_config("[tui]\ntheme = \"pink\"\n").is_err());
}
#[test]
fn defaults() {
let config = Config::default();
assert!(config.wrapper.is_empty() && config.animate);
}
#[test]
fn retired_daemon_scrollback_setting_is_ignored() {
let config = parse_config("[daemon]\nscrollback = 1048576\n").unwrap();
assert!(config.commands.is_empty());
}
#[test]
fn parse_empty_toml() {
let config = parse_config("").unwrap();
assert!(config.commands.is_empty() && config.wrapper.is_empty());
}
#[test]
fn the_retired_default_agent_setting_is_ignored() {
let toml = r#"
[spawn]
default_agent = "codex"
"#;
let config = parse_config(toml).unwrap();
assert!(config.wrapper.is_empty());
}
#[test]
fn parse_spawn_wrapper() {
let config = parse_config("[spawn]\nwrapper = [\"direnv\", \"exec\", \".\"]\n").unwrap();
assert_eq!(config.wrapper, ["direnv", "exec", "."]);
let config = parse_config("[spawn]\nwrapper = []\n").unwrap();
assert!(config.wrapper.is_empty());
let err = parse_config("[spawn]\nwrapper = [\"\", \"exec\"]\n").unwrap_err();
assert!(err.to_string().contains("spawn.wrapper"), "{err}");
assert!(parse_config("[spawn]\nwrapper = \"direnv exec .\"\n").is_err());
}
#[test]
fn parse_session_finder() {
let toml = r#"
[session]
finder = "fzf"
"#;
let config = parse_config(toml).unwrap();
assert_eq!(config.finder.unwrap(), "fzf");
}
#[test]
fn run_and_reply_keys_are_reserved() {
for key in ["r", "R", "a"] {
let toml =
format!("[[tui.commands]]\nname = \"x\"\nkey = \"{key}\"\ncommand = \"true\"\n");
let err = parse_config(&toml).unwrap_err().to_string();
assert!(err.contains("reserved"), "{key}: {err}");
}
}
#[test]
fn the_newer_dashboard_keys_yield_to_a_custom_command() {
for key in YIELDING_KEYS {
let toml =
format!("[[tui.commands]]\nname = \"x\"\nkey = \"{key}\"\ncommand = \"true\"\n");
let config = parse_config(&toml).unwrap();
assert_eq!(config.commands[0].key_char(), *key);
}
}
#[test]
fn custom_command_placeholders() {
let values = CommandValues {
task: "Chat design",
id: "c-0000abcd",
dir: Path::new("/src/my tam"),
provider: "claude",
};
assert_eq!(
expand_command("x {task} {id} {dir} {provider}", &values),
"x 'Chat design' c-0000abcd '/src/my tam' claude"
);
}
#[test]
fn shell_quote_safe_string() {
assert_eq!(shell_quote("/tmp/foo-bar"), "/tmp/foo-bar");
}
#[test]
fn shell_quote_spaces() {
assert_eq!(shell_quote("/tmp/my project"), "'/tmp/my project'");
}
#[test]
fn shell_quote_injection() {
assert_eq!(shell_quote("/tmp/foo; rm -rf /"), "'/tmp/foo; rm -rf /'");
}
#[test]
fn legacy_hook_commands() {
assert!(is_legacy_hook_command("tam hook-notify --event stop"));
assert!(is_legacy_hook_command("zinc hook-notify --event stop"));
assert!(is_legacy_hook_command(
"/usr/bin/tam hook-notify --event notification:idle_prompt"
));
assert!(!is_legacy_hook_command(
"'/nix/store/x/bin/tam' hook-notify"
));
assert!(!is_legacy_hook_command("tam hook-notify"));
assert!(!is_legacy_hook_command("notify-send done"));
}
#[test]
fn strips_only_legacy_hooks() {
let legacy = |e: &str| serde_json::json!({"type": "command", "command": format!("tam hook-notify --event {e}"), "timeout": 5});
let mine = serde_json::json!({"type": "command", "command": "notify-send stop"});
let mut settings = serde_json::json!({
"model": "opus",
"hooks": {
"UserPromptSubmit": [{"hooks": [legacy("user_prompt_submit")]}],
"Stop": [{"hooks": [legacy("stop"), mine.clone()]}],
"Notification": [
{"matcher": "idle_prompt", "hooks": [legacy("notification:idle_prompt")]},
{"matcher": "permission_prompt", "hooks": [legacy("notification:permission_prompt")]}
],
"PreToolUse": [{"matcher": "Bash", "hooks": [mine.clone()]}]
}
});
let removed = strip_legacy_hooks(&mut settings);
assert_eq!(
removed,
vec![
"Notification(idle_prompt)",
"Notification(permission_prompt)",
"Stop",
"UserPromptSubmit"
]
);
assert_eq!(
settings,
serde_json::json!({
"model": "opus",
"hooks": {
"Stop": [{"hooks": [mine.clone()]}],
"PreToolUse": [{"matcher": "Bash", "hooks": [mine]}]
}
})
);
assert!(strip_legacy_hooks(&mut settings).is_empty());
}
#[test]
fn drops_hooks_key_when_only_legacy_hooks() {
let mut settings = serde_json::json!({
"hooks": {"Stop": [{"hooks": [{"type": "command", "command": "tam hook-notify --event stop"}]}]}
});
assert_eq!(strip_legacy_hooks(&mut settings), vec!["Stop"]);
assert_eq!(settings, serde_json::json!({}));
}
#[test]
fn removes_legacy_hooks_from_file() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("settings.json");
std::fs::write(
&path,
r#"{"hooks":{"Stop":[{"hooks":[{"type":"command","command":"tam hook-notify --event stop"}]}]},"theme":"dark"}"#,
)
.unwrap();
remove_legacy_claude_hooks(&path).unwrap();
let after: serde_json::Value =
serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
assert_eq!(after, serde_json::json!({"theme": "dark"}));
remove_legacy_claude_hooks(&dir.path().join("none.json")).unwrap();
}
}