feat(maestro): infer session activity state + foreground (Active/Idle/AwaitingInput) (#18)
ci / rust (push) Failing after 2m15s

Add an orthogonal activity dimension to sessions, inferred entirely from
structured tmux/`/proc` state — no PTY byte-stream parsing, no work on the
attach hot path.

Protocol: `ActivityState` enum (Active/Idle/AwaitingInput) + three additive,
serde-defaulted `Session` fields (`foreground`, `activity`, `last_activity_at`)
so the snapshot/diff flow stays forward-compatible with older daemons.

Detection (piggybacks the existing ~2s liveness tick, no new timer):
- foreground command via `tmux list-panes -F '#{pane_pid} #{pane_current_command}
  #{pane_tty}'`;
- quiescence from a per-`PtyBridge` last-read `Instant`, bumped in `run_reader`;
- AwaitingInput via best-effort `/proc` walk to the deepest descendant: state
  `S` + a non-timer `wchan` + an stdio fd on the pane tty. (`/proc/pid/syscall`
  is ptrace-gated and unreadable for own sessions, so `wchan` distinguishes a
  blocking read from a plain `sleep`.)
- combine: recent output → Active; else input-blocked → AwaitingInput; else
  Idle. Emit `SessionUpdated` only on (activity, foreground) change — no diff
  storms at steady state.

`maestro list` gains an ACTIVITY column and shows the live foreground. Pure
combine/parse logic is unit-tested; an e2e asserts `cat` → AwaitingInput and
`sleep` → Idle with correct foreground against real tmux.

Closes #18
Closes #8

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-21 14:51:08 +00:00
co-authored by Claude Opus 4.8
parent c8bf240624
commit fd80ae152a
21 changed files with 580 additions and 9 deletions
+59
View File
@@ -522,6 +522,65 @@ async fn spawn_records_history_and_infers_gitroot_for_form() {
client.kill_session(s.id).await.expect("kill");
}
#[tokio::test(flavor = "multi_thread")]
async fn activity_reflects_foreground_and_input_blocking() {
// NAP-340 (#18): the watcher infers each session's foreground command and
// activity from structured tmux/proc state. `cat` parks in read() on the
// pty → AwaitingInput; `sleep` is quiescent but not reading the tty → Idle
// (the distinction the /proc/<pid>/syscall check buys us). Both report the
// right foreground. Exercised against real tmux.
use maestro_protocol::ActivityState;
let (_reg, _daemons, client) = bring_up(&["host"]).await;
let cwd = TempDir::new().unwrap();
let cat = client
.spawn_session(&create_req("host", cwd.path(), "cat"))
.await
.expect("spawn cat");
let slp = client
.spawn_session(&create_req("host", cwd.path(), "sleep 300"))
.await
.expect("spawn sleep");
// Initial state is Active; the watcher needs a couple ticks past the
// quiescence window (3s) to settle into the steady states.
wait_for_async(
"cat → AwaitingInput, sleep → Idle, with correct foreground",
Duration::from_secs(20),
|| {
let client = client.clone();
let (cat_id, sleep_id) = (cat.id, slp.id);
async move {
let sessions = match client.list_sessions().await {
Ok(s) => s,
Err(_) => return false,
};
let cat_ok = sessions
.iter()
.find(|s| s.id == cat_id)
.map(|s| {
s.activity == ActivityState::AwaitingInput
&& s.foreground.as_deref() == Some("cat")
})
.unwrap_or(false);
let sleep_ok = sessions
.iter()
.find(|s| s.id == sleep_id)
.map(|s| {
s.activity == ActivityState::Idle
&& s.foreground.as_deref() == Some("sleep")
})
.unwrap_or(false);
cat_ok && sleep_ok
}
},
)
.await;
client.kill_session(cat.id).await.expect("kill cat");
client.kill_session(slp.id).await.expect("kill sleep");
}
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------