fix(maestro): run spawned commands through a login+interactive shell
ci / rust (push) Failing after 1m46s

Presets spawn bare commands (`claude`, `opencode`); tmux runs them via
`$SHELL -c` non-interactively, so `~/.zshrc` — where the user's PATH
additions (`~/.local/bin`, `~/.opencode/bin`, …) live — is never sourced.
Under the stripped systemd-user PATH `maestrod` runs with, those binaries
aren't found → "command not found". `zsh` worked only because it's in the
system PATH.

Wrap each session command in `exec $SHELL -lic '<cmd>'` so an interactive
login shell sources the user's rc and restores their PATH before exec'ing
the real process. `exec` keeps the process tree flat so pane-command
inference is unaffected. Falls back to the bare command when $SHELL is unset.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-21 20:36:21 +00:00
co-authored by Claude Opus 4.8
parent 5602cf5fc8
commit 6167559fea
+77 -4
View File
@@ -3,6 +3,7 @@
//! All sessions managed through this wrapper are named `maestro-<uuid>` so that
//! the daemon can recover its own sessions on restart (and ignore foreign ones).
use std::env;
use std::ffi::{OsStr, OsString};
use std::os::unix::ffi::OsStrExt;
use std::path::{Path, PathBuf};
@@ -79,8 +80,10 @@ impl Tmux {
/// `tmux set-option -g default-terminal tmux-256color \; new-session -d -e TERM=xterm-256color -e COLORTERM=truecolor -s maestro-<id> -c <cwd> <command>`.
///
/// `command` is passed as a single shell-line argument to tmux, which
/// invokes it via `$SHELL -c`. Two layers ensure attach clients see color:
/// `command` is wrapped by [`login_shell_command`] (so the spawned process
/// inherits the user's rc-configured PATH) and passed as a single shell-line
/// argument to tmux, which invokes it via `$SHELL -c`. Two layers ensure
/// attach clients see color:
/// the `-e TERM=…`/`COLORTERM=…` flags populate the spawned shell's env
/// even when `maestrod` runs from a stripped systemd unit, and the chained
/// `set-option -g default-terminal tmux-256color` overrides tmux's
@@ -90,9 +93,10 @@ impl Tmux {
/// default `exit-empty on`.
pub fn new_session(&self, id: Uuid, cwd: &Path, command: &str) -> Result<(), TmuxError> {
let name = session_name(id);
let command = login_shell_command(env::var_os("SHELL").as_deref(), command);
let out = self
.cmd()
.args(new_session_args(&name, cwd, command, None))
.args(new_session_args(&name, cwd, &command, None))
.output()?;
check(out)
}
@@ -111,9 +115,10 @@ impl Tmux {
sink_command: &str,
) -> Result<(), TmuxError> {
let name = session_name(id);
let command = login_shell_command(env::var_os("SHELL").as_deref(), command);
let out = self
.cmd()
.args(new_session_args(&name, cwd, command, Some(sink_command)))
.args(new_session_args(&name, cwd, &command, Some(sink_command)))
.output()?;
check(out)
}
@@ -294,6 +299,49 @@ pub fn session_name(id: Uuid) -> String {
format!("{SESSION_PREFIX}{id}")
}
/// Wrap a session command so it runs through the user's login + interactive
/// shell.
///
/// `maestrod` usually runs from a stripped systemd user unit whose `PATH`
/// lacks per-user toolchain dirs (`~/.local/bin`, `~/.opencode/bin`, …). tmux
/// runs a new-session command via `$SHELL -c` *non-interactively*, so a bare
/// command like `claude` or `opencode` — which lives only on the PATH the user
/// exports from `~/.zshrc` / `~/.bashrc` — fails with "command not found".
/// Re-execing an interactive login shell sources that rc first, restoring the
/// PATH the user sees in a normal terminal. `exec` keeps the process tree flat
/// so pane-command inference still sees the real child.
///
/// Falls back to the bare command when `$SHELL` is unset/empty (nothing better
/// to wrap with).
fn login_shell_command(shell: Option<&OsStr>, command: &str) -> String {
match shell {
Some(shell) if !shell.is_empty() => {
format!(
"exec {} -lic {}",
shell.to_string_lossy(),
single_quote(command)
)
}
_ => command.to_string(),
}
}
/// POSIX single-quote a string: wrap in `'…'`, rewriting embedded `'` as
/// `'\''`. Safe to splice into a shell command line.
fn single_quote(s: &str) -> String {
let mut out = String::with_capacity(s.len() + 2);
out.push('\'');
for ch in s.chars() {
if ch == '\'' {
out.push_str("'\\''");
} else {
out.push(ch);
}
}
out.push('\'');
out
}
/// Args passed to `tmux` for `new_session` / `new_session_piped`, excluding
/// the binary itself and any socket/tmpdir flags injected by [`Tmux::cmd`].
/// When `pipe_sink` is `Some`, a chained `pipe-pane -t <name> <sink>` is
@@ -393,6 +441,31 @@ mod tests {
);
}
#[test]
fn login_shell_command_wraps_in_interactive_login_shell() {
assert_eq!(
login_shell_command(Some(OsStr::new("/usr/bin/zsh")), "claude"),
"exec /usr/bin/zsh -lic 'claude'"
);
}
#[test]
fn login_shell_command_quotes_command_with_spaces_and_quotes() {
assert_eq!(
login_shell_command(Some(OsStr::new("/bin/bash")), "sh -c 'echo hi'"),
r#"exec /bin/bash -lic 'sh -c '\''echo hi'\'''"#
);
}
#[test]
fn login_shell_command_passes_through_without_shell() {
assert_eq!(login_shell_command(None, "claude"), "claude");
assert_eq!(
login_shell_command(Some(OsStr::new("")), "claude"),
"claude"
);
}
#[test]
fn new_session_args_chain_pipe_pane_when_sink_present() {
let args = new_session_args(