1use std::path::PathBuf;
6
7use serde::{Deserialize, Serialize};
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
10#[serde(rename_all = "snake_case")]
11pub enum Role {
12 Planner,
13 Worker,
14 Reviewer,
15}
16
17#[derive(Debug, Clone, PartialEq, Eq)]
19pub struct Job {
20 pub role: Role,
21 pub prompt: String,
22 pub cwd: PathBuf,
24 pub run_dir: PathBuf,
26 pub raw_log: PathBuf,
28 pub schema: Option<&'static str>,
30}
31
32#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
34#[serde(tag = "type", rename_all = "snake_case")]
35pub enum HarnessEvent {
36 Started { model: Option<String> },
37 Text { text: String },
38 Tool { name: String, detail: String },
39}
40
41#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
42#[serde(tag = "type", rename_all = "snake_case")]
43pub enum Outcome {
44 Done {
45 text: String,
46 structured: Option<serde_json::Value>,
47 },
48 Failed {
49 kind: FailureKind,
50 message: String,
51 reset_at: Option<u64>,
53 },
54}
55
56impl Outcome {
57 pub fn failed(kind: FailureKind, message: impl Into<String>) -> Outcome {
59 Outcome::Failed {
60 kind,
61 message: message.into(),
62 reset_at: None,
63 }
64 }
65}
66
67#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
68#[serde(rename_all = "snake_case")]
69pub enum FailureKind {
70 Quota,
72 Auth,
74 Timeout,
76 Unknown,
78}
79
80#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
81pub struct Probe {
82 pub version: String,
83}
84
85#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error, Serialize, Deserialize)]
86#[serde(tag = "type", content = "message", rename_all = "snake_case")]
87pub enum ProbeError {
88 #[error("{0}")]
89 Missing(String),
90 #[error("{0}")]
91 Unsupported(String),
92 #[error("{0}")]
93 NotLoggedIn(String),
94}
95
96#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
97pub struct QuotaReport {
98 pub remaining_fraction: f64,
99 pub reset_at: Option<u64>,
101}
102
103pub trait Runner {
104 fn preflight(&mut self, lane: &str) -> Result<Probe, ProbeError>;
106 fn quota(&mut self, lane: &str) -> Option<QuotaReport>;
108 fn run(
111 &mut self,
112 lane: &str,
113 job: &Job,
114 on_spawn: &mut dyn FnMut(i32),
115 on_event: &mut dyn FnMut(HarnessEvent),
116 ) -> Outcome;
117}
118
119#[cfg(test)]
120mod tests {
121 use super::*;
122 use serde_json::json;
123
124 #[test]
126 fn a_failed_outcome_is_written_the_way_the_log_documents_it() {
127 let outcome = Outcome::Failed {
128 kind: FailureKind::Quota,
129 message: "balance is empty".into(),
130 reset_at: None,
131 };
132
133 assert_eq!(
134 serde_json::to_value(&outcome).unwrap(),
135 json!({ "type": "failed", "kind": "quota", "message": "balance is empty", "reset_at": null })
136 );
137 }
138
139 #[test]
140 fn harness_events_are_tagged_with_a_snake_case_type() {
141 let tool = HarnessEvent::Tool {
142 name: "edit".into(),
143 detail: "src/http.rs".into(),
144 };
145 let text: HarnessEvent =
146 serde_json::from_value(json!({ "type": "text", "text": "hi" })).unwrap();
147
148 assert_eq!(
149 serde_json::to_value(&tool).unwrap(),
150 json!({ "type": "tool", "name": "edit", "detail": "src/http.rs" })
151 );
152 assert_eq!(text, HarnessEvent::Text { text: "hi".into() });
153 }
154}