retro-core 3.1.0

Core library for retro, the active context curator for AI coding agents
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
//! v3 analysis: reuse the v2 engine (compact sessions, graph prompt, schema,
//! response parsing) and apply the resulting GraphOperations to the markdown
//! store instead of SQLite.

use chrono::Utc;

use crate::analysis::backend::AnalysisBackend;
use crate::analysis::{GRAPH_ANALYSIS_RESPONSE_SCHEMA, parse_graph_response_full, prompts};
use crate::errors::CoreError;
use crate::models::{
    EdgeType, GraphOperation, KnowledgeNode, NodeScope, NodeType as V2NodeType, Session,
};
use crate::store::{Node, NodeType, Scope, Store, is_valid_slug};
use crate::util::truncate_str;

/// Ceiling on a created node's body. Nothing legitimate is this large — the
/// prompt shows existing nodes truncated to 200 chars, so a giant create is
/// model garbage, not knowledge.
const MAX_CREATE_CONTENT_CHARS: usize = 10_000;

/// Result of one v3 analysis batch.
#[derive(Debug, Default)]
pub struct V3AnalyzeResult {
    pub sessions_analyzed: usize,
    pub nodes_created: usize,
    pub nodes_updated: usize,
    pub nodes_merged: usize,
    pub nodes_invalidated: usize,
    pub edges_ignored: usize,
    pub ops_skipped: usize,
    /// Short reasons for skipped operations (for health/summary display).
    pub skipped: Vec<String>,
    pub reasoning: String,
    pub input_tokens: u64,
    pub output_tokens: u64,
    /// Bodies of nodes created/updated — for briefing notifications.
    pub learned: Vec<String>,
}

impl V3AnalyzeResult {
    fn skip(&mut self, reason: String) {
        self.ops_skipped += 1;
        self.skipped.push(reason);
    }
}

/// Shim: present a v3 store node to the v2 prompt builder. Only id, content,
/// confidence, type, and scope influence the prompt (content truncated to
/// 200 chars there).
fn shim(node: &Node) -> KnowledgeNode {
    KnowledgeNode {
        id: node.id.clone(),
        node_type: match node.node_type {
            NodeType::Rule => V2NodeType::Rule,
            NodeType::Preference => V2NodeType::Preference,
            NodeType::Pattern => V2NodeType::Pattern,
            NodeType::Memory => V2NodeType::Memory,
        },
        scope: match &node.scope {
            Scope::Global => NodeScope::Global,
            Scope::Project(_) => NodeScope::Project,
        },
        content: node.body.clone(),
        confidence: node.confidence,
    }
}

fn v3_node_type(t: &V2NodeType) -> NodeType {
    match t {
        V2NodeType::Rule | V2NodeType::Directive => NodeType::Rule,
        V2NodeType::Preference => NodeType::Preference,
        V2NodeType::Pattern | V2NodeType::Skill => NodeType::Pattern,
        V2NodeType::Memory => NodeType::Memory,
    }
}

fn union_sources(existing: &mut Vec<String>, extra: &[String]) {
    for s in extra {
        if !existing.contains(s) {
            existing.push(s.clone());
        }
    }
}

/// Analyze one batch of parsed sessions against the store and apply the
/// resulting operations. `project_slug` scopes project-level operations.
/// Caller is responsible for: session filtering by project, scrubbing,
/// budget accounting (one backend call per invocation), and committing.
pub fn analyze_sessions(
    store: &Store,
    backend: &dyn AnalysisBackend,
    sessions: &[Session],
    project_slug: Option<&str>,
) -> Result<V3AnalyzeResult, CoreError> {
    let mut result = V3AnalyzeResult::default();

    // Low-signal filter (same rule as v2: < 2 user messages = skip).
    let signal: Vec<&Session> = sessions
        .iter()
        .filter(|s| s.user_messages.len() >= 2)
        .collect();
    if signal.is_empty() {
        return Ok(result);
    }
    result.sessions_analyzed = signal.len();
    let session_sources: Vec<String> = signal
        .iter()
        .map(|s| format!("session:{}", s.session_id))
        .collect();

    // Existing-node context: active nodes for global + this project's scope.
    let loaded = store.load_all()?;
    let mut active: Vec<&Node> = loaded
        .nodes
        .iter()
        .map(|(_, n)| n)
        .filter(|n| n.is_active())
        .filter(|n| match (&n.scope, project_slug) {
            (Scope::Global, _) => true,
            (Scope::Project(slug), Some(p)) => slug == p,
            (Scope::Project(_), None) => false,
        })
        .collect();
    // Highest-value nodes first: the prompt builder caps at 50 nodes, so sort
    // by confidence desc, recency desc, id asc (stable tiebreak) to keep the
    // most important nodes visible and prevent duplicate re-creation.
    active.sort_by(|a, b| {
        b.confidence
            .partial_cmp(&a.confidence)
            .unwrap_or(std::cmp::Ordering::Equal)
            .then_with(|| b.updated.cmp(&a.updated))
            .then_with(|| a.id.cmp(&b.id))
    });
    let context: Vec<KnowledgeNode> = active.into_iter().map(shim).collect();

    let compact: Vec<_> = signal
        .iter()
        .map(|s| prompts::to_compact_session(s))
        .collect();
    let prompt = prompts::build_graph_analysis_prompt(&compact, &context, project_slug);
    let response = backend.execute(&prompt, Some(GRAPH_ANALYSIS_RESPONSE_SCHEMA))?;
    result.input_tokens = response.input_tokens;
    result.output_tokens = response.output_tokens;

    let (reasoning, operations) = parse_graph_response_full(&response.text, project_slug)?;
    result.reasoning = reasoning;
    let today = Utc::now().date_naive();

    for op in operations {
        match op {
            GraphOperation::CreateNode {
                node_type,
                scope,
                project_id,
                content,
                confidence,
            } => {
                // LLM output is untrusted: reject junk content before any write.
                let body = content.trim().to_string();
                if body.is_empty() {
                    result.skip("create_node: empty content".to_string());
                    continue;
                }
                if body.chars().count() > MAX_CREATE_CONTENT_CHARS {
                    result.skip(format!(
                        "create_node: oversize content ({} chars)",
                        body.chars().count()
                    ));
                    continue;
                }
                let v3_scope = match scope {
                    NodeScope::Global => Scope::Global,
                    NodeScope::Project => {
                        match project_id.or_else(|| project_slug.map(String::from)) {
                            Some(slug) if is_valid_slug(&slug) => Scope::Project(slug),
                            Some(slug) => {
                                result.skip(format!(
                                    "create_node: invalid project id {:?}",
                                    truncate_str(&slug, 60)
                                ));
                                continue;
                            }
                            None => {
                                result.skip(
                                    "create_node: project-scoped create with no project"
                                        .to_string(),
                                );
                                continue;
                            }
                        }
                    }
                };
                let id = store.unique_slug(
                    &body
                        .split_whitespace()
                        .take(8)
                        .collect::<Vec<_>>()
                        .join(" "),
                    &v3_scope,
                );
                let node = Node {
                    id,
                    scope: v3_scope,
                    node_type: v3_node_type(&node_type),
                    confidence: confidence.clamp(0.0, 1.0),
                    sources: session_sources.clone(),
                    created: today,
                    updated: today,
                    invalidated_by: None,
                    body,
                };
                store.write_node(&node)?;
                result.learned.push(node.body.clone());
                result.nodes_created += 1;
            }
            GraphOperation::UpdateNode {
                id,
                confidence,
                content,
            } => {
                if !is_valid_slug(&id) {
                    result.skip(format!(
                        "update_node: invalid id {:?}",
                        truncate_str(&id, 60)
                    ));
                    continue;
                }
                let Some((scope, mut node)) = find_node(store, &id, project_slug)? else {
                    result.skip(format!(
                        "update_node: unknown id {:?}",
                        truncate_str(&id, 60)
                    ));
                    continue;
                };
                if let Some(c) = confidence {
                    node.confidence = c.clamp(0.0, 1.0);
                }
                let body_changed = content.is_some();
                if let Some(c) = content {
                    node.body = c.trim().to_string();
                }
                union_sources(&mut node.sources, &session_sources);
                node.updated = today;
                node.scope = scope;
                store.write_node(&node)?;
                if body_changed {
                    result.learned.push(node.body.clone());
                }
                result.nodes_updated += 1;
            }
            GraphOperation::MergeNodes { keep_id, remove_id } => {
                if !is_valid_slug(&keep_id) || !is_valid_slug(&remove_id) {
                    result.skip(format!(
                        "merge_nodes: invalid id {:?} / {:?}",
                        truncate_str(&keep_id, 60),
                        truncate_str(&remove_id, 60)
                    ));
                    continue;
                }
                if keep_id == remove_id {
                    result.skip(format!(
                        "merge_nodes: self-merge {:?}",
                        truncate_str(&keep_id, 60)
                    ));
                    continue;
                }
                let keep = find_node(store, &keep_id, project_slug)?;
                let removed = find_node(store, &remove_id, project_slug)?;
                let (Some((keep_scope, mut keep_node)), Some((remove_scope, remove_node))) =
                    (keep, removed)
                else {
                    result.skip(format!(
                        "merge_nodes: unknown id(s) {:?} / {:?}",
                        truncate_str(&keep_id, 60),
                        truncate_str(&remove_id, 60)
                    ));
                    continue;
                };
                union_sources(&mut keep_node.sources, &remove_node.sources);
                union_sources(&mut keep_node.sources, &session_sources);
                keep_node.updated = today;
                keep_node.scope = keep_scope;
                store.write_node(&keep_node)?;
                store.invalidate(&remove_scope, &remove_node.id, &keep_node.id)?;
                result.nodes_merged += 1;
            }
            GraphOperation::CreateEdge {
                source_id,
                target_id,
                edge_type,
            } => {
                if edge_type == EdgeType::Supersedes {
                    if !is_valid_slug(&source_id) || !is_valid_slug(&target_id) {
                        result.skip(format!(
                            "supersedes: invalid id {:?} / {:?}",
                            truncate_str(&source_id, 60),
                            truncate_str(&target_id, 60)
                        ));
                        continue;
                    }
                    if source_id == target_id {
                        result.skip(format!(
                            "supersedes: self-supersede {:?}",
                            truncate_str(&source_id, 60)
                        ));
                        continue;
                    }
                    // The source must exist — invalidated_by references it, and
                    // dangling references at write time are corruption.
                    if find_node(store, &source_id, project_slug)?.is_none() {
                        result.skip(format!(
                            "supersedes: unknown source {:?}",
                            truncate_str(&source_id, 60)
                        ));
                        continue;
                    }
                    let Some((scope, node)) = find_node(store, &target_id, project_slug)? else {
                        result.skip(format!(
                            "supersedes: unknown target {:?}",
                            truncate_str(&target_id, 60)
                        ));
                        continue;
                    };
                    store.invalidate(&scope, &node.id, &source_id)?;
                    result.nodes_invalidated += 1;
                } else {
                    // v3 stores no edges; supports/contradicts/derived_from/applies_to
                    // are counted for the summary and dropped.
                    result.edges_ignored += 1;
                }
            }
        }
    }
    Ok(result)
}

/// Resolve an operation's node id: try the batch's project scope first, then global.
fn find_node(
    store: &Store,
    id: &str,
    project_slug: Option<&str>,
) -> Result<Option<(Scope, Node)>, CoreError> {
    if let Some(slug) = project_slug {
        let scope = Scope::Project(slug.to_string());
        if let Some(node) = store.get(&scope, id)? {
            return Ok(Some((scope, node)));
        }
    }
    let scope = Scope::Global;
    if let Some(node) = store.get(&scope, id)? {
        return Ok(Some((scope, node)));
    }
    Ok(None)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::analysis::backend::MockBackend;
    use crate::models::{ParsedUserMessage, SessionMetadata};
    use tempfile::TempDir;

    fn session(id: &str, msgs: &[&str]) -> Session {
        Session {
            session_id: id.to_string(),
            project: "/tmp/proj".to_string(),
            session_path: format!("/tmp/{id}.jsonl"),
            user_messages: msgs
                .iter()
                .map(|m| ParsedUserMessage {
                    text: m.to_string(),
                    timestamp: None,
                })
                .collect(),
            assistant_messages: vec![],
            summaries: vec![],
            tools_used: vec![],
            errors: vec![],
            metadata: SessionMetadata {
                cwd: None,
                version: None,
                git_branch: None,
                model: None,
            },
        }
    }

    fn store() -> (TempDir, Store) {
        let tmp = TempDir::new().unwrap();
        let s = Store::open(tmp.path());
        s.ensure_layout().unwrap();
        (tmp, s)
    }

    #[test]
    fn create_node_writes_to_store_with_sources_and_clamped_confidence() {
        let (_tmp, store) = store();
        let response = r#"{"reasoning":"saw a rule","operations":[
            {"action":"create_node","node_type":"rule","scope":"project","content":"Always run smoke tests before full runs.","confidence":1.7}
        ]}"#;
        let backend = MockBackend::with_responses(vec![response.to_string()]);
        let result = analyze_sessions(
            &store,
            &backend,
            &[session("s1", &["please smoke test first", "ok run it"])],
            Some("my-proj"),
        )
        .unwrap();
        assert_eq!(result.nodes_created, 1);
        let loaded = store.load_all().unwrap();
        assert_eq!(loaded.nodes.len(), 1);
        let node = &loaded.nodes[0].1;
        assert_eq!(node.scope, Scope::Project("my-proj".to_string()));
        assert_eq!(node.node_type, NodeType::Rule);
        assert!((node.confidence - 1.0).abs() < f64::EPSILON, "clamped");
        assert_eq!(node.sources, vec!["session:s1".to_string()]);
        assert!(node.body.contains("smoke tests"));
    }

    #[test]
    fn update_and_merge_operations_mutate_existing_nodes() {
        let (_tmp, store) = store();
        let today = chrono::Utc::now().date_naive();
        let mk = |id: &str, conf: f64| Node {
            id: id.to_string(),
            scope: Scope::Global,
            node_type: NodeType::Rule,
            confidence: conf,
            sources: vec!["session:old".to_string()],
            created: today,
            updated: today,
            invalidated_by: None,
            body: format!("rule body {id}"),
        };
        store.write_node(&mk("keeper", 0.5)).unwrap();
        store.write_node(&mk("duplicate", 0.4)).unwrap();

        let response = r#"{"reasoning":"recurrence + dup","operations":[
            {"action":"update_node","node_id":"keeper","new_confidence":0.8},
            {"action":"merge_nodes","keep_id":"keeper","remove_id":"duplicate"}
        ]}"#;
        let backend = MockBackend::with_responses(vec![response.to_string()]);
        let result = analyze_sessions(
            &store,
            &backend,
            &[session("s2", &["msg one", "msg two"])],
            None,
        )
        .unwrap();
        assert_eq!(result.nodes_updated, 1);
        assert_eq!(result.nodes_merged, 1);

        let keeper = store.get(&Scope::Global, "keeper").unwrap().unwrap();
        assert!((keeper.confidence - 0.8).abs() < f64::EPSILON);
        assert!(keeper.sources.contains(&"session:s2".to_string()));
        assert!(
            keeper.sources.contains(&"session:old".to_string()),
            "merge unions sources"
        );

        let dup = store.get(&Scope::Global, "duplicate").unwrap().unwrap();
        assert_eq!(dup.invalidated_by.as_deref(), Some("keeper"));
    }

    #[test]
    fn supersedes_edge_invalidates_target_other_edges_ignored() {
        let (_tmp, store) = store();
        let today = chrono::Utc::now().date_naive();
        let mk = |id: &str| Node {
            id: id.to_string(),
            scope: Scope::Global,
            node_type: NodeType::Rule,
            confidence: 0.7,
            sources: vec![],
            created: today,
            updated: today,
            invalidated_by: None,
            body: format!("body {id}"),
        };
        store.write_node(&mk("new-way")).unwrap();
        store.write_node(&mk("old-way")).unwrap();

        let response = r#"{"reasoning":"contradiction resolved","operations":[
            {"action":"create_edge","source_id":"new-way","target_id":"old-way","edge_type":"supersedes"},
            {"action":"create_edge","source_id":"new-way","target_id":"old-way","edge_type":"supports"}
        ]}"#;
        let backend = MockBackend::with_responses(vec![response.to_string()]);
        let result =
            analyze_sessions(&store, &backend, &[session("s3", &["a", "b"])], None).unwrap();
        assert_eq!(result.nodes_invalidated, 1);
        assert_eq!(result.edges_ignored, 1);
        let old = store.get(&Scope::Global, "old-way").unwrap().unwrap();
        assert_eq!(old.invalidated_by.as_deref(), Some("new-way"));
    }

    #[test]
    fn low_signal_sessions_are_filtered_before_any_ai_call() {
        let (_tmp, store) = store();
        let backend = MockBackend::with_responses(vec![]); // any AI call would error
        let result = analyze_sessions(
            &store,
            &backend,
            &[session("tiny", &["single message"])],
            None,
        )
        .unwrap();
        assert_eq!(result.sessions_analyzed, 0);
        assert_eq!(result.nodes_created, 0);
    }

    #[test]
    fn existing_nodes_appear_in_prompt_context() {
        let (_tmp, store) = store();
        let today = chrono::Utc::now().date_naive();
        store
            .write_node(&Node {
                id: "existing-rule".to_string(),
                scope: Scope::Global,
                node_type: NodeType::Rule,
                confidence: 0.9,
                sources: vec![],
                created: today,
                updated: today,
                invalidated_by: None,
                body: "a very distinctive existing rule body".to_string(),
            })
            .unwrap();
        let response = r#"{"reasoning":"nothing new","operations":[]}"#;
        let backend = MockBackend::with_responses(vec![response.to_string()]);
        analyze_sessions(&store, &backend, &[session("s4", &["a", "b"])], None).unwrap();
        let prompts = backend.prompts_seen.lock().unwrap();
        assert_eq!(prompts.len(), 1);
        assert!(
            prompts[0].contains("existing-rule"),
            "prompt shows store node ids"
        );
    }

    #[test]
    fn hostile_ids_are_skipped_not_written() {
        let (tmp, store) = store();
        let response = r#"{"reasoning":"hostile","operations":[
            {"action":"create_node","node_type":"rule","scope":"project","project_id":"../../escape","content":"malicious rule content","confidence":0.9},
            {"action":"update_node","node_id":"../x","new_confidence":0.9},
            {"action":"create_edge","source_id":"bad\nid","target_id":"some-target","edge_type":"supersedes"}
        ]}"#;
        let backend = MockBackend::with_responses(vec![response.to_string()]);
        let result =
            analyze_sessions(&store, &backend, &[session("s5", &["a", "b"])], None).unwrap();
        assert_eq!(result.ops_skipped, 3, "skipped: {:?}", result.skipped);
        assert_eq!(result.nodes_created, 0);
        assert_eq!(result.nodes_updated, 0);
        assert_eq!(result.nodes_invalidated, 0);
        assert!(
            store.load_all().unwrap().nodes.is_empty(),
            "store unchanged"
        );
        // the traversal path would land at <tmp>/escape — must not exist
        assert!(!tmp.path().join("escape").exists());
    }

    #[test]
    fn self_merge_is_skipped() {
        let (_tmp, store) = store();
        let today = chrono::Utc::now().date_naive();
        store
            .write_node(&Node {
                id: "solo".to_string(),
                scope: Scope::Global,
                node_type: NodeType::Rule,
                confidence: 0.6,
                sources: vec![],
                created: today,
                updated: today,
                invalidated_by: None,
                body: "solo body".to_string(),
            })
            .unwrap();
        let response = r#"{"reasoning":"bad merge","operations":[
            {"action":"merge_nodes","keep_id":"solo","remove_id":"solo"}
        ]}"#;
        let backend = MockBackend::with_responses(vec![response.to_string()]);
        let result =
            analyze_sessions(&store, &backend, &[session("s6", &["a", "b"])], None).unwrap();
        assert_eq!(result.ops_skipped, 1, "skipped: {:?}", result.skipped);
        assert_eq!(result.nodes_merged, 0);
        let solo = store.get(&Scope::Global, "solo").unwrap().unwrap();
        assert!(solo.is_active(), "self-merge must not invalidate the node");
    }

    #[test]
    fn empty_and_oversize_creates_are_skipped() {
        let (_tmp, store) = store();
        let big = "x".repeat(20_000);
        let response = format!(
            r#"{{"reasoning":"junk","operations":[
            {{"action":"create_node","node_type":"rule","scope":"global","content":"","confidence":0.5}},
            {{"action":"create_node","node_type":"rule","scope":"global","content":"{big}","confidence":0.5}}
        ]}}"#
        );
        let backend = MockBackend::with_responses(vec![response]);
        let result =
            analyze_sessions(&store, &backend, &[session("s7", &["a", "b"])], None).unwrap();
        assert_eq!(result.ops_skipped, 2, "skipped: {:?}", result.skipped);
        assert_eq!(result.nodes_created, 0);
        assert!(store.load_all().unwrap().nodes.is_empty());
    }

    #[test]
    fn supersedes_with_missing_source_is_skipped() {
        let (_tmp, store) = store();
        let today = chrono::Utc::now().date_naive();
        store
            .write_node(&Node {
                id: "victim".to_string(),
                scope: Scope::Global,
                node_type: NodeType::Rule,
                confidence: 0.7,
                sources: vec![],
                created: today,
                updated: today,
                invalidated_by: None,
                body: "victim body".to_string(),
            })
            .unwrap();
        let response = r#"{"reasoning":"dangling","operations":[
            {"action":"create_edge","source_id":"ghost-source","target_id":"victim","edge_type":"supersedes"}
        ]}"#;
        let backend = MockBackend::with_responses(vec![response.to_string()]);
        let result =
            analyze_sessions(&store, &backend, &[session("s8", &["a", "b"])], None).unwrap();
        assert_eq!(result.ops_skipped, 1, "skipped: {:?}", result.skipped);
        assert_eq!(result.nodes_invalidated, 0);
        let victim = store.get(&Scope::Global, "victim").unwrap().unwrap();
        assert!(victim.is_active(), "no dangling invalidated_by references");
    }

    #[test]
    fn unknown_update_and_merge_ids_are_counted() {
        let (_tmp, store) = store();
        let response = r#"{"reasoning":"ghosts","operations":[
            {"action":"update_node","node_id":"nonexistent-node","new_confidence":0.9},
            {"action":"merge_nodes","keep_id":"ghost-a","remove_id":"ghost-b"}
        ]}"#;
        let backend = MockBackend::with_responses(vec![response.to_string()]);
        let result =
            analyze_sessions(&store, &backend, &[session("s9", &["a", "b"])], None).unwrap();
        assert_eq!(result.ops_skipped, 2, "skipped: {:?}", result.skipped);
        assert_eq!(result.nodes_updated, 0);
        assert_eq!(result.nodes_merged, 0);
        assert!(
            result
                .skipped
                .iter()
                .any(|s| s.contains("nonexistent-node")),
            "reason names the id: {:?}",
            result.skipped
        );
        assert!(
            result.skipped.iter().any(|s| s.contains("ghost-a")),
            "reason names the id: {:?}",
            result.skipped
        );
    }

    #[test]
    fn reasoning_is_populated_from_response() {
        let (_tmp, store) = store();
        let response = r#"{"reasoning":"found two rules","operations":[]}"#;
        let backend = MockBackend::with_responses(vec![response.to_string()]);
        let result =
            analyze_sessions(&store, &backend, &[session("s10", &["a", "b"])], None).unwrap();
        assert_eq!(result.reasoning, "found two rules");
    }

    #[test]
    fn context_prefers_high_confidence_nodes() {
        let (_tmp, store) = store();
        let today = chrono::Utc::now().date_naive();
        let mk = |id: &str, conf: f64| Node {
            id: id.to_string(),
            scope: Scope::Global,
            node_type: NodeType::Rule,
            confidence: conf,
            sources: vec![],
            created: today,
            updated: today,
            invalidated_by: None,
            body: format!("body {id}"),
        };
        // 55 filler nodes at 0.5 — the prompt builder caps at 50, so without
        // sorting the high-confidence node can fall off the end.
        for i in 0..55 {
            store.write_node(&mk(&format!("node-{i:02}"), 0.5)).unwrap();
        }
        store.write_node(&mk("top-priority-rule", 0.99)).unwrap();

        let response = r#"{"reasoning":"nothing new","operations":[]}"#;
        let backend = MockBackend::with_responses(vec![response.to_string()]);
        analyze_sessions(&store, &backend, &[session("s11", &["a", "b"])], None).unwrap();
        let prompts = backend.prompts_seen.lock().unwrap();
        assert!(
            prompts[0].contains("top-priority-rule"),
            "highest-confidence node must be visible in the capped prompt context"
        );
    }
}