terraphim_agent 1.16.30

Terraphim AI Agent CLI - Command-line interface with interactive REPL and ASCII graph visualization
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
//! Procedure storage for captured successful procedures.
//!
//! This module provides persistent storage for CapturedProcedure instances,
//! with Aho-Corasick-based deduplication support.
//!
//! # Example
//!
//! ```
//! use std::path::PathBuf;
//! use terraphim_agent::learnings::procedure::ProcedureStore;
//! use terraphim_types::procedure::{CapturedProcedure, ProcedureStep};
//!
//! # fn example() -> std::io::Result<()> {
//! let store = ProcedureStore::new(PathBuf::from("~/.config/terraphim/learnings/procedures.jsonl"));
//!
//! let mut procedure = CapturedProcedure::new(
//!     "install-rust".to_string(),
//!     "Install Rust".to_string(),
//!     "Install Rust toolchain".to_string(),
//! );
//!
//! procedure.add_step(ProcedureStep {
//!     ordinal: 1,
//!     command: "curl https://sh.rustup.rs | sh".to_string(),
//!     precondition: None,
//!     postcondition: None,
//!     working_dir: None,
//!     privileged: false,
//!     tags: vec![],
//! });
//!
//! store.save(&procedure)?;
//! # Ok(())
//! # }
//! ```

use std::fs::{self, File, OpenOptions};
use std::io::{self, BufRead, BufReader, Write};
use std::path::PathBuf;

use terraphim_automata::matcher::find_matches;
#[cfg(test)]
use terraphim_types::procedure::ProcedureConfidence;
use terraphim_types::{
    NormalizedTerm, NormalizedTermValue, Thesaurus, procedure::CapturedProcedure,
};

/// Storage for captured procedures with deduplication support.
pub struct ProcedureStore {
    /// Path to the JSONL storage file
    store_path: PathBuf,
}

impl ProcedureStore {
    /// Create a new ProcedureStore with the given path.
    ///
    /// The path should be a JSONL file (e.g., `procedures.jsonl`).
    /// Parent directories will be created automatically when saving.
    pub fn new(store_path: PathBuf) -> Self {
        Self { store_path }
    }

    /// Get the default store path in the user's config directory.
    ///
    /// Returns `~/.config/terraphim/learnings/procedures.jsonl` on Unix-like systems,
    /// or the equivalent config directory on other platforms.
    ///
    /// Note: This function is not used internally but is provided as a convenience
    /// for external callers who want a sensible default path.
    #[allow(dead_code)]
    pub fn default_path() -> PathBuf {
        dirs::config_dir()
            .unwrap_or_else(|| PathBuf::from("~/.config"))
            .join("terraphim")
            .join("learnings")
            .join("procedures.jsonl")
    }

    /// Ensure the parent directory exists.
    fn ensure_dir_exists(&self) -> io::Result<()> {
        if let Some(parent) = self.store_path.parent() {
            fs::create_dir_all(parent)?;
        }
        Ok(())
    }

    /// Save a procedure to storage.
    ///
    /// If a procedure with the same ID already exists, it will be updated.
    /// This operation performs deduplication checks before saving.
    pub fn save(&self, procedure: &CapturedProcedure) -> io::Result<()> {
        self.ensure_dir_exists()?;

        // Load existing procedures
        let mut procedures = self.load_all()?;

        // Check for existing procedure with same ID
        let existing_index = procedures.iter().position(|p| p.id == procedure.id);

        if let Some(index) = existing_index {
            // Update existing procedure
            procedures[index] = procedure.clone();
        } else {
            // Add new procedure
            procedures.push(procedure.clone());
        }

        // Write all procedures back to file
        self.write_all(&procedures)
    }

    /// Save a procedure with deduplication check.
    ///
    /// If a similar procedure (matching title via Aho-Corasick) with high confidence
    /// (> 0.8) exists, merge the steps instead of creating a duplicate.
    ///
    /// Returns the saved (or merged) procedure.
    pub fn save_with_dedup(
        &self,
        mut procedure: CapturedProcedure,
    ) -> io::Result<CapturedProcedure> {
        self.ensure_dir_exists()?;

        // Load existing procedures for dedup check
        let existing_procedures = self.load_all()?;

        // Build thesaurus from existing procedure titles for deduplication
        let mut thesaurus = Thesaurus::new("procedure_titles".to_string());
        for (idx, existing) in existing_procedures.iter().enumerate() {
            let normalized_title = existing.title.to_lowercase();
            let term = NormalizedTerm::new(idx as u64, NormalizedTermValue::from(normalized_title));
            thesaurus.insert(
                NormalizedTermValue::from(existing.title.to_lowercase()),
                term,
            );
        }

        // Check for matching titles using Aho-Corasick
        let matches = find_matches(&procedure.title.to_lowercase(), thesaurus, false)
            .map_err(io::Error::other)?;

        let mut merged = false;
        let mut merged_procedure_id = None;

        for matched in matches {
            // Find the matching procedure
            if let Some(existing) = existing_procedures
                .iter()
                .find(|p| p.title.to_lowercase() == matched.term.to_lowercase())
            {
                // Check if it has high confidence
                if existing.confidence.is_high_confidence() {
                    log::info!(
                        "Found similar procedure '{}' with high confidence ({}), merging steps",
                        existing.title,
                        existing.confidence.score
                    );

                    // Merge steps into the new procedure
                    procedure.merge_steps(existing);
                    merged = true;
                    merged_procedure_id = Some(existing.id.clone());
                    break;
                }
            }
        }

        if merged {
            // If we merged with an existing procedure, update the ID to match
            if let Some(existing_id) = merged_procedure_id {
                procedure.id = existing_id;
            }
        }

        // Save the (possibly merged) procedure
        self.save(&procedure)?;

        Ok(procedure)
    }

    /// Load all procedures from storage.
    pub fn load_all(&self) -> io::Result<Vec<CapturedProcedure>> {
        if !self.store_path.exists() {
            return Ok(Vec::new());
        }

        let file = File::open(&self.store_path)?;
        let reader = BufReader::new(file);
        let mut procedures = Vec::new();

        for line in reader.lines() {
            let line = line?;
            if line.trim().is_empty() {
                continue;
            }

            match serde_json::from_str::<CapturedProcedure>(&line) {
                Ok(procedure) => procedures.push(procedure),
                Err(e) => {
                    log::warn!("Failed to parse procedure from JSONL: {}", e);
                    continue;
                }
            }
        }

        Ok(procedures)
    }

    /// Write all procedures to storage (internal helper).
    fn write_all(&self, procedures: &[CapturedProcedure]) -> io::Result<()> {
        let mut file = OpenOptions::new()
            .write(true)
            .create(true)
            .truncate(true)
            .open(&self.store_path)?;

        for procedure in procedures {
            let json = serde_json::to_string(procedure)
                .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
            writeln!(file, "{}", json)?;
        }

        file.flush()?;
        Ok(())
    }

    /// Find procedures by title (case-insensitive substring search).
    pub fn find_by_title(&self, query: &str) -> io::Result<Vec<CapturedProcedure>> {
        let all = self.load_all()?;
        let query_lower = query.to_lowercase();

        let filtered: Vec<_> = all
            .into_iter()
            .filter(|p| {
                p.title.to_lowercase().contains(&query_lower)
                    || p.description.to_lowercase().contains(&query_lower)
            })
            .collect();

        Ok(filtered)
    }

    /// Find a procedure by its exact ID.
    pub fn find_by_id(&self, id: &str) -> io::Result<Option<CapturedProcedure>> {
        let all = self.load_all()?;
        Ok(all.into_iter().find(|p| p.id == id))
    }

    /// Update the confidence metrics for a procedure.
    ///
    /// Records a success or failure and updates the score.
    pub fn update_confidence(&self, id: &str, success: bool) -> io::Result<()> {
        let mut procedures = self.load_all()?;

        if let Some(procedure) = procedures.iter_mut().find(|p| p.id == id) {
            if success {
                procedure.record_success();
            } else {
                procedure.record_failure();
            }
            self.write_all(&procedures)?;
        } else {
            return Err(io::Error::new(
                io::ErrorKind::NotFound,
                format!("Procedure with ID '{}' not found", id),
            ));
        }

        Ok(())
    }

    /// Delete a procedure by ID.
    pub fn delete(&self, id: &str) -> io::Result<bool> {
        let mut procedures = self.load_all()?;
        let original_len = procedures.len();

        procedures.retain(|p| p.id != id);

        if procedures.len() != original_len {
            self.write_all(&procedures)?;
            Ok(true)
        } else {
            Ok(false)
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;
    use terraphim_types::procedure::ProcedureStep;

    fn create_test_store() -> (TempDir, ProcedureStore) {
        let temp_dir = TempDir::new().unwrap();
        let store_path = temp_dir.path().join("procedures.jsonl");
        let store = ProcedureStore::new(store_path);
        (temp_dir, store)
    }

    fn create_test_procedure(id: &str, title: &str) -> CapturedProcedure {
        let mut procedure = CapturedProcedure::new(
            id.to_string(),
            title.to_string(),
            format!("Description for {}", title),
        );

        procedure.add_step(ProcedureStep {
            ordinal: 1,
            command: "echo test".to_string(),
            precondition: None,
            postcondition: None,
            working_dir: None,
            privileged: false,
            tags: vec!["test".to_string()],
        });

        procedure
    }

    #[test]
    fn test_procedure_store_save_and_load() {
        let (_temp_dir, store) = create_test_store();

        let procedure = create_test_procedure("test-1", "Test Procedure");
        store.save(&procedure).unwrap();

        let loaded = store.load_all().unwrap();
        assert_eq!(loaded.len(), 1);
        assert_eq!(loaded[0].id, "test-1");
        assert_eq!(loaded[0].title, "Test Procedure");
    }

    #[test]
    fn test_procedure_store_find_by_title() {
        let (_temp_dir, store) = create_test_store();

        let proc1 = create_test_procedure("test-1", "Install Rust");
        let proc2 = create_test_procedure("test-2", "Install Node.js");
        let proc3 = create_test_procedure("test-3", "Deploy Application");

        store.save(&proc1).unwrap();
        store.save(&proc2).unwrap();
        store.save(&proc3).unwrap();

        let results = store.find_by_title("Install").unwrap();
        assert_eq!(results.len(), 2);
        assert!(results.iter().any(|p| p.title == "Install Rust"));
        assert!(results.iter().any(|p| p.title == "Install Node.js"));
    }

    #[test]
    fn test_procedure_store_update_confidence() {
        let (_temp_dir, store) = create_test_store();

        let mut procedure = create_test_procedure("test-1", "Test Procedure");
        procedure.confidence = ProcedureConfidence::new();
        store.save(&procedure).unwrap();

        // Record some successes
        store.update_confidence("test-1", true).unwrap();
        store.update_confidence("test-1", true).unwrap();
        store.update_confidence("test-1", false).unwrap();

        let loaded = store.load_all().unwrap();
        assert_eq!(loaded[0].confidence.success_count, 2);
        assert_eq!(loaded[0].confidence.failure_count, 1);
        assert_eq!(loaded[0].confidence.score, 2.0 / 3.0);
    }

    #[test]
    fn test_procedure_store_update_confidence_not_found() {
        let (_temp_dir, store) = create_test_store();

        let result = store.update_confidence("nonexistent", true);
        assert!(result.is_err());
        assert!(result.unwrap_err().kind() == io::ErrorKind::NotFound);
    }

    #[test]
    fn test_dedup_matching_titles() {
        let (_temp_dir, store) = create_test_store();

        // Create a procedure with high confidence
        let mut existing_proc = create_test_procedure("existing-id", "Rust Install");
        // Use record_success to properly set the score
        for _ in 0..10 {
            existing_proc.record_success();
        }
        existing_proc.record_failure();
        // Score should be ~0.909, high confidence
        assert!(existing_proc.confidence.is_high_confidence());

        existing_proc.add_step(ProcedureStep {
            ordinal: 2,
            command: "rustc --version".to_string(),
            precondition: None,
            postcondition: None,
            working_dir: None,
            privileged: false,
            tags: vec![],
        });
        store.save(&existing_proc).unwrap();

        // Create a new procedure with title that contains the pattern "rust install"
        let mut new_proc = create_test_procedure("new-id", "Rust Install Guide");
        new_proc.add_step(ProcedureStep {
            ordinal: 1,
            command: "curl https://sh.rustup.rs | sh".to_string(),
            precondition: None,
            postcondition: None,
            working_dir: None,
            privileged: false,
            tags: vec![],
        });

        // Save with deduplication - should merge with existing
        let saved = store.save_with_dedup(new_proc).unwrap();

        // Should have merged steps (echo test from both, plus rustc and curl)
        // new_proc has: echo test, curl
        // existing has: echo test, rustc
        // After merge: echo test, curl, rustc = 3 steps
        assert_eq!(
            saved.step_count(),
            3,
            "Expected 3 steps after merge: echo test, curl, rustc"
        );

        // Verify the merged procedure is saved (should replace existing)
        let all = store.load_all().unwrap();
        assert_eq!(all.len(), 1, "Should have only 1 procedure after merge");
        assert_eq!(
            all[0].step_count(),
            3,
            "Saved procedure should have 3 steps"
        );
    }

    #[test]
    fn test_dedup_no_match_for_different_titles() {
        let (_temp_dir, store) = create_test_store();

        // Create a procedure with high confidence
        let mut existing_proc = create_test_procedure("existing-id", "Install Rust");
        existing_proc.confidence.success_count = 10;
        existing_proc.confidence.failure_count = 0;
        existing_proc.confidence.score = 1.0;
        store.save(&existing_proc).unwrap();

        // Create a new procedure with different title
        let new_proc = create_test_procedure("new-id", "Deploy to Kubernetes");

        // Save with deduplication - should create new
        let saved = store.save_with_dedup(new_proc).unwrap();

        // Should be a new procedure
        assert_eq!(saved.id, "new-id");

        // Verify both procedures exist
        let all = store.load_all().unwrap();
        assert_eq!(all.len(), 2);
    }

    #[test]
    fn test_procedure_store_delete() {
        let (_temp_dir, store) = create_test_store();

        let proc1 = create_test_procedure("test-1", "Procedure 1");
        let proc2 = create_test_procedure("test-2", "Procedure 2");

        store.save(&proc1).unwrap();
        store.save(&proc2).unwrap();

        let deleted = store.delete("test-1").unwrap();
        assert!(deleted);

        let loaded = store.load_all().unwrap();
        assert_eq!(loaded.len(), 1);
        assert_eq!(loaded[0].id, "test-2");

        // Deleting non-existent should return false
        let deleted_again = store.delete("test-1").unwrap();
        assert!(!deleted_again);
    }

    #[test]
    fn test_procedure_store_find_by_id() {
        let (_temp_dir, store) = create_test_store();

        let proc1 = create_test_procedure("test-1", "Procedure 1");
        store.save(&proc1).unwrap();

        let found = store.find_by_id("test-1").unwrap();
        assert!(found.is_some());
        assert_eq!(found.unwrap().title, "Procedure 1");

        let not_found = store.find_by_id("nonexistent").unwrap();
        assert!(not_found.is_none());
    }
}