godmode-core 0.8.1

Core library for godmode: task graphs, session state, dispatch, and Claude Code plugin integrations.
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
//! Memory banking — persistent source-backed project context at `.ctx/memory-bank/`.

use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::time::SystemTime;

use anyhow::{Context, Result};

/// All memory-bank template file names in display order.
const TEMPLATE_FILES: &[&str] = &[
    "project-brief.md",
    "product-context.md",
    "tech-context.md",
    "system-patterns.md",
    "active-context.md",
    "progress.md",
];

/// Resolved memory-bank directory for a git root.
///
/// Prefers the canonical `.ctx/memory-bank/`. Falls back to the intermediate
/// `.ctx/godmode/memory-bank/` and then the original `.ctx/memory-banking/`
/// so existing checkouts keep resolving until they are migrated.
pub fn memory_banking_dir(git_root: &Path) -> PathBuf {
    let canonical = git_root.join(".ctx").join("memory-bank");
    if canonical.exists() {
        return canonical;
    }
    let intermediate = git_root.join(".ctx").join("godmode").join("memory-bank");
    if intermediate.exists() {
        return intermediate;
    }
    let legacy = git_root.join(".ctx").join("memory-banking");
    if legacy.exists() {
        return legacy;
    }
    canonical
}

/// Returns true if the memory-bank directory exists and has at least one .md file.
pub fn exists(git_root: &Path) -> bool {
    let dir = memory_banking_dir(git_root);
    dir.is_dir()
        && fs::read_dir(&dir).is_ok_and(|mut entries| {
            entries.any(|e| e.is_ok_and(|e| e.path().extension().is_some_and(|ext| ext == "md")))
        })
}

/// List .md files in the memory-banking directory.
pub fn list_files(git_root: &Path) -> Result<Vec<PathBuf>> {
    let dir = memory_banking_dir(git_root);
    if !dir.is_dir() {
        return Ok(vec![]);
    }
    let mut files: Vec<PathBuf> = fs::read_dir(&dir)?
        .filter_map(|e| e.ok())
        .map(|e| e.path())
        .filter(|p| p.extension().is_some_and(|ext| ext == "md"))
        .collect();
    files.sort();
    Ok(files)
}

/// Get the unix timestamp of the most recent commit (seconds since epoch).
/// Returns 0 if git is unavailable or repo has no commits.
fn latest_commit_timestamp(git_root: &Path) -> u64 {
    Command::new("git")
        .args(["log", "-1", "--format=%ct"])
        .current_dir(git_root)
        .output()
        .ok()
        .and_then(|o| {
            if o.status.success() {
                String::from_utf8(o.stdout).ok()
            } else {
                None
            }
        })
        .and_then(|s| s.trim().parse::<u64>().ok())
        .unwrap_or(0)
}

/// Check which files are stale (modified before the latest commit).
pub fn stale_files(git_root: &Path) -> Result<Vec<String>> {
    let commit_ts = latest_commit_timestamp(git_root);
    if commit_ts == 0 {
        return Ok(vec![]);
    }

    let commit_time = SystemTime::UNIX_EPOCH + std::time::Duration::from_secs(commit_ts);
    let files = list_files(git_root)?;
    let mut stale = Vec::new();

    for file in files {
        let modified = fs::metadata(&file)
            .and_then(|m| m.modified())
            .unwrap_or(SystemTime::UNIX_EPOCH);
        if modified < commit_time
            && let Some(name) = file.file_name()
        {
            stale.push(name.to_string_lossy().to_string());
        }
    }

    Ok(stale)
}

/// Print all memory-bank file contents for context injection.
/// Returns the output as a string (for --json mode) or prints directly.
pub fn inject(git_root: &Path, json: bool) -> Result<()> {
    let files = list_files(git_root)?;
    if files.is_empty() {
        return Ok(());
    }

    let stale = stale_files(git_root)?;

    if json {
        let mut entries = Vec::new();
        for file in &files {
            let name = file
                .file_name()
                .unwrap_or_default()
                .to_string_lossy()
                .to_string();
            let content =
                fs::read_to_string(file).with_context(|| format!("reading {}", file.display()))?;
            let is_stale = stale.contains(&name);
            entries.push(serde_json::json!({
                "file": name,
                "content": content,
                "stale": is_stale,
            }));
        }
        let output = serde_json::json!({
            "memory_banking": entries,
            "stale_files": stale,
        });
        println!("{}", serde_json::to_string_pretty(&output)?);
    } else {
        println!("[memory-banking] Injecting project context from .ctx/memory-bank/");
        for file in &files {
            let basename = file.file_name().unwrap_or_default().to_string_lossy();
            let content =
                fs::read_to_string(file).with_context(|| format!("reading {}", file.display()))?;
            println!("--- {} ---", basename);
            println!("{}", content);
        }
        if !stale.is_empty() {
            println!(
                "[memory-banking] STALE: {} — older than latest commit. Consider updating.",
                stale.join(", ")
            );
        }
    }

    Ok(())
}

/// Check for recent commits and print a reminder to update memory-bank files.
pub fn remind(git_root: &Path, json: bool) -> Result<()> {
    let output = Command::new("git")
        .args(["log", "--since=2 hours ago", "--oneline"])
        .current_dir(git_root)
        .output()?;

    let has_commits =
        output.status.success() && !String::from_utf8_lossy(&output.stdout).trim().is_empty();

    if has_commits {
        if json {
            let msg = serde_json::json!({
                "reminder": true,
                "message": "Session had commits. Update .ctx/memory-bank/active-context.md and progress.md before ending.",
            });
            println!("{}", serde_json::to_string_pretty(&msg)?);
        } else {
            println!(
                "[memory-banking] Session had commits. Update .ctx/memory-bank/active-context.md and progress.md before ending."
            );
        }
    }

    Ok(())
}

/// Create `.ctx/memory-bank/` with empty template files.
pub fn init(git_root: &Path) -> Result<()> {
    let dir = memory_banking_dir(git_root);
    fs::create_dir_all(&dir).with_context(|| format!("creating {}", dir.display()))?;

    for &name in TEMPLATE_FILES {
        let path = dir.join(name);
        if !path.exists() {
            let heading = name.trim_end_matches(".md").replace('-', " ");
            let content = format!(
                "# {}\n\n<!-- TODO: populate from source code -->\n",
                capitalize(&heading)
            );
            fs::write(&path, content).with_context(|| format!("writing {}", path.display()))?;
        }
    }

    println!(
        "[memory-banking] Initialized .ctx/memory-bank/ with {} template files.",
        TEMPLATE_FILES.len()
    );
    Ok(())
}

/// Show status of memory-banking directory.
pub fn status(git_root: &Path, json: bool) -> Result<()> {
    let dir = memory_banking_dir(git_root);
    if !dir.is_dir() {
        if json {
            println!(r#"{{"exists": false}}"#);
        } else {
            println!("[memory-banking] Not initialized. Run `godmode memory-banking init`.");
        }
        return Ok(());
    }

    let files = list_files(git_root)?;
    let stale = stale_files(git_root)?;

    if json {
        let file_names: Vec<String> = files
            .iter()
            .filter_map(|f| f.file_name().map(|n| n.to_string_lossy().to_string()))
            .collect();
        let output = serde_json::json!({
            "exists": true,
            "files": file_names,
            "stale_files": stale,
            "total": files.len(),
        });
        println!("{}", serde_json::to_string_pretty(&output)?);
    } else {
        println!(
            "[memory-banking] {} files in .ctx/memory-bank/",
            files.len()
        );
        for file in &files {
            let name = file
                .file_name()
                .unwrap_or_default()
                .to_string_lossy()
                .to_string();
            let marker = if stale.contains(&name) {
                " (STALE)"
            } else {
                ""
            };
            println!("  {}{}", name, marker);
        }
    }

    Ok(())
}

fn capitalize(s: &str) -> String {
    let mut chars = s.chars();
    match chars.next() {
        None => String::new(),
        Some(c) => c.to_uppercase().to_string() + chars.as_str(),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use tempfile::TempDir;

    fn setup_fake_root() -> TempDir {
        TempDir::new().expect("create tempdir")
    }

    fn setup_with_memory_bank(files: &[(&str, &str)]) -> TempDir {
        let tmp = setup_fake_root();
        let mb = tmp.path().join(".ctx").join("godmode").join("memory-bank");
        fs::create_dir_all(&mb).expect("create memory-bank dir");
        for (name, content) in files {
            fs::write(mb.join(name), content).expect("write file");
        }
        tmp
    }

    #[test]
    fn memory_banking_dir_prefers_new_path() {
        let tmp = setup_fake_root();
        let new = tmp.path().join(".ctx").join("godmode").join("memory-bank");
        fs::create_dir_all(&new).unwrap();
        assert_eq!(memory_banking_dir(tmp.path()), new);
    }

    #[test]
    fn memory_banking_dir_falls_back_to_legacy() {
        let tmp = setup_fake_root();
        let legacy = tmp.path().join(".ctx").join("memory-banking");
        fs::create_dir_all(&legacy).unwrap();
        assert_eq!(memory_banking_dir(tmp.path()), legacy);
    }

    #[test]
    fn memory_banking_dir_defaults_to_new_when_neither_exists() {
        let p = Path::new("/some/project");
        assert_eq!(
            memory_banking_dir(p),
            PathBuf::from("/some/project/.ctx/memory-bank")
        );
    }

    #[test]
    fn exists_false_when_no_dir() {
        let tmp = setup_fake_root();
        assert!(!exists(tmp.path()));
    }

    #[test]
    fn exists_false_when_dir_empty() {
        let tmp = setup_fake_root();
        let mb = tmp.path().join(".ctx").join("godmode").join("memory-bank");
        fs::create_dir_all(&mb).unwrap();
        assert!(!exists(tmp.path()));
    }

    #[test]
    fn exists_false_when_only_non_md_files() {
        let tmp = setup_fake_root();
        let mb = tmp.path().join(".ctx").join("godmode").join("memory-bank");
        fs::create_dir_all(&mb).unwrap();
        fs::write(mb.join(".DS_Store"), "junk").unwrap();
        assert!(!exists(tmp.path()));
    }

    #[test]
    fn exists_true_when_md_present() {
        let tmp = setup_with_memory_bank(&[("progress.md", "# Progress")]);
        assert!(exists(tmp.path()));
    }

    #[test]
    fn list_files_empty_when_no_dir() {
        let tmp = setup_fake_root();
        let files = list_files(tmp.path()).unwrap();
        assert!(files.is_empty());
    }

    #[test]
    fn list_files_returns_only_md_sorted() {
        let tmp = setup_with_memory_bank(&[
            ("z-file.md", "z"),
            ("a-file.md", "a"),
            (".DS_Store", "junk"),
            ("notes.txt", "not md"),
        ]);
        let files = list_files(tmp.path()).unwrap();
        let names: Vec<&str> = files
            .iter()
            .filter_map(|p| p.file_name()?.to_str())
            .collect();
        assert_eq!(names, vec!["a-file.md", "z-file.md"]);
    }

    #[test]
    fn init_creates_all_template_files() {
        let tmp = setup_fake_root();
        init(tmp.path()).unwrap();

        let mb = memory_banking_dir(tmp.path());
        assert!(mb.is_dir());

        for &name in TEMPLATE_FILES {
            let path = mb.join(name);
            assert!(path.exists(), "missing {name}");
            let content = fs::read_to_string(&path).unwrap();
            assert!(
                content.starts_with("# "),
                "file {name} should start with heading"
            );
        }
    }

    #[test]
    fn init_does_not_overwrite_existing() {
        let tmp = setup_with_memory_bank(&[("progress.md", "# Custom content")]);
        init(tmp.path()).unwrap();

        let content =
            fs::read_to_string(memory_banking_dir(tmp.path()).join("progress.md")).unwrap();
        assert_eq!(content, "# Custom content");
    }

    #[test]
    fn stale_files_empty_when_no_git() {
        let tmp = setup_with_memory_bank(&[("progress.md", "# p")]);
        // No .git dir, so latest_commit_timestamp returns 0
        let stale = stale_files(tmp.path()).unwrap();
        assert!(stale.is_empty());
    }

    #[test]
    fn capitalize_works() {
        assert_eq!(capitalize("hello world"), "Hello world");
        assert_eq!(capitalize(""), "");
        assert_eq!(capitalize("A"), "A");
    }
}