sara-tasks 1.5.2

Sara — folder-aware task manager
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
use anyhow::Result;
use rusqlite::Connection;
use serde_json::{Value, json};
use std::collections::{HashMap, HashSet};

use crate::infrastructure::{db, embedding};

/// Cosine floor for calling two memories a genuine conflict candidate.
///
/// Sharing a file or a tag proves *co-occurrence*, not contradiction: on a real
/// store 84% of file-overlap pairs share exactly one path, and the pass is
/// quadratic in memories-per-file, so a single hub file drags in hundreds of
/// unrelated pairs. Scoring those pairs by embedding cosine gives a median of
/// ~0.55 — the median "conflict" is barely on topic. A 0.75 floor removes ~94%
/// of them while keeping every hand-verified duplicate.
///
/// Deliberately higher than `recall`'s general threshold (0.30) and than
/// `insight`'s `RELATED_THRESHOLD` (0.55) for the same reason those were
/// chosen: a false candidate is noise a human or agent must burn attention on,
/// so this pass buys precision with recall.
pub const DEFAULT_CONFLICT_THRESHOLD: f32 = 0.75;

/// A pair of memories that may be in conflict (no memory_links edge between them).
#[derive(Debug)]
pub struct ConflictCandidate {
    pub label_a: String,
    pub label_b: String,
    pub snippet_a: String,
    pub snippet_b: String,
    pub shared_files: Vec<String>,
    pub shared_tags: Vec<String>,
    /// Embedding cosine between the two bodies. `None` when either memory has
    /// no embedding row yet — such a pair is kept (fail-open) so a lagging
    /// index never silently hides a candidate.
    pub cosine: Option<f32>,
}

/// Core shared by CLI and MCP. Scans active memories and returns conflict
/// candidates: pairs sharing file-path links OR full tag sets, with no
/// `memory_links` edge between them in either direction, **and** whose bodies
/// are semantically close enough (`cosine >= threshold`) to be worth review.
///
/// `project` scopes the scan to memories linked to that project (both sides of
/// a pair must belong to it), so a scan run in one province does not report
/// another's. `limit` caps the returned list — candidates are always sorted by
/// cosine descending, so the worst offender is first and a cap is a clean
/// truncation rather than an arbitrary sample.
pub fn diagnose_value(
    conn: &Connection,
    threshold: f32,
    project: Option<&str>,
    limit: Option<usize>,
) -> Result<Value> {
    let memories = db::list_memories(conn)?;

    // Build a set of (uuid, uuid) pairs that already have a link edge.
    let all_links = db::all_memory_links(conn)?;
    let linked_pairs: HashSet<(String, String)> = all_links
        .iter()
        .flat_map(|l| {
            // Treat edges as undirected for conflict-suppression purposes.
            [
                (l.from_uuid.clone(), l.to_uuid.clone()),
                (l.to_uuid.clone(), l.from_uuid.clone()),
            ]
        })
        .collect();

    // Embeddings, keyed by uuid — the topical signal that separates a real
    // conflict from two memories that merely touched the same file.
    let vectors: HashMap<String, Vec<f32>> = db::active_embeddings(conn)?.into_iter().collect();

    // For each memory, collect its files and normalised tags.
    struct MemInfo {
        uuid: String,
        label: String,
        body: String,
        files: Vec<String>,
        tags: Vec<String>,
    }

    let mut infos: Vec<MemInfo> = Vec::new();
    for m in &memories {
        if let Some(scope) = project {
            let projects = db::get_item_projects(conn, &m.uuid).unwrap_or_default();
            if !projects.iter().any(|p| p.eq_ignore_ascii_case(scope)) {
                continue;
            }
        }
        let uuid = m.uuid.to_string();
        let files = db::get_item_files(conn, &m.uuid).unwrap_or_default();
        let label = m
            .display_id
            .map(|id| format!("m{id}"))
            .unwrap_or_else(|| uuid[..8].to_string());
        let tags: Vec<String> = m.tags.iter().map(|t| t.to_lowercase()).collect();
        infos.push(MemInfo {
            uuid,
            label,
            body: m.body.clone(),
            files,
            tags,
        });
    }

    // Index memories by file path.
    let mut by_file: HashMap<String, Vec<usize>> = HashMap::new();
    for (i, info) in infos.iter().enumerate() {
        for f in &info.files {
            by_file.entry(f.clone()).or_default().push(i);
        }
    }

    // Cosine between two memories, or None when either lacks an embedding.
    let score = |a: &MemInfo, b: &MemInfo| -> Option<f32> {
        let va = vectors.get(&a.uuid)?;
        let vb = vectors.get(&b.uuid)?;
        Some(embedding::cosine(va, vb))
    };

    // Collect conflict candidates — deduplicate with a seen set.
    let mut seen: HashSet<(usize, usize)> = HashSet::new();
    let mut candidates: Vec<ConflictCandidate> = Vec::new();

    // 1. File-path overlap.
    for indices in by_file.values() {
        for &i in indices {
            for &j in indices {
                if i >= j {
                    continue;
                }
                let key = (i.min(j), i.max(j));
                if seen.contains(&key) {
                    continue;
                }
                let a = &infos[i];
                let b = &infos[j];
                if linked_pairs.contains(&(a.uuid.clone(), b.uuid.clone())) {
                    seen.insert(key);
                    continue;
                }
                // Fail-open: an unscorable pair (missing embedding) is kept.
                let cosine = score(a, b);
                if cosine.is_some_and(|c| c < threshold) {
                    seen.insert(key);
                    continue;
                }
                let shared_files: Vec<String> = a
                    .files
                    .iter()
                    .filter(|f| b.files.contains(f))
                    .cloned()
                    .collect();
                candidates.push(ConflictCandidate {
                    label_a: a.label.clone(),
                    label_b: b.label.clone(),
                    snippet_a: a.body.chars().take(80).collect(),
                    snippet_b: b.body.chars().take(80).collect(),
                    shared_files,
                    shared_tags: vec![],
                    cosine,
                });
                seen.insert(key);
            }
        }
    }

    // 2. Full tag-set overlap (all tags identical, no link).
    for i in 0..infos.len() {
        for j in (i + 1)..infos.len() {
            let key = (i, j);
            if seen.contains(&key) {
                continue;
            }
            let a = &infos[i];
            let b = &infos[j];
            if a.tags.is_empty() || b.tags.is_empty() {
                continue;
            }
            let a_set: HashSet<&String> = a.tags.iter().collect();
            let b_set: HashSet<&String> = b.tags.iter().collect();
            if a_set != b_set {
                continue;
            }
            if linked_pairs.contains(&(a.uuid.clone(), b.uuid.clone())) {
                seen.insert(key);
                continue;
            }
            let cosine = score(a, b);
            if cosine.is_some_and(|c| c < threshold) {
                seen.insert(key);
                continue;
            }
            candidates.push(ConflictCandidate {
                label_a: a.label.clone(),
                label_b: b.label.clone(),
                snippet_a: a.body.chars().take(80).collect(),
                snippet_b: b.body.chars().take(80).collect(),
                shared_files: vec![],
                shared_tags: a.tags.clone(),
                cosine,
            });
            seen.insert(key);
        }
    }

    // Worst offender first. Unscorable pairs (no embedding) sort last rather
    // than masquerading as top hits. The previous order was `HashMap`
    // iteration order, i.e. reshuffled between runs.
    candidates.sort_by(|a, b| {
        let ka = a.cosine.unwrap_or(f32::NEG_INFINITY);
        let kb = b.cosine.unwrap_or(f32::NEG_INFINITY);
        kb.partial_cmp(&ka)
            .unwrap_or(std::cmp::Ordering::Equal)
            .then_with(|| a.label_a.cmp(&b.label_a))
            .then_with(|| a.label_b.cmp(&b.label_b))
    });

    let total = candidates.len();
    if let Some(n) = limit {
        candidates.truncate(n);
    }

    let items: Vec<Value> = candidates
        .iter()
        .map(|c| {
            json!({
                "label_a":      c.label_a,
                "label_b":      c.label_b,
                "snippet_a":    c.snippet_a,
                "snippet_b":    c.snippet_b,
                "shared_files": c.shared_files,
                "shared_tags":  c.shared_tags,
                "cosine":       c.cosine,
            })
        })
        .collect();

    Ok(json!({
        "conflicts": items,
        "count": items.len(),
        "total": total,
        "threshold": threshold,
        "project": project,
    }))
}

/// `sara diagnose-memories` (alias: `sara conflicts`)
pub fn run(
    conn: &Connection,
    json_output: bool,
    threshold: f32,
    project: Option<&str>,
    limit: Option<usize>,
) -> Result<()> {
    let v = diagnose_value(conn, threshold, project, limit)?;
    let count = v["count"].as_u64().unwrap_or(0);
    let total = v["total"].as_u64().unwrap_or(count);

    if json_output {
        println!("{}", serde_json::to_string_pretty(&v)?);
        return Ok(());
    }

    if count == 0 {
        println!("No unlinked conflict candidates found (cosine >= {threshold:.2}).");
        return Ok(());
    }

    let scope = project
        .map(|p| format!(" in project '{p}'"))
        .unwrap_or_default();
    println!(
        "{total} potential conflict(s){scope} — related memories (cosine >= {threshold:.2}) sharing files or tags with no link:"
    );
    if count < total {
        println!("Showing the {count} closest; pass --limit to widen.");
    }
    println!();
    if let Some(conflicts) = v["conflicts"].as_array() {
        for c in conflicts {
            let la = c["label_a"].as_str().unwrap_or("?");
            let lb = c["label_b"].as_str().unwrap_or("?");
            let sa = c["snippet_a"].as_str().unwrap_or("");
            let sb = c["snippet_b"].as_str().unwrap_or("");
            let score = c["cosine"]
                .as_f64()
                .map(|c| format!("{c:.2}"))
                .unwrap_or_else(|| "".to_string());

            let shared_files: Vec<&str> = c["shared_files"]
                .as_array()
                .map(|a| a.iter().filter_map(|x| x.as_str()).collect())
                .unwrap_or_default();
            let shared_tags: Vec<&str> = c["shared_tags"]
                .as_array()
                .map(|a| a.iter().filter_map(|x| x.as_str()).collect())
                .unwrap_or_default();

            if !shared_files.is_empty() {
                let names: Vec<&str> = shared_files
                    .iter()
                    .map(|p| {
                        std::path::Path::new(p)
                            .file_name()
                            .and_then(|n| n.to_str())
                            .unwrap_or(p)
                    })
                    .collect();
                println!("  {la}{lb}  [{score}] [file: {}]", names.join(", "));
            } else {
                println!(
                    "  {la}{lb}  [{score}] [tags: {}]",
                    shared_tags.join(", ")
                );
            }
            println!("    {la}: {sa}");
            println!("    {lb}: {sb}");
            println!();
        }
    }

    println!(
        "Resolve with: sara relearn <label> / sara learn --supersedes <label> / sara link-memory <a> similar_to <b>"
    );
    Ok(())
}

#[cfg(test)]
mod tests {
    use crate::infrastructure::{db, embedding, model::Item};
    use uuid::Uuid;

    /// Seed a memory with a file link, a tag and a real embedding, so the
    /// cosine gate is exercised rather than bypassed by the fail-open path.
    fn insert_memory_with_file(
        conn: &rusqlite::Connection,
        body: &str,
        tag: &str,
        file: &str,
    ) -> Uuid {
        let mut item = Item::new_memory(body.to_string(), body.to_string(), None);
        item.tags = vec![tag.to_string()];
        item.path = Some(String::new());
        db::insert_item(conn, &mut item).unwrap();
        db::set_item_projects(conn, &item.uuid, &["test".to_string()]).unwrap();
        db::set_item_files(conn, &item.uuid, &[file.to_string()]).unwrap();
        embedding::index_memory(conn, &item);
        item.uuid
    }

    const NEAR_A: &str =
        "dependabot bumped NSubstitute to 6.2.0 which broke dotnet restore with NU1608";
    const NEAR_B: &str =
        "the dependabot NSubstitute 6.2.0 bump broke the dotnet restore build with NU1608";
    const FAR: &str = "sourdough bread proofs best overnight in a cold refrigerator";

    #[test]
    fn near_duplicate_pair_on_same_file_is_reported() {
        let conn = db::open_in_memory_for_test();
        let file = "/tmp/sara_diag_near.rs".to_string();
        insert_memory_with_file(&conn, NEAR_A, "tag-a", &file);
        insert_memory_with_file(&conn, NEAR_B, "tag-b", &file);

        let v =
            super::diagnose_value(&conn, super::DEFAULT_CONFLICT_THRESHOLD, None, None).unwrap();
        assert_eq!(
            v["count"].as_u64().unwrap(),
            1,
            "a topically near-identical pair on one file is a real candidate"
        );
        let c = v["conflicts"][0]["cosine"].as_f64().unwrap();
        assert!(
            c >= super::DEFAULT_CONFLICT_THRESHOLD as f64,
            "reported cosine {c} must clear the threshold"
        );
    }

    #[test]
    fn unrelated_pair_on_same_file_is_not_reported() {
        let conn = db::open_in_memory_for_test();
        let file = "/tmp/sara_diag_far.rs".to_string();
        insert_memory_with_file(&conn, NEAR_A, "tag-a", &file);
        insert_memory_with_file(&conn, FAR, "tag-b", &file);

        let v =
            super::diagnose_value(&conn, super::DEFAULT_CONFLICT_THRESHOLD, None, None).unwrap();
        assert_eq!(
            v["count"].as_u64().unwrap(),
            0,
            "sharing a file is co-occurrence, not conflict — the cosine gate must drop this"
        );
    }

    #[test]
    fn pair_without_embeddings_is_kept_fail_open() {
        let conn = db::open_in_memory_for_test();
        let file = "/tmp/sara_diag_noemb.rs".to_string();
        let a = insert_memory_with_file(&conn, NEAR_A, "tag-a", &file);
        let b = insert_memory_with_file(&conn, FAR, "tag-b", &file);
        // Simulate a lagging index: drop both embedding rows.
        for u in [a, b] {
            db::delete_embedding(&conn, &u.to_string()).unwrap();
        }

        let v =
            super::diagnose_value(&conn, super::DEFAULT_CONFLICT_THRESHOLD, None, None).unwrap();
        assert_eq!(
            v["count"].as_u64().unwrap(),
            1,
            "an unscorable pair must be kept, never silently hidden"
        );
        assert!(v["conflicts"][0]["cosine"].is_null());
    }

    #[test]
    fn candidates_are_sorted_by_cosine_descending() {
        let conn = db::open_in_memory_for_test();
        let file = "/tmp/sara_diag_sort.rs".to_string();
        insert_memory_with_file(&conn, NEAR_A, "tag-a", &file);
        insert_memory_with_file(&conn, NEAR_B, "tag-b", &file);
        insert_memory_with_file(
            &conn,
            "NSubstitute version pinning is handled in Directory.Packages.props",
            "tag-c",
            &file,
        );

        // Threshold 0 keeps every pair so the ordering itself is under test.
        let v = super::diagnose_value(&conn, 0.0, None, None).unwrap();
        let scores: Vec<f64> = v["conflicts"]
            .as_array()
            .unwrap()
            .iter()
            .map(|c| c["cosine"].as_f64().unwrap())
            .collect();
        assert!(scores.len() >= 2, "expected several pairs to order");
        assert!(
            scores.windows(2).all(|w| w[0] >= w[1]),
            "worst offender must come first, got {scores:?}"
        );
    }

    #[test]
    fn limit_truncates_but_total_reports_the_full_count() {
        let conn = db::open_in_memory_for_test();
        let file = "/tmp/sara_diag_limit.rs".to_string();
        insert_memory_with_file(&conn, NEAR_A, "tag-a", &file);
        insert_memory_with_file(&conn, NEAR_B, "tag-b", &file);
        insert_memory_with_file(&conn, FAR, "tag-c", &file);

        let v = super::diagnose_value(&conn, 0.0, None, Some(1)).unwrap();
        assert_eq!(v["count"].as_u64().unwrap(), 1);
        assert!(
            v["total"].as_u64().unwrap() > 1,
            "total must report the untruncated count"
        );
    }

    #[test]
    fn project_scope_excludes_other_provinces() {
        let conn = db::open_in_memory_for_test();
        let file = "/tmp/sara_diag_scope.rs".to_string();
        let a = insert_memory_with_file(&conn, NEAR_A, "tag-a", &file);
        insert_memory_with_file(&conn, NEAR_B, "tag-b", &file);
        // Move one side of the pair into a different project.
        db::set_item_projects(&conn, &a, &["elsewhere".to_string()]).unwrap();

        let scoped =
            super::diagnose_value(&conn, super::DEFAULT_CONFLICT_THRESHOLD, Some("test"), None)
                .unwrap();
        assert_eq!(
            scoped["count"].as_u64().unwrap(),
            0,
            "a pair straddling two projects must not surface in either scope"
        );

        let unscoped =
            super::diagnose_value(&conn, super::DEFAULT_CONFLICT_THRESHOLD, None, None).unwrap();
        assert_eq!(unscoped["count"].as_u64().unwrap(), 1);
    }

    #[test]
    fn linked_memories_do_not_appear_in_diagnose() {
        let conn = db::open_in_memory_for_test();
        let file = "/tmp/sara_diag_linked_test.rs".to_string();
        let uuid_a = insert_memory_with_file(&conn, NEAR_A, "tag-c", &file);
        let uuid_b = insert_memory_with_file(&conn, NEAR_B, "tag-d", &file);

        // Link them — they should disappear from diagnose output.
        db::insert_memory_link(
            &conn,
            &uuid_a.to_string(),
            &uuid_b.to_string(),
            "supersedes",
            1.0,
        )
        .unwrap();

        let v =
            super::diagnose_value(&conn, super::DEFAULT_CONFLICT_THRESHOLD, None, None).unwrap();
        assert_eq!(v["count"].as_u64().unwrap(), 0);
    }
}