rho-coding-agent 3.7.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! The prompts a session knows by their idempotency key (#1777), which a
//! prompt sent again with the key answers as, starting nothing.
//!
//! A keyed prompt's user message is saved under a [`MessageKey`] that names
//! a one-way digest of the session and the key, then the prompt's turn
//! ([`row_key`]). A load finds every keyed prompt in the session's saved
//! rows, on every branch, so the daemon knows a key as long as its message
//! is saved, across restarts too, and only the digest is ever saved or shown. A prompt that
//! saves no message, such as `/compact`, is known while the daemon keeps the
//! session loaded, and, as the job records the latest turn's digest and
//! message, until another turn starts.

use std::{collections::HashMap, sync::Arc};

use rho_protocol::{IdempotencyKey, MessageId, TurnId};
use rho_sdk::SessionId;
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use tokio::sync::watch;

use super::{
    super::jobs::{JobRecord, TurnRecord},
    PromptStart,
};
use crate::{app::session_events::user_message_id, session::tree::MessageKey};

/// A prompt's idempotency key as the session keeps it: a one-way digest of
/// the session's id and the key, so a key names nothing in another session
/// and a saved row never reveals it.
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub(in crate::app::daemon_host) struct KeyDigest(String);

/// How many hex digits of the SHA-256 a [`KeyDigest`] keeps: 128 bits, as
/// many as the UUID the ACP spec suggests clients use for the key.
const DIGEST_HEX: usize = 32;

/// What a [`row_key`] starts with.
const ROW_KEY_PREFIX: &str = "prompt-";

impl KeyDigest {
    pub(super) fn of(session: &SessionId, key: &IdempotencyKey) -> Self {
        let session = session.as_str();
        let mut hash = Sha256::new();
        // Length-prefixed, so no other session and key hash the same bytes.
        hash.update((session.len() as u64).to_be_bytes());
        hash.update(session);
        hash.update(key.as_str());
        let mut hex = hex::encode(hash.finalize());
        hex.truncate(DIGEST_HEX);
        Self(hex)
    }
}

/// The key the user message of turn `turn`, whose prompt named `digest`, is
/// saved under. A replay names the message from it, as live does.
pub(super) fn row_key(digest: &KeyDigest, turn: &TurnId) -> MessageKey {
    MessageKey::new(format!("{ROW_KEY_PREFIX}{}-{turn}", digest.0))
}

/// The digest and turn a saved row's key names, when a keyed prompt's user
/// message was saved under it.
fn parse_row_key(key: &MessageKey) -> Option<(KeyDigest, TurnId)> {
    let rest = key.as_str().strip_prefix(ROW_KEY_PREFIX)?;
    let digest = rest.get(..DIGEST_HEX)?;
    let turn = rest.get(DIGEST_HEX..)?.strip_prefix('-')?;
    let hex = digest.bytes().all(|byte| byte.is_ascii_hexdigit());
    (hex && !turn.is_empty()).then(|| (KeyDigest(digest.to_owned()), TurnId::new(turn)))
}

/// The user message a prompt's turn inserted, set before the turn tells its
/// prompt it began ([`PromptStart::Began`]), so the request, and one that
/// sends the prompt again, answer with it. A turn that began, or ended,
/// without inserting one settles it unset; one that only queued for
/// capacity has yet to insert it, so a prompt sent again waits for it
/// ([`Self::settled`]).
#[derive(Clone, Debug)]
pub(super) struct PromptMessage(Arc<watch::Sender<Inserting>>);

/// How far the turn of a [`PromptMessage`] got with its user message.
#[derive(Clone, Debug, PartialEq, Eq)]
enum Inserting {
    /// It may yet insert one.
    Pending,
    Inserted(MessageId),
    /// It began, or ended, inserting none the daemon knew.
    None,
}

impl Default for PromptMessage {
    fn default() -> Self {
        Self(Arc::new(watch::channel(Inserting::Pending).0))
    }
}

impl PromptMessage {
    /// A turn that began, having inserted `message` if it is known.
    fn settled_as(message: Option<MessageId>) -> Self {
        let inserting = message.map_or(Inserting::None, Inserting::Inserted);
        Self(Arc::new(watch::channel(inserting).0))
    }

    /// Records the turn's user message, unless it settled already.
    pub(super) fn set(&self, message: MessageId) {
        self.0.send_if_modified(|inserting| {
            let pending = *inserting == Inserting::Pending;
            if pending {
                *inserting = Inserting::Inserted(message);
            }
            pending
        });
    }

    /// The turn began or ended: a message it has not inserted by now it
    /// never inserts.
    pub(super) fn settle(&self) {
        self.0.send_if_modified(|inserting| {
            let pending = *inserting == Inserting::Pending;
            if pending {
                *inserting = Inserting::None;
            }
            pending
        });
    }

    /// The message, if the turn inserted one by now.
    pub(super) fn get(&self) -> Option<MessageId> {
        match &*self.0.borrow() {
            Inserting::Inserted(message) => Some(message.clone()),
            Inserting::Pending | Inserting::None => None,
        }
    }

    /// The message, once the turn inserted it or settled without one.
    pub(super) async fn settled(&self) -> Option<MessageId> {
        let mut inserting = self.0.subscribe();
        // The sender lives as long as `self`.
        let _ = inserting
            .wait_for(|inserting| *inserting != Inserting::Pending)
            .await;
        self.get()
    }
}

/// The turn an earlier prompt naming a key started, as a prompt sent again
/// with the key answers.
#[derive(Clone)]
pub(super) struct SentBefore {
    pub(super) id: TurnId,
    /// How the turn began, once it has.
    pub(super) start: watch::Receiver<PromptStart>,
    pub(super) message: PromptMessage,
}

impl SentBefore {
    /// A turn that began, inserting `message` if it is known.
    fn began(id: TurnId, message: Option<MessageId>) -> Self {
        // The sender is dropped: the turn began, and nothing changes that.
        let (_, start) = watch::channel(PromptStart::Began);
        let message = PromptMessage::settled_as(message);
        Self { id, start, message }
    }
}

/// The keyed prompts a session knows, by their digest, which its turn lock
/// guards.
pub(super) struct PromptKeys(HashMap<KeyDigest, SentBefore>);

impl PromptKeys {
    /// The keyed prompts a session's load finds: those whose user message
    /// it saved under one of `saved` keys, and the turn its job `job` last
    /// ran, as an earlier daemon or an earlier load recorded it, if a
    /// prompt naming a key started it and it began. One that did not begin
    /// ran nothing, so its key starts a turn again.
    ///
    /// `saved` are the keys of the rows on every branch of the session's
    /// tree, not only the one it continues: a message on a branch the
    /// session moved away from is still kept, and selecting that branch
    /// again shows it, so its key still names it, as the spec has a key
    /// known while its message is kept. A move between branches then never
    /// changes what the session knows.
    pub(super) fn loaded(job: &JobRecord, saved: &[MessageKey]) -> Self {
        let mut keys: HashMap<_, _> = saved
            .iter()
            .filter_map(|key| {
                let (digest, turn) = parse_row_key(key)?;
                Some((digest, SentBefore::began(turn, Some(user_message_id(key)))))
            })
            .collect();
        if let Some(TurnRecord {
            id,
            key_digest: Some(digest),
            began: true,
            message,
            continues_after_restart: _,
        }) = job.turn.clone()
        {
            // A saved row names the turn's message as well as the record.
            keys.entry(digest)
                .or_insert_with(|| SentBefore::began(id, message));
        }
        Self(keys)
    }

    /// The turn an earlier prompt naming `digest` started, if that turn
    /// began or may yet. A turn that ended before it began ran nothing: its
    /// key starts a turn again.
    pub(super) fn sent_before(&self, digest: &KeyDigest) -> Option<SentBefore> {
        self.0
            .get(digest)
            .filter(|sent| !matches!(*sent.start.borrow(), PromptStart::Ended(_)))
            .cloned()
    }

    /// The prompt naming `digest` started the turn `sent`.
    pub(super) fn turn_started(&mut self, digest: KeyDigest, sent: SentBefore) {
        self.0.insert(digest, sent);
    }
}