innate 0.1.16

Innate — self-growing procedural knowledge layer for AI 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
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
use super::*;

#[test]
fn min_score_gate_drops_subthreshold_candidates() {
    // The relevance gate (used by always-on hooks) must drop candidates below the
    // threshold before packing/trace, so an impossibly high gate yields empty knowledge
    // while no gate returns the same chunk.
    let (kb, _f) = tmp_kb();
    let id = kb
        .add(
            "Prefer composition over inheritance",
            "note",
            Some("design principle"),
            None,
            "manual",
            None,
        )
        .unwrap();

    let base = RecallParams {
        query: "design principle",
        budget: 6000,
        trace: false,
        include_sparks: false,
        top: None,
        source: "sdk",
        expand_deps: "false",
        allow_trim: false,
        refine_mode: "off",
        min_score: None,
        session_only: false,
        ..Default::default()
    };

    // No gate: the chunk is retrievable.
    let ungated = kb.recall(base.clone()).unwrap();
    assert!(ungated
        .knowledge
        .iter()
        .any(|c| c["id"].as_str() == Some(id.as_str())));

    // Fused scores span at most ~1.05; a gate of 2.0 must drop everything.
    let gated = kb
        .recall(RecallParams {
            min_score: Some(2.0),
            session_only: false,
            ..base
        })
        .unwrap();
    assert!(gated.knowledge.is_empty());
    assert!(gated.empty);
}

#[test]
fn agent_source_dimension_is_captured_and_orthogonal_to_channel() {
    // The agent product identity (INNATE_AGENT) lands on both the chunk and the
    // recall episodic_log, independently of the access channel (event_source).
    let (kb, _f) = tmp_kb();
    std::env::set_var("INNATE_AGENT", "claude-code");

    let id = kb
        .add(
            "Use cargo build --release to compile innate",
            "note",
            Some("build the binary"),
            None,
            "manual",
            None,
        )
        .unwrap();
    let chunk = kb.storage.get_chunk(&id).unwrap().unwrap();
    assert_eq!(chunk["agent"].as_str(), Some("claude-code"));

    let res = kb
        .recall(RecallParams {
            query: "build the binary",
            budget: 6000,
            trace: true,
            source: "cli",
            expand_deps: "false",
            refine_mode: "off",
            ..Default::default()
        })
        .unwrap();
    let log = kb
        .storage
        .get_episodic_log(&res.trace_id)
        .unwrap()
        .unwrap();
    assert_eq!(log["agent"].as_str(), Some("claude-code"));
    // Channel and agent are distinct dimensions.
    assert_eq!(log["event_source"].as_str(), Some("cli"));

    std::env::remove_var("INNATE_AGENT");
}

#[test]
fn add_and_recall() {
    let (kb, _f) = tmp_kb();
    let id = kb
        .add(
            "Always validate user input at system boundaries",
            "note",
            Some("input validation"),
            None,
            "manual",
            None,
        )
        .unwrap();
    assert!(!id.is_empty());

    let result = kb
        .recall(RecallParams {
            query: "validate input",
            budget: 6000,
            trace: false,
            include_sparks: false,
            top: None,
            source: "sdk",
            expand_deps: "false",
            allow_trim: false,
            refine_mode: "off",
            min_score: None,
            session_only: false,
            ..Default::default()
        })
        .unwrap();
    assert!(!result.trace_id.is_empty());
}

#[test]
fn warm_cache_reflects_writes_made_after_first_recall() {
    // Locks the incremental vector-cache invariant: a recall warms the in-memory
    // cache, and a chunk added afterwards must still be retrievable by the next
    // recall on the same long-lived KnowledgeBase (no stale-cache miss).
    let (kb, _f) = tmp_kb();

    let id_a = kb
        .add(
            "First fact about caching",
            "note",
            Some("caching"),
            None,
            "manual",
            None,
        )
        .unwrap();

    // Warm the cache.
    let first = kb
        .recall(RecallParams {
            query: "caching",
            budget: 6000,
            trace: false,
            include_sparks: false,
            top: None,
            source: "sdk",
            expand_deps: "false",
            allow_trim: false,
            refine_mode: "off",
            min_score: None,
            session_only: false,
            ..Default::default()
        })
        .unwrap();
    assert!(first
        .knowledge
        .iter()
        .any(|c| c["id"].as_str() == Some(id_a.as_str())));

    // Write after the cache is warm — must land in the in-memory cache.
    let id_b = kb
        .add(
            "Second fact added later",
            "note",
            Some("later"),
            None,
            "manual",
            None,
        )
        .unwrap();

    let second = kb
        .recall(RecallParams {
            query: "later",
            budget: 6000,
            trace: false,
            include_sparks: false,
            top: None,
            source: "sdk",
            expand_deps: "false",
            allow_trim: false,
            refine_mode: "off",
            min_score: None,
            session_only: false,
            ..Default::default()
        })
        .unwrap();
    assert!(
        second
            .knowledge
            .iter()
            .any(|c| c["id"].as_str() == Some(id_b.as_str())),
        "chunk added after cache warm-up was not retrievable"
    );
}

#[test]
fn curate_compacts_old_terminal_logs_and_keeps_recent() {
    // Step 8 of curate compacts terminal episodic_log rows older than the
    // configurable window, NULLing heavy payload while preserving the row.
    let (kb, _f) = tmp_kb();
    let lib = kb.storage.lib_id().unwrap();

    let mk = |trace: &str, ts: &str| crate::storage::EpisodicLogRow {
        id: crate::utils::gen_uuid(),
        trace_id: trace.to_string(),
        lib_id: lib.clone(),
        ts: ts.to_string(),
        output_summary: Some("heavy payload to be compacted".to_string()),
        event_source: "sdk".to_string(),
        task_state: "completed".to_string(),
        usage_state: "unknown".to_string(),
        distill_state: "distilled".to_string(),
        ..Default::default()
    };

    // One ancient terminal log (well before the 30-day window) and one fresh one.
    kb.storage
        .upsert_episodic_log(&mk("old-trace", "2020-01-01T00:00:00.000Z"))
        .unwrap();
    kb.storage
        .upsert_episodic_log(&mk("new-trace", &crate::utils::utc_now_iso()))
        .unwrap();

    kb.evolve("manual").unwrap();

    let old = kb.storage.get_episodic_log("old-trace").unwrap().unwrap();
    let new = kb.storage.get_episodic_log("new-trace").unwrap().unwrap();
    assert!(
        old["output_summary"].is_null(),
        "old terminal log should have been compacted"
    );
    assert_eq!(
        new["output_summary"].as_str(),
        Some("heavy payload to be compacted"),
        "recent terminal log must be preserved"
    );
}

#[test]
fn vacuum_runs_and_reports_size() {
    let (kb, _f) = tmp_kb();
    let (before, after) = kb.storage.vacuum().unwrap();
    assert!(before > 0 && after > 0);
    assert!(after <= before, "vacuum must not grow the file");
}

#[test]
fn spark_and_promote() {
    let (kb, _f) = tmp_kb();
    let sid = kb
        .spark("Use HNSW index for recall scalability", None, None)
        .unwrap();
    assert!(!sid.is_empty());

    let nid = kb.promote_spark(&sid, "note").unwrap();
    assert!(!nid.is_empty());

    let chunk = kb.storage.get_chunk(&nid).unwrap().unwrap();
    assert_eq!(chunk["origin"].as_str().unwrap(), "captured");
    assert_eq!(chunk["state"].as_str().unwrap(), "active");
}

#[test]
fn record_state_machine() {
    let (kb, _f) = tmp_kb();
    let trace_id = crate::utils::gen_uuid();
    kb.record(RecordParams {
        trace_id: &trace_id,
        query: Some("test query"),
        output: None,
        output_summary: Some("summary"),
        outcome: Some("ok"),
        used: None,
        feedback_up: None,
        feedback_down: None,
        nomination: None,
        priority: 0,
        source: "cli",
        ..Default::default()
    })
    .unwrap();
    let log = kb.storage.get_episodic_log(&trace_id).unwrap().unwrap();
    assert_eq!(log["distill_state"].as_str().unwrap(), "new");

    kb.record(RecordParams {
        trace_id: &trace_id,
        query: None,
        output: None,
        output_summary: None,
        outcome: Some("ok"),
        used: None,
        feedback_up: None,
        feedback_down: None,
        nomination: None,
        priority: 0,
        source: "cli",
        ..Default::default()
    })
    .unwrap();
    let log2 = kb.storage.get_episodic_log(&trace_id).unwrap().unwrap();
    assert_eq!(log2["distill_state"].as_str().unwrap(), "new");
}

#[test]
fn late_material_reopens_insufficient_material_log() {
    let (kb, _f) = tmp_kb();
    let trace_id = crate::utils::gen_uuid();
    kb.record(RecordParams {
        trace_id: &trace_id,
        query: Some("late material"),
        output: None,
        output_summary: None,
        outcome: Some("ok"),
        used: None,
        feedback_up: None,
        feedback_down: None,
        nomination: None,
        priority: 0,
        source: "sdk",
        ..Default::default()
    })
    .unwrap();
    let discarded = kb.storage.get_episodic_log(&trace_id).unwrap().unwrap();
    assert_eq!(discarded["distill_state"].as_str(), Some("discarded"));
    assert_eq!(
        discarded["distill_note"].as_str(),
        Some("insufficient_material")
    );

    kb.record(RecordParams {
        trace_id: &trace_id,
        query: None,
        output: None,
        output_summary: Some("material arrived after completion"),
        outcome: None,
        used: None,
        feedback_up: None,
        feedback_down: None,
        nomination: None,
        priority: 0,
        source: "sdk",
        ..Default::default()
    })
    .unwrap();

    let reopened = kb.storage.get_episodic_log(&trace_id).unwrap().unwrap();
    assert_eq!(reopened["distill_state"].as_str(), Some("new"));
    assert!(reopened["distill_note"].is_null());
}

#[test]
fn mcp_is_a_valid_event_source() {
    let (kb, _f) = tmp_kb();
    let result = kb
        .recall(RecallParams {
            query: "mcp source",
            budget: 6000,
            trace: true,
            include_sparks: false,
            top: None,
            source: "mcp",
            expand_deps: "false",
            allow_trim: false,
            refine_mode: "off",
            min_score: None,
            session_only: false,
            ..Default::default()
        })
        .unwrap();
    kb.record(RecordParams {
        trace_id: &result.trace_id,
        query: None,
        output: None,
        output_summary: Some("closed through MCP"),
        outcome: Some("ok"),
        used: None,
        feedback_up: None,
        feedback_down: None,
        nomination: None,
        priority: 0,
        source: "mcp",
        ..Default::default()
    })
    .unwrap();
}

#[test]
fn unknown_usage_does_not_penalize_selected_chunks() {
    let (kb, _f) = tmp_kb();
    let chunk_id = kb
        .add(
            "Use bounded retries",
            "note",
            Some("bounded retries"),
            None,
            "manual",
            None,
        )
        .unwrap();
    let first = kb
        .recall(RecallParams {
            query: "bounded retries",
            budget: 6000,
            trace: true,
            include_sparks: false,
            top: None,
            source: "sdk",
            expand_deps: "false",
            allow_trim: false,
            refine_mode: "off",
            min_score: None,
            session_only: false,
            ..Default::default()
        })
        .unwrap();
    let before = kb.storage.get_chunk(&chunk_id).unwrap().unwrap()["confidence"]
        .as_f64()
        .unwrap();
    kb.record(RecordParams {
        trace_id: &first.trace_id,
        query: None,
        output: None,
        output_summary: Some("completed without usage attribution"),
        outcome: Some("ok"),
        used: None,
        feedback_up: None,
        feedback_down: None,
        nomination: None,
        priority: 0,
        source: "sdk",
        ..Default::default()
    })
    .unwrap();
    let after_unknown = kb.storage.get_chunk(&chunk_id).unwrap().unwrap()["confidence"]
        .as_f64()
        .unwrap();
    assert_eq!(before, after_unknown);

    let second = kb
        .recall(RecallParams {
            query: "bounded retries",
            budget: 6000,
            trace: true,
            include_sparks: false,
            top: None,
            source: "sdk",
            expand_deps: "false",
            allow_trim: false,
            refine_mode: "off",
            min_score: None,
            session_only: false,
            ..Default::default()
        })
        .unwrap();
    let explicitly_unused: Vec<String> = vec![];
    kb.record(RecordParams {
        trace_id: &second.trace_id,
        query: None,
        output: None,
        output_summary: Some("completed and explicitly used no recalled chunks"),
        outcome: Some("ok"),
        used: Some(&explicitly_unused),
        feedback_up: None,
        feedback_down: None,
        nomination: None,
        priority: 0,
        source: "sdk",
        ..Default::default()
    })
    .unwrap();
    let after_known_none = kb.storage.get_chunk(&chunk_id).unwrap().unwrap()["confidence"]
        .as_f64()
        .unwrap();
    assert!(after_known_none < after_unknown);
}

#[test]
fn feedback_is_auditable_and_builds_contextual_governance_evidence() {
    let (kb, _f) = tmp_kb();
    let chunk_id = kb
        .add(
            "Always retry forever",
            "note",
            Some("retry policy"),
            None,
            "manual",
            None,
        )
        .unwrap();

    for i in 0..2 {
        let recall = kb
            .recall(RecallParams {
                query: "retry policy",
                budget: 6000,
                trace: true,
                include_sparks: false,
                top: None,
                source: "sdk",
                expand_deps: "false",
                allow_trim: false,
                refine_mode: "off",
                min_score: None,
                session_only: false,
                ..Default::default()
            })
            .unwrap();
        let used = vec![chunk_id.clone()];
        let actor = format!("tester-{i}");
        kb.record(RecordParams {
            trace_id: &recall.trace_id,
            query: None,
            output: None,
            output_summary: Some("retry policy was unsuitable"),
            outcome: Some("fail"),
            used: Some(&used),
            used_attribution: "explicit",
            used_complete: Some(true),
            feedback_up: None,
            feedback_down: Some(&used),
            feedback_kind: "user",
            feedback_actor: Some(&actor),
            feedback_reason: Some("unbounded retry is unsafe"),
            nomination: None,
            priority: 0,
            task_state: None,
            source: "sdk",
            ..Default::default()
        })
        .unwrap();
    }

    let feedback_count = kb
        .storage
        .query_chunks_params(
            "SELECT COUNT(*) AS count FROM feedback_events WHERE chunk_id=?",
            rusqlite::params![chunk_id],
        )
        .unwrap()[0]["count"]
        .as_i64();
    assert_eq!(feedback_count, Some(2));
    let proposals = kb
        .storage
        .query_chunks("SELECT * FROM governance_proposals WHERE state='pending'")
        .unwrap();
    assert_eq!(proposals.len(), 1);
    let context = kb
        .storage
        .query_chunks("SELECT * FROM chunk_context_stats")
        .unwrap();
    assert_eq!(context.len(), 1);
    assert_eq!(context[0]["failure_count"].as_i64(), Some(2));
    assert_eq!(context[0]["negative_feedback"].as_i64(), Some(2));
}

#[test]
fn record_requests_evolve_and_inspect_reports_feedback_metrics() {
    let file = NamedTempFile::new().unwrap();
    {
        let kb = KnowledgeBase::open(file.path()).unwrap();
        kb.storage
            .set_meta("evolve.threshold_new_count", "1")
            .unwrap();
    }
    let kb = KnowledgeBase::open(file.path()).unwrap();
    let trace_id = crate::utils::gen_uuid();
    kb.record(RecordParams {
        trace_id: &trace_id,
        query: Some("queue evolve"),
        output: None,
        output_summary: Some("reusable material"),
        outcome: Some("ok"),
        used: Some(&[]),
        feedback_up: None,
        feedback_down: None,
        nomination: None,
        priority: 0,
        source: "sdk",
        ..Default::default()
    })
    .unwrap();

    let requests = kb
        .storage
        .query_chunks("SELECT * FROM evolve_requests WHERE state='pending'")
        .unwrap();
    assert_eq!(requests.len(), 1);
    let inspect = kb.inspect().unwrap();
    assert_eq!(
        inspect["feedback_loop"]["trace_completion_rate"].as_f64(),
        Some(1.0)
    );
    assert_eq!(
        inspect["feedback_loop"]["usage_annotation_rate"].as_f64(),
        Some(1.0)
    );
    assert_eq!(
        inspect["feedback_loop"]["pending_evolve_requests"].as_i64(),
        Some(1)
    );
}

#[test]
fn invalidate_cascade() {
    let (kb, _f) = tmp_kb();
    let id = kb
        .add("sensitive content", "note", None, None, "manual", None)
        .unwrap();
    kb.invalidate(&id, "test").unwrap();
    let chunk = kb.storage.get_chunk(&id).unwrap().unwrap();
    assert_eq!(chunk["state"].as_str().unwrap(), "archived");
    assert_eq!(chunk["confidence"].as_f64().unwrap(), 0.0);
    let h = chunk["content_hash"].as_str().unwrap();
    assert!(kb.storage.is_hash_invalidated(h).unwrap());
}

// 混合检索:词法/BM25 通道应召回「精确 token 命中、但语义嵌入(此处用哑向量)
// 命中不到」的 chunk —— 这正是向量检索对错误码/参数/符号名的盲区。
#[test]
fn lexical_channel_recovers_exact_token_match() {
    let (kb, _f) = tmp_kb();
    // 一堆干扰 chunk + 一个含罕见精确 token(错误码 E0599)的目标 chunk。
    for i in 0..5 {
        kb.add(
            &format!("general advice number {i} about unrelated topics"),
            "note",
            Some("misc guidance"),
            None,
            "manual",
            None,
        )
        .unwrap();
    }
    let target = kb
        .add(
            "Fix E0599 no-method-found by importing the relevant trait into scope",
            "note",
            Some("rust trait method resolution"),
            None,
            "manual",
            None,
        )
        .unwrap();

    // 用纯精确 token 查询 —— 哑向量不会把它排上来,词法通道必须命中。
    let r = kb
        .recall(RecallParams {
            query: "E0599",
            budget: 8000,
            trace: false,
            source: "sdk",
            ..Default::default()
        })
        .unwrap();
    let hit = r
        .knowledge
        .iter()
        .any(|c| c.get("id").and_then(|v| v.as_str()) == Some(target.as_str()));
    assert!(hit, "词法通道应召回含精确 token E0599 的 chunk");
}

// 部分 d:opt-in 重排器应在 rerank=true 时重排候选(此处用无网络的桩重排器,把指定
// id 顶到最前),且默认 rerank=false 时不触发(热路径保持无 LLM)。
struct PinReranker {
    first: String,
}
impl crate::refine::Reranker for PinReranker {
    fn rerank(&self, _q: &str, candidates: &[Value]) -> crate::errors::Result<Vec<String>> {
        let mut ids: Vec<String> = candidates
            .iter()
            .filter_map(|c| c["id"].as_str().map(str::to_string))
            .collect();
        ids.sort_by_key(|id| usize::from(*id != self.first));
        Ok(ids)
    }
}

#[test]
fn opt_in_reranker_reorders_shortlist() {
    let file = NamedTempFile::new().unwrap();
    let kb = KnowledgeBase::open_with(file.path(), None, None, None, None, None).unwrap();
    // Three chunks sharing a trigger word so all surface as candidates.
    let mut ids = Vec::new();
    for tag in ["alpha", "beta", "gamma"] {
        ids.push(
            kb.add(
                &format!("workflow step {tag} for the pipeline"),
                "note",
                Some("pipeline workflow step"),
                None,
                "manual",
                None,
            )
            .unwrap(),
        );
    }
    let pinned = ids[2].clone();
    let kb = kb.with_reranker(std::sync::Arc::new(PinReranker {
        first: pinned.clone(),
    }));

    let reranked = kb
        .recall(RecallParams {
            query: "pipeline workflow step",
            budget: 100_000,
            trace: false,
            source: "sdk",
            rerank: true,
            ..Default::default()
        })
        .unwrap();
    assert_eq!(
        reranked.knowledge.first().and_then(|c| c["id"].as_str()),
        Some(pinned.as_str()),
        "rerank=true should pin the reranker's first id to the front"
    );
}

#[test]
fn inspect_returns_counts() {
    let (kb, _f) = tmp_kb();
    kb.add("test chunk", "note", None, None, "manual", None)
        .unwrap();
    let info = kb.inspect().unwrap();
    let active = info["chunks"]["active"].as_i64().unwrap_or(0);
    assert!(active >= 1);
}

#[test]
fn evolve_smoke() {
    let (kb, _f) = tmp_kb();
    let result = kb.evolve("manual").unwrap();
    assert!(result["distilled"].is_number());
}

struct CountingRefiner {
    calls: Arc<AtomicUsize>,
}

impl Refiner for CountingRefiner {
    fn refine(&self, chunks: Vec<Value>, _budget: Option<usize>) -> Result<Vec<Value>> {
        self.calls.fetch_add(1, Ordering::SeqCst);
        Ok(chunks)
    }
}

#[test]
fn refine_runs_only_in_adapt_mode() {
    let file = NamedTempFile::new().unwrap();
    let calls = Arc::new(AtomicUsize::new(0));
    let refiner = Arc::new(CountingRefiner {
        calls: Arc::clone(&calls),
    });
    let kb = KnowledgeBase::open_with(file.path(), None, Some(refiner), None, None, None).unwrap();
    kb.add("Refiner mode test", "note", None, None, "manual", None)
        .unwrap();

    kb.recall(RecallParams {
        query: "Refiner mode test",
        budget: 6000,
        trace: false,
        include_sparks: false,
        top: None,
        source: "sdk",
        expand_deps: "false",
        allow_trim: false,
        refine_mode: "off",
        min_score: None,
        session_only: false,
        ..Default::default()
    })
    .unwrap();
    assert_eq!(calls.load(Ordering::SeqCst), 0);

    kb.recall(RecallParams {
        query: "Refiner mode test",
        budget: 6000,
        trace: false,
        include_sparks: false,
        top: None,
        source: "sdk",
        expand_deps: "false",
        allow_trim: false,
        refine_mode: "adapt",
        min_score: None,
        session_only: false,
        ..Default::default()
    })
    .unwrap();
    assert_eq!(calls.load(Ordering::SeqCst), 1);
}

#[test]
fn actr_activation_zero_for_unused_chunks() {
    use crate::kb::actr_activation;
    // Never-used knowledge contributes nothing → recall stays zero-regression
    // for freshly-added chunks (used_count == 0 or no last_used_at timestamp).
    let now = "2026-06-19T00:00:00.000Z";
    assert_eq!(
        actr_activation(0, Some("2026-06-18T00:00:00.000Z"), now),
        0.0
    );
    assert_eq!(actr_activation(5, None, now), 0.0);
}

#[test]
fn actr_activation_recency_and_frequency_monotonic() {
    use crate::kb::actr_activation;
    let now = "2026-06-19T00:00:00.000Z";
    let recent = actr_activation(3, Some("2026-06-18T00:00:00.000Z"), now); // 1 day ago
    let stale = actr_activation(3, Some("2026-03-19T00:00:00.000Z"), now); // ~3 months ago
                                                                           // More recent use ⇒ higher activation at equal frequency.
    assert!(
        recent > stale,
        "recent {recent} should exceed stale {stale}"
    );

    let used_once = actr_activation(1, Some("2026-06-18T00:00:00.000Z"), now);
    let used_many = actr_activation(20, Some("2026-06-18T00:00:00.000Z"), now);
    // More uses ⇒ higher activation at equal recency.
    assert!(
        used_many > used_once,
        "many {used_many} should exceed once {used_once}"
    );
}

#[test]
fn actr_activation_is_bounded_unit_interval() {
    use crate::kb::actr_activation;
    let now = "2026-06-19T00:00:00.000Z";
    // Extreme frequency, just used: still strictly below 1.0.
    let hot = actr_activation(100_000, Some("2026-06-19T00:00:00.000Z"), now);
    assert!(hot > 0.0 && hot < 1.0, "activation {hot} must be in (0,1)");
    // Single ancient use: still strictly above 0.0.
    let cold = actr_activation(1, Some("2000-01-01T00:00:00.000Z"), now);
    assert!(
        cold > 0.0 && cold < 1.0,
        "activation {cold} must be in (0,1)"
    );
}