datasphere 0.2.0

Background daemon that distills knowledge from Claude Code sessions into a searchable graph
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
//! Persistent job queue for daemon processing
//!
//! AIDEV-NOTE: Append-only JSONL file at ~/.datasphere/queue.jsonl
//! Latest entry per source_id wins (allows status updates without rewriting).
//! Supports pending → processing → done/failed transitions.

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs::{File, OpenOptions};
use std::io::{BufRead, BufReader, Write};
use std::path::PathBuf;

/// Job status in the queue
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum JobStatus {
    Pending,
    Processing,
    Done,
    Failed,
}

/// A job in the queue
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Job {
    /// Unique identifier (session UUID or file path)
    pub source_id: String,
    /// Type of source
    pub source_type: String,
    /// Project ID (directory name in ~/.claude/projects/)
    pub project_id: String,
    /// Path to transcript file
    pub transcript_path: String,
    /// When the job was queued
    pub queued_at: DateTime<Utc>,
    /// Current status
    pub status: JobStatus,
    /// Error message if failed
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error: Option<String>,
}

/// Persistent job queue backed by JSONL file
pub struct Queue {
    path: PathBuf,
}

impl Queue {
    /// Open or create queue at the default location (~/.datasphere/queue.jsonl)
    pub fn open_default() -> Result<Self, String> {
        let ds_dir = dirs::home_dir()
            .ok_or("Could not determine home directory")?
            .join(".datasphere");

        std::fs::create_dir_all(&ds_dir)
            .map_err(|e| format!("Failed to create datasphere directory: {}", e))?;

        let path = ds_dir.join("queue.jsonl");
        Ok(Self { path })
    }

    /// Add a job to the queue (deduplicates by source_id)
    /// Returns Ok(true) if job was added, Ok(false) if skipped (already pending/processing)
    pub fn add(&self, job: Job) -> Result<bool, String> {
        // Check if already pending/processing
        let jobs = self.load_all()?;
        if let Some(existing) = jobs.get(&job.source_id) {
            if existing.status == JobStatus::Pending || existing.status == JobStatus::Processing {
                // Already queued, skip
                return Ok(false);
            }
        }

        self.append(&job)?;
        Ok(true)
    }

    /// Get oldest pending job and mark it as processing
    pub fn pop_pending(&self) -> Result<Option<Job>, String> {
        let jobs = self.load_all()?;

        // Find oldest pending job
        let mut pending: Vec<_> = jobs
            .into_values()
            .filter(|j| j.status == JobStatus::Pending)
            .collect();

        pending.sort_by(|a, b| a.queued_at.cmp(&b.queued_at));

        if let Some(mut job) = pending.into_iter().next() {
            job.status = JobStatus::Processing;
            self.append(&job)?;
            Ok(Some(job))
        } else {
            Ok(None)
        }
    }

    /// Mark a job as done
    pub fn mark_done(&self, source_id: &str) -> Result<(), String> {
        let jobs = self.load_all()?;
        if let Some(mut job) = jobs.get(source_id).cloned() {
            job.status = JobStatus::Done;
            job.error = None;
            self.append(&job)?;
        }
        Ok(())
    }

    /// Mark a job as failed with error message
    pub fn mark_failed(&self, source_id: &str, error: &str) -> Result<(), String> {
        let jobs = self.load_all()?;
        if let Some(mut job) = jobs.get(source_id).cloned() {
            job.status = JobStatus::Failed;
            job.error = Some(error.to_string());
            self.append(&job)?;
        }
        Ok(())
    }

    /// Mark a job as pending (for returning processing jobs to the queue)
    pub fn mark_pending(&self, source_id: &str) -> Result<(), String> {
        let jobs = self.load_all()?;
        if let Some(mut job) = jobs.get(source_id).cloned() {
            job.status = JobStatus::Pending;
            job.error = None;
            self.append(&job)?;
        }
        Ok(())
    }

    /// List all pending jobs
    pub fn list_pending(&self) -> Result<Vec<Job>, String> {
        let jobs = self.load_all()?;
        let mut pending: Vec<_> = jobs
            .into_values()
            .filter(|j| j.status == JobStatus::Pending)
            .collect();
        pending.sort_by(|a, b| a.queued_at.cmp(&b.queued_at));
        Ok(pending)
    }

    /// List all processing jobs
    pub fn list_processing(&self) -> Result<Vec<Job>, String> {
        let jobs = self.load_all()?;
        Ok(jobs
            .into_values()
            .filter(|j| j.status == JobStatus::Processing)
            .collect())
    }

    /// Count jobs by status
    pub fn counts(&self) -> Result<(usize, usize, usize, usize), String> {
        let jobs = self.load_all()?;
        let pending = jobs.values().filter(|j| j.status == JobStatus::Pending).count();
        let processing = jobs.values().filter(|j| j.status == JobStatus::Processing).count();
        let done = jobs.values().filter(|j| j.status == JobStatus::Done).count();
        let failed = jobs.values().filter(|j| j.status == JobStatus::Failed).count();
        Ok((pending, processing, done, failed))
    }

    /// Clear completed (done) jobs by compacting the file
    pub fn clear_done(&self) -> Result<usize, String> {
        let jobs = self.load_all()?;
        let done_count = jobs.values().filter(|j| j.status == JobStatus::Done).count();

        // Rewrite file without done jobs
        let remaining: Vec<_> = jobs
            .into_values()
            .filter(|j| j.status != JobStatus::Done)
            .collect();

        self.rewrite(&remaining)?;
        Ok(done_count)
    }

    /// Delete entire queue (all jobs, all statuses)
    pub fn nuke(&self) -> Result<usize, String> {
        let jobs = self.load_all()?;
        let total = jobs.len();

        // Truncate file to empty
        File::create(&self.path)
            .map_err(|e| format!("Failed to nuke queue: {}", e))?;

        Ok(total)
    }

    /// Retry all failed jobs (moves them from Failed → Pending)
    /// Returns number of jobs requeued
    pub fn retry_all(&self) -> Result<usize, String> {
        let jobs = self.load_all()?;
        let mut count = 0;

        for (_, mut job) in jobs.into_iter() {
            if job.status == JobStatus::Failed {
                job.status = JobStatus::Pending;
                job.error = None;
                job.queued_at = Utc::now();
                self.append(&job)?;
                count += 1;
            }
        }

        Ok(count)
    }

    /// Retry a specific failed job by source_id
    /// Returns Ok(true) if job was requeued, Ok(false) if not found or not failed
    pub fn retry_one(&self, source_id: &str) -> Result<bool, String> {
        let jobs = self.load_all()?;

        if let Some(mut job) = jobs.get(source_id).cloned() {
            if job.status == JobStatus::Failed {
                job.status = JobStatus::Pending;
                job.error = None;
                job.queued_at = Utc::now();
                self.append(&job)?;
                return Ok(true);
            }
        }

        Ok(false)
    }

    /// List all failed jobs
    pub fn list_failed(&self) -> Result<Vec<Job>, String> {
        let jobs = self.load_all()?;
        let mut failed: Vec<_> = jobs
            .into_values()
            .filter(|j| j.status == JobStatus::Failed)
            .collect();
        failed.sort_by(|a, b| a.queued_at.cmp(&b.queued_at));
        Ok(failed)
    }

    /// Load all jobs, deduplicating by source_id (latest wins)
    fn load_all(&self) -> Result<HashMap<String, Job>, String> {
        let mut jobs = HashMap::new();

        if !self.path.exists() {
            return Ok(jobs);
        }

        let file = File::open(&self.path)
            .map_err(|e| format!("Failed to open queue file: {}", e))?;

        for line in BufReader::new(file).lines() {
            let line = line.map_err(|e| format!("Failed to read line: {}", e))?;
            if line.trim().is_empty() {
                continue;
            }

            match serde_json::from_str::<Job>(&line) {
                Ok(job) => {
                    jobs.insert(job.source_id.clone(), job);
                }
                Err(e) => {
                    eprintln!("Warning: skipping malformed queue entry: {}", e);
                }
            }
        }

        Ok(jobs)
    }

    /// Append a job entry to the file
    fn append(&self, job: &Job) -> Result<(), String> {
        let mut file = OpenOptions::new()
            .create(true)
            .append(true)
            .open(&self.path)
            .map_err(|e| format!("Failed to open queue file for append: {}", e))?;

        let line = serde_json::to_string(job)
            .map_err(|e| format!("Failed to serialize job: {}", e))?;

        writeln!(file, "{}", line)
            .map_err(|e| format!("Failed to write to queue file: {}", e))?;

        Ok(())
    }

    /// Rewrite the entire file with the given jobs
    fn rewrite(&self, jobs: &[Job]) -> Result<(), String> {
        let mut file = File::create(&self.path)
            .map_err(|e| format!("Failed to create queue file: {}", e))?;

        for job in jobs {
            let line = serde_json::to_string(job)
                .map_err(|e| format!("Failed to serialize job: {}", e))?;
            writeln!(file, "{}", line)
                .map_err(|e| format!("Failed to write to queue file: {}", e))?;
        }

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::{tempdir, TempDir};

    /// Returns (queue, _tempdir) - must keep _tempdir alive for test duration
    fn test_queue() -> (Queue, TempDir) {
        let dir = tempdir().unwrap();
        let queue = Queue {
            path: dir.path().join("queue.jsonl"),
        };
        (queue, dir)
    }

    #[test]
    fn test_add_and_pop() {
        let (queue, _dir) = test_queue();

        let job = Job {
            source_id: "test-123".to_string(),
            source_type: "session".to_string(),
            project_id: "-test-project".to_string(),
            transcript_path: "/path/to/test.jsonl".to_string(),
            queued_at: Utc::now(),
            status: JobStatus::Pending,
            error: None,
        };

        queue.add(job.clone()).unwrap();

        let popped = queue.pop_pending().unwrap();
        assert!(popped.is_some());
        let popped = popped.unwrap();
        assert_eq!(popped.source_id, "test-123");
        assert_eq!(popped.status, JobStatus::Processing);

        // Should be no more pending
        let next = queue.pop_pending().unwrap();
        assert!(next.is_none());
    }

    #[test]
    fn test_deduplication() {
        let (queue, _dir) = test_queue();

        let job = Job {
            source_id: "test-123".to_string(),
            source_type: "session".to_string(),
            project_id: "-test-project".to_string(),
            transcript_path: "/path/to/test.jsonl".to_string(),
            queued_at: Utc::now(),
            status: JobStatus::Pending,
            error: None,
        };

        let added = queue.add(job.clone()).unwrap();
        assert!(added); // First add should succeed

        let added = queue.add(job.clone()).unwrap();
        assert!(!added); // Duplicate should be skipped

        let pending = queue.list_pending().unwrap();
        assert_eq!(pending.len(), 1);
    }

    #[test]
    fn test_mark_pending() {
        let (queue, _dir) = test_queue();

        let job = Job {
            source_id: "test-123".to_string(),
            source_type: "session".to_string(),
            project_id: "-test-project".to_string(),
            transcript_path: "/path/to/test.jsonl".to_string(),
            queued_at: Utc::now(),
            status: JobStatus::Pending,
            error: None,
        };

        queue.add(job.clone()).unwrap();

        // Pop to mark as processing
        let popped = queue.pop_pending().unwrap().unwrap();
        assert_eq!(popped.status, JobStatus::Processing);

        // Verify it's processing
        let processing = queue.list_processing().unwrap();
        assert_eq!(processing.len(), 1);

        // Mark back to pending
        queue.mark_pending("test-123").unwrap();

        // Verify it's pending again
        let pending = queue.list_pending().unwrap();
        assert_eq!(pending.len(), 1);
        let processing = queue.list_processing().unwrap();
        assert_eq!(processing.len(), 0);
    }

    #[test]
    fn test_retry_all() {
        let (queue, _dir) = test_queue();

        // Add two jobs and mark them as failed
        let job1 = Job {
            source_id: "test-1".to_string(),
            source_type: "session".to_string(),
            project_id: "-test-project".to_string(),
            transcript_path: "/path/to/test1.jsonl".to_string(),
            queued_at: Utc::now(),
            status: JobStatus::Pending,
            error: None,
        };
        let job2 = Job {
            source_id: "test-2".to_string(),
            source_type: "session".to_string(),
            project_id: "-test-project".to_string(),
            transcript_path: "/path/to/test2.jsonl".to_string(),
            queued_at: Utc::now(),
            status: JobStatus::Pending,
            error: None,
        };

        queue.add(job1).unwrap();
        queue.add(job2).unwrap();

        // Mark both as failed
        queue.mark_failed("test-1", "error 1").unwrap();
        queue.mark_failed("test-2", "error 2").unwrap();

        // Verify they're failed
        let failed = queue.list_failed().unwrap();
        assert_eq!(failed.len(), 2);
        assert_eq!(queue.list_pending().unwrap().len(), 0);

        // Retry all
        let retried = queue.retry_all().unwrap();
        assert_eq!(retried, 2);

        // Verify they're now pending
        let pending = queue.list_pending().unwrap();
        assert_eq!(pending.len(), 2);
        assert_eq!(queue.list_failed().unwrap().len(), 0);

        // Error should be cleared
        for job in &pending {
            assert!(job.error.is_none());
        }
    }

    #[test]
    fn test_retry_one() {
        let (queue, _dir) = test_queue();

        let job = Job {
            source_id: "test-123".to_string(),
            source_type: "session".to_string(),
            project_id: "-test-project".to_string(),
            transcript_path: "/path/to/test.jsonl".to_string(),
            queued_at: Utc::now(),
            status: JobStatus::Pending,
            error: None,
        };

        queue.add(job).unwrap();
        queue.mark_failed("test-123", "some error").unwrap();

        // Verify it's failed
        assert_eq!(queue.list_failed().unwrap().len(), 1);
        assert_eq!(queue.list_pending().unwrap().len(), 0);

        // Retry non-existent job should return false
        assert!(!queue.retry_one("nonexistent").unwrap());

        // Retry the failed job
        assert!(queue.retry_one("test-123").unwrap());

        // Verify it's now pending
        assert_eq!(queue.list_pending().unwrap().len(), 1);
        assert_eq!(queue.list_failed().unwrap().len(), 0);

        // Retrying again should return false (not failed anymore)
        assert!(!queue.retry_one("test-123").unwrap());
    }
}