rustbrain-core 0.3.18

Core engine for rustbrain: SQLite knowledge graph, ranked FTS, CSR mmap cache, and graph-aware AI context
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
//! Create structured Markdown notes for the brain (`note new`).

use crate::error::{BrainError, Result};
use crate::id::node_id_from_rel_path;
use crate::types::NodeType;
use std::path::{Path, PathBuf};

/// Options for [`create_note`].
#[derive(Debug, Clone)]
pub struct NoteNewOptions {
    /// Node type (`goal`, `adr`, `concept`, …).
    pub node_type: NodeType,
    /// Human title (also used for H1).
    pub title: String,
    /// Optional body text after the title (agents use `--note`).
    pub note: Option<String>,
    /// Optional extra tags.
    pub tags: Vec<String>,
    /// Optional aliases.
    pub aliases: Vec<String>,
    /// Subdirectory under workspace (default chosen from type).
    pub dir: Option<PathBuf>,
    /// If true, overwrite an existing file at the target path.
    pub force: bool,
}

/// Result of creating a note on disk.
#[derive(Debug, Clone)]
pub struct NoteCreated {
    /// Absolute path written.
    pub path: PathBuf,
    /// Relative path from workspace.
    pub rel_path: PathBuf,
    /// Stable node id after next sync.
    pub node_id: String,
}

/// Default docs subdirectory for a node type.
pub fn default_dir_for_type(ty: &NodeType) -> &'static str {
    match ty {
        NodeType::Goal => "docs/goals",
        NodeType::Adr => "docs/adr",
        NodeType::Alternative => "docs/adr",
        NodeType::Concept => "docs/concepts",
        NodeType::Analysis => "docs/analysis",
        NodeType::Symbol => "docs/concepts",
        NodeType::Reference => "docs/concepts",
        NodeType::EdgeCase => "docs/edge_cases",
        // Root Keep a Changelog file is the hub; supplementary release notes live here.
        NodeType::Changelog => "docs/changelogs",
        NodeType::Plan => "docs/plans",
    }
}

/// Slugify a title into a filename stem.
pub fn slugify_title(title: &str) -> String {
    let mut out = String::new();
    let mut prev_dash = false;
    for c in title.chars() {
        if c.is_ascii_alphanumeric() {
            out.push(c.to_ascii_lowercase());
            prev_dash = false;
        } else if !prev_dash && !out.is_empty() {
            out.push('-');
            prev_dash = true;
        }
    }
    while out.ends_with('-') {
        out.pop();
    }
    if out.is_empty() {
        "untitled".into()
    } else {
        out
    }
}

/// Create a Markdown note under the workspace.
///
/// Does **not** open SQLite — call [`crate::Brain::sync`] afterward to index.
pub fn create_note(workspace: &Path, opts: &NoteNewOptions) -> Result<NoteCreated> {
    if opts.title.trim().is_empty() {
        return Err(BrainError::other("note title must not be empty"));
    }

    let dir_rel = opts
        .dir
        .clone()
        .unwrap_or_else(|| PathBuf::from(default_dir_for_type(&opts.node_type)));
    let abs_dir = workspace.join(&dir_rel);
    std::fs::create_dir_all(&abs_dir)?;

    let stem = slugify_title(&opts.title);
    let filename = format!("{stem}.md");
    let abs_path = abs_dir.join(&filename);
    let rel_path = dir_rel.join(&filename);

    if abs_path.exists() && !opts.force {
        return Err(BrainError::other(format!(
            "note already exists: {} (use --force to overwrite)",
            rel_path.display()
        )));
    }

    let mut tags = opts.tags.clone();
    if tags.is_empty() {
        tags.push(opts.node_type.as_str().to_string());
    }

    let tags_yaml = format_yaml_list(&tags);
    let aliases_yaml = if opts.aliases.is_empty() {
        String::new()
    } else {
        format!("aliases: {}\n", format_yaml_list(&opts.aliases))
    };

    let body = opts.note.as_deref().unwrap_or("").trim();
    let body_section = if body.is_empty() {
        match opts.node_type {
            NodeType::Adr => {
                "\n## Status\n\nProposed\n\n## Context\n\n<!-- why this decision is needed -->\n\n## Decision\n\n<!-- what we decided -->\n\n## Consequences\n\n<!-- trade-offs -->\n"
                    .to_string()
            }
            NodeType::Goal => {
                "\n## Goals\n\n- \n\n## Non-goals\n\n- \n".to_string()
            }
            NodeType::Analysis => ANALYSIS_NOTE_TEMPLATE.to_string(),
            NodeType::Plan => PLAN_NOTE_TEMPLATE.to_string(),
            NodeType::Changelog => CHANGELOG_NOTE_TEMPLATE.to_string(),
            _ => "\n".to_string(),
        }
    } else {
        format!("\n{body}\n")
    };

    let status_yaml = if matches!(opts.node_type, NodeType::Plan) {
        "status: backlog\n"
    } else {
        ""
    };

    let content = format!(
        "---\n\
         tags: {tags_yaml}\n\
         node_type: {}\n\
         {status_yaml}\
         {aliases_yaml}\
         ---\n\
         # {}\n\
         {body_section}",
        opts.node_type.as_str(),
        opts.title.trim(),
    );

    std::fs::write(&abs_path, content)?;

    let node_id = node_id_from_rel_path(&rel_path);
    Ok(NoteCreated {
        path: abs_path,
        rel_path,
        node_id,
    })
}

/// Scaffold body for empty analysis notes (episodic, not a decision).
const ANALYSIS_NOTE_TEMPLATE: &str = r#"
## Question / scope

<!-- What are we investigating? (crate comparison, perf, design options, data, …) -->

## When

<!-- ISO date/time is useful in the title or here, e.g. 2026-07-31 -->

## Findings

-

## Artifacts (optional)

<!-- Links or summaries of evidence: criterion `cargo bench` output, tables, logs, PRs, plots. Prefer paths/commands over pasting huge dumps. -->

-

## Recommendations (optional — not a decision)

<!-- Promotion path: if/when you commit, write an ADR and link it with [[…]]. -->

-

## Open questions / edge cases

-

## Related

<!-- [[goals/…]]  [[concepts/…]]  symbol:Type::method  [[docs/edge_cases/…]] -->

"#;

/// Scaffold for plans / roadmaps / tasklists / todos (not a decision; not ship history).
///
/// Status vocabulary (indexed densely on sync): backlog | in_progress | qa | done |
/// cancelled | undone. Set overall with frontmatter `status:` and/or section headings
/// and checkboxes (`- [ ]` open, `- [x]` done, `- [/]` in progress, `- [~]` cancelled).
const PLAN_NOTE_TEMPLATE: &str = r#"
## Status

<!-- overall: backlog | in_progress | qa | done | cancelled | undone -->
in_progress

## Intent

<!-- What outcome does this plan unlock? Link goals: [[docs/goals/…]] -->

## Backlog

- [ ] 

## In Progress

- [/] 

## QA

- [?] 

## Done

- [x] 

## Cancelled

- [~] 

## Priority / order

1. 

## Out of scope

- 

## Related

<!-- [[changelog]]  [[roadmap]]  [[docs/adr/…]]  analysis notes -->

"#;

/// Scaffold for supplementary changelog notes (prefer root CHANGELOG.md as hub).
const CHANGELOG_NOTE_TEMPLATE: &str = r#"
## Version

<!-- e.g. 0.4.0 — also update root CHANGELOG.md (Keep a Changelog) when shipping -->

## Summary

-

## Added

-

## Changed

-

## Fixed

-

## Related

<!-- Prefer the root hub: [[changelog]]  ADRs: [[docs/adr/…]] -->

"#;

fn format_yaml_list(items: &[String]) -> String {
    if items.is_empty() {
        return "[]".into();
    }
    let inner = items
        .iter()
        .map(|t| {
            if t.chars().any(|c| c.is_whitespace() || c == ':' || c == '#') {
                format!("\"{}\"", t.replace('"', "\\\""))
            } else {
                t.clone()
            }
        })
        .collect::<Vec<_>>()
        .join(", ");
    format!("[{inner}]")
}

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

    #[test]
    fn creates_analysis_under_docs_analysis() {
        let dir = tempdir().unwrap();
        let created = create_note(
            dir.path(),
            &NoteNewOptions {
                node_type: NodeType::Analysis,
                title: "Criterion bench: query path".into(),
                note: Some(
                    "Compared baseline vs patched run_sql. p50 improved 12% on cold cache.".into(),
                ),
                tags: vec!["bench".into(), "criterion".into()],
                aliases: vec![],
                dir: None,
                force: false,
            },
        )
        .unwrap();
        assert!(created
            .rel_path
            .to_string_lossy()
            .starts_with("docs/analysis/"));
        let text = std::fs::read_to_string(&created.path).unwrap();
        assert!(text.contains("node_type: analysis"));
        assert!(text.contains("p50 improved"));
        assert!(text.contains("# Criterion bench: query path"));
    }

    #[test]
    fn analysis_empty_body_gets_scaffold_sections() {
        let dir = tempdir().unwrap();
        let created = create_note(
            dir.path(),
            &NoteNewOptions {
                node_type: NodeType::Analysis,
                title: "egui vs tauri".into(),
                note: None,
                tags: vec![],
                aliases: vec![],
                dir: None,
                force: false,
            },
        )
        .unwrap();
        let text = std::fs::read_to_string(&created.path).unwrap();
        assert!(text.contains("## Question / scope"));
        assert!(text.contains("## Findings"));
        assert!(text.contains("## Recommendations"));
        assert!(text.contains("cargo bench"));
    }

    #[test]
    fn plan_scaffold_under_docs_plans() {
        let dir = tempdir().unwrap();
        let created = create_note(
            dir.path(),
            &NoteNewOptions {
                node_type: NodeType::Plan,
                title: "Q3 roadmap".into(),
                note: None,
                tags: vec![],
                aliases: vec![],
                dir: None,
                force: false,
            },
        )
        .unwrap();
        assert!(created
            .rel_path
            .to_string_lossy()
            .starts_with("docs/plans/"));
        let text = std::fs::read_to_string(&created.path).unwrap();
        assert!(text.contains("node_type: plan"));
        assert!(text.contains("status: backlog"));
        assert!(text.contains("## Backlog"));
        assert!(text.contains("- [ ]"));
        assert!(text.contains("## Done"));
    }

    #[test]
    fn creates_adr_with_body() {
        let dir = tempdir().unwrap();
        let created = create_note(
            dir.path(),
            &NoteNewOptions {
                node_type: NodeType::Adr,
                title: "Use SQLite".into(),
                note: Some("We need embedded storage.".into()),
                tags: vec!["storage".into()],
                aliases: vec![],
                dir: None,
                force: false,
            },
        )
        .unwrap();
        assert!(created.path.exists());
        let text = std::fs::read_to_string(&created.path).unwrap();
        assert!(text.contains("node_type: adr"));
        assert!(text.contains("# Use SQLite"));
        assert!(text.contains("We need embedded storage."));
        assert_eq!(created.node_id, "docs/adr/use-sqlite");
    }

    #[test]
    fn refuses_overwrite_without_force() {
        let dir = tempdir().unwrap();
        let opts = NoteNewOptions {
            node_type: NodeType::Concept,
            title: "Dup".into(),
            note: None,
            tags: vec![],
            aliases: vec![],
            dir: None,
            force: false,
        };
        create_note(dir.path(), &opts).unwrap();
        assert!(create_note(dir.path(), &opts).is_err());
    }
}