lean-ctx-engine 3.12.3

LeanCTX | OSS library: the open-source runtime for the Context Gateway for AI agents behind the lean-ctx CLI, MCP server and proxy 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
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::path::PathBuf;

/// Average relations per current fact the graph may hold before its weakest
/// edges are pruned. Knowledge has no fact limit; this bounds edge *density*
/// (an edge-producing loop cannot grow the graph faster than the facts it
/// links), so the bound grows with the store instead of capping it.
pub const EDGES_PER_FACT: usize = 8;
/// Floor of [`edge_bound`], so a young store keeps room for relations (the
/// bound the old default of 200 facts gave).
pub const MIN_EDGE_BOUND: usize = 1_600;

/// The relation-edge bound for a store with `current_facts` current facts.
pub fn edge_bound(current_facts: usize) -> usize {
    current_facts
        .saturating_mul(EDGES_PER_FACT)
        .max(MIN_EDGE_BOUND)
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub struct KnowledgeNodeRef {
    pub category: String,
    pub key: String,
}

impl KnowledgeNodeRef {
    pub fn new(category: &str, key: &str) -> Self {
        Self {
            category: category.trim().to_string(),
            key: key.trim().to_string(),
        }
    }

    pub fn id(&self) -> String {
        format!("{}/{}", self.category, self.key)
    }
}

/// The kinds this store writes. Every released reader of `relations.json`
/// deserializes the kind strictly, so a new kind is added only once the
/// oldest supported reader parses it; kinds a newer writer stored are kept
/// as written (see [`KnowledgeRelationGraph::load`]).
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[serde(rename_all = "snake_case")]
pub enum KnowledgeEdgeKind {
    DependsOn,
    RelatedTo,
    Supports,
    Contradicts,
    Supersedes,
}

impl KnowledgeEdgeKind {
    pub fn parse(input: &str) -> Option<Self> {
        match input.trim().to_lowercase().as_str() {
            "depends_on" | "depends" => Some(Self::DependsOn),
            "related_to" | "related" => Some(Self::RelatedTo),
            "supports" | "support" => Some(Self::Supports),
            "contradicts" | "contradict" => Some(Self::Contradicts),
            "supersedes" | "supersede" => Some(Self::Supersedes),
            _ => None,
        }
    }

    pub fn as_str(&self) -> &'static str {
        match self {
            KnowledgeEdgeKind::DependsOn => "depends_on",
            KnowledgeEdgeKind::RelatedTo => "related_to",
            KnowledgeEdgeKind::Supports => "supports",
            KnowledgeEdgeKind::Contradicts => "contradicts",
            KnowledgeEdgeKind::Supersedes => "supersedes",
        }
    }
}

fn default_strength() -> f64 {
    0.5
}
fn default_decay_rate() -> f64 {
    0.02
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KnowledgeEdge {
    pub from: KnowledgeNodeRef,
    pub to: KnowledgeNodeRef,
    pub kind: KnowledgeEdgeKind,
    pub created_at: DateTime<Utc>,
    #[serde(default)]
    pub last_seen: Option<DateTime<Utc>>,
    #[serde(default)]
    pub count: u32,
    pub source_session: String,
    #[serde(default = "default_strength")]
    pub strength: f64,
    #[serde(default = "default_decay_rate")]
    pub decay_rate: f64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct KnowledgeRelationGraph {
    pub project_hash: String,
    pub edges: Vec<KnowledgeEdge>,
    pub updated_at: DateTime<Utc>,
    /// Ephemeral incident-edge lookup rebuilt after JSON load and mutations.
    /// Skipping it preserves the existing relations.json format.
    #[serde(skip)]
    pub(crate) edge_positions: BTreeMap<String, Vec<usize>>,
    /// Edges this build cannot read (a kind or shape a newer writer stored),
    /// kept as written and saved back unchanged, so no reader drops what it
    /// does not understand.
    #[serde(skip)]
    foreign_edges: Vec<serde_json::Value>,
    /// The file exists but does not parse: [`Self::save`] refuses, so a
    /// writer never replaces relations it could not read with an empty set.
    #[serde(skip)]
    unreadable: bool,
}

impl Default for KnowledgeRelationGraph {
    fn default() -> Self {
        Self {
            project_hash: String::new(),
            edges: Vec::new(),
            updated_at: Utc::now(),
            edge_positions: BTreeMap::new(),
            foreign_edges: Vec::new(),
            unreadable: false,
        }
    }
}

impl KnowledgeRelationGraph {
    pub fn new(project_hash: &str) -> Self {
        Self {
            project_hash: project_hash.to_string(),
            edges: Vec::new(),
            updated_at: Utc::now(),
            edge_positions: BTreeMap::new(),
            foreign_edges: Vec::new(),
            unreadable: false,
        }
    }

    pub fn path(project_hash: &str) -> Result<PathBuf, String> {
        let dir = crate::core::data_dir::lean_ctx_data_dir()?
            .join("knowledge")
            .join(project_hash);
        Ok(dir.join("relations.json"))
    }

    pub fn load(project_hash: &str) -> Option<Self> {
        Self::load_checked(project_hash).ok().flatten()
    }

    /// `Ok(None)` when there is no file, `Err` when it exists but cannot be
    /// read or parsed.
    fn load_checked(project_hash: &str) -> Result<Option<Self>, String> {
        let path = Self::path(project_hash)?;
        let content = match std::fs::read_to_string(&path) {
            Ok(content) => content,
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None),
            Err(e) => return Err(format!("{}: {e}", path.display())),
        };
        let mut g = Self::from_json(&content)
            .ok_or_else(|| format!("{} does not parse", path.display()))?;
        if g.project_hash.trim().is_empty() {
            g.project_hash = project_hash.to_string();
        }
        g.rebuild_index();
        Ok(Some(g))
    }

    /// Parse a stored graph edge by edge: an edge this build cannot read is
    /// kept in [`Self::foreign_edges`] instead of failing the whole file.
    fn from_json(content: &str) -> Option<Self> {
        let mut doc: serde_json::Value = serde_json::from_str(content).ok()?;
        let raw = doc.as_object_mut()?.remove("edges");
        let mut g: Self = serde_json::from_value(doc).ok()?;
        match raw {
            None | Some(serde_json::Value::Null) => {}
            Some(serde_json::Value::Array(raw)) => {
                for edge in raw {
                    match serde_json::from_value::<KnowledgeEdge>(edge.clone()) {
                        Ok(edge) => g.edges.push(edge),
                        Err(_) => g.foreign_edges.push(edge),
                    }
                }
            }
            Some(_) => return None,
        }
        Some(g)
    }

    pub fn load_or_create(project_hash: &str) -> Self {
        match Self::load_checked(project_hash) {
            Ok(Some(g)) => g,
            Ok(None) => Self::new(project_hash),
            Err(error) => {
                tracing::warn!("[knowledge] relations not loaded, kept as they are: {error}");
                let mut g = Self::new(project_hash);
                g.unreadable = true;
                g
            }
        }
    }

    /// Load, change and save the graph under the project's knowledge lock,
    /// the in-process and cross-process lock fact edits and the cognition loop
    /// hold, so a concurrent writer (another process, the dashboard, the
    /// cognition loop) never loses an edge. `change` returns `None` when it
    /// changed nothing; the graph is then not written. Must not be called while
    /// that lock is held.
    pub fn mutate_locked<T>(
        project_root: &str,
        project_hash: &str,
        change: impl FnOnce(&mut Self) -> Option<T>,
    ) -> Result<Option<T>, String> {
        crate::core::knowledge::ProjectKnowledge::with_project_lock_checked(project_root, || {
            let mut graph = Self::load_or_create(project_hash);
            match change(&mut graph) {
                Some(output) => graph.save().map(|()| Some(output)),
                None => Ok(None),
            }
        })?
    }

    pub fn save(&mut self) -> Result<(), String> {
        if self.unreadable {
            return Err(
                "relations.json could not be read; it is not overwritten (fix or remove it)"
                    .to_string(),
            );
        }
        let path = Self::path(&self.project_hash)?;
        if let Some(dir) = path.parent() {
            std::fs::create_dir_all(dir).map_err(|e| e.to_string())?;
        }

        self.updated_at = Utc::now();
        self.edges.sort_by(|a, b| {
            a.from
                .category
                .cmp(&b.from.category)
                .then_with(|| a.from.key.cmp(&b.from.key))
                .then_with(|| a.kind.as_str().cmp(b.kind.as_str()))
                .then_with(|| a.to.category.cmp(&b.to.category))
                .then_with(|| a.to.key.cmp(&b.to.key))
                .then_with(|| b.count.cmp(&a.count))
                .then_with(|| b.last_seen.cmp(&a.last_seen))
                .then_with(|| b.created_at.cmp(&a.created_at))
        });
        self.rebuild_index();

        let json = if self.foreign_edges.is_empty() {
            serde_json::to_string_pretty(self)
        } else {
            // Foreign edges follow the readable ones, in the order they were read.
            serde_json::to_value(&*self).and_then(|mut doc| {
                if let Some(serde_json::Value::Array(edges)) = doc.get_mut("edges") {
                    edges.extend(self.foreign_edges.iter().cloned());
                }
                serde_json::to_string_pretty(&doc)
            })
        }
        .map_err(|e| e.to_string())?;
        // `load` treats an unparsable file as absent; a torn write must not
        // turn into an empty graph that the next save persists.
        crate::core::atomic_fs::try_atomic_write(&path, json.as_bytes(), None)
            .map_err(|e| e.to_string())
    }

    pub fn upsert_edge(
        &mut self,
        from: KnowledgeNodeRef,
        to: KnowledgeNodeRef,
        kind: KnowledgeEdgeKind,
        session_id: &str,
    ) -> bool {
        let now = Utc::now();
        if let Some(e) = self
            .edges
            .iter_mut()
            .find(|e| e.from == from && e.to == to && e.kind == kind)
        {
            e.count = e.count.saturating_add(1).max(1);
            e.last_seen = Some(now);
            e.source_session = session_id.to_string();
            e.strength = (e.strength + 0.1 * (1.0 - e.strength)).min(1.0);
            self.updated_at = now;
            return false;
        }

        self.edges.push(KnowledgeEdge {
            from,
            to,
            kind,
            created_at: now,
            last_seen: Some(now),
            count: 1,
            source_session: session_id.to_string(),
            strength: default_strength(),
            decay_rate: default_decay_rate(),
        });
        self.rebuild_index();
        self.updated_at = now;
        true
    }

    pub fn remove_edge(
        &mut self,
        from: &KnowledgeNodeRef,
        to: &KnowledgeNodeRef,
        kind: Option<KnowledgeEdgeKind>,
    ) -> usize {
        let before = self.edges.len();
        self.edges.retain(|e| {
            if &e.from != from || &e.to != to {
                return true;
            }
            if let Some(k) = kind {
                e.kind != k
            } else {
                false
            }
        });
        let removed = before.saturating_sub(self.edges.len());
        if removed > 0 {
            self.rebuild_index();
        }
        removed
    }

    pub fn enforce_cap(&mut self, max_edges: usize) -> bool {
        if max_edges == 0 || self.edges.len() <= max_edges {
            return false;
        }

        self.edges.sort_by(|a, b| {
            b.count
                .cmp(&a.count)
                .then_with(|| b.last_seen.cmp(&a.last_seen))
                .then_with(|| b.created_at.cmp(&a.created_at))
                .then_with(|| a.from.category.cmp(&b.from.category))
                .then_with(|| a.from.key.cmp(&b.from.key))
                .then_with(|| a.kind.as_str().cmp(b.kind.as_str()))
                .then_with(|| a.to.category.cmp(&b.to.category))
                .then_with(|| a.to.key.cmp(&b.to.key))
        });

        self.edges.truncate(max_edges);
        self.rebuild_index();
        true
    }

    /// Hebbian strengthening: saturating formula so strength approaches but never exceeds 1.0
    pub fn strengthen_edge(
        &mut self,
        from: &KnowledgeNodeRef,
        to: &KnowledgeNodeRef,
        amount: f64,
    ) -> bool {
        if let Some(e) = self
            .edges
            .iter_mut()
            .find(|e| &e.from == from && &e.to == to)
        {
            e.strength = (e.strength + amount * (1.0 - e.strength)).min(1.0);
            e.last_seen = Some(Utc::now());
            e.count = e.count.saturating_add(1);
            return true;
        }
        if let Some(e) = self
            .edges
            .iter_mut()
            .find(|e| &e.from == to && &e.to == from)
        {
            e.strength = (e.strength + amount * (1.0 - e.strength)).min(1.0);
            e.last_seen = Some(Utc::now());
            e.count = e.count.saturating_add(1);
            return true;
        }
        false
    }

    /// Time-based exponential decay on all edge strengths
    pub fn decay_all_edges(&mut self, days_elapsed: f64) {
        for e in &mut self.edges {
            e.strength *= (1.0 - e.decay_rate).powf(days_elapsed);
            e.strength = e.strength.max(0.0);
        }
    }

    /// Remove edges whose strength has fallen below `threshold`
    pub fn prune_weak_edges(&mut self, threshold: f64) -> usize {
        let before = self.edges.len();
        self.edges.retain(|e| e.strength >= threshold);
        let removed = before - self.edges.len();
        if removed > 0 {
            self.rebuild_index();
        }
        removed
    }

    /// Rebuilds the in-memory incident-edge index from persisted edge positions.
    pub(crate) fn rebuild_index(&mut self) {
        self.edge_positions.clear();
        for (position, edge) in self.edges.iter().enumerate() {
            self.edge_positions
                .entry(edge.from.id())
                .or_default()
                .push(position);
            if edge.to != edge.from {
                self.edge_positions
                    .entry(edge.to.id())
                    .or_default()
                    .push(position);
            }
        }
    }

    /// Returns only the edges incident to `node`, using the load-time index.
    pub fn incident_edges(&self, node: &KnowledgeNodeRef) -> Vec<&KnowledgeEdge> {
        self.edge_positions
            .get(&node.id())
            .into_iter()
            .flatten()
            .filter_map(|&position| self.edges.get(position))
            .collect()
    }
}

pub fn parse_node_ref(input: &str) -> Option<KnowledgeNodeRef> {
    let s = input.trim();
    if s.is_empty() {
        return None;
    }

    if let Some((cat, key)) = s.split_once('/') {
        let cat = cat.trim();
        let key = key.trim();
        if !cat.is_empty() && !key.is_empty() {
            return Some(KnowledgeNodeRef::new(cat, key));
        }
    }
    if let Some((cat, key)) = s.split_once(':') {
        let cat = cat.trim();
        let key = key.trim();
        if !cat.is_empty() && !key.is_empty() {
            return Some(KnowledgeNodeRef::new(cat, key));
        }
    }

    None
}

pub fn format_mermaid(edges: &[KnowledgeEdge]) -> String {
    if edges.is_empty() {
        return "graph TD\n  %% no relations".to_string();
    }

    fn id_for(n: &KnowledgeNodeRef) -> String {
        let mut out = String::from("K_");
        for ch in n.id().chars() {
            if ch.is_ascii_alphanumeric() {
                out.push(ch);
            } else {
                out.push('_');
            }
        }
        out
    }

    let mut lines = Vec::new();
    lines.push("graph TD".to_string());
    for e in edges {
        let from = id_for(&e.from);
        let to = id_for(&e.to);
        let from_label = e.from.id();
        let to_label = e.to.id();
        lines.push(format!(
            "  {from}[\"{from_label}\"] -->|{}| {to}[\"{to_label}\"]",
            e.kind.as_str()
        ));
    }
    lines.join("\n")
}

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

    /// Parallel writers each add their own edge; under the knowledge lock no
    /// read-modify-write drops another writer's edge.
    #[test]
    fn concurrent_locked_writers_keep_every_edge() {
        let _data = crate::core::data_dir::isolated_data_dir();
        let project = tempfile::tempdir().unwrap();
        let root = project.path().to_string_lossy().into_owned();
        let hash = crate::core::project_hash::hash_project_root(&root);
        // Six writers race as well as sixteen did, and stay inside the lock's
        // 10 s wait on a loaded CI host (sixteen serialized fsyncs did not).
        const WRITERS: usize = 6;
        std::thread::scope(|s| {
            for i in 0..WRITERS {
                let (root, hash) = (&root, &hash);
                s.spawn(move || {
                    KnowledgeRelationGraph::mutate_locked(root, hash, |graph| {
                        graph.upsert_edge(
                            KnowledgeNodeRef::new("a", &format!("{i}")),
                            KnowledgeNodeRef::new("b", "target"),
                            KnowledgeEdgeKind::RelatedTo,
                            "s1",
                        );
                        Some(())
                    })
                    .expect("locked write");
                });
            }
        });
        let graph = KnowledgeRelationGraph::load(&hash).expect("saved graph");
        assert_eq!(
            graph.edges.len(),
            WRITERS,
            "an edge was lost to a concurrent write"
        );

        // A change that changes nothing writes nothing.
        let untouched = KnowledgeRelationGraph::mutate_locked(&root, &hash, |_| None::<()>);
        assert_eq!(untouched, Ok(None));
    }

    #[test]
    fn strengthen_edge_saturating() {
        let mut graph = KnowledgeRelationGraph::new("test");
        let from = KnowledgeNodeRef::new("a", "1");
        let to = KnowledgeNodeRef::new("b", "2");
        graph.upsert_edge(from.clone(), to.clone(), KnowledgeEdgeKind::RelatedTo, "s1");

        let initial = graph.edges[0].strength;
        assert!((initial - 0.5).abs() < 0.01);

        graph.strengthen_edge(&from, &to, 0.3);
        assert!(graph.edges[0].strength > initial);
        assert!(graph.edges[0].strength <= 1.0);

        for _ in 0..100 {
            graph.strengthen_edge(&from, &to, 0.5);
        }
        assert!(graph.edges[0].strength <= 1.0);
        assert!(graph.edges[0].strength > 0.99);
    }

    #[test]
    fn decay_reduces_strength() {
        let mut graph = KnowledgeRelationGraph::new("test");
        let from = KnowledgeNodeRef::new("a", "1");
        let to = KnowledgeNodeRef::new("b", "2");
        graph.upsert_edge(from, to, KnowledgeEdgeKind::RelatedTo, "s1");

        let initial = graph.edges[0].strength;
        graph.decay_all_edges(10.0);
        assert!(graph.edges[0].strength < initial);
        assert!(graph.edges[0].strength > 0.0);
    }

    #[test]
    fn prune_weak_edges_removes_below_threshold() {
        let mut graph = KnowledgeRelationGraph::new("test");
        graph.upsert_edge(
            KnowledgeNodeRef::new("a", "1"),
            KnowledgeNodeRef::new("b", "2"),
            KnowledgeEdgeKind::RelatedTo,
            "s1",
        );
        graph.upsert_edge(
            KnowledgeNodeRef::new("c", "3"),
            KnowledgeNodeRef::new("d", "4"),
            KnowledgeEdgeKind::RelatedTo,
            "s2",
        );

        graph.edges[1].strength = 0.01;

        let removed = graph.prune_weak_edges(0.05);
        assert_eq!(removed, 1);
        assert_eq!(graph.edges.len(), 1);
    }

    #[test]
    fn backward_compatible_edge_deserialization() {
        let json = r#"{
            "from": {"category": "a", "key": "1"},
            "to": {"category": "b", "key": "2"},
            "kind": "related_to",
            "created_at": "2024-01-01T00:00:00Z",
            "count": 1,
            "source_session": "s1"
        }"#;
        let edge: KnowledgeEdge = serde_json::from_str(json).unwrap();
        assert!((edge.strength - 0.5).abs() < 0.01);
        assert!((edge.decay_rate - 0.02).abs() < 0.001);
    }

    /// A file holding an old-shape edge and an edge of a kind this build does
    /// not know survives load → change → save: the old edge is read, the
    /// unknown one is written back as it was, and a second save of the
    /// same graph writes the same edges.
    #[test]
    fn unknown_kinds_survive_a_write() {
        let _isolated = crate::core::data_dir::isolated_data_dir();
        let project = tempfile::tempdir().unwrap();
        let root = project.path().to_string_lossy().into_owned();
        let hash = crate::core::project_hash::hash_project_root(&root);
        let future = serde_json::json!({
            "from": {"category": "a", "key": "1"},
            "to": {"category": "c", "key": "3"},
            "kind": "answers",
            "created_at": "2026-11-01T00:00:00Z",
            "count": 2,
            "source_session": "newer-build"
        });
        let doc = serde_json::json!({
            "project_hash": hash,
            "edges": [
                {"from": {"category": "a", "key": "1"}, "to": {"category": "b", "key": "2"},
                 "kind": "related_to", "created_at": "2024-01-01T00:00:00Z",
                 "count": 1, "source_session": "s1"},
                future
            ],
            "updated_at": "2024-01-01T00:00:00Z"
        });
        let path = KnowledgeRelationGraph::path(&hash).unwrap();
        std::fs::create_dir_all(path.parent().unwrap()).unwrap();
        std::fs::write(&path, doc.to_string()).unwrap();

        KnowledgeRelationGraph::mutate_locked(&root, &hash, |g| {
            assert_eq!(g.edges.len(), 1, "the old-shape edge is read");
            g.upsert_edge(
                KnowledgeNodeRef::new("a", "1"),
                KnowledgeNodeRef::new("d", "4"),
                KnowledgeEdgeKind::DependsOn,
                "s2",
            );
            Some(())
        })
        .expect("write");
        let edges_on_disk = || -> Vec<serde_json::Value> {
            let saved: serde_json::Value =
                serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
            saved["edges"].as_array().unwrap().clone()
        };
        let first = edges_on_disk();
        assert_eq!(first.len(), 3);
        assert_eq!(first.last(), Some(&future), "kept exactly as written");

        let mut again = KnowledgeRelationGraph::load(&hash).unwrap();
        again.save().unwrap();
        assert_eq!(edges_on_disk(), first, "same graph, same edges on disk");
    }

    /// A file that does not parse is never replaced by an empty graph.
    #[test]
    fn an_unreadable_file_is_not_overwritten() {
        let _isolated = crate::core::data_dir::isolated_data_dir();
        let project = tempfile::tempdir().unwrap();
        let root = project.path().to_string_lossy().into_owned();
        let hash = crate::core::project_hash::hash_project_root(&root);
        let path = KnowledgeRelationGraph::path(&hash).unwrap();
        std::fs::create_dir_all(path.parent().unwrap()).unwrap();
        std::fs::write(&path, "{\"edges\": [ torn").unwrap();

        let written = KnowledgeRelationGraph::mutate_locked(&root, &hash, |g| {
            g.upsert_edge(
                KnowledgeNodeRef::new("a", "1"),
                KnowledgeNodeRef::new("b", "2"),
                KnowledgeEdgeKind::DependsOn,
                "s1",
            );
            Some(())
        });
        assert!(written.is_err(), "{written:?}");
        assert_eq!(
            std::fs::read_to_string(&path).unwrap(),
            "{\"edges\": [ torn"
        );
    }

    #[test]
    fn load_rebuilds_ephemeral_index_without_changing_json() {
        let _isolated = crate::core::data_dir::isolated_data_dir();
        let project_hash = "knowledge-relations-index";
        let from = KnowledgeNodeRef::new("architecture", "storage");
        let to = KnowledgeNodeRef::new("dependency", "sqlite");
        let mut graph = KnowledgeRelationGraph::new(project_hash);
        graph.upsert_edge(
            from.clone(),
            to,
            KnowledgeEdgeKind::DependsOn,
            "test-session",
        );
        graph.save().unwrap();

        let loaded = KnowledgeRelationGraph::load(project_hash).expect("saved graph should load");
        assert_eq!(loaded.edge_positions.get(&from.id()).map(Vec::len), Some(1));
        assert_eq!(loaded.incident_edges(&from).len(), 1);

        let json = serde_json::to_string(&loaded).unwrap();
        assert!(!json.contains("\"edge_positions\""));
    }

    #[test]
    fn strengthen_edge_bidirectional() {
        let mut graph = KnowledgeRelationGraph::new("test");
        let from = KnowledgeNodeRef::new("a", "1");
        let to = KnowledgeNodeRef::new("b", "2");
        graph.upsert_edge(from.clone(), to.clone(), KnowledgeEdgeKind::RelatedTo, "s1");

        let found = graph.strengthen_edge(&to, &from, 0.2);
        assert!(found);
        assert!(graph.edges[0].strength > 0.5);
    }
}