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
use super::*;
/// Parameters for [`KnowledgeBase::recall`].
///
/// Borrowed, `Default`-able: construct with `RecallParams { query, budget, source, ..Default::default() }`.
/// Empty-string defaults are normalized inside `recall`: `expand_deps` empty → `"false"`,
/// `refine_mode` empty → `"off"`.
#[derive(Debug, Clone, Default)]
pub struct RecallParams<'a> {
pub query: &'a str,
pub budget: usize,
pub trace: bool,
pub include_sparks: bool,
pub top: Option<usize>,
pub source: &'a str,
pub expand_deps: &'a str, // "false" | "direct" | "closure"
pub allow_trim: bool, // if true, invoke Refiner::trim when block doesn't fit
pub refine_mode: &'a str, // "off" | "trim" | "adapt" — recorded in trace
/// Relevance gate: drop candidates whose fused score is below this value
/// **before** packing/trace, so the trace only records knowledge that was
/// actually surfaced. `None` disables the gate. Used by always-on hooks
/// (UserPromptSubmit / SessionStart) to stay high-frequency without noise.
pub min_score: Option<f64>,
/// Session trace mode: open an episodic log for later record-correlation but
/// write **no** per-chunk `retrieved`/`selected` usage events. Used by the
/// daemon, which recalls only to obtain a `trace_id` and discards the
/// knowledge without ever placing it in a model context. `selected` must
/// strictly mean "entered the model context", so a caller that does not
/// inject the result must set this. Defaults to `false` (full injection).
pub session_only: bool,
/// Part (d) — opt-in offline LLM rerank of the candidate shortlist. Off by
/// default so the hot hook path stays no-LLM; only set it in latency-tolerant
/// callers ("deep recall"). No-op unless a reranker was injected (LLM configured),
/// and non-fatal: a reranker error falls back to the fused order.
pub rerank: bool,
}
impl KnowledgeBase {
pub fn recall(&self, params: RecallParams<'_>) -> Result<RecallResult> {
let RecallParams {
query,
budget,
trace,
include_sparks,
top,
source,
expand_deps,
allow_trim,
refine_mode,
min_score,
session_only,
rerank,
} = params;
let expand_deps = if expand_deps.is_empty() {
"false"
} else {
expand_deps
};
let refine_mode = if refine_mode.is_empty() {
"off"
} else {
refine_mode
};
validate_source(source)?;
let trace_id = gen_uuid();
let now = utc_now_iso();
// Calibration path: derive the context_key from a Situation. A bare query degrades
// exactly to the legacy `content_hash(normalize_query(query))`, so recall stays
// zero-regression while sharing one key derivation with appraise (Spec §2.2).
let situation = Situation::from_query(query);
let context_key = situation.context_key(&self.situation_coarse_keys);
// Part (c) — query-embedding granularity. When enabled, anchor the vector
// query on the normalized situation signature in addition to the raw words.
// The lexical channel still matches the raw query (it indexes chunk text).
let embed_query = if self.embed_situation_signature {
let sig = situation.coarse_signature(&self.situation_coarse_keys);
if signature_has_signal(&sig) {
format!("{sig}\n{query}")
} else {
query.to_string()
}
} else {
query.to_string()
};
let (q_content, q_trigger) = self
.embedding
.embed_both(&embed_query)
.map_err(|e| InnateError::EmbeddingUnavailable(e.to_string()))?;
// ANN candidates (non-spark) — vector channels + lexical/BM25 (hybrid).
let mut candidates = self.ann_candidates(&q_content, &q_trigger, query)?;
self.apply_soft_dep_bonus(&mut candidates)?;
// Score + anti-trigger penalty
let mut scored = self.score_candidates(candidates, query, &context_key, &now)?;
// Part (d) — opt-in offline rerank of the shortlist before packing. Non-fatal:
// a reranker error or empty result leaves the fused order untouched.
if rerank {
self.apply_rerank(query, &mut scored);
}
// Relevance gate — drop sub-threshold candidates before packing/trace so the
// trace records only what was actually surfaced (keeps selected→used stats clean).
if let Some(min) = min_score {
scored.retain(|(fused, _)| *fused >= min);
}
// First-fit pack with dep expansion
let (selected, skipped, skipped_reasons) =
self.pack(&scored, budget, expand_deps, allow_trim, query)?;
let depth_skipped: Vec<String> = skipped_reasons
.iter()
.filter(|(_, r)| r.as_str() == "dep_depth_limit")
.map(|(id, _)| id.clone())
.collect();
// Density refill
let mut selected = selected;
if self.density_refill {
selected = self.density_refill(selected, &skipped, budget);
}
let limited = limit_knowledge(selected, top);
let visible = if refine_mode == "adapt" {
self.refiner
.refine(limited.clone(), Some(budget))
.unwrap_or(limited)
} else {
limited
};
// Sparks
let sparks = if include_sparks {
self.recall_sparks(&q_content, &q_trigger)?
} else {
vec![]
};
if trace {
self.write_recall_trace(
&trace_id,
query,
&context_key,
&scored,
&visible,
&sparks,
&depth_skipped,
&skipped_reasons,
refine_mode,
source,
&now,
session_only,
)?;
}
let empty = visible.is_empty() && sparks.is_empty();
Ok(RecallResult {
knowledge: visible,
sparks,
trace_id,
empty,
depth_skipped,
skipped_reasons,
})
}
pub(super) fn ann_candidates(
&self,
q_content: &[f32],
q_trigger: &[f32],
query: &str,
) -> Result<HashMap<String, CandidateInfo>> {
let embed_version = self
.storage
.get_meta("embed_version")?
.and_then(|v| v.parse::<i64>().ok())
.unwrap_or(1);
let content_res = self
.storage
.search_vec_content(q_content, self.top_k_candidates * 2)?;
let trigger_res = self
.storage
.search_vec_trigger(q_trigger, self.top_k_candidates * 2)?;
// Hybrid 检索 — lexical/BM25 channel. Recovers exact-term matches (error
// codes, flags, symbol names) that embedding similarity blurs away. Empty
// when the query has no usable tokens, so vector-only behaviour is preserved.
let lexical_res = self
.storage
.search_lexical(query, self.top_k_candidates * 2)?;
// Collect unique ids across all three channels and batch-fetch in one query.
let all_ids: Vec<&str> = {
let mut seen = HashSet::new();
content_res
.iter()
.chain(trigger_res.iter())
.chain(lexical_res.iter())
.map(|(id, _)| id.as_str())
.filter(|id| seen.insert(*id))
.collect()
};
let chunks = self.storage.get_chunks_by_ids(&all_ids)?;
let mut candidates: HashMap<String, CandidateInfo> = HashMap::new();
for (cid, sim) in &content_res {
if let Some(chunk) = chunks.get(cid) {
if chunk_is_valid_for_recall(chunk, embed_version) {
let e = candidates.entry(cid.clone()).or_insert_with(|| new_candidate(chunk));
e.sim_content = e.sim_content.max(*sim);
}
}
}
for (cid, sim) in &trigger_res {
if let Some(chunk) = chunks.get(cid) {
if chunk_is_valid_for_recall(chunk, embed_version) {
let e = candidates.entry(cid.clone()).or_insert_with(|| new_candidate(chunk));
e.sim_trigger = e.sim_trigger.max(*sim);
}
}
}
for (cid, sim) in &lexical_res {
if let Some(chunk) = chunks.get(cid) {
if chunk_is_valid_for_recall(chunk, embed_version) {
let e = candidates.entry(cid.clone()).or_insert_with(|| new_candidate(chunk));
e.sim_lexical = e.sim_lexical.max(*sim);
}
}
}
Ok(candidates)
}
pub(super) fn apply_soft_dep_bonus(
&self,
candidates: &mut HashMap<String, CandidateInfo>,
) -> Result<()> {
// Collect non-spark candidate ids and batch-fetch their outgoing deps
// in a single query (was one get_deps per candidate).
let src_ids: Vec<String> = candidates
.iter()
.filter(|(_, info)| info.chunk.get("origin").and_then(Value::as_str) != Some("spark"))
.map(|(cid, _)| cid.clone())
.collect();
if src_ids.is_empty() {
return Ok(());
}
let src_refs: Vec<&str> = src_ids.iter().map(String::as_str).collect();
let deps_map = self.storage.get_deps_batch(&src_refs)?;
// Gather distinct soft-dep targets and batch-fetch them in one query
// (was one get_chunk per soft edge).
let mut target_ids: Vec<String> = Vec::new();
let mut seen: HashSet<String> = HashSet::new();
for deps in deps_map.values() {
for (dst, kind, _) in deps {
if kind == "soft" && seen.insert(dst.clone()) {
target_ids.push(dst.clone());
}
}
}
if target_ids.is_empty() {
return Ok(());
}
let target_refs: Vec<&str> = target_ids.iter().map(String::as_str).collect();
let targets = self.storage.get_chunks_by_ids(&target_refs)?;
for src in &src_ids {
let Some(deps) = deps_map.get(src) else {
continue;
};
for (dst, kind, _) in deps {
if kind != "soft" {
continue;
}
let Some(target) = targets.get(dst) else {
continue;
};
if target.get("state").and_then(Value::as_str) == Some("archived") {
continue;
}
if target.get("origin").and_then(Value::as_str) == Some("spark") {
continue;
}
let e = candidates
.entry(dst.clone())
.or_insert_with(|| new_candidate(target));
e.sim_content = (e.sim_content + 0.05).min(1.0);
}
}
Ok(())
}
fn score_candidates(
&self,
candidates: HashMap<String, CandidateInfo>,
query: &str,
context_key: &str,
now: &str,
) -> Result<Vec<(f64, Value)>> {
// Batch-fetch context scores for all candidates in one query
// (was one context_score lookup per candidate).
let cand_ids: Vec<String> = candidates
.values()
.filter_map(|info| {
info.chunk
.get("id")
.and_then(Value::as_str)
.map(str::to_string)
})
.collect();
let cand_refs: Vec<&str> = cand_ids.iter().map(String::as_str).collect();
// 方案 D 与 recall 解耦:recall 恒用中性 Laplace 先验,不读 intuition.* 旋钮。
let ctx_scores = self.storage.context_scores_batch(
&cand_refs,
context_key,
RECALL_PRIOR_M,
RECALL_BASE_RATE,
)?;
let mut scored: Vec<(f64, Value)> = Vec::with_capacity(candidates.len());
for info in candidates.into_values() {
let conf = info
.chunk
.get("confidence")
.and_then(Value::as_f64)
.unwrap_or(0.5);
let chunk_id = info.chunk.get("id").and_then(Value::as_str).unwrap_or("");
let context_score = ctx_scores.get(chunk_id).copied().unwrap_or(0.0);
// ACT-R base-level activation: recency × frequency from usage history.
// Zero for never-used chunks, so freshly-added knowledge is unaffected.
let used_count = info
.chunk
.get("used_count")
.and_then(Value::as_i64)
.unwrap_or(0);
let last_used_at = info.chunk.get("last_used_at").and_then(Value::as_str);
let activation = actr_activation(used_count, last_used_at, now);
let mut fused = self.w_content * info.sim_content as f64
+ self.w_trigger * info.sim_trigger as f64
+ self.w_lexical * info.sim_lexical as f64
+ self.w_confidence * conf
+ self.w_context * context_score
+ self.w_activation * activation;
if info.chunk.get("state").and_then(Value::as_str) == Some("pending") {
fused *= PENDING_RECALL_PENALTY;
}
let anti = info
.chunk
.get("anti_trigger_desc")
.and_then(Value::as_str)
.unwrap_or("");
if !anti.is_empty() && anti_trigger_hit(query, anti) {
fused *= self.anti_trigger_penalty;
}
let mut chunk = info.chunk;
chunk["_context_score"] = json!(context_score);
chunk["_activation"] = json!(activation);
chunk["_sim_lexical"] = json!(info.sim_lexical);
chunk["_fused_score"] = json!(fused);
scored.push((fused, chunk));
}
scored.sort_by(|a, b| b.0.partial_cmp(&a.0).unwrap_or(std::cmp::Ordering::Equal));
scored.truncate(self.top_k_candidates);
Ok(scored)
}
/// Part (d) — reorder the scored shortlist by the injected reranker. Stable:
/// reranked ids move to the front in the reranker's order; anything the reranker
/// omits keeps its fused-relative position. Non-fatal — a reranker error or empty
/// result leaves `scored` untouched, so retrieval never depends on the LLM.
fn apply_rerank(&self, query: &str, scored: &mut [(f64, Value)]) {
let chunks: Vec<Value> = scored.iter().map(|(_, c)| c.clone()).collect();
let order = match self.reranker.rerank(query, &chunks) {
Ok(order) if !order.is_empty() => order,
_ => return,
};
let rank: HashMap<&str, usize> = order
.iter()
.enumerate()
.map(|(i, id)| (id.as_str(), i))
.collect();
scored.sort_by_key(|(_, c)| {
rank.get(c["id"].as_str().unwrap_or(""))
.copied()
.unwrap_or(usize::MAX)
});
}
fn pack(
&self,
scored: &[(f64, Value)],
budget: usize,
expand_deps: &str,
allow_trim: bool,
query: &str,
) -> Result<PackResult> {
let mut selected: Vec<Value> = vec![];
let mut skipped: Vec<(Vec<Value>, f64, usize)> = vec![];
let mut skipped_reasons: HashMap<String, String> = HashMap::new();
let mut used_ids: HashSet<String> = HashSet::new();
let mut used_tokens: usize = 0;
for (fused, chunk) in scored {
let cid = chunk["id"].as_str().unwrap_or("").to_string();
if used_ids.contains(&cid) {
continue;
}
// Build block with dep expansion; fail-closed on dep issues.
let (block, dep_skip_reason) = self.build_dep_block(chunk, expand_deps)?;
if let Some(reason) = dep_skip_reason {
skipped_reasons.insert(cid, reason);
continue;
}
let new_block: Vec<Value> = block
.iter()
.filter(|b| !used_ids.contains(b["id"].as_str().unwrap_or("")))
.cloned()
.collect();
let cost = block_cost(&new_block);
if used_tokens + cost <= budget {
for b in &block {
let bid = b["id"].as_str().unwrap_or("").to_string();
if !used_ids.contains(&bid) {
let mut b = b.clone();
b["_fused_score"] = json!(fused);
selected.push(b);
used_ids.insert(bid);
}
}
used_tokens += cost;
} else if allow_trim {
// Attempt refiner trim — NullRefiner returns None (no-op).
if let Some(trimmed) =
self.refiner
.trim(&block, query, budget.saturating_sub(used_tokens))
{
let trim_cost = block_cost(&trimmed);
if used_tokens + trim_cost <= budget {
for b in &trimmed {
let bid = b["id"].as_str().unwrap_or("").to_string();
if !used_ids.contains(&bid) {
let mut b = b.clone();
b["_fused_score"] = json!(fused);
b["_trimmed"] = json!(true);
selected.push(b);
used_ids.insert(bid);
}
}
used_tokens += trim_cost;
continue;
}
}
skipped.push((block, *fused, cost));
} else {
skipped.push((block, *fused, cost));
}
}
Ok((selected, skipped, skipped_reasons))
}
/// Expand a seed chunk into a block according to `expand_deps`.
/// Returns `(block, Some(skip_reason))` if the block should be discarded (fail-closed).
fn build_dep_block(
&self,
seed: &Value,
expand_deps: &str,
) -> Result<(Vec<Value>, Option<String>)> {
if expand_deps == "false" || expand_deps.is_empty() {
return Ok((vec![seed.clone()], None));
}
let seed_id = seed["id"].as_str().unwrap_or("");
match expand_deps {
"direct" => {
let deps = self.storage.get_deps(seed_id)?;
let mut block = vec![seed.clone()];
for (dep_id, kind, _) in &deps {
if kind != "hard" {
continue;
}
match self.validate_hard_dep(dep_id)? {
Some(chunk) => block.push(chunk),
None => return Ok((vec![], Some("hard_dep_unavailable".to_string()))),
}
}
Ok((block, None))
}
"closure" => {
let mut block = vec![seed.clone()];
let mut visited: HashSet<String> = [seed_id.to_string()].into();
match self.expand_hard_closure(seed_id, &mut visited, &mut block, 0, 3)? {
Some(reason) => Ok((vec![], Some(reason))),
None => Ok((block, None)),
}
}
_ => Ok((vec![seed.clone()], None)),
}
}
/// Returns the chunk if the hard dep is usable, None if it should cause fail-closed.
fn validate_hard_dep(&self, dep_id: &str) -> Result<Option<Value>> {
match self.storage.get_chunk(dep_id)? {
None => Ok(None),
Some(chunk) => {
let state = chunk.get("state").and_then(Value::as_str).unwrap_or("");
let origin = chunk.get("origin").and_then(Value::as_str).unwrap_or("");
let embed_v = chunk
.get("embed_version")
.and_then(Value::as_i64)
.unwrap_or(0);
if state == "archived" || origin == "spark" || embed_v == 0 {
Ok(None)
} else {
Ok(Some(chunk))
}
}
}
}
/// BFS hard-dep expansion up to `max_depth`. Returns Some(reason) on fail-closed.
fn expand_hard_closure(
&self,
id: &str,
visited: &mut HashSet<String>,
block: &mut Vec<Value>,
depth: usize,
max_depth: usize,
) -> Result<Option<String>> {
if depth >= max_depth {
return Ok(Some("dep_depth_limit".to_string()));
}
let deps = self.storage.get_deps(id)?;
for (dep_id, kind, _) in &deps {
if kind != "hard" {
continue;
}
if visited.contains(dep_id) {
continue;
} // cycle guard
visited.insert(dep_id.clone());
match self.validate_hard_dep(dep_id)? {
None => return Ok(Some("hard_dep_unavailable".to_string())),
Some(chunk) => {
block.push(chunk);
if let Some(reason) =
self.expand_hard_closure(dep_id, visited, block, depth + 1, max_depth)?
{
return Ok(Some(reason));
}
}
}
}
Ok(None)
}
fn density_refill(
&self,
mut selected: Vec<Value>,
skipped: &[(Vec<Value>, f64, usize)],
budget: usize,
) -> Vec<Value> {
let used_tokens = block_cost(&selected);
if used_tokens >= budget {
return selected;
}
let selected_ids: HashSet<String> = selected
.iter()
.filter_map(|c| c["id"].as_str().map(str::to_string))
.collect();
let mut density_items: Vec<(f64, Vec<Value>, usize)> = skipped
.iter()
.filter_map(|(block, fscore, _)| {
let block: Vec<Value> = block
.iter()
.filter(|b| !selected_ids.contains(b["id"].as_str().unwrap_or("")))
.cloned()
.collect();
if block.is_empty() {
return None;
}
let cost = block_cost(&block);
let density = fscore / cost.max(1) as f64;
Some((density, block, cost))
})
.collect();
density_items.sort_by(|a, b| b.0.partial_cmp(&a.0).unwrap_or(std::cmp::Ordering::Equal));
let mut used_tokens = block_cost(&selected);
let mut added_ids: HashSet<String> = selected_ids;
for (_, block, cost) in density_items {
if used_tokens + cost <= budget {
for b in block {
let bid = b["id"].as_str().unwrap_or("").to_string();
if !added_ids.contains(&bid) {
selected.push(b);
added_ids.insert(bid);
}
}
used_tokens += cost;
}
}
selected
}
fn recall_sparks(&self, q_content: &[f32], q_trigger: &[f32]) -> Result<Vec<Value>> {
let embed_version = self
.storage
.get_meta("embed_version")?
.and_then(|v| v.parse::<i64>().ok())
.unwrap_or(1);
let content_res = self
.storage
.search_vec_content(q_content, self.top_k_candidates)?;
let trigger_res = self
.storage
.search_vec_trigger(q_trigger, self.top_k_candidates)?;
// Batch-fetch all candidate chunk IDs (mirrors the pattern in ann_candidates).
let all_ids: Vec<&str> = {
let mut seen = HashSet::new();
content_res
.iter()
.chain(trigger_res.iter())
.map(|(id, _)| id.as_str())
.filter(|id| seen.insert(*id))
.collect()
};
let chunks = self.storage.get_chunks_by_ids(&all_ids)?;
let mut spark_scores: HashMap<String, (f32, Value)> = HashMap::new();
for (cid, sim) in content_res.iter().chain(trigger_res.iter()) {
if let Some(chunk) = chunks.get(cid) {
if chunk.get("origin").and_then(Value::as_str) != Some("spark") {
continue;
}
if chunk.get("state").and_then(Value::as_str) == Some("archived") {
continue;
}
let maturity = chunk.get("maturity").and_then(Value::as_str).unwrap_or("");
if maturity == "promoted" || maturity == "dropped" {
continue;
}
let ev = chunk
.get("embed_version")
.and_then(Value::as_i64)
.unwrap_or(1);
if ev < embed_version {
continue;
}
let entry = spark_scores
.entry(cid.clone())
.or_insert_with(|| (*sim, chunk.clone()));
if *sim > entry.0 {
*entry = (*sim, chunk.clone());
}
}
}
let mut sparks: Vec<(f32, Value)> = spark_scores.into_values().collect();
sparks.sort_by(|a, b| b.0.partial_cmp(&a.0).unwrap_or(std::cmp::Ordering::Equal));
Ok(sparks
.into_iter()
.take(self.top_k_candidates)
.map(|(_, c)| c)
.collect())
}
#[allow(clippy::too_many_arguments)]
fn write_recall_trace(
&self,
trace_id: &str,
query: &str,
context_key: &str,
scored: &[(f64, Value)],
visible: &[Value],
sparks: &[Value],
depth_skipped: &[String],
skipped_reasons: &HashMap<String, String>,
refine_mode: &str,
source: &str,
now: &str,
session_only: bool,
) -> Result<()> {
let lib_id = self.storage.lib_id()?;
// `selected` must strictly mean "entered the model context". Record the
// per-chunk retrieved/selected/refined events only when the result is
// actually surfaced: skip them for empty results (nothing surfaced) and
// for session-only recalls (daemon discards the knowledge). The episodic
// log is still written in both cases — an empty result as a terminal
// `known_none`/`discarded` row (no-answer telemetry, never `open`), a
// session recall as an `open` row for later record-correlation.
let is_empty = visible.is_empty() && sparks.is_empty();
let record_selection = !is_empty && !session_only;
self.storage.begin_immediate()?;
let result = (|| -> Result<()> {
if record_selection {
for (rank, (_, chunk)) in scored.iter().enumerate() {
let cid = chunk["id"].as_str().unwrap_or("");
let sim = chunk.get("_fused_score").and_then(Value::as_f64);
// For dep-skipped seeds, record their skip reason as refine_mode.
let rm = skipped_reasons
.get(cid)
.map(|r| format!("skipped:{r}"))
.or_else(|| {
if refine_mode != "off" && !refine_mode.is_empty() {
Some(refine_mode.to_string())
} else {
None
}
});
self.storage.insert_usage_trace(
trace_id,
Some(cid),
"retrieved",
1.0,
sim,
rm.as_deref(),
None,
Some((rank + 1) as i64),
None,
source,
now,
)?;
}
for (rank, chunk) in visible.iter().enumerate() {
let cid = chunk["id"].as_str().unwrap_or("");
self.storage.insert_usage_trace(
trace_id,
Some(cid),
"selected",
1.0,
None,
None,
None,
Some((rank + 1) as i64),
None,
source,
now,
)?;
// Write 'refined' event for chunks that came through the trim path.
if chunk
.get("_trimmed")
.and_then(Value::as_bool)
.unwrap_or(false)
{
self.storage.insert_usage_trace(
trace_id,
Some(cid),
"refined",
1.0,
None,
Some("trim"),
None,
Some((rank + 1) as i64),
None,
source,
now,
)?;
}
}
// Write 'retrieved' events for sparks (for recurring-spark count tracking).
for (rank, chunk) in sparks.iter().enumerate() {
let cid = chunk["id"].as_str().unwrap_or("");
self.storage.insert_usage_trace(
trace_id,
Some(cid),
"retrieved",
1.0,
None,
Some("spark"),
None,
Some((rank + 1) as i64),
None,
source,
now,
)?;
}
}
// The snapshot mirrors what was surfaced: empty for known_none and
// session-only recalls so no chunk is credited with a selection.
let snapshot = json!({
"retrieved": if record_selection { scored.iter().map(|(_, c)| c["id"].as_str().unwrap_or("")).collect::<Vec<_>>() } else { vec![] },
"selected": if record_selection { visible.iter().map(|c| c["id"].as_str().unwrap_or("")).collect::<Vec<_>>() } else { vec![] },
"sparks": if record_selection { sparks.iter().map(|c| c["id"].as_str().unwrap_or("")).collect::<Vec<_>>() } else { vec![] },
"depth_skipped": depth_skipped,
"skipped_reasons": skipped_reasons,
"session_only": session_only,
});
// Empty recall → terminal known_none/discarded (kept out of the `open`
// pool that feeds trace-completion stats). Otherwise `open` for the
// record() outcome transition (incl. session-only daemon traces).
// `usage_state='known_none'` is the no-answer signal; `task_state`
// stays 'recalled' (both bounded by schema CHECK constraints).
let (usage_state, distill_state) = if is_empty {
("known_none", "discarded")
} else {
("unknown", "open")
};
let log = EpisodicLogRow {
id: gen_uuid(),
trace_id: trace_id.to_string(),
lib_id,
ts: now.to_string(),
query: Some(query.to_string()),
recall_snapshot: Some(snapshot.to_string()),
event_source: source.to_string(),
agent: agent_source(),
task_state: "recalled".to_string(),
usage_state: usage_state.to_string(),
context_key: Some(context_key.to_string()),
distill_state: distill_state.to_string(),
..Default::default()
};
self.storage.upsert_episodic_log(&log)?;
self.storage.commit()
})();
if result.is_err() {
let _ = self.storage.rollback();
}
result
}
}