rho-coding-agent 3.10.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! How a session's turns are recorded in the job registry once it is added.

use super::{stopping, writing};
use crate::app::daemon_host::jobs::{JobAgent, JobRecord, JobRegistry, RegisterError, SessionJob};
use rho_protocol::{ErrorCode, RpcError};
use rho_sdk::SessionId;

/// What a session's job records of it as the session opens.
pub(super) struct JobOpening {
    /// The agent it runs as.
    pub(super) agent: JobAgent,
    /// The title it opens with, its saved one, if it has one.
    pub(super) title: Option<String>,
}

/// How a session's turns are recorded once it is added.
pub(super) enum SessionJobRecord {
    /// On a new job, registered as the session is added.
    Register(JobRecord),
    /// On the session's last job, which an earlier daemon or an earlier load
    /// recorded.
    Reuse(JobRecord),
}

impl SessionJobRecord {
    pub(super) fn job(&self) -> &JobRecord {
        match self {
            Self::Register(record) | Self::Reuse(record) => record,
        }
    }

    /// Records the job in `jobs` as the one of `session`'s turns, which
    /// open as `opening` says, refusing it when the registry no longer agrees, as
    /// when another job began writing the session while it reopened
    /// (#1661).
    pub(super) fn claim(
        self,
        jobs: &JobRegistry,
        session: &SessionId,
        opening: JobOpening,
    ) -> Result<(), RpcError> {
        let JobOpening { agent, title } = opening;
        match (self, jobs.session_job(session)) {
            (Self::Register(record), SessionJob::New) => {
                let record = JobRecord {
                    agent: Some(agent),
                    title,
                    ..record
                };
                jobs.register(record).map_err(|error| match error {
                    // Stopping, or restarting for an upgrade.
                    RegisterError::Closed => stopping(),
                    RegisterError::Io(error) => RpcError::new(
                        ErrorCode::InternalError,
                        format!("could not record the session's job: {error}"),
                    ),
                })
            }
            (Self::Reuse(record), SessionJob::Last(last)) if last.id == record.id => {
                // The session reopens with the grant its environment
                // carries, held to the job's limit, so its record says the
                // same.
                jobs.set_grant(&record.id, record.grant);
                jobs.set_agent(&record.id, agent);
                if let Some(title) = title {
                    jobs.set_title(&record.id, &title);
                }
                Ok(())
            }
            (_, SessionJob::Busy(job)) => Err(writing(&job, session)),
            (Self::Register(_) | Self::Reuse(_), SessionJob::New | SessionJob::Last(_)) => {
                Err(RpcError::new(
                    ErrorCode::SessionBusy,
                    format!("session {session}'s job changed while the daemon opened it"),
                ))
            }
        }
    }
}