rho-coding-agent 3.12.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! A session's reports to the session that started it, for a session
//! another session started as an agent that reports to its starter
//! (`starter_hears: reports`; see `agent::StarterHears`).
//!
//! Such a session works on its own between reports, so most of its turns
//! end telling its starter nothing. Its `report_to_starter` tool marks the
//! turn under way with a [`ReportKind`] in the session's [`StarterReport`],
//! and as that turn ends the daemon tells the starter, with the turn's
//! final message as the report (see `daemon_host::sessions::peer_wake`).

use std::sync::{Arc, Mutex, MutexGuard, PoisonError};

use rho_sdk::SessionId;
use serde::{Deserialize, Serialize};

use super::self_questions::SelfQuestions;

/// What a report says of the reporting session's work.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub(crate) enum ReportKind {
    /// Its whole task is finished.
    Done,
    /// It can't go on without a decision its starter or a person makes.
    NeedsDecision,
    /// Something it can't fix stops it.
    Blocked,
}

impl ReportKind {
    /// Every kind, as the tool offers them.
    pub(crate) const ALL: [Self; 3] = [Self::Done, Self::NeedsDecision, Self::Blocked];

    /// The kind's name, as serde writes it.
    pub(crate) fn as_str(self) -> &'static str {
        match self {
            Self::Done => "done",
            Self::NeedsDecision => "needs_decision",
            Self::Blocked => "blocked",
        }
    }
}

/// Where a reporting session's turn marks its report, whom it reports to,
/// and where it asks its own questions, which it does without waiting (see
/// `app::self_questions`). Cheap to clone; clones share the mark and the
/// questions.
#[derive(Clone, Debug)]
pub(crate) struct StarterReport {
    starter: SessionId,
    marked: Arc<Mutex<Option<ReportKind>>>,
    questions: SelfQuestions,
}

impl StarterReport {
    /// The reports of a session that session `starter` started.
    pub(crate) fn to(starter: SessionId) -> Self {
        Self {
            starter,
            marked: Arc::default(),
            questions: SelfQuestions::new(),
        }
    }

    /// The session the reports go to.
    pub(crate) fn starter(&self) -> &SessionId {
        &self.starter
    }

    /// Where the session asks its own questions, and their answers arrive.
    pub(crate) fn questions(&self) -> &SelfQuestions {
        &self.questions
    }

    /// Marks the turn under way as reporting `kind`; a later mark in the
    /// same turn replaces it.
    pub(crate) fn mark(&self, kind: ReportKind) {
        *self.marked() = Some(kind);
    }

    /// The report the turn that just ended marked, if it marked one; the
    /// next turn starts unmarked.
    pub(crate) fn take(&self) -> Option<ReportKind> {
        self.marked().take()
    }

    fn marked(&self) -> MutexGuard<'_, Option<ReportKind>> {
        // Only ever replaced or taken whole.
        self.marked.lock().unwrap_or_else(PoisonError::into_inner)
    }
}