iicp-client 0.7.94

Use the open IICP AI mesh from Rust without running a node
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
// SPDX-License-Identifier: Apache-2.0
//! Opt-in single-host SQLite persistence for the draft lifecycle profile.
//!
//! The adapter coordinates local processes through transactional state/event
//! updates. It is not a distributed consensus store and does not protect
//! against restoration of the complete database.

use crate::service_lifecycle::{
    legal_transition, now_ms, terminal, LifecycleError, LifecycleEvent, LifecycleFuture,
    LifecyclePersistence, LifecycleRecord,
};
use rusqlite::{params, Connection, OptionalExtension, TransactionBehavior};
use serde_json::{Map, Value};
use std::{
    path::{Path, PathBuf},
    time::Duration,
};

const SCHEMA_VERSION: i64 = 1;

#[derive(Clone, Debug)]
pub struct SqliteLifecyclePersistence {
    path: PathBuf,
    max_events: usize,
    terminal_status_ttl_ms: u64,
}

impl SqliteLifecyclePersistence {
    pub fn open(
        path: impl AsRef<Path>,
        max_events: usize,
        terminal_status_ttl_ms: u64,
    ) -> Result<Self, LifecycleError> {
        let store = Self {
            path: path.as_ref().to_path_buf(),
            max_events: max_events.max(2),
            terminal_status_ttl_ms,
        };
        if let Some(parent) = store.path.parent() {
            std::fs::create_dir_all(parent).map_err(storage)?;
        }
        let db = store.connect()?;
        let version: i64 = db
            .query_row("PRAGMA user_version", [], |row| row.get(0))
            .map_err(storage)?;
        if !matches!(version, 0 | SCHEMA_VERSION) {
            return Err(LifecycleError::Storage(format!(
                "unsupported lifecycle database version {version}"
            )));
        }
        db.execute_batch(
            "CREATE TABLE IF NOT EXISTS lifecycle_tasks (
                task_id TEXT PRIMARY KEY,
                idempotency_key TEXT NOT NULL UNIQUE,
                request_digest TEXT NOT NULL,
                state TEXT NOT NULL,
                latest_sequence INTEGER NOT NULL,
                updated_at_ms INTEGER NOT NULL
            );
            CREATE TABLE IF NOT EXISTS lifecycle_events (
                task_id TEXT NOT NULL,
                sequence INTEGER NOT NULL,
                state TEXT NOT NULL,
                is_final INTEGER NOT NULL,
                observed_at_ms INTEGER NOT NULL,
                detail_json BLOB NOT NULL,
                PRIMARY KEY(task_id, sequence),
                FOREIGN KEY(task_id) REFERENCES lifecycle_tasks(task_id) ON DELETE CASCADE
            );
            PRAGMA user_version=1;",
        )
        .map_err(storage)?;
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            std::fs::set_permissions(&store.path, std::fs::Permissions::from_mode(0o600))
                .map_err(storage)?;
        }
        Ok(store)
    }

    fn connect(&self) -> Result<Connection, LifecycleError> {
        let db = Connection::open(&self.path).map_err(storage)?;
        db.busy_timeout(Duration::from_secs(5)).map_err(storage)?;
        db.pragma_update(None, "journal_mode", "WAL")
            .map_err(storage)?;
        db.pragma_update(None, "foreign_keys", "ON")
            .map_err(storage)?;
        Ok(db)
    }

    fn load_record(db: &Connection, task_id: &str) -> Result<LifecycleRecord, LifecycleError> {
        let task = db
            .query_row(
                "SELECT task_id,idempotency_key,request_digest,state,updated_at_ms FROM lifecycle_tasks WHERE task_id=?1",
                [task_id],
                |row| {
                    Ok((
                        row.get::<_, String>(0)?,
                        row.get::<_, String>(1)?,
                        row.get::<_, String>(2)?,
                        row.get::<_, String>(3)?,
                        row.get::<_, u64>(4)?,
                    ))
                },
            )
            .optional()
            .map_err(storage)?
            .ok_or(LifecycleError::UnknownTask)?;
        let mut statement = db
            .prepare(
                "SELECT task_id,sequence,state,is_final,observed_at_ms,detail_json
                 FROM lifecycle_events WHERE task_id=?1 ORDER BY sequence",
            )
            .map_err(storage)?;
        let events = statement
            .query_map([task_id], |row| {
                let detail: Vec<u8> = row.get(5)?;
                let detail = serde_json::from_slice(&detail).map_err(|error| {
                    rusqlite::Error::FromSqlConversionFailure(
                        detail.len(),
                        rusqlite::types::Type::Blob,
                        Box::new(error),
                    )
                })?;
                Ok(LifecycleEvent {
                    task_id: row.get(0)?,
                    sequence: row.get(1)?,
                    state: row.get(2)?,
                    is_final: row.get::<_, i64>(3)? != 0,
                    observed_at_ms: row.get(4)?,
                    detail,
                })
            })
            .map_err(storage)?
            .collect::<Result<Vec<_>, _>>()
            .map_err(storage)?;
        if events.is_empty() {
            return Err(LifecycleError::Storage(
                "task has no lifecycle events".into(),
            ));
        }
        Ok(LifecycleRecord {
            task_id: task.0,
            idempotency_key: task.1,
            request_digest: task.2,
            state: task.3,
            events,
            updated_at_ms: task.4,
        })
    }

    fn submit_sync(
        &self,
        task_id: &str,
        idempotency_key: &str,
        request_digest: &str,
    ) -> Result<(LifecycleRecord, bool), LifecycleError> {
        let mut db = self.connect()?;
        let tx = db
            .transaction_with_behavior(TransactionBehavior::Immediate)
            .map_err(storage)?;
        let existing = tx
            .query_row(
                "SELECT task_id,idempotency_key,request_digest FROM lifecycle_tasks
                 WHERE task_id=?1 OR idempotency_key=?2",
                params![task_id, idempotency_key],
                |row| {
                    Ok((
                        row.get::<_, String>(0)?,
                        row.get::<_, String>(1)?,
                        row.get::<_, String>(2)?,
                    ))
                },
            )
            .optional()
            .map_err(storage)?;
        if let Some(existing) = existing {
            if existing
                != (
                    task_id.to_owned(),
                    idempotency_key.to_owned(),
                    request_digest.to_owned(),
                )
            {
                return Err(LifecycleError::Conflict(
                    "task or idempotency identifier reused for different content".into(),
                ));
            }
            let record = Self::load_record(&tx, task_id)?;
            tx.commit().map_err(storage)?;
            return Ok((record, false));
        }
        let now = now_ms();
        tx.execute(
            "INSERT INTO lifecycle_tasks VALUES(?1,?2,?3,'accepted',0,?4)",
            params![task_id, idempotency_key, request_digest, now],
        )
        .map_err(storage)?;
        tx.execute(
            "INSERT INTO lifecycle_events VALUES(?1,0,'accepted',0,?2,?3)",
            params![task_id, now, b"{}".as_slice()],
        )
        .map_err(storage)?;
        let record = Self::load_record(&tx, task_id)?;
        tx.commit().map_err(storage)?;
        Ok((record, true))
    }

    fn status_sync(&self, task_id: &str) -> Result<LifecycleRecord, LifecycleError> {
        let mut db = self.connect()?;
        let tx = db
            .transaction_with_behavior(TransactionBehavior::Immediate)
            .map_err(storage)?;
        let row = tx
            .query_row(
                "SELECT state,updated_at_ms FROM lifecycle_tasks WHERE task_id=?1",
                [task_id],
                |row| Ok((row.get::<_, String>(0)?, row.get::<_, u64>(1)?)),
            )
            .optional()
            .map_err(storage)?
            .ok_or(LifecycleError::UnknownTask)?;
        if terminal(&row.0) && now_ms().saturating_sub(row.1) > self.terminal_status_ttl_ms {
            tx.execute("DELETE FROM lifecycle_tasks WHERE task_id=?1", [task_id])
                .map_err(storage)?;
            tx.commit().map_err(storage)?;
            return Err(LifecycleError::UnknownTask);
        }
        let record = Self::load_record(&tx, task_id)?;
        tx.commit().map_err(storage)?;
        Ok(record)
    }

    fn transition_sync(
        &self,
        task_id: &str,
        requested_state: &str,
        detail: Value,
    ) -> Result<LifecycleEvent, LifecycleError> {
        let state = if requested_state == "timed_out" {
            "expired"
        } else {
            requested_state
        };
        let detail = content_free_detail(detail)?;
        let detail_json = serde_json::to_vec(&detail).map_err(storage)?;
        let mut db = self.connect()?;
        let tx = db
            .transaction_with_behavior(TransactionBehavior::Immediate)
            .map_err(storage)?;
        let current = tx
            .query_row(
                "SELECT state,latest_sequence FROM lifecycle_tasks WHERE task_id=?1",
                [task_id],
                |row| Ok((row.get::<_, String>(0)?, row.get::<_, u64>(1)?)),
            )
            .optional()
            .map_err(storage)?
            .ok_or(LifecycleError::UnknownTask)?;
        if !legal_transition(&current.0, state) {
            return Err(LifecycleError::Conflict(format!(
                "illegal transition {} -> {state}",
                current.0
            )));
        }
        let sequence = current.1 + 1;
        let now = now_ms();
        tx.execute(
            "UPDATE lifecycle_tasks SET state=?1,latest_sequence=?2,updated_at_ms=?3 WHERE task_id=?4",
            params![state, sequence, now, task_id],
        )
        .map_err(storage)?;
        tx.execute(
            "INSERT INTO lifecycle_events VALUES(?1,?2,?3,?4,?5,?6)",
            params![
                task_id,
                sequence,
                state,
                i64::from(terminal(state)),
                now,
                detail_json
            ],
        )
        .map_err(storage)?;
        let cutoff = sequence.saturating_sub(self.max_events as u64 - 1);
        tx.execute(
            "DELETE FROM lifecycle_events WHERE task_id=?1 AND sequence<?2",
            params![task_id, cutoff],
        )
        .map_err(storage)?;
        tx.commit().map_err(storage)?;
        Ok(LifecycleEvent {
            task_id: task_id.into(),
            sequence,
            state: state.into(),
            is_final: terminal(state),
            observed_at_ms: now,
            detail,
        })
    }

    fn events_after_sync(
        &self,
        task_id: &str,
        after_sequence: i64,
    ) -> Result<Vec<LifecycleEvent>, LifecycleError> {
        let record = self.status_sync(task_id)?;
        let first = record.events.first().map_or(0, |event| event.sequence);
        if after_sequence >= 0 && (after_sequence as u64).saturating_add(1) < first {
            let latest_sequence = record.latest_sequence();
            return Err(LifecycleError::ResumeUnavailable {
                state: record.state,
                latest_sequence,
            });
        }
        Ok(record
            .events
            .into_iter()
            .filter(|event| event.sequence as i64 > after_sequence)
            .collect())
    }
}

impl LifecyclePersistence for SqliteLifecyclePersistence {
    fn submit<'a>(
        &'a self,
        task_id: &'a str,
        idempotency_key: &'a str,
        request_digest: &'a str,
    ) -> LifecycleFuture<'a, (LifecycleRecord, bool)> {
        let store = self.clone();
        let task_id = task_id.to_owned();
        let idempotency_key = idempotency_key.to_owned();
        let request_digest = request_digest.to_owned();
        Box::pin(async move {
            tokio::task::spawn_blocking(move || {
                store.submit_sync(&task_id, &idempotency_key, &request_digest)
            })
            .await
            .map_err(storage)?
        })
    }

    fn status<'a>(&'a self, task_id: &'a str) -> LifecycleFuture<'a, LifecycleRecord> {
        let store = self.clone();
        let task_id = task_id.to_owned();
        Box::pin(async move {
            tokio::task::spawn_blocking(move || store.status_sync(&task_id))
                .await
                .map_err(storage)?
        })
    }

    fn transition<'a>(
        &'a self,
        task_id: &'a str,
        state: &'a str,
        detail: Value,
    ) -> LifecycleFuture<'a, LifecycleEvent> {
        let store = self.clone();
        let task_id = task_id.to_owned();
        let state = state.to_owned();
        Box::pin(async move {
            tokio::task::spawn_blocking(move || store.transition_sync(&task_id, &state, detail))
                .await
                .map_err(storage)?
        })
    }

    fn cancel<'a>(&'a self, task_id: &'a str) -> LifecycleFuture<'a, LifecycleRecord> {
        let store = self.clone();
        let task_id = task_id.to_owned();
        Box::pin(async move {
            tokio::task::spawn_blocking(move || {
                let current = store.status_sync(&task_id)?;
                if !terminal(&current.state) {
                    store.transition_sync(
                        &task_id,
                        "cancelled",
                        serde_json::json!({"outcome": "cancelled"}),
                    )?;
                }
                store.status_sync(&task_id)
            })
            .await
            .map_err(storage)?
        })
    }

    fn events_after<'a>(
        &'a self,
        task_id: &'a str,
        after_sequence: i64,
    ) -> LifecycleFuture<'a, Vec<LifecycleEvent>> {
        let store = self.clone();
        let task_id = task_id.to_owned();
        Box::pin(async move {
            tokio::task::spawn_blocking(move || store.events_after_sync(&task_id, after_sequence))
                .await
                .map_err(storage)?
        })
    }
}

fn content_free_detail(detail: Value) -> Result<Value, LifecycleError> {
    if detail.is_null() {
        return Ok(Value::Object(Map::new()));
    }
    let object = detail.as_object().ok_or_else(|| {
        LifecycleError::Conflict("durable lifecycle detail must be an object".into())
    })?;
    let mut result = Map::new();
    for (key, value) in object {
        match key.as_str() {
            "progress" => {
                let progress = value.as_object().ok_or_else(|| {
                    LifecycleError::Conflict("invalid durable lifecycle progress".into())
                })?;
                if progress
                    .keys()
                    .any(|key| !matches!(key.as_str(), "completed_units" | "total_units" | "unit"))
                {
                    return Err(LifecycleError::Conflict(
                        "invalid durable lifecycle progress".into(),
                    ));
                }
                let completed = progress
                    .get("completed_units")
                    .and_then(Value::as_u64)
                    .ok_or_else(|| {
                        LifecycleError::Conflict("invalid durable lifecycle progress counts".into())
                    })?;
                let total = progress
                    .get("total_units")
                    .and_then(Value::as_u64)
                    .ok_or_else(|| {
                        LifecycleError::Conflict("invalid durable lifecycle progress counts".into())
                    })?;
                if total < completed {
                    return Err(LifecycleError::Conflict(
                        "invalid durable lifecycle progress counts".into(),
                    ));
                }
                if let Some(unit) = progress.get("unit") {
                    safe_token(unit.as_str().ok_or_else(|| {
                        LifecycleError::Conflict("invalid progress unit".into())
                    })?)?;
                }
                result.insert(key.clone(), value.clone());
            }
            "receipt_digest" | "checkpoint_digest" => {
                let digest = value
                    .as_str()
                    .ok_or_else(|| LifecycleError::Conflict(format!("invalid {key}")))?;
                let hex = digest.strip_prefix("sha256:").unwrap_or(digest);
                if hex.len() != 64 || !hex.bytes().all(|byte| byte.is_ascii_hexdigit()) {
                    return Err(LifecycleError::Conflict(format!("invalid {key}")));
                }
                result.insert(key.clone(), Value::String(digest.to_ascii_lowercase()));
            }
            "event_id" | "reason_code" | "outcome" => {
                safe_token(value.as_str().ok_or_else(|| {
                    LifecycleError::Conflict(format!("invalid durable lifecycle {key}"))
                })?)?;
                result.insert(key.clone(), value.clone());
            }
            _ => {
                return Err(LifecycleError::Conflict(format!(
                    "durable lifecycle detail contains unsupported field: {key}"
                )));
            }
        }
    }
    Ok(Value::Object(result))
}

fn safe_token(value: &str) -> Result<(), LifecycleError> {
    if value.is_empty()
        || value.len() > 256
        || !value
            .bytes()
            .all(|byte| byte.is_ascii_alphanumeric() || b"._:-".contains(&byte))
    {
        return Err(LifecycleError::Conflict(
            "invalid durable lifecycle token".into(),
        ));
    }
    Ok(())
}

fn storage(error: impl std::fmt::Display) -> LifecycleError {
    LifecycleError::Storage(error.to_string())
}