1pub mod board;
37pub mod store;
38
39#[cfg(test)]
40mod tests;
41
42use serde::{Deserialize, Serialize};
43
44use lean_ctx_protocol::UtcTimestamp;
45use lean_ctx_protocol::cloud::tasks::{TaskActorKind, TaskActorV1, TaskState};
46
47pub use board::{Amendment, Board, LocalTask, Proposal, SharedRef, Submission, Transition};
48pub use store::Store;
49
50pub const LOCAL_VAULT: &str = "local";
52pub const DEFAULT_CLAIM_MINUTES: u32 = 60;
54pub const MIN_CLAIM_MINUTES: u32 = 5;
55pub const MAX_CLAIM_MINUTES: u32 = 240;
57
58#[derive(Debug, Clone, PartialEq, Eq)]
60pub enum Actor {
61 Agent { id: String, label: Option<String> },
63 Person { label: String },
65}
66
67impl Actor {
68 pub fn agent(id: impl Into<String>) -> Self {
69 Self::Agent {
70 id: id.into(),
71 label: None,
72 }
73 }
74
75 pub fn person(label: impl Into<String>) -> Self {
76 Self::Person {
77 label: label.into(),
78 }
79 }
80
81 pub fn agent_id(&self) -> Option<&str> {
83 match self {
84 Self::Agent { id, .. } => Some(id),
85 Self::Person { .. } => None,
86 }
87 }
88
89 pub fn view(&self) -> TaskActorV1 {
92 match self {
93 Self::Agent { id, label } => TaskActorV1 {
94 kind: TaskActorKind::Instance,
95 label: label.clone().unwrap_or_else(|| id.clone()),
96 agent: Some(id.clone()),
97 owner: None,
98 model: None,
99 mine: false,
100 profile_id: None,
101 own: None,
102 },
103 Self::Person { label } => TaskActorV1 {
104 kind: TaskActorKind::Person,
105 label: label.clone(),
106 agent: None,
107 owner: None,
108 model: None,
109 mine: false,
110 profile_id: None,
111 own: None,
112 },
113 }
114 }
115
116 fn is(&self, actor: &TaskActorV1) -> bool {
118 match self {
119 Self::Agent { id, .. } => actor.agent.as_deref() == Some(id.as_str()),
120 Self::Person { .. } => actor.kind == TaskActorKind::Person,
121 }
122 }
123
124 fn is_exactly(&self, actor: &TaskActorV1) -> bool {
127 match self {
128 Self::Agent { .. } => self.is(actor),
129 Self::Person { label } => actor.kind == TaskActorKind::Person && actor.label == *label,
130 }
131 }
132}
133
134#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
136#[serde(rename_all = "snake_case")]
137pub enum TaskEvent {
138 Proposed,
139 Accepted,
140 Declined,
141 Claimed,
142 Updated,
143 Released,
144 Submitted,
145 Failed,
146 Verified,
147 Rejected,
148 Stopped,
149 Amended,
151 Shared,
153 Fields,
155}
156
157impl TaskEvent {
158 pub fn as_str(self) -> &'static str {
159 match self {
160 Self::Proposed => "proposed",
161 Self::Accepted => "accepted",
162 Self::Declined => "declined",
163 Self::Claimed => "claimed",
164 Self::Updated => "updated",
165 Self::Released => "released",
166 Self::Submitted => "submitted",
167 Self::Failed => "failed",
168 Self::Verified => "verified",
169 Self::Rejected => "rejected",
170 Self::Stopped => "stopped",
171 Self::Amended => "amended",
172 Self::Shared => "shared",
173 Self::Fields => "fields",
174 }
175 }
176}
177
178#[derive(Debug, Clone, PartialEq, Eq)]
180pub struct Change {
181 pub seq: u64,
183 pub number: u64,
184 pub event: TaskEvent,
185 pub state: TaskState,
186 pub worker: Option<String>,
189}
190
191#[derive(Debug, Clone, PartialEq, Eq)]
194pub enum TaskError {
195 NotFound(u64),
196 InvalidState {
198 number: u64,
199 reason: &'static str,
200 },
201 Forbidden {
203 number: u64,
204 reason: &'static str,
205 },
206 Invalid(String),
208 Store(String),
210}
211
212impl TaskError {
213 pub fn code(&self) -> &'static str {
216 match self {
217 Self::NotFound(_) => "not_found",
218 Self::InvalidState { .. } => "invalid_state",
219 Self::Forbidden { .. } => "forbidden",
220 Self::Invalid(_) => "invalid_argument",
221 Self::Store(_) => "unavailable",
222 }
223 }
224
225 pub fn reason(&self) -> Option<&'static str> {
227 match self {
228 Self::InvalidState { reason, .. } | Self::Forbidden { reason, .. } => Some(reason),
229 _ => None,
230 }
231 }
232}
233
234impl std::fmt::Display for TaskError {
235 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
236 match self {
237 Self::NotFound(n) => write!(f, "task #{n} does not exist"),
238 Self::InvalidState { number, reason } | Self::Forbidden { number, reason } => {
239 write!(f, "task #{number}: {}", describe(reason))
240 }
241 Self::Invalid(message) | Self::Store(message) => f.write_str(message),
242 }
243 }
244}
245
246impl std::error::Error for TaskError {}
247
248fn describe(reason: &str) -> &'static str {
249 match reason {
250 "task_claimed" => "another agent holds its claim (task_claimed)",
251 "task_not_open" => "it is not open (task_not_open)",
252 "not_proposed" => "it is not a proposal (not_proposed)",
253 "not_claimed" => "it is not claimed (not_claimed)",
254 "task_closed" => "it is closed (task_closed)",
255 "not_submitted" => "no result waits for a check (not_submitted)",
256 "self_verify" => {
257 "the agent that submitted the result never checks it; a person or another agent does (self_verify)"
258 }
259 "not_holder" => "only the agent holding its claim does that (not_holder)",
260 "agents_claim" => "only an agent claims a task (agents_claim)",
261 "task_submitted" => "its result waits for a check; verify it instead (task_submitted)",
262 "agents_stop" => "only a person stops a task (agents_stop)",
263 "already_shared" => "it was already brought into a vault (already_shared)",
264 "too_many_tasks" => "the project holds too many unfinished tasks (too_many_tasks)",
265 "not_submitter" => {
266 "only the agent that submitted its result corrects it; a merge record also a person or a coordinator session (not_submitter)"
267 }
268 "not_merger" => {
269 "the reviewer never records the merge; the submitter, a person or a coordinator session does (not_merger)"
270 }
271 "not_amendable" => "it has no submitted or done result to amend (not_amendable)",
272 "record_merge_only" => {
273 "it is done: it only takes its merge record, --merge <sha> (record_merge_only)"
274 }
275 "not_merged_yet" => {
276 "its result waits for a check; a merge is recorded once it is done (not_merged_yet)"
277 }
278 "merge_recorded" => "its merge is already recorded (merge_recorded)",
279 _ => "refused",
280 }
281}
282
283pub fn timestamp(at: lean_ctx_time::UtcDateTime) -> UtcTimestamp {
285 UtcTimestamp::new(at.format("%Y-%m-%dT%H:%M:%SZ").to_string())
286 .unwrap_or_else(|_| unreachable_timestamp())
287}
288
289#[cold]
290fn unreachable_timestamp() -> UtcTimestamp {
291 UtcTimestamp::new("1970-01-01T00:00:00Z").unwrap_or_else(|_| unreachable!())
293}