rho-coding-agent 3.10.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! The session file of a run on an external agent runtime that a host
//! follows ([`crate::session::SessionEngine::Acp`]): created with the run's
//! prompt when the run starts, so the session is listed and searched, and a
//! subagent's goes with its parent's delete, and given the run's reply when
//! it ends. A workflow node's, which has no parent, outlives its workflow
//! run's delete, after which it no longer replays.

use rho_sdk::model::ModelIdentity;

use super::ExternalRun;
use crate::{
    app::session_events::runtime_model,
    permission::PermissionMode,
    run_artifacts::RunArtifactIdentity,
    session::{PromptRow, RunArtifacts, Session, SessionEngine},
};

/// A run's session file, open until the run ends.
pub(super) struct ExternalRunFile {
    session: Session,
    prompt: String,
    /// Whether the prompt's row was saved as the run started.
    prompt_row: PromptRow,
    model: ModelIdentity,
}

impl ExternalRunFile {
    /// Creates the file of `run`'s session, for a run of the agent bound as
    /// `identity` in permission mode `mode`.
    pub(super) fn create(
        run: &ExternalRun,
        identity: &RunArtifactIdentity,
        mode: PermissionMode,
    ) -> anyhow::Result<Self> {
        let artifacts = run.status.parent().ok_or_else(|| {
            anyhow::anyhow!(
                "the run's status {} is in no directory",
                run.status.display()
            )
        })?;
        let engine = SessionEngine::Acp {
            artifacts: RunArtifacts::of(artifacts, &crate::paths::rho_dir()?)?,
            permission_mode: mode.into(),
        };
        let session = Session::create_on_engine(
            &run.cwd,
            run.session.as_str(),
            (&identity.agent_id, &identity.agent_fingerprint),
            run.provenance.clone(),
            &engine,
        )?;
        let model = ModelIdentity::new(
            identity.provider.clone(),
            identity.runtime.as_str(),
            runtime_model(identity.model.as_deref()),
        );
        // The file is there: a session that lacks its prompt still replays,
        // and its reply's save adds the prompt.
        let prompt_row = match session.save_engine_prompt(&run.prompt, model.clone()) {
            Ok(()) => PromptRow::Saved,
            Err(error) => {
                tracing::warn!(
                    session = %run.session,
                    error = format!("{error:#}"),
                    "could not save an external run's prompt to its session file"
                );
                PromptRow::Unsaved
            }
        };
        Ok(Self {
            session,
            prompt: run.prompt.clone(),
            prompt_row,
            model,
        })
    }

    /// Saves the run's `reply`, if it gave one, after its prompt, and closes
    /// the file. Logged when it could not: the file keeps the prompt, and
    /// the session still replays from the run's journal.
    pub(super) fn finish(self, reply: Option<&str>) {
        let Some(reply) = reply else {
            return;
        };
        if let Err(error) =
            self.session
                .save_engine_reply(&self.prompt, reply, self.model, self.prompt_row)
        {
            tracing::warn!(
                session = self.session.id(),
                error = format!("{error:#}"),
                "could not save an external run's reply to its session file"
            );
        }
    }
}