orch-cli 0.112.0

Orch, the agent orchestration engine: runs coding agents on tasks and cron jobs
Documentation
//! Moving one task out of `blocked`, shared by `orch task unblock` and the job scheduler

use crate::backends::{ExternalBackend, ExternalId, Status};
use crate::engine::tasks::status_to_task_status;
use crate::store::{self, TaskStatus, TaskStore};
use std::sync::Arc;

/// Where an unblocked task went
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Unblocked {
    New,
    Waiting,
}

/// Store-first status update: writes SQLite, then mirrors to the backend
pub async fn update_status_store_first(
    store: Option<&Arc<TaskStore>>,
    backend: &Arc<dyn ExternalBackend>,
    repo: &str,
    id: &ExternalId,
    status: Status,
) -> anyhow::Result<()> {
    let db_status = status_to_task_status(status);
    if let Some(s) = store
        && let Ok(Some(store_id)) = s.resolve_task_id(repo, &id.0).await
    {
        s.update_status(store_id, db_status).await?;
    }
    if let Err(e) = backend.update_status(id, status).await {
        tracing::warn!(
            task_id = id.0,
            ?status,
            err = %e,
            "failed to mirror status to backend — store is authoritative"
        );
    }
    Ok(())
}

/// Reset the task's counters and move it to `new`, or to `waiting` while a dependency is open.
/// An internal task has no `backend`
pub async fn unblock_task(
    store: &Arc<TaskStore>,
    backend: Option<&Arc<dyn ExternalBackend>>,
    repo: &str,
    external_id: &str,
    store_id: Option<i64>,
) -> anyhow::Result<Unblocked> {
    let shared = Some(Arc::clone(store));
    store::store_reset_counters(&shared, repo, external_id).await;
    if let Some(store_id) = store_id
        && store.status_after_unblock(store_id).await? == TaskStatus::Waiting
    {
        if let Some(backend) = backend {
            let _ = backend
                .update_status(&ExternalId(external_id.to_string()), Status::Waiting)
                .await;
        }
        return Ok(Unblocked::Waiting);
    }

    if let Some(backend) = backend {
        let id = ExternalId(external_id.to_string());
        update_status_store_first(Some(store), backend, repo, &id, Status::New).await?;
    } else {
        let store_id = store_id
            .ok_or_else(|| anyhow::anyhow!("internal task {external_id} has no store id"))?;
        store.update_status(store_id, TaskStatus::New).await?;
    }
    Ok(Unblocked::New)
}