takeln 0.11.0

Typed Rust runtime for durable DAG-based agent workflows
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
495
496
497
498
//! SQLite-backed checkpointer using `rusqlite`.
//!
//! Feature-gated behind the `sqlite` feature flag.

use async_trait::async_trait;
use std::sync::Arc;

use crate::checkpoint::{Checkpointer, ClaimResult};
use crate::checkpoint_meta::{CheckpointMeta, CheckpointStatus, RetentionPolicy};
use crate::dag::DAG;
use crate::error::TakelnError;
use crate::graph::State;
use crate::hitl::YieldRequest;

/// SQLite-backed checkpointer for durable local persistence.
///
/// Requires the `sqlite` feature flag. Uses `rusqlite` with synchronous
/// operations offloaded to `spawn_blocking` for async compatibility.
///
/// The database file persists across process restarts, making this ideal for
/// single-process deployments that need durability without a database server.
pub struct SqliteCheckpointer<S: State> {
    conn: Arc<std::sync::Mutex<rusqlite::Connection>>,
    _marker: std::marker::PhantomData<S>,
}

impl<S: State> SqliteCheckpointer<S> {
    /// Create a new SQLite checkpointer at the given path.
    ///
    /// Creates the database file and table if they don't exist.
    pub fn new(path: &str) -> Result<Self, TakelnError> {
        let conn = rusqlite::Connection::open(path)
            .map_err(|e| TakelnError::CheckpointError(format!("Failed to open SQLite: {}", e)))?;

        conn.execute_batch(
            "CREATE TABLE IF NOT EXISTS takeln_checkpoints (
                id TEXT PRIMARY KEY,
                thread_id TEXT NOT NULL,
                state TEXT NOT NULL,
                next_node TEXT NOT NULL,
                dag TEXT,
                status TEXT NOT NULL,
                created_at TEXT NOT NULL,
                yield_request TEXT,
                claimed_interrupt TEXT,
                resolved_interrupt TEXT
            );
            CREATE INDEX IF NOT EXISTS idx_takeln_thread_created ON takeln_checkpoints(thread_id, created_at);",
        )
        .map_err(|e| TakelnError::CheckpointError(format!("Failed to create table: {}", e)))?;

        // Migrate older databases if needed
        let _ = conn.execute("ALTER TABLE takeln_checkpoints ADD COLUMN yield_request TEXT", []);
        let _ = conn.execute("ALTER TABLE takeln_checkpoints ADD COLUMN claimed_interrupt TEXT", []);
        let _ = conn.execute("ALTER TABLE takeln_checkpoints ADD COLUMN resolved_interrupt TEXT", []);

        Ok(Self {
            conn: Arc::new(std::sync::Mutex::new(conn)),
            _marker: std::marker::PhantomData,
        })
    }

    /// Create a new in-memory SQLite checkpointer (useful for testing).
    pub fn in_memory() -> Result<Self, TakelnError> {
        Self::new(":memory:")
    }
}

#[async_trait]
impl<S: State> Checkpointer<S> for SqliteCheckpointer<S> {
    async fn save_state(
        &self,
        thread_id: String,
        state: S,
        next_node: String,
        dag: Option<&DAG>,
        status: CheckpointStatus,
        yield_request: Option<YieldRequest>,
        claimed_interrupt: Option<String>,
        resolved_interrupt: Option<String>,
    ) -> Result<String, TakelnError> {
        let checkpoint_id = uuid::Uuid::new_v4().to_string();
        let state_json = serde_json::to_string(&state).map_err(|e| TakelnError::SerializationError(e.to_string()))?;
        let dag_json = dag
            .map(serde_json::to_string)
            .transpose()
            .map_err(|e| TakelnError::SerializationError(e.to_string()))?;
        let yield_request_json = yield_request
            .map(|req| serde_json::to_string(&req))
            .transpose()
            .map_err(|e| TakelnError::SerializationError(e.to_string()))?;
        let status_str = format!("{:?}", status);
        let created_at = chrono::Utc::now().to_rfc3339();

        let conn = self.conn.clone();
        let cp_id = checkpoint_id.clone();

        tokio::task::spawn_blocking(move || {
            let conn = conn
                .lock()
                .map_err(|e| TakelnError::CheckpointError(format!("Mutex poisoning: {}", e)))?;

            conn.execute(
                "INSERT INTO takeln_checkpoints (id, thread_id, state, next_node, dag, status, created_at, yield_request, claimed_interrupt, resolved_interrupt) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10)",
                rusqlite::params![
                    cp_id,
                    thread_id,
                    state_json,
                    next_node,
                    dag_json,
                    status_str,
                    created_at,
                    yield_request_json,
                    claimed_interrupt,
                    resolved_interrupt
                ],
            )
            .map_err(|e| TakelnError::CheckpointError(format!("Failed to save: {}", e)))?;

            Ok(cp_id)
        })
        .await
        .map_err(|e| TakelnError::CheckpointError(format!("spawn_blocking join error: {}", e)))?
    }

    async fn load_state(&self, thread_id: String) -> Result<Option<(S, CheckpointMeta, Option<DAG>)>, TakelnError> {
        let conn = self.conn.clone();

        let row_opt = tokio::task::spawn_blocking(move || {
            let conn = conn
                .lock()
                .map_err(|e| TakelnError::CheckpointError(format!("Mutex poisoning: {}", e)))?;

            let mut stmt = conn.prepare(
                "SELECT id, state, next_node, dag, status, created_at, yield_request, claimed_interrupt, resolved_interrupt FROM takeln_checkpoints WHERE thread_id = ?1 ORDER BY created_at DESC LIMIT 1",
            ).map_err(|e| TakelnError::CheckpointError(format!("Failed to prepare: {}", e)))?;

            let result = stmt.query_row(rusqlite::params![thread_id], |row| {
                Ok((
                    row.get::<_, String>(0)?,
                    row.get::<_, String>(1)?,
                    row.get::<_, String>(2)?,
                    row.get::<_, Option<String>>(3)?,
                    row.get::<_, String>(4)?,
                    row.get::<_, String>(5)?,
                    row.get::<_, Option<String>>(6)?,
                    row.get::<_, Option<String>>(7)?,
                    row.get::<_, Option<String>>(8)?,
                    thread_id.clone(),
                ))
            });

            match result {
                Ok(row) => Ok(Some(row)),
                Err(rusqlite::Error::QueryReturnedNoRows) => Ok(None),
                Err(e) => Err(TakelnError::CheckpointError(format!("Failed to load: {}", e))),
            }
        })
        .await
        .map_err(|e| TakelnError::CheckpointError(format!("spawn_blocking join error: {}", e)))??;

        match row_opt {
            Some((
                id,
                state_str,
                next_node,
                dag_str,
                status_str,
                created_at_str,
                yield_request_str,
                claimed_interrupt_str,
                resolved_interrupt_str,
                thread_id,
            )) => {
                let state: S =
                    serde_json::from_str(&state_str).map_err(|e| TakelnError::DeserializationError(e.to_string()))?;
                let dag: Option<DAG> = dag_str
                    .map(|s| serde_json::from_str(&s))
                    .transpose()
                    .map_err(|e| TakelnError::DeserializationError(e.to_string()))?;
                let created_at = chrono::DateTime::parse_from_rfc3339(&created_at_str)
                    .map(|dt| dt.with_timezone(&chrono::Utc))
                    .unwrap_or_else(|_| chrono::Utc::now());
                let yield_request = yield_request_str
                    .map(|s| serde_json::from_str(&s))
                    .transpose()
                    .map_err(|e| TakelnError::DeserializationError(e.to_string()))?;
                let meta = CheckpointMeta {
                    checkpoint_id: id,
                    thread_id,
                    next_node,
                    graph_version: None,
                    state_schema_version: None,
                    status: parse_status(&status_str),
                    created_at,
                    yield_request,
                    claimed_interrupt: claimed_interrupt_str,
                    resolved_interrupt: resolved_interrupt_str,
                };
                Ok(Some((state, meta, dag)))
            }
            None => Ok(None),
        }
    }

    async fn load_version(
        &self,
        thread_id: String,
        checkpoint_id: String,
    ) -> Result<Option<(S, CheckpointMeta, Option<DAG>)>, TakelnError> {
        let conn = self.conn.clone();

        let row_opt = tokio::task::spawn_blocking(move || {
            let conn = conn
                .lock()
                .map_err(|e| TakelnError::CheckpointError(format!("Mutex poisoning: {}", e)))?;

            let mut stmt = conn.prepare(
                "SELECT id, state, next_node, dag, status, created_at, yield_request, claimed_interrupt, resolved_interrupt FROM takeln_checkpoints WHERE thread_id = ?1 AND id = ?2",
            ).map_err(|e| TakelnError::CheckpointError(format!("Failed to prepare: {}", e)))?;

            let result = stmt.query_row(rusqlite::params![thread_id, checkpoint_id], |row| {
                Ok((
                    row.get::<_, String>(0)?,
                    row.get::<_, String>(1)?,
                    row.get::<_, String>(2)?,
                    row.get::<_, Option<String>>(3)?,
                    row.get::<_, String>(4)?,
                    row.get::<_, String>(5)?,
                    row.get::<_, Option<String>>(6)?,
                    row.get::<_, Option<String>>(7)?,
                    row.get::<_, Option<String>>(8)?,
                    thread_id.clone(),
                ))
            });

            match result {
                Ok(row) => Ok(Some(row)),
                Err(rusqlite::Error::QueryReturnedNoRows) => Ok(None),
                Err(e) => Err(TakelnError::CheckpointError(format!("Failed to load version: {}", e))),
            }
        })
        .await
        .map_err(|e| TakelnError::CheckpointError(format!("spawn_blocking join error: {}", e)))??;

        match row_opt {
            Some((
                id,
                state_str,
                next_node,
                dag_str,
                status_str,
                created_at_str,
                yield_request_str,
                claimed_interrupt_str,
                resolved_interrupt_str,
                thread_id,
            )) => {
                let state: S =
                    serde_json::from_str(&state_str).map_err(|e| TakelnError::DeserializationError(e.to_string()))?;
                let dag: Option<DAG> = dag_str
                    .map(|s| serde_json::from_str(&s))
                    .transpose()
                    .map_err(|e| TakelnError::DeserializationError(e.to_string()))?;
                let created_at = chrono::DateTime::parse_from_rfc3339(&created_at_str)
                    .map(|dt| dt.with_timezone(&chrono::Utc))
                    .unwrap_or_else(|_| chrono::Utc::now());
                let yield_request = yield_request_str
                    .map(|s| serde_json::from_str(&s))
                    .transpose()
                    .map_err(|e| TakelnError::DeserializationError(e.to_string()))?;
                let meta = CheckpointMeta {
                    checkpoint_id: id,
                    thread_id,
                    next_node,
                    graph_version: None,
                    state_schema_version: None,
                    status: parse_status(&status_str),
                    created_at,
                    yield_request,
                    claimed_interrupt: claimed_interrupt_str,
                    resolved_interrupt: resolved_interrupt_str,
                };
                Ok(Some((state, meta, dag)))
            }
            None => Ok(None),
        }
    }

    async fn list_checkpoints(&self, thread_id: String) -> Result<Vec<CheckpointMeta>, TakelnError> {
        let conn = self.conn.clone();

        tokio::task::spawn_blocking(move || {
            let conn = conn
                .lock()
                .map_err(|e| TakelnError::CheckpointError(format!("Mutex poisoning: {}", e)))?;

            let mut stmt = conn.prepare(
                "SELECT id, next_node, status, created_at, yield_request, claimed_interrupt, resolved_interrupt FROM takeln_checkpoints WHERE thread_id = ?1 ORDER BY created_at ASC",
            ).map_err(|e| TakelnError::CheckpointError(format!("Failed to prepare: {}", e)))?;

            let rows = stmt
                .query_map(rusqlite::params![thread_id], |row| {
                    Ok((
                        row.get::<_, String>(0)?,
                        row.get::<_, String>(1)?,
                        row.get::<_, String>(2)?,
                        row.get::<_, String>(3)?,
                        row.get::<_, Option<String>>(4)?,
                        row.get::<_, Option<String>>(5)?,
                        row.get::<_, Option<String>>(6)?,
                    ))
                })
                .map_err(|e| TakelnError::CheckpointError(format!("Failed to list: {}", e)))?;

            let mut result = Vec::new();
            for row in rows {
                let (id, next_node, status_str, created_at_str, yield_request_str, claimed_interrupt_str, resolved_interrupt_str) =
                    row.map_err(|e| TakelnError::CheckpointError(format!("Row error: {}", e)))?;
                let created_at = chrono::DateTime::parse_from_rfc3339(&created_at_str)
                    .map(|dt| dt.with_timezone(&chrono::Utc))
                    .unwrap_or_else(|_| chrono::Utc::now());
                let yield_request = yield_request_str
                    .map(|s| serde_json::from_str(&s))
                    .transpose()
                    .map_err(|e| TakelnError::DeserializationError(e.to_string()))?;
                result.push(CheckpointMeta {
                    checkpoint_id: id,
                    thread_id: thread_id.clone(),
                    next_node,
                    graph_version: None,
                    state_schema_version: None,
                    status: parse_status(&status_str),
                    created_at,
                    yield_request,
                    claimed_interrupt: claimed_interrupt_str,
                    resolved_interrupt: resolved_interrupt_str,
                });
            }
            Ok(result)
        })
        .await
        .map_err(|e| TakelnError::CheckpointError(format!("spawn_blocking join error: {}", e)))?
    }

    async fn delete_checkpoints(&self, thread_id: String, policy: RetentionPolicy) -> Result<usize, TakelnError> {
        let conn = self.conn.clone();

        tokio::task::spawn_blocking(move || {
            let conn = conn
                .lock()
                .map_err(|e| TakelnError::CheckpointError(format!("Mutex poisoning: {}", e)))?;

            match policy {
                RetentionPolicy::KeepAll => Ok(0),
                RetentionPolicy::KeepLast(n) => {
                    let deleted = conn.execute(
                        "DELETE FROM takeln_checkpoints WHERE thread_id = ?1 AND id NOT IN (SELECT id FROM takeln_checkpoints WHERE thread_id = ?1 ORDER BY created_at DESC LIMIT ?2)",
                        rusqlite::params![thread_id, n],
                    ).map_err(|e| TakelnError::CheckpointError(format!("Failed to delete: {}", e)))?;
                    Ok(deleted)
                }
                RetentionPolicy::OlderThan(duration) => {
                    let cutoff = chrono::Utc::now()
                        - chrono::Duration::from_std(duration).map_err(|e| TakelnError::CheckpointError(e.to_string()))?;
                    let cutoff_str = cutoff.to_rfc3339();
                    let deleted = conn
                        .execute(
                            "DELETE FROM takeln_checkpoints WHERE thread_id = ?1 AND created_at < ?2",
                            rusqlite::params![thread_id, cutoff_str],
                        )
                        .map_err(|e| TakelnError::CheckpointError(format!("Failed to delete: {}", e)))?;
                    Ok(deleted)
                }
            }
        })
        .await
        .map_err(|e| TakelnError::CheckpointError(format!("spawn_blocking join error: {}", e)))?
    }

    async fn claim_interrupt(&self, thread_id: &str, interrupt_id: &str) -> Result<ClaimResult, TakelnError> {
        let conn = self.conn.clone();
        let thread_id = thread_id.to_string();
        let interrupt_id = interrupt_id.to_string();

        tokio::task::spawn_blocking(move || {
            let mut conn_guard = conn.lock().map_err(|e| TakelnError::CheckpointError(format!("Mutex poisoning: {}", e)))?;
            let tx = conn_guard.transaction().map_err(|e| TakelnError::CheckpointError(format!("Failed to start transaction: {}", e)))?;

            let exists: bool = {
                // 1. Check if already resolved in history
                let mut stmt = tx.prepare(
                    "SELECT EXISTS(SELECT 1 FROM takeln_checkpoints WHERE thread_id = ?1 AND resolved_interrupt = ?2)"
                ).map_err(|e| TakelnError::CheckpointError(format!("Failed to prepare: {}", e)))?;

                stmt.query_row(rusqlite::params![thread_id, interrupt_id], |row| row.get(0))
                    .map_err(|e| TakelnError::CheckpointError(format!("Failed to check interrupt: {}", e)))?
            };

            if exists {
                return Ok(ClaimResult::AlreadyCompleted);
            }

            // 2. Get latest checkpoint status, yield_request, and claimed_interrupt
            let latest_opt = {
                let mut stmt = tx.prepare(
                    "SELECT status, yield_request, claimed_interrupt FROM takeln_checkpoints WHERE thread_id = ?1 ORDER BY created_at DESC LIMIT 1"
                ).map_err(|e| TakelnError::CheckpointError(format!("Failed to prepare: {}", e)))?;

                stmt.query_row(rusqlite::params![thread_id], |row| {
                    Ok((
                        row.get::<_, String>(0)?,
                        row.get::<_, Option<String>>(1)?,
                        row.get::<_, Option<String>>(2)?,
                    ))
                })
            };

            let (status, yield_req_str, claimed_interrupt) = match latest_opt {
                Ok(triplet) => triplet,
                Err(rusqlite::Error::QueryReturnedNoRows) => {
                    return Err(TakelnError::NothingToResume(thread_id));
                }
                Err(e) => return Err(TakelnError::CheckpointError(format!("Failed to read latest: {}", e))),
            };

            let status_enum = parse_status(&status);
            if status_enum == CheckpointStatus::Running && claimed_interrupt.as_deref() == Some(&interrupt_id) {
                return Ok(ClaimResult::InProgress);
            }

            if status_enum != CheckpointStatus::Yielded {
                if status_enum == CheckpointStatus::Running {
                    return Err(TakelnError::ExecutionError(format!(
                        "Resume claim failed: thread '{}' is already running with claimed_interrupt '{}'",
                        thread_id,
                        claimed_interrupt.as_deref().unwrap_or("none")
                    )));
                } else {
                    return Err(TakelnError::NothingToResume(format!(
                        "Thread '{}' latest checkpoint status is '{}', expected Yielded", thread_id, status
                    )));
                }
            }

            let yield_req: YieldRequest = match yield_req_str {
                Some(s) => serde_json::from_str(&s).map_err(|e| TakelnError::DeserializationError(e.to_string()))?,
                None => {
                    return Err(TakelnError::NothingToResume(format!(
                        "Thread '{}' has Yielded status but no yield_request metadata", thread_id
                    )));
                }
            };

            if yield_req.interrupt_id != interrupt_id {
                return Err(TakelnError::InvalidResume(format!(
                    "Expected interrupt_id '{}', got '{}'", yield_req.interrupt_id, interrupt_id
                )));
            }

            // 3. Atomically update status and claimed_interrupt (NOT resolved_interrupt)
            let rows_affected = tx.execute(
                r#"
                UPDATE takeln_checkpoints
                SET status = 'Running', claimed_interrupt = ?1
                WHERE id = (
                    SELECT id FROM takeln_checkpoints
                    WHERE thread_id = ?2
                    ORDER BY created_at DESC LIMIT 1
                ) AND status = 'Yielded'
                "#,
                rusqlite::params![interrupt_id, thread_id],
            ).map_err(|e| TakelnError::CheckpointError(format!("Failed to update: {}", e)))?;

            if rows_affected == 1 {
                tx.commit().map_err(|e| TakelnError::CheckpointError(format!("Failed to commit: {}", e)))?;
                Ok(ClaimResult::Claimed)
            } else {
                Err(TakelnError::ExecutionError(format!(
                    "Concurrent resume claim failed for thread '{}'", thread_id
                )))
            }
        })
        .await
        .map_err(|e| TakelnError::CheckpointError(format!("spawn_blocking join error: {}", e)))?
    }
}

fn parse_status(s: &str) -> CheckpointStatus {
    match s {
        "Complete" => CheckpointStatus::Complete,
        "Running" => CheckpointStatus::Running,
        "Yielded" => CheckpointStatus::Yielded,
        "Failed" => CheckpointStatus::Failed,
        "Interrupted" => CheckpointStatus::Interrupted,
        _ => CheckpointStatus::Complete,
    }
}