rho-coding-agent 3.12.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! A started child's own input requests, put to the host under the child.
//!
//! A session the parent started with the `sessions` tool asks a person for
//! its approvals, plan approvals and questions, as any session does; a
//! subagent's questions come through its parent's requests instead
//! ([`super::child_kind::ChildKind::asks_host`]). The child's relay hands
//! its observed requests here, and each is asked and answered as a root
//! session's are ([`crate::app::acp::inputs`]), scoped to the child's
//! `sessionId`. The first answer from any client wins; dropping this, as a
//! relay that stops does, withdraws what it asked.
//!
//! The child runs on its own, so asking leaves a question's timeout
//! running ([`QuestionTimeouts::Keep`]): one nobody answers falls back to
//! its default answers, as it did before the host was asked.

use std::sync::{atomic::AtomicU64, Arc};

use agent_client_protocol::{v1::schema as v1, v2::schema as v2};
use rho_protocol::{InputRequest, SessionEvent, SessionId};

use super::port::V2Port;
use crate::app::{
    acp::{
        host_features::HostFeatures,
        inputs::{Asking, Asks, InputContext, Inputs, QuestionTimeouts, TurnCancels},
    },
    daemon_client::Daemon,
};

/// One child's requests open on the host.
pub(super) struct ChildInputs {
    open: Inputs,
    context: InputContext,
    /// The requests open as the current observe began, asked once its
    /// replay is through, so the host has seen the calls they name.
    listed: Option<Vec<InputRequest>>,
}

impl ChildInputs {
    /// The requests of `session`, asked of the host under `child` through
    /// `port` as `asking` allows, as a host with `features` draws them;
    /// `placeholders` numbers the cards of calls the host never saw.
    pub(super) fn new(
        daemon: Arc<Daemon>,
        session: SessionId,
        child: &v2::SessionId,
        port: Arc<V2Port>,
        features: HostFeatures,
        placeholders: Arc<AtomicU64>,
        asking: Asking,
    ) -> Self {
        let context = InputContext {
            daemon,
            session,
            acp_id: v1::SessionId::new(child.0.as_ref()),
            port,
            features,
            placeholders,
            // Nothing waits for a child's requests to be withdrawn: closing
            // its parent aborts the relay, whose drop withdraws them, and
            // answers without waiting.
            asks: Asks::default(),
            // A host may not cancel a started child's turn, so no cancel
            // withdraws what it asked.
            cancels: TurnCancels::default(),
            timeouts: QuestionTimeouts::Keep,
            asking,
        };
        Self {
            open: Inputs::default(),
            context,
            listed: None,
        }
    }

    /// Takes the requests open as an observe began.
    pub(super) fn listed(&mut self, requests: Vec<InputRequest>) {
        self.listed = Some(requests);
    }

    /// Asks the requests listed as the observe began, once its replay is
    /// through, and withdraws the rest.
    pub(super) fn caught_up(&mut self) {
        if let Some(requests) = self.listed.take() {
            self.open.reconcile(requests, &self.context);
        }
    }

    /// Takes a live event of the child's: a request is asked, and one
    /// resolved, here or elsewhere, is withdrawn.
    pub(super) fn event(&mut self, event: &SessionEvent) {
        match event {
            SessionEvent::InputRequested { request } => {
                self.open.open(request.as_ref().clone(), &self.context);
            }
            SessionEvent::InputResolved { id, .. } => self.open.resolve(*id),
            _ => {}
        }
    }
}