1use super::*;
6use crate::traits::{
7 GoalRun, Intention, IntentionStatus, Mandate, MandateActionOutcome, MandateActivityLevel,
8 MandateAuthority, MandateAutonomyMode, MandateDecisionCycle, MandateDecisionOrigin,
9 MandateDecisionOutcome, MandateDispatchGate, MandateFinalizationRejectReason,
10 MandateFinalizationStaleReason, MandateLearningNote, MandateMutationAttempt,
11 MandateMutationAttemptStatus, MandateMutationDispatchClaim, MandateMutationEvidence,
12 MandateMutationQuotaBlockReason, MandateMutationQuotaState, MandateMutationReservation,
13 MandateObjectiveMeasurement, MandateOperatingUpdates, MandateReconciliationReason,
14 MandateReconciliationResolution, MandateRunFinalizationRequest, MandateRunFinalizationResult,
15 MandateRunProofCounts, MandateStatus, MandateStore, MandateSuspension, MandateSuspensionKind,
16 MandateTerminationKind, MandateWakeSignal, Task,
17};
18use sha2::{Digest, Sha256};
19
20mod controller;
21mod decisions;
22mod finalize;
23mod measurements;
24mod mutations;
25mod recovery;
26mod reviews;
27mod rows;
28mod status;
29mod tasks;
30#[cfg(test)]
31mod tests;
32mod transitions;
33mod validate;
34
35use mutations::*;
36use recovery::*;
37use rows::*;
38use transitions::*;
39use validate::*;
40
41#[async_trait]
42impl<S: WorkDatabase> MandateStore for S {
43 async fn create_mandate_controller(
44 &self,
45 goal: &Goal,
46 mandate: &Mandate,
47 ) -> anyhow::Result<()> {
48 controller::create_mandate_controller(self, goal, mandate).await
49 }
50
51 async fn get_mandate(&self, id: &str) -> anyhow::Result<Option<Mandate>> {
52 controller::get_mandate(self, id).await
53 }
54
55 async fn get_mandate_for_goal(&self, goal_id: &str) -> anyhow::Result<Option<Mandate>> {
56 controller::get_mandate_for_goal(self, goal_id).await
57 }
58
59 async fn list_mandates(
60 &self,
61 session_id: Option<&str>,
62 include_terminal: bool,
63 ) -> anyhow::Result<Vec<Mandate>> {
64 controller::list_mandates(self, session_id, include_terminal).await
65 }
66
67 async fn is_mandate_session_authorized(
68 &self,
69 mandate_id: &str,
70 session_id: &str,
71 ) -> anyhow::Result<bool> {
72 controller::is_mandate_session_authorized(self, mandate_id, session_id).await
73 }
74
75 async fn update_mandate(&self, mandate: &Mandate) -> anyhow::Result<()> {
76 controller::update_mandate(self, mandate).await
77 }
78
79 async fn confirm_mandate(
80 &self,
81 mandate_id: &str,
82 expected_version: i64,
83 activation_duration_secs: Option<i64>,
84 ) -> anyhow::Result<bool> {
85 controller::confirm_mandate(self, mandate_id, expected_version, activation_duration_secs)
86 .await
87 }
88
89 async fn transition_mandate_status(
90 &self,
91 mandate_id: &str,
92 from_status: MandateStatus,
93 to_status: MandateStatus,
94 ) -> anyhow::Result<bool> {
95 status::transition_mandate_status(self, mandate_id, from_status, to_status).await
96 }
97
98 async fn resume_mandate_with_context(
99 &self,
100 mandate_id: &str,
101 from_status: MandateStatus,
102 expected_version: i64,
103 controller_context: Option<&str>,
104 ) -> anyhow::Result<bool> {
105 status::resume_mandate_with_context(
106 self,
107 mandate_id,
108 from_status,
109 expected_version,
110 controller_context,
111 )
112 .await
113 }
114
115 async fn record_active_mandate_owner_guidance(
116 &self,
117 mandate_id: &str,
118 expected_version: i64,
119 owner_session: &str,
120 controller_context: &str,
121 ) -> anyhow::Result<bool> {
122 status::record_active_mandate_owner_guidance(
123 self,
124 mandate_id,
125 expected_version,
126 owner_session,
127 controller_context,
128 )
129 .await
130 }
131
132 async fn keep_mandate_controller_active(
133 &self,
134 mandate_id: &str,
135 expected_version: i64,
136 ) -> anyhow::Result<bool> {
137 status::keep_mandate_controller_active(self, mandate_id, expected_version).await
138 }
139
140 async fn wake_mandates_for_signal(
141 &self,
142 signal: &MandateWakeSignal,
143 ) -> anyhow::Result<Vec<String>> {
144 status::wake_mandates_for_signal(self, signal).await
145 }
146
147 async fn resolve_mandate_suspension(
148 &self,
149 mandate_id: &str,
150 expected_version: i64,
151 expected_kind: MandateSuspensionKind,
152 controller_context: Option<&str>,
153 reconciliation_resolution: Option<MandateReconciliationResolution>,
154 owner_guidance: &str,
155 owner_session: &str,
156 ) -> anyhow::Result<bool> {
157 status::resolve_mandate_suspension(
158 self,
159 mandate_id,
160 expected_version,
161 expected_kind,
162 controller_context,
163 reconciliation_resolution,
164 owner_guidance,
165 owner_session,
166 )
167 .await
168 }
169
170 async fn record_mandate_objective_measurement(
171 &self,
172 measurement: &MandateObjectiveMeasurement,
173 ) -> anyhow::Result<()> {
174 measurements::record_mandate_objective_measurement(self, measurement).await
175 }
176
177 async fn list_mandate_objective_measurements(
178 &self,
179 mandate_id: &str,
180 limit: i64,
181 ) -> anyhow::Result<Vec<MandateObjectiveMeasurement>> {
182 measurements::list_mandate_objective_measurements(self, mandate_id, limit).await
183 }
184
185 async fn record_mandate_learning_note(&self, note: &MandateLearningNote) -> anyhow::Result<()> {
186 measurements::record_mandate_learning_note(self, note).await
187 }
188
189 async fn list_mandate_learning_notes(
190 &self,
191 mandate_id: &str,
192 limit: i64,
193 ) -> anyhow::Result<Vec<MandateLearningNote>> {
194 measurements::list_mandate_learning_notes(self, mandate_id, limit).await
195 }
196
197 async fn list_mandate_action_outcomes(
198 &self,
199 mandate_id: &str,
200 limit: i64,
201 ) -> anyhow::Result<Vec<MandateActionOutcome>> {
202 decisions::list_mandate_action_outcomes(self, mandate_id, limit).await
203 }
204
205 async fn create_mandate_review_run(
206 &self,
207 mandate_id: &str,
208 review_lease_token: &str,
209 goal_run_id: &str,
210 root_task: &Task,
211 ) -> anyhow::Result<GoalRun> {
212 reviews::create_mandate_review_run(
213 self,
214 mandate_id,
215 review_lease_token,
216 goal_run_id,
217 root_task,
218 )
219 .await
220 }
221
222 async fn claim_due_mandates(
223 &self,
224 limit: i64,
225 lease_owner: &str,
226 lease_secs: i64,
227 ) -> anyhow::Result<Vec<Mandate>> {
228 reviews::claim_due_mandates(self, limit, lease_owner, lease_secs).await
229 }
230
231 async fn release_mandate_review_lease(
232 &self,
233 mandate_id: &str,
234 lease_token: &str,
235 retry_at: &str,
236 ) -> anyhow::Result<bool> {
237 reviews::release_mandate_review_lease(self, mandate_id, lease_token, retry_at).await
238 }
239
240 async fn create_mandate_task_from_attempt(
241 &self,
242 task: &Task,
243 mandate_id: &str,
244 mandate_version: i64,
245 goal_run_id: &str,
246 root_task_attempt_id: &str,
247 max_non_root_tasks: i64,
248 ) -> anyhow::Result<bool> {
249 tasks::create_mandate_task_from_attempt(
250 self,
251 task,
252 mandate_id,
253 mandate_version,
254 goal_run_id,
255 root_task_attempt_id,
256 max_non_root_tasks,
257 )
258 .await
259 }
260
261 async fn claim_mandate_task_from_attempt(
262 &self,
263 task_id: &str,
264 worker_instance_id: &str,
265 mandate_id: &str,
266 mandate_version: i64,
267 goal_run_id: &str,
268 root_task_attempt_id: &str,
269 lease_secs: i64,
270 ) -> anyhow::Result<Option<crate::traits::TaskAttempt>> {
271 tasks::claim_mandate_task_from_attempt(
272 self,
273 task_id,
274 worker_instance_id,
275 mandate_id,
276 mandate_version,
277 goal_run_id,
278 root_task_attempt_id,
279 lease_secs,
280 )
281 .await
282 }
283
284 async fn record_mandate_decision(
285 &self,
286 decision: &MandateDecisionCycle,
287 intention: Option<&Intention>,
288 task_attempt_id: Option<&str>,
289 ) -> anyhow::Result<()> {
290 decisions::record_mandate_decision(self, decision, intention, task_attempt_id).await
291 }
292
293 async fn record_mandate_decision_with_updates(
294 &self,
295 decision: &MandateDecisionCycle,
296 intention: Option<&Intention>,
297 operating_updates: Option<&MandateOperatingUpdates>,
298 task_attempt_id: Option<&str>,
299 ) -> anyhow::Result<()> {
300 decisions::record_mandate_decision_with_updates(
301 self,
302 decision,
303 intention,
304 operating_updates,
305 task_attempt_id,
306 )
307 .await
308 }
309
310 async fn get_mandate_decision_for_run(
311 &self,
312 goal_run_id: &str,
313 ) -> anyhow::Result<Option<MandateDecisionCycle>> {
314 decisions::get_mandate_decision_for_run(self, goal_run_id).await
315 }
316
317 async fn current_run_evidence_receipt_ids(
318 &self,
319 goal_run_id: &str,
320 limit: usize,
321 ) -> anyhow::Result<Vec<String>> {
322 decisions::current_run_evidence_receipt_ids(self, goal_run_id, limit).await
323 }
324
325 async fn list_mandate_decisions(
326 &self,
327 mandate_id: &str,
328 limit: i64,
329 ) -> anyhow::Result<Vec<MandateDecisionCycle>> {
330 decisions::list_mandate_decisions(self, mandate_id, limit).await
331 }
332
333 async fn list_intentions(
334 &self,
335 mandate_id: &str,
336 limit: i64,
337 ) -> anyhow::Result<Vec<Intention>> {
338 decisions::list_intentions(self, mandate_id, limit).await
339 }
340
341 async fn reserve_mandate_action_attempt(
342 &self,
343 reservation: &MandateMutationReservation,
344 ) -> anyhow::Result<Option<MandateMutationAttempt>> {
345 mutations::reserve_mandate_action_attempt(self, reservation).await
346 }
347
348 async fn claim_mandate_mutation_dispatch(
349 &self,
350 claim: &MandateMutationDispatchClaim,
351 ) -> anyhow::Result<bool> {
352 mutations::claim_mandate_mutation_dispatch(self, claim).await
353 }
354
355 async fn get_mandate_mutation_quota_state(
356 &self,
357 mandate_id: &str,
358 as_of: &str,
359 ) -> anyhow::Result<Option<MandateMutationQuotaState>> {
360 mutations::get_mandate_mutation_quota_state(self, mandate_id, as_of).await
361 }
362
363 async fn list_mandate_mutation_attempts_for_run(
364 &self,
365 goal_run_id: &str,
366 ) -> anyhow::Result<Vec<MandateMutationAttempt>> {
367 mutations::list_mandate_mutation_attempts_for_run(self, goal_run_id).await
368 }
369
370 async fn finalize_mandate_run_from_proof(
371 &self,
372 request: &MandateRunFinalizationRequest,
373 ) -> anyhow::Result<MandateRunFinalizationResult> {
374 finalize::finalize_mandate_run_from_proof(self, request).await
375 }
376}