innate 0.1.12

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
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,
    };

    // 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),
            ..base
        })
        .unwrap();
    assert!(gated.knowledge.is_empty());
    assert!(gated.empty);
}

#[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,
        })
        .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,
        })
        .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,
        })
        .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,
        })
        .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,
        })
        .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,
        })
        .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,
            })
            .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",
        })
        .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());
}

#[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,
    })
    .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,
    })
    .unwrap();
    assert_eq!(calls.load(Ordering::SeqCst), 1);
}