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
+56
View File
@@ -0,0 +1,56 @@
//! Activity-state inference combine logic (NAP-340).
//!
//! Pure decision from three structured signals — PTY quiescence, foreground
//! process, and `/proc` block-state — so it's unit-testable without tmux or a
//! live session. The orchestration (gathering the signals each liveness tick
//! and emitting `SessionUpdated` on change) lives in [`crate::session`].
use std::time::Duration;
use maestro_protocol::ActivityState;
/// PTY output more recent than this keeps a session `Active`. It must exceed the
/// liveness tick (~2s, [`crate::session::DEFAULT_LIVENESS_INTERVAL`]) so a
/// session emitting a chunk every tick stays `Active` instead of flapping
/// Active↔Idle. This window *is* the debounce the issue calls for.
pub const ACTIVE_WINDOW: Duration = Duration::from_secs(3);
/// Combine the observed signals into an [`ActivityState`]:
/// recent output ⇒ `Active`; else blocked on a terminal read ⇒ `AwaitingInput`;
/// else `Idle`.
pub fn decide(idle_for: Duration, awaiting_input: bool) -> ActivityState {
if idle_for < ACTIVE_WINDOW {
ActivityState::Active
} else if awaiting_input {
ActivityState::AwaitingInput
} else {
ActivityState::Idle
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn recent_output_is_active_regardless_of_block_state() {
assert_eq!(decide(Duration::from_millis(100), false), ActivityState::Active);
assert_eq!(decide(Duration::from_millis(100), true), ActivityState::Active);
}
#[test]
fn quiescent_and_blocked_is_awaiting_input() {
assert_eq!(
decide(ACTIVE_WINDOW + Duration::from_secs(1), true),
ActivityState::AwaitingInput
);
}
#[test]
fn quiescent_and_not_blocked_is_idle() {
assert_eq!(
decide(ACTIVE_WINDOW + Duration::from_secs(1), false),
ActivityState::Idle
);
}
}