use std::io::{Read, Seek, SeekFrom};
use std::path::Path;
use std::process::Command as StdCommand;
use std::time::{Duration, Instant};
use tokio::time::sleep;
use tokio_util::sync::CancellationToken;
use rinne_core::worker::{emit, EventSink, ExecStatus, WorkerEvent};
use rinne_core::{Result, RinneError};
use super::subprocess::{LineMapper, SubprocessOutput, SubprocessSpec};
const MAX_CAPTURE_BYTES: usize = 8 * 1024 * 1024;
const LAUNCHER_MARKER: &str = "rinne-launcher:";
const POLL: Duration = Duration::from_millis(100);
const RESULT_STABLE: Duration = Duration::from_millis(1200);
const TERM_GRACE: Duration = Duration::from_secs(4);
const WATCHDOG_POLL_SECS: u64 = 2;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TerminalMode {
Capture,
Interactive,
}
pub async fn run(
spec: SubprocessSpec,
events: &EventSink,
cancel: &CancellationToken,
mapper: LineMapper,
) -> Result<SubprocessOutput> {
let mode = if looks_like_single_turn(&spec) && spec.result_file.is_none() {
TerminalMode::Capture
} else {
TerminalMode::Interactive
};
run_with_mode(spec, events, cancel, mapper, mode).await
}
pub async fn run_with_mode(
spec: SubprocessSpec,
events: &EventSink,
cancel: &CancellationToken,
mapper: LineMapper,
mode: TerminalMode,
) -> Result<SubprocessOutput> {
let started = Instant::now();
let suffix = unique_suffix();
let stage_tag = format!("rinne-stage-{}-{}", std::process::id(), suffix);
let scratch = std::env::temp_dir().join(&stage_tag);
std::fs::create_dir_all(&scratch)
.map_err(|e| RinneError::Worker(format!("external terminal scratch dir: {e}")))?;
let log_path = scratch.join("harness.log");
let exit_path = scratch.join("exit.code");
let pid_path = scratch.join("wrapper.pid");
let tty_path = scratch.join("tty.name");
let tag_path = scratch.join("stage.tag");
let win_path = scratch.join("window.id");
let script_path = scratch.join("run-harness.command");
std::fs::write(&log_path, b"").ok();
let _ = std::fs::write(&tag_path, &stage_tag);
let _ = std::fs::remove_file(&exit_path);
write_launcher(
&script_path,
&spec,
&log_path,
&exit_path,
&pid_path,
&tty_path,
&stage_tag,
mode,
)?;
emit(
events,
WorkerEvent::Message(match mode {
TerminalMode::Capture => "opening single-turn harness in system Terminal…".into(),
TerminalMode::Interactive => "opening harness product UI in system Terminal…".into(),
}),
);
open_system_terminal(&script_path, &spec.workspace, &stage_tag, &win_path)?;
let deadline = spec
.timeout
.map(|d| Instant::now() + d)
.unwrap_or_else(|| Instant::now() + Duration::from_secs(3600));
wait_for_file(&pid_path, cancel, deadline).await?;
let mut log_file = std::fs::File::open(&log_path).ok();
let mut log_pos: u64 = 0;
let mut captured = String::new();
let mut truncated = false;
let mut line_buf = String::new();
let mut terminal_status: Option<ExecStatus> = None;
let mut got_result_file = false;
let mut last_result_size: u64 = 0;
let mut result_stable_since: Option<Instant> = None;
let mut stage_lines_emitted: usize = 0;
loop {
if cancel.is_cancelled() {
soft_stop(&pid_path, &exit_path).await;
ensure_stage_window_closed(&tty_path, &tag_path, &win_path).await;
terminal_status = Some(ExecStatus::Cancelled);
break;
}
if Instant::now() >= deadline {
soft_stop(&pid_path, &exit_path).await;
ensure_stage_window_closed(&tty_path, &tag_path, &win_path).await;
terminal_status = Some(ExecStatus::TimedOut);
break;
}
if let Some(ref rf) = spec.result_file {
if let Ok(partial) = std::fs::read_to_string(rf) {
if !partial.is_empty() {
stream_result_to_stage(events, &partial, &mut stage_lines_emitted);
}
}
if let Some(text) = stable_result(rf, &mut last_result_size, &mut result_stable_since) {
captured = text;
got_result_file = true;
stream_result_to_stage(events, &captured, &mut stage_lines_emitted);
emit(
events,
WorkerEvent::Message("✓ deliverable ready — closing Terminal".into()),
);
soft_stop(&pid_path, &exit_path).await;
ensure_stage_window_closed(&tty_path, &tag_path, &win_path).await;
terminal_status = Some(ExecStatus::Success);
break;
}
}
if let Some(ref mut lf) = log_file {
if lf.seek(SeekFrom::Start(log_pos)).is_ok() {
let mut buf = Vec::new();
if lf.read_to_end(&mut buf).is_ok() && !buf.is_empty() {
log_pos += buf.len() as u64;
let chunk = String::from_utf8_lossy(&buf);
for ch in chunk.chars() {
if ch == '\n' {
flush_line(
&mut line_buf,
&mut captured,
&mut truncated,
mapper,
events,
);
} else if ch != '\r' {
line_buf.push(ch);
}
}
}
}
}
if exit_path.exists() {
if !line_buf.is_empty() {
flush_line(&mut line_buf, &mut captured, &mut truncated, mapper, events);
}
if let Some(ref rf) = spec.result_file {
if let Ok(text) = std::fs::read_to_string(rf) {
let text = text.trim().to_string();
if !text.is_empty() {
captured = text;
got_result_file = true;
stream_result_to_stage(events, &captured, &mut stage_lines_emitted);
}
}
}
ensure_stage_window_closed(&tty_path, &tag_path, &win_path).await;
break;
}
sleep(POLL).await;
}
if !got_result_file {
if let Ok(meta) = std::fs::metadata(&log_path) {
if meta.len() > log_pos {
if let Ok(mut f) = std::fs::File::open(&log_path) {
let _ = f.seek(SeekFrom::Start(log_pos));
let mut rest = String::new();
let _ = f.read_to_string(&mut rest);
for ch in rest.chars() {
if ch == '\n' {
flush_line(
&mut line_buf,
&mut captured,
&mut truncated,
mapper,
events,
);
} else if ch != '\r' {
line_buf.push(ch);
}
}
if !line_buf.is_empty() {
flush_line(&mut line_buf, &mut captured, &mut truncated, mapper, events);
}
}
}
}
if let Some(ref rf) = spec.result_file {
if let Ok(text) = std::fs::read_to_string(rf) {
let text = text.trim().to_string();
if !text.is_empty() {
captured = text;
got_result_file = true;
stream_result_to_stage(events, &captured, &mut stage_lines_emitted);
}
}
}
}
if got_result_file || !captured.trim().is_empty() {
stream_result_to_stage(events, &captured, &mut stage_lines_emitted);
}
let exit_code = std::fs::read_to_string(&exit_path)
.ok()
.and_then(|s| s.trim().parse::<i32>().ok());
let status = terminal_status.unwrap_or_else(|| {
resolve_status(
spec.result_file.is_some(),
got_result_file,
captured.trim().is_empty(),
exit_code,
cancel.is_cancelled(),
)
});
ensure_stage_window_closed(&tty_path, &tag_path, &win_path).await;
if matches!(status, ExecStatus::Success) {
let _ = std::fs::remove_dir_all(&scratch);
} else {
emit(
events,
WorkerEvent::Message(format!(
"session artifacts kept at {} (exit {:?})",
scratch.display(),
exit_code
)),
);
}
emit(events, WorkerEvent::Done);
Ok(SubprocessOutput {
stdout: captured,
stderr: String::new(),
exit_code: if got_result_file { Some(0) } else { exit_code },
status,
wall_ms: started.elapsed().as_millis() as u64,
})
}
fn stream_result_to_stage(events: &EventSink, text: &str, emitted: &mut usize) {
const MAX_STAGE_LINES: usize = 250;
let lines: Vec<&str> = text.lines().collect();
if lines.is_empty() {
return;
}
if *emitted == 0 {
emit(events, WorkerEvent::Message("── harness output ──".into()));
}
while *emitted < lines.len() && *emitted < MAX_STAGE_LINES {
let line = lines[*emitted];
*emitted += 1;
if line.is_empty() || is_terminal_noise(line) {
continue;
}
let shown = if line.chars().count() > 400 {
let take: String = line.chars().take(400).collect();
format!("{take}…")
} else {
line.to_string()
};
emit(events, WorkerEvent::Message(shown));
}
if lines.len() > MAX_STAGE_LINES && *emitted == MAX_STAGE_LINES {
emit(
events,
WorkerEvent::Message(format!(
"… ({} more lines truncated in Stage)",
lines.len() - MAX_STAGE_LINES
)),
);
*emitted = lines.len(); }
}
fn stable_result(
path: &Path,
last_size: &mut u64,
stable_since: &mut Option<Instant>,
) -> Option<String> {
let meta = std::fs::metadata(path).ok()?;
let len = meta.len();
if len == 0 {
return None;
}
if len != *last_size {
*last_size = len;
*stable_since = Some(Instant::now());
return None;
}
let since = stable_since.get_or_insert_with(Instant::now);
if since.elapsed() < RESULT_STABLE {
return None;
}
let text = std::fs::read_to_string(path).ok()?;
let text = text.trim().to_string();
if text.is_empty() {
return None;
}
if is_terminal_noise(&text) {
return None;
}
Some(text)
}
fn looks_like_single_turn(spec: &SubprocessSpec) -> bool {
spec.args.iter().any(|a| {
matches!(
a.as_str(),
"-p" | "--print" | "--single" | "--output-format" | "--prompt-file" | "exec" | "run"
) || a.starts_with("--output-format=")
|| a.starts_with("--prompt-file=")
})
}
fn write_launcher(
script_path: &Path,
spec: &SubprocessSpec,
log_path: &Path,
exit_path: &Path,
pid_path: &Path,
tty_path: &Path,
stage_tag: &str,
mode: TerminalMode,
) -> Result<()> {
let mut script = String::new();
script.push_str("#!/bin/bash\n");
script.push_str("set -u\n");
script.push_str(&format!(
"cd {} || exit 90\n",
sh_quote(&spec.workspace.display().to_string())
));
script.push_str("export TERM=\"${TERM:-xterm-256color}\"\n");
script.push_str("export COLORTERM=\"${COLORTERM:-truecolor}\"\n");
script.push_str("export DISABLE_AUTO_UPDATE=true\n");
script.push_str(&format!("export RINNE_STAGE_TAG={}\n", sh_quote(stage_tag)));
for (k, v) in &spec.env {
script.push_str(&format!("export {}={}\n", k, sh_quote(v)));
}
let pid_q = sh_quote(&pid_path.display().to_string());
let exit_q = sh_quote(&exit_path.display().to_string());
let log_q = sh_quote(&log_path.display().to_string());
let tty_q = sh_quote(&tty_path.display().to_string());
let tag_q = sh_quote(stage_tag);
let sleep_pid_q = sh_quote(
&pid_path
.with_file_name("watchdog-sleep.pid")
.display()
.to_string(),
);
script.push_str("reset_tty() {\n");
script.push_str(" if [ -w /dev/tty ]; then\n");
script.push_str(" printf '\\033[?1003l\\033[?1002l\\033[?1000l\\033[?1006l\\033[?1015l\\033[?1005l' >/dev/tty 2>/dev/null || true\n");
script.push_str(" printf '\\033[?25h\\033[?1049l\\033[0m' >/dev/tty 2>/dev/null || true\n");
script.push_str(" fi\n");
script.push_str("}\n");
script.push_str("set_stage_title() {\n");
script.push_str(&format!(" TAG={tag_q}\n"));
script.push_str(" # OSC 0/2: icon + window title (Terminal.app / iTerm honor these).\n");
script.push_str(" if [ -w /dev/tty ]; then\n");
script.push_str(" printf '\\033]0;%s\\007' \"$TAG\" >/dev/tty 2>/dev/null || true\n");
script.push_str(" printf '\\033]2;%s\\007' \"$TAG\" >/dev/tty 2>/dev/null || true\n");
script.push_str(" fi\n");
script.push_str(" printf '\\033]0;%s\\007' \"$TAG\" 2>/dev/null || true\n");
script.push_str(" printf '\\033]2;%s\\007' \"$TAG\" 2>/dev/null || true\n");
script.push_str("}\n");
script.push_str("on_exit() {\n");
script.push_str(" ec=$?\n");
script.push_str(&format!(
" if [ -n \"${{RINNE_WATCHDOG:-}}\" ]; then\n\
\x20 kill \"$RINNE_WATCHDOG\" 2>/dev/null || true\n\
\x20 kill \"$(cat {} 2>/dev/null)\" 2>/dev/null || true\n\
\x20 wait \"$RINNE_WATCHDOG\" 2>/dev/null || true\n\
\x20 fi\n",
sleep_pid_q
));
script.push_str(" reset_tty\n");
script.push_str(" set_stage_title\n");
script.push_str(&format!(" echo \"$ec\" > {exit_q}\n"));
script.push_str(&format!(
" echo \"{LAUNCHER_MARKER} harness exit $ec\" >> {log_q} 2>/dev/null || true\n"
));
script.push_str("}\n");
script.push_str("trap on_exit EXIT\n");
script.push_str("trap 'exit 143' TERM\n");
script.push_str("trap 'exit 130' INT\n");
script.push_str(&format!("echo $$ > {pid_q}\n"));
script.push_str(&format!(
"RINNE_PID={}\n\
(\n\
\x20 while kill -0 \"$RINNE_PID\" 2>/dev/null; do\n\
\x20 sleep {} & echo $! > {}; wait $!\n\
\x20 done\n\
\x20 pkill -TERM -P $$ 2>/dev/null || true\n\
) </dev/null >/dev/null 2>&1 &\n\
RINNE_WATCHDOG=$!\n",
std::process::id(),
WATCHDOG_POLL_SECS,
sleep_pid_q
));
script.push_str("TTY_NAME=$(tty 2>/dev/null | sed 's|^/dev/||' || true)\n");
script.push_str(&format!(
"[ -n \"${{TTY_NAME:-}}\" ] && echo \"$TTY_NAME\" > {tty_q} || true\n"
));
script.push_str("set_stage_title\n");
script.push_str("reset_tty\n");
script.push_str("set_stage_title\n");
script.push_str("cmd=(\n");
script.push_str(&format!(" {}\n", sh_quote(&spec.program)));
for a in &spec.args {
script.push_str(&format!(" {}\n", sh_quote(a)));
}
script.push_str(")\n");
let prompt_file = script_path
.parent()
.unwrap_or(Path::new("/tmp"))
.join("prompt.txt");
if let Some(ref input) = spec.stdin {
let _ = std::fs::write(&prompt_file, input);
}
match mode {
TerminalMode::Interactive => {
script.push_str("if [ -w /dev/tty ]; then\n");
script.push_str(" printf '\\033[2J\\033[H' >/dev/tty 2>/dev/null || true\n");
script.push_str("fi\n");
if spec.stdin.is_some() {
script.push_str(&format!(
"\"${{cmd[@]}}\" < {}\n",
sh_quote(&prompt_file.display().to_string())
));
} else {
script.push_str("\"${cmd[@]}\"\n");
}
script.push_str("ec=$?\n");
script.push_str("exit \"$ec\"\n");
}
TerminalMode::Capture => {
script.push_str("echo \"Rinne single-turn session\" >> ");
script.push_str(&log_q);
script.push_str("\n");
if spec.stdin.is_some() {
script.push_str(&format!(
"\"${{cmd[@]}}\" < {} 2>&1 | tee -a {}\n",
sh_quote(&prompt_file.display().to_string()),
log_q
));
} else {
script.push_str(&format!("\"${{cmd[@]}}\" 2>&1 | tee -a {}\n", log_q));
}
script.push_str("ec=${PIPESTATUS[0]}\n");
script.push_str("exit \"$ec\"\n");
}
}
std::fs::write(script_path, script)
.map_err(|e| RinneError::Worker(format!("write terminal launcher: {e}")))?;
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let mut perms = std::fs::metadata(script_path)
.map_err(|e| RinneError::Worker(format!("stat launcher: {e}")))?
.permissions();
perms.set_mode(0o755);
std::fs::set_permissions(script_path, perms)
.map_err(|e| RinneError::Worker(format!("chmod launcher: {e}")))?;
}
Ok(())
}
fn open_system_terminal(
script_path: &Path,
workspace: &Path,
stage_tag: &str,
win_path: &Path,
) -> Result<()> {
let script = script_path.display().to_string();
let cwd = workspace.display().to_string();
let prefer = std::env::var("RINNE_EXTERNAL_TERMINAL")
.unwrap_or_default()
.to_ascii_lowercase();
#[cfg(target_os = "macos")]
{
if choose_macos_terminal(&prefer) == MacTerminal::ITerm {
return open_iterm(&script, &cwd, stage_tag, win_path);
}
if open_macos_terminal_capture_id(&script, stage_tag, win_path).is_ok() {
return Ok(());
}
if script_path
.extension()
.and_then(|e| e.to_str())
.is_some_and(|e| e == "command")
{
return open_macos_command_file(script_path);
}
return open_macos_terminal_capture_id(&script, stage_tag, win_path);
}
#[cfg(not(target_os = "macos"))]
{
let _ = (cwd, stage_tag, win_path);
if prefer == "kitty" || which("kitty") {
return run_detached("kitty", &["--title", stage_tag, "bash", &script]);
}
if prefer == "gnome" || which("gnome-terminal") {
return run_detached(
"gnome-terminal",
&["--title", stage_tag, "--", "bash", &script],
);
}
if which("x-terminal-emulator") {
return run_detached("x-terminal-emulator", &["-e", "bash", &script]);
}
if which("xterm") {
return run_detached("xterm", &["-T", stage_tag, "-e", "bash", &script]);
}
Err(RinneError::Worker(
"no system terminal found (install gnome-terminal, xterm, or kitty; \
or set RINNE_EXTERNAL_TERMINAL)"
.into(),
))
}
}
#[cfg(target_os = "macos")]
fn open_macos_command_file(script_path: &Path) -> Result<()> {
let status = StdCommand::new("open")
.arg("-a")
.arg("Terminal")
.arg(script_path)
.status()
.map_err(|e| RinneError::Worker(format!("open Terminal: {e}")))?;
if status.success() {
Ok(())
} else {
Err(RinneError::Worker(format!(
"open Terminal failed with {status}"
)))
}
}
#[cfg(target_os = "macos")]
fn open_macos_terminal_capture_id(script: &str, stage_tag: &str, win_path: &Path) -> Result<()> {
let apple = format!(
r#"tell application "Terminal"
activate
set newTab to do script "exec bash {script}"
delay 0.35
try
set custom title of newTab to "{tag}"
end try
try
set wid to id of front window
return wid as text
on error
return ""
end try
end tell"#,
script = sh_quote(script).replace('\\', "\\\\"),
tag = stage_tag.replace('\\', "\\\\").replace('"', "\\\"")
);
let output = StdCommand::new("osascript")
.arg("-e")
.arg(&apple)
.output()
.map_err(|e| RinneError::Worker(format!("osascript Terminal: {e}")))?;
if !output.status.success() {
return Err(RinneError::Worker(format!(
"osascript Terminal failed with {}",
output.status
)));
}
let wid = String::from_utf8_lossy(&output.stdout).trim().to_string();
if !wid.is_empty() {
let _ = std::fs::write(win_path, &wid);
}
Ok(())
}
#[cfg(target_os = "macos")]
fn open_iterm(script: &str, _cwd: &str, stage_tag: &str, win_path: &Path) -> Result<()> {
let apple = format!(
r#"tell application "iTerm"
activate
try
tell current window
create tab with default profile
tell current session
write text "exec bash {script}"
set name to "{tag}"
end tell
try
set wid to id
return wid as text
end try
end tell
on error
create window with default profile
tell current session of current window
write text "exec bash {script}"
set name to "{tag}"
end tell
try
set wid to id of current window
return wid as text
end try
end try
return ""
end tell"#,
script = sh_quote(script),
tag = stage_tag.replace('\\', "\\\\").replace('"', "\\\"")
);
let output = StdCommand::new("osascript")
.arg("-e")
.arg(&apple)
.output()
.map_err(|e| RinneError::Worker(format!("osascript iTerm: {e}")))?;
if output.status.success() {
let wid = String::from_utf8_lossy(&output.stdout).trim().to_string();
if !wid.is_empty() {
let _ = std::fs::write(win_path, &wid);
}
Ok(())
} else {
open_macos_terminal_capture_id(script, stage_tag, win_path)
}
}
fn which(bin: &str) -> bool {
let Some(path_var) = std::env::var_os("PATH") else {
return false;
};
let exts: Vec<String> = if cfg!(windows) {
std::env::var("PATHEXT")
.unwrap_or_else(|_| ".EXE;.CMD;.BAT;.COM".to_string())
.split(';')
.map(|s| s.to_string())
.collect()
} else {
vec![String::new()]
};
for dir in std::env::split_paths(&path_var) {
for ext in &exts {
let candidate = dir.join(format!("{bin}{ext}"));
if candidate.is_file() {
return true;
}
}
}
false
}
#[cfg(not(target_os = "macos"))]
fn run_detached(program: &str, args: &[&str]) -> Result<()> {
StdCommand::new(program)
.args(args)
.stdin(std::process::Stdio::null())
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.spawn()
.map_err(|e| RinneError::Worker(format!("spawn {program}: {e}")))?;
Ok(())
}
async fn wait_for_file(path: &Path, cancel: &CancellationToken, deadline: Instant) -> Result<()> {
loop {
if cancel.is_cancelled() {
return Err(RinneError::Worker(
"cancelled before terminal started".into(),
));
}
if Instant::now() >= deadline {
return Err(RinneError::Worker(
"timeout waiting for external terminal to start".into(),
));
}
if path.exists() {
return Ok(());
}
sleep(POLL).await;
}
}
async fn ensure_stage_window_closed(tty_path: &Path, tag_path: &Path, win_path: &Path) {
let tag = std::fs::read_to_string(tag_path)
.unwrap_or_default()
.trim()
.to_string();
let tty = std::fs::read_to_string(tty_path)
.unwrap_or_default()
.trim()
.trim_start_matches("/dev/")
.to_string();
let win_id = std::fs::read_to_string(win_path)
.unwrap_or_default()
.trim()
.to_string();
for attempt in 0..6 {
close_stage_window_once(&tty, &tag, &win_id);
if !stage_window_still_open(&tag, &win_id) {
return;
}
sleep(Duration::from_millis(150 + attempt * 100)).await;
}
close_stage_window_once(&tty, &tag, &win_id);
close_stage_window_once(&tty, &tag, &win_id);
}
fn close_stage_window_once(tty: &str, tag: &str, win_id: &str) {
#[cfg(target_os = "macos")]
{
if !win_id.is_empty() && win_id.chars().all(|c| c.is_ascii_digit()) {
let script = format!(
r#"tell application "Terminal"
try
close (every window whose id is {win_id}) saving no
end try
end tell"#
);
run_osascript(&script);
let script = format!(
r#"tell application "iTerm"
try
repeat with w in windows
try
if (id of w as text) is "{win_id}" then close w
end try
end repeat
end try
end tell"#
);
run_osascript(&script);
}
if !tag.is_empty() {
let tag_esc = tag.replace('\\', "\\\\").replace('"', "\\\"");
let script = format!(
r#"tell application "Terminal"
try
set wins to windows whose name contains "{tag_esc}"
repeat with w in wins
try
close w saving no
end try
end repeat
end try
end tell"#
);
run_osascript(&script);
let script = format!(
r#"tell application "iTerm"
try
repeat with w in windows
repeat with t in tabs of w
repeat with s in sessions of t
try
if name of s contains "{tag_esc}" then close s
end try
end repeat
end repeat
try
if name of w contains "{tag_esc}" then close w
end try
end repeat
end try
end tell"#
);
run_osascript(&script);
let script = format!(
r#"tell application "System Events"
if exists process "Terminal" then
tell process "Terminal"
set frontmost to true
repeat with w in windows
try
if name of w contains "{tag_esc}" then
perform action "AXRaise" of w
keystroke "w" using command down
delay 0.12
keystroke return
end if
end try
end repeat
end tell
end if
if exists process "iTerm2" then
tell process "iTerm2"
set frontmost to true
repeat with w in windows
try
if name of w contains "{tag_esc}" then
perform action "AXRaise" of w
keystroke "w" using command down
delay 0.12
keystroke return
end if
end try
end repeat
end tell
end if
end tell"#
);
run_osascript(&script);
}
if !tty.is_empty() {
let script = format!(
r#"tell application "Terminal"
repeat with w in windows
try
set t to tty of selected tab of w
if t is "{tty}" or t is "/dev/{tty}" then
close w saving no
end if
end try
end repeat
end tell"#
);
run_osascript(&script);
let script = format!(
r#"tell application "iTerm"
repeat with w in windows
repeat with t in tabs of w
repeat with s in sessions of t
try
if tty of s contains "{tty}" then close s
end try
end repeat
end repeat
end repeat
end tell"#
);
run_osascript(&script);
}
}
#[cfg(not(target_os = "macos"))]
{
if !tag.is_empty() {
let _ = StdCommand::new("wmctrl")
.args(["-c", tag])
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.status();
let _ = StdCommand::new("xdotool")
.args(["search", "--name", tag, "windowclose", "%@"])
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.status();
}
let _ = tty;
let _ = win_id;
}
}
fn stage_window_still_open(tag: &str, win_id: &str) -> bool {
if tag.is_empty() && win_id.is_empty() {
return false;
}
#[cfg(target_os = "macos")]
{
let tag_esc = tag.replace('\\', "\\\\").replace('"', "\\\"");
let mut checks = String::new();
if !tag.is_empty() {
checks.push_str(&format!(
r#"
tell application "Terminal"
try
set n to count of (windows whose name contains "{tag_esc}")
if n > 0 then return "yes"
end try
end tell
"#
));
}
if !win_id.is_empty() && win_id.chars().all(|c| c.is_ascii_digit()) {
checks.push_str(&format!(
r#"
tell application "Terminal"
try
set n to count of (windows whose id is {win_id})
if n > 0 then return "yes"
end try
end tell
"#
));
}
checks.push_str(r#"return "no""#);
let output = StdCommand::new("osascript").arg("-e").arg(&checks).output();
if let Ok(out) = output {
let s = String::from_utf8_lossy(&out.stdout);
return s.contains("yes");
}
return true;
}
#[cfg(not(target_os = "macos"))]
{
if tag.is_empty() {
return false;
}
if let Ok(out) = StdCommand::new("wmctrl").args(["-l"]).output() {
let s = String::from_utf8_lossy(&out.stdout);
return s.contains(tag);
}
false
}
}
fn run_osascript(script: &str) {
let _ = StdCommand::new("osascript")
.arg("-e")
.arg(script)
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.status();
}
async fn soft_stop(pid_path: &Path, exit_path: &Path) {
let Some(pid) = read_pid(pid_path) else {
return;
};
#[cfg(unix)]
{
unsafe {
for child in descendants_deepest_first(pid, &live_child_pids) {
libc::kill(child, libc::SIGTERM);
}
libc::kill(pid, libc::SIGTERM);
}
let deadline = Instant::now() + TERM_GRACE;
while Instant::now() < deadline {
#[cfg(unix)]
{
let alive = unsafe { libc::kill(pid, 0) == 0 };
if !alive {
if !exit_path.exists() {
let _ = std::fs::write(exit_path, b"143");
}
return;
}
}
sleep(Duration::from_millis(100)).await;
}
unsafe {
for child in descendants_deepest_first(pid, &live_child_pids) {
libc::kill(child, libc::SIGKILL);
}
libc::kill(pid, libc::SIGKILL);
}
if !exit_path.exists() {
let _ = std::fs::write(exit_path, b"137");
}
}
#[cfg(not(unix))]
{
let _ = (pid, exit_path);
}
}
fn read_pid(pid_path: &Path) -> Option<i32> {
std::fs::read_to_string(pid_path)
.ok()
.and_then(|s| s.trim().parse().ok())
}
fn flush_line(
line_buf: &mut String,
captured: &mut String,
truncated: &mut bool,
mapper: LineMapper,
events: &EventSink,
) {
let line = std::mem::take(line_buf);
if is_terminal_noise(&line) {
return;
}
if captured.len() < MAX_CAPTURE_BYTES {
captured.push_str(&line);
captured.push('\n');
} else if !*truncated {
*truncated = true;
captured.push_str("\n…[output truncated]\n");
}
for ev in mapper(&line) {
emit(events, ev);
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum MacTerminal {
Apple,
ITerm,
}
fn choose_macos_terminal(prefer: &str) -> MacTerminal {
match prefer.trim().to_ascii_lowercase().as_str() {
"iterm" | "iterm2" => MacTerminal::ITerm,
_ => MacTerminal::Apple,
}
}
fn live_child_pids(pid: i32) -> Vec<i32> {
let Ok(out) = StdCommand::new("pgrep")
.arg("-P")
.arg(pid.to_string())
.output()
else {
return Vec::new();
};
parse_pgrep(&String::from_utf8_lossy(&out.stdout))
}
fn parse_pgrep(stdout: &str) -> Vec<i32> {
stdout
.lines()
.filter_map(|l| l.trim().parse::<i32>().ok())
.filter(|p| *p > 0)
.collect()
}
fn descendants_deepest_first(root: i32, children_of: &dyn Fn(i32) -> Vec<i32>) -> Vec<i32> {
let mut seen = std::collections::HashSet::from([root]);
let mut queue = std::collections::VecDeque::from([root]);
let mut order: Vec<i32> = Vec::new();
while let Some(pid) = queue.pop_front() {
for child in children_of(pid) {
if child > 0 && seen.insert(child) {
order.push(child);
queue.push_back(child);
}
}
}
order.reverse();
order
}
fn resolve_status(
wanted_result: bool,
got_result_file: bool,
captured_empty: bool,
exit_code: Option<i32>,
cancelled: bool,
) -> ExecStatus {
if got_result_file && !captured_empty {
return ExecStatus::Success;
}
match exit_code {
Some(c) if c != 0 => return ExecStatus::Failed(format!("exited {c}")),
None if cancelled => return ExecStatus::Cancelled,
_ => {}
}
if wanted_result {
return ExecStatus::Failed(
"harness produced no deliverable — result file was never written".into(),
);
}
match exit_code {
Some(_) => ExecStatus::Success,
None => ExecStatus::Failed("harness terminal closed without exit code".into()),
}
}
fn is_terminal_noise(line: &str) -> bool {
let t = line.trim();
if t.is_empty() {
return false;
}
if t.starts_with(LAUNCHER_MARKER) {
return true;
}
if t.len() >= 4
&& t.chars()
.all(|c| c.is_ascii_digit() || matches!(c, ';' | 'M' | 'm'))
&& t.contains(';')
&& (t.contains('M') || t.ends_with('m'))
{
return true;
}
false
}
fn sh_quote(s: &str) -> String {
if s.is_empty() {
return "''".into();
}
let mut out = String::from("'");
for ch in s.chars() {
if ch == '\'' {
out.push_str("'\\''");
} else {
out.push(ch);
}
}
out.push('\'');
out
}
fn unique_suffix() -> u128 {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_nanos())
.unwrap_or(0)
}
#[cfg(test)]
mod tests {
use super::*;
fn tree<'a>(edges: &'a [(i32, &'a [i32])]) -> impl Fn(i32) -> Vec<i32> + 'a {
move |pid| {
edges
.iter()
.find(|(p, _)| *p == pid)
.map(|(_, kids)| kids.to_vec())
.unwrap_or_default()
}
}
#[test]
fn descendants_are_returned_leaves_before_parents() {
let f = tree(&[(10, &[20, 21]), (20, &[30])]);
let got = descendants_deepest_first(10, &f);
assert!(!got.contains(&10), "root must not be signalled: {got:?}");
let pos = |p: i32| got.iter().position(|x| *x == p).unwrap();
assert!(
pos(30) < pos(20),
"a child must be signalled before its parent"
);
assert_eq!(got.len(), 3, "{got:?}");
}
#[test]
fn an_explicit_terminal_preference_beats_an_installed_iterm() {
assert_eq!(choose_macos_terminal("terminal"), MacTerminal::Apple);
assert_eq!(choose_macos_terminal("terminal.app"), MacTerminal::Apple);
assert_eq!(choose_macos_terminal("iterm"), MacTerminal::ITerm);
assert_eq!(choose_macos_terminal("iterm2"), MacTerminal::ITerm);
}
#[test]
fn with_no_preference_the_system_terminal_is_used() {
assert_eq!(choose_macos_terminal(""), MacTerminal::Apple);
assert_eq!(choose_macos_terminal(" "), MacTerminal::Apple);
}
#[test]
fn an_unrecognised_preference_falls_back_to_terminal_app() {
assert_eq!(choose_macos_terminal("wezterm"), MacTerminal::Apple);
}
#[test]
fn launcher_never_closes_its_own_window_from_inside() {
let dir = std::env::temp_dir().join(format!("rinne-ext-noclose-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
let script = dir.join("run.command");
let spec = SubprocessSpec {
program: "claude".into(),
args: vec!["hello".into()],
workspace: dir.clone(),
stdin: None,
timeout: None,
env: vec![],
result_file: Some(dir.join("result.txt")),
};
write_launcher(
&script,
&spec,
&dir.join("h.log"),
&dir.join("e.code"),
&dir.join("w.pid"),
&dir.join("tty.name"),
"rinne-stage-noclose-tag",
TerminalMode::Interactive,
)
.unwrap();
let body = std::fs::read_to_string(&script).unwrap();
assert!(
!body.contains("osascript"),
"launcher must not run AppleScript inside its own window: {body}"
);
assert!(
!body.contains("close_window"),
"window closing belongs to Rinne, not the launcher: {body}"
);
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn launcher_reaps_the_harness_when_rinne_dies() {
let dir = std::env::temp_dir().join(format!("rinne-ext-wd-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
let script = dir.join("run.command");
let spec = SubprocessSpec {
program: "claude".into(),
args: vec!["hello".into()],
workspace: dir.clone(),
stdin: None,
timeout: None,
env: vec![],
result_file: Some(dir.join("result.txt")),
};
write_launcher(
&script,
&spec,
&dir.join("h.log"),
&dir.join("e.code"),
&dir.join("w.pid"),
&dir.join("tty.name"),
"rinne-stage-wd-tag",
TerminalMode::Interactive,
)
.unwrap();
let body = std::fs::read_to_string(&script).unwrap();
assert!(
body.contains(&format!("RINNE_PID={}", std::process::id())),
"launcher must know the Rinne pid that spawned it"
);
assert!(
body.contains("kill -0 \"$RINNE_PID\""),
"launcher must poll that pid for liveness"
);
assert_eq!(
body.lines()
.filter(|l| l.starts_with("trap ") && l.ends_with(" EXIT"))
.count(),
1,
"exactly one EXIT trap, or on_exit is silently replaced: {body}"
);
assert!(
body.contains("pkill -TERM -P $$"),
"watchdog must signal children so on_exit still resets the TTY"
);
assert!(
body.contains("watchdog-sleep.pid"),
"watchdog must record its sleep pid so on_exit can reap it: {body}"
);
assert!(
body.contains("</dev/null >/dev/null 2>&1 &"),
"watchdog stdio must be detached from the window's tty: {body}"
);
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn pgrep_output_parses_and_ignores_junk() {
assert_eq!(parse_pgrep("120\n131\n"), vec![120, 131]);
assert_eq!(parse_pgrep(""), Vec::<i32>::new());
assert_eq!(parse_pgrep("\n \nnot-a-pid\n7\n"), vec![7]);
assert_eq!(parse_pgrep("0\n-1\n9"), vec![9]);
}
#[test]
fn which_finds_a_real_binary_and_rejects_garbage() {
#[cfg(unix)]
assert!(which("sh"), "sh should be on PATH");
#[cfg(windows)]
assert!(which("cmd"), "cmd should be on PATH");
assert!(!which("rinne-definitely-not-a-binary-xyzzy"));
}
#[test]
fn descendants_of_a_leaf_is_empty() {
let f = tree(&[]);
assert!(descendants_deepest_first(99, &f).is_empty());
}
#[test]
fn descendants_terminates_on_a_cycle() {
let f = tree(&[(1, &[2]), (2, &[1])]);
let got = descendants_deepest_first(1, &f);
assert_eq!(got, vec![2], "{got:?}");
}
#[test]
fn a_missing_deliverable_fails_even_when_the_harness_exits_clean() {
let status = resolve_status(true, false, true, Some(0), false);
assert!(
matches!(status, ExecStatus::Failed(_)),
"expected Failed, got {status:?}"
);
}
#[test]
fn a_produced_deliverable_succeeds() {
assert!(matches!(
resolve_status(true, true, false, Some(0), false),
ExecStatus::Success
));
}
#[test]
fn capture_sessions_still_succeed_on_a_clean_exit() {
assert!(matches!(
resolve_status(false, false, false, Some(0), false),
ExecStatus::Success
));
}
#[test]
fn launcher_bookkeeping_is_not_harness_output() {
assert!(is_terminal_noise("rinne-launcher: harness exit 0"));
assert!(is_terminal_noise(" rinne-launcher: harness exit 143 "));
assert!(!is_terminal_noise("editing src/main.rs"));
assert!(!is_terminal_noise(
"the launcher script is described in rinne-launcher docs"
));
}
#[test]
fn sh_quote_handles_spaces_and_quotes() {
assert_eq!(sh_quote("hello"), "'hello'");
assert_eq!(sh_quote("a b"), "'a b'");
assert!(sh_quote("it's").contains("\\'"));
}
#[test]
fn interactive_launcher_resets_tty_and_avoids_script_tee() {
let dir = std::env::temp_dir().join(format!("rinne-ext-int-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
let script = dir.join("run.command");
let log = dir.join("h.log");
let exit = dir.join("e.code");
let pid = dir.join("w.pid");
let result = dir.join("result.txt");
let spec = SubprocessSpec {
program: "grok".into(),
args: vec![
"--fullscreen".into(),
"--always-approve".into(),
"hello".into(),
],
workspace: dir.clone(),
stdin: None,
timeout: None,
env: vec![],
result_file: Some(result),
};
let tty = dir.join("tty.name");
write_launcher(
&script,
&spec,
&log,
&exit,
&pid,
&tty,
"rinne-stage-test-tag",
TerminalMode::Interactive,
)
.unwrap();
let body = std::fs::read_to_string(&script).unwrap();
assert!(body.contains("reset_tty"), "must reset mouse tracking");
assert!(body.contains("1000l"), "must disable mouse mode 1000");
assert!(
body.contains("set_stage_title"),
"must title window for close-by-name"
);
assert!(body.contains("rinne-stage-test-tag"));
assert!(body.contains("trap on_exit EXIT"));
assert!(!body.contains("script -q"), "must not use script(1)");
assert!(
!body.contains("tee -a") || body.contains("Capture"),
"interactive must not tee"
);
assert!(!body.contains("set -m"), "no job-control pgid races");
assert!(!body.contains("Rinne → harness session"));
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn capture_launcher_uses_tee_not_script() {
let dir = std::env::temp_dir().join(format!("rinne-ext-cap-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
let script = dir.join("run.command");
let log = dir.join("h.log");
let exit = dir.join("e.code");
let pid = dir.join("w.pid");
let tty = dir.join("tty.name");
let spec = SubprocessSpec {
program: "echo".into(),
args: vec!["-p".into(), "hi".into()],
workspace: dir.clone(),
stdin: None,
timeout: Some(Duration::from_secs(5)),
env: vec![],
result_file: None,
};
write_launcher(
&script,
&spec,
&log,
&exit,
&pid,
&tty,
"rinne-stage-cap-tag",
TerminalMode::Capture,
)
.unwrap();
let body = std::fs::read_to_string(&script).unwrap();
assert!(body.contains("tee"));
assert!(!body.contains("script -q"));
assert!(body.contains("rinne-stage-cap-tag"));
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn mouse_noise_filtered() {
assert!(is_terminal_noise("35;6M35;35;7M65;35;7M"));
assert!(is_terminal_noise("36;18M35;37;18M"));
assert!(!is_terminal_noise("planning complete"));
assert!(!is_terminal_noise(r#"{"goal":"x"}"#));
}
}