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
+68
View File
@@ -34,6 +34,28 @@ pub struct Session {
pub metadata: HashMap<String, Value>,
pub status: SessionStatus,
pub created_at: DateTime<Utc>,
/// Current foreground command in the session's pane (e.g. `claude`,
/// `bash`), as tmux reports it — distinct from the `command` it was spawned
/// with. Daemon-derived (NAP-340); `None` until the daemon's activity
/// watcher has observed it. All three activity fields default on the wire so
/// older daemons/payloads stay forward-compatible.
#[serde(default)]
pub foreground: Option<String>,
/// Inferred activity dimension, orthogonal to [`SessionStatus`]
/// (NAP-340). Derived from structured tmux/`/proc` state, never from the
/// byte stream.
#[serde(default)]
pub activity: ActivityState,
/// Wall-clock time of the session's most recent observed PTY output.
/// Drives "idle for Ns" displays. Defaults to the Unix epoch on payloads
/// from daemons that predate the field.
#[serde(default = "epoch")]
pub last_activity_at: DateTime<Utc>,
}
/// Default for [`Session::last_activity_at`] when an older payload omits it.
fn epoch() -> DateTime<Utc> {
DateTime::from_timestamp(0, 0).expect("unix epoch is a valid timestamp")
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
@@ -42,6 +64,20 @@ pub enum SessionStatus {
Dead,
}
/// Inferred session activity, orthogonal to [`SessionStatus`] (NAP-340). The
/// daemon derives this from tmux pane state, PTY quiescence, and `/proc`
/// inspection — never by parsing the terminal byte stream.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
pub enum ActivityState {
/// Producing output, or the foreground process is busy.
Active,
/// Foreground process alive but quiescent. The forward-compatible default.
#[default]
Idle,
/// Blocked on a `read()` from the pty — waiting on the user.
AwaitingInput,
}
/// Optional worktree request attached to a `CreateSessionRequest`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct WorktreeSpec {
@@ -632,9 +668,41 @@ mod tests {
created_at: DateTime::parse_from_rfc3339("2026-05-24T12:00:00Z")
.unwrap()
.with_timezone(&Utc),
foreground: Some("claude".into()),
activity: ActivityState::Active,
last_activity_at: DateTime::parse_from_rfc3339("2026-05-24T12:05:00Z")
.unwrap()
.with_timezone(&Utc),
}
}
#[test]
fn session_activity_fields_default_on_legacy_payload() {
// A pre-NAP-340 daemon omits the activity fields; they must deserialize
// to the forward-compat defaults rather than failing.
let json = serde_json::json!({
"id": Uuid::from_u128(1),
"daemon_id": "laptop",
"cwd": "/tmp",
"command": "bash",
"status": "Running",
"created_at": "2026-05-24T12:00:00Z"
});
let s: Session = serde_json::from_value(json).unwrap();
assert!(s.foreground.is_none());
assert_eq!(s.activity, ActivityState::Idle);
assert_eq!(s.last_activity_at, epoch());
}
#[test]
fn activity_state_serializes_pascal_case_and_defaults_idle() {
assert_eq!(
serde_json::to_value(ActivityState::AwaitingInput).unwrap(),
serde_json::json!("AwaitingInput")
);
assert_eq!(ActivityState::default(), ActivityState::Idle);
}
#[test]
fn session_round_trips() {
let s = sample_session();