codetether-agent 4.0.0

A2A-native AI coding agent for the CodeTether ecosystem
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
//! OKR Persistence Layer
//!
//! Provides file-based persistence for OKR entities using Config::data_dir().
//! Supports CRUD operations for ID, run, and status queries.

use crate::okr::{Okr, OkrRun, OkrRunStatus, OkrStatus};
use anyhow::{Context, Result};
use std::path::PathBuf;
use std::sync::Arc;
use tokio::fs;
use tokio::sync::RwLock;
use uuid::Uuid;

/// Repository for OKR persistence operations
pub struct OkrRepository {
    base_path: PathBuf,
    cache: Arc<RwLock<OkrCache>>,
}

/// In-memory cache for OKR data
#[derive(Debug, Default)]
struct OkrCache {
    okrs: Vec<Okr>,
    runs: Vec<OkrRun>,
}

impl OkrRepository {
    /// Create a new OKR repository with the given base path
    pub fn new(base_path: PathBuf) -> Self {
        let repo = Self {
            base_path,
            cache: Arc::new(RwLock::new(OkrCache::default())),
        };
        repo
    }

    /// Create repository from Config::data_dir() or fallback to temp
    pub async fn from_config() -> Result<Self> {
        let base_path = crate::config::Config::data_dir()
            .map(|p| p.join("okr"))
            .unwrap_or_else(|| std::env::temp_dir().join("codetether").join("okr"));

        // Ensure directory exists
        fs::create_dir_all(&base_path)
            .await
            .context("Failed to create OKR data directory")?;

        tracing::info!(path = %base_path.display(), "OKR repository initialized");

        Ok(Self::new(base_path))
    }

    /// Get the file path for an OKR
    fn okr_path(&self, id: Uuid) -> PathBuf {
        self.base_path
            .join("objectives")
            .join(format!("{}.json", id))
    }

    /// Get the file path for an OKR run
    fn run_path(&self, id: Uuid) -> PathBuf {
        self.base_path.join("runs").join(format!("{}.json", id))
    }

    /// Get the directory path for OKRs
    fn okrs_dir(&self) -> PathBuf {
        self.base_path.join("objectives")
    }

    /// Get the directory path for runs
    fn runs_dir(&self) -> PathBuf {
        self.base_path.join("runs")
    }

    // ============ OKR CRUD ============

    /// Create a new OKR
    pub async fn create_okr(&self, okr: Okr) -> Result<Okr> {
        // Validate
        okr.validate()?;

        // Ensure directory exists
        fs::create_dir_all(self.okrs_dir()).await?;

        // Write to file (atomic: tmp + rename)
        let path = self.okr_path(okr.id);
        let tmp_path = path.with_extension("json.tmp");
        let json = serde_json::to_string_pretty(&okr)?;
        fs::write(&tmp_path, &json).await?;
        fs::rename(&tmp_path, &path).await?;

        // Update cache
        let mut cache = self.cache.write().await;
        cache.okrs.push(okr.clone());

        tracing::info!(okr_id = %okr.id, title = %okr.title, "OKR created");
        Ok(okr)
    }

    /// Get an OKR by ID
    pub async fn get_okr(&self, id: Uuid) -> Result<Option<Okr>> {
        // Check cache first
        {
            let cache = self.cache.read().await;
            if let Some(okr) = cache.okrs.iter().find(|o| o.id == id) {
                return Ok(Some(okr.clone()));
            }
        }

        // Load from disk
        let path = self.okr_path(id);
        if !path.exists() {
            return Ok(None);
        }

        let data = fs::read_to_string(&path).await?;
        let okr: Okr = serde_json::from_str(&data).context("Failed to parse OKR")?;

        // Update cache
        let mut cache = self.cache.write().await;
        if !cache.okrs.iter().any(|o| o.id == okr.id) {
            cache.okrs.push(okr.clone());
        }

        Ok(Some(okr))
    }

    /// Update an existing OKR
    pub async fn update_okr(&self, mut okr: Okr) -> Result<Okr> {
        okr.updated_at = chrono::Utc::now();

        // Validate
        okr.validate()?;

        // Write to file
        let path = self.okr_path(okr.id);
        fs::create_dir_all(self.okrs_dir()).await?;
        let json = serde_json::to_string_pretty(&okr)?;
        fs::write(&path, &json).await?;

        // Update cache
        let mut cache = self.cache.write().await;
        if let Some(existing) = cache.okrs.iter_mut().find(|o| o.id == okr.id) {
            *existing = okr.clone();
        }

        tracing::info!(okr_id = %okr.id, "OKR updated");
        Ok(okr)
    }

    /// Delete an OKR by ID
    pub async fn delete_okr(&self, id: Uuid) -> Result<bool> {
        let path = self.okr_path(id);

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

        fs::remove_file(&path).await?;

        // Remove from cache
        let mut cache = self.cache.write().await;
        cache.okrs.retain(|o| o.id != id);

        tracing::info!(okr_id = %id, "OKR deleted");
        Ok(true)
    }

    /// List all OKRs
    pub async fn list_okrs(&self) -> Result<Vec<Okr>> {
        // Check cache first
        {
            let cache = self.cache.read().await;
            if !cache.okrs.is_empty() {
                return Ok(cache.okrs.clone());
            }
        }

        // Load from disk
        let dir = self.okrs_dir();
        if !dir.exists() {
            return Ok(Vec::new());
        }

        let mut okrs = Vec::new();
        let mut entries = fs::read_dir(&dir).await?;

        while let Some(entry) = entries.next_entry().await? {
            let path = entry.path();
            if path.extension().map_or(false, |e| e == "json") {
                if let Ok(data) = fs::read_to_string(&path).await {
                    if let Ok(okr) = serde_json::from_str::<Okr>(&data) {
                        okrs.push(okr);
                    }
                }
            }
        }

        // Update cache
        let mut cache = self.cache.write().await;
        cache.okrs = okrs.clone();

        Ok(okrs)
    }

    /// Query OKRs by status
    pub async fn query_okrs_by_status(&self, status: OkrStatus) -> Result<Vec<Okr>> {
        let all = self.list_okrs().await?;
        Ok(all.into_iter().filter(|o| o.status == status).collect())
    }

    /// Query OKRs by owner
    pub async fn query_okrs_by_owner(&self, owner: &str) -> Result<Vec<Okr>> {
        let all = self.list_okrs().await?;
        Ok(all
            .into_iter()
            .filter(|o| o.owner.as_deref() == Some(owner))
            .collect())
    }

    /// Query OKRs by tenant
    pub async fn query_okrs_by_tenant(&self, tenant_id: &str) -> Result<Vec<Okr>> {
        let all = self.list_okrs().await?;
        Ok(all
            .into_iter()
            .filter(|o| o.tenant_id.as_deref() == Some(tenant_id))
            .collect())
    }

    // ============ OKR Run CRUD ============

    /// Create a new OKR run
    pub async fn create_run(&self, run: OkrRun) -> Result<OkrRun> {
        // Validate
        run.validate()?;

        // Ensure directory exists
        fs::create_dir_all(self.runs_dir()).await?;

        // Write to file
        let path = self.run_path(run.id);
        let json = serde_json::to_string_pretty(&run)?;
        fs::write(&path, &json).await?;

        // Update cache
        let mut cache = self.cache.write().await;
        cache.runs.push(run.clone());

        tracing::info!(run_id = %run.id, okr_id = %run.okr_id, name = %run.name, "OKR run created");
        Ok(run)
    }

    /// Get a run by ID
    pub async fn get_run(&self, id: Uuid) -> Result<Option<OkrRun>> {
        // Check cache
        {
            let cache = self.cache.read().await;
            if let Some(run) = cache.runs.iter().find(|r| r.id == id) {
                return Ok(Some(run.clone()));
            }
        }

        // Load from disk
        let path = self.run_path(id);
        if !path.exists() {
            return Ok(None);
        }

        let data = fs::read_to_string(&path).await?;
        let run: OkrRun = serde_json::from_str(&data).context("Failed to parse OKR run")?;

        // Update cache
        let mut cache = self.cache.write().await;
        if !cache.runs.iter().any(|r| r.id == run.id) {
            cache.runs.push(run.clone());
        }

        Ok(Some(run))
    }

    /// Update a run
    pub async fn update_run(&self, mut run: OkrRun) -> Result<OkrRun> {
        run.updated_at = chrono::Utc::now();

        // Write to file
        let path = self.run_path(run.id);
        fs::create_dir_all(self.runs_dir()).await?;
        let json = serde_json::to_string_pretty(&run)?;
        fs::write(&path, &json).await?;

        // Update cache
        let mut cache = self.cache.write().await;
        if let Some(existing) = cache.runs.iter_mut().find(|r| r.id == run.id) {
            *existing = run.clone();
        }

        tracing::info!(run_id = %run.id, "OKR run updated");
        Ok(run)
    }

    /// Delete a run
    pub async fn delete_run(&self, id: Uuid) -> Result<bool> {
        let path = self.run_path(id);

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

        fs::remove_file(&path).await?;

        // Remove from cache
        let mut cache = self.cache.write().await;
        cache.runs.retain(|r| r.id != id);

        tracing::info!(run_id = %id, "OKR run deleted");
        Ok(true)
    }

    /// List all runs
    pub async fn list_runs(&self) -> Result<Vec<OkrRun>> {
        // Check cache
        {
            let cache = self.cache.read().await;
            if !cache.runs.is_empty() {
                return Ok(cache.runs.clone());
            }
        }

        // Load from disk
        let dir = self.runs_dir();
        if !dir.exists() {
            return Ok(Vec::new());
        }

        let mut runs = Vec::new();
        let mut entries = fs::read_dir(&dir).await?;

        while let Some(entry) = entries.next_entry().await? {
            let path = entry.path();
            if path.extension().map_or(false, |e| e == "json") {
                if let Ok(data) = fs::read_to_string(&path).await {
                    if let Ok(run) = serde_json::from_str::<OkrRun>(&data) {
                        runs.push(run);
                    }
                }
            }
        }

        // Update cache
        let mut cache = self.cache.write().await;
        cache.runs = runs.clone();

        Ok(runs)
    }

    /// Query runs by OKR ID
    pub async fn query_runs_by_okr(&self, okr_id: Uuid) -> Result<Vec<OkrRun>> {
        let all = self.list_runs().await?;
        Ok(all.into_iter().filter(|r| r.okr_id == okr_id).collect())
    }

    /// Query runs by status
    pub async fn query_runs_by_status(&self, status: OkrRunStatus) -> Result<Vec<OkrRun>> {
        let all = self.list_runs().await?;
        Ok(all.into_iter().filter(|r| r.status == status).collect())
    }

    /// Query runs by correlation ID
    pub async fn query_runs_by_correlation(&self, correlation_id: &str) -> Result<Vec<OkrRun>> {
        let all = self.list_runs().await?;
        Ok(all
            .into_iter()
            .filter(|r| r.correlation_id.as_deref() == Some(correlation_id))
            .collect())
    }

    /// Query runs by relay checkpoint ID
    pub async fn query_runs_by_checkpoint(&self, checkpoint_id: &str) -> Result<Vec<OkrRun>> {
        let all = self.list_runs().await?;
        Ok(all
            .into_iter()
            .filter(|r| r.relay_checkpoint_id.as_deref() == Some(checkpoint_id))
            .collect())
    }

    /// Query runs by session ID
    pub async fn query_runs_by_session(&self, session_id: &str) -> Result<Vec<OkrRun>> {
        let all = self.list_runs().await?;
        Ok(all
            .into_iter()
            .filter(|r| r.session_id.as_deref() == Some(session_id))
            .collect())
    }

    // ============ Utility Methods ============

    /// Clear the in-memory cache (useful for forcing reload from disk)
    pub async fn clear_cache(&self) {
        let mut cache = self.cache.write().await;
        cache.okrs.clear();
        cache.runs.clear();
        tracing::debug!("OKR repository cache cleared");
    }

    /// Get statistics about the repository
    pub async fn stats(&self) -> Result<OkrRepositoryStats> {
        let okrs = self.list_okrs().await?;
        let runs = self.list_runs().await?;

        let okr_status_counts = okrs
            .iter()
            .fold(std::collections::HashMap::new(), |mut acc, o| {
                *acc.entry(o.status).or_insert(0) += 1;
                acc
            });

        let run_status_counts = runs
            .iter()
            .fold(std::collections::HashMap::new(), |mut acc, r| {
                *acc.entry(r.status).or_insert(0) += 1;
                acc
            });

        Ok(OkrRepositoryStats {
            total_okrs: okrs.len(),
            total_runs: runs.len(),
            okr_status_counts,
            run_status_counts,
        })
    }
}

/// Statistics about the OKR repository
#[derive(Debug, serde::Serialize)]
pub struct OkrRepositoryStats {
    pub total_okrs: usize,
    pub total_runs: usize,
    pub okr_status_counts: std::collections::HashMap<OkrStatus, usize>,
    pub run_status_counts: std::collections::HashMap<OkrRunStatus, usize>,
}

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

    fn temp_repo() -> OkrRepository {
        let temp_dir = std::env::temp_dir().join(format!("okr_test_{}", Uuid::new_v4()));
        OkrRepository::new(temp_dir)
    }

    #[tokio::test]
    async fn test_create_and_get_okr() {
        let repo = temp_repo();

        let mut okr = Okr::new("Test Objective", "Description");
        let kr = KeyResult::new(okr.id, "KR1", 100.0, "%");
        okr.add_key_result(kr);

        let created = repo.create_okr(okr).await.unwrap();

        let fetched = repo.get_okr(created.id).await.unwrap();
        assert!(fetched.is_some());
        assert_eq!(fetched.unwrap().title, "Test Objective");
    }

    #[tokio::test]
    async fn test_update_okr() {
        let repo = temp_repo();

        let mut okr = Okr::new("Test", "Desc");
        let kr = KeyResult::new(okr.id, "KR1", 100.0, "%");
        okr.add_key_result(kr);

        let created = repo.create_okr(okr).await.unwrap();

        let mut to_update = created.clone();
        to_update.title = "Updated Title".to_string();
        to_update.status = OkrStatus::Active;

        let updated = repo.update_okr(to_update).await.unwrap();
        assert_eq!(updated.title, "Updated Title");
        assert_eq!(updated.status, OkrStatus::Active);
    }

    #[tokio::test]
    async fn test_delete_okr() {
        let repo = temp_repo();

        let mut okr = Okr::new("Test", "Desc");
        let kr = KeyResult::new(okr.id, "KR1", 100.0, "%");
        okr.add_key_result(kr);

        let created = repo.create_okr(okr).await.unwrap();
        let deleted = repo.delete_okr(created.id).await.unwrap();
        assert!(deleted);

        let fetched = repo.get_okr(created.id).await.unwrap();
        assert!(fetched.is_none());
    }

    #[tokio::test]
    async fn test_query_okrs_by_status() {
        let repo = temp_repo();

        let mut okr1 = Okr::new("Active OKR", "Desc");
        let kr1 = KeyResult::new(okr1.id, "KR1", 100.0, "%");
        okr1.add_key_result(kr1);
        okr1.status = OkrStatus::Active;

        let mut okr2 = Okr::new("Draft OKR", "Desc");
        let kr2 = KeyResult::new(okr2.id, "KR2", 100.0, "%");
        okr2.add_key_result(kr2);
        okr2.status = OkrStatus::Draft;

        repo.create_okr(okr1).await.unwrap();
        repo.create_okr(okr2).await.unwrap();

        let active = repo.query_okrs_by_status(OkrStatus::Active).await.unwrap();
        assert_eq!(active.len(), 1);
        assert_eq!(active[0].title, "Active OKR");
    }

    #[tokio::test]
    async fn test_crud_runs() {
        let repo = temp_repo();

        // First create an OKR
        let mut okr = Okr::new("Test OKR", "Desc");
        let okr_id = okr.id;
        let kr = KeyResult::new(okr.id, "KR1", 100.0, "%");
        okr.add_key_result(kr);
        repo.create_okr(okr).await.unwrap();

        // Create a run
        let mut run = OkrRun::new(okr_id, "Q1 Run");
        run.correlation_id = Some("corr-123".to_string());
        run.session_id = Some("session-456".to_string());

        let created = repo.create_run(run).await.unwrap();

        // Fetch by ID
        let fetched = repo.get_run(created.id).await.unwrap();
        assert!(fetched.is_some());
        assert_eq!(fetched.unwrap().name, "Q1 Run");

        // Query by OKR
        let runs_for_okr = repo.query_runs_by_okr(okr_id).await.unwrap();
        assert_eq!(runs_for_okr.len(), 1);

        // Query by correlation
        let runs_by_corr = repo.query_runs_by_correlation("corr-123").await.unwrap();
        assert_eq!(runs_by_corr.len(), 1);

        // Query by status
        let pending = repo
            .query_runs_by_status(OkrRunStatus::Draft)
            .await
            .unwrap();
        assert_eq!(pending.len(), 1);
    }

    #[tokio::test]
    async fn test_list_and_stats() {
        let repo = temp_repo();

        // Create OKRs
        let mut okr = Okr::new("Test", "Desc");
        let kr = KeyResult::new(okr.id, "KR", 100.0, "%");
        okr.add_key_result(kr);
        repo.create_okr(okr).await.unwrap();

        // List
        let all_okrs = repo.list_okrs().await.unwrap();
        assert_eq!(all_okrs.len(), 1);

        let all_runs = repo.list_runs().await.unwrap();
        assert_eq!(all_runs.len(), 0);

        // Stats
        let stats = repo.stats().await.unwrap();
        assert_eq!(stats.total_okrs, 1);
    }
}