terraphim_agent 1.16.34

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
use std::path::PathBuf;
use terraphim_automata::{ThesaurusBuilder, builder::Logseq};

/// Detect if running in CI environment (GitHub Actions, Docker containers in CI, etc.)
fn is_ci_environment() -> bool {
    // Check standard CI environment variables
    std::env::var("CI").is_ok()
        || std::env::var("GITHUB_ACTIONS").is_ok()
        // Check if running as root in a container (common in CI Docker containers)
        || (std::env::var("USER").as_deref() == Ok("root")
            && std::path::Path::new("/.dockerenv").exists())
        // Check if the home directory is /root (typical for CI containers)
        || std::env::var("HOME").as_deref() == Ok("/root")
}

/// Check if an error is expected in CI (KG path not found, thesaurus build issues)
fn is_ci_expected_kg_error(err: &str) -> bool {
    err.contains("No such file or directory")
        || err.contains("KG path does not exist")
        || err.contains("Failed to build thesaurus")
        || err.contains("Knowledge graph not configured")
        || err.contains("not found")
        || err.contains("thesaurus")
        || err.contains("automata")
        || err.contains("IO error")
        || err.contains("Io error")
}

fn extract_clean_output(output: &str) -> String {
    output
        .lines()
        .filter(|line| {
            !line.contains("INFO")
                && !line.contains("WARN")
                && !line.contains("DEBUG")
                && !line.contains("OpenDal")
                && !line.contains("Creating role")
                && !line.contains("Successfully built thesaurus")
                && !line.contains("Starting summarization worker")
                && !line.contains("Failed to load config")
                && !line.contains("Failed to load thesaurus")
                && !line.contains("ERROR")
                && !line.trim().is_empty()
        })
        .collect::<Vec<&str>>()
        .join("\n")
}

/// Build a thesaurus from the existing KG markdown files in docs/src/kg/
async fn build_test_thesaurus() -> Result<terraphim_types::Thesaurus, Box<dyn std::error::Error>> {
    // Use CARGO_MANIFEST_DIR to find workspace root
    // CARGO_MANIFEST_DIR points to crates/terraphim_agent
    let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| ".".to_string());
    let manifest_path = PathBuf::from(manifest_dir);

    // Go up two levels: crates/terraphim_agent -> crates -> workspace_root
    let workspace_root = manifest_path
        .parent()
        .and_then(|p| p.parent())
        .ok_or("Cannot find workspace root from CARGO_MANIFEST_DIR")?;

    let kg_path = workspace_root.join("docs/src/kg");

    if !kg_path.exists() {
        return Err(format!(
            "KG path does not exist: {:?}\nworkspace_root: {:?}\nmanifest_dir: {:?}",
            kg_path, workspace_root, manifest_path
        )
        .into());
    }

    let logseq_builder = Logseq::default();
    let thesaurus = logseq_builder
        .build("test_role".to_string(), kg_path)
        .await?;

    Ok(thesaurus)
}

/// Perform replacement using the KG thesaurus
async fn replace_with_kg(
    text: &str,
    link_type: terraphim_automata::LinkType,
) -> Result<String, Box<dyn std::error::Error>> {
    let thesaurus = build_test_thesaurus().await?;
    let result = terraphim_automata::replace_matches(text, thesaurus, link_type)?;
    Ok(String::from_utf8(result)?)
}

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

    #[tokio::test]
    async fn test_replace_npm_to_bun() {
        let result = replace_with_kg("npm", terraphim_automata::LinkType::PlainText).await;

        match result {
            Ok(output) => {
                assert!(
                    output.contains("bun"),
                    "Expected 'bun' in output, got: {}",
                    output
                );
            }
            Err(e) => {
                let err_str = e.to_string();
                if is_ci_environment() && is_ci_expected_kg_error(&err_str) {
                    println!(
                        "Test skipped in CI - KG fixtures unavailable: {}",
                        err_str.lines().next().unwrap_or("")
                    );
                    return;
                }
                panic!("Failed to perform replacement: {}", e);
            }
        }
    }

    #[tokio::test]
    async fn test_replace_yarn_to_bun() {
        let result = replace_with_kg("yarn", terraphim_automata::LinkType::PlainText).await;

        match result {
            Ok(output) => {
                assert!(
                    output.contains("bun"),
                    "Expected 'bun' in output, got: {}",
                    output
                );
            }
            Err(e) => {
                let err_str = e.to_string();
                if is_ci_environment() && is_ci_expected_kg_error(&err_str) {
                    println!(
                        "Test skipped in CI - KG fixtures unavailable: {}",
                        err_str.lines().next().unwrap_or("")
                    );
                    return;
                }
                panic!("Failed to perform replacement: {}", e);
            }
        }
    }

    #[tokio::test]
    async fn test_replace_pnpm_install_to_bun() {
        let result = replace_with_kg("pnpm install", terraphim_automata::LinkType::PlainText).await;

        match result {
            Ok(output) => {
                assert!(
                    output.contains("bun install"),
                    "Expected 'bun install' in output, got: {}",
                    output
                );
            }
            Err(e) => {
                let err_str = e.to_string();
                if is_ci_environment() && is_ci_expected_kg_error(&err_str) {
                    println!(
                        "Test skipped in CI - KG fixtures unavailable: {}",
                        err_str.lines().next().unwrap_or("")
                    );
                    return;
                }
                panic!("Failed to perform replacement: {}", e);
            }
        }
    }

    #[tokio::test]
    async fn test_replace_yarn_install_to_bun() {
        let result = replace_with_kg("yarn install", terraphim_automata::LinkType::PlainText).await;

        match result {
            Ok(output) => {
                assert!(
                    output.contains("bun install"),
                    "Expected 'bun install' in output, got: {}",
                    output
                );
            }
            Err(e) => {
                let err_str = e.to_string();
                if is_ci_environment() && is_ci_expected_kg_error(&err_str) {
                    println!(
                        "Test skipped in CI - KG fixtures unavailable: {}",
                        err_str.lines().next().unwrap_or("")
                    );
                    return;
                }
                panic!("Failed to perform replacement: {}", e);
            }
        }
    }

    #[tokio::test]
    async fn test_replace_with_markdown_format() {
        let result = replace_with_kg("npm", terraphim_automata::LinkType::MarkdownLinks).await;

        match result {
            Ok(output) => {
                assert!(
                    output.contains("[bun]"),
                    "Expected '[bun]' in markdown output, got: {}",
                    output
                );
            }
            Err(e) => {
                let err_str = e.to_string();
                if is_ci_environment() && is_ci_expected_kg_error(&err_str) {
                    println!(
                        "Test skipped in CI - KG fixtures unavailable: {}",
                        err_str.lines().next().unwrap_or("")
                    );
                    return;
                }
                panic!("Failed to perform replacement: {}", e);
            }
        }
    }

    #[test]
    fn test_replace_help_output() {
        let output = Command::new("cargo")
            .args([
                "run",
                "--quiet",
                "-p",
                "terraphim_agent",
                "--bin",
                "terraphim-agent",
                "--",
                "replace",
                "--help",
            ])
            .output()
            .expect("Failed to execute command");

        let stdout = String::from_utf8_lossy(&output.stdout);

        assert!(
            stdout.contains("replace") || stdout.contains("Replace"),
            "Help output should mention replace command"
        );
        assert!(
            stdout.contains("text") || stdout.contains("TEXT"),
            "Help output should mention text argument"
        );
    }

    #[test]
    fn test_extract_clean_output_helper() {
        let raw_output = r#"INFO: Starting process
DEBUG: Loading configuration
bun
WARN: Some warning
ERROR: Failed to load thesaurus
"#;
        let clean = extract_clean_output(raw_output);
        assert_eq!(clean, "bun");
    }

    #[test]
    fn test_extract_clean_output_multiline() {
        let raw_output = r#"[2025-10-06T19:35:46Z WARN  opendal::services] service=memory
bun install
[2025-10-06T19:35:46Z ERROR terraphim_service] Failed to load
"#;
        let clean = extract_clean_output(raw_output);
        assert_eq!(clean, "bun install");
    }

    // ============================================================
    // Issue #394 Tests: Case Preservation and URL Protection
    // ============================================================

    /// Test that URLs are protected from replacement
    #[tokio::test]
    async fn test_url_protection_plain_url() {
        use terraphim_types::{NormalizedTerm, NormalizedTermValue, Thesaurus};

        let mut thesaurus = Thesaurus::new("test".to_string());
        thesaurus.insert(
            NormalizedTermValue::from("example"),
            NormalizedTerm::new(1u64, NormalizedTermValue::from("example"))
                .with_display_value("REPLACED".to_string()),
        );

        let text = "Visit https://example.com for more info";
        let result = terraphim_automata::replace_matches(
            text,
            thesaurus,
            terraphim_automata::LinkType::PlainText,
        )
        .expect("Replacement should succeed");
        let result_str = String::from_utf8(result).expect("Valid UTF-8");

        // URL should remain unchanged
        assert!(
            result_str.contains("https://example.com"),
            "URL should be protected, got: {}",
            result_str
        );
    }

    /// Test that markdown link URLs are protected while display text is replaced
    #[tokio::test]
    async fn test_url_protection_markdown_link() {
        use terraphim_types::{NormalizedTerm, NormalizedTermValue, Thesaurus};

        let mut thesaurus = Thesaurus::new("test".to_string());
        thesaurus.insert(
            NormalizedTermValue::from("claude"),
            NormalizedTerm::new(1u64, NormalizedTermValue::from("claude"))
                .with_display_value("Terraphim".to_string()),
        );

        let text = "[Claude](https://claude.ai/code)";
        let result = terraphim_automata::replace_matches(
            text,
            thesaurus,
            terraphim_automata::LinkType::PlainText,
        )
        .expect("Replacement should succeed");
        let result_str = String::from_utf8(result).expect("Valid UTF-8");

        // URL should be preserved
        assert!(
            result_str.contains("https://claude.ai/code"),
            "Markdown link URL should be protected, got: {}",
            result_str
        );
        // Display text should be replaced
        assert!(
            result_str.contains("Terraphim"),
            "Display text should be replaced, got: {}",
            result_str
        );
    }

    /// Test that email addresses are protected from replacement
    #[tokio::test]
    async fn test_url_protection_email() {
        use terraphim_types::{NormalizedTerm, NormalizedTermValue, Thesaurus};

        let mut thesaurus = Thesaurus::new("test".to_string());
        thesaurus.insert(
            NormalizedTermValue::from("anthropic"),
            NormalizedTerm::new(1u64, NormalizedTermValue::from("anthropic"))
                .with_display_value("Company".to_string()),
        );

        let text = "Contact noreply@anthropic.com for help";
        let result = terraphim_automata::replace_matches(
            text,
            thesaurus,
            terraphim_automata::LinkType::PlainText,
        )
        .expect("Replacement should succeed");
        let result_str = String::from_utf8(result).expect("Valid UTF-8");

        // Email should remain unchanged
        assert!(
            result_str.contains("noreply@anthropic.com"),
            "Email should be protected, got: {}",
            result_str
        );
    }

    /// Test that display_value preserves case in replacement output
    #[tokio::test]
    async fn test_case_preservation_with_display_value() {
        use terraphim_types::{NormalizedTerm, NormalizedTermValue, Thesaurus};

        let mut thesaurus = Thesaurus::new("test".to_string());
        // Simulate what happens when building thesaurus from "Terraphim AI.md"
        thesaurus.insert(
            NormalizedTermValue::from("claude code"), // lowercase key for matching
            NormalizedTerm::new(1u64, NormalizedTermValue::from("terraphim ai"))
                .with_display_value("Terraphim AI".to_string()),
        );

        let text = "Using Claude Code for development";
        let result = terraphim_automata::replace_matches(
            text,
            thesaurus,
            terraphim_automata::LinkType::PlainText,
        )
        .expect("Replacement should succeed");
        let result_str = String::from_utf8(result).expect("Valid UTF-8");

        // Should use display_value with proper case
        assert!(
            result_str.contains("Terraphim AI"),
            "Should preserve case from display_value, got: {}",
            result_str
        );
        // Should NOT contain lowercase version
        assert!(
            !result_str.contains("terraphim ai"),
            "Should not output lowercase, got: {}",
            result_str
        );
    }

    /// Test fallback to normalized value when display_value is None
    #[tokio::test]
    async fn test_fallback_when_no_display_value() {
        use terraphim_types::{NormalizedTerm, NormalizedTermValue, Thesaurus};

        let mut thesaurus = Thesaurus::new("test".to_string());
        // NormalizedTerm without display_value (backward compatibility)
        thesaurus.insert(
            NormalizedTermValue::from("foo"),
            NormalizedTerm::new(1u64, NormalizedTermValue::from("bar")), // No display_value
        );

        let text = "Replace foo here";
        let result = terraphim_automata::replace_matches(
            text,
            thesaurus,
            terraphim_automata::LinkType::PlainText,
        )
        .expect("Replacement should succeed");
        let result_str = String::from_utf8(result).expect("Valid UTF-8");

        // Should fallback to normalized value
        assert!(
            result_str.contains("bar"),
            "Should fallback to normalized value, got: {}",
            result_str
        );
    }

    /// Test combined case preservation and URL protection (issue #394 example)
    #[tokio::test]
    async fn test_issue_394_combined_scenario() {
        use terraphim_types::{NormalizedTerm, NormalizedTermValue, Thesaurus};

        let mut thesaurus = Thesaurus::new("test".to_string());
        thesaurus.insert(
            NormalizedTermValue::from("claude code"),
            NormalizedTerm::new(1u64, NormalizedTermValue::from("terraphim ai"))
                .with_display_value("Terraphim AI".to_string()),
        );

        // This is the exact example from issue #394
        let text = "Generated with [Claude Code](https://claude.ai/claude-code)";
        let result = terraphim_automata::replace_matches(
            text,
            thesaurus,
            terraphim_automata::LinkType::PlainText,
        )
        .expect("Replacement should succeed");
        let result_str = String::from_utf8(result).expect("Valid UTF-8");

        // Display text should be replaced with proper case
        assert!(
            result_str.contains("Terraphim AI"),
            "Display text should use proper case, got: {}",
            result_str
        );
        // URL should NOT be modified
        assert!(
            result_str.contains("https://claude.ai/claude-code"),
            "URL should be protected, got: {}",
            result_str
        );
    }
}