atheneum 0.3.1

Agent coordination graph database - episodic and semantic memory for multi-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
use anyhow::Result;
use chrono::Utc;
use serde_json::{json, Value};
use sqlitegraph::GraphEntity;

use super::{AppliedKanbanUpdate, AtheneumGraph, BlockerType, RequirementStatus, TaskDetail};

#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
pub enum KanbanStatus {
    Todo,
    InProgress,
    Done,
    Blocked,
    Archived,
}

impl KanbanStatus {
    pub fn as_str(&self) -> &'static str {
        match self {
            KanbanStatus::Todo => "TODO",
            KanbanStatus::InProgress => "IN_PROGRESS",
            KanbanStatus::Done => "DONE",
            KanbanStatus::Blocked => "BLOCKED",
            KanbanStatus::Archived => "ARCHIVED",
        }
    }

    pub fn parse(s: &str) -> Option<Self> {
        match s.to_ascii_uppercase().as_str() {
            "TODO" => Some(KanbanStatus::Todo),
            "IN_PROGRESS" | "IN-PROGRESS" | "INPROGRESS" | "IN PROGRESS" => {
                Some(KanbanStatus::InProgress)
            }
            "DONE" => Some(KanbanStatus::Done),
            "BLOCKED" => Some(KanbanStatus::Blocked),
            "ARCHIVED" | "ARCHIVE" => Some(KanbanStatus::Archived),
            _ => None,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
pub struct KanbanUpdate {
    pub task_title: String,
    pub new_status: KanbanStatus,
}

impl AtheneumGraph {
    pub fn create_task(
        &self,
        title: &str,
        description: Option<&str>,
        project_id: Option<&str>,
    ) -> Result<i64> {
        let created_at = Utc::now().to_rfc3339();

        let title_s = title.to_string();
        let description_s = description.map(|s| s.to_string());
        let project_s = project_id.map(|s| s.to_string());
        let created_at_s = created_at.clone();
        let sql_id = self.with_raw_connection(|conn| {
            conn.execute(
                "INSERT INTO tasks
                    (title, description, status, project_id, metadata, created_at)
                 VALUES (?1, ?2, 'TODO', ?3, '{}', ?4)",
                rusqlite::params![title_s, description_s, project_s, created_at_s],
            )?;
            Ok(conn.last_insert_rowid())
        })?;

        let mut data = json!({
            "sql_id": sql_id,
            "title": title,
            "description": description,
            "status": KanbanStatus::Todo.as_str(),
            "created_at": created_at,
        });
        if let (Some(pid), Some(obj)) = (project_id, data.as_object_mut()) {
            obj.insert("project_id".to_string(), Value::String(pid.to_string()));
        }
        let entity = GraphEntity {
            id: 0,
            kind: "Task".to_string(),
            name: title.to_string(),
            file_path: None,
            data,
        };
        let task_id = self
            .inner
            .insert_entity(&entity)
            .map_err(|e| anyhow::anyhow!("Failed to insert Task: {}", e))?;

        let indexed = GraphEntity {
            id: task_id,
            ..entity
        };
        if let Err(e) = self.add_entity_to_search_index(&indexed) {
            eprintln!("[atheneum] task auto-index warning: {}", e);
        }

        Ok(task_id)
    }

    pub fn update_task_status(&self, task_id: i64, status: KanbanStatus) -> Result<()> {
        let entity = self.get_entity(task_id)?;
        let sql_id = entity.data.get("sql_id").and_then(|v| v.as_i64());

        let now = Utc::now().to_rfc3339();

        if let Some(sql_id) = sql_id {
            let status_str = status.as_str().to_string();
            let ts = now.clone();
            self.with_raw_connection(|conn| {
                conn.execute(
                    "UPDATE tasks SET status = ?1, status_updated_at = ?2 WHERE id = ?3",
                    rusqlite::params![status_str, ts, sql_id],
                )?;
                Ok(())
            })?;
        }

        let mut data = entity.data;
        if let Some(obj) = data.as_object_mut() {
            obj.insert(
                "status".to_string(),
                Value::String(status.as_str().to_string()),
            );
            obj.insert("status_updated_at".to_string(), Value::String(now));
        }
        self.update_entity_data(task_id, &data)
    }

    pub fn find_task_by_title(&self, title: &str, project_id: Option<&str>) -> Result<Option<i64>> {
        let tasks = self.entities_by_kind("Task")?;
        Ok(tasks
            .into_iter()
            .find(|t| {
                let title_matches = t.data.get("title").and_then(|v| v.as_str()) == Some(title);
                let project_matches = match project_id {
                    None => true,
                    Some(pid) => t.data.get("project_id").and_then(|v| v.as_str()) == Some(pid),
                };
                title_matches && project_matches
            })
            .map(|t| t.id))
    }

    pub fn list_tasks_by_status(
        &self,
        status: KanbanStatus,
        project_id: Option<&str>,
    ) -> Result<Vec<GraphEntity>> {
        let want = status.as_str();
        Ok(self
            .entities_by_kind("Task")?
            .into_iter()
            .filter(|t| t.data.get("status").and_then(|v| v.as_str()) == Some(want))
            .filter(|t| match project_id {
                None => true,
                Some(pid) => t.data.get("project_id").and_then(|v| v.as_str()) == Some(pid),
            })
            .collect())
    }

    pub fn list_tasks(&self, project_id: Option<&str>) -> Result<Vec<GraphEntity>> {
        Ok(self
            .entities_by_kind("Task")?
            .into_iter()
            .filter(|t| t.data.get("status").and_then(|v| v.as_str()) != Some("ARCHIVED"))
            .filter(|t| match project_id {
                None => true,
                Some(pid) => t.data.get("project_id").and_then(|v| v.as_str()) == Some(pid),
            })
            .collect())
    }

    pub fn add_requirement(
        &self,
        task_id: i64,
        statement: &str,
        verification_method: Option<&str>,
    ) -> Result<i64> {
        let task_entity = self.get_entity(task_id)?;
        let task_sql_id = task_entity
            .data
            .get("sql_id")
            .and_then(|v| v.as_i64())
            .ok_or_else(|| {
                anyhow::anyhow!(
                    "Task graph_entity {} has no sql_id pointer; migration may not have run",
                    task_id
                )
            })?;

        let created_at = Utc::now().to_rfc3339();
        let statement_s = statement.to_string();
        let verification_s = verification_method.map(|s| s.to_string());
        let created_at_s = created_at.clone();
        let sql_id = self.with_raw_connection(|conn| {
            conn.execute(
                "INSERT INTO requirements
                    (task_id, statement, status, verification_method, created_at)
                 VALUES (?1, ?2, 'UNMET', ?3, ?4)",
                rusqlite::params![task_sql_id, statement_s, verification_s, created_at_s],
            )?;
            Ok(conn.last_insert_rowid())
        })?;

        let data = json!({
            "sql_id": sql_id,
            "task_id": task_id,
            "statement": statement,
            "status": RequirementStatus::Unmet.as_str(),
            "verification_method": verification_method,
            "created_at": created_at,
        });
        let entity = GraphEntity {
            id: 0,
            kind: "Requirement".to_string(),
            name: statement.to_string(),
            file_path: None,
            data,
        };
        self.inner
            .insert_entity(&entity)
            .map_err(|e| anyhow::anyhow!("Failed to insert Requirement: {}", e))
    }

    pub fn mark_requirement_met(&self, req_id: i64) -> Result<()> {
        let entity = self.get_entity(req_id)?;
        let sql_id = entity.data.get("sql_id").and_then(|v| v.as_i64());
        let now = Utc::now().to_rfc3339();

        if let Some(sql_id) = sql_id {
            let ts = now.clone();
            self.with_raw_connection(|conn| {
                conn.execute(
                    "UPDATE requirements SET status = 'MET', met_at = ?1 WHERE id = ?2",
                    rusqlite::params![ts, sql_id],
                )?;
                Ok(())
            })?;
        }

        let mut data = entity.data;
        if let Some(obj) = data.as_object_mut() {
            obj.insert(
                "status".to_string(),
                Value::String(RequirementStatus::Met.as_str().to_string()),
            );
            obj.insert("met_at".to_string(), Value::String(now));
        }
        self.update_entity_data(req_id, &data)
    }

    pub fn add_blocker(
        &self,
        task_id: i64,
        description: &str,
        blocker_type: BlockerType,
    ) -> Result<i64> {
        let task_entity = self.get_entity(task_id)?;
        let task_sql_id = task_entity
            .data
            .get("sql_id")
            .and_then(|v| v.as_i64())
            .ok_or_else(|| {
                anyhow::anyhow!(
                    "Task graph_entity {} has no sql_id pointer; migration may not have run",
                    task_id
                )
            })?;

        let created_at = Utc::now().to_rfc3339();
        let description_s = description.to_string();
        let blocker_kind = blocker_type.as_str().to_string();
        let created_at_s = created_at.clone();
        let sql_id = self.with_raw_connection(|conn| {
            conn.execute(
                "INSERT INTO blockers
                    (task_id, description, blocker_type, resolved_at, created_at)
                 VALUES (?1, ?2, ?3, NULL, ?4)",
                rusqlite::params![task_sql_id, description_s, blocker_kind, created_at_s],
            )?;
            Ok(conn.last_insert_rowid())
        })?;

        let data = json!({
            "sql_id": sql_id,
            "task_id": task_id,
            "description": description,
            "blocker_type": blocker_type.as_str(),
            "resolved_at": Value::Null,
            "created_at": created_at,
        });
        let entity = GraphEntity {
            id: 0,
            kind: "Blocker".to_string(),
            name: description.to_string(),
            file_path: None,
            data,
        };
        self.inner
            .insert_entity(&entity)
            .map_err(|e| anyhow::anyhow!("Failed to insert Blocker: {}", e))
    }

    pub fn resolve_blocker(&self, blocker_id: i64) -> Result<()> {
        let entity = self.get_entity(blocker_id)?;
        let sql_id = entity.data.get("sql_id").and_then(|v| v.as_i64());
        let now = Utc::now().to_rfc3339();

        if let Some(sql_id) = sql_id {
            let ts = now.clone();
            self.with_raw_connection(|conn| {
                conn.execute(
                    "UPDATE blockers SET resolved_at = ?1 WHERE id = ?2",
                    rusqlite::params![ts, sql_id],
                )?;
                Ok(())
            })?;
        }

        let mut data = entity.data;
        if let Some(obj) = data.as_object_mut() {
            obj.insert("resolved_at".to_string(), Value::String(now));
        }
        self.update_entity_data(blocker_id, &data)
    }

    pub fn get_task_with_details(&self, task_id: i64) -> Result<TaskDetail> {
        let task = self.get_entity(task_id)?;
        let requirements = self
            .entities_by_kind("Requirement")?
            .into_iter()
            .filter(|r| r.data.get("task_id").and_then(|v| v.as_i64()) == Some(task_id))
            .collect();
        let blockers = self
            .entities_by_kind("Blocker")?
            .into_iter()
            .filter(|b| b.data.get("task_id").and_then(|v| v.as_i64()) == Some(task_id))
            .collect();
        Ok(TaskDetail {
            task,
            requirements,
            blockers,
        })
    }

    pub fn apply_kanban_updates_from_journal(
        &self,
        journal_section_id: i64,
    ) -> Result<Vec<AppliedKanbanUpdate>> {
        let section = self.get_entity(journal_section_id)?;
        let project_id = section.data.get("project_id").and_then(|v| v.as_str());
        let updates = section
            .data
            .get("kanban_updates")
            .and_then(|v| v.as_array())
            .cloned()
            .unwrap_or_default();

        let mut applied = Vec::new();
        for update in updates {
            let Some(title) = update.get("task_title").and_then(|v| v.as_str()) else {
                continue;
            };
            let Some(status_str) = update.get("new_status").and_then(|v| v.as_str()) else {
                continue;
            };
            let Some(new_status) = KanbanStatus::parse(status_str) else {
                continue;
            };
            let Some(task_id) = self.find_task_by_title(title, project_id)? else {
                continue;
            };

            let task = self.get_entity(task_id)?;
            let previous_status = task
                .data
                .get("status")
                .and_then(|v| v.as_str())
                .and_then(KanbanStatus::parse)
                .unwrap_or(KanbanStatus::Todo);

            self.update_task_status(task_id, new_status)?;
            applied.push(AppliedKanbanUpdate {
                task_id,
                task_title: title.to_string(),
                previous_status,
                new_status,
            });
        }
        Ok(applied)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn kanban_status_parse_all_variants() {
        assert_eq!(KanbanStatus::parse("TODO"), Some(KanbanStatus::Todo));
        assert_eq!(KanbanStatus::parse("todo"), Some(KanbanStatus::Todo));
        assert_eq!(
            KanbanStatus::parse("IN_PROGRESS"),
            Some(KanbanStatus::InProgress)
        );
        assert_eq!(
            KanbanStatus::parse("IN-PROGRESS"),
            Some(KanbanStatus::InProgress)
        );
        assert_eq!(
            KanbanStatus::parse("INPROGRESS"),
            Some(KanbanStatus::InProgress)
        );
        assert_eq!(
            KanbanStatus::parse("IN PROGRESS"),
            Some(KanbanStatus::InProgress)
        );
        assert_eq!(
            KanbanStatus::parse("in progress"),
            Some(KanbanStatus::InProgress)
        );
        assert_eq!(KanbanStatus::parse("DONE"), Some(KanbanStatus::Done));
        assert_eq!(KanbanStatus::parse("BLOCKED"), Some(KanbanStatus::Blocked));
        assert_eq!(KanbanStatus::parse("UNKNOWN"), None);
        assert_eq!(KanbanStatus::parse(""), None);
    }
}