indra 0.10.0

Run, watch and orchestrate AI coding agents (Claude Code, Codex, Gemini, Ollama) across machines: a web console, workflows with conditions and loops, boards, an MCP server and graph memory, in one binary.
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
//! Parsing one Markdown note: title, type, tags, aliases and outgoing links.
//!
//! Link syntax, all Obsidian-compatible:
//!   `[[target]]`, `[[target|label]]`, `[[target#heading]]`  -> rel `related`
//!   `rel:: [[target]]` (a Dataview inline field, optionally a list item) -> rel `rel`
//!   `[label](other-note.md)`                                -> rel `related`
//!   a frontmatter key whose value holds `[[target]]`        -> rel = that key

use crate::mem::frontmatter::{self, Frontmatter, Value};
use serde::Serialize;
use std::path::PathBuf;

pub const DEFAULT_REL: &str = "related";

#[derive(Debug, Clone, Serialize, PartialEq)]
pub struct Link {
    pub target: String,
    pub rel: String,
    /// 1-based line in the file, 0 when it came from frontmatter.
    pub line: usize,
}

#[derive(Debug, Clone)]
pub struct Note {
    /// Vault-relative path without `.md`, `/`-separated. Unique.
    pub id: String,
    pub path: PathBuf,
    pub title: String,
    pub kind: Option<String>,
    pub tags: Vec<String>,
    pub aliases: Vec<String>,
    pub fm_id: Option<String>,
    pub created: Option<String>,
    pub updated: Option<String>,
    pub source: Option<String>,
    pub body: String,
    pub links: Vec<Link>,
}

impl Note {
    pub fn parse(id: &str, path: PathBuf, src: &str) -> Note {
        let (fm_text, body) = frontmatter::split(src);
        let fm = fm_text.map(frontmatter::parse).unwrap_or_default();
        let body_offset = match fm_text {
            Some(f) => f.lines().count() + 2,
            None => 0,
        };

        let basename = id.rsplit('/').next().unwrap_or(id).to_string();
        let title = fm
            .get_str("title")
            .or_else(|| first_heading(body))
            .unwrap_or(basename);

        let mut tags: Vec<String> = fm
            .get_list("tags")
            .into_iter()
            .map(|t| t.trim_start_matches('#').to_string())
            .filter(|t| !t.is_empty())
            .collect();
        for t in inline_tags(body) {
            if !tags.iter().any(|x| x.eq_ignore_ascii_case(&t)) {
                tags.push(t);
            }
        }

        let mut links = frontmatter_links(&fm);
        links.extend(body_links(body, body_offset));

        Note {
            id: id.to_string(),
            path,
            title,
            kind: fm.get_str("type").or_else(|| fm.get_str("kind")),
            tags,
            aliases: fm.get_list("aliases"),
            fm_id: fm.get_str("id"),
            created: fm.get_str("created"),
            updated: fm.get_str("updated"),
            source: fm.get_str("source"),
            body: body.to_string(),
            links,
        }
    }
}

fn first_heading(body: &str) -> Option<String> {
    let mut fence = false;
    for line in body.lines() {
        let t = line.trim();
        if t.starts_with("```") || t.starts_with("~~~") {
            fence = !fence;
            continue;
        }
        if !fence {
            if let Some(h) = t.strip_prefix("# ") {
                let h = h.trim();
                if !h.is_empty() {
                    return Some(h.to_string());
                }
            }
        }
    }
    None
}

fn frontmatter_links(fm: &Frontmatter) -> Vec<Link> {
    let mut out = Vec::new();
    for (key, value) in &fm.entries {
        let values: Vec<&String> = match value {
            Value::Str(s) => vec![s],
            Value::List(l) => l.iter().collect(),
        };
        let rel = normalize_rel(key).unwrap_or_else(|| DEFAULT_REL.to_string());
        for v in values {
            for target in wikilinks(v) {
                out.push(Link {
                    target,
                    rel: rel.clone(),
                    line: 0,
                });
            }
        }
    }
    out
}

fn body_links(body: &str, offset: usize) -> Vec<Link> {
    let mut out = Vec::new();
    let mut fence: Option<&str> = None;
    for (i, raw) in body.lines().enumerate() {
        let t = raw.trim_start();
        if let Some(open) = fence {
            if t.starts_with(open) {
                fence = None;
            }
            continue;
        }
        if t.starts_with("```") {
            fence = Some("```");
            continue;
        }
        if t.starts_with("~~~") {
            fence = Some("~~~");
            continue;
        }
        let line = strip_inline_code(raw);
        let (rel, scan) = match inline_field(&line) {
            Some((rel, value)) => (rel, value),
            None => (DEFAULT_REL.to_string(), line.as_str()),
        };
        let lineno = offset + i + 1;
        for target in wikilinks(scan) {
            out.push(Link {
                target,
                rel: rel.clone(),
                line: lineno,
            });
        }
        for target in md_links(scan) {
            out.push(Link {
                target,
                rel: rel.clone(),
                line: lineno,
            });
        }
    }
    out
}

/// `- rel:: value` or `rel:: value` -> (normalised rel, value).
pub fn inline_field(line: &str) -> Option<(String, &str)> {
    let mut t = line.trim_start();
    for bullet in ["- ", "* ", "+ "] {
        if let Some(rest) = t.strip_prefix(bullet) {
            t = rest.trim_start();
            break;
        }
    }
    let idx = t.find("::")?;
    let key = &t[..idx];
    if key.is_empty() || key.contains("[[") || key.trim() != key {
        return None;
    }
    let rel = normalize_rel(key)?;
    Some((rel, &t[idx + 2..]))
}

/// Lowercase, spaces to `-`; must then be `[a-z0-9_-]+` and start with a letter.
pub fn normalize_rel(s: &str) -> Option<String> {
    let r: String = s
        .trim()
        .to_lowercase()
        .chars()
        .map(|c| if c.is_whitespace() { '-' } else { c })
        .collect();
    let ok = !r.is_empty()
        && r.len() <= 64
        && r.chars()
            .next()
            .map(|c| c.is_ascii_lowercase())
            .unwrap_or(false)
        && r.chars()
            .all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '-' || c == '_');
    ok.then_some(r)
}

fn strip_inline_code(line: &str) -> String {
    if !line.contains('`') {
        return line.to_string();
    }
    let mut out = String::with_capacity(line.len());
    let mut in_code = false;
    for c in line.chars() {
        if c == '`' {
            in_code = !in_code;
            continue;
        }
        if !in_code {
            out.push(c);
        }
    }
    out
}

/// Every `[[...]]` target in a string, skipping embeds of non-note files.
pub fn wikilinks(s: &str) -> Vec<String> {
    let mut out = Vec::new();
    let mut rest = s;
    while let Some(start) = rest.find("[[") {
        let after = &rest[start + 2..];
        let Some(end) = after.find("]]") else { break };
        let inner = &after[..end];
        let embed = start > 0 && rest[..start].ends_with('!');
        rest = &after[end + 2..];
        if inner.contains('\n') || inner.contains("[[") {
            continue;
        }
        let target = inner.split('|').next().unwrap_or("");
        let target = target.split(['#', '^']).next().unwrap_or("").trim();
        if target.is_empty() {
            continue;
        }
        let target = target.strip_suffix(".md").unwrap_or(target);
        if embed && has_file_ext(target) {
            continue;
        }
        out.push(target.to_string());
    }
    out
}

/// `[label](path.md)` links to other notes; URLs and non-note files are ignored.
fn md_links(s: &str) -> Vec<String> {
    let mut out = Vec::new();
    let mut rest = s;
    while let Some(i) = rest.find("](") {
        let after = &rest[i + 2..];
        let Some(end) = after.find(')') else { break };
        let href = after[..end].trim();
        rest = &after[end + 1..];
        let href = href.split('#').next().unwrap_or("");
        if href.contains("://") || href.starts_with("mailto:") || !href.ends_with(".md") {
            continue;
        }
        let href = href.replace("%20", " ");
        let href = href.trim_start_matches("./");
        out.push(href.trim_end_matches(".md").to_string());
    }
    out
}

fn has_file_ext(t: &str) -> bool {
    match t.rsplit_once('.') {
        Some((_, ext)) => {
            !ext.is_empty() && ext.len() <= 5 && ext.chars().all(|c| c.is_ascii_alphanumeric())
        }
        None => false,
    }
}

/// Obsidian-style inline `#tags`, outside code. A tag needs one non-digit.
fn inline_tags(body: &str) -> Vec<String> {
    let mut out: Vec<String> = Vec::new();
    let mut fence = false;
    for raw in body.lines() {
        let t = raw.trim_start();
        if t.starts_with("```") || t.starts_with("~~~") {
            fence = !fence;
            continue;
        }
        if fence {
            continue;
        }
        let line = strip_inline_code(raw);
        let chars: Vec<char> = line.chars().collect();
        let mut i = 0;
        while i < chars.len() {
            let boundary = i == 0 || chars[i - 1].is_whitespace() || chars[i - 1] == '(';
            if chars[i] == '#' && boundary {
                let mut j = i + 1;
                while j < chars.len()
                    && (chars[j].is_alphanumeric() || matches!(chars[j], '_' | '-' | '/'))
                {
                    j += 1;
                }
                let tag: String = chars[i + 1..j].iter().collect();
                let tag = tag.trim_end_matches(['-', '/']).to_string();
                if !tag.is_empty()
                    && tag.chars().any(|c| !c.is_ascii_digit())
                    && !out.iter().any(|x| x.eq_ignore_ascii_case(&tag))
                {
                    out.push(tag);
                }
                i = j.max(i + 1);
            } else {
                i += 1;
            }
        }
    }
    out
}

/// A filesystem-safe, lowercase ASCII slug of at most 80 chars.
pub fn slugify(s: &str) -> String {
    let mut out = String::new();
    let mut dash = false;
    for c in s.chars() {
        if c.is_ascii_alphanumeric() {
            out.push(c.to_ascii_lowercase());
            dash = false;
        } else if !dash && !out.is_empty() {
            out.push('-');
            dash = true;
        }
    }
    while out.ends_with('-') {
        out.pop();
    }
    if out.len() > 80 {
        out.truncate(80);
        while out.ends_with('-') {
            out.pop();
        }
    }
    out
}

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

    fn note(src: &str) -> Note {
        Note::parse("folder/my-note", PathBuf::from("x.md"), src)
    }

    #[test]
    fn wikilink_forms() {
        assert_eq!(
            wikilinks("see [[A]], [[b|label]], [[c#Head]], [[d^blk]], [[e.md]], ![[img.png]], ![[f]] [[ ]]"),
            vec!["A", "b", "c", "d", "e", "f"]
        );
        assert_eq!(wikilinks("[[unclosed"), Vec::<String>::new());
    }

    #[test]
    fn typed_and_untyped_links() {
        let n = note(
            "---\ntitle: T\nrelated: [\"[[fm-target]]\"]\nparent: \"[[up]]\"\n---\n# Heading\nplain [[a]] and `[[code]]`\n- Mitigated By:: [[b]], [[c]]\nfixes:: [[d]]\n```\n[[fenced]]\n```\nsee [x](other%20note.md) and [y](https://x.io/a.md)\n",
        );
        let got: Vec<(String, String)> = n
            .links
            .iter()
            .map(|l| (l.target.clone(), l.rel.clone()))
            .collect();
        let want = [
            ("fm-target", "related"),
            ("up", "parent"),
            ("a", "related"),
            ("b", "mitigated-by"),
            ("c", "mitigated-by"),
            ("d", "fixes"),
            ("other note", "related"),
        ];
        let want: Vec<(String, String)> = want
            .iter()
            .map(|(a, b)| (a.to_string(), b.to_string()))
            .collect();
        assert_eq!(got, want);
        // `plain [[a]]` is body line 2, after 3 frontmatter lines and their 2 fences.
        assert_eq!(n.links[2].line, 7);
    }

    #[test]
    fn title_and_tags() {
        let n = note("# From Heading\ntext #sqli #django/orm `#nope` #123 a#b (#paren)\n");
        assert_eq!(n.title, "From Heading");
        assert_eq!(n.tags, vec!["sqli", "django/orm", "paren"]);
        let n = note("no heading");
        assert_eq!(n.title, "my-note");
        let n = note("---\ntags: [\"#a\", b]\ntype: pattern\n---\n#b #c\n");
        assert_eq!(n.tags, vec!["a", "b", "c"]);
        assert_eq!(n.kind.as_deref(), Some("pattern"));
    }

    #[test]
    fn slug() {
        assert_eq!(
            slugify("  Django .extra() bypasses ORM!! "),
            "django-extra-bypasses-orm"
        );
        assert_eq!(slugify("../../etc/passwd"), "etc-passwd");
        assert_eq!(slugify("日本"), "");
        assert_eq!(slugify(&"a".repeat(200)).len(), 80);
    }

    #[test]
    fn rel_names() {
        assert_eq!(
            normalize_rel("Mitigated By").as_deref(),
            Some("mitigated-by")
        );
        assert_eq!(normalize_rel("9lives"), None);
        assert_eq!(normalize_rel("a/b"), None);
    }
}