coyote-ai 0.7.4

An all-in-one, batteries included LLM CLI Tool
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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
use std::collections::HashSet;
use std::path::{Path, PathBuf};
use std::{env, fs};

use anyhow::{Context, Result, anyhow, bail};
use indexmap::IndexMap;
use serde_json::{Value, json};

use super::{FunctionDeclaration, JsonSchema};
use crate::config::RequestContext;
use crate::config::memory::{
    MemoryFile, MemoryFrontmatter, MemoryStore, WorkspaceMemory, bootstrap_workspace_memory,
    find_git_root,
};
use crate::config::paths;

pub const MEMORY_FUNCTION_PREFIX: &str = "memory__";

const PER_FILE_SOFT_CAP: usize = 2_000;

pub fn memory_function_declarations() -> Vec<FunctionDeclaration> {
    vec![
        FunctionDeclaration {
            name: format!("{MEMORY_FUNCTION_PREFIX}read"),
            description: "Read the full content of a specific memory file by its name slug."
                .to_string(),
            parameters: JsonSchema {
                type_value: Some("object".to_string()),
                properties: Some(IndexMap::from([(
                    "name".to_string(),
                    JsonSchema {
                        type_value: Some("string".to_string()),
                        description: Some(
                            "The `name:` slug of the memory file to read (from MEMORY.md index)"
                                .into(),
                        ),
                        ..Default::default()
                    },
                )])),
                required: Some(vec!["name".to_string()]),
                ..Default::default()
            },
            agent: false,
        },
        FunctionDeclaration {
            name: format!("{MEMORY_FUNCTION_PREFIX}write"),
            description:
                "Create or replace a memory file. Caller must also update MEMORY.md index."
                    .to_string(),
            parameters: JsonSchema {
                type_value: Some("object".to_string()),
                properties: Some(IndexMap::from([
                    (
                        "name".to_string(),
                        JsonSchema {
                            type_value: Some("string".to_string()),
                            description: Some(
                                "Short kebab-case slug for the file (no extension)".into(),
                            ),
                            ..Default::default()
                        },
                    ),
                    (
                        "description".to_string(),
                        JsonSchema {
                            type_value: Some("string".to_string()),
                            description: Some("One-line description for the MEMORY.md index".into()),
                            ..Default::default()
                        },
                    ),
                    (
                        "type".to_string(),
                        JsonSchema {
                            type_value: Some("string".to_string()),
                            description: Some(
                                "Memory type: user | feedback | project | reference".into(),
                            ),
                            ..Default::default()
                        },
                    ),
                    (
                        "content".to_string(),
                        JsonSchema {
                            type_value: Some("string".to_string()),
                            description: Some("The full markdown body of the memory file".into()),
                            ..Default::default()
                        },
                    ),
                    (
                        "scope".to_string(),
                        JsonSchema {
                            type_value: Some("string".to_string()),
                            description: Some(
                                "Where to write: 'global' (user-level) or 'workspace' (project-level)"
                                    .into(),
                            ),
                            ..Default::default()
                        },
                    ),
                ])),
                required: Some(vec![
                    "name".to_string(),
                    "description".to_string(),
                    "content".to_string(),
                    "scope".to_string(),
                ]),
                ..Default::default()
            },
            agent: false,
        },
        FunctionDeclaration {
            name: format!("{MEMORY_FUNCTION_PREFIX}list"),
            description: "List all known drill files with metadata (size, type, scope).".to_string(),
            parameters: JsonSchema {
                type_value: Some("object".to_string()),
                properties: Some(IndexMap::new()),
                ..Default::default()
            },
            agent: false,
        },
        FunctionDeclaration {
            name: format!("{MEMORY_FUNCTION_PREFIX}lint"),
            description: "Health-check memory: orphan files, broken [[wikilinks]], oversized files."
                .to_string(),
            parameters: JsonSchema {
                type_value: Some("object".to_string()),
                properties: Some(IndexMap::new()),
                ..Default::default()
            },
            agent: false,
        },
        FunctionDeclaration {
            name: format!("{MEMORY_FUNCTION_PREFIX}edit_index"),
            description:
                "Replace the entire MEMORY.md index at the given scope. Use to add always-on facts, \
                 reorganize, prune stale entries, or fix descriptions. Coyote manages the path; \
                 NEVER use fs_write or any other generic file tool on MEMORY.md."
                    .to_string(),
            parameters: JsonSchema {
                type_value: Some("object".to_string()),
                properties: Some(IndexMap::from([
                    (
                        "scope".to_string(),
                        JsonSchema {
                            type_value: Some("string".to_string()),
                            description: Some(
                                "Where to edit: 'global' (user-level) or 'workspace' (project-level)"
                                    .into(),
                            ),
                            ..Default::default()
                        },
                    ),
                    (
                        "content".to_string(),
                        JsonSchema {
                            type_value: Some("string".to_string()),
                            description: Some("Full new contents of MEMORY.md".into()),
                            ..Default::default()
                        },
                    ),
                ])),
                required: Some(vec!["scope".to_string(), "content".to_string()]),
                ..Default::default()
            },
            agent: false,
        },
    ]
}

pub fn handle_memory_tool(ctx: &mut RequestContext, cmd_name: &str, args: &Value) -> Result<Value> {
    if !ctx.should_register_memory_tools() {
        bail!("Memory tools are disabled (memory off or function calling unavailable).");
    }

    let action = cmd_name
        .strip_prefix(MEMORY_FUNCTION_PREFIX)
        .unwrap_or(cmd_name);
    let cwd = env::current_dir().context("get cwd")?;
    let store = MemoryStore::new(&cwd);

    match action {
        "read" => {
            let name = args
                .get("name")
                .and_then(Value::as_str)
                .ok_or_else(|| anyhow!("name is required"))?;
            let file = find_file(&store, name)?
                .ok_or_else(|| anyhow!("memory file '{}' not found", name))?;

            Ok(json!({
                "name": file.frontmatter.name,
                "type": file.frontmatter.kind,
                "content": file.body,
            }))
        }
        "list" => {
            let files = store.list_files()?;
            let entries: Vec<_> = files
                .iter()
                .map(|f| {
                    json!({
                        "name": f.frontmatter.name,
                        "description": f.frontmatter.description,
                        "type": f.frontmatter.kind,
                        "char_len": f.char_len(),
                        "path": f.path.display().to_string(),
                    })
                })
                .collect();

            Ok(json!({
                "files": entries,
                "global_index_exists": paths::global_memory_index_path().exists(),
                "workspace": store.workspace.as_ref().map(workspace_label),
            }))
        }
        "write" => {
            let name = arg_str(args, "name")?;
            let description = arg_str(args, "description")?;
            let content = arg_str(args, "content")?;
            let scope = arg_str(args, "scope")?;
            let kind = args.get("type").and_then(Value::as_str).map(String::from);

            let target_dir = match scope.as_str() {
                "global" => paths::global_memory_dir(),
                "workspace" => workspace_write_dir(&store, &cwd)?,
                other => bail!("unknown scope '{}': use 'global' or 'workspace'", other),
            };
            let file = MemoryFile {
                path: target_dir.join(format!("{name}.md")),
                frontmatter: MemoryFrontmatter {
                    name: name.clone(),
                    description: Some(description.clone()),
                    kind,
                },
                body: content,
            };
            file.save()?;

            let index_path = target_dir.join("MEMORY.md");
            let index_updated = ensure_index_entry(&index_path, &name, &description)?;

            Ok(json!({
                "status": "ok",
                "path": file.path.display().to_string(),
                "index_path": index_path.display().to_string(),
                "index_updated": index_updated,
            }))
        }
        "edit_index" => {
            let scope = arg_str(args, "scope")?;
            let content = arg_str(args, "content")?;
            let target_dir = match scope.as_str() {
                "global" => paths::global_memory_dir(),
                "workspace" => workspace_write_dir(&store, &cwd)?,
                other => bail!("unknown scope '{}': use 'global' or 'workspace'", other),
            };
            let index_path = write_memory_index(&target_dir, &content)?;

            Ok(json!({
                "status": "ok",
                "path": index_path.display().to_string(),
            }))
        }
        "lint" => lint_memory(&store),
        _ => bail!("unknown memory action: {action}"),
    }
}

fn write_memory_index(target_dir: &Path, content: &str) -> Result<PathBuf> {
    fs::create_dir_all(target_dir)?;
    let index_path = target_dir.join("MEMORY.md");
    fs::write(&index_path, content)?;
    Ok(index_path)
}

fn ensure_index_entry(index_path: &Path, name: &str, description: &str) -> Result<bool> {
    let existing = fs::read_to_string(index_path).unwrap_or_default();
    let already_referenced =
        existing.contains(&format!("[[{name}]]")) || existing.contains(&format!("{name}.md"));

    if already_referenced {
        return Ok(false);
    }

    let entry = format!("- [[{name}]]: {description}\n");
    let new_content = if existing.is_empty() {
        format!("# Memory Index\n\n{entry}")
    } else if existing.ends_with('\n') {
        format!("{existing}{entry}")
    } else {
        format!("{existing}\n{entry}")
    };

    if let Some(parent) = index_path.parent() {
        fs::create_dir_all(parent)?;
    }

    fs::write(index_path, new_content)?;
    Ok(true)
}

fn arg_str(args: &Value, key: &str) -> Result<String> {
    args.get(key)
        .and_then(Value::as_str)
        .map(String::from)
        .ok_or_else(|| anyhow!("{} is required", key))
}

fn find_file(store: &MemoryStore, name: &str) -> Result<Option<MemoryFile>> {
    Ok(store
        .list_files()?
        .into_iter()
        .find(|f| f.frontmatter.name == name))
}

fn workspace_write_dir(store: &MemoryStore, cwd: &Path) -> Result<PathBuf> {
    match &store.workspace {
        Some(WorkspaceMemory::Structured { dir, .. }) => Ok(dir.clone()),
        Some(WorkspaceMemory::Lite { workspace_root, .. }) => {
            Ok(paths::workspace_memory_dir_for(workspace_root))
        }
        None => match find_git_root(cwd) {
            Some(git_root) => bootstrap_workspace_memory(&git_root),
            None => bail!(
                "no workspace memory discoverable and not inside a git repository for auto-bootstrap. \
                 If you want workspace memory, run `coyote --init-memory workspace`."
            ),
        },
    }
}

fn workspace_label(w: &WorkspaceMemory) -> Value {
    match w {
        WorkspaceMemory::Structured { workspace_root, .. } => json!({
            "mode": "structured",
            "root": workspace_root.display().to_string(),
        }),
        WorkspaceMemory::Lite {
            workspace_root,
            file,
        } => json!({
            "mode": "lite",
            "root": workspace_root.display().to_string(),
            "file": file.display().to_string(),
        }),
    }
}

fn lint_memory(store: &MemoryStore) -> Result<Value> {
    let files = store.list_files()?;
    let names: HashSet<&str> = files.iter().map(|f| f.frontmatter.name.as_str()).collect();

    let mut oversized = Vec::new();
    let mut broken_links = Vec::new();
    for f in &files {
        if f.char_len() > PER_FILE_SOFT_CAP {
            oversized.push(json!({"name": &f.frontmatter.name, "chars": f.char_len()}));
        }
        for link in extract_wikilinks(&f.body) {
            if !names.contains(link.as_str()) {
                broken_links.push(json!({"from": &f.frontmatter.name, "to": link}));
            }
        }
    }

    let index_content = store
        .load_global_index()?
        .or_else(|| store.load_workspace_index().ok().flatten())
        .unwrap_or_default();
    let mut orphans = Vec::new();
    for f in &files {
        if !index_content.contains(&f.frontmatter.name) {
            orphans.push(f.frontmatter.name.clone());
        }
    }

    Ok(json!({
        "total_files": files.len(),
        "oversized": oversized,
        "broken_wikilinks": broken_links,
        "orphans": orphans,
    }))
}

fn extract_wikilinks(body: &str) -> Vec<String> {
    let mut out = Vec::new();
    let bytes = body.as_bytes();
    let mut i = 0;
    while i + 1 < bytes.len() {
        if bytes[i] == b'['
            && bytes[i + 1] == b'['
            && let Some(end_rel) = body[i + 2..].find("]]")
        {
            out.push(body[i + 2..i + 2 + end_rel].to_string());
            i = i + 2 + end_rel + 2;
            continue;
        }
        i += 1;
    }

    out
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::memory::discover_workspace_memory;
    use std::fs;
    use std::time;

    fn temp_root(label: &str) -> PathBuf {
        let unique = time::SystemTime::now()
            .duration_since(time::UNIX_EPOCH)
            .unwrap()
            .as_nanos();
        let root = env::temp_dir().join(format!("coyote-function-memory-{label}-{unique}"));
        fs::create_dir_all(&root).unwrap();
        root
    }

    #[test]
    fn extract_wikilinks_finds_all_pairs() {
        let body = "see [[alpha]] and [[bravo]] but not [single] or [[unclosed";

        assert_eq!(
            extract_wikilinks(body),
            vec!["alpha".to_string(), "bravo".to_string()]
        );
    }

    #[test]
    fn extract_wikilinks_handles_empty_and_no_links() {
        assert!(extract_wikilinks("").is_empty());
        assert!(extract_wikilinks("nothing here").is_empty());
    }

    #[test]
    fn ensure_index_entry_appends_when_missing() {
        let root = temp_root("index_append");
        let index = root.join("MEMORY.md");
        fs::write(&index, "# Memory Index\n\n- [[existing]]: already here\n").unwrap();

        let updated = ensure_index_entry(&index, "new_one", "newly added").unwrap();
        assert!(updated);
        let content = fs::read_to_string(&index).unwrap();
        assert!(content.contains("- [[existing]]: already here"));
        assert!(content.contains("- [[new_one]]: newly added"));

        let _ = fs::remove_dir_all(&root);
    }

    #[test]
    fn ensure_index_entry_skips_when_referenced() {
        let root = temp_root("index_skip");
        let index = root.join("MEMORY.md");
        let original = "# Memory Index\n\n- [[existing]]: already here\n";
        fs::write(&index, original).unwrap();

        let updated = ensure_index_entry(&index, "existing", "different description").unwrap();
        assert!(!updated);
        assert_eq!(fs::read_to_string(&index).unwrap(), original);

        let _ = fs::remove_dir_all(&root);
    }

    #[test]
    fn ensure_index_entry_creates_index_when_absent() {
        let root = temp_root("index_create");
        let index = root.join("memory").join("MEMORY.md");

        let updated = ensure_index_entry(&index, "first", "first ever").unwrap();
        assert!(updated);
        let content = fs::read_to_string(&index).unwrap();
        assert!(content.starts_with("# Memory Index"));
        assert!(content.contains("- [[first]]: first ever"));

        let _ = fs::remove_dir_all(&root);
    }

    #[test]
    fn workspace_write_dir_returns_structured_dir_directly() {
        let root = temp_root("ws_structured");
        let workspace = root.join("ws");
        let structured = workspace.join(".coyote").join("memory");
        fs::create_dir_all(&structured).unwrap();
        fs::write(structured.join("MEMORY.md"), "idx").unwrap();

        let store = MemoryStore {
            global_dir: root.join("g"),
            workspace: discover_workspace_memory(&workspace),
        };

        let dir = workspace_write_dir(&store, &workspace).unwrap();
        assert_eq!(dir, structured);

        let _ = fs::remove_dir_all(&root);
    }

    #[test]
    fn workspace_write_dir_promotes_lite_to_structured_subdir() {
        let root = temp_root("ws_lite_promote");
        let workspace = root.join("ws");
        fs::create_dir_all(&workspace).unwrap();
        fs::write(workspace.join("COYOTE.md"), "lite").unwrap();

        let store = MemoryStore {
            global_dir: root.join("g"),
            workspace: discover_workspace_memory(&workspace),
        };

        let dir = workspace_write_dir(&store, &workspace).unwrap();
        assert_eq!(dir, workspace.join(".coyote").join("memory"));

        let _ = fs::remove_dir_all(&root);
    }

    #[test]
    fn workspace_write_dir_errors_when_no_workspace_and_no_git() {
        let root = temp_root("ws_none");
        let bare = root.join("nowhere");
        fs::create_dir_all(&bare).unwrap();

        let store = MemoryStore {
            global_dir: root.join("g"),
            workspace: discover_workspace_memory(&bare),
        };

        let err = workspace_write_dir(&store, &bare).unwrap_err();
        let msg = err.to_string();
        assert!(msg.contains("no workspace memory discoverable"));
        assert!(msg.contains("coyote --init-memory workspace"));

        let _ = fs::remove_dir_all(&root);
    }

    #[test]
    fn workspace_write_dir_auto_bootstraps_inside_git_repo() {
        let root = temp_root("ws_bootstrap");
        let repo = root.join("repo");
        fs::create_dir_all(repo.join(".git")).unwrap();
        let nested = repo.join("src").join("deep");
        fs::create_dir_all(&nested).unwrap();

        let store = MemoryStore {
            global_dir: root.join("g"),
            workspace: discover_workspace_memory(&nested),
        };
        assert!(store.workspace.is_none());

        let dir = workspace_write_dir(&store, &nested).unwrap();
        assert_eq!(dir, repo.join(".coyote").join("memory"));
        assert!(dir.join("MEMORY.md").exists());
        let gi = fs::read_to_string(repo.join(".gitignore")).unwrap();
        assert!(gi.contains(".coyote/memory/"));

        let _ = fs::remove_dir_all(&root);
    }

    #[test]
    fn find_file_returns_matching_file() {
        let root = temp_root("find_file");
        let workspace = root.join("ws");
        let structured = workspace.join(".coyote").join("memory");
        fs::create_dir_all(&structured).unwrap();
        fs::write(structured.join("MEMORY.md"), "idx").unwrap();
        fs::write(
            structured.join("target.md"),
            "---\nname: target\n---\nfound me\n",
        )
        .unwrap();
        fs::write(
            structured.join("other.md"),
            "---\nname: other\n---\nignored\n",
        )
        .unwrap();

        let store = MemoryStore {
            global_dir: root.join("g"),
            workspace: discover_workspace_memory(&workspace),
        };

        let hit = find_file(&store, "target").unwrap();
        assert!(hit.is_some());
        assert_eq!(hit.unwrap().body.trim(), "found me");

        let miss = find_file(&store, "nope").unwrap();
        assert!(miss.is_none());

        let _ = fs::remove_dir_all(&root);
    }

    #[test]
    fn write_memory_index_creates_dir_and_writes_content() {
        let root = temp_root("write_index_create");
        let target = root.join("nested").join(".coyote").join("memory");

        let path =
            write_memory_index(&target, "# Workspace Memory Index\n\n- [[foo]]: hello\n").unwrap();

        assert_eq!(path, target.join("MEMORY.md"));
        assert!(path.exists());
        assert_eq!(
            fs::read_to_string(&path).unwrap(),
            "# Workspace Memory Index\n\n- [[foo]]: hello\n"
        );

        let _ = fs::remove_dir_all(&root);
    }

    #[test]
    fn write_memory_index_replaces_existing_content() {
        let root = temp_root("write_index_replace");
        fs::create_dir_all(&root).unwrap();
        let index = root.join("MEMORY.md");
        fs::write(&index, "# Old\n\n- [[stale]]: gone\n").unwrap();

        let path = write_memory_index(&root, "# New\n").unwrap();

        assert_eq!(path, index);
        assert_eq!(fs::read_to_string(&path).unwrap(), "# New\n");

        let _ = fs::remove_dir_all(&root);
    }

    #[test]
    fn lint_flags_orphans_broken_links_and_oversized() {
        let root = temp_root("lint");
        let workspace = root.join("ws");
        let structured = workspace.join(".coyote").join("memory");
        fs::create_dir_all(&structured).unwrap();

        fs::write(structured.join("MEMORY.md"), "- referenced\n").unwrap();
        fs::write(
            structured.join("referenced.md"),
            "---\nname: referenced\n---\nlinks to [[missing]] and [[also_missing]]\n",
        )
        .unwrap();
        fs::write(
            structured.join("orphan.md"),
            "---\nname: orphan\n---\nnot in the index\n",
        )
        .unwrap();
        let huge_body = "x".repeat(PER_FILE_SOFT_CAP + 100);
        fs::write(
            structured.join("huge.md"),
            format!("---\nname: huge\n---\n{huge_body}\n"),
        )
        .unwrap();

        let store = MemoryStore {
            global_dir: root.join("nonexistent_global"),
            workspace: discover_workspace_memory(&workspace),
        };

        let report = lint_memory(&store).unwrap();
        assert_eq!(report["total_files"], 3);

        let orphans = report["orphans"].as_array().unwrap();
        let orphan_names: Vec<&str> = orphans.iter().filter_map(|v| v.as_str()).collect();
        assert!(orphan_names.contains(&"orphan"));
        assert!(orphan_names.contains(&"huge"));
        assert!(!orphan_names.contains(&"referenced"));

        let broken = report["broken_wikilinks"].as_array().unwrap();
        let broken_targets: Vec<&str> = broken.iter().filter_map(|v| v["to"].as_str()).collect();
        assert!(broken_targets.contains(&"missing"));
        assert!(broken_targets.contains(&"also_missing"));

        let oversized = report["oversized"].as_array().unwrap();
        let oversized_names: Vec<&str> = oversized
            .iter()
            .filter_map(|v| v["name"].as_str())
            .collect();
        assert_eq!(oversized_names, vec!["huge"]);

        let _ = fs::remove_dir_all(&root);
    }
}