use std::path::PathBuf;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Role {
Planner,
Worker,
Reviewer,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Job {
pub role: Role,
pub prompt: String,
pub cwd: PathBuf,
pub run_dir: PathBuf,
pub raw_log: PathBuf,
pub schema: Option<&'static str>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum HarnessEvent {
Started { model: Option<String> },
Text { text: String },
Tool { name: String, detail: String },
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum Outcome {
Done {
text: String,
structured: Option<serde_json::Value>,
},
Failed {
kind: FailureKind,
message: String,
reset_at: Option<u64>,
},
}
impl Outcome {
pub fn failed(kind: FailureKind, message: impl Into<String>) -> Outcome {
Outcome::Failed {
kind,
message: message.into(),
reset_at: None,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum FailureKind {
Quota,
Auth,
Timeout,
Unknown,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Probe {
pub version: String,
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error, Serialize, Deserialize)]
#[serde(tag = "type", content = "message", rename_all = "snake_case")]
pub enum ProbeError {
#[error("{0}")]
Missing(String),
#[error("{0}")]
Unsupported(String),
#[error("{0}")]
NotLoggedIn(String),
}
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct QuotaReport {
pub remaining_fraction: f64,
pub reset_at: Option<u64>,
}
pub trait Runner {
fn preflight(&mut self, lane: &str) -> Result<Probe, ProbeError>;
fn quota(&mut self, lane: &str) -> Option<QuotaReport>;
fn run(
&mut self,
lane: &str,
job: &Job,
on_spawn: &mut dyn FnMut(i32),
on_event: &mut dyn FnMut(HarnessEvent),
) -> Outcome;
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn a_failed_outcome_is_written_the_way_the_log_documents_it() {
let outcome = Outcome::Failed {
kind: FailureKind::Quota,
message: "balance is empty".into(),
reset_at: None,
};
assert_eq!(
serde_json::to_value(&outcome).unwrap(),
json!({ "type": "failed", "kind": "quota", "message": "balance is empty", "reset_at": null })
);
}
#[test]
fn harness_events_are_tagged_with_a_snake_case_type() {
let tool = HarnessEvent::Tool {
name: "edit".into(),
detail: "src/http.rs".into(),
};
let text: HarnessEvent =
serde_json::from_value(json!({ "type": "text", "text": "hi" })).unwrap();
assert_eq!(
serde_json::to_value(&tool).unwrap(),
json!({ "type": "tool", "name": "edit", "detail": "src/http.rs" })
);
assert_eq!(text, HarnessEvent::Text { text: "hi".into() });
}
}