rustbrain-core 0.3.20

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
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
//! Algorithmic **plan / task status** densification for FTS and agent summaries.
//!
//! Plans are ordinary Markdown. Rustbrain does **not** require a kanban product.
//! On index, it extracts a compact status vocabulary so agents can query and pack
//! status without reading whole roadmaps:
//!
//! | Canonical | Accepted surface forms |
//! |-----------|------------------------|
//! | `backlog` | backlog, todo, open, pending, queued |
//! | `in_progress` | in_progress, in-progress, inprogress, wip, doing, active |
//! | `qa` | qa, review, in-review, testing, verification |
//! | `done` | done, complete, completed, finished, closed, shipped (task-level) |
//! | `cancelled` | cancelled, canceled, wontfix, dropped, obsolete |
//! | `blocked` | blocked, stuck, on_hold, paused, deferred, undone, reopen, reopened |
//!
//! Sources (first wins for *overall* status when explicit):
//! 1. YAML frontmatter `status:` / `state:`
//! 2. `## Status` section body (first status token)
//! 3. Section headings (`## In Progress`, `## Done`, …)
//! 4. Checkbox lines (`- [ ]`, `- [x]`, `- [/]`, `- [~]`)
//!
//! Dense FTS tokens use stable prefixes (`status:…`, `task:status:slug`) so they
//! survive stopword stripping and stay machine-readable.

use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;

/// Canonical task / plan lifecycle status.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, PartialOrd, Ord)]
#[serde(rename_all = "snake_case")]
pub enum PlanStatus {
    /// Not started / queue.
    Backlog,
    /// Actively being worked.
    InProgress,
    /// Review / QA / testing.
    Qa,
    /// Finished successfully.
    Done,
    /// Explicitly abandoned.
    Cancelled,
    /// Blocked, stuck, on hold, or reopened (not actively progressing).
    Blocked,
}

impl PlanStatus {
    /// Stable token used in FTS and summaries.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Backlog => "backlog",
            Self::InProgress => "in_progress",
            Self::Qa => "qa",
            Self::Done => "done",
            Self::Cancelled => "cancelled",
            Self::Blocked => "blocked",
        }
    }

    /// Parse free-form status text into a canonical value.
    pub fn parse(s: &str) -> Option<Self> {
        let t = s
            .trim()
            .trim_matches(|c: char| c == '"' || c == '\'' || c == '`')
            .to_ascii_lowercase()
            .replace('-', "_")
            .replace(' ', "_");
        match t.as_str() {
            "backlog" | "todo" | "to_do" | "open" | "pending" | "queued" | "queue" | "later"
            | "icebox" => Some(Self::Backlog),
            "in_progress" | "inprogress" | "wip" | "doing" | "active" | "started" | "working"
            | "progress" => Some(Self::InProgress),
            "qa" | "review" | "in_review" | "testing" | "test" | "verification" | "verify"
            | "code_review" | "pr_review" => Some(Self::Qa),
            "done" | "complete" | "completed" | "finished" | "closed" | "shipped" | "resolved"
            | "fixed" | "merged" => Some(Self::Done),
            "cancelled" | "canceled" | "wontfix" | "wont_fix" | "dropped" | "obsolete"
            | "abandoned" | "declined" => Some(Self::Cancelled),
            // Canonical `blocked`; `undone` kept as alias for older notes.
            "blocked" | "stuck" | "on_hold" | "paused" | "deferred" | "undone" | "reopen"
            | "reopened" => Some(Self::Blocked),
            _ => None,
        }
    }
}

/// One extracted checklist / task line.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PlanTask {
    /// Canonical status.
    pub status: PlanStatus,
    /// Short task text (truncated).
    pub text: String,
}

/// Aggregated densification for a plan note body (+ optional frontmatter status).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PlanStatusDigest {
    /// Overall plan status when detectable.
    pub overall: Option<PlanStatus>,
    /// Counts by status (from tasks + section bulk).
    pub counts: BTreeMap<String, usize>,
    /// Individual tasks (capped).
    pub tasks: Vec<PlanTask>,
    /// Open = backlog + in_progress + qa + blocked.
    pub open: usize,
    /// Done count.
    pub done: usize,
    /// Cancelled count.
    pub cancelled: usize,
}

impl PlanStatusDigest {
    /// Dense one-line summary for `Node.summary` / agent skimming.
    pub fn summary_line(&self) -> String {
        let overall = self
            .overall
            .map(|s| s.as_str())
            .unwrap_or("unspecified");
        format!(
            "plan status={overall} · open {} · done {} · cancelled {}",
            self.open, self.done, self.cancelled
        )
    }

    /// Tokens appended to FTS body (information-dense, stopword-resistant).
    pub fn fts_block(&self) -> String {
        let mut parts = Vec::new();
        parts.push("[plan-status]".to_string());
        if let Some(o) = self.overall {
            parts.push(format!("status:{}", o.as_str()));
            parts.push(format!("plan_status:{}", o.as_str()));
        }
        parts.push(format!("plan_open:{}", self.open));
        parts.push(format!("plan_done:{}", self.done));
        parts.push(format!("plan_cancelled:{}", self.cancelled));
        for (k, n) in &self.counts {
            if *n > 0 {
                parts.push(format!("count:{k}:{n}"));
                // Repeat status tokens for soft ranking weight (bounded).
                for _ in 0..(*n).min(5) {
                    parts.push(format!("status:{k}"));
                }
            }
        }
        for t in self.tasks.iter().take(40) {
            let slug = slug_token(&t.text);
            if !slug.is_empty() {
                parts.push(format!("task:{}:{}", t.status.as_str(), slug));
            }
            parts.push(format!("status:{}", t.status.as_str()));
        }
        parts.join(" ")
    }

    /// True when we found any signal (worth densifying).
    pub fn has_signal(&self) -> bool {
        self.overall.is_some() || !self.tasks.is_empty() || self.counts.values().any(|n| *n > 0)
    }
}

/// Build a digest from optional frontmatter status string + markdown body.
pub fn densify_plan(frontmatter_status: Option<&str>, body: &str) -> PlanStatusDigest {
    let mut counts: BTreeMap<String, usize> = BTreeMap::new();
    let mut tasks: Vec<PlanTask> = Vec::new();
    let mut overall = frontmatter_status.and_then(PlanStatus::parse);

    // ## Status section: first non-empty line / first status token
    if overall.is_none() {
        if let Some(sec) = section_body(body, "status") {
            overall = first_status_in_text(&sec);
        }
    }

    // Headings as section-scoped status for following checkboxes
    let mut section_status: Option<PlanStatus> = None;
    for line in body.lines() {
        let t = line.trim();
        if let Some(rest) = t.strip_prefix('#') {
            let heading = rest.trim_start_matches('#').trim();
            let h = heading.to_ascii_lowercase();
            // "## In Progress" / "## Done" / "## Backlog" / "## QA"
            section_status = PlanStatus::parse(&h).or_else(|| {
                // heading may be longer: "In Progress (this week)"
                h.split_whitespace()
                    .find_map(|w| PlanStatus::parse(w))
                    .or_else(|| {
                        for key in [
                            "in_progress",
                            "in progress",
                            "backlog",
                            "done",
                            "qa",
                            "review",
                            "cancelled",
                            "canceled",
                            "todo",
                            "blocked",
                        ] {
                            if h.contains(key) {
                                return PlanStatus::parse(key);
                            }
                        }
                        None
                    })
            });
            continue;
        }

        if let Some((st, text)) = parse_checkbox_line(t) {
            let status = st.or(section_status).unwrap_or(PlanStatus::Backlog);
            *counts.entry(status.as_str().to_string()).or_default() += 1;
            if tasks.len() < 64 {
                tasks.push(PlanTask {
                    status,
                    text: truncate(&text, 80),
                });
            }
            continue;
        }

        // Bullet with leading status tag: "- in_progress: foo" or "- [done] foo"
        if let Some(rest) = t.strip_prefix('-').or_else(|| t.strip_prefix('*')) {
            let rest = rest.trim();
            if let Some((st, text)) = parse_tagged_bullet(rest) {
                *counts.entry(st.as_str().to_string()).or_default() += 1;
                if tasks.len() < 64 {
                    tasks.push(PlanTask {
                        status: st,
                        text: truncate(&text, 80),
                    });
                }
            }
        }
    }

    // If still no overall, infer from task mix.
    if overall.is_none() && !tasks.is_empty() {
        overall = Some(infer_overall(&counts));
    }

    let done = *counts.get("done").unwrap_or(&0);
    let cancelled = *counts.get("cancelled").unwrap_or(&0);
    let open = counts
        .iter()
        .filter(|(k, _)| matches!(k.as_str(), "backlog" | "in_progress" | "qa" | "blocked"))
        .map(|(_, n)| *n)
        .sum();

    PlanStatusDigest {
        overall,
        counts,
        tasks,
        open,
        done,
        cancelled,
    }
}

fn infer_overall(counts: &BTreeMap<String, usize>) -> PlanStatus {
    let get = |k: &str| *counts.get(k).unwrap_or(&0);
    if get("in_progress") > 0 {
        return PlanStatus::InProgress;
    }
    if get("qa") > 0 {
        return PlanStatus::Qa;
    }
    if get("blocked") > 0 {
        return PlanStatus::Blocked;
    }
    if get("backlog") > 0 {
        return PlanStatus::Backlog;
    }
    if get("done") > 0 && get("cancelled") == 0 {
        return PlanStatus::Done;
    }
    if get("cancelled") > 0 && get("done") == 0 && get("backlog") == 0 {
        return PlanStatus::Cancelled;
    }
    PlanStatus::Backlog
}

fn parse_checkbox_line(t: &str) -> Option<(Option<PlanStatus>, String)> {
    let t = t.strip_prefix('-').or_else(|| t.strip_prefix('*'))?.trim();
    // - [ ] text  / - [x] text / - [/] / - [~]
    if !t.starts_with('[') {
        return None;
    }
    let close = t.find(']')?;
    let mark = t[1..close].trim();
    let text = t[close + 1..].trim().to_string();
    if text.is_empty() {
        return None;
    }
    let st = match mark.to_ascii_lowercase().as_str() {
        "" | " " => Some(PlanStatus::Backlog),
        "x" => Some(PlanStatus::Done),
        "/" | ">" | "o" => Some(PlanStatus::InProgress),
        "~" | "-" => Some(PlanStatus::Cancelled),
        "?" => Some(PlanStatus::Qa),
        "!" => Some(PlanStatus::Blocked),
        other => PlanStatus::parse(other),
    };
    Some((st, text))
}

fn parse_tagged_bullet(rest: &str) -> Option<(PlanStatus, String)> {
    // [done] text
    if let Some(inner) = rest.strip_prefix('[') {
        if let Some(end) = inner.find(']') {
            let tag = &inner[..end];
            let text = inner[end + 1..].trim();
            if let Some(st) = PlanStatus::parse(tag) {
                if !text.is_empty() {
                    return Some((st, text.to_string()));
                }
            }
        }
    }
    // status: text  or status - text
    for sep in [':', '', '-'] {
        if let Some((left, right)) = rest.split_once(sep) {
            if let Some(st) = PlanStatus::parse(left.trim()) {
                let text = right.trim();
                if !text.is_empty() && text.len() < 200 {
                    return Some((st, text.to_string()));
                }
            }
        }
    }
    None
}

fn section_body(body: &str, name: &str) -> Option<String> {
    let name = name.to_ascii_lowercase();
    let mut lines = body.lines().peekable();
    let mut out = String::new();
    let mut in_sec = false;
    while let Some(line) = lines.next() {
        let t = line.trim();
        if let Some(rest) = t.strip_prefix('#') {
            let heading = rest.trim_start_matches('#').trim().to_ascii_lowercase();
            if in_sec {
                break;
            }
            if heading == name || heading.starts_with(&format!("{name} ")) {
                in_sec = true;
                continue;
            }
        }
        if in_sec {
            out.push_str(line);
            out.push('\n');
        }
    }
    if out.trim().is_empty() {
        None
    } else {
        Some(out)
    }
}

fn first_status_in_text(s: &str) -> Option<PlanStatus> {
    for raw in s.split(|c: char| c.is_whitespace() || c == ',' || c == ';' || c == '|') {
        if let Some(st) = PlanStatus::parse(raw) {
            return Some(st);
        }
    }
    // whole first line
    s.lines()
        .map(str::trim)
        .find(|l| !l.is_empty() && !l.starts_with("<!--"))
        .and_then(PlanStatus::parse)
}

fn slug_token(s: &str) -> String {
    let mut out = String::new();
    let mut prev = false;
    for c in s.chars().take(48) {
        if c.is_ascii_alphanumeric() {
            out.push(c.to_ascii_lowercase());
            prev = false;
        } else if !prev && !out.is_empty() {
            out.push('_');
            prev = true;
        }
    }
    while out.ends_with('_') {
        out.pop();
    }
    out
}

fn truncate(s: &str, max: usize) -> String {
    if s.chars().count() <= max {
        s.to_string()
    } else {
        let t: String = s.chars().take(max.saturating_sub(1)).collect();
        format!("{t}")
    }
}

/// Enrich FTS body + summary for plan notes when status signal exists.
pub fn enrich_plan_index_fields(
    body: &str,
    frontmatter_status: Option<&str>,
    base_summary: &str,
) -> (String, String) {
    let dig = densify_plan(frontmatter_status, body);
    if !dig.has_signal() {
        return (body.to_string(), base_summary.to_string());
    }
    let fts = format!("{body}\n\n{}\n", dig.fts_block());
    let summary = if base_summary.is_empty() {
        dig.summary_line()
    } else {
        format!("{} · {}", dig.summary_line(), base_summary)
    };
    // Keep summary bounded for SQLite / agent skims
    let summary = if summary.chars().count() > 240 {
        truncate(&summary, 240)
    } else {
        summary
    };
    (fts, summary)
}

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

    #[test]
    fn parses_checkboxes_and_sections() {
        let body = r#"
## Status

in_progress

## Backlog

- [ ] Design API
- [ ] Write docs

## In Progress

- [/] Implement parser

## Done

- [x] Scaffold types

## Cancelled

- [~] Old approach
"#;
        let d = densify_plan(None, body);
        assert_eq!(d.overall, Some(PlanStatus::InProgress));
        assert_eq!(d.done, 1);
        assert_eq!(d.cancelled, 1);
        assert!(d.open >= 3);
        let fts = d.fts_block();
        assert!(fts.contains("status:in_progress"));
        assert!(fts.contains("task:done:"));
        assert!(fts.contains("plan_open:"));
    }

    #[test]
    fn frontmatter_overall_wins() {
        let d = densify_plan(Some("qa"), "- [ ] still open\n");
        assert_eq!(d.overall, Some(PlanStatus::Qa));
    }

    #[test]
    fn tagged_bullets() {
        let body = "- done: ship 0.3\n- blocked: flaky CI\n";
        let d = densify_plan(None, body);
        assert!(d.tasks.iter().any(|t| t.status == PlanStatus::Done));
        assert!(d.tasks.iter().any(|t| t.status == PlanStatus::Blocked));
    }

    #[test]
    fn parse_aliases() {
        assert_eq!(PlanStatus::parse("WIP"), Some(PlanStatus::InProgress));
        assert_eq!(PlanStatus::parse("wontfix"), Some(PlanStatus::Cancelled));
        assert_eq!(PlanStatus::parse("in-review"), Some(PlanStatus::Qa));
        assert_eq!(PlanStatus::parse("blocked"), Some(PlanStatus::Blocked));
        // Legacy alias
        assert_eq!(PlanStatus::parse("undone"), Some(PlanStatus::Blocked));
    }
}