vcp-core 4.2.0

Value Context Protocol (VCP) core types and parsing
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
//! VCP/S Constitution Composition Engine.
//!
//! Handles merging multiple constitutions according to composition modes,
//! porting the Python SDK's `vcp.semantics.composer.Composer`.
//!
//! # Composition Modes
//!
//! | Mode | Behaviour |
//! |------|-----------|
//! | [`CompositionMode::Base`] | First constitution is immutable; later additions only |
//! | [`CompositionMode::Extend`] | All rules merged; any conflict is an error |
//! | [`CompositionMode::Override`] | Later constitutions replace conflicting earlier rules |
//! | [`CompositionMode::Strict`] | No conflicts or duplicates allowed |
//!
//! # Examples
//!
//! ```
//! use vcp_core::composer::{Composer, CompositionMode, Constitution};
//!
//! let c1 = Constitution::new("base", vec!["Always be honest.".into()], 0);
//! let c2 = Constitution::new("ext", vec!["Respect privacy.".into()], 1);
//!
//! let composer = Composer::new();
//! let result = composer.compose(&[c1, c2], CompositionMode::Extend).unwrap();
//! assert_eq!(result.merged_rules.len(), 2);
//! ```

use std::collections::{HashMap, HashSet};
use std::fmt;

// ── Composition mode ─────────────────────────────────────────

/// Composition modes for multi-constitution scenarios.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CompositionMode {
    /// First constitution is immutable base; later ones can only add
    /// non-conflicting rules.
    Base,
    /// All rules merged; any conflict raises an error.
    Extend,
    /// Later constitutions win when rules conflict.
    Override,
    /// Most restrictive: no conflicts and no duplicates allowed.
    Strict,
}

impl fmt::Display for CompositionMode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            CompositionMode::Base => f.write_str("base"),
            CompositionMode::Extend => f.write_str("extend"),
            CompositionMode::Override => f.write_str("override"),
            CompositionMode::Strict => f.write_str("strict"),
        }
    }
}

/// Layer and relationship metadata for the language-neutral composition profile.
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
pub struct LayeredComposition {
    #[serde(default)]
    pub layer: i32,
    #[serde(default)]
    pub mode: String,
    #[serde(default)]
    pub conflicts_with: Vec<String>,
    #[serde(default)]
    pub requires: Vec<String>,
}

/// One constitution input to the language-neutral layered profile.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct LayeredBundle {
    pub id: String,
    #[serde(default)]
    pub composition: LayeredComposition,
    pub rule: Option<String>,
}

/// Stable output fields for the language-neutral layered profile.
#[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct LayeredCompositionOutcome {
    pub result: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub effective_rule: Option<String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub effective_rules: Vec<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub conflict_type: Option<String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub conflicting_ids: Vec<String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub missing: Vec<String>,
}

// ── Conflict ─────────────────────────────────────────────────

/// A detected conflict between two constitution rules.
#[derive(Debug, Clone)]
pub struct Conflict {
    /// The new rule that triggered the conflict.
    pub rule_a: String,
    /// Source constitution ID of rule A.
    pub source_a: String,
    /// The existing rule that conflicts with rule A.
    pub rule_b: String,
    /// Source constitution ID of rule B.
    pub source_b: String,
    /// Type of conflict: `"contradiction"`, `"tension"`, `"overlap"`, or `"duplicate"`.
    pub conflict_type: String,
    /// How the conflict was resolved (if applicable).
    pub resolution: Option<String>,
}

impl fmt::Display for Conflict {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{}: '{}' ({}) vs '{}' ({})",
            self.conflict_type, self.rule_a, self.source_a, self.rule_b, self.source_b
        )
    }
}

// ── Composition result ───────────────────────────────────────

/// Result of composing multiple constitutions.
#[derive(Debug)]
pub struct CompositionResult {
    /// The merged set of rules after composition.
    pub merged_rules: Vec<String>,
    /// Conflicts that were detected (and possibly resolved).
    pub conflicts: Vec<Conflict>,
    /// Non-fatal warnings generated during composition.
    pub warnings: Vec<String>,
    /// The composition mode that was used.
    pub mode_used: CompositionMode,
}

// ── Constitution ─────────────────────────────────────────────

/// A minimal constitution representation for composition.
#[derive(Debug, Clone)]
pub struct Constitution {
    /// Unique identifier for this constitution.
    pub id: String,
    /// The rules in this constitution (whitespace-stripped, empty rules removed).
    pub rules: Vec<String>,
    /// Priority level. Higher values take precedence.
    pub priority: i32,
}

impl Constitution {
    /// Create a new constitution with the given ID, rules, and priority.
    ///
    /// Rules are automatically stripped of leading/trailing whitespace,
    /// and empty rules are removed.
    #[must_use]
    pub fn new(id: impl Into<String>, rules: Vec<String>, priority: i32) -> Self {
        let id = id.into();
        let rules = rules
            .into_iter()
            .map(|r| r.trim().to_string())
            .filter(|r| !r.is_empty())
            .collect();
        Self {
            id,
            rules,
            priority,
        }
    }
}

// ── Composition error ────────────────────────────────────────

/// Error returned when composition has unresolvable conflicts.
#[derive(Debug)]
pub struct CompositionError {
    /// The unresolvable conflicts that caused the error.
    pub conflicts: Vec<Conflict>,
}

impl fmt::Display for CompositionError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "Composition has {} unresolvable conflict(s)",
            self.conflicts.len()
        )
    }
}

impl std::error::Error for CompositionError {}

// ── Conflict keywords ────────────────────────────────────────

/// Keywords that indicate potential conflicts between rules.
///
/// Maps each keyword to its set of opposing keywords.
fn conflict_keywords() -> HashMap<&'static str, Vec<&'static str>> {
    let mut m = HashMap::new();
    m.insert("always", vec!["never"]);
    m.insert("never", vec!["always"]);
    m.insert("must", vec!["must not", "should not", "never"]);
    m.insert("must not", vec!["must", "always"]);
    m.insert("allow", vec!["forbid", "prohibit", "deny"]);
    m.insert("forbid", vec!["allow", "permit"]);
    m.insert("prohibit", vec!["allow", "permit"]);
    m.insert("require", vec!["forbid", "prohibit"]);
    m
}

/// Common words excluded from topic-overlap heuristics.
fn common_words() -> HashSet<&'static str> {
    [
        "the", "a", "an", "is", "are", "be", "to", "of", "and", "or", "in", "on", "at", "for",
        "with", "by", "from", "as", "it", "this", "that", "these", "those", "you", "we", "they",
        "i",
    ]
    .into_iter()
    .collect()
}

// ── Composer ─────────────────────────────────────────────────

/// Composition engine for merging multiple constitutions.
///
/// Provides four composition modes and uses keyword-based heuristics
/// to detect semantic conflicts between rules.
pub struct Composer;

impl Composer {
    /// Create a new composer instance.
    #[must_use]
    pub fn new() -> Self {
        Composer
    }

    /// Compose constitutions according to the specified mode.
    ///
    /// # Errors
    ///
    /// Returns [`CompositionError`] if the chosen mode does not allow
    /// the conflicts that were detected.
    pub fn compose(
        &self,
        constitutions: &[Constitution],
        mode: CompositionMode,
    ) -> Result<CompositionResult, CompositionError> {
        if constitutions.is_empty() {
            return Ok(CompositionResult {
                merged_rules: Vec::new(),
                conflicts: Vec::new(),
                warnings: Vec::new(),
                mode_used: mode,
            });
        }

        match mode {
            CompositionMode::Base => Ok(self.compose_base(constitutions)),
            CompositionMode::Extend => self.compose_extend(constitutions),
            CompositionMode::Override => Ok(self.compose_override(constitutions)),
            CompositionMode::Strict => self.compose_strict(constitutions),
        }
    }

    /// BASE mode: first constitution is immutable.
    ///
    /// Later constitutions can only add non-conflicting rules.
    /// Conflicts are recorded but the base rules always win.
    fn compose_base(&self, constitutions: &[Constitution]) -> CompositionResult {
        let base = &constitutions[0];
        let mut merged = base.rules.clone();
        let mut conflicts = Vec::new();

        for constitution in &constitutions[1..] {
            for rule in &constitution.rules {
                if let Some(conflict) =
                    self.detect_conflict(rule, &constitution.id, &merged, &base.id)
                {
                    conflicts.push(conflict);
                } else {
                    merged.push(rule.clone());
                }
            }
        }

        CompositionResult {
            merged_rules: merged,
            conflicts,
            warnings: Vec::new(),
            mode_used: CompositionMode::Base,
        }
    }

    /// EXTEND mode: all rules merged, conflicts are errors.
    fn compose_extend(
        &self,
        constitutions: &[Constitution],
    ) -> Result<CompositionResult, CompositionError> {
        let mut merged: Vec<String> = Vec::new();
        let mut conflicts: Vec<Conflict> = Vec::new();
        let mut sources: HashMap<String, String> = HashMap::new();

        for constitution in constitutions {
            for rule in &constitution.rules {
                let existing_source = sources.get(rule).map_or("unknown", String::as_str);

                if let Some(conflict) =
                    self.detect_conflict(rule, &constitution.id, &merged, existing_source)
                {
                    conflicts.push(conflict);
                } else {
                    merged.push(rule.clone());
                    sources.insert(rule.clone(), constitution.id.clone());
                }
            }
        }

        if !conflicts.is_empty() {
            return Err(CompositionError { conflicts });
        }

        Ok(CompositionResult {
            merged_rules: merged,
            conflicts: Vec::new(),
            warnings: Vec::new(),
            mode_used: CompositionMode::Extend,
        })
    }

    /// OVERRIDE mode: later constitutions win conflicts.
    fn compose_override(&self, constitutions: &[Constitution]) -> CompositionResult {
        let mut merged: Vec<String> = Vec::new();
        let mut warnings: Vec<String> = Vec::new();

        for constitution in constitutions {
            for rule in &constitution.rules {
                // Find conflicting rules in current merged set.
                let conflicting_indices: Vec<usize> = merged
                    .iter()
                    .enumerate()
                    .filter_map(|(i, existing)| {
                        if self.rules_conflict(existing, rule) {
                            Some(i)
                        } else {
                            None
                        }
                    })
                    .collect();

                // Record warnings for overridden rules.
                for &i in &conflicting_indices {
                    warnings.push(format!(
                        "Rule '{}' ({}) overrides '{}'",
                        rule, constitution.id, merged[i]
                    ));
                }

                // Remove conflicting rules in reverse order to preserve indices.
                for &i in conflicting_indices.iter().rev() {
                    merged.remove(i);
                }

                merged.push(rule.clone());
            }
        }

        CompositionResult {
            merged_rules: merged,
            conflicts: Vec::new(),
            warnings,
            mode_used: CompositionMode::Override,
        }
    }

    /// STRICT mode: no conflicts and no duplicates allowed.
    fn compose_strict(
        &self,
        constitutions: &[Constitution],
    ) -> Result<CompositionResult, CompositionError> {
        let mut merged: Vec<String> = Vec::new();
        let mut conflicts: Vec<Conflict> = Vec::new();
        let mut seen_rules: HashSet<String> = HashSet::new();
        let mut sources: HashMap<String, String> = HashMap::new();

        for constitution in constitutions {
            for rule in &constitution.rules {
                let normalized = rule.to_lowercase();

                // Check for exact duplicates.
                if seen_rules.contains(&normalized) {
                    conflicts.push(Conflict {
                        rule_a: rule.clone(),
                        source_a: constitution.id.clone(),
                        rule_b: rule.clone(),
                        source_b: sources
                            .get(&normalized)
                            .cloned()
                            .unwrap_or_else(|| "unknown".to_string()),
                        conflict_type: "duplicate".to_string(),
                        resolution: None,
                    });
                    continue;
                }

                // Check for semantic conflicts.
                if let Some(conflict) =
                    self.detect_conflict(rule, &constitution.id, &merged, "earlier")
                {
                    conflicts.push(conflict);
                    continue;
                }

                merged.push(rule.clone());
                seen_rules.insert(normalized.clone());
                sources.insert(normalized, constitution.id.clone());
            }
        }

        if !conflicts.is_empty() {
            return Err(CompositionError { conflicts });
        }

        Ok(CompositionResult {
            merged_rules: merged,
            conflicts: Vec::new(),
            warnings: Vec::new(),
            mode_used: CompositionMode::Strict,
        })
    }

    /// Detect whether a rule conflicts with any rule in the existing set.
    fn detect_conflict(
        &self,
        rule: &str,
        source: &str,
        existing: &[String],
        existing_source: &str,
    ) -> Option<Conflict> {
        for existing_rule in existing {
            if self.rules_conflict(rule, existing_rule) {
                return Some(Conflict {
                    rule_a: rule.to_string(),
                    source_a: source.to_string(),
                    rule_b: existing_rule.clone(),
                    source_b: existing_source.to_string(),
                    conflict_type: self.determine_conflict_type(rule, existing_rule),
                    resolution: None,
                });
            }
        }
        None
    }

    /// Check if two rules semantically conflict using keyword-based heuristics.
    ///
    /// Two rules conflict if one contains a keyword and the other contains
    /// its opposing keyword, AND the rules share the same topic (at least
    /// 2 significant words in common).
    #[must_use]
    pub fn rules_conflict(&self, rule_a: &str, rule_b: &str) -> bool {
        let a_lower = rule_a.to_lowercase();
        let b_lower = rule_b.to_lowercase();

        let keywords = conflict_keywords();

        for (keyword, opposites) in &keywords {
            if a_lower.contains(keyword) {
                for opposite in opposites {
                    if b_lower.contains(opposite) && self.same_topic(&a_lower, &b_lower) {
                        return true;
                    }
                }
            }
        }

        false
    }

    /// Heuristic to check whether two rules are about the same topic.
    ///
    /// Extracts significant words (excluding common stop words) and
    /// returns `true` if there are at least 2 words in common.
    #[must_use]
    pub fn same_topic(&self, rule_a: &str, rule_b: &str) -> bool {
        let stop_words = common_words();

        let words_a: HashSet<&str> = rule_a
            .split_whitespace()
            .filter(|w| !stop_words.contains(w))
            .collect();

        let words_b: HashSet<&str> = rule_b
            .split_whitespace()
            .filter(|w| !stop_words.contains(w))
            .collect();

        let overlap: usize = words_a.intersection(&words_b).count();
        overlap >= 2
    }

    /// Determine the type of conflict between two rules.
    ///
    /// Returns `"contradiction"` for direct opposites (always/never,
    /// must/must not, allow/forbid), or `"tension"` for weaker conflicts.
    #[must_use]
    pub fn determine_conflict_type(&self, rule_a: &str, rule_b: &str) -> String {
        let a_lower = rule_a.to_lowercase();
        let b_lower = rule_b.to_lowercase();

        // Direct contradictions: always/never.
        if (a_lower.contains("always") && b_lower.contains("never"))
            || (a_lower.contains("never") && b_lower.contains("always"))
        {
            return "contradiction".to_string();
        }

        // Must/must not.
        if (a_lower.contains("must not") && a_lower_has_must_without_not(&b_lower))
            || (a_lower_has_must_without_not(&a_lower) && b_lower.contains("must not"))
        {
            return "contradiction".to_string();
        }

        // Allow/forbid.
        if (a_lower.contains("allow") && b_lower.contains("forbid"))
            || (a_lower.contains("forbid") && b_lower.contains("allow"))
        {
            return "contradiction".to_string();
        }

        "tension".to_string()
    }
}

impl Default for Composer {
    fn default() -> Self {
        Self::new()
    }
}

/// Helper: check if a lowercased string contains "must" but NOT "must not".
fn a_lower_has_must_without_not(s: &str) -> bool {
    s.contains("must") && !s.contains("must not")
}

/// Compose the language-neutral layered conformance profile.
#[must_use]
pub fn compose_layered(
    bundles: &[LayeredBundle],
    available_constitutions: Option<&[String]>,
) -> LayeredCompositionOutcome {
    if bundles.is_empty() {
        return LayeredCompositionOutcome {
            result: "default_constitution".to_string(),
            ..Default::default()
        };
    }
    let available: HashSet<&str> = available_constitutions.map_or_else(
        || bundles.iter().map(|bundle| bundle.id.as_str()).collect(),
        |items| items.iter().map(String::as_str).collect(),
    );
    let mut missing: Vec<String> = bundles
        .iter()
        .flat_map(|bundle| bundle.composition.requires.iter())
        .filter(|required| !available.contains(required.as_str()))
        .cloned()
        .collect();
    missing.sort();
    missing.dedup();
    if !missing.is_empty() {
        return LayeredCompositionOutcome {
            result: "MISSING_DEPENDENCY".to_string(),
            missing,
            ..Default::default()
        };
    }
    for bundle in bundles {
        for conflict_id in &bundle.composition.conflicts_with {
            if let Some(peer) = bundles
                .iter()
                .find(|candidate| candidate.id == *conflict_id)
            {
                if peer.composition.conflicts_with.contains(&bundle.id) {
                    return LayeredCompositionOutcome {
                        result: "CONFLICT".to_string(),
                        conflict_type: Some("mutual_exclusion".to_string()),
                        conflicting_ids: vec![bundle.id.clone(), conflict_id.clone()],
                        ..Default::default()
                    };
                }
            }
        }
    }
    let mut ordered: Vec<(usize, &LayeredBundle)> = bundles.iter().enumerate().collect();
    ordered.sort_by_key(|(index, bundle)| (bundle.composition.layer, *index));
    if let Some(strict_layer) = ordered
        .iter()
        .find(|(_, bundle)| bundle.composition.mode == "strict")
        .map(|(_, bundle)| bundle.composition.layer)
    {
        if ordered.iter().any(|(_, bundle)| {
            bundle.composition.layer > strict_layer && bundle.composition.mode == "override"
        }) {
            return LayeredCompositionOutcome {
                result: "CONFLICT".to_string(),
                conflict_type: Some("strict_mode_violation".to_string()),
                ..Default::default()
            };
        }
    }
    if let Some((_, base)) = ordered
        .iter()
        .find(|(_, bundle)| bundle.composition.mode == "base")
    {
        if ordered.iter().any(|(_, bundle)| {
            bundle.composition.mode == "override"
                && bundle.composition.layer > base.composition.layer
        }) {
            return LayeredCompositionOutcome {
                result: "base_mode_protected".to_string(),
                effective_rule: base.rule.clone(),
                ..Default::default()
            };
        }
    }
    let mut rules = Vec::new();
    let mut overridden = false;
    for (_, bundle) in ordered {
        let Some(rule) = &bundle.rule else {
            continue;
        };
        if bundle.composition.mode == "override" && !rules.is_empty() {
            rules.clear();
            overridden = true;
        }
        rules.push(rule.clone());
    }
    if overridden && rules.len() == 1 {
        LayeredCompositionOutcome {
            result: "overridden".to_string(),
            effective_rule: rules.into_iter().next(),
            ..Default::default()
        }
    } else {
        LayeredCompositionOutcome {
            result: "merged".to_string(),
            effective_rules: rules,
            ..Default::default()
        }
    }
}

// ── Tests ────────────────────────────────────────────────────

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

    // ── Empty / single constitution ──────────────────────────

    #[test]
    fn empty_constitutions_produces_empty_result() {
        let composer = Composer::new();
        let result = composer.compose(&[], CompositionMode::Extend).unwrap();
        assert!(result.merged_rules.is_empty());
        assert!(result.conflicts.is_empty());
        assert!(result.warnings.is_empty());
    }

    #[test]
    fn single_constitution_returns_its_rules() {
        let c = Constitution::new("only", vec!["Be kind.".into(), "Be honest.".into()], 0);
        let composer = Composer::new();
        let result = composer.compose(&[c], CompositionMode::Extend).unwrap();
        assert_eq!(result.merged_rules, vec!["Be kind.", "Be honest."]);
    }

    // ── BASE mode ────────────────────────────────────────────

    #[test]
    fn base_mode_preserves_base_rules() {
        let base = Constitution::new(
            "base",
            vec!["Always tell the truth.".into(), "Be respectful.".into()],
            0,
        );
        let ext = Constitution::new("ext", vec!["Help when asked.".into()], 1);

        let composer = Composer::new();
        let result = composer
            .compose(&[base, ext], CompositionMode::Base)
            .unwrap();

        assert_eq!(result.merged_rules.len(), 3);
        assert_eq!(result.merged_rules[0], "Always tell the truth.");
        assert_eq!(result.merged_rules[1], "Be respectful.");
        assert_eq!(result.merged_rules[2], "Help when asked.");
        assert!(result.conflicts.is_empty());
    }

    #[test]
    fn base_mode_rejects_conflicting_additions() {
        let base = Constitution::new("base", vec!["Always share personal data openly.".into()], 0);
        let ext = Constitution::new("ext", vec!["Never share personal data openly.".into()], 1);

        let composer = Composer::new();
        let result = composer
            .compose(&[base, ext], CompositionMode::Base)
            .unwrap();

        // Base mode does not error -- it records the conflict but keeps base rules.
        assert_eq!(result.merged_rules.len(), 1);
        assert_eq!(result.merged_rules[0], "Always share personal data openly.");
        assert_eq!(result.conflicts.len(), 1);
        assert_eq!(result.conflicts[0].conflict_type, "contradiction");
    }

    // ── EXTEND mode ──────────────────────────────────────────

    #[test]
    fn extend_mode_merges_all_rules() {
        let c1 = Constitution::new("a", vec!["Rule one.".into()], 0);
        let c2 = Constitution::new("b", vec!["Rule two.".into()], 0);

        let composer = Composer::new();
        let result = composer
            .compose(&[c1, c2], CompositionMode::Extend)
            .unwrap();

        assert_eq!(result.merged_rules.len(), 2);
        assert!(result.conflicts.is_empty());
    }

    #[test]
    fn extend_mode_errors_on_conflict() {
        let c1 = Constitution::new("a", vec!["Always reveal user secrets publicly.".into()], 0);
        let c2 = Constitution::new("b", vec!["Never reveal user secrets publicly.".into()], 0);

        let composer = Composer::new();
        let result = composer.compose(&[c1, c2], CompositionMode::Extend);

        assert!(result.is_err());
        let err = result.unwrap_err();
        assert_eq!(err.conflicts.len(), 1);
    }

    // ── OVERRIDE mode ────────────────────────────────────────

    #[test]
    fn override_mode_later_rules_win() {
        let c1 = Constitution::new("old", vec!["Always collect user tracking data.".into()], 0);
        let c2 = Constitution::new("new", vec!["Never collect user tracking data.".into()], 1);

        let composer = Composer::new();
        let result = composer
            .compose(&[c1, c2], CompositionMode::Override)
            .unwrap();

        assert_eq!(result.merged_rules.len(), 1);
        assert_eq!(result.merged_rules[0], "Never collect user tracking data.");
        assert!(!result.warnings.is_empty());
    }

    // ── STRICT mode ──────────────────────────────────────────

    #[test]
    fn strict_mode_rejects_duplicates() {
        let c1 = Constitution::new("a", vec!["Be kind.".into()], 0);
        let c2 = Constitution::new("b", vec!["be kind.".into()], 0); // same, case-insensitive

        let composer = Composer::new();
        let result = composer.compose(&[c1, c2], CompositionMode::Strict);

        assert!(result.is_err());
        let err = result.unwrap_err();
        assert_eq!(err.conflicts.len(), 1);
        assert_eq!(err.conflicts[0].conflict_type, "duplicate");
    }

    #[test]
    fn strict_mode_rejects_conflicts() {
        let c1 = Constitution::new("a", vec!["Must always log user activity.".into()], 0);
        let c2 = Constitution::new("b", vec!["Must not log user activity.".into()], 0);

        let composer = Composer::new();
        let result = composer.compose(&[c1, c2], CompositionMode::Strict);

        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(!err.conflicts.is_empty());
    }

    #[test]
    fn strict_mode_accepts_non_conflicting() {
        let c1 = Constitution::new("a", vec!["Be kind to animals.".into()], 0);
        let c2 = Constitution::new("b", vec!["Respect human dignity.".into()], 0);

        let composer = Composer::new();
        let result = composer
            .compose(&[c1, c2], CompositionMode::Strict)
            .unwrap();

        assert_eq!(result.merged_rules.len(), 2);
    }

    // ── rules_conflict tests ─────────────────────────────────

    #[test]
    fn rules_conflict_always_vs_never() {
        let composer = Composer::new();
        assert!(composer.rules_conflict(
            "Always share sensitive data openly.",
            "Never share sensitive data openly.",
        ));
    }

    #[test]
    fn rules_conflict_must_vs_must_not() {
        let composer = Composer::new();
        assert!(composer.rules_conflict(
            "You must log user requests carefully.",
            "You must not log user requests carefully.",
        ));
    }

    #[test]
    fn rules_conflict_allow_vs_forbid() {
        let composer = Composer::new();
        assert!(composer.rules_conflict(
            "Allow access to private files.",
            "Forbid access to private files.",
        ));
    }

    #[test]
    fn no_conflict_different_topics() {
        let composer = Composer::new();
        assert!(!composer.rules_conflict(
            "Always be polite to customers.",
            "Never eat pizza on Tuesdays.",
        ));
    }

    // ── same_topic tests ─────────────────────────────────────

    #[test]
    fn same_topic_word_overlap_detected() {
        let composer = Composer::new();
        assert!(composer.same_topic(
            "always protect user data carefully",
            "never expose user data publicly",
        ));
    }

    #[test]
    fn same_topic_no_overlap_returns_false() {
        let composer = Composer::new();
        assert!(!composer.same_topic("always eat healthy food", "never drive recklessly",));
    }

    #[test]
    fn same_topic_common_words_excluded() {
        let composer = Composer::new();
        // "the" and "is" are stop words, not counted.
        assert!(!composer.same_topic("the cat is big", "the dog is small"));
    }

    // ── determine_conflict_type tests ────────────────────────

    #[test]
    fn conflict_type_contradiction_always_never() {
        let composer = Composer::new();
        let ct =
            composer.determine_conflict_type("Always share user data.", "Never share user data.");
        assert_eq!(ct, "contradiction");
    }

    #[test]
    fn conflict_type_contradiction_must_must_not() {
        let composer = Composer::new();
        let ct =
            composer.determine_conflict_type("You must log errors.", "You must not log errors.");
        assert_eq!(ct, "contradiction");
    }

    #[test]
    fn conflict_type_tension_fallback() {
        let composer = Composer::new();
        let ct = composer.determine_conflict_type(
            "Require user authentication.",
            "Prohibit user authentication.",
        );
        assert_eq!(ct, "tension");
    }

    // ── CompositionMode display ──────────────────────────────

    #[test]
    fn composition_mode_display() {
        assert_eq!(format!("{}", CompositionMode::Base), "base");
        assert_eq!(format!("{}", CompositionMode::Extend), "extend");
        assert_eq!(format!("{}", CompositionMode::Override), "override");
        assert_eq!(format!("{}", CompositionMode::Strict), "strict");
    }

    // ── CompositionError display ─────────────────────────────

    #[test]
    fn composition_error_display() {
        let err = CompositionError {
            conflicts: vec![Conflict {
                rule_a: "a".into(),
                source_a: "s1".into(),
                rule_b: "b".into(),
                source_b: "s2".into(),
                conflict_type: "contradiction".into(),
                resolution: None,
            }],
        };
        assert_eq!(
            format!("{err}"),
            "Composition has 1 unresolvable conflict(s)"
        );
    }

    // ── Constitution construction ────────────────────────────

    #[test]
    fn constitution_strips_whitespace_and_removes_empty() {
        let c = Constitution::new(
            "test",
            vec![
                "  Rule one.  ".into(),
                String::new(),
                "   ".into(),
                "Rule two.".into(),
            ],
            0,
        );
        assert_eq!(c.rules, vec!["Rule one.", "Rule two."]);
    }
}