orch-cli 0.105.13

Orch, the agent orchestration engine: runs coding agents on tasks and cron jobs
Documentation
//! Apply one store write: a direct write on a single node, or a command Raft replicated

use sqlx::SqliteConnection;

use crate::store::dependencies::apply as dep_apply;
use crate::store::jobs::apply as jobs_apply;
use crate::store::kv;
use crate::store::tasks::apply as task_apply;
use crate::store::{Command, Reply};

/// `replicated` is true when Raft applies the command, so new ids come from the replicated
/// rows. A direct write keeps SQLite's AUTOINCREMENT, as orch on one machine always has
pub(crate) async fn run(
    cmd: Command,
    conn: &mut SqliteConnection,
    now: &str,
    replicated: bool,
) -> anyhow::Result<Reply> {
    if replicated {
        cmd.check_replicable()?;
    }

    Ok(match cmd {
        Command::KvSet { key, value } => {
            kv::set(conn, now, &key, &value).await?;
            Reply::Done
        }
        Command::KvIncrement { key } => Reply::Count(kv::increment(conn, now, &key).await?),
        Command::KvInsertIfAbsent { key, value } => {
            Reply::Value(kv::insert_if_absent(conn, now, &key, &value).await?)
        }
        Command::KvDelete { key } => {
            kv::delete(conn, &key).await?;
            Reply::Done
        }
        cmd @ (Command::MoveRepo { .. }
        | Command::DepReplace { .. }
        | Command::DepLinkKnown { .. }
        | Command::DepBlockClaim { .. }) => dep_apply::run(cmd, conn, now).await?,
        Command::JobStateUpsert(state) => {
            jobs_apply::upsert_job_state(conn, &state).await?;
            Reply::Done
        }
        Command::ChannelSubscribe {
            channel,
            thread_id,
            repo,
            topic_id,
        } => {
            jobs_apply::subscribe(conn, &channel, &thread_id, &repo, &topic_id).await?;
            Reply::Done
        }
        Command::ChannelUnsubscribe {
            channel,
            thread_id,
            repo,
        } => {
            jobs_apply::unsubscribe(conn, &channel, &thread_id, &repo).await?;
            Reply::Done
        }
        task => task_apply::run(task, conn, now, replicated).await?,
    })
}