A plain deadline loop says the same thing without catching a panic to turn it back into an assert. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
94 lines
3.1 KiB
Rust
94 lines
3.1 KiB
Rust
//! Activity detection against real processes on a real tty.
|
|
//!
|
|
//! The review noted this pipeline had no end-to-end coverage — `activity::decide`
|
|
//! was unit-tested, but nothing exercised the `/proc` inspection that feeds it.
|
|
//! These use a real tmux pane because that is the only way to get a controlling
|
|
//! terminal with a genuine foreground process group.
|
|
|
|
mod common;
|
|
|
|
use std::time::Duration;
|
|
|
|
use common::TmuxFixture;
|
|
use maestro_daemon::proc::awaiting_input;
|
|
use tempfile::TempDir;
|
|
use uuid::Uuid;
|
|
|
|
/// Poll `awaiting_input` until it reports `want`, so we don't race the shell
|
|
/// reaching its blocking read. Reports what it last saw — the useful half of
|
|
/// the failure.
|
|
fn wait_for_awaiting(fx: &TmuxFixture, id: Uuid, want: bool, label: &str) {
|
|
let deadline = std::time::Instant::now() + Duration::from_secs(5);
|
|
let mut last = None;
|
|
while std::time::Instant::now() < deadline {
|
|
if let Ok(Some(pane)) = fx.tmux.pane_info(id) {
|
|
let got = awaiting_input(pane.pane_pid, &pane.tty);
|
|
if got == want {
|
|
return;
|
|
}
|
|
last = Some(got);
|
|
}
|
|
std::thread::sleep(Duration::from_millis(25));
|
|
}
|
|
panic!("{label}: wanted awaiting_input={want}, last saw {last:?}");
|
|
}
|
|
|
|
#[test]
|
|
fn an_idle_shell_is_awaiting_input() {
|
|
let fx = TmuxFixture::new();
|
|
let cwd = TempDir::new().unwrap();
|
|
let id = Uuid::new_v4();
|
|
fx.tmux
|
|
.new_session(id, cwd.path(), "bash --noprofile --norc")
|
|
.expect("new_session");
|
|
|
|
wait_for_awaiting(&fx, id, true, "an idle shell sits in read()");
|
|
}
|
|
|
|
/// The case the old heuristic got wrong. It walked `children` to the deepest
|
|
/// descendant, so it landed on the background `sleep` — which is not on the
|
|
/// terminal — and reported the session as *not* awaiting input, while the
|
|
/// foreground process was blocked on `read`.
|
|
#[test]
|
|
fn a_foreground_reader_with_a_background_helper_is_awaiting_input() {
|
|
let fx = TmuxFixture::new();
|
|
let cwd = TempDir::new().unwrap();
|
|
let script = cwd.path().join("agentish.sh");
|
|
std::fs::write(&script, "#!/bin/bash\nsleep 300 &\nread -r line\n").unwrap();
|
|
#[cfg(unix)]
|
|
{
|
|
use std::os::unix::fs::PermissionsExt;
|
|
std::fs::set_permissions(&script, std::fs::Permissions::from_mode(0o755)).unwrap();
|
|
}
|
|
|
|
let id = Uuid::new_v4();
|
|
fx.tmux
|
|
.new_session(
|
|
id,
|
|
cwd.path(),
|
|
&format!("bash --noprofile --norc {}", script.display()),
|
|
)
|
|
.expect("new_session");
|
|
|
|
wait_for_awaiting(
|
|
&fx,
|
|
id,
|
|
true,
|
|
"a foreground reader must win over a newer background child",
|
|
);
|
|
}
|
|
|
|
/// A pure timer sleep also parks in 'S' with the terminal inherited; it must
|
|
/// read as idle, not as waiting on the user.
|
|
#[test]
|
|
fn a_foreground_sleep_is_not_awaiting_input() {
|
|
let fx = TmuxFixture::new();
|
|
let cwd = TempDir::new().unwrap();
|
|
let id = Uuid::new_v4();
|
|
fx.tmux
|
|
.new_session(id, cwd.path(), "sleep 300")
|
|
.expect("new_session");
|
|
|
|
wait_for_awaiting(&fx, id, false, "a timer sleep is not user input");
|
|
}
|