use std::collections::HashMap;
use taimux_core::{conv, state, tmux};
pub fn transcript_is_settled(text: &str, now: i64) -> Result<(), String> {
let Some(last) = text.lines().rfind(|l| !l.trim().is_empty()) else {
return Ok(()); };
if taimux_core::json::field(last, "type") == "assistant"
&& (last.contains("\"type\":\"tool_use\"") || last.contains("\"type\": \"tool_use\""))
{
return Err("a tool call is still running".into());
}
let ts = taimux_core::json::field(last, "timestamp");
if ts.is_empty() {
return Ok(());
}
let Some(then) = parse_iso8601(&ts) else {
return Ok(()); };
if now - then < 45 {
return Err("active in the last 45s".into());
}
Ok(())
}
fn parse_iso8601(s: &str) -> Option<i64> {
taimux_core::turn::epoch_ms(s).map(|ms| ms.div_euclid(1000))
}
pub fn screen_has_no_dialog(screen: &str) -> Result<(), String> {
if state::awaits_input(screen) {
return Err("a dialog is waiting for an answer".into());
}
let tail: Vec<&str> = screen
.lines()
.filter(|l| !l.trim().is_empty())
.rev()
.take(4)
.collect();
if tail
.iter()
.any(|l| l.contains("Press Ctrl-C again to exit"))
{
return Err("a Ctrl-C is already half-pressed".into());
}
Ok(())
}
pub fn screen_has_no_draft(screen: &str) -> Result<(), String> {
let txt: Vec<&str> = screen.lines().filter(|l| !l.trim().is_empty()).collect();
if txt.is_empty() {
return Ok(());
}
let Some(box_line) = txt.iter().rev().find(|l| l.contains('❯')) else {
return Err("no prompt box on screen".into());
};
let after = box_line.split_once('❯').map(|(_, r)| r).unwrap_or("");
let rest: String = after
.chars()
.filter(|c| !c.is_whitespace() && *c != '\u{a0}')
.collect();
if rest.is_empty() {
Ok(())
} else {
Err("unsent text in the prompt box".into())
}
}
const BOX_ROWS: usize = 6;
type Cmd = Vec<String>;
type ZoomSteps = (Vec<Cmd>, Vec<Cmd>);
fn zoom_steps(
pane: &str,
zoomed: bool,
pane_rows: usize,
window_rows: usize,
active: &str,
last: &str,
) -> Option<ZoomSteps> {
if pane_rows >= BOX_ROWS || window_rows < BOX_ROWS || window_rows <= pane_rows {
return None;
}
let cmd = |a: &[&str]| a.iter().map(|s| s.to_string()).collect::<Cmd>();
let mut go = Vec::new();
if zoomed {
go.push(cmd(&["resize-pane", "-Z", "-t", pane]));
}
go.push(cmd(&["resize-pane", "-Z", "-t", pane]));
let mut back = vec![cmd(&["resize-pane", "-Z", "-t", pane])];
if active != pane {
if !last.is_empty() && last != pane {
back.push(cmd(&["select-pane", "-t", last]));
}
if !active.is_empty() {
back.push(cmd(&["select-pane", "-t", active]));
}
}
if zoomed && !active.is_empty() {
back.push(cmd(&["resize-pane", "-Z", "-t", active]));
}
Some((go, back))
}
struct Restoring(Vec<Cmd>);
impl Drop for Restoring {
fn drop(&mut self) {
for c in &self.0 {
tmux::run(&c.iter().map(String::as_str).collect::<Vec<_>>());
}
}
}
pub fn capture_zoomed(pane: &str) -> Option<String> {
if !taimux_core::env::on("TAIMUX_ZOOM_TO_READ") {
return None;
}
let geom = tmux::ask(&[
"display-message",
"-p",
"-t",
pane,
"-F",
"#{window_zoomed_flag}\t#{pane_height}\t#{window_height}\t#{window_id}",
])?;
let g: Vec<&str> = geom.trim_end().split('\t').collect();
if g.len() < 4 {
return None;
}
let (zoomed, win) = (g[0] == "1", g[3]);
let pane_rows: usize = g[1].parse().ok()?;
let window_rows: usize = g[2].parse().ok()?;
let mut active = String::new();
let mut last = String::new();
for l in tmux::ask(&[
"list-panes",
"-t",
win,
"-F",
"#{pane_id}\t#{pane_active}\t#{pane_last}",
])?
.lines()
{
let c: Vec<&str> = l.split('\t').collect();
if c.len() < 3 {
continue;
}
if c[1] == "1" {
active = c[0].to_string();
}
if c[2] == "1" {
last = c[0].to_string();
}
}
let (go, back) = zoom_steps(pane, zoomed, pane_rows, window_rows, &active, &last)?;
for c in &go {
tmux::run(&c.iter().map(String::as_str).collect::<Vec<_>>());
}
let _restore = Restoring(back);
let deadline = std::time::Instant::now() + std::time::Duration::from_millis(500);
loop {
let screen = tmux::capture(pane).unwrap_or_default();
if screen.contains('❯') || std::time::Instant::now() >= deadline {
return Some(screen);
}
std::thread::sleep(std::time::Duration::from_millis(10));
}
}
pub fn version_of_pid(pid: i32, versions_dir: &str) -> Option<String> {
let exe = std::fs::read_link(format!("/proc/{}/exe", pid)).ok()?;
let exe = exe.to_string_lossy();
let exe = exe.trim_end_matches(" (deleted)"); let prefix = format!("{}/", versions_dir);
exe.strip_prefix(&prefix)
.filter(|rest| !rest.is_empty())
.map(|rest| rest.split('/').next().unwrap_or(rest).to_string())
}
pub struct Planned {
pub pane: String,
pub target: String,
pub pid: i32,
pub cmd: String,
pub title: String,
pub via: String,
pub respawn: Option<String>,
}
pub struct PlannedBg {
pub id: String,
pub pid: i32,
pub name: String,
pub via: String,
}
pub struct Plan {
pub newver: String,
pub launcher: String,
pub go: Vec<Planned>,
pub go_bg: Vec<PlannedBg>,
pub skipped: Vec<String>,
pub unresolved: bool,
}
pub struct Opts {
pub include_busy: bool,
pub only_panes: Vec<String>,
pub force_transcript: Option<String>,
pub self_pane: String,
}
pub trait Env {
fn capture(&self, pane: &str) -> String;
fn capture_zoomed(&self, _pane: &str) -> Option<String> {
None
}
fn hook_state(&self, pane: &str, pid: i32) -> Option<String>;
fn hook_mode(&self, _pane: &str, _pid: i32) -> Option<String> {
None
}
fn own_state(&self, _pid: i32, _screen: &str) -> Option<state::State> {
None
}
fn elsewhere(&self, _pane: &str, _pid: i32) -> Option<Elsewhere> {
None
}
fn background(&self) -> Vec<BgSession> {
Vec::new()
}
fn version_of_pid(&self, pid: i32) -> Option<String>;
fn cwd_of(&self, pid: i32) -> Option<String>;
fn argv_of(&self, pid: i32) -> Vec<String>;
fn resolve(&self, pane: &str, cwd: &str, title: &str, pid: i32) -> Result<String, String>;
fn read_transcript(&self, path: &str) -> Option<String>;
fn now(&self) -> i64;
}
pub struct Elsewhere {
pub id: String,
pub pid: i32,
pub version: Option<String>,
pub transcript: Option<String>,
}
#[derive(Clone)]
pub struct BgSession {
pub id: String,
pub pid: i32,
pub version: Option<String>,
pub name: String,
pub state: Option<state::State>,
pub transcript: Option<String>,
pub in_flight: Option<taimux_core::status::InFlight>,
}
fn typable_id(id: &str) -> bool {
!id.is_empty() && id.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'-')
}
fn settled(screen: &str, transcript: Option<&str>, o: &Opts, e: &dyn Env) -> Result<(), String> {
screen_has_no_dialog(screen)?;
if o.include_busy {
return Ok(());
}
screen_has_no_draft(screen)?;
match transcript.and_then(|t| e.read_transcript(t)) {
Some(text) => transcript_is_settled(&text, e.now()),
None => Ok(()),
}
}
pub fn plan(
rows: &str,
newver: &str,
launcher: &str,
o: &Opts,
e: &dyn Env,
versions_dir: &str,
agents: &dyn Fn() -> String,
) -> Plan {
let mut p = Plan {
newver: newver.to_string(),
launcher: launcher.to_string(),
go: Vec::new(),
go_bg: Vec::new(),
skipped: Vec::new(),
unresolved: false,
};
let mut claimed: HashMap<String, String> = HashMap::new();
let mut matched: Vec<i32> = Vec::new();
for line in rows.lines() {
let f: Vec<&str> = line.split('\t').collect();
if f.len() < 7 || f[3] != "claude" {
continue;
}
let (id, tgt, cwd, title) = (f[0], f[1], f[2], f[6]);
let pid: i32 = f[4].parse().unwrap_or(0);
let Some(ver) = (pid != 0).then(|| e.version_of_pid(pid)).flatten() else {
p.skipped.push(format!(
"{} {} looks like claude but no session process was found under the pane",
id, tgt
));
continue;
};
matched.push(pid);
if !o.only_panes.is_empty() && !o.only_panes.iter().any(|w| w == id) {
continue;
}
let away = e.elsewhere(id, pid);
if let Some(a) = &away {
matched.push(a.pid);
}
let ver = match &away {
Some(a) => a.version.clone().unwrap_or_else(|| "?".into()),
None => ver,
};
if ver == newver {
continue; }
if id == o.self_pane {
p.skipped.push(format!(
"{} {} {}, this pane: restart it by hand (killing it would kill taimux)",
id, tgt, ver
));
continue;
}
let mut screen = e.capture(id);
if !screen.contains('❯') {
if let Some(bigger) = e.capture_zoomed(id) {
screen = bigger;
}
}
let hook = e.hook_state(id, pid);
let st = state::reading(&screen, e.own_state(pid, &screen), hook.as_deref());
if st.as_str() != "idle" && !o.include_busy {
p.skipped.push(format!(
"{} {} {}, {}: rerun when idle, or --include-busy",
id,
tgt,
ver,
st.as_str()
));
continue;
}
if state::background(&screen, hook.as_deref()) && !o.include_busy {
p.skipped.push(format!(
"{} {} {}, work still in flight: rerun once it reports back, or --include-busy",
id, tgt, ver
));
continue;
}
if let Some(a) = &away {
if o.force_transcript.is_some() {
p.skipped.push(format!(
"{} {} {}, shows background session {}: --transcript does not apply, its worker is respawned instead",
id, tgt, ver, a.id
));
continue;
}
if !typable_id(&a.id) {
p.skipped.push(format!(
"{} {} {}, shows background session {:?}, an id not safe to type",
id, tgt, ver, a.id
));
continue;
}
if let Err(why) = settled(&screen, a.transcript.as_deref(), o, e) {
p.skipped
.push(format!("{} {} {}, not settled: {}", id, tgt, ver, why));
continue;
}
p.go.push(Planned {
pane: id.to_string(),
target: tgt.to_string(),
pid,
cmd: format!("command claude attach {}", a.id),
title: title.to_string(),
via: format!(
"{} -> {}, background session {}, {}",
ver,
newver,
a.id,
st.as_str()
),
respawn: Some(a.id.clone()),
});
continue;
}
let ccwd = e.cwd_of(pid).unwrap_or_else(|| cwd.to_string());
let (transcript, via) = match &o.force_transcript {
Some(t) => (t.clone(), "--transcript, given".to_string()),
None => match e.resolve(id, &ccwd, title, pid) {
Ok(t) => {
let (path, why) = t.split_once('\t').unwrap_or((t.as_str(), ""));
(path.to_string(), why.to_string())
}
Err(why) => {
p.skipped
.push(format!("{} {} {}, unresolved: {}", id, tgt, ver, why));
p.unresolved = true;
continue;
}
},
};
if let Err(why) = settled(&screen, Some(&transcript), o, e) {
p.skipped
.push(format!("{} {} {}, not settled: {}", id, tgt, ver, why));
continue;
}
if let Some(first) = claimed.get(&transcript) {
p.skipped.push(format!(
"{} {} {}, resolves to the same transcript as {}",
id, tgt, ver, first
));
continue;
}
claimed.insert(transcript.clone(), id.to_string());
let mode = conv::current_mode(&screen, e.hook_mode(id, pid).as_deref());
let Some(cmd) = conv::build_cmd(&e.argv_of(pid), &transcript, &ccwd, cwd, mode.as_deref())
else {
p.skipped
.push(format!("{} {} {}, could not read its argv", id, tgt, ver));
continue;
};
p.go.push(Planned {
pane: id.to_string(),
target: tgt.to_string(),
pid,
cmd,
title: title.to_string(),
via: format!("{} -> {}, {}, {}", ver, newver, via, st.as_str()),
respawn: None,
});
}
if o.only_panes.is_empty() {
for b in e.background() {
if matched.contains(&b.pid) {
continue;
}
matched.push(b.pid);
let ver = b.version.clone().unwrap_or_else(|| "?".into());
if ver == newver {
continue;
}
match plan_bg(&b, o, e) {
Ok(st) => p.go_bg.push(PlannedBg {
id: b.id.clone(),
pid: b.pid,
name: b.name.clone(),
via: format!("{} -> {}, no pane, {}", ver, newver, st.as_str()),
}),
Err(why) => p
.skipped
.push(format!("{} background {}, {}", b.id, ver, why)),
}
}
}
let mut agents_json: Option<String> = None;
for np in nonpane_pids(&matched, versions_dir) {
let Some(ver) = e.version_of_pid(np) else {
continue;
};
if ver == newver {
continue;
}
let j = agents_json.get_or_insert_with(agents);
p.skipped.push(format!(
"pid {} {}, not a tmux pane: {}",
np,
ver,
describe_nonpane(&e.argv_of(np), j)
));
}
p
}
fn plan_bg(b: &BgSession, o: &Opts, e: &dyn Env) -> Result<state::State, String> {
if !typable_id(&b.id) {
return Err(format!("an id not safe to pass on: {:?}", b.id));
}
let st = match b.state {
Some(st @ state::State::Idle) => st,
Some(state::State::Input) => {
return Err(format!(
"waiting for an answer: claude attach {} to give it",
b.id
))
}
Some(st @ state::State::Run) if o.include_busy => st,
Some(st) => {
return Err(format!(
"{}: rerun when idle, or --include-busy",
st.as_str()
))
}
None => return Err("says nothing of what it is doing".into()),
};
match b.in_flight {
Some(f) if f.queued > 0 => {
return Err(format!(
"a reply is saved for its next start, which a respawn would send: claude attach {} to see it",
b.id
))
}
_ if o.include_busy => {}
Some(f) if f.tasks > 0 => {
return Err(
"work still in flight: rerun once it reports back, or --include-busy".into(),
)
}
Some(_) => {}
None => {
return Err(
"its job record says nothing of work in flight: --include-busy to respawn it anyway"
.into(),
)
}
}
if !o.include_busy {
if let Some(text) = b.transcript.as_deref().and_then(|t| e.read_transcript(t)) {
transcript_is_settled(&text, e.now()).map_err(|why| format!("not settled: {}", why))?;
}
}
Ok(st)
}
pub fn render(p: &Plan) -> String {
let mut s = format!("claude: {} installed at {}\n\n", p.newver, p.launcher);
if p.go.is_empty() && p.go_bg.is_empty() {
s.push_str("nothing to restart.\n");
} else {
s.push_str(&format!("to restart ({}):\n", p.go.len() + p.go_bg.len()));
for g in &p.go {
s.push_str(&format!(" {:<5} {:<14} {}\n", g.pane, g.target, g.title));
s.push_str(&format!(" {}\n", g.via));
match &g.respawn {
Some(job) => {
s.push_str(&format!(" claude respawn {}, then {}\n", job, g.cmd))
}
None => s.push_str(&format!(" {}\n", g.cmd)),
}
}
for b in &p.go_bg {
s.push_str(&format!(" {:<5} {:<14} {}\n", b.id, "background", b.name));
s.push_str(&format!(" {}\n", b.via));
s.push_str(&format!(" claude respawn {}\n", b.id));
}
}
if !p.skipped.is_empty() {
s.push_str(&format!("\nskipped ({}):\n", p.skipped.len()));
for k in &p.skipped {
s.push_str(&format!(" {}\n", k));
}
}
if p.unresolved {
s.push_str(
"\nAn unresolved pane means guessing, so it was left alone: restart it by hand\n\
with `claude -c` in that pane, or from its /resume picker. Each session records\n\
its pane at the next SessionStart, so a pane resolves cleanly once restarted.\n",
);
}
s
}
pub fn restart_pane(pane: &str, pid: i32, cmd: &str) -> bool {
use std::thread::sleep;
use std::time::Duration;
tmux::run(&["send-keys", "-t", pane, "C-c"]);
sleep(Duration::from_millis(300));
tmux::run(&["send-keys", "-t", pane, "C-c"]);
let mut waited = 0;
let mut sent_exit = false;
while alive(pid) {
sleep(Duration::from_millis(400));
waited += 1;
if waited >= 6 && !sent_exit {
tmux::run(&["send-keys", "-t", pane, "/exit", "Enter"]);
sent_exit = true;
}
if waited >= 30 {
return false;
}
}
sleep(Duration::from_millis(500));
tmux::run(&["send-keys", "-t", pane, "C-c"]);
sleep(Duration::from_millis(200));
tmux::run(&["send-keys", "-t", pane, cmd, "Enter"])
}
pub fn respawn(launcher: &str, job: &str) -> Result<(), String> {
let o = std::process::Command::new(launcher)
.args(["respawn", job])
.stdin(std::process::Stdio::null())
.output()
.map_err(|e| format!("could not run {}: {}", launcher, e))?;
if o.status.success() {
return Ok(());
}
let said = format!(
"{}\n{}",
String::from_utf8_lossy(&o.stderr),
String::from_utf8_lossy(&o.stdout)
);
Err(said
.lines()
.map(str::trim)
.find(|l| !l.is_empty())
.map(str::to_string)
.unwrap_or_else(|| format!("claude respawn {}: {}", job, o.status)))
}
pub fn reattach(pane: &str, client: i32, cmd: &str) -> bool {
use std::thread::sleep;
use std::time::Duration;
let gone = |polls: u32| {
for _ in 0..polls {
if !alive(client) {
return true;
}
sleep(Duration::from_millis(200));
}
!alive(client)
};
if !gone(10) && version_of_pid(client, &versions_dir()).is_some() {
let _ = std::process::Command::new("kill")
.args(["-TERM", &client.to_string()])
.stdin(std::process::Stdio::null())
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.status();
}
if !gone(25) {
return false;
}
sleep(Duration::from_millis(300));
tmux::run(&["send-keys", "-t", pane, "C-c"]);
sleep(Duration::from_millis(200));
tmux::run(&["send-keys", "-t", pane, cmd, "Enter"])
}
fn alive(pid: i32) -> bool {
std::path::Path::new(&format!("/proc/{}", pid)).exists()
}
pub fn confirm_yes(answer: &str) -> bool {
matches!(answer.trim(), "" | "y" | "Y" | "yes" | "YES" | "Yes")
}
pub struct Live {
pub versions_dir: String,
}
impl Env for Live {
fn capture(&self, pane: &str) -> String {
tmux::ask_raw(&["capture-pane", "-p", "-t", pane]).unwrap_or_default()
}
fn capture_zoomed(&self, pane: &str) -> Option<String> {
capture_zoomed(pane)
}
fn hook_state(&self, pane: &str, pid: i32) -> Option<String> {
taimux_core::hook::hook_state_of(pane, pid)
}
fn hook_mode(&self, pane: &str, pid: i32) -> Option<String> {
taimux_core::hook::hook_entry(pane, pid).map(|e| e.mode)
}
fn own_state(&self, pid: i32, screen: &str) -> Option<state::State> {
taimux_core::status::of(pid, &|| screen.to_string()).and_then(|o| o.state())
}
fn elsewhere(&self, pane: &str, pid: i32) -> Option<Elsewhere> {
let o =
taimux_core::status::of(pid, &|| self.capture(pane)).filter(|o| o.via.elsewhere())?;
let id = if o.job.is_empty() {
o.session_id.get(..8).unwrap_or(&o.session_id)
} else {
&o.job
};
Some(Elsewhere {
id: id.to_string(),
pid: o.pid,
version: version_of_pid(o.pid, &self.versions_dir),
transcript: o.transcript().map(|t| t.display().to_string()),
})
}
fn background(&self) -> Vec<BgSession> {
taimux_core::status::background()
.into_iter()
.map(|o| {
let id = if o.job.is_empty() {
o.session_id.get(..8).unwrap_or(&o.session_id).to_string()
} else {
o.job.clone()
};
BgSession {
version: version_of_pid(o.pid, &self.versions_dir),
state: o.state(),
transcript: o.transcript().map(|t| t.display().to_string()),
in_flight: taimux_core::status::in_flight(&id),
pid: o.pid,
name: o.name,
id,
}
})
.collect()
}
fn version_of_pid(&self, pid: i32) -> Option<String> {
version_of_pid(pid, &self.versions_dir)
}
fn cwd_of(&self, pid: i32) -> Option<String> {
std::fs::read_link(format!("/proc/{}/cwd", pid))
.ok()
.map(|p| p.to_string_lossy().into_owned())
.filter(|s| !s.is_empty())
}
fn argv_of(&self, pid: i32) -> Vec<String> {
conv::argv_of(pid)
}
fn resolve(&self, pane: &str, cwd: &str, title: &str, pid: i32) -> Result<String, String> {
conv::resolve(pane, cwd, title, pid)
.map(|r| format!("{}\t{}", r.transcript.display(), r.why))
}
fn read_transcript(&self, path: &str) -> Option<String> {
std::fs::read_to_string(path).ok()
}
fn now(&self) -> i64 {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_secs() as i64)
.unwrap_or(0)
}
}
pub fn installed(launcher: &str, versions_dir: &str) -> Result<String, String> {
let target = std::fs::canonicalize(launcher)
.map(|p| p.to_string_lossy().into_owned())
.unwrap_or_default();
let prefix = format!("{}/", versions_dir);
match target.strip_prefix(&prefix) {
Some(rest) if !rest.is_empty() => Ok(rest.split('/').next().unwrap_or(rest).to_string()),
_ => Err(format!(
"restart: {} does not point into {} (got '{}')",
launcher,
versions_dir,
if target.is_empty() {
"nothing"
} else {
&target
}
)),
}
}
pub fn versions_dir() -> String {
format!(
"{}/.local/share/claude/versions",
std::env::var("HOME").unwrap_or_default()
)
}
pub fn launcher() -> String {
format!(
"{}/.local/bin/claude",
std::env::var("HOME").unwrap_or_default()
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_timestamp_becomes_epoch_seconds() {
assert_eq!(parse_iso8601("2026-09-02T01:23:45.678Z"), Some(1788312225));
assert_eq!(parse_iso8601("1970-01-01T00:00:00Z"), Some(0));
assert_eq!(parse_iso8601("2024-02-29T00:00:00Z"), Some(1709164800));
}
#[test]
fn an_unparseable_timestamp_is_no_answer() {
assert_eq!(parse_iso8601(""), None);
assert_eq!(parse_iso8601("yesterday"), None);
assert_eq!(parse_iso8601("2026-09-02 01:23:45"), None);
assert_eq!(parse_iso8601("2026-13-02T01:23:45Z"), None);
}
#[test]
fn a_tool_call_still_running_is_not_settled() {
let t = r#"{"type":"assistant","message":{"content":[{"type":"tool_use"}]},"timestamp":"2020-01-01T00:00:00Z"}"#;
assert_eq!(
transcript_is_settled(t, 1788312225),
Err("a tool call is still running".into())
);
}
#[test]
fn recent_activity_is_not_settled_whatever_the_screen_said() {
let t = r#"{"type":"user","timestamp":"2026-09-02T01:23:45Z"}"#;
assert!(transcript_is_settled(t, 1788312225 + 10).is_err());
assert!(transcript_is_settled(t, 1788312225 + 100).is_ok());
}
#[test]
fn an_empty_or_odd_transcript_does_not_block() {
assert!(transcript_is_settled("", 0).is_ok());
assert!(transcript_is_settled("not json at all\n", 0).is_ok());
assert!(transcript_is_settled(r#"{"type":"user"}"#, 0).is_ok());
}
#[test]
fn a_dialog_on_screen_blocks_a_restart() {
let footer = "some output\n\nDo you want to proceed?\n";
assert_eq!(
screen_has_no_dialog(footer),
Err("a dialog is waiting for an answer".into())
);
let choice = "some output\n1. Yes\n2. No\n❯ 1. Yes\n";
assert_eq!(
screen_has_no_dialog(choice),
Err("a dialog is waiting for an answer".into())
);
assert!(screen_has_no_dialog("some output\n❯ \n").is_ok());
}
#[test]
fn a_half_pressed_ctrl_c_blocks_a_restart() {
let screen = "work\n\nPress Ctrl-C again to exit\n";
assert_eq!(
screen_has_no_dialog(screen),
Err("a Ctrl-C is already half-pressed".into())
);
}
#[test]
fn an_empty_prompt_box_is_no_draft() {
assert!(screen_has_no_draft("stuff\n❯ \n").is_ok());
assert!(screen_has_no_draft("stuff\n❯ \u{a0}\u{a0}\n").is_ok());
assert_eq!(
screen_has_no_draft("stuff\n❯ half a thought\n"),
Err("unsent text in the prompt box".into())
);
}
#[test]
fn a_pane_tall_enough_to_read_is_left_alone() {
assert!(zoom_steps("%1", false, 6, 40, "%2", "%3").is_none());
assert!(zoom_steps("%1", false, 40, 40, "%2", "%3").is_none());
assert!(zoom_steps("%1", false, 3, 3, "%2", "%3").is_none());
assert!(zoom_steps("%1", false, 3, 5, "%2", "%3").is_none());
}
#[test]
fn a_short_pane_is_zoomed_and_the_selection_put_back() {
let (go, back) = zoom_steps("%1", false, 3, 17, "%2", "%3").unwrap();
assert_eq!(go, vec![vec!["resize-pane", "-Z", "-t", "%1"]]);
assert_eq!(
back,
vec![
vec!["resize-pane", "-Z", "-t", "%1"],
vec!["select-pane", "-t", "%3"],
vec!["select-pane", "-t", "%2"],
]
);
}
#[test]
fn a_window_zoomed_elsewhere_is_handed_back_zoomed() {
let (go, back) = zoom_steps("%1", true, 3, 17, "%2", "%3").unwrap();
assert_eq!(
go,
vec![
vec!["resize-pane", "-Z", "-t", "%1"],
vec!["resize-pane", "-Z", "-t", "%1"],
]
);
assert_eq!(
back,
vec![
vec!["resize-pane", "-Z", "-t", "%1"],
vec!["select-pane", "-t", "%3"],
vec!["select-pane", "-t", "%2"],
vec!["resize-pane", "-Z", "-t", "%2"],
]
);
}
#[test]
fn an_active_short_pane_has_no_selection_to_restore() {
let (go, back) = zoom_steps("%1", false, 3, 17, "%1", "%3").unwrap();
assert_eq!(go.len(), 1);
assert_eq!(back, vec![vec!["resize-pane", "-Z", "-t", "%1"]]);
}
#[test]
fn no_prompt_box_is_a_refusal() {
assert_eq!(
screen_has_no_draft("just some text\n"),
Err("no prompt box on screen".into())
);
assert!(screen_has_no_draft("").is_ok());
assert!(screen_has_no_draft(" \n\n").is_ok());
}
#[test]
fn a_bare_enter_confirms() {
assert!(confirm_yes(""));
assert!(confirm_yes("y"));
assert!(confirm_yes("YES"));
assert!(!confirm_yes("n"));
assert!(!confirm_yes("no"));
assert!(!confirm_yes("maybe"));
}
#[test]
fn the_launcher_must_point_into_the_versions_dir() {
let root = std::env::temp_dir().join(format!("jmrs{}", std::process::id()));
let vers = root.join("versions");
std::fs::create_dir_all(&vers).unwrap();
std::fs::write(vers.join("2.1.258"), "x").unwrap();
let link = root.join("claude");
std::os::unix::fs::symlink(vers.join("2.1.258"), &link).unwrap();
assert_eq!(
installed(&link.to_string_lossy(), &vers.to_string_lossy()),
Ok("2.1.258".into())
);
std::fs::write(root.join("elsewhere"), "x").unwrap();
std::fs::remove_file(&link).unwrap();
std::os::unix::fs::symlink(root.join("elsewhere"), &link).unwrap();
let e = installed(&link.to_string_lossy(), &vers.to_string_lossy()).expect_err("refused");
assert!(e.contains("elsewhere"));
let _ = std::fs::remove_dir_all(&root);
}
}
pub fn nonpane_pids(matched: &[i32], versions_dir: &str) -> Vec<i32> {
let mut out = Vec::new();
for e in std::fs::read_dir("/proc").into_iter().flatten().flatten() {
let Some(pid) = e.file_name().to_str().and_then(|s| s.parse::<i32>().ok()) else {
continue;
};
if matched.contains(&pid) {
continue;
}
let comm = std::fs::read_to_string(format!("/proc/{}/comm", pid)).unwrap_or_default();
let argv = conv::argv_of(pid);
let is_claude = comm.trim() == "claude"
|| argv
.first()
.map(|a| a.starts_with(&format!("{}/", versions_dir)))
.unwrap_or(false);
if is_claude {
out.push(pid);
}
}
out.sort_unstable();
out
}
fn json_object_with_prefix(text: &str, key: &str, prefix: &str) -> Option<String> {
let needle = format!("\"{}\":\"{}", key, prefix);
let at = text.find(&needle).or_else(|| {
let spaced = format!("\"{}\": \"{}", key, prefix);
text.find(&spaced)
})?;
let mut depth = 0i32;
let bytes = text.as_bytes();
let mut start = None;
for i in (0..at).rev() {
match bytes[i] {
b'}' => depth += 1,
b'{' => {
if depth == 0 {
start = Some(i);
break;
}
depth -= 1;
}
_ => {}
}
}
let start = start?;
let mut depth = 0i32;
for i in start..bytes.len() {
match bytes[i] {
b'{' => depth += 1,
b'}' => {
depth -= 1;
if depth == 0 {
return Some(text[start..=i].to_string());
}
}
_ => {}
}
}
None
}
pub fn describe_nonpane(argv: &[String], agents_json: &str) -> String {
if argv.is_empty() {
return "gone".into();
}
let (mut sid, mut res, mut forked) = (String::new(), String::new(), false);
for i in 1..argv.len() {
match argv[i].as_str() {
"--fork-session" => forked = true,
"--session-id" => sid = argv.get(i + 1).cloned().unwrap_or_default(),
"-r" | "--resume" => res = argv.get(i + 1).cloned().unwrap_or_default(),
_ => {}
}
}
let own = base_id(if sid.is_empty() { &res } else { &sid });
let parent = if forked && !res.is_empty() {
base_id(&res)
} else {
String::new()
};
let (mut kind, mut st, mut name) = ("?".to_string(), "?".to_string(), String::new());
if !own.is_empty() {
if let Some(obj) = json_object_with_prefix(agents_json, "sessionId", &own) {
let f = |k: &str| taimux_core::json::field(&obj, k);
let k = f("kind");
if !k.is_empty() {
kind = k;
}
let s = {
let a = f("state");
if a.is_empty() {
f("status")
} else {
a
}
};
if !s.is_empty() {
st = s;
}
name = f("name");
}
}
let mut out = format!("{} {}", kind, st);
if forked {
out.push_str(" fork");
}
if !name.is_empty() {
out.push_str(&format!(" \"{}\"", name));
}
if !own.is_empty() {
out.push_str(&format!(" [{}]", own.chars().take(8).collect::<String>()));
}
if !parent.is_empty() {
out.push_str(&format!(
" of [{}]",
parent.chars().take(8).collect::<String>()
));
}
out
}
fn base_id(s: &str) -> String {
if s.is_empty() {
return String::new();
}
let b = s.rsplit('/').next().unwrap_or(s);
b.strip_suffix(".jsonl").unwrap_or(b).to_string()
}
#[cfg(test)]
mod nonpane_tests {
use super::*;
fn v(a: &[&str]) -> Vec<String> {
a.iter().map(|s| s.to_string()).collect()
}
#[test]
fn a_session_id_is_read_out_of_a_path_or_taken_as_it_stands() {
assert_eq!(base_id("/a/b/263946b5-9bd7.jsonl"), "263946b5-9bd7");
assert_eq!(base_id("263946b5"), "263946b5");
assert_eq!(base_id(""), "");
}
#[test]
fn the_right_object_comes_back_whole() {
let j = r#"[{"sessionId":"aaa111","kind":"task","meta":{"a":1},"name":"first"},
{"sessionId":"bbb222","kind":"agent","name":"second"}]"#;
let o = json_object_with_prefix(j, "sessionId", "bbb222").expect("found");
assert!(o.contains("second"));
assert!(!o.contains("first"));
let o = json_object_with_prefix(j, "sessionId", "aaa").expect("found by prefix");
assert!(o.contains("first"));
assert!(
o.contains("\"meta\":{\"a\":1}"),
"nested field truncated: {}",
o
);
assert!(json_object_with_prefix(j, "sessionId", "zzz").is_none());
}
#[test]
fn a_process_with_no_argv_is_simply_gone() {
assert_eq!(describe_nonpane(&[], "[]"), "gone");
}
#[test]
fn an_unnamed_session_still_says_what_it_can() {
let d = describe_nonpane(&v(&["claude", "--session-id", "263946b5-9bd7"]), "[]");
assert_eq!(d, "? ? [263946b5]");
}
#[test]
fn a_named_one_says_kind_state_and_name() {
let j =
r#"[{"sessionId":"263946b5-9bd7","kind":"task","state":"running","name":"the thing"}]"#;
let d = describe_nonpane(&v(&["claude", "--session-id", "263946b5-9bd7"]), j);
assert_eq!(d, "task running \"the thing\" [263946b5]");
}
#[test]
fn status_stands_in_for_state() {
let j = r#"[{"sessionId":"aaa11111","kind":"agent","status":"idle"}]"#;
let d = describe_nonpane(&v(&["claude", "--session-id", "aaa11111"]), j);
assert_eq!(d, "agent idle [aaa11111]");
}
#[test]
fn a_fork_names_its_parent_too() {
let d = describe_nonpane(
&v(&[
"claude",
"--session-id",
"child111",
"--fork-session",
"--resume",
"/p/parent22.jsonl",
]),
"[]",
);
assert_eq!(d, "? ? fork [child111] of [parent22]");
}
}
#[cfg(test)]
mod plan_tests {
use super::*;
struct Fake {
vers: HashMap<i32, String>,
screens: HashMap<String, String>,
resolved: HashMap<String, Result<String, String>>,
zoomed: HashMap<String, String>,
zooms: std::cell::Cell<usize>,
hooks: HashMap<String, String>,
modes: HashMap<String, String>,
own: HashMap<i32, state::State>,
away: HashMap<i32, (String, String, Option<String>)>,
bgs: Vec<BgSession>,
transcript: String,
now: i64,
}
impl Env for Fake {
fn capture(&self, pane: &str) -> String {
self.screens.get(pane).cloned().unwrap_or_default()
}
fn capture_zoomed(&self, pane: &str) -> Option<String> {
let bigger = self.zoomed.get(pane).cloned();
if bigger.is_some() {
self.zooms.set(self.zooms.get() + 1);
}
bigger
}
fn hook_state(&self, pane: &str, _pid: i32) -> Option<String> {
self.hooks.get(pane).cloned()
}
fn hook_mode(&self, pane: &str, _pid: i32) -> Option<String> {
self.modes.get(pane).cloned()
}
fn own_state(&self, pid: i32, _screen: &str) -> Option<state::State> {
self.own.get(&pid).copied()
}
fn elsewhere(&self, _pane: &str, pid: i32) -> Option<Elsewhere> {
self.away.get(&pid).map(|(id, ver, transcript)| Elsewhere {
id: id.clone(),
pid: 1000 + pid,
version: Some(ver.clone()),
transcript: transcript.clone(),
})
}
fn background(&self) -> Vec<BgSession> {
self.bgs.clone()
}
fn version_of_pid(&self, pid: i32) -> Option<String> {
self.vers.get(&pid).cloned()
}
fn cwd_of(&self, _pid: i32) -> Option<String> {
Some("/w".into())
}
fn argv_of(&self, _pid: i32) -> Vec<String> {
vec!["claude".into()]
}
fn resolve(&self, pane: &str, _c: &str, _t: &str, _p: i32) -> Result<String, String> {
self.resolved
.get(pane)
.cloned()
.unwrap_or_else(|| Err("no candidate".into()))
}
fn read_transcript(&self, _path: &str) -> Option<String> {
Some(self.transcript.clone())
}
fn now(&self) -> i64 {
self.now
}
}
fn fake() -> Fake {
let mut vers = HashMap::new();
vers.insert(11, "2.1.100".to_string()); vers.insert(22, "2.1.258".to_string()); let mut screens = HashMap::new();
screens.insert("%1".to_string(), "some output\n❯ \n".to_string());
screens.insert("%2".to_string(), "some output\n❯ \n".to_string());
let mut resolved = HashMap::new();
resolved.insert("%1".to_string(), Ok("/t/a.jsonl\tpane map".to_string()));
Fake {
vers,
screens,
resolved,
zoomed: HashMap::new(),
zooms: std::cell::Cell::new(0),
hooks: HashMap::new(),
modes: HashMap::new(),
own: HashMap::new(),
away: HashMap::new(),
bgs: Vec::new(),
transcript: r#"{"type":"user","timestamp":"2020-01-01T00:00:00Z"}"#.to_string(),
now: 1788312225,
}
}
fn opts() -> Opts {
Opts {
include_busy: false,
only_panes: Vec::new(),
force_transcript: None,
self_pane: String::new(),
}
}
const ROWS: &str = "%1\tw:1.1\t/w\tclaude\t11\tclaude\tproj: the stale one\n\
%2\tw:2.1\t/w\tclaude\t22\tclaude\tproj: the current one";
fn plan_of(e: &Fake, o: &Opts) -> Plan {
plan(ROWS, "2.1.258", "/l/claude", o, e, "/nowhere", &|| {
"[]".into()
})
}
#[test]
fn a_stale_pane_is_planned_and_a_current_one_is_not() {
let p = plan_of(&fake(), &opts());
assert_eq!(p.go.len(), 1);
assert_eq!(p.go[0].pane, "%1");
assert_eq!(p.go[0].cmd, "command claude --resume /t/a.jsonl");
assert_eq!(p.go[0].via, "2.1.100 -> 2.1.258, pane map, idle");
assert!(p.skipped.is_empty());
}
#[test]
fn a_session_comes_back_in_the_mode_it_is_in() {
let rule = "────────────────────────────────";
let mut e = fake();
e.screens.insert(
"%1".into(),
format!("some output\n{rule}\n❯ \n{rule}\n ⏵⏵ accept edits on (shift+tab to cycle)\n"),
);
e.modes.insert("%1".into(), "auto".into()); let p = plan_of(&e, &opts());
assert_eq!(p.go.len(), 1, "{:?}", p.skipped);
assert_eq!(
p.go[0].cmd,
"command claude --permission-mode acceptEdits --resume /t/a.jsonl"
);
let mut e = fake();
e.modes.insert("%1".into(), "plan".into());
let p = plan_of(&e, &opts());
assert_eq!(
p.go[0].cmd,
"command claude --permission-mode plan --resume /t/a.jsonl"
);
let mut e = fake();
e.modes.insert("%1".into(), "-".into());
assert_eq!(
plan_of(&e, &opts()).go[0].cmd,
"command claude --resume /t/a.jsonl"
);
}
#[test]
fn the_rendered_plan_reads_the_way_it_always_did() {
let out = render(&plan_of(&fake(), &opts()));
assert_eq!(
out,
"claude: 2.1.258 installed at /l/claude\n\n\
to restart (1):\n\
\x20 %1 w:1.1 proj: the stale one\n\
\x20 2.1.100 -> 2.1.258, pane map, idle\n\
\x20 command claude --resume /t/a.jsonl\n"
);
}
#[test]
fn this_pane_is_never_restarted() {
let mut o = opts();
o.self_pane = "%1".into();
let p = plan_of(&fake(), &o);
assert!(p.go.is_empty());
assert!(p.skipped[0].contains("this pane"));
assert!(p.skipped[0].contains("killing it would kill taimux"));
}
#[test]
fn a_pane_that_is_not_idle_waits_for_include_busy() {
let mut e = fake();
e.screens
.insert("%1".into(), "Twisting… (35s · ↓ 1.6k tokens)\n❯ \n".into());
let p = plan_of(&e, &opts());
assert!(p.go.is_empty());
assert!(p.skipped[0].contains("rerun when idle, or --include-busy"));
let mut o = opts();
o.include_busy = true;
assert_eq!(plan_of(&e, &o).go.len(), 1);
}
#[test]
fn a_session_that_says_it_is_busy_is_busy() {
let mut e = fake();
e.hooks.insert("%1".into(), "input".into());
e.own.insert(11, state::State::Run);
let p = plan_of(&e, &opts());
assert!(p.go.is_empty());
assert!(
p.skipped[0].contains("run: rerun when idle"),
"{:?}",
p.skipped
);
e.hooks.insert("%1".into(), "run".into());
e.own.insert(11, state::State::Idle);
assert_eq!(plan_of(&e, &opts()).go.len(), 1);
}
#[test]
fn a_pane_showing_a_background_session_is_respawned_and_attached_again() {
let mut e = fake();
e.own.insert(11, state::State::Idle);
e.away
.insert(11, ("2447b564".into(), "2.1.100".into(), None));
e.resolved.insert("%1".into(), Err("never asked".into()));
let p = plan_of(&e, &opts());
assert_eq!(p.go.len(), 1, "{:?}", p.skipped);
let g = &p.go[0];
assert_eq!(g.pane, "%1");
assert_eq!(g.pid, 11, "the pane's own process, the client");
assert_eq!(g.respawn.as_deref(), Some("2447b564"));
assert_eq!(g.cmd, "command claude attach 2447b564");
assert_eq!(
g.via,
"2.1.100 -> 2.1.258, background session 2447b564, idle"
);
assert!(!p.unresolved);
}
#[test]
fn an_attached_pane_is_judged_by_its_workers_version() {
let mut e = fake();
e.away
.insert(22, ("880ae832".into(), "2.1.250".into(), None));
let p = plan_of(&e, &opts());
assert_eq!(p.go.len(), 2, "{:?}", p.skipped);
assert_eq!(
(p.go[0].pane.as_str(), p.go[0].respawn.as_deref()),
("%1", None)
);
assert_eq!(
(p.go[1].pane.as_str(), p.go[1].respawn.as_deref()),
("%2", Some("880ae832"))
);
assert!(p.go[1].via.starts_with("2.1.250 -> 2.1.258, "));
let mut e = fake();
e.away
.insert(11, ("880ae832".into(), "2.1.258".into(), None));
let p = plan_of(&e, &opts());
assert!(p.go.is_empty());
assert!(
p.skipped.iter().all(|s| !s.starts_with("%1")),
"{:?}",
p.skipped
);
}
#[test]
fn a_background_session_waits_like_any_pane() {
let bg = |e: &mut Fake| {
e.away.insert(
11,
(
"2447b564".into(),
"2.1.100".into(),
Some("/t/bg.jsonl".into()),
),
);
};
let mut busy = opts();
busy.include_busy = true;
let mut e = fake();
bg(&mut e);
e.own.insert(11, state::State::Run);
let p = plan_of(&e, &opts());
assert!(p.go.is_empty());
assert!(
p.skipped[0].contains("2.1.100, run: rerun when idle, or --include-busy"),
"{:?}",
p.skipped
);
assert_eq!(plan_of(&e, &busy).go.len(), 1);
let mut e = fake();
bg(&mut e);
e.screens.insert(
"%1".into(),
"✻ Cogitated for 3s · done 4:55 PM · 1 shell still running\n❯ \n".into(),
);
assert!(plan_of(&e, &opts()).skipped[0].contains("work still in flight"));
let mut e = fake();
bg(&mut e);
e.screens.insert(
"%1".into(),
"some output\n\nDo you want to proceed?\n".into(),
);
let p = plan_of(&e, &busy);
assert!(p.go.is_empty());
assert!(
p.skipped[0].contains("a dialog is waiting"),
"{:?}",
p.skipped
);
let mut e = fake();
bg(&mut e);
e.screens
.insert("%1".into(), "stuff\n❯ half a thought\n".into());
assert!(plan_of(&e, &opts()).skipped[0].contains("unsent text in the prompt box"));
let mut e = fake();
bg(&mut e);
e.transcript = r#"{"type":"user","timestamp":"2026-09-02T01:23:40Z"}"#.into();
assert!(plan_of(&e, &opts()).skipped[0].contains("not settled"));
e.away
.insert(11, ("2447b564".into(), "2.1.100".into(), None));
assert_eq!(plan_of(&e, &opts()).go.len(), 1);
let mut e = fake();
bg(&mut e);
let mut me = opts();
me.self_pane = "%1".into();
assert!(plan_of(&e, &me).skipped[0].contains("this pane"));
}
#[test]
fn two_panes_on_one_background_session_are_both_attached_again() {
let mut e = fake();
e.vers.insert(22, "2.1.100".into());
e.away
.insert(11, ("2447b564".into(), "2.1.100".into(), None));
e.away
.insert(22, ("2447b564".into(), "2.1.100".into(), None));
let p = plan_of(&e, &opts());
assert_eq!(p.go.len(), 2, "{:?}", p.skipped);
assert!(p
.go
.iter()
.all(|g| g.respawn.as_deref() == Some("2447b564")));
}
#[test]
fn a_given_transcript_does_not_apply_to_a_background_session() {
let mut e = fake();
e.away
.insert(11, ("2447b564".into(), "2.1.100".into(), None));
let mut o = opts();
o.only_panes = vec!["%1".into()];
o.force_transcript = Some("/t/other.jsonl".into());
let p = plan_of(&e, &o);
assert!(p.go.is_empty());
assert!(
p.skipped[0].contains("--transcript does not apply"),
"{:?}",
p.skipped
);
}
#[test]
fn an_id_unsafe_to_type_is_refused() {
let mut e = fake();
e.away
.insert(11, ("2447b564; rm -rf ~".into(), "2.1.100".into(), None));
let p = plan_of(&e, &opts());
assert!(p.go.is_empty());
assert!(p.skipped[0].contains("not safe to type"), "{:?}", p.skipped);
assert!(typable_id("2447b564"));
assert!(typable_id("58b11e6b-39fd-484a-aa0f-44e86a8eb6d1"));
assert!(!typable_id(""));
assert!(!typable_id("a b"));
assert!(!typable_id("$(x)"));
}
fn bg_session() -> BgSession {
BgSession {
id: "f3836aef".into(),
pid: 500,
version: Some("2.1.100".into()),
name: "jumpmux: stage".into(),
state: Some(state::State::Idle),
transcript: Some("/t/bg.jsonl".into()),
in_flight: Some(taimux_core::status::InFlight {
tasks: 0,
queued: 0,
}),
}
}
#[test]
fn a_background_session_no_pane_shows_is_respawned() {
let mut e = fake();
let mut current = bg_session();
current.id = "aaaaaaaa".into();
current.pid = 501;
current.version = Some("2.1.258".into());
e.bgs = vec![bg_session(), current];
let p = plan_of(&e, &opts());
assert_eq!(p.go.len(), 1, "the stale pane, as before");
assert_eq!(p.go_bg.len(), 1, "{:?}", p.skipped);
assert_eq!((p.go_bg[0].id.as_str(), p.go_bg[0].pid), ("f3836aef", 500));
assert_eq!(p.go_bg[0].via, "2.1.100 -> 2.1.258, no pane, idle");
let out = render(&p);
assert!(out.contains("to restart (2):\n"), "{out}");
assert!(
out.ends_with(
"\x20 f3836aef background jumpmux: stage\n\
\x20 2.1.100 -> 2.1.258, no pane, idle\n\
\x20 claude respawn f3836aef\n"
),
"{out}"
);
let mut o = opts();
o.only_panes = vec!["%1".into()];
assert!(plan_of(&e, &o).go_bg.is_empty());
}
#[test]
fn a_background_session_a_pane_shows_is_left_to_the_pane() {
let mut e = fake();
e.away
.insert(11, ("f3836aef".into(), "2.1.100".into(), None));
let mut b = bg_session();
b.pid = 1011; e.bgs = vec![b];
let p = plan_of(&e, &opts());
assert!(p.go_bg.is_empty(), "planned twice");
assert_eq!(p.go[0].respawn.as_deref(), Some("f3836aef"));
}
#[test]
fn a_background_session_no_pane_shows_waits_like_any_pane() {
let mut busy = opts();
busy.include_busy = true;
let with = |f: &dyn Fn(&mut BgSession)| {
let mut e = fake();
let mut b = bg_session();
f(&mut b);
e.bgs = vec![b];
e
};
let refused = |e: &Fake, o: &Opts, why: &str| {
let p = plan_of(e, o);
assert!(p.go_bg.is_empty(), "planned despite {why}");
assert!(
p.skipped
.iter()
.any(|s| s.starts_with("f3836aef background 2.1.100, ") && s.contains(why)),
"{why}: {:?}",
p.skipped
);
};
let e = with(&|b| b.state = Some(state::State::Input));
refused(&e, &busy, "waiting for an answer: claude attach f3836aef");
let e = with(&|b| b.state = Some(state::State::Run));
refused(&e, &opts(), "run: rerun when idle, or --include-busy");
assert_eq!(plan_of(&e, &busy).go_bg.len(), 1);
let e = with(&|b| b.state = None);
refused(&e, &busy, "says nothing of what it is doing");
let e = with(&|b| {
b.in_flight = Some(taimux_core::status::InFlight {
tasks: 0,
queued: 1,
})
});
refused(&e, &busy, "a reply is saved for its next start");
let e = with(&|b| {
b.in_flight = Some(taimux_core::status::InFlight {
tasks: 1,
queued: 0,
})
});
refused(&e, &opts(), "work still in flight");
assert_eq!(plan_of(&e, &busy).go_bg.len(), 1);
let e = with(&|b| b.in_flight = None);
refused(&e, &opts(), "says nothing of work in flight");
assert_eq!(plan_of(&e, &busy).go_bg.len(), 1);
let mut e = with(&|_| {});
e.transcript = r#"{"type":"user","timestamp":"2026-09-02T01:23:40Z"}"#.into();
refused(&e, &opts(), "not settled");
let e = with(&|b| b.id = "f38; x".into());
assert!(plan_of(&e, &busy).go_bg.is_empty());
}
#[test]
fn the_rendered_plan_names_the_respawn() {
let mut e = fake();
e.away
.insert(11, ("2447b564".into(), "2.1.100".into(), None));
assert_eq!(
render(&plan_of(&e, &opts())),
"claude: 2.1.258 installed at /l/claude\n\n\
to restart (1):\n\
\x20 %1 w:1.1 proj: the stale one\n\
\x20 2.1.100 -> 2.1.258, background session 2447b564, idle\n\
\x20 claude respawn 2447b564, then command claude attach 2447b564\n"
);
}
#[test]
fn a_pane_with_work_in_flight_waits_for_include_busy() {
let mut e = fake();
e.screens.insert(
"%1".into(),
"✻ Cogitated for 1m 3s · done 4:55 PM · 2 shells still running\n❯ \n".into(),
);
let p = plan_of(&e, &opts());
assert!(p.go.is_empty());
assert!(
p.skipped[0].contains("work still in flight"),
"{:?}",
p.skipped
);
let mut e = fake();
e.hooks.insert("%1".into(), "bg".into());
let p = plan_of(&e, &opts());
assert!(p.go.is_empty());
assert!(
p.skipped[0].contains("work still in flight"),
"{:?}",
p.skipped
);
let mut o = opts();
o.include_busy = true;
assert_eq!(plan_of(&e, &o).go.len(), 1);
}
#[test]
fn a_dialog_blocks_a_restart_even_with_include_busy() {
let mut e = fake();
e.screens
.insert("%1".into(), "output\n\nDo you want to proceed?\n".into());
let mut o = opts();
o.include_busy = true;
let p = plan_of(&e, &o);
assert!(p.go.is_empty());
assert!(p.skipped[0].contains("a dialog is waiting for an answer"));
}
#[test]
fn an_unsent_draft_is_left_alone() {
let mut e = fake();
e.screens
.insert("%1".into(), "output\n❯ half a thought\n".into());
let p = plan_of(&e, &opts());
assert!(p.skipped[0].contains("unsent text in the prompt box"));
}
#[test]
fn a_pane_too_short_for_its_box_is_read_zoomed() {
let mut e = fake();
e.screens.insert(
"%1".into(),
" current: 2.1.100 · latest…\n────────\n".into(),
);
e.zoomed.insert("%1".into(), "some output\n❯ \n".into());
let p = plan_of(&e, &opts());
assert_eq!(e.zooms.get(), 1);
assert_eq!(p.go.len(), 1);
assert_eq!(p.go[0].pane, "%1");
assert!(p.skipped.is_empty());
}
#[test]
fn a_draft_hidden_by_a_short_pane_still_refuses() {
let mut e = fake();
e.screens
.insert("%1".into(), " current: 2.1.100…\n".into());
e.zoomed
.insert("%1".into(), "output\n❯ half a thought\n".into());
let p = plan_of(&e, &opts());
assert_eq!(e.zooms.get(), 1);
assert!(p.go.is_empty());
assert!(p.skipped[0].contains("unsent text in the prompt box"));
}
#[test]
fn a_dialog_hidden_by_a_short_pane_is_found_by_the_zoom() {
let mut e = fake();
e.screens.insert("%1".into(), " Bash command\n".into());
e.zoomed.insert(
"%1".into(),
"Do you want to proceed?\n❯ 1. Yes\n 2. No\n".into(),
);
let mut o = opts();
o.include_busy = true;
let p = plan_of(&e, &o);
assert!(p.go.is_empty());
assert!(p.skipped[0].contains("a dialog is waiting for an answer"));
}
#[test]
fn a_pane_that_shows_its_box_is_not_zoomed() {
let e = fake();
let p = plan_of(&e, &opts());
assert_eq!(e.zooms.get(), 0);
assert_eq!(p.go.len(), 1);
}
#[test]
fn a_recently_active_transcript_is_left_alone() {
let mut e = fake();
e.transcript = r#"{"type":"user","timestamp":"2026-09-02T01:23:45Z"}"#.into();
e.now = 1788312225 + 10;
let p = plan_of(&e, &opts());
assert!(p.skipped[0].contains("active in the last 45s"));
}
#[test]
fn an_unresolved_pane_says_so_and_earns_the_paragraph() {
let mut e = fake();
e.resolved
.insert("%1".into(), Err("3 transcripts share this title".into()));
let p = plan_of(&e, &opts());
assert!(p.go.is_empty());
assert!(p.unresolved);
assert!(p.skipped[0].contains("unresolved: 3 transcripts share this title"));
assert!(render(&p).contains("restart it by hand"));
}
#[test]
fn one_transcript_is_never_resumed_into_two_panes() {
let mut e = fake();
e.vers.insert(22, "2.1.100".into()); e.resolved
.insert("%2".into(), Ok("/t/a.jsonl\tpane map".into()));
let p = plan_of(&e, &opts());
assert_eq!(p.go.len(), 1);
assert_eq!(p.go[0].pane, "%1");
assert!(p.skipped[0].contains("resolves to the same transcript as %1"));
}
#[test]
fn only_panes_narrows_the_plan_without_changing_the_verdicts() {
let mut o = opts();
o.only_panes = vec!["%2".into()];
let p = plan_of(&fake(), &o);
assert!(p.go.is_empty());
assert!(p.skipped.is_empty()); }
#[test]
fn a_pane_with_no_session_process_is_reported_not_dropped() {
let rows = "%9\tw:9.9\t/w\tclaude\t0\tclaude\tno pid here";
let p = plan(rows, "2.1.258", "/l", &opts(), &fake(), "/nowhere", &|| {
"[]".into()
});
assert!(p.go.is_empty());
assert_eq!(p.skipped.len(), 1);
assert!(p.skipped[0].contains("no session process was found"));
}
#[test]
fn nothing_to_restart_says_so() {
let mut e = fake();
e.vers.insert(11, "2.1.258".into());
let out = render(&plan_of(&e, &opts()));
assert!(out.contains("nothing to restart."));
assert!(!out.contains("to restart ("));
}
#[test]
fn a_given_transcript_overrides_the_ladder() {
let mut e = fake();
e.resolved.insert("%1".into(), Err("no candidate".into()));
let mut o = opts();
o.force_transcript = Some("/given.jsonl".into());
let p = plan_of(&e, &o);
assert_eq!(p.go.len(), 1);
assert!(p.go[0].via.contains("--transcript, given"));
assert!(p.go[0].cmd.contains("--resume /given.jsonl"));
}
}