Skip to main content

aidaemon_work/sqlite/mandates/
mod.rs

1//! The SQLite implementation of [`MandateStore`]: each method's body lives
2//! in the child module for its concern, and the shared row decoding,
3//! validation and transitions in their own.
4
5use super::*;
6use crate::traits::{
7    GoalRun, Intention, IntentionStatus, Mandate, MandateActivityLevel, MandateAuthority,
8    MandateAutonomyMode, MandateDecisionCycle, MandateDecisionOrigin, MandateDecisionOutcome,
9    MandateDispatchGate, MandateFinalizationRejectReason, MandateFinalizationStaleReason,
10    MandateLearningNote, MandateMutationAttempt, MandateMutationAttemptStatus,
11    MandateMutationDispatchClaim, MandateMutationEvidence, MandateMutationQuotaBlockReason,
12    MandateMutationQuotaState, MandateMutationReservation, MandateObjectiveMeasurement,
13    MandateOperatingUpdates, MandateReconciliationReason, MandateReconciliationResolution,
14    MandateRunFinalizationRequest, MandateRunFinalizationResult, MandateRunProofCounts,
15    MandateStatus, MandateStore, MandateSuspension, MandateSuspensionKind, MandateTerminationKind,
16    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 create_mandate_review_run(
198        &self,
199        mandate_id: &str,
200        review_lease_token: &str,
201        goal_run_id: &str,
202        root_task: &Task,
203    ) -> anyhow::Result<GoalRun> {
204        reviews::create_mandate_review_run(
205            self,
206            mandate_id,
207            review_lease_token,
208            goal_run_id,
209            root_task,
210        )
211        .await
212    }
213
214    async fn claim_due_mandates(
215        &self,
216        limit: i64,
217        lease_owner: &str,
218        lease_secs: i64,
219    ) -> anyhow::Result<Vec<Mandate>> {
220        reviews::claim_due_mandates(self, limit, lease_owner, lease_secs).await
221    }
222
223    async fn release_mandate_review_lease(
224        &self,
225        mandate_id: &str,
226        lease_token: &str,
227        retry_at: &str,
228    ) -> anyhow::Result<bool> {
229        reviews::release_mandate_review_lease(self, mandate_id, lease_token, retry_at).await
230    }
231
232    async fn create_mandate_task_from_attempt(
233        &self,
234        task: &Task,
235        mandate_id: &str,
236        mandate_version: i64,
237        goal_run_id: &str,
238        root_task_attempt_id: &str,
239        max_non_root_tasks: i64,
240    ) -> anyhow::Result<bool> {
241        tasks::create_mandate_task_from_attempt(
242            self,
243            task,
244            mandate_id,
245            mandate_version,
246            goal_run_id,
247            root_task_attempt_id,
248            max_non_root_tasks,
249        )
250        .await
251    }
252
253    async fn claim_mandate_task_from_attempt(
254        &self,
255        task_id: &str,
256        worker_instance_id: &str,
257        mandate_id: &str,
258        mandate_version: i64,
259        goal_run_id: &str,
260        root_task_attempt_id: &str,
261        lease_secs: i64,
262    ) -> anyhow::Result<Option<crate::traits::TaskAttempt>> {
263        tasks::claim_mandate_task_from_attempt(
264            self,
265            task_id,
266            worker_instance_id,
267            mandate_id,
268            mandate_version,
269            goal_run_id,
270            root_task_attempt_id,
271            lease_secs,
272        )
273        .await
274    }
275
276    async fn record_mandate_decision(
277        &self,
278        decision: &MandateDecisionCycle,
279        intention: Option<&Intention>,
280        task_attempt_id: Option<&str>,
281    ) -> anyhow::Result<()> {
282        decisions::record_mandate_decision(self, decision, intention, task_attempt_id).await
283    }
284
285    async fn record_mandate_decision_with_updates(
286        &self,
287        decision: &MandateDecisionCycle,
288        intention: Option<&Intention>,
289        operating_updates: Option<&MandateOperatingUpdates>,
290        task_attempt_id: Option<&str>,
291    ) -> anyhow::Result<()> {
292        decisions::record_mandate_decision_with_updates(
293            self,
294            decision,
295            intention,
296            operating_updates,
297            task_attempt_id,
298        )
299        .await
300    }
301
302    async fn get_mandate_decision_for_run(
303        &self,
304        goal_run_id: &str,
305    ) -> anyhow::Result<Option<MandateDecisionCycle>> {
306        decisions::get_mandate_decision_for_run(self, goal_run_id).await
307    }
308
309    async fn current_run_evidence_receipt_ids(
310        &self,
311        goal_run_id: &str,
312        limit: usize,
313    ) -> anyhow::Result<Vec<String>> {
314        decisions::current_run_evidence_receipt_ids(self, goal_run_id, limit).await
315    }
316
317    async fn list_mandate_decisions(
318        &self,
319        mandate_id: &str,
320        limit: i64,
321    ) -> anyhow::Result<Vec<MandateDecisionCycle>> {
322        decisions::list_mandate_decisions(self, mandate_id, limit).await
323    }
324
325    async fn list_intentions(
326        &self,
327        mandate_id: &str,
328        limit: i64,
329    ) -> anyhow::Result<Vec<Intention>> {
330        decisions::list_intentions(self, mandate_id, limit).await
331    }
332
333    async fn reserve_mandate_action_attempt(
334        &self,
335        reservation: &MandateMutationReservation,
336    ) -> anyhow::Result<Option<MandateMutationAttempt>> {
337        mutations::reserve_mandate_action_attempt(self, reservation).await
338    }
339
340    async fn claim_mandate_mutation_dispatch(
341        &self,
342        claim: &MandateMutationDispatchClaim,
343    ) -> anyhow::Result<bool> {
344        mutations::claim_mandate_mutation_dispatch(self, claim).await
345    }
346
347    async fn get_mandate_mutation_quota_state(
348        &self,
349        mandate_id: &str,
350        as_of: &str,
351    ) -> anyhow::Result<Option<MandateMutationQuotaState>> {
352        mutations::get_mandate_mutation_quota_state(self, mandate_id, as_of).await
353    }
354
355    async fn list_mandate_mutation_attempts_for_run(
356        &self,
357        goal_run_id: &str,
358    ) -> anyhow::Result<Vec<MandateMutationAttempt>> {
359        mutations::list_mandate_mutation_attempts_for_run(self, goal_run_id).await
360    }
361
362    async fn finalize_mandate_run_from_proof(
363        &self,
364        request: &MandateRunFinalizationRequest,
365    ) -> anyhow::Result<MandateRunFinalizationResult> {
366        finalize::finalize_mandate_run_from_proof(self, request).await
367    }
368}