rho-coding-agent 3.1.0

A fast Rust agent harness with a small footprint and opinionated defaults
//! A prompt's lifecycle. A host starts each prompt with
//! [`SessionRuntime::begin_prompt`] and holds the returned [`PromptScope`]
//! until the prompt ends. Every in-prompt call (a turn, `/compact`,
//! `/workflow:`, an `/agent:` rebind, the plan handoff) takes the scope, so
//! the type, not a convention, keeps them within a prompt. Everything else a
//! prompt's end needs is the runtime's own: a turn notes a credential
//! failure for the next prompt to renew, and a turn that reached the model's
//! answer asks to name the session (see `title.rs`).

use rho_sdk::CancellationToken;

use super::{
    prompt_gate::{ActiveWork, PromptGeneration},
    SessionHandle, SessionRuntime,
};
use crate::config::Config;

/// One prompt the runtime started, held for the whole prompt. Dropping it
/// ends the prompt, however the prompt returns, so a cancel aimed at it does
/// not reach the next one.
///
/// Each scope belongs to one prompt generation: a scope dropped only after
/// a newer prompt began never ends that prompt, and reads its own as
/// cancelled.
#[must_use = "dropping a PromptScope ends the prompt at once"]
pub(crate) struct PromptScope {
    cancellation: CancellationToken,
    handle: SessionHandle,
    generation: PromptGeneration,
    /// Whether the prompt's start attached the session's deferred tool
    /// sources, which changed its tool list.
    attached_tool_sources: bool,
}

impl PromptScope {
    /// Opens the next prompt on `handle`'s gate.
    pub(super) fn begin(handle: &SessionHandle) -> Self {
        let (cancellation, generation) = handle.gate().begin();
        Self {
            cancellation,
            handle: handle.clone(),
            generation,
            attached_tool_sources: false,
        }
    }

    /// Whether the session's tool list changed as the prompt started,
    /// because its deferred tool sources attached then.
    pub(crate) fn attached_tool_sources(&self) -> bool {
        self.attached_tool_sources
    }

    /// Trips when the prompt is cancelled before its first run starts, and
    /// is already cancelled when the session is closing, so work the host
    /// does first, such as expanding a command, can stop.
    pub(crate) fn cancellation(&self) -> &CancellationToken {
        &self.cancellation
    }

    /// Whether this prompt was cancelled, at any point so far, or the
    /// session is closing.
    pub(super) fn is_cancelled(&self) -> bool {
        self.handle.gate().is_cancelled(self.generation)
    }

    /// Makes `work` what this prompt runs now, so a cancel reaches it, and
    /// one that already landed cancels it at once; a model run's steering
    /// goes to [`SessionHandle::steer`].
    pub(super) fn activate(&self, work: ActiveWork) {
        self.handle.gate().activate(self.generation, work);
    }
}

impl Drop for PromptScope {
    fn drop(&mut self) {
        self.handle.gate().finish(self.generation);
    }
}

impl SessionRuntime {
    /// Starts a prompt: refuses it while a compaction's rewrite is unsaved,
    /// opens the window in which a cancel reaches it, rebuilds what the
    /// session built on providers `process_config` changed (see
    /// `providers.rs`), renews credentials a logout or a refused request
    /// left stale, and waits for tool sources
    /// the session still loads. Hold the returned scope
    /// for the whole prompt and pass it to each of the prompt's calls.
    pub(crate) async fn begin_prompt(
        &mut self,
        process_config: &Config,
    ) -> anyhow::Result<PromptScope> {
        if self.unsaved_compaction {
            return Err(super::UnsavedCompaction.into());
        }
        let mut scope = PromptScope::begin(&self.handle);
        // Providers first: a changed host leaves the provider stale for the
        // renewal to rebuild.
        let renewed = match self.refresh_providers(process_config).await {
            Ok(()) => self.renew_stale_credentials(process_config).await,
            Err(error) => Err(error),
        };
        // With nothing to renew from, as after a logout, the renewal's read
        // cached the miss in the OS store, and the login that fixes it may
        // run in another process: the next prompt rereads.
        self.note_credential_failure(&renewed);
        renewed?;
        // The prompt's tools must be there before it runs. A cancel stops
        // the wait, and the prompt then ends as cancelled. Last, so a
        // prompt whose start attached them returns the scope that says so.
        if let Some(loaded) = self.tool_sources_loaded() {
            tokio::select! {
                () = scope.cancellation().cancelled() => {}
                () = loaded => {}
            }
        }
        scope.attached_tool_sources = self.attach_loaded_tool_sources(process_config).await?;
        Ok(scope)
    }
}