hird 0.3.0

Cross-harness agent work queue and shared assertion memory, backed by one local SQLite database
Documentation
//! Error type shared by the repository layer.
//!
//! Every variant's `Display` is written to be relayed verbatim to a human or a
//! model โ€” see the "errors are descriptive strings" rule in DESIGN.md ยง6.

use crate::model::{Blocker, Conflict, Question, Recess, Recusal, Status};

pub type Result<T> = std::result::Result<T, Error>;

#[derive(Debug, thiserror::Error)]
pub enum Error {
    #[error("task {seq} not found")]
    TaskNotFound { seq: i64 },

    #[error("assertion {id} not found")]
    AssertionNotFound { id: String },

    /// A claim lost the race, or the task was never claimable.
    #[error("{}", claim_conflict_message(*.seq, *.status, .holder.as_deref(), .lease_expires_at.as_deref()))]
    ClaimConflict {
        seq: i64,
        status: Status,
        holder: Option<String>,
        lease_expires_at: Option<String>,
    },

    /// Someone other than the lease holder tried to drive the task.
    #[error("{}", not_holder_message(*.seq, *.status, .holder.as_deref(), .actor))]
    NotHolder {
        seq: i64,
        status: Status,
        holder: Option<String>,
        actor: String,
    },

    /// The requested move is not an edge of the status machine.
    #[error("cannot {transition} task {seq}: it is {status}")]
    InvalidTransition {
        seq: i64,
        status: Status,
        transition: &'static str,
    },

    /// The task's dependencies have not all finished.
    #[error("{}", blocked_message(*.seq, .blockers))]
    Blocked { seq: i64, blockers: Vec<Blocker> },

    /// The task is open, but its last holder asked for input no agent should
    /// guess at. The human path must answer before claims resume.
    #[error("{}", awaiting_answer_message(*.seq, .question))]
    AwaitingAnswer { seq: i64, question: Box<Question> },

    /// Adding this dependency would close a cycle in the graph.
    #[error("{}", cycle_message(*.seq, *.on, .path))]
    DependencyCycle {
        seq: i64,
        on: i64,
        /// The existing chain from `on` back to `seq`, which the new edge
        /// would close.
        path: Vec<i64>,
    },

    /// This harness is the one barred from working the task.
    #[error("{}", recused_message(*.seq, .recusal, .actor))]
    Recused {
        seq: i64,
        recusal: Box<Recusal>,
        actor: String,
    },

    /// The policy serving this connection refused the move.
    #[error("task {seq}: {reason}")]
    Gated { seq: i64, reason: String },

    /// The task is ready, but this session lacks capabilities it requires.
    #[error("{}", missing_capabilities_message(*.seq, .required, .available))]
    MissingCapabilities {
        seq: i64,
        required: Vec<String>,
        available: Vec<String>,
    },

    /// The task's project is in recess: the human stood the queue down, so no
    /// claim is handed out until they lift it. Work in flight is untouched.
    #[error("task {seq} is in recess: {}", .recess.describe())]
    InRecess { seq: i64, recess: Recess },

    /// The declared file scope overlaps work another agent is doing.
    #[error("{}", conflict_message(*.seq, .conflicts))]
    PathConflict { seq: i64, conflicts: Vec<Conflict> },

    #[error("{0}")]
    Invalid(String),

    #[error(transparent)]
    Sqlite(#[from] rusqlite::Error),
}

impl Error {
    pub fn invalid(msg: impl Into<String>) -> Self {
        Error::Invalid(msg.into())
    }
}

fn claim_conflict_message(
    seq: i64,
    status: Status,
    holder: Option<&str>,
    lease_expires_at: Option<&str>,
) -> String {
    match (holder, lease_expires_at) {
        (Some(holder), Some(expires)) => {
            format!("task {seq} is {status} by {holder} until {expires}")
        }
        (Some(holder), None) => format!("task {seq} is {status} by {holder}"),
        _ => format!("task {seq} is {status}, not open, so it cannot be claimed"),
    }
}

fn blocked_message(seq: i64, blockers: &[Blocker]) -> String {
    let listed = join_and(blockers.iter().map(|b| match b.pending_review {
        Some(review) if b.status == Status::Done => format!(
            "task {} ({}, done but under review {review})",
            b.seq,
            crate::fmt::truncate(&b.title, 40),
        ),
        _ => format!(
            "task {} ({}, {})",
            b.seq,
            crate::fmt::truncate(&b.title, 40),
            b.status
        ),
    }));
    // A dependency that is finished but for its verdict deserves a different
    // last clause: the fix is not to do the work, it is to wait for โ€” or be โ€”
    // the harness that reads it.
    if blockers
        .iter()
        .all(|b| b.status == Status::Done && b.pending_review.is_some())
    {
        format!(
            "task {seq} is blocked by {listed}; the work is finished, but this queue \
             holds dependents until the review delivers its verdict"
        )
    } else {
        format!(
            "task {seq} is blocked by {listed}; it becomes claimable once every \
             dependency is done"
        )
    }
}

fn awaiting_answer_message(seq: i64, question: &Question) -> String {
    format!(
        "task {seq} is awaiting an answer: {}; use `hird answer {seq} <ANSWER>` before an agent claims it",
        crate::fmt::truncate(&question.question, 120)
    )
}

fn cycle_message(seq: i64, on: i64, path: &[i64]) -> String {
    let chain = path
        .iter()
        .map(|s| s.to_string())
        .collect::<Vec<_>>()
        .join(" -> ");
    format!(
        "task {seq} cannot depend on task {on}: task {on} already depends on \
         task {seq} ({chain}), and that would be a cycle"
    )
}

fn recused_message(seq: i64, recusal: &Recusal, actor: &str) -> String {
    let worker = recusal.worker.as_deref().unwrap_or("nobody");
    let reason = if recusal.reason.is_empty() {
        String::new()
    } else {
        format!(" ({})", recusal.reason)
    };
    format!(
        "task {seq} is recused from task {} ({}){reason}, which {worker} worked โ€” so {actor} \
         cannot take it. Tell the human it needs an agent in a different harness.",
        recusal.from_seq,
        crate::fmt::truncate(&recusal.from_title, 40),
    )
}

fn missing_capabilities_message(seq: i64, required: &[String], available: &[String]) -> String {
    let missing = join_and(required.iter().cloned());
    let advertised = if available.is_empty() {
        "none".to_string()
    } else {
        available.join(", ")
    };
    format!(
        "task {seq} requires {missing}, but this session advertises {advertised}; start a capable harness or update HIRD_CAPABILITIES"
    )
}

fn conflict_message(seq: i64, conflicts: &[Conflict]) -> String {
    let listed = join_and(conflicts.iter().map(Conflict::describe));
    format!("task {seq} would collide with work already under way: {listed}")
}

/// `a`, `a and b`, `a, b and c` โ€” used wherever an error names several things.
fn join_and(items: impl IntoIterator<Item = String>) -> String {
    let items: Vec<String> = items.into_iter().collect();
    match items.split_last() {
        None => "nothing".to_string(),
        Some((last, [])) => last.clone(),
        Some((last, rest)) => format!("{} and {last}", rest.join(", ")),
    }
}

fn not_holder_message(seq: i64, status: Status, holder: Option<&str>, actor: &str) -> String {
    match holder {
        Some(holder) => format!(
            "task {seq} is held by {holder}, not {actor}; only the lease holder can update it"
        ),
        None => format!("task {seq} is {status} and unclaimed; {actor} must claim it first"),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn claim_conflict_reads_like_the_design_example() {
        let err = Error::ClaimConflict {
            seq: 42,
            status: Status::Claimed,
            holder: Some("codex:9f2c".into()),
            lease_expires_at: Some("2026-07-25T14:32:00.000Z".into()),
        };
        assert_eq!(
            err.to_string(),
            "task 42 is claimed by codex:9f2c until 2026-07-25T14:32:00.000Z"
        );
    }

    #[test]
    fn claim_conflict_without_a_holder_explains_the_status() {
        let err = Error::ClaimConflict {
            seq: 7,
            status: Status::Done,
            holder: None,
            lease_expires_at: None,
        };
        assert_eq!(
            err.to_string(),
            "task 7 is done, not open, so it cannot be claimed"
        );
    }

    #[test]
    fn not_holder_names_both_parties() {
        let err = Error::NotHolder {
            seq: 3,
            status: Status::InProgress,
            holder: Some("codex:9f2c".into()),
            actor: "claude-code:af31".into(),
        };
        assert_eq!(
            err.to_string(),
            "task 3 is held by codex:9f2c, not claude-code:af31; \
             only the lease holder can update it"
        );
    }
}