khive-pack-knowledge 0.14.0

Knowledge verb pack — lore corpus (atoms/domains), TF-IDF retrieval, concept registration
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
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
use std::collections::{HashMap, HashSet};

use khive_runtime::{
    hex_prefix_to_uuid_pattern, KhiveRuntime, Namespace, NamespaceToken, RuntimeError,
};
use khive_storage::types::{SqlStatement, SqlValue};
use serde_json::{json, Value};
use uuid::Uuid;

use super::{
    apply_status_multipliers, atom_embed_text, atom_from_row,
    attach_body_lines_timeout_degradation, attach_hydration_degradation,
    attach_lexical_timeout_degradation, attach_member_sizing_timeout_degradation,
    candidate_fallback, deprecated_allowed_by_status_policy, deser, enforce_min_score_floor,
    filter_hits_by_status, filter_hits_by_type, format_kg_entities_markdown, fuse_ann_hits,
    is_stop, load_atom_body_line_counts, load_domain_by_id_or_slug, load_domain_member_token_sizes,
    pack_compose_markdown, parse_domain_members, rerank_search_with_stored_vectors,
    rerank_text_items, rerank_with_embeddings, row_i64, run_candidate_stages, search_core,
    search_decomposed, search_kg_entities, sql_err, status_values, trim_kg_entities_to_budget,
    vamana, AnnAvailability, Atom, CandidateStageOptions, CandidateStageOutcome, ComposeParams,
    Domain, FtsTermBudget, KnowledgeHandlers, LexicalPass, QueryEmbeddingCache,
    RoleSpecificEmbedding, ScoredTextItem, SearchCoreOutcome, SearchCtx, SearchParams,
    SuggestParams, Weights, CHARS_PER_TOKEN, D_SUGGEST_RERANK_ALPHA, KG_BLEND_CAP, MIN_TERM_LEN,
};
#[cfg(test)]
use super::{compose_read_tests, MEMBER_SIZING_READ_TIMEOUT};

// ─── handler impls ────────────────────────────────────────────────────────────

impl KnowledgeHandlers {
    pub(crate) async fn search(
        runtime: &KhiveRuntime,
        token: &NamespaceToken,
        params: Value,
        ann: &vamana::SharedAnn,
    ) -> Result<Value, RuntimeError> {
        khive_storage::ensure_request_read_active("knowledge.search")?;
        let p: SearchParams = deser(params)?;
        let raw_query = p.query.trim().to_string();
        if raw_query.is_empty() {
            return Err(RuntimeError::InvalidInput("query must not be empty".into()));
        }

        if let Some(ms) = p.min_score {
            if !ms.is_finite() {
                return Err(RuntimeError::InvalidInput(
                    "min_score must be a finite number".into(),
                ));
            }
        }
        if let Some(ib) = p.intersection_bonus {
            if !ib.is_finite() {
                return Err(RuntimeError::InvalidInput(
                    "intersection_bonus must be a finite number".into(),
                ));
            }
        }
        if let Some(ra) = p.rerank_alpha {
            if !ra.is_finite() {
                return Err(RuntimeError::InvalidInput(
                    "rerank_alpha must be a finite number".into(),
                ));
            }
        }
        if let Some(ref w) = p.weights {
            let pairs: &[(&str, Option<f64>)] = &[
                ("w_exact_name", w.w_exact_name),
                ("w_name", w.w_name),
                ("w_tags", w.w_tags),
                ("w_content", w.w_content),
                ("expand_discount", w.expand_discount),
                ("coverage_alpha", w.coverage_alpha),
                ("w_bigram", w.w_bigram),
            ];
            for (name, val) in pairs {
                if let Some(v) = val {
                    if !v.is_finite() {
                        return Err(RuntimeError::InvalidInput(format!(
                            "weights.{name} must be a finite number"
                        )));
                    }
                }
            }
        }

        let limit = p.limit.unwrap_or(10).clamp(1, 100);
        let min_score = p.min_score.unwrap_or(0.0) as f32;
        let w = Weights::from_opts(&p);
        if let Some(kind) = p.kind.as_deref() {
            if !matches!(kind, "atom" | "domain") {
                return Err(RuntimeError::InvalidInput(format!(
                    "kind must be one of: atom, domain; got {kind:?}"
                )));
            }
        }
        let type_filter = p.kind.as_deref();
        let do_decompose = p.decompose.unwrap_or(false);
        let decompose_threshold = p.decompose_threshold.unwrap_or(4);
        let intersection_bonus = p.intersection_bonus.unwrap_or(0.25) as f32;
        let requested_rerank = p.rerank.unwrap_or(true);
        let do_rerank = requested_rerank && !runtime.default_embedder_name().is_empty();
        let rerank_alpha = p.rerank_alpha.unwrap_or(0.7) as f32;
        let fetch_limit = if do_rerank { limit * 3 } else { limit }.min(100);

        let non_stop_count = raw_query
            .split_whitespace()
            .filter(|w| w.len() >= MIN_TERM_LEN && !is_stop(&w.to_lowercase()))
            .count();

        let ns = token.namespace().as_str().to_owned();
        if let Some(status) = p.status.as_ref() {
            let valid_shape = match status {
                Value::String(_) => true,
                Value::Array(items) => items.iter().all(Value::is_string),
                _ => false,
            };
            if !valid_shape {
                return Err(RuntimeError::InvalidInput(
                    "status must be a string or an array of strings".into(),
                ));
            }
        }
        let requested_statuses = status_values(p.status.as_ref());

        // Normalize exclude_status once: trim whitespace, treat blank as absent.
        // This single normalized value feeds both the SQL predicate (via SearchCtx)
        // and the ANN post-hydration filter, ensuring both result sources see the
        // identical exclusion set regardless of how the caller formatted the value.
        let exclude_status_normalized: Option<&str> = p
            .exclude_status
            .as_deref()
            .map(str::trim)
            .filter(|s| !s.is_empty());
        if let Some(status) = exclude_status_normalized {
            if !matches!(status, "draft" | "reviewed" | "deprecated") {
                return Err(RuntimeError::InvalidInput(format!(
                    "exclude_status must be one of: draft, reviewed, deprecated; got {status:?}"
                )));
            }
        }

        // Precedence (highest to lowest, matches ADR-047 §Status filtering):
        //   1. explicit status=  → no exclusion; SQL and hydrated ANN use the allowlist
        //   2. no status=, explicit exclude_status= (non-blank) → use that exclusion
        //   3. no status=, include_drafts=true → exclude only deprecated
        //   4. default (no status params / blank exclude_status) → exclude draft and deprecated
        let effective_exclude_statuses: Vec<&str> = if !requested_statuses.is_empty() {
            // Caller specified exact status; the shared allowlist wins.
            vec![]
        } else if let Some(ex) = exclude_status_normalized {
            vec![ex]
        } else {
            let include_drafts = p.include_drafts.unwrap_or(false);
            if include_drafts {
                vec!["deprecated"]
            } else {
                vec!["draft", "deprecated"]
            }
        };
        // The zero multiplier for deprecated is also a final eligibility gate.
        // Resolve its override from the same precedence policy used before FTS
        // caps and ANN refill; otherwise explicit exclude_status can admit a
        // row early only for the multiplier stage to remove it later.
        let allow_deprecated =
            deprecated_allowed_by_status_policy(&requested_statuses, &effective_exclude_statuses);

        let term_budget = FtsTermBudget::new();
        let ctx = SearchCtx {
            runtime,
            ns: &ns,
            role: p.role.as_deref(),
            type_filter,
            min_score,
            w: &w,
            fetch_limit,
            statuses: &requested_statuses,
            exclude_statuses: &effective_exclude_statuses,
            term_budget: &term_budget,
        };

        let ann_k = fetch_limit.max(20);
        let (
            SearchCoreOutcome {
                mut hits,
                lexical_timeouts,
                lexical_state,
            },
            CandidateStageOutcome {
                ann_hits,
                ann_availability,
                hydration_failures,
                mut query_embedding,
            },
        ) = run_candidate_stages(
            &ctx,
            token,
            ann,
            &raw_query,
            CandidateStageOptions {
                query_embedding: QueryEmbeddingCache::default(),
                ann_target: ann_k,
                ann_initial_k: ann_k,
                operation: "knowledge.search",
            },
            async {
                if do_decompose && non_stop_count >= decompose_threshold {
                    search_decomposed(&ctx, &raw_query, intersection_bonus).await
                } else {
                    search_core(&ctx, &raw_query, LexicalPass::Full).await
                }
            },
        )
        .await?;

        let mut ann_unavailable = false;
        if !ann_hits.is_empty() {
            fuse_ann_hits(&mut hits, &ann_hits, min_score);
        }
        // FTS hits remain valid partial results. Preserve the existing
        // advisory only for a non-empty corpus with no lexical fallback.
        if matches!(
            ann_availability,
            Some(AnnAvailability::WarmingTimedOut {
                corpus_non_empty: true
            })
        ) && hits.is_empty()
        {
            ann_unavailable = true;
        }
        // Apply shared eligibility unconditionally so every source observes the
        // same final status and kind contract even when ANN did not run.
        filter_hits_by_status(&mut hits, &requested_statuses, &effective_exclude_statuses);
        filter_hits_by_type(&mut hits, type_filter);

        // The lexical stage now owns its own budget (issue #1930 Amendment
        // 2), so `lexical_timeout` no longer implies the request read
        // deadline is spent — only that stage's narrower budget is. Gate on
        // the live ambient deadline instead: a lexical-only degradation
        // with request time left to spare still gets its embedding rerank.
        let mut rerank_provenance = None;
        if do_rerank && !hits.is_empty() && !khive_storage::request_read_is_cancelled() {
            rerank_provenance = rerank_search_with_stored_vectors(
                runtime,
                token,
                &ns,
                &raw_query,
                &mut query_embedding,
                &mut hits,
                rerank_alpha,
            )
            .await?;
        }

        apply_status_multipliers(&mut hits, allow_deprecated);
        enforce_min_score_floor(&mut hits, min_score);
        hits.truncate(limit);

        let atom_ids: Vec<String> = hits
            .iter()
            .filter(|hit| !hit.is_domain)
            .map(|hit| hit.id.clone())
            .collect();
        let mut body_lines_timed_out = false;
        let body_line_counts = if khive_storage::request_read_is_cancelled() {
            None
        } else {
            match load_atom_body_line_counts(runtime, &ns, &atom_ids).await? {
                Some(counts) => Some(counts),
                None => {
                    body_lines_timed_out = true;
                    None
                }
            }
        };

        let results: Vec<Value> = hits
            .iter()
            .map(|h| {
                let body_lines = if h.is_domain {
                    None
                } else {
                    body_line_counts
                        .as_ref()
                        .and_then(|counts| counts.get(&h.id).copied())
                };
                json!({
                    "id": h.id,
                    "slug": h.slug,
                    "name": h.name,
                    "content": h.content,
                    "body_lines": body_lines,
                    "tags": h.tags,
                    "status": h.status,
                    "finalized": h.finalized,
                    "kind": if h.is_domain { "domain" } else { "atom" },
                    "score": h.score,
                    "score_provenance": h.provenance.to_json(),
                })
            })
            .collect();
        let count = results.len();

        let mut out = json!({
            "results": results,
            "total": count,
            "candidate_provenance": {
                "lexical": lexical_state.as_str(),
                "fallback": candidate_fallback(&hits),
                "terms_truncated": term_budget.truncated(),
            },
        });
        if let Some(provenance) = rerank_provenance {
            out["rerank_provenance"] = provenance;
        }
        if ann_unavailable {
            out["ann_unavailable"] = json!(true);
        }
        attach_lexical_timeout_degradation(&mut out, &lexical_timeouts);
        if body_lines_timed_out {
            attach_body_lines_timeout_degradation(&mut out);
        }
        attach_hydration_degradation(&mut out, hydration_failures);
        // The lexical stage's own budget no longer implies the request
        // deadline is spent (issue #1930 Amendment 2), so this last check
        // re-reads the live ambient deadline rather than the stage-local
        // flags: skip only when the request has actually stopped, never a
        // verb-level error for a degradation already reported above.
        if !khive_storage::request_read_is_cancelled() {
            khive_storage::ensure_request_read_active("knowledge.search")?;
        }
        Ok(out)
    }

    /// Suggest domains with measured compose-member costs and live member counts.
    /// Present domains without live members have size zero and members zero.
    /// Every unmeasured domain is withheld under the stable
    /// `degraded.member_sizing_timeout.excluded` key, whether sizing timed out or not.
    pub(crate) async fn suggest(
        runtime: &KhiveRuntime,
        token: &NamespaceToken,
        params: Value,
        ann: &vamana::SharedAnn,
    ) -> Result<Value, RuntimeError> {
        let (out, _) = Self::suggest_with_query_embedding(
            runtime,
            token,
            params,
            ann,
            QueryEmbeddingCache::default(),
        )
        .await?;
        Ok(out)
    }

    /// Internal suggest path that accepts and returns the request's cached
    /// query vector. Auto-compose calls this directly so its suggest, atom,
    /// section, and KG stages all share one successful query embedding.
    async fn suggest_with_query_embedding(
        runtime: &KhiveRuntime,
        token: &NamespaceToken,
        params: Value,
        ann: &vamana::SharedAnn,
        query_embedding: QueryEmbeddingCache,
    ) -> Result<(Value, QueryEmbeddingCache), RuntimeError> {
        khive_storage::ensure_request_read_active("knowledge.suggest")?;
        let p: SuggestParams = deser(params)?;
        let raw_query = p.query.trim().to_string();
        if raw_query.is_empty() {
            return Err(RuntimeError::InvalidInput("query must not be empty".into()));
        }
        let word_count = raw_query.split_whitespace().count();
        if word_count < 5 {
            return Err(RuntimeError::InvalidInput(format!(
                "suggest query must be at least 5 words for meaningful domain matching \
                 (got {word_count}). Use knowledge.search for short keyword queries."
            )));
        }
        let limit = p.limit.unwrap_or(8).clamp(1, 100);
        let ns = token.namespace().as_str().to_owned();

        // Exclude draft and deprecated domain atoms by default — same quality
        // default as knowledge.search.  Draft domain atoms are incomplete and
        // should not drive auto-compose or agent orientation.
        const SUGGEST_EXCLUDE: &[&str] = &["draft", "deprecated"];

        let term_budget = FtsTermBudget::new();
        let ctx = SearchCtx {
            runtime,
            ns: &ns,
            role: p.role.as_deref(),
            type_filter: Some("domain"),
            min_score: 0.0,
            w: &Weights::default(),
            fetch_limit: limit * 3,
            statuses: &[],
            exclude_statuses: SUGGEST_EXCLUDE,
            term_budget: &term_budget,
        };

        // Over-fetch aggressively: the corpus is ~27% domains / ~73% atoms, so
        // limit*3 would return mostly atoms that all get dropped after type filtering.
        // 50× over-fetch (floor 200) gives domains a fair chance to appear in the
        // top ANN neighbors before the type gate discards atom hits.
        let ann_k = (limit * 50).max(200);
        let (
            SearchCoreOutcome {
                mut hits,
                lexical_timeouts,
                ..
            },
            CandidateStageOutcome {
                ann_hits,
                ann_availability,
                hydration_failures,
                mut query_embedding,
            },
        ) = run_candidate_stages(
            &ctx,
            token,
            ann,
            &raw_query,
            CandidateStageOptions {
                query_embedding,
                ann_target: ctx.fetch_limit,
                ann_initial_k: ann_k,
                operation: "knowledge.suggest",
            },
            search_core(&ctx, &raw_query, LexicalPass::Full),
        )
        .await?;

        let mut ann_unavailable = false;
        if !ann_hits.is_empty() {
            fuse_ann_hits(&mut hits, &ann_hits, 0.0);
        }
        // Suggest always reports degraded candidate recall for a
        // non-empty corpus, even when lexical candidates survived.
        if let Some(AnnAvailability::WarmingTimedOut { corpus_non_empty }) = ann_availability {
            ann_unavailable = corpus_non_empty;
        }

        filter_hits_by_status(&mut hits, &[], SUGGEST_EXCLUDE);
        filter_hits_by_type(&mut hits, Some("domain"));

        // The lexical stage now owns its own budget (issue #1930 Amendment
        // 2), so `lexical_timeout` no longer implies the request read
        // deadline is spent — only that stage's narrower budget is. Gate on
        // the live ambient deadline instead: a lexical-only degradation
        // with request time left to spare still gets its embedding rerank.
        let fresh_rerank_applied = if khive_storage::request_read_is_cancelled() {
            false
        } else {
            rerank_with_embeddings(
                runtime,
                &raw_query,
                &mut query_embedding,
                &mut hits,
                D_SUGGEST_RERANK_ALPHA,
            )
            .await?
        };

        // Safety net: retain only domain hits in case any non-domain survived above.
        hits.retain(|h| h.is_domain);
        hits.truncate(limit);

        let domain_ids: Vec<String> = hits.iter().map(|h| h.id.clone()).collect();
        // A cancelled ambient deadline skips the call the same way an
        // internal timeout inside it does — both leave every domain in this
        // batch unmeasured, never a measured member cost
        // (issue #2396 fix 3).
        let (member_token_sizes, member_sizing_timed_out) =
            if khive_storage::request_read_is_cancelled() {
                (HashMap::new(), !domain_ids.is_empty())
            } else {
                #[cfg(test)]
                let measured = if MEMBER_SIZING_READ_TIMEOUT.try_with(|_| ()).is_ok() {
                    khive_storage::scope_request_read_deadline(
                        std::time::Duration::ZERO,
                        load_domain_member_token_sizes(runtime, &ns, &domain_ids),
                    )
                    .await
                } else {
                    load_domain_member_token_sizes(runtime, &ns, &domain_ids).await
                };
                #[cfg(not(test))]
                let measured = load_domain_member_token_sizes(runtime, &ns, &domain_ids).await;
                measured?
            };
        if !khive_storage::request_read_is_cancelled() {
            khive_storage::ensure_request_read_active("knowledge.suggest")?;
        }
        // A domain the sizing pass returned no row for is unmeasured as well:
        // a missing entry never defaults to a fabricated zero.
        let unmeasured_domain_ids: HashSet<&str> = domain_ids
            .iter()
            .map(String::as_str)
            .filter(|id| member_sizing_timed_out || !member_token_sizes.contains_key(*id))
            .collect();

        // Price the member atom bodies that compose expands, not the much smaller
        // domain mirror description used for retrieval. The batched join keeps the
        // suggest -> fold budget in compose's estimated-token unit without an N+1
        // hydration pass. A domain whose members were not measured is withheld
        // from `results` entirely and reported under
        // `degraded.member_sizing_timeout.excluded` instead — `suggest`'s
        // documented contract (issue #105) is that `results` feeds
        // `knowledge.fold`'s `candidates` unmodified, and `FoldCandidate::size`
        // is a non-optional `usize`, so leaving an unpriced item in `results`
        // (as `size: null`, issue #2396 fix 3) turned one unmeasured domain into
        // a hard parse error for the whole fold request. Exclusion keeps the
        // passthrough valid while still refusing to let an unpriced domain enter
        // a budgeted fold selection for free.
        let results: Vec<Value> = hits
            .iter()
            .filter(|h| !unmeasured_domain_ids.contains(h.id.as_str()))
            .filter_map(|h| {
                let sizing = member_token_sizes.get(&h.id)?;
                Some(json!({
                    "id": h.id,
                    "name": h.name,
                    "score": h.score,
                    "size": sizing.tokens,
                    "members": sizing.live_members,
                }))
            })
            .collect();
        let count = results.len();
        let excluded: Vec<Value> = hits
            .iter()
            .enumerate()
            .filter(|(_, h)| unmeasured_domain_ids.contains(h.id.as_str()))
            .map(|(i, h)| {
                json!({
                    "id": h.id,
                    "name": h.name,
                    "rank": i + 1,
                    "score": h.score,
                })
            })
            .collect();

        let mut out = json!({ "results": results, "total": count });
        if ann_unavailable {
            // issue #91: escalate degradation to a top-level, self-explaining
            // signal instead of a bare total:0 or an unflagged partial list.
            // `ann_unavailable` is kept unchanged for existing callers; `degraded`
            // states the consequence so a caller does not have to infer it.
            out["ann_unavailable"] = json!(true);
            let (mode, note): (&str, &str) = match (count, fresh_rerank_applied) {
                (0, _) => (
                    "no_match",
                    "ANN index unavailable and lexical/FTS matching also found no \
                     domain for this query. This does NOT confirm the corpus has \
                     nothing relevant — only that this call could not find one. \
                     Do not cache as an absence; retry once the index is healthy.",
                ),
                (_, true) => (
                    "ann_candidates_degraded",
                    "ANN index unavailable: candidate retrieval used lexical/FTS \
                     matching, but fresh embedding cosine reranking was applied to \
                     those candidates. Final ranking includes a dense signal, while \
                     topically relevant domains outside the lexical candidate set may \
                     still be missing. Do not cache; retry once the index is healthy.",
                ),
                (_, false) => (
                    "lexical_only",
                    "ANN index unavailable and fresh embedding reranking did not run: \
                     these results were ranked by lexical/FTS matching only. Ranking \
                     may be less precise than a healthy call, and topically relevant \
                     domains outside the lexical match may be missing. Do not cache; \
                     retry once the index is healthy.",
                ),
            };
            out["degraded"] = json!({
                "reason": "ann_unavailable",
                "mode": mode,
                "cache_safe": false,
                "note": note,
            });
        }
        attach_lexical_timeout_degradation(&mut out, &lexical_timeouts);
        attach_hydration_degradation(&mut out, hydration_failures);
        attach_member_sizing_timeout_degradation(&mut out, &excluded);
        // The lexical stage's own budget no longer implies the request
        // deadline is spent (issue #1930 Amendment 2), so this last check
        // re-reads the live ambient deadline rather than the stage-local
        // flag: skip only when the request has actually stopped, never a
        // verb-level error for a degradation already reported above.
        if !khive_storage::request_read_is_cancelled() {
            khive_storage::ensure_request_read_active("knowledge.suggest")?;
        }
        Ok((out, query_embedding))
    }

    pub(super) const COMPOSE_ATOM_CHUNK_SIZE: usize = 64;

    pub(super) fn compose_domain_member_ids(atoms: &[Atom], members: &[String]) -> HashSet<String> {
        let mut first_by_slug = HashMap::with_capacity(atoms.len());
        for atom in atoms {
            first_by_slug.entry(atom.slug.as_str()).or_insert(atom.id);
        }
        members
            .iter()
            .filter_map(|slug| first_by_slug.get(slug.as_str()))
            .map(|id| id.to_string())
            .collect()
    }

    pub(super) async fn load_compose_atom_window(
        runtime: &KhiveRuntime,
        ns: &str,
        references: &[String],
    ) -> Result<Vec<Result<Atom, RuntimeError>>, RuntimeError> {
        if references.is_empty() {
            return Ok(Vec::new());
        }
        assert!(references.len() <= Self::COMPOSE_ATOM_CHUNK_SIZE);
        khive_storage::ensure_request_read_active("knowledge.compose")?;
        let raw: Vec<_> = references
            .iter()
            .map(|reference| reference.trim())
            .collect();
        let mut inputs = Vec::with_capacity(raw.len());
        let mut params = vec![SqlValue::Text(ns.to_owned())];
        for (ordinal, reference) in raw.iter().enumerate() {
            let is_uuid = Uuid::parse_str(reference).is_ok();
            let is_prefix = !is_uuid
                && reference.len() >= 8
                && reference.len() <= 36
                && reference
                    .chars()
                    .all(|character| character.is_ascii_hexdigit() || character == '-');
            let first = params.len() + 1;
            inputs.push(format!(
                "({ordinal},?{first},{},?{})",
                usize::from(is_uuid),
                first + 1
            ));
            params.push(SqlValue::Text((*reference).to_owned()));
            params.push(if is_prefix {
                SqlValue::Text(format!("{}%", hex_prefix_to_uuid_pattern(reference)))
            } else {
                SqlValue::Null
            });
        }
        let statement = SqlStatement {
            sql: format!(
                "WITH input(ordinal,raw_ref,is_uuid,prefix) AS (VALUES {}) \
                 SELECT input.ordinal AS compose_ordinal, \
                    CASE WHEN input.is_uuid=0 AND NOT EXISTS( \
                        SELECT 1 FROM knowledge_atoms slug WHERE slug.slug=input.raw_ref \
                        AND slug.namespace=?1 AND slug.deleted_at IS NULL) THEN 1 ELSE 0 END AS compose_prefix, atom.* \
                 FROM input JOIN knowledge_atoms atom ON \
                    atom.rowid IN(SELECT by_id.rowid FROM knowledge_atoms by_id \
                        WHERE input.is_uuid=1 AND by_id.id=input.raw_ref \
                        AND by_id.namespace=?1 AND by_id.deleted_at IS NULL \
                        UNION ALL SELECT by_slug.rowid FROM knowledge_atoms by_slug \
                        WHERE input.is_uuid=0 AND by_slug.slug=input.raw_ref \
                        AND by_slug.namespace=?1 AND by_slug.deleted_at IS NULL LIMIT 1) \
                    OR atom.rowid IN(SELECT prefixed.rowid FROM knowledge_atoms prefixed \
                        WHERE input.is_uuid=0 AND input.prefix IS NOT NULL \
                        AND NOT EXISTS(SELECT 1 FROM knowledge_atoms slug \
                            WHERE slug.slug=input.raw_ref AND slug.namespace=?1 AND slug.deleted_at IS NULL) \
                        AND prefixed.namespace=?1 AND prefixed.deleted_at IS NULL \
                        AND prefixed.id LIKE input.prefix LIMIT 2) \
                 ORDER BY input.ordinal",
                inputs.join(",")
            ),
            params,
            label: Some("compose_atom_window".into()),
        };
        let context = if Uuid::parse_str(raw[0]).is_ok() {
            "compose atom by id"
        } else {
            "compose atom by slug"
        };
        let access = runtime.sql();
        let mut reader = access
            .reader()
            .await
            .map_err(|error| sql_err("compose atom reader", error))?;
        #[cfg(test)]
        compose_read_tests::observe_reader();
        #[cfg(test)]
        let before = runtime
            .backend()
            .pool()
            .reader_acquisition_snapshot()
            .pooled_checkouts;
        let rows = reader
            .query_all(statement)
            .await
            .map_err(|error| sql_err(context, error))?;
        #[cfg(test)]
        compose_read_tests::observe_query(runtime, before);
        let mut aligned: Vec<Vec<khive_storage::types::SqlRow>> =
            (0..raw.len()).map(|_| Vec::new()).collect();
        for row in rows {
            let ordinal = row_i64(&row, "compose_ordinal")
                .and_then(|ordinal| usize::try_from(ordinal).ok())
                .filter(|ordinal| *ordinal < aligned.len())
                .ok_or_else(|| {
                    RuntimeError::Internal("invalid compose atom window ordinal".into())
                })?;
            aligned[ordinal].push(row);
        }
        let outcomes = raw
            .iter()
            .zip(aligned)
            .map(|(reference, rows)| {
                if rows.len() > 1
                    && rows.first().and_then(|row| row_i64(row, "compose_prefix")) == Some(1)
                {
                    return Err(RuntimeError::InvalidInput(format!(
                        "ambiguous atom prefix {reference:?} matches multiple atoms"
                    )));
                }
                rows.into_iter()
                    .next()
                    .and_then(|row| atom_from_row(&row))
                    .ok_or_else(|| RuntimeError::NotFound(format!("atom not found: {reference:?}")))
            })
            .collect();
        #[cfg(test)]
        compose_read_tests::pause_after_window().await;
        Ok(outcomes)
    }

    pub(crate) async fn compose(
        runtime: &KhiveRuntime,
        token: &NamespaceToken,
        params: Value,
        ann: &vamana::SharedAnn,
        type_weights: HashMap<String, f32>,
    ) -> Result<Value, RuntimeError> {
        let p: ComposeParams = deser(params)?;

        // Registry dispatch already mints an exact token for an explicit
        // namespace. Direct handler callers must provide that same authorized
        // token; never turn an untrusted business parameter into a stronger
        // namespace capability here.
        let effective_token = match p.namespace.as_deref() {
            Some(ns_str) => {
                let ns = Namespace::parse(ns_str).map_err(|e| {
                    RuntimeError::InvalidInput(format!("invalid namespace {ns_str:?}: {e}"))
                })?;
                if &ns != token.namespace() {
                    return Err(RuntimeError::InvalidInput(
                        "knowledge.compose namespace does not match authorized token namespace"
                            .to_string(),
                    ));
                }
                // Equality above makes this a safe exact-scope narrowing of
                // any broader direct-call token.
                token.with_namespace(ns)
            }
            None => token.clone(),
        };
        let token = &effective_token;

        let raw_query = p.query.trim().to_string();
        if raw_query.is_empty() {
            return Err(RuntimeError::InvalidInput("query must not be empty".into()));
        }
        let explain = p.explain.unwrap_or(false);

        let mut domain_ids: Vec<String> = p
            .domain_ids
            .unwrap_or_default()
            .into_iter()
            .filter(|s| !s.trim().is_empty())
            .collect();
        let atom_ids: Vec<String> = p
            .atom_ids
            .unwrap_or_default()
            .into_iter()
            .filter(|s| !s.trim().is_empty())
            .collect();

        let is_auto = domain_ids.is_empty() && atom_ids.is_empty();
        // `atom_ids`-only calls (caller pinned exact atoms, no domain_ids) never
        // blend KG entities — the caller opted into exactly those atoms. Auto and
        // explicit-domain_ids calls both blend (ADR-051 Amendment 1).
        let atom_ids_only = domain_ids.is_empty() && !atom_ids.is_empty();
        let blend_kg = p.blend_kg.unwrap_or(true) && !atom_ids_only;
        let mut suggest_ann_unavailable = false;
        let mut suggest_hydration_failures = 0usize;
        let mut query_embedding = QueryEmbeddingCache::default();
        if is_auto {
            let word_count = raw_query.split_whitespace().count();
            if word_count < 10 {
                return Err(RuntimeError::InvalidInput(format!(
                    "auto-compose query must be at least 10 words for effective domain selection \
                     (got {word_count}). Provide explicit domain_ids/atom_ids for shorter queries."
                )));
            }
        }

        // #887: unconditional per-stage timing, WARN-on-slow and
        // WARN-on-abandoned. See `super::compose::ComposeTiming` for the
        // full rationale and the completion-contract every early return
        // below must honor (`finish()` before returning, or route the error
        // through `try_or_finish!`). Each `begin(Phase::X)` fires *before*
        // the phase's (possibly fallible, possibly long-running) work — not
        // after — so an in-flight phase is never lost from the breakdown if
        // the request errors, is cancelled, or is abandoned mid-phase.
        use super::compose::Phase;
        let mut timing = super::compose::ComposeTiming::start(&raw_query, is_auto);
        macro_rules! try_or_finish {
            ($e:expr) => {
                match $e {
                    Ok(v) => v,
                    Err(e) => {
                        timing.finish(0);
                        return Err(e.into());
                    }
                }
            };
        }
        try_or_finish!(timing.begin(Phase::Suggest));

        if is_auto {
            let auto_limit = p.auto_limit.unwrap_or(5).clamp(1, 20);
            let suggest_attempt = Self::suggest_with_query_embedding(
                runtime,
                token,
                json!({ "query": &raw_query, "limit": auto_limit }),
                ann,
                std::mem::take(&mut query_embedding),
            )
            .await;
            try_or_finish!(khive_storage::ensure_request_read_active(
                "knowledge.compose"
            ));
            let suggest_result = match suggest_attempt {
                Ok((v, reused_query_embedding)) => {
                    query_embedding = reused_query_embedding;
                    suggest_ann_unavailable = v
                        .get("ann_unavailable")
                        .and_then(|f| f.as_bool())
                        .unwrap_or(false);
                    suggest_hydration_failures = v
                        .pointer("/degraded/hydration_failures")
                        .and_then(Value::as_u64)
                        .and_then(|count| usize::try_from(count).ok())
                        .unwrap_or(0);
                    v
                }
                Err(e) => {
                    try_or_finish!(khive_storage::ensure_request_read_active(
                        "knowledge.compose"
                    ));
                    tracing::warn!(target: "khive_pack_knowledge::knowledge::search", error = %e, "auto-compose: internal suggest failed, returning empty");
                    let response = json!({
                        "status": "ok",
                        "data": {
                            "query": raw_query,
                            "markdown": "# Knowledge Briefing\n\nDomain suggestion unavailable.",
                            "domains": [],
                            "atoms": [],
                            "count": 0,
                            "suggest_error": e.to_string(),
                        },
                    });
                    try_or_finish!(khive_storage::ensure_request_read_active(
                        "knowledge.compose"
                    ));
                    timing.finish(0);
                    return Ok(response);
                }
            };
            if let Some(results) = suggest_result.get("results").and_then(|v| v.as_array()) {
                for r in results.iter().filter(|r| r["members"] != 0) {
                    if let Some(id) = r.get("id").and_then(|v| v.as_str()) {
                        domain_ids.push(id.to_string());
                    }
                }
            }
            if domain_ids.is_empty() {
                let mut data = json!({
                    "query": raw_query,
                    "markdown": "# Knowledge Briefing\n\nNo matching domains found for auto-suggest.",
                    "domains": [],
                    "atoms": [],
                    "count": 0,
                });
                if suggest_ann_unavailable {
                    data["ann_unavailable"] = json!(true);
                }
                attach_hydration_degradation(&mut data, suggest_hydration_failures);
                let response = json!({ "status": "ok", "data": data });
                try_or_finish!(khive_storage::ensure_request_read_active(
                    "knowledge.compose"
                ));
                timing.finish(0);
                return Ok(response);
            }
        }
        try_or_finish!(timing.begin(Phase::Fetch));

        let ns = token.namespace().as_str().to_owned();

        let mut resolved_domains: Vec<Domain> = Vec::new();
        let mut member_slugs: Vec<String> = Vec::new();

        for id in &domain_ids {
            try_or_finish!(khive_storage::ensure_request_read_active(
                "knowledge.compose"
            ));
            let domain = try_or_finish!(load_domain_by_id_or_slug(runtime, &ns, id).await);
            let members = try_or_finish!(parse_domain_members(&domain));
            member_slugs.extend(members);
            resolved_domains.push(domain);
        }

        let mut seen_ids: HashSet<String> = HashSet::new();
        let mut ordered_atoms: Vec<Atom> = Vec::new();
        let mut omitted_members: Vec<String> = Vec::new();

        for window in member_slugs.chunks(Self::COMPOSE_ATOM_CHUNK_SIZE) {
            let outcomes =
                try_or_finish!(Self::load_compose_atom_window(runtime, &ns, window).await);
            for (slug, outcome) in window.iter().zip(outcomes) {
                try_or_finish!(khive_storage::ensure_request_read_active(
                    "knowledge.compose"
                ));
                let atom = match outcome {
                    Ok(atom) => atom,
                    Err(RuntimeError::NotFound(_)) => {
                        omitted_members.push(slug.clone());
                        continue;
                    }
                    Err(error) => {
                        timing.finish(0);
                        return Err(error);
                    }
                };
                if seen_ids.insert(atom.id.to_string()) {
                    ordered_atoms.push(atom);
                }
            }
        }
        for window in atom_ids.chunks(Self::COMPOSE_ATOM_CHUNK_SIZE) {
            let outcomes =
                try_or_finish!(Self::load_compose_atom_window(runtime, &ns, window).await);
            for outcome in outcomes {
                try_or_finish!(khive_storage::ensure_request_read_active(
                    "knowledge.compose"
                ));
                let atom = try_or_finish!(outcome);
                if seen_ids.insert(atom.id.to_string()) {
                    ordered_atoms.push(atom);
                }
            }
        }

        // Auto-compose inherits the same quality default as knowledge.search and
        // knowledge.suggest: draft and deprecated atoms are excluded unless the caller
        // explicitly provided atom_ids (which is an opt-in to whatever those IDs hold).
        if is_auto {
            const COMPOSE_EXCLUDE: &[&str] = &["draft", "deprecated"];
            ordered_atoms.retain(|a| {
                let status = a.status.as_deref().unwrap_or("");
                !COMPOSE_EXCLUDE.contains(&status)
            });
        }

        if ordered_atoms.is_empty() {
            try_or_finish!(khive_storage::ensure_request_read_active(
                "knowledge.compose"
            ));
            let mut data = json!({
                "query": raw_query,
                "markdown": "# Knowledge Briefing\n\nNo atoms found.",
                "domains": [],
                "atoms": [],
                "count": 0,
            });
            if suggest_ann_unavailable {
                data["ann_unavailable"] = json!(true);
            }
            if !omitted_members.is_empty() {
                data["omissions"] = json!(omitted_members);
            }
            attach_hydration_degradation(&mut data, suggest_hydration_failures);
            let response = json!({ "status": "ok", "data": data });
            try_or_finish!(khive_storage::ensure_request_read_active(
                "knowledge.compose"
            ));
            timing.finish(0);
            return Ok(response);
        }

        let mut items: Vec<ScoredTextItem> = ordered_atoms
            .iter()
            .map(|a| ScoredTextItem {
                id: a.id.to_string(),
                slug: a.slug.clone(),
                name: a.name.clone(),
                text: atom_embed_text(a),
                score: 1.0,
            })
            .collect();

        try_or_finish!(timing.begin(Phase::Rerank));
        // The KG blend below requires a role-specific vector (search_kg_entities
        // gates on it, never a generic one — #2307). When this compose call can
        // reach the blend, attempt embed_query directly so a real failure keeps
        // the blend on its degradation path; rerank_text_items's own
        // combined-batch fallback still covers the failure case below. When the
        // blend cannot run anyway (blend_kg is off, or atom_ids_only), skip the
        // solo call and let rerank_text_items embed query + candidates in one
        // combined batch, matching the pre-cache single-job cost.
        if blend_kg
            && query_embedding.role_specific.is_not_attempted()
            && !runtime.default_embedder_name().is_empty()
        {
            let embedded = try_or_finish!(
                khive_storage::await_request_read_phase(
                    "knowledge.compose",
                    runtime.embed_query(&raw_query),
                )
                .await
            );
            query_embedding.role_specific = match embedded {
                Ok(v) => RoleSpecificEmbedding::Vector(v),
                Err(_) => RoleSpecificEmbedding::Failed,
            };
        }
        try_or_finish!(
            rerank_text_items(runtime, &raw_query, &mut query_embedding, &mut items,).await
        );

        let atom_ids: Vec<String> = ordered_atoms.iter().map(|a| a.id.to_string()).collect();
        let atom_cosine_scores: HashMap<String, f32> = items
            .iter()
            .map(|item| (item.id.clone(), item.score))
            .collect();

        try_or_finish!(timing.begin(Phase::Fetch));
        let section_map =
            try_or_finish!(super::compose::load_sections(runtime, &ns, &atom_ids).await);

        let has_sections = !section_map.is_empty();
        try_or_finish!(timing.begin(Phase::Rerank));

        let section_results = if has_sections {
            let domain_member_ids = Self::compose_domain_member_ids(&ordered_atoms, &member_slugs);

            let domain_scores: HashMap<String, f32> = ordered_atoms
                .iter()
                .map(|a| {
                    let id = a.id.to_string();
                    let score = if domain_member_ids.contains(&id) {
                        1.0
                    } else {
                        0.0
                    };
                    (id, score)
                })
                .collect();

            try_or_finish!(khive_storage::ensure_request_read_active(
                "knowledge.compose"
            ));

            if let Some(qe) = query_embedding.any() {
                try_or_finish!(super::compose::score_sections(
                    &raw_query,
                    qe,
                    &atom_cosine_scores,
                    &section_map,
                    &domain_scores,
                    &type_weights,
                    &super::compose::ComposeScoreWeights::default(),
                ))
            } else {
                Vec::new()
            }
        } else {
            Vec::new()
        };
        try_or_finish!(timing.begin(Phase::Trim));

        let max_tokens = p.max_tokens.unwrap_or(8000).clamp(500, 100_000);
        let char_budget = max_tokens * CHARS_PER_TOKEN;

        let packed = pack_compose_markdown(
            &raw_query,
            &resolved_domains,
            &ordered_atoms,
            &items,
            &section_results,
            explain,
            char_budget,
        );
        let section_json: Vec<Value> = if explain {
            packed
                .sections
                .iter()
                .map(|s| {
                        json!({
                            "section_id": s.section_id,
                            "atom_id": s.atom_id,
                            "section_type": s.section_type,
                            "heading": s.heading,
                            "score": (s.score * 10000.0).round() / 10000.0,
                            "breakdown": {
                                "section_cosine": (s.score_breakdown.section_cosine * 10000.0).round() / 10000.0,
                                "section_bm25": (s.score_breakdown.section_bm25 * 10000.0).round() / 10000.0,
                                "atom_cosine": (s.score_breakdown.atom_cosine * 10000.0).round() / 10000.0,
                                "domain_score": (s.score_breakdown.domain_score * 10000.0).round() / 10000.0,
                                "type_weight": (s.score_breakdown.type_weight * 10000.0).round() / 10000.0,
                            },
                        })
                })
                .collect()
        } else {
            Vec::new()
        };

        // KG entity blend (ADR-051 Amendment 1): additive "Knowledge graph"
        // section, trimmed against whatever budget the atom/section body left
        // over. Runs after the body is finalized so entities never displace an
        // atom or section — see `trim_kg_entities_to_budget`.
        //
        // Self-calibrating inclusion floor: an entity only blends in if its
        // rerank score clears the minimum rerank score among the atoms that
        // actually made the final body. A compose whose final body has zero
        // atoms (everything trimmed by `max_tokens`) has no floor to
        // calibrate against, so it blends no entities at all (ADR-051
        // Amendment 1, zero-atom edge case).
        let entity_score_floor: Option<f32> = packed
            .included_atom_ids
            .iter()
            .filter_map(|id| atom_cosine_scores.get(id).copied())
            .fold(None, |acc, s| Some(acc.map_or(s, |a: f32| a.min(s))));
        let mut markdown = packed.markdown;
        let mut kg_entities_json: Vec<Value> = Vec::new();
        if blend_kg {
            if let Some(floor) = entity_score_floor {
                // Discovery/hydration failures degrade to an atom-only
                // response instead of aborting the whole compose — the
                // finalized atom/section body above is still a valid,
                // useful briefing even without the supplementary KG section.
                // KG entities live on the core (main) backend; on a
                // secondary-assigned pack runtime this search would silently
                // blend against an empty graph (ADR-073).
                match search_kg_entities(
                    &runtime.core(),
                    token,
                    &ns,
                    &raw_query,
                    &mut query_embedding,
                    KG_BLEND_CAP,
                    floor,
                )
                .await
                {
                    Ok(kg_hits) => {
                        let remaining_budget = char_budget.saturating_sub(markdown.len());
                        let kg_hits = trim_kg_entities_to_budget(kg_hits, remaining_budget);
                        if !kg_hits.is_empty() {
                            let kg_markdown = format_kg_entities_markdown(&kg_hits);
                            if kg_markdown.len() <= remaining_budget {
                                markdown.push_str(&kg_markdown);
                                kg_entities_json = kg_hits
                                    .iter()
                                    .map(|e| {
                                        json!({
                                            "id": e.id,
                                            "kind": e.kind,
                                            "name": e.name,
                                            "score": (e.score * 10000.0).round() / 10000.0,
                                        })
                                    })
                                    .collect();
                            }
                        }
                    }
                    Err(e) => {
                        try_or_finish!(khive_storage::ensure_request_read_active(
                            "knowledge.compose"
                        ));
                        tracing::warn!(
                            target: "khive_pack_knowledge::knowledge::search",
                            error = %e,
                            "knowledge.compose: KG entity blend failed, continuing with atom-only response"
                        );
                    }
                }
            }
        }

        try_or_finish!(khive_storage::ensure_request_read_active(
            "knowledge.compose"
        ));

        let atom_json: Vec<Value> = items
            .iter()
            .filter(|item| packed.included_atom_ids.contains(&item.id))
            .map(|item| {
                json!({
                    "id": item.id,
                    "slug": item.slug,
                    "name": item.name,
                    "score": (item.score * 10000.0).round() / 10000.0,
                })
            })
            .collect();

        let domain_json: Vec<Value> = resolved_domains
            .iter()
            .map(|d| json!({ "id": d.id.to_string(), "slug": d.slug, "name": d.name }))
            .collect();

        let count = atom_json.len();

        let mut data = json!({
            "query": raw_query,
            "markdown": markdown,
            "domains": domain_json,
            "atoms": atom_json,
            "count": count,
        });
        if explain && !section_json.is_empty() {
            data["sections"] = json!(section_json);
            data["section_count"] = json!(section_json.len());
        }
        if !kg_entities_json.is_empty() {
            data["entities"] = json!(kg_entities_json);
        }
        if suggest_ann_unavailable {
            data["ann_unavailable"] = json!(true);
        }
        if !omitted_members.is_empty() {
            data["omissions"] = json!(omitted_members);
        }
        attach_hydration_degradation(&mut data, suggest_hydration_failures);

        let response = json!({
            "status": "ok",
            "data": data,
        });
        try_or_finish!(khive_storage::ensure_request_read_active(
            "knowledge.compose"
        ));
        timing.finish(count);
        Ok(response)
    }
}