rho-coding-agent 3.10.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! The rows a daemon session's agent rail gives the sessions it started
//! with the `sessions` tool, from their jobs (`daemon_jobs`): named as
//! `/jobs` names them, with what each is doing and how long its turn ran.
//! A click on one follows that session (`pointer_actions.rs`). `/attach`
//! lists every session it started, the rail's and those it no longer shows
//! (`attach_picker.rs`).

use std::{collections::HashMap, time::Duration};

use rho_protocol::{JobState, JobSummary, SessionId};

use super::{
    agent_rail::{RailState, StartedSession},
    daemon_job::{self, ChildAgent},
    daemon_jobs::shows_on_rail,
    App,
};

/// What the rail remembers of the sessions it shows between job lists.
#[derive(Default)]
pub(super) struct StartedSessions {
    /// How long each listed session's turn has run, the last time a job
    /// list said so: a turn that ended keeps the time it ran.
    turn_times: HashMap<SessionId, Duration>,
    /// Every session the followed session started that the last job list
    /// named, as `/attach` lists them.
    listed: Vec<StartedSession>,
}

impl StartedSessions {
    /// Every session the followed session started, as the last job list
    /// named them.
    pub(super) fn listed(&self) -> &[StartedSession] {
        &self.listed
    }
}

impl App {
    /// Takes `jobs`, the jobs of the sessions the followed session started,
    /// at `now` (Unix seconds): `/attach` lists all of them, and the agent
    /// rail those worth showing. Returns whether the rail shows any.
    pub(super) fn show_started_sessions(&mut self, jobs: &[&JobSummary], now: u64) -> bool {
        let started = &mut self.daemon_rail.sessions;
        let times = &mut started.turn_times;
        times.retain(|session, _| jobs.iter().any(|job| job.session.as_ref() == Some(session)));
        let (mut rail, mut listed) = (Vec::new(), Vec::new());
        for job in jobs {
            let Some(row) = started_session(job, times, now) else {
                continue;
            };
            if shows_on_rail(job, now) {
                rail.push(row.clone());
            }
            listed.push(row);
        }
        let listed_changed = started.listed != listed;
        started.listed = listed;
        let shows = !rail.is_empty();
        if self.agent_rail.ingest_sessions(rail) | listed_changed {
            self.refresh_attach_picker();
        }
        shows
    }
}

/// The rail row of `job`, a session the followed session started, at `now`
/// (Unix seconds), noting how long its turn has run in `times`. Its time is
/// that of its turn under way, or the one that ended; a turn waiting for a
/// slot has waited since its job last changed.
fn started_session(
    job: &JobSummary,
    times: &mut HashMap<SessionId, Duration>,
    now: u64,
) -> Option<StartedSession> {
    let session = job.session.clone()?;
    let turn = job.live.as_ref().and_then(|live| live.turn_elapsed_ms);
    if let Some(ms) = turn {
        times.insert(session.clone(), Duration::from_millis(ms));
    }
    let state = state(job);
    let elapsed = match state {
        RailState::Queued => Some(Duration::from_secs(now.saturating_sub(job.updated_at))),
        RailState::Starting
        | RailState::Working
        | RailState::NeedsInput
        | RailState::Idle
        | RailState::Done
        | RailState::Failed
        | RailState::Stopped => times.get(&session).copied(),
    };
    Some(StartedSession {
        agent: job
            .child
            .as_ref()
            .and_then(ChildAgent::of)
            .as_ref()
            .and_then(ChildAgent::label),
        title: daemon_job::title(job),
        session,
        state,
        elapsed,
    })
}

/// What a started session's job says it is doing.
fn state(job: &JobSummary) -> RailState {
    if daemon_job::needs_input(job) {
        return RailState::NeedsInput;
    }
    match job.state {
        JobState::Queued => RailState::Queued,
        JobState::Running | JobState::WaitingForInput => RailState::Working,
        JobState::Idle => RailState::Idle,
        JobState::Completed => RailState::Done,
        JobState::Failed | JobState::TimedOut => RailState::Failed,
        JobState::Cancelled | JobState::Interrupted | JobState::Unknown => RailState::Stopped,
        // `JobState` is non-exhaustive: a state this build does not know
        // has nothing to wait on (see `daemon_jobs::may_still_change`).
        _ => RailState::Stopped,
    }
}

#[cfg(test)]
#[path = "daemon_started_tests.rs"]
mod tests;