oxibrain-core 0.5.0

Core domain types, temporal fold, extraction, ranking, and packing for oxibrain
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
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
//! Extraction types and pure functions (DESIGN §7). The LLM arrives in M3, but
//! non-determinism is contained: schema generation, prompt building, and validation
//! are all pure functions of the predicate registry. The LLM call itself is the
//! only non-deterministic step, and its output is cached (§7.3).
//!
//! This module defines:
//! - Types: `Claim`, `MentionRef`, `ClaimObject`, `ExtractionResponse`, `ExtractSummary`
//! - Identity: `ExtractorConfig`, `ExtractMechanism`
//! - Schema: `schema_from_registry` (pure fn of the registry)
//! - Prompt: `build_extraction_prompt` (pure fn of the registry)
//! - Validation: `validate_claims` (pure fn of claims + content + registry)

use crate::knowledge::Polarity;
use crate::registry::{LiteralType, ObjectKind, PredicateDef};
use serde::{Deserialize, Serialize};

// ─── Extractor identity (§7.5) ───────────────────────────────────────────────

/// Configuration for an extractor: model, prompt version, mechanism.
/// Hashes to an ExtractorId (§7.5). Only the registry MAJOR version invalidates
/// the cache (D8); adding a predicate is a minor bump and does not force re-extraction.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExtractorConfig {
    pub model_id: String,
    pub prompt_version: u32,
    pub registry_major: u32,
    pub mechanism: ExtractMechanism,
    pub max_tokens: u32,
    /// blake3 hex digest of the model weights. Changing weights must change
    /// the extractor id (§9.5) — a silent quality change would poison the
    /// extraction cache.
    #[serde(default)]
    pub model_digest: Option<String>,
    /// Optional Foundation profile id (`profiles.json::id`) that bound the
    /// model when extraction ran. Folded into [`ExtractorConfig::id`] so
    /// cached summaries are invalidated when the role binding changes
    /// (Task 5, §13). `None` for the legacy compat env / explicit override
    /// path — the hash stays identical to the pre-Foundation config so
    /// existing caches continue to hit. The profile id is non-secret and
    /// is stored only in the cache half (extractor_id), never in any
    /// truth-half identifier; Keychain locators, display names, and
    /// wall-clock values are deliberately excluded.
    #[serde(default)]
    pub provider_profile_id: Option<String>,
}

/// How structured output is enforced (§7.4). Recorded in the ExtractorId hash.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ExtractMechanism {
    /// Provider-native JSON Schema structured output (OpenAI).
    JsonSchema,
    /// Forced tool call (Anthropic).
    ToolCall,
    /// GBNF grammar-constrained decoding (local GGUF path, §9.4 D28). The
    /// grammar is generated from the predicate registry (P4).
    Grammar,
    /// JSON mode without schema enforcement (weakest; validator is the only gate).
    JsonMode,
}

impl ExtractorConfig {
    /// ExtractorId = blake3(model_id, prompt_version, registry_major,
    /// mechanism[, model_digest[, provider_profile_id]]). Only the MAJOR
    /// registry version invalidates the cache (D8). The model digest —
    /// when present — invalidates it on weight changes (§9.5). The
    /// Foundation profile id — when present — invalidates it when the
    /// role binding changes (Task 5, §13); absent profile id keeps the
    /// hash identical to the pre-Foundation config so legacy caches
    /// continue to hit.
    pub fn id(&self) -> String {
        let mut hasher = blake3::Hasher::new();
        hasher.update(self.model_id.as_bytes());
        hasher.update(&self.prompt_version.to_le_bytes());
        hasher.update(&self.registry_major.to_le_bytes());
        hasher.update(&[self.mechanism as u8]);
        if let Some(digest) = &self.model_digest {
            hasher.update(digest.as_bytes());
        }
        if let Some(profile_id) = &self.provider_profile_id {
            hasher.update(profile_id.as_bytes());
        }
        hex::encode(hasher.finalize().as_bytes())
    }
}

// ─── Extraction types ────────────────────────────────────────────────────────

/// A reference to an entity mention in the episode text.
///
/// Extraction contract v2 (ADR-006): the model provides a verbatim `quote`
/// copied from the episode containing the `surface`; the byte `span` is
/// derived server-side from the located quote. Legacy responses without a
/// quote keep the span-verbatim ladder below. Either way, a valid mention's
/// `surface` appears verbatim at `[span.0, span.1)` — the fabricated-entity
/// gate is unchanged.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MentionRef {
    pub surface: String,
    pub entity_type: String,
    /// Verbatim snippet copied from the episode containing `surface`.
    /// When present, the model-provided `span` is ignored and the span is
    /// derived by locating the quote (first occurrence, exact bytes).
    #[serde(default)]
    pub quote: Option<String>,
    /// Byte span `[start, end)` into the episode. Server-derived when
    /// `quote` is present; otherwise checked against the content by the
    /// legacy ladder. Defaults to `(0, 0)` when the model omits it.
    #[serde(default)]
    pub span: (u32, u32),
}

/// The object of an extracted claim.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum ClaimObject {
    Entity {
        mention: MentionRef,
    },
    Literal {
        literal_type: String,
        value: String,
        /// Verbatim snippet containing the `value` (ADR-006). When present,
        /// the span is derived server-side; the value must occur inside the
        /// located quote — stronger than the legacy bounds-only check.
        #[serde(default)]
        quote: Option<String>,
        #[serde(default)]
        span: (u32, u32),
    },
}

/// One extracted claim from the LLM. Maps to one assertion + mentions.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Claim {
    pub predicate: String,
    pub subject: MentionRef,
    pub object: ClaimObject,
    pub polarity: Polarity,
    /// Epoch millis. None = TIME_MIN (always true).
    #[serde(default)]
    pub valid_from: Option<i64>,
    /// Epoch millis. None = TIME_MAX (still true).
    #[serde(default)]
    pub valid_to: Option<i64>,
    pub confidence: f32,
}

/// The parsed LLM response.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExtractionResponse {
    pub claims: Vec<Claim>,
}

/// Summary of an extraction batch.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ExtractSummary {
    pub extracted: usize,
    pub quarantined: usize,
    pub episodes_done: usize,
    pub episodes_failed: usize,
    /// (episode_id, error) for every failed episode. Batch loops used to
    /// discard these, making deterministic per-episode failures (truncated
    /// tool calls, unparseable responses) undiagnosable from the outside.
    #[serde(default)]
    pub failures: Vec<(String, String)>,
}

/// Extraction budget limits (§7.6). The queue holds on exhaustion; it never drops.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExtractionBudget {
    pub max_concurrent: usize,
    pub max_episodes_per_batch: usize,
    pub max_tokens_per_episode: u32,
    pub max_repair_attempts: u32,
    pub lease_timeout_secs: u64,
}

impl Default for ExtractionBudget {
    fn default() -> Self {
        Self {
            max_concurrent: 4,
            max_episodes_per_batch: 50,
            max_tokens_per_episode: 8192,
            max_repair_attempts: 1,
            lease_timeout_secs: 300,
        }
    }
}

// ─── Validation ──────────────────────────────────────────────────────────────

/// Result of validating claims against the registry + episode content.
#[derive(Debug, Clone)]
pub struct ValidationResult {
    pub valid: Vec<Claim>,
    pub invalid: Vec<(Claim, Vec<ValidationError>)>,
}

/// A validation error for a single claim (§7.4).
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum ValidationError {
    UnknownPredicate {
        predicate: String,
    },
    SubjectTypeMismatch {
        predicate: String,
        expected: Vec<String>,
        got: String,
    },
    ObjectTypeMismatch {
        predicate: String,
        expected: String,
        got: String,
    },
    MalformedLiteral {
        literal_type: String,
        value: String,
        reason: String,
    },
    SpanOutOfBounds {
        span: (u32, u32),
        content_len: usize,
    },
    SurfaceNotVerbatim {
        surface: String,
        span: (u32, u32),
        found: String,
    },
    ConfidenceOutOfRange {
        confidence: f32,
    },
}

/// Validate parsed claims against the registry + episode content (§7.4).
/// Pure function: same claims + content + registry → same result.
pub fn validate_claims(
    claims: &[Claim],
    content: &str,
    predicates: &[PredicateDef],
) -> ValidationResult {
    let mut valid = Vec::new();
    let mut invalid = Vec::new();

    for claim in claims {
        let mut errors = Vec::new();

        // 1. Confidence range.
        if !(0.0..=1.0).contains(&claim.confidence) {
            errors.push(ValidationError::ConfidenceOutOfRange {
                confidence: claim.confidence,
            });
        }

        // 2. Predicate exists in registry.
        let Some(pred_def) = predicates.iter().find(|p| p.name == claim.predicate) else {
            errors.push(ValidationError::UnknownPredicate {
                predicate: claim.predicate.clone(),
            });
            invalid.push((claim.clone(), errors));
            continue;
        };

        // 3. Subject type matches predicate.subject_types.
        if !pred_def
            .subject_types
            .iter()
            .any(|t| t == &claim.subject.entity_type)
        {
            errors.push(ValidationError::SubjectTypeMismatch {
                predicate: claim.predicate.clone(),
                expected: pred_def
                    .subject_types
                    .iter()
                    .map(|t| t.to_string())
                    .collect(),
                got: claim.subject.entity_type.clone(),
            });
        }

        // 4. Object type matches predicate.object_kind.
        match (&claim.object, &pred_def.object_kind) {
            (ClaimObject::Entity { mention }, ObjectKind::Entity(expected)) => {
                if !expected.0.iter().any(|t| t == &mention.entity_type) {
                    errors.push(ValidationError::ObjectTypeMismatch {
                        predicate: claim.predicate.clone(),
                        expected: expected.0.join("|"),
                        got: mention.entity_type.clone(),
                    });
                }
            }
            (ClaimObject::Literal { literal_type, .. }, ObjectKind::Literal(expected_lt)) => {
                if !literal_type_matches(literal_type, expected_lt) {
                    errors.push(ValidationError::ObjectTypeMismatch {
                        predicate: claim.predicate.clone(),
                        expected: format!("{expected_lt:?}"),
                        got: literal_type.clone(),
                    });
                }
            }
            (
                ClaimObject::Literal {
                    literal_type,
                    value,
                    ..
                },
                ObjectKind::Enum { variants },
            ) => {
                if !variants.iter().any(|v| v == value) {
                    errors.push(ValidationError::ObjectTypeMismatch {
                        predicate: claim.predicate.clone(),
                        expected: format!("enum: {}", variants.join("|")),
                        got: value.clone(),
                    });
                }
                let _ = literal_type; // enum values are strings; type is not constrained further
            }
            (ClaimObject::Entity { .. }, ObjectKind::Literal(_))
            | (ClaimObject::Entity { .. }, ObjectKind::Enum { .. })
            | (ClaimObject::Literal { .. }, ObjectKind::Entity(_)) => {
                errors.push(ValidationError::ObjectTypeMismatch {
                    predicate: claim.predicate.clone(),
                    expected: format!("{:?}", pred_def.object_kind),
                    got: match &claim.object {
                        ClaimObject::Entity { .. } => "entity".into(),
                        ClaimObject::Literal { .. } => "literal".into(),
                    },
                });
            }
        }

        // 5-7. Spans and surfaces. Model offsets drift on multibyte text and
        // casing; repair before rejecting. The fabricated-entity gate still
        // holds: a claim survives only when each surface is literally present
        // in the content at the (possibly corrected) span.
        let mut repaired = claim.clone();
        if !resolve_mention(&mut repaired.subject, content) {
            errors.push(ValidationError::SurfaceNotVerbatim {
                surface: claim.subject.surface.clone(),
                span: claim.subject.span,
                found: String::new(),
            });
        }
        if let ClaimObject::Entity { mention } = &mut repaired.object {
            if !resolve_mention(mention, content) {
                errors.push(ValidationError::SurfaceNotVerbatim {
                    surface: mention.surface.clone(),
                    span: mention.span,
                    found: String::new(),
                });
            }
        }
        if let ClaimObject::Literal {
            quote, value, span, ..
        } = &mut repaired.object
        {
            // ADR-006: a quoted literal derives its span server-side and
            // the value must occur inside the located quote — stronger than
            // the legacy bounds-only check.
            match quote.as_deref() {
                Some(q) => match locate_in_quote(content, q, value) {
                    Some((derived, canonical)) => {
                        if canonical != *value {
                            *value = canonical;
                        }
                        *span = derived;
                    }
                    None => errors.push(ValidationError::SurfaceNotVerbatim {
                        surface: value.clone(),
                        span: *span,
                        found: String::new(),
                    }),
                },
                None => check_span(span, content, &mut errors),
            }
        }

        if errors.is_empty() {
            valid.push(repaired);
        } else {
            invalid.push((claim.clone(), errors));
        }
    }

    ValidationResult { valid, invalid }
}

fn check_span(span: &(u32, u32), content: &str, errors: &mut Vec<ValidationError>) {
    let len = content.len();
    if span.0 as usize >= len || span.1 as usize > len || span.0 >= span.1 {
        errors.push(ValidationError::SpanOutOfBounds {
            span: *span,
            content_len: len,
        });
    }
}

/// Resolve a mention against the content, repairing span-interpretation drift
/// in place. Returns `true` when the surface is verbatim-present at the span.
///
/// Resolution ladder (ADR-006) — every step keeps the fabricated-entity gate
/// intact: the bytes at the (final) span must spell the surface. Relocating a
/// model-provided span to a *different* location is deliberately NOT done:
/// the injection suite (oxibrain-store/tests/injection_suite.rs) requires
/// that a span citing the wrong bytes is rejected even when the surface
/// occurs elsewhere. The quote step does not relocate a span — it *derives*
/// one from verbatim evidence the model copied.
/// 0. Quote — locate the copied snippet, derive the span server-side.
/// 1. Exact byte span.
/// 2. Char-index span — models count chars on multibyte (Korean/CJK) text.
/// 3. Casing drift at the same span — canonicalize surface to the source text.
fn resolve_mention(m: &mut MentionRef, content: &str) -> bool {
    // 0. Quote-based evidence (extraction contract v2).
    if let Some(quote) = m.quote.as_deref() {
        return match locate_in_quote(content, quote, &m.surface) {
            Some((span, canonical)) => {
                m.surface = canonical;
                m.span = span;
                true
            }
            None => false,
        };
    }
    let (a, b) = (m.span.0 as usize, m.span.1 as usize);
    // 1. Exact byte span.
    if content.get(a..b) == Some(m.surface.as_str()) {
        return true;
    }
    // 2. Char-index span.
    if let Some(range) = char_span_to_bytes(content, a, b) {
        if content.get(range.clone()) == Some(m.surface.as_str()) {
            m.span = (range.start as u32, range.end as u32);
            return true;
        }
    }
    // 3. Casing drift at the same span: the content is the source of truth.
    if let Some(found) = content.get(a..b) {
        if found.eq_ignore_ascii_case(m.surface.as_str()) {
            m.surface = found.to_string();
            return true;
        }
    }
    false
}

/// Locate `needle` (a surface or literal value) inside a verbatim `quote`
/// window in `content` (ADR-006). First occurrences everywhere; exact bytes
/// first, ASCII-case-insensitive fallback canonicalizes the needle to the
/// source text. Returns `Some((byte_span, canonical_needle))`.
///
/// Pure and deterministic. An empty needle never resolves; an empty quote
/// window contains nothing, so degenerate "surface anywhere" holes cannot
/// open. This is evidence the model *copied*, not offsets it *counted*.
fn locate_in_quote(content: &str, quote: &str, needle: &str) -> Option<((u32, u32), String)> {
    if needle.is_empty() {
        return None;
    }
    let q0 = content.find(quote)?;
    let window = &content[q0..q0 + quote.len()];
    if let Some(off) = window.find(needle) {
        let span = ((q0 + off) as u32, (q0 + off + needle.len()) as u32);
        return Some((span, needle.to_string()));
    }
    // Casing fallback: the content is the source of truth.
    let lowered = needle.to_ascii_lowercase();
    for (off, _) in window.char_indices() {
        let candidate = &window[off..];
        if candidate.len() < needle.len() {
            break;
        }
        // `needle.len()` may land mid-character (multibyte window); such
        // offsets cannot match — skip them, later offsets still can.
        let Some(head) = candidate.get(..needle.len()) else {
            continue;
        };
        if head.to_ascii_lowercase() == lowered {
            let found = head.to_string();
            let span = ((q0 + off) as u32, (q0 + off + needle.len()) as u32);
            return Some((span, found));
        }
    }
    None
}

/// Convert a (char_index_start, char_index_end) span to a byte range.
/// Returns `None` when the indices don't address this content.
fn char_span_to_bytes(content: &str, a: usize, b: usize) -> Option<std::ops::Range<usize>> {
    if b < a {
        return None;
    }
    let mut start = None;
    let mut end = None;
    let mut idx = 0usize;
    for (bi, _) in content.char_indices() {
        if idx == a {
            start = Some(bi);
        }
        if idx == b {
            end = Some(bi);
            break;
        }
        idx += 1;
    }
    // `b` may equal the char count — the range then runs to the end.
    let end = end.or((idx == b).then_some(content.len()))?;
    Some(start?..end)
}

fn literal_type_matches(given: &str, expected: &LiteralType) -> bool {
    match expected {
        LiteralType::Text => given == "text",
        LiteralType::Date => given == "date",
        LiteralType::DateTime => given == "datetime",
        LiteralType::Number => given == "number",
        LiteralType::Bool => given == "bool",
        LiteralType::Quantity { .. } => given == "quantity",
    }
}

// ─── Schema generation (§6.1) ────────────────────────────────────────────────

/// Generate the extraction JSON Schema from the predicate registry (P4).
/// Pure function: same registry → same schema. The schema constrains structure;
/// semantic rules (predicate↔type matching) are enforced by `validate_claims`.
pub fn schema_from_registry(predicates: &[PredicateDef]) -> serde_json::Value {
    let pred_names: Vec<&str> = predicates.iter().map(|p| p.name.as_str()).collect();
    let entity_types: Vec<&str> = predicates
        .iter()
        .flat_map(|p| {
            let subjects = p.subject_types.iter().map(|t| t.as_str());
            let objects = match &p.object_kind {
                ObjectKind::Entity(types) => types.0.iter().map(|t| t.as_str()).collect(),
                _ => vec![],
            };
            subjects.chain(objects)
        })
        .collect::<std::collections::BTreeSet<_>>()
        .into_iter()
        .collect();

    let mention_schema = serde_json::json!({
        "type": "object",
        "properties": {
            "surface": { "type": "string", "description": "Verbatim text from the episode" },
            "entity_type": { "type": "string", "enum": entity_types },
            "quote": {
                "type": "string",
                "description": "A short snippet copied EXACTLY from the episode that contains the surface"
            }
        },
        "required": ["surface", "entity_type", "quote"]
    });

    let object_schema = serde_json::json!({
        "type": "object",
        "properties": {
            "kind": { "type": "string", "enum": ["entity", "literal"] }
        },
        "required": ["kind"],
        "oneOf": [
            {
                "properties": {
                    "kind": { "const": "entity" },
                    "mention": mention_schema.clone()
                },
                "required": ["mention"]
            },
            {
                "properties": {
                    "kind": { "const": "literal" },
                    "literal_type": { "type": "string", "enum": ["text", "date", "datetime", "number", "bool", "quantity"] },
                    "value": { "type": "string" },
                    "quote": {
                        "type": "string",
                        "description": "A short snippet copied EXACTLY from the episode that contains the value"
                    }
                },
                "required": ["literal_type", "value", "quote"]
            }
        ]
    });

    serde_json::json!({
        "type": "object",
        "properties": {
            "claims": {
                "type": "array",
                "items": {
                    "type": "object",
                    "properties": {
                        "predicate": {
                            "type": "string",
                            "enum": pred_names
                        },
                        "subject": mention_schema,
                        "object": object_schema,
                        "polarity": {
                            "type": "string",
                            "enum": ["affirm", "deny"]
                        },
                        "valid_from": {
                            "type": ["integer", "null"],
                            "description": "Epoch millis, or null for 'always'"
                        },
                        "valid_to": {
                            "type": ["integer", "null"],
                            "description": "Epoch millis, or null for 'still true'"
                        },
                        "confidence": {
                            "type": "number",
                            "minimum": 0.0,
                            "maximum": 1.0
                        }
                    },
                    "required": ["predicate", "subject", "object", "polarity", "confidence"]
                }
            }
        },
        "required": ["claims"]
    })
}

/// Build a GBNF alternation of JSON string literals for an enum.
/// Produces: `"\"value1\"" | "\"value2\"" | ...`
/// which in GBNF matches one of the JSON strings "value1", "value2", ...
fn enum_alternation(values: &[&str]) -> String {
    values
        .iter()
        .map(|v| format!("\"\\\"{v}\\\"\""))
        .collect::<Vec<_>>()
        .join(" | ")
}

/// Generate a GBNF grammar from the predicate registry (§9.4, D28, P4).
///
/// Sibling of [`schema_from_registry`] — one registry, two consumers.
/// The grammar constrains structure and enum values; semantic rules
/// (confidence range, span validity, type matching) are enforced by
/// [`validate_claims`].
///
/// Any JSON matching this grammar parses into an [`ExtractionResponse`],
/// and any valid serialized [`ExtractionResponse`] is accepted by the grammar.
/// The grammar enforces a canonical key order; serde deserializes by field
/// name, so the order is irrelevant on the consumer side.
pub fn grammar_from_registry(predicates: &[PredicateDef]) -> String {
    // Collect enum values — same logic as schema_from_registry.
    let pred_names: Vec<&str> = predicates.iter().map(|p| p.name.as_str()).collect();
    let entity_types: Vec<&str> = predicates
        .iter()
        .flat_map(|p| {
            let subjects = p.subject_types.iter().map(|t| t.as_str());
            let objects = match &p.object_kind {
                ObjectKind::Entity(types) => types.0.iter().map(|t| t.as_str()).collect(),
                _ => vec![],
            };
            subjects.chain(objects)
        })
        .collect::<std::collections::BTreeSet<_>>()
        .into_iter()
        .collect();

    let pred_alts = enum_alternation(&pred_names);
    let etype_alts = enum_alternation(&entity_types);

    // GBNF (GGML BNF) for llama.cpp. See llama.cpp grammars/README.md.
    // {{ and }} are format! escapes for literal { and }.
    // GBNF (GGML BNF) for llama.cpp. Each rule on a single line — the
    // parser treats newlines as rule separators. See llama.cpp grammars/README.md.
    format!(
        r#"root ::= ws "{{" ws "\"claims\"" ws ":" ws "[" ws claims ws "]" ws "}}"
claims ::= (claim (ws "," ws claim)*)?
claim ::= "{{" ws "\"predicate\"" ws ":" ws predicate ws "," ws "\"subject\"" ws ":" ws mention ws "," ws "\"object\"" ws ":" ws object-union ws "," ws "\"polarity\"" ws ":" ws polarity ws "," ws valid-from-opt valid-to-opt "\"confidence\"" ws ":" ws number ws "}}"
valid-from-opt ::= ("\"valid_from\"" ws ":" ws temporal-val ws "," ws)?
valid-to-opt ::= ("\"valid_to\"" ws ":" ws temporal-val ws "," ws)?
temporal-val ::= "null" | integer
mention ::= "{{" ws "\"surface\"" ws ":" ws string ws "," ws "\"entity_type\"" ws ":" ws entity-type ws "," ws "\"quote\"" ws ":" ws nonempty-string ws "}}"
object-union ::= entity-object | literal-object
entity-object ::= "{{" ws "\"kind\"" ws ":" ws "\"entity\"" ws "," ws "\"mention\"" ws ":" ws mention ws "}}"
literal-object ::= "{{" ws "\"kind\"" ws ":" ws "\"literal\"" ws "," ws "\"literal_type\"" ws ":" ws literal-type ws "," ws "\"value\"" ws ":" ws string ws "," ws "\"quote\"" ws ":" ws nonempty-string ws "}}"
entity-type ::= {etype_alts}
literal-type ::= "\"text\"" | "\"date\"" | "\"datetime\"" | "\"number\"" | "\"bool\"" | "\"quantity\""
predicate ::= {pred_alts}
polarity ::= "\"affirm\"" | "\"deny\""
string ::= "\"" ([^"\\] | "\\" (["\\/bfnrt] | "u" [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F]))* "\"" ws
nonempty-string ::= "\"" ([^"\\] | "\\" (["\\/bfnrt] | "u" [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F]))+ "\"" ws
number ::= ("-"? ([0-9] | [1-9] [0-9]*)) ("." [0-9]+)? ([eE] [-+]? [0-9]+)? ws
integer ::= "-"? ([0-9] | [1-9] [0-9]*) ws
ws ::= [ \t\n]*
"#,
    )
}

/// Build the extraction system prompt from the registry (P4: no hard-coded predicates).
/// Pure function. Contract v2 (ADR-006): mentions carry a verbatim `quote`
/// copied from the episode; the server derives byte spans. The model is never
/// asked to count offsets — it is asked to copy.
pub fn build_extraction_prompt(predicates: &[PredicateDef]) -> String {
    let mut s = String::new();
    s.push_str(
        "You are a knowledge extraction engine. Extract structured claims from the given text. \
         Each claim references entities by their VERBATIM surface form and a quote copied from the text.\n\n\
         Available predicates:\n",
    );
    for p in predicates {
        let obj_desc = match &p.object_kind {
            ObjectKind::Entity(types) => format!("entity: {}", types.0.join("|")),
            ObjectKind::Literal(lt) => format!("literal: {lt:?}"),
            ObjectKind::Enum { variants } => format!("enum: {}", variants.join("|")),
        };
        s.push_str(&format!("- {} ({}): {}\n", p.name, obj_desc, p.description));
        if !p.examples.is_empty() {
            s.push_str(&format!("  Examples: {}\n", p.examples.join("; ")));
        }
    }
    s.push_str(
        "\nReturn JSON matching the provided schema. For each entity mention and each literal \
         value, provide:\n\
         - surface: the text exactly as it appears in the episode.\n\
         - quote: a short snippet (up to ~120 characters) copied EXACTLY from the episode, \
         character for character, taken from the SAME sentence where the surface appears. \
         Copy it — do not paraphrase, do not count character positions. The quote is \
         REQUIRED for every mention, including subjects; an empty quote is an error.\n\n\
         Rules:\n\
         - The surface MUST occur inside your quote, and the quote MUST occur in the episode.\n\
         - Subject and object types must match the predicate's definition.\n\
         - Set confidence to your confidence in the claim (0.0 to 1.0).\n\
         - Use valid_from/valid_to for time-bounded claims. Use null for 'always true' or 'still true'.\n",
    );
    s
}

/// A golden-corpus example for few-shot extraction (§9.6, 10.8).
/// The `text` is the episode content; `claims_json` is the expected
/// extraction output as a JSON string.
#[derive(Debug, Clone)]
pub struct FewShotExample {
    pub text: String,
    pub claims_json: String,
}
/// Select the k most similar golden episodes to the target text, using
/// character trigram Jaccard similarity (§9.6, 10.8). Language-independent
/// by construction (P11).
///
/// Pure function: same inputs → same selection.
pub fn few_shot_examples<'a>(
    target_text: &str,
    corpus: &'a [FewShotExample],
    k: usize,
) -> Vec<&'a FewShotExample> {
    if corpus.is_empty() || k == 0 {
        return Vec::new();
    }
    let target_shingles = oxibrain_index::shingles(target_text.to_lowercase().trim(), 3);
    let mut scored: Vec<(f64, &FewShotExample)> = corpus
        .iter()
        .map(|ex| {
            let ex_shingles = oxibrain_index::shingles(ex.text.to_lowercase().trim(), 3);
            let sim = oxibrain_index::jaccard(&target_shingles, &ex_shingles);
            (sim, ex)
        })
        .collect();
    // Sort by similarity descending; tie-break on text for determinism.
    scored.sort_by(|a, b| {
        b.0.partial_cmp(&a.0)
            .unwrap_or(std::cmp::Ordering::Equal)
            .then_with(|| a.1.text.cmp(&b.1.text))
    });
    scored.iter().take(k).map(|(_, ex)| *ex).collect()
}

/// Format selected few-shot examples as prompt text (§9.6, 10.8).
/// Injected into the system prompt before the target episode.
pub fn format_few_shot(examples: &[&FewShotExample]) -> String {
    if examples.is_empty() {
        return String::new();
    }
    let mut out = String::from("\nHere are some examples of correct extraction:\n\n");
    for (i, ex) in examples.iter().enumerate() {
        out.push_str(&format!("Example {}:\n", i + 1));
        out.push_str(&format!("Input: {}\n", ex.text));
        out.push_str(&format!("Output: {}\n\n", ex.claims_json));
    }
    out
}

/// Built-in few-shot corpus for the extraction prompt (§9.6, 10.8, ADR-006).
///
/// Multilingual by design (P11): an English person-facts note, a Korean note,
/// and a literal-value note. Every example is validated against `core_v1()`
/// and the quote contract by `few_shot_corpus_examples_validate` — a broken
/// example teaches the model the wrong contract, so the test is load-bearing.
/// Selection is language-independent (trigram Jaccard, see [`few_shot_examples`]).
pub fn default_few_shot_corpus() -> Vec<FewShotExample> {
    vec![
        FewShotExample {
            text: "Alice works on ProjectX at Acme Corp. Bob knows Carol.".into(),
            claims_json: r#"{"claims":[
  {"predicate":"works_on",
   "subject":{"surface":"Alice","entity_type":"Person","quote":"Alice works on ProjectX"},
   "object":{"kind":"entity","mention":{"surface":"ProjectX","entity_type":"Project","quote":"Alice works on ProjectX"}},
   "polarity":"affirm","confidence":0.95},
  {"predicate":"employed_by",
   "subject":{"surface":"Alice","entity_type":"Person","quote":"Alice works on ProjectX at Acme Corp"},
   "object":{"kind":"entity","mention":{"surface":"Acme Corp","entity_type":"Organization","quote":"at Acme Corp"}},
   "polarity":"affirm","confidence":0.9},
  {"predicate":"knows",
   "subject":{"surface":"Bob","entity_type":"Person","quote":"Bob knows Carol"},
   "object":{"kind":"entity","mention":{"surface":"Carol","entity_type":"Person","quote":"Bob knows Carol"}},
   "polarity":"affirm","confidence":0.9}
]}"#.into(),
        },
        FewShotExample {
            text: "김민수는 Acme Corp에 다니고 있다. 이서연은 brain-ui 프로젝트를 진행한다.".into(),
            claims_json: r#"{"claims":[
  {"predicate":"employed_by",
   "subject":{"surface":"김민수","entity_type":"Person","quote":"김민수는 Acme Corp에 다니고 있다"},
   "object":{"kind":"entity","mention":{"surface":"Acme Corp","entity_type":"Organization","quote":"Acme Corp에 다니고 있다"}},
   "polarity":"affirm","confidence":0.9},
  {"predicate":"works_on",
   "subject":{"surface":"이서연","entity_type":"Person","quote":"이서연은 brain-ui 프로젝트를 진행한다"},
   "object":{"kind":"entity","mention":{"surface":"brain-ui","entity_type":"Project","quote":"brain-ui 프로젝트를 진행한다"}},
   "polarity":"affirm","confidence":0.9}
]}"#.into(),
        },
        FewShotExample {
            text: "Alice's full name is Alice Smith. She was born in Seoul.".into(),
            claims_json: r#"{"claims":[
  {"predicate":"full_name",
   "subject":{"surface":"Alice","entity_type":"Person","quote":"Alice's full name is Alice Smith"},
   "object":{"kind":"literal","literal_type":"text","value":"Alice Smith","quote":"full name is Alice Smith"},
   "polarity":"affirm","confidence":0.95},
  {"predicate":"born_in",
   "subject":{"surface":"Alice","entity_type":"Person","quote":"Alice's full name"},
   "object":{"kind":"entity","mention":{"surface":"Seoul","entity_type":"Place","quote":"born in Seoul"}},
   "polarity":"affirm","confidence":0.9}
]}"#.into(),
        },
    ]
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn extractor_id_deterministic() {
        let c = ExtractorConfig {
            model_id: "claude-sonnet-4-5".into(),
            prompt_version: 1,
            registry_major: 1,
            mechanism: ExtractMechanism::ToolCall,
            max_tokens: 8192,
            model_digest: None,
            provider_profile_id: None,
        };
        assert_eq!(c.id(), c.id());
    }

    #[test]
    fn grammar_mechanism_changes_extractor_id() {
        // GBNF-constrained decoding (local path, §9.4 D28) must be recorded
        // in the ExtractorId — different output mechanism, different cache key.
        let base = ExtractorConfig {
            model_id: "qwen2.5-1.5b-instruct".into(),
            prompt_version: 1,
            registry_major: 1,
            mechanism: ExtractMechanism::Grammar,
            max_tokens: 8192,
            model_digest: None,
            provider_profile_id: None,
        };
        assert_eq!(base.id(), base.id());
        let json_mode = ExtractorConfig {
            mechanism: ExtractMechanism::JsonSchema,
            ..base.clone()
        };
        assert_ne!(base.id(), json_mode.id());
    }

    #[test]
    fn resolve_mention_repairs_casing_but_not_location() {
        // Casing drift at the exact span → surface canonicalized to content.
        let content = "The user prefers Rust.";
        let mut m = MentionRef {
            surface: "the user".into(),
            entity_type: "person".into(),
            quote: None,
            span: (0, 8),
        };
        assert!(resolve_mention(&mut m, content));
        assert_eq!(m.surface, "The user");

        // Wrong span is rejected even when the surface occurs verbatim
        // elsewhere — the injection suite relies on this (no relocation).
        let mut m = MentionRef {
            surface: "Rust".into(),
            entity_type: "technology".into(),
            quote: None,
            span: (0, 4),
        };
        assert!(!resolve_mention(&mut m, content));
    }

    #[test]
    fn resolve_mention_rejects_fabricated_surface() {
        let content = "The user prefers Rust.";
        let mut m = MentionRef {
            surface: "Python".into(),
            entity_type: "technology".into(),
            quote: None,
            span: (0, 6),
        };
        assert!(!resolve_mention(&mut m, content));
    }

    #[test]
    fn extractor_id_changes_with_model() {
        let base = ExtractorConfig {
            model_id: "a".into(),
            prompt_version: 1,
            registry_major: 1,
            mechanism: ExtractMechanism::JsonSchema,
            max_tokens: 4096,
            model_digest: None,
            provider_profile_id: None,
        };
        let diff = ExtractorConfig {
            model_id: "b".into(),
            ..base.clone()
        };
        assert_ne!(base.id(), diff.id());
    }

    #[test]
    fn extractor_id_changes_with_mechanism() {
        let base = ExtractorConfig {
            model_id: "a".into(),
            prompt_version: 1,
            registry_major: 1,
            mechanism: ExtractMechanism::JsonSchema,
            max_tokens: 4096,
            model_digest: None,
            provider_profile_id: None,
        };
        let diff = ExtractorConfig {
            mechanism: ExtractMechanism::ToolCall,
            ..base.clone()
        };
        assert_ne!(base.id(), diff.id());
    }

    #[test]
    fn extractor_id_changes_with_registry_major() {
        let base = ExtractorConfig {
            model_id: "a".into(),
            prompt_version: 1,
            registry_major: 1,
            mechanism: ExtractMechanism::JsonSchema,
            max_tokens: 4096,
            model_digest: None,
            provider_profile_id: None,
        };
        let diff = ExtractorConfig {
            registry_major: 2,
            ..base.clone()
        };
        assert_ne!(base.id(), diff.id());
    }

    #[test]
    fn extractor_id_changes_with_digest() {
        // §9.5: changing model weights must change the extractor id, or a
        // silent quality change would poison the extraction cache.
        let base = ExtractorConfig {
            model_id: "qwen2.5-1.5b".into(),
            prompt_version: 1,
            registry_major: 1,
            mechanism: ExtractMechanism::JsonSchema,
            max_tokens: 8192,
            model_digest: Some("abc123".into()),
            provider_profile_id: None,
        };
        let diff = ExtractorConfig {
            model_digest: Some("def456".into()),
            provider_profile_id: None,
            ..base.clone()
        };
        assert_ne!(
            base.id(),
            diff.id(),
            "weight change must invalidate ExtractorId"
        );

        // A missing digest must also differ (opt-in digest changes the id).
        let nodigest = ExtractorConfig {
            model_digest: None,
            provider_profile_id: None,
            ..base.clone()
        };
        assert_ne!(base.id(), nodigest.id());
    }

    #[test]
    fn schema_contains_all_predicates() {
        let schema = schema_from_registry(crate::registry::core_v1());
        let claims_items =
            &schema["properties"]["claims"]["items"]["properties"]["predicate"]["enum"];
        let names: Vec<String> = claims_items
            .as_array()
            .unwrap()
            .iter()
            .map(|v| v.as_str().unwrap().to_string())
            .collect();
        assert!(names.contains(&"works_on".to_string()));
        assert!(names.contains(&"employed_by".to_string()));
        assert!(names.contains(&"born_in".to_string()));
    }

    #[test]
    fn prompt_contains_predicate_descriptions() {
        let prompt = build_extraction_prompt(crate::registry::core_v1());
        assert!(prompt.contains("works_on"));
        assert!(prompt.contains("project"));
        assert!(prompt.contains("VERBATIM"));
    }

    #[test]
    fn prompt_v2_teaches_quotes_not_offsets() {
        let prompt = build_extraction_prompt(crate::registry::core_v1());
        assert!(
            prompt.contains("quote"),
            "v2 prompt must teach quote copying"
        );
        assert!(
            !prompt.contains("byte offset"),
            "v2 prompt must not demand offset arithmetic"
        );
    }

    #[test]
    fn grammar_uses_quotes_not_spans() {
        let g = grammar_from_registry(crate::registry::core_v1());
        let norm: String = g.split_whitespace().collect::<Vec<_>>().join(" ");
        assert!(
            norm.contains("\"\\\"quote\\\"\""),
            "mention and literal rules must require a quote"
        );
        assert!(
            !norm.contains("\"span\""),
            "model-facing grammar must not ask for numeric spans (ADR-006)"
        );
    }

    #[test]
    fn schema_uses_quotes_not_spans() {
        let s = schema_from_registry(crate::registry::core_v1());
        let mention = &s["properties"]["claims"]["items"]["properties"]["subject"];
        assert!(
            mention["required"]
                .as_array()
                .unwrap()
                .iter()
                .any(|v| v == "quote"),
            "mention must require quote"
        );
        assert!(
            !mention["properties"]
                .as_object()
                .unwrap()
                .contains_key("span"),
            "model-facing schema must not ask for numeric spans"
        );
    }

    #[test]
    fn few_shot_corpus_examples_validate() {
        // Every built-in few-shot example must be a fully valid extraction
        // under the current registry and the quote contract — the examples
        // teach the contract; a broken example teaches the wrong thing.
        for ex in default_few_shot_corpus() {
            let parsed: ExtractionResponse = serde_json::from_str(&ex.claims_json)
                .unwrap_or_else(|e| panic!("corpus example not parseable: {e}"));
            let result = validate_claims(&parsed.claims, &ex.text, crate::registry::core_v1());
            assert!(
                result.valid.len() == parsed.claims.len() && result.invalid.is_empty(),
                "corpus example invalid: {:#?}",
                result.invalid
            );
        }
    }

    fn make_claim(
        predicate: &str,
        subj_surface: &str,
        subj_type: &str,
        subj_span: (u32, u32),
        obj_surface: &str,
        obj_type: &str,
        obj_span: (u32, u32),
    ) -> Claim {
        Claim {
            predicate: predicate.into(),
            subject: MentionRef {
                surface: subj_surface.into(),
                entity_type: subj_type.into(),
                quote: None,
                span: subj_span,
            },
            object: ClaimObject::Entity {
                mention: MentionRef {
                    surface: obj_surface.into(),
                    entity_type: obj_type.into(),
                    quote: None,
                    span: obj_span,
                },
            },
            polarity: Polarity::Affirm,
            valid_from: None,
            valid_to: None,
            confidence: 0.9,
        }
    }

    #[test]
    fn validate_valid_claim() {
        let content = "Alice works on ProjectX at Acme Corp.";
        let claim = make_claim(
            "works_on",
            "Alice",
            "Person",
            (0, 5),
            "ProjectX",
            "Project",
            (15, 23),
        );
        let result = validate_claims(&[claim], content, crate::registry::core_v1());
        assert_eq!(result.valid.len(), 1);
        assert!(result.invalid.is_empty());
    }

    #[test]
    fn validate_part_of_accepts_project_object() {
        // Regression: part_of's object allowed only Organization before
        // registry minor 4 — project-part-of-project and artifact-part-of-
        // project knowledge was rejected as object_type_mismatch.
        let content = "The parser module belongs to ProjectX.";
        let claim = make_claim(
            "part_of",
            "parser module",
            "Artifact",
            (4, 17),
            "ProjectX",
            "Project",
            (29, 37),
        );
        let result = validate_claims(&[claim], content, crate::registry::core_v1());
        assert_eq!(result.valid.len(), 1, "errors: {:?}", result.invalid);
        assert!(result.invalid.is_empty());
    }

    #[test]
    fn validate_object_type_mismatch_lists_allowed_types() {
        let content = "Alice works on ProjectX.";
        let claim = make_claim(
            "works_on",
            "Alice",
            "Person",
            (0, 5),
            "ProjectX",
            "Place",
            (15, 23),
        );
        let result = validate_claims(&[claim], content, crate::registry::core_v1());
        assert!(result.valid.is_empty());
        assert!(result.invalid[0].1.iter().any(|e| matches!(
            e,
            ValidationError::ObjectTypeMismatch { expected, got, .. }
                if expected == "Project" && got == "Place"
        )));
    }

    #[test]
    fn validate_unknown_predicate() {
        let content = "Alice works on ProjectX.";
        let claim = make_claim(
            "unknown_pred",
            "Alice",
            "Person",
            (0, 5),
            "ProjectX",
            "Project",
            (15, 23),
        );
        let result = validate_claims(&[claim], content, crate::registry::core_v1());
        assert!(result.valid.is_empty());
        assert_eq!(result.invalid.len(), 1);
    }

    #[test]
    fn validate_fabricated_entity_rejected() {
        let content = "Alice works on ProjectX.";
        // Surface "Bob" doesn't appear at span [0, 5) — the content there is "Alice".
        let claim = make_claim(
            "works_on",
            "Bob",
            "Person",
            (0, 5),
            "ProjectX",
            "Project",
            (15, 23),
        );
        let result = validate_claims(&[claim], content, crate::registry::core_v1());
        assert!(result.valid.is_empty());
        assert_eq!(result.invalid.len(), 1);
        assert!(matches!(
            result.invalid[0].1[0],
            ValidationError::SurfaceNotVerbatim { .. }
        ));
    }

    #[test]
    fn validate_span_out_of_bounds() {
        // Entity mentions with drifted spans are repaired (relocated) as long
        // as the surface is verbatim in the content; fabricated surfaces are
        // still rejected — the fabricated-entity gate.
        let content = "Alice works on ProjectX.";
        let claim = make_claim(
            "works_on",
            "Alice",
            "Person",
            (0, 5),
            "Zanzibar",
            "Project",
            (999, 1000),
        );
        let result = validate_claims(&[claim], content, crate::registry::core_v1());
        assert!(result.valid.is_empty());
    }

    #[test]
    fn validate_subject_type_mismatch() {
        let content = "Acme Corp employs Alice (0-5).";
        let claim = make_claim(
            "employed_by",
            "Acme Corp",
            "Organization",
            (0, 9),
            "Somewhere",
            "Organization",
            (17, 26),
        );
        let result = validate_claims(&[claim], content, crate::registry::core_v1());
        assert!(result.valid.is_empty());
        assert!(matches!(
            result.invalid[0].1[0],
            ValidationError::SubjectTypeMismatch { .. }
        ));
    }

    // ─── quote-based evidence (ADR-006) ──────────────────────────────────

    fn make_claim_with_quote(
        predicate: &str,
        subj_surface: &str,
        subj_type: &str,
        subj_quote: &str,
        obj_surface: &str,
        obj_type: &str,
        obj_quote: &str,
    ) -> Claim {
        Claim {
            predicate: predicate.into(),
            subject: MentionRef {
                surface: subj_surface.into(),
                entity_type: subj_type.into(),
                quote: Some(subj_quote.into()),
                span: (0, 0), // deliberately wrong: quotes replace model spans
            },
            object: ClaimObject::Entity {
                mention: MentionRef {
                    surface: obj_surface.into(),
                    entity_type: obj_type.into(),
                    quote: Some(obj_quote.into()),
                    span: (0, 0),
                },
            },
            polarity: Polarity::Affirm,
            valid_from: None,
            valid_to: None,
            confidence: 0.9,
        }
    }

    #[test]
    fn quote_locates_and_derives_span_multilingual() {
        // Small models cannot count bytes; they can copy. The server derives
        // the span from the located quote — Korean included.
        let content = "김민수는 Acme Corp에 다니고 있다. 이서연은 brain-ui를 진행한다.";
        let claim = make_claim_with_quote(
            "employed_by",
            "김민수",
            "Person",
            "김민수는 Acme Corp에 다니고 있다",
            "Acme Corp",
            "Organization",
            "Acme Corp에 다니고 있다",
        );
        let result = validate_claims(&[claim], content, crate::registry::core_v1());
        assert_eq!(result.valid.len(), 1, "errors: {:?}", result.invalid);
        let start = content.find("김민수").unwrap() as u32;
        assert_eq!(
            result.valid[0].subject.span,
            (start, start + "김민수".len() as u32)
        );
    }

    #[test]
    fn quote_disambiguates_multiple_occurrences() {
        // "Alice" occurs twice; the quote picks the mention the model read.
        let content = "Alice met Bob. Later Alice left.";
        let claim = make_claim_with_quote(
            "knows",
            "Alice",
            "Person",
            "Alice met Bob",
            "Bob",
            "Person",
            "Alice met Bob",
        );
        let result = validate_claims(&[claim], content, crate::registry::core_v1());
        assert_eq!(result.valid.len(), 1, "errors: {:?}", result.invalid);
        assert_eq!(result.valid[0].subject.span, (0, 5)); // first Alice
    }

    #[test]
    fn quote_not_found_rejects() {
        // A fabricated quote (not copied from the episode) is the new
        // fabricated-entity gate: nothing to locate.
        let content = "Alice works on ProjectX.";
        let claim = make_claim_with_quote(
            "works_on",
            "Alice",
            "Person",
            "Alice works on SecreTProJect", // not verbatim in content
            "ProjectX",
            "Project",
            "works on ProjectX",
        );
        let result = validate_claims(&[claim], content, crate::registry::core_v1());
        assert!(result.valid.is_empty(), "fabricated quote must be rejected");
        assert!(matches!(
            result.invalid[0].1[0],
            ValidationError::SurfaceNotVerbatim { .. }
        ));
    }

    #[test]
    fn quote_without_surface_rejects() {
        // The quote is real, but the surface does not occur inside it —
        // the evidence does not support the mention.
        let content = "Alice works on ProjectX. Bob knows Carol.";
        let claim = make_claim_with_quote(
            "works_on",
            "Bob",
            "Person",
            "Alice works on ProjectX", // quote exists, Bob is not in it
            "ProjectX",
            "Project",
            "Alice works on ProjectX",
        );
        let result = validate_claims(&[claim], content, crate::registry::core_v1());
        assert!(result.valid.is_empty(), "surface absent from quote");
        assert!(matches!(
            result.invalid[0].1[0],
            ValidationError::SurfaceNotVerbatim { .. }
        ));
    }

    #[test]
    fn quote_casing_canonicalizes_surface() {
        let content = "Alice works on ProjectX at Acme Corp.";
        let claim = make_claim_with_quote(
            "employed_by",
            "alice",
            "Person",
            "Alice works on ProjectX at Acme Corp",
            "acme corp",
            "Organization",
            "at Acme Corp",
        );
        let result = validate_claims(&[claim], content, crate::registry::core_v1());
        assert_eq!(result.valid.len(), 1, "errors: {:?}", result.invalid);
        assert_eq!(result.valid[0].subject.surface, "Alice");
    }

    #[test]
    fn quote_casing_fallback_survives_multibyte_window() {
        // Regression: the ASCII-case fallback slices `candidate[..needle.len()]`;
        let content = "김민수는 한국 지사 Acme Corp에서 일한다. 본사는 미국에 있다.";
        // panic BEFORE the loop ever reaches the real (ASCII) occurrence.
        // The fallback must skip non-boundary offsets, not crash.
        let claim = make_claim_with_quote(
            "employed_by",
            "김민수",
            "Person",
            "김민수는 한국 지사 Acme Corp에서 일한다",
            "ACME",
            "Organization",
            "한국 지사 Acme Corp에서",
        );
        let result = validate_claims(&[claim], content, crate::registry::core_v1());
        assert_eq!(result.valid.len(), 1, "errors: {:?}", result.invalid);
        // The object surface canonicalizes to the source casing.
        assert_eq!(
            result.valid[0].subject.surface, "김민수",
            "exact-match subject must pass untouched"
        );
        if let ClaimObject::Entity { mention } = &result.valid[0].object {
            assert_eq!(
                mention.surface, "Acme",
                "casing must canonicalize to source"
            );
            let start = content.find("Acme").unwrap() as u32;
            assert_eq!(mention.span, (start, start + "Acme".len() as u32));
        } else {
            panic!("expected entity object");
        }
    }

    #[test]
    fn literal_quote_derives_span() {
        // Literal objects carry quotes too; the value must occur inside the
        // located quote (stronger than the old bounds-only check).
        let content = "Alice's full name is Alice Smith.";
        let claim = Claim {
            predicate: "full_name".into(),
            subject: MentionRef {
                surface: "Alice".into(),
                entity_type: "Person".into(),
                quote: Some("full name is Alice Smith".into()),
                span: (0, 0),
            },
            object: ClaimObject::Literal {
                literal_type: "text".into(),
                value: "Alice Smith".into(),
                quote: Some("full name is Alice Smith".into()),
                span: (0, 0),
            },
            polarity: Polarity::Affirm,
            valid_from: None,
            valid_to: None,
            confidence: 0.9,
        };
        let result = validate_claims(&[claim], content, crate::registry::core_v1());
        assert_eq!(result.valid.len(), 1, "errors: {:?}", result.invalid);
        let start = content.find("Alice Smith").unwrap() as u32;
        if let ClaimObject::Literal { span, .. } = &result.valid[0].object {
            assert_eq!(*span, (start, start + "Alice Smith".len() as u32));
        } else {
            panic!("expected literal object");
        }
    }

    // ─── grammar_from_registry tests ──────────────────────────────────────

    #[test]
    fn grammar_smoke_has_rules() {
        let g = grammar_from_registry(crate::registry::core_v1());
        // Normalize whitespace so alignment in the template doesn't break matching.
        let norm: String = g.split_whitespace().collect::<Vec<_>>().join(" ");
        for rule in [
            "root",
            "claims",
            "claim",
            "mention",
            "object-union",
            "predicate",
            "entity-type",
            "polarity",
            "literal-type",
            "string",
            "number",
            "integer",
            "ws",
        ] {
            let needle = format!("{rule} ::=");
            assert!(
                norm.contains(&needle),
                "grammar missing rule definition for `{rule}`"
            );
        }
    }

    #[test]
    fn grammar_forbids_empty_quotes() {
        // Measured on qwen2.5-1.5b: the model lazily emits "quote":"" for
        // subjects. An empty quote is no evidence — the grammar must force
        // at least one character, so the model has to copy something.
        let g = grammar_from_registry(crate::registry::core_v1());
        let norm: String = g.split_whitespace().collect::<Vec<_>>().join(" ");
        assert!(
            norm.contains("nonempty-string ::="),
            "a nonempty-string rule must exist"
        );
        assert!(
            norm.contains("\"\\\"quote\\\"\" ws \":\" ws nonempty-string"),
            "quote fields must use nonempty-string"
        );
    }

    #[test]
    fn grammar_and_schema_agree_on_predicates() {
        let preds = crate::registry::core_v1();

        let grammar = grammar_from_registry(preds);
        let schema = schema_from_registry(preds);

        let schema_preds: std::collections::BTreeSet<String> =
            schema["properties"]["claims"]["items"]["properties"]["predicate"]["enum"]
                .as_array()
                .unwrap()
                .iter()
                .map(|v| v.as_str().unwrap().to_string())
                .collect();

        for name in &schema_preds {
            // The grammar must contain a GBNF literal for this predicate name.
            let needle = format!("\\\"{name}\\\"");
            assert!(
                grammar.contains(&needle),
                "grammar missing predicate `{name}` present in schema"
            );
        }
    }

    #[test]
    fn grammar_and_schema_agree_on_entity_types() {
        let preds = crate::registry::core_v1();
        let grammar = grammar_from_registry(preds);
        let schema = schema_from_registry(preds);

        let schema_types: std::collections::BTreeSet<String> = schema["properties"]["claims"]["items"]
            ["properties"]["subject"]["properties"]["entity_type"]["enum"]
            .as_array()
            .unwrap()
            .iter()
            .map(|v| v.as_str().unwrap().to_string())
            .collect();

        for name in &schema_types {
            let needle = format!("\\\"{name}\\\"");
            assert!(
                grammar.contains(&needle),
                "grammar missing entity type `{name}` present in schema"
            );
        }
    }

    #[test]
    fn grammar_has_polarity_and_literal_type_enums() {
        let g = grammar_from_registry(crate::registry::core_v1());
        assert!(g.contains("\\\"affirm\\\""));
        assert!(g.contains("\\\"deny\\\""));
        for lt in ["text", "date", "datetime", "number", "bool", "quantity"] {
            assert!(
                g.contains(&format!("\\\"{lt}\\\"")),
                "grammar missing literal type `{lt}`"
            );
        }
    }

    #[test]
    fn grammar_valid_response_roundtrips_serde() {
        // A Claim serialized to JSON should round-trip through serde.
        // This is the structural half of the grammar/schema agreement: the
        // grammar generates the same structure that serde expects.
        let claim = make_claim(
            "works_on",
            "Alice",
            "Person",
            (0, 5),
            "ProjectX",
            "Project",
            (15, 23),
        );
        let resp = ExtractionResponse {
            claims: vec![claim],
        };
        let json = serde_json::to_string(&resp).unwrap();
        let back: ExtractionResponse = serde_json::from_str(&json).unwrap();
        assert_eq!(back.claims.len(), 1);
        assert_eq!(back.claims[0].predicate, "works_on");
    }

    #[test]
    fn grammar_has_optional_temporal_fields() {
        let g = grammar_from_registry(crate::registry::core_v1());
        assert!(g.contains("valid-from-opt"));
        assert!(g.contains("valid-to-opt"));
        // GBNF literals use backslash-escaped quotes.
        assert!(g.contains("\\\"valid_from\\\""));
    }
    #[test]
    fn grammar_supports_empty_claims() {
        // An empty claims array {"claims":[]} must be accepted.
        let g = grammar_from_registry(crate::registry::core_v1());
        // The claims rule uses ? to allow zero claims.
        assert!(g.contains("(claim (ws \",\" ws claim)*)?"));
    }

    // ── Few-shot selection (§9.6, 10.8) ────────────────────────────────

    #[test]
    fn few_shot_selects_most_similar() {
        let corpus = vec![
            FewShotExample {
                text: "Alice works at Acme.".into(),
                claims_json: r#"{"claims":[]}"#.into(),
            },
            FewShotExample {
                text: "Bob likes pizza.".into(),
                claims_json: r#"{"claims":[]}"#.into(),
            },
        ];
        let target = "Alice works at Globex.";
        let selected = few_shot_examples(target, &corpus, 1);
        assert_eq!(selected.len(), 1);
        assert!(
            selected[0].text.contains("Alice"),
            "should pick the most similar example, got: {}",
            selected[0].text
        );
    }

    #[test]
    fn few_shot_empty_corpus_returns_empty() {
        let corpus: Vec<FewShotExample> = vec![];
        let selected = few_shot_examples("any text", &corpus, 3);
        assert!(selected.is_empty());
    }

    #[test]
    fn few_shot_k_caps_results() {
        let corpus: Vec<FewShotExample> = (0..10)
            .map(|i| FewShotExample {
                text: format!("Sample text {i}."),
                claims_json: r#"{"claims":[]}"#.into(),
            })
            .collect();
        let selected = few_shot_examples("Sample text", &corpus, 3);
        assert_eq!(selected.len(), 3);
    }

    #[test]
    fn few_shot_format_includes_input_output() {
        let ex = FewShotExample {
            text: "Alice works at Acme.".into(),
            claims_json: r#"{"claims":[]}"#.into(),
        };
        let formatted = format_few_shot(&[&ex]);
        assert!(formatted.contains("Alice works at Acme"));
        assert!(formatted.contains(r#"{"claims":[]}"#));
    }

    #[test]
    fn few_shot_format_empty_returns_empty_string() {
        let formatted = format_few_shot(&[]);
        assert_eq!(formatted, "");
    }
}