terraphim_agent 1.20.3

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
//! Export captured corrections as reviewable KG markdown artefacts.
//!
//! Reads `correction-*.md` files from the learnings directory, groups
//! compatible corrections by their canonical `corrected` value, and emits
//! one Logseq-style KG markdown file per unique corrected term.

use std::collections::{HashMap, HashSet};
use std::fs;
use std::path::Path;

use super::capture::{CorrectionEvent, CorrectionType};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CorrectionTypeFilter {
    ToolPreference,
    All,
}

impl CorrectionTypeFilter {
    fn matches(&self, ct: &CorrectionType) -> bool {
        match self {
            CorrectionTypeFilter::All => true,
            CorrectionTypeFilter::ToolPreference => *ct == CorrectionType::ToolPreference,
        }
    }
}

#[derive(Debug)]
struct MergedCorrection {
    corrected: String,
    originals: Vec<String>,
    session_ids: Vec<String>,
    contexts: Vec<String>,
    captured_at: Option<chrono::DateTime<chrono::Utc>>,
}

impl MergedCorrection {
    fn to_kg_markdown(&self) -> String {
        let mut md = String::new();

        let synonyms = self.originals.join(", ");
        let captured = self
            .captured_at
            .map(|dt| dt.to_rfc3339())
            .unwrap_or_else(|| "unknown".to_string());

        md.push_str("---\n");
        md.push_str("type::: kg_entry\n");
        md.push_str("correction_type: tool_preference\n");
        md.push_str(&format!("captured_at: {}\n", captured));
        md.push_str("source: learned\n");
        md.push_str(&format!("synonyms:: {}\n", synonyms));
        md.push_str("priority:: 70\n");
        md.push_str("pinned:: false\n");
        md.push_str("---\n\n");

        md.push_str(&format!("# {}\n\n", self.corrected));

        md.push_str("## Original\n\n");
        md.push_str(&format!(
            "Multiple alternatives resolved to this correction:\n{}\n\n",
            self.originals
                .iter()
                .map(|o| format!("- `{}`", o))
                .collect::<Vec<_>>()
                .join("\n")
        ));

        md.push_str("## Corrected\n\n");
        md.push_str(&format!("`{}`\n\n", self.corrected));

        if !self.contexts.is_empty() && !self.contexts.iter().all(|c| c.is_empty()) {
            md.push_str("## Context\n\n");
            for ctx in &self.contexts {
                if !ctx.is_empty() {
                    md.push_str(&format!("- {}\n", ctx));
                }
            }
            md.push('\n');
        }

        if !self.session_ids.is_empty() {
            md.push_str("## Sessions\n\n");
            for sid in &self.session_ids {
                md.push_str(&format!("- {}\n", sid));
            }
            md.push('\n');
        }

        md
    }
}

fn slugify(s: &str) -> String {
    let mut result = String::new();
    let mut prev_was_sep = false;

    for c in s.chars() {
        if c.is_alphanumeric() || c == '-' || c == '_' {
            if c.is_uppercase() {
                for low in c.to_lowercase() {
                    result.push(low);
                }
            } else {
                result.push(c);
            }
            prev_was_sep = false;
        } else if !prev_was_sep {
            result.push('-');
            prev_was_sep = true;
        }
    }

    while result.ends_with('-') {
        result.pop();
    }

    result
}

fn deterministic_filename(corrected: &str, index: usize) -> String {
    let base = slugify(corrected);
    if index == 0 {
        format!("{}.md", base)
    } else {
        format!("{}-{}.md", base, index)
    }
}

fn read_corrections(learnings_dir: &Path) -> Result<Vec<CorrectionEvent>, std::io::Error> {
    let mut corrections = Vec::new();

    if !learnings_dir.exists() || !learnings_dir.is_dir() {
        return Ok(corrections);
    }

    for entry in fs::read_dir(learnings_dir)? {
        let entry = entry?;
        let path = entry.path();

        let filename = match path.file_name().and_then(|n| n.to_str()) {
            Some(name) if name.starts_with("correction-") && name.ends_with(".md") => name,
            _ => continue,
        };

        let content = match fs::read_to_string(&path) {
            Ok(c) => c,
            Err(e) => {
                log::warn!("Cannot read correction file {:?}: {}", filename, e);
                continue;
            }
        };

        if let Some(correction) = CorrectionEvent::from_markdown(&content) {
            corrections.push(correction);
        }
    }

    Ok(corrections)
}

fn group_corrections(
    corrections: Vec<CorrectionEvent>,
    filter: CorrectionTypeFilter,
) -> HashMap<String, MergedCorrection> {
    let mut groups: HashMap<String, MergedCorrection> = HashMap::new();

    for correction in corrections {
        if !filter.matches(&correction.correction_type) {
            continue;
        }

        if correction.original.is_empty() || correction.corrected.is_empty() {
            continue;
        }

        let key = correction.corrected.clone();
        let entry = groups.entry(key).or_insert_with(|| MergedCorrection {
            corrected: correction.corrected.clone(),
            originals: Vec::new(),
            session_ids: Vec::new(),
            contexts: Vec::new(),
            captured_at: None,
        });

        if !entry.originals.contains(&correction.original) {
            entry.originals.push(correction.original.clone());
        }

        if let Some(ref sid) = correction.session_id
            && !entry.session_ids.contains(sid)
        {
            entry.session_ids.push(sid.clone());
        }

        if !correction.context_description.is_empty()
            && !entry.contexts.contains(&correction.context_description)
        {
            entry.contexts.push(correction.context_description.clone());
        }

        if entry.captured_at.is_none()
            || correction
                .context
                .captured_at
                .lt(&entry.captured_at.unwrap_or(correction.context.captured_at))
        {
            entry.captured_at = Some(correction.context.captured_at);
        }
    }

    groups
}

pub fn export_corrections_as_kg(
    learnings_dir: &Path,
    output_dir: &Path,
    filter: CorrectionTypeFilter,
) -> Result<usize, std::io::Error> {
    let corrections = read_corrections(learnings_dir)?;
    let groups = group_corrections(corrections, filter);

    if groups.is_empty() {
        log::info!("No corrections to export");
        return Ok(0);
    }

    if !output_dir.exists() {
        fs::create_dir_all(output_dir)?;
    }

    let mut exported = 0usize;
    let mut used_names: HashSet<String> = HashSet::new();

    for (_, mut merged) in groups {
        merged.originals.sort();
        merged.session_ids.sort();
        merged.contexts.sort();
        merged.contexts.dedup();

        let mut idx = 0;
        let filename = loop {
            let name = deterministic_filename(&merged.corrected, idx);
            if !used_names.contains(&name) {
                break name;
            }
            idx += 1;
        };
        used_names.insert(filename.clone());
        let path = output_dir.join(&filename);

        let content = merged.to_kg_markdown();
        fs::write(&path, content)?;

        log::info!("Exported correction KG: {:?}", path);
        exported += 1;
    }

    Ok(exported)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::learnings::LearningSource;
    use tempfile::TempDir;

    fn make_correction(
        correction_type: CorrectionType,
        original: &str,
        corrected: &str,
    ) -> CorrectionEvent {
        CorrectionEvent::new(
            correction_type,
            original.to_string(),
            corrected.to_string(),
            String::new(),
            LearningSource::Project,
        )
    }

    fn write_correction(dir: &Path, name: &str, event: &CorrectionEvent) {
        let filename = format!("correction-{}.md", name);
        let path = dir.join(filename);
        fs::write(path, event.to_markdown()).expect("failed to write test correction");
    }

    #[test]
    fn test_export_empty_dir() {
        let tmp = TempDir::new().unwrap();
        let out = TempDir::new().unwrap();

        let count =
            export_corrections_as_kg(tmp.path(), out.path(), CorrectionTypeFilter::All).unwrap();
        assert_eq!(count, 0);
    }

    #[test]
    fn test_export_single_tool_preference() {
        let tmp = TempDir::new().unwrap();
        let out = TempDir::new().unwrap();

        let event = make_correction(CorrectionType::ToolPreference, "npm install", "bun install");
        write_correction(tmp.path(), "001", &event);

        let count =
            export_corrections_as_kg(tmp.path(), out.path(), CorrectionTypeFilter::All).unwrap();
        assert_eq!(count, 1);

        let expected = out.path().join("bun-install.md");
        assert!(expected.exists());

        let content = fs::read_to_string(&expected).unwrap();
        assert!(content.contains("# bun install"));
        assert!(content.contains("synonyms:: npm install"));
        assert!(content.contains("type::: kg_entry"));
    }

    #[test]
    fn test_export_merges_multiple_for_same_corrected() {
        let tmp = TempDir::new().unwrap();
        let out = TempDir::new().unwrap();

        let event1 = make_correction(CorrectionType::ToolPreference, "npm install", "bun install");
        let event2 = make_correction(CorrectionType::ToolPreference, "npm i", "bun install");
        let event3 = make_correction(CorrectionType::ToolPreference, "yarn add", "bun install");

        write_correction(tmp.path(), "001", &event1);
        write_correction(tmp.path(), "002", &event2);
        write_correction(tmp.path(), "003", &event3);

        let count =
            export_corrections_as_kg(tmp.path(), out.path(), CorrectionTypeFilter::All).unwrap();
        assert_eq!(count, 1);

        let expected = out.path().join("bun-install.md");
        let content = fs::read_to_string(&expected).unwrap();
        assert!(content.contains("npm install"));
        assert!(content.contains("npm i"));
    }

    #[test]
    fn test_export_filters_non_tool_preference() {
        let tmp = TempDir::new().unwrap();
        let out = TempDir::new().unwrap();

        let tool = make_correction(CorrectionType::ToolPreference, "npm", "bun");
        let naming = make_correction(CorrectionType::Naming, "foo", "bar");

        write_correction(tmp.path(), "tool", &tool);
        write_correction(tmp.path(), "naming", &naming);

        let count =
            export_corrections_as_kg(tmp.path(), out.path(), CorrectionTypeFilter::ToolPreference)
                .unwrap();
        assert_eq!(count, 1);

        assert!(out.path().join("bun.md").exists());
        assert!(!out.path().join("bar.md").exists());
    }

    #[test]
    fn test_export_all_includes_named_corrections() {
        let tmp = TempDir::new().unwrap();
        let out = TempDir::new().unwrap();

        let tool = make_correction(CorrectionType::ToolPreference, "npm", "bun");
        let naming = make_correction(CorrectionType::Naming, "foo", "bar");

        write_correction(tmp.path(), "tool", &tool);
        write_correction(tmp.path(), "naming", &naming);

        let count =
            export_corrections_as_kg(tmp.path(), out.path(), CorrectionTypeFilter::All).unwrap();
        assert_eq!(count, 2);

        assert!(out.path().join("bun.md").exists());
        assert!(out.path().join("bar.md").exists());
    }

    #[test]
    fn test_export_deterministic_filenames() {
        let tmp = TempDir::new().unwrap();
        let out = TempDir::new().unwrap();

        let e1 = make_correction(CorrectionType::ToolPreference, "npm", "bun");
        let e2 = make_correction(CorrectionType::ToolPreference, "yarn", "bun");

        write_correction(tmp.path(), "001", &e1);
        write_correction(tmp.path(), "002", &e2);

        let count =
            export_corrections_as_kg(tmp.path(), out.path(), CorrectionTypeFilter::All).unwrap();
        assert_eq!(count, 1);

        assert!(out.path().join("bun.md").exists());
    }

    #[test]
    fn test_slugify() {
        assert_eq!(slugify("Hello World!"), "hello-world");
        assert_eq!(slugify("npm install"), "npm-install");
        assert_eq!(slugify("foo/bar"), "foo-bar");
    }
}