aidaemon-work 0.25.0

Goals, mandates, tasks and work coordination with a SQLite store; internal to aidaemon, no stability promise
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
//! Mandate lifecycle: status transitions, resuming, owner guidance, wake signals and suspensions.

use super::*;

pub(super) async fn transition_mandate_status<S: WorkDatabase>(
    store: &S,
    mandate_id: &str,
    from_status: MandateStatus,
    to_status: MandateStatus,
) -> anyhow::Result<bool> {
    anyhow::ensure!(
        from_status != to_status,
        "mandate status transition is a no-op"
    );
    // Keep lifecycle evolution explicit. Terminal mandates are immutable,
    // and only owner-mediated resume paths may leave paused/ASK states.
    // Invalid-but-known transitions report a failed CAS rather than an
    // error so callers cannot distinguish them from a concurrent change.
    if !from_status.can_transition_to(to_status) {
        return Ok(false);
    }
    let now = chrono::Utc::now().to_rfc3339();
    let suspension = match to_status {
        MandateStatus::Paused => Some(MandateSuspension::new(
            MandateSuspensionKind::OwnerPaused,
            None,
        )),
        MandateStatus::AwaitingInput => Some(MandateSuspension::new(
            MandateSuspensionKind::ReviewFailed,
            Some("lifecycle_transition".to_string()),
        )),
        MandateStatus::Active | MandateStatus::Completed | MandateStatus::Cancelled => None,
    };
    let mut tx = store.work_pool().begin_with("BEGIN IMMEDIATE").await?;
    let goal_id = sqlx::query_scalar::<_, String>(
        "UPDATE mandates
             SET status = ?,
                 next_review_at = CASE WHEN ? = 'active' THEN ? ELSE next_review_at END,
                 review_lease_token = NULL, review_lease_expires_at = NULL,
                 suspension_json = ?,
                 version = version + 1, updated_at = ?
             WHERE id = ? AND status = ?
               AND (? != 'active' OR confirmed_at IS NOT NULL)
             RETURNING goal_id",
    )
    .bind(to_status.as_str())
    .bind(to_status.as_str())
    .bind(&now)
    .bind(suspension.as_ref().map(serde_json::to_string).transpose()?)
    .bind(&now)
    .bind(mandate_id)
    .bind(from_status.as_str())
    .bind(to_status.as_str())
    .fetch_optional(&mut *tx)
    .await?;
    let Some(goal_id) = goal_id else {
        tx.rollback().await?;
        return Ok(false);
    };

    // A lifecycle transition is an authority-epoch change. Close every
    // open mandate run and its work in the same transaction so pausing and
    // later resuming cannot resurrect a pre-pause ACT or attempt.
    let claimed_unresolved = invalidate_open_mandate_runs(&mut tx, &goal_id, &now).await?;
    let effective_status = if to_status == MandateStatus::Paused && claimed_unresolved > 0 {
        let suspension = MandateSuspension::new(
            MandateSuspensionKind::ReconciliationRequired,
            Some("lifecycle_invalidated_after_dispatch_claim".to_string()),
        );
        let changed = sqlx::query(
            "UPDATE mandates SET status = 'awaiting_input', suspension_json = ?, updated_at = ?
                 WHERE id = ? AND status = 'paused'",
        )
        .bind(serde_json::to_string(&suspension)?)
        .bind(&now)
        .bind(mandate_id)
        .execute(&mut *tx)
        .await?;
        anyhow::ensure!(
            changed.rows_affected() == 1,
            "claimed mutation requires typed reconciliation"
        );
        MandateStatus::AwaitingInput
    } else {
        to_status
    };
    update_controller_status(&mut tx, &goal_id, effective_status, &now).await?;
    tx.commit().await?;
    Ok(true)
}

pub(super) async fn resume_mandate_with_context<S: WorkDatabase>(
    store: &S,
    mandate_id: &str,
    from_status: MandateStatus,
    expected_version: i64,
    controller_context: Option<&str>,
) -> anyhow::Result<bool> {
    anyhow::ensure!(
        from_status == MandateStatus::Paused
            && from_status.can_transition_to(MandateStatus::Active),
        "ordinary resume is only valid for owner-paused mandates"
    );
    anyhow::ensure!(expected_version > 0, "mandate version must be positive");
    if let Some(context) = controller_context {
        anyhow::ensure!(
            context.chars().count() <= 64_000,
            "mandate controller context is too large"
        );
        serde_json::from_str::<serde_json::Value>(context)?;
    }
    let now = chrono::Utc::now().to_rfc3339();
    let mut tx = store.work_pool().begin_with("BEGIN IMMEDIATE").await?;
    let goal_id = sqlx::query_scalar::<_, String>(
        "UPDATE mandates
             SET status = 'active', next_review_at = ?,
                 review_lease_token = NULL, review_lease_expires_at = NULL,
                 suspension_json = NULL,
                 version = version + 1, updated_at = ?
             WHERE id = ? AND status = ? AND version = ?
               AND confirmed_at IS NOT NULL
               AND json_extract(suspension_json, '$.kind') = 'owner_paused'
             RETURNING goal_id",
    )
    .bind(&now)
    .bind(&now)
    .bind(mandate_id)
    .bind(from_status.as_str())
    .bind(expected_version)
    .fetch_optional(&mut *tx)
    .await?;
    let Some(goal_id) = goal_id else {
        tx.rollback().await?;
        return Ok(false);
    };
    invalidate_open_mandate_runs(&mut tx, &goal_id, &now).await?;
    let controller = sqlx::query(
        "UPDATE goals
             SET status = 'active', context = COALESCE(?, context),
                 completed_at = NULL, dispatch_failures = 0, updated_at = ?
             WHERE id = ? AND domain = 'orchestration' AND goal_type = 'continuous'",
    )
    .bind(controller_context)
    .bind(&now)
    .bind(&goal_id)
    .execute(&mut *tx)
    .await?;
    anyhow::ensure!(
        controller.rows_affected() == 1,
        "mandate controller goal not found"
    );
    tx.commit().await?;
    Ok(true)
}

pub(super) async fn record_active_mandate_owner_guidance<S: WorkDatabase>(
    store: &S,
    mandate_id: &str,
    expected_version: i64,
    owner_session: &str,
    controller_context: &str,
) -> anyhow::Result<bool> {
    anyhow::ensure!(
        controller_context.chars().count() <= 64_000,
        "mandate controller context is too large"
    );
    serde_json::from_str::<serde_json::Value>(controller_context)?;
    let now = chrono::Utc::now().to_rfc3339();
    let updated = sqlx::query(
        "UPDATE goals SET context = ?, updated_at = ?
             WHERE id = (
                 SELECT goal_id FROM mandates
                 WHERE id = ? AND version = ? AND status = 'active'
                   AND confirmed_at IS NOT NULL AND created_by_session = ?
             )
               AND domain = 'orchestration' AND goal_type = 'continuous'",
    )
    .bind(controller_context)
    .bind(&now)
    .bind(mandate_id)
    .bind(expected_version)
    .bind(owner_session)
    .execute(store.work_pool())
    .await?;
    Ok(updated.rows_affected() == 1)
}

pub(super) async fn keep_mandate_controller_active<S: WorkDatabase>(
    store: &S,
    mandate_id: &str,
    expected_version: i64,
) -> anyhow::Result<bool> {
    anyhow::ensure!(expected_version > 0, "mandate version must be positive");
    let now = chrono::Utc::now().to_rfc3339();
    let result = sqlx::query(
        "UPDATE goals
             SET status = 'active', completed_at = NULL, dispatch_failures = 0, updated_at = ?
             WHERE id = (
                 SELECT goal_id FROM mandates
                 WHERE id = ? AND status = 'active' AND confirmed_at IS NOT NULL
                   AND version = ?
             )
               AND domain = 'orchestration' AND goal_type = 'continuous'",
    )
    .bind(&now)
    .bind(mandate_id)
    .bind(expected_version)
    .execute(store.work_pool())
    .await?;
    Ok(result.rows_affected() == 1)
}

pub(super) async fn wake_mandates_for_signal<S: WorkDatabase>(
    store: &S,
    signal: &MandateWakeSignal,
) -> anyhow::Result<Vec<String>> {
    signal.validate().map_err(anyhow::Error::msg)?;
    let mandates = store.list_mandates(None, false).await?;
    let mut target = reqwest::Url::parse(&signal.target_url)?;
    target.set_query(None);
    target.set_fragment(None);
    let target_url = target.to_string();
    let signal_digest = hex::encode(Sha256::digest(
        format!(
            "aidaemon.mandate.wake.v1\0{}\0{}\0{}\0{}\0{}",
            signal.kind.as_str(),
            signal.source,
            signal.target_url,
            signal.account_id.as_deref().unwrap_or(""),
            signal.dedupe_key
        )
        .as_bytes(),
    ));
    let received_at = chrono::Utc::now().to_rfc3339();
    let mut awakened = Vec::new();
    let mut tx = store.work_pool().begin_with("BEGIN IMMEDIATE").await?;
    for mandate in mandates
        .into_iter()
        .filter(|mandate| crate::authority::mandate_accepts_wake_signal(mandate, signal))
    {
        let inserted = sqlx::query(
            "INSERT OR IGNORE INTO mandate_wake_signals (
                    mandate_id, mandate_version, signal_digest, kind, source,
                    target_url, account_id, occurred_at, received_at
                 ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
        )
        .bind(&mandate.id)
        .bind(mandate.version)
        .bind(&signal_digest)
        .bind(signal.kind.as_str())
        .bind(&signal.source)
        .bind(&target_url)
        .bind(&signal.account_id)
        .bind(&signal.occurred_at)
        .bind(&received_at)
        .execute(&mut *tx)
        .await?;
        if inserted.rows_affected() == 0 {
            continue;
        }
        let updated = sqlx::query(
            "UPDATE mandates
                 SET next_review_at = CASE
                        WHEN julianday(next_review_at) > julianday(?) THEN ?
                        ELSE next_review_at
                     END
                 WHERE id = ? AND version = ? AND status = 'active'
                   AND autonomy_mode = 'autopilot' AND confirmed_at IS NOT NULL",
        )
        .bind(&received_at)
        .bind(&received_at)
        .bind(&mandate.id)
        .bind(mandate.version)
        .execute(&mut *tx)
        .await?;
        if updated.rows_affected() == 1 {
            awakened.push(mandate.id);
        }
    }
    tx.commit().await?;
    Ok(awakened)
}

// The trait method's parameters, passed through.
#[allow(clippy::too_many_arguments)]
pub(super) async fn resolve_mandate_suspension<S: WorkDatabase>(
    store: &S,
    mandate_id: &str,
    expected_version: i64,
    expected_kind: MandateSuspensionKind,
    controller_context: Option<&str>,
    reconciliation_resolution: Option<MandateReconciliationResolution>,
    owner_guidance: &str,
    owner_session: &str,
) -> anyhow::Result<bool> {
    anyhow::ensure!(
        !owner_guidance.trim().is_empty() && owner_guidance.trim() == owner_guidance,
        "owner guidance is required and must be canonical"
    );
    anyhow::ensure!(
        owner_guidance.chars().count() <= 1024 && owner_guidance.len() <= 1024,
        "owner guidance exceeds its 1 KiB bound"
    );
    anyhow::ensure!(
        !owner_session.trim().is_empty(),
        "owner session is required"
    );
    if let Some(context) = controller_context {
        anyhow::ensure!(
            context.chars().count() <= 64_000,
            "mandate controller context is too large"
        );
        serde_json::from_str::<serde_json::Value>(context)?;
    }
    match expected_kind {
        MandateSuspensionKind::AwaitingAnswer => anyhow::ensure!(
            reconciliation_resolution.is_none(),
            "non-reconciliation suspensions cannot carry a reconciliation resolution"
        ),
        MandateSuspensionKind::ReconciliationRequired
        | MandateSuspensionKind::ExecutionLeaseLost
        | MandateSuspensionKind::ReviewFailed
        | MandateSuspensionKind::AuthorityRevokedWithUnresolvedMutation => anyhow::ensure!(
            reconciliation_resolution.is_some(),
            "safety suspension requires a typed reconciliation resolution"
        ),
        MandateSuspensionKind::OwnerPaused => {
            anyhow::bail!("owner-paused mandates must use ordinary resume")
        }
    }

    let now = chrono::Utc::now().to_rfc3339();
    let mut tx = store.work_pool().begin_with("BEGIN IMMEDIATE").await?;
    let current = sqlx::query(
        "SELECT goal_id, created_by_session, suspension_json
             FROM mandates
             WHERE id = ? AND version = ? AND status = 'awaiting_input'
               AND confirmed_at IS NOT NULL",
    )
    .bind(mandate_id)
    .bind(expected_version)
    .fetch_optional(&mut *tx)
    .await?;
    let Some(current) = current else {
        tx.rollback().await?;
        return Ok(false);
    };
    anyhow::ensure!(
        current.get::<String, _>("created_by_session") == owner_session,
        "mandate does not belong to this owner session"
    );
    let suspension: MandateSuspension = serde_json::from_str(
        &current
            .get::<Option<String>, _>("suspension_json")
            .ok_or_else(|| anyhow::anyhow!("awaiting-input mandate has no typed suspension"))?,
    )?;
    anyhow::ensure!(
        suspension.kind == expected_kind,
        "mandate suspension changed before it was resolved"
    );
    let goal_id: String = current.get("goal_id");

    if let Some(resolution) = reconciliation_resolution {
        sqlx::query(
            "INSERT INTO mandate_reconciliations (
                    id, mandate_id, suspended_version, suspension_kind, resolution,
                    owner_guidance, resolved_by_session, resolved_at
                 ) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
        )
        .bind(uuid::Uuid::new_v4().to_string())
        .bind(mandate_id)
        .bind(expected_version)
        .bind(expected_kind.as_str())
        .bind(resolution.as_str())
        .bind(owner_guidance)
        .bind(owner_session)
        .bind(&now)
        .execute(&mut *tx)
        .await?;
    }

    let updated = sqlx::query(
        "UPDATE mandates
             SET status = 'active', suspension_json = NULL, next_review_at = ?,
                 review_lease_token = NULL, review_lease_expires_at = NULL,
                 version = version + 1, updated_at = ?
             WHERE id = ? AND version = ? AND status = 'awaiting_input'",
    )
    .bind(&now)
    .bind(&now)
    .bind(mandate_id)
    .bind(expected_version)
    .execute(&mut *tx)
    .await?;
    if updated.rows_affected() != 1 {
        tx.rollback().await?;
        return Ok(false);
    }
    if let Some(context) = controller_context {
        sqlx::query("UPDATE goals SET context = ?, updated_at = ? WHERE id = ?")
            .bind(context)
            .bind(&now)
            .bind(&goal_id)
            .execute(&mut *tx)
            .await?;
    }
    update_controller_status(&mut tx, &goal_id, MandateStatus::Active, &now).await?;
    tx.commit().await?;
    Ok(true)
}